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