summaryrefslogtreecommitdiff
path: root/src/MenuCreator.cc
blob: a2bdb5ce49797542219dcaa334cd0e06997360a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// MenuCreator.cc for Fluxbox
// Copyright (c) 2004 Henrik Kinnunen (fluxgen at users.sourceforge.net)
//                and Simon Bowden    (rathnor at users.sourceforge.net)
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

// $Id: MenuCreator.cc,v 1.8 2004/06/08 13:15:30 rathnor Exp $

#include "MenuCreator.hh"

#include "Screen.hh"
#include "CommandParser.hh"
#include "fluxbox.hh"
#include "CommandParser.hh"
#include "Window.hh"

#include "FbMenu.hh"
#include "IconMenu.hh"
#include "WorkspaceMenu.hh"
#include "LayerMenu.hh"
#include "SendToMenu.hh"

#include "FbMenuParser.hh"
#include "StyleMenuItem.hh"

#include "FbTk/I18n.hh"
#include "FbTk/MultiButtonMenuItem.hh"
#include "FbTk/RefCount.hh"
#include "FbTk/MacroCommand.hh"
#include "FbTk/SimpleCommand.hh"
#include "FbTk/StringUtil.hh"
#include "FbTk/Directory.hh"
#include "FbTk/MenuSeparator.hh"
#include "FbTk/MenuIcon.hh"

#include <iostream>
using namespace std;

template <>
void LayerMenuItem<FluxboxWindow>::click(int button, int time) {
    m_object->moveToLayer(m_layernum);
}

static void createStyleMenu(FbTk::Menu &parent, const std::string &label, 
                                   const std::string &directory) {
    // perform shell style ~ home directory expansion
    string stylesdir(FbTk::StringUtil::expandFilename(directory));

    if (!FbTk::Directory::isDirectory(stylesdir))
        return;

    FbTk::Directory dir(stylesdir.c_str());

    // create a vector of all the filenames in the directory
    // add sort it
    std::vector<std::string> filelist(dir.entries());
    for (size_t file_index = 0; file_index < dir.entries(); ++file_index)
        filelist[file_index] = dir.readFilename();

    std::sort(filelist.begin(), filelist.end(), less<string>());

    // for each file in directory add filename and path to menu
    for (size_t file_index = 0; file_index < dir.entries(); file_index++) {
        std::string style(stylesdir + '/' + filelist[file_index]);
        // add to menu only if the file is a regular file, and not a
        // .file or a backup~ file
        if ((FbTk::Directory::isRegularFile(style) &&
             (filelist[file_index][0] != '.') &&
             (style[style.length() - 1] != '~')) ||
            FbTk::Directory::isRegularFile(style + "/theme.cfg"))
            parent.insert(new StyleMenuItem(filelist[file_index], style));
    } 
    // update menu graphics
    parent.update();
    Fluxbox::instance()->saveMenuFilename(stylesdir.c_str());

}

static void translateMenuItem(Parser &parse,
                              const std::string &str_key, 
                              const std::string &str_label, 
                              const std::string &str_cmd,
                              FbTk::Menu &menu);


static void parseMenu(Parser &pars, FbTk::Menu &menu) {
    Parser::Item item, item2, item3;
    while (!pars.eof()) {
        pars>>item>>item2>>item3;
        if (item.second == "end")
            return;
        translateMenuItem(pars,
                          item.second,
                          item2.second,
                          item3.second,
                          menu);

    }
}

