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