]> icculus.org git repositories - mikachu/openbox.git/blob - src/Workspace.cc
added first revision of the BestFit placement type.
[mikachu/openbox.git] / 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
47 #ifdef    HAVE_STDIO_H
48 #  include <stdio.h>
49 #endif // HAVE_STDIO_H
50
51 #ifdef    HAVE_STDLIB_H
52 #  include <stdlib.h>
53 #endif // HAVE_STDLIB_H
54
55 #ifdef    STDC_HEADERS
56 #  include <string.h>
57 #endif // STDC_HEADERS
58
59 #include <vector>
60 typedef vector<Rect> rectList;
61
62 Workspace::Workspace(BScreen *scrn, int i) {
63   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.getArea().Intersect(*siter)) {
338       //Check for space to the left of the window
339       if(win.getXFrame() > siter->x())
340         result.push_back(Rect(siter->x(), siter->y(),
341                               win.getXFrame() - siter->x() - 1,
342                               siter->h()));
343       //Check for space above the window
344       if(win.getYFrame() > siter->y())
345         result.push_back(Rect(siter->x(), siter->y(),
346                               siter->w(),
347                               win.getYFrame() - siter->y() - 1));
348       //Check for space to the right of the window
349       if((win.getXFrame()+win.getWidth()) <
350          (siter->x()+siter->w()))
351         result.push_back(Rect(win.getXFrame() + win.getWidth() + 1,
352                               siter->y(),
353                               siter->x() + siter->w() -
354                               win.getXFrame() - win.getWidth() - 1,
355                               siter->h()));
356       //Check for space below the window
357       if((win.getYFrame()+win.getHeight()) <
358          (siter->y()+siter->h()))
359         result.push_back(Rect(siter->x(),
360                               win.getYFrame() + win.getHeight() + 1,
361                               siter->w(),
362                               siter->y() + siter->h()-
363                               win.getYFrame() - win.getHeight() - 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->getWidth() + (screen->getBorderWidth() * 4);
431            int curr_h =
432              ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
433              (screen->getBorderWidth() * 4);
434    
435            if (curr->getXFrame() < test_x + win_size.w() &&
436                curr->getXFrame() + curr_w > test_x &&
437                curr->getYFrame() < test_y + win_size.h() &&
438                curr->getYFrame() + curr_h > test_y) {
439               placed = false;
440            }
441         }
442  
443         // Removed code for checking toolbar and slit
444         // The space passed in should not include either
445         
446         if (placed) {
447            place_x = test_x;
448            place_y = test_y;
449  
450            break;
451         }   
452         
453         test_x += (change_x * delta_x);
454      }
455
456      test_y += (change_y * delta_y);
457   }
458   return new Point(place_x, place_y);
459 }
460
461 void Workspace::placeWindow(OpenboxWindow *win) {
462   assert(win != NULL);
463
464   Bool placed = False;
465
466   const int win_w = win->getWidth() + (screen->getBorderWidth() * 4),
467     win_h = win->getHeight() + (screen->getBorderWidth() * 4),
468 #ifdef    SLIT
469     slit_x = screen->getSlit()->getX() - screen->getBorderWidth(),
470     slit_y = screen->getSlit()->getY() - screen->getBorderWidth(),
471     slit_w = screen->getSlit()->getWidth() +
472       (screen->getBorderWidth() * 4),
473     slit_h = screen->getSlit()->getHeight() +
474       (screen->getBorderWidth() * 4),
475 #endif // SLIT
476     toolbar_x = screen->getToolbar()->getX() - screen->getBorderWidth(),
477     toolbar_y = screen->getToolbar()->getY() - screen->getBorderWidth(),
478     toolbar_w = screen->getToolbar()->getWidth() +
479       (screen->getBorderWidth() * 4),
480     toolbar_h = screen->getToolbar()->getHeight() + 
481       (screen->getBorderWidth() * 4),
482     start_pos = 0,
483     change_y =
484       ((screen->getColPlacementDirection() == BScreen::TopBottom) ? 1 : -1),
485     change_x =
486       ((screen->getRowPlacementDirection() == BScreen::LeftRight) ? 1 : -1),
487     delta_x = 8, delta_y = 8;
488
489   int test_x, test_y, place_x = 0, place_y = 0;
490   LinkedListIterator<OpenboxWindow> it(windowList);
491
492   Rect space(0, 0,
493              screen->getWidth(),
494              screen->getHeight()
495             );
496   Size window_size(win_w, win_h);
497
498   switch (screen->getPlacementPolicy()) {
499   case BScreen::BestFitPlacement: {
500     Point *spot = bestFitPlacement(window_size, space);
501     if (spot != NULL) {
502       place_x=spot->x();
503       place_y=spot->y();
504       delete spot;
505       placed=true;
506     }
507     break;
508   }
509   case BScreen::RowSmartPlacement: {
510     Point *spot=rowSmartPlacement(window_size, space);
511     if (spot != NULL) {
512       place_x=spot->x();
513       place_y=spot->y();
514       delete spot;
515       placed=true;
516     }
517     break;
518   }
519
520   case BScreen::ColSmartPlacement: {
521     test_x = (screen->getRowPlacementDirection() == BScreen::LeftRight) ?
522       start_pos : screen->getWidth() - win_w - start_pos;
523
524     while (!placed &&
525            ((screen->getRowPlacementDirection() == BScreen::RightLeft) ?
526             test_x > 0 : test_x + win_w < (signed) screen->getWidth())) {
527       test_y = (screen->getColPlacementDirection() == BScreen::TopBottom) ?
528         start_pos : screen->getHeight() - win_h - start_pos;
529       
530       while (!placed &&
531              ((screen->getColPlacementDirection() == BScreen::BottomTop) ?
532               test_y > 0 : test_y + win_h < (signed) screen->getHeight())) {
533         placed = True;
534
535         it.reset();
536         for (OpenboxWindow *curr = it.current(); placed && curr;
537              it++, curr = it.current()) {
538           if (curr->isMaximizedFull()) // fully maximized, ignore it
539             continue;
540           int curr_w = curr->getWidth() + (screen->getBorderWidth() * 4);
541           int curr_h =
542             ((curr->isShaded()) ? curr->getTitleHeight() : curr->getHeight()) +
543             (screen->getBorderWidth() * 4);
544
545           if (curr->getXFrame() < test_x + win_w &&
546               curr->getXFrame() + curr_w > test_x &&
547               curr->getYFrame() < test_y + win_h &&
548               curr->getYFrame() + curr_h > test_y) {
549             placed = False;
550           }
551         }
552
553         if (placed &&
554             (toolbar_x < test_x + win_w &&
555              toolbar_x + toolbar_w > test_x &&
556              toolbar_y < test_y + win_h &&
557              toolbar_y + toolbar_h > test_y)
558 #ifdef    SLIT
559             ||
560             (slit_x < test_x + win_w &&
561              slit_x + slit_w > test_x &&
562              slit_y < test_y + win_h &&
563              slit_y + slit_h > test_y)
564 #endif // SLIT
565             )
566           placed = False;
567
568         if (placed) {
569           place_x = test_x;
570           place_y = test_y;
571
572           break;
573         }
574
575         test_y += (change_y * delta_y);
576       }
577
578       test_x += (change_x * delta_x);
579     }
580
581     break;
582   }
583   } // switch
584
585   if (! placed) {
586     if (((unsigned) cascade_x > (screen->getWidth() / 2)) ||
587         ((unsigned) cascade_y > (screen->getHeight() / 2)))
588       cascade_x = cascade_y = 32;
589
590     place_x = cascade_x;
591     place_y = cascade_y;
592
593     cascade_x += win->getTitleHeight();
594     cascade_y += win->getTitleHeight();
595   }
596   
597   if (place_x + win_w > (signed) screen->getWidth())
598     place_x = (((signed) screen->getWidth()) - win_w) / 2;
599   if (place_y + win_h > (signed) screen->getHeight())
600     place_y = (((signed) screen->getHeight()) - win_h) / 2;
601
602   win->configure(place_x, place_y, win->getWidth(), win->getHeight());
603 }