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