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) {
89 if (place) placeWindow(*w);
92 w->setWindowNumber(_windows.size());
94 _zorder.push_front(w);
95 _windows.push_back(w);
97 clientmenu->insert((const char **) w->getTitle());
100 screen.updateNetizenWindowAdd(w->getClientWindow(), id);
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);
122 XSetInputFocus(screen.getOpenbox().getXDisplay(),
123 PointerRoot, None, CurrentTime);
129 lastfocus = (OpenboxWindow *) 0;
131 _windows.erase(_windows.begin() + w->getWindowNumber());
132 clientmenu->remove(w->getWindowNumber());
133 clientmenu->update();
135 screen.updateNetizenWindowDel(w->getClientWindow());
137 winVect::iterator it = _windows.begin();
138 for (int i=0; it != _windows.end(); ++it, ++i)
139 (*it)->setWindowNumber(i);
141 return _windows.size();
145 void Workspace::focusWindow(OpenboxWindow *win) {
146 if (win != (OpenboxWindow *) 0)
147 clientmenu->setItemSelected(win->getWindowNumber(), true);
148 if (_focused != (OpenboxWindow *) 0)
149 clientmenu->setItemSelected(_focused->getWindowNumber(), false);
154 void Workspace::showAll(void) {
155 winList::iterator it;
156 for (it = _zorder.begin(); it != _zorder.end(); ++it)
157 (*it)->deiconify(false, false);
161 void Workspace::hideAll(void) {
162 winList::reverse_iterator it;
163 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
164 if (!(*it)->isStuck())
169 void Workspace::removeAll(void) {
170 winVect::iterator it;
171 for (it = _windows.begin(); it != _windows.end(); ++it)
176 void Workspace::raiseWindow(OpenboxWindow *w) {
177 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
179 while (bottom->isTransient() && bottom->getTransientFor())
180 bottom = bottom->getTransientFor();
184 while (win->hasTransient() && win->getTransient()) {
185 win = win->getTransient();
190 Window *nstack = new Window[i], *curr = nstack;
195 *(curr++) = win->getFrameWindow();
196 screen.updateNetizenWindowRaise(win->getClientWindow());
198 if (! win->isIconic()) {
199 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
200 wkspc->_zorder.remove(win);
201 wkspc->_zorder.push_front(win);
204 if (! win->hasTransient() || ! win->getTransient())
207 win = win->getTransient();
210 screen.raiseWindows(nstack, i);
216 void Workspace::lowerWindow(OpenboxWindow *w) {
217 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
219 while (bottom->isTransient() && bottom->getTransientFor())
220 bottom = bottom->getTransientFor();
224 while (win->hasTransient() && win->getTransient()) {
225 win = win->getTransient();
230 Window *nstack = new Window[i], *curr = nstack;
234 *(curr++) = win->getFrameWindow();
235 screen.updateNetizenWindowLower(win->getClientWindow());
237 if (! win->isIconic()) {
238 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
239 wkspc->_zorder.remove(win);
240 wkspc->_zorder.push_back(win);
243 if (! win->getTransientFor())
246 win = win->getTransientFor();
249 screen.getOpenbox().grab();
251 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
252 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
254 screen.getOpenbox().ungrab();
260 void Workspace::reconfigure(void) {
261 clientmenu->reconfigure();
263 winVect::iterator it;
264 for (it = _windows.begin(); it != _windows.end(); ++it)
265 if ((*it)->validateClient())
266 (*it)->reconfigure();
270 OpenboxWindow *Workspace::getWindow(int index) {
271 if ((index >= 0) && (index < _windows.size()))
272 return _windows[index];
274 return (OpenboxWindow *) 0;
278 const int Workspace::getCount(void) {
279 return _windows.size();
283 void Workspace::update(void) {
284 clientmenu->update();
285 screen.getToolbar()->redrawWindowLabel(True);
289 Bool Workspace::isCurrent(void) {
290 return (id == screen.getCurrentWorkspaceID());
294 void Workspace::setCurrent(void) {
295 screen.changeWorkspaceID(id);
299 void Workspace::setName(const char *new_name) {
304 name = bstrdup(new_name);
306 name = new char[128];
307 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
308 "Workspace %d"), id + 1);
311 clientmenu->setLabel(name);
312 clientmenu->update();
313 screen.saveWorkspaceNames();
317 void Workspace::shutdown(void) {
318 while (!_windows.empty())
319 _windows[0]->restore();
322 static rectList calcSpace(const Rect &win, const rectList &spaces) {
324 rectList::const_iterator siter;
325 for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
326 if(win.Intersect(*siter)) {
327 //Check for space to the left of the window
328 if(win.x() > siter->x())
329 result.push_back(Rect(siter->x(), siter->y(),
330 win.x() - siter->x() - 1,
332 //Check for space above the window
333 if(win.y() > siter->y())
334 result.push_back(Rect(siter->x(), siter->y(),
336 win.y() - siter->y() - 1));
337 //Check for space to the right of the window
338 if((win.x()+win.w()) <
339 (siter->x()+siter->w()))
340 result.push_back(Rect(win.x() + win.w() + 1,
342 siter->x() + siter->w() -
343 win.x() - win.w() - 1,
345 //Check for space below the window
346 if((win.y()+win.h()) <
347 (siter->y()+siter->h()))
348 result.push_back(Rect(siter->x(),
349 win.y() + win.h() + 1,
351 siter->y() + siter->h()-
352 win.y() - win.h() - 1));
356 result.push_back(*siter);
361 bool rowRLBT(const Rect &first, const Rect &second){
362 if (first.y()+first.h()==second.y()+second.h())
363 return first.x()+first.w()>second.x()+second.w();
364 return first.y()+first.h()>second.y()+second.h();
367 bool rowRLTB(const Rect &first, const Rect &second){
368 if (first.y()==second.y())
369 return first.x()+first.w()>second.x()+second.w();
370 return first.y()<second.y();
373 bool rowLRBT(const Rect &first, const Rect &second){
374 if (first.y()+first.h()==second.y()+second.h())
375 return first.x()<second.x();
376 return first.y()+first.h()>second.y()+second.h();
379 bool rowLRTB(const Rect &first, const Rect &second){
380 if (first.y()==second.y())
381 return first.x()<second.x();
382 return first.y()<second.y();
385 bool colLRTB(const Rect &first, const Rect &second){
386 if (first.x()==second.x())
387 return first.y()<second.y();
388 return first.x()<second.x();
391 bool colLRBT(const Rect &first, const Rect &second){
392 if (first.x()==second.x())
393 return first.y()+first.h()>second.y()+second.h();
394 return first.x()<second.x();
397 bool colRLTB(const Rect &first, const Rect &second){
398 if (first.x()+first.w()==second.x()+second.w())
399 return first.y()<second.y();
400 return first.x()+first.w()>second.x()+second.w();
403 bool colRLBT(const Rect &first, const Rect &second){
404 if (first.x()+first.w()==second.x()+second.w())
405 return first.y()+first.h()>second.y()+second.h();
406 return first.x()+first.w()>second.x()+second.w();
410 //BestFitPlacement finds the smallest free space that fits the window
411 //to be placed. It currentl ignores whether placement is right to left or top
413 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
416 rectList::const_iterator siter;
417 spaces.push_back(space); //initially the entire screen is free
420 winVect::iterator it;
421 for (it = _windows.begin(); it != _windows.end(); ++it)
422 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
425 //Find first space that fits the window
427 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
428 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
431 else if(siter->w()*siter->h()<best->h()*best->w())
436 Point *pt = new Point(best->origin());
437 if (screen.colPlacementDirection() != BScreen::TopBottom)
438 pt->setY(pt->y() + (best->h() - win_size.h()));
439 if (screen.rowPlacementDirection() != BScreen::LeftRight)
440 pt->setX(pt->x() + (best->w() - win_size.w()));
443 return NULL; //fall back to cascade
446 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
452 XQueryPointer(screen.getOpenbox().getXDisplay(), screen.getRootWindow(),
453 &r, &c, &rx, &ry, &x, &y, &m);
454 pt = new Point(rx - win_size.w() / 2, ry - win_size.h() / 2);
456 if (pt->x() < space.x())
458 if (pt->y() < space.y())
460 if (pt->x() + win_size.w() > space.x() + space.w())
461 pt->setX(space.x() + space.w() - win_size.w());
462 if (pt->y() + win_size.h() > space.y() + space.h())
463 pt->setY(space.y() + space.h() - win_size.h());
467 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
471 rectList::const_iterator siter;
472 spaces.push_back(space); //initially the entire screen is free
475 winVect::iterator it;
476 for (it = _windows.begin(); it != _windows.end(); ++it)
477 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
479 //Sort spaces by preference
480 if(screen.rowPlacementDirection() == BScreen::RightLeft)
481 if(screen.colPlacementDirection() == BScreen::TopBottom)
482 sort(spaces.begin(),spaces.end(),rowRLTB);
484 sort(spaces.begin(),spaces.end(),rowRLBT);
486 if(screen.colPlacementDirection() == BScreen::TopBottom)
487 sort(spaces.begin(),spaces.end(),rowLRTB);
489 sort(spaces.begin(),spaces.end(),rowLRBT);
491 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
492 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
498 Point *pt = new Point(best->origin());
499 if (screen.colPlacementDirection() != BScreen::TopBottom)
500 pt->setY(best->y() + best->h() - win_size.h());
501 if (screen.rowPlacementDirection() != BScreen::LeftRight)
502 pt->setX(best->x()+best->w()-win_size.w());
505 return NULL; //fall back to cascade
508 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
512 rectList::const_iterator siter;
513 spaces.push_back(space); //initially the entire screen is free
516 winVect::iterator it;
517 for (it = _windows.begin(); it != _windows.end(); ++it)
518 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
520 //Sort spaces by user preference
521 if(screen.colPlacementDirection() == BScreen::TopBottom)
522 if(screen.rowPlacementDirection() == BScreen::LeftRight)
523 sort(spaces.begin(),spaces.end(),colLRTB);
525 sort(spaces.begin(),spaces.end(),colRLTB);
527 if(screen.rowPlacementDirection() == BScreen::LeftRight)
528 sort(spaces.begin(),spaces.end(),colLRBT);
530 sort(spaces.begin(),spaces.end(),colRLBT);
532 //Find first space that fits the window
534 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
535 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
541 Point *pt = new Point(best->origin());
542 if (screen.colPlacementDirection() != BScreen::TopBottom)
543 pt->setY(pt->y() + (best->h() - win_size.h()));
544 if (screen.rowPlacementDirection() != BScreen::LeftRight)
545 pt->setX(pt->x() + (best->w() - win_size.w()));
548 return NULL; //fall back to cascade
552 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
554 if ((cascade_x + win.area().w() + screen.getBorderWidth() * 2 >
555 (space.x() + space.w())) ||
556 (cascade_y + win.area().h() + screen.getBorderWidth() * 2 >
557 (space.y() + space.h())))
558 cascade_x = cascade_y = 0;
559 if (cascade_x < space.x() || cascade_y < space.y()) {
560 cascade_x = space.x();
561 cascade_y = space.y();
564 Point *p = new Point(cascade_x, cascade_y);
565 cascade_x += win.getTitleHeight();
566 cascade_y += win.getTitleHeight();
571 void Workspace::placeWindow(OpenboxWindow &win) {
572 Rect space = screen.availableArea();
573 const Size window_size(win.area().w()+screen.getBorderWidth() * 2,
574 win.area().h()+screen.getBorderWidth() * 2);
577 switch (screen.placementPolicy()) {
578 case BScreen::BestFitPlacement:
579 place = bestFitPlacement(window_size, space);
581 case BScreen::RowSmartPlacement:
582 place = rowSmartPlacement(window_size, space);
584 case BScreen::ColSmartPlacement:
585 place = colSmartPlacement(window_size, space);
587 case BScreen::UnderMousePlacement:
588 case BScreen::ClickMousePlacement:
589 place = underMousePlacement(window_size, space);
594 place = cascadePlacement(win, space);
596 ASSERT(place != NULL);
597 if (place->x() + window_size.w() > (signed) space.x() + space.w())
598 place->setX(((signed) space.x() + space.w() - window_size.w()) / 2);
599 if (place->y() + window_size.h() > (signed) space.y() + space.h())
600 place->setY(((signed) space.y() + space.h() - window_size.h()) / 2);
602 win.configure(place->x(), place->y(), win.area().w(), win.area().h());