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