+ 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;
+ }
+
+
+ // 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();