r877: Fix files that were missing from a make dist tarball.
[cinelerra_cv.git] / guicast / bcrecentlist.C
blobd9a681145d03dbe6d52389b1f11f74db9e9c09a4
1 #include <string.h>
3 #include "bchash.h"
4 #include "bctextbox.h"
5 #include "bclistbox.h"
6 #include "bclistboxitem.h"
7 #include "bcrecentlist.h"
9 // NOTE: textbox can be NULL if no textbox is associated
10 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults, 
11                              BC_TextBox *textbox, int max, 
12                              int x, int y, int w, int h) 
13         : BC_ListBox(x, y, w, h, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1) 
15         this->type = type;
16         this->defaults = defaults;
17         this->textbox = textbox;
18         set_tooltip("Choose from recently used");
21 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults, 
22                              BC_TextBox *textbox) 
23         : BC_ListBox(textbox->get_x() + textbox->get_w(), textbox->get_y(),
24                      textbox->get_w(), RECENT_POPUP_HEIGHT,
25                      LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
27         this->type = type;
28         this->defaults = defaults;
29         this->textbox = textbox;
30         set_tooltip("Choose from recently used");
33 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults) 
34         : BC_ListBox(-1, -1, -1, -1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
36         this->type = type;
37         this->defaults = defaults;
38         this->textbox = NULL;
41 BC_RecentList::~BC_RecentList() {
42         items.remove_all_objects();
45 int BC_RecentList::handle_event() {
46         BC_ListBoxItem *item = get_selection(0, 0);
47         if (item < 0) return 0;
48         char *text = item->get_text();
49         if (text && textbox) {
50                 // change the text in the textbox
51                 textbox->update(text);
52                 // tell the textbox to deal with it
53                 textbox->handle_event();
54         }
55         return 0;
59 int BC_RecentList::load_items(const char *prefix) {
61         if (! prefix) prefix = "ANY";
63         if (items.total > 0) {
64                 items.remove_all_objects();
65         }
67         int count;
68         for (count = 0; count < RECENT_MAX_ITEMS; count++) {
69                 char save[BCTEXTLEN];
70                 char text[BCTEXTLEN];
71                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
72                 sprintf(text, "");
73                 defaults->get(save, text);
74                 if (strlen(text) == 0) break;
75                 items.append(new BC_ListBoxItem(text));
76         }
78         // only update if we are part of a window
79         if (textbox) update(&items, 0, 0, 1);
81         return count;
82 }       
85 int BC_RecentList::add_item(const char *prefix, char *text) {
87         if (! prefix) prefix = "ANY";
88         
89         // remove an old item if it matches the new text
90         for (int i = 0; i < items.total; i++) {
91                 BC_ListBoxItem *item = items.values[i];
92                 if (strcmp(text, item->get_text()) == 0) {
93                         items.remove_object(item);
94                 }
95         }
97         // make a new item and put it at the top of the list
98         items.insert(new BC_ListBoxItem(text), 0);
100         // save up to maximum items of the list for future use
101         int count;
102         for (count = 0; 
103              count < RECENT_MAX_ITEMS && count < items.total; 
104              count++) {
105                 BC_ListBoxItem *item = items.values[count];
106                 char save[BCTEXTLEN];
107                 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
108                 defaults->update(save, item->get_text());
109         }
111         return count;