-IMPLEMENT_DYNAMIC_CLASS(wxListBox,wxControl)
-
-wxListBox::wxListBox(void)
-{
- m_list = NULL;
-};
-
-wxListBox::wxListBox( wxWindow *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size,
- const int n, const wxString choices[],
- const long style, const wxString &name )
-{
- Create( parent, id, pos, size, n, choices, style, name );
-};
-
-bool wxListBox::Create( wxWindow *parent, wxWindowID id,
- const wxPoint &pos, const wxSize &size,
- const int n, const wxString choices[],
- const long style, const wxString &name )
-{
- m_needParent = TRUE;
-
- PreCreation( parent, id, pos, size, style, name );
-
- m_widget = gtk_scrolled_window_new( NULL, NULL );
- gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(m_widget),
- GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC );
-
- m_list = GTK_LIST( gtk_list_new() );
- gtk_list_set_selection_mode( GTK_LIST(m_list), GTK_SELECTION_BROWSE );
-
- gtk_container_add (GTK_CONTAINER(m_widget), GTK_WIDGET(m_list) );
- gtk_widget_show( GTK_WIDGET(m_list) );
-
- for (int i = 0; i < n; i++)
- {
- GtkWidget *list_item;
- list_item = gtk_list_item_new_with_label( choices[i] );
-
- gtk_signal_connect( GTK_OBJECT(list_item), "select",
- GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
-
- gtk_container_add( GTK_CONTAINER(m_list), list_item );
-
- gtk_widget_show( list_item );
- };
-
- PostCreation();
-
- Show( TRUE );
-
- return TRUE;
-};
-
-void wxListBox::Append( const wxString &item )
-{
- GtkWidget *list_item;
- list_item = gtk_list_item_new_with_label( item );
-
- gtk_signal_connect( GTK_OBJECT(list_item), "select",
- GTK_SIGNAL_FUNC(gtk_listitem_select_callback), (gpointer)this );
-
- gtk_container_add( GTK_CONTAINER(m_list), list_item );
-
- gtk_widget_show( list_item );
-};
-
-void wxListBox::Append( const wxString &WXUNUSED(item), char *WXUNUSED(clientData) )
-{
-};
-
-void wxListBox::Clear(void)
-{
- gtk_list_clear_items( m_list, 0, Number() );
-};
-
-void wxListBox::Delete( int n )
-{
- gtk_list_clear_items( m_list, n, n );
-};
-
-void wxListBox::Deselect( int n )
-{
- gtk_list_unselect_item( m_list, n );
-};
-
-int wxListBox::FindString( const wxString &item ) const
-{
- GList *child = m_list->children;
- int count = 0;
- while (child)
- {
- GtkBin *bin = GTK_BIN( child->data );
- GtkLabel *label = GTK_LABEL( bin->child );
- if (item == label->label) return count;
- count++;
- child = child->next;
- };
- return -1;
-};
-
-char *wxListBox::GetClientData( const int WXUNUSED(n) ) const
-{
- return NULL;
-};
-
-int wxListBox::GetSelection(void) const
-{
- GList *selection = m_list->selection;
- if (selection)
- {
- GList *child = m_list->children;
- int count = 0;
- while (child)
+IMPLEMENT_DYNAMIC_CLASS(wxListBox, wxControl)
+
+// ----------------------------------------------------------------------------
+// construction
+// ----------------------------------------------------------------------------
+
+void wxListBox::Init()
+{
+ m_treeview = (GtkTreeView*) NULL;
+#if wxUSE_CHECKLISTBOX
+ m_hasCheckBoxes = false;
+#endif // wxUSE_CHECKLISTBOX
+ m_spacePressed = false;
+}
+
+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 )
+{
+ m_needParent = true;
+ m_acceptsFocus = true;
+ m_blockEvent = false;
+
+ 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( (GtkAdjustment*) NULL, (GtkAdjustment*) NULL );
+ 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 && wxUSE_NATIVEGTKCHECKLIST
+ if(m_hasCheckBoxes)
+ ((wxCheckListBox*)this)->DoCreateCheckList();
+#endif // wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
+
+ // 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 && wxUSE_NATIVEGTKCHECKLIST
+ if(m_hasCheckBoxes)
+ m_liststore = gtk_list_store_new(2, G_TYPE_BOOLEAN,
+ GTK_TYPE_TREE_ENTRY);
+ else
+#endif
+ m_liststore = gtk_list_store_new(1, GTK_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);
+
+
+ GtkTreeSelection* selection = gtk_tree_view_get_selection( m_treeview );
+ gtk_tree_selection_set_select_function(selection,
+ (GtkTreeSelectionFunc)gtk_listitem_select_cb,
+ this, NULL); //NULL == destroycb
+
+ GtkSelectionMode mode;
+ if (style & wxLB_MULTIPLE)
+ {
+ mode = GTK_SELECTION_MULTIPLE;
+ }
+ else if (style & wxLB_EXTENDED)
+ {
+ mode = GTK_SELECTION_EXTENDED;
+ }
+ else
+ {
+ // if style was 0 set single mode
+ m_windowStyle |= wxLB_SINGLE;
+ mode = GTK_SELECTION_SINGLE;
+ }
+
+ gtk_tree_selection_set_mode( selection, mode );
+
+ //Handle sortable stuff
+ if(style & 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) );
+
+ wxListBox::DoInsertItems(wxArrayString(n, choices), 0); // insert initial items
+
+ //treeview-specific events
+ g_signal_connect(m_treeview, "row-activated",
+ G_CALLBACK(gtk_listbox_row_activated_callback), this);
+
+ // other events
+ g_signal_connect (m_treeview, "button_press_event",
+ G_CALLBACK (gtk_listbox_button_press_callback),
+ this);
+ g_signal_connect (m_treeview, "key_press_event",
+ G_CALLBACK (gtk_listbox_key_press_callback),
+ this);
+
+ m_parent->DoAddChild( this );
+
+ PostCreation(size);
+ SetBestSize(size); // need this too because this is a wxControlWithItems
+
+ return true;
+}
+
+wxListBox::~wxListBox()
+{
+ m_hasVMT = false;
+
+ Clear();
+}
+
+// ----------------------------------------------------------------------------
+// adding items
+// ----------------------------------------------------------------------------
+
+void wxListBox::GtkInsertItems(const wxArrayString& items,
+ void** clientData, unsigned int pos)
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ InvalidateBestSize();
+
+ // Create and set column ids and GValues
+
+ unsigned int nNum = items.GetCount();
+ unsigned int nCurCount = wxListBox::GetCount();
+ wxASSERT_MSG(pos <= nCurCount, wxT("Invalid index passed to wxListBox"));
+
+ GtkTreeIter* pIter = NULL; // append by default
+ GtkTreeIter iter;
+ if (pos != nCurCount)
+ {
+ gboolean res = gtk_tree_model_iter_nth_child(
+ GTK_TREE_MODEL(m_liststore),
+ &iter, NULL, //NULL = parent = get first
+ (int)pos );
+ if(!res)
+ {
+ wxLogSysError(wxT("internal wxListBox error in insertion"));
+ return;
+ }
+
+ pIter = &iter;
+ }
+
+ for (unsigned int i = 0; i < nNum; ++i)
+ {
+ wxString label = items[i];
+
+#if wxUSE_CHECKLISTBOX && !wxUSE_NATIVEGTKCHECKLIST
+ if (m_hasCheckBoxes)
+ {
+ label.Prepend(wxCHECKLBOX_STRING);
+ }
+#endif // wxUSE_CHECKLISTBOX
+
+
+ GtkTreeEntry* entry = gtk_tree_entry_new();
+ gtk_tree_entry_set_label(entry, wxConvUTF8.cWX2MB(label));
+ gtk_tree_entry_set_destroy_func(entry,
+ (GtkTreeEntryDestroy)gtk_tree_entry_destroy_cb,
+ this);
+
+ if (clientData)
+ gtk_tree_entry_set_userdata(entry, clientData[i]);
+
+ GtkTreeIter itercur;
+ gtk_list_store_insert_before(m_liststore, &itercur, pIter);
+
+#if wxUSE_CHECKLISTBOX && wxUSE_NATIVEGTKCHECKLIST
+ if (m_hasCheckBoxes)
+ {
+ gtk_list_store_set(m_liststore, &itercur,
+ 0, FALSE, //FALSE == not toggled
+ 1, entry, -1);
+ }
+ else
+#endif
+ gtk_list_store_set(m_liststore, &itercur,
+ 0, entry, -1);
+
+ g_object_unref (entry); //liststore always refs :)
+ }
+}
+
+void wxListBox::DoInsertItems(const wxArrayString& items, unsigned int pos)
+{
+ wxCHECK_RET( IsValidInsert(pos), wxT("invalid index in wxListBox::InsertItems") );
+
+ GtkInsertItems(items, NULL, pos);
+}
+
+int wxListBox::DoAppend( const wxString& item )
+{
+ // Call DoInsertItems
+ unsigned int nWhere = wxListBox::GetCount();
+ wxArrayString aItems;
+ aItems.Add(item);
+ wxListBox::DoInsertItems(aItems, nWhere);
+ return nWhere;
+}
+
+void wxListBox::DoSetItems( const wxArrayString& items,
+ void **clientData)
+{
+ Clear();
+ GtkInsertItems(items, clientData, 0);
+}
+
+// ----------------------------------------------------------------------------
+// deleting items
+// ----------------------------------------------------------------------------
+
+void wxListBox::Clear()
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ InvalidateBestSize();
+
+ gtk_list_store_clear( m_liststore ); /* well, THAT was easy :) */
+}
+
+void wxListBox::Delete(unsigned int n)
+{
+ wxCHECK_RET( m_treeview != NULL, wxT("invalid listbox") );
+
+ InvalidateBestSize();
+
+ GtkTreeIter iter;
+ gboolean res = gtk_tree_model_iter_nth_child(
+ GTK_TREE_MODEL(m_liststore),
+ &iter, NULL, //NULL = parent = get first
+ n
+ );
+
+ wxCHECK_RET( res, wxT("wrong listbox index") );
+
+ //this returns false if iter is invalid (i.e. deleting item
+ //at end) but since we don't use iter, well... :)
+ gtk_list_store_remove(m_liststore, &iter);
+}
+
+// ----------------------------------------------------------------------------
+// get GtkTreeEntry from position (note: you need to g_unref it if valid)
+// ----------------------------------------------------------------------------
+
+struct _GtkTreeEntry* wxListBox::GtkGetEntry(int n) const
+{
+ GtkTreeIter iter;
+ gboolean res = gtk_tree_model_iter_nth_child(
+ GTK_TREE_MODEL(m_liststore),
+ &iter, NULL, //NULL = parent = get first
+ n );
+
+ if (!res)