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 //-----------------------------------------------------------------------------
117 gtk_listitem_changed_callback(GtkTreeSelection
* WXUNUSED(selection
),
120 if (g_blockEventsOnDrag
) return;
122 listbox
->GTKOnSelectionChanged();
127 //-----------------------------------------------------------------------------
129 //-----------------------------------------------------------------------------
133 gtk_listbox_key_press_callback( GtkWidget
*WXUNUSED(widget
),
134 GdkEventKey
*gdk_event
,
137 if ((gdk_event
->keyval
== GDK_Return
) ||
138 (gdk_event
->keyval
== GDK_ISO_Enter
) ||
139 (gdk_event
->keyval
== GDK_KP_Enter
))
142 if (!listbox
->HasMultipleSelection())
143 index
= listbox
->GetSelection();
147 if (listbox
->GetSelections( sels
) < 1)
152 if (index
!= wxNOT_FOUND
)
154 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
155 event
.SetEventObject( listbox
);
157 GtkTreeEntry
* entry
= listbox
->GTKGetEntry( index
);
159 // indicate that this is a selection
160 event
.SetExtraLong( 1 );
162 event
.SetInt( index
);
163 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
165 if ( listbox
->HasClientObjectData() )
166 event
.SetClientObject(
167 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
169 else if ( listbox
->HasClientUntypedData() )
170 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
172 /* bool ret = */ listbox
->HandleWindowEvent( event
);
174 g_object_unref (entry
);
176 // wxMac and wxMSW always invoke default action
179 // DClick not handled -> invoke default action
180 wxWindow
*tlw
= wxGetTopLevelParent( listbox
);
183 GtkWindow
*gtk_window
= GTK_WINDOW( tlw
->GetHandle() );
185 gtk_window_activate_default( gtk_window
);
189 // Always intercept, otherwise we'd get another dclick
190 // event from row_activated
199 //-----------------------------------------------------------------------------
200 // GtkTreeEntry destruction (to destroy client data)
201 //-----------------------------------------------------------------------------
204 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
207 if (listbox
->HasClientObjectData())
209 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
211 delete (wxClientData
*)userdata
;
216 //-----------------------------------------------------------------------------
217 // Sorting callback (standard CmpNoCase return value)
218 //-----------------------------------------------------------------------------
221 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
227 GtkTreeEntry
* entry2
;
229 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
231 WXLISTBOX_DATACOLUMN_ARG(listbox
),
233 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
235 WXLISTBOX_DATACOLUMN_ARG(listbox
),
237 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
238 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
240 //We compare collate keys here instead of calling g_utf8_collate
241 //as it is rather slow (and even the docs reccommend this)
242 int ret
= strcmp(gtk_tree_entry_get_collate_key(entry
),
243 gtk_tree_entry_get_collate_key(entry2
)) >= 0;
245 g_object_unref (entry
);
246 g_object_unref (entry2
);
252 //-----------------------------------------------------------------------------
253 // Searching callback (TRUE == not equal, FALSE == equal)
254 //-----------------------------------------------------------------------------
257 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
258 gint
WXUNUSED(column
),
265 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
267 WXLISTBOX_DATACOLUMN_ARG(listbox
),
269 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
270 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
272 int ret
= strcmp(keycollatekey
,
273 gtk_tree_entry_get_collate_key(entry
));
275 g_object_unref (entry
);
281 //-----------------------------------------------------------------------------
283 //-----------------------------------------------------------------------------
285 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
287 // ----------------------------------------------------------------------------
289 // ----------------------------------------------------------------------------
291 void wxListBox::Init()
294 #if wxUSE_CHECKLISTBOX
295 m_hasCheckBoxes
= false;
296 #endif // wxUSE_CHECKLISTBOX
299 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
300 const wxPoint
&pos
, const wxSize
&size
,
301 const wxArrayString
& choices
,
302 long style
, const wxValidator
& validator
,
303 const wxString
&name
)
305 wxCArrayString
chs(choices
);
307 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
308 style
, validator
, name
);
311 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
312 const wxPoint
&pos
, const wxSize
&size
,
313 int n
, const wxString choices
[],
314 long style
, const wxValidator
& validator
,
315 const wxString
&name
)
317 if (!PreCreation( parent
, pos
, size
) ||
318 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
320 wxFAIL_MSG( wxT("wxListBox creation failed") );
324 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
325 g_object_ref(m_widget
);
326 if (style
& wxLB_ALWAYS_SB
)
328 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
329 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
333 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
334 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
338 GTKScrolledWindowSetBorder(m_widget
, style
);
340 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
342 //wxListBox doesn't have a header :)
343 //NB: If enabled SetFirstItem doesn't work correctly
344 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
346 #if wxUSE_CHECKLISTBOX
348 ((wxCheckListBox
*)this)->DoCreateCheckList();
349 #endif // wxUSE_CHECKLISTBOX
351 // Create the data column
352 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
353 gtk_cell_renderer_text_new(),
355 WXLISTBOX_DATACOLUMN
, NULL
);
357 // Now create+set the model (GtkListStore) - first argument # of columns
358 #if wxUSE_CHECKLISTBOX
360 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
361 GTK_TYPE_TREE_ENTRY
);
364 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
366 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
368 g_object_unref (m_liststore
); //free on treeview destruction
370 // Disable the pop-up textctrl that enables searching - note that
371 // the docs specify that even if this disabled (which we are doing)
372 // the user can still have it through the start-interactive-search
373 // key binding...either way we want to provide a searchequal callback
374 // NB: If this is enabled a doubleclick event (activate) gets sent
375 // on a successful search
376 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
377 gtk_tree_view_set_search_equal_func(m_treeview
,
378 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
382 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
384 GtkSelectionMode mode
;
385 // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
386 if ( style
& (wxLB_MULTIPLE
| wxLB_EXTENDED
) )
388 mode
= GTK_SELECTION_MULTIPLE
;
390 else // no multi-selection flags specified
392 m_windowStyle
|= wxLB_SINGLE
;
394 // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because
395 // the latter allows to not select any items at all while a single
396 // selection listbox is supposed to always have a selection (at least
397 // once the user selected something, it might not have any initially).
398 mode
= GTK_SELECTION_BROWSE
;
401 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
402 gtk_tree_selection_set_mode( selection
, mode
);
404 // Handle sortable stuff
405 if(HasFlag(wxLB_SORT
))
407 // Setup sorting in ascending (wx) order
408 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
409 WXLISTBOX_DATACOLUMN
,
412 // Set the sort callback
413 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
414 WXLISTBOX_DATACOLUMN
,
415 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
417 NULL
//"destroy notifier"
422 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
424 gtk_widget_show( GTK_WIDGET(m_treeview
) );
425 m_focusWidget
= GTK_WIDGET(m_treeview
);
427 Append(n
, choices
); // insert initial items
429 // generate dclick events
430 g_signal_connect_after(m_treeview
, "row-activated",
431 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
433 // for intercepting dclick generation by <ENTER>
434 g_signal_connect (m_treeview
, "key_press_event",
435 G_CALLBACK (gtk_listbox_key_press_callback
),
437 m_parent
->DoAddChild( this );
440 SetInitialSize(size
); // need this too because this is a wxControlWithItems
442 g_signal_connect_after (selection
, "changed",
443 G_CALLBACK (gtk_listitem_changed_callback
), this);
448 wxListBox::~wxListBox()
455 void wxListBox::GTKDisableEvents()
457 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
459 g_signal_handlers_block_by_func(selection
,
460 (gpointer
) gtk_listitem_changed_callback
, this);
463 void wxListBox::GTKEnableEvents()
465 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
467 g_signal_handlers_unblock_by_func(selection
,
468 (gpointer
) gtk_listitem_changed_callback
, this);
470 UpdateOldSelections();
474 void wxListBox::Update()
479 gdk_window_process_updates(GTK_WIDGET(m_treeview
)->window
, TRUE
);
482 // ----------------------------------------------------------------------------
484 // ----------------------------------------------------------------------------
486 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
489 wxClientDataType type
)
491 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
493 InvalidateBestSize();
495 GtkTreeIter
* pIter
= NULL
; // append by default
497 if ( pos
!= GetCount() )
499 wxCHECK_MSG( GTKGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
500 wxT("internal wxListBox error in insertion") );
505 const unsigned int numItems
= items
.GetCount();
506 for ( unsigned int i
= 0; i
< numItems
; ++i
)
508 GtkTreeEntry
* entry
= gtk_tree_entry_new();
509 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
510 gtk_tree_entry_set_destroy_func(entry
,
511 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
515 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
517 GTKSetItem(itercur
, entry
);
519 g_object_unref (entry
);
522 AssignNewItemClientData(GTKGetIndexFor(itercur
), clientData
, i
, type
);
525 UpdateOldSelections();
527 return pos
+ numItems
- 1;
530 // ----------------------------------------------------------------------------
532 // ----------------------------------------------------------------------------
534 void wxListBox::DoClear()
536 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
538 GTKDisableEvents(); // just in case
540 InvalidateBestSize();
542 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
547 void wxListBox::DoDeleteOneItem(unsigned int n
)
549 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
551 InvalidateBestSize();
553 GTKDisableEvents(); // just in case
556 wxCHECK_RET( GTKGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
558 // this returns false if iter is invalid (e.g. deleting item at end) but
559 // since we don't use iter, we ignore the return value
560 gtk_list_store_remove(m_liststore
, &iter
);
565 // ----------------------------------------------------------------------------
566 // helper functions for working with iterators
567 // ----------------------------------------------------------------------------
569 bool wxListBox::GTKGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
571 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
574 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
581 int wxListBox::GTKGetIndexFor(GtkTreeIter
& iter
) const
584 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
586 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
588 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, wxT("failed to get iterator path") );
590 int idx
= pIntPath
[0];
592 gtk_tree_path_free( path
);
597 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
598 GtkTreeEntry
*wxListBox::GTKGetEntry(unsigned n
) const
601 if ( !GTKGetIteratorFor(n
, &iter
) )
605 GtkTreeEntry
* entry
= NULL
;
606 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
607 WXLISTBOX_DATACOLUMN
, &entry
, -1);
612 void wxListBox::GTKSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
614 #if wxUSE_CHECKLISTBOX
615 if ( m_hasCheckBoxes
)
617 gtk_list_store_set(m_liststore
, &iter
,
618 0, FALSE
, // FALSE == not toggled
623 #endif // wxUSE_CHECKLISTBOX
625 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
629 // ----------------------------------------------------------------------------
631 // ----------------------------------------------------------------------------
633 void* wxListBox::DoGetItemClientData(unsigned int n
) const
635 wxCHECK_MSG( IsValid(n
), NULL
,
636 wxT("Invalid index passed to GetItemClientData") );
638 GtkTreeEntry
* entry
= GTKGetEntry(n
);
639 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
641 void* userdata
= gtk_tree_entry_get_userdata( entry
);
642 g_object_unref (entry
);
646 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
648 wxCHECK_RET( IsValid(n
),
649 wxT("Invalid index passed to SetItemClientData") );
651 GtkTreeEntry
* entry
= GTKGetEntry(n
);
652 wxCHECK_RET(entry
, wxT("could not get entry"));
654 gtk_tree_entry_set_userdata( entry
, clientData
);
655 g_object_unref (entry
);
658 // ----------------------------------------------------------------------------
659 // string list access
660 // ----------------------------------------------------------------------------
662 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
664 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
665 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
667 GtkTreeEntry
* entry
= GTKGetEntry(n
);
668 wxCHECK_RET( entry
, wxT("wrong listbox index") );
670 // update the item itself
671 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
673 // and update the model which will refresh the tree too
675 wxCHECK_RET( GTKGetIteratorFor(n
, &iter
), wxT("failed to get iterator") );
677 // FIXME: this resets the checked status of a wxCheckListBox item
679 GTKSetItem(iter
, entry
);
682 wxString
wxListBox::GetString(unsigned int n
) const
684 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
686 GtkTreeEntry
* entry
= GTKGetEntry(n
);
687 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
689 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
691 g_object_unref (entry
);
695 unsigned int wxListBox::GetCount() const
697 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
699 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
702 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
704 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
706 //Sort of hackish - maybe there is a faster way
707 unsigned int nCount
= wxListBox::GetCount();
709 for(unsigned int i
= 0; i
< nCount
; ++i
)
711 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
716 // it's not an error if the string is not found -> no wxCHECK
720 // ----------------------------------------------------------------------------
722 // ----------------------------------------------------------------------------
724 void wxListBox::GTKOnSelectionChanged()
726 if ( HasFlag(wxLB_MULTIPLE
| wxLB_EXTENDED
) )
730 else // single selection
732 const int index
= GetSelection();
733 if ( !DoChangeSingleSelection(index
) )
736 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, GetId() );
737 event
.SetEventObject( this );
739 if (index
== wxNOT_FOUND
)
741 // indicate that this is a deselection
742 event
.SetExtraLong( 0 );
745 HandleWindowEvent( event
);
751 GtkTreeEntry
* entry
= GTKGetEntry( index
);
753 // indicate that this is a selection
754 event
.SetExtraLong( 1 );
756 event
.SetInt( index
);
757 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
759 if ( HasClientObjectData() )
760 event
.SetClientObject(
761 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
763 else if ( HasClientUntypedData() )
764 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
766 HandleWindowEvent( event
);
768 g_object_unref (entry
);
773 int wxListBox::GetSelection() const
775 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
776 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
777 wxT("must be single selection listbox"));
780 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
782 // only works on single-sel
783 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
786 return GTKGetIndexFor(iter
);
789 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
791 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
797 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
799 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
800 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
803 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
807 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
810 return aSelections
.GetCount();
813 bool wxListBox::IsSelected( int n
) const
815 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
817 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
820 wxCHECK_MSG( GTKGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
822 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
825 void wxListBox::DoSetSelection( int n
, bool select
)
827 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
831 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
833 // passing -1 to SetSelection() is documented to deselect all items
834 if ( n
== wxNOT_FOUND
)
836 gtk_tree_selection_unselect_all(selection
);
841 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
845 wxCHECK_RET( GTKGetIteratorFor(n
, &iter
), wxT("Invalid index") );
848 gtk_tree_selection_select_iter(selection
, &iter
);
850 gtk_tree_selection_unselect_iter(selection
, &iter
);
852 GtkTreePath
* path
= gtk_tree_model_get_path(
853 GTK_TREE_MODEL(m_liststore
), &iter
);
855 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
857 gtk_tree_path_free(path
);
862 void wxListBox::DoScrollToCell(int n
, float alignY
, float alignX
)
864 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
865 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
867 //RN: I have no idea why this line is needed...
868 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
872 if ( !GTKGetIteratorFor(n
, &iter
) )
875 GtkTreePath
* path
= gtk_tree_model_get_path(
876 GTK_TREE_MODEL(m_liststore
), &iter
);
878 // Scroll to the desired cell (0.0 == topleft alignment)
879 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
880 TRUE
, alignY
, alignX
);
882 gtk_tree_path_free(path
);
885 void wxListBox::DoSetFirstItem(int n
)
887 DoScrollToCell(n
, 0, 0);
890 void wxListBox::EnsureVisible(int n
)
892 DoScrollToCell(n
, 0.5, 0);
895 // ----------------------------------------------------------------------------
897 // ----------------------------------------------------------------------------
899 int wxListBox::DoListHitTest(const wxPoint
& point
) const
901 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
902 // we only want visible items we need to check for it manually here
903 if ( !GetClientRect().Contains(point
) )
906 // need to translate from master window since it is in client coords
908 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
909 &binx
, &biny
, NULL
, NULL
, NULL
);
912 if ( !gtk_tree_view_get_path_at_pos
918 NULL
, // [out] column (always 0 here)
919 NULL
, // [out] x-coord relative to the cell (not interested)
920 NULL
// [out] y-coord relative to the cell
926 int index
= gtk_tree_path_get_indices(path
)[0];
927 gtk_tree_path_free(path
);
932 // ----------------------------------------------------------------------------
934 // ----------------------------------------------------------------------------
937 void wxListBox::GTKApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
939 // RN: Is this needed anymore?
940 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, NULL
);
942 #endif // wxUSE_TOOLTIPS
944 GtkWidget
*wxListBox::GetConnectWidget()
946 // the correct widget for listbox events (such as mouse clicks for example)
947 // is m_treeview, not the parent scrolled window
948 return GTK_WIDGET(m_treeview
);
951 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
953 return gtk_tree_view_get_bin_window(m_treeview
);
956 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
958 if (m_hasBgCol
&& m_backgroundColour
.Ok())
960 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
963 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
964 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
965 gdk_window_clear( window
);
969 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
972 wxSize
wxListBox::DoGetBestSize() const
974 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
976 // Start with a minimum size that's not too small
978 GetTextExtent( wxT("X"), &cx
, &cy
);
982 // Find the widest string.
983 const unsigned int count
= GetCount();
987 for ( unsigned int i
= 0; i
< count
; i
++ )
989 GetTextExtent(GetString(i
), &wLine
, NULL
);
990 if ( wLine
> lbWidth
)
997 // And just a bit more for the checkbox if present and then some
998 // (these are rough guesses)
999 #if wxUSE_CHECKLISTBOX
1000 if ( m_hasCheckBoxes
)
1003 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1007 // Add room for the scrollbar
1008 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1010 // Don't make the listbox too tall but don't make it too small neither
1011 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1013 wxSize
best(lbWidth
, lbHeight
);
1014 CacheBestSize(best
);
1020 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1022 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1025 #endif // wxUSE_LISTBOX