foobar2000 SDK  2015-08-03
dropdown_helper.cpp
Go to the documentation of this file.
1 #include "stdafx.h"
2 #include "dropdown_helper.h"
3 
4 #ifdef _WIN32
5 
7 {
8  pfc::string8 temp; get_state(temp);
9  const char * src = temp;
10  while(*src)
11  {
12  int ptr = 0;
13  while(src[ptr] && src[ptr]!=separator) ptr++;
14  if (ptr>0)
15  {
16  out.add_item(pfc::strdup_n(src,ptr));
17  src += ptr;
18  }
19  while(*src==separator) src++;
20  }
21 }
22 
24 {
25  t_size n;
27  for(n=0;n<src.get_count();n++)
28  {
29  temp.add_string(src[n]);
30  temp.add_char(separator);
31  }
32  set_state(temp);
33 }
34 
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 
45 {
47  build_list(list);
48  g_setup_dropdown_fromlist(wnd,list);
49  list.free_all();
50 }
51 
52 bool _cfg_dropdown_history_base::add_item(const char * item)
53 {
54  if (!item || !*item) return false;
55  pfc::string8 meh;
56  if (strchr(item,separator))
57  {
58  uReplaceChar(meh,item,-1,separator,'|',false);
59  item = meh;
60  }
62  build_list(list);
63  unsigned n;
64  bool found = false;
65  for(n=0;n<list.get_count();n++)
66  {
67  if (!strcmp(list[n],item))
68  {
69  char* temp = list.remove_by_idx(n);
70  list.insert_item(temp,0);
71  found = true;
72  }
73  }
74 
75  if (!found)
76  {
77  while(list.get_count() > m_max) list.delete_by_idx(list.get_count()-1);
78  list.insert_item(_strdup(item),0);
79  }
80  parse_list(list);
81  list.free_all();
82  return found;
83 }
84 
85 bool _cfg_dropdown_history_base::add_item(const char *item, HWND combobox) {
86  const bool state = add_item(item);
87  if (state) uSendMessageText(combobox, CB_ADDSTRING, 0, item);
88  return state;
89 }
90 
92 {
93  pfc::string8 temp; get_state(temp);
94  const char * src = temp;
95  while(*src)
96  {
97  if (*src!=separator) return false;
98  src++;
99  }
100  return true;
101 }
102 
103 bool _cfg_dropdown_history_base::on_context(HWND wnd,LPARAM coords) {
104  try {
105  int coords_x = (short)LOWORD(coords), coords_y = (short)HIWORD(coords);
106  if (coords_x == -1 && coords_y == -1)
107  {
108  RECT asdf;
109  GetWindowRect(wnd,&asdf);
110  coords_x = (asdf.left + asdf.right) / 2;
111  coords_y = (asdf.top + asdf.bottom) / 2;
112  }
113  enum {ID_ERASE_ALL = 1, ID_ERASE_ONE };
114  HMENU menu = CreatePopupMenu();
115  uAppendMenu(menu,MF_STRING,ID_ERASE_ALL,"Wipe history");
116  {
117  pfc::string8 tempvalue;
118  uGetWindowText(wnd,tempvalue);
119  if (!tempvalue.is_empty())
120  uAppendMenu(menu,MF_STRING,ID_ERASE_ONE,"Remove this history item");
121  }
122  int cmd = TrackPopupMenu(menu,TPM_RIGHTBUTTON|TPM_NONOTIFY|TPM_RETURNCMD,coords_x,coords_y,0,wnd,0);
123  DestroyMenu(menu);
124  switch(cmd)
125  {
126  case ID_ERASE_ALL:
127  {
128  set_state("");
129  pfc::string8 value;//preserve old value while wiping dropdown list
130  uGetWindowText(wnd,value);
131  uSendMessage(wnd,CB_RESETCONTENT,0,0);
132  uSetWindowText(wnd,value);
133  return true;
134  }
135  case ID_ERASE_ONE:
136  {
137  pfc::string8 value;
138  uGetWindowText(wnd,value);
139 
141  build_list(list);
142  bool found = false;
143  for(t_size n=0;n<list.get_size();n++)
144  {
145  if (!strcmp(value,list[n]))
146  {
147  free(list[n]);
148  list.remove_by_idx(n);
149  found = true;
150  break;
151  }
152  }
153  if (found) parse_list(list);
154  g_setup_dropdown_fromlist(wnd,list);
155  list.free_all();
156  return found;
157  }
158  }
159  } catch(...) {}
160  return false;
161 }
162 
163 #endif // _WIN32
t_size get_size() const
Definition: list.h:366
BOOL SHARED_EXPORT uGetWindowText(HWND wnd, pfc::string_base &out)
virtual void set_state(const char *val)=0
void build_list(pfc::ptr_list_t< char > &out)
virtual void get_state(pfc::string_base &out) const =0
LRESULT SHARED_EXPORT uSendMessageText(HWND wnd, UINT msg, WPARAM wp, const char *text)
t_size get_count() const
Definition: list.h:365
size_t t_size
Definition: int_types.h:48
t_size add_item(const T &item)
Definition: list.h:522
T remove_by_idx(t_size idx)
Definition: list.h:331
char * strdup_n(const char *src, t_size len)
Definition: string_base.cpp:75
BOOL SHARED_EXPORT uSetWindowText(HWND wnd, const char *p_text)
BOOL SHARED_EXPORT uAppendMenu(HMENU menu, UINT flags, UINT_PTR id, const char *content)
bool is_empty() const
Definition: string_base.h:213
t_size insert_item(const t_in &item, t_size idx)
Definition: list.h:323
void free_all()
Definition: ptr_list.h:14
bool on_context(HWND wnd, LPARAM coords)
t_size uReplaceChar(pfc::string_base &out, const char *src, t_size src_len, unsigned c1, unsigned c2, bool casesens)
Definition: shared.h:394
void delete_by_idx(t_size p_index)
Definition: ptr_list.h:19
bool add_item(const char *item)
void parse_list(const pfc::ptr_list_t< char > &src)
string8_t< pfc::alloc_fast_aggressive > string8_fastalloc
Definition: string_base.h:436