static void translateMenuItem(Parser &parse,
                              const std::string &str_key, 
                              const std::string &str_label, 
                              const std::string &str_cmd,
                              FbTk::Menu &menu) {
    
    const int screen_number = menu.screenNumber();
    _FB_USES_NLS;

    if (str_key == "end") {
        return;
    } else if (str_key == "nop") { 
        menu.insert(str_label.c_str());
    } else if (str_key == "icons") {
        FbTk::Menu *submenu = MenuCreator::createMenuType("iconmenu", menu.screenNumber());
        if (submenu == 0)
            return;
        if (str_label.empty())
            menu.insert(_FBTEXT(Menu, Icons, "Icons", "Iconic windows menu title"));
        else
            menu.insert(str_label.c_str(), submenu);
    } else if (str_key == "exit") { // exit
        FbTk::RefCount<FbTk::Command> exit_cmd(CommandParser::instance().parseLine("exit"));
        if (str_label.empty())
            menu.insert(_FBTEXT(Menu, Exit, "Exit", "Exit Command"), exit_cmd);
        else
            menu.insert(str_label.c_str(), exit_cmd);
    } else if (str_key == "exec") {
        // execute and hide menu
        using namespace FbTk;
        RefCount<Command> exec_cmd(CommandParser::instance().parseLine("exec " + str_cmd));
        RefCount<Command> hide_menu(new SimpleCommand<FbTk::Menu>(menu, 
                                                                  &Menu::hide));
        MacroCommand *exec_and_hide = new FbTk::MacroCommand();
        exec_and_hide->add(hide_menu);
        exec_and_hide->add(exec_cmd);
        RefCount<Command> exec_and_hide_cmd(exec_and_hide);
        menu.insert(str_label.c_str(), exec_and_hide_cmd);
    }
    else if (str_key == "style") {	// style
        menu.insert(new StyleMenuItem(str_label, str_cmd));
    } else if (str_key == "config") {
        BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
        if (screen != 0)
            menu.insert(str_label.c_str(), &screen->configMenu());
    } // end of config                
    else if (str_key == "include") { // include
        string newfile = FbTk::StringUtil::expandFilename(str_label);
        // inject this file into the current menu
        MenuCreator::createFromFile(newfile, menu);
        Fluxbox::instance()->saveMenuFilename(newfile.c_str());
    } // end of include
    else if (str_key == "submenu") {
            
        FbTk::Menu *submenu = MenuCreator::createMenu("", screen_number);
        if (submenu == 0)
            return;

        if (str_cmd.size())
            submenu->setLabel(str_cmd.c_str());
        else
            submenu->setLabel(str_label.c_str());

        parseMenu(parse, *submenu);
        submenu->update();
        menu.insert(str_label.c_str(), submenu);
        // save to screen list so we can delete it later
        BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
        if (screen != 0)                    
            screen->saveMenu(*submenu);

    } // end of submenu
    else if (str_key == "restart") {
        FbTk::RefCount<FbTk::Command> restart_fb(CommandParser::instance().
                                                 parseLine("restart"));
        if (str_label.empty())
            menu.insert(_FBTEXT(Menu, Restart, "Restart", "Restart Command"), restart_fb);
        else
            menu.insert(str_label.c_str(), restart_fb);
    } // end of restart
    else if (str_key == "reconfig") { // reconf
        FbTk::RefCount<FbTk::Command> 
            reconfig_fb_cmd(CommandParser::instance().
                            parseLine("reconfigure"));
        if (str_label.empty())
            menu.insert(_FBTEXT(Menu, Reconfigure, "Reload Config", "Reload all the configs"), reconfig_fb_cmd);
        else 
            menu.insert(str_label.c_str(), reconfig_fb_cmd);

    } else if (str_key == "stylesdir" || str_key == "stylesmenu") {
        createStyleMenu(menu, str_label, 
                        str_key == "stylesmenu" ? str_cmd : str_label);
    } // end of stylesdir
    else if (str_key == "workspaces") {
        BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
        if (screen != 0) {
            screen->getWorkspacemenu().setInternalMenu();
            menu.insert(str_label.c_str(), &screen->getWorkspacemenu());
        }
    } else if (str_key == "separator") {
        menu.insert(new FbTk::MenuSeparator());
    }/* else if (str_key == "icon") {
        FbTk::RefCount<FbTk::Command> cmd(CommandParser::instance().parseLine(str_cmd));
        FbTk::MenuItem *item = new FbTk::MenuIcon(str_label, str_cmd, screen_number);
        item->setCommand(cmd);
        menu.insert(item);
        
        }*/
    else { // ok, if we didn't find any special menu item we try with command parser
        // we need to attach command with arguments so command parser can parse it
        string line = str_key + " " + str_cmd;
        FbTk::RefCount<FbTk::Command> command(CommandParser::instance().parseLine(line));
        if (*command != 0)
            menu.insert(str_label.c_str(), command);
    }

}


