]> Dogcows Code - chaz/openbox/blob - src/Workspace.cc
merge the sticky window fix from 1.2.
[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 winVect::iterator winit = std::find(_windows.begin(), _windows.end(), w);
110
111 if (winit == _windows.end()) {
112 if (w == _last)
113 _last = (OpenboxWindow *) 0;
114 if (w == _focused)
115 _focused = (OpenboxWindow *) 0;
116 return _windows.size();
117 }
118
119 _zorder.remove(w);
120
121 if (w == _last)
122 _last = (OpenboxWindow *) 0;
123 if (w == _focused) {
124 OpenboxWindow *fw = (OpenboxWindow *) 0;
125 if (w->isTransient() && w->getTransientFor() &&
126 w->getTransientFor()->isVisible())
127 fw = w->getTransientFor();
128 else if (screen.sloppyFocus()) // sloppy focus
129 fw = (OpenboxWindow *) 0;
130 else if (!_zorder.empty()) // click focus
131 fw = _zorder.front();
132
133 if (!(fw != (OpenboxWindow *) 0 && fw->setInputFocus()))
134 screen.getOpenbox().focusWindow(0);
135 }
136
137 _windows.erase(winit);
138 clientmenu->remove(w->getWindowNumber());
139 clientmenu->update();
140
141 screen.updateNetizenWindowDel(w->getClientWindow());
142
143 winVect::iterator it = _windows.begin();
144 for (int i=0; it != _windows.end(); ++it, ++i)
145 (*it)->setWindowNumber(i);
146
147 return _windows.size();
148 }
149
150
151 void Workspace::focusWindow(OpenboxWindow *win) {
152 if (_focused != (OpenboxWindow *) 0)
153 clientmenu->setItemSelected(_focused->getWindowNumber(), false);
154 _focused = win;
155 // make sure the focused window belongs to this workspace before highlighting
156 // it in the menu (sticky windows arent in this workspace's menu).
157 if (_focused != (OpenboxWindow *) 0 && _focused->getWorkspaceNumber() == id)
158 clientmenu->setItemSelected(_focused->getWindowNumber(), true);
159 if (win != (OpenboxWindow *) 0)
160 _last = win;
161 }
162
163
164 void Workspace::showAll(void) {
165 winList::iterator it;
166 for (it = _zorder.begin(); it != _zorder.end(); ++it)
167 (*it)->deiconify(false, false);
168 }
169
170
171 void Workspace::hideAll(void) {
172 winList::reverse_iterator it;
173 for (it = _zorder.rbegin(); it != _zorder.rend(); ++it)
174 if (!(*it)->isStuck())
175 (*it)->withdraw();
176 }
177
178
179 void Workspace::removeAll(void) {
180 winVect::iterator it;
181 for (it = _windows.begin(); it != _windows.end(); ++it)
182 (*it)->iconify();
183 }
184
185
186 void Workspace::raiseWindow(OpenboxWindow *w) {
187 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
188
189 while (bottom->isTransient() && bottom->getTransientFor())
190 bottom = bottom->getTransientFor();
191
192 int i = 1;
193 win = bottom;
194 while (win->hasTransient() && win->getTransient()) {
195 win = win->getTransient();
196
197 i++;
198 }
199
200 Window *nstack = new Window[i], *curr = nstack;
201 Workspace *wkspc;
202
203 win = bottom;
204 while (true) {
205 *(curr++) = win->getFrameWindow();
206 screen.updateNetizenWindowRaise(win->getClientWindow());
207
208 if (! win->isIconic()) {
209 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
210 wkspc->_zorder.remove(win);
211 wkspc->_zorder.push_front(win);
212 }
213
214 if (! win->hasTransient() || ! win->getTransient())
215 break;
216
217 win = win->getTransient();
218 }
219
220 screen.raiseWindows(nstack, i);
221
222 delete [] nstack;
223 }
224
225
226 void Workspace::lowerWindow(OpenboxWindow *w) {
227 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
228
229 while (bottom->isTransient() && bottom->getTransientFor())
230 bottom = bottom->getTransientFor();
231
232 int i = 1;
233 win = bottom;
234 while (win->hasTransient() && win->getTransient()) {
235 win = win->getTransient();
236
237 i++;
238 }
239
240 Window *nstack = new Window[i], *curr = nstack;
241 Workspace *wkspc;
242
243 while (true) {
244 *(curr++) = win->getFrameWindow();
245 screen.updateNetizenWindowLower(win->getClientWindow());
246
247 if (! win->isIconic()) {
248 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
249 wkspc->_zorder.remove(win);
250 wkspc->_zorder.push_back(win);
251 }
252
253 if (! win->getTransientFor())
254 break;
255
256 win = win->getTransientFor();
257 }
258
259 screen.getOpenbox().grab();
260
261 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
262 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
263
264 screen.getOpenbox().ungrab();
265
266 delete [] nstack;
267 }
268
269
270 void Workspace::reconfigure(void) {
271 clientmenu->reconfigure();
272
273 winVect::iterator it;
274 for (it = _windows.begin(); it != _windows.end(); ++it)
275 if ((*it)->validateClient())
276 (*it)->reconfigure();
277 }
278
279
280 OpenboxWindow *Workspace::getWindow(int index) {
281 if ((index >= 0) && (index < (signed)_windows.size()))
282 return _windows[index];
283 else
284 return (OpenboxWindow *) 0;
285 }
286
287
288 int Workspace::getCount(void) {
289 return (signed)_windows.size();
290 }
291
292
293 void Workspace::update(void) {
294 clientmenu->update();
295 screen.getToolbar()->redrawWindowLabel(true);
296 }
297
298
299 bool Workspace::isCurrent(void) {
300 return (id == screen.getCurrentWorkspaceID());
301 }
302
303
304 void Workspace::setCurrent(void) {
305 screen.changeWorkspaceID(id);
306 }
307
308
309 void Workspace::setName(const char *new_name) {
310 if (name)
311 delete [] name;
312
313 if (new_name) {
314 name = bstrdup(new_name);
315 } else {
316 name = new char[128];
317 sprintf(name, i18n(WorkspaceSet, WorkspaceDefaultNameFormat,
318 "Workspace %d"), id + 1);
319 }
320
321 clientmenu->setLabel(name);
322 clientmenu->update();
323 screen.saveWorkspaceNames();
324 }
325
326
327 void Workspace::shutdown(void) {
328 while (!_windows.empty())
329 _windows[0]->restore();
330 }
331
332 static rectList calcSpace(const Rect &win, const rectList &spaces) {
333 rectList result;
334 rectList::const_iterator siter;
335 for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
336 if(win.Intersect(*siter)) {
337 //Check for space to the left of the window
338 if(win.x() > siter->x())
339 result.push_back(Rect(siter->x(), siter->y(),
340 win.x() - siter->x() - 1,
341 siter->h()));
342 //Check for space above the window
343 if(win.y() > siter->y())
344 result.push_back(Rect(siter->x(), siter->y(),
345 siter->w(),
346 win.y() - siter->y() - 1));
347 //Check for space to the right of the window
348 if((win.x()+win.w()) <
349 (siter->x()+siter->w()))
350 result.push_back(Rect(win.x() + win.w() + 1,
351 siter->y(),
352 siter->x() + siter->w() -
353 win.x() - win.w() - 1,
354 siter->h()));
355 //Check for space below the window
356 if((win.y()+win.h()) <
357 (siter->y()+siter->h()))
358 result.push_back(Rect(siter->x(),
359 win.y() + win.h() + 1,
360 siter->w(),
361 siter->y() + siter->h()-
362 win.y() - win.h() - 1));
363
364 }
365 else
366 result.push_back(*siter);
367 }
368 return result;
369 }
370
371 bool rowRLBT(const Rect &first, const Rect &second){
372 if (first.y()+first.h()==second.y()+second.h())
373 return first.x()+first.w()>second.x()+second.w();
374 return first.y()+first.h()>second.y()+second.h();
375 }
376
377 bool rowRLTB(const Rect &first, const Rect &second){
378 if (first.y()==second.y())
379 return first.x()+first.w()>second.x()+second.w();
380 return first.y()<second.y();
381 }
382
383 bool rowLRBT(const Rect &first, const Rect &second){
384 if (first.y()+first.h()==second.y()+second.h())
385 return first.x()<second.x();
386 return first.y()+first.h()>second.y()+second.h();
387 }
388
389 bool rowLRTB(const Rect &first, const Rect &second){
390 if (first.y()==second.y())
391 return first.x()<second.x();
392 return first.y()<second.y();
393 }
394
395 bool colLRTB(const Rect &first, const Rect &second){
396 if (first.x()==second.x())
397 return first.y()<second.y();
398 return first.x()<second.x();
399 }
400
401 bool colLRBT(const Rect &first, const Rect &second){
402 if (first.x()==second.x())
403 return first.y()+first.h()>second.y()+second.h();
404 return first.x()<second.x();
405 }
406
407 bool colRLTB(const Rect &first, const Rect &second){
408 if (first.x()+first.w()==second.x()+second.w())
409 return first.y()<second.y();
410 return first.x()+first.w()>second.x()+second.w();
411 }
412
413 bool colRLBT(const Rect &first, const Rect &second){
414 if (first.x()+first.w()==second.x()+second.w())
415 return first.y()+first.h()>second.y()+second.h();
416 return first.x()+first.w()>second.x()+second.w();
417 }
418
419
420 //BestFitPlacement finds the smallest free space that fits the window
421 //to be placed. It currentl ignores whether placement is right to left or top
422 //to bottom.
423 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space) {
424 const Rect *best;
425 rectList spaces;
426 rectList::const_iterator siter;
427 spaces.push_back(space); //initially the entire screen is free
428
429 //Find Free Spaces
430 winVect::iterator it;
431 for (it = _windows.begin(); it != _windows.end(); ++it)
432 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
433 spaces);
434
435 //Find first space that fits the window
436 best = NULL;
437 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
438 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
439 if (best==NULL)
440 best = &*siter;
441 else if(siter->w()*siter->h()<best->h()*best->w())
442 best = &*siter;
443 }
444 }
445 if (best != NULL) {
446 Point *pt = new Point(best->origin());
447 if (screen.colPlacementDirection() != BScreen::TopBottom)
448 pt->setY(pt->y() + (best->h() - win_size.h()));
449 if (screen.rowPlacementDirection() != BScreen::LeftRight)
450 pt->setX(pt->x() + (best->w() - win_size.w()));
451 return pt;
452 } else
453 return NULL; //fall back to cascade
454 }
455
456 Point *Workspace::underMousePlacement(const Size &win_size, const Rect &space) {
457 Point *pt;
458
459 int x, y, rx, ry;
460 Window c, r;
461 unsigned int m;
462 XQueryPointer(screen.getOpenbox().getXDisplay(), screen.getRootWindow(),
463 &r, &c, &rx, &ry, &x, &y, &m);
464 pt = new Point(rx - win_size.w() / 2, ry - win_size.h() / 2);
465
466 if (pt->x() < space.x())
467 pt->setX(space.x());
468 if (pt->y() < space.y())
469 pt->setY(space.y());
470 if (pt->x() + win_size.w() > space.x() + space.w())
471 pt->setX(space.x() + space.w() - win_size.w());
472 if (pt->y() + win_size.h() > space.y() + space.h())
473 pt->setY(space.y() + space.h() - win_size.h());
474 return pt;
475 }
476
477 Point *Workspace::rowSmartPlacement(const Size &win_size, const Rect &space) {
478 const Rect *best;
479 rectList spaces;
480
481 rectList::const_iterator siter;
482 spaces.push_back(space); //initially the entire screen is free
483
484 //Find Free Spaces
485 winVect::iterator it;
486 for (it = _windows.begin(); it != _windows.end(); ++it)
487 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
488 spaces);
489 //Sort spaces by preference
490 if(screen.rowPlacementDirection() == BScreen::RightLeft)
491 if(screen.colPlacementDirection() == BScreen::TopBottom)
492 sort(spaces.begin(),spaces.end(),rowRLTB);
493 else
494 sort(spaces.begin(),spaces.end(),rowRLBT);
495 else
496 if(screen.colPlacementDirection() == BScreen::TopBottom)
497 sort(spaces.begin(),spaces.end(),rowLRTB);
498 else
499 sort(spaces.begin(),spaces.end(),rowLRBT);
500 best = NULL;
501 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
502 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
503 best = &*siter;
504 break;
505 }
506
507 if (best != NULL) {
508 Point *pt = new Point(best->origin());
509 if (screen.colPlacementDirection() != BScreen::TopBottom)
510 pt->setY(best->y() + best->h() - win_size.h());
511 if (screen.rowPlacementDirection() != BScreen::LeftRight)
512 pt->setX(best->x()+best->w()-win_size.w());
513 return pt;
514 } else
515 return NULL; //fall back to cascade
516 }
517
518 Point *Workspace::colSmartPlacement(const Size &win_size, const Rect &space) {
519 const Rect *best;
520 rectList spaces;
521
522 rectList::const_iterator siter;
523 spaces.push_back(space); //initially the entire screen is free
524
525 //Find Free Spaces
526 winVect::iterator it;
527 for (it = _windows.begin(); it != _windows.end(); ++it)
528 spaces = calcSpace((*it)->area().Inflate(screen.getBorderWidth() * 4),
529 spaces);
530 //Sort spaces by user preference
531 if(screen.colPlacementDirection() == BScreen::TopBottom)
532 if(screen.rowPlacementDirection() == BScreen::LeftRight)
533 sort(spaces.begin(),spaces.end(),colLRTB);
534 else
535 sort(spaces.begin(),spaces.end(),colRLTB);
536 else
537 if(screen.rowPlacementDirection() == BScreen::LeftRight)
538 sort(spaces.begin(),spaces.end(),colLRBT);
539 else
540 sort(spaces.begin(),spaces.end(),colRLBT);
541
542 //Find first space that fits the window
543 best = NULL;
544 for (siter=spaces.begin(); siter!=spaces.end(); ++siter)
545 if ((siter->w() >= win_size.w()) && (siter->h() >= win_size.h())) {
546 best = &*siter;
547 break;
548 }
549
550 if (best != NULL) {
551 Point *pt = new Point(best->origin());
552 if (screen.colPlacementDirection() != BScreen::TopBottom)
553 pt->setY(pt->y() + (best->h() - win_size.h()));
554 if (screen.rowPlacementDirection() != BScreen::LeftRight)
555 pt->setX(pt->x() + (best->w() - win_size.w()));
556 return pt;
557 } else
558 return NULL; //fall back to cascade
559 }
560
561
562 Point *const Workspace::cascadePlacement(const OpenboxWindow &win,
563 const Rect &space) {
564 if ((cascade_x + win.area().w() + screen.getBorderWidth() * 2 >
565 (space.x() + space.w())) ||
566 (cascade_y + win.area().h() + screen.getBorderWidth() * 2 >
567 (space.y() + space.h())))
568 cascade_x = cascade_y = 0;
569 if (cascade_x < space.x() || cascade_y < space.y()) {
570 cascade_x = space.x();
571 cascade_y = space.y();
572 }
573
574 Point *p = new Point(cascade_x, cascade_y);
575 cascade_x += win.getTitleHeight();
576 cascade_y += win.getTitleHeight();
577 return p;
578 }
579
580
581 void Workspace::placeWindow(OpenboxWindow &win) {
582 Rect space = screen.availableArea();
583 const Size window_size(win.area().w()+screen.getBorderWidth() * 2,
584 win.area().h()+screen.getBorderWidth() * 2);
585 Point *place = NULL;
586
587 switch (screen.placementPolicy()) {
588 case BScreen::BestFitPlacement:
589 place = bestFitPlacement(window_size, space);
590 break;
591 case BScreen::RowSmartPlacement:
592 place = rowSmartPlacement(window_size, space);
593 break;
594 case BScreen::ColSmartPlacement:
595 place = colSmartPlacement(window_size, space);
596 break;
597 case BScreen::UnderMousePlacement:
598 case BScreen::ClickMousePlacement:
599 place = underMousePlacement(window_size, space);
600 break;
601 } // switch
602
603 if (place == NULL)
604 place = cascadePlacement(win, space);
605
606 ASSERT(place != NULL);
607 if (place->x() + window_size.w() > (signed) space.x() + space.w())
608 place->setX(((signed) space.x() + space.w() - window_size.w()) / 2);
609 if (place->y() + window_size.h() > (signed) space.y() + space.h())
610 place->setY(((signed) space.y() + space.h() - window_size.h()) / 2);
611
612 win.configure(place->x(), place->y(), win.area().w(), win.area().h());
613 delete place;
614 }
This page took 0.06345 seconds and 4 git commands to generate.