Remove m_blockEvents and use Disable/Enable instead, some more rearraging
[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 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
123 event.SetEventObject( listbox );
124
125 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
126 {
127 wxArrayInt selections;
128 listbox->GetSelections( selections );
129
130 if (selections.GetCount() == 0)
131 {
132 // indicate that this is a deselection
133 event.SetExtraLong( 0 );
134 event.SetInt( -1 );
135
136 listbox->HandleWindowEvent( event );
137
138 return;
139 }
140 else
141 {
142 // indicate that this is a selection
143 event.SetExtraLong( 1 );
144 event.SetInt( selections[0] );
145
146 listbox->HandleWindowEvent( event );
147 }
148 }
149 else
150 {
151 int index = listbox->GetSelection();
152 if (index == wxNOT_FOUND)
153 {
154 // indicate that this is a deselection
155 event.SetExtraLong( 0 );
156 event.SetInt( -1 );
157
158 listbox->HandleWindowEvent( event );
159
160 return;
161 }
162 else
163 {
164 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
165
166 // indicate that this is a selection
167 event.SetExtraLong( 1 );
168
169 event.SetInt( index );
170 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
171
172 if ( listbox->HasClientObjectData() )
173 event.SetClientObject(
174 (wxClientData*) gtk_tree_entry_get_userdata(entry)
175 );
176 else if ( listbox->HasClientUntypedData() )
177 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
178
179 listbox->HandleWindowEvent( event );
180
181 g_object_unref (entry);
182 }
183 }
184 }
185 }
186
187 //-----------------------------------------------------------------------------
188 // "key_press_event"
189 //-----------------------------------------------------------------------------
190
191 extern "C" {
192 static gint
193 gtk_listbox_key_press_callback( GtkWidget *WXUNUSED(widget),
194 GdkEventKey *gdk_event,
195 wxListBox *listbox )
196 {
197 if ((gdk_event->keyval == GDK_Return) ||
198 (gdk_event->keyval == GDK_ISO_Enter) ||
199 (gdk_event->keyval == GDK_KP_Enter))
200 {
201 int index = listbox->GetSelection();
202 if (index != wxNOT_FOUND)
203 {
204
205 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
206 event.SetEventObject( listbox );
207
208 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
209
210 // indicate that this is a selection
211 event.SetExtraLong( 1 );
212
213 event.SetInt( index );
214 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
215
216 if ( listbox->HasClientObjectData() )
217 event.SetClientObject(
218 (wxClientData*) gtk_tree_entry_get_userdata(entry)
219 );
220 else if ( listbox->HasClientUntypedData() )
221 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
222
223 /* bool ret = */ listbox->HandleWindowEvent( event );
224
225 g_object_unref (entry);
226
227 // wxMac and wxMSW always invoke default action
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 if (!PreCreation( parent, pos, size ) ||
369 !CreateBase( parent, id, pos, size, style, validator, name ))
370 {
371 wxFAIL_MSG( wxT("wxListBox creation failed") );
372 return false;
373 }
374
375 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
376 if (style & wxLB_ALWAYS_SB)
377 {
378 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
379 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
380 }
381 else
382 {
383 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
384 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
385 }
386
387
388 GtkScrolledWindowSetBorder(m_widget, style);
389
390 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
391
392 //wxListBox doesn't have a header :)
393 //NB: If enabled SetFirstItem doesn't work correctly
394 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
395
396 #if wxUSE_CHECKLISTBOX
397 if(m_hasCheckBoxes)
398 ((wxCheckListBox*)this)->DoCreateCheckList();
399 #endif // wxUSE_CHECKLISTBOX
400
401 // Create the data column
402 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
403 gtk_cell_renderer_text_new(),
404 "text",
405 WXLISTBOX_DATACOLUMN, NULL);
406
407 // Now create+set the model (GtkListStore) - first argument # of columns
408 #if wxUSE_CHECKLISTBOX
409 if(m_hasCheckBoxes)
410 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
411 GTK_TYPE_TREE_ENTRY);
412 else
413 #endif
414 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
415
416 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
417
418 g_object_unref (m_liststore); //free on treeview destruction
419
420 // Disable the pop-up textctrl that enables searching - note that
421 // the docs specify that even if this disabled (which we are doing)
422 // the user can still have it through the start-interactive-search
423 // key binding...either way we want to provide a searchequal callback
424 // NB: If this is enabled a doubleclick event (activate) gets sent
425 // on a successful search
426 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
427 gtk_tree_view_set_search_equal_func(m_treeview,
428 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
429 this,
430 NULL);
431
432 gtk_tree_view_set_enable_search(m_treeview, FALSE);
433
434 GtkSelectionMode mode;
435 if (style & wxLB_MULTIPLE)
436 {
437 mode = GTK_SELECTION_MULTIPLE;
438 }
439 else if (style & wxLB_EXTENDED)
440 {
441 mode = GTK_SELECTION_EXTENDED;
442 }
443 else
444 {
445 // if style was 0 set single mode
446 m_windowStyle |= wxLB_SINGLE;
447 mode = GTK_SELECTION_SINGLE;
448 }
449
450 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
451 gtk_tree_selection_set_mode( selection, mode );
452
453 // Handle sortable stuff
454 if(HasFlag(wxLB_SORT))
455 {
456 // Setup sorting in ascending (wx) order
457 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
458 WXLISTBOX_DATACOLUMN,
459 GTK_SORT_ASCENDING);
460
461 // Set the sort callback
462 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
463 WXLISTBOX_DATACOLUMN,
464 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
465 this, //userdata
466 NULL //"destroy notifier"
467 );
468 }
469
470
471 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
472
473 gtk_widget_show( GTK_WIDGET(m_treeview) );
474 m_focusWidget = GTK_WIDGET(m_treeview);
475
476 Append(n, choices); // insert initial items
477
478 // generate dclick events
479 g_signal_connect_after(m_treeview, "row-activated",
480 G_CALLBACK(gtk_listbox_row_activated_callback), this);
481
482 // for intercepting dclick generation by <ENTER>
483 g_signal_connect (m_treeview, "key_press_event",
484 G_CALLBACK (gtk_listbox_key_press_callback),
485 this);
486 m_parent->DoAddChild( this );
487
488 PostCreation(size);
489 SetInitialSize(size); // need this too because this is a wxControlWithItems
490
491 g_signal_connect_after (selection, "changed",
492 G_CALLBACK (gtk_listitem_changed_callback), this);
493
494 return true;
495 }
496
497 wxListBox::~wxListBox()
498 {
499 m_hasVMT = false;
500
501 Clear();
502 }
503
504 void wxListBox::GtkDisableEvents()
505 {
506 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
507
508 g_signal_handlers_block_by_func(selection,
509 (gpointer) gtk_listitem_changed_callback, this);
510 }
511
512 void wxListBox::GtkEnableEvents()
513 {
514 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
515
516 g_signal_handlers_unblock_by_func(selection,
517 (gpointer) gtk_listitem_changed_callback, this);
518
519 GtkUpdateOldSelection();
520 }
521
522 // ----------------------------------------------------------------------------
523 // adding items
524 // ----------------------------------------------------------------------------
525
526 int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
527 unsigned int pos,
528 void **clientData,
529 wxClientDataType type)
530 {
531 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
532
533 InvalidateBestSize();
534
535 GtkTreeIter* pIter = NULL; // append by default
536 GtkTreeIter iter;
537 if ( pos != GetCount() )
538 {
539 wxCHECK_MSG( GtkGetIteratorFor(pos, &iter), wxNOT_FOUND,
540 wxT("internal wxListBox error in insertion") );
541
542 pIter = &iter;
543 }
544
545 const unsigned int numItems = items.GetCount();
546 for ( unsigned int i = 0; i < numItems; ++i )
547 {
548 GtkTreeEntry* entry = gtk_tree_entry_new();
549 gtk_tree_entry_set_label(entry, wxGTK_CONV(items[i]));
550 gtk_tree_entry_set_destroy_func(entry,
551 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
552 this);
553
554 GtkTreeIter itercur;
555 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
556
557 GtkSetItem(itercur, entry);
558
559 g_object_unref (entry);
560
561 if (clientData)
562 AssignNewItemClientData(GtkGetIndexFor(itercur), clientData, i, type);
563 }
564
565 return pos + numItems - 1;
566 }
567
568 // ----------------------------------------------------------------------------
569 // deleting items
570 // ----------------------------------------------------------------------------
571
572 void wxListBox::DoClear()
573 {
574 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
575
576 InvalidateBestSize();
577
578 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
579 }
580
581 void wxListBox::DoDeleteOneItem(unsigned int n)
582 {
583 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
584
585 InvalidateBestSize();
586
587 GtkTreeIter iter;
588 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("wrong listbox index") );
589
590 // this returns false if iter is invalid (e.g. deleting item at end) but
591 // since we don't use iter, we ignore the return value
592 gtk_list_store_remove(m_liststore, &iter);
593 }
594
595 // ----------------------------------------------------------------------------
596 // helper functions for working with iterators
597 // ----------------------------------------------------------------------------
598
599 bool wxListBox::GtkGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
600 {
601 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
602 iter, NULL, pos) )
603 {
604 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
605 return false;
606 }
607
608 return true;
609 }
610
611 int wxListBox::GtkGetIndexFor(GtkTreeIter& iter) const
612 {
613 GtkTreePath *path =
614 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
615
616 gint* pIntPath = gtk_tree_path_get_indices(path);
617
618 wxCHECK_MSG( pIntPath, wxNOT_FOUND, _T("failed to get iterator path") );
619
620 int idx = pIntPath[0];
621
622 gtk_tree_path_free( path );
623
624 return idx;
625 }
626
627 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
628 GtkTreeEntry *wxListBox::GtkGetEntry(unsigned n) const
629 {
630 GtkTreeIter iter;
631 if ( !GtkGetIteratorFor(n, &iter) )
632 return NULL;
633
634
635 GtkTreeEntry* entry = NULL;
636 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
637 WXLISTBOX_DATACOLUMN, &entry, -1);
638
639 return entry;
640 }
641
642 void wxListBox::GtkSetItem(GtkTreeIter& iter, const GtkTreeEntry *entry)
643 {
644 #if wxUSE_CHECKLISTBOX
645 if ( m_hasCheckBoxes )
646 {
647 gtk_list_store_set(m_liststore, &iter,
648 0, FALSE, // FALSE == not toggled
649 1, entry,
650 -1);
651 }
652 else
653 #endif // wxUSE_CHECKLISTBOX
654 {
655 gtk_list_store_set(m_liststore, &iter, 0, entry, -1);
656 }
657 }
658
659 // ----------------------------------------------------------------------------
660 // client data
661 // ----------------------------------------------------------------------------
662
663 void* wxListBox::DoGetItemClientData(unsigned int n) const
664 {
665 wxCHECK_MSG( IsValid(n), NULL,
666 wxT("Invalid index passed to GetItemClientData") );
667
668 GtkTreeEntry* entry = GtkGetEntry(n);
669 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
670
671 void* userdata = gtk_tree_entry_get_userdata( entry );
672 g_object_unref (entry);
673 return userdata;
674 }
675
676 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
677 {
678 wxCHECK_RET( IsValid(n),
679 wxT("Invalid index passed to SetItemClientData") );
680
681 GtkTreeEntry* entry = GtkGetEntry(n);
682 wxCHECK_RET(entry, wxT("could not get entry"));
683
684 gtk_tree_entry_set_userdata( entry, clientData );
685 g_object_unref (entry);
686 }
687
688 // ----------------------------------------------------------------------------
689 // string list access
690 // ----------------------------------------------------------------------------
691
692 void wxListBox::SetString(unsigned int n, const wxString& label)
693 {
694 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
695 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
696
697 GtkTreeEntry* entry = GtkGetEntry(n);
698 wxCHECK_RET( entry, wxT("wrong listbox index") );
699
700 // update the item itself
701 gtk_tree_entry_set_label(entry, wxGTK_CONV(label));
702
703 // and update the model which will refresh the tree too
704 GtkTreeIter iter;
705 wxCHECK_RET( GtkGetIteratorFor(n, &iter), _T("failed to get iterator") );
706
707 // FIXME: this resets the checked status of a wxCheckListBox item
708
709 GtkSetItem(iter, entry);
710 }
711
712 wxString wxListBox::GetString(unsigned int n) const
713 {
714 wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
715
716 GtkTreeEntry* entry = GtkGetEntry(n);
717 wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
718
719 wxString label = wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry) );
720
721 g_object_unref (entry);
722 return label;
723 }
724
725 unsigned int wxListBox::GetCount() const
726 {
727 wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
728
729 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
730 }
731
732 int wxListBox::FindString( const wxString &item, bool bCase ) const
733 {
734 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
735
736 //Sort of hackish - maybe there is a faster way
737 unsigned int nCount = wxListBox::GetCount();
738
739 for(unsigned int i = 0; i < nCount; ++i)
740 {
741 if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
742 return (int)i;
743 }
744
745
746 // it's not an error if the string is not found -> no wxCHECK
747 return wxNOT_FOUND;
748 }
749
750 // ----------------------------------------------------------------------------
751 // selection
752 // ----------------------------------------------------------------------------
753
754 int wxListBox::GetSelection() const
755 {
756 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
757 wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
758 wxT("must be single selection listbox"));
759
760 GtkTreeIter iter;
761 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
762
763 // only works on single-sel
764 if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
765 return wxNOT_FOUND;
766
767 return GtkGetIndexFor(iter);
768 }
769
770 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
771 {
772 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
773
774 aSelections.Empty();
775
776 int i = 0;
777 GtkTreeIter iter;
778 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
779
780 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
781 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
782 do
783 {
784 if (gtk_tree_selection_iter_is_selected(selection, &iter))
785 aSelections.Add(i);
786
787 i++;
788 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
789 }
790
791 return aSelections.GetCount();
792 }
793
794 bool wxListBox::IsSelected( int n ) const
795 {
796 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
797
798 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
799
800 GtkTreeIter iter;
801 wxCHECK_MSG( GtkGetIteratorFor(n, &iter), false, wxT("Invalid index") );
802
803 return gtk_tree_selection_iter_is_selected(selection, &iter);
804 }
805
806 void wxListBox::DoSetSelection( int n, bool select )
807 {
808 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
809
810 GtkDisableEvents();
811
812 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
813
814 // passing -1 to SetSelection() is documented to deselect all items
815 if ( n == wxNOT_FOUND )
816 {
817 gtk_tree_selection_unselect_all(selection);
818 GtkEnableEvents();
819 return;
820 }
821
822 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
823
824
825 GtkTreeIter iter;
826 wxCHECK_RET( GtkGetIteratorFor(n, &iter), wxT("Invalid index") );
827
828 if (select)
829 gtk_tree_selection_select_iter(selection, &iter);
830 else
831 gtk_tree_selection_unselect_iter(selection, &iter);
832
833 GtkTreePath* path = gtk_tree_model_get_path(
834 GTK_TREE_MODEL(m_liststore), &iter);
835
836 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
837
838 gtk_tree_path_free(path);
839
840 GtkEnableEvents();
841 }
842
843 void wxListBox::GtkUpdateOldSelection()
844 {
845 if (HasFlag(wxLB_MULTIPLE))
846 GetSelections( m_oldSelection );
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 = 0;
967 int lbHeight = 10;
968
969 // Find the widest string.
970 const unsigned int count = GetCount();
971 if ( count )
972 {
973 int wLine;
974 for ( unsigned int i = 0; i < count; i++ )
975 {
976 GetTextExtent(GetString(i), &wLine, NULL);
977 if ( wLine > lbWidth )
978 lbWidth = wLine;
979 }
980 }
981
982 lbWidth += 3 * cx;
983
984 // And just a bit more for the checkbox if present and then some
985 // (these are rough guesses)
986 #if wxUSE_CHECKLISTBOX
987 if ( m_hasCheckBoxes )
988 {
989 lbWidth += 35;
990 cy = cy > 25 ? cy : 25; // rough height of checkbox
991 }
992 #endif
993
994 // Add room for the scrollbar
995 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
996
997 // Don't make the listbox too tall but don't make it too small neither
998 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
999
1000 wxSize best(lbWidth, lbHeight);
1001 CacheBestSize(best);
1002 return best;
1003 }
1004
1005 // static
1006 wxVisualAttributes
1007 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1008 {
1009 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1010 }
1011
1012 #endif // wxUSE_LISTBOX