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