Using some references instead of pointers for the menus.
[mikachu/openbox.git] / src / Configmenu.cc
1 // Configmenu.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
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 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #  define _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include "i18n.h"
34 #include "Configmenu.h"
35 #include "Toolbar.h"
36 #include "Window.h"
37 #include "Screen.h"
38
39 Configmenu::Configmenu(BScreen &scr) : Basemenu(scr), screen(scr)
40 {
41   setLabel(i18n->getMessage(ConfigmenuSet, ConfigmenuConfigOptions,
42                             "Config options"));
43   setInternalMenu();
44
45   focusmenu = new Focusmenu(this);
46   placementmenu = new Placementmenu(this);
47
48   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuFocusModel,
49                           "Focus Model"), focusmenu);
50   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuWindowPlacement,
51                           "Window Placement"), placementmenu);
52   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuImageDithering,
53                           "Image Dithering"), 1);
54   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuOpaqueMove,
55                           "Opaque Window Moving"), 2);
56   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuFullMax,
57                           "Full Maximization"), 3);
58   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuFocusNew,
59                           "Focus New Windows"), 4);
60   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuFocusLast,
61                           "Focus Last Window on Workspace"), 5);
62   update();
63
64   setItemSelected(2, screen.getImageControl()->doDither());
65   setItemSelected(3, screen.doOpaqueMove());
66   setItemSelected(4, screen.doFullMax());
67   setItemSelected(5, screen.doFocusNew());
68   setItemSelected(6, screen.doFocusLast());
69 }
70
71 Configmenu::~Configmenu(void) {
72   delete focusmenu;
73   delete placementmenu;
74 }
75
76 void Configmenu::itemSelected(int button, int index) {
77   if (button != 1)
78     return;
79
80   BasemenuItem *item = find(index);
81
82   if (!item->function())
83     return;
84
85   switch(item->function()) {
86   case 1: { // dither
87     screen.getImageControl()->
88       setDither((! screen.getImageControl()->doDither()));
89
90     setItemSelected(index, screen.getImageControl()->doDither());
91
92     break;
93   }
94
95   case 2: { // opaque move
96     screen.saveOpaqueMove((! screen.doOpaqueMove()));
97
98     setItemSelected(index, screen.doOpaqueMove());
99
100     break;
101   }
102
103   case 3: { // full maximization
104     screen.saveFullMax((! screen.doFullMax()));
105
106     setItemSelected(index, screen.doFullMax());
107
108     break;
109   }
110   case 4: { // focus new windows
111     screen.saveFocusNew((! screen.doFocusNew()));
112
113     setItemSelected(index, screen.doFocusNew());
114     break;
115   }
116
117   case 5: { // focus last window on workspace
118     screen.saveFocusLast((! screen.doFocusLast()));
119     setItemSelected(index, screen.doFocusLast());
120     break;
121   }
122   } // switch
123 }
124
125 void Configmenu::reconfigure(void) {
126   focusmenu->reconfigure();
127   placementmenu->reconfigure();
128
129   Basemenu::reconfigure();
130 }
131
132 Configmenu::Focusmenu::Focusmenu(Configmenu *cm) : Basemenu(cm->screen) {
133   configmenu = cm;
134
135   setLabel(i18n->getMessage(ConfigmenuSet, ConfigmenuFocusModel,
136                             "Focus Model"));
137   setInternalMenu();
138
139   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuClickToFocus,
140                           "Click To Focus"), 1);
141   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuSloppyFocus,
142                           "Sloppy Focus"), 2);
143   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuAutoRaise,
144                           "Auto Raise"), 3);
145   update();
146
147   setItemSelected(0, (! configmenu->screen.isSloppyFocus()));
148   setItemSelected(1, configmenu->screen.isSloppyFocus());
149   setItemEnabled(2, configmenu->screen.isSloppyFocus());
150   setItemSelected(2, configmenu->screen.doAutoRaise());
151 }
152
153 void Configmenu::Focusmenu::itemSelected(int button, int index) {
154   if (button != 1)
155     return;
156
157   BasemenuItem *item = find(index);
158
159   if (!item->function())
160     return;
161
162   switch (item->function()) {
163   case 1: // click to focus
164     configmenu->screen.saveSloppyFocus(False);
165     configmenu->screen.saveAutoRaise(False);
166
167     if (! configmenu->screen.getOpenbox()->getFocusedWindow())
168       XSetInputFocus(configmenu->screen.getOpenbox()->getXDisplay(),
169                      configmenu->screen.getToolbar()->getWindowID(),
170                      RevertToParent, CurrentTime);
171     else
172       XSetInputFocus(configmenu->screen.getOpenbox()->getXDisplay(),
173                      configmenu->screen.getOpenbox()->
174                      getFocusedWindow()->getClientWindow(),
175                      RevertToParent, CurrentTime);
176
177     configmenu->screen.reconfigure();
178
179     break;
180
181   case 2: // sloppy focus
182     configmenu->screen.saveSloppyFocus(True);
183
184     configmenu->screen.reconfigure();
185
186     break;
187
188   case 3: // auto raise with sloppy focus
189     Bool change = ((configmenu->screen.doAutoRaise()) ? False : True);
190     configmenu->screen.saveAutoRaise(change);
191
192     break;
193   }
194
195   setItemSelected(0, (! configmenu->screen.isSloppyFocus()));
196   setItemSelected(1, configmenu->screen.isSloppyFocus());
197   setItemEnabled(2, configmenu->screen.isSloppyFocus());
198   setItemSelected(2, configmenu->screen.doAutoRaise());
199 }
200
201 Configmenu::Placementmenu::Placementmenu(Configmenu *cm) :
202  Basemenu(cm->screen) {
203   configmenu = cm;
204
205   setLabel(i18n->getMessage(ConfigmenuSet, ConfigmenuWindowPlacement,
206                             "Window Placement"));
207   setInternalMenu();
208
209   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuSmartRows,
210                           "Smart Placement (Rows)"),
211          BScreen::RowSmartPlacement);
212   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuSmartCols,
213                           "Smart Placement (Columns)"),
214          BScreen::ColSmartPlacement);
215   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuCascade,
216                           "Cascade Placement"), BScreen::CascadePlacement);
217   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuBestFit,
218                           "Best Fit Placement"), BScreen::BestFitPlacement);
219   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuLeftRight,
220                           "Left to Right"), BScreen::LeftRight);
221   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuRightLeft,
222                           "Right to Left"), BScreen::RightLeft);
223   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuTopBottom,
224                           "Top to Bottom"), BScreen::TopBottom);
225   insert(i18n->getMessage(ConfigmenuSet, ConfigmenuBottomTop,
226                           "Bottom to Top"), BScreen::BottomTop);
227   update();
228
229   switch (configmenu->screen.getPlacementPolicy()) {
230   case BScreen::RowSmartPlacement:
231     setItemSelected(0, True);
232     break;
233
234   case BScreen::ColSmartPlacement:
235     setItemSelected(1, True);
236     break;
237
238   case BScreen::CascadePlacement:
239     setItemSelected(2, True);
240     break;
241
242   case BScreen::BestFitPlacement:
243     setItemSelected(3, True);
244     break;
245   }
246
247   Bool rl = (configmenu->screen.getRowPlacementDirection() ==
248              BScreen::LeftRight),
249        tb = (configmenu->screen.getColPlacementDirection() ==
250              BScreen::TopBottom);
251
252   setItemSelected(4, rl);
253   setItemSelected(5, ! rl);
254
255   setItemSelected(6, tb);
256   setItemSelected(7, ! tb);
257 }
258
259 void Configmenu::Placementmenu::itemSelected(int button, int index) {
260   if (button != 1)
261     return;
262
263   BasemenuItem *item = find(index);
264
265   if (!item->function())
266     return;
267
268   switch (item->function()) {
269   case BScreen::RowSmartPlacement:
270     configmenu->screen.savePlacementPolicy(item->function());
271
272     setItemSelected(0, True);
273     setItemSelected(1, False);
274     setItemSelected(2, False);
275     setItemSelected(3, False);
276
277     break;
278
279   case BScreen::ColSmartPlacement:
280     configmenu->screen.savePlacementPolicy(item->function());
281
282     setItemSelected(0, False);
283     setItemSelected(1, True);
284     setItemSelected(2, False);
285     setItemSelected(3, False);
286
287     break;
288
289   case BScreen::CascadePlacement:
290     configmenu->screen.savePlacementPolicy(item->function());
291
292     setItemSelected(0, False);
293     setItemSelected(1, False);
294     setItemSelected(2, True);
295     setItemSelected(3, False);
296
297     break;
298
299   case BScreen::BestFitPlacement:
300     configmenu->screen.savePlacementPolicy(item->function());
301
302     setItemSelected(0, False);
303     setItemSelected(1, False);
304     setItemSelected(2, False);
305     setItemSelected(3, True);
306
307     break;
308
309   case BScreen::LeftRight:
310     configmenu->screen.saveRowPlacementDirection(BScreen::LeftRight);
311
312     setItemSelected(4, True);
313     setItemSelected(5, False);
314
315     break;
316
317   case BScreen::RightLeft:
318     configmenu->screen.saveRowPlacementDirection(BScreen::RightLeft);
319
320     setItemSelected(4, False);
321     setItemSelected(5, True);
322
323     break;
324
325   case BScreen::TopBottom:
326     configmenu->screen.saveColPlacementDirection(BScreen::TopBottom);
327
328     setItemSelected(5, True);
329     setItemSelected(6, False);
330
331     break;
332
333   case BScreen::BottomTop:
334     configmenu->screen.saveColPlacementDirection(BScreen::BottomTop);
335
336     setItemSelected(5, False);
337     setItemSelected(6, True);
338
339     break;
340   }
341 }