Wt examples 3.3.12
DemoTreeList.C
Go to the documentation of this file.
1/*
2 * Copyright (C) 2008 Emweb bvba, Kessel-Lo, Belgium.
3 *
4 * See the LICENSE file for terms of use.
5 */
6#include <boost/lexical_cast.hpp>
7
8#include <Wt/WApplication>
9#include <Wt/WText>
10#include <Wt/WImage>
11#include <Wt/WPushButton>
12
13#include "DemoTreeList.h"
14#include "TreeNode.h"
15#include "IconPair.h"
16
17using namespace Wt;
18using std::rand;
19
21 : WContainerWidget(parent),
22 testCount_(0)
23{
25 (new WText("<h2>Wt Tree List example</h2>"
26 "<p>This is a simple demo of a treelist, implemented using"
27 " <a href='http://witty.sourceforge.net/'>Wt</a>.</p>"
28 "<p>The leafs of the tree contain the source code of the "
29 "tree-list in the classes <b>TreeNode</b> and "
30 "<b>IconPair</b>, as well as the implementation of this "
31 "demo itself in the class <b>DemoTreeList</b>.</p>"));
32
33 tree_ = makeTreeMap("Examples", 0);
35
36 TreeNode *treelist = makeTreeMap("Tree List", tree_);
37 TreeNode *wstateicon = makeTreeMap("class IconPair", treelist);
38 makeTreeFile("<a href=\"IconPair.h\">IconPair.h</a>", wstateicon);
39 makeTreeFile("<a href=\"IconPair.C\">IconPair.C</a>", wstateicon);
40 TreeNode *wtreenode = makeTreeMap("class TreeNode", treelist);
41 makeTreeFile("<a href=\"TreeNode.h\">TreeNode.h</a>", wtreenode);
42 makeTreeFile("<a href=\"TreeNode.C\">TreeNode.C</a>", wtreenode);
43 TreeNode *demotreelist = makeTreeMap("class DemoTreeList", treelist);
44 makeTreeFile("<a href=\"DemoTreeList.h\">DemoTreeList.h</a>", demotreelist);
45 makeTreeFile("<a href=\"DemoTreeList.C\">DemoTreeList.C</a>", demotreelist);
46
47 testMap_ = makeTreeMap("Test map", tree_);
48
49 /*
50 * Buttons to dynamically demonstrate changing the tree contents.
51 */
53 (new WText("<p>Use the following buttons to change the tree "
54 "contents:</p>"));
55
57 = new WPushButton("Add map", this);
59
61 = new WPushButton("Remove map", this);
64
66 (new WText("<p>Remarks:"
67 "<ul>"
68 "<li><p>This is not the instantiation of a pre-defined "
69 "tree list component, but the full implementation of such "
70 "a component, in about 350 lines of C++ code !</p> "
71 "<p>In comparison, the <a href='http://myfaces.apache.org'> "
72 "Apache MyFaces</a> JSF implementation of tree2, with similar "
73 "functionality, uses about 2400 lines of Java, and 140 lines "
74 "of JavaScript code.</p></li>"
75 "<li><p>Once loaded, the tree list does not require any "
76 "interaction with the server for handling the click events on "
77 "the <img src='icons/nav-plus-line-middle.gif' /> and "
78 "<img src='icons/nav-minus-line-middle.gif' /> icons, "
79 "because these events have been connected to slots using "
80 "STATIC connections. Such connections are converted to the "
81 "appropriate JavaScript code that is inserted into the page. "
82 "Still, the events are signaled to the server to update the "
83 "application state.</p></li>"
84 "<li><p>In contrast, the buttons for manipulating the tree "
85 "contents use DYNAMIC connections, and thus the update "
86 "is computed at server-side, and communicated back to the "
87 "browser (by default using AJAX).</p></li>"
88 "<li><p>When loading a page, only visible widgets (that are not "
89 "<b>setHidden(true)</b>) are transmitted. "
90 "The remaining widgets are loaded in the background after "
91 "rendering the page. "
92 "As a result the application is loaded as fast as possible.</p>"
93 "</li>"
94 "<li><p>The browser reload button is supported and behaves as "
95 "expected: the page is reloaded from the server. Again, "
96 "only visible widgets are transmitted immediately.</p> "
97 "<p>(For the curious, this is the way to see the actual "
98 "HTML/JavaScript code !)</p></li>"
99 "</ul></p>"));
100}
101
103{
104 TreeNode *node
105 = makeTreeMap("Map " + boost::lexical_cast<std::string>(++testCount_),
106 testMap_);
107 makeTreeFile("File " + boost::lexical_cast<std::string>(testCount_),
108 node);
109
111}
112
114{
115 int numMaps = testMap_->childNodes().size();
116
117 if (numMaps > 0) {
118 int c = rand() % numMaps;
119
120 TreeNode *child = testMap_->childNodes()[c];
122 delete child;
123
124 if (numMaps == 1)
126 }
127}
128
129TreeNode *DemoTreeList::makeTreeMap(const std::string name, TreeNode *parent)
130{
131 IconPair *labelIcon
132 = new IconPair("icons/yellow-folder-closed.png",
133 "icons/yellow-folder-open.png",
134 false);
135
136 TreeNode *node = new TreeNode(name, PlainText, labelIcon, 0);
137 if (parent)
138 parent->addChildNode(node);
139
140 return node;
141}
142
143TreeNode *DemoTreeList::makeTreeFile(const std::string name,
144 TreeNode *parent)
145{
146 IconPair *labelIcon
147 = new IconPair("icons/document.png", "icons/yellow-folder-open.png",
148 false);
149
150 TreeNode *node = new TreeNode(name, XHTMLText, labelIcon, 0);
151 if (parent)
152 parent->addChildNode(node);
153
154 return node;
155}
156
158{
159 WApplication *app = new WApplication(env);
160 new DemoTreeList(app->root());
161
162 /*
163 * The look & feel of the tree node is configured using a CSS style sheet.
164 * If you are not familiar with CSS, you can use the WCssDecorationStyle
165 * class ...
166 */
167 WCssDecorationStyle treeNodeLabelStyle;
168 treeNodeLabelStyle.font().setFamily(WFont::Serif, "Helvetica");
169 app->styleSheet().addRule(".treenodelabel", treeNodeLabelStyle);
170
171 /*
172 * ... or if you speak CSS fluently, you can add verbatim rules.
173 */
174 app->styleSheet().addRule(".treenodechildcount",
175 "color:blue; font-family:Helvetica,serif;");
176
177 return app;
178}
179
180int main(int argc, char **argv)
181{
182 return WRun(argc, argv, &createApplication);
183}
184
WApplication * createApplication(const WEnvironment &env)
int main(int argc, char **argv)
A demonstration of the treelist.
void removeMap()
Remove a map.
Wt::WPushButton * addMapButton_
void addMap()
Add a map.
DemoTreeList(Wt::WContainerWidget *parent)
Create a DemoTreeList.
TreeNode * tree_
Wt::WPushButton * removeMapButton_
TreeNode * makeTreeMap(const std::string name, TreeNode *parent)
Create a "map" node, and insert in the given parent.
TreeNode * makeTreeFile(const std::string name, TreeNode *parent)
Create a "file" node, and insert in the given parent.
TreeNode * testMap_
An icon pair (identical to WIconPair)
Definition IconPair.h:35
Example implementation of a single tree list node.
Definition TreeNode.h:56
const std::vector< TreeNode * > & childNodes() const
Returns the list of children.
Definition TreeNode.h:82
void removeChildNode(TreeNode *node)
Removes a child node.
Definition TreeNode.C:96
WCssStyleSheet & styleSheet()
WContainerWidget * root() const
virtual void addWidget(WWidget *widget)
WCssTextRule * addRule(const std::string &selector, const WString &declarations, const std::string &ruleName=std::string())
void setFamily(GenericFamily genericFamily, const WString &specificFamilies=WString())
EventSignal< WMouseEvent > & clicked()
void disable()
WWidget * parent() const
void enable()
XHTMLText
PlainText

Generated on Fri May 17 2024 for the C++ Web Toolkit (Wt) by doxygen 1.9.8