remove Features5, rework gradient plugin, fix paste_edl track title bug, gl_probe...
[goodguy/cinelerra.git] / cinelerra-5.1 / 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 #include "language.h"
30
31 // NOTE: textbox can be NULL if no textbox is associated
32 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults,
33                              BC_TextBox *textbox, int max,
34                              int x, int y, int w, int h)
35         : BC_ListBox(x, y, w, h, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
36 {
37         this->type = type;
38         this->defaults = defaults;
39         this->textbox = textbox;
40         set_tooltip(_("Choose from recently used"));
41 }
42
43 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults,
44                              BC_TextBox *textbox)
45         : BC_ListBox(textbox->get_x() + textbox->get_w(), textbox->get_y(),
46                      textbox->get_w(), RECENT_POPUP_HEIGHT,
47                      LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
48 {
49         this->type = type;
50         this->defaults = defaults;
51         this->textbox = textbox;
52         set_tooltip(_("Choose from recently used"));
53 }
54
55 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults)
56         : BC_ListBox(-1, -1, -1, -1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
57 {
58         this->type = type;
59         this->defaults = defaults;
60         this->textbox = NULL;
61 }
62
63 BC_RecentList::~BC_RecentList()
64 {
65         items.remove_all_objects();
66 }
67
68 int BC_RecentList::handle_event()
69 {
70         BC_ListBoxItem *item = get_selection(0, 0);
71         if( !item ) return 0;
72         char *text = item->get_text();
73         if (text && textbox) {
74                 // change the text in the textbox
75                 textbox->update(text);
76                 // tell the textbox to deal with it
77                 textbox->handle_event();
78         }
79         return 0;
80 }
81
82
83 int BC_RecentList::load_items(const char *prefix)
84 {
85
86         if (! prefix) prefix = "ANY";
87
88         if (items.total > 0) {
89                 items.remove_all_objects();
90         }
91
92         int count;
93         for (count = 0; count < RECENT_MAX_ITEMS; count++) {
94                 char save[BCTEXTLEN];
95                 char text[BCTEXTLEN];
96                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
97                 text[0] = 0;
98                 defaults->get(save, text);
99                 if (strlen(text) == 0) break;
100                 items.append(new BC_ListBoxItem(text));
101         }
102
103         // only update if we are part of a window
104         if (textbox) update(&items, 0, 0, 1);
105
106         return count;
107 }
108
109
110 int BC_RecentList::add_item(const char *prefix, const char *text)
111 {
112
113         if (! prefix) prefix = "ANY";
114
115         // remove an old item if it matches the new text
116         for (int i = 0; i < items.total; i++) {
117                 BC_ListBoxItem *item = items.values[i];
118                 if (strcmp(text, item->get_text()) == 0) {
119                         items.remove_object(item);
120                 }
121         }
122
123         // make a new item and put it at the top of the list
124         items.insert(new BC_ListBoxItem(text), 0);
125
126         // save up to maximum items of the list for future use
127         int count;
128         for (count = 0;
129              count < RECENT_MAX_ITEMS && count < items.total;
130              count++) {
131                 BC_ListBoxItem *item = items.values[count];
132                 char save[BCTEXTLEN];
133                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
134                 defaults->update(save, item->get_text());
135         }
136
137         return count;
138 }
139