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"
37 #include <gdk/gdkkeysyms.h>
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
44 extern bool g_blockEventsOnScroll
;
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
52 #if wxUSE_CHECKLISTBOX
53 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
55 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
56 #endif // wxUSE_CHECKLISTBOX
58 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
66 gtk_listbox_row_activated_callback(GtkTreeView
*treeview
,
68 GtkTreeViewColumn
*col
,
71 if (g_isIdle
) wxapp_install_idle_handler();
73 if (g_blockEventsOnDrag
) return;
74 if (g_blockEventsOnScroll
) return;
76 // This is triggered by either a double-click or a space press
78 int sel
= gtk_tree_path_get_indices(path
)[0];
80 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
81 event
.SetEventObject( listbox
);
83 if (listbox
->IsSelected(sel
))
85 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(sel
);
90 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
92 if ( listbox
->HasClientObjectData() )
93 event
.SetClientObject( (wxClientData
*) gtk_tree_entry_get_userdata(entry
) );
94 else if ( listbox
->HasClientUntypedData() )
95 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
97 g_object_unref (entry
);
101 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
110 listbox
->GetEventHandler()->ProcessEvent( event
);
114 //-----------------------------------------------------------------------------
116 //-----------------------------------------------------------------------------
120 gtk_listitem_changed_callback( GtkTreeSelection
* selection
, wxListBox
*listbox
)
122 if (g_blockEventsOnDrag
) return;
124 if (listbox
->m_blockEvent
) return;
126 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
127 event
.SetEventObject( listbox
);
129 if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
))
131 wxArrayInt selections
;
132 listbox
->GetSelections( selections
);
134 if (selections
.GetCount() == 0)
136 // indicate that this is a deselection
137 event
.SetExtraLong( 0 );
140 listbox
->GetEventHandler()->ProcessEvent( event
);
146 // indicate that this is a selection
147 event
.SetExtraLong( 1 );
148 event
.SetInt( selections
[0] );
150 listbox
->GetEventHandler()->ProcessEvent( event
);
155 int index
= listbox
->GetSelection();
156 if (index
== wxNOT_FOUND
)
158 // indicate that this is a deselection
159 event
.SetExtraLong( 0 );
162 listbox
->GetEventHandler()->ProcessEvent( event
);
168 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
170 // indicate that this is a selection
171 event
.SetExtraLong( 1 );
173 event
.SetInt( index
);
174 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
176 if ( listbox
->HasClientObjectData() )
177 event
.SetClientObject(
178 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
180 else if ( listbox
->HasClientUntypedData() )
181 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
183 listbox
->GetEventHandler()->ProcessEvent( event
);
185 g_object_unref (entry
);
191 //-----------------------------------------------------------------------------
192 // GtkTreeEntry destruction (to destroy client data)
193 //-----------------------------------------------------------------------------
196 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
199 if (listbox
->HasClientObjectData())
201 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
203 delete (wxClientData
*)userdata
;
208 //-----------------------------------------------------------------------------
209 // Sorting callback (standard CmpNoCase return value)
210 //-----------------------------------------------------------------------------
213 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
219 GtkTreeEntry
* entry2
;
221 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
223 WXLISTBOX_DATACOLUMN_ARG(listbox
),
225 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
227 WXLISTBOX_DATACOLUMN_ARG(listbox
),
229 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
230 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
232 //We compare collate keys here instead of calling g_utf8_collate
233 //as it is rather slow (and even the docs reccommend this)
234 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
235 gtk_tree_entry_get_collate_key(entry2
));
237 g_object_unref (entry
);
238 g_object_unref (entry2
);
244 //-----------------------------------------------------------------------------
245 // Searching callback (TRUE == not equal, FALSE == equal)
246 //-----------------------------------------------------------------------------
249 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
257 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
259 WXLISTBOX_DATACOLUMN_ARG(listbox
),
261 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
262 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
264 int ret
= strcasecmp(keycollatekey
,
265 gtk_tree_entry_get_collate_key(entry
));
267 g_object_unref (entry
);
273 //-----------------------------------------------------------------------------
275 //-----------------------------------------------------------------------------
277 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
279 // ----------------------------------------------------------------------------
281 // ----------------------------------------------------------------------------
283 void wxListBox::Init()
285 m_treeview
= (GtkTreeView
*) NULL
;
286 #if wxUSE_CHECKLISTBOX
287 m_hasCheckBoxes
= false;
288 #endif // wxUSE_CHECKLISTBOX
291 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
292 const wxPoint
&pos
, const wxSize
&size
,
293 const wxArrayString
& choices
,
294 long style
, const wxValidator
& validator
,
295 const wxString
&name
)
297 wxCArrayString
chs(choices
);
299 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
300 style
, validator
, name
);
303 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
304 const wxPoint
&pos
, const wxSize
&size
,
305 int n
, const wxString choices
[],
306 long style
, const wxValidator
& validator
,
307 const wxString
&name
)
310 m_blockEvent
= false;
312 if (!PreCreation( parent
, pos
, size
) ||
313 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
315 wxFAIL_MSG( wxT("wxListBox creation failed") );
319 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
320 if (style
& wxLB_ALWAYS_SB
)
322 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
323 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
327 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
328 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
332 GtkScrolledWindowSetBorder(m_widget
, style
);
334 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
336 //wxListBox doesn't have a header :)
337 //NB: If enabled SetFirstItem doesn't work correctly
338 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
340 #if wxUSE_CHECKLISTBOX
342 ((wxCheckListBox
*)this)->DoCreateCheckList();
343 #endif // wxUSE_CHECKLISTBOX
345 // Create the data column
346 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
347 gtk_cell_renderer_text_new(),
349 WXLISTBOX_DATACOLUMN
, NULL
);
351 // Now create+set the model (GtkListStore) - first argument # of columns
352 #if wxUSE_CHECKLISTBOX
354 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
355 GTK_TYPE_TREE_ENTRY
);
358 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
360 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
362 g_object_unref (m_liststore
); //free on treeview destruction
364 // Disable the pop-up textctrl that enables searching - note that
365 // the docs specify that even if this disabled (which we are doing)
366 // the user can still have it through the start-interactive-search
367 // key binding...either way we want to provide a searchequal callback
368 // NB: If this is enabled a doubleclick event (activate) gets sent
369 // on a successful search
370 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
371 gtk_tree_view_set_search_equal_func(m_treeview
,
372 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
376 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
379 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
381 g_signal_connect_after (selection
, "changed",
382 G_CALLBACK (gtk_listitem_changed_callback
), this);
384 GtkSelectionMode mode
;
385 if (style
& wxLB_MULTIPLE
)
387 mode
= GTK_SELECTION_MULTIPLE
;
389 else if (style
& wxLB_EXTENDED
)
391 mode
= GTK_SELECTION_EXTENDED
;
395 // if style was 0 set single mode
396 m_windowStyle
|= wxLB_SINGLE
;
397 mode
= GTK_SELECTION_SINGLE
;
400 gtk_tree_selection_set_mode( selection
, mode
);
402 // Handle sortable stuff
403 if(style
& wxLB_SORT
)
405 // Setup sorting in ascending (wx) order
406 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
407 WXLISTBOX_DATACOLUMN
,
410 // Set the sort callback
411 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
412 WXLISTBOX_DATACOLUMN
,
413 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
415 NULL
//"destroy notifier"
420 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
422 gtk_widget_show( GTK_WIDGET(m_treeview
) );
423 m_focusWidget
= GTK_WIDGET(m_treeview
);
425 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
427 // generate dclick events
428 g_signal_connect_after(m_treeview
, "row-activated",
429 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
431 m_parent
->DoAddChild( this );
434 SetInitialSize(size
); // need this too because this is a wxControlWithItems
439 wxListBox::~wxListBox()
446 // ----------------------------------------------------------------------------
448 // ----------------------------------------------------------------------------
450 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
451 void** clientData
, unsigned int pos
)
453 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
455 InvalidateBestSize();
457 // Create and set column ids and GValues
459 unsigned int nNum
= items
.GetCount();
460 unsigned int nCurCount
= wxListBox::GetCount();
461 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
463 GtkTreeIter
* pIter
= NULL
; // append by default
465 if (pos
!= nCurCount
)
467 wxCHECK_RET( GtkGetIteratorFor(pos
, &iter
),
468 wxT("internal wxListBox error in insertion") );
473 for (unsigned int i
= 0; i
< nNum
; ++i
)
475 wxString label
= items
[i
];
477 GtkTreeEntry
* entry
= gtk_tree_entry_new();
478 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
479 gtk_tree_entry_set_destroy_func(entry
,
480 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
484 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
487 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
489 GtkSetItem(itercur
, entry
);
491 g_object_unref (entry
);
495 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
497 wxCHECK_RET( IsValidInsert(pos
), wxT("invalid index in wxListBox::InsertItems") );
499 GtkInsertItems(items
, NULL
, pos
);
502 int wxListBox::DoAppend( const wxString
& item
)
504 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
506 InvalidateBestSize();
508 GtkTreeEntry
* entry
= gtk_tree_entry_new();
509 gtk_tree_entry_set_label( entry
, wxGTK_CONV(item
) );
510 gtk_tree_entry_set_destroy_func(entry
,
511 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
515 gtk_list_store_insert_before( m_liststore
, &itercur
, NULL
);
517 GtkSetItem(itercur
, entry
);
519 g_object_unref (entry
);
521 return GtkGetIndexFor(itercur
);
524 void wxListBox::DoSetItems( const wxArrayString
& items
,
528 GtkInsertItems(items
, clientData
, 0);
531 // ----------------------------------------------------------------------------
533 // ----------------------------------------------------------------------------
535 void wxListBox::Clear()
537 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
539 InvalidateBestSize();
541 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
544 void wxListBox::Delete(unsigned int n
)
546 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
548 InvalidateBestSize();
551 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
553 // this returns false if iter is invalid (e.g. deleting item at end) but
554 // since we don't use iter, we ignore the return value
555 gtk_list_store_remove(m_liststore
, &iter
);
558 // ----------------------------------------------------------------------------
559 // helper functions for working with iterators
560 // ----------------------------------------------------------------------------
562 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
564 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
567 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
574 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
577 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
579 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
581 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
583 int idx
= pIntPath
[0];
585 gtk_tree_path_free( path
);
590 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
591 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
594 if ( !GtkGetIteratorFor(n
, &iter
) )
598 GtkTreeEntry
* entry
= NULL
;
599 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
600 WXLISTBOX_DATACOLUMN
, &entry
, -1);
605 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
607 #if wxUSE_CHECKLISTBOX
608 if ( m_hasCheckBoxes
)
610 gtk_list_store_set(m_liststore
, &iter
,
611 0, FALSE
, // FALSE == not toggled
616 #endif // wxUSE_CHECKLISTBOX
618 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
622 // ----------------------------------------------------------------------------
624 // ----------------------------------------------------------------------------
626 void* wxListBox::DoGetItemClientData(unsigned int n
) const
628 wxCHECK_MSG( IsValid(n
), NULL
,
629 wxT("Invalid index passed to GetItemClientData") );
631 GtkTreeEntry
* entry
= GtkGetEntry(n
);
632 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
634 void* userdata
= gtk_tree_entry_get_userdata( entry
);
635 g_object_unref (entry
);
639 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
641 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
644 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
646 wxCHECK_RET( IsValid(n
),
647 wxT("Invalid index passed to SetItemClientData") );
649 GtkTreeEntry
* entry
= GtkGetEntry(n
);
650 wxCHECK_RET(entry
, wxT("could not get entry"));
652 gtk_tree_entry_set_userdata( entry
, clientData
);
653 g_object_unref (entry
);
656 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
658 // wxItemContainer already deletes data for us
659 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
662 // ----------------------------------------------------------------------------
663 // string list access
664 // ----------------------------------------------------------------------------
666 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
668 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
669 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
671 GtkTreeEntry
* entry
= GtkGetEntry(n
);
672 wxCHECK_RET( entry
, wxT("wrong listbox index") );
674 // update the item itself
675 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
677 // and update the model which will refresh the tree too
679 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
681 // FIXME: this resets the checked status of a wxCheckListBox item
683 GtkSetItem(iter
, entry
);
686 wxString
wxListBox::GetString(unsigned int n
) const
688 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
690 GtkTreeEntry
* entry
= GtkGetEntry(n
);
691 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
693 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
695 g_object_unref (entry
);
699 unsigned int wxListBox::GetCount() const
701 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
703 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
706 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
708 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
710 //Sort of hackish - maybe there is a faster way
711 unsigned int nCount
= wxListBox::GetCount();
713 for(unsigned int i
= 0; i
< nCount
; ++i
)
715 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
720 // it's not an error if the string is not found -> no wxCHECK
724 // ----------------------------------------------------------------------------
726 // ----------------------------------------------------------------------------
728 int wxListBox::GetSelection() const
730 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
731 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
732 wxT("must be single selection listbox"));
735 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
737 // only works on single-sel
738 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
741 return GtkGetIndexFor(iter
);
744 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
746 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
752 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
754 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
755 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
758 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
762 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
765 return aSelections
.GetCount();
768 bool wxListBox::IsSelected( int n
) const
770 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
772 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
775 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
777 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
780 void wxListBox::DoSetSelection( int n
, bool select
)
782 // passing -1 to SetSelection() is documented to deselect all items
783 if ( n
== wxNOT_FOUND
)
785 // ... and not generate any events in the process
790 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
792 // don't generate the selection event
793 GtkSetSelection(n
, select
, true);
796 void wxListBox::GtkDeselectAll()
798 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
800 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
804 gtk_tree_selection_unselect_all(selection
);
806 m_blockEvent
= false;
809 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
811 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
813 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
816 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
818 m_blockEvent
= blockEvent
;
821 gtk_tree_selection_select_iter(selection
, &iter
);
823 gtk_tree_selection_unselect_iter(selection
, &iter
);
825 m_blockEvent
= false;
828 void wxListBox::DoSetFirstItem( int n
)
830 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
831 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
833 //RN: I have no idea why this line is needed...
834 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
838 if ( !GtkGetIteratorFor(n
, &iter
) )
841 GtkTreePath
* path
= gtk_tree_model_get_path(
842 GTK_TREE_MODEL(m_liststore
), &iter
);
844 // Scroll to the desired cell (0.0 == topleft alignment)
845 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
848 gtk_tree_path_free(path
);
851 // ----------------------------------------------------------------------------
853 // ----------------------------------------------------------------------------
855 int wxListBox::DoListHitTest(const wxPoint
& point
) const
857 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
858 // we only want visible items we need to check for it manually here
859 if ( !GetClientRect().Contains(point
) )
862 // need to translate from master window since it is in client coords
864 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
865 &binx
, &biny
, NULL
, NULL
, NULL
);
868 if ( !gtk_tree_view_get_path_at_pos
874 NULL
, // [out] column (always 0 here)
875 NULL
, // [out] x-coord relative to the cell (not interested)
876 NULL
// [out] y-coord relative to the cell
882 int index
= gtk_tree_path_get_indices(path
)[0];
883 gtk_tree_path_free(path
);
888 // ----------------------------------------------------------------------------
890 // ----------------------------------------------------------------------------
893 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
895 // RN: Is this needed anymore?
896 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
898 #endif // wxUSE_TOOLTIPS
900 GtkWidget
*wxListBox::GetConnectWidget()
902 // the correct widget for listbox events (such as mouse clicks for example)
903 // is m_treeview, not the parent scrolled window
904 return GTK_WIDGET(m_treeview
);
907 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
909 return gtk_tree_view_get_bin_window(m_treeview
);
912 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
914 if (m_hasBgCol
&& m_backgroundColour
.Ok())
916 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
919 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
920 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
921 gdk_window_clear( window
);
925 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
928 wxSize
wxListBox::DoGetBestSize() const
930 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
932 // Start with a minimum size that's not too small
934 GetTextExtent( wxT("X"), &cx
, &cy
);
935 int lbWidth
= 3 * cx
;
938 // Get the visible area of the tree view (limit to the 10th item
939 // so that it isn't too big)
940 unsigned int count
= GetCount();
945 // Find the widest line
946 for(unsigned int i
= 0; i
< count
; i
++) {
947 wxString
str(GetString(i
));
948 GetTextExtent(str
, &wLine
, NULL
);
949 lbWidth
= wxMax(lbWidth
, wLine
);
954 // And just a bit more for the checkbox if present and then some
955 // (these are rough guesses)
956 #if wxUSE_CHECKLISTBOX
957 if ( m_hasCheckBoxes
)
960 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
964 // don't make the listbox too tall (limit height to around 10 items) but don't
965 // make it too small neither
966 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
969 // Add room for the scrollbar
970 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
972 wxSize
best(lbWidth
, lbHeight
);
979 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
981 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
984 #endif // wxUSE_LISTBOX