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