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