1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/listbox.cpp
4 // Author: Robert Roebling
5 // Modified By: Ryan Norton (GtkTreeView implementation)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
16 #include "wx/listbox.h"
19 #include "wx/dynarray.h"
23 #include "wx/settings.h"
24 #include "wx/checklst.h"
25 #include "wx/arrstr.h"
28 #include "wx/gtk/private.h"
29 #include "wx/gtk/treeentry_gtk.h"
32 #include "wx/tooltip.h"
37 #include <gdk/gdkkeysyms.h>
39 //-----------------------------------------------------------------------------
41 //-----------------------------------------------------------------------------
43 extern bool g_blockEventsOnDrag
;
44 extern bool g_blockEventsOnScroll
;
48 //-----------------------------------------------------------------------------
49 // Macro to tell which row the strings are in (1 if native checklist, 0 if not)
50 //-----------------------------------------------------------------------------
52 #if wxUSE_CHECKLISTBOX && 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 (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 // don't need to install idle handler, its done from "event" signal
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 // don't need to install idle handler, its done from "event" signal
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;
247 //-----------------------------------------------------------------------------
248 // "select" and "deselect"
249 //-----------------------------------------------------------------------------
252 static gboolean
gtk_listitem_select_cb( GtkTreeSelection
* selection
,
255 gboolean is_selected
,
258 if (g_isIdle
) wxapp_install_idle_handler();
260 if (!listbox
->m_hasVMT
) return TRUE
;
261 if (g_blockEventsOnDrag
) return TRUE
;
263 if (listbox
->m_spacePressed
) return FALSE
; //see keyevent callback
264 if (listbox
->m_blockEvent
) return TRUE
;
266 // NB: wxdocs explicitly say that this event only gets sent when
267 // something is actually selected, plus the controls example
268 // assumes so and passes -1 to the dogetclientdata funcs if not
270 // OK, so basically we need to do a bit of a run-around here as
271 // 1) is_selected says whether the item(s?) are CURRENTLY selected -
272 // i.e. if is_selected is FALSE then the item is going to be
273 // selected right now!
274 // 2) However, since it is not already selected and the user
275 // will expect it to be we need to manually select it and
276 // return FALSE telling GTK we handled the selection
277 if (is_selected
) return TRUE
;
279 int nIndex
= gtk_tree_path_get_indices(path
)[0];
280 GtkTreeEntry
* entry
= listbox
->GtkGetEntry(nIndex
);
284 //Now, as mentioned above, we manually select the row that is/was going
285 //to be selected anyway by GTK
286 listbox
->m_blockEvent
= true; //if we don't block events we will lock the
287 //app due to recursion!!
289 GtkTreeSelection
* selection
=
290 gtk_tree_view_get_selection(listbox
->m_treeview
);
292 gtk_tree_model_get_iter(GTK_TREE_MODEL(listbox
->m_liststore
), &iter
, path
);
293 gtk_tree_selection_select_iter(selection
, &iter
);
295 listbox
->m_blockEvent
= false;
297 //Finally, send the wx event
298 wxCommandEvent
event(wxEVT_COMMAND_LISTBOX_SELECTED
, listbox
->GetId() );
299 event
.SetEventObject( listbox
);
301 // indicate whether this is a selection or a deselection
302 event
.SetExtraLong( 1 );
304 event
.SetInt(nIndex
);
305 event
.SetString(wxConvUTF8
.cMB2WX(gtk_tree_entry_get_label(entry
)));
307 if ( listbox
->HasClientObjectData() )
308 event
.SetClientObject(
309 (wxClientData
*) gtk_tree_entry_get_userdata(entry
)
311 else if ( listbox
->HasClientUntypedData() )
312 event
.SetClientData( gtk_tree_entry_get_userdata(entry
) );
314 listbox
->GetEventHandler()->ProcessEvent( event
);
316 g_object_unref (entry
);
317 return FALSE
; //We handled it/did it manually
320 return TRUE
; //allow selection to change
324 //-----------------------------------------------------------------------------
325 // GtkTreeEntry destruction (to destroy client data)
326 //-----------------------------------------------------------------------------
329 static void gtk_tree_entry_destroy_cb(GtkTreeEntry
* entry
,
332 if(listbox
->HasClientObjectData())
334 gpointer userdata
= gtk_tree_entry_get_userdata(entry
);
336 delete (wxClientData
*)userdata
;
341 //-----------------------------------------------------------------------------
342 // Sorting callback (standard CmpNoCase return value)
343 //-----------------------------------------------------------------------------
346 static gint
gtk_listbox_sort_callback(GtkTreeModel
*model
,
352 GtkTreeEntry
* entry2
;
354 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
356 WXLISTBOX_DATACOLUMN_ARG(listbox
),
358 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
360 WXLISTBOX_DATACOLUMN_ARG(listbox
),
362 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
363 wxCHECK_MSG(entry2
, 0, wxT("Could not get entry2"));
365 //We compare collate keys here instead of calling g_utf8_collate
366 //as it is rather slow (and even the docs reccommend this)
367 int ret
= strcasecmp(gtk_tree_entry_get_collate_key(entry
),
368 gtk_tree_entry_get_collate_key(entry2
));
370 g_object_unref (entry
);
371 g_object_unref (entry2
);
377 //-----------------------------------------------------------------------------
378 // Searching callback (TRUE == not equal, FALSE == equal)
379 //-----------------------------------------------------------------------------
382 static gboolean
gtk_listbox_searchequal_callback(GtkTreeModel
* model
,
390 gtk_tree_model_get(GTK_TREE_MODEL(listbox
->m_liststore
),
392 WXLISTBOX_DATACOLUMN_ARG(listbox
),
394 wxCHECK_MSG(entry
, 0, wxT("Could not get entry"));
395 gchar
* keycollatekey
= g_utf8_collate_key(key
, -1);
397 int ret
= strcasecmp(keycollatekey
,
398 gtk_tree_entry_get_collate_key(entry
));
400 g_free(keycollatekey
);
401 g_object_unref (entry
);
407 //-----------------------------------------------------------------------------
409 //-----------------------------------------------------------------------------
411 IMPLEMENT_DYNAMIC_CLASS(wxListBox
, wxControl
)
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 void wxListBox::Init()
419 m_treeview
= (GtkTreeView
*) NULL
;
420 #if wxUSE_CHECKLISTBOX
421 m_hasCheckBoxes
= false;
422 #endif // wxUSE_CHECKLISTBOX
423 m_spacePressed
= false;
426 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
427 const wxPoint
&pos
, const wxSize
&size
,
428 const wxArrayString
& choices
,
429 long style
, const wxValidator
& validator
,
430 const wxString
&name
)
432 wxCArrayString
chs(choices
);
434 return Create( parent
, id
, pos
, size
, chs
.GetCount(), chs
.GetStrings(),
435 style
, validator
, name
);
438 bool wxListBox::Create( wxWindow
*parent
, wxWindowID id
,
439 const wxPoint
&pos
, const wxSize
&size
,
440 int n
, const wxString choices
[],
441 long style
, const wxValidator
& validator
,
442 const wxString
&name
)
445 m_acceptsFocus
= true;
446 m_blockEvent
= false;
448 if (!PreCreation( parent
, pos
, size
) ||
449 !CreateBase( parent
, id
, pos
, size
, style
, validator
, name
))
451 wxFAIL_MSG( wxT("wxListBox creation failed") );
455 m_widget
= gtk_scrolled_window_new( (GtkAdjustment
*) NULL
, (GtkAdjustment
*) NULL
);
456 if (style
& wxLB_ALWAYS_SB
)
458 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
459 GTK_POLICY_AUTOMATIC
, GTK_POLICY_ALWAYS
);
463 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget
),
464 GTK_POLICY_AUTOMATIC
, GTK_POLICY_AUTOMATIC
);
468 GtkScrolledWindowSetBorder(m_widget
, style
);
470 m_treeview
= GTK_TREE_VIEW( gtk_tree_view_new( ) );
472 //wxListBox doesn't have a header :)
473 //NB: If enabled SetFirstItem doesn't work correctly
474 gtk_tree_view_set_headers_visible(m_treeview
, FALSE
);
476 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
478 ((wxCheckListBox
*)this)->DoCreateCheckList();
479 #endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
481 // Create the data column
482 gtk_tree_view_insert_column_with_attributes(m_treeview
, -1, "",
483 gtk_cell_renderer_text_new(),
485 WXLISTBOX_DATACOLUMN
, NULL
);
487 // Now create+set the model (GtkListStore) - first argument # of columns
488 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
490 m_liststore
= gtk_list_store_new(2, G_TYPE_BOOLEAN
,
491 GTK_TYPE_TREE_ENTRY
);
494 m_liststore
= gtk_list_store_new(1, GTK_TYPE_TREE_ENTRY
);
496 gtk_tree_view_set_model(m_treeview
, GTK_TREE_MODEL(m_liststore
));
498 g_object_unref (m_liststore
); //free on treeview destruction
500 // Disable the pop-up textctrl that enables searching - note that
501 // the docs specify that even if this disabled (which we are doing)
502 // the user can still have it through the start-interactive-search
503 // key binding...either way we want to provide a searchequal callback
504 // NB: If this is enabled a doubleclick event (activate) gets sent
505 // on a successful search
506 gtk_tree_view_set_search_column(m_treeview
, WXLISTBOX_DATACOLUMN
);
507 gtk_tree_view_set_search_equal_func(m_treeview
,
508 (GtkTreeViewSearchEqualFunc
) gtk_listbox_searchequal_callback
,
512 gtk_tree_view_set_enable_search(m_treeview
, FALSE
);
515 GtkTreeSelection
* selection
= gtk_tree_view_get_selection( m_treeview
);
516 gtk_tree_selection_set_select_function(selection
,
517 (GtkTreeSelectionFunc
)gtk_listitem_select_cb
,
518 this, NULL
); //NULL == destroycb
520 GtkSelectionMode mode
;
521 if (style
& wxLB_MULTIPLE
)
523 mode
= GTK_SELECTION_MULTIPLE
;
525 else if (style
& wxLB_EXTENDED
)
527 mode
= GTK_SELECTION_EXTENDED
;
531 // if style was 0 set single mode
532 m_windowStyle
|= wxLB_SINGLE
;
533 mode
= GTK_SELECTION_SINGLE
;
536 gtk_tree_selection_set_mode( selection
, mode
);
538 //Handle sortable stuff
539 if(style
& wxLB_SORT
)
541 //Setup sorting in ascending (wx) order
542 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore
),
543 WXLISTBOX_DATACOLUMN
,
546 //Set the sort callback
547 gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore
),
548 WXLISTBOX_DATACOLUMN
,
549 (GtkTreeIterCompareFunc
) gtk_listbox_sort_callback
,
551 NULL
//"destroy notifier"
556 gtk_container_add (GTK_CONTAINER (m_widget
), GTK_WIDGET(m_treeview
) );
558 gtk_widget_show( GTK_WIDGET(m_treeview
) );
559 m_focusWidget
= GTK_WIDGET(m_treeview
);
561 wxListBox::DoInsertItems(wxArrayString(n
, choices
), 0); // insert initial items
563 //treeview-specific events
564 g_signal_connect(m_treeview
, "row-activated",
565 G_CALLBACK(gtk_listbox_row_activated_callback
), this);
568 g_signal_connect (m_treeview
, "button_press_event",
569 G_CALLBACK (gtk_listbox_button_press_callback
),
571 g_signal_connect (m_treeview
, "key_press_event",
572 G_CALLBACK (gtk_listbox_key_press_callback
),
575 m_parent
->DoAddChild( this );
578 SetBestSize(size
); // need this too because this is a wxControlWithItems
583 wxListBox::~wxListBox()
590 // ----------------------------------------------------------------------------
592 // ----------------------------------------------------------------------------
594 void wxListBox::GtkInsertItems(const wxArrayString
& items
,
595 void** clientData
, unsigned int pos
)
597 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
599 InvalidateBestSize();
601 // Create and set column ids and GValues
603 unsigned int nNum
= items
.GetCount();
604 unsigned int nCurCount
= wxListBox::GetCount();
605 wxASSERT_MSG(pos
<= nCurCount
, wxT("Invalid index passed to wxListBox"));
607 GtkTreeIter
* pIter
= NULL
; // append by default
609 if (pos
!= nCurCount
)
611 gboolean res
= gtk_tree_model_iter_nth_child(
612 GTK_TREE_MODEL(m_liststore
),
613 &iter
, NULL
, //NULL = parent = get first
617 wxLogSysError(wxT("internal wxListBox error in insertion"));
624 for (unsigned int i
= 0; i
< nNum
; ++i
)
626 wxString label
= items
[i
];
628 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
631 label
.Prepend(wxCHECKLBOX_STRING
);
633 #endif // wxUSE_CHECKLISTBOX
636 GtkTreeEntry
* entry
= gtk_tree_entry_new();
637 gtk_tree_entry_set_label(entry
, wxConvUTF8
.cWX2MB(label
));
638 gtk_tree_entry_set_destroy_func(entry
,
639 (GtkTreeEntryDestroy
)gtk_tree_entry_destroy_cb
,
643 gtk_tree_entry_set_userdata(entry
, clientData
[i
]);
646 gtk_list_store_insert_before(m_liststore
, &itercur
, pIter
);
648 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
651 gtk_list_store_set(m_liststore
, &itercur
,
652 0, FALSE
, //FALSE == not toggled
657 gtk_list_store_set(m_liststore
, &itercur
,
660 g_object_unref (entry
); //liststore always refs :)
664 void wxListBox::DoInsertItems(const wxArrayString
& items
, unsigned int pos
)
666 wxCHECK_RET( IsValidInsert(pos
), wxT("invalid index in wxListBox::InsertItems") );
668 GtkInsertItems(items
, NULL
, pos
);
671 int wxListBox::DoAppend( const wxString
& item
)
673 // Call DoInsertItems
674 unsigned int nWhere
= wxListBox::GetCount();
675 wxArrayString aItems
;
677 wxListBox::DoInsertItems(aItems
, nWhere
);
681 void wxListBox::DoSetItems( const wxArrayString
& items
,
685 GtkInsertItems(items
, clientData
, 0);
688 // ----------------------------------------------------------------------------
690 // ----------------------------------------------------------------------------
692 void wxListBox::Clear()
694 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
696 InvalidateBestSize();
698 gtk_list_store_clear( m_liststore
); /* well, THAT was easy :) */
701 void wxListBox::Delete(unsigned int n
)
703 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
705 InvalidateBestSize();
708 gboolean res
= gtk_tree_model_iter_nth_child(
709 GTK_TREE_MODEL(m_liststore
),
710 &iter
, NULL
, //NULL = parent = get first
714 wxCHECK_RET( res
, wxT("wrong listbox index") );
716 //this returns false if iter is invalid (i.e. deleting item
717 //at end) but since we don't use iter, well... :)
718 gtk_list_store_remove(m_liststore
, &iter
);
721 // ----------------------------------------------------------------------------
722 // get GtkTreeEntry from position (note: you need to g_unref it if valid)
723 // ----------------------------------------------------------------------------
725 struct _GtkTreeEntry
* wxListBox::GtkGetEntry(int n
) const
728 gboolean res
= gtk_tree_model_iter_nth_child(
729 GTK_TREE_MODEL(m_liststore
),
730 &iter
, NULL
, //NULL = parent = get first
735 wxLogDebug(wxT("gtk_tree_model_iter_nth_child failed\n")
736 wxT("Passed in value was:[%i] List size:[%u]"),
737 n
, wxListBox::GetCount() );
742 GtkTreeEntry
* entry
= NULL
;
743 gtk_tree_model_get(GTK_TREE_MODEL(m_liststore
), &iter
,
744 WXLISTBOX_DATACOLUMN
, &entry
, -1);
749 // ----------------------------------------------------------------------------
751 // ----------------------------------------------------------------------------
753 void* wxListBox::DoGetItemClientData(unsigned int n
) const
755 wxCHECK_MSG( IsValid(n
), NULL
,
756 wxT("Invalid index passed to GetItemClientData") );
758 GtkTreeEntry
* entry
= GtkGetEntry(n
);
759 wxCHECK_MSG(entry
, NULL
, wxT("could not get entry"));
761 void* userdata
= gtk_tree_entry_get_userdata( entry
);
762 g_object_unref (entry
);
766 wxClientData
* wxListBox::DoGetItemClientObject(unsigned int n
) const
768 return (wxClientData
*) wxListBox::DoGetItemClientData(n
);
771 void wxListBox::DoSetItemClientData(unsigned int n
, void* clientData
)
773 wxCHECK_RET( IsValid(n
),
774 wxT("Invalid index passed to SetItemClientData") );
776 GtkTreeEntry
* entry
= GtkGetEntry(n
);
777 wxCHECK_RET(entry
, wxT("could not get entry"));
779 gtk_tree_entry_set_userdata( entry
, clientData
);
780 g_object_unref (entry
);
783 void wxListBox::DoSetItemClientObject(unsigned int n
, wxClientData
* clientData
)
785 // wxItemContainer already deletes data for us
786 wxListBox::DoSetItemClientData(n
, (void*) clientData
);
789 // ----------------------------------------------------------------------------
790 // string list access
791 // ----------------------------------------------------------------------------
793 void wxListBox::SetString(unsigned int n
, const wxString
&string
)
795 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetString") );
796 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
798 GtkTreeEntry
* entry
= GtkGetEntry(n
);
799 wxCHECK_RET( entry
, wxT("wrong listbox index") );
801 wxString label
= string
;
803 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
805 label
.Prepend(wxCHECKLBOX_STRING
);
806 #endif // wxUSE_CHECKLISTBOX
808 // RN: This may look wierd but the problem is that the TreeView
809 // doesn't resort or update when changed above and there is no real
810 // notification function...
811 void* userdata
= gtk_tree_entry_get_userdata(entry
);
812 gtk_tree_entry_set_userdata(entry
, NULL
); //don't delete on destroy
813 g_object_unref (entry
);
815 bool bWasSelected
= wxListBox::IsSelected(n
);
816 wxListBox::Delete(n
);
818 wxArrayString aItems
;
820 GtkInsertItems(aItems
, &userdata
, n
);
822 wxListBox::GtkSetSelection(n
, true, true);
825 wxString
wxListBox::GetString(unsigned int n
) const
827 wxCHECK_MSG( m_treeview
!= NULL
, wxEmptyString
, wxT("invalid listbox") );
829 GtkTreeEntry
* entry
= GtkGetEntry(n
);
830 wxCHECK_MSG( entry
, wxEmptyString
, wxT("wrong listbox index") );
832 wxString label
= wxGTK_CONV_BACK( gtk_tree_entry_get_label(entry
) );
834 #if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
835 // checklistboxes have "[±] " prepended to their lables, remove it
837 // NB: 4 below is the length of wxCHECKLBOX_STRING from wx/gtk/checklst.h
838 if ( m_hasCheckBoxes
)
840 #endif // wxUSE_CHECKLISTBOX
842 g_object_unref (entry
);
846 unsigned int wxListBox::GetCount() const
848 wxCHECK_MSG( m_treeview
!= NULL
, 0, wxT("invalid listbox") );
850 return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore
), NULL
);
853 int wxListBox::FindString( const wxString
&item
, bool bCase
) const
855 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
857 //Sort of hackish - maybe there is a faster way
858 unsigned int nCount
= wxListBox::GetCount();
860 for(unsigned int i
= 0; i
< nCount
; ++i
)
862 if( item
.IsSameAs( wxListBox::GetString(i
), bCase
) )
867 // it's not an error if the string is not found -> no wxCHECK
871 // ----------------------------------------------------------------------------
873 // ----------------------------------------------------------------------------
875 int wxListBox::GetSelection() const
877 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox"));
878 wxCHECK_MSG( HasFlag(wxLB_SINGLE
), wxNOT_FOUND
,
879 wxT("must be single selection listbox"));
882 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
884 // only works on single-sel
885 if (!gtk_tree_selection_get_selected(selection
, NULL
, &iter
))
889 gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore
), &iter
);
891 int sel
= gtk_tree_path_get_indices(path
)[0];
893 gtk_tree_path_free(path
);
898 int wxListBox::GetSelections( wxArrayInt
& aSelections
) const
900 wxCHECK_MSG( m_treeview
!= NULL
, wxNOT_FOUND
, wxT("invalid listbox") );
906 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
908 if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore
), &iter
))
909 { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
912 if (gtk_tree_selection_iter_is_selected(selection
, &iter
))
916 } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore
), &iter
));
919 return aSelections
.GetCount();
922 bool wxListBox::IsSelected( int n
) const
924 wxCHECK_MSG( m_treeview
!= NULL
, false, wxT("invalid listbox") );
926 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
929 gboolean res
= gtk_tree_model_iter_nth_child(
930 GTK_TREE_MODEL(m_liststore
),
931 &iter
, NULL
, //NULL = parent = get first
934 wxCHECK_MSG( res
, false, wxT("Invalid index") );
936 return gtk_tree_selection_iter_is_selected(selection
, &iter
);
939 void wxListBox::DoSetSelection( int n
, bool select
)
941 // passing -1 to SetSelection() is documented to deselect all items
942 if ( n
== wxNOT_FOUND
)
944 // ... and not generate any events in the process
948 wxCHECK_RET( IsValid(n
), wxT("invalid index in wxListBox::SetSelection") );
950 // don't generate the selection event
951 GtkSetSelection(n
, select
, true);
954 void wxListBox::GtkDeselectAll()
956 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
958 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
962 gtk_tree_selection_unselect_all(selection
);
964 m_blockEvent
= false;
967 void wxListBox::GtkSetSelection(int n
, const bool select
, const bool blockEvent
)
969 wxCHECK_RET( m_treeview
!= NULL
, wxT("invalid listbox") );
971 GtkTreeSelection
* selection
= gtk_tree_view_get_selection(m_treeview
);
974 gboolean res
= gtk_tree_model_iter_nth_child(
975 GTK_TREE_MODEL(m_liststore
),
976 &iter
, NULL
, //NULL = parent = get first
979 wxCHECK_RET( res
, wxT("Invalid index") );
981 m_blockEvent
= blockEvent
;
984 gtk_tree_selection_select_iter(selection
, &iter
);
986 gtk_tree_selection_unselect_iter(selection
, &iter
);
988 m_blockEvent
= false;
991 void wxListBox::DoSetFirstItem( int n
)
993 wxCHECK_RET( m_treeview
, wxT("invalid listbox") );
994 wxCHECK_RET( IsValid(n
), wxT("invalid index"));
996 //RN: I have no idea why this line is needed...
997 if (gdk_pointer_is_grabbed () && GTK_WIDGET_HAS_GRAB (m_treeview
))
1001 gtk_tree_model_iter_nth_child(
1002 GTK_TREE_MODEL(m_liststore
),
1004 NULL
, //NULL = parent = get first
1008 GtkTreePath
* path
= gtk_tree_model_get_path(
1009 GTK_TREE_MODEL(m_liststore
), &iter
);
1011 // Scroll to the desired cell (0.0 == topleft alignment)
1012 gtk_tree_view_scroll_to_cell(m_treeview
, path
, NULL
,
1015 gtk_tree_path_free(path
);
1018 // ----------------------------------------------------------------------------
1020 // ----------------------------------------------------------------------------
1022 int wxListBox::DoListHitTest(const wxPoint
& point
) const
1024 // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
1025 // we only want visible items we need to check for it manually here
1026 if ( !GetClientRect().Contains(point
) )
1029 // need to translate from master window since it is in client coords
1031 gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview
),
1032 &binx
, &biny
, NULL
, NULL
, NULL
);
1035 if ( !gtk_tree_view_get_path_at_pos
1041 NULL
, // [out] column (always 0 here)
1042 NULL
, // [out] x-coord relative to the cell (not interested)
1043 NULL
// [out] y-coord relative to the cell
1049 int index
= gtk_tree_path_get_indices(path
)[0];
1050 gtk_tree_path_free(path
);
1055 // ----------------------------------------------------------------------------
1057 // ----------------------------------------------------------------------------
1060 void wxListBox::ApplyToolTip( GtkTooltips
*tips
, const wxChar
*tip
)
1062 // RN: Is this needed anymore?
1063 gtk_tooltips_set_tip( tips
, GTK_WIDGET( m_treeview
), wxGTK_CONV(tip
), (gchar
*) NULL
);
1065 #endif // wxUSE_TOOLTIPS
1067 GtkWidget
*wxListBox::GetConnectWidget()
1069 // the correct widget for listbox events (such as mouse clicks for example)
1070 // is m_treeview, not the parent scrolled window
1071 return GTK_WIDGET(m_treeview
);
1074 GdkWindow
*wxListBox::GTKGetWindow(wxArrayGdkWindows
& WXUNUSED(windows
)) const
1076 return gtk_tree_view_get_bin_window(m_treeview
);
1079 void wxListBox::DoApplyWidgetStyle(GtkRcStyle
*style
)
1081 if (m_hasBgCol
&& m_backgroundColour
.Ok())
1083 GdkWindow
*window
= gtk_tree_view_get_bin_window(m_treeview
);
1086 m_backgroundColour
.CalcPixel( gdk_drawable_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 wxSize
wxListBox::DoGetBestSize() const
1097 wxCHECK_MSG(m_treeview
, wxDefaultSize
, wxT("invalid tree view"));
1099 // Start with a minimum size that's not too small
1101 GetTextExtent( wxT("X"), &cx
, &cy
);
1102 int lbWidth
= 3 * cx
;
1105 // Get the visible area of the tree view (limit to the 10th item
1106 // so that it isn't too big)
1107 unsigned int count
= GetCount();
1112 // Find the widest line
1113 for(unsigned int i
= 0; i
< count
; i
++) {
1114 wxString
str(GetString(i
));
1115 GetTextExtent(str
, &wLine
, NULL
);
1116 lbWidth
= wxMax(lbWidth
, wLine
);
1121 // And just a bit more for the checkbox if present and then some
1122 // (these are rough guesses)
1123 #if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
1124 if ( m_hasCheckBoxes
)
1127 cy
= cy
> 25 ? cy
: 25; // rough height of checkbox
1131 // don't make the listbox too tall (limit height to around 10 items) but don't
1132 // make it too small neither
1133 lbHeight
= (cy
+4) * wxMin(wxMax(count
, 3), 10);
1136 // Add room for the scrollbar
1137 lbWidth
+= wxSystemSettings::GetMetric(wxSYS_VSCROLL_X
);
1139 wxSize
best(lbWidth
, lbHeight
);
1140 CacheBestSize(best
);
1146 wxListBox::GetClassDefaultAttributes(wxWindowVariant
WXUNUSED(variant
))
1148 return GetDefaultAttributesFromGTKWidget(gtk_tree_view_new
, true);
1151 #endif // wxUSE_LISTBOX