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