+// Searching callback (TRUE == not equal, FALSE == equal)
+//-----------------------------------------------------------------------------
+
+extern "C" {
+static gboolean gtk_listbox_searchequal_callback(GtkTreeModel * WXUNUSED(model),
+ gint WXUNUSED(column),
+ const gchar* key,
+ GtkTreeIter* iter,
+ wxListBox* listbox)
+{
+ wxTreeEntry* entry = GetEntry(listbox->m_liststore, iter, listbox);
+ wxCHECK_MSG(entry, 0, wxT("Could not get entry"));
+
+ wxGtkString keycollatekey(g_utf8_collate_key(key, -1));
+
+ return strcmp(keycollatekey, wx_tree_entry_get_collate_key(entry)) != 0;
+}
+}
+
+//-----------------------------------------------------------------------------
+// wxListBox
+//-----------------------------------------------------------------------------
+
+// ----------------------------------------------------------------------------
+// construction
+// ----------------------------------------------------------------------------
+
+void wxListBox::Init()
+{
+ m_treeview = NULL;
+#if wxUSE_CHECKLISTBOX
+ m_hasCheckBoxes = false;
+#endif // wxUSE_CHECKLISTBOX
+}
+
+bool wxListBox::Create( wxWindow *parent, wxWindowID id,
+ const wxPoint &pos, const wxSize &size,
+ const wxArrayString& choices,
+ long style, const wxValidator& validator,
+ const wxString &name )
+{
+ wxCArrayString chs(choices);
+
+ return Create( parent, id, pos, size, chs.GetCount(), chs.GetStrings(),
+ style, validator, name );
+}
+
+bool wxListBox::Create( wxWindow *parent, wxWindowID id,
+ const wxPoint &pos, const wxSize &size,
+ int n, const wxString choices[],
+ long style, const wxValidator& validator,
+ const wxString &name )
+{
+ if (!PreCreation( parent, pos, size ) ||
+ !CreateBase( parent, id, pos, size, style, validator, name ))
+ {
+ wxFAIL_MSG( wxT("wxListBox creation failed") );
+ return false;
+ }
+
+ m_widget = gtk_scrolled_window_new( NULL, NULL );
+ g_object_ref(m_widget);
+ if (style & wxLB_ALWAYS_SB)
+ {
+ gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
+ GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS );
+ }
+ else
+ {
+ gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
+ GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
+ }
+
+
+ GTKScrolledWindowSetBorder(m_widget, style);
+
+ m_treeview = GTK_TREE_VIEW( gtk_tree_view_new( ) );
+
+ //wxListBox doesn't have a header :)
+ //NB: If enabled SetFirstItem doesn't work correctly
+ gtk_tree_view_set_headers_visible(m_treeview, FALSE);
+
+#if wxUSE_CHECKLISTBOX
+ if(m_hasCheckBoxes)
+ ((wxCheckListBox*)this)->DoCreateCheckList();
+#endif // wxUSE_CHECKLISTBOX
+
+ // Create the data column
+ gtk_tree_view_insert_column_with_attributes(m_treeview, -1, "",
+ gtk_cell_renderer_text_new(),
+ "text",
+ WXLISTBOX_DATACOLUMN, NULL);
+
+ // Now create+set the model (GtkListStore) - first argument # of columns
+#if wxUSE_CHECKLISTBOX
+ if(m_hasCheckBoxes)
+ m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
+ WX_TYPE_TREE_ENTRY);
+ else
+#endif
+ m_liststore = gtk_list_store_new(1, WX_TYPE_TREE_ENTRY);
+
+ gtk_tree_view_set_model(m_treeview, GTK_TREE_MODEL(m_liststore));
+
+ g_object_unref (m_liststore); //free on treeview destruction
+
+ // Disable the pop-up textctrl that enables searching - note that
+ // the docs specify that even if this disabled (which we are doing)
+ // the user can still have it through the start-interactive-search
+ // key binding...either way we want to provide a searchequal callback
+ // NB: If this is enabled a doubleclick event (activate) gets sent
+ // on a successful search
+ gtk_tree_view_set_search_column(m_treeview, WXLISTBOX_DATACOLUMN);
+ gtk_tree_view_set_search_equal_func(m_treeview,
+ (GtkTreeViewSearchEqualFunc) gtk_listbox_searchequal_callback,
+ this,
+ NULL);
+
+ gtk_tree_view_set_enable_search(m_treeview, FALSE);
+
+ GtkSelectionMode mode;
+ // GTK_SELECTION_EXTENDED is a deprecated synonym for GTK_SELECTION_MULTIPLE
+ if ( style & (wxLB_MULTIPLE | wxLB_EXTENDED) )
+ {
+ mode = GTK_SELECTION_MULTIPLE;
+ }
+ else // no multi-selection flags specified
+ {
+ m_windowStyle |= wxLB_SINGLE;
+
+ // Notice that we must use BROWSE and not GTK_SELECTION_SINGLE because
+ // the latter allows to not select any items at all while a single
+ // selection listbox is supposed to always have a selection (at least
+ // once the user selected something, it might not have any initially).
+ mode = GTK_SELECTION_BROWSE;
+ }
+
+ GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
+ gtk_tree_selection_set_mode( selection, mode );
+
+ // Handle sortable stuff
+ if(HasFlag(wxLB_SORT))
+ {
+ // Setup sorting in ascending (wx) order
+ gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(m_liststore),
+ WXLISTBOX_DATACOLUMN,
+ GTK_SORT_ASCENDING);
+
+ // Set the sort callback
+ gtk_tree_sortable_set_sort_func(GTK_TREE_SORTABLE(m_liststore),
+ WXLISTBOX_DATACOLUMN,
+ (GtkTreeIterCompareFunc) gtk_listbox_sort_callback,
+ this, //userdata
+ NULL //"destroy notifier"
+ );
+ }
+
+
+ gtk_container_add (GTK_CONTAINER (m_widget), GTK_WIDGET(m_treeview) );
+
+ gtk_widget_show( GTK_WIDGET(m_treeview) );
+ m_focusWidget = GTK_WIDGET(m_treeview);
+
+ Append(n, choices); // insert initial items
+
+ // generate dclick events
+ g_signal_connect_after(m_treeview, "row-activated",
+ G_CALLBACK(gtk_listbox_row_activated_callback), this);
+
+ // for intercepting dclick generation by <ENTER>
+ g_signal_connect (m_treeview, "key_press_event",
+ G_CALLBACK (gtk_listbox_key_press_callback),
+ this);
+ m_parent->DoAddChild( this );
+
+ PostCreation(size);
+ SetInitialSize(size); // need this too because this is a wxControlWithItems
+
+ g_signal_connect_after (selection, "changed",
+ G_CALLBACK (gtk_listitem_changed_callback), this);
+
+ return true;
+}
+
+wxListBox::~wxListBox()
+{
+ if (m_treeview)
+ {
+ GTKDisconnect(m_treeview);
+ GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
+ if (selection)
+ GTKDisconnect(selection);
+ }
+
+ Clear();
+}
+
+void wxListBox::GTKDisableEvents()
+{
+ GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
+
+ g_signal_handlers_block_by_func(selection,
+ (gpointer) gtk_listitem_changed_callback, this);
+}
+
+void wxListBox::GTKEnableEvents()
+{
+ GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
+
+ g_signal_handlers_unblock_by_func(selection,
+ (gpointer) gtk_listitem_changed_callback, this);
+
+ UpdateOldSelections();
+}
+
+
+void wxListBox::Update()
+{
+ wxWindow::Update();
+
+ if (m_treeview)
+ gdk_window_process_updates(gtk_widget_get_window(GTK_WIDGET(m_treeview)), true);
+}
+
+// ----------------------------------------------------------------------------
+// adding items
+// ----------------------------------------------------------------------------
+
+int wxListBox::DoInsertItems(const wxArrayStringsAdapter& items,
+ unsigned int pos,
+ void **clientData,
+ wxClientDataType type)
+{
+ wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
+
+ InvalidateBestSize();
+ int n = DoInsertItemsInLoop(items, pos, clientData, type);
+ UpdateOldSelections();
+ return n;
+}
+
+int wxListBox::DoInsertOneItem(const wxString& item, unsigned int pos)
+{
+ wxTreeEntry* entry = wx_tree_entry_new();
+ wx_tree_entry_set_label(entry, wxGTK_CONV(item));
+ wx_tree_entry_set_destroy_func(entry, (wxTreeEntryDestroy)tree_entry_destroy_cb, this);
+
+#if wxUSE_CHECKLISTBOX
+ int entryCol = int(m_hasCheckBoxes);
+#else
+ int entryCol = 0;
+#endif
+ gtk_list_store_insert_with_values(m_liststore, NULL, pos, entryCol, entry, -1);
+ g_object_unref(entry);
+
+ return pos;
+}
+
+// ----------------------------------------------------------------------------
+// deleting items
+// ----------------------------------------------------------------------------
+
+void wxListBox::DoClear()
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ GTKDisableEvents(); // just in case
+
+ InvalidateBestSize();
+
+ gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
+
+ GTKEnableEvents();
+
+ UpdateOldSelections();
+}
+
+void wxListBox::DoDeleteOneItem(unsigned int n)
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ InvalidateBestSize();
+
+ GTKDisableEvents(); // just in case
+
+ GtkTreeIter iter;
+ wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("wrong listbox index") );
+
+ // this returns false if iter is invalid (e.g. deleting item at end) but
+ // since we don't use iter, we ignore the return value
+ gtk_list_store_remove(m_liststore, &iter);
+
+ GTKEnableEvents();
+}
+
+// ----------------------------------------------------------------------------
+// helper functions for working with iterators
+// ----------------------------------------------------------------------------
+
+bool wxListBox::GTKGetIteratorFor(unsigned pos, GtkTreeIter *iter) const
+{
+ if ( !gtk_tree_model_iter_nth_child(GTK_TREE_MODEL(m_liststore),
+ iter, NULL, pos) )
+ {
+ wxLogDebug(wxT("gtk_tree_model_iter_nth_child(%u) failed"), pos);
+ return false;
+ }
+
+ return true;
+}
+
+int wxListBox::GTKGetIndexFor(GtkTreeIter& iter) const
+{
+ GtkTreePath *path =
+ gtk_tree_model_get_path(GTK_TREE_MODEL(m_liststore), &iter);
+
+ gint* pIntPath = gtk_tree_path_get_indices(path);
+
+ wxCHECK_MSG( pIntPath, wxNOT_FOUND, wxT("failed to get iterator path") );
+
+ int idx = pIntPath[0];
+
+ gtk_tree_path_free( path );
+
+ return idx;
+}
+
+// get GtkTreeEntry from position (note: you need to g_unref it if valid)
+wxTreeEntry* wxListBox::GTKGetEntry(unsigned n) const
+{
+ GtkTreeIter iter;
+ if ( !GTKGetIteratorFor(n, &iter) )
+ return NULL;
+
+ return GetEntry(m_liststore, &iter, this);
+}
+
+// ----------------------------------------------------------------------------
+// client data
+// ----------------------------------------------------------------------------
+
+void* wxListBox::DoGetItemClientData(unsigned int n) const
+{
+ wxTreeEntry* entry = GTKGetEntry(n);
+ wxCHECK_MSG(entry, NULL, wxT("could not get entry"));
+
+ return wx_tree_entry_get_userdata(entry);
+}
+
+void wxListBox::DoSetItemClientData(unsigned int n, void* clientData)
+{
+ wxTreeEntry* entry = GTKGetEntry(n);
+ wxCHECK_RET(entry, wxT("could not get entry"));
+
+ wx_tree_entry_set_userdata(entry, clientData);
+}
+
+// ----------------------------------------------------------------------------
+// string list access
+// ----------------------------------------------------------------------------
+
+void wxListBox::SetString(unsigned int n, const wxString& label)
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ GtkTreeIter iter;
+ wxCHECK_RET(GTKGetIteratorFor(n, &iter), "invalid index");
+ wxTreeEntry* entry = GetEntry(m_liststore, &iter, this);
+
+ // update the item itself
+ wx_tree_entry_set_label(entry, wxGTK_CONV(label));
+
+ // signal row changed
+ GtkTreeModel* tree_model = GTK_TREE_MODEL(m_liststore);
+ GtkTreePath* path = gtk_tree_model_get_path(tree_model, &iter);
+ gtk_tree_model_row_changed(tree_model, path, &iter);
+ gtk_tree_path_free(path);
+}
+
+wxString wxListBox::GetString(unsigned int n) const
+{
+ wxCHECK_MSG( m_treeview != NULL, wxEmptyString, wxT("invalid listbox") );
+
+ wxTreeEntry* entry = GTKGetEntry(n);
+ wxCHECK_MSG( entry, wxEmptyString, wxT("wrong listbox index") );
+
+ return wxGTK_CONV_BACK(wx_tree_entry_get_label(entry));
+}
+
+unsigned int wxListBox::GetCount() const
+{
+ wxCHECK_MSG( m_treeview != NULL, 0, wxT("invalid listbox") );
+
+ return (unsigned int)gtk_tree_model_iter_n_children(GTK_TREE_MODEL(m_liststore), NULL);
+}
+
+int wxListBox::FindString( const wxString &item, bool bCase ) const
+{
+ wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
+
+ //Sort of hackish - maybe there is a faster way
+ unsigned int nCount = wxListBox::GetCount();
+
+ for(unsigned int i = 0; i < nCount; ++i)
+ {
+ if( item.IsSameAs( wxListBox::GetString(i), bCase ) )
+ return (int)i;
+ }
+