1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/listbox.cpp
4 // Author: Robert Roebling
5 // Modified By: Ryan Norton (GtkTreeView implementation)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
23 #include "wx/settings.h"
24 #include "wx/checklst.h"
25 #include "wx/arrstr.h"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/treeentry_gtk.h"
32 #include "wx/tooltip.h"
36 #include <gdk/gdkkeysyms.h>
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
43 extern bool g_blockEventsOnScroll
;
47 //-----------------------------------------------------------------------------
48 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
49 //-----------------------------------------------------------------------------
51 #if wxUSE_CHECKLISTBOX
52 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
55 #endif // wxUSE_CHECKLISTBOX
57 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
65 gtk_listbox_row_activated_callback(GtkTreeView
* WXUNUSED(treeview
),
67 GtkTreeViewColumn
* WXUNUSED(col
),
70 if (g_blockEventsOnDrag
) return;
71 if (g_blockEventsOnScroll
) return;
73 // This is triggered by either a double-click or a space press
75 int sel
= gtk_tree_path_get_indices(path
)[0];
77 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
78 event
.SetEventObject( listbox
);
80 if (listbox
->IsSelected(sel
))
82 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(sel
);
87 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
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
) );
94 g_object_unref (entry
);
98 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
107 listbox
->HandleWindowEvent( event
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
115 static void SendEvent( wxCommandEvent
&event
, wxListBox
*listbox
, int item
)
117 event
.SetInt( item
);
118 event
.SetString( listbox
->GetString( item
) );
119 if ( listbox
->HasClientObjectData() )
120 event
.SetClientObject( listbox
->GetClientObject(item
) );
121 else if ( listbox
->HasClientUntypedData() )
122 event
.SetClientData( listbox
->GetClientData(item
) );
123 listbox
->HandleWindowEvent( event
);
128 gtk_listitem_changed_callback(GtkTreeSelection
* WXUNUSED(selection
),
131 if (g_blockEventsOnDrag
) return;
133 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
134 event
.SetEventObject( listbox
);
136 if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
))
138 wxArrayInt selections
;
139 listbox
->GetSelections( selections
);
141 if ((selections
.GetCount() == 0) && (listbox
->m_oldSelection
.GetCount() == 0))
143 // nothing changed, just leave
147 if (selections
.GetCount() == listbox
->m_oldSelection
.GetCount())
149 bool changed
= false;
151 for (idx
= 0; idx
< selections
.GetCount(); idx
++)
153 if (selections
[idx
] != listbox
->m_oldSelection
[idx
])
160 // nothing changed, just leave
165 if (selections
.GetCount() == 0)
167 // indicate that this is a deselection
168 event
.SetExtraLong( 0 );
169 int item
= listbox
->m_oldSelection
[0];
170 listbox
->m_oldSelection
= selections
;
171 SendEvent( event
, listbox
, item
);
176 // Now test if any new item is selected
177 bool any_new_selected
= false;
179 for (idx
= 0; idx
< selections
.GetCount(); idx
++)
181 item
= selections
[idx
];
182 if (listbox
->m_oldSelection
.Index(item
) == wxNOT_FOUND
)
184 any_new_selected
= true;
189 if (any_new_selected
)
191 // indicate that this is a selection
192 event
.SetExtraLong( 1 );
193 listbox
->m_oldSelection
= selections
;
194 SendEvent( event
, listbox
, item
);
198 // Now test if any new item is deselected
199 bool any_new_deselected
= false;
200 for (idx
= 0; idx
< listbox
->m_oldSelection
.GetCount(); idx
++)
202 item
= listbox
->m_oldSelection
[idx
];
203 if (selections
.Index(item
) == wxNOT_FOUND
)
205 any_new_deselected
= true;
210 if (any_new_deselected
)
212 // indicate that this is a selection
213 event
.SetExtraLong( 0 );
214 listbox
->m_oldSelection
= selections
;
215 SendEvent( event
, listbox
, item
);
219 wxLogError( wxT("Wrong wxListBox selection") );
223 int index
= listbox
->GetSelection();
224 if (index
== wxNOT_FOUND
)
226 // indicate that this is a deselection
227 event
.SetExtraLong( 0 );
230 listbox
->HandleWindowEvent( event
);
236 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
238 // indicate that this is a selection
239 event
.SetExtraLong( 1 );
241 event
.SetInt( index
);
242 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
244 if ( listbox
->HasClientObjectData() )
245 event
.SetClientObject(
246 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
248 else if ( listbox
->HasClientUntypedData() )
249 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
251 listbox
->HandleWindowEvent( event
);
253 g_object_unref (entry
);
259 //-----------------------------------------------------------------------------
261 //-----------------------------------------------------------------------------
265 gtk_listbox_key_press_callback( GtkWidget
*WXUNUSED(widget
),
266 GdkEventKey
*gdk_event
,
269 if ((gdk_event
->keyval
== GDK_Return
) ||
270 (gdk_event
->keyval
== GDK_ISO_Enter
) ||
271 (gdk_event
->keyval
== GDK_KP_Enter
))
273 int index
= listbox
->GetSelection();
274 if (index
!= wxNOT_FOUND
)
277 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
278 event
.SetEventObject( listbox
);
280 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
282 // indicate that this is a selection
283 event
.SetExtraLong( 1 );
285 event
.SetInt( index
);
286 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
288 if ( listbox
->HasClientObjectData() )
289 event
.SetClientObject(
290 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
292 else if ( listbox
->HasClientUntypedData() )
293 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
295 /* bool ret = */ listbox
->HandleWindowEvent( event
);
297 g_object_unref (entry
);
299 // wxMac and wxMSW always invoke default action
302 // DClick not handled -> invoke default action
303 wxWindow
*tlw
= wxGetTopLevelParent( listbox
);
306 GtkWindow
*gtk_window
= GTK_WINDOW( tlw
->GetHandle() );
308 gtk_window_activate_default( gtk_window
);
312 // Always intercept, otherwise we'd get another dclick
313 // event from row_activated
322 //-----------------------------------------------------------------------------
323 // GtkTreeEntry destruction (to destroy client data)
324 //-----------------------------------------------------------------------------
327 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
330 if (listbox
->HasClientObjectData())
332 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
334 delete (wxClientData
*)userdata
;
339 //-----------------------------------------------------------------------------
340 // Sorting callback (standard CmpNoCase return value)
341 //-----------------------------------------------------------------------------
344 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
350 GtkTreeEntry
* entry2
;
352 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
354 WXLISTBOX_DATACOLUMN_ARG(listbox
),
356 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
358 WXLISTBOX_DATACOLUMN_ARG(listbox
),
360 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
361 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
363 //We compare collate keys here instead of calling g_utf8_collate
364 //as it is rather slow (and even the docs reccommend this)
365 int ret
= strcmp(gtk_tree_entry_get_collate_key(entry
),
366 gtk_tree_entry_get_collate_key(entry2
));
368 g_object_unref (entry
);
369 g_object_unref (entry2
);
375 //-----------------------------------------------------------------------------
376 // Searching callback (TRUE == not equal, FALSE == equal)
377 //-----------------------------------------------------------------------------
380 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
381 gint
WXUNUSED(column
),
388 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
390 WXLISTBOX_DATACOLUMN_ARG(listbox
),
392 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
393 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
395 int ret
= strcmp(keycollatekey
,
396 gtk_tree_entry_get_collate_key(entry
));
398 g_object_unref (entry
);
404 //-----------------------------------------------------------------------------
406 //-----------------------------------------------------------------------------
408 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
410 // ----------------------------------------------------------------------------
412 // ----------------------------------------------------------------------------
414 void wxListBox::Init()
416 m_treeview
= (GtkTreeView
*) NULL
;
417 #if wxUSE_CHECKLISTBOX
418 m_hasCheckBoxes
= false;
419 #endif // wxUSE_CHECKLISTBOX
422 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
423 const wxPoint
&pos
, const wxSize
&size
,
424 const wxArrayString
& choices
,
425 long style
, const wxValidator
& validator
,
426 const wxString
&name
)
428 wxCArrayString
chs(choices
);
430 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
431 style
, validator
, name
);
434 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
435 const wxPoint
&pos
, const wxSize
&size
,
436 int n
, const wxString choices
[],
437 long style
, const wxValidator
& validator
,
438 const wxString
&name
)
440 if (!PreCreation( parent
, pos
, size
) ||
441 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
443 wxFAIL_MSG( wxT("wxListBox creation failed") );
447 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
448 if (style
& wxLB_ALWAYS_SB
)
450 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
451 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
455 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
456 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
460 GtkScrolledWindowSetBorder(m_widget
, style
);
462 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
464 //wxListBox doesn't have a header :)
465 //NB: If enabled SetFirstItem doesn't work correctly
466 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
468 #if wxUSE_CHECKLISTBOX
470 ((wxCheckListBox
*)this)->DoCreateCheckList();
471 #endif // wxUSE_CHECKLISTBOX
473 // Create the data column
474 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
475 gtk_cell_renderer_text_new(),
477 WXLISTBOX_DATACOLUMN
, NULL
);
479 // Now create+set the model (GtkListStore) - first argument # of columns
480 #if wxUSE_CHECKLISTBOX
482 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
483 GTK_TYPE_TREE_ENTRY
);
486 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
488 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
490 g_object_unref (m_liststore
); //free on treeview destruction
492 // Disable the pop-up textctrl that enables searching - note that
493 // the docs specify that even if this disabled (which we are doing)
494 // the user can still have it through the start-interactive-search
495 // key binding...either way we want to provide a searchequal callback
496 // NB: If this is enabled a doubleclick event (activate) gets sent
497 // on a successful search
498 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
499 gtk_tree_view_set_search_equal_func(m_treeview
,
500 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
504 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
506 GtkSelectionMode mode
;
507 if (style
& wxLB_MULTIPLE
)
509 mode
= GTK_SELECTION_MULTIPLE
;
511 else if (style
& wxLB_EXTENDED
)
513 mode
= GTK_SELECTION_EXTENDED
;
517 // if style was 0 set single mode
518 m_windowStyle
|= wxLB_SINGLE
;
519 mode
= GTK_SELECTION_SINGLE
;
522 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
523 gtk_tree_selection_set_mode( selection
, mode
);
525 // Handle sortable stuff
526 if(HasFlag(wxLB_SORT
))
528 // Setup sorting in ascending (wx) order
529 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
530 WXLISTBOX_DATACOLUMN
,
533 // Set the sort callback
534 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
535 WXLISTBOX_DATACOLUMN
,
536 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
538 NULL
//"destroy notifier"
543 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
545 gtk_widget_show( GTK_WIDGET(m_treeview
) );
546 m_focusWidget
= GTK_WIDGET(m_treeview
);
548 Append(n
, choices
); // insert initial items
550 // generate dclick events
551 g_signal_connect_after(m_treeview
, "row-activated",
552 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
554 // for intercepting dclick generation by <ENTER>
555 g_signal_connect (m_treeview
, "key_press_event",
556 G_CALLBACK (gtk_listbox_key_press_callback
),
558 m_parent
->DoAddChild( this );
561 SetInitialSize(size
); // need this too because this is a wxControlWithItems
563 g_signal_connect_after (selection
, "changed",
564 G_CALLBACK (gtk_listitem_changed_callback
), this);
569 wxListBox::~wxListBox()
576 void wxListBox::GtkDisableEvents()
578 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
580 g_signal_handlers_block_by_func(selection
,
581 (gpointer
) gtk_listitem_changed_callback
, this);
584 void wxListBox::GtkEnableEvents()
586 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
588 g_signal_handlers_unblock_by_func(selection
,
589 (gpointer
) gtk_listitem_changed_callback
, this);
591 GtkUpdateOldSelection();
594 // ----------------------------------------------------------------------------
596 // ----------------------------------------------------------------------------
598 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
601 wxClientDataType type
)
603 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
605 InvalidateBestSize();
607 GtkTreeIter
* pIter
= NULL
; // append by default
609 if ( pos
!= GetCount() )
611 wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
612 wxT("internal wxListBox error in insertion") );
617 const unsigned int numItems
= items
.GetCount();
618 for ( unsigned int i
= 0; i
< numItems
; ++i
)
620 GtkTreeEntry
* entry
= gtk_tree_entry_new();
621 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
622 gtk_tree_entry_set_destroy_func(entry
,
623 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
627 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
629 GtkSetItem(itercur
, entry
);
631 g_object_unref (entry
);
634 AssignNewItemClientData(GtkGetIndexFor(itercur
), clientData
, i
, type
);
637 return pos
+ numItems
- 1;
640 // ----------------------------------------------------------------------------
642 // ----------------------------------------------------------------------------
644 void wxListBox::DoClear()
646 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
648 InvalidateBestSize();
650 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
653 void wxListBox::DoDeleteOneItem(unsigned int n
)
655 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
657 InvalidateBestSize();
660 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
662 // this returns false if iter is invalid (e.g. deleting item at end) but
663 // since we don't use iter, we ignore the return value
664 gtk_list_store_remove(m_liststore
, &iter
);
667 // ----------------------------------------------------------------------------
668 // helper functions for working with iterators
669 // ----------------------------------------------------------------------------
671 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
673 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
676 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
683 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
686 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
688 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
690 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
692 int idx
= pIntPath
[0];
694 gtk_tree_path_free( path
);
699 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
700 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
703 if ( !GtkGetIteratorFor(n
, &iter
) )
707 GtkTreeEntry
* entry
= NULL
;
708 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
709 WXLISTBOX_DATACOLUMN
, &entry
, -1);
714 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
716 #if wxUSE_CHECKLISTBOX
717 if ( m_hasCheckBoxes
)
719 gtk_list_store_set(m_liststore
, &iter
,
720 0, FALSE
, // FALSE == not toggled
725 #endif // wxUSE_CHECKLISTBOX
727 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
731 // ----------------------------------------------------------------------------
733 // ----------------------------------------------------------------------------
735 void* wxListBox::DoGetItemClientData(unsigned int n
) const
737 wxCHECK_MSG( IsValid(n
), NULL
,
738 wxT("Invalid index passed to GetItemClientData") );
740 GtkTreeEntry
* entry
= GtkGetEntry(n
);
741 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
743 void* userdata
= gtk_tree_entry_get_userdata( entry
);
744 g_object_unref (entry
);
748 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
750 wxCHECK_RET( IsValid(n
),
751 wxT("Invalid index passed to SetItemClientData") );
753 GtkTreeEntry
* entry
= GtkGetEntry(n
);
754 wxCHECK_RET(entry
, wxT("could not get entry"));
756 gtk_tree_entry_set_userdata( entry
, clientData
);
757 g_object_unref (entry
);
760 // ----------------------------------------------------------------------------
761 // string list access
762 // ----------------------------------------------------------------------------
764 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
766 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
767 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
769 GtkTreeEntry
* entry
= GtkGetEntry(n
);
770 wxCHECK_RET( entry
, wxT("wrong listbox index") );
772 // update the item itself
773 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
775 // and update the model which will refresh the tree too
777 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
779 // FIXME: this resets the checked status of a wxCheckListBox item
781 GtkSetItem(iter
, entry
);
784 wxString
wxListBox::GetString(unsigned int n
) const
786 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
788 GtkTreeEntry
* entry
= GtkGetEntry(n
);
789 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
791 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
793 g_object_unref (entry
);
797 unsigned int wxListBox::GetCount() const
799 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
801 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
804 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
806 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
808 //Sort of hackish - maybe there is a faster way
809 unsigned int nCount
= wxListBox::GetCount();
811 for(unsigned int i
= 0; i
< nCount
; ++i
)
813 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
818 // it's not an error if the string is not found -> no wxCHECK
822 // ----------------------------------------------------------------------------
824 // ----------------------------------------------------------------------------
826 int wxListBox::GetSelection() const
828 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
829 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
830 wxT("must be single selection listbox"));
833 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
835 // only works on single-sel
836 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
839 return GtkGetIndexFor(iter
);
842 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
844 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
850 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
852 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
853 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
856 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
860 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
863 return aSelections
.GetCount();
866 bool wxListBox::IsSelected( int n
) const
868 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
870 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
873 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
875 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
878 void wxListBox::DoSetSelection( int n
, bool select
)
880 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
884 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
886 // passing -1 to SetSelection() is documented to deselect all items
887 if ( n
== wxNOT_FOUND
)
889 gtk_tree_selection_unselect_all(selection
);
894 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
898 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
901 gtk_tree_selection_select_iter(selection
, &iter
);
903 gtk_tree_selection_unselect_iter(selection
, &iter
);
905 GtkTreePath
* path
= gtk_tree_model_get_path(
906 GTK_TREE_MODEL(m_liststore
), &iter
);
908 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
910 gtk_tree_path_free(path
);
915 void wxListBox::GtkUpdateOldSelection()
917 if (HasFlag(wxLB_MULTIPLE
) || HasFlag(wxLB_EXTENDED
))
918 GetSelections( m_oldSelection
);
921 void wxListBox::DoScrollToCell(int n
, float alignY
, float alignX
)
923 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
924 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
926 //RN: I have no idea why this line is needed...
927 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
931 if ( !GtkGetIteratorFor(n
, &iter
) )
934 GtkTreePath
* path
= gtk_tree_model_get_path(
935 GTK_TREE_MODEL(m_liststore
), &iter
);
937 // Scroll to the desired cell (0.0 == topleft alignment)
938 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
939 TRUE
, alignY
, alignX
);
941 gtk_tree_path_free(path
);
944 void wxListBox::DoSetFirstItem(int n
)
946 DoScrollToCell(n
, 0, 0);
949 void wxListBox::EnsureVisible(int n
)
951 DoScrollToCell(n
, 0.5, 0);
954 // ----------------------------------------------------------------------------
956 // ----------------------------------------------------------------------------
958 int wxListBox::DoListHitTest(const wxPoint
& point
) const
960 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
961 // we only want visible items we need to check for it manually here
962 if ( !GetClientRect().Contains(point
) )
965 // need to translate from master window since it is in client coords
967 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
968 &binx
, &biny
, NULL
, NULL
, NULL
);
971 if ( !gtk_tree_view_get_path_at_pos
977 NULL
, // [out] column (always 0 here)
978 NULL
, // [out] x-coord relative to the cell (not interested)
979 NULL
// [out] y-coord relative to the cell
985 int index
= gtk_tree_path_get_indices(path
)[0];
986 gtk_tree_path_free(path
);
991 // ----------------------------------------------------------------------------
993 // ----------------------------------------------------------------------------
996 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
998 // RN: Is this needed anymore?
999 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, (gchar
*) NULL
);
1001 #endif // wxUSE_TOOLTIPS
1003 GtkWidget
*wxListBox::GetConnectWidget()
1005 // the correct widget for listbox events (such as mouse clicks for example)
1006 // is m_treeview, not the parent scrolled window
1007 return GTK_WIDGET(m_treeview
);
1010 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1012 return gtk_tree_view_get_bin_window(m_treeview
);
1015 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1017 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1019 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1022 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
1023 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1024 gdk_window_clear( window
);
1028 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
1031 wxSize
wxListBox::DoGetBestSize() const
1033 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1035 // Start with a minimum size that's not too small
1037 GetTextExtent( wxT("X"), &cx
, &cy
);
1041 // Find the widest string.
1042 const unsigned int count
= GetCount();
1046 for ( unsigned int i
= 0; i
< count
; i
++ )
1048 GetTextExtent(GetString(i
), &wLine
, NULL
);
1049 if ( wLine
> lbWidth
)
1056 // And just a bit more for the checkbox if present and then some
1057 // (these are rough guesses)
1058 #if wxUSE_CHECKLISTBOX
1059 if ( m_hasCheckBoxes
)
1062 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1066 // Add room for the scrollbar
1067 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1069 // Don't make the listbox too tall but don't make it too small neither
1070 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1072 wxSize
best(lbWidth
, lbHeight
);
1073 CacheBestSize(best
);
1079 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1081 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1084 #endif // wxUSE_LISTBOX