Don't hide the window in its dtor in wxGTK.
[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 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_LISTBOX
14
15 #include "wx/listbox.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/dynarray.h"
19 #include "wx/intl.h"
20 #include "wx/log.h"
21 #include "wx/utils.h"
22 #include "wx/settings.h"
23 #include "wx/checklst.h"
24 #include "wx/arrstr.h"
25 #endif
26
27 #if wxUSE_TOOLTIPS
28 #include "wx/tooltip.h"
29 #endif
30
31 #include <gtk/gtk.h>
32 #include "wx/gtk/private.h"
33 #include "wx/gtk/private/gtk2-compat.h"
34 #include "wx/gtk/private/object.h"
35 #include "wx/gtk/private/treeentry_gtk.h"
36
37 #include <gdk/gdkkeysyms.h>
38 #ifdef __WXGTK3__
39 #include <gdk/gdkkeysyms-compat.h>
40 #endif
41
42 //-----------------------------------------------------------------------------
43 // data
44 //-----------------------------------------------------------------------------
45
46 extern bool g_blockEventsOnDrag;
47 extern bool g_blockEventsOnScroll;
48
49
50
51 //-----------------------------------------------------------------------------
52 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
53 //-----------------------------------------------------------------------------
54
55 #if wxUSE_CHECKLISTBOX
56 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
57 #else
58 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
59 #endif // wxUSE_CHECKLISTBOX
60
61 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
62
63 // ----------------------------------------------------------------------------
64 // helper functions
65 // ----------------------------------------------------------------------------
66
67 namespace
68 {
69
70 // Return the entry for the given listbox item.
71 wxTreeEntry *
72 GetEntry(GtkListStore *store, GtkTreeIter *iter, const wxListBox *listbox)
73 {
74 wxTreeEntry* entry;
75 gtk_tree_model_get(GTK_TREE_MODEL(store),
76 iter,
77 WXLISTBOX_DATACOLUMN_ARG(listbox),
78 &entry,
79 -1);
80 g_object_unref(entry);
81 return entry;
82 }
83
84 } // anonymous namespace
85
86 //-----------------------------------------------------------------------------
87 // "row-activated"
88 //-----------------------------------------------------------------------------
89
90 extern "C" {
91 static void
92 gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED(treeview),
93 GtkTreePath *path,
94 GtkTreeViewColumn * WXUNUSED(col),
95 wxListBox *listbox)
96 {
97 if (g_blockEventsOnDrag) return;
98 if (g_blockEventsOnScroll) return;
99
100 // This is triggered by either a double-click or a space press
101
102 int sel = gtk_tree_path_get_indices(path)[0];
103
104 listbox->GTKOnActivated(sel);
105 }
106 }
107
108 //-----------------------------------------------------------------------------
109 // "changed"
110 //-----------------------------------------------------------------------------
111
112 extern "C" {
113 static void
114 gtk_listitem_changed_callback(GtkTreeSelection * WXUNUSED(selection),
115 wxListBox *listbox )
116 {
117 if (g_blockEventsOnDrag) return;
118
119 listbox->GTKOnSelectionChanged();
120 }
121
122 }
123
124 //-----------------------------------------------------------------------------
125 // "key_press_event"
126 //-----------------------------------------------------------------------------
127
128 extern "C" {
129 static gboolean
130 gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget),
131 GdkEventKey *gdk_event,
132 wxListBox *listbox )
133 {
134 if ((gdk_event->keyval == GDK_Return) ||
135 (gdk_event->keyval == GDK_ISO_Enter) ||
136 (gdk_event->keyval == GDK_KP_Enter))
137 {
138 int index = -1;
139 if (!listbox->HasMultipleSelection())
140 index = listbox->GetSelection();
141 else
142 {
143 wxArrayInt sels;
144 if (listbox->GetSelections( sels ) < 1)
145 return FALSE;
146 index = sels[0];
147 }
148
149 if (index != wxNOT_FOUND)
150 {
151 listbox->GTKOnActivated(index);
152
153 // wxMac and wxMSW always invoke default action
154 // if (!ret)
155 {
156 // DClick not handled -> invoke default action
157 wxWindow *tlw = wxGetTopLevelParent( listbox );
158 if (tlw)
159 {
160 GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() );
161 if (gtk_window)
162 gtk_window_activate_default( gtk_window );
163 }
164 }
165
166 // Always intercept, otherwise we'd get another dclick
167 // event from row_activated
168 return TRUE;
169 }
170 }
171
172 return FALSE;
173 }
174 }
175
176 //-----------------------------------------------------------------------------
177 // GtkTreeEntry destruction (to destroy client data)
178 //-----------------------------------------------------------------------------
179
180 extern "C" {
181 static void tree_entry_destroy_cb(wxTreeEntry* entry,
182 wxListBox* listbox)
183 {
184 if (listbox->HasClientObjectData())
185 {
186 void* userdata = wx_tree_entry_get_userdata(entry);
187 if (userdata)
188 delete (wxClientData *)userdata;
189 }
190 }
191 }
192
193 //-----------------------------------------------------------------------------
194 // Sorting callback (standard CmpNoCase return value)
195 //-----------------------------------------------------------------------------
196
197 extern "C" {
198 static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
199 GtkTreeIter *a,
200 GtkTreeIter *b,
201 wxListBox *listbox)
202 {
203 wxTreeEntry* entry1 = GetEntry(listbox->m_liststore, a, listbox);
204 wxCHECK_MSG(entry1, 0, wxT("Could not get first entry"));
205
206 wxTreeEntry* entry2 = GetEntry(listbox->m_liststore, b, listbox);
207 wxCHECK_MSG(entry2, 0, wxT("Could not get second entry"));
208
209 //We compare collate keys here instead of calling g_utf8_collate
210 //as it is rather slow (and even the docs recommend this)
211 return strcmp(wx_tree_entry_get_collate_key(entry1),
212 wx_tree_entry_get_collate_key(entry2)) >= 0;
213 }
214 }
215
216 //-----------------------------------------------------------------------------
217 // Searching callback (TRUE == not equal, FALSE == equal)
218 //-----------------------------------------------------------------------------
219
220 extern "C" {
221 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
222 gint WXUNUSED(column),
223 const gchar* key,
224 GtkTreeIter* iter,
225 wxListBox* listbox)
226 {
227 wxTreeEntry* entry = GetEntry(listbox->m_liststore, iter, listbox);
228 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
229
230 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
231
232 return strcmp(keycollatekey, wx_tree_entry_get_collate_key(entry)) != 0;
233 }
234 }
235
236 //-----------------------------------------------------------------------------
237 // wxListBox
238 //-----------------------------------------------------------------------------
239
240 // ----------------------------------------------------------------------------
241 // construction
242 // ----------------------------------------------------------------------------
243
244 void wxListBox::Init()
245 {
246 m_treeview = NULL;
247 #if wxUSE_CHECKLISTBOX
248 m_hasCheckBoxes = false;
249 #endif // wxUSE_CHECKLISTBOX
250 }
251
252 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
253 const wxPoint &pos, const wxSize &size,
254 const wxArrayString& choices,
255 long style, const wxValidator& validator,
256 const wxString &name )
257 {
258 wxCArrayString chs(choices);
259
260 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
261 style, validator, name );
262 }
263
264 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
265 const wxPoint &pos, const wxSize &size,
266 int n, const wxString choices[],
267 long style, const wxValidator& validator,
268 const wxString &name )
269 {
270 if (!PreCreation( parent, pos, size ) ||
271 !CreateBase( parent, id, pos, size, style, validator, name ))
272 {
273 wxFAIL_MSG( wxT("wxListBox creation failed") );
274 return false;
275 }
276
277 m_widget = gtk_scrolled_window_new( NULL, NULL );
278 g_object_ref(m_widget);
279 if (style & wxLB_ALWAYS_SB)
280 {
281 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
282 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
283 }
284 else
285 {
286 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
287 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
288 }
289
290
291 GTKScrolledWindowSetBorder(m_widget, style);
292
293 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
294
295 //wxListBox doesn't have a header :)
296 //NB: If enabled SetFirstItem doesn't work correctly
297 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
298
299 #if wxUSE_CHECKLISTBOX
300 if(m_hasCheckBoxes)
301 ((wxCheckListBox*)this)->DoCreateCheckList();
302 #endif // wxUSE_CHECKLISTBOX
303
304 // Create the data column
305 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
306 gtk_cell_renderer_text_new(),
307 "text",
308 WXLISTBOX_DATACOLUMN, NULL);
309
310 // Now create+set the model (GtkListStore) - first argument # of columns
311 #if wxUSE_CHECKLISTBOX
312 if(m_hasCheckBoxes)
313 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
314 WX_TYPE_TREE_ENTRY);
315 else
316 #endif
317 m_liststore = gtk_list_store_new(1, WX_TYPE_TREE_ENTRY);
318
319 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
320
321 g_object_unref (m_liststore); //free on treeview destruction
322
323 // Disable the pop-up textctrl that enables searching - note that
324 // the docs specify that even if this disabled (which we are doing)
325 // the user can still have it through the start-interactive-search
326 // key binding...either way we want to provide a searchequal callback
327 // NB: If this is enabled a doubleclick event (activate) gets sent
328 // on a successful search
329 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
330 gtk_tree_view_set_search_equal_func(m_treeview,
331 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
332 this,
333 NULL);
334
335 gtk_tree_view_set_enable_search(m_treeview, FALSE);
336
337 GtkSelectionMode mode;
338 // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
339 if ( style & (wxLB_MULTIPLE | wxLB_EXTENDED) )
340 {
341 mode = GTK_SELECTION_MULTIPLE;
342 }
343 else // no multi-selection flags specified
344 {
345 m_windowStyle |= wxLB_SINGLE;
346
347 // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because
348 // the latter allows to not select any items at all while a single
349 // selection listbox is supposed to always have a selection (at least
350 // once the user selected something, it might not have any initially).
351 mode = GTK_SELECTION_BROWSE;
352 }
353
354 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
355 gtk_tree_selection_set_mode( selection, mode );
356
357 // Handle sortable stuff
358 if(HasFlag(wxLB_SORT))
359 {
360 // Setup sorting in ascending (wx) order
361 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
362 WXLISTBOX_DATACOLUMN,
363 GTK_SORT_ASCENDING);
364
365 // Set the sort callback
366 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
367 WXLISTBOX_DATACOLUMN,
368 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
369 this, //userdata
370 NULL //"destroy notifier"
371 );
372 }
373
374
375 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
376
377 gtk_widget_show( GTK_WIDGET(m_treeview) );
378 m_focusWidget = GTK_WIDGET(m_treeview);
379
380 Append(n, choices); // insert initial items
381
382 // generate dclick events
383 g_signal_connect_after(m_treeview, "row-activated",
384 G_CALLBACK(gtk_listbox_row_activated_callback), this);
385
386 // for intercepting dclick generation by <ENTER>
387 g_signal_connect (m_treeview, "key_press_event",
388 G_CALLBACK (gtk_listbox_key_press_callback),
389 this);
390 m_parent->DoAddChild( this );
391
392 PostCreation(size);
393 SetInitialSize(size); // need this too because this is a wxControlWithItems
394
395 g_signal_connect_after (selection, "changed",
396 G_CALLBACK (gtk_listitem_changed_callback), this);
397
398 return true;
399 }
400
401 wxListBox::~wxListBox()
402 {
403 if (m_treeview)
404 {
405 GTKDisconnect(m_treeview);
406 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
407 if (selection)
408 GTKDisconnect(selection);
409 }
410
411 Clear();
412 }
413
414 void wxListBox::GTKDisableEvents()
415 {
416 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
417
418 g_signal_handlers_block_by_func(selection,
419 (gpointer) gtk_listitem_changed_callback, this);
420 }
421
422 void wxListBox::GTKEnableEvents()
423 {
424 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
425
426 g_signal_handlers_unblock_by_func(selection,
427 (gpointer) gtk_listitem_changed_callback, this);
428
429 UpdateOldSelections();
430 }
431
432
433 void wxListBox::Update()
434 {
435 wxWindow::Update();
436
437 if (m_treeview)
438 gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(m_treeview)), true);
439 }
440
441 // ----------------------------------------------------------------------------
442 // adding items
443 // ----------------------------------------------------------------------------
444
445 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
446 unsigned int pos,
447 void **clientData,
448 wxClientDataType type)
449 {
450 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
451
452 InvalidateBestSize();
453 int n = DoInsertItemsInLoop(items, pos, clientData, type);
454 UpdateOldSelections();
455 return n;
456 }
457
458 int wxListBox::DoInsertOneItem(const wxString& item, unsigned int pos)
459 {
460 wxTreeEntry* entry = wx_tree_entry_new();
461 wx_tree_entry_set_label(entry, wxGTK_CONV(item));
462 wx_tree_entry_set_destroy_func(entry, (wxTreeEntryDestroy)tree_entry_destroy_cb, this);
463
464 #if wxUSE_CHECKLISTBOX
465 int entryCol = int(m_hasCheckBoxes);
466 #else
467 int entryCol = 0;
468 #endif
469 gtk_list_store_insert_with_values(m_liststore, NULL, pos, entryCol, entry, -1);
470 g_object_unref(entry);
471
472 return pos;
473 }
474
475 // ----------------------------------------------------------------------------
476 // deleting items
477 // ----------------------------------------------------------------------------
478
479 void wxListBox::DoClear()
480 {
481 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
482
483 GTKDisableEvents(); // just in case
484
485 InvalidateBestSize();
486
487 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
488
489 GTKEnableEvents();
490
491 UpdateOldSelections();
492 }
493
494 void wxListBox::DoDeleteOneItem(unsigned int n)
495 {
496 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
497
498 InvalidateBestSize();
499
500 GTKDisableEvents(); // just in case
501
502 GtkTreeIter iter;
503 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
504
505 // this returns false if iter is invalid (e.g. deleting item at end) but
506 // since we don't use iter, we ignore the return value
507 gtk_list_store_remove(m_liststore, &iter);
508
509 GTKEnableEvents();
510 }
511
512 // ----------------------------------------------------------------------------
513 // helper functions for working with iterators
514 // ----------------------------------------------------------------------------
515
516 bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
517 {
518 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
519 iter, NULL, pos) )
520 {
521 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
522 return false;
523 }
524
525 return true;
526 }
527
528 int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
529 {
530 GtkTreePath *path =
531 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
532
533 gint* pIntPath = gtk_tree_path_get_indices(path);
534
535 wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
536
537 int idx = pIntPath[0];
538
539 gtk_tree_path_free( path );
540
541 return idx;
542 }
543
544 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
545 wxTreeEntry* wxListBox::GTKGetEntry(unsigned n) const
546 {
547 GtkTreeIter iter;
548 if ( !GTKGetIteratorFor(n, &iter) )
549 return NULL;
550
551 return GetEntry(m_liststore, &iter, this);
552 }
553
554 // ----------------------------------------------------------------------------
555 // client data
556 // ----------------------------------------------------------------------------
557
558 void* wxListBox::DoGetItemClientData(unsigned int n) const
559 {
560 wxTreeEntry* entry = GTKGetEntry(n);
561 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
562
563 return wx_tree_entry_get_userdata(entry);
564 }
565
566 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
567 {
568 wxTreeEntry* entry = GTKGetEntry(n);
569 wxCHECK_RET(entry, wxT("could not get entry"));
570
571 wx_tree_entry_set_userdata(entry, clientData);
572 }
573
574 // ----------------------------------------------------------------------------
575 // string list access
576 // ----------------------------------------------------------------------------
577
578 void wxListBox::SetString(unsigned int n, const wxString& label)
579 {
580 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
581
582 GtkTreeIter iter;
583 wxCHECK_RET(GTKGetIteratorFor(n, &iter), "invalid index");
584 wxTreeEntry* entry = GetEntry(m_liststore, &iter, this);
585
586 // update the item itself
587 wx_tree_entry_set_label(entry, wxGTK_CONV(label));
588
589 // signal row changed
590 GtkTreeModel* tree_model = GTK_TREE_MODEL(m_liststore);
591 GtkTreePath* path = gtk_tree_model_get_path(tree_model, &iter);
592 gtk_tree_model_row_changed(tree_model, path, &iter);
593 gtk_tree_path_free(path);
594 }
595
596 wxString wxListBox::GetString(unsigned int n) const
597 {
598 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
599
600 wxTreeEntry* entry = GTKGetEntry(n);
601 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
602
603 return wxGTK_CONV_BACK(wx_tree_entry_get_label(entry));
604 }
605
606 unsigned int wxListBox::GetCount() const
607 {
608 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
609
610 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
611 }
612
613 int wxListBox::FindString( const wxString &item, bool bCase ) const
614 {
615 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
616
617 //Sort of hackish - maybe there is a faster way
618 unsigned int nCount = wxListBox::GetCount();
619
620 for(unsigned int i = 0; i < nCount; ++i)
621 {
622 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
623 return (int)i;
624 }
625
626
627 // it's not an error if the string is not found -> no wxCHECK
628 return wxNOT_FOUND;
629 }
630
631 // ----------------------------------------------------------------------------
632 // selection
633 // ----------------------------------------------------------------------------
634
635 void wxListBox::GTKOnActivated(int item)
636 {
637 SendEvent(wxEVT_LISTBOX_DCLICK, item, IsSelected(item));
638 }
639
640 void wxListBox::GTKOnSelectionChanged()
641 {
642 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
643 {
644 CalcAndSendEvent();
645 }
646 else // single selection
647 {
648 const int item = GetSelection();
649 if ( DoChangeSingleSelection(item) )
650 SendEvent(wxEVT_LISTBOX, item, true);
651 }
652 }
653
654 int wxListBox::GetSelection() const
655 {
656 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
657 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
658 wxT("must be single selection listbox"));
659
660 GtkTreeIter iter;
661 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
662
663 // only works on single-sel
664 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
665 return wxNOT_FOUND;
666
667 return GTKGetIndexFor(iter);
668 }
669
670 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
671 {
672 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
673
674 aSelections.Empty();
675
676 int i = 0;
677 GtkTreeIter iter;
678 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
679
680 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
681 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
682 do
683 {
684 if (gtk_tree_selection_iter_is_selected(selection, &iter))
685 aSelections.Add(i);
686
687 i++;
688 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
689 }
690
691 return aSelections.GetCount();
692 }
693
694 bool wxListBox::IsSelected( int n ) const
695 {
696 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
697
698 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
699
700 GtkTreeIter iter;
701 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
702
703 return gtk_tree_selection_iter_is_selected(selection, &iter) != 0;
704 }
705
706 void wxListBox::DoSetSelection( int n, bool select )
707 {
708 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
709
710 GTKDisableEvents();
711
712 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
713
714 // passing -1 to SetSelection() is documented to deselect all items
715 if ( n == wxNOT_FOUND )
716 {
717 gtk_tree_selection_unselect_all(selection);
718 GTKEnableEvents();
719 return;
720 }
721
722 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
723
724
725 GtkTreeIter iter;
726 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
727
728 if (select)
729 gtk_tree_selection_select_iter(selection, &iter);
730 else
731 gtk_tree_selection_unselect_iter(selection, &iter);
732
733 GtkTreePath* path = gtk_tree_model_get_path(
734 GTK_TREE_MODEL(m_liststore), &iter);
735
736 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
737
738 gtk_tree_path_free(path);
739
740 GTKEnableEvents();
741 }
742
743 void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
744 {
745 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
746 wxCHECK_RET( IsValid(n), wxT("invalid index"));
747
748 //RN: I have no idea why this line is needed...
749 if (gtk_widget_has_grab(GTK_WIDGET(m_treeview)))
750 return;
751
752 GtkTreeIter iter;
753 if ( !GTKGetIteratorFor(n, &iter) )
754 return;
755
756 GtkTreePath* path = gtk_tree_model_get_path(
757 GTK_TREE_MODEL(m_liststore), &iter);
758
759 // Scroll to the desired cell (0.0 == topleft alignment)
760 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
761 TRUE, alignY, alignX);
762
763 gtk_tree_path_free(path);
764 }
765
766 void wxListBox::DoSetFirstItem(int n)
767 {
768 DoScrollToCell(n, 0, 0);
769 }
770
771 void wxListBox::EnsureVisible(int n)
772 {
773 DoScrollToCell(n, 0.5, 0);
774 }
775
776 // ----------------------------------------------------------------------------
777 // hittest
778 // ----------------------------------------------------------------------------
779
780 int wxListBox::DoListHitTest(const wxPoint& point) const
781 {
782 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
783 // we only want visible items we need to check for it manually here
784 if ( !GetClientRect().Contains(point) )
785 return wxNOT_FOUND;
786
787 // need to translate from master window since it is in client coords
788 gint binx, biny;
789 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
790 &binx, &biny, NULL, NULL);
791
792 GtkTreePath* path;
793 if ( !gtk_tree_view_get_path_at_pos
794 (
795 m_treeview,
796 point.x - binx,
797 point.y - biny,
798 &path,
799 NULL, // [out] column (always 0 here)
800 NULL, // [out] x-coord relative to the cell (not interested)
801 NULL // [out] y-coord relative to the cell
802 ) )
803 {
804 return wxNOT_FOUND;
805 }
806
807 int index = gtk_tree_path_get_indices(path)[0];
808 gtk_tree_path_free(path);
809
810 return index;
811 }
812
813 // ----------------------------------------------------------------------------
814 // helpers
815 // ----------------------------------------------------------------------------
816
817 GtkWidget *wxListBox::GetConnectWidget()
818 {
819 // the correct widget for listbox events (such as mouse clicks for example)
820 // is m_treeview, not the parent scrolled window
821 return GTK_WIDGET(m_treeview);
822 }
823
824 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
825 {
826 return gtk_tree_view_get_bin_window(m_treeview);
827 }
828
829 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
830 {
831 #ifdef __WXGTK3__
832 // don't know if this is even necessary, or how to do it
833 #else
834 if (m_hasBgCol && m_backgroundColour.IsOk())
835 {
836 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
837 if (window)
838 {
839 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
840 gdk_window_set_background( window, m_backgroundColour.GetColor() );
841 gdk_window_clear( window );
842 }
843 }
844 #endif
845
846 GTKApplyStyle(GTK_WIDGET(m_treeview), style);
847 }
848
849 wxSize wxListBox::DoGetBestSize() const
850 {
851 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
852
853 // Start with a minimum size that's not too small
854 int cx, cy;
855 GetTextExtent( wxT("X"), &cx, &cy);
856 int lbWidth = 0;
857 int lbHeight = 10;
858
859 // Find the widest string.
860 const unsigned int count = GetCount();
861 if ( count )
862 {
863 int wLine;
864 for ( unsigned int i = 0; i < count; i++ )
865 {
866 GetTextExtent(GetString(i), &wLine, NULL);
867 if ( wLine > lbWidth )
868 lbWidth = wLine;
869 }
870 }
871
872 lbWidth += 3 * cx;
873
874 // And just a bit more for the checkbox if present and then some
875 // (these are rough guesses)
876 #if wxUSE_CHECKLISTBOX
877 if ( m_hasCheckBoxes )
878 {
879 lbWidth += 35;
880 cy = cy > 25 ? cy : 25; // rough height of checkbox
881 }
882 #endif
883
884 // Add room for the scrollbar
885 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
886
887 // Don't make the listbox too tall but don't make it too small neither
888 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
889
890 wxSize best(lbWidth, lbHeight);
891 CacheBestSize(best);
892 return best;
893 }
894
895 // static
896 wxVisualAttributes
897 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
898 {
899 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new(), true);
900 }
901
902 #endif // wxUSE_LISTBOX