]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/listbox.cpp
correct access for virtual
[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
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
43extern bool g_blockEventsOnDrag;
44extern 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 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) );
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 154extern "C" {
7a30ee8f 155static gint
014b0d06
VZ
156gtk_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 200extern "C" {
ff8bfdbb 201static gint
caf6e6de
WS
202gtk_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
251extern "C" {
252static 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 328extern "C" {
caf6e6de 329static 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 345extern "C" {
4a46cbe8
RR
346static 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 381extern "C" {
4a46cbe8
RR
382static 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"));
e808cf8a 395 wxGtkString 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
3fe39b0c 400 g_object_unref (entry);
4a46cbe8
RR
401
402 return ret != 0;
f2e93537 403}
865bb325 404}
f2e93537
RR
405
406//-----------------------------------------------------------------------------
407// wxListBox
408//-----------------------------------------------------------------------------
409
4a46cbe8 410IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
c801d85f 411
2ee3ee1b
VZ
412// ----------------------------------------------------------------------------
413// construction
414// ----------------------------------------------------------------------------
415
b6e5dfef 416void wxListBox::Init()
c801d85f 417{
4a46cbe8 418 m_treeview = (GtkTreeView*) NULL;
88ac883a 419#if wxUSE_CHECKLISTBOX
caf6e6de 420 m_hasCheckBoxes = false;
88ac883a 421#endif // wxUSE_CHECKLISTBOX
4a46cbe8 422 m_spacePressed = false;
6de97a3b 423}
c801d85f 424
584ad2a3
MB
425bool wxListBox::Create( wxWindow *parent, wxWindowID id,
426 const wxPoint &pos, const wxSize &size,
427 const wxArrayString& choices,
428 long style, const wxValidator& validator,
429 const wxString &name )
430{
431 wxCArrayString chs(choices);
432
433 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
434 style, validator, name );
435}
436
dcf40a56 437bool wxListBox::Create( wxWindow *parent, wxWindowID id,
fd0eed64
RR
438 const wxPoint &pos, const wxSize &size,
439 int n, const wxString choices[],
2ee3ee1b
VZ
440 long style, const wxValidator& validator,
441 const wxString &name )
c801d85f 442{
caf6e6de
WS
443 m_needParent = true;
444 m_acceptsFocus = true;
445 m_blockEvent = false;
dcf40a56 446
4dcaf11a
RR
447 if (!PreCreation( parent, pos, size ) ||
448 !CreateBase( parent, id, pos, size, style, validator, name ))
449 {
223d09f6 450 wxFAIL_MSG( wxT("wxListBox creation failed") );
caf6e6de 451 return false;
4dcaf11a 452 }
6de97a3b 453
fd0eed64 454 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
034be888
RR
455 if (style & wxLB_ALWAYS_SB)
456 {
457 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
458 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
459 }
460 else
461 {
462 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
463 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
464 }
ff8bfdbb 465
6493aaca
VZ
466
467 GtkScrolledWindowSetBorder(m_widget, style);
468
4a46cbe8
RR
469 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
470
471 //wxListBox doesn't have a header :)
472 //NB: If enabled SetFirstItem doesn't work correctly
473 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
474
475#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
476 if(m_hasCheckBoxes)
477 ((wxCheckListBox*)this)->DoCreateCheckList();
478#endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
479
480 // Create the data column
caf6e6de 481 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
4a46cbe8 482 gtk_cell_renderer_text_new(),
caf6e6de 483 "text",
4a46cbe8
RR
484 WXLISTBOX_DATACOLUMN, NULL);
485
486 // Now create+set the model (GtkListStore) - first argument # of columns
4a46cbe8
RR
487#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
488 if(m_hasCheckBoxes)
489 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
490 GTK_TYPE_TREE_ENTRY);
491 else
492#endif
493 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
494
495 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
caf6e6de 496
3fe39b0c 497 g_object_unref (m_liststore); //free on treeview destruction
caf6e6de 498
4a46cbe8
RR
499 // Disable the pop-up textctrl that enables searching - note that
500 // the docs specify that even if this disabled (which we are doing)
501 // the user can still have it through the start-interactive-search
502 // key binding...either way we want to provide a searchequal callback
caf6e6de 503 // NB: If this is enabled a doubleclick event (activate) gets sent
4a46cbe8
RR
504 // on a successful search
505 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
caf6e6de 506 gtk_tree_view_set_search_equal_func(m_treeview,
4a46cbe8
RR
507 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
508 this,
509 NULL);
510
511 gtk_tree_view_set_enable_search(m_treeview, FALSE);
512
513
514 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
caf6e6de 515 gtk_tree_selection_set_select_function(selection,
4a46cbe8 516 (GtkTreeSelectionFunc)gtk_listitem_select_cb,
caf6e6de 517 this, NULL); //NULL == destroycb
dcf40a56 518
4a82abc8 519 GtkSelectionMode mode;
fd0eed64 520 if (style & wxLB_MULTIPLE)
4a82abc8 521 {
fd0eed64 522 mode = GTK_SELECTION_MULTIPLE;
4a82abc8 523 }
fd0eed64 524 else if (style & wxLB_EXTENDED)
4a82abc8 525 {
fd0eed64 526 mode = GTK_SELECTION_EXTENDED;
4a82abc8
RR
527 }
528 else
529 {
530 // if style was 0 set single mode
531 m_windowStyle |= wxLB_SINGLE;
4fcfa27c 532 mode = GTK_SELECTION_SINGLE;
4a82abc8 533 }
6a6d4eed 534
4a46cbe8
RR
535 gtk_tree_selection_set_mode( selection, mode );
536
caf6e6de 537 //Handle sortable stuff
4a46cbe8
RR
538 if(style & wxLB_SORT)
539 {
540 //Setup sorting in ascending (wx) order
caf6e6de
WS
541 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
542 WXLISTBOX_DATACOLUMN,
4a46cbe8
RR
543 GTK_SORT_ASCENDING);
544
545 //Set the sort callback
546 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
547 WXLISTBOX_DATACOLUMN,
548 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
549 this, //userdata
550 NULL //"destroy notifier"
551 );
552 }
553
dcf40a56 554
dcd3c79c 555 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
caf6e6de 556
4a46cbe8 557 gtk_widget_show( GTK_WIDGET(m_treeview) );
0bb3fc29 558 m_focusWidget = GTK_WIDGET(m_treeview);
dcf40a56 559
4a46cbe8 560 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
17a1ebd1 561
4a46cbe8
RR
562 //treeview-specific events
563 g_signal_connect(m_treeview, "row-activated",
564 G_CALLBACK(gtk_listbox_row_activated_callback), this);
2ee3ee1b 565
4a46cbe8
RR
566 // other events
567 g_signal_connect (m_treeview, "button_press_event",
568 G_CALLBACK (gtk_listbox_button_press_callback),
569 this);
570 g_signal_connect (m_treeview, "key_press_event",
571 G_CALLBACK (gtk_listbox_key_press_callback),
572 this);
dcf40a56 573
f03fc89f 574 m_parent->DoAddChild( this );
ff8bfdbb 575
abdeb9e7
RD
576 PostCreation(size);
577 SetBestSize(size); // need this too because this is a wxControlWithItems
dcf40a56 578
caf6e6de 579 return true;
6de97a3b 580}
c801d85f 581
fd0eed64 582wxListBox::~wxListBox()
c801d85f 583{
caf6e6de 584 m_hasVMT = false;
4a82abc8 585
caaa4cfd 586 Clear();
6de97a3b 587}
c801d85f 588
8161b5b9
VZ
589// ----------------------------------------------------------------------------
590// adding items
591// ----------------------------------------------------------------------------
592
caf6e6de 593void wxListBox::GtkInsertItems(const wxArrayString& items,
aa61d352 594 void** clientData, unsigned int pos)
bb0ca8df 595{
4a46cbe8 596 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 597
9f884528
RD
598 InvalidateBestSize();
599
4a46cbe8 600 // Create and set column ids and GValues
9ef6ff8c 601
aa61d352
VZ
602 unsigned int nNum = items.GetCount();
603 unsigned int nCurCount = wxListBox::GetCount();
4a46cbe8 604 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
bb0ca8df 605
2eb1512a
MR
606 GtkTreeIter* pIter = NULL; // append by default
607 GtkTreeIter iter;
608 if (pos != nCurCount)
bb0ca8df 609 {
4a46cbe8
RR
610 gboolean res = gtk_tree_model_iter_nth_child(
611 GTK_TREE_MODEL(m_liststore),
612 &iter, NULL, //NULL = parent = get first
8228b893 613 (int)pos );
4a46cbe8 614 if(!res)
0a07a7d8 615 {
4a46cbe8 616 wxLogSysError(wxT("internal wxListBox error in insertion"));
caf6e6de 617 return;
0a07a7d8 618 }
bb0ca8df 619
4a46cbe8 620 pIter = &iter;
bb0ca8df 621 }
2ee3ee1b 622
aa61d352 623 for (unsigned int i = 0; i < nNum; ++i)
2ee3ee1b 624 {
4a46cbe8 625 wxString label = items[i];
ff8bfdbb 626
4a46cbe8 627#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
8228b893
WS
628 if (m_hasCheckBoxes)
629 {
630 label.Prepend(wxCHECKLBOX_STRING);
631 }
88ac883a 632#endif // wxUSE_CHECKLISTBOX
fc54776e 633
9ef6ff8c 634
4a46cbe8
RR
635 GtkTreeEntry* entry = gtk_tree_entry_new();
636 gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
caf6e6de 637 gtk_tree_entry_set_destroy_func(entry,
4a46cbe8 638 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
9fa72bd2 639 this);
dcf40a56 640
4a46cbe8
RR
641 if (clientData)
642 gtk_tree_entry_set_userdata(entry, clientData[i]);
9fa72bd2 643
4a46cbe8
RR
644 GtkTreeIter itercur;
645 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
c9433ea9 646
4a46cbe8
RR
647#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
648 if (m_hasCheckBoxes)
649 {
caf6e6de 650 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8
RR
651 0, FALSE, //FALSE == not toggled
652 1, entry, -1);
653 }
654 else
655#endif
caf6e6de 656 gtk_list_store_set(m_liststore, &itercur,
4a46cbe8 657 0, entry, -1);
fd0eed64 658
3fe39b0c 659 g_object_unref (entry); //liststore always refs :)
4a46cbe8
RR
660 }
661}
17a1ebd1 662
aa61d352 663void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
4a46cbe8 664{
8228b893
WS
665 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
666
aa61d352 667 GtkInsertItems(items, NULL, pos);
4a46cbe8 668}
014b0d06 669
4a46cbe8
RR
670int wxListBox::DoAppend( const wxString& item )
671{
cdff1318 672 // Call DoInsertItems
aa61d352 673 unsigned int nWhere = wxListBox::GetCount();
4a46cbe8
RR
674 wxArrayString aItems;
675 aItems.Add(item);
676 wxListBox::DoInsertItems(aItems, nWhere);
677 return nWhere;
fd0eed64
RR
678}
679
2ee3ee1b
VZ
680void 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
691void 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 699
aa61d352 700void wxListBox::Delete(unsigned int n)
c801d85f 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
aa61d352 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
724struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
725{
726 GtkTreeIter iter;
727 gboolean res = gtk_tree_model_iter_nth_child(
728 GTK_TREE_MODEL(m_liststore),
729 &iter, NULL, //NULL = parent = get first
730 n );
731
732 if (!res)
733 {
734 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
8228b893 735 wxT("Passed in value was:[%i] List size:[%u]"),
4a46cbe8
RR
736 n, wxListBox::GetCount() );
737 return NULL;
f5e27805 738 }
ff8bfdbb 739
4a46cbe8
RR
740
741 GtkTreeEntry* entry = NULL;
742 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
743 WXLISTBOX_DATACOLUMN, &entry, -1);
744
745 return entry;
2ee3ee1b
VZ
746}
747
8161b5b9
VZ
748// ----------------------------------------------------------------------------
749// client data
750// ----------------------------------------------------------------------------
751
aa61d352 752void* wxListBox::DoGetItemClientData(unsigned int n) const
8161b5b9 753{
aa61d352 754 wxCHECK_MSG( IsValid(n), NULL,
4a46cbe8 755 wxT("Invalid index passed to GetItemClientData") );
8161b5b9 756
4a46cbe8
RR
757 GtkTreeEntry* entry = GtkGetEntry(n);
758 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
8161b5b9 759
4a46cbe8 760 void* userdata = gtk_tree_entry_get_userdata( entry );
3fe39b0c 761 g_object_unref (entry);
4a46cbe8 762 return userdata;
8161b5b9
VZ
763}
764
aa61d352 765wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
8161b5b9 766{
4a46cbe8 767 return (wxClientData*) wxListBox::DoGetItemClientData(n);
8161b5b9
VZ
768}
769
aa61d352 770void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
8161b5b9 771{
aa61d352 772 wxCHECK_RET( IsValid(n),
4a46cbe8 773 wxT("Invalid index passed to SetItemClientData") );
8161b5b9 774
4a46cbe8
RR
775 GtkTreeEntry* entry = GtkGetEntry(n);
776 wxCHECK_RET(entry, wxT("could not get entry"));
8161b5b9 777
4a46cbe8 778 gtk_tree_entry_set_userdata( entry, clientData );
3fe39b0c 779 g_object_unref (entry);
8161b5b9
VZ
780}
781
aa61d352 782void wxListBox::DoSetItemClientObject(unsigned 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
aa61d352 792void wxListBox::SetString(unsigned int n, const wxString &string)
2ee3ee1b 793{
8228b893 794 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
4a46cbe8 795 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
2ee3ee1b 796
4a46cbe8
RR
797 GtkTreeEntry* entry = GtkGetEntry(n);
798 wxCHECK_RET( entry, wxT("wrong listbox index") );
2ee3ee1b 799
4a46cbe8
RR
800 wxString label = string;
801
802#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
8228b893
WS
803 if (m_hasCheckBoxes)
804 label.Prepend(wxCHECKLBOX_STRING);
2ee3ee1b 805#endif // wxUSE_CHECKLISTBOX
2ee3ee1b 806
caf6e6de 807 // RN: This may look wierd but the problem is that the TreeView
4a46cbe8
RR
808 // doesn't resort or update when changed above and there is no real
809 // notification function...
810 void* userdata = gtk_tree_entry_get_userdata(entry);
811 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
3fe39b0c 812 g_object_unref (entry);
4a46cbe8
RR
813
814 bool bWasSelected = wxListBox::IsSelected(n);
815 wxListBox::Delete(n);
816
817 wxArrayString aItems;
818 aItems.Add(label);
aa61d352 819 GtkInsertItems(aItems, &userdata, n);
4a46cbe8 820 if (bWasSelected)
caf6e6de 821 wxListBox::GtkSetSelection(n, true, true);
6de97a3b 822}
c801d85f 823
aa61d352 824wxString wxListBox::GetString(unsigned int n) const
c801d85f 825{
4a46cbe8 826 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
fc54776e 827
4a46cbe8
RR
828 GtkTreeEntry* entry = GtkGetEntry(n);
829 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
830
831 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
2ee3ee1b 832
4a46cbe8
RR
833#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
834