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
->m_blockEvent
) return;
124 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
125 event
.SetEventObject( listbox
);
127 if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
))
129 wxArrayInt selections
;
130 listbox
->GetSelections( selections
);
132 if (selections
.GetCount() == 0)
134 // indicate that this is a deselection
135 event
.SetExtraLong( 0 );
138 listbox
->HandleWindowEvent( event
);
144 // indicate that this is a selection
145 event
.SetExtraLong( 1 );
146 event
.SetInt( selections
[0] );
148 listbox
->HandleWindowEvent( event
);
153 int index
= listbox
->GetSelection();
154 if (index
== wxNOT_FOUND
)
156 // indicate that this is a deselection
157 event
.SetExtraLong( 0 );
160 listbox
->HandleWindowEvent( event
);
166 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
168 // indicate that this is a selection
169 event
.SetExtraLong( 1 );
171 event
.SetInt( index
);
172 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
174 if ( listbox
->HasClientObjectData() )
175 event
.SetClientObject(
176 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
178 else if ( listbox
->HasClientUntypedData() )
179 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
181 listbox
->HandleWindowEvent( event
);
183 g_object_unref (entry
);
189 //-----------------------------------------------------------------------------
191 //-----------------------------------------------------------------------------
195 gtk_listbox_key_press_callback( GtkWidget
*WXUNUSED(widget
),
196 GdkEventKey
*gdk_event
,
199 if ((gdk_event
->keyval
== GDK_Return
) ||
200 (gdk_event
->keyval
== GDK_ISO_Enter
) ||
201 (gdk_event
->keyval
== GDK_KP_Enter
))
203 int index
= listbox
->GetSelection();
204 if (index
!= wxNOT_FOUND
)
207 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
208 event
.SetEventObject( listbox
);
210 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
212 // indicate that this is a selection
213 event
.SetExtraLong( 1 );
215 event
.SetInt( index
);
216 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
218 if ( listbox
->HasClientObjectData() )
219 event
.SetClientObject(
220 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
222 else if ( listbox
->HasClientUntypedData() )
223 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
225 /* bool ret = */ listbox
->HandleWindowEvent( event
);
227 g_object_unref (entry
);
229 // wxMac and wxMSW always invoke default action
232 // DClick not handled -> invoke default action
233 wxWindow
*tlw
= wxGetTopLevelParent( listbox
);
236 GtkWindow
*gtk_window
= GTK_WINDOW( tlw
->GetHandle() );
238 gtk_window_activate_default( gtk_window
);
242 // Always intercept, otherwise we'd get another dclick
243 // event from row_activated
252 //-----------------------------------------------------------------------------
253 // GtkTreeEntry destruction (to destroy client data)
254 //-----------------------------------------------------------------------------
257 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
260 if (listbox
->HasClientObjectData())
262 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
264 delete (wxClientData
*)userdata
;
269 //-----------------------------------------------------------------------------
270 // Sorting callback (standard CmpNoCase return value)
271 //-----------------------------------------------------------------------------
274 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
280 GtkTreeEntry
* entry2
;
282 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
284 WXLISTBOX_DATACOLUMN_ARG(listbox
),
286 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
288 WXLISTBOX_DATACOLUMN_ARG(listbox
),
290 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
291 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
293 //We compare collate keys here instead of calling g_utf8_collate
294 //as it is rather slow (and even the docs reccommend this)
295 int ret
= strcmp(gtk_tree_entry_get_collate_key(entry
),
296 gtk_tree_entry_get_collate_key(entry2
));
298 g_object_unref (entry
);
299 g_object_unref (entry2
);
305 //-----------------------------------------------------------------------------
306 // Searching callback (TRUE == not equal, FALSE == equal)
307 //-----------------------------------------------------------------------------
310 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
311 gint
WXUNUSED(column
),
318 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
320 WXLISTBOX_DATACOLUMN_ARG(listbox
),
322 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
323 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
325 int ret
= strcmp(keycollatekey
,
326 gtk_tree_entry_get_collate_key(entry
));
328 g_object_unref (entry
);
334 //-----------------------------------------------------------------------------
336 //-----------------------------------------------------------------------------
338 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
340 // ----------------------------------------------------------------------------
342 // ----------------------------------------------------------------------------
344 void wxListBox::Init()
346 m_treeview
= (GtkTreeView
*) NULL
;
347 #if wxUSE_CHECKLISTBOX
348 m_hasCheckBoxes
= false;
349 #endif // wxUSE_CHECKLISTBOX
352 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
353 const wxPoint
&pos
, const wxSize
&size
,
354 const wxArrayString
& choices
,
355 long style
, const wxValidator
& validator
,
356 const wxString
&name
)
358 wxCArrayString
chs(choices
);
360 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
361 style
, validator
, name
);
364 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
365 const wxPoint
&pos
, const wxSize
&size
,
366 int n
, const wxString choices
[],
367 long style
, const wxValidator
& validator
,
368 const wxString
&name
)
370 m_blockEvent
= false;
372 if (!PreCreation( parent
, pos
, size
) ||
373 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
375 wxFAIL_MSG( wxT("wxListBox creation failed") );
379 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
380 if (style
& wxLB_ALWAYS_SB
)
382 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
383 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
387 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
388 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
392 GtkScrolledWindowSetBorder(m_widget
, style
);
394 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
396 //wxListBox doesn't have a header :)
397 //NB: If enabled SetFirstItem doesn't work correctly
398 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
400 #if wxUSE_CHECKLISTBOX
402 ((wxCheckListBox
*)this)->DoCreateCheckList();
403 #endif // wxUSE_CHECKLISTBOX
405 // Create the data column
406 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
407 gtk_cell_renderer_text_new(),
409 WXLISTBOX_DATACOLUMN
, NULL
);
411 // Now create+set the model (GtkListStore) - first argument # of columns
412 #if wxUSE_CHECKLISTBOX
414 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
415 GTK_TYPE_TREE_ENTRY
);
418 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
420 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
422 g_object_unref (m_liststore
); //free on treeview destruction
424 // Disable the pop-up textctrl that enables searching - note that
425 // the docs specify that even if this disabled (which we are doing)
426 // the user can still have it through the start-interactive-search
427 // key binding...either way we want to provide a searchequal callback
428 // NB: If this is enabled a doubleclick event (activate) gets sent
429 // on a successful search
430 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
431 gtk_tree_view_set_search_equal_func(m_treeview
,
432 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
436 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
439 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
441 g_signal_connect_after (selection
, "changed",
442 G_CALLBACK (gtk_listitem_changed_callback
), this);
444 GtkSelectionMode mode
;
445 if (style
& wxLB_MULTIPLE
)
447 mode
= GTK_SELECTION_MULTIPLE
;
449 else if (style
& wxLB_EXTENDED
)
451 mode
= GTK_SELECTION_EXTENDED
;
455 // if style was 0 set single mode
456 m_windowStyle
|= wxLB_SINGLE
;
457 mode
= GTK_SELECTION_SINGLE
;
460 gtk_tree_selection_set_mode( selection
, mode
);
462 // Handle sortable stuff
463 if(HasFlag(wxLB_SORT
))
465 // Setup sorting in ascending (wx) order
466 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
467 WXLISTBOX_DATACOLUMN
,
470 // Set the sort callback
471 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
472 WXLISTBOX_DATACOLUMN
,
473 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
475 NULL
//"destroy notifier"
480 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
482 gtk_widget_show( GTK_WIDGET(m_treeview
) );
483 m_focusWidget
= GTK_WIDGET(m_treeview
);
485 Append(n
, choices
); // insert initial items
487 // generate dclick events
488 g_signal_connect_after(m_treeview
, "row-activated",
489 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
491 // for intercepting dclick generation by <ENTER>
492 g_signal_connect (m_treeview
, "key_press_event",
493 G_CALLBACK (gtk_listbox_key_press_callback
),
495 m_parent
->DoAddChild( this );
498 SetInitialSize(size
); // need this too because this is a wxControlWithItems
503 wxListBox::~wxListBox()
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
517 wxClientDataType type
)
519 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
521 InvalidateBestSize();
523 GtkTreeIter
* pIter
= NULL
; // append by default
525 if ( pos
!= GetCount() )
527 wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
528 wxT("internal wxListBox error in insertion") );
533 const unsigned int numItems
= items
.GetCount();
534 for ( unsigned int i
= 0; i
< numItems
; ++i
)
536 GtkTreeEntry
* entry
= gtk_tree_entry_new();
537 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
538 gtk_tree_entry_set_destroy_func(entry
,
539 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
543 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
545 GtkSetItem(itercur
, entry
);
547 g_object_unref (entry
);
550 AssignNewItemClientData(GtkGetIndexFor(itercur
), clientData
, i
, type
);
553 return pos
+ numItems
- 1;
556 // ----------------------------------------------------------------------------
558 // ----------------------------------------------------------------------------
560 void wxListBox::DoClear()
562 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
564 InvalidateBestSize();
566 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
569 void wxListBox::DoDeleteOneItem(unsigned int n
)
571 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
573 InvalidateBestSize();
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
);
583 // ----------------------------------------------------------------------------
584 // helper functions for working with iterators
585 // ----------------------------------------------------------------------------
587 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
589 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
592 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
599 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
602 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
604 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
606 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
608 int idx
= pIntPath
[0];
610 gtk_tree_path_free( path
);
615 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
616 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
619 if ( !GtkGetIteratorFor(n
, &iter
) )
623 GtkTreeEntry
* entry
= NULL
;
624 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
625 WXLISTBOX_DATACOLUMN
, &entry
, -1);
630 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
632 #if wxUSE_CHECKLISTBOX
633 if ( m_hasCheckBoxes
)
635 gtk_list_store_set(m_liststore
, &iter
,
636 0, FALSE
, // FALSE == not toggled
641 #endif // wxUSE_CHECKLISTBOX
643 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
647 // ----------------------------------------------------------------------------
649 // ----------------------------------------------------------------------------
651 void* wxListBox::DoGetItemClientData(unsigned int n
) const
653 wxCHECK_MSG( IsValid(n
), NULL
,
654 wxT("Invalid index passed to GetItemClientData") );
656 GtkTreeEntry
* entry
= GtkGetEntry(n
);
657 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
659 void* userdata
= gtk_tree_entry_get_userdata( entry
);
660 g_object_unref (entry
);
664 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
666 wxCHECK_RET( IsValid(n
),
667 wxT("Invalid index passed to SetItemClientData") );
669 GtkTreeEntry
* entry
= GtkGetEntry(n
);
670 wxCHECK_RET(entry
, wxT("could not get entry"));
672 gtk_tree_entry_set_userdata( entry
, clientData
);
673 g_object_unref (entry
);
676 // ----------------------------------------------------------------------------
677 // string list access
678 // ----------------------------------------------------------------------------
680 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
682 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
683 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
685 GtkTreeEntry
* entry
= GtkGetEntry(n
);
686 wxCHECK_RET( entry
, wxT("wrong listbox index") );
688 // update the item itself
689 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
691 // and update the model which will refresh the tree too
693 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
695 // FIXME: this resets the checked status of a wxCheckListBox item
697 GtkSetItem(iter
, entry
);
700 wxString
wxListBox::GetString(unsigned int n
) const
702 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
704 GtkTreeEntry
* entry
= GtkGetEntry(n
);
705 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
707 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
709 g_object_unref (entry
);
713 unsigned int wxListBox::GetCount() const
715 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
717 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
720 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
722 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
724 //Sort of hackish - maybe there is a faster way
725 unsigned int nCount
= wxListBox::GetCount();
727 for(unsigned int i
= 0; i
< nCount
; ++i
)
729 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
734 // it's not an error if the string is not found -> no wxCHECK
738 // ----------------------------------------------------------------------------
740 // ----------------------------------------------------------------------------
742 int wxListBox::GetSelection() const
744 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
745 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
746 wxT("must be single selection listbox"));
749 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
751 // only works on single-sel
752 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
755 return GtkGetIndexFor(iter
);
758 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
760 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
766 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
768 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
769 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
772 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
776 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
779 return aSelections
.GetCount();
782 bool wxListBox::IsSelected( int n
) const
784 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
786 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
789 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
791 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
794 void wxListBox::DoSetSelection( int n
, bool select
)
796 // passing -1 to SetSelection() is documented to deselect all items
797 if ( n
== wxNOT_FOUND
)
799 // ... and not generate any events in the process
804 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
806 // don't generate the selection event
807 GtkSetSelection(n
, select
, true);
810 void wxListBox::GtkDeselectAll()
812 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
814 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
818 gtk_tree_selection_unselect_all(selection
);
820 m_blockEvent
= false;
823 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
825 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
827 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
830 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
832 m_blockEvent
= blockEvent
;
835 gtk_tree_selection_select_iter(selection
, &iter
);
837 gtk_tree_selection_unselect_iter(selection
, &iter
);
839 GtkTreePath
* path
= gtk_tree_model_get_path(
840 GTK_TREE_MODEL(m_liststore
), &iter
);
842 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
844 gtk_tree_path_free(path
);
846 m_blockEvent
= false;
849 void wxListBox::DoSetFirstItem( int n
)
851 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
852 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
854 //RN: I have no idea why this line is needed...
855 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
859 if ( !GtkGetIteratorFor(n
, &iter
) )
862 GtkTreePath
* path
= gtk_tree_model_get_path(
863 GTK_TREE_MODEL(m_liststore
), &iter
);
865 // Scroll to the desired cell (0.0 == topleft alignment)
866 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
869 gtk_tree_path_free(path
);
872 // ----------------------------------------------------------------------------
874 // ----------------------------------------------------------------------------
876 int wxListBox::DoListHitTest(const wxPoint
& point
) const
878 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
879 // we only want visible items we need to check for it manually here
880 if ( !GetClientRect().Contains(point
) )
883 // need to translate from master window since it is in client coords
885 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
886 &binx
, &biny
, NULL
, NULL
, NULL
);
889 if ( !gtk_tree_view_get_path_at_pos
895 NULL
, // [out] column (always 0 here)
896 NULL
, // [out] x-coord relative to the cell (not interested)
897 NULL
// [out] y-coord relative to the cell
903 int index
= gtk_tree_path_get_indices(path
)[0];
904 gtk_tree_path_free(path
);
909 // ----------------------------------------------------------------------------
911 // ----------------------------------------------------------------------------
914 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
916 // RN: Is this needed anymore?
917 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, (gchar
*) NULL
);
919 #endif // wxUSE_TOOLTIPS
921 GtkWidget
*wxListBox::GetConnectWidget()
923 // the correct widget for listbox events (such as mouse clicks for example)
924 // is m_treeview, not the parent scrolled window
925 return GTK_WIDGET(m_treeview
);
928 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
930 return gtk_tree_view_get_bin_window(m_treeview
);
933 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
935 if (m_hasBgCol
&& m_backgroundColour
.Ok())
937 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
940 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
941 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
942 gdk_window_clear( window
);
946 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
949 wxSize
wxListBox::DoGetBestSize() const
951 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
953 // Start with a minimum size that's not too small
955 GetTextExtent( wxT("X"), &cx
, &cy
);
956 int lbWidth
= 3 * cx
;
959 // Get the visible area of the tree view (limit to the 10th item
960 // so that it isn't too big)
961 unsigned int count
= GetCount();
966 // Find the widest line
967 for(unsigned int i
= 0; i
< count
; i
++) {
968 wxString
str(GetString(i
));
969 GetTextExtent(str
, &wLine
, NULL
);
970 lbWidth
= wxMax(lbWidth
, wLine
);
975 // And just a bit more for the checkbox if present and then some
976 // (these are rough guesses)
977 #if wxUSE_CHECKLISTBOX
978 if ( m_hasCheckBoxes
)
981 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
985 // don't make the listbox too tall (limit height to around 10 items) but don't
986 // make it too small neither
987 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
990 // Add room for the scrollbar
991 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
993 wxSize
best(lbWidth
, lbHeight
);
1000 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1002 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1005 #endif // wxUSE_LISTBOX