static void parseWindowMenu(Parser &parse, FbTk::Menu &menu, FluxboxWindow &win) {

    Parser::Item item, item2, item3;
    while (!parse.eof()) {
        parse>>item>>item2>>item3;
        if (MenuCreator::createWindowMenuItem(item.second, item2.second, menu, win))
            continue;

        if (item.second == "end") {
            return;
        } else if (item.second == "submenu") {
            FbTk::Menu *submenu = MenuCreator::createMenu(item2.second, menu.screenNumber());
            parseWindowMenu(parse, *submenu, win);
            submenu->update();
            menu.insert(item2.second.c_str(), submenu);

        } else { // try non window menu specific stuff
            translateMenuItem(parse, item.second, item2.second, item3.second, menu);
        }
    }
}

FbTk::Menu *MenuCreator::createMenu(const std::string &label, int screen_number) {
    BScreen *screen = Fluxbox::instance()->findScreen(screen_number);
    if (screen == 0)
        return 0;

    FbTk::Menu *menu = new FbMenu(screen->menuTheme(), 
                                  screen->imageControl(), 
                                  *screen->layerManager().
                                  getLayer(Fluxbox::instance()->getMenuLayer()));
    if (!label.empty())
        menu->setLabel(label.c_str());

    return menu;
}

bool getStart(FbMenuParser &parser, std::string &label) {
    Parser::Item item, item2, item3;
    while (!parser.eof()) {
        // get first begin line
        parser>>item>>item2>>item3;
        if (item.second == "begin") {
            break;
        }
    }
    if (parser.eof())
        return false;
    label = item2.second;
    return true;
}

FbTk::Menu *MenuCreator::createFromFile(const std::string &filename, int screen_number) {
    std::string real_filename = FbTk::StringUtil::expandFilename(filename);
    FbMenuParser parser(real_filename);
    if (!parser.isLoaded())
        return 0;

    std::string label;
    if (!getStart(parser, label))
        return 0;
    
    FbTk::Menu *menu = createMenu(label, screen_number);
    if (menu != 0)
        parseMenu(parser, *menu);

    return menu;
}


void MenuCreator::createFromFile(const std::string &filename, 
                                 FbTk::Menu &inject_into) {

    std::string real_filename = FbTk::StringUtil::expandFilename(filename);
    FbMenuParser parser(real_filename);
    if (!parser.isLoaded())
        return;

    std::string label;
    if (!getStart(parser, label))
        return;

    parseMenu(parser, inject_into);
}


void MenuCreator::createFromFile(const std::string &filename, 
                                 FbTk::Menu &inject_into, 
                                 FluxboxWindow &win) {
    std::string real_filename = FbTk::StringUtil::expandFilename(filename);
    FbMenuParser parser(real_filename);
    if (!parser.isLoaded())
        return;

    std::string label;

    if (!getStart(parser, label))
        return;

    parseWindowMenu(parser, inject_into, win);
}


FbTk::Menu *MenuCreator::createMenuType(const std::string &type, int screen_num) {
    BScreen *screen = Fluxbox::instance()->findScreen(screen_num);
    if (screen == 0)
        return 0;
    if (type == "iconmenu") {
        return new IconMenu(*screen);
    } else if (type == "workspacemenu") {
        return new WorkspaceMenu(*screen);
    }
    return 0;
}

