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