4 /****************************** GLUI_TreePanel::GLUI_TreePanel() *********/
6 GLUI_TreePanel::GLUI_TreePanel(GLUI_Node *parent, const char *name,
16 h = GLUI_DEFAULT_CONTROL_HEIGHT + 7;
19 parent->add_control( this );
22 /****************************** GLUI_TreePanel::set_color() *********/
24 void GLUI_TreePanel::set_color(float r, float g, float b)
32 /************************ GLUI_TreePanel::set_level_color() *********/
34 void GLUI_TreePanel::set_level_color(float r, float g, float b)
42 /****************************** GLUI_TreePanel::ab() *********/
44 /* Adds branch to curr_root */
45 GLUI_Tree *GLUI_TreePanel::ab(const char *name, GLUI_Tree *root)
54 temp = new GLUI_Tree(curr_root, name);
59 curr_branch = NULL; /* Currently at leaf */
61 if (temp->dynamicCastGLUI_Tree())
62 ((GLUI_Tree *)temp)->set_current(true);
64 // glui->deactivate_current_control();
65 //glui->activate_control( temp, GLUI_ACTIVATE_TAB );
70 /****************************** GLUI_TreePanel::fb() *********/
72 /* Goes up one level, resets curr_root and curr_branch to parents*/
73 void GLUI_TreePanel::fb(GLUI_Tree *branch)
75 if (((GLUI_Panel *)branch) == ((GLUI_Panel *)this))
78 if (((GLUI_Panel *)curr_branch) == ((GLUI_Panel *)this)) {
82 if (((GLUI_Panel *)curr_root) == ((GLUI_Panel *)this)) {
89 if ( branch->dynamicCastGLUI_Tree() )
90 ((GLUI_Tree *)branch)->set_current(false);
92 curr_branch = (GLUI_Tree *)branch->next();
93 curr_root = (GLUI_Panel *)branch->parent();
95 if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
96 curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
99 if ( curr_root->dynamicCastGLUI_Tree() )
100 ((GLUI_Tree *)curr_root)->set_current(true);
103 if (curr_root != NULL) { /* up one parent */
105 if (curr_root->dynamicCastGLUI_Tree())
106 ((GLUI_Tree *)curr_root)->set_current(false);
108 curr_branch = (GLUI_Tree *) curr_root->next();
109 curr_root = (GLUI_Panel *) curr_root->parent();
111 if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
112 curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
114 if (curr_root->dynamicCastGLUI_Tree())
115 ((GLUI_Tree *)curr_root)->set_current(true);
124 /****************************** GLUI_TreePanel::refresh() *********/
126 void GLUI_TreePanel::refresh()
128 glui->deactivate_current_control();
129 glui->activate_control( curr_root, GLUI_ACTIVATE_TAB );
134 /****************************** GLUI_TreePanel::initNode() *********/
136 void GLUI_TreePanel::initNode(GLUI_Tree *temp)
140 int level = temp->get_level();
141 int child_number = 1;
143 GLUI_Tree *ptree = temp->parent()->dynamicCastGLUI_Tree();
145 level = ptree->get_level() + 1;
146 GLUI_Tree *prevTree = temp->prev()->dynamicCastGLUI_Tree();
148 child_number = prevTree->get_child_number() + 1;
150 } else if (temp->dynamicCastGLUI_Tree() &&
151 temp->parent()->dynamicCastGLUI_TreePanel()) {
152 child_number = ++root_children;
154 temp->set_id(uniqueID()); // -1 if unset
155 temp->set_level(level);
156 temp->set_child_number(child_number);
159 /****************************** GLUI_TreePanel::formatNode() *********/
161 void GLUI_TreePanel::formatNode(GLUI_Tree *temp)
165 int level = temp->get_level();
166 int child_number = temp->get_child_number();
167 GLUI_String level_name="";
168 GLUI_String full_name="";
170 temp->level_name == "";
172 if (format & GLUI_TREEPANEL_DISPLAY_HIERARCHY) {
173 if (format & GLUI_TREEPANEL_HIERARCHY_LEVEL_ONLY) {
174 glui_format_str(level_name, "%d", level);
176 if (format & GLUI_TREEPANEL_HIERARCHY_NUMERICDOT) {
177 if ( temp->parent()->dynamicCastGLUI_Tree() )
178 glui_format_str(level_name, "%s.%d",
179 ((GLUI_Tree *)(temp->parent()))->level_name.c_str(),
182 glui_format_str(level_name, "%d", child_number);
186 temp->set_level_color(lred, lgreen, lblue);
187 temp->set_format(format);
188 temp->level_name = level_name;
190 if (format & GLUI_TREEPANEL_ALTERNATE_COLOR) {
192 case (7): temp->set_color(.5,.5,.5); break;
193 case (6): temp->set_color(.3,.5,.5); break;
194 case (5): temp->set_color(.5,.3,.5); break;
195 case (4): temp->set_color(.3,.3,.5); break;
196 case (3): temp->set_color(.5,.5,.3); break;
197 case (2): temp->set_color(.3,.5,.3); break;
198 case (1): temp->set_color(.5,.3,.3); break;
199 default: temp->set_color(.3,.3,.3);
202 temp->set_color(red,green,blue);
205 if (format & GLUI_TREEPANEL_DISABLE_BAR) {
208 if (format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
210 if ( curr_root->dynamicCastGLUI_Tree() )
211 ((GLUI_Tree *)curr_root)->enable_bar();
213 if (format & GLUI_TREEPANEL_CONNECT_CHILDREN_ONLY) {
215 if (temp->prev() && temp->prev()->dynamicCastGLUI_Tree() )
217 ((GLUI_Tree *)temp->prev())->enable_bar();
223 /****************************** GLUI_TreePanel::update_all() *********/
225 void GLUI_TreePanel::update_all()
227 printf("GLUI_TreePanel::update_all() doesn't work yet. - JVK\n");
229 GLUI_Panel *saved_root = curr_root;
230 GLUI_Tree *saved_branch = curr_branch;
233 if (curr_branch && curr_branch->dynamicCastGLUI_Tree())
234 formatNode((GLUI_Tree *)curr_branch);
236 while (curr_root && curr_branch != this->first_child()) {
237 if (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
238 formatNode((GLUI_Tree *)curr_branch);
242 curr_root = saved_root;
243 curr_branch = saved_branch;
246 /****************************** GLUI_TreePanel::expand_all() *********/
248 void GLUI_TreePanel::expand_all()
250 GLUI_Panel *saved_root = curr_root;
251 GLUI_Tree *saved_branch = curr_branch;
254 if (curr_root->dynamicCastGLUI_Tree())
255 ((GLUI_Tree*)curr_root)->open();
257 while (curr_root != NULL && curr_branch != this->first_child()) {
258 if (curr_root->dynamicCastGLUI_Tree())
259 ((GLUI_Tree*)curr_root)->open();
263 curr_root = saved_root;
264 curr_branch = saved_branch;
267 /****************************** GLUI_TreePanel::collapse_all() *********/
269 void GLUI_TreePanel::collapse_all()
271 GLUI_Panel *saved_root = curr_root;
272 GLUI_Tree *saved_branch = curr_branch;
276 while (curr_root != NULL && curr_branch != this->first_child()) {
277 if (curr_root->dynamicCastGLUI_Tree() &&
278 curr_branch == NULL) { /* we want to close everything leaf-first */
279 ((GLUI_Tree*)curr_root)->close();
280 /* Rather than simply next(), we need to manually move the
281 curr_root because this node has been moved to the
282 collapsed_node list */
283 curr_branch = (GLUI_Tree *)curr_root->next();
284 curr_root = (GLUI_Panel *)curr_root->parent();
289 curr_root = saved_root;
290 curr_branch = saved_branch;
294 /****************************** GLUI_TreePanel::db() *********/
296 /* Deletes the curr_root */
297 void GLUI_TreePanel::db(GLUI_Tree *root)
299 GLUI_Tree *temp_branch;
300 GLUI_Panel *temp_root;
302 if (((GLUI_Control *)root) == ((GLUI_Control *)this))
306 curr_root = (GLUI_Tree *)root;
310 if (curr_root == NULL || ((GLUI_Panel *)curr_root) == ((GLUI_Panel *)this)) {
316 temp_branch = (GLUI_Tree *)curr_root->next(); /* Next branch, if any */
317 temp_root = (GLUI_Panel *)curr_root->parent(); /* new root */
320 curr_branch = (GLUI_Tree *) temp_branch;
321 curr_root = (GLUI_Panel *) temp_root;
322 if (curr_root->dynamicCastGLUI_Tree())
323 ((GLUI_Tree *)curr_root)->open();
325 if ((format & GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) == GLUI_TREEPANEL_DISABLE_DEEPEST_BAR) {
326 if (curr_root->dynamicCastGLUI_Tree() && ((GLUI_Tree *)curr_root->next()) == NULL)
327 ((GLUI_Tree *)curr_root)->disable_bar();
332 /****************************** GLUI_TreePanel::descendBranch() *********/
334 /* Finds the very last branch of curr_root, resets vars */
335 void GLUI_TreePanel::descendBranch(GLUI_Panel *root) {
339 resetToRoot(curr_root);
340 if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) {
341 if (curr_root->dynamicCastGLUI_Tree())
342 ((GLUI_Tree *)curr_root)->set_current(false);
343 descendBranch(curr_branch);
347 /****************************** GLUI_TreePanel::next() *********/
349 void GLUI_TreePanel::next()
351 if (curr_root == NULL)
354 if (curr_branch == NULL && (curr_root->collapsed_node).first_child() != NULL)
355 curr_branch = (GLUI_Tree *)(curr_root->collapsed_node).first_child();
358 if (curr_branch != NULL && curr_branch != ((GLUI_Panel *)this)) { /* Descend into branch */
359 if (curr_root->dynamicCastGLUI_Tree())
360 ((GLUI_Tree *)curr_root)->set_current(false);
361 resetToRoot(curr_branch);
362 } else if (curr_branch == NULL) {
363 fb(NULL); /* Backup and move on */
367 /****************************** GLUI_TreePanel::resetToRoot() *********/
369 /* Resets curr_root and curr branch to TreePanel and lastChild */
370 void GLUI_TreePanel::resetToRoot(GLUI_Panel *new_root)
372 GLUI_Panel *root = this;
373 if (new_root != NULL)
376 if (curr_root->dynamicCastGLUI_Tree())
377 ((GLUI_Tree *)curr_root)->set_current(true);
378 curr_branch = (GLUI_Tree *)root->first_child();
380 /* since Trees are collapsable, we need to check the collapsed nodes
381 in case the curr_root is collapsed */
382 if (curr_branch == NULL && (root->collapsed_node).first_child() != NULL) {
383 curr_branch = (GLUI_Tree *)(root->collapsed_node).first_child();
385 while (curr_branch && curr_branch->dynamicCastGLUI_Tree()) {
386 curr_branch=(GLUI_Tree *)curr_branch->next();