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 char *tmp
= screen
.getNameOfWorkspace(id
);
79 Workspace::~Workspace(void) {
87 const int Workspace::addWindow(OpenboxWindow
*w
, Bool place
) {
90 if (place
) placeWindow(*w
);
93 w
->setWindowNumber(_windows
.size());
95 _zorder
.push_front(w
);
96 _windows
.push_back(w
);
98 clientmenu
->insert((const char **) w
->getTitle());
101 screen
.updateNetizenWindowAdd(w
->getClientWindow(), id
);
105 return w
->getWindowNumber();
109 const int Workspace::removeWindow(OpenboxWindow
*w
) {
114 if (w
->isFocused()) {
115 if (w
->isTransient() && w
->getTransientFor() &&
116 w
->getTransientFor()->isVisible()) {
117 w
->getTransientFor()->setInputFocus();
119 if (screen
.sloppyFocus() || // sloppy focus
120 _zorder
.empty() || // click focus but no windows
121 !_zorder
.front()->setInputFocus()) { // tried window, but wont focus
122 screen
.getOpenbox().focusWindow((OpenboxWindow
*) 0);
123 XSetInputFocus(screen
.getOpenbox().getXDisplay(),
124 PointerRoot
, None
, CurrentTime
);
130 lastfocus
= (OpenboxWindow
*) 0;
132 _windows
.erase(_windows
.begin() + w
->getWindowNumber());
133 clientmenu
->remove(w
->getWindowNumber());
134 clientmenu
->update();
136 screen
.updateNetizenWindowDel(w
->getClientWindow());
138 winVect::iterator it
= _windows
.begin();
139 for (int i
=0; it
!= _windows
.end(); ++it
, ++i
)
140 (*it
)->setWindowNumber(i
);
142 return _windows
.size();
146 void Workspace::focusWindow(OpenboxWindow
*win
) {
147 if (win
!= (OpenboxWindow
*) 0)
148 clientmenu
->setItemSelected(win
->getWindowNumber(), true);
149 if (_focused
!= (OpenboxWindow
*) 0)
150 clientmenu
->setItemSelected(_focused
->getWindowNumber(), false);
155 void Workspace::showAll(void) {
156 winList::iterator it
;
157 for (it
= _zorder
.begin(); it
!= _zorder
.end(); ++it
)
158 (*it
)->deiconify(false, false);
162 void Workspace::hideAll(void) {
163 winList::reverse_iterator it
;
164 for (it
= _zorder
.rbegin(); it
!= _zorder
.rend(); ++it
)
165 if (!(*it
)->isStuck())
170 void Workspace::removeAll(void) {
171 winVect::iterator it
;
172 for (it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
177 void Workspace::raiseWindow(OpenboxWindow
*w
) {
178 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
180 while (bottom
->isTransient() && bottom
->getTransientFor())
181 bottom
= bottom
->getTransientFor();
185 while (win
->hasTransient() && win
->getTransient()) {
186 win
= win
->getTransient();
191 Window
*nstack
= new Window
[i
], *curr
= nstack
;
196 *(curr
++) = win
->getFrameWindow();
197 screen
.updateNetizenWindowRaise(win
->getClientWindow());
199 if (! win
->isIconic()) {
200 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
201 wkspc
->_zorder
.remove(win
);
202 wkspc
->_zorder
.push_front(win
);
205 if (! win
->hasTransient() || ! win
->getTransient())
208 win
= win
->getTransient();
211 screen
.raiseWindows(nstack
, i
);
217 void Workspace::lowerWindow(OpenboxWindow
*w
) {
218 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
220 while (bottom
->isTransient() && bottom
->getTransientFor())
221 bottom
= bottom
->getTransientFor();
225 while (win
->hasTransient() && win
->getTransient()) {
226 win
= win
->getTransient();
231 Window
*nstack
= new Window
[i
], *curr
= nstack
;
235 *(curr
++) = win
->getFrameWindow();
236 screen
.updateNetizenWindowLower(win
->getClientWindow());
238 if (! win
->isIconic()) {
239 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
240 wkspc
->_zorder
.remove(win
);
241 wkspc
->_zorder
.push_back(win
);
244 if (! win
->getTransientFor())
247 win
= win
->getTransientFor();
250 screen
.getOpenbox().grab();
252 XLowerWindow(screen
.getBaseDisplay().getXDisplay(), *nstack
);
253 XRestackWindows(screen
.getBaseDisplay().getXDisplay(), nstack
, i
);
255 screen
.getOpenbox().ungrab();
261 void Workspace::reconfigure(void) {
262 clientmenu
->reconfigure();
264 winVect::iterator it
;
265 for (it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
266 if ((*it
)->validateClient())
267 (*it
)->reconfigure();
271 OpenboxWindow
*Workspace::getWindow(int index
) {
272 if ((index
>= 0) && (index
< _windows
.size()))
273 return _windows
[index
];
275 return (OpenboxWindow
*) 0;
279 const int Workspace::getCount(void) {
280 return _windows
.size();
284 void Workspace::update(void) {
285 clientmenu
->update();
286 screen
.getToolbar()->redrawWindowLabel(True
);
290 Bool
Workspace::isCurrent(void) {
291 return (id
== screen
.getCurrentWorkspaceID());
295 void Workspace::setCurrent(void) {
296 screen
.changeWorkspaceID(id
);
300 void Workspace::setName(char *new_name
) {
305 name
= bstrdup(new_name
);
307 name
= new char[128];
308 sprintf(name
, i18n
->getMessage(WorkspaceSet
, WorkspaceDefaultNameFormat
,
309 "Workspace %d"), id
+ 1);
312 clientmenu
->setLabel(name
);
313 clientmenu
->update();
314 screen
.saveWorkspaceNames();
318 void Workspace::shutdown(void) {
319 while (!_windows
.empty()) {
320 _windows
[0]->restore();
325 static rectList
calcSpace(const Rect
&win
, const rectList
&spaces
) {
327 rectList::const_iterator siter
;
328 for(siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
329 if(win
.Intersect(*siter
)) {
330 //Check for space to the left of the window
331 if(win
.x() > siter
->x())
332 result
.push_back(Rect(siter
->x(), siter
->y(),
333 win
.x() - siter
->x() - 1,
335 //Check for space above the window
336 if(win
.y() > siter
->y())
337 result
.push_back(Rect(siter
->x(), siter
->y(),
339 win
.y() - siter
->y() - 1));
340 //Check for space to the right of the window
341 if((win
.x()+win
.w()) <
342 (siter
->x()+siter
->w()))
343 result
.push_back(Rect(win
.x() + win
.w() + 1,
345 siter
->x() + siter
->w() -
346 win
.x() - win
.w() - 1,
348 //Check for space below the window
349 if((win
.y()+win
.h()) <
350 (siter
->y()+siter
->h()))
351 result
.push_back(Rect(siter
->x(),
352 win
.y() + win
.h() + 1,
354 siter
->y() + siter
->h()-
355 win
.y() - win
.h() - 1));
359 result
.push_back(*siter
);
364 bool rowRLBT(const Rect
&first
, const Rect
&second
){
365 if (first
.y()+first
.h()==second
.y()+second
.h())
366 return first
.x()+first
.w()>second
.x()+second
.w();
367 return first
.y()+first
.h()>second
.y()+second
.h();
370 bool rowRLTB(const Rect
&first
, const Rect
&second
){
371 if (first
.y()==second
.y())
372 return first
.x()+first
.w()>second
.x()+second
.w();
373 return first
.y()<second
.y();
376 bool rowLRBT(const Rect
&first
, const Rect
&second
){
377 if (first
.y()+first
.h()==second
.y()+second
.h())
378 return first
.x()<second
.x();
379 return first
.y()+first
.h()>second
.y()+second
.h();
382 bool rowLRTB(const Rect
&first
, const Rect
&second
){
383 if (first
.y()==second
.y())
384 return first
.x()<second
.x();
385 return first
.y()<second
.y();
388 bool colLRTB(const Rect
&first
, const Rect
&second
){
389 if (first
.x()==second
.x())
390 return first
.y()<second
.y();
391 return first
.x()<second
.x();
394 bool colLRBT(const Rect
&first
, const Rect
&second
){
395 if (first
.x()==second
.x())
396 return first
.y()+first
.h()>second
.y()+second
.h();
397 return first
.x()<second
.x();
400 bool colRLTB(const Rect
&first
, const Rect
&second
){
401 if (first
.x()+first
.w()==second
.x()+second
.w())
402 return first
.y()<second
.y();
403 return first
.x()+first
.w()>second
.x()+second
.w();
406 bool colRLBT(const Rect
&first
, const Rect
&second
){
407 if (first
.x()+first
.w()==second
.x()+second
.w())
408 return first
.y()+first
.h()>second
.y()+second
.h();
409 return first
.x()+first
.w()>second
.x()+second
.w();
413 //BestFitPlacement finds the smallest free space that fits the window
414 //to be placed. It currentl ignores whether placement is right to left or top
416 Point
*Workspace::bestFitPlacement(const Size
&win_size
, const Rect
&space
) {
419 rectList::const_iterator siter
;
420 spaces
.push_back(space
); //initially the entire screen is free
423 for (winVect::iterator it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
424 spaces
= calcSpace((*it
)->area().Inflate(screen
.getBorderWidth() * 4),
427 //Find first space that fits the window
429 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
430 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
433 else if(siter
->w()*siter
->h()<best
->h()*best
->w())
438 Point
*pt
= new Point(best
->origin());
439 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
440 pt
->setY(pt
->y() + (best
->h() - win_size
.h()));
441 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
442 pt
->setX(pt
->x() + (best
->w() - win_size
.w()));
445 return NULL
; //fall back to cascade
448 Point
*Workspace::underMousePlacement(const Size
&win_size
, const Rect
&space
) {
454 XQueryPointer(screen
.getOpenbox().getXDisplay(), screen
.getRootWindow(),
455 &r
, &c
, &rx
, &ry
, &x
, &y
, &m
);
456 pt
= new Point(rx
- win_size
.w() / 2, ry
- win_size
.h() / 2);
458 if (pt
->x() < space
.x())
460 if (pt
->y() < space
.y())
462 if (pt
->x() + win_size
.w() > space
.x() + space
.w())
463 pt
->setX(space
.x() + space
.w() - win_size
.w());
464 if (pt
->y() + win_size
.h() > space
.y() + space
.h())
465 pt
->setY(space
.y() + space
.h() - win_size
.h());
469 Point
*Workspace::rowSmartPlacement(const Size
&win_size
, const Rect
&space
) {
473 rectList::const_iterator siter
;
474 spaces
.push_back(space
); //initially the entire screen is free
477 for (winVect::iterator it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
478 spaces
= calcSpace((*it
)->area().Inflate(screen
.getBorderWidth() * 4),
480 //Sort spaces by preference
481 if(screen
.rowPlacementDirection() == BScreen::RightLeft
)
482 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
483 sort(spaces
.begin(),spaces
.end(),rowRLTB
);
485 sort(spaces
.begin(),spaces
.end(),rowRLBT
);
487 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
488 sort(spaces
.begin(),spaces
.end(),rowLRTB
);
490 sort(spaces
.begin(),spaces
.end(),rowLRBT
);
492 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
493 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
499 Point
*pt
= new Point(best
->origin());
500 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
501 pt
->setY(best
->y() + best
->h() - win_size
.h());
502 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
503 pt
->setX(best
->x()+best
->w()-win_size
.w());
506 return NULL
; //fall back to cascade
509 Point
*Workspace::colSmartPlacement(const Size
&win_size
, const Rect
&space
) {
513 rectList::const_iterator siter
;
514 spaces
.push_back(space
); //initially the entire screen is free
517 for (winVect::iterator 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());