aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPavel Labath <pavelo@centrum.sk>2011-07-01 16:22:02 (GMT)
committerPavel Labath <pavelo@centrum.sk>2011-07-01 16:22:02 (GMT)
commitcf91b0e81ba4b14bfc095f7bbfe17dfe8da87bb7 (patch)
treebb9b304b413bab52ee8321814ca7dca3f98d6daf /src
parent4ef496df68eb7c778e4624f924de7f93829b7ae0 (diff)
downloadfluxbox_pavel-cf91b0e81ba4b14bfc095f7bbfe17dfe8da87bb7.zip
fluxbox_pavel-cf91b0e81ba4b14bfc095f7bbfe17dfe8da87bb7.tar.bz2
Handle all menu entry types when loading from lua
Diffstat (limited to 'src')
-rw-r--r--src/MenuCreator.cc209
1 files changed, 33 insertions, 176 deletions
diff --git a/src/MenuCreator.cc b/src/MenuCreator.cc
index 60669db..2c99d61 100644
--- a/src/MenuCreator.cc
+++ b/src/MenuCreator.cc
@@ -71,13 +71,16 @@ FbTk::StringConvertor MenuCreator::m_stringconvertor(FbTk::StringConvertor::ToFb
71 71
72namespace { 72namespace {
73 73
74void createStyleMenu(FbTk::Menu &parent, const string &label, 74std::auto_ptr<FbMenu> createStyleMenu(int screen_number, const string &label,
75 AutoReloadHelper *reloader, const string &directory) { 75 AutoReloadHelper *reloader, const string &directory) {
76
77 std::auto_ptr<FbMenu> menu(MenuCreator::createMenu(label, screen_number));
78
76 // perform shell style ~ home directory expansion 79 // perform shell style ~ home directory expansion
77 string stylesdir(FbTk::StringUtil::expandFilename(directory)); 80 string stylesdir(FbTk::StringUtil::expandFilename(directory));
78 81
79 if (!FbTk::FileUtil::isDirectory(stylesdir.c_str())) 82 if (!FbTk::FileUtil::isDirectory(stylesdir.c_str()))
80 return; 83 return menu;
81 84
82 if (reloader) 85 if (reloader)
83 reloader->addFile(stylesdir); 86 reloader->addFile(stylesdir);
@@ -102,21 +105,24 @@ void createStyleMenu(FbTk::Menu &parent, const string &label,
102 (style[style.length() - 1] != '~')) || 105 (style[style.length() - 1] != '~')) ||
103 FbTk::FileUtil::isRegularFile((style + "/theme.cfg").c_str()) || 106 FbTk::FileUtil::isRegularFile((style + "/theme.cfg").c_str()) ||
104 FbTk::FileUtil::isRegularFile((style + "/style.cfg").c_str())) 107 FbTk::FileUtil::isRegularFile((style + "/style.cfg").c_str()))
105 parent.insert(new StyleMenuItem(filelist[file_index], style)); 108 menu->insert(new StyleMenuItem(filelist[file_index], style));
106 } 109 }
107 // update menu graphics 110 // update menu graphics
108 parent.updateMenu(); 111 menu->updateMenu();
109 112 return menu;
110} 113}
111 114
112void createRootCmdMenu(FbTk::Menu &parent, const string &label, 115std::auto_ptr<FbMenu> createRootCmdMenu(int screen_number, const string &label,
113 const string &directory, AutoReloadHelper *reloader, 116 const string &directory, AutoReloadHelper *reloader,
114 const string &cmd) { 117 const string &cmd) {
118
119 std::auto_ptr<FbMenu> menu(MenuCreator::createMenu(label, screen_number));
120
115 // perform shell style ~ home directory expansion 121 // perform shell style ~ home directory expansion
116 string rootcmddir(FbTk::StringUtil::expandFilename(directory)); 122 string rootcmddir(FbTk::StringUtil::expandFilename(directory));
117 123
118 if (!FbTk::FileUtil::isDirectory(rootcmddir.c_str())) 124 if (!FbTk::FileUtil::isDirectory(rootcmddir.c_str()))
119 return; 125 return menu;
120 126
121 if (reloader) 127 if (reloader)
122 reloader->addFile(rootcmddir); 128 reloader->addFile(rootcmddir);
@@ -140,11 +146,11 @@ void createRootCmdMenu(FbTk::Menu &parent, const string &label,
140 if ((FbTk::FileUtil::isRegularFile(rootcmd.c_str()) && 146 if ((FbTk::FileUtil::isRegularFile(rootcmd.c_str()) &&
141 (filelist[file_index][0] != '.') && 147 (filelist[file_index][0] != '.') &&
142 (rootcmd[rootcmd.length() - 1] != '~'))) 148 (rootcmd[rootcmd.length() - 1] != '~')))
143 parent.insert(new RootCmdMenuItem(filelist[file_index], rootcmd, cmd)); 149 menu->insert(new RootCmdMenuItem(filelist[file_index], rootcmd, cmd));
144 } 150 }
145 // update menu graphics 151 // update menu graphics
146 parent.updateMenu(); 152 menu->updateMenu();
147 153 return menu;
148} 154}
149 155
150 156
@@ -181,11 +187,6 @@ public:
181 187
182}; 188};
183 189
184void translateMenuItem(FbTk::Parser &parse, ParseItem &item,
185 FbTk::StringConvertor &labelconvertor,
186 AutoReloadHelper *reloader);
187
188
189void parseMenu(FbTk::Parser &pars, FbTk::Menu &menu, 190void parseMenu(FbTk::Parser &pars, FbTk::Menu &menu,
190 FbTk::StringConvertor &label_convertor, 191 FbTk::StringConvertor &label_convertor,
191 AutoReloadHelper *reloader) { 192 AutoReloadHelper *reloader) {
@@ -194,158 +195,7 @@ void parseMenu(FbTk::Parser &pars, FbTk::Menu &menu,
194 pitem.load(pars, label_convertor); 195 pitem.load(pars, label_convertor);
195 if (pitem.key() == "end") 196 if (pitem.key() == "end")
196 return; 197 return;
197 translateMenuItem(pars, pitem, label_convertor, reloader); 198// translateMenuItem(pars, pitem, label_convertor, reloader);
198 }
199}
200
201void translateMenuItem(FbTk::Parser &parse, ParseItem &pitem,
202 FbTk::StringConvertor &labelconvertor,
203 AutoReloadHelper *reloader) {
204 if (pitem.menu() == 0)
205 throw string("translateMenuItem: We must have a menu in ParseItem!");
206
207 FbTk::Menu &menu = *pitem.menu();
208 const string &str_key = pitem.key();
209 const string &str_cmd = pitem.command();
210 const string &str_label = pitem.label();
211
212 const int screen_number = menu.screenNumber();
213 _FB_USES_NLS;
214
215 if (str_key == "end") {
216 return;
217 } else if (str_key == "nop") {
218 int menuSize = menu.insert(str_label);
219 menu.setItemEnabled(menuSize-1, false);
220 } else if (str_key == "icons") {
221 FbTk::Menu *submenu = MenuCreator::createMenuType("iconmenu", menu.screenNumber());
222 if (submenu == 0)
223 return;
224 if (str_label.empty())
225 menu.insert(_FB_XTEXT(Menu, Icons, "Icons", "Iconic windows menu title"));
226 else
227 menu.insert(str_label, submenu);
228 } else if (str_key == "exit") { // exit
229 FbTk::RefCount<FbTk::Command<void> > exit_cmd(FbTk::CommandParser<void>::instance().parse("exit"));
230 if (str_label.empty())
231 menu.insert(_FB_XTEXT(Menu, Exit, "Exit", "Exit Command"), exit_cmd);
232 else
233 menu.insert(str_label, exit_cmd);
234 } else if (str_key == "exec") {
235 // execute and hide menu
236 FbTk::RefCount<FbTk::Command<void> > exec_cmd(FbTk::CommandParser<void>::instance().parse("exec " + str_cmd));
237 menu.insert(str_label, exec_cmd);
238 } else if (str_key == "macrocmd") {
239 FbTk::RefCount<FbTk::Command<void> > macro_cmd(FbTk::CommandParser<void>::instance().parse("macrocmd " + str_cmd));
240 menu.insert(str_label, macro_cmd);
241 } else if (str_key == "style") { // style
242 menu.insert(new StyleMenuItem(str_label, str_cmd));
243 } else if (str_key == "config") {
244 BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
245 if (screen != 0)
246 menu.insert(str_label, &screen->configMenu());
247 } // end of config
248 else if (str_key == "include") { // include
249
250 // this will make sure we dont get stuck in a loop
251 static size_t safe_counter = 0;
252 if (safe_counter > 10)
253 return;
254
255 safe_counter++;
256
257 string newfile = FbTk::StringUtil::expandFilename(str_label);
258 if (FbTk::FileUtil::isDirectory(newfile.c_str())) {
259 // inject every file in this directory into the current menu
260 FbTk::Directory dir(newfile.c_str());
261
262 vector<string> filelist(dir.entries());
263 for (size_t file_index = 0; file_index < dir.entries(); ++file_index)
264 filelist[file_index] = dir.readFilename();
265 sort(filelist.begin(), filelist.end(), less<string>());
266
267 for (size_t file_index = 0; file_index < dir.entries(); file_index++) {
268 string thisfile(newfile + '/' + filelist[file_index]);
269
270 if (FbTk::FileUtil::isRegularFile(thisfile.c_str()) &&
271 (filelist[file_index][0] != '.') &&
272 (thisfile[thisfile.length() - 1] != '~')) {
273 MenuCreator::createFromFile(thisfile, menu, reloader, false);
274 }
275 }
276
277 } else {
278 // inject this file into the current menu
279 MenuCreator::createFromFile(newfile, menu, reloader, false);
280 }
281
282 safe_counter--;
283
284 } // end of include
285 else if (str_key == "submenu") {
286
287 FbTk::Menu *submenu = MenuCreator::createMenu("", screen_number);
288 if (submenu == 0)
289 return;
290
291 if (!str_cmd.empty())
292 submenu->setLabel(str_cmd);
293 else
294 submenu->setLabel(str_label);
295
296 parseMenu(parse, *submenu, labelconvertor, reloader);
297 submenu->updateMenu();
298 menu.insert(str_label, submenu);
299
300 } // end of submenu
301 else if (str_key == "stylesdir" || str_key == "stylesmenu") {
302 createStyleMenu(menu, str_label, reloader,
303 str_key == "stylesmenu" ? str_cmd : str_label);
304 } // end of stylesdir
305 else if (str_key == "themesdir" || str_key == "themesmenu") {
306 createStyleMenu(menu, str_label, reloader,
307 str_key == "themesmenu" ? str_cmd : str_label);
308 } // end of themesdir
309 else if (str_key == "wallpapers" || str_key == "wallpapermenu" ||
310 str_key == "rootcommands") {
311 createRootCmdMenu(menu, str_label, str_label, reloader,
312 str_cmd == "" ? realProgramName("fbsetbg") : str_cmd);
313 } // end of wallpapers
314 else if (str_key == "workspaces") {
315 BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
316 if (screen != 0) {
317 screen->workspaceMenu().setInternalMenu();
318 menu.insert(str_label, &screen->workspaceMenu());
319 }
320 } else if (str_key == "separator") {
321 menu.insert(new FbTk::MenuSeparator());
322 } else if (str_key == "encoding") {
323 MenuCreator::startEncoding(str_cmd);
324 } else if (str_key == "endencoding") {
325 MenuCreator::endEncoding();
326 } else if (!MenuCreator::createWindowMenuItem(str_key, str_label, menu)) {
327 // if we didn't find any special menu item we try with command parser
328 // we need to attach command to arguments so command parser can parse it
329 string line = str_key + " " + str_cmd;
330 FbTk::RefCount<FbTk::Command<void> > command(FbTk::CommandParser<void>::instance().parse(line));
331 if (command != 0) {
332 // special NLS default labels
333 if (str_label.empty()) {
334 if (str_key == "reconfig" || str_key == "reconfigure") {
335 menu.insert(_FB_XTEXT(Menu, Reconfigure, "Reload Config", "Reload all the configs"), command);
336 return;
337 } else if (str_key == "restart") {
338 menu.insert(_FB_XTEXT(Menu, Restart, "Restart", "Restart Command"), command);
339 return;
340 }
341 }
342 menu.insert(str_label, command);
343 }
344 }
345 if (menu.numberOfItems() != 0) {
346 FbTk::MenuItem *item = menu.find(menu.numberOfItems() - 1);
347 if (item != 0 && !pitem.icon().empty())
348 item->setIcon(pitem.icon(), menu.screenNumber());
349 } 199 }
350} 200}
351 201
@@ -411,7 +261,8 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv,
411 const string &str_label = getField(l, -1, "label", conv); 261 const string &str_label = getField(l, -1, "label", conv);
412 const string &str_key = getField(l, -1, "type"); 262 const string &str_key = getField(l, -1, "type");
413 const FbTk::CommandParser<void> &parser = FbTk::CommandParser<void>::instance(); 263 const FbTk::CommandParser<void> &parser = FbTk::CommandParser<void>::instance();
414 BScreen *screen = Fluxbox::instance()->findScreen(menu.screenNumber()); 264 int screen_number = menu.screenNumber();
265 BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
415 size_t old_size = menu.numberOfItems(); 266 size_t old_size = menu.numberOfItems();
416 267
417 // first items that don't need additional parameters 268 // first items that don't need additional parameters
@@ -421,7 +272,7 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv,
421 int size = menu.insert(str_label); 272 int size = menu.insert(str_label);
422 menu.setItemEnabled(size-1, false); 273 menu.setItemEnabled(size-1, false);
423 } else if(str_key == "icons") { 274 } else if(str_key == "icons") {
424 FbTk::Menu *submenu = MenuCreator::createMenuType("iconmenu", menu.screenNumber()); 275 FbTk::Menu *submenu = MenuCreator::createMenuType("iconmenu", screen_number);
425 if (submenu == 0) 276 if (submenu == 0)
426 return; 277 return;
427 if (str_label.empty()) 278 if (str_label.empty())
@@ -438,7 +289,7 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv,
438 menu.insert(str_label, &screen->configMenu()); 289 menu.insert(str_label, &screen->configMenu());
439 } else if(str_key == "menu") { 290 } else if(str_key == "menu") {
440 l.pushvalue(-1); 291 l.pushvalue(-1);
441 menu.insert(str_label, createMenu_(l, menu.screenNumber(), *conv, reloader).release()); 292 menu.insert(str_label, createMenu_(l, screen_number, *conv, reloader).release());
442 } else { 293 } else {
443 // items that have a parameter 294 // items that have a parameter
444 const string &str_cmd = getField(l, -1, "param"); 295 const string &str_cmd = getField(l, -1, "param");
@@ -452,14 +303,20 @@ insertMenuItem(lua::state &l, FbMenu &menu, FbTk::StringConvertor &parent_conv,
452 } else if(str_key == "style") 303 } else if(str_key == "style")
453 menu.insert(new StyleMenuItem(str_label, str_cmd)); 304 menu.insert(new StyleMenuItem(str_label, str_cmd));
454 else if (str_key == "stylesdir") 305 else if (str_key == "stylesdir")
455 createStyleMenu(menu, str_label, reloader, str_cmd); 306 menu.insert(str_label,
307 createStyleMenu(screen_number, str_label, reloader, str_cmd).release());
456 else if (str_key == "wallpapers") { 308 else if (str_key == "wallpapers") {
457 const string &program = getField(l, -1, "program"); 309 string program = getField(l, -1, "program");
458 createRootCmdMenu(menu, str_label, str_cmd, reloader, 310 if(program.empty())
459 program.empty() ? realProgramName("fbsetbg") : program); 311 program = realProgramName("fbsetbg");
312 menu.insert(str_label, createRootCmdMenu(screen_number, str_label, str_cmd,
313 reloader, program).release() );
460 } else if (str_key == "workspaces") { 314 } else if (str_key == "workspaces") {
461/* screen->workspaceMenu().setInternalMenu(); 315 screen->workspaceMenu().setInternalMenu();
462 menu.insert(str_label, &screen->workspaceMenu());*/ 316 menu.insert(str_label, &screen->workspaceMenu());
317 } else {
318 // finally, try window-related commands
319 MenuCreator::createWindowMenuItem(str_key, str_label, menu);
463 } 320 }
464 } 321 }
465 322