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
->GetEventHandler()->ProcessEvent( 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
->GetEventHandler()->ProcessEvent( event
);
144 // indicate that this is a selection
145 event
.SetExtraLong( 1 );
146 event
.SetInt( selections
[0] );
148 listbox
->GetEventHandler()->ProcessEvent( event
);
153 int index
= listbox
->GetSelection();
154 if (index
== wxNOT_FOUND
)
156 // indicate that this is a deselection
157 event
.SetExtraLong( 0 );
160 listbox
->GetEventHandler()->ProcessEvent( 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
->GetEventHandler()->ProcessEvent( event
);
183 g_object_unref (entry
);
189 //-----------------------------------------------------------------------------
190 // GtkTreeEntry destruction (to destroy client data)
191 //-----------------------------------------------------------------------------
194 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
197 if (listbox
->HasClientObjectData())
199 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
201 delete (wxClientData
*)userdata
;
206 //-----------------------------------------------------------------------------
207 // Sorting callback (standard CmpNoCase return value)
208 //-----------------------------------------------------------------------------
212 static gint
gtk_listbox_sort_callback(GtkTreeModel
* WXUNUSED(model
),
218 GtkTreeEntry
* entry2
;
220 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
222 WXLISTBOX_DATACOLUMN_ARG(listbox
),
224 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
226 WXLISTBOX_DATACOLUMN_ARG(listbox
),
228 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
229 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
231 //We compare collate keys here instead of calling g_utf8_collate
232 //as it is rather slow (and even the docs reccommend this)
233 int ret
= strcmp(gtk_tree_entry_get_collate_key(entry
),
234 gtk_tree_entry_get_collate_key(entry2
));
236 g_object_unref (entry
);
237 g_object_unref (entry2
);
243 //-----------------------------------------------------------------------------
244 // Searching callback (TRUE == not equal, FALSE == equal)
245 //-----------------------------------------------------------------------------
248 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* WXUNUSED(model
),
249 gint
WXUNUSED(column
),
256 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
258 WXLISTBOX_DATACOLUMN_ARG(listbox
),
260 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
261 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
263 int ret
= strcmp(keycollatekey
,
264 gtk_tree_entry_get_collate_key(entry
));
266 g_object_unref (entry
);
272 //-----------------------------------------------------------------------------
274 //-----------------------------------------------------------------------------
276 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControlWithItems
)
278 // ----------------------------------------------------------------------------
280 // ----------------------------------------------------------------------------
282 void wxListBox::Init()
284 m_treeview
= (GtkTreeView
*) NULL
;
285 #if wxUSE_CHECKLISTBOX
286 m_hasCheckBoxes
= false;
287 #endif // wxUSE_CHECKLISTBOX
290 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
291 const wxPoint
&pos
, const wxSize
&size
,
292 const wxArrayString
& choices
,
293 long style
, const wxValidator
& validator
,
294 const wxString
&name
)
296 wxCArrayString
chs(choices
);
298 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
299 style
, validator
, name
);
302 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
303 const wxPoint
&pos
, const wxSize
&size
,
304 int n
, const wxString choices
[],
305 long style
, const wxValidator
& validator
,
306 const wxString
&name
)
308 m_blockEvent
= false;
310 if (!PreCreation( parent
, pos
, size
) ||
311 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
313 wxFAIL_MSG( wxT("wxListBox creation failed") );
317 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
318 if (style
& wxLB_ALWAYS_SB
)
320 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
321 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
325 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
326 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
330 GtkScrolledWindowSetBorder(m_widget
, style
);
332 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
334 //wxListBox doesn't have a header :)
335 //NB: If enabled SetFirstItem doesn't work correctly
336 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
338 #if wxUSE_CHECKLISTBOX
340 ((wxCheckListBox
*)this)->DoCreateCheckList();
341 #endif // wxUSE_CHECKLISTBOX
343 // Create the data column
344 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
345 gtk_cell_renderer_text_new(),
347 WXLISTBOX_DATACOLUMN
, NULL
);
349 // Now create+set the model (GtkListStore) - first argument # of columns
350 #if wxUSE_CHECKLISTBOX
352 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
353 GTK_TYPE_TREE_ENTRY
);
356 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
358 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
360 g_object_unref (m_liststore
); //free on treeview destruction
362 // Disable the pop-up textctrl that enables searching - note that
363 // the docs specify that even if this disabled (which we are doing)
364 // the user can still have it through the start-interactive-search
365 // key binding...either way we want to provide a searchequal callback
366 // NB: If this is enabled a doubleclick event (activate) gets sent
367 // on a successful search
368 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
369 gtk_tree_view_set_search_equal_func(m_treeview
,
370 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
374 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
377 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
379 g_signal_connect_after (selection
, "changed",
380 G_CALLBACK (gtk_listitem_changed_callback
), this);
382 GtkSelectionMode mode
;
383 if (style
& wxLB_MULTIPLE
)
385 mode
= GTK_SELECTION_MULTIPLE
;
387 else if (style
& wxLB_EXTENDED
)
389 mode
= GTK_SELECTION_EXTENDED
;
393 // if style was 0 set single mode
394 m_windowStyle
|= wxLB_SINGLE
;
395 mode
= GTK_SELECTION_SINGLE
;
398 gtk_tree_selection_set_mode( selection
, mode
);
400 // Handle sortable stuff
401 if(HasFlag(wxLB_SORT
))
403 // Setup sorting in ascending (wx) order
404 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
405 WXLISTBOX_DATACOLUMN
,
408 // Set the sort callback
409 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
410 WXLISTBOX_DATACOLUMN
,
411 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
413 NULL
//"destroy notifier"
418 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
420 gtk_widget_show( GTK_WIDGET(m_treeview
) );
421 m_focusWidget
= GTK_WIDGET(m_treeview
);
423 Append(n
, choices
); // insert initial items
425 // generate dclick events
426 g_signal_connect_after(m_treeview
, "row-activated",
427 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
429 m_parent
->DoAddChild( this );
432 SetInitialSize(size
); // need this too because this is a wxControlWithItems
437 wxListBox::~wxListBox()
444 // ----------------------------------------------------------------------------
446 // ----------------------------------------------------------------------------
448 int wxListBox::DoInsertItems(const wxArrayStringsAdapter
& items
,
451 wxClientDataType
WXUNUSED(type
))
453 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
455 InvalidateBestSize();
457 GtkTreeIter
* pIter
= NULL
; // append by default
459 if ( pos
!= GetCount() )
461 wxCHECK_MSG( GtkGetIteratorFor(pos
, &iter
), wxNOT_FOUND
,
462 wxT("internal wxListBox error in insertion") );
467 const unsigned int numItems
= items
.GetCount();
468 for ( unsigned int i
= 0; i
< numItems
; ++i
)
470 GtkTreeEntry
* entry
= gtk_tree_entry_new();
471 gtk_tree_entry_set_label(entry
, wxGTK_CONV(items
[i
]));
472 gtk_tree_entry_set_destroy_func(entry
,
473 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
477 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
480 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
482 GtkSetItem(itercur
, entry
);
484 g_object_unref (entry
);
487 return pos
+ numItems
- 1;
490 // ----------------------------------------------------------------------------
492 // ----------------------------------------------------------------------------
494 void wxListBox::DoClear()
496 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
498 InvalidateBestSize();
500 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
503 void wxListBox::DoDeleteOneItem(unsigned int n
)
505 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
507 InvalidateBestSize();
510 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("wrong listbox index") );
512 // this returns false if iter is invalid (e.g. deleting item at end) but
513 // since we don't use iter, we ignore the return value
514 gtk_list_store_remove(m_liststore
, &iter
);
517 // ----------------------------------------------------------------------------
518 // helper functions for working with iterators
519 // ----------------------------------------------------------------------------
521 bool wxListBox::GtkGetIteratorFor(unsigned pos
, GtkTreeIter
*iter
) const
523 if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore
),
526 wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos
);
533 int wxListBox::GtkGetIndexFor(GtkTreeIter
& iter
) const
536 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
538 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
540 wxCHECK_MSG( pIntPath
, wxNOT_FOUND
, _T("failed to get iterator path") );
542 int idx
= pIntPath
[0];
544 gtk_tree_path_free( path
);
549 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
550 GtkTreeEntry
*wxListBox::GtkGetEntry(unsigned n
) const
553 if ( !GtkGetIteratorFor(n
, &iter
) )
557 GtkTreeEntry
* entry
= NULL
;
558 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
559 WXLISTBOX_DATACOLUMN
, &entry
, -1);
564 void wxListBox::GtkSetItem(GtkTreeIter
& iter
, const GtkTreeEntry
*entry
)
566 #if wxUSE_CHECKLISTBOX
567 if ( m_hasCheckBoxes
)
569 gtk_list_store_set(m_liststore
, &iter
,
570 0, FALSE
, // FALSE == not toggled
575 #endif // wxUSE_CHECKLISTBOX
577 gtk_list_store_set(m_liststore
, &iter
, 0, entry
, -1);
581 // ----------------------------------------------------------------------------
583 // ----------------------------------------------------------------------------
585 void* wxListBox::DoGetItemClientData(unsigned int n
) const
587 wxCHECK_MSG( IsValid(n
), NULL
,
588 wxT("Invalid index passed to GetItemClientData") );
590 GtkTreeEntry
* entry
= GtkGetEntry(n
);
591 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
593 void* userdata
= gtk_tree_entry_get_userdata( entry
);
594 g_object_unref (entry
);
598 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
600 wxCHECK_RET( IsValid(n
),
601 wxT("Invalid index passed to SetItemClientData") );
603 GtkTreeEntry
* entry
= GtkGetEntry(n
);
604 wxCHECK_RET(entry
, wxT("could not get entry"));
606 gtk_tree_entry_set_userdata( entry
, clientData
);
607 g_object_unref (entry
);
610 // ----------------------------------------------------------------------------
611 // string list access
612 // ----------------------------------------------------------------------------
614 void wxListBox::SetString(unsigned int n
, const wxString
& label
)
616 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
617 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
619 GtkTreeEntry
* entry
= GtkGetEntry(n
);
620 wxCHECK_RET( entry
, wxT("wrong listbox index") );
622 // update the item itself
623 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
625 // and update the model which will refresh the tree too
627 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), _T("failed to get iterator") );
629 // FIXME: this resets the checked status of a wxCheckListBox item
631 GtkSetItem(iter
, entry
);
634 wxString
wxListBox::GetString(unsigned int n
) const
636 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
638 GtkTreeEntry
* entry
= GtkGetEntry(n
);
639 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
641 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
643 g_object_unref (entry
);
647 unsigned int wxListBox::GetCount() const
649 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
651 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
654 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
656 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
658 //Sort of hackish - maybe there is a faster way
659 unsigned int nCount
= wxListBox::GetCount();
661 for(unsigned int i
= 0; i
< nCount
; ++i
)
663 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
668 // it's not an error if the string is not found -> no wxCHECK
672 // ----------------------------------------------------------------------------
674 // ----------------------------------------------------------------------------
676 int wxListBox::GetSelection() const
678 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
679 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
680 wxT("must be single selection listbox"));
683 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
685 // only works on single-sel
686 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
689 return GtkGetIndexFor(iter
);
692 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
694 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
700 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
702 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
703 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
706 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
710 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
713 return aSelections
.GetCount();
716 bool wxListBox::IsSelected( int n
) const
718 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
720 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
723 wxCHECK_MSG( GtkGetIteratorFor(n
, &iter
), false, wxT("Invalid index") );
725 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
728 void wxListBox::DoSetSelection( int n
, bool select
)
730 // passing -1 to SetSelection() is documented to deselect all items
731 if ( n
== wxNOT_FOUND
)
733 // ... and not generate any events in the process
738 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
740 // don't generate the selection event
741 GtkSetSelection(n
, select
, true);
744 void wxListBox::GtkDeselectAll()
746 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
748 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
752 gtk_tree_selection_unselect_all(selection
);
754 m_blockEvent
= false;
757 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
759 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
761 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
764 wxCHECK_RET( GtkGetIteratorFor(n
, &iter
), wxT("Invalid index") );
766 m_blockEvent
= blockEvent
;
769 gtk_tree_selection_select_iter(selection
, &iter
);
771 gtk_tree_selection_unselect_iter(selection
, &iter
);
773 GtkTreePath
* path
= gtk_tree_model_get_path(
774 GTK_TREE_MODEL(m_liststore
), &iter
);
776 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
, FALSE
, 0.0f
, 0.0f
);
778 gtk_tree_path_free(path
);
780 m_blockEvent
= false;
783 void wxListBox::DoSetFirstItem( int n
)
785 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
786 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
788 //RN: I have no idea why this line is needed...
789 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
793 if ( !GtkGetIteratorFor(n
, &iter
) )
796 GtkTreePath
* path
= gtk_tree_model_get_path(
797 GTK_TREE_MODEL(m_liststore
), &iter
);
799 // Scroll to the desired cell (0.0 == topleft alignment)
800 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
803 gtk_tree_path_free(path
);
806 // ----------------------------------------------------------------------------
808 // ----------------------------------------------------------------------------
810 int wxListBox::DoListHitTest(const wxPoint
& point
) const
812 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
813 // we only want visible items we need to check for it manually here
814 if ( !GetClientRect().Contains(point
) )
817 // need to translate from master window since it is in client coords
819 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
820 &binx
, &biny
, NULL
, NULL
, NULL
);
823 if ( !gtk_tree_view_get_path_at_pos
829 NULL
, // [out] column (always 0 here)
830 NULL
, // [out] x-coord relative to the cell (not interested)
831 NULL
// [out] y-coord relative to the cell
837 int index
= gtk_tree_path_get_indices(path
)[0];
838 gtk_tree_path_free(path
);
843 // ----------------------------------------------------------------------------
845 // ----------------------------------------------------------------------------
848 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const gchar
*tip
)
850 // RN: Is this needed anymore?
851 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), tip
, (gchar
*) NULL
);
853 #endif // wxUSE_TOOLTIPS
855 GtkWidget
*wxListBox::GetConnectWidget()
857 // the correct widget for listbox events (such as mouse clicks for example)
858 // is m_treeview, not the parent scrolled window
859 return GTK_WIDGET(m_treeview
);
862 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
864 return gtk_tree_view_get_bin_window(m_treeview
);
867 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
869 if (m_hasBgCol
&& m_backgroundColour
.Ok())
871 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
874 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
875 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
876 gdk_window_clear( window
);
880 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
883 wxSize
wxListBox::DoGetBestSize() const
885 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
887 // Start with a minimum size that's not too small
889 GetTextExtent( wxT("X"), &cx
, &cy
);
890 int lbWidth
= 3 * cx
;
893 // Get the visible area of the tree view (limit to the 10th item
894 // so that it isn't too big)
895 unsigned int count
= GetCount();
900 // Find the widest line
901 for(unsigned int i
= 0; i
< count
; i
++) {
902 wxString
str(GetString(i
));
903 GetTextExtent(str
, &wLine
, NULL
);
904 lbWidth
= wxMax(lbWidth
, wLine
);
909 // And just a bit more for the checkbox if present and then some
910 // (these are rough guesses)
911 #if wxUSE_CHECKLISTBOX
912 if ( m_hasCheckBoxes
)
915 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
919 // don't make the listbox too tall (limit height to around 10 items) but don't
920 // make it too small neither
921 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
924 // Add room for the scrollbar
925 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
927 wxSize
best(lbWidth
, lbHeight
);
934 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
936 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
939 #endif // wxUSE_LISTBOX