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
*treeview
,
67 GtkTreeViewColumn
*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
->GetEventHandler()->ProcessEvent( event
);
111 //-----------------------------------------------------------------------------
113 //-----------------------------------------------------------------------------
117 gtk_listitem_changed_callback( GtkTreeSelection
* selection
, wxListBox
*listbox
)
119 if (g_blockEventsOnDrag
) return;
121 if (listbox
->m_blockEvent
) return;
123 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
124 event
.SetEventObject( listbox
);
126 if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
))
128 wxArrayInt selections
;
129 listbox
->GetSelections( selections
);
131 if (selections
.GetCount() == 0)
133 // indicate that this is a deselection
134 event
.SetExtraLong( 0 );
137 listbox
->GetEventHandler()->ProcessEvent( event
);
143 // indicate that this is a selection
144 event
.SetExtraLong( 1 );
145 event
.SetInt( selections
[0] );
147 listbox
->GetEventHandler()->ProcessEvent( event
);
152 int index
= listbox
->GetSelection();
153 if (index
== wxNOT_FOUND
)
155 // indicate that this is a deselection
156 event
.SetExtraLong( 0 );
159 listbox
->GetEventHandler()->ProcessEvent( event
);
165 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
167 // indicate that this is a selection
168 event
.SetExtraLong( 1 );
170 event
.SetInt( index
);
171 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
173 if ( listbox
->HasClientObjectData() )
174 event
.SetClientObject(
175 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
177 else if ( listbox
->HasClientUntypedData() )
178 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
180 listbox
->GetEventHandler()->ProcessEvent( event
);
182 g_object_unref (entry
);
188 //-----------------------------------------------------------------------------
189 // GtkTreeEntry destruction (to destroy client data)
190 //-----------------------------------------------------------------------------
193 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
196 if (listbox
->HasClientObjectData())
198 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
200 delete (wxClientData
*)userdata
;
205 //-----------------------------------------------------------------------------
206 // Sorting callback (standard CmpNoCase return value)
207 //-----------------------------------------------------------------------------
210 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
216 GtkTreeEntry
* entry2
;
218 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
220 WXLISTBOX_DATACOLUMN_ARG(listbox
),
222 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
224 WXLISTBOX_DATACOLUMN_ARG(listbox
),
226 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
227 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
229 //We compare collate keys here instead of calling g_utf8_collate
230 //as it is rather slow (and even the docs reccommend this)
231 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
232 gtk_tree_entry_get_collate_key(entry2
));
234 g_object_unref (entry
);
235 g_object_unref (entry2
);
241 //-----------------------------------------------------------------------------
242 // Searching callback (TRUE == not equal, FALSE == equal)
243 //-----------------------------------------------------------------------------
246 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
254 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
256 WXLISTBOX_DATACOLUMN_ARG(listbox
),
258 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
259 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
261 int ret
= strcasecmp(keycollatekey
,
262 gtk_tree_entry_get_collate_key(entry
));
264 g_object_unref (entry
);
270 //-----------------------------------------------------------------------------
272 //-----------------------------------------------------------------------------
274 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
276 // ----------------------------------------------------------------------------
278 // ----------------------------------------------------------------------------
280 void wxListBox::Init()
282 m_treeview
= (GtkTreeView
*) NULL
;
283 #if wxUSE_CHECKLISTBOX
284 m_hasCheckBoxes
= false;
285 #endif // wxUSE_CHECKLISTBOX
288 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
289 const wxPoint
&pos
, const wxSize
&size
,
290 const wxArrayString
& choices
,
291 long style
, const wxValidator
& validator
,
292 const wxString
&name
)
294 wxCArrayString
chs(choices
);
296 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
297 style
, validator
, name
);
300 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
301 const wxPoint
&pos
, const wxSize
&size
,
302 int n
, const wxString choices
[],
303 long style
, const wxValidator
& validator
,
304 const wxString
&name
)
306 m_blockEvent
= false;
308 if (!PreCreation( parent
, pos
, size
) ||
309 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
311 wxFAIL_MSG( wxT("wxListBox creation failed") );
315 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
316 if (style
& wxLB_ALWAYS_SB
)
318 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
319 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
323 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
324 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
328 GtkScrolledWindowSetBorder(m_widget
, style
);
330 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
332 //wxListBox doesn't have a header :)
333 //NB: If enabled SetFirstItem doesn't work correctly
334 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
336 #if wxUSE_CHECKLISTBOX
338 ((wxCheckListBox
*)this)->DoCreateCheckList();
339 #endif // wxUSE_CHECKLISTBOX
341 // Create the data column
342 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
343 gtk_cell_renderer_text_new(),
345 WXLISTBOX_DATACOLUMN
, NULL
);
347 // Now create+set the model (GtkListStore) - first argument # of columns
348 #if wxUSE_CHECKLISTBOX
350 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
351 GTK_TYPE_TREE_ENTRY
);
354 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
356 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
358 g_object_unref (m_liststore
); //free on treeview destruction
360 // Disable the pop-up textctrl that enables searching - note that
361 // the docs specify that even if this disabled (which we are doing)
362 // the user can still have it through the start-interactive-search
363 // key binding...either way we want to provide a searchequal callback
364 // NB: If this is enabled a doubleclick event (activate) gets sent
365 // on a successful search
366 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
367 gtk_tree_view_set_search_equal_func(m_treeview
,
368 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
372 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
375 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
377 g_signal_connect_after (selection
, "changed",
378 G_CALLBACK (gtk_listitem_changed_callback
), this);
380 GtkSelectionMode mode
;
381 if (style
& wxLB_MULTIPLE
)
383 mode
= GTK_SELECTION_MULTIPLE
;
385 else if (style
& wxLB_EXTENDED
)
387 mode
= GTK_SELECTION_EXTENDED
;
391 // if style was 0 set single mode
392 m_windowStyle
|= wxLB_SINGLE
;
393 mode
= GTK_SELECTION_SINGLE
;
396 gtk_tree_selection_set_mode( selection
, mode
);
398 // Handle sortable stuff
399 if(style
& wxLB_SORT
)
401 // Setup sorting in ascending (wx) order
402 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
403 WXLISTBOX_DATACOLUMN
,
406 // Set the sort callback
407 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
408 WXLISTBOX_DATACOLUMN
,
409 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
411 NULL
//"destroy notifier"
416 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
418 gtk_widget_show( GTK_WIDGET(m_treeview
) );
419 m_focusWidget
= GTK_WIDGET(m_treeview
);
421 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
423 // generate dclick events
424 g_signal_connect_after(m_treeview
, "row-activated",
425 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
427 m_parent
->DoAddChild( this );
430 SetInitialSize(size
); // need this too because this is a wxControlWithItems
435 wxListBox::~wxListBox()
442 // ----------------------------------------------------------------------------
444 // ----------------------------------------------------------------------------
446 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
447 void** clientData
, unsigned int pos
)
449 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
451 InvalidateBestSize();
453 // Create and set column ids and GValues
455 unsigned int nNum
= items
.GetCount();
456 unsigned int nCurCount
= wxListBox::GetCount();
457 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
459 GtkTreeIter
* pIter
= NULL
; // append by default
461 if (pos
!= nCurCount
)
463 wxCHECK_RET( GtkGetIteratorFor(pos
, &iter
),
464 wxT("internal wxListBox error in insertion") );
469 for (unsigned int i
= 0; i
< nNum
; ++i
)
471 wxString label
= items
[i
];
473 GtkTreeEntry
* entry
= gtk_tree_entry_new();
474 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
475 gtk_tree_entry_set_destroy_func(entry
,
476 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
480 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
483 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
485 GtkSetItem(itercur
, entry
);
487 g_object_unref (entry
);
491 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
493 wxCHECK_RET( IsValidInsert(pos
), wxT("invalid index in wxListBox::InsertItems") );
495 GtkInsertItems(items
, NULL
, pos
);
498 int wxListBox::DoAppend( const wxString
& item
)
500 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
502 InvalidateBestSize();
504 GtkTreeEntry
* entry
= gtk_tree_entry_new();
505 gtk_tree_entry_set_label( entry
, wxGTK_CONV(item
) );
506 gtk_tree_entry_set_destroy_func(entry
,
507 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
511 gtk_list_store_insert_before( m_liststore
, &itercur
, NULL
);
513 GtkSetItem(itercur
, entry
);
515 g_object_unref (entry
);
517 return GtkGetIndexFor(itercur
);
520 void wxListBox::DoSetItems( const wxArrayString
& items
,
524 GtkInsertItems(items
, clientData
, 0);
527 // ----------------------------------------------------------------------------
529 // ----------------------------------------------------------------------------
531 void wxListBox::Clear()
533 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
535 InvalidateBestSize();
537 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
540 void wxListBox::Delete(unsigned int n
)
542 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
544 InvalidateBestSize();
547 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
549 // this returns false if iter is invalid (e.g. deleting item at end) but
550 // since we don't use iter, we ignore the return value
551 gtk_list_store_remove(m_liststore
, &iter
);
554 // ----------------------------------------------------------------------------
555 // helper functions for working with iterators
556 // ----------------------------------------------------------------------------
558 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
560 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
563 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
570 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
573 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
575 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
577 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
579 int idx
= pIntPath
[0];
581 gtk_tree_path_free( path
);
586 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
587 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
590 if ( !GtkGetIteratorFor(n
, &iter
) )
594 GtkTreeEntry
* entry
= NULL
;
595 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
596 WXLISTBOX_DATACOLUMN
, &entry
, -1);
601 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
603 #if wxUSE_CHECKLISTBOX
604 if ( m_hasCheckBoxes
)
606 gtk_list_store_set(m_liststore
, &iter
,
607 0, FALSE
, // FALSE == not toggled
612 #endif // wxUSE_CHECKLISTBOX
614 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
618 // ----------------------------------------------------------------------------
620 // ----------------------------------------------------------------------------
622 void* wxListBox::DoGetItemClientData(unsigned int n
) const
624 wxCHECK_MSG( IsValid(n
), NULL
,
625 wxT("Invalid index passed to GetItemClientData") );
627 GtkTreeEntry
* entry
= GtkGetEntry(n
);
628 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
630 void* userdata
= gtk_tree_entry_get_userdata( entry
);
631 g_object_unref (entry
);
635 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
637 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
640 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
642 wxCHECK_RET( IsValid(n
),
643 wxT("Invalid index passed to SetItemClientData") );
645 GtkTreeEntry
* entry
= GtkGetEntry(n
);
646 wxCHECK_RET(entry
, wxT("could not get entry"));
648 gtk_tree_entry_set_userdata( entry
, clientData
);
649 g_object_unref (entry
);
652 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
654 // wxItemContainer already deletes data for us
655 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
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
), _T("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 int wxListBox::GetSelection() const
726 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
727 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
728 wxT("must be single selection listbox"));
731 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
733 // only works on single-sel
734 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
737 return GtkGetIndexFor(iter
);
740 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
742 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
748 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
750 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
751 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
754 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
758 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
761 return aSelections
.GetCount();
764 bool wxListBox::IsSelected( int n
) const
766 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
768 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
771 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
773 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
776 void wxListBox::DoSetSelection( int n
, bool select
)
778 // passing -1 to SetSelection() is documented to deselect all items
779 if ( n
== wxNOT_FOUND
)
781 // ... and not generate any events in the process
786 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
788 // don't generate the selection event
789 GtkSetSelection(n
, select
, true);
792 void wxListBox::GtkDeselectAll()
794 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
796 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
800 gtk_tree_selection_unselect_all(selection
);
802 m_blockEvent
= false;
805 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
807 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
809 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
812 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
814 m_blockEvent
= blockEvent
;
817 gtk_tree_selection_select_iter(selection
, &iter
);
819 gtk_tree_selection_unselect_iter(selection
, &iter
);
821 m_blockEvent
= false;
824 void wxListBox::DoSetFirstItem( int n
)
826 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
827 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
829 //RN: I have no idea why this line is needed...
830 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
834 if ( !GtkGetIteratorFor(n
, &iter
) )
837 GtkTreePath
* path
= gtk_tree_model_get_path(
838 GTK_TREE_MODEL(m_liststore
), &iter
);
840 // Scroll to the desired cell (0.0 == topleft alignment)
841 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
844 gtk_tree_path_free(path
);
847 // ----------------------------------------------------------------------------
849 // ----------------------------------------------------------------------------
851 int wxListBox::DoListHitTest(const wxPoint
& point
) const
853 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
854 // we only want visible items we need to check for it manually here
855 if ( !GetClientRect().Contains(point
) )
858 // need to translate from master window since it is in client coords
860 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
861 &binx
, &biny
, NULL
, NULL
, NULL
);
864 if ( !gtk_tree_view_get_path_at_pos
870 NULL
, // [out] column (always 0 here)
871 NULL
, // [out] x-coord relative to the cell (not interested)
872 NULL
// [out] y-coord relative to the cell
878 int index
= gtk_tree_path_get_indices(path
)[0];
879 gtk_tree_path_free(path
);
884 // ----------------------------------------------------------------------------
886 // ----------------------------------------------------------------------------
889 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
891 // RN: Is this needed anymore?
892 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, (gchar
*) NULL
);
894 #endif // wxUSE_TOOLTIPS
896 GtkWidget
*wxListBox::GetConnectWidget()
898 // the correct widget for listbox events (such as mouse clicks for example)
899 // is m_treeview, not the parent scrolled window
900 return GTK_WIDGET(m_treeview
);
903 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
905 return gtk_tree_view_get_bin_window(m_treeview
);
908 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
910 if (m_hasBgCol
&& m_backgroundColour
.Ok())
912 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
915 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
916 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
917 gdk_window_clear( window
);
921 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
924 wxSize
wxListBox::DoGetBestSize() const
926 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
928 // Start with a minimum size that's not too small
930 GetTextExtent( wxT("X"), &cx
, &cy
);
931 int lbWidth
= 3 * cx
;
934 // Get the visible area of the tree view (limit to the 10th item
935 // so that it isn't too big)
936 unsigned int count
= GetCount();
941 // Find the widest line
942 for(unsigned int i
= 0; i
< count
; i
++) {
943 wxString
str(GetString(i
));
944 GetTextExtent(str
, &wLine
, NULL
);
945 lbWidth
= wxMax(lbWidth
, wLine
);
950 // And just a bit more for the checkbox if present and then some
951 // (these are rough guesses)
952 #if wxUSE_CHECKLISTBOX
953 if ( m_hasCheckBoxes
)
956 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
960 // don't make the listbox too tall (limit height to around 10 items) but don't
961 // make it too small neither
962 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
965 // Add room for the scrollbar
966 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
968 wxSize
best(lbWidth
, lbHeight
);
975 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
977 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
980 #endif // wxUSE_LISTBOX