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