1 // tree.cpp: implementation of the CTree class.
3 //////////////////////////////////////////////////////////////////////
10 extern CMainWnd *g_pMainWnd;
13 static void button_release (GtkWidget *widget, GdkEventButton *event, gpointer data)
16 g_pMainWnd->HandleDrop();
19 //////////////////////////////////////////////////////////////////////
20 // Construction/Destruction
21 //////////////////////////////////////////////////////////////////////
35 void CTree::Init(GtkWidget *win)
40 m_Tree=gtk_tree_new();
42 // Add tree to passed scroll window
43 gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW(win),(GtkWidget*)m_Tree);
46 gtk_widget_show(m_Tree);
49 gtk_signal_connect (GTK_OBJECT(m_Tree), "select_child",
50 GTK_SIGNAL_FUNC(cb_select_child), m_Tree);
52 // create the root item
53 GtkWidget *item=gtk_tree_item_new_with_label("root");
54 gtk_tree_append(GTK_TREE(m_Tree),item);
55 gtk_widget_show(item);
57 gtk_signal_connect (GTK_OBJECT (item), "button_release_event",GTK_SIGNAL_FUNC (button_release), NULL);
59 m_Root=gtk_tree_new();
60 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item),m_Root);
61 gtk_tree_item_expand(GTK_TREE_ITEM(item));
65 gtk_signal_connect (GTK_OBJECT(m_Root), "select_child",
66 GTK_SIGNAL_FUNC(cb_select_child), m_Tree);
74 gtk_widget_destroy(m_Tree);
78 void CTree::AddPath(const char *buff)
83 for (int c=0 ; c<path.GetLength() ; c++)
85 if (path.GetAt(c)=='\\')
92 // lets tokenize it first
93 if (path.GetAt(path.GetLength()-1)=='/')
95 // its just a folder path, no file
99 GtkWidget *node=m_Root;
101 long pos=path.Find('/');
104 Str folder=path.Left(pos);
105 path=path.Mid(pos+1);
107 // add a folder, or retrieve its node if it already exists
108 node=AddFolder(folder.GetBuffer(),node);
113 // add the filename to the final nested node
114 AddFile(path.GetBuffer(),node);
118 void CTree::AddFile(const char *buff, GtkWidget *node)
125 GtkWidget *item=gtk_tree_item_new_with_label(buff);
126 gtk_tree_append(GTK_TREE(node),item);
127 gtk_widget_show(item);
129 gtk_widget_set_name(item,file);
131 // gtk_signal_connect (GTK_OBJECT(item), "select_child",
132 // GTK_SIGNAL_FUNC(cb_select_child), item);
134 gtk_signal_connect (GTK_OBJECT (item), "button_release_event",GTK_SIGNAL_FUNC (button_release), NULL);
137 // a file is the last item in the tree so no tree is needed
140 GtkWidget* CTree::AddFolder(const char *buff,GtkWidget *node)
147 // search the node's siblings for the folder
148 GList *children = gtk_container_children (GTK_CONTAINER (node));
151 GtkTreeItem *tree_item=(GTK_TREE_ITEM (children->data));
153 // check item for equality with the folder
154 // and weather it has a tree attached
156 if (GTK_TREE_ITEM_SUBTREE(tree_item))
158 GtkItem *item=GTK_ITEM(tree_item);
160 GList *item_children=gtk_container_children(GTK_CONTAINER(item));
162 GtkLabel *label=GTK_LABEL(item_children->data);
164 if (strcmp(label->label,buff)==0)
166 g_list_free(item_children);
167 g_list_free(children);
169 // return tree attached to the item
170 return GTK_TREE_ITEM_SUBTREE(tree_item);
173 g_list_free(item_children);
176 children = g_list_remove_link (children, children);
179 g_list_free(children);
181 // ok, not found so we add it
182 GtkWidget *item=gtk_tree_item_new_with_label(buff);
183 gtk_tree_append(GTK_TREE(node),item);
184 gtk_widget_show(item);
186 GtkWidget *tree=gtk_tree_new();
187 gtk_tree_item_set_subtree(GTK_TREE_ITEM(item),tree);
188 gtk_widget_set_name(tree,buff);
190 gtk_signal_connect (GTK_OBJECT(tree), "select_child",
191 GTK_SIGNAL_FUNC(cb_select_child), tree);
193 gtk_signal_connect (GTK_OBJECT (item), "button_release_event",GTK_SIGNAL_FUNC (button_release), NULL);
196 //gtk_signal_connect (GTK_OBJECT(tree), "selection_changed",
197 // GTK_SIGNAL_FUNC(cb_selection_changed), tree, this);
203 std::string CTree::GetFullPath(GtkWidget *child)
208 GtkLabel *label = GTK_LABEL (GTK_BIN (child)->child);
209 gtk_label_get (label, &name);
212 if (path.compare("root")==0)
217 // this gets an items parent tree
218 GtkWidget *tr=(child->parent);
220 if (GTK_WIDGET(tr)==m_Root)
224 child=GTK_TREE(tr)->tree_owner;
226 // and now the items label
227 label = GTK_LABEL (GTK_BIN (child)->child);
228 gtk_label_get (label, &name);
229 std::string lbl=name;
236 void CTree::RemovePath(const char *path)
242 void CTree::SelectChild(GtkWidget *child)
244 g_pMainWnd->UpdateStatus();
245 g_pMainWnd->UpdateToolBar();
248 void cb_select_child (GtkWidget *root_tree, GtkWidget *child, GtkWidget *subtree)
250 g_pMainWnd->m_Tree.SelectChild(child);
254 std::string CTree::GetSelected()
258 if (!GTK_TREE(m_Tree)->selection)
261 GList *selected = (GTK_TREE(m_Tree)->selection);
263 GtkWidget *item=GTK_WIDGET(selected->data);
265 if (GTK_TREE_ITEM(item)->subtree)
268 path=GetFullPath(item);