]> Dogcows Code - chaz/openbox/blob - src/Workspace.cc
Initial revision
[chaz/openbox] / src / Workspace.cc
1 // Workspace.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 <X11/Xlib.h>
34 #include <X11/Xatom.h>
35
36 #include "i18n.h"
37 #include "openbox.h"
38 #include "Clientmenu.h"
39 #include "Screen.h"
40 #include "Toolbar.h"
41 #include "Window.h"
42 #include "Workspace.h"
43 #include "Windowmenu.h"
44
45 #ifdef HAVE_STDIO_H
46 # include <stdio.h>
47 #endif // HAVE_STDIO_H
48
49 #ifdef STDC_HEADERS
50 # include <string.h>
51 #endif // STDC_HEADERS
52
53
54 Workspace::Workspace(BScreen *scrn, int i) {
55 screen = scrn;
56
57 cascade_x = cascade_y = 32;
58
59 id = i;
60
61 stackingList = new LinkedList<OpenboxWindow>;
62 windowList = new LinkedList<OpenboxWindow>;
63 clientmenu = new Clientmenu(this);
64
65 lastfocus = (OpenboxWindow *) 0;
66
67 name = (char *) 0;
68 char *tmp = screen->getNameOfWorkspace(id);
69 setName(tmp);
70 }
71
72
73 Workspace::~Workspace(void) {
74 delete stackingList;
75 delete windowList;
76 delete clientmenu;
77
78 if (name)
79 delete [] name;
80 }
81
82
83 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
84 if (! w) return -1;
85
86 if (place) placeWindow(w);
87
88 w->setWorkspace(id);
89 w->setWindowNumber(windowList->count());
90
91 stackingList->insert(w, 0);
92 windowList->insert(w);
93
94 clientmenu->insert((const char **) w->getTitle());
95 clientmenu->update();
96
97 screen->updateNetizenWindowAdd(w->getClientWindow(), id);
98
99 raiseWindow(w);
100
101 return w->getWindowNumber();
102 }
103
104
105 const int Workspace::removeWindow(OpenboxWindow *w) {
106 if (! w) return -1;
107
108 stackingList->remove(w);
109
110 if (w->isFocused()) {
111 if (w->isTransient() && w->getTransientFor() &&
112 w->getTransientFor()->isVisible()) {
113 w->getTransientFor()->setInputFocus();
114 } else if (screen->isSloppyFocus()) {
115 screen->getOpenbox()->setFocusedWindow((OpenboxWindow *) 0);
116 } else {
117 OpenboxWindow *top = stackingList->first();
118 if (! top || ! top->setInputFocus()) {
119 screen->getOpenbox()->setFocusedWindow((OpenboxWindow *) 0);
120 XSetInputFocus(screen->getOpenbox()->getXDisplay(),
121 screen->getToolbar()->getWindowID(),
122 RevertToParent, CurrentTime);
123 }
124 }
125 }
126
127 if (lastfocus == w)
128 lastfocus = (OpenboxWindow *) 0;
129
130 windowList->remove(w->getWindowNumber());
131 clientmenu->remove(w->getWindowNumber());
132 clientmenu->update();
133
134 screen->updateNetizenWindowDel(w->getClientWindow());
135
136 LinkedListIterator<OpenboxWindow> it(windowList);
137 OpenboxWindow *bw = it.current();
138 for (int i = 0; bw; it++, i++, bw = it.current())
139 bw->setWindowNumber(i);
140
141 return windowList->count();
142 }
143
144
145 void Workspace::showAll(void) {
146 LinkedListIterator<OpenboxWindow> it(stackingList);
147 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
148 bw->deiconify(False, False);
149 }
150
151
152 void Workspace::hideAll(void) {
153 LinkedList<OpenboxWindow> lst;
154
155 LinkedListIterator<OpenboxWindow> it(stackingList);
156 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
157 lst.insert(bw, 0);
158
159 LinkedListIterator<OpenboxWindow> it2(&lst);
160 for (OpenboxWindow *bw = it2.current(); bw; it2++, bw = it2.current())
161 if (! bw->isStuck())
162 bw->withdraw();
163 }
164
165
166 void Workspace::removeAll(void) {
167 LinkedListIterator<OpenboxWindow> it(windowList);
168 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
169 bw->iconify();
170 }
171
172
173 void Workspace::raiseWindow(OpenboxWindow *w) {
174 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
175
176 while (bottom->isTransient() && bottom->getTransientFor())
177 bottom = bottom->getTransientFor();
178
179 int i = 1;
180 win = bottom;
181 while (win->hasTransient() && win->getTransient()) {
182 win = win->getTransient();
183
184 i++;
185 }
186
187 Window *nstack = new Window[i], *curr = nstack;
188 Workspace *wkspc;
189
190 win = bottom;
191 while (True) {
192 *(curr++) = win->getFrameWindow();
193 screen->updateNetizenWindowRaise(win->getClientWindow());
194
195 if (! win->isIconic()) {
196 wkspc = screen->getWorkspace(win->getWorkspaceNumber());
197 wkspc->stackingList->remove(win);
198 wkspc->stackingList->insert(win, 0);
199 }
200
201 if (! win->hasTransient() || ! win->getTransient())
202 break;
203
204 win = win->getTransient();
205 }
206
207 screen->raiseWindows(nstack, i);
208
209 delete [] nstack;
210 }
211
212
213 void Workspace::lowerWindow(OpenboxWindow *w) {
214 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
215
216 while (bottom->isTransient() && bottom->getTransientFor())
217 bottom = bottom->getTransientFor();
218
219 int i = 1;
220 win = bottom;
221 while (win->hasTransient() && win->getTransient()) {
222 win = win->getTransient();
223
224 i++;
225 }
226
227 Window *nstack = new Window[i], *curr = nstack;
228 Workspace *wkspc;
229
230 while (True) {
231 *(curr++) = win->getFrameWindow();
232 screen->updateNetizenWindowLower(win->getClientWindow());
233
234 if (! win->isIconic()) {
235 wkspc = screen->getWorkspace(win->getWorkspaceNumber());
236 wkspc->stackingList->remove(win);
237 wkspc->stackingList->insert(win);
238 }
239
240 if (! win->getTransientFor())
241 break;
242
243 win = win->getTransientFor();
244 }
245
246 screen->getOpenbox()->grab();
247
248 XLowerWindow(screen->getBaseDisplay()->getXDisplay(), *nstack);
249 XRestackWindows(screen->getBaseDisplay()->getXDisplay(), nstack, i);
250
251 screen->getOpenbox()->ungrab();
252
253 delete [] nstack;
254 }
255
256
257 void Workspace::reconfigure(void) {
258 clientmenu->reconfigure();
259
260 LinkedListIterator<OpenboxWindow> it(windowList);
261 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current()) {
262 if (bw->validateClient())
263 bw->reconfigure();
264 }
265 }
266
267
268 OpenboxWindow *Workspace::getWindow(int index) {
269 if ((index >= 0) && (index < windowList->count()))
270 return windowList->find(index);
271 else
272 return 0;
273 }
274
275
276 const int Workspace::getCount(void) {
277 return windowList->count();
278 }
279
280
281 void Workspace::update(void) {
282 clientmenu->update();
283 screen->getToolbar()->redrawWindowLabel(True);
284 }
285
286
287 Bool Workspace::isCurrent(void) {
288 return (id == screen->getCurrentWorkspaceID());
289 }
290
291
292 Bool Workspace::isLastWindow(OpenboxWindow *w) {
293 return (w == windowList->last());
294 }
295
296 void Workspace::setCurrent(void) {
297 screen->changeWorkspaceID(id);
298 }
299
300
301 void Workspace::setName(char *new_name) {
302 if (name)
303 delete [] name;
304
305 if (new_name) {
306 name = bstrdup(new_name);
307 } else {
308 name = new char[128];
309 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
310 "Workspace %d"), id + 1);
311 }
312
313 clientmenu->setLabel(name);
314 clientmenu->update();
315 }
316
317
318 void Workspace::shutdown(void) {
319 while (windowList->count()) {
320 windowList->first()->restore();
321 delete windowList->first();
322 }
323 }
324
325 void Workspace::placeWindow(OpenboxWindow *win) {
326 Bool placed = False;
327
328 const int win_w = win->getWidth() + (screen->getBorderWidth() * 4),
329 win_h = win->getHeight() + (screen->getBorderWidth() * 4),
330 #ifdef SLIT
331 slit_x = screen->getSlit()->getX() - screen->getBorderWidth(),
332 slit_y = screen->getSlit()->getY() - screen->getBorderWidth(),
333 slit_w = screen->getSlit()->getWidth() +
334 (screen->getBorderWidth() * 4),
335 slit_h = screen->getSlit()->getHeight() +
336 (screen->getBorderWidth() * 4),
337 #endif // SLIT
338 toolbar_x = screen->getToolbar()->getX() - screen->getBorderWidth(),
339 toolbar_y = screen->getToolbar()->getY() - screen->getBorderWidth(),
340 toolbar_w = screen->getToolbar()->getWidth() +
341 (screen->getBorderWidth() * 4),
342 toolbar_h = screen->getToolbar()->getHeight() +
343 (screen->getBorderWidth() * 4),
344 start_pos = 0,
345 change_y =
346 ((screen->getColPlacementDirection() == BScreen::TopBottom) ? 1 : -1),
347 change_x =
348 ((screen->getRowPlacementDirection() == BScreen::LeftRight) ? 1 : -1),
349 delta_x = 8, delta_y = 8;
350
351 int test_x, test_y, place_x = 0, place_y = 0;
352 LinkedListIterator<OpenboxWindow> it(windowList);
353
354 switch (screen->getPlacementPolicy()) {
355 case BScreen::RowSmartPlacement: {
356 test_y = (screen->getColPlacementDirection() == BScreen::TopBottom) ?
357 start_pos : screen->getHeight() - win_h - start_pos;
358
359 while (!placed &&
360 ((screen->getColPlacementDirection() == BScreen::BottomTop) ?
361 test_y > 0 : test_y + win_h < (signed) screen->getHeight())) {
362 test_x = (screen->getRowPlacementDirection() == BScreen::LeftRight) ?
363 start_pos : screen->getWidth() - win_w - start_pos;
364
365 while (!placed &&
366 ((screen->getRowPlacementDirection() == BScreen::RightLeft) ?
367 test_x > 0 : test_x + win_w < (signed) screen->getWidth())) {
368 placed = True;
369
370 it.reset();
371 for (OpenboxWindow *curr = it.current(); placed && curr;
372 it++, curr = it.current()) {
373 if (curr->isMaximizedFull()) // fully maximized, ignore it
374 continue;
375 int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
376 int curr_h =
377 ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
378 (screen->getBorderWidth() * 4);
379
380 if (curr->getXFrame() < test_x + win_w &&
381 curr->getXFrame() + curr_w > test_x &&
382 curr->getYFrame() < test_y + win_h &&
383 curr->getYFrame() + curr_h > test_y) {
384 placed = False;
385 }
386 }
387
388 if (placed &&
389 (toolbar_x < test_x + win_w &&
390 toolbar_x + toolbar_w > test_x &&
391 toolbar_y < test_y + win_h &&
392 toolbar_y + toolbar_h > test_y)
393 #ifdef SLIT
394 ||
395 (slit_x < test_x + win_w &&
396 slit_x + slit_w > test_x &&
397 slit_y < test_y + win_h &&
398 slit_y + slit_h > test_y)
399 #endif // SLIT
400 )
401 placed = False;
402
403 if (placed) {
404 place_x = test_x;
405 place_y = test_y;
406
407 break;
408 }
409
410 test_x += (change_x * delta_x);
411 }
412
413 test_y += (change_y * delta_y);
414 }
415
416 break;
417 }
418
419 case BScreen::ColSmartPlacement: {
420 test_x = (screen->getRowPlacementDirection() == BScreen::LeftRight) ?
421 start_pos : screen->getWidth() - win_w - start_pos;
422
423 while (!placed &&
424 ((screen->getRowPlacementDirection() == BScreen::RightLeft) ?
425 test_x > 0 : test_x + win_w < (signed) screen->getWidth())) {
426 test_y = (screen->getColPlacementDirection() == BScreen::TopBottom) ?
427 start_pos : screen->getHeight() - win_h - start_pos;
428
429 while (!placed &&
430 ((screen->getColPlacementDirection() == BScreen::BottomTop) ?
431 test_y > 0 : test_y + win_h < (signed) screen->getHeight())) {
432 placed = True;
433
434 it.reset();
435 for (OpenboxWindow *curr = it.current(); placed && curr;
436 it++, curr = it.current()) {
437 if (curr->isMaximizedFull()) // fully maximized, ignore it
438 continue;
439 int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
440 int curr_h =
441 ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
442 (screen->getBorderWidth() * 4);
443
444 if (curr->getXFrame() < test_x + win_w &&
445 curr->getXFrame() + curr_w > test_x &&
446 curr->getYFrame() < test_y + win_h &&
447 curr->getYFrame() + curr_h > test_y) {
448 placed = False;
449 }
450 }
451
452 if (placed &&
453 (toolbar_x < test_x + win_w &&
454 toolbar_x + toolbar_w > test_x &&
455 toolbar_y < test_y + win_h &&
456 toolbar_y + toolbar_h > test_y)
457 #ifdef SLIT
458 ||
459 (slit_x < test_x + win_w &&
460 slit_x + slit_w > test_x &&
461 slit_y < test_y + win_h &&
462 slit_y + slit_h > test_y)
463 #endif // SLIT
464 )
465 placed = False;
466
467 if (placed) {
468 place_x = test_x;
469 place_y = test_y;
470
471 break;
472 }
473
474 test_y += (change_y * delta_y);
475 }
476
477 test_x += (change_x * delta_x);
478 }
479
480 break;
481 }
482 } // switch
483
484 if (! placed) {
485 if (((unsigned) cascade_x > (screen->getWidth() / 2)) ||
486 ((unsigned) cascade_y > (screen->getHeight() / 2)))
487 cascade_x = cascade_y = 32;
488
489 place_x = cascade_x;
490 place_y = cascade_y;
491
492 cascade_x += win->getTitleHeight();
493 cascade_y += win->getTitleHeight();
494 }
495
496 if (place_x + win_w > (signed) screen->getWidth())
497 place_x = (((signed) screen->getWidth()) - win_w) / 2;
498 if (place_y + win_h > (signed) screen->getHeight())
499 place_y = (((signed) screen->getHeight()) - win_h) / 2;
500
501 win->configure(place_x, place_y, win->getWidth(), win->getHeight());
502 }
This page took 0.058549 seconds and 4 git commands to generate.