]> Dogcows Code - chaz/openbox/blob - src/Workspace.cc
256ad3da9845c18c6f1b8f3b7f7a7d53eb14a32b
[chaz/openbox] / src / Workspace.cc
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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 // stupid macros needed to access some functions in version 2 of the GNU C
25 // library
26 #ifndef _GNU_SOURCE
27 #define _GNU_SOURCE
28 #endif // _GNU_SOURCE
29
30 #ifdef HAVE_CONFIG_H
31 # include "../config.h"
32 #endif // HAVE_CONFIG_H
33
34 #ifdef HAVE_STDIO_H
35 # include <stdio.h>
36 #endif // HAVE_STDIO_H
37
38 #ifdef HAVE_STDLIB_H
39 # include <stdlib.h>
40 #endif // HAVE_STDLIB_H
41
42 #ifdef HAVE_STRING_H
43 # include <string.h>
44 #endif // HAVE_STRING_H
45
46 #include <X11/Xlib.h>
47 #include <X11/Xatom.h>
48
49 #include "i18n.h"
50 #include "openbox.h"
51 #include "Clientmenu.h"
52 #include "Screen.h"
53 #include "Toolbar.h"
54 #include "Window.h"
55 #include "Workspace.h"
56 #include "Windowmenu.h"
57 #include "Geometry.h"
58 #include "Util.h"
59
60 #include <algorithm>
61 #include <vector>
62 typedef std::vector<Rect> rectList;
63
64 Workspace::Workspace(BScreen &scrn, int i) : screen(scrn) {
65 cascade_x = cascade_y = 0;
66 _focused = (OpenboxWindow *) 0;
67 id = i;
68
69 clientmenu = new Clientmenu(*this);
70
71 lastfocus = (OpenboxWindow *) 0;
72
73 name = (char *) 0;
74 char *tmp = screen.getNameOfWorkspace(id);
75 setName(tmp);
76 }
77
78
79 Workspace::~Workspace(void) {
80 delete clientmenu;
81
82 if (name)
83 delete [] name;
84 }
85
86
87 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
88 if (! w) return -1;
89
90 if (place) placeWindow(*w);
91
92 w->setWorkspace(id);
93 w->setWindowNumber(_windows.size());
94
95 _zorder.push_front(w);
96 _windows.push_back(w);
97
98 clientmenu->insert((const char **) w->getTitle());
99 clientmenu->update();
100
101 screen.updateNetizenWindowAdd(w->getClientWindow(), id);
102
103 raiseWindow(w);
104
105 return w->getWindowNumber();
106 }
107
108
109 const int Workspace::removeWindow(OpenboxWindow *w) {
110 if (! w) return -1;
111
112 _zorder.remove(w);
113
114 if (w->isFocused()) {
115 if (w->isTransient() && w->getTransientFor() &&
116 w->getTransientFor()->isVisible()) {
117 w->getTransientFor()->setInputFocus();
118 } else {
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);
125 }
126 }
127 }
128
129 if (lastfocus == w)
130 lastfocus = (OpenboxWindow *) 0;
131
132 _windows.erase(_windows.begin() + w->getWindowNumber());
133 clientmenu->remove(w->getWindowNumber());
134 clientmenu->update();
135
136 screen.updateNetizenWindowDel(w->getClientWindow());
137
138 winVect::iterator it = _windows.begin();
139 for (int i=0; it != _windows.end(); ++it, ++i)
140 (*it)->setWindowNumber(i);
141
142 return _windows.size();
143 }
144
145
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);
151 _focused = win;
152 }
153
154
155 void Workspace::showAll(void) {
156 winList::iterator it;
157 for (it = _zorder.begin(); it != _zorder.end(); ++it)
158 (*it)->deiconify(false, false);
159 }
160
161
162 void Workspace::hideAll(void) {
163 winList::reverse_iterator it;
164 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
165 if (!(*it)->isStuck())
166 (*it)->withdraw();
167 }
168
169
170 void Workspace::removeAll(void) {
171 winVect::iterator it;
172 for (it = _windows.begin(); it != _windows.end(); ++it)
173 (*it)->iconify();
174 }
175
176
177 void Workspace::raiseWindow(OpenboxWindow *w) {
178 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
179
180 while (bottom->isTransient() && bottom->getTransientFor())
181 bottom = bottom->getTransientFor();
182
183 int i = 1;
184 win = bottom;
185 while (win->hasTransient() && win->getTransient()) {
186 win = win->getTransient();
187
188 i++;
189 }
190
191 Window *nstack = new Window[i], *curr = nstack;
192 Workspace *wkspc;
193
194 win = bottom;
195 while (True) {
196 *(curr++) = win->getFrameWindow();
197 screen.updateNetizenWindowRaise(win->getClientWindow());
198
199 if (! win->isIconic()) {
200 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
201 wkspc->_zorder.remove(win);
202 wkspc->_zorder.push_front(win);
203 }
204
205 if (! win->hasTransient() || ! win->getTransient())
206 break;
207
208 win = win->getTransient();
209 }
210
211 screen.raiseWindows(nstack, i);
212
213 delete [] nstack;
214 }
215
216
217 void Workspace::lowerWindow(OpenboxWindow *w) {
218 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
219
220 while (bottom->isTransient() && bottom->getTransientFor())
221 bottom = bottom->getTransientFor();
222
223 int i = 1;
224 win = bottom;
225 while (win->hasTransient() && win->getTransient()) {
226 win = win->getTransient();
227
228 i++;
229 }
230
231 Window *nstack = new Window[i], *curr = nstack;
232 Workspace *wkspc;
233
234 while (True) {
235 *(curr++) = win->getFrameWindow();
236 screen.updateNetizenWindowLower(win->getClientWindow());
237
238 if (! win->isIconic()) {
239 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
240 wkspc->_zorder.remove(win);
241 wkspc->_zorder.push_back(win);
242 }
243
244 if (! win->getTransientFor())
245 break;
246
247 win = win->getTransientFor();
248 }
249
250 screen.getOpenbox().grab();
251
252 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
253 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
254
255 screen.getOpenbox().ungrab();
256
257 delete [] nstack;
258 }
259
260
261 void Workspace::reconfigure(void) {
262 clientmenu->reconfigure();
263
264 winVect::iterator it;
265 for (it = _windows.begin(); it != _windows.end(); ++it)
266 if ((*it)->validateClient())
267 (*it)->reconfigure();
268 }
269
270
271 OpenboxWindow *Workspace::getWindow(int index) {
272 if ((index >= 0) && (index < _windows.size()))
273 return _windows[index];
274 else
275 return (OpenboxWindow *) 0;
276 }
277
278
279 const int Workspace::getCount(void) {
280 return _windows.size();
281 }
282
283
284 void Workspace::update(void) {
285 clientmenu->update();
286 screen.getToolbar()->redrawWindowLabel(True);
287 }
288
289
290 Bool Workspace::isCurrent(void) {
291 return (id == screen.getCurrentWorkspaceID());
292 }
293
294
295 void Workspace::setCurrent(void) {
296 screen.changeWorkspaceID(id);
297 }
298
299
300 void Workspace::setName(char *new_name) {
301 if (name)
302 delete [] name;
303
304 if (new_name) {
305 name = bstrdup(new_name);
306 } else {
307 name = new char[128];
308 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
309 "Workspace %d"), id + 1);
310 }
311
312 clientmenu->setLabel(name);
313 clientmenu->update();
314 screen.saveWorkspaceNames();
315 }
316
317
318 void Workspace::shutdown(void) {
319 while (!_windows.empty()) {
320 _windows[0]->restore();
321 delete _windows[0];
322 }
323 }
324
325 static rectList calcSpace(const Rect &win, const rectList &spaces) {
326 rectList result;
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,
334 siter->h()));
335 //Check for space above the window
336 if(win.y() > siter->y())
337 result.push_back(Rect(siter->x(), siter->y(),
338 siter->w(),
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,
344 siter->y(),
345 siter->x() + siter->w() -
346 win.x() - win.w() - 1,
347 siter->h()));
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,
353 siter->w(),
354 siter->y() + siter->h()-
355 win.y() - win.h() - 1));
356
357 }
358 else
359 result.push_back(*siter);
360 }
361 return result;
362 }
363
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();
368 }
369
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();
374 }
375
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();
380 }
381
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();
386 }
387
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();
392 }
393
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();
398 }
399
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();
404 }
405
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();
410 }
411
412
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
415 //to bottom.
416 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
417 const Rect *best;
418 rectList spaces;
419 rectList::const_iterator siter;
420 spaces.push_back(space); //initially the entire screen is free
421
422 //Find Free Spaces
423 for (winVect::iterator it = _windows.begin(); it != _windows.end(); ++it)
424 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
425 spaces);
426
427 //Find first space that fits the window
428 best = NULL;
429 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
430 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
431 if (best==NULL)
432 best = &*siter;
433 else if(siter->w()*siter->h()<best->h()*best->w())
434 best = &*siter;
435 }
436 }
437 if (best != NULL) {
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()));
443 return pt;
444 } else
445 return NULL; //fall back to cascade
446 }
447
448 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
449 Point *pt;
450
451 int x, y, rx, ry;
452 Window c, r;
453 unsigned int m;
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);
457
458 if (pt->x() < space.x())
459 pt->setX(space.x());
460 if (pt->y() < space.y())
461 pt->setY(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());
466 return pt;
467 }
468
469 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
470 const Rect *best;
471 rectList spaces;
472
473 rectList::const_iterator siter;
474 spaces.push_back(space); //initially the entire screen is free
475
476 //Find Free Spaces
477 for (winVect::iterator it = _windows.begin(); it != _windows.end(); ++it)
478 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
479 spaces);
480 //Sort spaces by preference
481 if(screen.rowPlacementDirection() == BScreen::RightLeft)
482 if(screen.colPlacementDirection() == BScreen::TopBottom)
483 sort(spaces.begin(),spaces.end(),rowRLTB);
484 else
485 sort(spaces.begin(),spaces.end(),rowRLBT);
486 else
487 if(screen.colPlacementDirection() == BScreen::TopBottom)
488 sort(spaces.begin(),spaces.end(),rowLRTB);
489 else
490 sort(spaces.begin(),spaces.end(),rowLRBT);
491 best = NULL;
492 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
493 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
494 best = &*siter;
495 break;
496 }
497
498 if (best != NULL) {
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());
504 return pt;
505 } else
506 return NULL; //fall back to cascade
507 }
508
509 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
510 const Rect *best;
511 rectList spaces;
512
513 rectList::const_iterator siter;
514 spaces.push_back(space); //initially the entire screen is free
515
516 //Find Free Spaces
517 for (winVect::iterator it = _windows.begin(); it != _windows.end(); ++it)
518 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
519 spaces);
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);
524 else
525 sort(spaces.begin(),spaces.end(),colRLTB);
526 else
527 if(screen.rowPlacementDirection() == BScreen::LeftRight)
528 sort(spaces.begin(),spaces.end(),colLRBT);
529 else
530 sort(spaces.begin(),spaces.end(),colRLBT);
531
532 //Find first space that fits the window
533 best = NULL;
534 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
535 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
536 best = &*siter;
537 break;
538 }
539
540 if (best != NULL) {
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()));
546 return pt;
547 } else
548 return NULL; //fall back to cascade
549 }
550
551
552 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
553 const Rect &space) {
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();
562 }
563
564 Point *p = new Point(cascade_x, cascade_y);
565 cascade_x += win.getTitleHeight();
566 cascade_y += win.getTitleHeight();
567 return p;
568 }
569
570
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);
575 Point *place = NULL;
576
577 switch (screen.placementPolicy()) {
578 case BScreen::BestFitPlacement:
579 place = bestFitPlacement(window_size, space);
580 break;
581 case BScreen::RowSmartPlacement:
582 place = rowSmartPlacement(window_size, space);
583 break;
584 case BScreen::ColSmartPlacement:
585 place = colSmartPlacement(window_size, space);
586 break;
587 case BScreen::UnderMousePlacement:
588 case BScreen::ClickMousePlacement:
589 place = underMousePlacement(window_size, space);
590 break;
591 } // switch
592
593 if (place == NULL)
594 place = cascadePlacement(win, space);
595
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);
601
602 win.configure(place->x(), place->y(), win.area().w(), win.area().h());
603 delete place;
604 }
This page took 0.059944 seconds and 4 git commands to generate.