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