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