]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
make sure wxNativeFontInfo is defined
[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 #include "wx/gtk/private.h"
29 #include "wx/gtk/private/object.h"
30 #include "wx/gtk/treeentry_gtk.h"
31
32 #if wxUSE_TOOLTIPS
33 #include "wx/tooltip.h"
34 #endif
35
36 #include <gtk/gtk.h>
37 #include <gdk/gdkkeysyms.h>
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44 extern bool g_blockEventsOnScroll;
45
46
47
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
51
52 #if wxUSE_CHECKLISTBOX
53 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54 #else
55 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
56 #endif // wxUSE_CHECKLISTBOX
57
58 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
59
60 // ----------------------------------------------------------------------------
61 // helper functions
62 // ----------------------------------------------------------------------------
63
64 namespace
65 {
66
67 // Return the entry for the given listbox item.
68 //
69 // Return value must be released by caller if non-NULL.
70 GtkTreeEntry *
71 GetEntry(GtkListStore *store, GtkTreeIter *iter, const wxListBox *listbox)
72 {
73 GtkTreeEntry* entry;
74 gtk_tree_model_get(GTK_TREE_MODEL(store),
75 iter,
76 WXLISTBOX_DATACOLUMN_ARG(listbox),
77 &entry,
78 -1);
79
80 return entry;
81 }
82
83 } // anonymous namespace
84
85 //-----------------------------------------------------------------------------
86 // "row-activated"
87 //-----------------------------------------------------------------------------
88
89 extern "C" {
90 static void
91 gtk_listbox_row_activated_callback(GtkTreeView * WXUNUSED(treeview),
92 GtkTreePath *path,
93 GtkTreeViewColumn * WXUNUSED(col),
94 wxListBox *listbox)
95 {
96 if (g_blockEventsOnDrag) return;
97 if (g_blockEventsOnScroll) return;
98
99 // This is triggered by either a double-click or a space press
100
101 int sel = gtk_tree_path_get_indices(path)[0];
102
103 listbox->GTKOnActivated(sel);
104 }
105 }
106
107 //-----------------------------------------------------------------------------
108 // "changed"
109 //-----------------------------------------------------------------------------
110
111 extern "C" {
112 static void
113 gtk_listitem_changed_callback(GtkTreeSelection * WXUNUSED(selection),
114 wxListBox *listbox )
115 {
116 if (g_blockEventsOnDrag) return;
117
118 listbox->GTKOnSelectionChanged();
119 }
120
121 }
122
123 //-----------------------------------------------------------------------------
124 // "key_press_event"
125 //-----------------------------------------------------------------------------
126
127 extern "C" {
128 static gboolean
129 gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget),
130 GdkEventKey *gdk_event,
131 wxListBox *listbox )
132 {
133 if ((gdk_event->keyval == GDK_Return) ||
134 (gdk_event->keyval == GDK_ISO_Enter) ||
135 (gdk_event->keyval == GDK_KP_Enter))
136 {
137 int index = -1;
138 if (!listbox->HasMultipleSelection())
139 index = listbox->GetSelection();
140 else
141 {
142 wxArrayInt sels;
143 if (listbox->GetSelections( sels ) < 1)
144 return FALSE;
145 index = sels[0];
146 }
147
148 if (index != wxNOT_FOUND)
149 {
150 listbox->GTKOnActivated(index);
151
152 // wxMac and wxMSW always invoke default action
153 // if (!ret)
154 {
155 // DClick not handled -> invoke default action
156 wxWindow *tlw = wxGetTopLevelParent( listbox );
157 if (tlw)
158 {
159 GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() );
160 if (gtk_window)
161 gtk_window_activate_default( gtk_window );
162 }
163 }
164
165 // Always intercept, otherwise we'd get another dclick
166 // event from row_activated
167 return TRUE;
168 }
169 }
170
171 return FALSE;
172 }
173 }
174
175 //-----------------------------------------------------------------------------
176 // GtkTreeEntry destruction (to destroy client data)
177 //-----------------------------------------------------------------------------
178
179 extern "C" {
180 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
181 wxListBox* listbox)
182 {
183 if (listbox->HasClientObjectData())
184 {
185 gpointer userdata = gtk_tree_entry_get_userdata(entry);
186 if (userdata)
187 delete (wxClientData *)userdata;
188 }
189 }
190 }
191
192 //-----------------------------------------------------------------------------
193 // Sorting callback (standard CmpNoCase return value)
194 //-----------------------------------------------------------------------------
195
196 extern "C" {
197 static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
198 GtkTreeIter *a,
199 GtkTreeIter *b,
200 wxListBox *listbox)
201 {
202 wxGtkObject<GtkTreeEntry> entry1(GetEntry(listbox->m_liststore, a, listbox));
203 wxCHECK_MSG(entry1, 0, wxT("Could not get first entry"));
204
205 wxGtkObject<GtkTreeEntry> 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 reccommend this)
210 return strcmp(gtk_tree_entry_get_collate_key(entry1),
211 gtk_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 gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
221 gint WXUNUSED(column),
222 const gchar* key,
223 GtkTreeIter* iter,
224 wxListBox* listbox)
225 {
226 wxGtkObject<GtkTreeEntry>
227 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, gtk_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 GTK_TYPE_TREE_ENTRY);
315 else
316 #endif
317 m_liststore = gtk_list_store_new(1, GTK_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 m_hasVMT = false;
404
405 Clear();
406 }
407
408 void wxListBox::GTKDisableEvents()
409 {
410 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
411
412 g_signal_handlers_block_by_func(selection,
413 (gpointer) gtk_listitem_changed_callback, this);
414 }
415
416 void wxListBox::GTKEnableEvents()
417 {
418 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
419
420 g_signal_handlers_unblock_by_func(selection,
421 (gpointer) gtk_listitem_changed_callback, this);
422
423 UpdateOldSelections();
424 }
425
426
427 void wxListBox::Update()
428 {
429 wxWindow::Update();
430
431 if (m_treeview)
432 gdk_window_process_updates(GTK_WIDGET(m_treeview)->window, TRUE);
433 }
434
435 // ----------------------------------------------------------------------------
436 // adding items
437 // ----------------------------------------------------------------------------
438
439 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
440 unsigned int pos,
441 void **clientData,
442 wxClientDataType type)
443 {
444 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
445
446 InvalidateBestSize();
447
448 GtkTreeIter* pIter = NULL; // append by default
449 GtkTreeIter iter;
450 if ( pos != GetCount() )
451 {
452 wxCHECK_MSG( GTKGetIteratorFor(pos, &iter), wxNOT_FOUND,
453 wxT("internal wxListBox error in insertion") );
454
455 pIter = &iter;
456 }
457
458 const unsigned int numItems = items.GetCount();
459 for ( unsigned int i = 0; i < numItems; ++i )
460 {
461 wxGtkObject<GtkTreeEntry> entry(gtk_tree_entry_new());
462 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
463 gtk_tree_entry_set_destroy_func(entry,
464 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
465 this);
466
467 GtkTreeIter itercur;
468 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
469
470 GTKSetItem(itercur, entry);
471
472 if (clientData)
473 AssignNewItemClientData(GTKGetIndexFor(itercur), clientData, i, type);
474 }
475
476 UpdateOldSelections();
477
478 return pos + numItems - 1;
479 }
480
481 // ----------------------------------------------------------------------------
482 // deleting items
483 // ----------------------------------------------------------------------------
484
485 void wxListBox::DoClear()
486 {
487 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
488
489 GTKDisableEvents(); // just in case
490
491 InvalidateBestSize();
492
493 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
494
495 GTKEnableEvents();
496 }
497
498 void wxListBox::DoDeleteOneItem(unsigned int n)
499 {
500 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
501
502 InvalidateBestSize();
503
504 GTKDisableEvents(); // just in case
505
506 GtkTreeIter iter;
507 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
508
509 // this returns false if iter is invalid (e.g. deleting item at end) but
510 // since we don't use iter, we ignore the return value
511 gtk_list_store_remove(m_liststore, &iter);
512
513 GTKEnableEvents();
514 }
515
516 // ----------------------------------------------------------------------------
517 // helper functions for working with iterators
518 // ----------------------------------------------------------------------------
519
520 bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
521 {
522 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
523 iter, NULL, pos) )
524 {
525 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
526 return false;
527 }
528
529 return true;
530 }
531
532 int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
533 {
534 GtkTreePath *path =
535 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
536
537 gint* pIntPath = gtk_tree_path_get_indices(path);
538
539 wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
540
541 int idx = pIntPath[0];
542
543 gtk_tree_path_free( path );
544
545 return idx;
546 }
547
548 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
549 GtkTreeEntry *wxListBox::GTKGetEntry(unsigned n) const
550 {
551 GtkTreeIter iter;
552 if ( !GTKGetIteratorFor(n, &iter) )
553 return NULL;
554
555 return GetEntry(m_liststore, &iter, this);
556 }
557
558 void wxListBox::GTKSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
559 {
560 #if wxUSE_CHECKLISTBOX
561 if ( m_hasCheckBoxes )
562 {
563 gtk_list_store_set(m_liststore, &iter,
564 0, FALSE, // FALSE == not toggled
565 1, entry,
566 -1);
567 }
568 else
569 #endif // wxUSE_CHECKLISTBOX
570 {
571 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
572 }
573 }
574
575 // ----------------------------------------------------------------------------
576 // client data
577 // ----------------------------------------------------------------------------
578
579 void* wxListBox::DoGetItemClientData(unsigned int n) const
580 {
581 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
582 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
583
584 return gtk_tree_entry_get_userdata( entry );
585 }
586
587 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
588 {
589 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
590 wxCHECK_RET(entry, wxT("could not get entry"));
591
592 gtk_tree_entry_set_userdata( entry, clientData );
593 }
594
595 // ----------------------------------------------------------------------------
596 // string list access
597 // ----------------------------------------------------------------------------
598
599 void wxListBox::SetString(unsigned int n, const wxString& label)
600 {
601 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
602 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
603
604 GtkTreeEntry* entry = GTKGetEntry(n);
605 wxCHECK_RET( entry, wxT("wrong listbox index") );
606
607 // update the item itself
608 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
609
610 // and update the model which will refresh the tree too
611 GtkTreeIter iter;
612 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("failed to get iterator") );
613
614 // FIXME: this resets the checked status of a wxCheckListBox item
615
616 GTKSetItem(iter, entry);
617 }
618
619 wxString wxListBox::GetString(unsigned int n) const
620 {
621 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
622
623 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
624 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
625
626 return wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
627 }
628
629 unsigned int wxListBox::GetCount() const
630 {
631 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
632
633 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
634 }
635
636 int wxListBox::FindString( const wxString &item, bool bCase ) const
637 {
638 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
639
640 //Sort of hackish - maybe there is a faster way
641 unsigned int nCount = wxListBox::GetCount();
642
643 for(unsigned int i = 0; i < nCount; ++i)
644 {
645 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
646 return (int)i;
647 }
648
649
650 // it's not an error if the string is not found -> no wxCHECK
651 return wxNOT_FOUND;
652 }
653
654 // ----------------------------------------------------------------------------
655 // selection
656 // ----------------------------------------------------------------------------
657
658 void wxListBox::GTKOnActivated(int item)
659 {
660 SendEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, item, IsSelected(item));
661 }
662
663 void wxListBox::GTKOnSelectionChanged()
664 {
665 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
666 {
667 CalcAndSendEvent();
668 }
669 else // single selection
670 {
671 const int item = GetSelection();
672 if ( DoChangeSingleSelection(item) )
673 SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, true);
674 }
675 }
676
677 int wxListBox::GetSelection() const
678 {
679 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
680 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
681 wxT("must be single selection listbox"));
682
683 GtkTreeIter iter;
684 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
685
686 // only works on single-sel
687 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
688 return wxNOT_FOUND;
689
690 return GTKGetIndexFor(iter);
691 }
692
693 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
694 {
695 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
696
697 aSelections.Empty();
698
699 int i = 0;
700 GtkTreeIter iter;
701 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
702
703 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
704 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
705 do
706 {
707 if (gtk_tree_selection_iter_is_selected(selection, &iter))
708 aSelections.Add(i);
709
710 i++;
711 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
712 }
713
714 return aSelections.GetCount();
715 }
716
717 bool wxListBox::IsSelected( int n ) const
718 {
719 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
720
721 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
722
723 GtkTreeIter iter;
724 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
725
726 return gtk_tree_selection_iter_is_selected(selection, &iter);
727 }
728
729 void wxListBox::DoSetSelection( int n, bool select )
730 {
731 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
732
733 GTKDisableEvents();
734
735 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
736
737 // passing -1 to SetSelection() is documented to deselect all items
738 if ( n == wxNOT_FOUND )
739 {
740 gtk_tree_selection_unselect_all(selection);
741 GTKEnableEvents();
742 return;
743 }
744
745 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
746
747
748 GtkTreeIter iter;
749 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
750
751 if (select)
752 gtk_tree_selection_select_iter(selection, &iter);
753 else
754 gtk_tree_selection_unselect_iter(selection, &iter);
755
756 GtkTreePath* path = gtk_tree_model_get_path(
757 GTK_TREE_MODEL(m_liststore), &iter);
758
759 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
760
761 gtk_tree_path_free(path);
762
763 GTKEnableEvents();
764 }
765
766 void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
767 {
768 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
769 wxCHECK_RET( IsValid(n), wxT("invalid index"));
770
771 //RN: I have no idea why this line is needed...
772 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
773 return;
774
775 GtkTreeIter iter;
776 if ( !GTKGetIteratorFor(n, &iter) )
777 return;
778
779 GtkTreePath* path = gtk_tree_model_get_path(
780 GTK_TREE_MODEL(m_liststore), &iter);
781
782 // Scroll to the desired cell (0.0 == topleft alignment)
783 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
784 TRUE, alignY, alignX);
785
786 gtk_tree_path_free(path);
787 }
788
789 void wxListBox::DoSetFirstItem(int n)
790 {
791 DoScrollToCell(n, 0, 0);
792 }
793
794 void wxListBox::EnsureVisible(int n)
795 {
796 DoScrollToCell(n, 0.5, 0);
797 }
798
799 // ----------------------------------------------------------------------------
800 // hittest
801 // ----------------------------------------------------------------------------
802
803 int wxListBox::DoListHitTest(const wxPoint& point) const
804 {
805 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
806 // we only want visible items we need to check for it manually here
807 if ( !GetClientRect().Contains(point) )
808 return wxNOT_FOUND;
809
810 // need to translate from master window since it is in client coords
811 gint binx, biny;
812 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
813 &binx, &biny, NULL, NULL, NULL);
814
815 GtkTreePath* path;
816 if ( !gtk_tree_view_get_path_at_pos
817 (
818 m_treeview,
819 point.x - binx,
820 point.y - biny,
821 &path,
822 NULL, // [out] column (always 0 here)
823 NULL, // [out] x-coord relative to the cell (not interested)
824 NULL // [out] y-coord relative to the cell
825 ) )
826 {
827 return wxNOT_FOUND;
828 }
829
830 int index = gtk_tree_path_get_indices(path)[0];
831 gtk_tree_path_free(path);
832
833 return index;
834 }
835
836 // ----------------------------------------------------------------------------
837 // helpers
838 // ----------------------------------------------------------------------------
839
840 #if wxUSE_TOOLTIPS
841 void wxListBox::GTKApplyToolTip( GtkTooltips *tips, const gchar *tip )
842 {
843 #if GTK_CHECK_VERSION(2, 12, 0)
844 if (!gtk_check_version(2, 12, 0))
845 {
846 gtk_widget_set_tooltip_text(GTK_WIDGET( m_treeview ), tip);
847 }
848 else
849 #endif
850 {
851 // RN: Is this needed anymore?
852 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, NULL );
853 }
854 }
855 #endif // wxUSE_TOOLTIPS
856
857 GtkWidget *wxListBox::GetConnectWidget()
858 {
859 // the correct widget for listbox events (such as mouse clicks for example)
860 // is m_treeview, not the parent scrolled window
861 return GTK_WIDGET(m_treeview);
862 }
863
864 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
865 {
866 return gtk_tree_view_get_bin_window(m_treeview);
867 }
868
869 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
870 {
871 if (m_hasBgCol && m_backgroundColour.Ok())
872 {
873 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
874 if (window)
875 {
876 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
877 gdk_window_set_background( window, m_backgroundColour.GetColor() );
878 gdk_window_clear( window );
879 }
880 }
881
882 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
883 }
884
885 wxSize wxListBox::DoGetBestSize() const
886 {
887 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
888
889 // Start with a minimum size that's not too small
890 int cx, cy;
891 GetTextExtent( wxT("X"), &cx, &cy);
892 int lbWidth = 0;
893 int lbHeight = 10;
894
895 // Find the widest string.
896 const unsigned int count = GetCount();
897 if ( count )
898 {
899 int wLine;
900 for ( unsigned int i = 0; i < count; i++ )
901 {
902 GetTextExtent(GetString(i), &wLine, NULL);
903 if ( wLine > lbWidth )
904 lbWidth = wLine;
905 }
906 }
907
908 lbWidth += 3 * cx;
909
910 // And just a bit more for the checkbox if present and then some
911 // (these are rough guesses)
912 #if wxUSE_CHECKLISTBOX
913 if ( m_hasCheckBoxes )
914 {
915 lbWidth += 35;
916 cy = cy > 25 ? cy : 25; // rough height of checkbox
917 }
918 #endif
919
920 // Add room for the scrollbar
921 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
922
923 // Don't make the listbox too tall but don't make it too small neither
924 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
925
926 wxSize best(lbWidth, lbHeight);
927 CacheBestSize(best);
928 return best;
929 }
930
931 // static
932 wxVisualAttributes
933 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
934 {
935 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
936 }
937
938 #endif // wxUSE_LISTBOX