X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/e21a504837179fc2be49f72e03664c8d8cc82148..f5eafd0e55812155780cc9ec2910ac25fd1629e7:/src/gtk/treectrl.cpp diff --git a/src/gtk/treectrl.cpp b/src/gtk/treectrl.cpp index 5248a0741f..b1df8d5abc 100644 --- a/src/gtk/treectrl.cpp +++ b/src/gtk/treectrl.cpp @@ -6,7 +6,7 @@ // Created: 07/05/98 // RCS-ID: $Id$ // Copyright: (c) Denis Pershin -// Licence: wxWindows licence +// Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ @@ -32,7 +32,11 @@ //static void wxConvertToGtkTreeItem(wxTreeCtrl *owner, wxTreeItem& info, GtkTreeItem **gtkItem); //static void wxConvertFromGtkTreeItem(wxTreeItem& info, GtkTreeItem *gtkItem); + static void gtk_treectrl_count_callback (GtkWidget *widget, gpointer data); +static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data); +static void gtk_treectrl_first_visible_callback(GtkWidget *widget, gpointer data); + // static void gtk_treectrl_next_callback (GtkWidget *widget, gpointer data); // static void gtk_treectrl_next_visible_callback (GtkWidget *widget, gpointer data); // static void gtk_treectrl_next_selected_callback (GtkWidget *widget, gpointer data); @@ -106,7 +110,7 @@ bool wxTreeCtrl::Create(wxWindow *parent, wxWindowID id, const wxPoint& pos, y = 0; m_needParent = TRUE; - + printf("precreate\n"); PreCreation( parent, id, pos, size, style, name ); @@ -123,20 +127,22 @@ printf("3\n"); printf("4\n"); gtk_container_add(GTK_CONTAINER(m_widget), GTK_WIDGET(m_tree)); printf("5\n"); -// gtk_widget_set_parent(GTK_WIDGET(m_tree), m_widget); -printf("6\n"); gtk_widget_show(GTK_WIDGET(m_tree)); SetName(name); SetValidator(validator); +printf("Robert's new insertion code :-)\n"); + m_parent->AddChild( this ); + (m_parent->m_insertCallback)( m_parent, this ); + printf("postcreate\n"); PostCreation(); gtk_widget_realize(GTK_WIDGET(m_tree)); Show(TRUE); - + return TRUE; } @@ -158,7 +164,7 @@ size_t wxTreeCtrl::GetCount() const { int count = 0; if (m_anchor != NULL) - gtk_container_foreach(GTK_CONTAINER(m_anchor), gtk_treectrl_count_callback, &count); + gtk_treectrl_count_callback(GTK_WIDGET(m_anchor), &count); return count; } @@ -249,16 +255,22 @@ void wxTreeCtrl::SetItemData(const wxTreeItemId& item, wxTreeItemData *data) { } bool wxTreeCtrl::IsVisible(const wxTreeItemId& item) const { -#warning "Need to implement IsVisible" - return FALSE; + return GTK_WIDGET_VISIBLE(GTK_TREE_ITEM((GtkTreeItem *)item)); } bool wxTreeCtrl::ItemHasChildren(const wxTreeItemId& item) const { - int count = 0; + GtkTreeItem *p = (GtkTreeItem *)item; + + if (p->subtree == NULL) + return wxFalse; - gtk_container_foreach(GTK_CONTAINER((GtkTreeItem *)item), gtk_treectrl_count_callback, &count); + if (GTK_TREE(p->subtree)->children == NULL) + return wxFalse; - return (count != 0); + if (g_list_length(GTK_TREE(p->subtree)->children) == 0) + return wxFalse; + + return wxTrue; } bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const { @@ -266,27 +278,48 @@ bool wxTreeCtrl::IsExpanded(const wxTreeItemId& item) const { } bool wxTreeCtrl::IsSelected(const wxTreeItemId& item) const { -#warning "Need to implement IsSelected" - return FALSE; + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + if (!GTK_IS_TREE(parent)) + return wxFalse; + + if (g_list_index(GTK_TREE(parent)->children, p) == -1) + return wxFalse; + + return wxTrue; } wxTreeItemId wxTreeCtrl::GetRootItem() const { return m_anchor; } +static void gtk_treectrl_first_selected_callback(GtkWidget *widget, gpointer data) { + GtkTreeItem *p = (*((GtkTreeItem **)data)); + + GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree); + + if (tree->selection != NULL) { + p = (GtkTreeItem *)tree->selection->data; + return; + } + + if (GTK_IS_CONTAINER(widget)) + gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_selected_callback, data); +} + wxTreeItemId wxTreeCtrl::GetSelection() const { -#warning "Need to complete gtk_treectrl_next_selected_callback" - GtkTreeItem *next = NULL; + GtkTreeItem *p = NULL; - GList *list = gtk_container_children(GTK_CONTAINER(m_anchor)); - next = GTK_TREE_ITEM(list->data); -// gtk_container_foreach(GTK_CONTAINER(m_anchor), gtk_treectrl_next_selected_callback, &next); + if (m_anchor == NULL) + return NULL; + + gtk_treectrl_first_selected_callback(GTK_WIDGET(m_anchor), &p); - return next; + return p; } wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const { -#warning "data 'parent' is missing!!!" if (item.IsOk()) return (GtkTreeItem *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "parent"); @@ -294,37 +327,134 @@ wxTreeItemId wxTreeCtrl::GetParent(const wxTreeItemId& item) const { } wxTreeItemId wxTreeCtrl::GetFirstChild(const wxTreeItemId& item, long& cookie) const { -#warning "Need to implement GetFirstChild" - return NULL; + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + if (!GTK_IS_TREE(parent)) + return NULL; + + cookie = 0; + return GTK_TREE_ITEM(g_list_first(GTK_TREE(parent)->children)->data); } wxTreeItemId wxTreeCtrl::GetNextChild(const wxTreeItemId& item, long& cookie) const { -#warning "Need to implement GetNextChild" - return NULL; + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + if (!GTK_IS_TREE(parent)) + return NULL; + + cookie++; + return GTK_TREE_ITEM(g_list_nth(GTK_TREE(parent)->children, cookie)->data); +} + +wxTreeItemId wxTreeCtrl::GetLastChild(const wxTreeItemId& item) const +{ + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + wxCHECK_MSG( GTK_IS_TREE(parent), NULL, "invalid tree item" ); + + return GTK_TREE_ITEM(g_list_last(GTK_TREE(parent)->children)->data); } wxTreeItemId wxTreeCtrl::GetNextSibling(const wxTreeItemId& item) const { -#warning "Need to implement GetNextSibling" - return NULL; + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + if (!GTK_IS_TREE(parent)) + return NULL; + + if (g_list_index(GTK_TREE(parent)->children, p) == -1) + return NULL; + + return GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data); } wxTreeItemId wxTreeCtrl::GetPrevSibling(const wxTreeItemId& item) const { -#warning "Need to implement GetPrevSibling" - return NULL; + GtkTreeItem *p = (GtkTreeItem *)item; + GtkWidget *parent = GTK_WIDGET(p)->parent; + + if (!GTK_IS_TREE(parent)) + return NULL; + + if (g_list_index(GTK_TREE(parent)->children, p) == -1) + return NULL; + + return GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data); +} + +static void gtk_treectrl_first_visible_callback(GtkWidget *widget, gpointer data) { + GtkTreeItem *p = (*((GtkTreeItem **)data)); + + GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree); + + if (tree->children != NULL) { + guint len = g_list_length(tree->children); + for (guint i=0; ichildren, i)->data)))) { + p = GTK_TREE_ITEM((GtkTreeItem *)g_list_nth(tree->children, i)->data); + return; + } + } + } + + if (GTK_IS_CONTAINER(widget)) + gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_first_visible_callback, data); } wxTreeItemId wxTreeCtrl::GetFirstVisibleItem() const { -#warning "Need to implement GetFirstVisibleItem" - return NULL; + GtkTreeItem *p = NULL; + + if (m_anchor == NULL) + return NULL; + + gtk_treectrl_first_visible_callback(GTK_WIDGET(m_anchor), &p); + + return p; } wxTreeItemId wxTreeCtrl::GetNextVisible(const wxTreeItemId& item) const { -#warning "Need to implement GetNextVisible" + GtkTreeItem *p = (GtkTreeItem *)item; + GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent); + GtkTreeItem *q; + + if (!GTK_IS_TREE(parent)) + return NULL; + + if (parent->children == NULL) + return NULL; + + q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, p))->data); + + while (q != p) { + q = GTK_TREE_ITEM(g_list_next(g_list_find(GTK_TREE(parent)->children, q))->data); + if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q))) + return q; + } + return NULL; } wxTreeItemId wxTreeCtrl::GetPrevVisible(const wxTreeItemId& item) const { -#warning "Need to implement GetPrevVisible" + GtkTreeItem *p = (GtkTreeItem *)item; + GtkTree *parent = GTK_TREE(GTK_WIDGET(p)->parent); + GtkTreeItem *q; + + if (!GTK_IS_TREE(parent)) + return NULL; + + if (parent->children == NULL) + return NULL; + + q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, p))->data); + + while (q != p) { + q = GTK_TREE_ITEM(g_list_previous(g_list_find(GTK_TREE(parent)->children, q))->data); + if (GTK_WIDGET_VISIBLE(GTK_WIDGET(q))) + return q; + } + return NULL; } @@ -349,13 +479,13 @@ wxTreeItemId wxTreeCtrl::InsertItem(const wxTreeItemId& parent, wxTreeItemId wxTreeCtrl::AppendItem(const wxTreeItemId& parent, const wxString& text, int image, int selectedImage, - wxTreeItemData *data) { + wxTreeItemData *data) { return p_InsertItem(parent, text, image, selectedImage, data); } wxTreeItemId wxTreeCtrl::p_InsertItem(GtkTreeItem *p, const wxString& text, int image, int selectedImage, - wxTreeItemData *data) { + wxTreeItemData *data) { GtkTreeItem *item; printf("begin insert\n"); @@ -391,6 +521,8 @@ printf("begin insert\n"); gtk_widget_show(GTK_WIDGET(m_box)); gtk_object_set_data(GTK_OBJECT(item), "owner", this); + gtk_object_set_data(GTK_OBJECT(item), "data", data); + gtk_object_set_data(GTK_OBJECT(item), "parent", p); if (p != 0) { if (p->subtree == NULL) { @@ -408,16 +540,6 @@ printf("m_tree = %p\n", m_tree); gtk_container_add(GTK_CONTAINER(m_tree), GTK_WIDGET(item)); } -/* - if ((info.m_mask & wxTREE_MASK_CHILDREN) != 0) { - GtkTree *tree = GTK_TREE(gtk_tree_new()); - gtk_tree_item_set_subtree(GTK_TREE_ITEM(item), GTK_WIDGET(tree)); - gtk_widget_show(GTK_WIDGET(tree)); - } -*/ - - gtk_object_set_data(GTK_OBJECT(item), "data", data); - gtk_widget_show(GTK_WIDGET(item)); gtk_signal_connect(GTK_OBJECT(item), "select", @@ -483,8 +605,28 @@ void wxTreeCtrl::Toggle(const wxTreeItemId& item) { gtk_tree_item_expand(GTK_TREE_ITEM((GtkTreeItem *)item)); } +static void gtk_treectrl_unselect_callback(GtkWidget *widget, gpointer data) { + GtkTreeItem *p; + + GtkTree *tree = GTK_TREE(GTK_TREE_ITEM(widget)->subtree); + + if (tree->selection != NULL) { + guint len = g_list_length(tree->selection); + for (guint i=0; iselection, i)->data); + gtk_tree_unselect_child(tree, GTK_WIDGET(p)); + } + } + + if (GTK_IS_CONTAINER(widget)) + gtk_container_foreach(GTK_CONTAINER(widget), gtk_treectrl_unselect_callback, data); +} + void wxTreeCtrl::Unselect() { -#warning "Need to implement Unselect" + if (m_anchor == NULL) + return; + + gtk_treectrl_unselect_callback(GTK_WIDGET(m_anchor), NULL); } void wxTreeCtrl::SelectItem(const wxTreeItemId& item) { @@ -506,7 +648,23 @@ wxTextCtrl* wxTreeCtrl::EditLabel(const wxTreeItemId& item, wxClassInfo* textControlClass) { wxASSERT( textControlClass->IsKindOf(CLASSINFO(wxTextCtrl)) ); #warning "Need to implement EditLabel" - return m_textCtrl; +/* + char *s; + m_editItem = item; + + GtkLabel *m_label = (GtkLabel *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)item), "w_label"); + gtk_label_get(m_label, &s); + + m_textCtrl = new wxTextCtrl(this, -1, s); +// m_textCtrl->SetValue(s); + + gtk_object_set_data(GTK_OBJECT((GtkTreeItem *)item), "w_edit", m_textCtrl->m_widget); + + gtk_container_remove(GTK_CONTAINER((GtkTreeItem *)item), GTK_WIDGET(m_label)); + gtk_container_add(GTK_CONTAINER((GtkTreeItem *)item), m_textCtrl->m_widget); + +*/ + return m_textCtrl; } wxTextCtrl* wxTreeCtrl::GetEditControl() const { @@ -515,6 +673,18 @@ wxTextCtrl* wxTreeCtrl::GetEditControl() const { void wxTreeCtrl::EndEditLabel(const wxTreeItemId& item, bool discardChanges) { #warning "Need to implement EndEditLabel" +/* + GtkLabel *m_label = (GtkLabel *)gtk_object_get_data(GTK_OBJECT((GtkTreeItem *)m_editItem), "w_label"); + gtk_label_set(m_label, m_textCtrl->GetValue()); + + gtk_object_remove_data(GTK_OBJECT((GtkTreeItem *)m_editItem), "w_edit"); + + gtk_container_remove(GTK_CONTAINER((GtkTreeItem *)m_editItem), m_textCtrl->m_widget); + gtk_container_add(GTK_CONTAINER((GtkTreeItem *)m_editItem), GTK_WIDGET(m_label)); + + delete m_textCtrl; + m_textCtrl = NULL; +*/ } void wxTreeCtrl::ExpandItem(const wxTreeItemId& item, int action) { @@ -559,7 +729,7 @@ long wxTreeCtrl::GetChild(long item) const { if (next != NULL) return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); - + return (-1); } @@ -572,7 +742,7 @@ long wxTreeCtrl::GetFirstVisibleItem(void) const { if (next != NULL) return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); - + return (-1); } @@ -587,7 +757,7 @@ long wxTreeCtrl::GetNextVisibleItem(long item) const { if (next != NULL) return (long)gtk_object_get_data(GTK_OBJECT(next), "id"); - + return (-1); } @@ -727,40 +897,35 @@ long wxTreeCtrl::InsertItem(long parent, const wxString& label, int image, } */ -void wxTreeCtrl::SendExpanding(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_EXPANDING, GetId()); +void wxTreeCtrl::SendMessage(wxEventType command, const wxTreeItemId& item) { + wxTreeEvent event(command, GetId()); event.SetEventObject(this); + event.m_item = item; ProcessEvent(event); } +void wxTreeCtrl::SendExpanding(const wxTreeItemId& item) { + SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDING, item); +} + void wxTreeCtrl::SendExpanded(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_EXPANDED, GetId()); - event.SetEventObject(this); - ProcessEvent(event); + SendMessage(wxEVT_COMMAND_TREE_ITEM_EXPANDED, item); } void wxTreeCtrl::SendCollapsing(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, GetId()); - event.SetEventObject(this); - ProcessEvent(event); + SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSING, item); } void wxTreeCtrl::SendCollapsed(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, GetId()); - event.SetEventObject(this); - ProcessEvent(event); + SendMessage(wxEVT_COMMAND_TREE_ITEM_COLLAPSED, item); } void wxTreeCtrl::SendSelChanging(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGED, GetId()); - event.SetEventObject(this); - ProcessEvent(event); + SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGED, item); } void wxTreeCtrl::SendSelChanged(const wxTreeItemId& item) { - wxTreeEvent event(wxEVT_COMMAND_TREE_SEL_CHANGING, GetId()); - event.SetEventObject(this); - ProcessEvent(event); + SendMessage(wxEVT_COMMAND_TREE_SEL_CHANGING, item); } // Tree event