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