merging back XTI branch part 2
[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 wxCHECK_MSG( IsValid(n), NULL,
582 wxT("Invalid index passed to GetItemClientData") );
583
584 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
585 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
586
587 return gtk_tree_entry_get_userdata( entry );
588 }
589
590 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
591 {
592 wxCHECK_RET( IsValid(n),
593 wxT("Invalid index passed to SetItemClientData") );
594
595 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
596 wxCHECK_RET(entry, wxT("could not get entry"));
597
598 gtk_tree_entry_set_userdata( entry, clientData );
599 }
600
601 // ----------------------------------------------------------------------------
602 // string list access
603 // ----------------------------------------------------------------------------
604
605 void wxListBox::SetString(unsigned int n, const wxString& label)
606 {
607 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
608 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
609
610 GtkTreeEntry* entry = GTKGetEntry(n);
611 wxCHECK_RET( entry, wxT("wrong listbox index") );
612
613 // update the item itself
614 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
615
616 // and update the model which will refresh the tree too
617 GtkTreeIter iter;
618 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("failed to get iterator") );
619
620 // FIXME: this resets the checked status of a wxCheckListBox item
621
622 GTKSetItem(iter, entry);
623 }
624
625 wxString wxListBox::GetString(unsigned int n) const
626 {
627 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
628
629 wxGtkObject<GtkTreeEntry> entry(GTKGetEntry(n));
630 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
631
632 return wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
633 }
634
635 unsigned int wxListBox::GetCount() const
636 {
637 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
638
639 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
640 }
641
642 int wxListBox::FindString( const wxString &item, bool bCase ) const
643 {
644 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
645
646 //Sort of hackish - maybe there is a faster way
647 unsigned int nCount = wxListBox::GetCount();
648
649 for(unsigned int i = 0; i < nCount; ++i)
650 {
651 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
652 return (int)i;
653 }
654
655
656 // it's not an error if the string is not found -> no wxCHECK
657 return wxNOT_FOUND;
658 }
659
660 // ----------------------------------------------------------------------------
661 // selection
662 // ----------------------------------------------------------------------------
663
664 void wxListBox::GTKOnActivated(int item)
665 {
666 SendEvent(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, item, IsSelected(item));
667 }
668
669 void wxListBox::GTKOnSelectionChanged()
670 {
671 if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
672 {
673 CalcAndSendEvent();
674 }
675 else // single selection
676 {
677 const int item = GetSelection();
678 if ( DoChangeSingleSelection(item) )
679 SendEvent(wxEVT_COMMAND_LISTBOX_SELECTED, item, true);
680 }
681 }
682
683 int wxListBox::GetSelection() const
684 {
685 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
686 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
687 wxT("must be single selection listbox"));
688
689 GtkTreeIter iter;
690 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
691
692 // only works on single-sel
693 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
694 return wxNOT_FOUND;
695
696 return GTKGetIndexFor(iter);
697 }
698
699 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
700 {
701 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
702
703 aSelections.Empty();
704
705 int i = 0;
706 GtkTreeIter iter;
707 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
708
709 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
710 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
711 do
712 {
713 if (gtk_tree_selection_iter_is_selected(selection, &iter))
714 aSelections.Add(i);
715
716 i++;
717 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
718 }
719
720 return aSelections.GetCount();
721 }
722
723 bool wxListBox::IsSelected( int n ) const
724 {
725 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
726
727 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
728
729 GtkTreeIter iter;
730 wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
731
732 return gtk_tree_selection_iter_is_selected(selection, &iter);
733 }
734
735 void wxListBox::DoSetSelection( int n, bool select )
736 {
737 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
738
739 GTKDisableEvents();
740
741 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
742
743 // passing -1 to SetSelection() is documented to deselect all items
744 if ( n == wxNOT_FOUND )
745 {
746 gtk_tree_selection_unselect_all(selection);
747 GTKEnableEvents();
748 return;
749 }
750
751 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
752
753
754 GtkTreeIter iter;
755 wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
756
757 if (select)
758 gtk_tree_selection_select_iter(selection, &iter);
759 else
760 gtk_tree_selection_unselect_iter(selection, &iter);
761
762 GtkTreePath* path = gtk_tree_model_get_path(
763 GTK_TREE_MODEL(m_liststore), &iter);
764
765 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
766
767 gtk_tree_path_free(path);
768
769 GTKEnableEvents();
770 }
771
772 void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
773 {
774 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
775 wxCHECK_RET( IsValid(n), wxT("invalid index"));
776
777 //RN: I have no idea why this line is needed...
778 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
779 return;
780
781 GtkTreeIter iter;
782 if ( !GTKGetIteratorFor(n, &iter) )
783 return;
784
785 GtkTreePath* path = gtk_tree_model_get_path(
786 GTK_TREE_MODEL(m_liststore), &iter);
787
788 // Scroll to the desired cell (0.0 == topleft alignment)
789 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
790 TRUE, alignY, alignX);
791
792 gtk_tree_path_free(path);
793 }
794
795 void wxListBox::DoSetFirstItem(int n)
796 {
797 DoScrollToCell(n, 0, 0);
798 }
799
800 void wxListBox::EnsureVisible(int n)
801 {
802 DoScrollToCell(n, 0.5, 0);
803 }
804
805 // ----------------------------------------------------------------------------
806 // hittest
807 // ----------------------------------------------------------------------------
808
809 int wxListBox::DoListHitTest(const wxPoint& point) const
810 {
811 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
812 // we only want visible items we need to check for it manually here
813 if ( !GetClientRect().Contains(point) )
814 return wxNOT_FOUND;
815
816 // need to translate from master window since it is in client coords
817 gint binx, biny;
818 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
819 &binx, &biny, NULL, NULL, NULL);
820
821 GtkTreePath* path;
822 if ( !gtk_tree_view_get_path_at_pos
823 (
824 m_treeview,
825 point.x - binx,
826 point.y - biny,
827 &path,
828 NULL, // [out] column (always 0 here)
829 NULL, // [out] x-coord relative to the cell (not interested)
830 NULL // [out] y-coord relative to the cell
831 ) )
832 {
833 return wxNOT_FOUND;
834 }
835
836 int index = gtk_tree_path_get_indices(path)[0];
837 gtk_tree_path_free(path);
838
839 return index;
840 }
841
842 // ----------------------------------------------------------------------------
843 // helpers
844 // ----------------------------------------------------------------------------
845
846 #if wxUSE_TOOLTIPS
847 void wxListBox::GTKApplyToolTip( GtkTooltips *tips, const gchar *tip )
848 {
849 #if GTK_CHECK_VERSION(2, 12, 0)
850 if (!gtk_check_version(2, 12, 0))
851 {
852 gtk_widget_set_tooltip_text(GTK_WIDGET( m_treeview ), tip);
853 }
854 else
855 #endif
856 {
857 // RN: Is this needed anymore?
858 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, NULL );
859 }
860 }
861 #endif // wxUSE_TOOLTIPS
862
863 GtkWidget *wxListBox::GetConnectWidget()
864 {
865 // the correct widget for listbox events (such as mouse clicks for example)
866 // is m_treeview, not the parent scrolled window
867 return GTK_WIDGET(m_treeview);
868 }
869
870 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
871 {
872 return gtk_tree_view_get_bin_window(m_treeview);
873 }
874
875 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
876 {
877 if (m_hasBgCol && m_backgroundColour.Ok())
878 {
879 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
880 if (window)
881 {
882 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
883 gdk_window_set_background( window, m_backgroundColour.GetColor() );
884 gdk_window_clear( window );
885 }
886 }
887
888 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
889 }
890
891 wxSize wxListBox::DoGetBestSize() const
892 {
893 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
894
895 // Start with a minimum size that's not too small
896 int cx, cy;
897 GetTextExtent( wxT("X"), &cx, &cy);
898 int lbWidth = 0;
899 int lbHeight = 10;
900
901 // Find the widest string.
902 const unsigned int count = GetCount();
903 if ( count )
904 {
905 int wLine;
906 for ( unsigned int i = 0; i < count; i++ )
907 {
908 GetTextExtent(GetString(i), &wLine, NULL);
909 if ( wLine > lbWidth )
910 lbWidth = wLine;
911 }
912 }
913
914 lbWidth += 3 * cx;
915
916 // And just a bit more for the checkbox if present and then some
917 // (these are rough guesses)
918 #if wxUSE_CHECKLISTBOX
919 if ( m_hasCheckBoxes )
920 {
921 lbWidth += 35;
922 cy = cy > 25 ? cy : 25; // rough height of checkbox
923 }
924 #endif
925
926 // Add room for the scrollbar
927 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
928
929 // Don't make the listbox too tall but don't make it too small neither
930 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
931
932 wxSize best(lbWidth, lbHeight);
933 CacheBestSize(best);
934 return best;
935 }
936
937 // static
938 wxVisualAttributes
939 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
940 {
941 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
942 }
943
944 #endif // wxUSE_LISTBOX