aboutsummaryrefslogtreecommitdiff
path: root/src/tests/menutest.cc
blob: 6a8bd437bf2cf7301f9ac03d5fed97ebc45da0b5 (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
// menutest.cc a test app for Menus
// Copyright (c) 2006 Henrik Kinnunen (fluxgen at fluxbox dot org)
//
// 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.

#include "App.hh"
#include "FbWindow.hh"
#include "Font.hh"
#include "EventHandler.hh"
#include "EventManager.hh"
#include "GContext.hh"
#include "Color.hh"
#include "Menu.hh"
#include "MenuItem.hh"
#include "MenuSeparator.hh"
#include "StringUtil.hh"
#include "ImageControl.hh"

#include "../FbMenuParser.hh"

#include <X11/Xutil.h>
#include <X11/keysym.h>

#include <string>
#include <iostream>
using namespace std;

void doSubmenu(Parser &parser, FbTk::Menu &menu, 
               FbTk::MenuTheme &theme,
               FbTk::ImageControl &image_ctrl,
               const std::string &labelstr) {

    Parser::Item key, label, cmd, icon;

    FbTk::Menu *submenu = new FbTk::Menu(theme, image_ctrl);
    submenu->setLabel(labelstr.c_str());
    menu.insert(labelstr.c_str(), submenu);
    // skip submenu items
    if (key.second == "begin") {
        while (key.second != "end") {
            parser>>key>>label>>cmd>>icon;
            if (key.second == "begin")
                doSubmenu(parser, *submenu,
                          theme, image_ctrl,
                          label.second);
        }
    }
}

class App:public FbTk::App, public FbTk::EventHandler {
public:
    App(const char *displayname, const std::string &stylefile, const std::string &menufile):
        FbTk::App(displayname),
        m_image_ctrl(DefaultScreen(display())),
        m_menu_theme(DefaultScreen(display())),
        m_menu(m_menu_theme, m_image_ctrl) { 

        //m_menu_theme.frameFont().setAntialias(true);
        //m_menu_theme.titleFont().setAntialias(true);

        cerr<<"Loading menu: "<<menufile<<endl;
        FbMenuParser parser(menufile);
        if (parser.isLoaded()) { 
            // get start of file
            Parser::Item key, label, cmd, icon;
            while (!parser.eof()) {
                // get first begin line
                parser>>key>>label>>cmd>>icon;
                if (key.second == "begin")
                    break;
                
            }

            m_menu.setLabel(label.second.c_str());
            
            while (!parser.eof()) {
                parser>>key>>label>>cmd>>icon;
                if (key.second == "end")
                    break;

                string iconfile = icon.second;
                if (key.second == "separator")
                    m_menu.insert(new FbTk::MenuSeparator());
                else if (key.second == "begin") { // new submenu
                    doSubmenu(parser, m_menu,
                              m_menu_theme,
                              m_image_ctrl,
                              label.second);
                } else if (key.second != "styles" && 
                           key.second != "stylesdir")
                    m_menu.insert(label.second.c_str());
                    

                // set icon on items
                if (!iconfile.empty()) {
                    FbTk::MenuItem *item = m_menu.find(m_menu.numberOfItems() - 1);
                    item->setIcon(iconfile, m_menu.screenNumber());
                }
            }

        }

        cerr<<"Loading style: "<<stylefile<<endl;        
        FbTk::ThemeManager::instance().load(stylefile);

        m_menu.show();

    }

    ~App() {
    }


private:
    FbTk::ImageControl m_image_ctrl;
    FbTk::MenuTheme m_menu_theme;
    FbTk::Menu m_menu;
};

int main(int argc, char **argv) {
    string displayname("");
    string stylefile, menufile="~/.fluxbox/menu";
    for (int a=1; a<argc; ++a) {
        if (strcmp("-display", argv[a]) == 0 && a + 1 < argc) {
            displayname = argv[++a];
        } else if (strcmp("-style", argv[a]) == 0 && a + 1 < argc) {
            stylefile = argv[++a];
        } else if (strcmp("-menu", argv[a]) == 0 && a + 1 < argc) {
            menufile = argv[++a];
        }
    }
    menufile = FbTk::StringUtil::expandFilename(menufile);
    App app(displayname.c_str(), stylefile, menufile);

    app.eventLoop();
	
}