diff options
Diffstat (limited to 'src/ToolbarHandler.cc')
-rw-r--r-- | src/ToolbarHandler.cc | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/src/ToolbarHandler.cc b/src/ToolbarHandler.cc new file mode 100644 index 0000000..f94c6a7 --- /dev/null +++ b/src/ToolbarHandler.cc | |||
@@ -0,0 +1,322 @@ | |||
1 | // ToolbarHandler for fluxbox | ||
2 | // Copyright (c) 2003 Simon Bowden (rathnor at fluxbox.org) | ||
3 | // and Henrik Kinnunen (fluxgen at fluxbox.org) | ||
4 | // | ||
5 | // Permission is hereby granted, free of charge, to any person obtaining a | ||
6 | // copy of this software and associated documentation files (the "Software"), | ||
7 | // to deal in the Software without restriction, including without limitation | ||
8 | // the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
9 | // and/or sell copies of the Software, and to permit persons to whom the | ||
10 | // Software is furnished to do so, subject to the following conditions: | ||
11 | // | ||
12 | // The above copyright notice and this permission notice shall be included in | ||
13 | // all copies or substantial portions of the Software. | ||
14 | // | ||
15 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
16 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
17 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
18 | // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
19 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
20 | // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
21 | // DEALINGS IN THE SOFTWARE. | ||
22 | |||
23 | // $Id: ToolbarHandler.cc,v 1.7 2003/04/16 13:30:55 fluxgen Exp $ | ||
24 | |||
25 | /** | ||
26 | * The ToolbarHandler class acts as a rough interface to the toolbar. | ||
27 | * It deals with whether it should be there or not, so anything that | ||
28 | * always needs to be accessible must come through the handler. | ||
29 | */ | ||
30 | |||
31 | #include "ToolbarHandler.hh" | ||
32 | #include "Window.hh" | ||
33 | #include "Screen.hh" | ||
34 | #include "Workspace.hh" | ||
35 | #include "MenuItem.hh" | ||
36 | #include "Menu.hh" | ||
37 | #include "FbCommands.hh" | ||
38 | #include "RefCount.hh" | ||
39 | #include "SimpleCommand.hh" | ||
40 | #include "MacroCommand.hh" | ||
41 | #include "IntResMenuItem.hh" | ||
42 | #include "BoolMenuItem.hh" | ||
43 | |||
44 | namespace { | ||
45 | |||
46 | class ToolbarModeMenuItem : public FbTk::MenuItem { | ||
47 | public: | ||
48 | ToolbarModeMenuItem(const char *label, ToolbarHandler &handler, | ||
49 | ToolbarHandler::ToolbarMode mode, | ||
50 | FbTk::RefCount<FbTk::Command> &cmd): | ||
51 | FbTk::MenuItem(label, cmd), m_handler(handler), m_mode(mode) { | ||
52 | } | ||
53 | bool isEnabled() const { return m_handler.getMode() != m_mode; } | ||
54 | void click(int button, int time) { | ||
55 | m_handler.setMode(m_mode); | ||
56 | FbTk::MenuItem::click(button, time); | ||
57 | } | ||
58 | |||
59 | private: | ||
60 | ToolbarHandler &m_handler; | ||
61 | ToolbarHandler::ToolbarMode m_mode; | ||
62 | }; | ||
63 | |||
64 | void setupModeMenu(FbTk::Menu &menu, ToolbarHandler &handler) { | ||
65 | //I18n *i18n = I18n::instance(); | ||
66 | //using namespace FBNLS; | ||
67 | using namespace FbTk; | ||
68 | |||
69 | RefCount<Command> saverc_cmd(new SimpleCommand<Fluxbox>( | ||
70 | *Fluxbox::instance(), | ||
71 | &Fluxbox::save_rc)); | ||
72 | |||
73 | //TODO: nls | ||
74 | menu.insert(new ToolbarModeMenuItem("Off", handler, | ||
75 | ToolbarHandler::OFF, saverc_cmd)); | ||
76 | menu.insert(new ToolbarModeMenuItem("None", handler, | ||
77 | ToolbarHandler::NONE, saverc_cmd)); | ||
78 | menu.insert(new ToolbarModeMenuItem("Icons", handler, | ||
79 | ToolbarHandler::ICONS, saverc_cmd)); | ||
80 | menu.insert(new ToolbarModeMenuItem("Workspace Icons", handler, | ||
81 | ToolbarHandler::WORKSPACEICONS, saverc_cmd)); | ||
82 | menu.insert(new ToolbarModeMenuItem("Workspace", handler, | ||
83 | ToolbarHandler::WORKSPACE, saverc_cmd)); | ||
84 | menu.insert(new ToolbarModeMenuItem("All Windows", handler, | ||
85 | ToolbarHandler::ALLWINDOWS, saverc_cmd)); | ||
86 | menu.update(); | ||
87 | } | ||
88 | |||
89 | }; // end anonymous namespace | ||
90 | |||
91 | ToolbarHandler::ToolbarHandler(BScreen &screen, ToolbarMode mode) | ||
92 | : m_screen(screen), m_mode(mode), m_toolbar(0), m_current_workspace(0), | ||
93 | m_modemenu(*screen.menuTheme(), | ||
94 | screen.getScreenNumber(), *screen.getImageControl()), | ||
95 | m_toolbarmenu(*screen.menuTheme(), | ||
96 | screen.getScreenNumber(), *screen.getImageControl()) | ||
97 | { | ||
98 | m_modemenu.setInternalMenu(); | ||
99 | setupModeMenu(m_modemenu, *this); | ||
100 | setMode(mode, false); // the atomhandler part will initialise it shortly | ||
101 | } | ||
102 | |||
103 | void ToolbarHandler::setMode(ToolbarMode mode, bool initialise) { | ||
104 | if (mode < 0 || mode >= LASTMODE || (mode == m_mode && initialise)) | ||
105 | return; | ||
106 | |||
107 | if (mode == OFF) { | ||
108 | m_mode = mode; | ||
109 | m_toolbarmenu.removeAll(); | ||
110 | //TODO: nls | ||
111 | m_toolbarmenu.insert("Mode...", &m_modemenu); | ||
112 | m_toolbar.reset(0); | ||
113 | m_toolbarmenu.update(); | ||
114 | |||
115 | return; | ||
116 | } else if (!m_toolbar.get()) { | ||
117 | m_toolbarmenu.removeAll(); | ||
118 | |||
119 | m_toolbar.reset(new Toolbar(m_screen, | ||
120 | *m_screen.layerManager().getLayer(m_screen.getToolbarLayerNum()), m_toolbarmenu)); | ||
121 | m_toolbarmenu.insert("Mode...", &m_modemenu); | ||
122 | m_toolbarmenu.update(); | ||
123 | } | ||
124 | |||
125 | |||
126 | if (mode == NONE) { | ||
127 | // disableIconBar will clean up | ||
128 | m_toolbar->disableIconBar(); | ||
129 | } else { | ||
130 | // rebuild it | ||
131 | // be sure the iconbar is on | ||
132 | m_toolbar->enableIconBar(); | ||
133 | m_toolbar->delAllIcons(); | ||
134 | } | ||
135 | // reset Toolbar, and reload it (initForScreen) | ||
136 | m_mode = mode; | ||
137 | if (initialise) | ||
138 | initForScreen(m_screen); | ||
139 | } | ||
140 | |||
141 | void ToolbarHandler::initForScreen(BScreen &screen) { | ||
142 | if (&m_screen != &screen) | ||
143 | return; | ||
144 | switch (m_mode) { | ||
145 | case OFF: | ||
146 | break; | ||
147 | case NONE: | ||
148 | break; | ||
149 | case ALLWINDOWS: | ||
150 | { | ||
151 | BScreen::Workspaces::const_iterator workspace_it = m_screen.getWorkspacesList().begin(); | ||
152 | BScreen::Workspaces::const_iterator workspace_it_end = m_screen.getWorkspacesList().end(); | ||
153 | for (; workspace_it != workspace_it_end; ++workspace_it) { | ||
154 | Workspace::Windows &wins = (*workspace_it)->getWindowList(); | ||
155 | Workspace::Windows::iterator wit = wins.begin(); | ||
156 | Workspace::Windows::iterator wit_end = wins.end(); | ||
157 | for (; wit != wit_end; ++wit) { | ||
158 | m_toolbar->addIcon(*wit); | ||
159 | /* | ||
160 | FluxboxWindow::ClientList::iterator cit = (*wit)->clientList().begin(); | ||
161 | FluxboxWindow::ClientList::iterator cit_end = (*wit)->clientList().end(); | ||
162 | for (; cit != cit_end; ++cit) | ||
163 | m_toolbar->addIcon(*(*cit)); | ||
164 | */ | ||
165 | } | ||
166 | } | ||
167 | } | ||
168 | // fall through and add icons | ||
169 | case LASTMODE: | ||
170 | case ICONS: | ||
171 | { | ||
172 | BScreen::Icons &iconlist = m_screen.getIconList(); | ||
173 | BScreen::Icons::iterator iconit = iconlist.begin(); | ||
174 | BScreen::Icons::iterator iconit_end = iconlist.end(); | ||
175 | for(; iconit != iconit_end; ++iconit) { | ||
176 | m_toolbar->addIcon(*iconit); | ||
177 | } | ||
178 | } | ||
179 | break; | ||
180 | case WORKSPACE: | ||
181 | { | ||
182 | Workspace::Windows &wins = m_screen.getCurrentWorkspace()->getWindowList(); | ||
183 | Workspace::Windows::iterator wit = wins.begin(); | ||
184 | Workspace::Windows::iterator wit_end = wins.end(); | ||
185 | for (; wit != wit_end; ++wit) { | ||
186 | m_toolbar->addIcon(*wit); | ||
187 | } | ||
188 | } | ||
189 | // fall through and add icons for this workspace | ||
190 | case WORKSPACEICONS: | ||
191 | { | ||
192 | m_current_workspace = m_screen.getCurrentWorkspaceID(); | ||
193 | |||
194 | BScreen::Icons &wiconlist = m_screen.getIconList(); | ||
195 | BScreen::Icons::iterator iconit = wiconlist.begin(); | ||
196 | BScreen::Icons::iterator iconit_end = wiconlist.end(); | ||
197 | for(; iconit != iconit_end; ++iconit) { | ||
198 | if ((*iconit)->getWorkspaceNumber() == m_current_workspace) | ||
199 | m_toolbar->addIcon(*iconit); | ||
200 | } | ||
201 | } | ||
202 | break; | ||
203 | } | ||
204 | } | ||
205 | |||
206 | void ToolbarHandler::setupWindow(FluxboxWindow &win) { | ||
207 | if (&win.getScreen() != &m_screen) | ||
208 | return; | ||
209 | |||
210 | switch (m_mode) { | ||
211 | case OFF: | ||
212 | case NONE: | ||
213 | break; | ||
214 | case WORKSPACE: | ||
215 | if (win.getWorkspaceNumber() == m_current_workspace) | ||
216 | m_toolbar->addIcon(&win); | ||
217 | break; | ||
218 | case WORKSPACEICONS: | ||
219 | if (win.getWorkspaceNumber() != m_current_workspace) | ||
220 | break; | ||
221 | // else fall through and add the icon | ||
222 | case LASTMODE: | ||
223 | case ICONS: | ||
224 | if (win.isIconic()) { | ||
225 | m_toolbar->addIcon(&win); | ||
226 | } | ||
227 | break; | ||
228 | case ALLWINDOWS: | ||
229 | m_toolbar->addIcon(&win); | ||
230 | break; | ||
231 | } | ||
232 | } | ||
233 | |||
234 | void ToolbarHandler::updateWindowClose(FluxboxWindow &win) { | ||
235 | if (&win.getScreen() != &m_screen) | ||
236 | return; | ||
237 | |||
238 | // check status of window (in current workspace, etc) and remove if necessary | ||
239 | switch (m_mode) { | ||
240 | case OFF: | ||
241 | case NONE: | ||
242 | break; | ||
243 | case WORKSPACEICONS: | ||
244 | if (win.getWorkspaceNumber() != m_current_workspace) | ||
245 | break; | ||
246 | // else fall through and remove the icon | ||
247 | case LASTMODE: | ||
248 | case ICONS: | ||
249 | if (win.isIconic()) { | ||
250 | m_toolbar->delIcon(&win); | ||
251 | } | ||
252 | break; | ||
253 | case WORKSPACE: | ||
254 | if (win.getWorkspaceNumber() == m_current_workspace) | ||
255 | m_toolbar->delIcon(&win); | ||
256 | break; | ||
257 | case ALLWINDOWS: | ||
258 | m_toolbar->delIcon(&win); | ||
259 | break; | ||
260 | } | ||
261 | } | ||
262 | |||
263 | void ToolbarHandler::updateState(FluxboxWindow &win) { | ||
264 | if (&win.getScreen() != &m_screen) | ||
265 | return; | ||
266 | |||
267 | // this function only relevant for icons | ||
268 | switch (m_mode) { | ||
269 | case OFF: | ||
270 | case NONE: | ||
271 | case WORKSPACE: | ||
272 | case ALLWINDOWS: | ||
273 | break; | ||
274 | case WORKSPACEICONS: | ||
275 | if (win.getWorkspaceNumber() != m_current_workspace) break; | ||
276 | // else fall through and do the same as icons (knowing it is the right ws) | ||
277 | case LASTMODE: | ||
278 | case ICONS: | ||
279 | // if the window is iconic (it mustn't have been before), then add it | ||
280 | // else remove it | ||
281 | if (win.isIconic()) { | ||
282 | if (!m_toolbar->containsIcon(win)) { | ||
283 | m_toolbar->addIcon(&win); | ||
284 | } | ||
285 | } else { | ||
286 | m_toolbar->delIcon(&win); | ||
287 | } | ||
288 | break; | ||
289 | } | ||
290 | } | ||
291 | |||
292 | |||
293 | void ToolbarHandler::updateWorkspace(FluxboxWindow &win) { | ||
294 | if (&win.getScreen() != &m_screen) | ||
295 | return; | ||
296 | |||
297 | // don't care about current workspace except if in workspace mode | ||
298 | if (!(m_mode == WORKSPACE || (m_mode == WORKSPACEICONS && win.isIconic()))) return; | ||
299 | |||
300 | if (win.getWorkspaceNumber() == m_current_workspace) { | ||
301 | //!! TODO | ||
302 | // this shouldn't be needed, but is until Workspaces get fixed so that | ||
303 | // you only move between them, you don't 'add' and 'remove' | ||
304 | // alternatively, fix reassocaiteWindow so that the iconic stuff is | ||
305 | // done elsewhere | ||
306 | if (!m_toolbar->containsIcon(win)) | ||
307 | m_toolbar->addIcon(&win); | ||
308 | } else { | ||
309 | // relies on the fact that this runs but does nothing if window isn't contained. | ||
310 | m_toolbar->delIcon(&win); | ||
311 | } | ||
312 | } | ||
313 | |||
314 | void ToolbarHandler::updateCurrentWorkspace(BScreen &screen) { | ||
315 | if (&screen != &m_screen) return; | ||
316 | // if only displaying current workspace, update list | ||
317 | // otherwise ignore it | ||
318 | if (m_mode != WORKSPACE && m_mode != WORKSPACEICONS) return; | ||
319 | m_toolbar->delAllIcons(); | ||
320 | initForScreen(m_screen); | ||
321 | } | ||
322 | |||