]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
Performance optimization
[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
207 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
208 event.SetEventObject( listbox );
209
210 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
211
212 // indicate that this is a selection
213 event.SetExtraLong( 1 );
214
215 event.SetInt( index );
216 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
217
218 if ( listbox->HasClientObjectData() )
219 event.SetClientObject(
220 (wxClientData*) gtk_tree_entry_get_userdata(entry)
221 );
222 else if ( listbox->HasClientUntypedData() )
223 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
224
225 /* bool ret = */ listbox->HandleWindowEvent( event );
226
227 g_object_unref (entry);
228
229 // wxMac and wxMSW always invoke default action
230 // if (!ret)
231 {
232 // DClick not handled -> invoke default action
233 wxWindow *tlw = wxGetTopLevelParent( listbox );
234 if (tlw)
235 {
236 GtkWindow *gtk_window = GTK_WINDOW( tlw->GetHandle() );
237 if (gtk_window)
238 gtk_window_activate_default( gtk_window );
239 }
240 }
241
242 // Always intercept, otherwise we'd get another dclick
243 // event from row_activated
244 return TRUE;
245 }
246 }
247
248 return FALSE;
249 }
250 }
251
252 //-----------------------------------------------------------------------------
253 // GtkTreeEntry destruction (to destroy client data)
254 //-----------------------------------------------------------------------------
255
256 extern "C" {
257 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
258 wxListBox* listbox)
259 {
260 if (listbox->HasClientObjectData())
261 {
262 gpointer userdata = gtk_tree_entry_get_userdata(entry);
263 if (userdata)
264 delete (wxClientData *)userdata;
265 }
266 }
267 }
268
269 //-----------------------------------------------------------------------------
270 // Sorting callback (standard CmpNoCase return value)
271 //-----------------------------------------------------------------------------
272
273 extern "C" {
274 static gint gtk_listbox_sort_callback(GtkTreeModel * WXUNUSED(model),
275 GtkTreeIter *a,
276 GtkTreeIter *b,
277 wxListBox *listbox)
278 {
279 GtkTreeEntry* entry;
280 GtkTreeEntry* entry2;
281
282 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
283 a,
284 WXLISTBOX_DATACOLUMN_ARG(listbox),
285 &entry, -1);
286 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
287 b,
288 WXLISTBOX_DATACOLUMN_ARG(listbox),
289 &entry2, -1);
290 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
291 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
292
293 //We compare collate keys here instead of calling g_utf8_collate
294 //as it is rather slow (and even the docs reccommend this)
295 int ret = strcmp(gtk_tree_entry_get_collate_key(entry),
296 gtk_tree_entry_get_collate_key(entry2));
297
298 g_object_unref (entry);
299 g_object_unref (entry2);
300
301 return ret;
302 }
303 }
304
305 //-----------------------------------------------------------------------------
306 // Searching callback (TRUE == not equal, FALSE == equal)
307 //-----------------------------------------------------------------------------
308
309 extern "C" {
310 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
311 gint WXUNUSED(column),
312 const gchar* key,
313 GtkTreeIter* iter,
314 wxListBox* listbox)
315 {
316 GtkTreeEntry* entry;
317
318 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
319 iter,
320 WXLISTBOX_DATACOLUMN_ARG(listbox),
321 &entry, -1);
322 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
323 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
324
325 int ret = strcmp(keycollatekey,
326 gtk_tree_entry_get_collate_key(entry));
327
328 g_object_unref (entry);
329
330 return ret != 0;
331 }
332 }
333
334 //-----------------------------------------------------------------------------
335 // wxListBox
336 //-----------------------------------------------------------------------------
337
338 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControlWithItems)
339
340 // ----------------------------------------------------------------------------
341 // construction
342 // ----------------------------------------------------------------------------
343
344 void wxListBox::Init()
345 {
346 m_treeview = (GtkTreeView*) NULL;
347 #if wxUSE_CHECKLISTBOX
348 m_hasCheckBoxes = false;
349 #endif // wxUSE_CHECKLISTBOX
350 }
351
352 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
353 const wxPoint &pos, const wxSize &size,
354 const wxArrayString& choices,
355 long style, const wxValidator& validator,
356 const wxString &name )
357 {
358 wxCArrayString chs(choices);
359
360 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
361 style, validator, name );
362 }
363
364 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
365 const wxPoint &pos, const wxSize &size,
366 int n, const wxString choices[],
367 long style, const wxValidator& validator,
368 const wxString &name )
369 {
370 m_blockEvent = false;
371
372 if (!PreCreation( parent, pos, size ) ||
373 !CreateBase( parent, id, pos, size, style, validator, name ))
374 {
375 wxFAIL_MSG( wxT("wxListBox creation failed") );
376 return false;
377 }
378
379 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
380 if (style & wxLB_ALWAYS_SB)
381 {
382 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
383 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
384 }
385 else
386 {
387 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
388 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
389 }
390
391
392 GtkScrolledWindowSetBorder(m_widget, style);
393
394 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
395
396 //wxListBox doesn't have a header :)
397 //NB: If enabled SetFirstItem doesn't work correctly
398 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
399
400 #if wxUSE_CHECKLISTBOX
401 if(m_hasCheckBoxes)
402 ((wxCheckListBox*)this)->DoCreateCheckList();
403 #endif // wxUSE_CHECKLISTBOX
404
405 // Create the data column
406 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
407 gtk_cell_renderer_text_new(),
408 "text",
409 WXLISTBOX_DATACOLUMN, NULL);
410
411 // Now create+set the model (GtkListStore) - first argument # of columns
412 #if wxUSE_CHECKLISTBOX
413 if(m_hasCheckBoxes)
414 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
415 GTK_TYPE_TREE_ENTRY);
416 else
417 #endif
418 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
419
420 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
421
422 g_object_unref (m_liststore); //free on treeview destruction
423
424 // Disable the pop-up textctrl that enables searching - note that
425 // the docs specify that even if this disabled (which we are doing)
426 // the user can still have it through the start-interactive-search
427 // key binding...either way we want to provide a searchequal callback
428 // NB: If this is enabled a doubleclick event (activate) gets sent
429 // on a successful search
430 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
431 gtk_tree_view_set_search_equal_func(m_treeview,
432 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
433 this,
434 NULL);
435
436 gtk_tree_view_set_enable_search(m_treeview, FALSE);
437
438
439 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
440
441 g_signal_connect_after (selection, "changed",
442 G_CALLBACK (gtk_listitem_changed_callback), this);
443
444 GtkSelectionMode mode;
445 if (style & wxLB_MULTIPLE)
446 {
447 mode = GTK_SELECTION_MULTIPLE;
448 }
449 else if (style & wxLB_EXTENDED)
450 {
451 mode = GTK_SELECTION_EXTENDED;
452 }
453 else
454 {
455 // if style was 0 set single mode
456 m_windowStyle |= wxLB_SINGLE;
457 mode = GTK_SELECTION_SINGLE;
458 }
459
460 gtk_tree_selection_set_mode( selection, mode );
461
462 // Handle sortable stuff
463 if(HasFlag(wxLB_SORT))
464 {
465 // Setup sorting in ascending (wx) order
466 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
467 WXLISTBOX_DATACOLUMN,
468 GTK_SORT_ASCENDING);
469
470 // Set the sort callback
471 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
472 WXLISTBOX_DATACOLUMN,
473 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
474 this, //userdata
475 NULL //"destroy notifier"
476 );
477 }
478
479
480 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
481
482 gtk_widget_show( GTK_WIDGET(m_treeview) );
483 m_focusWidget = GTK_WIDGET(m_treeview);
484
485 Append(n, choices); // insert initial items
486
487 // generate dclick events
488 g_signal_connect_after(m_treeview, "row-activated",
489 G_CALLBACK(gtk_listbox_row_activated_callback), this);
490
491 // for intercepting dclick generation by <ENTER>
492 g_signal_connect (m_treeview, "key_press_event",
493 G_CALLBACK (gtk_listbox_key_press_callback),
494 this);
495 m_parent->DoAddChild( this );
496
497 PostCreation(size);
498 SetInitialSize(size); // need this too because this is a wxControlWithItems
499
500 return true;
501 }
502
503 wxListBox::~wxListBox()
504 {
505 m_hasVMT = false;
506
507 Clear();
508 }
509
510 // ----------------------------------------------------------------------------
511 // adding items
512 // ----------------------------------------------------------------------------
513
514 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
515 unsigned int pos,
516 void **clientData,
517 wxClientDataType type)
518 {
519 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
520
521 InvalidateBestSize();
522
523 GtkTreeIter* pIter = NULL; // append by default
524 GtkTreeIter iter;
525 if ( pos != GetCount() )
526 {
527 wxCHECK_MSG( GtkGetIteratorFor(pos, &iter), wxNOT_FOUND,
528 wxT("internal wxListBox error in insertion") );
529
530 pIter = &iter;
531 }
532
533 const unsigned int numItems = items.GetCount();
534 for ( unsigned int i = 0; i < numItems; ++i )
535 {
536 GtkTreeEntry* entry = gtk_tree_entry_new();
537 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
538 gtk_tree_entry_set_destroy_func(entry,
539 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
540 this);
541
542 GtkTreeIter itercur;
543 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
544
545 GtkSetItem(itercur, entry);
546
547 g_object_unref (entry);
548
549 if (clientData)
550 AssignNewItemClientData(GtkGetIndexFor(itercur), clientData, i, type);
551 }
552
553 return pos + numItems - 1;
554 }
555
556 // ----------------------------------------------------------------------------
557 // deleting items
558 // ----------------------------------------------------------------------------
559
560 void wxListBox::DoClear()
561 {
562 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
563
564 InvalidateBestSize();
565
566 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
567 }
568
569 void wxListBox::DoDeleteOneItem(unsigned int n)
570 {
571 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
572
573 InvalidateBestSize();
574
575 GtkTreeIter iter;
576 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
577
578 // this returns false if iter is invalid (e.g. deleting item at end) but
579 // since we don't use iter, we ignore the return value
580 gtk_list_store_remove(m_liststore, &iter);
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 // passing -1 to SetSelection() is documented to deselect all items
797 if ( n == wxNOT_FOUND )
798 {
799 // ... and not generate any events in the process
800 GtkDeselectAll();
801 return;
802 }
803
804 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
805
806 // don't generate the selection event
807 GtkSetSelection(n, select, true);
808 }
809
810 void wxListBox::GtkDeselectAll()
811 {
812 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
813
814 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
815
816 m_blockEvent = true;
817
818 gtk_tree_selection_unselect_all(selection);
819
820 m_blockEvent = false;
821 }
822
823 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
824 {
825 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
826
827 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
828
829 GtkTreeIter iter;
830 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
831
832 m_blockEvent = blockEvent;
833
834 if (select)
835 gtk_tree_selection_select_iter(selection, &iter);
836 else
837 gtk_tree_selection_unselect_iter(selection, &iter);
838
839 GtkTreePath* path = gtk_tree_model_get_path(
840 GTK_TREE_MODEL(m_liststore), &iter);
841
842 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
843
844 gtk_tree_path_free(path);
845
846 m_blockEvent = false;
847 }
848
849 void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
850 {
851 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
852 wxCHECK_RET( IsValid(n), wxT("invalid index"));
853
854 //RN: I have no idea why this line is needed...
855 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
856 return;
857
858 GtkTreeIter iter;
859 if ( !GtkGetIteratorFor(n, &iter) )
860 return;
861
862 GtkTreePath* path = gtk_tree_model_get_path(
863 GTK_TREE_MODEL(m_liststore), &iter);
864
865 // Scroll to the desired cell (0.0 == topleft alignment)
866 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
867 TRUE, alignY, alignX);
868
869 gtk_tree_path_free(path);
870 }
871
872 void wxListBox::DoSetFirstItem(int n)
873 {
874 DoScrollToCell(n, 0, 0);
875 }
876
877 void wxListBox::EnsureVisible(int n)
878 {
879 DoScrollToCell(n, 0.5, 0);
880 }
881
882 // ----------------------------------------------------------------------------
883 // hittest
884 // ----------------------------------------------------------------------------
885
886 int wxListBox::DoListHitTest(const wxPoint& point) const
887 {
888 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
889 // we only want visible items we need to check for it manually here
890 if ( !GetClientRect().Contains(point) )
891 return wxNOT_FOUND;
892
893 // need to translate from master window since it is in client coords
894 gint binx, biny;
895 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
896 &binx, &biny, NULL, NULL, NULL);
897
898 GtkTreePath* path;
899 if ( !gtk_tree_view_get_path_at_pos
900 (
901 m_treeview,
902 point.x - binx,
903 point.y - biny,
904 &path,
905 NULL, // [out] column (always 0 here)
906 NULL, // [out] x-coord relative to the cell (not interested)
907 NULL // [out] y-coord relative to the cell
908 ) )
909 {
910 return wxNOT_FOUND;
911 }
912
913 int index = gtk_tree_path_get_indices(path)[0];
914 gtk_tree_path_free(path);
915
916 return index;
917 }
918
919 // ----------------------------------------------------------------------------
920 // helpers
921 // ----------------------------------------------------------------------------
922
923 #if wxUSE_TOOLTIPS
924 void wxListBox::ApplyToolTip( GtkTooltips *tips, const gchar *tip )
925 {
926 // RN: Is this needed anymore?
927 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), tip, (gchar*) NULL );
928 }
929 #endif // wxUSE_TOOLTIPS
930
931 GtkWidget *wxListBox::GetConnectWidget()
932 {
933 // the correct widget for listbox events (such as mouse clicks for example)
934 // is m_treeview, not the parent scrolled window
935 return GTK_WIDGET(m_treeview);
936 }
937
938 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
939 {
940 return gtk_tree_view_get_bin_window(m_treeview);
941 }
942
943 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
944 {
945 if (m_hasBgCol && m_backgroundColour.Ok())
946 {
947 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
948 if (window)
949 {
950 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
951 gdk_window_set_background( window, m_backgroundColour.GetColor() );
952 gdk_window_clear( window );
953 }
954 }
955
956 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
957 }
958
959 wxSize wxListBox::DoGetBestSize() const
960 {
961 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
962
963 // Start with a minimum size that's not too small
964 int cx, cy;
965 GetTextExtent( wxT("X"), &cx, &cy);
966 int lbWidth = 3 * cx;
967 int lbHeight = 10;
968
969 // Get the visible area of the tree view (limit to the 10th item
970 // so that it isn't too big)
971 unsigned int count = GetCount();
972 if (count)
973 {
974 int wLine;
975
976 // Find the widest line
977 for(unsigned int i = 0; i < count; i++) {
978 wxString str(GetString(i));
979 GetTextExtent(str, &wLine, NULL);
980 lbWidth = wxMax(lbWidth, wLine);
981 }
982
983 lbWidth += 3 * cx;
984
985 // And just a bit more for the checkbox if present and then some
986 // (these are rough guesses)
987 #if wxUSE_CHECKLISTBOX
988 if ( m_hasCheckBoxes )
989 {
990 lbWidth += 35;
991 cy = cy > 25 ? cy : 25; // rough height of checkbox
992 }
993 #endif
994
995 // don't make the listbox too tall (limit height to around 10 items) but don't
996 // make it too small neither
997 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
998 }
999
1000 // Add room for the scrollbar
1001 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1002
1003 wxSize best(lbWidth, lbHeight);
1004 CacheBestSize(best);
1005 return best;
1006 }
1007
1008 // static
1009 wxVisualAttributes
1010 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1011 {
1012 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1013 }
1014
1015 #endif // wxUSE_LISTBOX