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