1 //
2 // "$Id$"
3 //
4 //	Simple Fl_Tree widget example. - erco 06/05/2010
5 //
6 // Copyright 2010 Greg Ercolano.
7 // Copyright 1998-2010 by Bill Spitzak and others.
8 //
9 // This library is free software. Distribution and use rights are outlined in
10 // the file "COPYING" which should have been included with this file.  If this
11 // file is missing or damaged, see the license at:
12 //
13 //     http://www.fltk.org/COPYING.php
14 //
15 // Please report all bugs and problems on the following page:
16 //
17 //     http://www.fltk.org/str.php
18 //
19 #include <stdio.h>
20 #include <FL/Fl.H>
21 #include <FL/Fl_Double_Window.H>
22 #include <FL/Fl_Tree.H>
23 
24 // Tree's callback
25 //    Invoked whenever an item's state changes.
26 //
TreeCallback(Fl_Widget * w,void * data)27 void TreeCallback(Fl_Widget *w, void *data) {
28   Fl_Tree *tree = (Fl_Tree*)w;
29   Fl_Tree_Item *item = (Fl_Tree_Item*)tree->callback_item();
30   if ( ! item ) return;
31   switch ( tree->callback_reason() ) {
32     case FL_TREE_REASON_SELECTED: {
33       char pathname[256];
34       tree->item_pathname(pathname, sizeof(pathname), item);
35       fprintf(stderr, "TreeCallback: Item selected='%s', Full pathname='%s'\n", item->label(), pathname);
36       break;
37     }
38     case FL_TREE_REASON_DESELECTED:
39       // fprintf(stderr, "TreeCallback: Item '%s' deselected\n", item->label());
40       break;
41     case FL_TREE_REASON_OPENED:
42       // fprintf(stderr, "TreeCallback: Item '%s' opened\n", item->label());
43       break;
44     case FL_TREE_REASON_CLOSED:
45       // fprintf(stderr, "TreeCallback: Item '%s' closed\n", item->label());
46       break;
47 #if FLTK_ABI_VERSION >= 10301
48     // To enable this callback, use tree->item_reselect_mode(FL_TREE_SELECTABLE_ALWAYS);
49     case FL_TREE_REASON_RESELECTED:
50       // fprintf(stderr, "TreeCallback: Item '%s' reselected\n", item->label());
51       break;
52 #endif
53     default:
54       break;
55   }
56 }
57 
main(int argc,char * argv[])58 int main(int argc, char *argv[]) {
59   Fl::scheme("gtk+");
60   Fl_Double_Window *win = new Fl_Double_Window(250, 400, "Simple Tree");
61   win->begin();
62   {
63     // Create the tree
64     Fl_Tree *tree = new Fl_Tree(10, 10, win->w()-20, win->h()-20);
65     tree->showroot(0);				// don't show root of tree
66     tree->callback(TreeCallback);		// setup a callback for the tree
67 
68     // Add some items
69     tree->add("Flintstones/Fred");
70     tree->add("Flintstones/Wilma");
71     tree->add("Flintstones/Pebbles");
72     tree->add("Simpsons/Homer");
73     tree->add("Simpsons/Marge");
74     tree->add("Simpsons/Bart");
75     tree->add("Simpsons/Lisa");
76     tree->add("Pathnames/\\/bin");		// front slashes
77     tree->add("Pathnames/\\/usr\\/sbin");
78     tree->add("Pathnames/C:\\\\Program Files");	// backslashes
79     tree->add("Pathnames/C:\\\\Documents and Settings");
80 
81     // Start with some items closed
82     tree->close("Simpsons");
83     tree->close("Pathnames");
84   }
85   win->end();
86   win->resizable(win);
87   win->show(argc, argv);
88   return(Fl::run());
89 }
90 
91 //
92 // End of "$Id$".
93 //
94