|
1
|
1 #include "StdAfx.h"
|
|
|
2
|
|
|
3 #include "dropdown_helper.h"
|
|
|
4
|
|
|
5 void _cfg_dropdown_history_base::build_list(pfc::ptr_list_t<char> & out)
|
|
|
6 {
|
|
|
7 pfc::string8 temp; get_state(temp);
|
|
|
8 const char * src = temp;
|
|
|
9 while(*src)
|
|
|
10 {
|
|
|
11 int ptr = 0;
|
|
|
12 while(src[ptr] && src[ptr]!=separator) ptr++;
|
|
|
13 if (ptr>0)
|
|
|
14 {
|
|
|
15 out.add_item(pfc::strdup_n(src,ptr));
|
|
|
16 src += ptr;
|
|
|
17 }
|
|
|
18 while(*src==separator) src++;
|
|
|
19 }
|
|
|
20 }
|
|
|
21
|
|
|
22 void _cfg_dropdown_history_base::parse_list(const pfc::ptr_list_t<char> & src)
|
|
|
23 {
|
|
|
24 t_size n;
|
|
|
25 pfc::string8_fastalloc temp;
|
|
|
26 for(n=0;n<src.get_count();n++)
|
|
|
27 {
|
|
|
28 temp.add_string(src[n]);
|
|
|
29 temp.add_char(separator);
|
|
|
30 }
|
|
|
31 set_state(temp);
|
|
|
32 }
|
|
|
33
|
|
|
34 #ifdef _WIN32
|
|
|
35 static void g_setup_dropdown_fromlist(HWND wnd,const pfc::ptr_list_t<char> & list)
|
|
|
36 {
|
|
|
37 t_size n, m = list.get_count();
|
|
|
38 uSendMessage(wnd,CB_RESETCONTENT,0,0);
|
|
|
39 for(n=0;n<m;n++) {
|
|
|
40 uSendMessageText(wnd,CB_ADDSTRING,0,list[n]);
|
|
|
41 }
|
|
|
42 }
|
|
|
43
|
|
|
44 void _cfg_dropdown_history_base::setup_dropdown_set_value(HWND wnd) {
|
|
|
45 pfc::ptr_list_t<char> list;
|
|
|
46 build_list(list);
|
|
|
47 g_setup_dropdown_fromlist(wnd, list);
|
|
|
48 if ( list.get_size() > 0 ) {
|
|
|
49 uSetWindowText(wnd, list[0] );
|
|
|
50 }
|
|
|
51 list.free_all();
|
|
|
52 }
|
|
|
53
|
|
|
54 void _cfg_dropdown_history_base::setup_dropdown(HWND wnd)
|
|
|
55 {
|
|
|
56 pfc::ptr_list_t<char> list;
|
|
|
57 build_list(list);
|
|
|
58 g_setup_dropdown_fromlist(wnd,list);
|
|
|
59 list.free_all();
|
|
|
60 }
|
|
|
61 #endif // _WIN32
|
|
|
62
|
|
|
63 bool _cfg_dropdown_history_base::add_item(const char * item)
|
|
|
64 {
|
|
|
65 if (!item || !*item) return false;
|
|
|
66 pfc::string8 meh;
|
|
|
67 if (strchr(item,separator))
|
|
|
68 {
|
|
|
69 uReplaceChar(meh,item,-1,separator,'|',false);
|
|
|
70 item = meh;
|
|
|
71 }
|
|
|
72 pfc::ptr_list_t<char> list;
|
|
|
73 build_list(list);
|
|
|
74 unsigned n;
|
|
|
75 bool found = false;
|
|
|
76 for(n=0;n<list.get_count();n++)
|
|
|
77 {
|
|
|
78 if (!strcmp(list[n],item))
|
|
|
79 {
|
|
|
80 char* temp = list.remove_by_idx(n);
|
|
|
81 list.insert_item(temp,0);
|
|
|
82 found = true;
|
|
|
83 }
|
|
|
84 }
|
|
|
85
|
|
|
86 if (!found)
|
|
|
87 {
|
|
|
88 while(list.get_count() > m_max) list.delete_by_idx(list.get_count()-1);
|
|
|
89 list.insert_item(strdup(item),0);
|
|
|
90 }
|
|
|
91 parse_list(list);
|
|
|
92 list.free_all();
|
|
|
93 return found;
|
|
|
94 }
|
|
|
95
|
|
|
96 bool _cfg_dropdown_history_base::is_empty()
|
|
|
97 {
|
|
|
98 pfc::string8 temp; get_state(temp);
|
|
|
99 const char * src = temp;
|
|
|
100 while(*src)
|
|
|
101 {
|
|
|
102 if (*src!=separator) return false;
|
|
|
103 src++;
|
|
|
104 }
|
|
|
105 return true;
|
|
|
106 }
|
|
|
107
|
|
|
108 #ifdef _WIN32
|
|
|
109 bool _cfg_dropdown_history_base::add_item(const char *item, HWND combobox) {
|
|
|
110 const bool state = add_item(item);
|
|
|
111 if (state) uSendMessageText(combobox, CB_ADDSTRING, 0, item);
|
|
|
112 return state;
|
|
|
113 }
|
|
|
114
|
|
|
115 bool _cfg_dropdown_history_base::on_context(HWND wnd,LPARAM coords) {
|
|
|
116 try {
|
|
|
117 int coords_x = (short)LOWORD(coords), coords_y = (short)HIWORD(coords);
|
|
|
118 if (coords_x == -1 && coords_y == -1)
|
|
|
119 {
|
|
|
120 RECT asdf;
|
|
|
121 GetWindowRect(wnd,&asdf);
|
|
|
122 coords_x = (asdf.left + asdf.right) / 2;
|
|
|
123 coords_y = (asdf.top + asdf.bottom) / 2;
|
|
|
124 }
|
|
|
125 enum {ID_ERASE_ALL = 1, ID_ERASE_ONE };
|
|
|
126 HMENU menu = CreatePopupMenu();
|
|
|
127 uAppendMenu(menu,MF_STRING,ID_ERASE_ALL,"Wipe history");
|
|
|
128 {
|
|
|
129 pfc::string8 tempvalue;
|
|
|
130 uGetWindowText(wnd,tempvalue);
|
|
|
131 if (!tempvalue.is_empty())
|
|
|
132 uAppendMenu(menu,MF_STRING,ID_ERASE_ONE,"Remove this history item");
|
|
|
133 }
|
|
|
134 int cmd = TrackPopupMenu(menu,TPM_RIGHTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,coords_x,coords_y,0,wnd,0);
|
|
|
135 DestroyMenu(menu);
|
|
|
136 switch(cmd)
|
|
|
137 {
|
|
|
138 case ID_ERASE_ALL:
|
|
|
139 {
|
|
|
140 set_state("");
|
|
|
141 pfc::string8 value;//preserve old value while wiping dropdown list
|
|
|
142 uGetWindowText(wnd,value);
|
|
|
143 uSendMessage(wnd,CB_RESETCONTENT,0,0);
|
|
|
144 uSetWindowText(wnd,value);
|
|
|
145 return true;
|
|
|
146 }
|
|
|
147 case ID_ERASE_ONE:
|
|
|
148 {
|
|
|
149 pfc::string8 value;
|
|
|
150 uGetWindowText(wnd,value);
|
|
|
151
|
|
|
152 pfc::ptr_list_t<char> list;
|
|
|
153 build_list(list);
|
|
|
154 bool found = false;
|
|
|
155 for(t_size n=0;n<list.get_size();n++)
|
|
|
156 {
|
|
|
157 if (!strcmp(value,list[n]))
|
|
|
158 {
|
|
|
159 free(list[n]);
|
|
|
160 list.remove_by_idx(n);
|
|
|
161 found = true;
|
|
|
162 break;
|
|
|
163 }
|
|
|
164 }
|
|
|
165 if (found) parse_list(list);
|
|
|
166 g_setup_dropdown_fromlist(wnd,list);
|
|
|
167 list.free_all();
|
|
|
168 return found;
|
|
|
169 }
|
|
|
170 }
|
|
|
171 } catch(...) {}
|
|
|
172 return false;
|
|
|
173 }
|
|
|
174 #endif
|