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