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