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