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
;
47 //-----------------------------------------------------------------------------
48 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
49 //-----------------------------------------------------------------------------
51 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
52 # define WXLISTBOX_DATACOLUMN_ARG(x) (x->m_hasCheckBoxes ? 1 : 0)
54 # define WXLISTBOX_DATACOLUMN_ARG(x) (0)
55 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
57 #define WXLISTBOX_DATACOLUMN WXLISTBOX_DATACOLUMN_ARG(this)
59 //-----------------------------------------------------------------------------
61 //-----------------------------------------------------------------------------
65 gtk_listbox_row_activated_callback(GtkTreeView
*treeview
,
67 GtkTreeViewColumn
*col
,
70 if (g_isIdle
) wxapp_install_idle_handler();
72 if (g_blockEventsOnDrag
) return;
73 if (g_blockEventsOnScroll
) return;
75 if (!listbox
->m_hasVMT
) return;
78 //1) This is triggered by either a double-click or a space press
79 //2) We handle both here because
80 //2a) in the case of a space/keypress we can't really know
81 // which item was pressed on because we can't get coords
83 //2b) It seems more correct
85 int sel
= gtk_tree_path_get_indices(path
)[0];
87 if(!listbox
->m_spacePressed
)
89 //Assume it was double-click
90 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED
, listbox
->GetId() );
91 event
.SetEventObject( listbox
);
93 if(listbox
->IsSelected(sel
))
95 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(sel
);
100 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
102 if ( listbox
->HasClientObjectData() )
103 event
.SetClientObject(
104 (wxClientData
*) gtk_tree_entry_get_userdata(entry
) );
105 else if ( listbox
->HasClientUntypedData() )
106 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
107 g_object_unref(G_OBJECT(entry
));
111 wxLogSysError(wxT("Internal error - could not get entry for double-click"));
118 listbox
->GetEventHandler()->ProcessEvent( event
);
122 listbox
->m_spacePressed
= false; //don't block selection behaviour anymore
124 //Space was pressed - toggle the appropriate checkbox and the selection
125 #if wxUSE_CHECKLISTBOX //Do it for both native and non-native
126 if (listbox
->m_hasCheckBoxes
)
128 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
130 clb
->Check( sel
, !clb
->IsChecked(sel
) );
132 wxCommandEvent
new_event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
133 new_event
.SetEventObject( listbox
);
134 new_event
.SetInt( sel
);
135 listbox
->GetEventHandler()->ProcessEvent( new_event
);
137 #endif // wxUSE_CHECKLISTBOX
139 if( (((listbox
->GetWindowStyleFlag() & wxLB_MULTIPLE
) != 0) ||
140 ((listbox
->GetWindowStyleFlag() & wxLB_EXTENDED
) != 0)) )
142 //toggle the selection + send event
143 listbox
->GtkSetSelection(sel
, !listbox
->IsSelected( sel
), FALSE
);
149 //-----------------------------------------------------------------------------
150 // "button_press_event"
151 //-----------------------------------------------------------------------------
155 gtk_listbox_button_press_callback( GtkWidget
*widget
,
156 GdkEventButton
*gdk_event
,
159 if (g_isIdle
) wxapp_install_idle_handler();
161 if (g_blockEventsOnDrag
) return FALSE
;
162 if (g_blockEventsOnScroll
) return FALSE
;
164 if (!listbox
->m_hasVMT
) return FALSE
;
166 //Just to be on the safe side - it should be unset in the activate callback
167 //but we don't want any obscure bugs if it doesn't get called somehow...
168 listbox
->m_spacePressed
= false;
170 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
171 if ((listbox
->m_hasCheckBoxes
) && (gdk_event
->x
< 15) && (gdk_event
->type
!= GDK_2BUTTON_PRESS
))
174 gtk_tree_view_get_path_at_pos(GTK_TREE_VIEW(widget
),
175 (gint
)gdk_event
->x
, (gint
)gdk_event
->y
,
176 &path
, NULL
, NULL
, NULL
);
177 int sel
= gtk_tree_path_get_indices(path
)[0];
178 gtk_tree_path_free(path
);
180 wxCheckListBox
*clb
= (wxCheckListBox
*)listbox
;
182 clb
->Check( sel
, !clb
->IsChecked(sel
) );
184 wxCommandEvent
event( wxEVT_COMMAND_CHECKLISTBOX_TOGGLED
, listbox
->GetId() );
185 event
.SetEventObject( listbox
);
187 listbox
->GetEventHandler()->ProcessEvent( event
);
189 #endif // wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
195 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
201 gtk_listbox_key_press_callback( GtkWidget
*widget
,
202 GdkEventKey
*gdk_event
,
205 if (g_isIdle
) wxapp_install_idle_handler();
207 if (g_blockEventsOnDrag
) return FALSE
;
212 if ((gdk_event
->keyval
== GDK_Tab
) || (gdk_event
->keyval
== GDK_ISO_Left_Tab
))
214 wxNavigationKeyEvent new_event
;
215 /* GDK reports GDK_ISO_Left_Tab for SHIFT-TAB */
216 new_event
.SetDirection( (gdk_event
->keyval
== GDK_Tab
) );
217 /* CTRL-TAB changes the (parent) window, i.e. switch notebook page */
218 new_event
.SetWindowChange( (gdk_event
->state
& GDK_CONTROL_MASK
) );
219 new_event
.SetCurrentFocus( listbox
);
220 ret
= listbox
->GetEventHandler()->ProcessEvent( new_event
);
223 if ((gdk_event
->keyval
== GDK_Return
) && (!ret
))
225 // eat return in all modes (RN:WHY?)
229 // Check or uncheck item with SPACE
230 if (gdk_event
->keyval
== ' ')
232 //In the keyevent we don't know the index of the item
233 //and the activated event gets called anyway...
235 //Also, activating with the space causes the treeview to
236 //unselect all the items and then select the item in question
237 //wx's behaviour is to just toggle the item's selection state
238 //and leave the others alone
239 listbox
->m_spacePressed
= true;
244 g_signal_stop_emission_by_name (widget
, "key_press_event");
252 //-----------------------------------------------------------------------------
253 // "select" and "deselect"
254 //-----------------------------------------------------------------------------
257 static gboolean
gtk_listitem_select_cb( GtkTreeSelection
* selection
,
260 gboolean is_selected
,
263 if (g_isIdle
) wxapp_install_idle_handler();
265 if (!listbox
->m_hasVMT
) return TRUE
;
266 if (g_blockEventsOnDrag
) return TRUE
;
268 if (listbox
->m_spacePressed
) return FALSE
; //see keyevent callback
269 if (listbox
->m_blockEvent
) return TRUE
;
271 // NB: wxdocs explicitly say that this event only gets sent when
272 // something is actually selected, plus the controls example
273 // assumes so and passes -1 to the dogetclientdata funcs if not
275 // OK, so basically we need to do a bit of a run-around here as
276 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
277 // i.e. if is_selected is FALSE then the item is going to be
278 // selected right now!
279 // 2) However, since it is not already selected and the user
280 // will expect it to be we need to manually select it and
281 // return FALSE telling GTK we handled the selection
282 if (is_selected
) return TRUE
;
284 int nIndex
= gtk_tree_path_get_indices(path
)[0];
285 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(nIndex
);
289 //Now, as mentioned above, we manually select the row that is/was going
290 //to be selected anyway by GTK
291 listbox
->m_blockEvent
= TRUE
; //if we don't block events we will lock the
292 //app due to recursion!!
294 GtkTreeSelection
* selection
=
295 gtk_tree_view_get_selection(listbox
->m_treeview
);
297 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox
->m_liststore
), &iter
, path
);
298 gtk_tree_selection_select_iter(selection
, &iter
);
300 listbox
->m_blockEvent
= FALSE
;
302 //Finally, send the wx event
303 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
304 event
.SetEventObject( listbox
);
306 // indicate whether this is a selection or a deselection
307 event
.SetExtraLong( 1 );
309 event
.SetInt(nIndex
);
310 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
312 if ( listbox
->HasClientObjectData() )
313 event
.SetClientObject(
314 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
316 else if ( listbox
->HasClientUntypedData() )
317 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
319 listbox
->GetEventHandler()->ProcessEvent( event
);
321 g_object_unref(G_OBJECT(entry
));
322 return FALSE
; //We handled it/did it manually
325 return TRUE
; //allow selection to change
329 //-----------------------------------------------------------------------------
330 // GtkTreeEntry destruction (to destroy client data)
331 //-----------------------------------------------------------------------------
334 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
337 if(listbox
->HasClientObjectData())
339 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
341 delete (wxClientData
*)userdata
;
346 //-----------------------------------------------------------------------------
347 // Sorting callback (standard CmpNoCase return value)
348 //-----------------------------------------------------------------------------
351 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
357 GtkTreeEntry
* entry2
;
359 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
361 WXLISTBOX_DATACOLUMN_ARG(listbox
),
363 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
365 WXLISTBOX_DATACOLUMN_ARG(listbox
),
367 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
368 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
370 //We compare collate keys here instead of calling g_utf8_collate
371 //as it is rather slow (and even the docs reccommend this)
372 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
373 gtk_tree_entry_get_collate_key(entry2
));
375 g_object_unref(G_OBJECT(entry
));
376 g_object_unref(G_OBJECT(entry2
));
382 //-----------------------------------------------------------------------------
383 // Searching callback (TRUE == not equal, FALSE == equal)
384 //-----------------------------------------------------------------------------
387 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
395 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
397 WXLISTBOX_DATACOLUMN_ARG(listbox
),
399 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
400 gchar
* keycollatekey
= g_utf8_collate_key(key
, -1);
402 int ret
= strcasecmp(keycollatekey
,
403 gtk_tree_entry_get_collate_key(entry
));
405 g_free(keycollatekey
);
406 g_object_unref(G_OBJECT(entry
));
412 //-----------------------------------------------------------------------------
414 //-----------------------------------------------------------------------------
416 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
418 // ----------------------------------------------------------------------------
420 // ----------------------------------------------------------------------------
422 void wxListBox::Init()
424 m_treeview
= (GtkTreeView
*) NULL
;
425 #if wxUSE_CHECKLISTBOX
426 m_hasCheckBoxes
= FALSE
;
427 #endif // wxUSE_CHECKLISTBOX
428 m_spacePressed
= false;
431 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
432 const wxPoint
&pos
, const wxSize
&size
,
433 const wxArrayString
& choices
,
434 long style
, const wxValidator
& validator
,
435 const wxString
&name
)
437 wxCArrayString
chs(choices
);
439 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
440 style
, validator
, name
);
443 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
444 const wxPoint
&pos
, const wxSize
&size
,
445 int n
, const wxString choices
[],
446 long style
, const wxValidator
& validator
,
447 const wxString
&name
)
450 m_acceptsFocus
= TRUE
;
451 m_blockEvent
= FALSE
;
453 if (!PreCreation( parent
, pos
, size
) ||
454 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
456 wxFAIL_MSG( wxT("wxListBox creation failed") );
460 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
461 if (style
& wxLB_ALWAYS_SB
)
463 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
464 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
468 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
469 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
472 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
474 //wxListBox doesn't have a header :)
475 //NB: If enabled SetFirstItem doesn't work correctly
476 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
478 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
480 ((wxCheckListBox
*)this)->DoCreateCheckList();
481 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
483 // Create the data column
484 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
485 gtk_cell_renderer_text_new(),
487 WXLISTBOX_DATACOLUMN
, NULL
);
489 // Now create+set the model (GtkListStore) - first argument # of columns
490 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
492 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
493 GTK_TYPE_TREE_ENTRY
);
496 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
498 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
500 g_object_unref(G_OBJECT(m_liststore
)); //free on treeview destruction
502 // Disable the pop-up textctrl that enables searching - note that
503 // the docs specify that even if this disabled (which we are doing)
504 // the user can still have it through the start-interactive-search
505 // key binding...either way we want to provide a searchequal callback
506 // NB: If this is enabled a doubleclick event (activate) gets sent
507 // on a successful search
508 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
509 gtk_tree_view_set_search_equal_func(m_treeview
,
510 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
514 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
517 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
518 gtk_tree_selection_set_select_function(selection
,
519 (GtkTreeSelectionFunc
)gtk_listitem_select_cb
,
520 this, NULL
); //NULL == destroycb
522 GtkSelectionMode mode
;
523 if (style
& wxLB_MULTIPLE
)
525 mode
= GTK_SELECTION_MULTIPLE
;
527 else if (style
& wxLB_EXTENDED
)
529 mode
= GTK_SELECTION_EXTENDED
;
533 // if style was 0 set single mode
534 m_windowStyle
|= wxLB_SINGLE
;
535 mode
= GTK_SELECTION_SINGLE
;
538 gtk_tree_selection_set_mode( selection
, mode
);
540 //Handle sortable stuff
541 if(style
& wxLB_SORT
)
543 //Setup sorting in ascending (wx) order
544 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
545 WXLISTBOX_DATACOLUMN
,
548 //Set the sort callback
549 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
550 WXLISTBOX_DATACOLUMN
,
551 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
553 NULL
//"destroy notifier"
558 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
560 gtk_widget_show( GTK_WIDGET(m_treeview
) );
562 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
564 //treeview-specific events
565 g_signal_connect(m_treeview
, "row-activated",
566 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
569 g_signal_connect (m_treeview
, "button_press_event",
570 G_CALLBACK (gtk_listbox_button_press_callback
),
572 g_signal_connect (m_treeview
, "key_press_event",
573 G_CALLBACK (gtk_listbox_key_press_callback
),
576 m_parent
->DoAddChild( this );
579 SetBestSize(size
); // need this too because this is a wxControlWithItems
584 wxListBox::~wxListBox()
591 // ----------------------------------------------------------------------------
593 // ----------------------------------------------------------------------------
595 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
596 void** clientData
, int pos
)
598 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
600 InvalidateBestSize();
602 // Create and set column ids and GValues
604 size_t nNum
= items
.GetCount();
605 int nCurCount
= wxListBox::GetCount();
606 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
608 GtkTreeIter
* pIter
= NULL
; // append by default
610 if (pos
!= nCurCount
)
612 gboolean res
= gtk_tree_model_iter_nth_child(
613 GTK_TREE_MODEL(m_liststore
),
614 &iter
, NULL
, //NULL = parent = get first
618 wxLogSysError(wxT("internal wxListBox error in insertion"));
625 for (size_t i
= 0; i
< nNum
; ++i
)
627 wxString label
= items
[i
];
629 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
632 label
.Prepend(wxCHECKLBOX_STRING
);
634 #endif // wxUSE_CHECKLISTBOX
637 GtkTreeEntry
* entry
= gtk_tree_entry_new();
638 gtk_tree_entry_set_label(entry
, wxConvUTF8
.cWX2MB(label
));
639 gtk_tree_entry_set_destroy_func(entry
,
640 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
644 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
647 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
649 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
652 gtk_list_store_set(m_liststore
, &itercur
,
653 0, FALSE
, //FALSE == not toggled
658 gtk_list_store_set(m_liststore
, &itercur
,
661 g_object_unref(G_OBJECT(entry
)); //liststore always refs :)
665 void wxListBox::DoInsertItems(const wxArrayString
& items
, int pos
)
667 GtkInsertItems(items
, NULL
, pos
);
670 int wxListBox::DoAppend( const wxString
& item
)
672 // Call DoInsertItems
673 int nWhere
= wxListBox::GetCount();
674 wxArrayString aItems
;
676 wxListBox::DoInsertItems(aItems
, nWhere
);
680 void wxListBox::DoSetItems( const wxArrayString
& items
,
684 GtkInsertItems(items
, clientData
, 0);
687 // ----------------------------------------------------------------------------
689 // ----------------------------------------------------------------------------
691 void wxListBox::Clear()
693 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
695 InvalidateBestSize();
697 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
700 void wxListBox::Delete( int n
)
702 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
704 InvalidateBestSize();
707 gboolean res
= gtk_tree_model_iter_nth_child(
708 GTK_TREE_MODEL(m_liststore
),
709 &iter
, NULL
, //NULL = parent = get first
713 wxCHECK_RET( res
, wxT("wrong listbox index") );
715 //this returns false if iter is invalid (i.e. deleting item
716 //at end) but since we don't use iter, well... :)
717 gtk_list_store_remove(m_liststore
, &iter
);
720 // ----------------------------------------------------------------------------
721 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
722 // ----------------------------------------------------------------------------
724 struct _GtkTreeEntry
* wxListBox::GtkGetEntry(int n
) const
727 gboolean res
= gtk_tree_model_iter_nth_child(
728 GTK_TREE_MODEL(m_liststore
),
729 &iter
, NULL
, //NULL = parent = get first
734 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
735 wxT("Passed in value was:[%i] List size:[%i]"),
736 n
, wxListBox::GetCount() );
741 GtkTreeEntry
* entry
= NULL
;
742 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
743 WXLISTBOX_DATACOLUMN
, &entry
, -1);
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 void* wxListBox::DoGetItemClientData( int n
) const
754 wxCHECK_MSG( n
>= 0 && n
< wxListBox::GetCount(), NULL
,
755 wxT("Invalid index passed to GetItemClientData") );
757 GtkTreeEntry
* entry
= GtkGetEntry(n
);
758 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
760 void* userdata
= gtk_tree_entry_get_userdata( entry
);
761 g_object_unref(G_OBJECT(entry
));
765 wxClientData
* wxListBox::DoGetItemClientObject( int n
) const
767 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
770 void wxListBox::DoSetItemClientData( int n
, void* clientData
)
772 wxCHECK_RET( n
>= 0 && n
< wxListBox::GetCount(),
773 wxT("Invalid index passed to SetItemClientData") );
775 GtkTreeEntry
* entry
= GtkGetEntry(n
);
776 wxCHECK_RET(entry
, wxT("could not get entry"));
778 gtk_tree_entry_set_userdata( entry
, clientData
);
779 g_object_unref(G_OBJECT(entry
));
782 void wxListBox::DoSetItemClientObject( int n
, wxClientData
* clientData
)
784 // wxItemContainer already deletes data for us
785 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
788 // ----------------------------------------------------------------------------
789 // string list access
790 // ----------------------------------------------------------------------------
792 void wxListBox::SetString( int n
, const wxString
&string
)
794 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
796 GtkTreeEntry
* entry
= GtkGetEntry(n
);
797 wxCHECK_RET( entry
, wxT("wrong listbox index") );
799 wxString label
= string
;
801 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
803 label
.Prepend(wxCHECKLBOX_STRING
);
804 #endif // wxUSE_CHECKLISTBOX
806 // RN: This may look wierd but the problem is that the TreeView
807 // doesn't resort or update when changed above and there is no real
808 // notification function...
809 void* userdata
= gtk_tree_entry_get_userdata(entry
);
810 gtk_tree_entry_set_userdata(entry
, NULL
); //don't delete on destroy
811 g_object_unref(G_OBJECT(entry
));
813 bool bWasSelected
= wxListBox::IsSelected(n
);
814 wxListBox::Delete(n
);
816 wxArrayString aItems
;
818 GtkInsertItems(aItems
, &userdata
, n
);
820 wxListBox::GtkSetSelection(n
, TRUE
, TRUE
);
823 wxString
wxListBox::GetString( int n
) const
825 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
827 GtkTreeEntry
* entry
= GtkGetEntry(n
);
828 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
830 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
832 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
833 // checklistboxes have "[±] " prepended to their lables, remove it
835 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
836 if ( m_hasCheckBoxes
)
838 #endif // wxUSE_CHECKLISTBOX
840 g_object_unref(G_OBJECT(entry
));
844 int wxListBox::GetCount() const
846 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
848 return gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
851 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
853 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
855 //Sort of hackish - maybe there is a faster way
856 int nCount
= wxListBox::GetCount();
858 for(int i
= 0; i
< nCount
; ++i
)
860 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
865 // it's not an error if the string is not found -> no wxCHECK
869 // ----------------------------------------------------------------------------
871 // ----------------------------------------------------------------------------
873 int wxListBox::GetSelection() const
875 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox"));
876 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), -1,
877 wxT("must be single selection listbox"));
880 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
882 // only works on single-sel
883 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
887 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
889 int sel
= gtk_tree_path_get_indices(path
)[0];
891 gtk_tree_path_free(path
);
896 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
898 wxCHECK_MSG( m_treeview
!= NULL
, -1, wxT("invalid listbox") );
904 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
906 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
907 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
910 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
914 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
917 return aSelections
.GetCount();
920 bool wxListBox::IsSelected( int n
) const
922 wxCHECK_MSG( m_treeview
!= NULL
, FALSE
, wxT("invalid listbox") );
924 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
927 gboolean res
= gtk_tree_model_iter_nth_child(
928 GTK_TREE_MODEL(m_liststore
),
929 &iter
, NULL
, //NULL = parent = get first
932 wxCHECK_MSG( res
, FALSE
, wxT("Invalid index") );
934 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
937 void wxListBox::DoSetSelection( int n
, bool select
)
939 return GtkSetSelection(n
, select
, TRUE
); //docs say no events here
942 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
944 wxCHECK_RET( m_treeview
!= NULL
, 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_RET( res
, wxT("Invalid index") );
956 m_blockEvent
= blockEvent
;
959 gtk_tree_selection_select_iter(selection
, &iter
);
961 gtk_tree_selection_unselect_iter(selection
, &iter
);
963 m_blockEvent
= FALSE
;
966 void wxListBox::DoSetFirstItem( int n
)
968 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
969 wxCHECK_RET( n
>= 0 && n
< wxListBox::GetCount(), wxT("invalid index"));
971 //RN: I have no idea why this line is needed...
972 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
976 gtk_tree_model_iter_nth_child(
977 GTK_TREE_MODEL(m_liststore
),
979 NULL
, //NULL = parent = get first
983 GtkTreePath
* path
= gtk_tree_model_get_path(
984 GTK_TREE_MODEL(m_liststore
), &iter
);
986 // Scroll to the desired cell (0.0 == topleft alignment)
987 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
990 gtk_tree_path_free(path
);
993 // ----------------------------------------------------------------------------
995 // ----------------------------------------------------------------------------
997 int wxListBox::DoListHitTest(const wxPoint
& point
) const
999 // need to translate from master window since it is in client coords
1001 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
1002 &binx
, &biny
, NULL
, NULL
, NULL
);
1005 if ( !gtk_tree_view_get_path_at_pos
1011 NULL
, // [out] column (always 0 here)
1012 NULL
, // [out] x-coord relative to the cell (not interested)
1013 NULL
// [out] y-coord relative to the cell
1019 int index
= gtk_tree_path_get_indices(path
)[0];
1020 gtk_tree_path_free(path
);
1025 // ----------------------------------------------------------------------------
1027 // ----------------------------------------------------------------------------
1030 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1032 // RN: Is this needed anymore?
1033 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
1035 #endif // wxUSE_TOOLTIPS
1037 GtkWidget
*wxListBox::GetConnectWidget()
1039 // the correct widget for listbox events (such as mouse clicks for example)
1040 // is m_treeview, not the parent scrolled window
1041 return GTK_WIDGET(m_treeview
);
1044 bool wxListBox::IsOwnGtkWindow( GdkWindow
*window
)
1046 return (window
== gtk_tree_view_get_bin_window(m_treeview
));
1049 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1051 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1053 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1056 m_backgroundColour
.CalcPixel( gdk_window_get_colormap( window
) );
1057 gdk_window_set_background( window
, m_backgroundColour
.GetColor() );
1058 gdk_window_clear( window
);
1062 gtk_widget_modify_style( GTK_WIDGET(m_treeview
), style
);
1065 void wxListBox::OnInternalIdle()
1067 //RN: Is this needed anymore?
1068 wxCursor cursor
= m_cursor
;
1069 if (g_globalCursor
.Ok()) cursor
= g_globalCursor
;
1071 if (GTK_WIDGET(m_treeview
)->window
&& cursor
.Ok())
1073 /* I now set the cursor the anew in every OnInternalIdle call
1074 as setting the cursor in a parent window also effects the
1075 windows above so that checking for the current cursor is
1077 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1078 gdk_window_set_cursor( window
, cursor
.GetCursor() );
1081 if (wxUpdateUIEvent::CanUpdate(this))
1082 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
1085 wxSize
wxListBox::DoGetBestSize() const
1087 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1089 // Start with a minimum size that's not too small
1091 GetTextExtent( wxT("X"), &cx
, &cy
);
1092 int lbWidth
= 3 * cx
;
1095 // Get the visible area of the tree view (limit to the 10th item
1096 // so that it isn't too big)
1097 int count
= GetCount();
1102 // Find the widest line
1103 for(int i
= 0; i
< count
; i
++) {
1104 wxString
str(GetString(i
));
1105 GetTextExtent(str
, &wLine
, NULL
);
1106 lbWidth
= wxMax(lbWidth
, wLine
);
1111 // And just a bit more for the checkbox if present and then some
1112 // (these are rough guesses)
1113 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
1114 if ( m_hasCheckBoxes
)
1117 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1121 // don't make the listbox too tall (limit height to around 10 items) but don't
1122 // make it too small neither
1123 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1126 // Add room for the scrollbar
1127 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1129 wxSize
best(lbWidth
, lbHeight
);
1130 CacheBestSize(best
);
1136 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1138 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1141 #endif // wxUSE_LISTBOX