Use GTK+ function to obtain GdkWindow for wxListBox.
[wxWidgets.git] / src / gtk / listbox.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/listbox.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified By: Ryan Norton (GtkTreeView implementation)
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/defs.h"
15
16 #if wxUSE_LISTBOX
17
18 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
20 #include "wx/arrstr.h"
21 #include "wx/utils.h"
22 #include "wx/intl.h"
23 #include "wx/checklst.h"
24 #include "wx/settings.h"
25 #include "wx/log.h"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/treeentry_gtk.h"
28
29 #if wxUSE_TOOLTIPS
30 #include "wx/tooltip.h"
31 #endif
32
33 #include <gdk/gdk.h>
34 #include <gtk/gtk.h>
35 #include <gdk/gdkkeysyms.h>
36
37 //-----------------------------------------------------------------------------
38 // data
39 //-----------------------------------------------------------------------------
40
41 extern bool g_blockEventsOnDrag;
42 extern bool g_blockEventsOnScroll;
43 extern wxCursor g_globalCursor;
44
45
46 //-----------------------------------------------------------------------------
47 // idle system
48 //-----------------------------------------------------------------------------
49
50 extern void wxapp_install_idle_handler();
51 extern bool g_isIdle;
52
53 //-----------------------------------------------------------------------------
54 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
55 //-----------------------------------------------------------------------------
56
57 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
58 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
59 #else
60 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
61 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
62
63 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
64
65 //-----------------------------------------------------------------------------
66 // "row-activated"
67 //-----------------------------------------------------------------------------
68
69 extern "C" {
70 static void
71 gtk_listbox_row_activated_callback(GtkTreeView *treeview,
72 GtkTreePath *path,
73 GtkTreeViewColumn *col,
74 wxListBox *listbox)
75 {
76 if (g_isIdle) wxapp_install_idle_handler();
77
78 if (g_blockEventsOnDrag) return;
79 if (g_blockEventsOnScroll) return;
80
81 if (!listbox->m_hasVMT) return;
82
83 //Notes:
84 //1) This is triggered by either a double-click or a space press
85 //2) We handle both here because
86 //2a) in the case of a space/keypress we can't really know
87 // which item was pressed on because we can't get coords
88 // from a keyevent
89 //2b) It seems more correct
90
91 int sel = gtk_tree_path_get_indices(path)[0];
92
93 if(!listbox->m_spacePressed)
94 {
95 //Assume it was double-click
96 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
97 event.SetEventObject( listbox );
98
99 if(listbox->IsSelected(sel))
100 {
101 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
102
103 if(entry)
104 {
105 event.SetInt(sel);
106 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
107
108 if ( listbox->HasClientObjectData() )
109 event.SetClientObject(
110 (wxClientData*) gtk_tree_entry_get_userdata(entry) );
111 else if ( listbox->HasClientUntypedData() )
112 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
113 g_object_unref(G_OBJECT(entry));
114 }
115 else
116 {
117 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
118 event.SetInt(-1);
119 }
120 }
121 else
122 event.SetInt(-1);
123
124 listbox->GetEventHandler()->ProcessEvent( event );
125 }
126 else
127 {
128 listbox->m_spacePressed = false; //don't block selection behaviour anymore
129
130 //Space was pressed - toggle the appropriate checkbox and the selection
131 #if wxUSE_CHECKLISTBOX //Do it for both native and non-native
132 if (listbox->m_hasCheckBoxes)
133 {
134 wxCheckListBox *clb = (wxCheckListBox *)listbox;
135
136 clb->Check( sel, !clb->IsChecked(sel) );
137
138 wxCommandEvent new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
139 new_event.SetEventObject( listbox );
140 new_event.SetInt( sel );
141 listbox->GetEventHandler()->ProcessEvent( new_event );
142 }
143 #endif // wxUSE_CHECKLISTBOX
144
145 if( (((listbox->GetWindowStyleFlag() & wxLB_MULTIPLE) != 0) ||
146 ((listbox->GetWindowStyleFlag() & wxLB_EXTENDED) != 0)) )
147 {
148 //toggle the selection + send event
149 if(listbox->IsSelected( sel ))
150 listbox->GtkSetSelection(sel, FALSE, FALSE);
151 else
152 listbox->GtkSetSelection(sel, TRUE, FALSE);
153 }
154 }
155 }
156 }
157
158 //-----------------------------------------------------------------------------
159 // "button_press_event"
160 //-----------------------------------------------------------------------------
161
162 extern "C" {
163 static gint
164 gtk_listbox_button_press_callback( GtkWidget *widget,
165 GdkEventButton *gdk_event,
166 wxListBox *listbox )
167 {
168 if (g_isIdle) wxapp_install_idle_handler();
169
170 if (g_blockEventsOnDrag) return FALSE;
171 if (g_blockEventsOnScroll) return FALSE;
172
173 if (!listbox->m_hasVMT) return FALSE;
174
175 //Just to be on the safe side - it should be unset in the activate callback
176 //but we don't want any obscure bugs if it doesn't get called somehow...
177 listbox->m_spacePressed = false;
178
179 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
180 if ((listbox->m_hasCheckBoxes) && (gdk_event->x < 15) && (gdk_event->type != GDK_2BUTTON_PRESS))
181 {
182 GtkTreePath* path;
183 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget),
184 (gint)gdk_event->x, (gint)gdk_event->y,
185 &path, NULL, NULL, NULL);
186 int sel = gtk_tree_path_get_indices(path)[0];
187 gtk_tree_path_free(path);
188
189 wxCheckListBox *clb = (wxCheckListBox *)listbox;
190
191 clb->Check( sel, !clb->IsChecked(sel) );
192
193 wxCommandEvent event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED, listbox->GetId() );
194 event.SetEventObject( listbox );
195 event.SetInt( sel );
196 listbox->GetEventHandler()->ProcessEvent( event );
197 }
198 #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
199
200 return FALSE;
201 }
202 }
203
204 //-----------------------------------------------------------------------------
205 // "key_press_event"
206 //-----------------------------------------------------------------------------
207
208 extern "C" {
209 static gint
210 gtk_listbox_key_press_callback( GtkWidget *widget,
211 GdkEventKey *gdk_event,
212 wxListBox *listbox )
213 {
214 if (g_isIdle) wxapp_install_idle_handler();
215
216 if (g_blockEventsOnDrag) return FALSE;
217
218
219 bool ret = FALSE;
220
221 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
222 {
223 wxNavigationKeyEvent new_event;
224 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
225 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
226 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
227 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
228 new_event.SetCurrentFocus( listbox );
229 ret = listbox->GetEventHandler()->ProcessEvent( new_event );
230 }
231
232 if ((gdk_event->keyval == GDK_Return) && (!ret))
233 {
234 // eat return in all modes (RN:WHY?)
235 ret = TRUE;
236 }
237
238 // Check or uncheck item with SPACE
239 if (gdk_event->keyval == ' ')
240 {
241 //In the keyevent we don't know the index of the item
242 //and the activated event gets called anyway...
243 //
244 //Also, activating with the space causes the treeview to
245 //unselect all the items and then select the item in question
246 //wx's behaviour is to just toggle the item's selection state
247 //and leave the others alone
248 listbox->m_spacePressed = true;
249 }
250
251 if (ret)
252 {
253 g_signal_stop_emission_by_name (widget, "key_press_event");
254 return TRUE;
255 }
256
257 return FALSE;
258 }
259 }
260
261 //-----------------------------------------------------------------------------
262 // "select" and "deselect"
263 //-----------------------------------------------------------------------------
264
265 extern "C" {
266 static gboolean gtk_listitem_select_cb( GtkTreeSelection* selection,
267 GtkTreeModel* model,
268 GtkTreePath* path,
269 gboolean is_selected,
270 wxListBox *listbox )
271 {
272 if (g_isIdle) wxapp_install_idle_handler();
273
274 if (!listbox->m_hasVMT) return TRUE;
275 if (g_blockEventsOnDrag) return TRUE;
276
277 if (listbox->m_spacePressed) return FALSE; //see keyevent callback
278 if (listbox->m_blockEvent) return TRUE;
279
280 // NB: wxdocs explicitly say that this event only gets sent when
281 // something is actually selected, plus the controls example
282 // assumes so and passes -1 to the dogetclientdata funcs if not
283
284 // OK, so basically we need to do a bit of a run-around here as
285 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
286 // i.e. if is_selected is FALSE then the item is going to be
287 // selected right now!
288 // 2) However, since it is not already selected and the user
289 // will expect it to be we need to manually select it and
290 // return FALSE telling GTK we handled the selection
291 if (is_selected) return TRUE;
292
293 int nIndex = gtk_tree_path_get_indices(path)[0];
294 GtkTreeEntry* entry = listbox->GtkGetEntry(nIndex);
295
296 if(entry)
297 {
298 //Now, as mentioned above, we manually select the row that is/was going
299 //to be selected anyway by GTK
300 listbox->m_blockEvent = TRUE; //if we don't block events we will lock the
301 //app due to recursion!!
302
303 GtkTreeSelection* selection =
304 gtk_tree_view_get_selection(listbox->m_treeview);
305 GtkTreeIter iter;
306 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox->m_liststore), &iter, path);
307 gtk_tree_selection_select_iter(selection, &iter);
308
309 listbox->m_blockEvent = FALSE;
310
311 //Finally, send the wx event
312 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
313 event.SetEventObject( listbox );
314
315 // indicate whether this is a selection or a deselection
316 event.SetExtraLong( 1 );
317
318 event.SetInt(nIndex);
319 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
320
321 if ( listbox->HasClientObjectData() )
322 event.SetClientObject(
323 (wxClientData*) gtk_tree_entry_get_userdata(entry)
324 );
325 else if ( listbox->HasClientUntypedData() )
326 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
327
328 listbox->GetEventHandler()->ProcessEvent( event );
329
330 g_object_unref(G_OBJECT(entry));
331 return FALSE; //We handled it/did it manually
332 }
333
334 return TRUE; //allow selection to change
335 }
336 }
337
338 //-----------------------------------------------------------------------------
339 // GtkTreeEntry destruction (to destroy client data)
340 //-----------------------------------------------------------------------------
341
342 extern "C" {
343 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
344 wxListBox* listbox)
345 {
346 if(listbox->HasClientObjectData())
347 {
348 gpointer userdata = gtk_tree_entry_get_userdata(entry);
349 if(userdata)
350 delete (wxClientData *)userdata;
351 }
352 }
353 }
354
355 //-----------------------------------------------------------------------------
356 // Sorting callback (standard CmpNoCase return value)
357 //-----------------------------------------------------------------------------
358
359 extern "C" {
360 static gint gtk_listbox_sort_callback(GtkTreeModel *model,
361 GtkTreeIter *a,
362 GtkTreeIter *b,
363 wxListBox *listbox)
364 {
365 GtkTreeEntry* entry;
366 GtkTreeEntry* entry2;
367
368 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
369 a,
370 WXLISTBOX_DATACOLUMN_ARG(listbox),
371 &entry, -1);
372 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
373 b,
374 WXLISTBOX_DATACOLUMN_ARG(listbox),
375 &entry2, -1);
376 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
377 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
378
379 //We compare collate keys here instead of calling g_utf8_collate
380 //as it is rather slow (and even the docs reccommend this)
381 int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry),
382 gtk_tree_entry_get_collate_key(entry2));
383
384 g_object_unref(G_OBJECT(entry));
385 g_object_unref(G_OBJECT(entry2));
386
387 return ret;
388 }
389 }
390
391 //-----------------------------------------------------------------------------
392 // Searching callback (TRUE == not equal, FALSE == equal)
393 //-----------------------------------------------------------------------------
394
395 extern "C" {
396 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
397 gint column,
398 const gchar* key,
399 GtkTreeIter* iter,
400 wxListBox* listbox)
401 {
402 GtkTreeEntry* entry;
403
404 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
405 iter,
406 WXLISTBOX_DATACOLUMN_ARG(listbox),
407 &entry, -1);
408 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
409 gchar* keycollatekey = g_utf8_collate_key(key, -1);
410
411 int ret = strcasecmp(keycollatekey,
412 gtk_tree_entry_get_collate_key(entry));
413
414 g_free(keycollatekey);
415 g_object_unref(G_OBJECT(entry));
416
417 return ret != 0;
418 }
419 }
420
421 //-----------------------------------------------------------------------------
422 // wxListBox
423 //-----------------------------------------------------------------------------
424
425 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
426
427 // ----------------------------------------------------------------------------
428 // construction
429 // ----------------------------------------------------------------------------
430
431 wxListBox::wxListBox()
432 {
433 m_treeview = (GtkTreeView*) NULL;
434 #if wxUSE_CHECKLISTBOX
435 m_hasCheckBoxes = FALSE;
436 #endif // wxUSE_CHECKLISTBOX
437 m_spacePressed = false;
438 }
439
440 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
441 const wxPoint &pos, const wxSize &size,
442 const wxArrayString& choices,
443 long style, const wxValidator& validator,
444 const wxString &name )
445 {
446 wxCArrayString chs(choices);
447
448 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
449 style, validator, name );
450 }
451
452 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
453 const wxPoint &pos, const wxSize &size,
454 int n, const wxString choices[],
455 long style, const wxValidator& validator,
456 const wxString &name )
457 {
458 m_needParent = TRUE;
459 m_acceptsFocus = TRUE;
460 m_blockEvent = FALSE;
461
462 if (!PreCreation( parent, pos, size ) ||
463 !CreateBase( parent, id, pos, size, style, validator, name ))
464 {
465 wxFAIL_MSG( wxT("wxListBox creation failed") );
466 return FALSE;
467 }
468
469 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
470 if (style & wxLB_ALWAYS_SB)
471 {
472 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
473 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
474 }
475 else
476 {
477 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
478 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
479 }
480
481 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
482
483 //wxListBox doesn't have a header :)
484 //NB: If enabled SetFirstItem doesn't work correctly
485 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
486
487 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
488 if(m_hasCheckBoxes)
489 ((wxCheckListBox*)this)->DoCreateCheckList();
490 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
491
492 // Create the data column
493 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
494 gtk_cell_renderer_text_new(),
495 "text",
496 WXLISTBOX_DATACOLUMN, NULL);
497
498 // Now create+set the model (GtkListStore) - first argument # of columns
499
500 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
501 if(m_hasCheckBoxes)
502 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
503 GTK_TYPE_TREE_ENTRY);
504 else
505 #endif
506 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
507
508 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
509
510 g_object_unref(G_OBJECT(m_liststore)); //free on treeview destruction
511
512 // Disable the pop-up textctrl that enables searching - note that
513 // the docs specify that even if this disabled (which we are doing)
514 // the user can still have it through the start-interactive-search
515 // key binding...either way we want to provide a searchequal callback
516 // NB: If this is enabled a doubleclick event (activate) gets sent
517 // on a successful search
518 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
519 gtk_tree_view_set_search_equal_func(m_treeview,
520 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
521 this,
522 NULL);
523
524 gtk_tree_view_set_enable_search(m_treeview, FALSE);
525
526
527 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
528 gtk_tree_selection_set_select_function(selection,
529 (GtkTreeSelectionFunc)gtk_listitem_select_cb,
530 this, NULL); //NULL == destroycb
531
532 GtkSelectionMode mode;
533 if (style & wxLB_MULTIPLE)
534 {
535 mode = GTK_SELECTION_MULTIPLE;
536 }
537 else if (style & wxLB_EXTENDED)
538 {
539 mode = GTK_SELECTION_EXTENDED;
540 }
541 else
542 {
543 // if style was 0 set single mode
544 m_windowStyle |= wxLB_SINGLE;
545 mode = GTK_SELECTION_SINGLE;
546 }
547
548 gtk_tree_selection_set_mode( selection, mode );
549
550 //Handle sortable stuff
551 if(style & wxLB_SORT)
552 {
553 //Setup sorting in ascending (wx) order
554 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
555 WXLISTBOX_DATACOLUMN,
556 GTK_SORT_ASCENDING);
557
558 //Set the sort callback
559 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
560 WXLISTBOX_DATACOLUMN,
561 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
562 this, //userdata
563 NULL //"destroy notifier"
564 );
565 }
566
567
568 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
569
570 gtk_widget_show( GTK_WIDGET(m_treeview) );
571
572 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
573
574 //treeview-specific events
575 g_signal_connect(m_treeview, "row-activated",
576 G_CALLBACK(gtk_listbox_row_activated_callback), this);
577
578 // other events
579 g_signal_connect (m_treeview, "button_press_event",
580 G_CALLBACK (gtk_listbox_button_press_callback),
581 this);
582 g_signal_connect (m_treeview, "key_press_event",
583 G_CALLBACK (gtk_listbox_key_press_callback),
584 this);
585
586 m_parent->DoAddChild( this );
587
588 PostCreation(size);
589 SetBestSize(size); // need this too because this is a wxControlWithItems
590
591 return TRUE;
592 }
593
594 wxListBox::~wxListBox()
595 {
596 m_hasVMT = FALSE;
597
598 Clear();
599 }
600
601 // ----------------------------------------------------------------------------
602 // adding items
603 // ----------------------------------------------------------------------------
604
605 void wxListBox::GtkInsertItems(const wxArrayString& items,
606 void** clientData, int pos)
607 {
608 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
609
610 InvalidateBestSize();
611
612 // Create and set column ids and GValues
613
614 size_t nNum = items.GetCount();
615 int nCurCount = wxListBox::GetCount();
616 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
617
618 GtkTreeIter* pIter;
619 if (pos == nCurCount)
620 {
621 pIter = NULL; // append
622 }
623 else
624 {
625 GtkTreeIter iter;
626 gboolean res = gtk_tree_model_iter_nth_child(
627 GTK_TREE_MODEL(m_liststore),
628 &iter, NULL, //NULL = parent = get first
629 pos );
630 if(!res)
631 {
632 wxLogSysError(wxT("internal wxListBox error in insertion"));
633 return;
634 }
635
636 pIter = &iter;
637 }
638
639 for (size_t i = 0; i < nNum; ++i)
640 {
641 wxString label = items[i];
642
643 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
644 if (m_hasCheckBoxes)
645 {
646 label.Prepend(wxCHECKLBOX_STRING);
647 }
648 #endif // wxUSE_CHECKLISTBOX
649
650
651 GtkTreeEntry* entry = gtk_tree_entry_new();
652 gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
653 gtk_tree_entry_set_destroy_func(entry,
654 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
655 this);
656
657 if (clientData)
658 gtk_tree_entry_set_userdata(entry, clientData[i]);
659
660 GtkTreeIter itercur;
661 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
662
663 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
664 if (m_hasCheckBoxes)
665 {
666 gtk_list_store_set(m_liststore, &itercur,
667 0, FALSE, //FALSE == not toggled
668 1, entry, -1);
669 }
670 else
671 #endif
672 gtk_list_store_set(m_liststore, &itercur,
673 0, entry, -1);
674
675 g_object_unref(G_OBJECT(entry)); //liststore always refs :)
676 }
677 }
678
679 void wxListBox::DoInsertItems(const wxArrayString& items, int pos)
680 {
681 GtkInsertItems(items, NULL, pos);
682 }
683
684 int wxListBox::DoAppend( const wxString& item )
685 {
686 InvalidateBestSize();
687
688 //Just call DoInsertItems for now
689 //RN: Originally I had gtk_list_store_append etc.
690 // here as an optimization but now the insert
691 // has been streamlined and its quite a bit of code duplication
692 int nWhere = wxListBox::GetCount();
693 wxArrayString aItems;
694 aItems.Add(item);
695 wxListBox::DoInsertItems(aItems, nWhere);
696 return nWhere;
697 }
698
699 void wxListBox::DoSetItems( const wxArrayString& items,
700 void **clientData)
701 {
702 Clear();
703 GtkInsertItems(items, clientData, 0);
704 }
705
706 // ----------------------------------------------------------------------------
707 // deleting items
708 // ----------------------------------------------------------------------------
709
710 void wxListBox::Clear()
711 {
712 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
713
714 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
715 }
716
717 void wxListBox::Delete( int n )
718 {
719 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
720
721 GtkTreeIter iter;
722 gboolean res = gtk_tree_model_iter_nth_child(
723 GTK_TREE_MODEL(m_liststore),
724 &iter, NULL, //NULL = parent = get first
725 n
726 );
727
728 wxCHECK_RET( res, wxT("wrong listbox index") );
729
730 //this returns false if iter is invalid (i.e. deleting item
731 //at end) but since we don't use iter, well... :)
732 gtk_list_store_remove(m_liststore, &iter);
733 }
734
735 // ----------------------------------------------------------------------------
736 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
737 // ----------------------------------------------------------------------------
738
739 struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
740 {
741 GtkTreeIter iter;
742 gboolean res = gtk_tree_model_iter_nth_child(
743 GTK_TREE_MODEL(m_liststore),
744 &iter, NULL, //NULL = parent = get first
745 n );
746
747 if (!res)
748 {
749 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
750 wxT("Passed in value was:[%i] List size:[%i]"),
751 n, wxListBox::GetCount() );
752 return NULL;
753 }
754
755
756 GtkTreeEntry* entry = NULL;
757 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
758 WXLISTBOX_DATACOLUMN, &entry, -1);
759
760 return entry;
761 }
762
763 // ----------------------------------------------------------------------------
764 // client data
765 // ----------------------------------------------------------------------------
766
767 void* wxListBox::DoGetItemClientData( int n ) const
768 {
769 wxCHECK_MSG( n >= 0 && n < wxListBox::GetCount(), NULL,
770 wxT("Invalid index passed to GetItemClientData") );
771
772 GtkTreeEntry* entry = GtkGetEntry(n);
773 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
774
775 void* userdata = gtk_tree_entry_get_userdata( entry );
776 g_object_unref(G_OBJECT(entry));
777 return userdata;
778 }
779
780 wxClientData* wxListBox::DoGetItemClientObject( int n ) const
781 {
782 return (wxClientData*) wxListBox::DoGetItemClientData(n);
783 }
784
785 void wxListBox::DoSetItemClientData( int n, void* clientData )
786 {
787 wxCHECK_RET( n >= 0 && n < wxListBox::GetCount(),
788 wxT("Invalid index passed to SetItemClientData") );
789
790 GtkTreeEntry* entry = GtkGetEntry(n);
791 wxCHECK_RET(entry, wxT("could not get entry"));
792
793 gtk_tree_entry_set_userdata( entry, clientData );
794 g_object_unref(G_OBJECT(entry));
795 }
796
797 void wxListBox::DoSetItemClientObject( int n, wxClientData* clientData )
798 {
799 // wxItemContainer already deletes data for us
800 wxListBox::DoSetItemClientData(n, (void*) clientData);
801 }
802
803 // ----------------------------------------------------------------------------
804 // string list access
805 // ----------------------------------------------------------------------------
806
807 void wxListBox::SetString( int n, const wxString &string )
808 {
809 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
810
811 GtkTreeEntry* entry = GtkGetEntry(n);
812 wxCHECK_RET( entry, wxT("wrong listbox index") );
813
814 wxString label = string;
815
816 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
817 if (m_hasCheckBoxes)
818 label.Prepend(wxCHECKLBOX_STRING);
819 #endif // wxUSE_CHECKLISTBOX
820
821 // RN: This may look wierd but the problem is that the TreeView
822 // doesn't resort or update when changed above and there is no real
823 // notification function...
824 void* userdata = gtk_tree_entry_get_userdata(entry);
825 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
826 g_object_unref(G_OBJECT(entry));
827
828 bool bWasSelected = wxListBox::IsSelected(n);
829 wxListBox::Delete(n);
830
831 wxArrayString aItems;
832 aItems.Add(label);
833 GtkInsertItems(aItems, &userdata, n);
834 if (bWasSelected)
835 wxListBox::GtkSetSelection(n, TRUE, TRUE);
836 }
837
838 wxString wxListBox::GetString( int n ) const
839 {
840 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
841
842 GtkTreeEntry* entry = GtkGetEntry(n);
843 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
844
845 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
846
847 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
848 // checklistboxes have "[±] " prepended to their lables, remove it
849 //
850 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
851 if ( m_hasCheckBoxes )
852 label.erase(0, 4);
853 #endif // wxUSE_CHECKLISTBOX
854
855 g_object_unref(G_OBJECT(entry));
856 return label;
857 }
858
859 int wxListBox::GetCount() const
860 {
861 wxCHECK_MSG( m_treeview != NULL, -1, wxT("invalid listbox") );
862
863 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
864 }
865
866 int wxListBox::FindString( const wxString &item, bool bCase ) const
867 {
868 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
869
870 //Sort of hackish - maybe there is a faster way
871 int nCount = wxListBox::GetCount();
872
873 for(int i = 0; i < nCount; ++i)
874 {
875 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
876 return i;
877 }
878
879
880 // it's not an error if the string is not found -> no wxCHECK
881 return wxNOT_FOUND;
882 }
883
884 // ----------------------------------------------------------------------------
885 // selection
886 // ----------------------------------------------------------------------------
887
888 int wxListBox::GetSelection() const
889 {
890 wxCHECK_MSG( m_treeview != NULL, -1, wxT("invalid listbox"));
891 wxCHECK_MSG( HasFlag(wxLB_SINGLE), -1,
892 wxT("must be single selection listbox"));
893
894 GtkTreeIter iter;
895 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
896
897 // only works on single-sel
898 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
899 return -1;
900
901 GtkTreePath* path =
902 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
903
904 int sel = gtk_tree_path_get_indices(path)[0];
905
906 gtk_tree_path_free(path);
907
908 return sel;
909 }
910
911 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
912 {
913 wxCHECK_MSG( m_treeview != NULL, -1, wxT("invalid listbox") );
914
915 aSelections.Empty();
916
917 int i = 0;
918 GtkTreeIter iter;
919 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
920
921 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
922 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
923 do
924 {
925 if (gtk_tree_selection_iter_is_selected(selection, &iter))
926 aSelections.Add(i);
927
928 i++;
929 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
930 }
931
932 return aSelections.GetCount();
933 }
934
935 bool wxListBox::IsSelected( int n ) const
936 {
937 wxCHECK_MSG( m_treeview != NULL, FALSE, wxT("invalid listbox") );
938
939 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
940
941 GtkTreeIter iter;
942 gboolean res = gtk_tree_model_iter_nth_child(
943 GTK_TREE_MODEL(m_liststore),
944 &iter, NULL, //NULL = parent = get first
945 n );
946
947 wxCHECK_MSG( res, FALSE, wxT("Invalid index") );
948
949 return gtk_tree_selection_iter_is_selected(selection, &iter);
950 }
951
952 void wxListBox::DoSetSelection( int n, bool select )
953 {
954 return GtkSetSelection(n, select, TRUE); //docs say no events here
955 }
956
957 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
958 {
959 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
960
961 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
962
963 GtkTreeIter iter;
964 gboolean res = gtk_tree_model_iter_nth_child(
965 GTK_TREE_MODEL(m_liststore),
966 &iter, NULL, //NULL = parent = get first
967 n
968 );
969 wxCHECK_RET( res, wxT("Invalid index") );
970
971 m_blockEvent = blockEvent;
972
973 if (select)
974 gtk_tree_selection_select_iter(selection, &iter);
975 else
976 gtk_tree_selection_unselect_iter(selection, &iter);
977
978 m_blockEvent = FALSE;
979 }
980
981 void wxListBox::DoSetFirstItem( int n )
982 {
983 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
984 wxCHECK_RET( n >= 0 && n < wxListBox::GetCount(), wxT("invalid index"));
985
986 //RN: I have no idea why this line is needed...
987 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
988 return;
989
990 // terribly efficient (RN:???)
991 // RN: Note that evidently the vadjustment property "vadjustment" from
992 // GtkTreeView is different from the "gtk-vadjustment"...
993 // (i.e. gtk_tree_view_get_vadjustment)
994 const gchar *vadjustment_key = "gtk-vadjustment";
995 guint vadjustment_key_id = g_quark_from_static_string (vadjustment_key);
996
997 GtkAdjustment *adjustment =
998 (GtkAdjustment*) g_object_get_qdata(G_OBJECT (m_treeview), vadjustment_key_id);
999 wxCHECK_RET( adjustment, wxT("invalid listbox code") );
1000
1001 // Get the greater of the item heights from each column
1002 gint cellheight = 0, cellheightcur;
1003 GList* columnlist = gtk_tree_view_get_columns(m_treeview);
1004 GList* curlist = columnlist;
1005
1006 while(curlist)
1007 {
1008 gtk_tree_view_column_cell_get_size(
1009 GTK_TREE_VIEW_COLUMN(curlist->data),
1010 NULL, NULL, NULL, NULL,
1011 &cellheightcur);
1012
1013 cellheight = cellheightcur > cellheight ?
1014 cellheightcur : cellheight;
1015
1016 curlist = curlist->next;
1017 }
1018
1019 g_list_free(columnlist);
1020
1021 float y = (float) (cellheight * n);
1022 if (y > adjustment->upper - adjustment->page_size)
1023 y = adjustment->upper - adjustment->page_size;
1024 gtk_adjustment_set_value( adjustment, y );
1025 }
1026
1027
1028 // ----------------------------------------------------------------------------
1029 // helpers
1030 // ----------------------------------------------------------------------------
1031
1032 #if wxUSE_TOOLTIPS
1033 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
1034 {
1035 // RN: Is this needed anymore?
1036 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
1037 }
1038 #endif // wxUSE_TOOLTIPS
1039
1040 GtkWidget *wxListBox::GetConnectWidget()
1041 {
1042 // return GTK_WIDGET(m_treeview);
1043 return m_widget;
1044 }
1045
1046 bool wxListBox::IsOwnGtkWindow( GdkWindow *window )
1047 {
1048 return (window == gtk_tree_view_get_bin_window(m_treeview));
1049 }
1050
1051 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
1052 {
1053 if (m_hasBgCol && m_backgroundColour.Ok())
1054 {
1055 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1056 if (window)
1057 {
1058 m_backgroundColour.CalcPixel( gdk_window_get_colormap( window ) );
1059 gdk_window_set_background( window, m_backgroundColour.GetColor() );
1060 gdk_window_clear( window );
1061 }
1062 }
1063
1064 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
1065 }
1066
1067 void wxListBox::OnInternalIdle()
1068 {
1069 //RN: Is this needed anymore?
1070 wxCursor cursor = m_cursor;
1071 if (g_globalCursor.Ok()) cursor = g_globalCursor;
1072
1073 if (GTK_WIDGET(m_treeview)->window && cursor.Ok())
1074 {
1075 /* I now set the cursor the anew in every OnInternalIdle call
1076 as setting the cursor in a parent window also effects the
1077 windows above so that checking for the current cursor is
1078 not possible. */
1079 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
1080 gdk_window_set_cursor( window, cursor.GetCursor() );
1081 }
1082
1083 if (wxUpdateUIEvent::CanUpdate(this))
1084 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
1085 }
1086
1087 wxSize wxListBox::DoGetBestSize() const
1088 {
1089 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
1090
1091 int lbWidth;
1092 int lbHeight;
1093
1094 // Get the visible area of the tree view
1095 GdkRectangle rect;
1096 gtk_tree_view_get_visible_rect(m_treeview, &rect);
1097 lbWidth = rect.width;
1098 lbHeight = rect.height;
1099
1100 // Add room for the scrollbar
1101 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1102
1103 // And just a bit more
1104 int cx, cy;
1105 GetTextExtent( wxT("X"), &cx, &cy);
1106 lbWidth += 3 * cx;
1107
1108 // don't make the listbox too tall (limit height to around 10 items) but don't
1109 // make it too small neither
1110 lbHeight = (cy+4) * wxMin(wxMax(GetCount(), 3), 10);
1111
1112 wxSize best(lbWidth, lbHeight);
1113 CacheBestSize(best);
1114 return best;
1115 }
1116
1117 // static
1118 wxVisualAttributes
1119 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1120 {
1121 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1122 }
1123
1124 #endif // wxUSE_LISTBOX