+ 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;
+ }
+
+
+ // it's not an error if the string is not found -> no wxCHECK
+ return wxNOT_FOUND;
+}
+
+// ----------------------------------------------------------------------------
+// selection
+// ----------------------------------------------------------------------------
+
+void wxListBox::GTKOnActivated(int item)
+{
+ SendEvent(wxEVT_LISTBOX_DCLICK, item, IsSelected(item));
+}
+
+void wxListBox::GTKOnSelectionChanged()
+{
+ if ( HasFlag(wxLB_MULTIPLE | wxLB_EXTENDED) )
+ {
+ CalcAndSendEvent();
+ }
+ else // single selection
+ {
+ const int item = GetSelection();
+ if ( DoChangeSingleSelection(item) )
+ SendEvent(wxEVT_LISTBOX, item, true);
+ }
+}
+
+int wxListBox::GetSelection() const
+{
+ wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox"));
+ wxCHECK_MSG( HasFlag(wxLB_SINGLE), wxNOT_FOUND,
+ wxT("must be single selection listbox"));
+
+ GtkTreeIter iter;
+ GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
+
+ // only works on single-sel
+ if (!gtk_tree_selection_get_selected(selection, NULL, &iter))
+ return wxNOT_FOUND;
+
+ return GTKGetIndexFor(iter);
+}
+
+int wxListBox::GetSelections( wxArrayInt& aSelections ) const
+{
+ wxCHECK_MSG( m_treeview != NULL, wxNOT_FOUND, wxT("invalid listbox") );
+
+ aSelections.Empty();
+
+ int i = 0;
+ GtkTreeIter iter;
+ GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
+
+ if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(m_liststore), &iter))
+ { //gtk_tree_selection_get_selected_rows is GTK 2.2+ so iter instead
+ do
+ {
+ if (gtk_tree_selection_iter_is_selected(selection, &iter))
+ aSelections.Add(i);
+
+ i++;
+ } while(gtk_tree_model_iter_next(GTK_TREE_MODEL(m_liststore), &iter));
+ }
+
+ return aSelections.GetCount();
+}
+
+bool wxListBox::IsSelected( int n ) const
+{
+ wxCHECK_MSG( m_treeview != NULL, false, wxT("invalid listbox") );
+
+ GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
+
+ GtkTreeIter iter;
+ wxCHECK_MSG( GTKGetIteratorFor(n, &iter), false, wxT("Invalid index") );
+
+ return gtk_tree_selection_iter_is_selected(selection, &iter) != 0;
+}
+
+void wxListBox::DoSetSelection( int n, bool select )
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ GTKDisableEvents();
+
+ GtkTreeSelection* selection = gtk_tree_view_get_selection(m_treeview);
+
+ // passing -1 to SetSelection() is documented to deselect all items
+ if ( n == wxNOT_FOUND )
+ {
+ gtk_tree_selection_unselect_all(selection);
+ GTKEnableEvents();
+ return;
+ }
+
+ wxCHECK_RET( IsValid(n), wxT("invalid index in wxListBox::SetSelection") );
+
+
+ GtkTreeIter iter;
+ wxCHECK_RET( GTKGetIteratorFor(n, &iter), wxT("Invalid index") );
+
+ if (select)
+ gtk_tree_selection_select_iter(selection, &iter);
+ else
+ gtk_tree_selection_unselect_iter(selection, &iter);
+
+ GtkTreePath* path = gtk_tree_model_get_path(
+ GTK_TREE_MODEL(m_liststore), &iter);
+
+ gtk_tree_view_scroll_to_cell(m_treeview, path, NULL, FALSE, 0.0f, 0.0f);
+
+ gtk_tree_path_free(path);
+
+ GTKEnableEvents();
+}
+
+void wxListBox::DoScrollToCell(int n, float alignY, float alignX)
+{
+ wxCHECK_RET( m_treeview, wxT("invalid listbox") );
+ wxCHECK_RET( IsValid(n), wxT("invalid index"));
+
+ //RN: I have no idea why this line is needed...
+ if (gtk_widget_has_grab(GTK_WIDGET(m_treeview)))
+ return;
+
+ GtkTreeIter iter;
+ if ( !GTKGetIteratorFor(n, &iter) )
+ return;
+
+ GtkTreePath* path = gtk_tree_model_get_path(
+ GTK_TREE_MODEL(m_liststore), &iter);
+
+ // Scroll to the desired cell (0.0 == topleft alignment)
+ gtk_tree_view_scroll_to_cell(m_treeview, path, NULL,
+ TRUE, alignY, alignX);
+
+ gtk_tree_path_free(path);
+}
+
+void wxListBox::DoSetFirstItem(int n)
+{
+ DoScrollToCell(n, 0, 0);
+}
+
+void wxListBox::EnsureVisible(int n)
+{
+ DoScrollToCell(n, 0.5, 0);
+}
+
+// ----------------------------------------------------------------------------
+// hittest
+// ----------------------------------------------------------------------------
+
+int wxListBox::DoListHitTest(const wxPoint& point) const
+{
+ // gtk_tree_view_get_path_at_pos() also gets items that are not visible and
+ // we only want visible items we need to check for it manually here
+ if ( !GetClientRect().Contains(point) )
+ return wxNOT_FOUND;
+
+ // need to translate from master window since it is in client coords
+ gint binx, biny;
+ gdk_window_get_geometry(gtk_tree_view_get_bin_window(m_treeview),
+ &binx, &biny, NULL, NULL);
+
+ GtkTreePath* path;
+ if ( !gtk_tree_view_get_path_at_pos
+ (
+ m_treeview,
+ point.x - binx,
+ point.y - biny,
+ &path,
+ NULL, // [out] column (always 0 here)
+ NULL, // [out] x-coord relative to the cell (not interested)
+ NULL // [out] y-coord relative to the cell
+ ) )