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