initial commit
[goodguy/history.git] / cinelerra-5.0 / guicast / bcrecentlist.C
1
2 /*
3  * CINELERRA
4  * Copyright (C) 2004 Nathan Kurz
5  * 
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * 
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  * 
20  */
21
22 #include <string.h>
23
24 #include "bchash.h"
25 #include "bctextbox.h"
26 #include "bclistbox.h"
27 #include "bclistboxitem.h"
28 #include "bcrecentlist.h"
29
30 // NOTE: textbox can be NULL if no textbox is associated
31 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults, 
32                              BC_TextBox *textbox, int max, 
33                              int x, int y, int w, int h) 
34         : BC_ListBox(x, y, w, h, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1) 
35 {
36         this->type = type;
37         this->defaults = defaults;
38         this->textbox = textbox;
39         set_tooltip("Choose from recently used");
40 }
41
42 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults, 
43                              BC_TextBox *textbox) 
44         : BC_ListBox(textbox->get_x() + textbox->get_w(), textbox->get_y(),
45                      textbox->get_w(), RECENT_POPUP_HEIGHT,
46                      LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
47 {
48         this->type = type;
49         this->defaults = defaults;
50         this->textbox = textbox;
51         set_tooltip("Choose from recently used");
52 }
53
54 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults) 
55         : BC_ListBox(-1, -1, -1, -1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
56 {
57         this->type = type;
58         this->defaults = defaults;
59         this->textbox = NULL;
60 }
61
62 BC_RecentList::~BC_RecentList() {
63         items.remove_all_objects();
64 }
65
66 int BC_RecentList::handle_event() {
67         BC_ListBoxItem *item = get_selection(0, 0);
68         if (item < 0) return 0;
69         char *text = item->get_text();
70         if (text && textbox) {
71                 // change the text in the textbox
72                 textbox->update(text);
73                 // tell the textbox to deal with it
74                 textbox->handle_event();
75         }
76         return 0;
77 }
78
79
80 int BC_RecentList::load_items(const char *prefix) {
81
82         if (! prefix) prefix = "ANY";
83
84         if (items.total > 0) {
85                 items.remove_all_objects();
86         }
87
88         int count;
89         for (count = 0; count < RECENT_MAX_ITEMS; count++) {
90                 char save[BCTEXTLEN];
91                 char text[BCTEXTLEN];
92                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
93                 text[0] = 0;
94                 defaults->get(save, text);
95                 if (strlen(text) == 0) break;
96                 items.append(new BC_ListBoxItem(text));
97         }
98
99         // only update if we are part of a window
100         if (textbox) update(&items, 0, 0, 1);
101
102         return count;
103 }       
104
105
106 int BC_RecentList::add_item(const char *prefix, char *text) {
107
108         if (! prefix) prefix = "ANY";
109         
110         // remove an old item if it matches the new text
111         for (int i = 0; i < items.total; i++) {
112                 BC_ListBoxItem *item = items.values[i];
113                 if (strcmp(text, item->get_text()) == 0) {
114                         items.remove_object(item);
115                 }
116         }
117
118         // make a new item and put it at the top of the list
119         items.insert(new BC_ListBoxItem(text), 0);
120
121         // save up to maximum items of the list for future use
122         int count;
123         for (count = 0; 
124              count < RECENT_MAX_ITEMS && count < items.total; 
125              count++) {
126                 BC_ListBoxItem *item = items.values[count];
127                 char save[BCTEXTLEN];
128                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
129                 defaults->update(save, item->get_text());
130         }
131
132         return count;
133 }