diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/tests/menutest.cc | 155 |
1 files changed, 155 insertions, 0 deletions
diff --git a/src/tests/menutest.cc b/src/tests/menutest.cc new file mode 100644 index 0000000..07add9f --- /dev/null +++ b/src/tests/menutest.cc | |||
@@ -0,0 +1,155 @@ | |||
1 | // menutest.cc a test app for Menus | ||
2 | // Copyright (c) 2005 Henrik Kinnunen (fluxgen<at>fluxbox dot org) | ||
3 | // | ||
4 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
5 | // copy of this software and associated documentation files (the "Software"), | ||
6 | // to deal in the Software without restriction, including without limitation | ||
7 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
8 | // and/or sell copies of the Software, and to permit persons to whom the | ||
9 | // Software is furnished to do so, subject to the following conditions: | ||
10 | // | ||
11 | // The above copyright notice and this permission notice shall be included in | ||
12 | // all copies or substantial portions of the Software. | ||
13 | // | ||
14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
17 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
19 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
20 | // DEALINGS IN THE SOFTWARE. | ||
21 | |||
22 | // $Id: menutest.cc 3700 2004-11-19 11:37:27Z mathias $ | ||
23 | |||
24 | #include "App.hh" | ||
25 | #include "FbWindow.hh" | ||
26 | #include "Font.hh" | ||
27 | #include "EventHandler.hh" | ||
28 | #include "EventManager.hh" | ||
29 | #include "GContext.hh" | ||
30 | #include "Color.hh" | ||
31 | #include "Menu.hh" | ||
32 | #include "MenuItem.hh" | ||
33 | #include "MenuSeparator.hh" | ||
34 | #include "StringUtil.hh" | ||
35 | #include "ImageControl.hh" | ||
36 | |||
37 | #include "../FbMenuParser.hh" | ||
38 | |||
39 | #include <X11/Xutil.h> | ||
40 | #include <X11/keysym.h> | ||
41 | |||
42 | #include <string> | ||
43 | #include <iostream> | ||
44 | using namespace std; | ||
45 | |||
46 | void doSubmenu(Parser &parser, FbTk::Menu &menu, | ||
47 | FbTk::MenuTheme &theme, | ||
48 | FbTk::ImageControl &image_ctrl, | ||
49 | const std::string labelstr) { | ||
50 | |||
51 | Parser::Item key, label, cmd, icon; | ||
52 | |||
53 | FbTk::Menu *submenu = new FbTk::Menu(theme, image_ctrl); | ||
54 | submenu->setLabel(labelstr.c_str()); | ||
55 | menu.insert(labelstr.c_str(), submenu); | ||
56 | // skip submenu items | ||
57 | if (key.second == "begin") { | ||
58 | while (key.second != "end") { | ||
59 | parser>>key>>label>>cmd>>icon; | ||
60 | if (key.second == "begin") | ||
61 | doSubmenu(parser, *submenu, | ||
62 | theme, image_ctrl, | ||
63 | label.second); | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | |||
68 | class App:public FbTk::App, public FbTk::EventHandler { | ||
69 | public: | ||
70 | App(const char *displayname, const std::string &stylefile, const std::string &menufile): | ||
71 | FbTk::App(displayname), | ||
72 | m_image_ctrl(DefaultScreen(display())), | ||
73 | m_menu_theme(DefaultScreen(display())), | ||
74 | m_menu(m_menu_theme, m_image_ctrl) { | ||
75 | |||
76 | m_menu_theme.frameFont().setAntialias(true); | ||
77 | m_menu_theme.titleFont().setAntialias(true); | ||
78 | |||
79 | cerr<<"Loading menu: "<<menufile<<endl; | ||
80 | FbMenuParser parser(menufile); | ||
81 | if (parser.isLoaded()) { | ||
82 | // get start of file | ||
83 | Parser::Item key, label, cmd, icon; | ||
84 | while (!parser.eof()) { | ||
85 | // get first begin line | ||
86 | parser>>key>>label>>cmd>>icon; | ||
87 | if (key.second == "begin") | ||
88 | break; | ||
89 | |||
90 | } | ||
91 | |||
92 | m_menu.setLabel(label.second.c_str()); | ||
93 | |||
94 | while (!parser.eof()) { | ||
95 | parser>>key>>label>>cmd>>icon; | ||
96 | if (key.second == "end") | ||
97 | break; | ||
98 | |||
99 | string iconfile = icon.second; | ||
100 | if (key.second == "separator") | ||
101 | m_menu.insert(new FbTk::MenuSeparator()); | ||
102 | else if (key.second == "begin") { // new submenu | ||
103 | doSubmenu(parser, m_menu, | ||
104 | m_menu_theme, | ||
105 | m_image_ctrl, | ||
106 | label.second); | ||
107 | } else if (key.second != "styles" && | ||
108 | key.second != "stylesdir") | ||
109 | m_menu.insert(label.second.c_str()); | ||
110 | |||
111 | |||
112 | // set icon on items | ||
113 | if (!iconfile.empty()) { | ||
114 | FbTk::MenuItem *item = m_menu.find(m_menu.numberOfItems() - 1); | ||
115 | item->setIcon(iconfile, m_menu.screenNumber()); | ||
116 | } | ||
117 | } | ||
118 | |||
119 | } | ||
120 | |||
121 | cerr<<"Loading style: "<<stylefile<<endl; | ||
122 | FbTk::ThemeManager::instance().load(stylefile); | ||
123 | |||
124 | m_menu.show(); | ||
125 | |||
126 | } | ||
127 | |||
128 | ~App() { | ||
129 | } | ||
130 | |||
131 | |||
132 | private: | ||
133 | FbTk::ImageControl m_image_ctrl; | ||
134 | FbTk::MenuTheme m_menu_theme; | ||
135 | FbTk::Menu m_menu; | ||
136 | }; | ||
137 | |||
138 | int main(int argc, char **argv) { | ||
139 | string displayname(""); | ||
140 | string stylefile, menufile="~/.fluxbox/menu"; | ||
141 | for (int a=1; a<argc; ++a) { | ||
142 | if (strcmp("-display", argv[a]) == 0 && a + 1 < argc) { | ||
143 | displayname = argv[++a]; | ||
144 | } else if (strcmp("-style", argv[a]) == 0 && a + 1 < argc) { | ||
145 | stylefile = argv[++a]; | ||
146 | } else if (strcmp("-menu", argv[a]) == 0 && a + 1 < argc) { | ||
147 | menufile = argv[++a]; | ||
148 | } | ||
149 | } | ||
150 | menufile = FbTk::StringUtil::expandFilename(menufile); | ||
151 | App app(displayname.c_str(), stylefile, menufile); | ||
152 | |||
153 | app.eventLoop(); | ||
154 | |||
155 | } | ||