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 = _last = (OpenboxWindow *) 0;
69 clientmenu = new Clientmenu(*this);
72 setName(screen.getNameOfWorkspace(id));
76 Workspace::~Workspace(void) {
84 int Workspace::addWindow(OpenboxWindow *w, bool place) {
87 if (place) placeWindow(*w);
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 return w->getWindowNumber();
106 int Workspace::removeWindow(OpenboxWindow *w) {
111 if (w->isFocused()) {
113 _last = (OpenboxWindow *) 0;
115 OpenboxWindow *fw = (OpenboxWindow *) 0;
116 if (w->isTransient() && w->getTransientFor() &&
117 w->getTransientFor()->isVisible())
118 fw = w->getTransientFor();
119 else if (screen.sloppyFocus()) // sloppy focus
120 fw = (OpenboxWindow *) 0;
121 else if (!_zorder.empty()) // click focus
122 fw = _zorder.front();
124 if (!(fw != (OpenboxWindow *) 0 && fw->setInputFocus()))
125 screen.getOpenbox().focusWindow(0);
128 _windows.erase(_windows.begin() + w->getWindowNumber());
129 clientmenu->remove(w->getWindowNumber());
130 clientmenu->update();
132 screen.updateNetizenWindowDel(w->getClientWindow());
134 winVect::iterator it = _windows.begin();
135 for (int i=0; it != _windows.end(); ++it, ++i)
136 (*it)->setWindowNumber(i);
138 return _windows.size();
142 void Workspace::focusWindow(OpenboxWindow *win) {
143 if (win != (OpenboxWindow *) 0)
144 clientmenu->setItemSelected(win->getWindowNumber(), true);
145 if (_focused != (OpenboxWindow *) 0)
146 clientmenu->setItemSelected(_focused->getWindowNumber(), false);
148 if (win != (OpenboxWindow *) 0)
153 void Workspace::showAll(void) {
154 winList::iterator it;
155 for (it = _zorder.begin(); it != _zorder.end(); ++it)
156 (*it)->deiconify(false, false);
160 void Workspace::hideAll(void) {
161 winList::reverse_iterator it;
162 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
163 if (!(*it)->isStuck())
168 void Workspace::removeAll(void) {
169 winVect::iterator it;
170 for (it = _windows.begin(); it != _windows.end(); ++it)
175 void Workspace::raiseWindow(OpenboxWindow *w) {
176 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
178 while (bottom->isTransient() && bottom->getTransientFor())
179 bottom = bottom->getTransientFor();
183 while (win->hasTransient() && win->getTransient()) {
184 win = win->getTransient();
189 Window *nstack = new Window[i], *curr = nstack;
194 *(curr++) = win->getFrameWindow();
195 screen.updateNetizenWindowRaise(win->getClientWindow());
197 if (! win->isIconic()) {
198 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
199 wkspc->_zorder.remove(win);
200 wkspc->_zorder.push_front(win);
203 if (! win->hasTransient() || ! win->getTransient())
206 win = win->getTransient();
209 screen.raiseWindows(nstack, i);
215 void Workspace::lowerWindow(OpenboxWindow *w) {
216 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
218 while (bottom->isTransient() && bottom->getTransientFor())
219 bottom = bottom->getTransientFor();
223 while (win->hasTransient() && win->getTransient()) {
224 win = win->getTransient();
229 Window *nstack = new Window[i], *curr = nstack;
233 *(curr++) = win->getFrameWindow();
234 screen.updateNetizenWindowLower(win->getClientWindow());
236 if (! win->isIconic()) {
237 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
238 wkspc->_zorder.remove(win);
239 wkspc->_zorder.push_back(win);
242 if (! win->getTransientFor())
245 win = win->getTransientFor();
248 screen.getOpenbox().grab();
250 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
251 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
253 screen.getOpenbox().ungrab();
259 void Workspace::reconfigure(void) {
260 clientmenu->reconfigure();
262 winVect::iterator it;
263 for (it = _windows.begin(); it != _windows.end(); ++it)
264 if ((*it)->validateClient())
265 (*it)->reconfigure();
269 OpenboxWindow *Workspace::getWindow(int index) {
270 if ((index >= 0) && (index < (signed)_windows.size()))
271 return _windows[index];
273 return (OpenboxWindow *) 0;
277 int Workspace::getCount(void) {
278 return (signed)_windows.size();
282 void Workspace::update(void) {
283 clientmenu->update();
284 screen.getToolbar()->redrawWindowLabel(true);
288 bool Workspace::isCurrent(void) {
289 return (id == screen.getCurrentWorkspaceID());
293 void Workspace::setCurrent(void) {
294 screen.changeWorkspaceID(id);
298 void Workspace::setName(const char *new_name) {
303 name = bstrdup(new_name);
305 name = new char[128];
306 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
307 "Workspace %d"), id + 1);
310 clientmenu->setLabel(name);
311 clientmenu->update();
312 screen.saveWorkspaceNames();
316 void Workspace::shutdown(void) {
317 while (!_windows.empty())
318 _windows[0]->restore();
321 static rectList calcSpace(const Rect &win, const rectList &spaces) {
323 rectList::const_iterator siter;
324 for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
325 if(win.Intersect(*siter)) {
326 //Check for space to the left of the window
327 if(win.x() > siter->x())
328 result.push_back(Rect(siter->x(), siter->y(),
329 win.x() - siter->x() - 1,
331 //Check for space above the window
332 if(win.y() > siter->y())
333 result.push_back(Rect(siter->x(), siter->y(),
335 win.y() - siter->y() - 1));
336 //Check for space to the right of the window
337 if((win.x()+win.w()) <
338 (siter->x()+siter->w()))
339 result.push_back(Rect(win.x() + win.w() + 1,
341 siter->x() + siter->w() -
342 win.x() - win.w() - 1,
344 //Check for space below the window
345 if((win.y()+win.h()) <
346 (siter->y()+siter->h()))
347 result.push_back(Rect(siter->x(),
348 win.y() + win.h() + 1,
350 siter->y() + siter->h()-
351 win.y() - win.h() - 1));
355 result.push_back(*siter);
360 bool rowRLBT(const Rect &first, const Rect &second){
361 if (first.y()+first.h()==second.y()+second.h())
362 return first.x()+first.w()>second.x()+second.w();
363 return first.y()+first.h()>second.y()+second.h();
366 bool rowRLTB(const Rect &first, const Rect &second){
367 if (first.y()==second.y())
368 return first.x()+first.w()>second.x()+second.w();
369 return first.y()<second.y();
372 bool rowLRBT(const Rect &first, const Rect &second){
373 if (first.y()+first.h()==second.y()+second.h())
374 return first.x()<second.x();
375 return first.y()+first.h()>second.y()+second.h();
378 bool rowLRTB(const Rect &first, const Rect &second){
379 if (first.y()==second.y())
380 return first.x()<second.x();
381 return first.y()<second.y();
384 bool colLRTB(const Rect &first, const Rect &second){
385 if (first.x()==second.x())
386 return first.y()<second.y();
387 return first.x()<second.x();
390 bool colLRBT(const Rect &first, const Rect &second){
391 if (first.x()==second.x())
392 return first.y()+first.h()>second.y()+second.h();
393 return first.x()<second.x();
396 bool colRLTB(const Rect &first, const Rect &second){
397 if (first.x()+first.w()==second.x()+second.w())
398 return first.y()<second.y();
399 return first.x()+first.w()>second.x()+second.w();
402 bool colRLBT(const Rect &first, const Rect &second){
403 if (first.x()+first.w()==second.x()+second.w())
404 return first.y()+first.h()>second.y()+second.h();
405 return first.x()+first.w()>second.x()+second.w();
409 //BestFitPlacement finds the smallest free space that fits the window
410 //to be placed. It currentl ignores whether placement is right to left or top
412 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
415 rectList::const_iterator siter;
416 spaces.push_back(space); //initially the entire screen is free
419 winVect::iterator it;
420 for (it = _windows.begin(); it != _windows.end(); ++it)
421 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
424 //Find first space that fits the window
426 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
427 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
430 else if(siter->w()*siter->h()<best->h()*best->w())
435 Point *pt = new Point(best->origin());
436 if (screen.colPlacementDirection() != BScreen::TopBottom)
437 pt->setY(pt->y() + (best->h() - win_size.h()));
438 if (screen.rowPlacementDirection() != BScreen::LeftRight)
439 pt->setX(pt->x() + (best->w() - win_size.w()));
442 return NULL; //fall back to cascade
445 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
451 XQueryPointer(screen.getOpenbox().getXDisplay(), screen.getRootWindow(),
452 &r, &c, &rx, &ry, &x, &y, &m);
453 pt = new Point(rx - win_size.w() / 2, ry - win_size.h() / 2);
455 if (pt->x() < space.x())
457 if (pt->y() < space.y())
459 if (pt->x() + win_size.w() > space.x() + space.w())
460 pt->setX(space.x() + space.w() - win_size.w());
461 if (pt->y() + win_size.h() > space.y() + space.h())
462 pt->setY(space.y() + space.h() - win_size.h());
466 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
470 rectList::const_iterator siter;
471 spaces.push_back(space); //initially the entire screen is free
474 winVect::iterator it;
475 for (it = _windows.begin(); it != _windows.end(); ++it)
476 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
478 //Sort spaces by preference
479 if(screen.rowPlacementDirection() == BScreen::RightLeft)
480 if(screen.colPlacementDirection() == BScreen::TopBottom)
481 sort(spaces.begin(),spaces.end(),rowRLTB);
483 sort(spaces.begin(),spaces.end(),rowRLBT);
485 if(screen.colPlacementDirection() == BScreen::TopBottom)
486 sort(spaces.begin(),spaces.end(),rowLRTB);
488 sort(spaces.begin(),spaces.end(),rowLRBT);
490 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
491 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
497 Point *pt = new Point(best->origin());
498 if (screen.colPlacementDirection() != BScreen::TopBottom)
499 pt->setY(best->y() + best->h() - win_size.h());
500 if (screen.rowPlacementDirection() != BScreen::LeftRight)
501 pt->setX(best->x()+best->w()-win_size.w());
504 return NULL; //fall back to cascade
507 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
511 rectList::const_iterator siter;
512 spaces.push_back(space); //initially the entire screen is free
515 winVect::iterator it;
516 for (it = _windows.begin(); it != _windows.end(); ++it)
517 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
519 //Sort spaces by user preference
520 if(screen.colPlacementDirection() == BScreen::TopBottom)
521 if(screen.rowPlacementDirection() == BScreen::LeftRight)
522 sort(spaces.begin(),spaces.end(),colLRTB);
524 sort(spaces.begin(),spaces.end(),colRLTB);
526 if(screen.rowPlacementDirection() == BScreen::LeftRight)
527 sort(spaces.begin(),spaces.end(),colLRBT);
529 sort(spaces.begin(),spaces.end(),colRLBT);
531 //Find first space that fits the window
533 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
534 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
540 Point *pt = new Point(best->origin());
541 if (screen.colPlacementDirection() != BScreen::TopBottom)
542 pt->setY(pt->y() + (best->h() - win_size.h()));
543 if (screen.rowPlacementDirection() != BScreen::LeftRight)
544 pt->setX(pt->x() + (best->w() - win_size.w()));
547 return NULL; //fall back to cascade
551 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
553 if ((cascade_x + win.area().w() + screen.getBorderWidth() * 2 >
554 (space.x() + space.w())) ||
555 (cascade_y + win.area().h() + screen.getBorderWidth() * 2 >
556 (space.y() + space.h())))
557 cascade_x = cascade_y = 0;
558 if (cascade_x < space.x() || cascade_y < space.y()) {
559 cascade_x = space.x();
560 cascade_y = space.y();
563 Point *p = new Point(cascade_x, cascade_y);
564 cascade_x += win.getTitleHeight();
565 cascade_y += win.getTitleHeight();
570 void Workspace::placeWindow(OpenboxWindow &win) {
571 Rect space = screen.availableArea();
572 const Size window_size(win.area().w()+screen.getBorderWidth() * 2,
573 win.area().h()+screen.getBorderWidth() * 2);
576 switch (screen.placementPolicy()) {
577 case BScreen::BestFitPlacement:
578 place = bestFitPlacement(window_size, space);
580 case BScreen::RowSmartPlacement:
581 place = rowSmartPlacement(window_size, space);
583 case BScreen::ColSmartPlacement:
584 place = colSmartPlacement(window_size, space);
586 case BScreen::UnderMousePlacement:
587 case BScreen::ClickMousePlacement:
588 place = underMousePlacement(window_size, space);
593 place = cascadePlacement(win, space);
595 ASSERT(place != NULL);
596 if (place->x() + window_size.w() > (signed) space.x() + space.w())
597 place->setX(((signed) space.x() + space.w() - window_size.w()) / 2);
598 if (place->y() + window_size.h() > (signed) space.y() + space.h())
599 place->setY(((signed) space.y() + space.h() - window_size.h()) / 2);
601 win.configure(place->x(), place->y(), win.area().w(), win.area().h());