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"
18 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
20 #include "wx/arrstr.h"
23 #include "wx/checklst.h"
24 #include "wx/settings.h"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/treeentry_gtk.h"
30 #include "wx/tooltip.h"
35 #include <gdk/gdkkeysyms.h>
37 //-----------------------------------------------------------------------------
39 //-----------------------------------------------------------------------------
41 extern bool g_blockEventsOnDrag
;
42 extern bool g_blockEventsOnScroll
;
43 extern wxCursor g_globalCursor
;
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 extern void wxapp_install_idle_handler();
53 //-----------------------------------------------------------------------------
54 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
55 //-----------------------------------------------------------------------------
57 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
58 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
60 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
61 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
63 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
65 //-----------------------------------------------------------------------------
67 //-----------------------------------------------------------------------------
71 gtk_listbox_row_activated_callback(GtkTreeView
*treeview
,
73 GtkTreeViewColumn
*col
,
76 if (g_isIdle
) wxapp_install_idle_handler();
78 if (g_blockEventsOnDrag
) return;
79 if (g_blockEventsOnScroll
) return;
81 if (!listbox
->m_hasVMT
) return;
84 //1) This is triggered by either a double-click or a space press
85 //2) We handle both here because
86 //2a) in the case of a space/keypress we can't really know
87 // which item was pressed on because we can't get coords
89 //2b) It seems more correct
91 int sel
= gtk_tree_path_get_indices(path
)[0];
93 if(!listbox
->m_spacePressed
)
95 //Assume it was double-click
96 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
97 event
.SetEventObject( listbox
);
99 if(listbox
->IsSelected(sel
))
101 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(sel
);
106 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
108 if ( listbox
->HasClientObjectData() )
109 event
.SetClientObject(
110 (wxClientData
*) gtk_tree_entry_get_userdata(entry
) );
111 else if ( listbox
->HasClientUntypedData() )
112 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
113 g_object_unref(G_OBJECT(entry
));
117 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
124 listbox
->GetEventHandler()->ProcessEvent( event
);
128 listbox
->m_spacePressed
= false; //don't block selection behaviour anymore
130 //Space was pressed - toggle the appropriate checkbox and the selection
131 #if wxUSE_CHECKLISTBOX //Do it for both native and non-native
132 if (listbox
->m_hasCheckBoxes
)
134 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
136 clb
->Check( sel
, !clb
->IsChecked(sel
) );
138 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
139 new_event
.SetEventObject( listbox
);
140 new_event
.SetInt( sel
);
141 listbox
->GetEventHandler()->ProcessEvent( new_event
);
143 #endif // wxUSE_CHECKLISTBOX
145 if( (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
146 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
148 //toggle the selection + send event
149 if(listbox
->IsSelected( sel
))
150 listbox
->GtkSetSelection(sel
, FALSE
, FALSE
);
152 listbox
->GtkSetSelection(sel
, TRUE
, FALSE
);
158 //-----------------------------------------------------------------------------
159 // "button_press_event"
160 //-----------------------------------------------------------------------------
164 gtk_listbox_button_press_callback( GtkWidget
*widget
,
165 GdkEventButton
*gdk_event
,
168 if (g_isIdle
) wxapp_install_idle_handler();
170 if (g_blockEventsOnDrag
) return FALSE
;
171 if (g_blockEventsOnScroll
) return FALSE
;
173 if (!listbox
->m_hasVMT
) return FALSE
;
175 //Just to be on the safe side - it should be unset in the activate callback
176 //but we don't want any obscure bugs if it doesn't get called somehow...
177 listbox
->m_spacePressed
= false;
179 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
180 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
183 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget
),
184 (gint
)gdk_event
->x
, (gint
)gdk_event
->y
,
185 &path
, NULL
, NULL
, NULL
);
186 int sel
= gtk_tree_path_get_indices(path
)[0];
187 gtk_tree_path_free(path
);
189 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
191 clb
->Check( sel
, !clb
->IsChecked(sel
) );
193 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
194 event
.SetEventObject( listbox
);
196 listbox
->GetEventHandler()->ProcessEvent( event
);
198 #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
204 //-----------------------------------------------------------------------------
206 //-----------------------------------------------------------------------------
210 gtk_listbox_key_press_callback( GtkWidget
*widget
,
211 GdkEventKey
*gdk_event
,
214 if (g_isIdle
) wxapp_install_idle_handler();
216 if (g_blockEventsOnDrag
) return FALSE
;
221 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
223 wxNavigationKeyEvent new_event
;
224 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
225 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
226 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
227 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
228 new_event
.SetCurrentFocus( listbox
);
229 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
232 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
234 // eat return in all modes (RN:WHY?)
238 // Check or uncheck item with SPACE
239 if (gdk_event
->keyval
== ' ')
241 //In the keyevent we don't know the index of the item
242 //and the activated event gets called anyway...
244 //Also, activating with the space causes the treeview to
245 //unselect all the items and then select the item in question
246 //wx's behaviour is to just toggle the item's selection state
247 //and leave the others alone
248 listbox
->m_spacePressed
= true;
253 g_signal_stop_emission_by_name (widget
, "key_press_event");
261 //-----------------------------------------------------------------------------
262 // "select" and "deselect"
263 //-----------------------------------------------------------------------------
266 static gboolean
gtk_listitem_select_cb( GtkTreeSelection
* selection
,
269 gboolean is_selected
,
272 if (g_isIdle
) wxapp_install_idle_handler();
274 if (!listbox
->m_hasVMT
) return TRUE
;
275 if (g_blockEventsOnDrag
) return TRUE
;
277 if (listbox
->m_spacePressed
) return FALSE
; //see keyevent callback
278 if (listbox
->m_blockEvent
) return TRUE
;
280 // NB: wxdocs explicitly say that this event only gets sent when
281 // something is actually selected, plus the controls example
282 // assumes so and passes -1 to the dogetclientdata funcs if not
284 // OK, so basically we need to do a bit of a run-around here as
285 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
286 // i.e. if is_selected is FALSE then the item is going to be
287 // selected right now!
288 // 2) However, since it is not already selected and the user
289 // will expect it to be we need to manually select it and
290 // return FALSE telling GTK we handled the selection
291 if (is_selected
) return TRUE
;
293 int nIndex
= gtk_tree_path_get_indices(path
)[0];
294 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(nIndex
);
298 //Now, as mentioned above, we manually select the row that is/was going
299 //to be selected anyway by GTK
300 listbox
->m_blockEvent
= TRUE
; //if we don't block events we will lock the
301 //app due to recursion!!
303 GtkTreeSelection
* selection
=
304 gtk_tree_view_get_selection(listbox
->m_treeview
);
306 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox
->m_liststore
), &iter
, path
);
307 gtk_tree_selection_select_iter(selection
, &iter
);
309 listbox
->m_blockEvent
= FALSE
;
311 //Finally, send the wx event
312 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
313 event
.SetEventObject( listbox
);
315 // indicate whether this is a selection or a deselection
316 event
.SetExtraLong( 1 );
318 event
.SetInt(nIndex
);
319 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
321 if ( listbox
->HasClientObjectData() )
322 event
.SetClientObject(
323 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
325 else if ( listbox
->HasClientUntypedData() )
326 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
328 listbox
->GetEventHandler()->ProcessEvent( event
);
330 g_object_unref(G_OBJECT(entry
));
331 return FALSE
; //We handled it/did it manually
334 return TRUE
; //allow selection to change
338 //-----------------------------------------------------------------------------
339 // GtkTreeEntry destruction (to destroy client data)
340 //-----------------------------------------------------------------------------
343 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
346 if(listbox
->HasClientObjectData())
348 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
350 delete (wxClientData
*)userdata
;
355 //-----------------------------------------------------------------------------
356 // Sorting callback (standard CmpNoCase return value)
357 //-----------------------------------------------------------------------------
360 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
366 GtkTreeEntry
* entry2
;
368 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
370 WXLISTBOX_DATACOLUMN_ARG(listbox
),
372 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
374 WXLISTBOX_DATACOLUMN_ARG(listbox
),
376 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
377 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
379 //We compare collate keys here instead of calling g_utf8_collate
380 //as it is rather slow (and even the docs reccommend this)
381 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
382 gtk_tree_entry_get_collate_key(entry2
));
384 g_object_unref(G_OBJECT(entry
));
385 g_object_unref(G_OBJECT(entry2
));
391 //-----------------------------------------------------------------------------
392 // Searching callback (TRUE == not equal, FALSE == equal)
393 //-----------------------------------------------------------------------------
396 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
404 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
406 WXLISTBOX_DATACOLUMN_ARG(listbox
),
408 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
409 gchar
* keycollatekey
= g_utf8_collate_key(key
, -1);
411 int ret
= strcasecmp(keycollatekey
,
412 gtk_tree_entry_get_collate_key(entry
));
414 g_free(keycollatekey
);
415 g_object_unref(G_OBJECT(entry
));
421 //-----------------------------------------------------------------------------
423 //-----------------------------------------------------------------------------
425 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
427 // ----------------------------------------------------------------------------
429 // ----------------------------------------------------------------------------
431 wxListBox::wxListBox()
433 m_treeview
= (GtkTreeView
*) NULL
;
434 #if wxUSE_CHECKLISTBOX
435 m_hasCheckBoxes
= FALSE
;
436 #endif // wxUSE_CHECKLISTBOX
437 m_spacePressed
= false;
440 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
441 const wxPoint
&pos
, const wxSize
&size
,
442 const wxArrayString
& choices
,
443 long style
, const wxValidator
& validator
,
444 const wxString
&name
)
446 wxCArrayString
chs(choices
);
448 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
449 style
, validator
, name
);
452 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
453 const wxPoint
&pos
, const wxSize
&size
,
454 int n
, const wxString choices
[],
455 long style
, const wxValidator
& validator
,
456 const wxString
&name
)
459 m_acceptsFocus
= TRUE
;
460 m_blockEvent
= FALSE
;
462 if (!PreCreation( parent
, pos
, size
) ||
463 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
465 wxFAIL_MSG( wxT("wxListBox creation failed") );
469 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
470 if (style
& wxLB_ALWAYS_SB
)
472 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
473 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
477 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
478 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
481 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
483 //wxListBox doesn't have a header :)
484 //NB: If enabled SetFirstItem doesn't work correctly
485 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
487 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
489 ((wxCheckListBox
*)this)->DoCreateCheckList();
490 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
492 // Create the data column
493 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
494 gtk_cell_renderer_text_new(),
496 WXLISTBOX_DATACOLUMN
, NULL
);
498 // Now create+set the model (GtkListStore) - first argument # of columns
500 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
502 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
503 GTK_TYPE_TREE_ENTRY
);
506 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
508 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
510 g_object_unref(G_OBJECT(m_liststore
)); //free on treeview destruction
512 // Disable the pop-up textctrl that enables searching - note that
513 // the docs specify that even if this disabled (which we are doing)
514 // the user can still have it through the start-interactive-search
515 // key binding...either way we want to provide a searchequal callback
516 // NB: If this is enabled a doubleclick event (activate) gets sent
517 // on a successful search
518 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
519 gtk_tree_view_set_search_equal_func(m_treeview
,
520 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
524 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
527 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
528 gtk_tree_selection_set_select_function(selection
,
529 (GtkTreeSelectionFunc
)gtk_listitem_select_cb
,
530 this, NULL
); //NULL == destroycb
532 GtkSelectionMode mode
;
533 if (style
& wxLB_MULTIPLE
)
535 mode
= GTK_SELECTION_MULTIPLE
;
537 else if (style
& wxLB_EXTENDED
)
539 mode
= GTK_SELECTION_EXTENDED
;
543 // if style was 0 set single mode
544 m_windowStyle
|= wxLB_SINGLE
;
545 mode
= GTK_SELECTION_SINGLE
;
548 gtk_tree_selection_set_mode( selection
, mode
);
550 //Handle sortable stuff
551 if(style
& wxLB_SORT
)
553 //Setup sorting in ascending (wx) order
554 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
555 WXLISTBOX_DATACOLUMN
,
558 //Set the sort callback
559 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
560 WXLISTBOX_DATACOLUMN
,
561 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
563 NULL
//"destroy notifier"
568 gtk_scrolled_window_add_with_viewport( GTK_SCROLLED_WINDOW(m_widget
),
569 GTK_WIDGET(m_treeview
) );
571 /* make list scroll when moving the focus down using cursor keys */
572 gtk_container_set_focus_vadjustment(
573 GTK_CONTAINER(m_treeview
),
574 gtk_scrolled_window_get_vadjustment(
575 GTK_SCROLLED_WINDOW(m_widget
)));
577 gtk_widget_show( GTK_WIDGET(m_treeview
) );
579 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
581 //treeview-specific events
582 g_signal_connect(m_treeview
, "row-activated",
583 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
586 g_signal_connect (m_treeview
, "button_press_event",
587 G_CALLBACK (gtk_listbox_button_press_callback
),
589 g_signal_connect (m_treeview
, "key_press_event",
590 G_CALLBACK (gtk_listbox_key_press_callback
),
593 m_parent
->DoAddChild( this );
596 SetBestSize(size
); // need this too because this is a wxControlWithItems
601 wxListBox::~wxListBox()
608 // ----------------------------------------------------------------------------
610 // ----------------------------------------------------------------------------
612 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
613 void** clientData
, int pos
)
615 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
617 InvalidateBestSize();
619 // Create and set column ids and GValues
621 size_t nNum
= items
.GetCount();
622 int nCurCount
= wxListBox::GetCount();
623 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
626 if (pos
== nCurCount
)
628 pIter
= NULL
; // append
633 gboolean res
= gtk_tree_model_iter_nth_child(
634 GTK_TREE_MODEL(m_liststore
),
635 &iter
, NULL
, //NULL = parent = get first
639 wxLogSysError(wxT("internal wxListBox error in insertion"));
646 for (size_t i
= 0; i
< nNum
; ++i
)
648 wxString label
= items
[i
];
650 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
653 label
.Prepend(wxCHECKLBOX_STRING
);
655 #endif // wxUSE_CHECKLISTBOX
658 GtkTreeEntry
* entry
= gtk_tree_entry_new();
659 gtk_tree_entry_set_label(entry
, wxConvUTF8
.cWX2MB(label
));
660 gtk_tree_entry_set_destroy_func(entry
,
661 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
665 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
668 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
670 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
673 gtk_list_store_set(m_liststore
, &itercur
,
674 0, FALSE
, //FALSE == not toggled
679 gtk_list_store_set(m_liststore
, &itercur
,
682 g_object_unref(G_OBJECT(entry
)); //liststore always refs :)
686 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
688 GtkInsertItems(items
, NULL
, pos
);
691 int wxListBox::DoAppend( const wxString
& item
)
693 InvalidateBestSize();
695 //Just call DoInsertItems for now
696 //RN: Originally I had gtk_list_store_append etc.
697 // here as an optimization but now the insert
698 // has been streamlined and its quite a bit of code duplication
699 int nWhere
= wxListBox::GetCount();
700 wxArrayString aItems
;
702 wxListBox::DoInsertItems(aItems
, nWhere
);
706 void wxListBox::DoSetItems( const wxArrayString
& items
,
710 GtkInsertItems(items
, clientData
, 0);
713 // ----------------------------------------------------------------------------
715 // ----------------------------------------------------------------------------
717 void wxListBox::Clear()
719 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
721 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
724 void wxListBox::Delete( int n
)
726 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
729 gboolean res
= gtk_tree_model_iter_nth_child(
730 GTK_TREE_MODEL(m_liststore
),
731 &iter
, NULL
, //NULL = parent = get first
735 wxCHECK_RET( res
, wxT("wrong listbox index") );
737 //this returns false if iter is invalid (i.e. deleting item
738 //at end) but since we don't use iter, well... :)
739 gtk_list_store_remove(m_liststore
, &iter
);
742 // ----------------------------------------------------------------------------
743 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
744 // ----------------------------------------------------------------------------
746 struct _GtkTreeEntry
* wxListBox::GtkGetEntry(int n
) const
749 gboolean res
= gtk_tree_model_iter_nth_child(
750 GTK_TREE_MODEL(m_liststore
),
751 &iter
, NULL
, //NULL = parent = get first
756 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
757 wxT("Passed in value was:[%i] List size:[%i]"),
758 n
, wxListBox::GetCount() );
763 GtkTreeEntry
* entry
= NULL
;
764 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
765 WXLISTBOX_DATACOLUMN
, &entry
, -1);
770 // ----------------------------------------------------------------------------
772 // ----------------------------------------------------------------------------
774 void* wxListBox::DoGetItemClientData( int n
) const
776 wxCHECK_MSG( n
>= 0 && n
< wxListBox::GetCount(), NULL
,
777 wxT("Invalid index passed to GetItemClientData") );
779 GtkTreeEntry
* entry
= GtkGetEntry(n
);
780 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
782 void* userdata
= gtk_tree_entry_get_userdata( entry
);
783 g_object_unref(G_OBJECT(entry
));
787 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
789 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
792 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
794 wxCHECK_RET( n
>= 0 && n
< wxListBox::GetCount(),
795 wxT("Invalid index passed to SetItemClientData") );
797 GtkTreeEntry
* entry
= GtkGetEntry(n
);
798 wxCHECK_RET(entry
, wxT("could not get entry"));
800 gtk_tree_entry_set_userdata( entry
, clientData
);
801 g_object_unref(G_OBJECT(entry
));
804 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
806 // wxItemContainer already deletes data for us
807 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
810 // ----------------------------------------------------------------------------
811 // string list access
812 // ----------------------------------------------------------------------------
814 void wxListBox::SetString( int n
, const wxString
&string
)
816 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
818 GtkTreeEntry
* entry
= GtkGetEntry(n
);
819 wxCHECK_RET( entry
, wxT("wrong listbox index") );
821 wxString label
= string
;
823 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
825 label
.Prepend(wxCHECKLBOX_STRING
);
826 #endif // wxUSE_CHECKLISTBOX
828 // RN: This may look wierd but the problem is that the TreeView
829 // doesn't resort or update when changed above and there is no real
830 // notification function...
831 void* userdata
= gtk_tree_entry_get_userdata(entry
);
832 gtk_tree_entry_set_userdata(entry
, NULL
); //don't delete on destroy
833 g_object_unref(G_OBJECT(entry
));
835 bool bWasSelected
= wxListBox::IsSelected(n
);
836 wxListBox::Delete(n
);
838 wxArrayString aItems
;
840 GtkInsertItems(aItems
, &userdata
, n
);
842 wxListBox::GtkSetSelection(n
, TRUE
, TRUE
);
845 wxString
wxListBox::GetString( int n
) const
847 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
849 GtkTreeEntry
* entry
= GtkGetEntry(n
);
850 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
852 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
854 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
855 // checklistboxes have "[±] " prepended to their lables, remove it
857 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
858 if ( m_hasCheckBoxes
)
860 #endif // wxUSE_CHECKLISTBOX
862 g_object_unref(G_OBJECT(entry
));
866 int wxListBox::GetCount() const
868 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
870 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
873 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
875 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
877 //Sort of hackish - maybe there is a faster way
878 int nCount
= wxListBox::GetCount();
880 for(int i
= 0; i
< nCount
; ++i
)
882 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
887 // it's not an error if the string is not found -> no wxCHECK
891 // ----------------------------------------------------------------------------
893 // ----------------------------------------------------------------------------
895 int wxListBox::GetSelection() const
897 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox"));
898 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), -1,
899 wxT("must be single selection listbox"));
902 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
904 // only works on single-sel
905 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
909 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
911 int sel
= gtk_tree_path_get_indices(path
)[0];
913 gtk_tree_path_free(path
);
918 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
920 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
926 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
928 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
929 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
932 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
936 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
939 return aSelections
.GetCount();
942 bool wxListBox::IsSelected( int n
) const
944 wxCHECK_MSG( m_treeview
!= NULL
, FALSE
, wxT("invalid listbox") );
946 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
949 gboolean res
= gtk_tree_model_iter_nth_child(
950 GTK_TREE_MODEL(m_liststore
),
951 &iter
, NULL
, //NULL = parent = get first
954 wxCHECK_MSG( res
, FALSE
, wxT("Invalid index") );
956 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
959 void wxListBox::DoSetSelection( int n
, bool select
)
961 return GtkSetSelection(n
, select
, TRUE
); //docs say no events here
964 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
966 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
968 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
971 gboolean res
= gtk_tree_model_iter_nth_child(
972 GTK_TREE_MODEL(m_liststore
),
973 &iter
, NULL
, //NULL = parent = get first
976 wxCHECK_RET( res
, wxT("Invalid index") );
978 m_blockEvent
= blockEvent
;
981 gtk_tree_selection_select_iter(selection
, &iter
);
983 gtk_tree_selection_unselect_iter(selection
, &iter
);
985 m_blockEvent
= FALSE
;
988 void wxListBox::DoSetFirstItem( int n
)
990 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
991 wxCHECK_RET( n
>= 0 && n
< wxListBox::GetCount(), wxT("invalid index"));
993 //RN: I have no idea why this line is needed...
994 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
997 // terribly efficient (RN:???)
998 // RN: Note that evidently the vadjustment property "vadjustment" from
999 // GtkTreeView is different from the "gtk-vadjustment"...
1000 // (i.e. gtk_tree_view_get_vadjustment)
1001 const gchar
*vadjustment_key
= "gtk-vadjustment";
1002 guint vadjustment_key_id
= g_quark_from_static_string (vadjustment_key
);
1004 GtkAdjustment
*adjustment
=
1005 (GtkAdjustment
*) g_object_get_qdata(G_OBJECT (m_treeview
), vadjustment_key_id
);
1006 wxCHECK_RET( adjustment
, wxT("invalid listbox code") );
1008 // Get the greater of the item heights from each column
1009 gint cellheight
= 0, cellheightcur
;
1010 GList
* columnlist
= gtk_tree_view_get_columns(m_treeview
);
1011 GList
* curlist
= columnlist
;
1015 gtk_tree_view_column_cell_get_size(
1016 GTK_TREE_VIEW_COLUMN(curlist
->data
),
1017 NULL
, NULL
, NULL
, NULL
,
1020 cellheight
= cellheightcur
> cellheight
?
1021 cellheightcur
: cellheight
;
1023 curlist
= curlist
->next
;
1026 g_list_free(columnlist
);
1028 float y
= (float) (cellheight
* n
);
1029 if (y
> adjustment
->upper
- adjustment
->page_size
)
1030 y
= adjustment
->upper
- adjustment
->page_size
;
1031 gtk_adjustment_set_value( adjustment
, y
);
1035 // ----------------------------------------------------------------------------
1037 // ----------------------------------------------------------------------------
1040 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1042 // RN: Is this needed anymore?
1043 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
1045 #endif // wxUSE_TOOLTIPS
1047 GtkWidget
*wxListBox::GetConnectWidget()
1049 // return GTK_WIDGET(m_treeview);
1053 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1055 return (window
== GetGtkBinWindow());
1058 GdkWindow
* wxListBox::GetGtkBinWindow()
1060 // GtkTreeView has 2 GdkWindows (just like GtkPizza and
1061 // any other GtkWidget that can scroll). The one on top
1062 // is usually called "->bin_window" and is the one we are
1063 // looking for here. Since GtkTreeView hides this in the
1064 // private data section, we assume that "->window" only has
1065 // one child window, namely, the "->bin_window".
1067 GdkWindow
*window
= GTK_WIDGET(m_treeview
)->window
;
1071 GList
*children
= gdk_window_peek_children( window
);
1072 if (g_list_length(children
) == 0)
1075 GdkWindow
*bin_window
= (GdkWindow
*) g_list_nth_data( children
, 0 );
1079 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1081 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1083 GdkWindow
*window
= GetGtkBinWindow();
1086 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1087 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1088 gdk_window_clear( window
);
1092 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
1095 void wxListBox::OnInternalIdle()
1097 //RN: Is this needed anymore?
1098 wxCursor cursor
= m_cursor
;
1099 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1101 if (GTK_WIDGET(m_treeview
)->window
&& cursor
.Ok())
1103 /* I now set the cursor the anew in every OnInternalIdle call
1104 as setting the cursor in a parent window also effects the
1105 windows above so that checking for the current cursor is
1107 GdkWindow
*window
= GetGtkBinWindow();
1108 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1111 if (wxUpdateUIEvent::CanUpdate(this))
1112 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1115 wxSize
wxListBox::DoGetBestSize() const
1117 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1122 // Get the visible area of the tree view
1124 gtk_tree_view_get_visible_rect(m_treeview
, &rect
);
1125 lbWidth
= rect
.width
;
1126 lbHeight
= rect
.height
;
1128 // Add room for the scrollbar
1129 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1131 // And just a bit more
1133 GetTextExtent( wxT("X"), &cx
, &cy
);
1136 // don't make the listbox too tall (limit height to around 10 items) but don't
1137 // make it too small neither
1138 lbHeight
= (cy
+4) * wxMin(wxMax(GetCount(), 3), 10);
1140 wxSize
best(lbWidth
, lbHeight
);
1141 CacheBestSize(best
);
1147 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1149 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1152 #endif // wxUSE_LISTBOX