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)
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
23 // stupid macros needed to access some functions in version 2 of the GNU C
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
34 #include <X11/Xatom.h>
38 #include "Clientmenu.h"
42 #include "Workspace.h"
43 #include "Windowmenu.h"
47 #endif // HAVE_STDIO_H
51 #endif // STDC_HEADERS
54 Workspace::Workspace(BScreen *scrn, int i) {
57 cascade_x = cascade_y = 32;
61 stackingList = new LinkedList<OpenboxWindow>;
62 windowList = new LinkedList<OpenboxWindow>;
63 clientmenu = new Clientmenu(this);
65 lastfocus = (OpenboxWindow *) 0;
68 char *tmp = screen->getNameOfWorkspace(id);
73 Workspace::~Workspace(void) {
83 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
86 if (place) placeWindow(w);
89 w->setWindowNumber(windowList->count());
91 stackingList->insert(w, 0);
92 windowList->insert(w);
94 clientmenu->insert((const char **) w->getTitle());
97 screen->updateNetizenWindowAdd(w->getClientWindow(), id);
101 return w->getWindowNumber();
105 const int Workspace::removeWindow(OpenboxWindow *w) {
108 stackingList->remove(w);
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);
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);
128 lastfocus = (OpenboxWindow *) 0;
130 windowList->remove(w->getWindowNumber());
131 clientmenu->remove(w->getWindowNumber());
132 clientmenu->update();
134 screen->updateNetizenWindowDel(w->getClientWindow());
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);
141 return windowList->count();
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);
152 void Workspace::hideAll(void) {
153 LinkedList<OpenboxWindow> lst;
155 LinkedListIterator<OpenboxWindow> it(stackingList);
156 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
159 LinkedListIterator<OpenboxWindow> it2(&lst);
160 for (OpenboxWindow *bw = it2.current(); bw; it2++, bw = it2.current())
166 void Workspace::removeAll(void) {
167 LinkedListIterator<OpenboxWindow> it(windowList);
168 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
173 void Workspace::raiseWindow(OpenboxWindow *w) {
174 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
176 while (bottom->isTransient() && bottom->getTransientFor())
177 bottom = bottom->getTransientFor();
181 while (win->hasTransient() && win->getTransient()) {
182 win = win->getTransient();
187 Window *nstack = new Window[i], *curr = nstack;
192 *(curr++) = win->getFrameWindow();
193 screen->updateNetizenWindowRaise(win->getClientWindow());
195 if (! win->isIconic()) {
196 wkspc = screen->getWorkspace(win->getWorkspaceNumber());
197 wkspc->stackingList->remove(win);
198 wkspc->stackingList->insert(win, 0);
201 if (! win->hasTransient() || ! win->getTransient())
204 win = win->getTransient();
207 screen->raiseWindows(nstack, i);
213 void Workspace::lowerWindow(OpenboxWindow *w) {
214 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
216 while (bottom->isTransient() && bottom->getTransientFor())
217 bottom = bottom->getTransientFor();
221 while (win->hasTransient() && win->getTransient()) {
222 win = win->getTransient();
227 Window *nstack = new Window[i], *curr = nstack;
231 *(curr++) = win->getFrameWindow();
232 screen->updateNetizenWindowLower(win->getClientWindow());
234 if (! win->isIconic()) {
235 wkspc = screen->getWorkspace(win->getWorkspaceNumber());
236 wkspc->stackingList->remove(win);
237 wkspc->stackingList->insert(win);
240 if (! win->getTransientFor())
243 win = win->getTransientFor();
246 screen->getOpenbox()->grab();
248 XLowerWindow(screen->getBaseDisplay()->getXDisplay(), *nstack);
249 XRestackWindows(screen->getBaseDisplay()->getXDisplay(), nstack, i);
251 screen->getOpenbox()->ungrab();
257 void Workspace::reconfigure(void) {
258 clientmenu->reconfigure();
260 LinkedListIterator<OpenboxWindow> it(windowList);
261 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current()) {
262 if (bw->validateClient())
268 OpenboxWindow *Workspace::getWindow(int index) {
269 if ((index >= 0) && (index < windowList->count()))
270 return windowList->find(index);
276 const int Workspace::getCount(void) {
277 return windowList->count();
281 void Workspace::update(void) {
282 clientmenu->update();
283 screen->getToolbar()->redrawWindowLabel(True);
287 Bool Workspace::isCurrent(void) {
288 return (id == screen->getCurrentWorkspaceID());
292 Bool Workspace::isLastWindow(OpenboxWindow *w) {
293 return (w == windowList->last());
296 void Workspace::setCurrent(void) {
297 screen->changeWorkspaceID(id);
301 void Workspace::setName(char *new_name) {
306 name = bstrdup(new_name);
308 name = new char[128];
309 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
310 "Workspace %d"), id + 1);
313 clientmenu->setLabel(name);
314 clientmenu->update();
318 void Workspace::shutdown(void) {
319 while (windowList->count()) {
320 windowList->first()->restore();
321 delete windowList->first();
325 void Workspace::placeWindow(OpenboxWindow *win) {
328 const int win_w = win->getWidth() + (screen->getBorderWidth() * 4),
329 win_h = win->getHeight() + (screen->getBorderWidth() * 4),
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),
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),
346 ((screen->getColPlacementDirection() == BScreen::TopBottom) ? 1 : -1),
348 ((screen->getRowPlacementDirection() == BScreen::LeftRight) ? 1 : -1),
349 delta_x = 8, delta_y = 8;
351 int test_x, test_y, place_x = 0, place_y = 0;
352 LinkedListIterator<OpenboxWindow> it(windowList);
354 switch (screen->getPlacementPolicy()) {
355 case BScreen::RowSmartPlacement: {
356 test_y = (screen->getColPlacementDirection() == BScreen::TopBottom) ?
357 start_pos : screen->getHeight() - win_h - start_pos;
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;
366 ((screen->getRowPlacementDirection() == BScreen::RightLeft) ?
367 test_x > 0 : test_x + win_w < (signed) screen->getWidth())) {
371 for (OpenboxWindow *curr = it.current(); placed && curr;
372 it++, curr = it.current()) {
373 if (curr->isMaximizedFull()) // fully maximized, ignore it
375 int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
377 ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
378 (screen->getBorderWidth() * 4);
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) {
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)
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)
410 test_x += (change_x * delta_x);
413 test_y += (change_y * delta_y);
419 case BScreen::ColSmartPlacement: {
420 test_x = (screen->getRowPlacementDirection() == BScreen::LeftRight) ?
421 start_pos : screen->getWidth() - win_w - start_pos;
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;
430 ((screen->getColPlacementDirection() == BScreen::BottomTop) ?
431 test_y > 0 : test_y + win_h < (signed) screen->getHeight())) {
435 for (OpenboxWindow *curr = it.current(); placed && curr;
436 it++, curr = it.current()) {
437 if (curr->isMaximizedFull()) // fully maximized, ignore it
439 int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
441 ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
442 (screen->getBorderWidth() * 4);
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) {
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)
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)
474 test_y += (change_y * delta_y);
477 test_x += (change_x * delta_x);
485 if (((unsigned) cascade_x > (screen->getWidth() / 2)) ||
486 ((unsigned) cascade_y > (screen->getHeight() / 2)))
487 cascade_x = cascade_y = 32;
492 cascade_x += win->getTitleHeight();
493 cascade_y += win->getTitleHeight();
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;
501 win->configure(place_x, place_y, win->getWidth(), win->getHeight());