1 // Workspace.cc for Openbox
2 // Copyright (c) 2002 - 2002 Ben Jansens (ben@orodu.net)
3 // Copyright (c) 2001 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.
24 // stupid macros needed to access some functions in version 2 of the GNU C
31 # include "../config.h"
32 #endif // HAVE_CONFIG_H
36 #endif // HAVE_STDIO_H
40 #endif // HAVE_STDLIB_H
44 #endif // HAVE_STRING_H
47 #include <X11/Xatom.h>
51 #include "Clientmenu.h"
55 #include "Workspace.h"
56 #include "Windowmenu.h"
62 typedef std::vector<Rect> rectList;
64 Workspace::Workspace(BScreen &scrn, int i) : screen(scrn) {
65 cascade_x = cascade_y = 0;
66 _focused = (OpenboxWindow *) 0;
69 clientmenu = new Clientmenu(*this);
71 lastfocus = (OpenboxWindow *) 0;
74 setName(screen.getNameOfWorkspace(id));
78 Workspace::~Workspace(void) {
86 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
90 w->setWindowNumber(_windows.size());
92 _zorder.push_front(w);
93 _windows.push_back(w);
95 clientmenu->insert((const char **) w->getTitle());
98 screen.updateNetizenWindowAdd(w->getClientWindow(), id);
102 if (place) placeWindow(*w);
104 return w->getWindowNumber();
108 const int Workspace::removeWindow(OpenboxWindow *w) {
113 if (w->isFocused()) {
114 if (w->isTransient() && w->getTransientFor() &&
115 w->getTransientFor()->isVisible()) {
116 w->getTransientFor()->setInputFocus();
118 if (screen.sloppyFocus() || // sloppy focus
119 _zorder.empty() || // click focus but no windows
120 !_zorder.front()->setInputFocus()) { // tried window, but wont focus
121 screen.getOpenbox().focusWindow((OpenboxWindow *) 0);
127 lastfocus = (OpenboxWindow *) 0;
129 _windows.erase(_windows.begin() + w->getWindowNumber());
130 clientmenu->remove(w->getWindowNumber());
131 clientmenu->update();
133 screen.updateNetizenWindowDel(w->getClientWindow());
135 winVect::iterator it = _windows.begin();
136 for (int i=0; it != _windows.end(); ++it, ++i)
137 (*it)->setWindowNumber(i);
139 return _windows.size();
143 void Workspace::focusWindow(OpenboxWindow *win) {
144 if (win != (OpenboxWindow *) 0)
145 clientmenu->setItemSelected(win->getWindowNumber(), true);
146 if (_focused != (OpenboxWindow *) 0)
147 clientmenu->setItemSelected(_focused->getWindowNumber(), false);
152 void Workspace::showAll(void) {
153 winList::iterator it;
154 for (it = _zorder.begin(); it != _zorder.end(); ++it)
155 (*it)->deiconify(false, false);
159 void Workspace::hideAll(void) {
160 winList::reverse_iterator it;
161 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
162 if (!(*it)->isStuck())
167 void Workspace::removeAll(void) {
168 winVect::iterator it;
169 for (it = _windows.begin(); it != _windows.end(); ++it)
174 void Workspace::raiseWindow(OpenboxWindow *w) {
175 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
177 while (bottom->isTransient() && bottom->getTransientFor())
178 bottom = bottom->getTransientFor();
182 while (win->hasTransient() && win->getTransient()) {
183 win = win->getTransient();
188 Window *nstack = new Window[i], *curr = nstack;
193 *(curr++) = win->getFrameWindow();
194 screen.updateNetizenWindowRaise(win->getClientWindow());
196 if (! win->isIconic()) {
197 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
198 wkspc->_zorder.remove(win);
199 wkspc->_zorder.push_front(win);
202 if (! win->hasTransient() || ! win->getTransient())
205 win = win->getTransient();
208 screen.raiseWindows(nstack, i);
214 void Workspace::lowerWindow(OpenboxWindow *w) {
215 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
217 while (bottom->isTransient() && bottom->getTransientFor())
218 bottom = bottom->getTransientFor();
222 while (win->hasTransient() && win->getTransient()) {
223 win = win->getTransient();
228 Window *nstack = new Window[i], *curr = nstack;
232 *(curr++) = win->getFrameWindow();
233 screen.updateNetizenWindowLower(win->getClientWindow());
235 if (! win->isIconic()) {
236 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
237 wkspc->_zorder.remove(win);
238 wkspc->_zorder.push_back(win);
241 if (! win->getTransientFor())
244 win = win->getTransientFor();
247 screen.getOpenbox().grab();
249 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
250 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
252 screen.getOpenbox().ungrab();
258 void Workspace::reconfigure(void) {
259 clientmenu->reconfigure();
261 winVect::iterator it;
262 for (it = _windows.begin(); it != _windows.end(); ++it)
263 if ((*it)->validateClient())
264 (*it)->reconfigure();
268 OpenboxWindow *Workspace::getWindow(int index) {
269 if ((index >= 0) && (index < _windows.size()))
270 return _windows[index];
272 return (OpenboxWindow *) 0;
276 const int Workspace::getCount(void) {
277 return _windows.size();
281 void Workspace::update(void) {
282 clientmenu->update();
283 screen.getToolbar()->redrawWindowLabel(True);
287 Bool Workspace::isCurrent(void) {
288 return (id == screen.getCurrentWorkspaceID());
292 void Workspace::setCurrent(void) {
293 screen.changeWorkspaceID(id);
297 void Workspace::setName(const char *new_name) {
302 name = bstrdup(new_name);
304 name = new char[128];
305 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
306 "Workspace %d"), id + 1);
309 clientmenu->setLabel(name);
310 clientmenu->update();
311 screen.saveWorkspaceNames();
315 void Workspace::shutdown(void) {
316 while (!_windows.empty())
317 _windows[0]->restore();
320 static rectList calcSpace(const Rect &win, const rectList &spaces) {
322 rectList::const_iterator siter;
323 for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
324 if(win.Intersect(*siter)) {
325 //Check for space to the left of the window
326 if(win.x() > siter->x())
327 result.push_back(Rect(siter->x(), siter->y(),
328 win.x() - siter->x() - 1,
330 //Check for space above the window
331 if(win.y() > siter->y())
332 result.push_back(Rect(siter->x(), siter->y(),
334 win.y() - siter->y() - 1));
335 //Check for space to the right of the window
336 if((win.x()+win.w()) <
337 (siter->x()+siter->w()))
338 result.push_back(Rect(win.x() + win.w() + 1,
340 siter->x() + siter->w() -
341 win.x() - win.w() - 1,
343 //Check for space below the window
344 if((win.y()+win.h()) <
345 (siter->y()+siter->h()))
346 result.push_back(Rect(siter->x(),
347 win.y() + win.h() + 1,
349 siter->y() + siter->h()-
350 win.y() - win.h() - 1));
354 result.push_back(*siter);
359 bool rowRLBT(const Rect &first, const Rect &second){
360 if (first.y()+first.h()==second.y()+second.h())
361 return first.x()+first.w()>second.x()+second.w();
362 return first.y()+first.h()>second.y()+second.h();
365 bool rowRLTB(const Rect &first, const Rect &second){
366 if (first.y()==second.y())
367 return first.x()+first.w()>second.x()+second.w();
368 return first.y()<second.y();
371 bool rowLRBT(const Rect &first, const Rect &second){
372 if (first.y()+first.h()==second.y()+second.h())
373 return first.x()<second.x();
374 return first.y()+first.h()>second.y()+second.h();
377 bool rowLRTB(const Rect &first, const Rect &second){
378 if (first.y()==second.y())
379 return first.x()<second.x();
380 return first.y()<second.y();
383 bool colLRTB(const Rect &first, const Rect &second){
384 if (first.x()==second.x())
385 return first.y()<second.y();
386 return first.x()<second.x();
389 bool colLRBT(const Rect &first, const Rect &second){
390 if (first.x()==second.x())
391 return first.y()+first.h()>second.y()+second.h();
392 return first.x()<second.x();
395 bool colRLTB(const Rect &first, const Rect &second){
396 if (first.x()+first.w()==second.x()+second.w())
397 return first.y()<second.y();
398 return first.x()+first.w()>second.x()+second.w();
401 bool colRLBT(const Rect &first, const Rect &second){
402 if (first.x()+first.w()==second.x()+second.w())
403 return first.y()+first.h()>second.y()+second.h();
404 return first.x()+first.w()>second.x()+second.w();
408 //BestFitPlacement finds the smallest free space that fits the window
409 //to be placed. It currentl ignores whether placement is right to left or top
411 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
414 rectList::const_iterator siter;
415 spaces.push_back(space); //initially the entire screen is free
418 winVect::iterator it;
419 for (it = _windows.begin(); it != _windows.end(); ++it)
420 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
423 //Find first space that fits the window
425 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
426 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
429 else if(siter->w()*siter->h()<best->h()*best->w())
434 Point *pt = new Point(best->origin());
435 if (screen.colPlacementDirection() != BScreen::TopBottom)
436 pt->setY(pt->y() + (best->h() - win_size.h()));
437 if (screen.rowPlacementDirection() != BScreen::LeftRight)
438 pt->setX(pt->x() + (best->w() - win_size.w()));
441 return NULL; //fall back to cascade
444 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
450 XQueryPointer(screen.getOpenbox().getXDisplay(), screen.getRootWindow(),
451 &r, &c, &rx, &ry, &x, &y, &m);
452 pt = new Point(rx - win_size.w() / 2, ry - win_size.h() / 2);
454 if (pt->x() < space.x())
456 if (pt->y() < space.y())
458 if (pt->x() + win_size.w() > space.x() + space.w())
459 pt->setX(space.x() + space.w() - win_size.w());
460 if (pt->y() + win_size.h() > space.y() + space.h())
461 pt->setY(space.y() + space.h() - win_size.h());
465 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
469 rectList::const_iterator siter;
470 spaces.push_back(space); //initially the entire screen is free
473 winVect::iterator it;
474 for (it = _windows.begin(); it != _windows.end(); ++it)
475 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
477 //Sort spaces by preference
478 if(screen.rowPlacementDirection() == BScreen::RightLeft)
479 if(screen.colPlacementDirection() == BScreen::TopBottom)
480 sort(spaces.begin(),spaces.end(),rowRLTB);
482 sort(spaces.begin(),spaces.end(),rowRLBT);
484 if(screen.colPlacementDirection() == BScreen::TopBottom)
485 sort(spaces.begin(),spaces.end(),rowLRTB);
487 sort(spaces.begin(),spaces.end(),rowLRBT);
489 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
490 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
496 Point *pt = new Point(best->origin());
497 if (screen.colPlacementDirection() != BScreen::TopBottom)
498 pt->setY(best->y() + best->h() - win_size.h());
499 if (screen.rowPlacementDirection() != BScreen::LeftRight)
500 pt->setX(best->x()+best->w()-win_size.w());
503 return NULL; //fall back to cascade
506 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
510 rectList::const_iterator siter;
511 spaces.push_back(space); //initially the entire screen is free
514 winVect::iterator it;
515 for (it = _windows.begin(); it != _windows.end(); ++it)
516 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
518 //Sort spaces by user preference
519 if(screen.colPlacementDirection() == BScreen::TopBottom)
520 if(screen.rowPlacementDirection() == BScreen::LeftRight)
521 sort(spaces.begin(),spaces.end(),colLRTB);
523 sort(spaces.begin(),spaces.end(),colRLTB);
525 if(screen.rowPlacementDirection() == BScreen::LeftRight)
526 sort(spaces.begin(),spaces.end(),colLRBT);
528 sort(spaces.begin(),spaces.end(),colRLBT);
530 //Find first space that fits the window
532 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
533 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
539 Point *pt = new Point(best->origin());
540 if (screen.colPlacementDirection() != BScreen::TopBottom)
541 pt->setY(pt->y() + (best->h() - win_size.h()));
542 if (screen.rowPlacementDirection() != BScreen::LeftRight)
543 pt->setX(pt->x() + (best->w() - win_size.w()));
546 return NULL; //fall back to cascade
550 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
552 if ((cascade_x + win.area().w() + screen.getBorderWidth() * 2 >
553 (space.x() + space.w())) ||
554 (cascade_y + win.area().h() + screen.getBorderWidth() * 2 >
555 (space.y() + space.h())))
556 cascade_x = cascade_y = 0;
557 if (cascade_x < space.x() || cascade_y < space.y()) {
558 cascade_x = space.x();
559 cascade_y = space.y();
562 Point *p = new Point(cascade_x, cascade_y);
563 cascade_x += win.getTitleHeight();
564 cascade_y += win.getTitleHeight();
569 void Workspace::placeWindow(OpenboxWindow &win) {
570 Rect space = screen.availableArea();
571 const Size window_size(win.area().w()+screen.getBorderWidth() * 2,
572 win.area().h()+screen.getBorderWidth() * 2);
575 switch (screen.placementPolicy()) {
576 case BScreen::BestFitPlacement:
577 place = bestFitPlacement(window_size, space);
579 case BScreen::RowSmartPlacement:
580 place = rowSmartPlacement(window_size, space);
582 case BScreen::ColSmartPlacement:
583 place = colSmartPlacement(window_size, space);
585 case BScreen::UnderMousePlacement:
586 case BScreen::ClickMousePlacement:
587 place = underMousePlacement(window_size, space);
592 place = cascadePlacement(win, space);
594 ASSERT(place != NULL);
595 if (place->x() + window_size.w() > (signed) space.x() + space.w())
596 place->setX(((signed) space.x() + space.w() - window_size.w()) / 2);
597 if (place->y() + window_size.h() > (signed) space.y() + space.h())
598 place->setY(((signed) space.y() + space.h() - window_size.h()) / 2);
600 win.configure(place->x(), place->y(), win.area().w(), win.area().h());