bool MenuCreator::createWindowMenuItem(const std::string &type, 
                                       const std::string &label,
                                       FbTk::Menu &menu,
                                       FluxboxWindow &win) {
    typedef FbTk::RefCount<FbTk::Command> RefCmd;
    typedef FbTk::SimpleCommand<FluxboxWindow> WindowCmd;
    _FB_USES_NLS;

    if (type == "shade") {
        RefCmd shade_cmd(new WindowCmd(win, &FluxboxWindow::shade));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Shade, "Shade", "Shade the window"):label.c_str(), shade_cmd);
    } else if (type == "maximize") {
        RefCmd maximize_cmd(new WindowCmd(win, &FluxboxWindow::maximizeFull));
        RefCmd maximize_vert_cmd(new WindowCmd(win, &FluxboxWindow::maximizeVertical));
        RefCmd maximize_horiz_cmd(new WindowCmd(win, &FluxboxWindow::maximizeHorizontal));
        FbTk::MultiButtonMenuItem *maximize_item = new FbTk::MultiButtonMenuItem(3, label.empty()?_FBTEXT(Windowmenu, Maximize, "Maximize", "Maximize the window"):label.c_str());
        // create maximize item with:
        // button1: Maximize normal
        // button2: Maximize Vertical
        // button3: Maximize Horizontal
        maximize_item->setCommand(1, maximize_cmd);
        maximize_item->setCommand(2, maximize_vert_cmd);
        maximize_item->setCommand(3, maximize_horiz_cmd);
        menu.insert(maximize_item);
    } else if (type == "iconify") {
        RefCmd iconify_cmd(new WindowCmd(win, &FluxboxWindow::iconify));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Iconify, "Iconify", "Iconify the window"):label.c_str(), iconify_cmd);
    } else if (type == "close") {
        RefCmd close_cmd(new WindowCmd(win, &FluxboxWindow::close));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Close, "Close", "Close the window"):label.c_str(), close_cmd);
    } else if (type == "lower") {
        RefCmd lower_cmd(new WindowCmd(win, &FluxboxWindow::lower));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Lower, "Lower", "Lower the window"):label.c_str(), lower_cmd);
    } else if (type == "raise") {
        RefCmd raise_cmd(new WindowCmd(win, &FluxboxWindow::raise));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Raise, "Raise", "Raise the window"):label.c_str(), raise_cmd);
    } else if (type == "stick") {
        RefCmd stick_cmd(new WindowCmd(win, &FluxboxWindow::stick));
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Stick, "Stick", "Stick the window"):label.c_str(), stick_cmd);
    } else if (type == "extramenus") {
        FluxboxWindow::ExtraMenus::iterator it = win.extraMenus().begin();
        FluxboxWindow::ExtraMenus::iterator it_end = win.extraMenus().end();
        for (; it != it_end; ++it) {
            it->second->disableTitle();
            menu.insert(it->first, it->second);
        }

    } else if (type == "sendto") {
        menu.insert(label.empty()?_FBTEXT(Windowmenu, SendTo, "Send To...", "Send to menu item name"):label.c_str(), new SendToMenu(win));
    } else if (type == "layer") {
        BScreen *screen = Fluxbox::instance()->findScreen(menu.screenNumber());
        if (screen == 0)
            return false;
        FbTk::Menu *submenu = new LayerMenu<FluxboxWindow>(screen->menuTheme(), 
                                                           screen->imageControl(), 
                                                           *screen->layerManager().
                                                           getLayer(Fluxbox::instance()->getMenuLayer()), 
                                                           &win,
                                                           false);
        submenu->disableTitle();
        menu.insert(label.empty()?_FBTEXT(Windowmenu, Layer, "Layer ...", "Layer menu"):label.c_str(), submenu);


    } else if (type == "separator") {
        menu.insert(new FbTk::MenuSeparator());
    } else
        return false;

    return true;
}