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"
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)
37 this->defaults = defaults;
38 this->textbox = textbox;
39 set_tooltip("Choose from recently used");
42 BC_RecentList::BC_RecentList(const char *type, BC_Hash *defaults,
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)
49 this->defaults = defaults;
50 this->textbox = textbox;
51 set_tooltip("Choose from recently used");
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)
58 this->defaults = defaults;
62 BC_RecentList::~BC_RecentList() {
63 items.remove_all_objects();
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();
80 int BC_RecentList::load_items(const char *prefix) {
82 if (! prefix) prefix = "ANY";
84 if (items.total > 0) {
85 items.remove_all_objects();
89 for (count = 0; count < RECENT_MAX_ITEMS; count++) {
92 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
94 defaults->get(save, text);
95 if (strlen(text) == 0) break;
96 items.append(new BC_ListBoxItem(text));
99 // only update if we are part of a window
100 if (textbox) update(&items, 0, 0, 1);
106 int BC_RecentList::add_item(const char *prefix, char *text) {
108 if (! prefix) prefix = "ANY";
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);
118 // make a new item and put it at the top of the list
119 items.insert(new BC_ListBoxItem(text), 0);
121 // save up to maximum items of the list for future use
124 count < RECENT_MAX_ITEMS && count < items.total;
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());