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