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