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