73d2859dd601da8bbeb0be4222f6f9c7988b84b9
[goodguy/history.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         BC_ListBoxItem *item = get_selection(0, 0);
70         if( !item ) return 0;
71         char *text = item->get_text();
72         if (text && textbox) {
73                 // change the text in the textbox
74                 textbox->update(text);
75                 // tell the textbox to deal with it
76                 textbox->handle_event();
77         }
78         return 0;
79 }
80
81
82 int BC_RecentList::load_items(const char *prefix) {
83
84         if (! prefix) prefix = "ANY";
85
86         if (items.total > 0) {
87                 items.remove_all_objects();
88         }
89
90         int count;
91         for (count = 0; count < RECENT_MAX_ITEMS; count++) {
92                 char save[BCTEXTLEN];
93                 char text[BCTEXTLEN];
94                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
95                 text[0] = 0;
96                 defaults->get(save, text);
97                 if (strlen(text) == 0) break;
98                 items.append(new BC_ListBoxItem(text));
99         }
100
101         // only update if we are part of a window
102         if (textbox) update(&items, 0, 0, 1);
103
104         return count;
105 }
106
107
108 int BC_RecentList::add_item(const char *prefix, char *text) {
109
110         if (! prefix) prefix = "ANY";
111
112         // remove an old item if it matches the new text
113         for (int i = 0; i < items.total; i++) {
114                 BC_ListBoxItem *item = items.values[i];
115                 if (strcmp(text, item->get_text()) == 0) {
116                         items.remove_object(item);
117                 }
118         }
119
120         // make a new item and put it at the top of the list
121         items.insert(new BC_ListBoxItem(text), 0);
122
123         // save up to maximum items of the list for future use
124         int count;
125         for (count = 0;
126              count < RECENT_MAX_ITEMS && count < items.total;
127              count++) {
128                 BC_ListBoxItem *item = items.values[count];
129                 char save[BCTEXTLEN];
130                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
131                 defaults->update(save, item->get_text());
132         }
133
134         return count;
135 }