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();
118 } else if (screen
.sloppyFocus()) {
119 screen
.getOpenbox().focusWindow((OpenboxWindow
*) 0);
121 if (_zorder
.empty() || !_zorder
.front()->setInputFocus()) {
122 screen
.getOpenbox().focusWindow((OpenboxWindow
*) 0);
123 XSetInputFocus(screen
.getOpenbox().getXDisplay(),
124 screen
.getToolbar()->getWindowID(),
125 RevertToParent
, CurrentTime
);
131 lastfocus
= (OpenboxWindow
*) 0;
133 _windows
.erase(_windows
.begin() + w
->getWindowNumber());
134 clientmenu
->remove(w
->getWindowNumber());
135 clientmenu
->update();
137 screen
.updateNetizenWindowDel(w
->getClientWindow());
139 winVect::iterator it
= _windows
.begin();
140 for (int i
=0; it
!= _windows
.end(); ++it
, ++i
)
141 (*it
)->setWindowNumber(i
);
143 return _windows
.size();
147 void Workspace::focusWindow(OpenboxWindow
*win
) {
148 if (win
!= (OpenboxWindow
*) 0)
149 clientmenu
->setItemSelected(win
->getWindowNumber(), true);
150 if (_focused
!= (OpenboxWindow
*) 0)
151 clientmenu
->setItemSelected(_focused
->getWindowNumber(), false);
156 void Workspace::showAll(void) {
157 winList::iterator it
;
158 for (it
= _zorder
.begin(); it
!= _zorder
.end(); ++it
)
159 (*it
)->deiconify(false, false);
163 void Workspace::hideAll(void) {
164 winList::reverse_iterator it
;
165 for (it
= _zorder
.rbegin(); it
!= _zorder
.rend(); ++it
)
166 if (!(*it
)->isStuck())
171 void Workspace::removeAll(void) {
172 winVect::iterator it
;
173 for (it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
178 void Workspace::raiseWindow(OpenboxWindow
*w
) {
179 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
181 while (bottom
->isTransient() && bottom
->getTransientFor())
182 bottom
= bottom
->getTransientFor();
186 while (win
->hasTransient() && win
->getTransient()) {
187 win
= win
->getTransient();
192 Window
*nstack
= new Window
[i
], *curr
= nstack
;
197 *(curr
++) = win
->getFrameWindow();
198 screen
.updateNetizenWindowRaise(win
->getClientWindow());
200 if (! win
->isIconic()) {
201 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
202 wkspc
->_zorder
.remove(win
);
203 wkspc
->_zorder
.push_front(win
);
206 if (! win
->hasTransient() || ! win
->getTransient())
209 win
= win
->getTransient();
212 screen
.raiseWindows(nstack
, i
);
218 void Workspace::lowerWindow(OpenboxWindow
*w
) {
219 OpenboxWindow
*win
= (OpenboxWindow
*) 0, *bottom
= w
;
221 while (bottom
->isTransient() && bottom
->getTransientFor())
222 bottom
= bottom
->getTransientFor();
226 while (win
->hasTransient() && win
->getTransient()) {
227 win
= win
->getTransient();
232 Window
*nstack
= new Window
[i
], *curr
= nstack
;
236 *(curr
++) = win
->getFrameWindow();
237 screen
.updateNetizenWindowLower(win
->getClientWindow());
239 if (! win
->isIconic()) {
240 wkspc
= screen
.getWorkspace(win
->getWorkspaceNumber());
241 wkspc
->_zorder
.remove(win
);
242 wkspc
->_zorder
.push_back(win
);
245 if (! win
->getTransientFor())
248 win
= win
->getTransientFor();
251 screen
.getOpenbox().grab();
253 XLowerWindow(screen
.getBaseDisplay().getXDisplay(), *nstack
);
254 XRestackWindows(screen
.getBaseDisplay().getXDisplay(), nstack
, i
);
256 screen
.getOpenbox().ungrab();
262 void Workspace::reconfigure(void) {
263 clientmenu
->reconfigure();
265 winVect::iterator it
;
266 for (it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
267 if ((*it
)->validateClient())
268 (*it
)->reconfigure();
272 OpenboxWindow
*Workspace::getWindow(int index
) {
273 if ((index
>= 0) && (index
< _windows
.size()))
274 return _windows
[index
];
276 return (OpenboxWindow
*) 0;
280 const int Workspace::getCount(void) {
281 return _windows
.size();
285 void Workspace::update(void) {
286 clientmenu
->update();
287 screen
.getToolbar()->redrawWindowLabel(True
);
291 Bool
Workspace::isCurrent(void) {
292 return (id
== screen
.getCurrentWorkspaceID());
296 void Workspace::setCurrent(void) {
297 screen
.changeWorkspaceID(id
);
301 void Workspace::setName(char *new_name
) {
306 name
= bstrdup(new_name
);
308 name
= new char[128];
309 sprintf(name
, i18n
->getMessage(WorkspaceSet
, WorkspaceDefaultNameFormat
,
310 "Workspace %d"), id
+ 1);
313 clientmenu
->setLabel(name
);
314 clientmenu
->update();
315 screen
.saveWorkspaceNames();
319 void Workspace::shutdown(void) {
320 while (!_windows
.empty()) {
321 _windows
[0]->restore();
322 _windows
.erase(_windows
.begin());
326 static rectList
calcSpace(const Rect
&win
, const rectList
&spaces
) {
328 rectList::const_iterator siter
;
329 for(siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
330 if(win
.Intersect(*siter
)) {
331 //Check for space to the left of the window
332 if(win
.x() > siter
->x())
333 result
.push_back(Rect(siter
->x(), siter
->y(),
334 win
.x() - siter
->x() - 1,
336 //Check for space above the window
337 if(win
.y() > siter
->y())
338 result
.push_back(Rect(siter
->x(), siter
->y(),
340 win
.y() - siter
->y() - 1));
341 //Check for space to the right of the window
342 if((win
.x()+win
.w()) <
343 (siter
->x()+siter
->w()))
344 result
.push_back(Rect(win
.x() + win
.w() + 1,
346 siter
->x() + siter
->w() -
347 win
.x() - win
.w() - 1,
349 //Check for space below the window
350 if((win
.y()+win
.h()) <
351 (siter
->y()+siter
->h()))
352 result
.push_back(Rect(siter
->x(),
353 win
.y() + win
.h() + 1,
355 siter
->y() + siter
->h()-
356 win
.y() - win
.h() - 1));
360 result
.push_back(*siter
);
365 bool rowRLBT(const Rect
&first
, const Rect
&second
){
366 if (first
.y()+first
.h()==second
.y()+second
.h())
367 return first
.x()+first
.w()>second
.x()+second
.w();
368 return first
.y()+first
.h()>second
.y()+second
.h();
371 bool rowRLTB(const Rect
&first
, const Rect
&second
){
372 if (first
.y()==second
.y())
373 return first
.x()+first
.w()>second
.x()+second
.w();
374 return first
.y()<second
.y();
377 bool rowLRBT(const Rect
&first
, const Rect
&second
){
378 if (first
.y()+first
.h()==second
.y()+second
.h())
379 return first
.x()<second
.x();
380 return first
.y()+first
.h()>second
.y()+second
.h();
383 bool rowLRTB(const Rect
&first
, const Rect
&second
){
384 if (first
.y()==second
.y())
385 return first
.x()<second
.x();
386 return first
.y()<second
.y();
389 bool colLRTB(const Rect
&first
, const Rect
&second
){
390 if (first
.x()==second
.x())
391 return first
.y()<second
.y();
392 return first
.x()<second
.x();
395 bool colLRBT(const Rect
&first
, const Rect
&second
){
396 if (first
.x()==second
.x())
397 return first
.y()+first
.h()>second
.y()+second
.h();
398 return first
.x()<second
.x();
401 bool colRLTB(const Rect
&first
, const Rect
&second
){
402 if (first
.x()+first
.w()==second
.x()+second
.w())
403 return first
.y()<second
.y();
404 return first
.x()+first
.w()>second
.x()+second
.w();
407 bool colRLBT(const Rect
&first
, const Rect
&second
){
408 if (first
.x()+first
.w()==second
.x()+second
.w())
409 return first
.y()+first
.h()>second
.y()+second
.h();
410 return first
.x()+first
.w()>second
.x()+second
.w();
414 //BestFitPlacement finds the smallest free space that fits the window
415 //to be placed. It currentl ignores whether placement is right to left or top
417 Point
*Workspace::bestFitPlacement(const Size
&win_size
, const Rect
&space
) {
420 rectList::const_iterator siter
;
421 spaces
.push_back(space
); //initially the entire screen is free
424 for (winVect::iterator it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
425 spaces
= calcSpace((*it
)->area().Inflate(screen
.getBorderWidth() * 4),
428 //Find first space that fits the window
430 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
) {
431 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
434 else if(siter
->w()*siter
->h()<best
->h()*best
->w())
439 Point
*pt
= new Point(best
->origin());
440 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
441 pt
->setY(pt
->y() + (best
->h() - win_size
.h()));
442 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
443 pt
->setX(pt
->x() + (best
->w() - win_size
.w()));
446 return NULL
; //fall back to cascade
449 Point
*Workspace::underMousePlacement(const Size
&win_size
, const Rect
&space
) {
455 XQueryPointer(screen
.getOpenbox().getXDisplay(), screen
.getRootWindow(),
456 &r
, &c
, &rx
, &ry
, &x
, &y
, &m
);
457 pt
= new Point(rx
- win_size
.w() / 2, ry
- win_size
.h() / 2);
459 if (pt
->x() < space
.x())
461 if (pt
->y() < space
.y())
463 if (pt
->x() + win_size
.w() > space
.x() + space
.w())
464 pt
->setX(space
.x() + space
.w() - win_size
.w());
465 if (pt
->y() + win_size
.h() > space
.y() + space
.h())
466 pt
->setY(space
.y() + space
.h() - win_size
.h());
470 Point
*Workspace::rowSmartPlacement(const Size
&win_size
, const Rect
&space
) {
474 rectList::const_iterator siter
;
475 spaces
.push_back(space
); //initially the entire screen is free
478 for (winVect::iterator it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
479 spaces
= calcSpace((*it
)->area().Inflate(screen
.getBorderWidth() * 4),
481 //Sort spaces by preference
482 if(screen
.rowPlacementDirection() == BScreen::RightLeft
)
483 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
484 sort(spaces
.begin(),spaces
.end(),rowRLTB
);
486 sort(spaces
.begin(),spaces
.end(),rowRLBT
);
488 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
489 sort(spaces
.begin(),spaces
.end(),rowLRTB
);
491 sort(spaces
.begin(),spaces
.end(),rowLRBT
);
493 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
494 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
500 Point
*pt
= new Point(best
->origin());
501 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
502 pt
->setY(best
->y() + best
->h() - win_size
.h());
503 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
504 pt
->setX(best
->x()+best
->w()-win_size
.w());
507 return NULL
; //fall back to cascade
510 Point
*Workspace::colSmartPlacement(const Size
&win_size
, const Rect
&space
) {
514 rectList::const_iterator siter
;
515 spaces
.push_back(space
); //initially the entire screen is free
518 for (winVect::iterator it
= _windows
.begin(); it
!= _windows
.end(); ++it
)
519 spaces
= calcSpace((*it
)->area().Inflate(screen
.getBorderWidth() * 4),
521 //Sort spaces by user preference
522 if(screen
.colPlacementDirection() == BScreen::TopBottom
)
523 if(screen
.rowPlacementDirection() == BScreen::LeftRight
)
524 sort(spaces
.begin(),spaces
.end(),colLRTB
);
526 sort(spaces
.begin(),spaces
.end(),colRLTB
);
528 if(screen
.rowPlacementDirection() == BScreen::LeftRight
)
529 sort(spaces
.begin(),spaces
.end(),colLRBT
);
531 sort(spaces
.begin(),spaces
.end(),colRLBT
);
533 //Find first space that fits the window
535 for (siter
=spaces
.begin(); siter
!=spaces
.end(); ++siter
)
536 if ((siter
->w() >= win_size
.w()) && (siter
->h() >= win_size
.h())) {
542 Point
*pt
= new Point(best
->origin());
543 if (screen
.colPlacementDirection() != BScreen::TopBottom
)
544 pt
->setY(pt
->y() + (best
->h() - win_size
.h()));
545 if (screen
.rowPlacementDirection() != BScreen::LeftRight
)
546 pt
->setX(pt
->x() + (best
->w() - win_size
.w()));
549 return NULL
; //fall back to cascade
553 Point
*const Workspace::cascadePlacement(const OpenboxWindow
&win
,
555 if ((cascade_x
+ win
.area().w() + screen
.getBorderWidth() * 2 >
556 (space
.x() + space
.w())) ||
557 (cascade_y
+ win
.area().h() + screen
.getBorderWidth() * 2 >
558 (space
.y() + space
.h())))
559 cascade_x
= cascade_y
= 0;
560 if (cascade_x
< space
.x() || cascade_y
< space
.y()) {
561 cascade_x
= space
.x();
562 cascade_y
= space
.y();
565 Point
*p
= new Point(cascade_x
, cascade_y
);
566 cascade_x
+= win
.getTitleHeight();
567 cascade_y
+= win
.getTitleHeight();
572 void Workspace::placeWindow(OpenboxWindow
&win
) {
573 Rect space
= screen
.availableArea();
574 const Size
window_size(win
.area().w()+screen
.getBorderWidth() * 2,
575 win
.area().h()+screen
.getBorderWidth() * 2);
578 switch (screen
.placementPolicy()) {
579 case BScreen::BestFitPlacement
:
580 place
= bestFitPlacement(window_size
, space
);
582 case BScreen::RowSmartPlacement
:
583 place
= rowSmartPlacement(window_size
, space
);
585 case BScreen::ColSmartPlacement
:
586 place
= colSmartPlacement(window_size
, space
);
588 case BScreen::UnderMousePlacement
:
589 case BScreen::ClickMousePlacement
:
590 place
= underMousePlacement(window_size
, space
);
595 place
= cascadePlacement(win
, space
);
597 ASSERT(place
!= NULL
);
598 if (place
->x() + window_size
.w() > (signed) space
.x() + space
.w())
599 place
->setX(((signed) space
.x() + space
.w() - window_size
.w()) / 2);
600 if (place
->y() + window_size
.h() > (signed) space
.y() + space
.h())
601 place
->setY(((signed) space
.y() + space
.h() - window_size
.h()) / 2);
603 win
.configure(place
->x(), place
->y(), win
.area().w(), win
.area().h());