]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/listbox.cpp
add a little extra padding around text in new tab theme
[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 <gdk/gdk.h>
36 #include <gtk/gtk.h>
37 #include <gdk/gdkkeysyms.h>
38
39 //-----------------------------------------------------------------------------
40 // data
41 //-----------------------------------------------------------------------------
42
43 extern bool g_blockEventsOnDrag;
44 extern bool g_blockEventsOnScroll;
45
46
47
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
51
52 #if wxUSE_CHECKLISTBOX
53 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54 #else
55 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
56 #endif // wxUSE_CHECKLISTBOX
57
58 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
59
60 //-----------------------------------------------------------------------------
61 // "row-activated"
62 //-----------------------------------------------------------------------------
63
64 extern "C" {
65 static void
66 gtk_listbox_row_activated_callback(GtkTreeView *treeview,
67 GtkTreePath *path,
68 GtkTreeViewColumn *col,
69 wxListBox *listbox)
70 {
71 if (g_isIdle) wxapp_install_idle_handler();
72
73 if (g_blockEventsOnDrag) return;
74 if (g_blockEventsOnScroll) return;
75
76 // This is triggered by either a double-click or a space press
77
78 int sel = gtk_tree_path_get_indices(path)[0];
79
80 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, listbox->GetId() );
81 event.SetEventObject( listbox );
82
83 if (listbox->IsSelected(sel))
84 {
85 GtkTreeEntry* entry = listbox->GtkGetEntry(sel);
86
87 if (entry)
88 {
89 event.SetInt(sel);
90 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
91
92 if ( listbox->HasClientObjectData() )
93 event.SetClientObject( (wxClientData*) gtk_tree_entry_get_userdata(entry) );
94 else if ( listbox->HasClientUntypedData() )
95 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
96
97 g_object_unref (entry);
98 }
99 else
100 {
101 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
102 event.SetInt(-1);
103 }
104 }
105 else
106 {
107 event.SetInt(-1);
108 }
109
110 listbox->GetEventHandler()->ProcessEvent( event );
111 }
112 }
113
114 //-----------------------------------------------------------------------------
115 // "key_press_event"
116 //-----------------------------------------------------------------------------
117
118 extern "C" {
119 static gint
120 gtk_listbox_key_press_callback( GtkWidget *widget,
121 GdkEventKey *gdk_event,
122 wxListBox *listbox )
123 {
124 if (g_blockEventsOnDrag) return FALSE;
125
126 if ((gdk_event->keyval == GDK_Tab) || (gdk_event->keyval == GDK_ISO_Left_Tab))
127 {
128 wxNavigationKeyEvent new_event;
129 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
130 new_event.SetDirection( (gdk_event->keyval == GDK_Tab) );
131 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
132 new_event.SetWindowChange( (gdk_event->state & GDK_CONTROL_MASK) );
133 new_event.SetCurrentFocus( listbox );
134 if (listbox->GetEventHandler()->ProcessEvent( new_event ))
135 return TRUE;
136 }
137
138 return FALSE;
139 }
140 }
141
142 //-----------------------------------------------------------------------------
143 // "changed"
144 //-----------------------------------------------------------------------------
145
146 extern "C" {
147 static void
148 gtk_listitem_changed_callback( GtkTreeSelection* selection, wxListBox *listbox )
149 {
150 if (g_blockEventsOnDrag) return;
151
152 if (listbox->m_blockEvent) return;
153
154 wxCommandEvent event(wxEVT_COMMAND_LISTBOX_SELECTED, listbox->GetId() );
155 event.SetEventObject( listbox );
156
157 if (listbox->HasFlag(wxLB_MULTIPLE) || listbox->HasFlag(wxLB_EXTENDED))
158 {
159 wxArrayInt selections;
160 listbox->GetSelections( selections );
161
162 if (selections.GetCount() == 0)
163 {
164 // indicate that this is a deselection
165 event.SetExtraLong( 0 );
166 event.SetInt( -1 );
167
168 listbox->GetEventHandler()->ProcessEvent( event );
169
170 return;
171 }
172 else
173 {
174 // indicate that this is a selection
175 event.SetExtraLong( 1 );
176 event.SetInt( selections[0] );
177
178 listbox->GetEventHandler()->ProcessEvent( event );
179 }
180 }
181 else
182 {
183 int index = listbox->GetSelection();
184 if (index == wxNOT_FOUND)
185 {
186 // indicate that this is a deselection
187 event.SetExtraLong( 0 );
188 event.SetInt( -1 );
189
190 listbox->GetEventHandler()->ProcessEvent( event );
191
192 return;
193 }
194 else
195 {
196 GtkTreeEntry* entry = listbox->GtkGetEntry( index );
197
198 // indicate that this is a selection
199 event.SetExtraLong( 1 );
200
201 event.SetInt( index );
202 event.SetString(wxConvUTF8.cMB2WX(gtk_tree_entry_get_label(entry)));
203
204 if ( listbox->HasClientObjectData() )
205 event.SetClientObject(
206 (wxClientData*) gtk_tree_entry_get_userdata(entry)
207 );
208 else if ( listbox->HasClientUntypedData() )
209 event.SetClientData( gtk_tree_entry_get_userdata(entry) );
210
211 listbox->GetEventHandler()->ProcessEvent( event );
212
213 g_object_unref (entry);
214 }
215 }
216 }
217 }
218
219 //-----------------------------------------------------------------------------
220 // GtkTreeEntry destruction (to destroy client data)
221 //-----------------------------------------------------------------------------
222
223 extern "C" {
224 static void gtk_tree_entry_destroy_cb(GtkTreeEntry* entry,
225 wxListBox* listbox)
226 {
227 if (listbox->HasClientObjectData())
228 {
229 gpointer userdata = gtk_tree_entry_get_userdata(entry);
230 if (userdata)
231 delete (wxClientData *)userdata;
232 }
233 }
234 }
235
236 //-----------------------------------------------------------------------------
237 // Sorting callback (standard CmpNoCase return value)
238 //-----------------------------------------------------------------------------
239
240 extern "C" {
241 static gint gtk_listbox_sort_callback(GtkTreeModel *model,
242 GtkTreeIter *a,
243 GtkTreeIter *b,
244 wxListBox *listbox)
245 {
246 GtkTreeEntry* entry;
247 GtkTreeEntry* entry2;
248
249 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
250 a,
251 WXLISTBOX_DATACOLUMN_ARG(listbox),
252 &entry, -1);
253 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
254 b,
255 WXLISTBOX_DATACOLUMN_ARG(listbox),
256 &entry2, -1);
257 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
258 wxCHECK_MSG(entry2, 0, wxT("Could not get entry2"));
259
260 //We compare collate keys here instead of calling g_utf8_collate
261 //as it is rather slow (and even the docs reccommend this)
262 int ret = strcasecmp(gtk_tree_entry_get_collate_key(entry),
263 gtk_tree_entry_get_collate_key(entry2));
264
265 g_object_unref (entry);
266 g_object_unref (entry2);
267
268 return ret;
269 }
270 }
271
272 //-----------------------------------------------------------------------------
273 // Searching callback (TRUE == not equal, FALSE == equal)
274 //-----------------------------------------------------------------------------
275
276 extern "C" {
277 static gboolean gtk_listbox_searchequal_callback(GtkTreeModel* model,
278 gint column,
279 const gchar* key,
280 GtkTreeIter* iter,
281 wxListBox* listbox)
282 {
283 GtkTreeEntry* entry;
284
285 gtk_tree_model_get(GTK_TREE_MODEL(listbox->m_liststore),
286 iter,
287 WXLISTBOX_DATACOLUMN_ARG(listbox),
288 &entry, -1);
289 wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
290 wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
291
292 int ret = strcasecmp(keycollatekey,
293 gtk_tree_entry_get_collate_key(entry));
294
295 g_object_unref (entry);
296
297 return ret != 0;
298 }
299 }
300
301 //-----------------------------------------------------------------------------
302 // wxListBox
303 //-----------------------------------------------------------------------------
304
305 IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
306
307 // ----------------------------------------------------------------------------
308 // construction
309 // ----------------------------------------------------------------------------
310
311 void wxListBox::Init()
312 {
313 m_treeview = (GtkTreeView*) NULL;
314 #if wxUSE_CHECKLISTBOX
315 m_hasCheckBoxes = false;
316 #endif // wxUSE_CHECKLISTBOX
317 }
318
319 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
320 const wxPoint &pos, const wxSize &size,
321 const wxArrayString& choices,
322 long style, const wxValidator& validator,
323 const wxString &name )
324 {
325 wxCArrayString chs(choices);
326
327 return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
328 style, validator, name );
329 }
330
331 bool wxListBox::Create( wxWindow *parent, wxWindowID id,
332 const wxPoint &pos, const wxSize &size,
333 int n, const wxString choices[],
334 long style, const wxValidator& validator,
335 const wxString &name )
336 {
337 m_needParent = true;
338 m_acceptsFocus = true;
339 m_blockEvent = false;
340
341 if (!PreCreation( parent, pos, size ) ||
342 !CreateBase( parent, id, pos, size, style, validator, name ))
343 {
344 wxFAIL_MSG( wxT("wxListBox creation failed") );
345 return false;
346 }
347
348 m_widget = gtk_scrolled_window_new( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
349 if (style & wxLB_ALWAYS_SB)
350 {
351 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
352 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
353 }
354 else
355 {
356 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
357 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
358 }
359
360
361 GtkScrolledWindowSetBorder(m_widget, style);
362
363 m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
364
365 //wxListBox doesn't have a header :)
366 //NB: If enabled SetFirstItem doesn't work correctly
367 gtk_tree_view_set_headers_visible(m_treeview, FALSE);
368
369 #if wxUSE_CHECKLISTBOX
370 if(m_hasCheckBoxes)
371 ((wxCheckListBox*)this)->DoCreateCheckList();
372 #endif // wxUSE_CHECKLISTBOX
373
374 // Create the data column
375 gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
376 gtk_cell_renderer_text_new(),
377 "text",
378 WXLISTBOX_DATACOLUMN, NULL);
379
380 // Now create+set the model (GtkListStore) - first argument # of columns
381 #if wxUSE_CHECKLISTBOX
382 if(m_hasCheckBoxes)
383 m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
384 GTK_TYPE_TREE_ENTRY);
385 else
386 #endif
387 m_liststore = gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY);
388
389 gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
390
391 g_object_unref (m_liststore); //free on treeview destruction
392
393 // Disable the pop-up textctrl that enables searching - note that
394 // the docs specify that even if this disabled (which we are doing)
395 // the user can still have it through the start-interactive-search
396 // key binding...either way we want to provide a searchequal callback
397 // NB: If this is enabled a doubleclick event (activate) gets sent
398 // on a successful search
399 gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
400 gtk_tree_view_set_search_equal_func(m_treeview,
401 (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
402 this,
403 NULL);
404
405 gtk_tree_view_set_enable_search(m_treeview, FALSE);
406
407
408 GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
409
410 g_signal_connect_after (selection, "changed",
411 G_CALLBACK (gtk_listitem_changed_callback), this);
412
413 GtkSelectionMode mode;
414 if (style & wxLB_MULTIPLE)
415 {
416 mode = GTK_SELECTION_MULTIPLE;
417 }
418 else if (style & wxLB_EXTENDED)
419 {
420 mode = GTK_SELECTION_EXTENDED;
421 }
422 else
423 {
424 // if style was 0 set single mode
425 m_windowStyle |= wxLB_SINGLE;
426 mode = GTK_SELECTION_SINGLE;
427 }
428
429 gtk_tree_selection_set_mode( selection, mode );
430
431 // Handle sortable stuff
432 if(style & wxLB_SORT)
433 {
434 // Setup sorting in ascending (wx) order
435 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
436 WXLISTBOX_DATACOLUMN,
437 GTK_SORT_ASCENDING);
438
439 // Set the sort callback
440 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
441 WXLISTBOX_DATACOLUMN,
442 (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
443 this, //userdata
444 NULL //"destroy notifier"
445 );
446 }
447
448
449 gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
450
451 gtk_widget_show( GTK_WIDGET(m_treeview) );
452 m_focusWidget = GTK_WIDGET(m_treeview);
453
454 wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
455
456 // generate dclick events
457 g_signal_connect_after(m_treeview, "row-activated",
458 G_CALLBACK(gtk_listbox_row_activated_callback), this);
459
460 // for panel navigation
461 g_signal_connect (m_treeview, "key_press_event",
462 G_CALLBACK (gtk_listbox_key_press_callback),
463 this);
464
465 m_parent->DoAddChild( this );
466
467 PostCreation(size);
468 SetInitialSize(size); // need this too because this is a wxControlWithItems
469
470 return true;
471 }
472
473 wxListBox::~wxListBox()
474 {
475 m_hasVMT = false;
476
477 Clear();
478 }
479
480 // ----------------------------------------------------------------------------
481 // adding items
482 // ----------------------------------------------------------------------------
483
484 void wxListBox::GtkInsertItems(const wxArrayString& items,
485 void** clientData, unsigned int pos)
486 {
487 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
488
489 InvalidateBestSize();
490
491 // Create and set column ids and GValues
492
493 unsigned int nNum = items.GetCount();
494 unsigned int nCurCount = wxListBox::GetCount();
495 wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
496
497 GtkTreeIter* pIter = NULL; // append by default
498 GtkTreeIter iter;
499 if (pos != nCurCount)
500 {
501 gboolean res = gtk_tree_model_iter_nth_child(
502 GTK_TREE_MODEL(m_liststore),
503 &iter, NULL, //NULL = parent = get first
504 (int)pos );
505 if(!res)
506 {
507 wxLogSysError(wxT("internal wxListBox error in insertion"));
508 return;
509 }
510
511 pIter = &iter;
512 }
513
514 for (unsigned int i = 0; i < nNum; ++i)
515 {
516 wxString label = items[i];
517
518 GtkTreeEntry* entry = gtk_tree_entry_new();
519 gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
520 gtk_tree_entry_set_destroy_func(entry,
521 (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
522 this);
523
524 if (clientData)
525 gtk_tree_entry_set_userdata(entry, clientData[i]);
526
527 GtkTreeIter itercur;
528 gtk_list_store_insert_before(m_liststore, &itercur, pIter);
529
530 #if wxUSE_CHECKLISTBOX
531 if (m_hasCheckBoxes)
532 {
533 gtk_list_store_set(m_liststore, &itercur,
534 0, FALSE, //FALSE == not toggled
535 1, entry, -1);
536 }
537 else
538 #endif
539 gtk_list_store_set(m_liststore, &itercur,
540 0, entry, -1);
541
542 g_object_unref (entry); //liststore always refs :)
543 }
544 }
545
546 void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
547 {
548 wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
549
550 GtkInsertItems(items, NULL, pos);
551 }
552
553 int wxListBox::DoAppend( const wxString& item )
554 {
555 // Call DoInsertItems
556 unsigned int nWhere = wxListBox::GetCount();
557 wxArrayString aItems;
558 aItems.Add(item);
559 wxListBox::DoInsertItems(aItems, nWhere);
560 return nWhere;
561 }
562
563 void wxListBox::DoSetItems( const wxArrayString& items,
564 void **clientData)
565 {
566 Clear();
567 GtkInsertItems(items, clientData, 0);
568 }
569
570 // ----------------------------------------------------------------------------
571 // deleting items
572 // ----------------------------------------------------------------------------
573
574 void wxListBox::Clear()
575 {
576 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
577
578 InvalidateBestSize();
579
580 gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
581 }
582
583 void wxListBox::Delete(unsigned int n)
584 {
585 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
586
587 InvalidateBestSize();
588
589 GtkTreeIter iter;
590 gboolean res = gtk_tree_model_iter_nth_child(
591 GTK_TREE_MODEL(m_liststore),
592 &iter, NULL, //NULL = parent = get first
593 n
594 );
595
596 wxCHECK_RET( res, wxT("wrong listbox index") );
597
598 //this returns false if iter is invalid (i.e. deleting item
599 //at end) but since we don't use iter, well... :)
600 gtk_list_store_remove(m_liststore, &iter);
601 }
602
603 // ----------------------------------------------------------------------------
604 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
605 // ----------------------------------------------------------------------------
606
607 struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
608 {
609 GtkTreeIter iter;
610 gboolean res = gtk_tree_model_iter_nth_child(
611 GTK_TREE_MODEL(m_liststore),
612 &iter, NULL, //NULL = parent = get first
613 n );
614
615 if (!res)
616 {
617 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
618 wxT("Passed in value was:[%i] List size:[%u]"),
619 n, wxListBox::GetCount() );
620 return NULL;
621 }
622
623
624 GtkTreeEntry* entry = NULL;
625 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore), &iter,
626 WXLISTBOX_DATACOLUMN, &entry, -1);
627
628 return entry;
629 }
630
631 // ----------------------------------------------------------------------------
632 // client data
633 // ----------------------------------------------------------------------------
634
635 void* wxListBox::DoGetItemClientData(unsigned int n) const
636 {
637 wxCHECK_MSG( IsValid(n), NULL,
638 wxT("Invalid index passed to GetItemClientData") );
639
640 GtkTreeEntry* entry = GtkGetEntry(n);
641 wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
642
643 void* userdata = gtk_tree_entry_get_userdata( entry );
644 g_object_unref (entry);
645 return userdata;
646 }
647
648 wxClientData* wxListBox::DoGetItemClientObject(unsigned int n) const
649 {
650 return (wxClientData*) wxListBox::DoGetItemClientData(n);
651 }
652
653 void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
654 {
655 wxCHECK_RET( IsValid(n),
656 wxT("Invalid index passed to SetItemClientData") );
657
658 GtkTreeEntry* entry = GtkGetEntry(n);
659 wxCHECK_RET(entry, wxT("could not get entry"));
660
661 gtk_tree_entry_set_userdata( entry, clientData );
662 g_object_unref (entry);
663 }
664
665 void wxListBox::DoSetItemClientObject(unsigned int n, wxClientData* clientData)
666 {
667 // wxItemContainer already deletes data for us
668 wxListBox::DoSetItemClientData(n, (void*) clientData);
669 }
670
671 // ----------------------------------------------------------------------------
672 // string list access
673 // ----------------------------------------------------------------------------
674
675 void wxListBox::SetString(unsigned int n, const wxString &string)
676 {
677 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetString") );
678 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
679
680 GtkTreeEntry* entry = GtkGetEntry(n);
681 wxCHECK_RET( entry, wxT("wrong listbox index") );
682
683 wxString label = string;
684
685 // RN: This may look wierd but the problem is that the TreeView
686 // doesn't resort or update when changed above and there is no real
687 // notification function...
688 void* userdata = gtk_tree_entry_get_userdata(entry);
689 gtk_tree_entry_set_userdata(entry, NULL); //don't delete on destroy
690 g_object_unref (entry);
691
692 bool bWasSelected = wxListBox::IsSelected(n);
693 wxListBox::Delete(n);
694
695 wxArrayString aItems;
696 aItems.Add(label);
697 GtkInsertItems(aItems, &userdata, n);
698 if (bWasSelected)
699 wxListBox::GtkSetSelection(n, true, true);
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 GtkTreePath* path =
758 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
759
760 int sel = gtk_tree_path_get_indices(path)[0];
761
762 gtk_tree_path_free(path);
763
764 return sel;
765 }
766
767 int wxListBox::GetSelections( wxArrayInt& aSelections ) const
768 {
769 wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
770
771 aSelections.Empty();
772
773 int i = 0;
774 GtkTreeIter iter;
775 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
776
777 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
778 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
779 do
780 {
781 if (gtk_tree_selection_iter_is_selected(selection, &iter))
782 aSelections.Add(i);
783
784 i++;
785 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
786 }
787
788 return aSelections.GetCount();
789 }
790
791 bool wxListBox::IsSelected( int n ) const
792 {
793 wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
794
795 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
796
797 GtkTreeIter iter;
798 gboolean res = gtk_tree_model_iter_nth_child(
799 GTK_TREE_MODEL(m_liststore),
800 &iter, NULL, //NULL = parent = get first
801 n );
802
803 wxCHECK_MSG( res, false, wxT("Invalid index") );
804
805 return gtk_tree_selection_iter_is_selected(selection, &iter);
806 }
807
808 void wxListBox::DoSetSelection( int n, bool select )
809 {
810 // passing -1 to SetSelection() is documented to deselect all items
811 if ( n == wxNOT_FOUND )
812 {
813 // ... and not generate any events in the process
814 GtkDeselectAll();
815 return;
816 }
817
818 wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
819
820 // don't generate the selection event
821 GtkSetSelection(n, select, true);
822 }
823
824 void wxListBox::GtkDeselectAll()
825 {
826 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
827
828 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
829
830 m_blockEvent = true;
831
832 gtk_tree_selection_unselect_all(selection);
833
834 m_blockEvent = false;
835 }
836
837 void wxListBox::GtkSetSelection(int n, const bool select, const bool blockEvent)
838 {
839 wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
840
841 GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
842
843 GtkTreeIter iter;
844 gboolean res = gtk_tree_model_iter_nth_child(
845 GTK_TREE_MODEL(m_liststore),
846 &iter, NULL, //NULL = parent = get first
847 n
848 );
849 wxCHECK_RET( res, wxT("Invalid index") );
850
851 m_blockEvent = blockEvent;
852
853 if (select)
854 gtk_tree_selection_select_iter(selection, &iter);
855 else
856 gtk_tree_selection_unselect_iter(selection, &iter);
857
858 m_blockEvent = false;
859 }
860
861 void wxListBox::DoSetFirstItem( int n )
862 {
863 wxCHECK_RET( m_treeview, wxT("invalid listbox") );
864 wxCHECK_RET( IsValid(n), wxT("invalid index"));
865
866 //RN: I have no idea why this line is needed...
867 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview))
868 return;
869
870 GtkTreeIter iter;
871 gtk_tree_model_iter_nth_child(
872 GTK_TREE_MODEL(m_liststore),
873 &iter,
874 NULL, //NULL = parent = get first
875 n
876 );
877
878 GtkTreePath* path = gtk_tree_model_get_path(
879 GTK_TREE_MODEL(m_liststore), &iter);
880
881 // Scroll to the desired cell (0.0 == topleft alignment)
882 gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
883 TRUE, 0.0f, 0.0f);
884
885 gtk_tree_path_free(path);
886 }
887
888 // ----------------------------------------------------------------------------
889 // hittest
890 // ----------------------------------------------------------------------------
891
892 int wxListBox::DoListHitTest(const wxPoint& point) const
893 {
894 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
895 // we only want visible items we need to check for it manually here
896 if ( !GetClientRect().Contains(point) )
897 return wxNOT_FOUND;
898
899 // need to translate from master window since it is in client coords
900 gint binx, biny;
901 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
902 &binx, &biny, NULL, NULL, NULL);
903
904 GtkTreePath* path;
905 if ( !gtk_tree_view_get_path_at_pos
906 (
907 m_treeview,
908 point.x - binx,
909 point.y - biny,
910 &path,
911 NULL, // [out] column (always 0 here)
912 NULL, // [out] x-coord relative to the cell (not interested)
913 NULL // [out] y-coord relative to the cell
914 ) )
915 {
916 return wxNOT_FOUND;
917 }
918
919 int index = gtk_tree_path_get_indices(path)[0];
920 gtk_tree_path_free(path);
921
922 return index;
923 }
924
925 // ----------------------------------------------------------------------------
926 // helpers
927 // ----------------------------------------------------------------------------
928
929 #if wxUSE_TOOLTIPS
930 void wxListBox::ApplyToolTip( GtkTooltips *tips, const wxChar *tip )
931 {
932 // RN: Is this needed anymore?
933 gtk_tooltips_set_tip( tips, GTK_WIDGET( m_treeview ), wxGTK_CONV(tip), (gchar*) NULL );
934 }
935 #endif // wxUSE_TOOLTIPS
936
937 GtkWidget *wxListBox::GetConnectWidget()
938 {
939 // the correct widget for listbox events (such as mouse clicks for example)
940 // is m_treeview, not the parent scrolled window
941 return GTK_WIDGET(m_treeview);
942 }
943
944 GdkWindow *wxListBox::GTKGetWindow(wxArrayGdkWindows& WXUNUSED(windows)) const
945 {
946 return gtk_tree_view_get_bin_window(m_treeview);
947 }
948
949 void wxListBox::DoApplyWidgetStyle(GtkRcStyle *style)
950 {
951 if (m_hasBgCol && m_backgroundColour.Ok())
952 {
953 GdkWindow *window = gtk_tree_view_get_bin_window(m_treeview);
954 if (window)
955 {
956 m_backgroundColour.CalcPixel( gdk_drawable_get_colormap( window ) );
957 gdk_window_set_background( window, m_backgroundColour.GetColor() );
958 gdk_window_clear( window );
959 }
960 }
961
962 gtk_widget_modify_style( GTK_WIDGET(m_treeview), style );
963 }
964
965 wxSize wxListBox::DoGetBestSize() const
966 {
967 wxCHECK_MSG(m_treeview, wxDefaultSize, wxT("invalid tree view"));
968
969 // Start with a minimum size that's not too small
970 int cx, cy;
971 GetTextExtent( wxT("X"), &cx, &cy);
972 int lbWidth = 3 * cx;
973 int lbHeight = 10;
974
975 // Get the visible area of the tree view (limit to the 10th item
976 // so that it isn't too big)
977 unsigned int count = GetCount();
978 if (count)
979 {
980 int wLine;
981
982 // Find the widest line
983 for(unsigned int i = 0; i < count; i++) {
984 wxString str(GetString(i));
985 GetTextExtent(str, &wLine, NULL);
986 lbWidth = wxMax(lbWidth, wLine);
987 }
988
989 lbWidth += 3 * cx;
990
991 // And just a bit more for the checkbox if present and then some
992 // (these are rough guesses)
993 #if wxUSE_CHECKLISTBOX
994 if ( m_hasCheckBoxes )
995 {
996 lbWidth += 35;
997 cy = cy > 25 ? cy : 25; // rough height of checkbox
998 }
999 #endif
1000
1001 // don't make the listbox too tall (limit height to around 10 items) but don't
1002 // make it too small neither
1003 lbHeight = (cy+4) * wxMin(wxMax(count, 3), 10);
1004 }
1005
1006 // Add room for the scrollbar
1007 lbWidth += wxSystemSettings::GetMetric(wxSYS_VSCROLL_X);
1008
1009 wxSize best(lbWidth, lbHeight);
1010 CacheBestSize(best);
1011 return best;
1012 }
1013
1014 // static
1015 wxVisualAttributes
1016 wxListBox::GetClassDefaultAttributes(wxWindowVariant WXUNUSED(variant))
1017 {
1018 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new, true);
1019 }
1020
1021 #endif // wxUSE_LISTBOX