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_listbox_key_press_callback( GtkWidget
*widget
,
121 GdkEventKey
*gdk_event
,
124 if (g_blockEventsOnDrag
) return FALSE
;
126 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
128 wxNavigationKeyEvent new_event
;
129 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
130 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
131 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
132 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
133 new_event
.SetCurrentFocus( listbox
);
134 if (listbox
->GetEventHandler()->ProcessEvent( new_event
))
142 //-----------------------------------------------------------------------------
144 //-----------------------------------------------------------------------------
148 gtk_listitem_changed_callback( GtkTreeSelection
* selection
, wxListBox
*listbox
)
150 if (g_blockEventsOnDrag
) return;
152 if (listbox
->m_blockEvent
) return;
154 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
155 event
.SetEventObject( listbox
);
157 if (listbox
->HasFlag(wxLB_MULTIPLE
) || listbox
->HasFlag(wxLB_EXTENDED
))
159 wxArrayInt selections
;
160 listbox
->GetSelections( selections
);
162 if (selections
.GetCount() == 0)
164 // indicate that this is a deselection
165 event
.SetExtraLong( 0 );
168 listbox
->GetEventHandler()->ProcessEvent( event
);
174 // indicate that this is a selection
175 event
.SetExtraLong( 1 );
176 event
.SetInt( selections
[0] );
178 listbox
->GetEventHandler()->ProcessEvent( event
);
183 int index
= listbox
->GetSelection();
184 if (index
== wxNOT_FOUND
)
186 // indicate that this is a deselection
187 event
.SetExtraLong( 0 );
190 listbox
->GetEventHandler()->ProcessEvent( event
);
196 GtkTreeEntry
* entry
= listbox
->GtkGetEntry( index
);
198 // indicate that this is a selection
199 event
.SetExtraLong( 1 );
201 event
.SetInt( index
);
202 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
204 if ( listbox
->HasClientObjectData() )
205 event
.SetClientObject(
206 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
208 else if ( listbox
->HasClientUntypedData() )
209 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
211 listbox
->GetEventHandler()->ProcessEvent( event
);
213 g_object_unref (entry
);
219 //-----------------------------------------------------------------------------
220 // GtkTreeEntry destruction (to destroy client data)
221 //-----------------------------------------------------------------------------
224 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
227 if (listbox
->HasClientObjectData())
229 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
231 delete (wxClientData
*)userdata
;
236 //-----------------------------------------------------------------------------
237 // Sorting callback (standard CmpNoCase return value)
238 //-----------------------------------------------------------------------------
241 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
247 GtkTreeEntry
* entry2
;
249 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
251 WXLISTBOX_DATACOLUMN_ARG(listbox
),
253 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
255 WXLISTBOX_DATACOLUMN_ARG(listbox
),
257 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
258 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
260 //We compare collate keys here instead of calling g_utf8_collate
261 //as it is rather slow (and even the docs reccommend this)
262 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
263 gtk_tree_entry_get_collate_key(entry2
));
265 g_object_unref (entry
);
266 g_object_unref (entry2
);
272 //-----------------------------------------------------------------------------
273 // Searching callback (TRUE == not equal, FALSE == equal)
274 //-----------------------------------------------------------------------------
277 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
285 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
287 WXLISTBOX_DATACOLUMN_ARG(listbox
),
289 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
290 wxGtkString
keycollatekey(g_utf8_collate_key(key
, -1));
292 int ret
= strcasecmp(keycollatekey
,
293 gtk_tree_entry_get_collate_key(entry
));
295 g_object_unref (entry
);
301 //-----------------------------------------------------------------------------
303 //-----------------------------------------------------------------------------
305 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 void wxListBox::Init()
313 m_treeview
= (GtkTreeView
*) NULL
;
314 #if wxUSE_CHECKLISTBOX
315 m_hasCheckBoxes
= false;
316 #endif // wxUSE_CHECKLISTBOX
319 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
320 const wxPoint
&pos
, const wxSize
&size
,
321 const wxArrayString
& choices
,
322 long style
, const wxValidator
& validator
,
323 const wxString
&name
)
325 wxCArrayString
chs(choices
);
327 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
328 style
, validator
, name
);
331 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
332 const wxPoint
&pos
, const wxSize
&size
,
333 int n
, const wxString choices
[],
334 long style
, const wxValidator
& validator
,
335 const wxString
&name
)
338 m_acceptsFocus
= true;
339 m_blockEvent
= false;
341 if (!PreCreation( parent
, pos
, size
) ||
342 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
344 wxFAIL_MSG( wxT("wxListBox creation failed") );
348 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
349 if (style
& wxLB_ALWAYS_SB
)
351 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
352 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
356 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
357 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
361 GtkScrolledWindowSetBorder(m_widget
, style
);
363 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
365 //wxListBox doesn't have a header :)
366 //NB: If enabled SetFirstItem doesn't work correctly
367 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
369 #if wxUSE_CHECKLISTBOX
371 ((wxCheckListBox
*)this)->DoCreateCheckList();
372 #endif // wxUSE_CHECKLISTBOX
374 // Create the data column
375 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
376 gtk_cell_renderer_text_new(),
378 WXLISTBOX_DATACOLUMN
, NULL
);
380 // Now create+set the model (GtkListStore) - first argument # of columns
381 #if wxUSE_CHECKLISTBOX
383 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
384 GTK_TYPE_TREE_ENTRY
);
387 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
389 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
391 g_object_unref (m_liststore
); //free on treeview destruction
393 // Disable the pop-up textctrl that enables searching - note that
394 // the docs specify that even if this disabled (which we are doing)
395 // the user can still have it through the start-interactive-search
396 // key binding...either way we want to provide a searchequal callback
397 // NB: If this is enabled a doubleclick event (activate) gets sent
398 // on a successful search
399 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
400 gtk_tree_view_set_search_equal_func(m_treeview
,
401 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
405 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
408 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
410 g_signal_connect_after (selection
, "changed",
411 G_CALLBACK (gtk_listitem_changed_callback
), this);
413 GtkSelectionMode mode
;
414 if (style
& wxLB_MULTIPLE
)
416 mode
= GTK_SELECTION_MULTIPLE
;
418 else if (style
& wxLB_EXTENDED
)
420 mode
= GTK_SELECTION_EXTENDED
;
424 // if style was 0 set single mode
425 m_windowStyle
|= wxLB_SINGLE
;
426 mode
= GTK_SELECTION_SINGLE
;
429 gtk_tree_selection_set_mode( selection
, mode
);
431 // Handle sortable stuff
432 if(style
& wxLB_SORT
)
434 // Setup sorting in ascending (wx) order
435 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
436 WXLISTBOX_DATACOLUMN
,
439 // Set the sort callback
440 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
441 WXLISTBOX_DATACOLUMN
,
442 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
444 NULL
//"destroy notifier"
449 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
451 gtk_widget_show( GTK_WIDGET(m_treeview
) );
452 m_focusWidget
= GTK_WIDGET(m_treeview
);
454 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
456 // generate dclick events
457 g_signal_connect_after(m_treeview
, "row-activated",
458 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
460 // for panel navigation
461 g_signal_connect (m_treeview
, "key_press_event",
462 G_CALLBACK (gtk_listbox_key_press_callback
),
465 m_parent
->DoAddChild( this );
468 SetInitialSize(size
); // need this too because this is a wxControlWithItems
473 wxListBox::~wxListBox()
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
485 void** clientData
, unsigned int pos
)
487 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
489 InvalidateBestSize();
491 // Create and set column ids and GValues
493 unsigned int nNum
= items
.GetCount();
494 unsigned int nCurCount
= wxListBox::GetCount();
495 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
497 GtkTreeIter
* pIter
= NULL
; // append by default
499 if (pos
!= nCurCount
)
501 gboolean res
= gtk_tree_model_iter_nth_child(
502 GTK_TREE_MODEL(m_liststore
),
503 &iter
, NULL
, //NULL = parent = get first
507 wxLogSysError(wxT("internal wxListBox error in insertion"));
514 for (unsigned int i
= 0; i
< nNum
; ++i
)
516 wxString label
= items
[i
];
518 GtkTreeEntry
* entry
= gtk_tree_entry_new();
519 gtk_tree_entry_set_label(entry
, wxGTK_CONV(label
));
520 gtk_tree_entry_set_destroy_func(entry
,
521 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
525 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
528 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
530 #if wxUSE_CHECKLISTBOX
533 gtk_list_store_set(m_liststore
, &itercur
,
534 0, FALSE
, //FALSE == not toggled
539 gtk_list_store_set(m_liststore
, &itercur
,
542 g_object_unref (entry
); //liststore always refs :)
546 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
548 wxCHECK_RET( IsValidInsert(pos
), wxT("invalid index in wxListBox::InsertItems") );
550 GtkInsertItems(items
, NULL
, pos
);
553 int wxListBox::DoAppend( const wxString
& item
)
555 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
557 InvalidateBestSize();
559 GtkTreeEntry
* entry
= gtk_tree_entry_new();
560 gtk_tree_entry_set_label( entry
, wxGTK_CONV(item
) );
561 gtk_tree_entry_set_destroy_func(entry
,
562 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
566 gtk_list_store_insert_before( m_liststore
, &itercur
, NULL
);
568 #if wxUSE_CHECKLISTBOX
571 gtk_list_store_set( m_liststore
, &itercur
,
572 0, FALSE
, //FALSE == not toggled
577 gtk_list_store_set(m_liststore
, &itercur
,
580 g_object_unref (entry
); //liststore always refs :)
582 GtkTreePath
* path
= gtk_tree_model_get_path(
583 GTK_TREE_MODEL(m_liststore
),
586 gint
* pIntPath
= gtk_tree_path_get_indices(path
);
588 if (pIntPath
== NULL
)
590 wxLogSysError(wxT("internal wxListBox error in insertion"));
594 int index
= pIntPath
[0];
596 gtk_tree_path_free( path
);
601 void wxListBox::DoSetItems( const wxArrayString
& items
,
605 GtkInsertItems(items
, clientData
, 0);
608 // ----------------------------------------------------------------------------
610 // ----------------------------------------------------------------------------
612 void wxListBox::Clear()
614 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
616 InvalidateBestSize();
618 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
621 void wxListBox::Delete(unsigned int n
)
623 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
625 InvalidateBestSize();
628 gboolean res
= gtk_tree_model_iter_nth_child(
629 GTK_TREE_MODEL(m_liststore
),
630 &iter
, NULL
, //NULL = parent = get first
634 wxCHECK_RET( res
, wxT("wrong listbox index") );
636 //this returns false if iter is invalid (i.e. deleting item
637 //at end) but since we don't use iter, well... :)
638 gtk_list_store_remove(m_liststore
, &iter
);
641 // ----------------------------------------------------------------------------
642 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
643 // ----------------------------------------------------------------------------
645 struct _GtkTreeEntry
* wxListBox::GtkGetEntry(int n
) const
648 gboolean res
= gtk_tree_model_iter_nth_child(
649 GTK_TREE_MODEL(m_liststore
),
650 &iter
, NULL
, //NULL = parent = get first
655 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
656 wxT("Passed in value was:[%i] List size:[%u]"),
657 n
, wxListBox::GetCount() );
662 GtkTreeEntry
* entry
= NULL
;
663 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
664 WXLISTBOX_DATACOLUMN
, &entry
, -1);
669 // ----------------------------------------------------------------------------
671 // ----------------------------------------------------------------------------
673 void* wxListBox::DoGetItemClientData(unsigned int n
) const
675 wxCHECK_MSG( IsValid(n
), NULL
,
676 wxT("Invalid index passed to GetItemClientData") );
678 GtkTreeEntry
* entry
= GtkGetEntry(n
);
679 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
681 void* userdata
= gtk_tree_entry_get_userdata( entry
);
682 g_object_unref (entry
);
686 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
688 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
691 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
693 wxCHECK_RET( IsValid(n
),
694 wxT("Invalid index passed to SetItemClientData") );
696 GtkTreeEntry
* entry
= GtkGetEntry(n
);
697 wxCHECK_RET(entry
, wxT("could not get entry"));
699 gtk_tree_entry_set_userdata( entry
, clientData
);
700 g_object_unref (entry
);
703 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
705 // wxItemContainer already deletes data for us
706 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
709 // ----------------------------------------------------------------------------
710 // string list access
711 // ----------------------------------------------------------------------------
713 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
715 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
716 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
718 GtkTreeEntry
* entry
= GtkGetEntry(n
);
719 wxCHECK_RET( entry
, wxT("wrong listbox index") );
721 wxString label
= string
;
723 // RN: This may look wierd but the problem is that the TreeView
724 // doesn't resort or update when changed above and there is no real
725 // notification function...
726 void* userdata
= gtk_tree_entry_get_userdata(entry
);
727 gtk_tree_entry_set_userdata(entry
, NULL
); //don't delete on destroy
728 g_object_unref (entry
);
730 bool bWasSelected
= wxListBox::IsSelected(n
);
731 wxListBox::Delete(n
);
733 wxArrayString aItems
;
735 GtkInsertItems(aItems
, &userdata
, n
);
737 wxListBox::GtkSetSelection(n
, true, true);
740 wxString
wxListBox::GetString(unsigned int n
) const
742 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
744 GtkTreeEntry
* entry
= GtkGetEntry(n
);
745 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
747 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
749 g_object_unref (entry
);
753 unsigned int wxListBox::GetCount() const
755 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
757 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
760 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
762 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
764 //Sort of hackish - maybe there is a faster way
765 unsigned int nCount
= wxListBox::GetCount();
767 for(unsigned int i
= 0; i
< nCount
; ++i
)
769 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
774 // it's not an error if the string is not found -> no wxCHECK
778 // ----------------------------------------------------------------------------
780 // ----------------------------------------------------------------------------
782 int wxListBox::GetSelection() const
784 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
785 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
786 wxT("must be single selection listbox"));
789 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
791 // only works on single-sel
792 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
796 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
798 int sel
= gtk_tree_path_get_indices(path
)[0];
800 gtk_tree_path_free(path
);
805 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
807 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
813 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
815 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
816 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
819 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
823 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
826 return aSelections
.GetCount();
829 bool wxListBox::IsSelected( int n
) const
831 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
833 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
836 gboolean res
= gtk_tree_model_iter_nth_child(
837 GTK_TREE_MODEL(m_liststore
),
838 &iter
, NULL
, //NULL = parent = get first
841 wxCHECK_MSG( res
, false, wxT("Invalid index") );
843 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
846 void wxListBox::DoSetSelection( int n
, bool select
)
848 // passing -1 to SetSelection() is documented to deselect all items
849 if ( n
== wxNOT_FOUND
)
851 // ... and not generate any events in the process
856 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
858 // don't generate the selection event
859 GtkSetSelection(n
, select
, true);
862 void wxListBox::GtkDeselectAll()
864 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
866 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
870 gtk_tree_selection_unselect_all(selection
);
872 m_blockEvent
= false;
875 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
877 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
879 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
882 gboolean res
= gtk_tree_model_iter_nth_child(
883 GTK_TREE_MODEL(m_liststore
),
884 &iter
, NULL
, //NULL = parent = get first
887 wxCHECK_RET( res
, wxT("Invalid index") );
889 m_blockEvent
= blockEvent
;
892 gtk_tree_selection_select_iter(selection
, &iter
);
894 gtk_tree_selection_unselect_iter(selection
, &iter
);
896 m_blockEvent
= false;
899 void wxListBox::DoSetFirstItem( int n
)
901 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
902 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
904 //RN: I have no idea why this line is needed...
905 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
909 gtk_tree_model_iter_nth_child(
910 GTK_TREE_MODEL(m_liststore
),
912 NULL
, //NULL = parent = get first
916 GtkTreePath
* path
= gtk_tree_model_get_path(
917 GTK_TREE_MODEL(m_liststore
), &iter
);
919 // Scroll to the desired cell (0.0 == topleft alignment)
920 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
923 gtk_tree_path_free(path
);
926 // ----------------------------------------------------------------------------
928 // ----------------------------------------------------------------------------
930 int wxListBox::DoListHitTest(const wxPoint
& point
) const
932 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
933 // we only want visible items we need to check for it manually here
934 if ( !GetClientRect().Contains(point
) )
937 // need to translate from master window since it is in client coords
939 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
940 &binx
, &biny
, NULL
, NULL
, NULL
);
943 if ( !gtk_tree_view_get_path_at_pos
949 NULL
, // [out] column (always 0 here)
950 NULL
, // [out] x-coord relative to the cell (not interested)
951 NULL
// [out] y-coord relative to the cell
957 int index
= gtk_tree_path_get_indices(path
)[0];
958 gtk_tree_path_free(path
);
963 // ----------------------------------------------------------------------------
965 // ----------------------------------------------------------------------------
968 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
970 // RN: Is this needed anymore?
971 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
973 #endif // wxUSE_TOOLTIPS
975 GtkWidget
*wxListBox::GetConnectWidget()
977 // the correct widget for listbox events (such as mouse clicks for example)
978 // is m_treeview, not the parent scrolled window
979 return GTK_WIDGET(m_treeview
);
982 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
984 return gtk_tree_view_get_bin_window(m_treeview
);
987 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
989 if (m_hasBgCol
&& m_backgroundColour
.Ok())
991 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
994 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
995 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
996 gdk_window_clear( window
);
1000 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
1003 wxSize
wxListBox::DoGetBestSize() const
1005 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1007 // Start with a minimum size that's not too small
1009 GetTextExtent( wxT("X"), &cx
, &cy
);
1010 int lbWidth
= 3 * cx
;
1013 // Get the visible area of the tree view (limit to the 10th item
1014 // so that it isn't too big)
1015 unsigned int count
= GetCount();
1020 // Find the widest line
1021 for(unsigned int i
= 0; i
< count
; i
++) {
1022 wxString
str(GetString(i
));
1023 GetTextExtent(str
, &wLine
, NULL
);
1024 lbWidth
= wxMax(lbWidth
, wLine
);
1029 // And just a bit more for the checkbox if present and then some
1030 // (these are rough guesses)
1031 #if wxUSE_CHECKLISTBOX
1032 if ( m_hasCheckBoxes
)
1035 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1039 // don't make the listbox too tall (limit height to around 10 items) but don't
1040 // make it too small neither
1041 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1044 // Add room for the scrollbar
1045 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1047 wxSize
best(lbWidth
, lbHeight
);
1048 CacheBestSize(best
);
1054 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1056 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1059 #endif // wxUSE_LISTBOX