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