[ 1816882 ] wxListBox::SetSelection() doesn't update the control
[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 *treeview,
66 GtkTreePath *path,
67 GtkTreeViewColumn *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 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
78 event.SetEventObject( listbox );
79
80 if (listbox->IsSelected(sel))
81 {
82 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
83
84 if (entry)
85 {
86 event.SetInt(sel);
87 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
88
89 if ( listbox->HasClientObjectData() )
90 event.SetClientObject( (wxClientData*) gtk_tree_entry_get_userdata(entry) );
91 else if ( listbox->HasClientUntypedData() )
92 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
93
94 g_object_unref (entry);
95 }
96 else
97 {
98 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
99 event.SetInt(-1);
100 }
101 }
102 else
103 {
104 event.SetInt(-1);
105 }
106
107 listbox->GetEventHandler()->ProcessEvent( event );
108 }
109 }
110
111 //-----------------------------------------------------------------------------
112 // "changed"
113 //-----------------------------------------------------------------------------
114
115 extern "C" {
116 static void
117 gtk_listitem_changed_callback( GtkTreeSelection* selection, wxListBox *listbox )
118 {
119 if (g_blockEventsOnDrag) return;
120
121 if (listbox->m_blockEvent) return;
122
123 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
124 event.SetEventObject( listbox );
125
126 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
127 {
128 wxArrayInt selections;
129 listbox->GetSelections( selections );
130
131 if (selections.GetCount() == 0)
132 {
133 // indicate that this is a deselection
134 event.SetExtraLong( 0 );
135 event.SetInt( -1 );
136
137 listbox->GetEventHandler()->ProcessEvent( event );
138
139 return;
140 }
141 else
142 {
143 // indicate that this is a selection
144 event.SetExtraLong( 1 );
145 event.SetInt( selections[0] );
146
147 listbox->GetEventHandler()->ProcessEvent( event );
148 }
149 }
150 else
151 {
152 int index = listbox->GetSelection();
153 if (index == wxNOT_FOUND)
154 {
155 // indicate that this is a deselection
156 event.SetExtraLong( 0 );
157 event.SetInt( -1 );
158
159 listbox->GetEventHandler()->ProcessEvent( event );
160
161 return;
162 }
163 else
164 {
165 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
166
167 // indicate that this is a selection
168 event.SetExtraLong( 1 );
169
170 event.SetInt( index );
171 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
172
173 if ( listbox->HasClientObjectData() )
174 event.SetClientObject(
175 (wxClientData*) gtk_tree_entry_get_userdata(entry)
176 );
177 else if ( listbox->HasClientUntypedData() )
178 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
179
180 listbox->GetEventHandler()->ProcessEvent( event );
181
182 g_object_unref (entry);
183 }
184 }
185 }
186 }
187
188 //-----------------------------------------------------------------------------
189 // GtkTreeEntry destruction (to destroy client data)
190 //-----------------------------------------------------------------------------
191
192 extern "C" {
193 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
194 wxListBox* listbox)
195 {
196 if (listbox->HasClientObjectData())
197 {
198 gpointer userdata = gtk_tree_entry_get_userdata(entry);
199 if (userdata)
200 delete (wxClientData *)userdata;
201 }
202 }
203 }
204
205 //-----------------------------------------------------------------------------
206 // Sorting callback (standard CmpNoCase return value)
207 //-----------------------------------------------------------------------------
208 #include <iostream>
209
210 extern "C" {
211 static gint gtk_listbox_sort_callback(GtkTreeModel *model,
212 GtkTreeIter *a,
213 GtkTreeIter *b,
214 wxListBox *listbox)
215 {
216 GtkTreeEntry* entry;
217 GtkTreeEntry* entry2;
218
219 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
220 a,
221 WXLISTBOX_DATACOLUMN_ARG(listbox),
222 &entry, -1);
223 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
224 b,
225 WXLISTBOX_DATACOLUMN_ARG(listbox),
226 &entry2, -1);
227 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
228 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
229
230 //We compare collate keys here instead of calling g_utf8_collate
231 //as it is rather slow (and even the docs reccommend this)
232 int ret = strcmp(gtk_tree_entry_get_collate_key(entry),
233 gtk_tree_entry_get_collate_key(entry2));
234
235 g_object_unref (entry);
236 g_object_unref (entry2);
237
238 return ret;
239 }
240 }
241
242 //-----------------------------------------------------------------------------
243 // Searching callback (TRUE == not equal, FALSE == equal)
244 //-----------------------------------------------------------------------------
245
246 extern "C" {
247 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
248 gint column,
249 const gchar* key,
250 GtkTreeIter* iter,
251 wxListBox* listbox)
252 {
253 GtkTreeEntry* entry;
254
255 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
256 iter,
257 WXLISTBOX_DATACOLUMN_ARG(listbox),
258 &entry, -1);
259 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
260 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
261
262 int ret = strcmp(keycollatekey,
263 gtk_tree_entry_get_collate_key(entry));
264
265 g_object_unref (entry);
266
267 return ret != 0;
268 }
269 }
270
271 //-----------------------------------------------------------------------------
272 // wxListBox
273 //-----------------------------------------------------------------------------
274
275 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
276
277 // ----------------------------------------------------------------------------
278 // construction
279 // ----------------------------------------------------------------------------
280
281 void wxListBox::Init()
282 {
283 m_treeview = (GtkTreeView*) NULL;
284 #if wxUSE_CHECKLISTBOX
285 m_hasCheckBoxes = false;
286 #endif // wxUSE_CHECKLISTBOX
287 }
288
289 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
290 const wxPoint &pos, const wxSize &size,
291 const wxArrayString& choices,
292 long style, const wxValidator& validator,
293 const wxString &name )
294 {
295 wxCArrayString chs(choices);
296
297 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
298 style, validator, name );
299 }
300
301 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
302 const wxPoint &pos, const wxSize &size,
303 int n, const wxString choices[],
304 long style, const wxValidator& validator,
305 const wxString &name )
306 {
307 m_blockEvent = false;
308
309 if (!PreCreation( parent, pos, size ) ||
310 !CreateBase( parent, id, pos, size, style, validator, name ))
311 {
312 wxFAIL_MSG( wxT("wxListBox creation failed") );
313 return false;
314 }
315
316 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
317 if (style & wxLB_ALWAYS_SB)
318 {
319 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
320 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
321 }
322 else
323 {
324 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
325 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
326 }
327
328
329 GtkScrolledWindowSetBorder(m_widget, style);
330
331 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
332
333 //wxListBox doesn't have a header :)
334 //NB: If enabled SetFirstItem doesn't work correctly
335 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
336
337 #if wxUSE_CHECKLISTBOX
338 if(m_hasCheckBoxes)
339 ((wxCheckListBox*)this)->DoCreateCheckList();
340 #endif // wxUSE_CHECKLISTBOX
341
342 // Create the data column
343 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
344 gtk_cell_renderer_text_new(),
345 "text",
346 WXLISTBOX_DATACOLUMN, NULL);
347
348 // Now create+set the model (GtkListStore) - first argument # of columns
349 #if wxUSE_CHECKLISTBOX
350 if(m_hasCheckBoxes)
351 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
352 GTK_TYPE_TREE_ENTRY);
353 else
354 #endif
355 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
356
357 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
358
359 g_object_unref (m_liststore); //free on treeview destruction
360
361 // Disable the pop-up textctrl that enables searching - note that
362 // the docs specify that even if this disabled (which we are doing)
363 // the user can still have it through the start-interactive-search
364 // key binding...either way we want to provide a searchequal callback
365 // NB: If this is enabled a doubleclick event (activate) gets sent
366 // on a successful search
367 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
368 gtk_tree_view_set_search_equal_func(m_treeview,
369 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
370 this,
371 NULL);
372
373 gtk_tree_view_set_enable_search(m_treeview, FALSE);
374
375
376 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
377
378 g_signal_connect_after (selection, "changed",
379 G_CALLBACK (gtk_listitem_changed_callback), this);
380
381 GtkSelectionMode mode;
382 if (style & wxLB_MULTIPLE)
383 {
384 mode = GTK_SELECTION_MULTIPLE;
385 }
386 else if (style & wxLB_EXTENDED)
387 {
388 mode = GTK_SELECTION_EXTENDED;
389 }
390 else
391 {
392 // if style was 0 set single mode
393 m_windowStyle |= wxLB_SINGLE;
394 mode = GTK_SELECTION_SINGLE;
395 }
396
397 gtk_tree_selection_set_mode( selection, mode );
398
399 // Handle sortable stuff
400 if(HasFlag(wxLB_SORT))
401 {
402 // Setup sorting in ascending (wx) order
403 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
404 WXLISTBOX_DATACOLUMN,
405 GTK_SORT_ASCENDING);
406
407 // Set the sort callback
408 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
409 WXLISTBOX_DATACOLUMN,
410 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
411 this, //userdata
412 NULL //"destroy notifier"
413 );
414 }
415
416
417 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
418
419 gtk_widget_show( GTK_WIDGET(m_treeview) );
420 m_focusWidget = GTK_WIDGET(m_treeview);
421
422 Append(n, choices); // insert initial items
423
424 // generate dclick events
425 g_signal_connect_after(m_treeview, "row-activated",
426 G_CALLBACK(gtk_listbox_row_activated_callback), this);
427
428 m_parent->DoAddChild( this );
429
430 PostCreation(size);
431 SetInitialSize(size); // need this too because this is a wxControlWithItems
432
433 return true;
434 }
435
436 wxListBox::~wxListBox()
437 {
438 m_hasVMT = false;
439
440 Clear();
441 }
442
443 // ----------------------------------------------------------------------------
444 // adding items
445 // ----------------------------------------------------------------------------
446
447 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
448 unsigned int pos,
449 void **clientData,
450 wxClientDataType type)
451 {
452 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
453
454 InvalidateBestSize();
455
456 GtkTreeIter* pIter = NULL; // append by default
457 GtkTreeIter iter;
458 if ( pos != GetCount() )
459 {
460 wxCHECK_MSG( GtkGetIteratorFor(pos, &iter), wxNOT_FOUND,
461 wxT("internal wxListBox error in insertion") );
462
463 pIter = &iter;
464 }
465
466 const unsigned int numItems = items.GetCount();
467 for ( unsigned int i = 0; i < numItems; ++i )
468 {
469 GtkTreeEntry* entry = gtk_tree_entry_new();
470 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
471 gtk_tree_entry_set_destroy_func(entry,
472 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
473 this);
474
475 if (clientData)
476 gtk_tree_entry_set_userdata(entry, clientData[i]);
477
478 GtkTreeIter itercur;
479 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
480
481 GtkSetItem(itercur, entry);
482
483 g_object_unref (entry);
484 }
485
486 return pos + numItems - 1;
487 }
488
489 // ----------------------------------------------------------------------------
490 // deleting items
491 // ----------------------------------------------------------------------------
492
493 void wxListBox::DoClear()
494 {
495 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
496
497 InvalidateBestSize();
498
499 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
500 }
501
502 void wxListBox::DoDeleteOneItem(unsigned int n)
503 {
504 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
505
506 InvalidateBestSize();
507
508 GtkTreeIter iter;
509 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
510
511 // this returns false if iter is invalid (e.g. deleting item at end) but
512 // since we don't use iter, we ignore the return value
513 gtk_list_store_remove(m_liststore, &iter);
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, _T("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
556 GtkTreeEntry* entry = NULL;
557 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
558 WXLISTBOX_DATACOLUMN, &entry, -1);
559
560 return entry;
561 }
562
563 void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
564 {
565 #if wxUSE_CHECKLISTBOX
566 if ( m_hasCheckBoxes )
567 {
568 gtk_list_store_set(m_liststore, &iter,
569 0, FALSE, // FALSE == not toggled
570 1, entry,
571 -1);
572 }
573 else
574 #endif // wxUSE_CHECKLISTBOX
575 {
576 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
577 }
578 }
579
580 // ----------------------------------------------------------------------------
581 // client data
582 // ----------------------------------------------------------------------------
583
584 void* wxListBox::DoGetItemClientData(unsigned int n) const
585 {
586 wxCHECK_MSG( IsValid(n), NULL,
587 wxT("Invalid index passed to GetItemClientData") );
588
589 GtkTreeEntry* entry = GtkGetEntry(n);
590 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
591
592 void* userdata = gtk_tree_entry_get_userdata( entry );
593 g_object_unref (entry);
594 return userdata;
595 }
596
597 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
598 {
599 wxCHECK_RET( IsValid(n),
600 wxT("Invalid index passed to SetItemClientData") );
601
602 GtkTreeEntry* entry = GtkGetEntry(n);
603 wxCHECK_RET(entry, wxT("could not get entry"));
604
605 gtk_tree_entry_set_userdata( entry, clientData );
606 g_object_unref (entry);
607 }
608
609 // ----------------------------------------------------------------------------
610 // string list access
611 // ----------------------------------------------------------------------------
612
613 void wxListBox::SetString(unsigned int n, const wxString& label)
614 {
615 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
616 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
617
618 GtkTreeEntry* entry = GtkGetEntry(n);
619 wxCHECK_RET( entry, wxT("wrong listbox index") );
620
621 // update the item itself
622 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
623
624 // and update the model which will refresh the tree too
625 GtkTreeIter iter;
626 wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
627
628 // FIXME: this resets the checked status of a wxCheckListBox item
629
630 GtkSetItem(iter, entry);
631 }
632
633 wxString wxListBox::GetString(unsigned int n) const
634 {
635 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
636
637 GtkTreeEntry* entry = GtkGetEntry(n);
638 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
639
640 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
641
642 g_object_unref (entry);
643 return label;
644 }
645
646 unsigned int wxListBox::GetCount() const
647 {
648 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
649
650 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
651 }
652
653 int wxListBox::FindString( const wxString &item, bool bCase ) const
654 {
655 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
656
657 //Sort of hackish - maybe there is a faster way
658 unsigned int nCount = wxListBox::GetCount();
659
660 for(unsigned int i = 0; i < nCount; ++i)
661 {
662 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
663 return (int)i;
664 }
665
666
667 // it's not an error if the string is not found -> no wxCHECK
668 return wxNOT_FOUND;
669 }
670
671 // ----------------------------------------------------------------------------
672 // selection
673 // ----------------------------------------------------------------------------
674
675 int wxListBox::GetSelection() const
676 {
677 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
678 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
679 wxT("must be single selection listbox"));
680
681 GtkTreeIter iter;
682 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
683
684 // only works on single-sel
685 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
686 return wxNOT_FOUND;
687
688 return GtkGetIndexFor(iter);
689 }
690
691 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
692 {
693 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
694
695 aSelections.Empty();
696
697 int i = 0;
698 GtkTreeIter iter;
699 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
700
701 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
702 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
703 do
704 {
705 if (gtk_tree_selection_iter_is_selected(selection, &iter))
706 aSelections.Add(i);
707
708 i++;
709 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
710 }
711
712 return aSelections.GetCount();
713 }
714
715 bool wxListBox::IsSelected( int n ) const
716 {
717 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
718
719 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
720
721 GtkTreeIter iter;
722 wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
723
724 return gtk_tree_selection_iter_is_selected(selection, &iter);
725 }
726
727 void wxListBox::DoSetSelection( int n, bool select )
728 {
729 // passing -1 to SetSelection() is documented to deselect all items
730 if ( n == wxNOT_FOUND )
731 {
732 // ... and not generate any events in the process
733 GtkDeselectAll();
734 return;
735 }
736
737 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
738
739 // don't generate the selection event
740 GtkSetSelection(n, select, true);
741 }
742
743 void wxListBox::GtkDeselectAll()
744 {
745 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
746
747 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
748
749 m_blockEvent = true;
750
751 gtk_tree_selection_unselect_all(selection);
752
753 m_blockEvent = false;
754 }
755
756 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
757 {
758 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
759
760 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
761
762 GtkTreeIter iter;
763 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
764
765 m_blockEvent = blockEvent;
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 m_blockEvent = false;
780 }
781
782 void wxListBox::DoSetFirstItem( int n )
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, 0.0f, 0.0f);
801
802 gtk_tree_path_free(path);
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::ApplyToolTip( GtkTooltips *tips, const gchar *tip )
848 {
849 // RN: Is this needed anymore?
850 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, (gchar*) NULL );
851 }
852 #endif // wxUSE_TOOLTIPS
853
854 GtkWidget *wxListBox::GetConnectWidget()
855 {
856 // the correct widget for listbox events (such as mouse clicks for example)
857 // is m_treeview, not the parent scrolled window
858 return GTK_WIDGET(m_treeview);
859 }
860
861 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
862 {
863 return gtk_tree_view_get_bin_window(m_treeview);
864 }
865
866 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
867 {
868 if (m_hasBgCol && m_backgroundColour.Ok())
869 {
870 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
871 if (window)
872 {
873 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
874 gdk_window_set_background( window, m_backgroundColour.GetColor() );
875 gdk_window_clear( window );
876 }
877 }
878
879 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
880 }
881
882 wxSize wxListBox::DoGetBestSize() const
883 {
884 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
885
886 // Start with a minimum size that's not too small
887 int cx, cy;
888 GetTextExtent( wxT("X"), &cx, &cy);
889 int lbWidth = 3 * cx;
890 int lbHeight = 10;
891
892 // Get the visible area of the tree view (limit to the 10th item
893 // so that it isn't too big)
894 unsigned int count = GetCount();
895 if (count)
896 {
897 int wLine;
898
899 // Find the widest line
900 for(unsigned int i = 0; i < count; i++) {
901 wxString str(GetString(i));
902 GetTextExtent(str, &wLine, NULL);
903 lbWidth = wxMax(lbWidth, wLine);
904 }
905
906 lbWidth += 3 * cx;
907
908 // And just a bit more for the checkbox if present and then some
909 // (these are rough guesses)
910 #if wxUSE_CHECKLISTBOX
911 if ( m_hasCheckBoxes )
912 {
913 lbWidth += 35;
914 cy = cy > 25 ? cy : 25; // rough height of checkbox
915 }
916 #endif
917
918 // don't make the listbox too tall (limit height to around 10 items) but don't
919 // make it too small neither
920 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
921 }
922
923 // Add room for the scrollbar
924 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
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