00001 #include <string.h>
00002
00003 #include "defaults.h"
00004 #include "bctextbox.h"
00005 #include "bclistbox.h"
00006 #include "bclistboxitem.h"
00007 #include "bcrecentlist.h"
00008
00009
00010 BC_RecentList::BC_RecentList(const char *type, Defaults *defaults,
00011 BC_TextBox *textbox, int max,
00012 int x, int y, int w, int h)
00013 : BC_ListBox(x, y, w, h, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
00014 {
00015 this->type = type;
00016 this->defaults = defaults;
00017 this->textbox = textbox;
00018 set_tooltip("Choose from recently used");
00019 }
00020
00021 BC_RecentList::BC_RecentList(const char *type, Defaults *defaults,
00022 BC_TextBox *textbox)
00023 : BC_ListBox(textbox->get_x() + textbox->get_w(), textbox->get_y(),
00024 textbox->get_w(), RECENT_POPUP_HEIGHT,
00025 LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
00026 {
00027 this->type = type;
00028 this->defaults = defaults;
00029 this->textbox = textbox;
00030 set_tooltip("Choose from recently used");
00031 }
00032
00033 BC_RecentList::BC_RecentList(const char *type, Defaults *defaults)
00034 : BC_ListBox(-1, -1, -1, -1, LISTBOX_TEXT, 0, 0, 0, 1, 0, 1)
00035 {
00036 this->type = type;
00037 this->defaults = defaults;
00038 this->textbox = NULL;
00039 }
00040
00041 BC_RecentList::~BC_RecentList() {
00042 items.remove_all_objects();
00043 }
00044
00045 int BC_RecentList::handle_event() {
00046 BC_ListBoxItem *item = get_selection(0, 0);
00047 if (item < 0) return 0;
00048 char *text = item->get_text();
00049 if (text && textbox) {
00050
00051 textbox->update(text);
00052
00053 textbox->handle_event();
00054 }
00055 return 0;
00056 }
00057
00058
00059 int BC_RecentList::load_items(const char *prefix) {
00060
00061 if (! prefix) prefix = "ANY";
00062
00063 if (items.total > 0) {
00064 items.remove_all_objects();
00065 }
00066
00067 int count;
00068 for (count = 0; count < RECENT_MAX_ITEMS; count++) {
00069 char save[BCTEXTLEN];
00070 char text[BCTEXTLEN];
00071 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
00072 sprintf(text, "");
00073 defaults->get(save, text);
00074 if (strlen(text) == 0) break;
00075 items.append(new BC_ListBoxItem(text));
00076 }
00077
00078
00079 if (textbox) update(&items, 0, 0, 1);
00080
00081 return count;
00082 }
00083
00084
00085 int BC_RecentList::add_item(const char *prefix, char *text) {
00086
00087 if (! prefix) prefix = "ANY";
00088
00089
00090 for (int i = 0; i < items.total; i++) {
00091 BC_ListBoxItem *item = items.values[i];
00092 if (strcmp(text, item->get_text()) == 0) {
00093 items.remove_object(item);
00094 }
00095 }
00096
00097
00098 items.insert(new BC_ListBoxItem(text), 0);
00099
00100
00101 int count;
00102 for (count = 0;
00103 count < RECENT_MAX_ITEMS && count < items.total;
00104 count++) {
00105 BC_ListBoxItem *item = items.values[count];
00106 char save[BCTEXTLEN];
00107 sprintf(save, "RECENT_%s_%s_%d", prefix, type, count);
00108 defaults->update(save, item->get_text());
00109 }
00110
00111 return count;
00112 }