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