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 *
GetEntry(GtkListStore * store,GtkTreeIter * iter,const wxListBox * listbox)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
gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED (treeview),GtkTreePath * path,GtkTreeViewColumn * WXUNUSED (col),wxListBox * listbox)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
gtk_listitem_changed_callback(GtkTreeSelection * WXUNUSED (selection),wxListBox * listbox)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
gtk_listbox_key_press_callback(GtkWidget * WXUNUSED (widget),GdkEventKey * gdk_event,wxListBox * listbox)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" {
tree_entry_destroy_cb(wxTreeEntry * entry,wxListBox * listbox)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 int
sort_callback(GtkTreeModel *,GtkTreeIter * a,GtkTreeIter * b,void * data)199 sort_callback(GtkTreeModel*, GtkTreeIter* a, GtkTreeIter* b, void* data)
200 {
201 wxListBox* listbox = static_cast<wxListBox*>(data);
202 wxTreeEntry* entry1 = GetEntry(listbox->m_liststore, a, listbox);
203 wxCHECK_MSG(entry1, 0, wxT("Could not get first entry"));
204
205 wxTreeEntry* entry2 = GetEntry(listbox->m_liststore, b, listbox);
206 wxCHECK_MSG(entry2, 0, wxT("Could not get second entry"));
207
208 //We compare collate keys here instead of calling g_utf8_collate
209 //as it is rather slow (and even the docs recommend this)
210 return strcmp(wx_tree_entry_get_collate_key(entry1),
211 wx_tree_entry_get_collate_key(entry2)) >= 0;
212 }
213 }
214
215 //-----------------------------------------------------------------------------
216 // Searching callback (TRUE == not equal, FALSE == equal)
217 //-----------------------------------------------------------------------------
218
219 extern "C" {
220 static gboolean
search_callback(GtkTreeModel *,int,const char * key,GtkTreeIter * iter,void * data)221 search_callback(GtkTreeModel*, int, const char* key, GtkTreeIter* iter, void* data)
222 {
223 wxListBox* listbox = static_cast<wxListBox*>(data);
224 wxTreeEntry* entry = GetEntry(listbox->m_liststore, iter, listbox);
225 wxCHECK_MSG(entry, true, "could not get entry");
226
227 wxGtkString keyc(g_utf8_collate_key(key, -1));
228
229 return strncmp(keyc, wx_tree_entry_get_collate_key(entry), strlen(keyc));
230 }
231 }
232
233 //-----------------------------------------------------------------------------
234 // wxListBox
235 //-----------------------------------------------------------------------------
236
237 // ----------------------------------------------------------------------------
238 // construction
239 // ----------------------------------------------------------------------------
240
Init()241 void wxListBox::Init()
242 {
243 m_treeview = NULL;
244 #if wxUSE_CHECKLISTBOX
245 m_hasCheckBoxes = false;
246 #endif // wxUSE_CHECKLISTBOX
247 }
248
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,const wxArrayString & choices,long style,const wxValidator & validator,const wxString & name)249 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
250 const wxPoint &pos, const wxSize &size,
251 const wxArrayString& choices,
252 long style, const wxValidator& validator,
253 const wxString &name )
254 {
255 wxCArrayString chs(choices);
256
257 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
258 style, validator, name );
259 }
260
Create(wxWindow * parent,wxWindowID id,const wxPoint & pos,const wxSize & size,int n,const wxString choices[],long style,const wxValidator & validator,const wxString & name)261 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
262 const wxPoint &pos, const wxSize &size,
263 int n, const wxString choices[],
264 long style, const wxValidator& validator,
265 const wxString &name )
266 {
267 if (!PreCreation( parent, pos, size ) ||
268 !CreateBase( parent, id, pos, size, style, validator, name ))
269 {
270 wxFAIL_MSG( wxT("wxListBox creation failed") );
271 return false;
272 }
273
274 m_widget = gtk_scrolled_window_new( NULL, NULL );
275 g_object_ref(m_widget);
276
277 GtkPolicyType vPolicy = GTK_POLICY_AUTOMATIC;
278 if (style & wxLB_ALWAYS_SB)
279 vPolicy = GTK_POLICY_ALWAYS;
280 else if (style & wxLB_NO_SB)
281 vPolicy = GTK_POLICY_NEVER;
282
283 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(m_widget),
284 GTK_POLICY_AUTOMATIC, vPolicy);
285
286
287 GTKScrolledWindowSetBorder(m_widget, style);
288
289 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
290
291 //wxListBox doesn't have a header :)
292 //NB: If enabled SetFirstItem doesn't work correctly
293 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
294
295 #if wxUSE_CHECKLISTBOX
296 if(m_hasCheckBoxes)
297 ((wxCheckListBox*)this)->DoCreateCheckList();
298 #endif // wxUSE_CHECKLISTBOX
299
300 // Create the data column
301 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
302 gtk_cell_renderer_text_new(),
303 "text",
304 WXLISTBOX_DATACOLUMN, NULL);
305
306 // Now create+set the model (GtkListStore) - first argument # of columns
307 #if wxUSE_CHECKLISTBOX
308 if(m_hasCheckBoxes)
309 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
310 WX_TYPE_TREE_ENTRY);
311 else
312 #endif
313 m_liststore = gtk_list_store_new(1, WX_TYPE_TREE_ENTRY);
314
315 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
316
317 g_object_unref (m_liststore); //free on treeview destruction
318
319 // Disable the pop-up textctrl that enables searching - note that
320 // the docs specify that even if this disabled (which we are doing)
321 // the user can still have it through the start-interactive-search
322 // key binding...either way we want to provide a searchequal callback
323 // NB: If this is enabled a doubleclick event (activate) gets sent
324 // on a successful search
325 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
326 gtk_tree_view_set_search_equal_func(m_treeview, search_callback, this, NULL);
327
328 gtk_tree_view_set_enable_search(m_treeview, FALSE);
329
330 GtkSelectionMode mode;
331 // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
332 if ( style & (wxLB_MULTIPLE | wxLB_EXTENDED) )
333 {
334 mode = GTK_SELECTION_MULTIPLE;
335 }
336 else // no multi-selection flags specified
337 {
338 m_windowStyle |= wxLB_SINGLE;
339
340 // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because
341 // the latter allows to not select any items at all while a single
342 // selection listbox is supposed to always have a selection (at least
343 // once the user selected something, it might not have any initially).
344 mode = GTK_SELECTION_BROWSE;
345 }
346
347 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
348 gtk_tree_selection_set_mode( selection, mode );
349
350 // Handle sortable stuff
351 if(HasFlag(wxLB_SORT))
352 {
353 // Setup sorting in ascending (wx) order
354 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
355 WXLISTBOX_DATACOLUMN,
356 GTK_SORT_ASCENDING);
357
358 // Set the sort callback
359 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
360 WXLISTBOX_DATACOLUMN,
361 sort_callback,
362 this, //userdata
363 NULL //"destroy notifier"
364 );
365 }
366
367
368 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
369
370 gtk_widget_show( GTK_WIDGET(m_treeview) );
371 m_focusWidget = GTK_WIDGET(m_treeview);
372
373 Append(n, choices); // insert initial items
374
375 // generate dclick events
376 g_signal_connect_after(m_treeview, "row-activated",
377 G_CALLBACK(gtk_listbox_row_activated_callback), this);
378
379 // for intercepting dclick generation by <ENTER>
380 g_signal_connect (m_treeview, "key_press_event",
381 G_CALLBACK (gtk_listbox_key_press_callback),
382 this);
383 m_parent->DoAddChild( this );
384
385 PostCreation(size);
386 SetInitialSize(size); // need this too because this is a wxControlWithItems
387
388 g_signal_connect_after (selection, "changed",
389 G_CALLBACK (gtk_listitem_changed_callback), this);
390
391 return true;
392 }
393
~wxListBox()394 wxListBox::~wxListBox()
395 {
396 if (m_treeview)
397 {
398 GTKDisconnect(m_treeview);
399 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
400 if (selection)
401 GTKDisconnect(selection);
402 }
403
404 Clear();
405 }
406
GTKDisableEvents()407 void wxListBox::GTKDisableEvents()
408 {
409 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
410
411 g_signal_handlers_block_by_func(selection,
412 (gpointer) gtk_listitem_changed_callback, this);
413 }
414
GTKEnableEvents()415 void wxListBox::GTKEnableEvents()
416 {
417 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
418
419 g_signal_handlers_unblock_by_func(selection,
420 (gpointer) gtk_listitem_changed_callback, this);
421
422 UpdateOldSelections();
423 }
424
425
Update()426 void wxListBox::Update()
427 {
428 wxWindow::Update();
429
430 if (m_treeview)
431 gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(m_treeview)), true);
432 }
433
434 // ----------------------------------------------------------------------------
435 // adding items
436 // ----------------------------------------------------------------------------
437
DoInsertItems(const wxArrayStringsAdapter & items,unsigned int pos,void ** clientData,wxClientDataType type)438 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
439 unsigned int pos,
440 void **clientData,
441 wxClientDataType type)
442 {
443 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
444
445 InvalidateBestSize();
446 int n = DoInsertItemsInLoop(items, pos, clientData, type);
447 UpdateOldSelections();
448 return n;
449 }
450
DoInsertOneItem(const wxString & item,unsigned int pos)451 int wxListBox::DoInsertOneItem(const wxString& item, unsigned int pos)
452 {
453 wxTreeEntry* entry = wx_tree_entry_new();
454 wx_tree_entry_set_label(entry, wxGTK_CONV(item));
455 wx_tree_entry_set_destroy_func(entry, (wxTreeEntryDestroy)tree_entry_destroy_cb, this);
456
457 #if wxUSE_CHECKLISTBOX
458 int entryCol = int(m_hasCheckBoxes);
459 #else
460 int entryCol = 0;
461 #endif
462 GtkTreeIter iter;
463 gtk_list_store_insert_with_values(m_liststore, &iter, pos, entryCol, entry, -1);
464 g_object_unref(entry);
465
466 if (HasFlag(wxLB_SORT))
467 pos = GTKGetIndexFor(iter);
468
469 return pos;
470 }
471
472 // ----------------------------------------------------------------------------
473 // deleting items
474 // ----------------------------------------------------------------------------
475
DoClear()476 void wxListBox::DoClear()
477 {
478 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
479
480 GTKDisableEvents(); // just in case
481
482 InvalidateBestSize();
483
484 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
485
486 GTKEnableEvents();
487
488 UpdateOldSelections();
489 }
490
DoDeleteOneItem(unsigned int n)491 void wxListBox::DoDeleteOneItem(unsigned int n)
492 {
493 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
494
495 InvalidateBestSize();
496
497 GTKDisableEvents(); // just in case
498
499 GtkTreeIter iter;
500 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
501
502 // this returns false if iter is invalid (e.g. deleting item at end) but
503 // since we don't use iter, we ignore the return value
504 gtk_list_store_remove(m_liststore, &iter);
505
506 GTKEnableEvents();
507 }
508
509 // ----------------------------------------------------------------------------
510 // helper functions for working with iterators
511 // ----------------------------------------------------------------------------
512
GTKGetIteratorFor(unsigned pos,GtkTreeIter * iter) const513 bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
514 {
515 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
516 iter, NULL, pos) )
517 {
518 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
519 return false;
520 }
521
522 return true;
523 }
524
GTKGetIndexFor(GtkTreeIter & iter) const525 int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
526 {
527 GtkTreePath *path =
528 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
529
530 gint* pIntPath = gtk_tree_path_get_indices(path);
531
532 wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
533
534 int idx = pIntPath[0];
535
536 gtk_tree_path_free( path );
537
538 return idx;
539 }
540
541 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
GTKGetEntry(unsigned n) const542 wxTreeEntry* wxListBox::GTKGetEntry(unsigned n) const
543 {
544 GtkTreeIter iter;
545 if ( !GTKGetIteratorFor(n, &iter) )
546 return NULL;
547
548 return GetEntry(m_liststore, &iter, this);
549 }
550
551 // ----------------------------------------------------------------------------
552 // client data
553 // ----------------------------------------------------------------------------
554
DoGetItemClientData(unsigned int n) const555 void* wxListBox::DoGetItemClientData(unsigned int n) const
556 {
557 wxTreeEntry* entry = GTKGetEntry(n);
558 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
559
560 return wx_tree_entry_get_userdata(entry);
561 }
562
DoSetItemClientData(unsigned int n,void * clientData)563 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
564 {
565 wxTreeEntry* entry = GTKGetEntry(n);
566 wxCHECK_RET(entry, wxT("could not get entry"));
567
568 wx_tree_entry_set_userdata(entry, clientData);
569 }
570
571 // ----------------------------------------------------------------------------
572 // string list access
573 // ----------------------------------------------------------------------------
574
SetString(unsigned int n,const wxString & label)575 void wxListBox::SetString(unsigned int n, const wxString& label)
576 {
577 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
578
579 GtkTreeIter iter;
580 wxCHECK_RET(GTKGetIteratorFor(n, &iter), "invalid index");
581 wxTreeEntry* entry = GetEntry(m_liststore, &iter, this);
582
583 // update the item itself
584 wx_tree_entry_set_label(entry, wxGTK_CONV(label));
585
586 // signal row changed
587 GtkTreeModel* tree_model = GTK_TREE_MODEL(m_liststore);
588 GtkTreePath* path = gtk_tree_model_get_path(tree_model, &iter);
589 gtk_tree_model_row_changed(tree_model, path, &iter);
590 gtk_tree_path_free(path);
591 }
592
GetString(unsigned int n) const593 wxString wxListBox::GetString(unsigned int n) const
594 {
595 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
596
597 wxTreeEntry* entry = GTKGetEntry(n);
598 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
599
600 return wxGTK_CONV_BACK(wx_tree_entry_get_label(entry));
601 }
602
GetCount() const603 unsigned int wxListBox::GetCount() const
604 {
605 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
606
607 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
608 }
609
FindString(const wxString & item,bool bCase) const610 int wxListBox::FindString( const wxString &item, bool bCase ) const
611 {
612 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
613
614 //Sort of hackish - maybe there is a faster way
615 unsigned int nCount = wxListBox::GetCount();
616
617 for(unsigned int i = 0; i < nCount; ++i)
618 {
619 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
620 return (int)i;
621 }
622
623
624 // it's not an error if the string is not found -> no wxCHECK
625 return wxNOT_FOUND;
626 }
627
628 // ----------------------------------------------------------------------------
629 // selection
630 // ----------------------------------------------------------------------------
631
GTKOnActivated(int item)632 void wxListBox::GTKOnActivated(int item)
633 {
634 SendEvent(wxEVT_LISTBOX_DCLICK, item, IsSelected(item));
635 }
636
GTKOnSelectionChanged()637 void wxListBox::GTKOnSelectionChanged()
638 {
639 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
640 {
641 CalcAndSendEvent();
642 }
643 else // single selection
644 {
645 const int item = GetSelection();
646 if (item >= 0 && DoChangeSingleSelection(item))
647 SendEvent(wxEVT_LISTBOX, item, true);
648 }
649 }
650
GetSelection() const651 int wxListBox::GetSelection() const
652 {
653 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
654 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
655 wxT("must be single selection listbox"));
656
657 GtkTreeIter iter;
658 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
659
660 // only works on single-sel
661 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
662 return wxNOT_FOUND;
663
664 return GTKGetIndexFor(iter);
665 }
666
GetSelections(wxArrayInt & aSelections) const667 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
668 {
669 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
670
671 aSelections.Empty();
672
673 int i = 0;
674 GtkTreeIter iter;
675 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
676
677 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
678 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
679 do
680 {
681 if (gtk_tree_selection_iter_is_selected(selection, &iter))
682 aSelections.Add(i);
683
684 i++;
685 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
686 }
687
688 return aSelections.GetCount();
689 }
690
IsSelected(int n) const691 bool wxListBox::IsSelected( int n ) const
692 {
693 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
694
695 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
696
697 GtkTreeIter iter;
698 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
699
700 return gtk_tree_selection_iter_is_selected(selection, &iter) != 0;
701 }
702
DoSetSelection(int n,bool select)703 void wxListBox::DoSetSelection( int n, bool select )
704 {
705 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
706
707 GTKDisableEvents();
708
709 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
710
711 // passing -1 to SetSelection() is documented to deselect all items
712 if ( n == wxNOT_FOUND )
713 {
714 gtk_tree_selection_unselect_all(selection);
715 GTKEnableEvents();
716 return;
717 }
718
719 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
720
721
722 GtkTreeIter iter;
723 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
724
725 if (select)
726 gtk_tree_selection_select_iter(selection, &iter);
727 else
728 gtk_tree_selection_unselect_iter(selection, &iter);
729
730 GtkTreePath* path = gtk_tree_model_get_path(
731 GTK_TREE_MODEL(m_liststore), &iter);
732
733 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
734
735 gtk_tree_path_free(path);
736
737 GTKEnableEvents();
738 }
739
DoScrollToCell(int n,float alignY,float alignX)740 void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
741 {
742 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
743 wxCHECK_RET( IsValid(n), wxT("invalid index"));
744
745 //RN: I have no idea why this line is needed...
746 if (gtk_widget_has_grab(GTK_WIDGET(m_treeview)))
747 return;
748
749 GtkTreeIter iter;
750 if ( !GTKGetIteratorFor(n, &iter) )
751 return;
752
753 GtkTreePath* path = gtk_tree_model_get_path(
754 GTK_TREE_MODEL(m_liststore), &iter);
755
756 // Scroll to the desired cell (0.0 == topleft alignment)
757 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
758 TRUE, alignY, alignX);
759
760 gtk_tree_path_free(path);
761 }
762
DoSetFirstItem(int n)763 void wxListBox::DoSetFirstItem(int n)
764 {
765 DoScrollToCell(n, 0, 0);
766 }
767
EnsureVisible(int n)768 void wxListBox::EnsureVisible(int n)
769 {
770 DoScrollToCell(n, 0.5, 0);
771 }
772
773 // ----------------------------------------------------------------------------
774 // hittest
775 // ----------------------------------------------------------------------------
776
DoListHitTest(const wxPoint & point) const777 int wxListBox::DoListHitTest(const wxPoint& point) const
778 {
779 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
780 // we only want visible items we need to check for it manually here
781 if ( !GetClientRect().Contains(point) )
782 return wxNOT_FOUND;
783
784 // need to translate from master window since it is in client coords
785 gint binx, biny;
786 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
787 &binx, &biny, NULL, NULL);
788
789 GtkTreePath* path;
790 if ( !gtk_tree_view_get_path_at_pos
791 (
792 m_treeview,
793 point.x - binx,
794 point.y - biny,
795 &path,
796 NULL, // [out] column (always 0 here)
797 NULL, // [out] x-coord relative to the cell (not interested)
798 NULL // [out] y-coord relative to the cell
799 ) )
800 {
801 return wxNOT_FOUND;
802 }
803
804 int index = gtk_tree_path_get_indices(path)[0];
805 gtk_tree_path_free(path);
806
807 return index;
808 }
809
810 // ----------------------------------------------------------------------------
811 // helpers
812 // ----------------------------------------------------------------------------
813
GetConnectWidget()814 GtkWidget *wxListBox::GetConnectWidget()
815 {
816 // the correct widget for listbox events (such as mouse clicks for example)
817 // is m_treeview, not the parent scrolled window
818 return GTK_WIDGET(m_treeview);
819 }
820
GTKGetWindow(wxArrayGdkWindows & WXUNUSED (windows)) const821 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
822 {
823 return gtk_tree_view_get_bin_window(m_treeview);
824 }
825
DoApplyWidgetStyle(GtkRcStyle * style)826 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
827 {
828 #ifdef __WXGTK3__
829 // don't know if this is even necessary, or how to do it
830 #else
831 if (m_hasBgCol && m_backgroundColour.IsOk())
832 {
833 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
834 if (window)
835 {
836 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
837 gdk_window_set_background( window, m_backgroundColour.GetColor() );
838 gdk_window_clear( window );
839 }
840 }
841 #endif
842
843 GTKApplyStyle(GTK_WIDGET(m_treeview), style);
844 }
845
DoGetBestSize() const846 wxSize wxListBox::DoGetBestSize() const
847 {
848 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
849
850 // Start with a minimum size that's not too small
851 int cx, cy;
852 GetTextExtent( wxT("X"), &cx, &cy);
853 int lbWidth = 0;
854 int lbHeight = 10;
855
856 // Find the widest string.
857 const unsigned int count = GetCount();
858 if ( count )
859 {
860 int wLine;
861 for ( unsigned int i = 0; i < count; i++ )
862 {
863 GetTextExtent(GetString(i), &wLine, NULL);
864 if ( wLine > lbWidth )
865 lbWidth = wLine;
866 }
867 }
868
869 lbWidth += 3 * cx;
870
871 // And just a bit more for the checkbox if present and then some
872 // (these are rough guesses)
873 #if wxUSE_CHECKLISTBOX
874 if ( m_hasCheckBoxes )
875 {
876 lbWidth += 35;
877 cy = cy > 25 ? cy : 25; // rough height of checkbox
878 }
879 #endif
880
881 // Add room for the scrollbar
882 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
883
884 // Don't make the listbox too tall but don't make it too small neither
885 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
886
887 wxSize best(lbWidth, lbHeight);
888 CacheBestSize(best);
889 return best;
890 }
891
892 // static
893 wxVisualAttributes
GetClassDefaultAttributes(wxWindowVariant WXUNUSED (variant))894 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
895 {
896 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new(), true);
897 }
898
899 #endif // wxUSE_LISTBOX
900