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 if (listbox
->HasFlag(wxLB_MULTIPLE
| wxLB_EXTENDED
))
124 listbox
->CalcAndSendEvent();
126 else // single selection
128 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
129 event
.SetEventObject( listbox
);
131 int index
= listbox
->GetSelection();
132 if (index
== wxNOT_FOUND
)
134 // indicate that this is a deselection
135 event
.SetExtraLong( 0 );
138 listbox
->HandleWindowEvent( event
);
144 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
146 // indicate that this is a selection
147 event
.SetExtraLong( 1 );
149 event
.SetInt( index
);
150 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
152 if ( listbox
->HasClientObjectData() )
153 event
.SetClientObject(
154 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
156 else if ( listbox
->HasClientUntypedData() )
157 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
159 listbox
->HandleWindowEvent( event
);
161 g_object_unref (entry
);
167 //-----------------------------------------------------------------------------
169 //-----------------------------------------------------------------------------
173 gtk_listbox_key_press_callback( GtkWidget
*WXUNUSED(widget
),
174 GdkEventKey
*gdk_event
,
177 if ((gdk_event
->keyval
== GDK_Return
) ||
178 (gdk_event
->keyval
== GDK_ISO_Enter
) ||
179 (gdk_event
->keyval
== GDK_KP_Enter
))
182 if (!listbox
->HasMultipleSelection())
183 index
= listbox
->GetSelection();
187 if (listbox
->GetSelections( sels
) < 1)
192 if (index
!= wxNOT_FOUND
)
194 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
195 event
.SetEventObject( listbox
);
197 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
199 // indicate that this is a selection
200 event
.SetExtraLong( 1 );
202 event
.SetInt( index
);
203 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
205 if ( listbox
->HasClientObjectData() )
206 event
.SetClientObject(
207 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
209 else if ( listbox
->HasClientUntypedData() )
210 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
212 /* bool ret = */ listbox
->HandleWindowEvent( event
);
214 g_object_unref (entry
);
216 // wxMac and wxMSW always invoke default action
219 // DClick not handled -> invoke default action
220 wxWindow
*tlw
= wxGetTopLevelParent( listbox
);
223 GtkWindow
*gtk_window
= GTK_WINDOW( tlw
->GetHandle() );
225 gtk_window_activate_default( gtk_window
);
229 // Always intercept, otherwise we'd get another dclick
230 // event from row_activated
239 //-----------------------------------------------------------------------------
240 // GtkTreeEntry destruction (to destroy client data)
241 //-----------------------------------------------------------------------------
244 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
247 if (listbox
->HasClientObjectData())
249 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
251 delete (wxClientData
*)userdata
;
256 //-----------------------------------------------------------------------------
257 // Sorting callback (standard CmpNoCase return value)
258 //-----------------------------------------------------------------------------
261 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
267 GtkTreeEntry
* entry2
;
269 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
271 WXLISTBOX_DATACOLUMN_ARG(listbox
),
273 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
275 WXLISTBOX_DATACOLUMN_ARG(listbox
),
277 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
278 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
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
));
285 g_object_unref (entry
);
286 g_object_unref (entry2
);
292 //-----------------------------------------------------------------------------
293 // Searching callback (TRUE == not equal, FALSE == equal)
294 //-----------------------------------------------------------------------------
297 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
298 gint
WXUNUSED(column
),
305 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
307 WXLISTBOX_DATACOLUMN_ARG(listbox
),
309 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
310 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
312 int ret
= strcmp(keycollatekey
,
313 gtk_tree_entry_get_collate_key(entry
));
315 g_object_unref (entry
);
321 //-----------------------------------------------------------------------------
323 //-----------------------------------------------------------------------------
325 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
327 // ----------------------------------------------------------------------------
329 // ----------------------------------------------------------------------------
331 void wxListBox::Init()
334 #if wxUSE_CHECKLISTBOX
335 m_hasCheckBoxes
= false;
336 #endif // wxUSE_CHECKLISTBOX
339 bool 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
)
345 wxCArrayString
chs(choices
);
347 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
348 style
, validator
, name
);
351 bool 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
)
357 if (!PreCreation( parent
, pos
, size
) ||
358 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
360 wxFAIL_MSG( wxT("wxListBox creation failed") );
364 m_widget
= gtk_scrolled_window_new( NULL
, NULL
);
365 g_object_ref(m_widget
);
366 if (style
& wxLB_ALWAYS_SB
)
368 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
369 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
373 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
374 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
378 GTKScrolledWindowSetBorder(m_widget
, style
);
380 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
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
);
386 #if wxUSE_CHECKLISTBOX
388 ((wxCheckListBox
*)this)->DoCreateCheckList();
389 #endif // wxUSE_CHECKLISTBOX
391 // Create the data column
392 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
393 gtk_cell_renderer_text_new(),
395 WXLISTBOX_DATACOLUMN
, NULL
);
397 // Now create+set the model (GtkListStore) - first argument # of columns
398 #if wxUSE_CHECKLISTBOX
400 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
401 GTK_TYPE_TREE_ENTRY
);
404 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
406 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
408 g_object_unref (m_liststore
); //free on treeview destruction
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
,
422 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
424 GtkSelectionMode mode
;
425 // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
426 if ( style
& (wxLB_MULTIPLE
| wxLB_EXTENDED
) )
428 mode
= GTK_SELECTION_MULTIPLE
;
430 else // no multi-selection flags specified
432 m_windowStyle
|= wxLB_SINGLE
;
433 mode
= GTK_SELECTION_SINGLE
;
436 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
437 gtk_tree_selection_set_mode( selection
, mode
);
439 // Handle sortable stuff
440 if(HasFlag(wxLB_SORT
))
442 // Setup sorting in ascending (wx) order
443 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
444 WXLISTBOX_DATACOLUMN
,
447 // Set the sort callback
448 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
449 WXLISTBOX_DATACOLUMN
,
450 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
452 NULL
//"destroy notifier"
457 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
459 gtk_widget_show( GTK_WIDGET(m_treeview
) );
460 m_focusWidget
= GTK_WIDGET(m_treeview
);
462 Append(n
, choices
); // insert initial items
464 // generate dclick events
465 g_signal_connect_after(m_treeview
, "row-activated",
466 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
468 // for intercepting dclick generation by <ENTER>
469 g_signal_connect (m_treeview
, "key_press_event",
470 G_CALLBACK (gtk_listbox_key_press_callback
),
472 m_parent
->DoAddChild( this );
475 SetInitialSize(size
); // need this too because this is a wxControlWithItems
477 g_signal_connect_after (selection
, "changed",
478 G_CALLBACK (gtk_listitem_changed_callback
), this);
483 wxListBox::~wxListBox()
490 void wxListBox::GtkDisableEvents()
492 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
494 g_signal_handlers_block_by_func(selection
,
495 (gpointer
) gtk_listitem_changed_callback
, this);
498 void wxListBox::GtkEnableEvents()
500 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
502 g_signal_handlers_unblock_by_func(selection
,
503 (gpointer
) gtk_listitem_changed_callback
, this);
505 UpdateOldSelections();
509 void wxListBox::Update()
514 gdk_window_process_updates(GTK_WIDGET(m_treeview
)->window
, TRUE
);
517 // ----------------------------------------------------------------------------
519 // ----------------------------------------------------------------------------
521 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
524 wxClientDataType type
)
526 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
528 InvalidateBestSize();
530 GtkTreeIter
* pIter
= NULL
; // append by default
532 if ( pos
!= GetCount() )
534 wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
535 wxT("internal wxListBox error in insertion") );
540 const unsigned int numItems
= items
.GetCount();
541 for ( unsigned int i
= 0; i
< numItems
; ++i
)
543 GtkTreeEntry
* entry
= gtk_tree_entry_new();
544 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
545 gtk_tree_entry_set_destroy_func(entry
,
546 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
550 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
552 GtkSetItem(itercur
, entry
);
554 g_object_unref (entry
);
557 AssignNewItemClientData(GtkGetIndexFor(itercur
), clientData
, i
, type
);
560 UpdateOldSelections();
562 return pos
+ numItems
- 1;
565 // ----------------------------------------------------------------------------
567 // ----------------------------------------------------------------------------
569 void wxListBox::DoClear()
571 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
573 GtkDisableEvents(); // just in case
575 InvalidateBestSize();
577 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
582 void wxListBox::DoDeleteOneItem(unsigned int n
)
584 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
586 InvalidateBestSize();
588 GtkDisableEvents(); // just in case
591 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
593 // this returns false if iter is invalid (e.g. deleting item at end) but
594 // since we don't use iter, we ignore the return value
595 gtk_list_store_remove(m_liststore
, &iter
);
600 // ----------------------------------------------------------------------------
601 // helper functions for working with iterators
602 // ----------------------------------------------------------------------------
604 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
606 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
609 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
616 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
619 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
621 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
623 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
625 int idx
= pIntPath
[0];
627 gtk_tree_path_free( path
);
632 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
633 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
636 if ( !GtkGetIteratorFor(n
, &iter
) )
640 GtkTreeEntry
* entry
= NULL
;
641 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
642 WXLISTBOX_DATACOLUMN
, &entry
, -1);
647 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
649 #if wxUSE_CHECKLISTBOX
650 if ( m_hasCheckBoxes
)
652 gtk_list_store_set(m_liststore
, &iter
,
653 0, FALSE
, // FALSE == not toggled
658 #endif // wxUSE_CHECKLISTBOX
660 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
664 // ----------------------------------------------------------------------------
666 // ----------------------------------------------------------------------------
668 void* wxListBox::DoGetItemClientData(unsigned int n
) const
670 wxCHECK_MSG( IsValid(n
), NULL
,
671 wxT("Invalid index passed to GetItemClientData") );
673 GtkTreeEntry
* entry
= GtkGetEntry(n
);
674 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
676 void* userdata
= gtk_tree_entry_get_userdata( entry
);
677 g_object_unref (entry
);
681 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
683 wxCHECK_RET( IsValid(n
),
684 wxT("Invalid index passed to SetItemClientData") );
686 GtkTreeEntry
* entry
= GtkGetEntry(n
);
687 wxCHECK_RET(entry
, wxT("could not get entry"));
689 gtk_tree_entry_set_userdata( entry
, clientData
);
690 g_object_unref (entry
);
693 // ----------------------------------------------------------------------------
694 // string list access
695 // ----------------------------------------------------------------------------
697 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
699 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
700 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
702 GtkTreeEntry
* entry
= GtkGetEntry(n
);
703 wxCHECK_RET( entry
, wxT("wrong listbox index") );
705 // update the item itself
706 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
708 // and update the model which will refresh the tree too
710 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
712 // FIXME: this resets the checked status of a wxCheckListBox item
714 GtkSetItem(iter
, entry
);
717 wxString
wxListBox::GetString(unsigned int n
) const
719 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
721 GtkTreeEntry
* entry
= GtkGetEntry(n
);
722 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
724 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
726 g_object_unref (entry
);
730 unsigned int wxListBox::GetCount() const
732 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
734 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
737 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
739 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
741 //Sort of hackish - maybe there is a faster way
742 unsigned int nCount
= wxListBox::GetCount();
744 for(unsigned int i
= 0; i
< nCount
; ++i
)
746 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
751 // it's not an error if the string is not found -> no wxCHECK
755 // ----------------------------------------------------------------------------
757 // ----------------------------------------------------------------------------
759 int wxListBox::GetSelection() const
761 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
762 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
763 wxT("must be single selection listbox"));
766 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
768 // only works on single-sel
769 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
772 return GtkGetIndexFor(iter
);
775 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
777 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
783 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
785 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
786 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
789 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
793 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
796 return aSelections
.GetCount();
799 bool wxListBox::IsSelected( int n
) const
801 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
803 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
806 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
808 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
811 void wxListBox::DoSetSelection( int n
, bool select
)
813 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
817 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
819 // passing -1 to SetSelection() is documented to deselect all items
820 if ( n
== wxNOT_FOUND
)
822 gtk_tree_selection_unselect_all(selection
);
827 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
831 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
834 gtk_tree_selection_select_iter(selection
, &iter
);
836 gtk_tree_selection_unselect_iter(selection
, &iter
);
838 GtkTreePath
* path
= gtk_tree_model_get_path(
839 GTK_TREE_MODEL(m_liststore
), &iter
);
841 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
843 gtk_tree_path_free(path
);
848 void wxListBox::DoScrollToCell(int n
, float alignY
, float alignX
)
850 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
851 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
853 //RN: I have no idea why this line is needed...
854 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
858 if ( !GtkGetIteratorFor(n
, &iter
) )
861 GtkTreePath
* path
= gtk_tree_model_get_path(
862 GTK_TREE_MODEL(m_liststore
), &iter
);
864 // Scroll to the desired cell (0.0 == topleft alignment)
865 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
866 TRUE
, alignY
, alignX
);
868 gtk_tree_path_free(path
);
871 void wxListBox::DoSetFirstItem(int n
)
873 DoScrollToCell(n
, 0, 0);
876 void wxListBox::EnsureVisible(int n
)
878 DoScrollToCell(n
, 0.5, 0);
881 // ----------------------------------------------------------------------------
883 // ----------------------------------------------------------------------------
885 int wxListBox::DoListHitTest(const wxPoint
& point
) const
887 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
888 // we only want visible items we need to check for it manually here
889 if ( !GetClientRect().Contains(point
) )
892 // need to translate from master window since it is in client coords
894 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
895 &binx
, &biny
, NULL
, NULL
, NULL
);
898 if ( !gtk_tree_view_get_path_at_pos
904 NULL
, // [out] column (always 0 here)
905 NULL
, // [out] x-coord relative to the cell (not interested)
906 NULL
// [out] y-coord relative to the cell
912 int index
= gtk_tree_path_get_indices(path
)[0];
913 gtk_tree_path_free(path
);
918 // ----------------------------------------------------------------------------
920 // ----------------------------------------------------------------------------
923 void wxListBox::GTKApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
925 // RN: Is this needed anymore?
926 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, NULL
);
928 #endif // wxUSE_TOOLTIPS
930 GtkWidget
*wxListBox::GetConnectWidget()
932 // the correct widget for listbox events (such as mouse clicks for example)
933 // is m_treeview, not the parent scrolled window
934 return GTK_WIDGET(m_treeview
);
937 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
939 return gtk_tree_view_get_bin_window(m_treeview
);
942 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
944 if (m_hasBgCol
&& m_backgroundColour
.Ok())
946 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
949 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
950 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
951 gdk_window_clear( window
);
955 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
958 wxSize
wxListBox::DoGetBestSize() const
960 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
962 // Start with a minimum size that's not too small
964 GetTextExtent( wxT("X"), &cx
, &cy
);
968 // Find the widest string.
969 const unsigned int count
= GetCount();
973 for ( unsigned int i
= 0; i
< count
; i
++ )
975 GetTextExtent(GetString(i
), &wLine
, NULL
);
976 if ( wLine
> lbWidth
)
983 // And just a bit more for the checkbox if present and then some
984 // (these are rough guesses)
985 #if wxUSE_CHECKLISTBOX
986 if ( m_hasCheckBoxes
)
989 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
993 // Add room for the scrollbar
994 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
996 // Don't make the listbox too tall but don't make it too small neither
997 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
999 wxSize
best(lbWidth
, lbHeight
);
1000 CacheBestSize(best
);
1006 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1008 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1011 #endif // wxUSE_LISTBOX