]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
11e62fe6 | 2 | // Name: src/gtk/listbox.cpp |
c801d85f KB |
3 | // Purpose: |
4 | // Author: Robert Roebling | |
4a46cbe8 | 5 | // Modified By: Ryan Norton (GtkTreeView implementation) |
f96aa4d9 RR |
6 | // Id: $Id$ |
7 | // Copyright: (c) 1998 Robert Roebling | |
65571936 | 8 | // Licence: wxWindows licence |
c801d85f KB |
9 | ///////////////////////////////////////////////////////////////////////////// |
10 | ||
14f355c2 VS |
11 | // For compilers that support precompilation, includes "wx.h". |
12 | #include "wx/wxprec.h" | |
13 | ||
dcf924a3 RR |
14 | #if wxUSE_LISTBOX |
15 | ||
2da2f941 | 16 | #include "wx/listbox.h" |
dcf924a3 | 17 | #include "wx/dynarray.h" |
2da2f941 | 18 | #include "wx/arrstr.h" |
09cf7c58 | 19 | #include "wx/utils.h" |
caaa4cfd RR |
20 | #include "wx/intl.h" |
21 | #include "wx/checklst.h" | |
72a16063 | 22 | #include "wx/settings.h" |
4a46cbe8 | 23 | #include "wx/log.h" |
fab591c5 | 24 | #include "wx/gtk/private.h" |
4a46cbe8 | 25 | #include "wx/gtk/treeentry_gtk.h" |
291a8f20 RR |
26 | |
27 | #if wxUSE_TOOLTIPS | |
b1170810 | 28 | #include "wx/tooltip.h" |
291a8f20 | 29 | #endif |
c801d85f | 30 | |
4a46cbe8 RR |
31 | #include <gdk/gdk.h> |
32 | #include <gtk/gtk.h> | |
16c1f79c | 33 | #include <gdk/gdkkeysyms.h> |
83624f79 | 34 | |
66bd6b93 RR |
35 | //----------------------------------------------------------------------------- |
36 | // data | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
d7fa7eaa RR |
39 | extern bool g_blockEventsOnDrag; |
40 | extern bool g_blockEventsOnScroll; | |
41 | extern wxCursor g_globalCursor; | |
caaa4cfd | 42 | |
7a30ee8f | 43 | |
4a82abc8 | 44 | |
c9433ea9 | 45 | //----------------------------------------------------------------------------- |
4a46cbe8 | 46 | // Macro to tell which row the strings are in (1 if native checklist, 0 if not) |
c9433ea9 RR |
47 | //----------------------------------------------------------------------------- |
48 | ||
4a46cbe8 RR |
49 | #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST |
50 | # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0) | |
51 | #else | |
52 | # define WXLISTBOX_DATACOLUMN_ARG(x) (0) | |
53 | #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST | |
17a1ebd1 | 54 | |
4a46cbe8 | 55 | #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this) |
c9433ea9 RR |
56 | |
57 | //----------------------------------------------------------------------------- | |
4a46cbe8 | 58 | // "row-activated" |
c9433ea9 RR |
59 | //----------------------------------------------------------------------------- |
60 | ||
865bb325 | 61 | extern "C" { |
4a46cbe8 RR |
62 | static void |
63 | gtk_listbox_row_activated_callback(GtkTreeView *treeview, | |
64 | GtkTreePath *path, | |
65 | GtkTreeViewColumn *col, | |
66 | wxListBox *listbox) | |
c9433ea9 | 67 | { |
4a46cbe8 | 68 | if (g_isIdle) wxapp_install_idle_handler(); |
c9433ea9 | 69 | |
4a46cbe8 RR |
70 | if (g_blockEventsOnDrag) return; |
71 | if (g_blockEventsOnScroll) return; | |
c9433ea9 | 72 | |
4a46cbe8 | 73 | if (!listbox->m_hasVMT) return; |
c9433ea9 | 74 | |
4a46cbe8 RR |
75 | //Notes: |
76 | //1) This is triggered by either a double-click or a space press | |
77 | //2) We handle both here because | |
78 | //2a) in the case of a space/keypress we can't really know | |
79 | // which item was pressed on because we can't get coords | |
80 | // from a keyevent | |
81 | //2b) It seems more correct | |
c9433ea9 | 82 | |
4a46cbe8 | 83 | int sel = gtk_tree_path_get_indices(path)[0]; |
c9433ea9 | 84 | |
4a46cbe8 RR |
85 | if(!listbox->m_spacePressed) |
86 | { | |
87 | //Assume it was double-click | |
88 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() ); | |
89 | event.SetEventObject( listbox ); | |
c9433ea9 | 90 | |
4a46cbe8 RR |
91 | if(listbox->IsSelected(sel)) |
92 | { | |
93 | GtkTreeEntry* entry = listbox->GtkGetEntry(sel); | |
caaa4cfd | 94 | |
4a46cbe8 RR |
95 | if(entry) |
96 | { | |
97 | event.SetInt(sel); | |
98 | event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry))); | |
7a30ee8f | 99 | |
4a46cbe8 | 100 | if ( listbox->HasClientObjectData() ) |
caf6e6de | 101 | event.SetClientObject( |
4a46cbe8 RR |
102 | (wxClientData*) gtk_tree_entry_get_userdata(entry) ); |
103 | else if ( listbox->HasClientUntypedData() ) | |
104 | event.SetClientData( gtk_tree_entry_get_userdata(entry) ); | |
105 | g_object_unref(G_OBJECT(entry)); | |
106 | } | |
107 | else | |
108 | { | |
109 | wxLogSysError(wxT("Internal error - could not get entry for double-click")); | |
110 | event.SetInt(-1); | |
111 | } | |
112 | } | |
113 | else | |
114 | event.SetInt(-1); | |
acfd422a | 115 | |
4a46cbe8 RR |
116 | listbox->GetEventHandler()->ProcessEvent( event ); |
117 | } | |
118 | else | |
119 | { | |
120 | listbox->m_spacePressed = false; //don't block selection behaviour anymore | |
caaa4cfd | 121 | |
4a46cbe8 RR |
122 | //Space was pressed - toggle the appropriate checkbox and the selection |
123 | #if wxUSE_CHECKLISTBOX //Do it for both native and non-native | |
124 | if (listbox->m_hasCheckBoxes) | |
125 | { | |
126 | wxCheckListBox *clb = (wxCheckListBox *)listbox; | |
caaa4cfd | 127 | |
4a46cbe8 | 128 | clb->Check( sel, !clb->IsChecked(sel) ); |
ff8bfdbb | 129 | |
4a46cbe8 RR |
130 | wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); |
131 | new_event.SetEventObject( listbox ); | |
132 | new_event.SetInt( sel ); | |
133 | listbox->GetEventHandler()->ProcessEvent( new_event ); | |
134 | } | |
135 | #endif // wxUSE_CHECKLISTBOX | |
ff8bfdbb | 136 | |
4a46cbe8 RR |
137 | if( (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) || |
138 | ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) ) | |
c64371de | 139 | { |
4a46cbe8 | 140 | //toggle the selection + send event |
caf6e6de | 141 | listbox->GtkSetSelection(sel, !listbox->IsSelected( sel ), false); |
4a46cbe8 | 142 | } |
6c8a980f | 143 | } |
7a30ee8f | 144 | } |
865bb325 | 145 | } |
7a30ee8f RR |
146 | |
147 | //----------------------------------------------------------------------------- | |
148 | // "button_press_event" | |
149 | //----------------------------------------------------------------------------- | |
150 | ||
865bb325 | 151 | extern "C" { |
7a30ee8f | 152 | static gint |
014b0d06 VZ |
153 | gtk_listbox_button_press_callback( GtkWidget *widget, |
154 | GdkEventButton *gdk_event, | |
155 | wxListBox *listbox ) | |
7a30ee8f RR |
156 | { |
157 | if (g_isIdle) wxapp_install_idle_handler(); | |
158 | ||
159 | if (g_blockEventsOnDrag) return FALSE; | |
160 | if (g_blockEventsOnScroll) return FALSE; | |
161 | ||
162 | if (!listbox->m_hasVMT) return FALSE; | |
163 | ||
4a46cbe8 RR |
164 | //Just to be on the safe side - it should be unset in the activate callback |
165 | //but we don't want any obscure bugs if it doesn't get called somehow... | |
166 | listbox->m_spacePressed = false; | |
7a30ee8f | 167 | |
4a46cbe8 | 168 | #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST |
7a30ee8f RR |
169 | if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS)) |
170 | { | |
4a46cbe8 | 171 | GtkTreePath* path; |
caf6e6de | 172 | gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget), |
4a46cbe8 RR |
173 | (gint)gdk_event->x, (gint)gdk_event->y, |
174 | &path, NULL, NULL, NULL); | |
175 | int sel = gtk_tree_path_get_indices(path)[0]; | |
176 | gtk_tree_path_free(path); | |
177 | ||
7a30ee8f RR |
178 | wxCheckListBox *clb = (wxCheckListBox *)listbox; |
179 | ||
180 | clb->Check( sel, !clb->IsChecked(sel) ); | |
181 | ||
182 | wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() ); | |
183 | event.SetEventObject( listbox ); | |
184 | event.SetInt( sel ); | |
185 | listbox->GetEventHandler()->ProcessEvent( event ); | |
4f22cf8d | 186 | } |
4a46cbe8 | 187 | #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST |
4f22cf8d | 188 | |
caaa4cfd RR |
189 | return FALSE; |
190 | } | |
865bb325 | 191 | } |
66bd6b93 | 192 | |
1144d24d RR |
193 | //----------------------------------------------------------------------------- |
194 | // "key_press_event" | |
195 | //----------------------------------------------------------------------------- | |
196 | ||
865bb325 | 197 | extern "C" { |
ff8bfdbb | 198 | static gint |
caf6e6de WS |
199 | gtk_listbox_key_press_callback( GtkWidget *widget, |
200 | GdkEventKey *gdk_event, | |
4a46cbe8 | 201 | wxListBox *listbox ) |
1144d24d | 202 | { |
4a46cbe8 RR |
203 | if (g_isIdle) wxapp_install_idle_handler(); |
204 | ||
205 | if (g_blockEventsOnDrag) return FALSE; | |
acfd422a | 206 | |
1144d24d | 207 | |
caf6e6de | 208 | bool ret = false; |
1144d24d | 209 | |
354aa1e3 RR |
210 | if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab)) |
211 | { | |
212 | wxNavigationKeyEvent new_event; | |
213 | /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */ | |
214 | new_event.SetDirection( (gdk_event->keyval == GDK_Tab) ); | |
215 | /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */ | |
216 | new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) ); | |
217 | new_event.SetCurrentFocus( listbox ); | |
218 | ret = listbox->GetEventHandler()->ProcessEvent( new_event ); | |
219 | } | |
9ef6ff8c | 220 | |
4a82abc8 RR |
221 | if ((gdk_event->keyval == GDK_Return) && (!ret)) |
222 | { | |
4a46cbe8 | 223 | // eat return in all modes (RN:WHY?) |
caf6e6de | 224 | ret = true; |
4a82abc8 | 225 | } |
f96b15a3 | 226 | |
fec195b2 | 227 | // Check or uncheck item with SPACE |
4a46cbe8 | 228 | if (gdk_event->keyval == ' ') |
fec195b2 | 229 | { |
4a46cbe8 RR |
230 | //In the keyevent we don't know the index of the item |
231 | //and the activated event gets called anyway... | |
232 | // | |
caf6e6de | 233 | //Also, activating with the space causes the treeview to |
4a46cbe8 RR |
234 | //unselect all the items and then select the item in question |
235 | //wx's behaviour is to just toggle the item's selection state | |
236 | //and leave the others alone | |
237 | listbox->m_spacePressed = true; | |
fec195b2 | 238 | } |
17a1ebd1 | 239 | |
354aa1e3 RR |
240 | if (ret) |
241 | { | |
9fa72bd2 | 242 | g_signal_stop_emission_by_name (widget, "key_press_event"); |
354aa1e3 RR |
243 | return TRUE; |
244 | } | |
ff8bfdbb | 245 | |
1144d24d RR |
246 | return FALSE; |
247 | } | |
865bb325 | 248 | } |
1144d24d | 249 | |
c801d85f | 250 | //----------------------------------------------------------------------------- |
a60c99e6 | 251 | // "select" and "deselect" |
c801d85f KB |
252 | //----------------------------------------------------------------------------- |
253 | ||
4a46cbe8 RR |
254 | extern "C" { |
255 | static gboolean gtk_listitem_select_cb( GtkTreeSelection* selection, | |
256 | GtkTreeModel* model, | |
257 | GtkTreePath* path, | |
258 | gboolean is_selected, | |
259 | wxListBox *listbox ) | |
c801d85f | 260 | { |
acfd422a RR |
261 | if (g_isIdle) wxapp_install_idle_handler(); |
262 | ||
4a46cbe8 RR |
263 | if (!listbox->m_hasVMT) return TRUE; |
264 | if (g_blockEventsOnDrag) return TRUE; | |
8161b5b9 | 265 | |
4a46cbe8 RR |
266 | if (listbox->m_spacePressed) return FALSE; //see keyevent callback |
267 | if (listbox->m_blockEvent) return TRUE; | |
9ef6ff8c | 268 | |
4a46cbe8 RR |
269 | // NB: wxdocs explicitly say that this event only gets sent when |
270 | // something is actually selected, plus the controls example | |
271 | // assumes so and passes -1 to the dogetclientdata funcs if not | |
8161b5b9 | 272 | |
4a46cbe8 | 273 | // OK, so basically we need to do a bit of a run-around here as |
caf6e6de WS |
274 | // 1) is_selected says whether the item(s?) are CURRENTLY selected - |
275 | // i.e. if is_selected is FALSE then the item is going to be | |
4a46cbe8 | 276 | // selected right now! |
caf6e6de | 277 | // 2) However, since it is not already selected and the user |
4a46cbe8 RR |
278 | // will expect it to be we need to manually select it and |
279 | // return FALSE telling GTK we handled the selection | |
280 | if (is_selected) return TRUE; | |
281 | ||
282 | int nIndex = gtk_tree_path_get_indices(path)[0]; | |
283 | GtkTreeEntry* entry = listbox->GtkGetEntry(nIndex); | |
159b66c0 | 284 | |
4a46cbe8 | 285 | if(entry) |
159b66c0 | 286 | { |
4a46cbe8 RR |
287 | //Now, as mentioned above, we manually select the row that is/was going |
288 | //to be selected anyway by GTK | |
caf6e6de | 289 | listbox->m_blockEvent = true; //if we don't block events we will lock the |
4a46cbe8 | 290 | //app due to recursion!! |
159b66c0 | 291 | |
caf6e6de | 292 | GtkTreeSelection* selection = |
4a46cbe8 RR |
293 | gtk_tree_view_get_selection(listbox->m_treeview); |
294 | GtkTreeIter iter; | |
295 | gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox->m_liststore), &iter, path); | |
296 | gtk_tree_selection_select_iter(selection, &iter); | |
159b66c0 | 297 | |
caf6e6de | 298 | listbox->m_blockEvent = false; |
4a46cbe8 RR |
299 | |
300 | //Finally, send the wx event | |
c64371de RD |
301 | wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() ); |
302 | event.SetEventObject( listbox ); | |
4a46cbe8 | 303 | |
c64371de | 304 | // indicate whether this is a selection or a deselection |
4a46cbe8 RR |
305 | event.SetExtraLong( 1 ); |
306 | ||
307 | event.SetInt(nIndex); | |
308 | event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry))); | |
dcf40a56 | 309 | |
6c8a980f | 310 | if ( listbox->HasClientObjectData() ) |
caf6e6de WS |
311 | event.SetClientObject( |
312 | (wxClientData*) gtk_tree_entry_get_userdata(entry) | |
4a46cbe8 | 313 | ); |
6c8a980f | 314 | else if ( listbox->HasClientUntypedData() ) |
4a46cbe8 | 315 | event.SetClientData( gtk_tree_entry_get_userdata(entry) ); |
09cf7c58 | 316 | |
c64371de | 317 | listbox->GetEventHandler()->ProcessEvent( event ); |
4a46cbe8 RR |
318 | |
319 | g_object_unref(G_OBJECT(entry)); | |
320 | return FALSE; //We handled it/did it manually | |
321 | } | |
322 | ||
323 | return TRUE; //allow selection to change | |
324 | } | |
6de97a3b | 325 | } |
c801d85f | 326 | |
4a46cbe8 RR |
327 | //----------------------------------------------------------------------------- |
328 | // GtkTreeEntry destruction (to destroy client data) | |
329 | //----------------------------------------------------------------------------- | |
330 | ||
865bb325 | 331 | extern "C" { |
caf6e6de | 332 | static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry, |
4a46cbe8 | 333 | wxListBox* listbox) |
865bb325 | 334 | { |
4a46cbe8 RR |
335 | if(listbox->HasClientObjectData()) |
336 | { | |
337 | gpointer userdata = gtk_tree_entry_get_userdata(entry); | |
338 | if(userdata) | |
339 | delete (wxClientData *)userdata; | |
340 | } | |
865bb325 VZ |
341 | } |
342 | } | |
343 | ||
4a46cbe8 RR |
344 | //----------------------------------------------------------------------------- |
345 | // Sorting callback (standard CmpNoCase return value) | |
346 | //----------------------------------------------------------------------------- | |
347 | ||
865bb325 | 348 | extern "C" { |
4a46cbe8 RR |
349 | static gint gtk_listbox_sort_callback(GtkTreeModel *model, |
350 | GtkTreeIter *a, | |
351 | GtkTreeIter *b, | |
352 | wxListBox *listbox) | |
865bb325 | 353 | { |
4a46cbe8 RR |
354 | GtkTreeEntry* entry; |
355 | GtkTreeEntry* entry2; | |
356 | ||
357 | gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore), | |
358 | a, | |
caf6e6de | 359 | WXLISTBOX_DATACOLUMN_ARG(listbox), |
4a46cbe8 RR |
360 | &entry, -1); |
361 | gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore), | |
362 | b, | |
caf6e6de | 363 | WXLISTBOX_DATACOLUMN_ARG(listbox), |
4a46cbe8 RR |
364 | &entry2, -1); |
365 | wxCHECK_MSG(entry, 0, wxT("Could not get entry")); | |
366 | wxCHECK_MSG(entry2, 0, wxT("Could not get entry2")); | |
367 | ||
368 | //We compare collate keys here instead of calling g_utf8_collate | |
369 | //as it is rather slow (and even the docs reccommend this) | |
370 | int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry), | |
371 | gtk_tree_entry_get_collate_key(entry2)); | |
372 | ||
373 | g_object_unref(G_OBJECT(entry)); | |
374 | g_object_unref(G_OBJECT(entry2)); | |
375 | ||
376 | return ret; | |
865bb325 VZ |
377 | } |
378 | } | |
379 | ||
a60c99e6 | 380 | //----------------------------------------------------------------------------- |
4a46cbe8 | 381 | // Searching callback (TRUE == not equal, FALSE == equal) |
c801d85f KB |
382 | //----------------------------------------------------------------------------- |
383 | ||
865bb325 | 384 | extern "C" { |
4a46cbe8 RR |
385 | static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model, |
386 | gint column, | |
387 | const gchar* key, | |
388 | GtkTreeIter* iter, | |
389 | wxListBox* listbox) | |
f2e93537 | 390 | { |
4a46cbe8 RR |
391 | GtkTreeEntry* entry; |
392 | ||
393 | gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore), | |
394 | iter, | |
caf6e6de | 395 | WXLISTBOX_DATACOLUMN_ARG(listbox), |
4a46cbe8 RR |
396 | &entry, -1); |
397 | wxCHECK_MSG(entry, 0, wxT("Could not get entry")); | |
398 | gchar* keycollatekey = g_utf8_collate_key(key, -1); | |
17a1ebd1 | 399 | |
4a46cbe8 RR |
400 | int ret = strcasecmp(keycollatekey, |
401 | gtk_tree_entry_get_collate_key(entry)); | |
17a1ebd1 | 402 | |
4a46cbe8 RR |
403 | g_free(keycollatekey); |
404 | g_object_unref(G_OBJECT(entry)); | |
405 | ||
406 | return ret != 0; | |
f2e93537 | 407 | } |
865bb325 | 408 | } |
f2e93537 RR |
409 | |
410 | //----------------------------------------------------------------------------- | |
411 | // wxListBox | |
412 | //----------------------------------------------------------------------------- | |
413 | ||
4a46cbe8 | 414 | IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl) |
c801d85f | 415 | |
2ee3ee1b VZ |
416 | // ---------------------------------------------------------------------------- |
417 | // construction | |
418 | // ---------------------------------------------------------------------------- | |
419 | ||
b6e5dfef | 420 | void wxListBox::Init() |
c801d85f | 421 | { |
4a46cbe8 | 422 | m_treeview = (GtkTreeView*) NULL; |
88ac883a | 423 | #if wxUSE_CHECKLISTBOX |
caf6e6de | 424 | m_hasCheckBoxes = false; |
88ac883a | 425 | #endif // wxUSE_CHECKLISTBOX |
4a46cbe8 | 426 | m_spacePressed = false; |
6de97a3b | 427 | } |
c801d85f | 428 | |
584ad2a3 MB |
429 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
430 | const wxPoint &pos, const wxSize &size, | |
431 | const wxArrayString& choices, | |
432 | long style, const wxValidator& validator, | |
433 | const wxString &name ) | |
434 | { | |
435 | wxCArrayString chs(choices); | |
436 | ||
437 | return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(), | |
438 | style, validator, name ); | |
439 | } | |
440 | ||
dcf40a56 | 441 | bool wxListBox::Create( wxWindow *parent, wxWindowID id, |
fd0eed64 RR |
442 | const wxPoint &pos, const wxSize &size, |
443 | int n, const wxString choices[], | |
2ee3ee1b VZ |
444 | long style, const wxValidator& validator, |
445 | const wxString &name ) | |
c801d85f | 446 | { |
caf6e6de WS |
447 | m_needParent = true; |
448 | m_acceptsFocus = true; | |
449 | m_blockEvent = false; | |
dcf40a56 | 450 | |
4dcaf11a RR |
451 | if (!PreCreation( parent, pos, size ) || |
452 | !CreateBase( parent, id, pos, size, style, validator, name )) | |
453 | { | |
223d09f6 | 454 | wxFAIL_MSG( wxT("wxListBox creation failed") ); |
caf6e6de | 455 | return false; |
4dcaf11a | 456 | } |
6de97a3b | 457 | |
fd0eed64 | 458 | m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL ); |
034be888 RR |
459 | if (style & wxLB_ALWAYS_SB) |
460 | { | |
461 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
462 | GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS ); | |
463 | } | |
464 | else | |
465 | { | |
466 | gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget), | |
467 | GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC ); | |
468 | } | |
ff8bfdbb | 469 | |
4a46cbe8 RR |
470 | m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) ); |
471 | ||
472 | //wxListBox doesn't have a header :) | |
473 | //NB: If enabled SetFirstItem doesn't work correctly | |
474 | gtk_tree_view_set_headers_visible(m_treeview, FALSE); | |
475 | ||
476 | #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST | |
477 | if(m_hasCheckBoxes) | |
478 | ((wxCheckListBox*)this)->DoCreateCheckList(); | |
479 | #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST | |
480 | ||
481 | // Create the data column | |
caf6e6de | 482 | gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "", |
4a46cbe8 | 483 | gtk_cell_renderer_text_new(), |
caf6e6de | 484 | "text", |
4a46cbe8 RR |
485 | WXLISTBOX_DATACOLUMN, NULL); |
486 | ||
487 | // Now create+set the model (GtkListStore) - first argument # of columns | |
4a46cbe8 RR |
488 | #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST |
489 | if(m_hasCheckBoxes) | |
490 | m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN, | |
491 | GTK_TYPE_TREE_ENTRY); | |
492 | else | |
493 | #endif | |
494 | m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY); | |
495 | ||
496 | gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore)); | |
caf6e6de | 497 | |
4a46cbe8 | 498 | g_object_unref(G_OBJECT(m_liststore)); //free on treeview destruction |
caf6e6de | 499 | |
4a46cbe8 RR |
500 | // Disable the pop-up textctrl that enables searching - note that |
501 | // the docs specify that even if this disabled (which we are doing) | |
502 | // the user can still have it through the start-interactive-search | |
503 | // key binding...either way we want to provide a searchequal callback | |
caf6e6de | 504 | // NB: If this is enabled a doubleclick event (activate) gets sent |
4a46cbe8 RR |
505 | // on a successful search |
506 | gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN); | |
caf6e6de | 507 | gtk_tree_view_set_search_equal_func(m_treeview, |
4a46cbe8 RR |
508 | (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback, |
509 | this, | |
510 | NULL); | |
511 | ||
512 | gtk_tree_view_set_enable_search(m_treeview, FALSE); | |
513 | ||
514 | ||
515 | GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview ); | |
caf6e6de | 516 | gtk_tree_selection_set_select_function(selection, |
4a46cbe8 | 517 | (GtkTreeSelectionFunc)gtk_listitem_select_cb, |
caf6e6de | 518 | this, NULL); //NULL == destroycb |
dcf40a56 | 519 | |
4a82abc8 | 520 | GtkSelectionMode mode; |
fd0eed64 | 521 | if (style & wxLB_MULTIPLE) |
4a82abc8 | 522 | { |
fd0eed64 | 523 | mode = GTK_SELECTION_MULTIPLE; |
4a82abc8 | 524 | } |
fd0eed64 | 525 | else if (style & wxLB_EXTENDED) |
4a82abc8 | 526 | { |
fd0eed64 | 527 | mode = GTK_SELECTION_EXTENDED; |
4a82abc8 RR |
528 | } |
529 | else | |
530 | { | |
531 | // if style was 0 set single mode | |
532 | m_windowStyle |= wxLB_SINGLE; | |
4fcfa27c | 533 | mode = GTK_SELECTION_SINGLE; |
4a82abc8 | 534 | } |
6a6d4eed | 535 | |
4a46cbe8 RR |
536 | gtk_tree_selection_set_mode( selection, mode ); |
537 | ||
caf6e6de | 538 | //Handle sortable stuff |
4a46cbe8 RR |
539 | if(style & wxLB_SORT) |
540 | { | |
541 | //Setup sorting in ascending (wx) order | |
caf6e6de WS |
542 | gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore), |
543 | WXLISTBOX_DATACOLUMN, | |
4a46cbe8 RR |
544 | GTK_SORT_ASCENDING); |
545 | ||
546 | //Set the sort callback | |
547 | gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore), | |
548 | WXLISTBOX_DATACOLUMN, | |
549 | (GtkTreeIterCompareFunc) gtk_listbox_sort_callback, | |
550 | this, //userdata | |
551 | NULL //"destroy notifier" | |
552 | ); | |
553 | } | |
554 | ||
dcf40a56 | 555 | |
dcd3c79c | 556 | gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) ); |
caf6e6de | 557 | |
4a46cbe8 | 558 | gtk_widget_show( GTK_WIDGET(m_treeview) ); |
dcf40a56 | 559 | |
4a46cbe8 | 560 | wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items |
17a1ebd1 | 561 | |
4a46cbe8 RR |
562 | //treeview-specific events |
563 | g_signal_connect(m_treeview, "row-activated", | |
564 | G_CALLBACK(gtk_listbox_row_activated_callback), this); | |
2ee3ee1b | 565 | |
4a46cbe8 RR |
566 | // other events |
567 | g_signal_connect (m_treeview, "button_press_event", | |
568 | G_CALLBACK (gtk_listbox_button_press_callback), | |
569 | this); | |
570 | g_signal_connect (m_treeview, "key_press_event", | |
571 | G_CALLBACK (gtk_listbox_key_press_callback), | |
572 | this); | |
dcf40a56 | 573 | |
f03fc89f | 574 | m_parent->DoAddChild( this ); |
ff8bfdbb | 575 | |
abdeb9e7 RD |
576 | PostCreation(size); |
577 | SetBestSize(size); // need this too because this is a wxControlWithItems | |
dcf40a56 | 578 | |
caf6e6de | 579 | return true; |
6de97a3b | 580 | } |
c801d85f | 581 | |
fd0eed64 | 582 | wxListBox::~wxListBox() |
c801d85f | 583 | { |
caf6e6de | 584 | m_hasVMT = false; |
4a82abc8 | 585 | |
caaa4cfd | 586 | Clear(); |
6de97a3b | 587 | } |
c801d85f | 588 | |
8161b5b9 VZ |
589 | // ---------------------------------------------------------------------------- |
590 | // adding items | |
591 | // ---------------------------------------------------------------------------- | |
592 | ||
caf6e6de | 593 | void wxListBox::GtkInsertItems(const wxArrayString& items, |
8228b893 | 594 | void** clientData, size_t pos) |
bb0ca8df | 595 | { |
4a46cbe8 | 596 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); |
2ee3ee1b | 597 | |
9f884528 RD |
598 | InvalidateBestSize(); |
599 | ||
4a46cbe8 | 600 | // Create and set column ids and GValues |
9ef6ff8c | 601 | |
4a46cbe8 | 602 | size_t nNum = items.GetCount(); |
8228b893 | 603 | size_t nCurCount = wxListBox::GetCount(); |
4a46cbe8 | 604 | wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox")); |
bb0ca8df | 605 | |
2eb1512a MR |
606 | GtkTreeIter* pIter = NULL; // append by default |
607 | GtkTreeIter iter; | |
608 | if (pos != nCurCount) | |
bb0ca8df | 609 | { |
4a46cbe8 RR |
610 | gboolean res = gtk_tree_model_iter_nth_child( |
611 | GTK_TREE_MODEL(m_liststore), | |
612 | &iter, NULL, //NULL = parent = get first | |
8228b893 | 613 | (int)pos ); |
4a46cbe8 | 614 | if(!res) |
0a07a7d8 | 615 | { |
4a46cbe8 | 616 | wxLogSysError(wxT("internal wxListBox error in insertion")); |
caf6e6de | 617 | return; |
0a07a7d8 | 618 | } |
bb0ca8df | 619 | |
4a46cbe8 | 620 | pIter = &iter; |
bb0ca8df | 621 | } |
2ee3ee1b | 622 | |
4a46cbe8 | 623 | for (size_t i = 0; i < nNum; ++i) |
2ee3ee1b | 624 | { |
4a46cbe8 | 625 | wxString label = items[i]; |
ff8bfdbb | 626 | |
4a46cbe8 | 627 | #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST |
8228b893 WS |
628 | if (m_hasCheckBoxes) |
629 | { | |
630 | label.Prepend(wxCHECKLBOX_STRING); | |
631 | } | |
88ac883a | 632 | #endif // wxUSE_CHECKLISTBOX |
fc54776e | 633 | |
9ef6ff8c | 634 | |
4a46cbe8 RR |
635 | GtkTreeEntry* entry = gtk_tree_entry_new(); |
636 | gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label)); | |
caf6e6de | 637 | gtk_tree_entry_set_destroy_func(entry, |
4a46cbe8 | 638 | (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb, |
9fa72bd2 | 639 | this); |
dcf40a56 | 640 | |
4a46cbe8 RR |
641 | if (clientData) |
642 | gtk_tree_entry_set_userdata(entry, clientData[i]); | |
9fa72bd2 | 643 | |
4a46cbe8 RR |
644 | GtkTreeIter itercur; |
645 | gtk_list_store_insert_before(m_liststore, &itercur, pIter); | |
c9433ea9 | 646 | |
4a46cbe8 RR |
647 | #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST |
648 | if (m_hasCheckBoxes) | |
649 | { | |
caf6e6de | 650 | gtk_list_store_set(m_liststore, &itercur, |
4a46cbe8 RR |
651 | 0, FALSE, //FALSE == not toggled |
652 | 1, entry, -1); | |
653 | } | |
654 | else | |
655 | #endif | |
caf6e6de | 656 | gtk_list_store_set(m_liststore, &itercur, |
4a46cbe8 | 657 | 0, entry, -1); |
fd0eed64 | 658 | |
4a46cbe8 RR |
659 | g_object_unref(G_OBJECT(entry)); //liststore always refs :) |
660 | } | |
661 | } | |
17a1ebd1 | 662 | |
4a46cbe8 RR |
663 | void wxListBox::DoInsertItems(const wxArrayString& items, int pos) |
664 | { | |
8228b893 WS |
665 | wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") ); |
666 | ||
667 | GtkInsertItems(items, NULL, (size_t)pos); | |
4a46cbe8 | 668 | } |
014b0d06 | 669 | |
4a46cbe8 RR |
670 | int wxListBox::DoAppend( const wxString& item ) |
671 | { | |
cdff1318 | 672 | // Call DoInsertItems |
8228b893 | 673 | int nWhere = (int)wxListBox::GetCount(); |
4a46cbe8 RR |
674 | wxArrayString aItems; |
675 | aItems.Add(item); | |
676 | wxListBox::DoInsertItems(aItems, nWhere); | |
677 | return nWhere; | |
fd0eed64 RR |
678 | } |
679 | ||
2ee3ee1b VZ |
680 | void wxListBox::DoSetItems( const wxArrayString& items, |
681 | void **clientData) | |
fd0eed64 | 682 | { |
2ee3ee1b | 683 | Clear(); |
4a46cbe8 | 684 | GtkInsertItems(items, clientData, 0); |
fd0eed64 | 685 | } |
dcf40a56 | 686 | |
2ee3ee1b | 687 | // ---------------------------------------------------------------------------- |
8161b5b9 | 688 | // deleting items |
2ee3ee1b | 689 | // ---------------------------------------------------------------------------- |
ff8bfdbb | 690 | |
fd0eed64 RR |
691 | void wxListBox::Clear() |
692 | { | |
4a46cbe8 | 693 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); |
fc54776e | 694 | |
cdff1318 RD |
695 | InvalidateBestSize(); |
696 | ||
4a46cbe8 | 697 | gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */ |
6de97a3b | 698 | } |
c801d85f KB |
699 | |
700 | void wxListBox::Delete( int n ) | |
701 | { | |
4a46cbe8 | 702 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); |
fc54776e | 703 | |
cdff1318 RD |
704 | InvalidateBestSize(); |
705 | ||
4a46cbe8 RR |
706 | GtkTreeIter iter; |
707 | gboolean res = gtk_tree_model_iter_nth_child( | |
708 | GTK_TREE_MODEL(m_liststore), | |
709 | &iter, NULL, //NULL = parent = get first | |
710 | n | |
711 | ); | |
dcf40a56 | 712 | |
4a46cbe8 | 713 | wxCHECK_RET( res, wxT("wrong listbox index") ); |
dcf40a56 | 714 | |
4a46cbe8 RR |
715 | //this returns false if iter is invalid (i.e. deleting item |
716 | //at end) but since we don't use iter, well... :) | |
717 | gtk_list_store_remove(m_liststore, &iter); | |
718 | } | |
dcf40a56 | 719 | |
4a46cbe8 RR |
720 | // ---------------------------------------------------------------------------- |
721 | // get GtkTreeEntry from position (note: you need to g_unref it if valid) | |
722 | // ---------------------------------------------------------------------------- | |
2ee3ee1b | 723 | |
4a46cbe8 RR |
724 | struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const |
725 | { | |
726 | GtkTreeIter iter; | |
727 | gboolean res = gtk_tree_model_iter_nth_child( | |
728 | GTK_TREE_MODEL(m_liststore), | |
729 | &iter, NULL, //NULL = parent = get first | |
730 | n ); | |
731 | ||
732 | if (!res) | |
733 | { | |
734 | wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n") | |
8228b893 | 735 | wxT("Passed in value was:[%i] List size:[%u]"), |
4a46cbe8 RR |
736 | n, wxListBox::GetCount() ); |
737 | return NULL; | |
f5e27805 | 738 | } |
ff8bfdbb | 739 | |
4a46cbe8 RR |
740 | |
741 | GtkTreeEntry* entry = NULL; | |
742 | gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter, | |
743 | WXLISTBOX_DATACOLUMN, &entry, -1); | |
744 | ||
745 | return entry; | |
2ee3ee1b VZ |
746 | } |
747 | ||
8161b5b9 VZ |
748 | // ---------------------------------------------------------------------------- |
749 | // client data | |
750 | // ---------------------------------------------------------------------------- | |
751 | ||
4a46cbe8 | 752 | void* wxListBox::DoGetItemClientData( int n ) const |
8161b5b9 | 753 | { |
8228b893 | 754 | wxCHECK_MSG( n >= 0 && (size_t)n < wxListBox::GetCount(), NULL, |
4a46cbe8 | 755 | wxT("Invalid index passed to GetItemClientData") ); |
8161b5b9 | 756 | |
4a46cbe8 RR |
757 | GtkTreeEntry* entry = GtkGetEntry(n); |
758 | wxCHECK_MSG(entry, NULL, wxT("could not get entry")); | |
8161b5b9 | 759 | |
4a46cbe8 RR |
760 | void* userdata = gtk_tree_entry_get_userdata( entry ); |
761 | g_object_unref(G_OBJECT(entry)); | |
762 | return userdata; | |
8161b5b9 VZ |
763 | } |
764 | ||
4a46cbe8 | 765 | wxClientData* wxListBox::DoGetItemClientObject( int n ) const |
8161b5b9 | 766 | { |
4a46cbe8 | 767 | return (wxClientData*) wxListBox::DoGetItemClientData(n); |
8161b5b9 VZ |
768 | } |
769 | ||
4a46cbe8 | 770 | void wxListBox::DoSetItemClientData( int n, void* clientData ) |
8161b5b9 | 771 | { |
8228b893 | 772 | wxCHECK_RET( n >= 0 && (size_t)n < wxListBox::GetCount(), |
4a46cbe8 | 773 | wxT("Invalid index passed to SetItemClientData") ); |
8161b5b9 | 774 | |
4a46cbe8 RR |
775 | GtkTreeEntry* entry = GtkGetEntry(n); |
776 | wxCHECK_RET(entry, wxT("could not get entry")); | |
8161b5b9 | 777 | |
4a46cbe8 RR |
778 | gtk_tree_entry_set_userdata( entry, clientData ); |
779 | g_object_unref(G_OBJECT(entry)); | |
8161b5b9 VZ |
780 | } |
781 | ||
4a46cbe8 | 782 | void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData ) |
8161b5b9 | 783 | { |
4a46cbe8 RR |
784 | // wxItemContainer already deletes data for us |
785 | wxListBox::DoSetItemClientData(n, (void*) clientData); | |
8161b5b9 VZ |
786 | } |
787 | ||
2ee3ee1b VZ |
788 | // ---------------------------------------------------------------------------- |
789 | // string list access | |
790 | // ---------------------------------------------------------------------------- | |
791 | ||
792 | void wxListBox::SetString( int n, const wxString &string ) | |
793 | { | |
8228b893 | 794 | wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") ); |
4a46cbe8 | 795 | wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") ); |
2ee3ee1b | 796 | |
4a46cbe8 RR |
797 | GtkTreeEntry* entry = GtkGetEntry(n); |
798 | wxCHECK_RET( entry, wxT("wrong listbox index") ); | |
2ee3ee1b | 799 | |
4a46cbe8 RR |
800 | wxString label = string; |
801 | ||
802 | #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST | |
8228b893 WS |
803 | if (m_hasCheckBoxes) |
804 | label.Prepend(wxCHECKLBOX_STRING); | |
2ee3ee1b | 805 | #endif // wxUSE_CHECKLISTBOX |
2ee3ee1b | 806 | |
caf6e6de | 807 | // RN: This may look wierd but the problem is that the TreeView |
4a46cbe8 RR |
808 | // doesn't resort or update when changed above and there is no real |
809 | // notification function... | |
810 | void* userdata = gtk_tree_entry_get_userdata(entry); | |
811 | gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy | |
812 | g_object_unref(G_OBJECT(entry)); | |
813 | ||
814 | bool bWasSelected = wxListBox::IsSelected(n); | |
815 | wxListBox::Delete(n); | |
816 | ||
817 | wxArrayString aItems; | |
818 | aItems.Add(label); | |
8228b893 | 819 | GtkInsertItems(aItems, &userdata, (size_t)n); |
4a46cbe8 | 820 | if (bWasSelected) |
caf6e6de | 821 | wxListBox::GtkSetSelection(n, true, true); |
6de97a3b | 822 | } |
c801d85f | 823 | |
2ee3ee1b | 824 | wxString wxListBox::GetString( int n ) const |
c801d85f | 825 | { |
4a46cbe8 | 826 | wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") ); |
fc54776e | 827 | |
4a46cbe8 RR |
828 | GtkTreeEntry* entry = GtkGetEntry(n); |
829 | wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") ); | |
830 | ||
831 | wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) ); | |
2ee3ee1b | 832 | |
4a46cbe8 RR |
833 | #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST |
834 |