]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
[ 1473731 ] 'wxColourBase and wxString <-> wxColour implementation' with minor modifi...
[wxWidgets.git] / src / gtk / listbox.cpp
CommitLineData
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
ad9835c9
WS
16#ifndef WX_PRECOMP
17 #include "wx/dynarray.h"
18#endif
19
2da2f941 20#include "wx/listbox.h"
2da2f941 21#include "wx/arrstr.h"
09cf7c58 22#include "wx/utils.h"
caaa4cfd
RR
23#include "wx/intl.h"
24#include "wx/checklst.h"
72a16063 25#include "wx/settings.h"
4a46cbe8 26#include "wx/log.h"
fab591c5 27#include "wx/gtk/private.h"
4a46cbe8 28#include "wx/gtk/treeentry_gtk.h"
291a8f20
RR
29
30#if wxUSE_TOOLTIPS
ad9835c9 31 #include "wx/tooltip.h"
291a8f20 32#endif
c801d85f 33
4a46cbe8
RR
34#include <gdk/gdk.h>
35#include <gtk/gtk.h>
16c1f79c 36#include <gdk/gdkkeysyms.h>
83624f79 37
66bd6b93
RR
38//-----------------------------------------------------------------------------
39// data
40//-----------------------------------------------------------------------------
41
d7fa7eaa
RR
42extern bool g_blockEventsOnDrag;
43extern bool g_blockEventsOnScroll;
44extern wxCursor g_globalCursor;
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 64extern "C" {
4a46cbe8
RR
65static void
66gtk_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) );
108 g_object_unref(G_OBJECT(entry));
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 154extern "C" {
7a30ee8f 155static gint
014b0d06
VZ
156gtk_listbox_button_press_callback( GtkWidget *widget,
157 GdkEventButton *gdk_event,
158 wxListBox *listbox )
7a30ee8f
RR
159{
160 if (g_isIdle) wxapp_install_idle_handler();
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 200extern "C" {
ff8bfdbb 201static gint
caf6e6de
WS
202gtk_listbox_key_press_callback( GtkWidget *widget,
203 GdkEventKey *gdk_event,
4a46cbe8 204 wxListBox *listbox )
1144d24d 205{
4a46cbe8
RR
206 if (g_isIdle) wxapp_install_idle_handler();
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
354aa1e3
RR
243 if (ret)
244 {
9fa72bd2 245 g_signal_stop_emission_by_name (widget, "key_press_event");
354aa1e3
RR
246 return TRUE;
247 }
ff8bfdbb 248
1144d24d
RR
249 return FALSE;
250}
865bb325 251}
1144d24d 252
c801d85f 253//-----------------------------------------------------------------------------
a60c99e6 254// "select" and "deselect"
c801d85f
KB
255//-----------------------------------------------------------------------------
256
4a46cbe8
RR
257extern "C" {
258static gboolean gtk_listitem_select_cb( GtkTreeSelection* selection,
259 GtkTreeModel* model,
260 GtkTreePath* path,
261 gboolean is_selected,
262 wxListBox *listbox )
c801d85f 263{
acfd422a
RR
264 if (g_isIdle) wxapp_install_idle_handler();
265
4a46cbe8
RR
266 if (!listbox->m_hasVMT) return TRUE;
267 if (g_blockEventsOnDrag) return TRUE;
8161b5b9 268
4a46cbe8
RR
269 if (listbox->m_spacePressed) return FALSE; //see keyevent callback
270 if (listbox->m_blockEvent) return TRUE;
9ef6ff8c 271
4a46cbe8
RR
272 // NB: wxdocs explicitly say that this event only gets sent when
273 // something is actually selected, plus the controls example
274 // assumes so and passes -1 to the dogetclientdata funcs if not
8161b5b9 275
4a46cbe8 276 // OK, so basically we need to do a bit of a run-around here as
caf6e6de
WS
277 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
278 // i.e. if is_selected is FALSE then the item is going to be
4a46cbe8 279 // selected right now!
caf6e6de 280 // 2) However, since it is not already selected and the user
4a46cbe8
RR
281 // will expect it to be we need to manually select it and
282 // return FALSE telling GTK we handled the selection
283 if (is_selected) return TRUE;
284
285 int nIndex = gtk_tree_path_get_indices(path)[0];
286 GtkTreeEntry* entry = listbox->GtkGetEntry(nIndex);
159b66c0 287
4a46cbe8 288 if(entry)
159b66c0 289 {
4a46cbe8
RR
290 //Now, as mentioned above, we manually select the row that is/was going
291 //to be selected anyway by GTK
caf6e6de 292 listbox->m_blockEvent = true; //if we don't block events we will lock the
4a46cbe8 293 //app due to recursion!!
159b66c0 294
caf6e6de 295 GtkTreeSelection* selection =
4a46cbe8
RR
296 gtk_tree_view_get_selection(listbox->m_treeview);
297 GtkTreeIter iter;
298 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox->m_liststore), &iter, path);
299 gtk_tree_selection_select_iter(selection, &iter);
159b66c0 300
caf6e6de 301 listbox->m_blockEvent = false;
4a46cbe8
RR
302
303 //Finally, send the wx event
c64371de
RD
304 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
305 event.SetEventObject( listbox );
4a46cbe8 306
c64371de 307 // indicate whether this is a selection or a deselection
4a46cbe8
RR
308 event.SetExtraLong( 1 );
309
310 event.SetInt(nIndex);
311 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
dcf40a56 312
6c8a980f 313 if ( listbox->HasClientObjectData() )
caf6e6de
WS
314 event.SetClientObject(
315 (wxClientData*) gtk_tree_entry_get_userdata(entry)
4a46cbe8 316 );
6c8a980f 317 else if ( listbox->HasClientUntypedData() )
4a46cbe8 318 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
09cf7c58 319
c64371de 320 listbox->GetEventHandler()->ProcessEvent( event );
4a46cbe8
RR
321
322 g_object_unref(G_OBJECT(entry));
323 return FALSE; //We handled it/did it manually
324 }
325
326 return TRUE; //allow selection to change
327}
6de97a3b 328}
c801d85f 329
4a46cbe8
RR
330//-----------------------------------------------------------------------------
331// GtkTreeEntry destruction (to destroy client data)
332//-----------------------------------------------------------------------------
333
865bb325 334extern "C" {
caf6e6de 335static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
4a46cbe8 336 wxListBox* listbox)
865bb325 337{
4a46cbe8
RR
338 if(listbox->HasClientObjectData())
339 {
340 gpointer userdata = gtk_tree_entry_get_userdata(entry);
341 if(userdata)
342 delete (wxClientData *)userdata;
343 }
865bb325
VZ
344}
345}
346
4a46cbe8
RR
347//-----------------------------------------------------------------------------
348// Sorting callback (standard CmpNoCase return value)
349//-----------------------------------------------------------------------------
350
865bb325 351extern "C" {
4a46cbe8
RR
352static gint gtk_listbox_sort_callback(GtkTreeModel *model,
353 GtkTreeIter *a,
354 GtkTreeIter *b,
355 wxListBox *listbox)
865bb325 356{
4a46cbe8
RR
357 GtkTreeEntry* entry;
358 GtkTreeEntry* entry2;
359
360 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
361 a,
caf6e6de 362 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
363 &entry, -1);
364 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
365 b,
caf6e6de 366 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
367 &entry2, -1);
368 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
369 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
370
371 //We compare collate keys here instead of calling g_utf8_collate
372 //as it is rather slow (and even the docs reccommend this)
373 int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry),
374 gtk_tree_entry_get_collate_key(entry2));
375
376 g_object_unref(G_OBJECT(entry));
377 g_object_unref(G_OBJECT(entry2));
378
379 return ret;
865bb325
VZ
380}
381}
382
a60c99e6 383//-----------------------------------------------------------------------------
4a46cbe8 384// Searching callback (TRUE == not equal, FALSE == equal)
c801d85f
KB
385//-----------------------------------------------------------------------------
386
865bb325 387extern "C" {
4a46cbe8
RR
388static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
389 gint column,
390 const gchar* key,
391 GtkTreeIter* iter,
392 wxListBox* listbox)
f2e93537 393{
4a46cbe8
RR
394 GtkTreeEntry* entry;
395
396 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
397 iter,
caf6e6de 398 WXLISTBOX_DATACOLUMN_ARG(listbox),
4a46cbe8
RR
399 &entry, -1);
400 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
401 gchar* keycollatekey = g_utf8_collate_key(key, -1);
17a1ebd1 402
4a46cbe8
RR
403 int ret = strcasecmp(keycollatekey,
404 gtk_tree_entry_get_collate_key(entry));
17a1ebd1 405
4a46cbe8
RR
406 g_free(keycollatekey);
407 g_object_unref(G_OBJECT(entry));
408
409 return ret != 0;
f2e93537 410}
865bb325 411}
f2e93537
RR
412
413//-----------------------------------------------------------------------------
414// wxListBox
415//-----------------------------------------------------------------------------
416
4a46cbe8 417IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
c801d85f 418
2ee3ee1b
VZ
419// ----------------------------------------------------------------------------
420// construction
421// ----------------------------------------------------------------------------
422
b6e5dfef 423void wxListBox::Init()
c801d85f 424{
4a46cbe8 425 m_treeview = (GtkTreeView*) NULL;
88ac883a 426#if wxUSE_CHECKLISTBOX
caf6e6de 427 m_hasCheckBoxes = false;
88ac883a 428#endif // wxUSE_CHECKLISTBOX
4a46cbe8 429 m_spacePressed = false;
6de97a3b 430}
c801d85f 431
584ad2a3
MB
432bool wxListBox::Create( wxWindow *parent, wxWindowID id,
433 const wxPoint &pos, const wxSize &size,
434 const wxArrayString& choices,
435 long style, const wxValidator& validator,
436 const wxString &name )
437{
438 wxCArrayString chs(choices);
439
440 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
441 style, validator, name );
442}
443
dcf40a56 444bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
445 const wxPoint &pos, const wxSize &size,
446 int n, const wxString choices[],
2ee3ee1b
VZ
447 long style, const wxValidator& validator,
448 const wxString &name )
c801d85f 449{
caf6e6de
WS
450 m_needParent = true;
451 m_acceptsFocus = true;
452 m_blockEvent = false;
dcf40a56 453
4dcaf11a
RR
454 if (!PreCreation( parent, pos, size ) ||
455 !CreateBase( parent, id, pos, size, style, validator, name ))
456 {
223d09f6 457 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 458 return false;
4dcaf11a 459 }
6de97a3b 460
fd0eed64 461 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
462 if (style & wxLB_ALWAYS_SB)
463 {
464 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
465 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
466 }
467 else
468 {
469 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
470 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
471 }
ff8bfdbb 472
6493aaca
VZ
473
474 GtkScrolledWindowSetBorder(m_widget, style);
475
4a46cbe8
RR
476 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
477
478 //wxListBox doesn't have a header :)
479 //NB: If enabled SetFirstItem doesn't work correctly
480 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
481
482#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
483 if(m_hasCheckBoxes)
484 ((wxCheckListBox*)this)->DoCreateCheckList();
485#endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
486
487 // Create the data column
caf6e6de 488 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 489 gtk_cell_renderer_text_new(),
caf6e6de 490 "text",
4a46cbe8
RR
491 WXLISTBOX_DATACOLUMN, NULL);
492
493 // Now create+set the model (GtkListStore) - first argument # of columns
4a46cbe8
RR
494#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
495 if(m_hasCheckBoxes)
496 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
497 GTK_TYPE_TREE_ENTRY);
498 else
499#endif
500 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
501
502 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 503
4a46cbe8 504 g_object_unref(G_OBJECT(m_liststore)); //free on treeview destruction
caf6e6de 505
4a46cbe8
RR
506 // Disable the pop-up textctrl that enables searching - note that
507 // the docs specify that even if this disabled (which we are doing)
508 // the user can still have it through the start-interactive-search
509 // key binding...either way we want to provide a searchequal callback
caf6e6de 510 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
511 // on a successful search
512 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 513 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
514 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
515 this,
516 NULL);
517
518 gtk_tree_view_set_enable_search(m_treeview, FALSE);
519
520
521 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
caf6e6de 522 gtk_tree_selection_set_select_function(selection,
4a46cbe8 523 (GtkTreeSelectionFunc)gtk_listitem_select_cb,
caf6e6de 524 this, NULL); //NULL == destroycb
dcf40a56 525
4a82abc8 526 GtkSelectionMode mode;
fd0eed64 527 if (style & wxLB_MULTIPLE)
4a82abc8 528 {
fd0eed64 529 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 530 }
fd0eed64 531 else if (style & wxLB_EXTENDED)
4a82abc8 532 {
fd0eed64 533 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
534 }
535 else
536 {
537 // if style was 0 set single mode
538 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 539 mode = GTK_SELECTION_SINGLE;
4a82abc8 540 }
6a6d4eed 541
4a46cbe8
RR
542 gtk_tree_selection_set_mode( selection, mode );
543
caf6e6de 544 //Handle sortable stuff
4a46cbe8
RR
545 if(style & wxLB_SORT)
546 {
547 //Setup sorting in ascending (wx) order
caf6e6de
WS
548 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
549 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
550 GTK_SORT_ASCENDING);
551
552 //Set the sort callback
553 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
554 WXLISTBOX_DATACOLUMN,
555 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
556 this, //userdata
557 NULL //"destroy notifier"
558 );
559 }
560
dcf40a56 561
dcd3c79c 562 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 563
4a46cbe8 564 gtk_widget_show( GTK_WIDGET(m_treeview) );
dcf40a56 565
4a46cbe8 566 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
17a1ebd1 567
4a46cbe8
RR
568 //treeview-specific events
569 g_signal_connect(m_treeview, "row-activated",
570 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 571
4a46cbe8
RR
572 // other events
573 g_signal_connect (m_treeview, "button_press_event",
574 G_CALLBACK (gtk_listbox_button_press_callback),
575 this);
576 g_signal_connect (m_treeview, "key_press_event",
577 G_CALLBACK (gtk_listbox_key_press_callback),
578 this);
dcf40a56 579
f03fc89f 580 m_parent->DoAddChild( this );
ff8bfdbb 581
abdeb9e7
RD
582 PostCreation(size);
583 SetBestSize(size); // need this too because this is a wxControlWithItems
dcf40a56 584
caf6e6de 585 return true;
6de97a3b 586}
c801d85f 587
fd0eed64 588wxListBox::~wxListBox()
c801d85f 589{
caf6e6de 590 m_hasVMT = false;
4a82abc8 591
caaa4cfd 592 Clear();
6de97a3b 593}
c801d85f 594
8161b5b9
VZ
595// ----------------------------------------------------------------------------
596// adding items
597// ----------------------------------------------------------------------------
598
caf6e6de 599void wxListBox::GtkInsertItems(const wxArrayString& items,
aa61d352 600 void** clientData, unsigned int pos)
bb0ca8df 601{
4a46cbe8 602 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 603
9f884528
RD
604 InvalidateBestSize();
605
4a46cbe8 606 // Create and set column ids and GValues
9ef6ff8c 607
aa61d352
VZ
608 unsigned int nNum = items.GetCount();
609 unsigned int nCurCount = wxListBox::GetCount();
4a46cbe8 610 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
bb0ca8df 611
2eb1512a
MR
612 GtkTreeIter* pIter = NULL; // append by default
613 GtkTreeIter iter;
614 if (pos != nCurCount)
bb0ca8df 615 {
4a46cbe8
RR
616 gboolean res = gtk_tree_model_iter_nth_child(
617 GTK_TREE_MODEL(m_liststore),
618 &iter, NULL, //NULL = parent = get first
8228b893 619 (int)pos );
4a46cbe8 620 if(!res)
0a07a7d8 621 {
4a46cbe8 622 wxLogSysError(wxT("internal wxListBox error in insertion"));
caf6e6de 623 return;
0a07a7d8 624 }
bb0ca8df 625
4a46cbe8 626 pIter = &iter;
bb0ca8df 627 }
2ee3ee1b 628
aa61d352 629 for (unsigned int i = 0; i < nNum; ++i)
2ee3ee1b 630 {
4a46cbe8 631 wxString label = items[i];
ff8bfdbb 632
4a46cbe8 633#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
8228b893
WS
634 if (m_hasCheckBoxes)
635 {
636 label.Prepend(wxCHECKLBOX_STRING);
637 }
88ac883a 638#endif // wxUSE_CHECKLISTBOX
fc54776e 639
9ef6ff8c 640
4a46cbe8
RR
641 GtkTreeEntry* entry = gtk_tree_entry_new();
642 gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
caf6e6de 643 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 644 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 645 this);
dcf40a56 646
4a46cbe8
RR
647 if (clientData)
648 gtk_tree_entry_set_userdata(entry, clientData[i]);
9fa72bd2 649
4a46cbe8
RR
650 GtkTreeIter itercur;
651 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 652
4a46cbe8
RR
653#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
654 if (m_hasCheckBoxes)
655 {
caf6e6de 656 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8
RR
657 0, FALSE, //FALSE == not toggled
658 1, entry, -1);
659 }
660 else
661#endif
caf6e6de 662 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8 663 0, entry, -1);
fd0eed64 664
4a46cbe8
RR
665 g_object_unref(G_OBJECT(entry)); //liststore always refs :)
666 }
667}
17a1ebd1 668
aa61d352 669void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
4a46cbe8 670{
8228b893
WS
671 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
672
aa61d352 673 GtkInsertItems(items, NULL, pos);
4a46cbe8 674}
014b0d06 675
4a46cbe8
RR
676int wxListBox::DoAppend( const wxString& item )
677{
cdff1318 678 // Call DoInsertItems
aa61d352 679 unsigned int nWhere = wxListBox::GetCount();
4a46cbe8
RR
680 wxArrayString aItems;
681 aItems.Add(item);
682 wxListBox::DoInsertItems(aItems, nWhere);
683 return nWhere;
fd0eed64
RR
684}
685
2ee3ee1b
VZ
686void wxListBox::DoSetItems( const wxArrayString& items,
687 void **clientData)
fd0eed64 688{
2ee3ee1b 689 Clear();
4a46cbe8 690 GtkInsertItems(items, clientData, 0);
fd0eed64 691}
dcf40a56 692
2ee3ee1b 693// ----------------------------------------------------------------------------
8161b5b9 694// deleting items
2ee3ee1b 695// ----------------------------------------------------------------------------
ff8bfdbb 696
fd0eed64
RR
697void wxListBox::Clear()
698{
4a46cbe8 699 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 700
cdff1318
RD
701 InvalidateBestSize();
702
4a46cbe8 703 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
6de97a3b 704}
c801d85f 705
aa61d352 706void wxListBox::Delete(unsigned int n)
c801d85f 707{
4a46cbe8 708 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
fc54776e 709
cdff1318
RD
710 InvalidateBestSize();
711
4a46cbe8
RR
712 GtkTreeIter iter;
713 gboolean res = gtk_tree_model_iter_nth_child(
714 GTK_TREE_MODEL(m_liststore),
715 &iter, NULL, //NULL = parent = get first
716 n
aa61d352 717 );
dcf40a56 718
4a46cbe8 719 wxCHECK_RET( res, wxT("wrong listbox index") );
dcf40a56 720
4a46cbe8
RR
721 //this returns false if iter is invalid (i.e. deleting item
722 //at end) but since we don't use iter, well... :)
723 gtk_list_store_remove(m_liststore, &iter);
724}
dcf40a56 725
4a46cbe8
RR
726// ----------------------------------------------------------------------------
727// get GtkTreeEntry from position (note: you need to g_unref it if valid)
728// ----------------------------------------------------------------------------
2ee3ee1b 729
4a46cbe8
RR
730struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
731{
732 GtkTreeIter iter;
733 gboolean res = gtk_tree_model_iter_nth_child(
734 GTK_TREE_MODEL(m_liststore),
735 &iter, NULL, //NULL = parent = get first
736 n );
737
738 if (!res)
739 {
740 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
8228b893 741 wxT("Passed in value was:[%i] List size:[%u]"),
4a46cbe8
RR
742 n, wxListBox::GetCount() );
743 return NULL;
f5e27805 744 }
ff8bfdbb 745
4a46cbe8
RR
746
747 GtkTreeEntry* entry = NULL;
748 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
749 WXLISTBOX_DATACOLUMN, &entry, -1);
750
751 return entry;
2ee3ee1b
VZ
752}
753
8161b5b9
VZ
754// ----------------------------------------------------------------------------
755// client data
756// ----------------------------------------------------------------------------
757
aa61d352 758void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 759{
aa61d352 760 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 761 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 762
4a46cbe8
RR
763 GtkTreeEntry* entry = GtkGetEntry(n);
764 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 765
4a46cbe8
RR
766 void* userdata = gtk_tree_entry_get_userdata( entry );
767 g_object_unref(G_OBJECT(entry));
768 return userdata;
8161b5b9
VZ
769}
770
aa61d352 771wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
8161b5b9 772{
4a46cbe8 773 return (wxClientData*) wxListBox::DoGetItemClientData(n);
8161b5b9
VZ
774}
775
aa61d352 776void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 777{
aa61d352 778 wxCHECK_RET( IsValid(n),
4a46cbe8 779 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 780
4a46cbe8
RR
781 GtkTreeEntry* entry = GtkGetEntry(n);
782 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 783
4a46cbe8
RR
784 gtk_tree_entry_set_userdata( entry, clientData );
785 g_object_unref(G_OBJECT(entry));
8161b5b9
VZ
786}
787
aa61d352 788void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
8161b5b9 789{
4a46cbe8
RR
790 // wxItemContainer already deletes data for us
791 wxListBox::DoSetItemClientData(n, (void*) clientData);
8161b5b9
VZ
792}
793
2ee3ee1b
VZ
794// ----------------------------------------------------------------------------
795// string list access
796// ----------------------------------------------------------------------------
797
aa61d352 798void wxListBox::SetString(unsigned int n, const wxString &string)
2ee3ee1b 799{
8228b893 800 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 801 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 802
4a46cbe8
RR
803 GtkTreeEntry* entry = GtkGetEntry(n);
804 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 805
4a46cbe8
RR
806 wxString label = string;
807
808#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
8228b893
WS
809 if (m_hasCheckBoxes)
810 label.Prepend(wxCHECKLBOX_STRING);
2ee3ee1b 811#endif // wxUSE_CHECKLISTBOX
2ee3ee1b 812
caf6e6de 813 // RN: This may look wierd but the problem is that the TreeView
4a46cbe8
RR
814 // doesn't resort or update when changed above and there is no real
815 // notification function...
816 void* userdata = gtk_tree_entry_get_userdata(entry);
817 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
818 g_object_unref(G_OBJECT(entry));
819
820 bool bWasSelected = wxListBox::IsSelected(n);
821 wxListBox::Delete(n);
822
823 wxArrayString aItems;
824 aItems.Add(label);
aa61d352 825 GtkInsertItems(aItems, &userdata, n);
4a46cbe8 826 if (bWasSelected)
caf6e6de 827 wxListBox::GtkSetSelection(n, true, true);
6de97a3b 828}
c801d85f 829
aa61d352 830wxString wxListBox::GetString(unsigned int n) const
c801d85f 831{
4a46cbe8 832 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 833
4a46cbe8
RR
834 GtkTreeEntry* entry = GtkGetEntry(n);
835 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
836
837 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 838
4a46cbe8
RR
839#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
840