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"
17 #include "wx/dynarray.h"
20 #include "wx/listbox.h"
21 #include "wx/arrstr.h"
24 #include "wx/checklst.h"
25 #include "wx/settings.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/treeentry_gtk.h"
31 #include "wx/tooltip.h"
36 #include <gdk/gdkkeysyms.h>
38 //-----------------------------------------------------------------------------
40 //-----------------------------------------------------------------------------
42 extern bool g_blockEventsOnDrag
;
43 extern bool g_blockEventsOnScroll
;
44 extern wxCursor g_globalCursor
;
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
52 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
53 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
55 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
56 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
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 if (!listbox
->m_hasVMT
) return;
79 //1) This is triggered by either a double-click or a space press
80 //2) We handle both here because
81 //2a) in the case of a space/keypress we can't really know
82 // which item was pressed on because we can't get coords
84 //2b) It seems more correct
86 int sel
= gtk_tree_path_get_indices(path
)[0];
88 if(!listbox
->m_spacePressed
)
90 //Assume it was double-click
91 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
92 event
.SetEventObject( listbox
);
94 if(listbox
->IsSelected(sel
))
96 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(sel
);
101 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
103 if ( listbox
->HasClientObjectData() )
104 event
.SetClientObject(
105 (wxClientData
*) gtk_tree_entry_get_userdata(entry
) );
106 else if ( listbox
->HasClientUntypedData() )
107 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
108 g_object_unref(G_OBJECT(entry
));
112 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
119 listbox
->GetEventHandler()->ProcessEvent( event
);
123 listbox
->m_spacePressed
= false; //don't block selection behaviour anymore
125 //Space was pressed - toggle the appropriate checkbox and the selection
126 #if wxUSE_CHECKLISTBOX //Do it for both native and non-native
127 if (listbox
->m_hasCheckBoxes
)
129 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
131 clb
->Check( sel
, !clb
->IsChecked(sel
) );
133 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
134 new_event
.SetEventObject( listbox
);
135 new_event
.SetInt( sel
);
136 listbox
->GetEventHandler()->ProcessEvent( new_event
);
138 #endif // wxUSE_CHECKLISTBOX
140 if( (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
141 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
143 //toggle the selection + send event
144 listbox
->GtkSetSelection(sel
, !listbox
->IsSelected( sel
), false);
150 //-----------------------------------------------------------------------------
151 // "button_press_event"
152 //-----------------------------------------------------------------------------
156 gtk_listbox_button_press_callback( GtkWidget
*widget
,
157 GdkEventButton
*gdk_event
,
160 if (g_isIdle
) wxapp_install_idle_handler();
162 if (g_blockEventsOnDrag
) return FALSE
;
163 if (g_blockEventsOnScroll
) return FALSE
;
165 if (!listbox
->m_hasVMT
) return FALSE
;
167 //Just to be on the safe side - it should be unset in the activate callback
168 //but we don't want any obscure bugs if it doesn't get called somehow...
169 listbox
->m_spacePressed
= false;
171 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
172 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
175 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget
),
176 (gint
)gdk_event
->x
, (gint
)gdk_event
->y
,
177 &path
, NULL
, NULL
, NULL
);
178 int sel
= gtk_tree_path_get_indices(path
)[0];
179 gtk_tree_path_free(path
);
181 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
183 clb
->Check( sel
, !clb
->IsChecked(sel
) );
185 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
186 event
.SetEventObject( listbox
);
188 listbox
->GetEventHandler()->ProcessEvent( event
);
190 #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
196 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
202 gtk_listbox_key_press_callback( GtkWidget
*widget
,
203 GdkEventKey
*gdk_event
,
206 if (g_isIdle
) wxapp_install_idle_handler();
208 if (g_blockEventsOnDrag
) return FALSE
;
213 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
215 wxNavigationKeyEvent new_event
;
216 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
217 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
218 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
219 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
220 new_event
.SetCurrentFocus( listbox
);
221 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
224 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
226 // eat return in all modes (RN:WHY?)
230 // Check or uncheck item with SPACE
231 if (gdk_event
->keyval
== ' ')
233 //In the keyevent we don't know the index of the item
234 //and the activated event gets called anyway...
236 //Also, activating with the space causes the treeview to
237 //unselect all the items and then select the item in question
238 //wx's behaviour is to just toggle the item's selection state
239 //and leave the others alone
240 listbox
->m_spacePressed
= true;
245 g_signal_stop_emission_by_name (widget
, "key_press_event");
253 //-----------------------------------------------------------------------------
254 // "select" and "deselect"
255 //-----------------------------------------------------------------------------
258 static gboolean
gtk_listitem_select_cb( GtkTreeSelection
* selection
,
261 gboolean is_selected
,
264 if (g_isIdle
) wxapp_install_idle_handler();
266 if (!listbox
->m_hasVMT
) return TRUE
;
267 if (g_blockEventsOnDrag
) return TRUE
;
269 if (listbox
->m_spacePressed
) return FALSE
; //see keyevent callback
270 if (listbox
->m_blockEvent
) return TRUE
;
272 // NB: wxdocs explicitly say that this event only gets sent when
273 // something is actually selected, plus the controls example
274 // assumes so and passes -1 to the dogetclientdata funcs if not
276 // OK, so basically we need to do a bit of a run-around here as
277 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
278 // i.e. if is_selected is FALSE then the item is going to be
279 // selected right now!
280 // 2) However, since it is not already selected and the user
281 // will expect it to be we need to manually select it and
282 // return FALSE telling GTK we handled the selection
283 if (is_selected
) return TRUE
;
285 int nIndex
= gtk_tree_path_get_indices(path
)[0];
286 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(nIndex
);
290 //Now, as mentioned above, we manually select the row that is/was going
291 //to be selected anyway by GTK
292 listbox
->m_blockEvent
= true; //if we don't block events we will lock the
293 //app due to recursion!!
295 GtkTreeSelection
* selection
=
296 gtk_tree_view_get_selection(listbox
->m_treeview
);
298 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox
->m_liststore
), &iter
, path
);
299 gtk_tree_selection_select_iter(selection
, &iter
);
301 listbox
->m_blockEvent
= false;
303 //Finally, send the wx event
304 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
305 event
.SetEventObject( listbox
);
307 // indicate whether this is a selection or a deselection
308 event
.SetExtraLong( 1 );
310 event
.SetInt(nIndex
);
311 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
313 if ( listbox
->HasClientObjectData() )
314 event
.SetClientObject(
315 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
317 else if ( listbox
->HasClientUntypedData() )
318 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
320 listbox
->GetEventHandler()->ProcessEvent( event
);
322 g_object_unref(G_OBJECT(entry
));
323 return FALSE
; //We handled it/did it manually
326 return TRUE
; //allow selection to change
330 //-----------------------------------------------------------------------------
331 // GtkTreeEntry destruction (to destroy client data)
332 //-----------------------------------------------------------------------------
335 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
338 if(listbox
->HasClientObjectData())
340 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
342 delete (wxClientData
*)userdata
;
347 //-----------------------------------------------------------------------------
348 // Sorting callback (standard CmpNoCase return value)
349 //-----------------------------------------------------------------------------
352 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
358 GtkTreeEntry
* entry2
;
360 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
362 WXLISTBOX_DATACOLUMN_ARG(listbox
),
364 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
366 WXLISTBOX_DATACOLUMN_ARG(listbox
),
368 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
369 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
371 //We compare collate keys here instead of calling g_utf8_collate
372 //as it is rather slow (and even the docs reccommend this)
373 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
374 gtk_tree_entry_get_collate_key(entry2
));
376 g_object_unref(G_OBJECT(entry
));
377 g_object_unref(G_OBJECT(entry2
));
383 //-----------------------------------------------------------------------------
384 // Searching callback (TRUE == not equal, FALSE == equal)
385 //-----------------------------------------------------------------------------
388 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
396 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
398 WXLISTBOX_DATACOLUMN_ARG(listbox
),
400 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
401 gchar
* keycollatekey
= g_utf8_collate_key(key
, -1);
403 int ret
= strcasecmp(keycollatekey
,
404 gtk_tree_entry_get_collate_key(entry
));
406 g_free(keycollatekey
);
407 g_object_unref(G_OBJECT(entry
));
413 //-----------------------------------------------------------------------------
415 //-----------------------------------------------------------------------------
417 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 void wxListBox::Init()
425 m_treeview
= (GtkTreeView
*) NULL
;
426 #if wxUSE_CHECKLISTBOX
427 m_hasCheckBoxes
= false;
428 #endif // wxUSE_CHECKLISTBOX
429 m_spacePressed
= false;
432 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
433 const wxPoint
&pos
, const wxSize
&size
,
434 const wxArrayString
& choices
,
435 long style
, const wxValidator
& validator
,
436 const wxString
&name
)
438 wxCArrayString
chs(choices
);
440 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
441 style
, validator
, name
);
444 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
445 const wxPoint
&pos
, const wxSize
&size
,
446 int n
, const wxString choices
[],
447 long style
, const wxValidator
& validator
,
448 const wxString
&name
)
451 m_acceptsFocus
= true;
452 m_blockEvent
= false;
454 if (!PreCreation( parent
, pos
, size
) ||
455 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
457 wxFAIL_MSG( wxT("wxListBox creation failed") );
461 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
462 if (style
& wxLB_ALWAYS_SB
)
464 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
465 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
469 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
470 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
474 GtkScrolledWindowSetBorder(m_widget
, style
);
476 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
478 //wxListBox doesn't have a header :)
479 //NB: If enabled SetFirstItem doesn't work correctly
480 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
482 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
484 ((wxCheckListBox
*)this)->DoCreateCheckList();
485 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
487 // Create the data column
488 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
489 gtk_cell_renderer_text_new(),
491 WXLISTBOX_DATACOLUMN
, NULL
);
493 // Now create+set the model (GtkListStore) - first argument # of columns
494 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
496 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
497 GTK_TYPE_TREE_ENTRY
);
500 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
502 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
504 g_object_unref(G_OBJECT(m_liststore
)); //free on treeview destruction
506 // Disable the pop-up textctrl that enables searching - note that
507 // the docs specify that even if this disabled (which we are doing)
508 // the user can still have it through the start-interactive-search
509 // key binding...either way we want to provide a searchequal callback
510 // NB: If this is enabled a doubleclick event (activate) gets sent
511 // on a successful search
512 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
513 gtk_tree_view_set_search_equal_func(m_treeview
,
514 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
518 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
521 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
522 gtk_tree_selection_set_select_function(selection
,
523 (GtkTreeSelectionFunc
)gtk_listitem_select_cb
,
524 this, NULL
); //NULL == destroycb
526 GtkSelectionMode mode
;
527 if (style
& wxLB_MULTIPLE
)
529 mode
= GTK_SELECTION_MULTIPLE
;
531 else if (style
& wxLB_EXTENDED
)
533 mode
= GTK_SELECTION_EXTENDED
;
537 // if style was 0 set single mode
538 m_windowStyle
|= wxLB_SINGLE
;
539 mode
= GTK_SELECTION_SINGLE
;
542 gtk_tree_selection_set_mode( selection
, mode
);
544 //Handle sortable stuff
545 if(style
& wxLB_SORT
)
547 //Setup sorting in ascending (wx) order
548 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
549 WXLISTBOX_DATACOLUMN
,
552 //Set the sort callback
553 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
554 WXLISTBOX_DATACOLUMN
,
555 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
557 NULL
//"destroy notifier"
562 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
564 gtk_widget_show( GTK_WIDGET(m_treeview
) );
566 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
568 //treeview-specific events
569 g_signal_connect(m_treeview
, "row-activated",
570 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
573 g_signal_connect (m_treeview
, "button_press_event",
574 G_CALLBACK (gtk_listbox_button_press_callback
),
576 g_signal_connect (m_treeview
, "key_press_event",
577 G_CALLBACK (gtk_listbox_key_press_callback
),
580 m_parent
->DoAddChild( this );
583 SetBestSize(size
); // need this too because this is a wxControlWithItems
588 wxListBox::~wxListBox()
595 // ----------------------------------------------------------------------------
597 // ----------------------------------------------------------------------------
599 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
600 void** clientData
, unsigned int pos
)
602 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
604 InvalidateBestSize();
606 // Create and set column ids and GValues
608 unsigned int nNum
= items
.GetCount();
609 unsigned int nCurCount
= wxListBox::GetCount();
610 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
612 GtkTreeIter
* pIter
= NULL
; // append by default
614 if (pos
!= nCurCount
)
616 gboolean res
= gtk_tree_model_iter_nth_child(
617 GTK_TREE_MODEL(m_liststore
),
618 &iter
, NULL
, //NULL = parent = get first
622 wxLogSysError(wxT("internal wxListBox error in insertion"));
629 for (unsigned int i
= 0; i
< nNum
; ++i
)
631 wxString label
= items
[i
];
633 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
636 label
.Prepend(wxCHECKLBOX_STRING
);
638 #endif // wxUSE_CHECKLISTBOX
641 GtkTreeEntry
* entry
= gtk_tree_entry_new();
642 gtk_tree_entry_set_label(entry
, wxConvUTF8
.cWX2MB(label
));
643 gtk_tree_entry_set_destroy_func(entry
,
644 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
648 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
651 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
653 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
656 gtk_list_store_set(m_liststore
, &itercur
,
657 0, FALSE
, //FALSE == not toggled
662 gtk_list_store_set(m_liststore
, &itercur
,
665 g_object_unref(G_OBJECT(entry
)); //liststore always refs :)
669 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
671 wxCHECK_RET( IsValidInsert(pos
), wxT("invalid index in wxListBox::InsertItems") );
673 GtkInsertItems(items
, NULL
, pos
);
676 int wxListBox::DoAppend( const wxString
& item
)
678 // Call DoInsertItems
679 unsigned int nWhere
= wxListBox::GetCount();
680 wxArrayString aItems
;
682 wxListBox::DoInsertItems(aItems
, nWhere
);
686 void wxListBox::DoSetItems( const wxArrayString
& items
,
690 GtkInsertItems(items
, clientData
, 0);
693 // ----------------------------------------------------------------------------
695 // ----------------------------------------------------------------------------
697 void wxListBox::Clear()
699 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
701 InvalidateBestSize();
703 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
706 void wxListBox::Delete(unsigned int n
)
708 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
710 InvalidateBestSize();
713 gboolean res
= gtk_tree_model_iter_nth_child(
714 GTK_TREE_MODEL(m_liststore
),
715 &iter
, NULL
, //NULL = parent = get first
719 wxCHECK_RET( res
, wxT("wrong listbox index") );
721 //this returns false if iter is invalid (i.e. deleting item
722 //at end) but since we don't use iter, well... :)
723 gtk_list_store_remove(m_liststore
, &iter
);
726 // ----------------------------------------------------------------------------
727 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
728 // ----------------------------------------------------------------------------
730 struct _GtkTreeEntry
* wxListBox::GtkGetEntry(int n
) const
733 gboolean res
= gtk_tree_model_iter_nth_child(
734 GTK_TREE_MODEL(m_liststore
),
735 &iter
, NULL
, //NULL = parent = get first
740 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
741 wxT("Passed in value was:[%i] List size:[%u]"),
742 n
, wxListBox::GetCount() );
747 GtkTreeEntry
* entry
= NULL
;
748 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
749 WXLISTBOX_DATACOLUMN
, &entry
, -1);
754 // ----------------------------------------------------------------------------
756 // ----------------------------------------------------------------------------
758 void* wxListBox::DoGetItemClientData(unsigned int n
) const
760 wxCHECK_MSG( IsValid(n
), NULL
,
761 wxT("Invalid index passed to GetItemClientData") );
763 GtkTreeEntry
* entry
= GtkGetEntry(n
);
764 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
766 void* userdata
= gtk_tree_entry_get_userdata( entry
);
767 g_object_unref(G_OBJECT(entry
));
771 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
773 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
776 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
778 wxCHECK_RET( IsValid(n
),
779 wxT("Invalid index passed to SetItemClientData") );
781 GtkTreeEntry
* entry
= GtkGetEntry(n
);
782 wxCHECK_RET(entry
, wxT("could not get entry"));
784 gtk_tree_entry_set_userdata( entry
, clientData
);
785 g_object_unref(G_OBJECT(entry
));
788 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
790 // wxItemContainer already deletes data for us
791 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
794 // ----------------------------------------------------------------------------
795 // string list access
796 // ----------------------------------------------------------------------------
798 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
800 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
801 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
803 GtkTreeEntry
* entry
= GtkGetEntry(n
);
804 wxCHECK_RET( entry
, wxT("wrong listbox index") );
806 wxString label
= string
;
808 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
810 label
.Prepend(wxCHECKLBOX_STRING
);
811 #endif // wxUSE_CHECKLISTBOX
813 // RN: This may look wierd but the problem is that the TreeView
814 // doesn't resort or update when changed above and there is no real
815 // notification function...
816 void* userdata
= gtk_tree_entry_get_userdata(entry
);
817 gtk_tree_entry_set_userdata(entry
, NULL
); //don't delete on destroy
818 g_object_unref(G_OBJECT(entry
));
820 bool bWasSelected
= wxListBox::IsSelected(n
);
821 wxListBox::Delete(n
);
823 wxArrayString aItems
;
825 GtkInsertItems(aItems
, &userdata
, n
);
827 wxListBox::GtkSetSelection(n
, true, true);
830 wxString
wxListBox::GetString(unsigned int n
) const
832 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
834 GtkTreeEntry
* entry
= GtkGetEntry(n
);
835 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
837 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
839 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
840 // checklistboxes have "[±] " prepended to their lables, remove it
842 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
843 if ( m_hasCheckBoxes
)
845 #endif // wxUSE_CHECKLISTBOX
847 g_object_unref(G_OBJECT(entry
));
851 unsigned int wxListBox::GetCount() const
853 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
855 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
858 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
860 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
862 //Sort of hackish - maybe there is a faster way
863 unsigned int nCount
= wxListBox::GetCount();
865 for(unsigned int i
= 0; i
< nCount
; ++i
)
867 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
872 // it's not an error if the string is not found -> no wxCHECK
876 // ----------------------------------------------------------------------------
878 // ----------------------------------------------------------------------------
880 int wxListBox::GetSelection() const
882 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox"));
883 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), -1,
884 wxT("must be single selection listbox"));
887 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
889 // only works on single-sel
890 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
894 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
896 int sel
= gtk_tree_path_get_indices(path
)[0];
898 gtk_tree_path_free(path
);
903 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
905 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
911 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
913 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
914 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
917 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
921 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
924 return aSelections
.GetCount();
927 bool wxListBox::IsSelected( int n
) const
929 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
931 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
934 gboolean res
= gtk_tree_model_iter_nth_child(
935 GTK_TREE_MODEL(m_liststore
),
936 &iter
, NULL
, //NULL = parent = get first
939 wxCHECK_MSG( res
, false, wxT("Invalid index") );
941 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
944 void wxListBox::DoSetSelection( int n
, bool select
)
946 return GtkSetSelection(n
, select
, true); //docs say no events here
949 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
951 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
953 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
956 gboolean res
= gtk_tree_model_iter_nth_child(
957 GTK_TREE_MODEL(m_liststore
),
958 &iter
, NULL
, //NULL = parent = get first
961 wxCHECK_RET( res
, wxT("Invalid index") );
963 m_blockEvent
= blockEvent
;
966 gtk_tree_selection_select_iter(selection
, &iter
);
968 gtk_tree_selection_unselect_iter(selection
, &iter
);
970 m_blockEvent
= false;
973 void wxListBox::DoSetFirstItem( int n
)
975 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
976 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
978 //RN: I have no idea why this line is needed...
979 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
983 gtk_tree_model_iter_nth_child(
984 GTK_TREE_MODEL(m_liststore
),
986 NULL
, //NULL = parent = get first
990 GtkTreePath
* path
= gtk_tree_model_get_path(
991 GTK_TREE_MODEL(m_liststore
), &iter
);
993 // Scroll to the desired cell (0.0 == topleft alignment)
994 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
997 gtk_tree_path_free(path
);
1000 // ----------------------------------------------------------------------------
1002 // ----------------------------------------------------------------------------
1004 int wxListBox::DoListHitTest(const wxPoint
& point
) const
1006 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
1007 // we only want visible items we need to check for it manually here
1008 if ( !GetClientRect().Inside(point
) )
1011 // need to translate from master window since it is in client coords
1013 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
1014 &binx
, &biny
, NULL
, NULL
, NULL
);
1017 if ( !gtk_tree_view_get_path_at_pos
1023 NULL
, // [out] column (always 0 here)
1024 NULL
, // [out] x-coord relative to the cell (not interested)
1025 NULL
// [out] y-coord relative to the cell
1031 int index
= gtk_tree_path_get_indices(path
)[0];
1032 gtk_tree_path_free(path
);
1037 // ----------------------------------------------------------------------------
1039 // ----------------------------------------------------------------------------
1042 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1044 // RN: Is this needed anymore?
1045 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
1047 #endif // wxUSE_TOOLTIPS
1049 GtkWidget
*wxListBox::GetConnectWidget()
1051 // the correct widget for listbox events (such as mouse clicks for example)
1052 // is m_treeview, not the parent scrolled window
1053 return GTK_WIDGET(m_treeview
);
1056 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1058 return (window
== gtk_tree_view_get_bin_window(m_treeview
));
1061 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1063 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1065 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1068 m_backgroundColour
.CalcPixel( gdk_drawable_get_colormap( window
) );
1069 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1070 gdk_window_clear( window
);
1074 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
1077 void wxListBox::OnInternalIdle()
1079 //RN: Is this needed anymore?
1080 wxCursor cursor
= m_cursor
;
1081 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1083 if (GTK_WIDGET(m_treeview
)->window
&& cursor
.Ok())
1085 /* I now set the cursor the anew in every OnInternalIdle call
1086 as setting the cursor in a parent window also effects the
1087 windows above so that checking for the current cursor is
1089 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1090 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1093 if (wxUpdateUIEvent::CanUpdate(this))
1094 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1097 wxSize
wxListBox::DoGetBestSize() const
1099 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1101 // Start with a minimum size that's not too small
1103 GetTextExtent( wxT("X"), &cx
, &cy
);
1104 int lbWidth
= 3 * cx
;
1107 // Get the visible area of the tree view (limit to the 10th item
1108 // so that it isn't too big)
1109 unsigned int count
= GetCount();
1114 // Find the widest line
1115 for(unsigned int i
= 0; i
< count
; i
++) {
1116 wxString
str(GetString(i
));
1117 GetTextExtent(str
, &wLine
, NULL
);
1118 lbWidth
= wxMax(lbWidth
, wLine
);
1123 // And just a bit more for the checkbox if present and then some
1124 // (these are rough guesses)
1125 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
1126 if ( m_hasCheckBoxes
)
1129 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1133 // don't make the listbox too tall (limit height to around 10 items) but don't
1134 // make it too small neither
1135 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1138 // Add room for the scrollbar
1139 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1141 wxSize
best(lbWidth
, lbHeight
);
1142 CacheBestSize(best
);
1148 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1150 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1153 #endif // wxUSE_LISTBOX