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
) || listbox
->HasFlag(wxLB_EXTENDED
))
124 listbox
->CalcAndSendEvent();
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
))
181 int index
= listbox
->GetSelection();
182 if (index
!= wxNOT_FOUND
)
185 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
186 event
.SetEventObject( listbox
);
188 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
190 // indicate that this is a selection
191 event
.SetExtraLong( 1 );
193 event
.SetInt( index
);
194 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
196 if ( listbox
->HasClientObjectData() )
197 event
.SetClientObject(
198 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
200 else if ( listbox
->HasClientUntypedData() )
201 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
203 /* bool ret = */ listbox
->HandleWindowEvent( event
);
205 g_object_unref (entry
);
207 // wxMac and wxMSW always invoke default action
210 // DClick not handled -> invoke default action
211 wxWindow
*tlw
= wxGetTopLevelParent( listbox
);
214 GtkWindow
*gtk_window
= GTK_WINDOW( tlw
->GetHandle() );
216 gtk_window_activate_default( gtk_window
);
220 // Always intercept, otherwise we'd get another dclick
221 // event from row_activated
230 //-----------------------------------------------------------------------------
231 // GtkTreeEntry destruction (to destroy client data)
232 //-----------------------------------------------------------------------------
235 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
238 if (listbox
->HasClientObjectData())
240 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
242 delete (wxClientData
*)userdata
;
247 //-----------------------------------------------------------------------------
248 // Sorting callback (standard CmpNoCase return value)
249 //-----------------------------------------------------------------------------
252 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
258 GtkTreeEntry
* entry2
;
260 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
262 WXLISTBOX_DATACOLUMN_ARG(listbox
),
264 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
266 WXLISTBOX_DATACOLUMN_ARG(listbox
),
268 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
269 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
271 //We compare collate keys here instead of calling g_utf8_collate
272 //as it is rather slow (and even the docs reccommend this)
273 int ret
= strcmp(gtk_tree_entry_get_collate_key(entry
),
274 gtk_tree_entry_get_collate_key(entry2
));
276 g_object_unref (entry
);
277 g_object_unref (entry2
);
283 //-----------------------------------------------------------------------------
284 // Searching callback (TRUE == not equal, FALSE == equal)
285 //-----------------------------------------------------------------------------
288 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
289 gint
WXUNUSED(column
),
296 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
298 WXLISTBOX_DATACOLUMN_ARG(listbox
),
300 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
301 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
303 int ret
= strcmp(keycollatekey
,
304 gtk_tree_entry_get_collate_key(entry
));
306 g_object_unref (entry
);
312 //-----------------------------------------------------------------------------
314 //-----------------------------------------------------------------------------
316 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
318 // ----------------------------------------------------------------------------
320 // ----------------------------------------------------------------------------
322 void wxListBox::Init()
324 m_treeview
= (GtkTreeView
*) NULL
;
325 #if wxUSE_CHECKLISTBOX
326 m_hasCheckBoxes
= false;
327 #endif // wxUSE_CHECKLISTBOX
330 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
331 const wxPoint
&pos
, const wxSize
&size
,
332 const wxArrayString
& choices
,
333 long style
, const wxValidator
& validator
,
334 const wxString
&name
)
336 wxCArrayString
chs(choices
);
338 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
339 style
, validator
, name
);
342 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
343 const wxPoint
&pos
, const wxSize
&size
,
344 int n
, const wxString choices
[],
345 long style
, const wxValidator
& validator
,
346 const wxString
&name
)
348 if (!PreCreation( parent
, pos
, size
) ||
349 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
351 wxFAIL_MSG( wxT("wxListBox creation failed") );
355 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
356 if (style
& wxLB_ALWAYS_SB
)
358 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
359 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
363 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
364 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
368 GtkScrolledWindowSetBorder(m_widget
, style
);
370 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
372 //wxListBox doesn't have a header :)
373 //NB: If enabled SetFirstItem doesn't work correctly
374 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
376 #if wxUSE_CHECKLISTBOX
378 ((wxCheckListBox
*)this)->DoCreateCheckList();
379 #endif // wxUSE_CHECKLISTBOX
381 // Create the data column
382 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
383 gtk_cell_renderer_text_new(),
385 WXLISTBOX_DATACOLUMN
, NULL
);
387 // Now create+set the model (GtkListStore) - first argument # of columns
388 #if wxUSE_CHECKLISTBOX
390 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
391 GTK_TYPE_TREE_ENTRY
);
394 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
396 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
398 g_object_unref (m_liststore
); //free on treeview destruction
400 // Disable the pop-up textctrl that enables searching - note that
401 // the docs specify that even if this disabled (which we are doing)
402 // the user can still have it through the start-interactive-search
403 // key binding...either way we want to provide a searchequal callback
404 // NB: If this is enabled a doubleclick event (activate) gets sent
405 // on a successful search
406 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
407 gtk_tree_view_set_search_equal_func(m_treeview
,
408 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
412 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
414 GtkSelectionMode mode
;
415 if (style
& wxLB_MULTIPLE
)
417 mode
= GTK_SELECTION_MULTIPLE
;
419 else if (style
& wxLB_EXTENDED
)
421 mode
= GTK_SELECTION_EXTENDED
;
425 // if style was 0 set single mode
426 m_windowStyle
|= wxLB_SINGLE
;
427 mode
= GTK_SELECTION_SINGLE
;
430 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
431 gtk_tree_selection_set_mode( selection
, mode
);
433 // Handle sortable stuff
434 if(HasFlag(wxLB_SORT
))
436 // Setup sorting in ascending (wx) order
437 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
438 WXLISTBOX_DATACOLUMN
,
441 // Set the sort callback
442 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
443 WXLISTBOX_DATACOLUMN
,
444 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
446 NULL
//"destroy notifier"
451 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
453 gtk_widget_show( GTK_WIDGET(m_treeview
) );
454 m_focusWidget
= GTK_WIDGET(m_treeview
);
456 Append(n
, choices
); // insert initial items
458 // generate dclick events
459 g_signal_connect_after(m_treeview
, "row-activated",
460 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
462 // for intercepting dclick generation by <ENTER>
463 g_signal_connect (m_treeview
, "key_press_event",
464 G_CALLBACK (gtk_listbox_key_press_callback
),
466 m_parent
->DoAddChild( this );
469 SetInitialSize(size
); // need this too because this is a wxControlWithItems
471 g_signal_connect_after (selection
, "changed",
472 G_CALLBACK (gtk_listitem_changed_callback
), this);
477 wxListBox::~wxListBox()
484 void wxListBox::GtkDisableEvents()
486 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
488 g_signal_handlers_block_by_func(selection
,
489 (gpointer
) gtk_listitem_changed_callback
, this);
492 void wxListBox::GtkEnableEvents()
494 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
496 g_signal_handlers_unblock_by_func(selection
,
497 (gpointer
) gtk_listitem_changed_callback
, this);
499 UpdateOldSelections();
502 // ----------------------------------------------------------------------------
504 // ----------------------------------------------------------------------------
506 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
509 wxClientDataType type
)
511 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
513 InvalidateBestSize();
515 GtkTreeIter
* pIter
= NULL
; // append by default
517 if ( pos
!= GetCount() )
519 wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
520 wxT("internal wxListBox error in insertion") );
525 const unsigned int numItems
= items
.GetCount();
526 for ( unsigned int i
= 0; i
< numItems
; ++i
)
528 GtkTreeEntry
* entry
= gtk_tree_entry_new();
529 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
530 gtk_tree_entry_set_destroy_func(entry
,
531 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
535 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
537 GtkSetItem(itercur
, entry
);
539 g_object_unref (entry
);
542 AssignNewItemClientData(GtkGetIndexFor(itercur
), clientData
, i
, type
);
545 UpdateOldSelections();
547 return pos
+ numItems
- 1;
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 void wxListBox::DoClear()
556 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
558 GtkDisableEvents(); // just in case
560 InvalidateBestSize();
562 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
567 void wxListBox::DoDeleteOneItem(unsigned int n
)
569 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
571 InvalidateBestSize();
573 GtkDisableEvents(); // just in case
576 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
578 // this returns false if iter is invalid (e.g. deleting item at end) but
579 // since we don't use iter, we ignore the return value
580 gtk_list_store_remove(m_liststore
, &iter
);
585 // ----------------------------------------------------------------------------
586 // helper functions for working with iterators
587 // ----------------------------------------------------------------------------
589 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
591 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
594 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
601 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
604 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
606 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
608 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
610 int idx
= pIntPath
[0];
612 gtk_tree_path_free( path
);
617 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
618 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
621 if ( !GtkGetIteratorFor(n
, &iter
) )
625 GtkTreeEntry
* entry
= NULL
;
626 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
627 WXLISTBOX_DATACOLUMN
, &entry
, -1);
632 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
634 #if wxUSE_CHECKLISTBOX
635 if ( m_hasCheckBoxes
)
637 gtk_list_store_set(m_liststore
, &iter
,
638 0, FALSE
, // FALSE == not toggled
643 #endif // wxUSE_CHECKLISTBOX
645 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
649 // ----------------------------------------------------------------------------
651 // ----------------------------------------------------------------------------
653 void* wxListBox::DoGetItemClientData(unsigned int n
) const
655 wxCHECK_MSG( IsValid(n
), NULL
,
656 wxT("Invalid index passed to GetItemClientData") );
658 GtkTreeEntry
* entry
= GtkGetEntry(n
);
659 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
661 void* userdata
= gtk_tree_entry_get_userdata( entry
);
662 g_object_unref (entry
);
666 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
668 wxCHECK_RET( IsValid(n
),
669 wxT("Invalid index passed to SetItemClientData") );
671 GtkTreeEntry
* entry
= GtkGetEntry(n
);
672 wxCHECK_RET(entry
, wxT("could not get entry"));
674 gtk_tree_entry_set_userdata( entry
, clientData
);
675 g_object_unref (entry
);
678 // ----------------------------------------------------------------------------
679 // string list access
680 // ----------------------------------------------------------------------------
682 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
684 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
685 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
687 GtkTreeEntry
* entry
= GtkGetEntry(n
);
688 wxCHECK_RET( entry
, wxT("wrong listbox index") );
690 // update the item itself
691 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
693 // and update the model which will refresh the tree too
695 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
697 // FIXME: this resets the checked status of a wxCheckListBox item
699 GtkSetItem(iter
, entry
);
702 wxString
wxListBox::GetString(unsigned int n
) const
704 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
706 GtkTreeEntry
* entry
= GtkGetEntry(n
);
707 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
709 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
711 g_object_unref (entry
);
715 unsigned int wxListBox::GetCount() const
717 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
719 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
722 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
724 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
726 //Sort of hackish - maybe there is a faster way
727 unsigned int nCount
= wxListBox::GetCount();
729 for(unsigned int i
= 0; i
< nCount
; ++i
)
731 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
736 // it's not an error if the string is not found -> no wxCHECK
740 // ----------------------------------------------------------------------------
742 // ----------------------------------------------------------------------------
744 int wxListBox::GetSelection() const
746 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
747 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
748 wxT("must be single selection listbox"));
751 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
753 // only works on single-sel
754 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
757 return GtkGetIndexFor(iter
);
760 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
762 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
768 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
770 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
771 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
774 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
778 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
781 return aSelections
.GetCount();
784 bool wxListBox::IsSelected( int n
) const
786 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
788 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
791 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
793 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
796 void wxListBox::DoSetSelection( int n
, bool select
)
798 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
802 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
804 // passing -1 to SetSelection() is documented to deselect all items
805 if ( n
== wxNOT_FOUND
)
807 gtk_tree_selection_unselect_all(selection
);
812 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
816 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
819 gtk_tree_selection_select_iter(selection
, &iter
);
821 gtk_tree_selection_unselect_iter(selection
, &iter
);
823 GtkTreePath
* path
= gtk_tree_model_get_path(
824 GTK_TREE_MODEL(m_liststore
), &iter
);
826 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
828 gtk_tree_path_free(path
);
833 void wxListBox::DoScrollToCell(int n
, float alignY
, float alignX
)
835 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
836 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
838 //RN: I have no idea why this line is needed...
839 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
843 if ( !GtkGetIteratorFor(n
, &iter
) )
846 GtkTreePath
* path
= gtk_tree_model_get_path(
847 GTK_TREE_MODEL(m_liststore
), &iter
);
849 // Scroll to the desired cell (0.0 == topleft alignment)
850 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
851 TRUE
, alignY
, alignX
);
853 gtk_tree_path_free(path
);
856 void wxListBox::DoSetFirstItem(int n
)
858 DoScrollToCell(n
, 0, 0);
861 void wxListBox::EnsureVisible(int n
)
863 DoScrollToCell(n
, 0.5, 0);
866 // ----------------------------------------------------------------------------
868 // ----------------------------------------------------------------------------
870 int wxListBox::DoListHitTest(const wxPoint
& point
) const
872 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
873 // we only want visible items we need to check for it manually here
874 if ( !GetClientRect().Contains(point
) )
877 // need to translate from master window since it is in client coords
879 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
880 &binx
, &biny
, NULL
, NULL
, NULL
);
883 if ( !gtk_tree_view_get_path_at_pos
889 NULL
, // [out] column (always 0 here)
890 NULL
, // [out] x-coord relative to the cell (not interested)
891 NULL
// [out] y-coord relative to the cell
897 int index
= gtk_tree_path_get_indices(path
)[0];
898 gtk_tree_path_free(path
);
903 // ----------------------------------------------------------------------------
905 // ----------------------------------------------------------------------------
908 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
910 // RN: Is this needed anymore?
911 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, (gchar
*) NULL
);
913 #endif // wxUSE_TOOLTIPS
915 GtkWidget
*wxListBox::GetConnectWidget()
917 // the correct widget for listbox events (such as mouse clicks for example)
918 // is m_treeview, not the parent scrolled window
919 return GTK_WIDGET(m_treeview
);
922 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
924 return gtk_tree_view_get_bin_window(m_treeview
);
927 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
929 if (m_hasBgCol
&& m_backgroundColour
.Ok())
931 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
934 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
935 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
936 gdk_window_clear( window
);
940 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
943 wxSize
wxListBox::DoGetBestSize() const
945 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
947 // Start with a minimum size that's not too small
949 GetTextExtent( wxT("X"), &cx
, &cy
);
953 // Find the widest string.
954 const unsigned int count
= GetCount();
958 for ( unsigned int i
= 0; i
< count
; i
++ )
960 GetTextExtent(GetString(i
), &wLine
, NULL
);
961 if ( wLine
> lbWidth
)
968 // And just a bit more for the checkbox if present and then some
969 // (these are rough guesses)
970 #if wxUSE_CHECKLISTBOX
971 if ( m_hasCheckBoxes
)
974 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
978 // Add room for the scrollbar
979 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
981 // Don't make the listbox too tall but don't make it too small neither
982 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
984 wxSize
best(lbWidth
, lbHeight
);
991 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
993 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
996 #endif // wxUSE_LISTBOX