]> Dogcows Code - chaz/openbox/blob - src/Workspace.cc
bestfit placement now honors the left->right and top->bottom options for window place...
[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 #include <X11/Xlib.h>
35 #include <X11/Xatom.h>
36
37 #include "i18n.h"
38 #include "openbox.h"
39 #include "Clientmenu.h"
40 #include "Screen.h"
41 #include "Toolbar.h"
42 #include "Window.h"
43 #include "Workspace.h"
44
45 #include "Windowmenu.h"
46 #include "Geometry.h"
47 #include "Util.h"
48
49 #ifdef HAVE_STDIO_H
50 # include <stdio.h>
51 #endif // HAVE_STDIO_H
52
53 #ifdef HAVE_STDLIB_H
54 # include <stdlib.h>
55 #endif // HAVE_STDLIB_H
56
57 #ifdef HAVE_STRING_H
58 # include <string.h>
59 #endif // HAVE_STRING_H
60
61 #include <vector>
62 typedef vector<Rect> rectList;
63
64 Workspace::Workspace(BScreen &scrn, int i) : screen(scrn) {
65
66 cascade_x = cascade_y = 32;
67
68 id = i;
69
70 stackingList = new LinkedList<OpenboxWindow>;
71 windowList = new LinkedList<OpenboxWindow>;
72 clientmenu = new Clientmenu(*this);
73
74 lastfocus = (OpenboxWindow *) 0;
75
76 name = (char *) 0;
77 char *tmp = screen.getNameOfWorkspace(id);
78 setName(tmp);
79 }
80
81
82 Workspace::~Workspace(void) {
83 delete stackingList;
84 delete windowList;
85 delete clientmenu;
86
87 if (name)
88 delete [] name;
89 }
90
91
92 const int Workspace::addWindow(OpenboxWindow *w, Bool place) {
93 if (! w) return -1;
94
95 if (place) placeWindow(w);
96
97 w->setWorkspace(id);
98 w->setWindowNumber(windowList->count());
99
100 stackingList->insert(w, 0);
101 windowList->insert(w);
102
103 clientmenu->insert((const char **) w->getTitle());
104 clientmenu->update();
105
106 screen.updateNetizenWindowAdd(w->getClientWindow(), id);
107
108 raiseWindow(w);
109
110 return w->getWindowNumber();
111 }
112
113
114 const int Workspace::removeWindow(OpenboxWindow *w) {
115 if (! w) return -1;
116
117 stackingList->remove(w);
118
119 if (w->isFocused()) {
120 if (w->isTransient() && w->getTransientFor() &&
121 w->getTransientFor()->isVisible()) {
122 w->getTransientFor()->setInputFocus();
123 } else if (screen.sloppyFocus()) {
124 screen.getOpenbox().setFocusedWindow((OpenboxWindow *) 0);
125 } else {
126 OpenboxWindow *top = stackingList->first();
127 if (! top || ! top->setInputFocus()) {
128 screen.getOpenbox().setFocusedWindow((OpenboxWindow *) 0);
129 XSetInputFocus(screen.getOpenbox().getXDisplay(),
130 screen.getToolbar()->getWindowID(),
131 RevertToParent, CurrentTime);
132 }
133 }
134 }
135
136 if (lastfocus == w)
137 lastfocus = (OpenboxWindow *) 0;
138
139 windowList->remove(w->getWindowNumber());
140 clientmenu->remove(w->getWindowNumber());
141 clientmenu->update();
142
143 screen.updateNetizenWindowDel(w->getClientWindow());
144
145 LinkedListIterator<OpenboxWindow> it(windowList);
146 OpenboxWindow *bw = it.current();
147 for (int i = 0; bw; it++, i++, bw = it.current())
148 bw->setWindowNumber(i);
149
150 return windowList->count();
151 }
152
153
154 void Workspace::showAll(void) {
155 LinkedListIterator<OpenboxWindow> it(stackingList);
156 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
157 bw->deiconify(False, False);
158 }
159
160
161 void Workspace::hideAll(void) {
162 LinkedList<OpenboxWindow> lst;
163
164 LinkedListIterator<OpenboxWindow> it(stackingList);
165 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
166 lst.insert(bw, 0);
167
168 LinkedListIterator<OpenboxWindow> it2(&lst);
169 for (OpenboxWindow *bw = it2.current(); bw; it2++, bw = it2.current())
170 if (! bw->isStuck())
171 bw->withdraw();
172 }
173
174
175 void Workspace::removeAll(void) {
176 LinkedListIterator<OpenboxWindow> it(windowList);
177 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current())
178 bw->iconify();
179 }
180
181
182 void Workspace::raiseWindow(OpenboxWindow *w) {
183 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
184
185 while (bottom->isTransient() && bottom->getTransientFor())
186 bottom = bottom->getTransientFor();
187
188 int i = 1;
189 win = bottom;
190 while (win->hasTransient() && win->getTransient()) {
191 win = win->getTransient();
192
193 i++;
194 }
195
196 Window *nstack = new Window[i], *curr = nstack;
197 Workspace *wkspc;
198
199 win = bottom;
200 while (True) {
201 *(curr++) = win->getFrameWindow();
202 screen.updateNetizenWindowRaise(win->getClientWindow());
203
204 if (! win->isIconic()) {
205 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
206 wkspc->stackingList->remove(win);
207 wkspc->stackingList->insert(win, 0);
208 }
209
210 if (! win->hasTransient() || ! win->getTransient())
211 break;
212
213 win = win->getTransient();
214 }
215
216 screen.raiseWindows(nstack, i);
217
218 delete [] nstack;
219 }
220
221
222 void Workspace::lowerWindow(OpenboxWindow *w) {
223 OpenboxWindow *win = (OpenboxWindow *) 0, *bottom = w;
224
225 while (bottom->isTransient() && bottom->getTransientFor())
226 bottom = bottom->getTransientFor();
227
228 int i = 1;
229 win = bottom;
230 while (win->hasTransient() && win->getTransient()) {
231 win = win->getTransient();
232
233 i++;
234 }
235
236 Window *nstack = new Window[i], *curr = nstack;
237 Workspace *wkspc;
238
239 while (True) {
240 *(curr++) = win->getFrameWindow();
241 screen.updateNetizenWindowLower(win->getClientWindow());
242
243 if (! win->isIconic()) {
244 wkspc = screen.getWorkspace(win->getWorkspaceNumber());
245 wkspc->stackingList->remove(win);
246 wkspc->stackingList->insert(win);
247 }
248
249 if (! win->getTransientFor())
250 break;
251
252 win = win->getTransientFor();
253 }
254
255 screen.getOpenbox().grab();
256
257 XLowerWindow(screen.getBaseDisplay().getXDisplay(), *nstack);
258 XRestackWindows(screen.getBaseDisplay().getXDisplay(), nstack, i);
259
260 screen.getOpenbox().ungrab();
261
262 delete [] nstack;
263 }
264
265
266 void Workspace::reconfigure(void) {
267 clientmenu->reconfigure();
268
269 LinkedListIterator<OpenboxWindow> it(windowList);
270 for (OpenboxWindow *bw = it.current(); bw; it++, bw = it.current()) {
271 if (bw->validateClient())
272 bw->reconfigure();
273 }
274 }
275
276
277 OpenboxWindow *Workspace::getWindow(int index) {
278 if ((index >= 0) && (index < windowList->count()))
279 return windowList->find(index);
280 else
281 return 0;
282 }
283
284
285 const int Workspace::getCount(void) {
286 return windowList->count();
287 }
288
289
290 void Workspace::update(void) {
291 clientmenu->update();
292 screen.getToolbar()->redrawWindowLabel(True);
293 }
294
295
296 Bool Workspace::isCurrent(void) {
297 return (id == screen.getCurrentWorkspaceID());
298 }
299
300
301 Bool Workspace::isLastWindow(OpenboxWindow *w) {
302 return (w == windowList->last());
303 }
304
305 void Workspace::setCurrent(void) {
306 screen.changeWorkspaceID(id);
307 }
308
309
310 void Workspace::setName(char *new_name) {
311 if (name)
312 delete [] name;
313
314 if (new_name) {
315 name = bstrdup(new_name);
316 } else {
317 name = new char[128];
318 sprintf(name, i18n->getMessage(WorkspaceSet, WorkspaceDefaultNameFormat,
319 "Workspace %d"), id + 1);
320 }
321
322 clientmenu->setLabel(name);
323 clientmenu->update();
324 screen.saveWorkspaceNames();
325 }
326
327
328 void Workspace::shutdown(void) {
329 while (windowList->count()) {
330 windowList->first()->restore();
331 delete windowList->first();
332 }
333 }
334
335 static rectList calcSpace(const Rect &win, const rectList &spaces) {
336 rectList result;
337 rectList::const_iterator siter;
338 for(siter=spaces.begin(); siter!=spaces.end(); ++siter) {
339 if(win.Intersect(*siter)) {
340 //Check for space to the left of the window
341 if(win.origin().x() > siter->x())
342 result.push_back(Rect(siter->x(), siter->y(),
343 win.origin().x() - siter->x() - 1,
344 siter->h()));
345 //Check for space above the window
346 if(win.origin().y() > siter->y())
347 result.push_back(Rect(siter->x(), siter->y(),
348 siter->w(),
349 win.origin().y() - siter->y() - 1));
350 //Check for space to the right of the window
351 if((win.origin().x()+win.size().w()) <
352 (siter->x()+siter->w()))
353 result.push_back(Rect(win.origin().x() + win.size().w() + 1,
354 siter->y(),
355 siter->x() + siter->w() -
356 win.origin().x() - win.size().w() - 1,
357 siter->h()));
358 //Check for space below the window
359 if((win.origin().y()+win.size().h()) <
360 (siter->y()+siter->h()))
361 result.push_back(Rect(siter->x(),
362 win.origin().y() + win.size().h() + 1,
363 siter->w(),
364 siter->y() + siter->h()-
365 win.origin().y() - win.size().h() - 1));
366
367 }
368 else
369 result.push_back(*siter);
370 }
371 return result;
372 }
373
374 //BestFitPlacement finds the smallest free space that fits the window
375 //to be placed. It currentl ignores whether placement is right to left or top
376 //to bottom.
377 Point *Workspace::bestFitPlacement(const Size &win_size, const Rect &space)
378 {
379 const Rect *best;
380 rectList spaces;
381 LinkedListIterator<OpenboxWindow> it(windowList);
382 rectList::const_iterator siter;
383 spaces.push_back(space); //initially the entire screen is free
384 it.reset();
385
386 //Find Free Spaces
387 for (OpenboxWindow *cur=it.current(); cur!=NULL; it++, cur=it.current())
388 spaces = calcSpace(cur->area().Inflate(screen.getBorderWidth() * 4),
389 spaces);
390
391 //Find first space that fits the window
392 best = NULL;
393 for (siter=spaces.begin(); siter!=spaces.end(); ++siter) {
394 if ((siter->w() >= win_size.w()) &&
395 (siter->h() >= win_size.h()))
396 best = siter;
397 }
398
399 if (best != NULL) {
400 Point *pt = new Point(best->origin());
401 if (screen.colPlacementDirection() != BScreen::TopBottom)
402 pt->setY(pt->y() + (best->h() - win_size.h()));
403 if (screen.rowPlacementDirection() != BScreen::LeftRight)
404 pt->setX(pt->x() + (best->w() - win_size.w()));
405 return pt;
406 } else
407 return NULL; //fall back to cascade
408 }
409
410 inline Point *Workspace::rowSmartPlacement(const Size &win_size,
411 const Rect &space){
412 bool placed=false;
413 int test_x, test_y, place_x = 0, place_y = 0;
414 int start_pos = 0;
415 int change_y =
416 ((screen.colPlacementDirection() == BScreen::TopBottom) ? 1 : -1);
417 int change_x =
418 ((screen.rowPlacementDirection() == BScreen::LeftRight) ? 1 : -1);
419 int delta_x = 8, delta_y = 8;
420 LinkedListIterator<OpenboxWindow> it(windowList);
421
422 test_y = (screen.colPlacementDirection() == BScreen::TopBottom) ?
423 start_pos : screen.size().h() - win_size.h() - start_pos;
424
425 while(!placed &&
426 ((screen.colPlacementDirection() == BScreen::BottomTop) ?
427 test_y > 0 : test_y + win_size.h() < (signed) space.h())) {
428 test_x = (screen.rowPlacementDirection() == BScreen::LeftRight) ?
429 start_pos : space.w() - win_size.w() - start_pos;
430 while (!placed &&
431 ((screen.rowPlacementDirection() == BScreen::RightLeft) ?
432 test_x > 0 : test_x + win_size.w() < (signed) space.w())) {
433 placed = true;
434
435 it.reset();
436 for (OpenboxWindow *curr = it.current(); placed && curr;
437 it++, curr = it.current()) {
438 int curr_w = curr->size().w() + (screen.getBorderWidth() * 4);
439 int curr_h = curr->size().h() + (screen.getBorderWidth() * 4);
440
441 if (curr->origin().x() < test_x + win_size.w() &&
442 curr->origin().x() + curr_w > test_x &&
443 curr->origin().y() < test_y + win_size.h() &&
444 curr->origin().y() + curr_h > test_y) {
445 placed = false;
446 }
447 }
448
449 // Removed code for checking toolbar and slit
450 // The space passed in should not include either
451
452 if (placed) {
453 place_x = test_x;
454 place_y = test_y;
455
456 break;
457 }
458
459 test_x += (change_x * delta_x);
460 }
461
462 test_y += (change_y * delta_y);
463 }
464 if (placed)
465 return new Point(place_x, place_y);
466 else
467 return NULL; // fall back to cascade
468 }
469
470 inline Point * Workspace::colSmartPlacement(const Size &win_size,
471 const Rect &space){
472 Point *pt;
473 bool placed=false;
474 int test_x, test_y;
475 int start_pos = 0;
476 int change_y =
477 ((screen.colPlacementDirection() == BScreen::TopBottom) ? 1 : -1);
478 int change_x =
479 ((screen.rowPlacementDirection() == BScreen::LeftRight) ? 1 : -1);
480 int delta_x = 8, delta_y = 8;
481 LinkedListIterator<OpenboxWindow> it(windowList);
482
483 test_x = (screen.rowPlacementDirection() == BScreen::LeftRight) ?
484 start_pos : screen.size().w() - win_size.w() - start_pos;
485
486 while(!placed &&
487 ((screen.rowPlacementDirection() == BScreen::RightLeft) ?
488 test_x > 0 : test_x + win_size.w() < (signed) space.w())) {
489 test_y = (screen.colPlacementDirection() == BScreen::TopBottom) ?
490 start_pos : screen.size().h() - win_size.h() - start_pos;
491
492 while(!placed &&
493 ((screen.colPlacementDirection() == BScreen::BottomTop) ?
494 test_y > 0 : test_y + win_size.h() < (signed) space.h())){
495
496 placed = true;
497
498 it.reset();
499 for (OpenboxWindow *curr = it.current(); placed && curr;
500 it++, curr = it.current()) {
501 int curr_w = curr->size().w() + (screen.getBorderWidth() * 4);
502 int curr_h = curr->size().h() + (screen.getBorderWidth() * 4);
503
504 if (curr->origin().x() < test_x + win_size.w() &&
505 curr->origin().x() + curr_w > test_x &&
506 curr->origin().y() < test_y + win_size.h() &&
507 curr->origin().y() + curr_h > test_y) {
508 placed = False;
509 }
510 }
511
512 // Removed code checking for intersection with Toolbar and Slit
513 // The space passed to this method should not include either
514
515 if (placed) {
516 pt= new Point(test_x,test_y);
517
518 break;
519 }
520
521 test_y += (change_y * delta_y);
522 }
523
524 test_x += (change_x * delta_x);
525 }
526 if (placed)
527 return pt;
528 else
529 return NULL;
530 }
531
532 Point *const Workspace::cascadePlacement(const OpenboxWindow *const win){
533 if (((unsigned) cascade_x > (screen.size().w() / 2)) ||
534 ((unsigned) cascade_y > (screen.size().h() / 2)))
535 cascade_x = cascade_y = 32;
536
537 cascade_x += win->getTitleHeight();
538 cascade_y += win->getTitleHeight();
539
540 return new Point(cascade_x, cascade_y);
541 }
542
543
544 void Workspace::placeWindow(OpenboxWindow *win) {
545 ASSERT(win != NULL);
546
547 // the following code is temporary and will be taken care of by Screen in the
548 // future (with the NETWM 'strut')
549 Rect space(0, 0, screen.size().w(), screen.size().h());
550
551 #ifdef SLIT
552 Slit *slit = screen.getSlit();
553 Toolbar *toolbar = screen.getToolbar();
554 int tbarh = screen.hideToolbar() ? 0 :
555 toolbar->getExposedHeight() + screen.getBorderWidth() * 2;
556 bool tbartop;
557 switch (toolbar->placement()) {
558 case Toolbar::TopLeft:
559 case Toolbar::TopCenter:
560 case Toolbar::TopRight:
561 tbartop = true;
562 break;
563 case Toolbar::BottomLeft:
564 case Toolbar::BottomCenter:
565 case Toolbar::BottomRight:
566 tbartop = false;
567 break;
568 default:
569 ASSERT(false); // unhandled placement
570 }
571 if ((slit->direction() == Slit::Horizontal &&
572 (slit->placement() == Slit::TopLeft ||
573 slit->placement() == Slit::TopRight)) ||
574 slit->placement() == Slit::TopCenter) {
575 // exclude top
576 if (tbartop) {
577 space.setY(slit->area().y());
578 space.setH(space.h() - space.y());
579 } else
580 space.setH(space.h() - tbarh);
581 space.setY(space.y() + slit->area().h() + screen.getBorderWidth() * 2);
582 space.setH(space.h() - (slit->area().h() + screen.getBorderWidth() * 2));
583 } else if ((slit->direction() == Slit::Vertical &&
584 (slit->placement() == Slit::TopRight ||
585 slit->placement() == Slit::BottomRight)) ||
586 slit->placement() == Slit::CenterRight) {
587 // exclude right
588 space.setW(space.w() - (slit->area().w() + screen.getBorderWidth() * 2));
589 if (tbartop)
590 space.setY(space.y() + tbarh);
591 space.setH(space.h() - tbarh);
592 } else if ((slit->direction() == Slit::Horizontal &&
593 (slit->placement() == Slit::BottomLeft ||
594 slit->placement() == Slit::BottomRight)) ||
595 slit->placement() == Slit::BottomCenter) {
596 // exclude bottom
597 space.setH(space.h() - (screen.size().h() - slit->area().y()));
598 } else {// if ((slit->direction() == Slit::Vertical &&
599 // (slit->placement() == Slit::TopLeft ||
600 // slit->placement() == Slit::BottomLeft)) ||
601 // slit->placement() == Slit::CenterLeft)
602 // exclude left
603 space.setX(slit->area().w() + screen.getBorderWidth() * 2);
604 space.setW(space.w() - (slit->area().w() + screen.getBorderWidth() * 2));
605 if (tbartop)
606 space.setY(space.y() + tbarh);
607 space.setH(space.h() - tbarh);
608 }
609 #else // !SLIT
610 Toolbar *toolbar = screen.getToolbar();
611 int tbarh = screen.hideToolbar() ? 0 :
612 toolbar->getExposedHeight() + screen.getBorderWidth() * 2;
613 switch (toolbar->placement()) {
614 case Toolbar::TopLeft:
615 case Toolbar::TopCenter:
616 case Toolbar::TopRight:
617 space.setY(toolbar->getExposedHeight());
618 space.setH(space.h() - toolbar->getExposedHeight());
619 break;
620 case Toolbar::BottomLeft:
621 case Toolbar::BottomCenter:
622 case Toolbar::BottomRight:
623 space.setH(space.h() - tbarh);
624 break;
625 default:
626 ASSERT(false); // unhandled placement
627 }
628 #endif // SLIT
629
630 const int win_w = win->size().w() + (screen.getBorderWidth() * 4),
631 win_h = win->size().h() + (screen.getBorderWidth() * 4),
632 start_pos = 0,
633 change_y =
634 ((screen.colPlacementDirection() == BScreen::TopBottom) ? 1 : -1),
635 change_x =
636 ((screen.rowPlacementDirection() == BScreen::LeftRight) ? 1 : -1),
637 delta_x = 8, delta_y = 8;
638
639 LinkedListIterator<OpenboxWindow> it(windowList);
640
641 Size window_size(win->size().w()+screen.getBorderWidth() * 4,
642 win->size().h()+screen.getBorderWidth() * 4);
643 Point *place = NULL;
644
645 switch (screen.placementPolicy()) {
646 case BScreen::BestFitPlacement:
647 place = bestFitPlacement(window_size, space);
648 break;
649 case BScreen::RowSmartPlacement:
650 place = rowSmartPlacement(window_size, space);
651 break;
652 case BScreen::ColSmartPlacement:
653 place = colSmartPlacement(window_size, space);
654 break;
655 } // switch
656
657 if (place == NULL)
658 place = cascadePlacement(win);
659
660 ASSERT(place != NULL);
661 if (place->x() + win_w > (signed) screen.size().w())
662 place->setX(((signed) screen.size().w() - win_w) / 2);
663 if (place->y() + win_h > (signed) screen.size().h())
664 place->setY(((signed) screen.size().h() - win_h) / 2);
665
666 win->configure(place->x(), place->y(), win->size().w(), win->size().h());
667 delete place;
668 }
This page took 0.073533 seconds and 5 git commands to generate.