4 * Copyright (C) 2004 Nathan Kurz
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.
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.
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
25 #include "bctextbox.h"
26 #include "bclistbox.h"
27 #include "bclistboxitem.h"
28 #include "bcrecentlist.h"
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)
38 this->defaults = defaults;
39 this->textbox = textbox;
40 set_tooltip(_("Choose from recently used"));
43 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults,
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)
50 this->defaults = defaults;
51 this->textbox = textbox;
52 set_tooltip(_("Choose from recently used"));
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)
59 this->defaults = defaults;
63 BC_RecentList::~BC_RecentList()
65 items.remove_all_objects();
68 int BC_RecentList::handle_event()
70 BC_ListBoxItem *item = get_selection(0, 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();
83 int BC_RecentList::load_items(const char *prefix)
86 if (! prefix) prefix = "ANY";
88 if (items.total > 0) {
89 items.remove_all_objects();
93 for (count = 0; count < RECENT_MAX_ITEMS; count++) {
96 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
98 defaults->get(save, text);
99 if (strlen(text) == 0) break;
100 items.append(new BC_ListBoxItem(text));
103 // only update if we are part of a window
104 if (textbox) update(&items, 0, 0, 1);
110 int BC_RecentList::add_item(const char *prefix, const char *text)
113 if (! prefix) prefix = "ANY";
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);
123 // make a new item and put it at the top of the list
124 items.insert(new BC_ListBoxItem(text), 0);
126 // save up to maximum items of the list for future use
129 count < RECENT_MAX_ITEMS && count < items.total;
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());