1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Workspace.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
30 #include <X11/Xatom.h>
34 #endif // HAVE_STDIO_H
38 #endif // HAVE_STRING_H
47 #include "blackbox.hh"
48 #include "Clientmenu.hh"
54 #include "Workspace.hh"
55 #include "Windowmenu.hh"
58 Workspace::Workspace(BScreen *scrn, unsigned int i) {
61 cascade_x = cascade_y = 32;
65 clientmenu = new Clientmenu(this);
67 lastfocus = (BlackboxWindow *) 0;
69 setName(screen->getNameOfWorkspace(id));
73 void Workspace::addWindow(BlackboxWindow *w, bool place) {
76 if (place) placeWindow(w);
79 w->setWindowNumber(windowList.size());
81 stackingList.push_front(w);
82 windowList.push_back(w);
84 clientmenu->insert(w->getTitle());
87 screen->updateNetizenWindowAdd(w->getClientWindow(), id);
93 unsigned int Workspace::removeWindow(BlackboxWindow *w) {
96 stackingList.remove(w);
98 if (w->isFocused() && ! screen->getBlackbox()->doShutdown()) {
99 BlackboxWindow *newfocus = 0;
100 if (w->isTransient())
101 newfocus = w->getTransientFor();
102 if (! newfocus && ! stackingList.empty())
103 newfocus = stackingList.front();
104 if (! newfocus || ! newfocus->setInputFocus())
105 screen->getBlackbox()->setFocusedWindow(0);
109 lastfocus = (BlackboxWindow *) 0;
111 windowList.remove(w);
112 clientmenu->remove(w->getWindowNumber());
113 clientmenu->update();
115 screen->updateNetizenWindowDel(w->getClientWindow());
117 BlackboxWindowList::iterator it = windowList.begin();
118 const BlackboxWindowList::iterator end = windowList.end();
120 for (; it != end; ++it, ++i)
121 (*it)->setWindowNumber(i);
124 cascade_x = cascade_y = 32;
130 void Workspace::showAll(void) {
131 std::for_each(stackingList.begin(), stackingList.end(),
132 std::mem_fun(&BlackboxWindow::show));
136 void Workspace::hideAll(void) {
137 // withdraw in reverse order to minimize the number of Expose events
139 BlackboxWindowList lst(stackingList.rbegin(), stackingList.rend());
141 BlackboxWindowList::iterator it = lst.begin();
142 const BlackboxWindowList::iterator end = lst.end();
143 for (; it != end; ++it) {
144 BlackboxWindow *bw = *it;
151 void Workspace::removeAll(void) {
152 while (! windowList.empty())
153 windowList.front()->iconify();
158 * returns the number of transients for win, plus the number of transients
159 * associated with each transient of win
161 static int countTransients(const BlackboxWindow * const win) {
162 int ret = win->getTransients().size();
164 BlackboxWindowList::const_iterator it, end = win->getTransients().end();
165 for (it = win->getTransients().begin(); it != end; ++it) {
166 ret += countTransients(*it);
174 * puts the transients of win into the stack. windows are stacked above
175 * the window before it in the stackvector being iterated, meaning
176 * stack[0] is on bottom, stack[1] is above stack[0], stack[2] is above
179 void Workspace::raiseTransients(const BlackboxWindow * const win,
180 StackVector::iterator &stack) {
181 if (win->getTransients().size() == 0) return; // nothing to do
183 // put win's transients in the stack
184 BlackboxWindowList::const_iterator it, end = win->getTransients().end();
185 for (it = win->getTransients().begin(); it != end; ++it) {
186 *stack++ = (*it)->getFrameWindow();
187 screen->updateNetizenWindowRaise((*it)->getClientWindow());
189 if (! (*it)->isIconic()) {
190 Workspace *wkspc = screen->getWorkspace((*it)->getWorkspaceNumber());
191 wkspc->stackingList.remove((*it));
192 wkspc->stackingList.push_front((*it));
196 // put transients of win's transients in the stack
197 for (it = win->getTransients().begin(); it != end; ++it) {
198 raiseTransients(*it, stack);
203 void Workspace::lowerTransients(const BlackboxWindow * const win,
204 StackVector::iterator &stack) {
205 if (win->getTransients().size() == 0) return; // nothing to do
207 // put transients of win's transients in the stack
208 BlackboxWindowList::const_reverse_iterator it,
209 end = win->getTransients().rend();
210 for (it = win->getTransients().rbegin(); it != end; ++it) {
211 lowerTransients(*it, stack);
214 // put win's transients in the stack
215 for (it = win->getTransients().rbegin(); it != end; ++it) {
216 *stack++ = (*it)->getFrameWindow();
217 screen->updateNetizenWindowLower((*it)->getClientWindow());
219 if (! (*it)->isIconic()) {
220 Workspace *wkspc = screen->getWorkspace((*it)->getWorkspaceNumber());
221 wkspc->stackingList.remove((*it));
222 wkspc->stackingList.push_back((*it));
229 void Workspace::raiseWindow(BlackboxWindow *w) {
230 BlackboxWindow *win = w;
232 // walk up the transient_for's to the window that is not a transient
233 while (win->isTransient()) {
234 if (! win->getTransientFor()) break;
235 win = win->getTransientFor();
238 // get the total window count (win and all transients)
239 unsigned int i = 1 + countTransients(win);
241 // stack the window with all transients above
242 StackVector stack_vector(i);
243 StackVector::iterator stack = stack_vector.begin();
245 *(stack++) = win->getFrameWindow();
246 screen->updateNetizenWindowRaise(win->getClientWindow());
247 if (! win->isIconic()) {
248 Workspace *wkspc = screen->getWorkspace(win->getWorkspaceNumber());
249 wkspc->stackingList.remove(win);
250 wkspc->stackingList.push_front(win);
253 raiseTransients(win, stack);
255 screen->raiseWindows(&stack_vector[0], stack_vector.size());
259 void Workspace::lowerWindow(BlackboxWindow *w) {
260 BlackboxWindow *win = w;
262 // walk up the transient_for's to the window that is not a transient
263 while (win->isTransient()) {
264 if (! win->getTransientFor()) break;
265 win = win->getTransientFor();
268 // get the total window count (win and all transients)
269 unsigned int i = 1 + countTransients(win);
271 // stack the window with all transients above
272 StackVector stack_vector(i);
273 StackVector::iterator stack = stack_vector.begin();
275 lowerTransients(win, stack);
277 *(stack++) = win->getFrameWindow();
278 screen->updateNetizenWindowLower(win->getClientWindow());
279 if (! win->isIconic()) {
280 Workspace *wkspc = screen->getWorkspace(win->getWorkspaceNumber());
281 wkspc->stackingList.remove(win);
282 wkspc->stackingList.push_back(win);
285 XLowerWindow(screen->getBaseDisplay()->getXDisplay(), stack_vector.front());
286 XRestackWindows(screen->getBaseDisplay()->getXDisplay(),
287 &stack_vector[0], stack_vector.size());
291 void Workspace::reconfigure(void) {
292 clientmenu->reconfigure();
293 std::for_each(windowList.begin(), windowList.end(),
294 std::mem_fun(&BlackboxWindow::reconfigure));
298 void Workspace::updateFocusModel(void) {
299 std::for_each(windowList.begin(), windowList.end(),
300 std::mem_fun(&BlackboxWindow::updateFocusModel));
304 BlackboxWindow *Workspace::getWindow(unsigned int index) {
305 if (index < windowList.size()) {
306 BlackboxWindowList::iterator it = windowList.begin();
307 for(; index > 0; --index, ++it); /* increment to index */
315 Workspace::getNextWindowInList(BlackboxWindow *w) {
316 BlackboxWindowList::iterator it = std::find(windowList.begin(),
319 assert(it != windowList.end()); // window must be in list
321 if (it == windowList.end())
322 return windowList.front(); // if we walked off the end, wrap around
328 BlackboxWindow* Workspace::getPrevWindowInList(BlackboxWindow *w) {
329 BlackboxWindowList::iterator it = std::find(windowList.begin(),
332 assert(it != windowList.end()); // window must be in list
333 if (it == windowList.begin())
334 return windowList.back(); // if we walked of the front, wrap around
340 BlackboxWindow* Workspace::getTopWindowOnStack(void) const {
341 return stackingList.front();
345 void Workspace::sendWindowList(Netizen &n) {
346 BlackboxWindowList::iterator it = windowList.begin(),
347 end = windowList.end();
348 for(; it != end; ++it)
349 n.sendWindowAdd((*it)->getClientWindow(), getID());
353 unsigned int Workspace::getCount(void) const {
354 return windowList.size();
358 bool Workspace::isCurrent(void) const {
359 return (id == screen->getCurrentWorkspaceID());
363 bool Workspace::isLastWindow(const BlackboxWindow* const w) const {
364 return (w == windowList.back());
367 void Workspace::setCurrent(void) {
368 screen->changeWorkspaceID(id);
372 void Workspace::setName(const string& new_name) {
373 if (! new_name.empty()) {
376 string tmp =i18n(WorkspaceSet, WorkspaceDefaultNameFormat, "Workspace %d");
377 assert(tmp.length() < 32);
378 char default_name[32];
379 sprintf(default_name, tmp.c_str(), id + 1);
383 clientmenu->setLabel(name);
384 clientmenu->update();
385 screen->saveWorkspaceNames();
390 * Calculate free space available for window placement.
392 typedef std::vector<Rect> rectList;
394 static rectList calcSpace(const Rect &win, const rectList &spaces) {
397 rectList::const_iterator siter, end = spaces.end();
398 for (siter = spaces.begin(); siter != end; ++siter) {
399 const Rect &curr = *siter;
401 if(! win.intersects(curr)) {
402 result.push_back(curr);
406 /* Use an intersection of win and curr to determine the space around
407 * curr that we can use.
409 * NOTE: the spaces calculated can overlap.
414 extra.setCoords(curr.left(), curr.top(),
415 isect.left() - 1, curr.bottom());
416 if (extra.valid()) result.push_back(extra);
419 extra.setCoords(curr.left(), curr.top(),
420 curr.right(), isect.top() - 1);
421 if (extra.valid()) result.push_back(extra);
424 extra.setCoords(isect.right() + 1, curr.top(),
425 curr.right(), curr.bottom());
426 if (extra.valid()) result.push_back(extra);
429 extra.setCoords(curr.left(), isect.bottom() + 1,
430 curr.right(), curr.bottom());
431 if (extra.valid()) result.push_back(extra);
437 static bool rowRLBT(const Rect &first, const Rect &second) {
438 if (first.bottom() == second.bottom())
439 return first.right() > second.right();
440 return first.bottom() > second.bottom();
443 static bool rowRLTB(const Rect &first, const Rect &second) {
444 if (first.y() == second.y())
445 return first.right() > second.right();
446 return first.y() < second.y();
449 static bool rowLRBT(const Rect &first, const Rect &second) {
450 if (first.bottom() == second.bottom())
451 return first.x() < second.x();
452 return first.bottom() > second.bottom();
455 static bool rowLRTB(const Rect &first, const Rect &second) {
456 if (first.y() == second.y())
457 return first.x() < second.x();
458 return first.y() < second.y();
461 static bool colLRTB(const Rect &first, const Rect &second) {
462 if (first.x() == second.x())
463 return first.y() < second.y();
464 return first.x() < second.x();
467 static bool colLRBT(const Rect &first, const Rect &second) {
468 if (first.x() == second.x())
469 return first.bottom() > second.bottom();
470 return first.x() < second.x();
473 static bool colRLTB(const Rect &first, const Rect &second) {
474 if (first.right() == second.right())
475 return first.y() < second.y();
476 return first.right() > second.right();
479 static bool colRLBT(const Rect &first, const Rect &second) {
480 if (first.right() == second.right())
481 return first.bottom() > second.bottom();
482 return first.right() > second.right();
486 bool Workspace::smartPlacement(Rect& win, const Rect& availableArea) {
488 spaces.push_back(availableArea); //initially the entire screen is free
491 BlackboxWindowList::const_iterator wit = windowList.begin(),
492 end = windowList.end();
494 for (; wit != end; ++wit) {
495 const BlackboxWindow* const curr = *wit;
497 if (curr->isShaded()) continue;
499 tmp.setRect(curr->frameRect().x(), curr->frameRect().y(),
500 curr->frameRect().width() + screen->getBorderWidth(),
501 curr->frameRect().height() + screen->getBorderWidth());
503 spaces = calcSpace(tmp, spaces);
506 if (screen->getPlacementPolicy() == BScreen::RowSmartPlacement) {
507 if(screen->getRowPlacementDirection() == BScreen::LeftRight) {
508 if(screen->getColPlacementDirection() == BScreen::TopBottom)
509 sort(spaces.begin(), spaces.end(), rowLRTB);
511 sort(spaces.begin(), spaces.end(), rowLRBT);
513 if(screen->getColPlacementDirection() == BScreen::TopBottom)
514 sort(spaces.begin(), spaces.end(), rowRLTB);
516 sort(spaces.begin(), spaces.end(), rowRLBT);
519 if(screen->getColPlacementDirection() == BScreen::TopBottom) {
520 if(screen->getRowPlacementDirection() == BScreen::LeftRight)
521 sort(spaces.begin(), spaces.end(), colLRTB);
523 sort(spaces.begin(), spaces.end(), colRLTB);
525 if(screen->getRowPlacementDirection() == BScreen::LeftRight)
526 sort(spaces.begin(), spaces.end(), colLRBT);
528 sort(spaces.begin(), spaces.end(), colRLBT);
532 rectList::const_iterator sit = spaces.begin(), spaces_end = spaces.end();
533 for(; sit != spaces_end; ++sit) {
534 if (sit->width() >= win.width() && sit->height() >= win.height())
538 if (sit == spaces_end)
541 //set new position based on the empty space found
542 const Rect& where = *sit;
546 // adjust the location() based on left/right and top/bottom placement
547 if (screen->getPlacementPolicy() == BScreen::RowSmartPlacement) {
548 if (screen->getRowPlacementDirection() == BScreen::RightLeft)
549 win.setX(where.right() - win.width());
550 if (screen->getColPlacementDirection() == BScreen::BottomTop)
551 win.setY(where.bottom() - win.height());
553 if (screen->getColPlacementDirection() == BScreen::BottomTop)
554 win.setY(win.y() + where.height() - win.height());
555 if (screen->getRowPlacementDirection() == BScreen::RightLeft)
556 win.setX(win.x() + where.width() - win.width());
562 bool Workspace::underMousePlacement(Rect &win, const Rect &availableArea) {
566 XQueryPointer(screen->getBlackbox()->getXDisplay(), screen->getRootWindow(),
567 &r, &c, &rx, &ry, &x, &y, &m);
568 x = rx - win.width() / 2;
569 y = ry - win.height() / 2;
571 if (x < availableArea.x())
572 x = availableArea.x();
573 if (y < availableArea.y())
574 y = availableArea.y();
575 if (x + win.width() > availableArea.x() + availableArea.width())
576 x = availableArea.x() + availableArea.width() - win.width();
577 if (y + win.height() > availableArea.y() + availableArea.height())
578 y = availableArea.y() + availableArea.height() - win.height();
587 bool Workspace::cascadePlacement(Rect &win, const Rect &availableArea) {
588 if ((cascade_x > static_cast<signed>(availableArea.width() / 2)) ||
589 (cascade_y > static_cast<signed>(availableArea.height() / 2)))
590 cascade_x = cascade_y = 32;
592 if (cascade_x == 32) {
593 cascade_x += availableArea.x();
594 cascade_y += availableArea.y();
597 win.setPos(cascade_x, cascade_y);
603 void Workspace::placeWindow(BlackboxWindow *win) {
604 Rect availableArea(screen->availableArea()),
605 new_win(availableArea.x(), availableArea.y(),
606 win->frameRect().width(), win->frameRect().height());
609 switch (screen->getPlacementPolicy()) {
610 case BScreen::RowSmartPlacement:
611 case BScreen::ColSmartPlacement:
612 placed = smartPlacement(new_win, availableArea);
614 case BScreen::UnderMousePlacement:
615 placed = underMousePlacement(new_win, availableArea);
617 break; // handled below
620 if (placed == False) {
621 cascadePlacement(new_win, availableArea);
622 cascade_x += win->getTitleHeight() + (screen->getBorderWidth() * 2);
623 cascade_y += win->getTitleHeight() + (screen->getBorderWidth() * 2);
626 if (new_win.right() > availableArea.right())
627 new_win.setX(availableArea.left());
628 if (new_win.bottom() > availableArea.bottom())
629 new_win.setY(availableArea.top());
630 win->configure(new_win.x(), new_win.y(), new_win.width(), new_win.height());