]> Dogcows Code - chaz/openbox/blob - src/Workspace.cc
*** empty log message ***
[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 = _last = (OpenboxWindow *) 0;
67 id = i;
68
69 clientmenu = new Clientmenu(*this);
70
71 name = (char *) 0;
72 setName(screen.getNameOfWorkspace(id));
73 }
74
75
76 Workspace::~Workspace(void) {
77 delete clientmenu;
78
79 if (name)
80 delete [] name;
81 }
82
83
84 int Workspace::addWindow(OpenboxWindow *w, bool place) {
85 if (! w) return -1;
86
87 if (place) placeWindow(*w);
88
89 w->setWorkspace(id);
90 w->setWindowNumber(_windows.size());
91
92 _zorder.push_front(w);
93 _windows.push_back(w);
94
95 clientmenu->insert((const char **) w->getTitle());
96 clientmenu->update();
97
98 screen.updateNetizenWindowAdd(w->getClientWindow(), id);
99
100 raiseWindow(w);
101
102 return w->getWindowNumber();
103 }
104
105
106 int Workspace::removeWindow(OpenboxWindow *w) {
107 if (! w) return -1;
108
109 _zorder.remove(w);
110
111 if (w->isFocused()) {
112 if (w == _last)
113 _last = (OpenboxWindow *) 0;
114
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();
123
124 if (!(fw != (OpenboxWindow *) 0 && fw->setInputFocus()))
125 screen.getOpenbox().focusWindow(0);
126 }
127
128 _windows.erase(_windows.begin() + w->getWindowNumber());
129 clientmenu->remove(w->getWindowNumber());
130 clientmenu->update();
131
132 screen.updateNetizenWindowDel(w->getClientWindow());
133
134 winVect::iterator it = _windows.begin();
135 for (int i=0; it != _windows.end(); ++it, ++i)
136 (*it)->setWindowNumber(i);
137
138 return _windows.size();
139 }
140
141
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);
147 _focused = win;
148 if (win != (OpenboxWindow *) 0)
149 _last = win;
150 }
151
152
153 void Workspace::showAll(void) {
154 winList::iterator it;
155 for (it = _zorder.begin(); it != _zorder.end(); ++it)
156 (*it)->deiconify(false, false);
157 }
158
159
160 void Workspace::hideAll(void) {
161 winList::reverse_iterator it;
162 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
163 if (!(*it)->isStuck())
164 (*it)->withdraw();
165 }
166
167
168 void Workspace::removeAll(void) {
169 winVect::iterator it;
170 for (it = _windows.begin(); it != _windows.end(); ++it)
171 (*it)->iconify();
172 }
173
174
175 void Workspace::raiseWindow(OpenboxWindow *w) {
176 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
177
178 while (bottom->isTransient() && bottom->getTransientFor())
179 bottom = bottom->getTransientFor();
180
181 int i = 1;
182 win = bottom;
183 while (win->hasTransient() && win->getTransient()) {
184 win = win->getTransient();
185
186 i++;
187 }
188
189 Window *nstack = new Window[i], *curr = nstack;
190 Workspace *wkspc;
191
192 win = bottom;
193 while (true) {
194 *(curr++) = win->getFrameWindow();
195 screen.updateNetizenWindowRaise(win->getClientWindow());
196
197 if (! win->isIconic()) {
198 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
199 wkspc->_zorder.remove(win);
200 wkspc->_zorder.push_front(win);
201 }
202
203 if (! win->hasTransient() || ! win->getTransient())
204 break;
205
206 win = win->getTransient();
207 }
208
209 screen.raiseWindows(nstack, i);
210
211 delete [] nstack;
212 }
213
214
215 void Workspace::lowerWindow(OpenboxWindow *w) {
216 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
217
218 while (bottom->isTransient() && bottom->getTransientFor())
219 bottom = bottom->getTransientFor();
220
221 int i = 1;
222 win = bottom;
223 while (win->hasTransient() && win->getTransient()) {
224 win = win->getTransient();
225
226 i++;
227 }
228
229 Window *nstack = new Window[i], *curr = nstack;
230 Workspace *wkspc;
231
232 while (true) {
233 *(curr++) = win->getFrameWindow();
234 screen.updateNetizenWindowLower(win->getClientWindow());
235
236 if (! win->isIconic()) {
237 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
238 wkspc->_zorder.remove(win);
239 wkspc->_zorder.push_back(win);
240 }
241
242 if (! win->getTransientFor())
243 break;
244
245 win = win->getTransientFor();
246 }
247
248 screen.getOpenbox().grab();
249
250 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
251 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
252
253 screen.getOpenbox().ungrab();
254
255 delete [] nstack;
256 }
257
258
259 void Workspace::reconfigure(void) {
260 clientmenu->reconfigure();
261
262 winVect::iterator it;
263 for (it = _windows.begin(); it != _windows.end(); ++it)
264 if ((*it)->validateClient())
265 (*it)->reconfigure();
266 }
267
268
269 OpenboxWindow *Workspace::getWindow(int index) {
270 if ((index >= 0) && (index < (signed)_windows.size()))
271 return _windows[index];
272 else
273 return (OpenboxWindow *) 0;
274 }
275
276
277 int Workspace::getCount(void) {
278 return (signed)_windows.size();
279 }
280
281
282 void Workspace::update(void) {
283 clientmenu->update();
284 screen.getToolbar()->redrawWindowLabel(true);
285 }
286
287
288 bool Workspace::isCurrent(void) {
289 return (id == screen.getCurrentWorkspaceID());
290 }
291
292
293 void Workspace::setCurrent(void) {
294 screen.changeWorkspaceID(id);
295 }
296
297
298 void Workspace::setName(const char *new_name) {
299 if (name)
300 delete [] name;
301
302 if (new_name) {
303 name = bstrdup(new_name);
304 } else {
305 name = new char[128];
306 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
307 "Workspace %d"), id + 1);
308 }
309
310 clientmenu->setLabel(name);
311 clientmenu->update();
312 screen.saveWorkspaceNames();
313 }
314
315
316 void Workspace::shutdown(void) {
317 while (!_windows.empty())
318 _windows[0]->restore();
319 }
320
321 static rectList calcSpace(const Rect &win, const rectList &spaces) {
322 rectList result;
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,
330 siter->h()));
331 //Check for space above the window
332 if(win.y() > siter->y())
333 result.push_back(Rect(siter->x(), siter->y(),
334 siter->w(),
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,
340 siter->y(),
341 siter->x() + siter->w() -
342 win.x() - win.w() - 1,
343 siter->h()));
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,
349 siter->w(),
350 siter->y() + siter->h()-
351 win.y() - win.h() - 1));
352
353 }
354 else
355 result.push_back(*siter);
356 }
357 return result;
358 }
359
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();
364 }
365
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();
370 }
371
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();
376 }
377
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();
382 }
383
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();
388 }
389
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();
394 }
395
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();
400 }
401
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();
406 }
407
408
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
411 //to bottom.
412 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
413 const Rect *best;
414 rectList spaces;
415 rectList::const_iterator siter;
416 spaces.push_back(space); //initially the entire screen is free
417
418 //Find Free Spaces
419 winVect::iterator it;
420 for (it = _windows.begin(); it != _windows.end(); ++it)
421 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
422 spaces);
423
424 //Find first space that fits the window
425 best = NULL;
426 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
427 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
428 if (best==NULL)
429 best = &*siter;
430 else if(siter->w()*siter->h()<best->h()*best->w())
431 best = &*siter;
432 }
433 }
434 if (best != NULL) {
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()));
440 return pt;
441 } else
442 return NULL; //fall back to cascade
443 }
444
445 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
446 Point *pt;
447
448 int x, y, rx, ry;
449 Window c, r;
450 unsigned int m;
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);
454
455 if (pt->x() < space.x())
456 pt->setX(space.x());
457 if (pt->y() < space.y())
458 pt->setY(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());
463 return pt;
464 }
465
466 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
467 const Rect *best;
468 rectList spaces;
469
470 rectList::const_iterator siter;
471 spaces.push_back(space); //initially the entire screen is free
472
473 //Find Free Spaces
474 winVect::iterator it;
475 for (it = _windows.begin(); it != _windows.end(); ++it)
476 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
477 spaces);
478 //Sort spaces by preference
479 if(screen.rowPlacementDirection() == BScreen::RightLeft)
480 if(screen.colPlacementDirection() == BScreen::TopBottom)
481 sort(spaces.begin(),spaces.end(),rowRLTB);
482 else
483 sort(spaces.begin(),spaces.end(),rowRLBT);
484 else
485 if(screen.colPlacementDirection() == BScreen::TopBottom)
486 sort(spaces.begin(),spaces.end(),rowLRTB);
487 else
488 sort(spaces.begin(),spaces.end(),rowLRBT);
489 best = NULL;
490 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
491 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
492 best = &*siter;
493 break;
494 }
495
496 if (best != NULL) {
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());
502 return pt;
503 } else
504 return NULL; //fall back to cascade
505 }
506
507 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
508 const Rect *best;
509 rectList spaces;
510
511 rectList::const_iterator siter;
512 spaces.push_back(space); //initially the entire screen is free
513
514 //Find Free Spaces
515 winVect::iterator it;
516 for (it = _windows.begin(); it != _windows.end(); ++it)
517 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
518 spaces);
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);
523 else
524 sort(spaces.begin(),spaces.end(),colRLTB);
525 else
526 if(screen.rowPlacementDirection() == BScreen::LeftRight)
527 sort(spaces.begin(),spaces.end(),colLRBT);
528 else
529 sort(spaces.begin(),spaces.end(),colRLBT);
530
531 //Find first space that fits the window
532 best = NULL;
533 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
534 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
535 best = &*siter;
536 break;
537 }
538
539 if (best != NULL) {
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()));
545 return pt;
546 } else
547 return NULL; //fall back to cascade
548 }
549
550
551 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
552 const Rect &space) {
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();
561 }
562
563 Point *p = new Point(cascade_x, cascade_y);
564 cascade_x += win.getTitleHeight();
565 cascade_y += win.getTitleHeight();
566 return p;
567 }
568
569
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);
574 Point *place = NULL;
575
576 switch (screen.placementPolicy()) {
577 case BScreen::BestFitPlacement:
578 place = bestFitPlacement(window_size, space);
579 break;
580 case BScreen::RowSmartPlacement:
581 place = rowSmartPlacement(window_size, space);
582 break;
583 case BScreen::ColSmartPlacement:
584 place = colSmartPlacement(window_size, space);
585 break;
586 case BScreen::UnderMousePlacement:
587 case BScreen::ClickMousePlacement:
588 place = underMousePlacement(window_size, space);
589 break;
590 } // switch
591
592 if (place == NULL)
593 place = cascadePlacement(win, space);
594
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);
600
601 win.configure(place->x(), place->y(), win.area().w(), win.area().h());
602 delete place;
603 }
This page took 0.065975 seconds and 4 git commands to generate.