]> icculus.org git repositories - dana/openbox.git/blob - src/Screen.cc
changing the number of workspaces in the rc does something without restarting now!
[dana/openbox.git] / src / Screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Screen.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 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 #include "../config.h"
25
26 extern "C" {
27 #include <X11/Xatom.h>
28 #include <X11/keysym.h>
29
30 #ifdef HAVE_STDLIB_H
31 #  include <stdlib.h>
32 #endif // HAVE_STDLIB_H
33
34 #ifdef HAVE_STRING_H
35 #  include <string.h>
36 #endif // HAVE_STRING_H
37
38 #ifdef    HAVE_CTYPE_H
39 #  include <ctype.h>
40 #endif // HAVE_CTYPE_H
41
42 #ifdef    HAVE_UNISTD_H
43 #  include <sys/types.h>
44 #  include <unistd.h>
45 #endif // HAVE_UNISTD_H
46
47 #ifdef    HAVE_DIRENT_H
48 #  include <dirent.h>
49 #endif // HAVE_DIRENT_H
50
51 #ifdef    HAVE_LOCALE_H
52 #  include <locale.h>
53 #endif // HAVE_LOCALE_H
54
55 #ifdef    HAVE_SYS_STAT_H
56 #  include <sys/stat.h>
57 #endif // HAVE_SYS_STAT_H
58
59 #ifdef    HAVE_STDARG_H
60 #  include <stdarg.h>
61 #endif // HAVE_STDARG_H
62 }
63
64 #include <assert.h>
65
66 #include <algorithm>
67 #include <functional>
68 #include <string>
69 using std::string;
70
71 #include "i18n.hh"
72 #include "blackbox.hh"
73 #include "Clientmenu.hh"
74 #include "Font.hh"
75 #include "GCCache.hh"
76 #include "Iconmenu.hh"
77 #include "Image.hh"
78 #include "Screen.hh"
79 #include "Slit.hh"
80 #include "Rootmenu.hh"
81 #include "Toolbar.hh"
82 #include "Util.hh"
83 #include "Window.hh"
84 #include "Workspace.hh"
85 #include "Workspacemenu.hh"
86 #include "XAtom.hh"
87
88 #ifndef   FONT_ELEMENT_SIZE
89 #define   FONT_ELEMENT_SIZE 50
90 #endif // FONT_ELEMENT_SIZE
91
92
93 static bool running = True;
94
95 static int anotherWMRunning(Display *display, XErrorEvent *) {
96   fprintf(stderr, i18n(ScreenSet, ScreenAnotherWMRunning,
97           "BScreen::BScreen: an error occured while querying the X server.\n"
98           "  another window manager already running on display %s.\n"),
99           DisplayString(display));
100
101   running = False;
102
103   return(-1);
104 }
105
106
107 BScreen::BScreen(Blackbox *bb, unsigned int scrn) : ScreenInfo(bb, scrn) {
108   blackbox = bb;
109   screenstr = "session.screen" + itostring(scrn) + '.';
110   config = blackbox->getConfig();
111   xatom = blackbox->getXAtom();
112
113   event_mask = ColormapChangeMask | EnterWindowMask | PropertyChangeMask |
114     SubstructureRedirectMask | ButtonPressMask | ButtonReleaseMask;
115
116   XErrorHandler old = XSetErrorHandler((XErrorHandler) anotherWMRunning);
117   XSelectInput(getBaseDisplay()->getXDisplay(), getRootWindow(), event_mask);
118   XSync(getBaseDisplay()->getXDisplay(), False);
119   XSetErrorHandler((XErrorHandler) old);
120
121   managed = running;
122   if (! managed) return;
123
124   fprintf(stderr, i18n(ScreenSet, ScreenManagingScreen,
125                        "BScreen::BScreen: managing screen %d "
126                        "using visual 0x%lx, depth %d\n"),
127           getScreenNumber(), XVisualIDFromVisual(getVisual()),
128           getDepth());
129
130   rootmenu = 0;
131
132   resource.mstyle.t_font = resource.mstyle.f_font = resource.tstyle.font =
133     resource.wstyle.font = (BFont *) 0;
134
135   geom_pixmap = None;
136
137   xatom->setSupported(this);    // set-up netwm support
138 #ifdef    HAVE_GETPID
139   xatom->setValue(getRootWindow(), XAtom::blackbox_pid, XAtom::cardinal,
140                   (unsigned long) getpid());
141 #endif // HAVE_GETPID
142   unsigned long geometry[] = { getWidth(),
143                                getHeight()};
144   xatom->setValue(getRootWindow(), XAtom::net_desktop_geometry,
145                   XAtom::cardinal, geometry, 2);
146   unsigned long viewport[] = {0,0};
147   xatom->setValue(getRootWindow(), XAtom::net_desktop_viewport,
148                   XAtom::cardinal, viewport, 2);
149                   
150
151   XDefineCursor(blackbox->getXDisplay(), getRootWindow(),
152                 blackbox->getSessionCursor());
153
154   // start off full screen, top left.
155   usableArea.setSize(getWidth(), getHeight());
156
157   image_control =
158     new BImageControl(blackbox, this, True, blackbox->getColorsPerChannel(),
159                       blackbox->getCacheLife(), blackbox->getCacheMax());
160   image_control->installRootColormap();
161   root_colormap_installed = True;
162
163   load_rc();
164   LoadStyle();
165
166   XGCValues gcv;
167   gcv.foreground = WhitePixel(blackbox->getXDisplay(), getScreenNumber())
168     ^ BlackPixel(blackbox->getXDisplay(), getScreenNumber());
169   gcv.function = GXxor;
170   gcv.subwindow_mode = IncludeInferiors;
171   opGC = XCreateGC(blackbox->getXDisplay(), getRootWindow(),
172                    GCForeground | GCFunction | GCSubwindowMode, &gcv);
173
174   const char *s =  i18n(ScreenSet, ScreenPositionLength,
175                         "0: 0000 x 0: 0000");
176   geom_w = resource.wstyle.font->measureString(s) + resource.bevel_width * 2;
177   geom_h = resource.wstyle.font->height() + resource.bevel_width * 2;
178
179   XSetWindowAttributes attrib;
180   unsigned long mask = CWBorderPixel | CWColormap | CWSaveUnder;
181   attrib.border_pixel = getBorderColor()->pixel();
182   attrib.colormap = getColormap();
183   attrib.save_under = True;
184
185   geom_window = XCreateWindow(blackbox->getXDisplay(), getRootWindow(),
186                               0, 0, geom_w, geom_h, resource.border_width,
187                               getDepth(), InputOutput, getVisual(),
188                               mask, &attrib);
189   geom_visible = False;
190
191   BTexture* texture = &(resource.wstyle.l_focus);
192   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
193   if (geom_pixmap == ParentRelative) {
194     texture = &(resource.wstyle.t_focus);
195     geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
196   }
197   if (! geom_pixmap)
198     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
199                          texture->color().pixel());
200   else
201     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
202                                geom_window, geom_pixmap);
203
204   workspacemenu = new Workspacemenu(this);
205   iconmenu = new Iconmenu(this);
206   configmenu = new Configmenu(this);
207
208   if (resource.workspaces != 0) {
209     for (unsigned int i = 0; i < resource.workspaces; ++i) {
210       Workspace *wkspc = new Workspace(this, workspacesList.size());
211       workspacesList.push_back(wkspc);
212       workspacemenu->insertWorkspace(wkspc);
213       workspacemenu->update();
214
215     }
216   } else {
217     Workspace *wkspc = new Workspace(this, workspacesList.size());
218     workspacesList.push_back(wkspc);
219     workspacemenu->insertWorkspace(wkspc);
220     workspacemenu->update();
221   }
222   saveWorkspaceNames();
223
224   updateNetizenWorkspaceCount();
225
226   workspacemenu->insert(i18n(IconSet, IconIcons, "Icons"), iconmenu);
227   workspacemenu->update();
228
229   current_workspace = workspacesList.front();
230   
231   xatom->setValue(getRootWindow(), XAtom::net_current_desktop,
232                   XAtom::cardinal, 0); //first workspace
233
234   workspacemenu->setItemSelected(2, True);
235
236   toolbar = new Toolbar(this);
237
238   slit = new Slit(this);
239
240   InitMenu();
241
242   raiseWindows(0, 0);     // this also initializes the empty stacking list
243   rootmenu->update();
244
245   updateClientList();     // initialize the client lists, which will be empty
246   updateAvailableArea();
247
248   changeWorkspaceID(0);
249
250   unsigned int i, j, nchild;
251   Window r, p, *children;
252   XQueryTree(blackbox->getXDisplay(), getRootWindow(), &r, &p,
253              &children, &nchild);
254
255   // preen the window list of all icon windows... for better dockapp support
256   for (i = 0; i < nchild; i++) {
257     if (children[i] == None) continue;
258
259     XWMHints *wmhints = XGetWMHints(blackbox->getXDisplay(),
260                                     children[i]);
261
262     if (wmhints) {
263       if ((wmhints->flags & IconWindowHint) &&
264           (wmhints->icon_window != children[i])) {
265         for (j = 0; j < nchild; j++) {
266           if (children[j] == wmhints->icon_window) {
267             children[j] = None;
268             break;
269           }
270         }
271       }
272
273       XFree(wmhints);
274     }
275   }
276
277   // manage shown windows
278   for (i = 0; i < nchild; ++i) {
279     if (children[i] == None || (! blackbox->validateWindow(children[i])))
280       continue;
281
282     XWindowAttributes attrib;
283     if (XGetWindowAttributes(blackbox->getXDisplay(), children[i], &attrib)) {
284       if (attrib.override_redirect) continue;
285
286       if (attrib.map_state != IsUnmapped) {
287         manageWindow(children[i]);
288       }
289     }
290   }
291
292   XFree(children);
293
294   // call this again just in case a window we found updates the Strut list
295   updateAvailableArea();
296 }
297
298
299 BScreen::~BScreen(void) {
300   if (! managed) return;
301
302   if (geom_pixmap != None)
303     image_control->removeImage(geom_pixmap);
304
305   if (geom_window != None)
306     XDestroyWindow(blackbox->getXDisplay(), geom_window);
307
308   std::for_each(workspacesList.begin(), workspacesList.end(),
309                 PointerAssassin());
310
311   std::for_each(iconList.begin(), iconList.end(), PointerAssassin());
312
313   std::for_each(netizenList.begin(), netizenList.end(), PointerAssassin());
314
315   while (! systrayWindowList.empty())
316     removeSystrayWindow(systrayWindowList[0]);
317
318   delete rootmenu;
319   delete workspacemenu;
320   delete iconmenu;
321   delete configmenu;
322   delete slit;
323   delete toolbar;
324   delete image_control;
325
326   if (resource.wstyle.font)
327     delete resource.wstyle.font;
328   if (resource.mstyle.t_font)
329     delete resource.mstyle.t_font;
330   if (resource.mstyle.f_font)
331     delete resource.mstyle.f_font;
332   if (resource.tstyle.font)
333     delete resource.tstyle.font;
334
335   XFreeGC(blackbox->getXDisplay(), opGC);
336 }
337
338
339 void BScreen::saveSloppyFocus(bool s) {
340   resource.sloppy_focus = s;
341
342   string fmodel;
343   if (resource.sloppy_focus) {
344     fmodel = "SloppyFocus";
345     if (resource.auto_raise) fmodel += " AutoRaise";
346     if (resource.click_raise) fmodel += " ClickRaise";
347   } else {
348     fmodel = "ClickToFocus";
349   }
350   config->setValue(screenstr + "focusModel", fmodel);
351 }
352
353
354 void BScreen::saveAutoRaise(bool a) {
355   resource.auto_raise = a;
356   saveSloppyFocus(resource.sloppy_focus);
357 }
358
359
360 void BScreen::saveClickRaise(bool c) {
361   resource.click_raise = c;
362   saveSloppyFocus(resource.sloppy_focus);
363 }
364
365
366 void BScreen::saveImageDither(bool d) {
367   image_control->setDither(d);
368   config->setValue(screenstr + "imageDither", doImageDither());
369 }
370
371
372 void BScreen::saveOpaqueMove(bool o) {
373   resource.opaque_move = o;
374   config->setValue(screenstr + "opaqueMove", resource.opaque_move);
375 }
376
377
378 void BScreen::saveFullMax(bool f) {
379   resource.full_max = f;
380   config->setValue(screenstr + "fullMaximization", resource.full_max);
381 }
382
383
384 void BScreen::saveFocusNew(bool f) {
385   resource.focus_new = f;
386   config->setValue(screenstr + "focusNewWindows", resource.focus_new);
387 }
388
389
390 void BScreen::saveFocusLast(bool f) {
391   resource.focus_last = f;
392   config->setValue(screenstr + "focusLastWindow", resource.focus_last);
393 }
394
395
396 void BScreen::saveAAFonts(bool f) {
397   resource.aa_fonts = f;
398   reconfigure();
399   config->setValue(screenstr + "antialiasFonts", resource.aa_fonts);
400 }
401
402
403 void BScreen::saveHideToolbar(bool h) {
404   resource.hide_toolbar = h;
405   if (resource.hide_toolbar)
406     toolbar->unmapToolbar();
407   else
408     toolbar->mapToolbar();
409   config->setValue(screenstr + "hideToolbar", resource.hide_toolbar);
410 }
411
412
413 void BScreen::saveWindowToWindowSnap(bool s) {
414   resource.window_to_window_snap = s;
415   config->setValue(screenstr + "windowToWindowSnap",
416                    resource.window_to_window_snap);
417 }
418
419
420 void BScreen::saveResizeZones(unsigned int z) {
421   resource.resize_zones = z;
422   config->setValue(screenstr + "resizeZones", resource.resize_zones);
423 }
424
425
426 void BScreen::saveWindowCornerSnap(bool s) {
427   resource.window_corner_snap = s;
428   config->setValue(screenstr + "windowCornerSnap",
429                    resource.window_corner_snap);
430 }
431
432
433 void BScreen::saveWorkspaces(unsigned int w) {
434   resource.workspaces = w;
435   config->setValue(screenstr + "workspaces", resource.workspaces);
436 }
437
438
439 void BScreen::savePlacementPolicy(int p) {
440   resource.placement_policy = p; 
441   const char *placement;
442   switch (resource.placement_policy) {
443   case CascadePlacement: placement = "CascadePlacement"; break;
444   case UnderMousePlacement: placement = "UnderMousePlacement"; break;
445   case ClickMousePlacement: placement = "ClickMousePlacement"; break;
446   case ColSmartPlacement: placement = "ColSmartPlacement"; break;
447   case RowSmartPlacement: default: placement = "RowSmartPlacement"; break;
448   }
449   config->setValue(screenstr + "windowPlacement", placement);
450 }
451
452
453 void BScreen::saveEdgeSnapThreshold(int t) {
454   resource.edge_snap_threshold = t;
455   config->setValue(screenstr + "edgeSnapThreshold",
456                    resource.edge_snap_threshold);
457 }
458
459
460 void BScreen::saveRowPlacementDirection(int d) {
461   resource.row_direction = d;
462   config->setValue(screenstr + "rowPlacementDirection",
463                    resource.row_direction == LeftRight ?
464                    "LeftToRight" : "RightToLeft");
465 }
466
467
468 void BScreen::saveColPlacementDirection(int d) {
469   resource.col_direction = d;
470   config->setValue(screenstr + "colPlacementDirection",
471                    resource.col_direction == TopBottom ?
472                    "TopToBottom" : "BottomToTop");
473 }
474
475
476 #ifdef    HAVE_STRFTIME
477 void BScreen::saveStrftimeFormat(const std::string& format) {
478   resource.strftime_format = format;
479   config->setValue(screenstr + "strftimeFormat", resource.strftime_format);
480 }
481
482 #else // !HAVE_STRFTIME
483
484 void BScreen::saveDateFormat(int f) {
485   resource.date_format = f;
486   config->setValue(screenstr + "dateFormat",
487                    resource.date_format == B_EuropeanDate ?
488                    "European" : "American");
489 }
490
491
492 void BScreen::saveClock24Hour(bool c) {
493   resource.clock24hour = c;
494   config->setValue(screenstr + "clockFormat", resource.clock24hour ? 24 : 12);
495 }
496 #endif // HAVE_STRFTIME
497
498
499 void BScreen::saveWorkspaceNames() {
500   string names;
501  
502   for (unsigned int i = 0; i < workspacesList.size(); ++i) {
503     names += workspacesList[i]->getName();
504     if (i < workspacesList.size() - 1)
505       names += ',';
506   }
507
508   config->setValue(screenstr + "workspaceNames", names);
509 }
510
511
512 void BScreen::savePlaceIgnoreShaded(bool i) {
513   resource.ignore_shaded = i;
514   config->setValue(screenstr + "placementIgnoreShaded",
515                    resource.ignore_shaded);
516 }
517
518
519 void BScreen::savePlaceIgnoreMaximized(bool i) {
520   resource.ignore_maximized = i;
521   config->setValue(screenstr + "placementIgnoreMaximized",
522                    resource.ignore_maximized);
523 }
524
525
526 void BScreen::save_rc(void) {
527   saveSloppyFocus(resource.sloppy_focus);
528   saveAutoRaise(resource.auto_raise);
529   saveImageDither(doImageDither());
530   saveAAFonts(resource.aa_fonts);
531   saveResizeZones(resource.resize_zones);
532   saveOpaqueMove(resource.opaque_move);
533   saveFullMax(resource.full_max);
534   saveFocusNew(resource.focus_new);
535   saveFocusLast(resource.focus_last);
536   saveHideToolbar(resource.hide_toolbar);
537   saveWindowToWindowSnap(resource.window_to_window_snap);
538   saveWindowCornerSnap(resource.window_corner_snap);
539   saveWorkspaces(resource.workspaces);
540   savePlacementPolicy(resource.placement_policy);
541   saveEdgeSnapThreshold(resource.edge_snap_threshold);
542   saveRowPlacementDirection(resource.row_direction);
543   saveColPlacementDirection(resource.col_direction);
544 #ifdef    HAVE_STRFTIME
545   saveStrftimeFormat(resource.strftime_format); 
546 #else // !HAVE_STRFTIME
547   saveDateFormat(resource.date_format);
548   savwClock24Hour(resource.clock24hour);
549 #endif // HAVE_STRFTIME
550   savePlaceIgnoreShaded(resource.ignore_shaded);
551   savePlaceIgnoreMaximized(resource.ignore_maximized);
552
553   toolbar->save_rc();
554   slit->save_rc();
555 }
556
557
558 void BScreen::load_rc(void) {
559   std::string s;
560   bool b;
561
562   if (! config->getValue(screenstr + "fullMaximization", resource.full_max))
563     resource.full_max = false;
564
565   if (! config->getValue(screenstr + "focusNewWindows", resource.focus_new))
566     resource.focus_new = false;
567
568   if (! config->getValue(screenstr + "focusLastWindow", resource.focus_last))
569     resource.focus_last = false;
570
571   if (! config->getValue(screenstr + "workspaces", resource.workspaces))
572     resource.workspaces = 1;
573
574   if (! config->getValue(screenstr + "opaqueMove", resource.opaque_move))
575     resource.opaque_move = false;
576
577   if (! config->getValue(screenstr + "antialiasFonts", resource.aa_fonts))
578     resource.aa_fonts = true;
579
580   if (! config->getValue(screenstr + "resizeZones", resource.resize_zones) ||
581       (resource.resize_zones != 1 && resource.resize_zones != 2 &&
582        resource.resize_zones != 4))
583       resource.resize_zones = 4;
584
585   if (! config->getValue(screenstr + "hideToolbar", resource.hide_toolbar))
586     resource.hide_toolbar = false;
587
588   if (! config->getValue(screenstr + "windowToWindowSnap",
589                          resource.window_to_window_snap))
590     resource.window_to_window_snap = true;
591
592   if (! config->getValue(screenstr + "windowCornerSnap",
593                          resource.window_corner_snap))
594     resource.window_corner_snap = true;
595
596   if (! config->getValue(screenstr + "imageDither", b))
597     b = true;
598   image_control->setDither(b);
599
600   if (! config->getValue(screenstr + "edgeSnapThreshold",
601                         resource.edge_snap_threshold))
602     resource.edge_snap_threshold = 4;
603   
604   if (config->getValue(screenstr + "rowPlacementDirection", s) &&
605       s == "RightToLeft")
606     resource.row_direction = RightLeft;
607   else
608     resource.row_direction = LeftRight;
609
610   if (config->getValue(screenstr + "colPlacementDirection", s) &&
611       s == "BottomToTop")
612     resource.col_direction = BottomTop;
613   else
614     resource.col_direction = TopBottom;
615
616   if (config->getValue(screenstr + "workspaceNames", s)) {
617     XAtom::StringVect workspaceNames;
618
619     string::const_iterator it = s.begin(), end = s.end();
620     while(1) {
621       string::const_iterator tmp = it;     // current string.begin()
622       it = std::find(tmp, end, ',');       // look for comma between tmp and end
623       workspaceNames.push_back(string(tmp, it)); // s[tmp:it]
624       if (it == end)
625         break;
626       ++it;
627     }
628
629     xatom->setValue(getRootWindow(), XAtom::net_desktop_names, XAtom::utf8,
630                     workspaceNames);
631   }
632
633   resource.sloppy_focus = true;
634   resource.auto_raise = false;
635   resource.click_raise = false;
636   if (config->getValue(screenstr + "focusModel", s)) {
637     if (s.find("ClickToFocus") != string::npos) {
638       resource.sloppy_focus = false;
639     } else {
640       // must be sloppy
641       if (s.find("AutoRaise") != string::npos)
642         resource.auto_raise = true;
643       if (s.find("ClickRaise") != string::npos)
644         resource.click_raise = true;
645     }
646   }
647
648   if (config->getValue(screenstr + "windowPlacement", s)) {
649     if (s == "CascadePlacement")
650       resource.placement_policy = CascadePlacement;
651     else if (s == "UnderMousePlacement")
652       resource.placement_policy = UnderMousePlacement;
653     else if (s == "ClickMousePlacement")
654       resource.placement_policy = ClickMousePlacement;
655     else if (s == "ColSmartPlacement")
656       resource.placement_policy = ColSmartPlacement;
657     else //if (s == "RowSmartPlacement")
658       resource.placement_policy = RowSmartPlacement;
659   } else
660     resource.placement_policy = RowSmartPlacement;
661
662 #ifdef    HAVE_STRFTIME
663   if (! config->getValue(screenstr + "strftimeFormat",
664                          resource.strftime_format))
665     resource.strftime_format = "%I:%M %p";
666 #else // !HAVE_STRFTIME
667   long l;
668
669   if (config->getValue(screenstr + "dateFormat", s) && s == "European")
670     resource.date_format = B_EuropeanDate;
671  else
672     resource.date_format = B_AmericanDate;
673
674   if (! config->getValue(screenstr + "clockFormat", l))
675     l = 12;
676   resource.clock24hour = l == 24;
677 #endif // HAVE_STRFTIME
678   
679   if (! config->getValue(screenstr + "placementIgnoreShaded",
680                          resource.ignore_shaded))
681     resource.ignore_shaded = true;
682
683   if (! config->getValue(screenstr + "placementIgnoreMaximized",
684                          resource.ignore_maximized))
685     resource.ignore_maximized = true;
686 }
687
688
689 void BScreen::changeWorkspaceCount(unsigned int new_count) {
690   assert(new_count > 0);
691
692   if (new_count < workspacesList.size()) {
693     // shrink
694     for (unsigned int i = workspacesList.size(); i > new_count; --i)
695       removeLastWorkspace();
696     // removeLast already sets the current workspace to the 
697     // last available one.
698   } else if (new_count > workspacesList.size()) {
699     // grow
700     for(unsigned int i = workspacesList.size(); i < new_count; ++i)
701       addWorkspace();
702   }
703 }
704
705
706 void BScreen::reconfigure(void) {
707   // don't reconfigure while saving the initial rc file, it's a waste and it
708   // breaks somethings (workspace names)
709   if (blackbox->isStartup()) return;
710
711   load_rc();
712   toolbar->load_rc();
713   slit->load_rc();
714   LoadStyle();
715
716   // we need to do this explicitly, because just loading this value from the rc
717   // does nothing
718   changeWorkspaceCount(resource.workspaces);
719
720   XGCValues gcv;
721   gcv.foreground = WhitePixel(blackbox->getXDisplay(),
722                               getScreenNumber());
723   gcv.function = GXinvert;
724   gcv.subwindow_mode = IncludeInferiors;
725   XChangeGC(blackbox->getXDisplay(), opGC,
726             GCForeground | GCFunction | GCSubwindowMode, &gcv);
727
728   const char *s = i18n(ScreenSet, ScreenPositionLength,
729                        "0: 0000 x 0: 0000");
730
731   geom_w = resource.wstyle.font->measureString(s) + resource.bevel_width * 2;
732   geom_h = resource.wstyle.font->height() + resource.bevel_width * 2;
733
734   BTexture* texture = &(resource.wstyle.l_focus);
735   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
736   if (geom_pixmap == ParentRelative) {
737     texture = &(resource.wstyle.t_focus);
738     geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
739   }
740   if (! geom_pixmap)
741     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
742                          texture->color().pixel());
743   else
744     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
745                                geom_window, geom_pixmap);
746
747   XSetWindowBorderWidth(blackbox->getXDisplay(), geom_window,
748                         resource.border_width);
749   XSetWindowBorder(blackbox->getXDisplay(), geom_window,
750                    resource.border_color.pixel());
751
752   workspacemenu->reconfigure();
753   iconmenu->reconfigure();
754
755   typedef std::vector<int> SubList;
756   SubList remember_subs;
757
758   // save the current open menus
759   Basemenu *menu = rootmenu;
760   int submenu;
761   while ((submenu = menu->getCurrentSubmenu()) >= 0) {
762     remember_subs.push_back(submenu);
763     menu = menu->find(submenu)->submenu();
764     assert(menu);
765   }
766   
767   InitMenu();
768   raiseWindows(0, 0);
769   rootmenu->reconfigure();
770
771   // reopen the saved menus
772   menu = rootmenu;
773   const SubList::iterator subs_end = remember_subs.end();
774   for (SubList::iterator it = remember_subs.begin(); it != subs_end; ++it) {
775     menu->drawSubmenu(*it);
776     menu = menu->find(*it)->submenu();
777     if (! menu)
778       break;
779   }
780
781   configmenu->reconfigure();
782
783   toolbar->reconfigure();
784
785   slit->reconfigure();
786
787   std::for_each(workspacesList.begin(), workspacesList.end(),
788                 std::mem_fun(&Workspace::reconfigure));
789
790   BlackboxWindowList::iterator iit = iconList.begin();
791   for (; iit != iconList.end(); ++iit) {
792     BlackboxWindow *bw = *iit;
793     if (bw->validateClient())
794       bw->reconfigure();
795   }
796
797   image_control->timeout();
798 }
799
800
801 void BScreen::rereadMenu(void) {
802   InitMenu();
803   raiseWindows(0, 0);
804
805   rootmenu->reconfigure();
806 }
807
808
809 void BScreen::LoadStyle(void) {
810   Configuration style(False);
811
812   const char *sfile = blackbox->getStyleFilename();
813   if (sfile != NULL) {
814     style.setFile(sfile);
815     if (! style.load()) {
816       style.setFile(DEFAULTSTYLE);
817       if (! style.load())
818         style.create();  // hardcoded default values will be used.
819     }
820   }
821
822   // merge in the rc file
823   style.merge(config->file(), True);
824
825   string s;
826
827   // load fonts/fontsets
828   if (resource.wstyle.font)
829     delete resource.wstyle.font;
830   if (resource.tstyle.font)
831     delete resource.tstyle.font;
832   if (resource.mstyle.f_font)
833     delete resource.mstyle.f_font;
834   if (resource.mstyle.t_font)
835     delete resource.mstyle.t_font;
836   resource.wstyle.font = resource.tstyle.font = resource.mstyle.f_font =
837     resource.mstyle.t_font = (BFont *) 0;
838
839   resource.wstyle.font = readDatabaseFont("window.", style);
840   resource.tstyle.font = readDatabaseFont("toolbar.", style);
841   resource.mstyle.t_font = readDatabaseFont("menu.title.", style);
842   resource.mstyle.f_font = readDatabaseFont("menu.frame.", style);
843
844   // load window config
845   resource.wstyle.t_focus =
846     readDatabaseTexture("window.title.focus", "white", style);
847   resource.wstyle.t_unfocus =
848     readDatabaseTexture("window.title.unfocus", "black", style);
849   resource.wstyle.l_focus =
850     readDatabaseTexture("window.label.focus", "white", style);
851   resource.wstyle.l_unfocus =
852     readDatabaseTexture("window.label.unfocus", "black", style);
853   resource.wstyle.h_focus =
854     readDatabaseTexture("window.handle.focus", "white", style);
855   resource.wstyle.h_unfocus =
856     readDatabaseTexture("window.handle.unfocus", "black", style);
857   resource.wstyle.g_focus =
858     readDatabaseTexture("window.grip.focus", "white", style);
859   resource.wstyle.g_unfocus =
860     readDatabaseTexture("window.grip.unfocus", "black", style);
861   resource.wstyle.b_focus =
862     readDatabaseTexture("window.button.focus", "white", style);
863   resource.wstyle.b_unfocus =
864     readDatabaseTexture("window.button.unfocus", "black", style);
865   resource.wstyle.b_pressed =
866     readDatabaseTexture("window.button.pressed", "black", style);
867   resource.wstyle.f_focus =
868     readDatabaseColor("window.frame.focusColor", "white", style);
869   resource.wstyle.f_unfocus =
870     readDatabaseColor("window.frame.unfocusColor", "black", style);
871   resource.wstyle.l_text_focus =
872     readDatabaseColor("window.label.focus.textColor", "black", style);
873   resource.wstyle.l_text_unfocus =
874     readDatabaseColor("window.label.unfocus.textColor", "white", style);
875   resource.wstyle.b_pic_focus =
876     readDatabaseColor("window.button.focus.picColor", "black", style);
877   resource.wstyle.b_pic_unfocus =
878     readDatabaseColor("window.button.unfocus.picColor", "white", style);
879
880   resource.wstyle.justify = LeftJustify;
881   if (style.getValue("window.justify", s)) {
882     if (s == "right" || s == "Right")
883       resource.wstyle.justify = RightJustify;
884     else if (s == "center" || s == "Center")
885       resource.wstyle.justify = CenterJustify;
886   }
887
888   // load toolbar config
889   resource.tstyle.toolbar =
890     readDatabaseTexture("toolbar", "black", style);
891   resource.tstyle.label =
892     readDatabaseTexture("toolbar.label", "black", style);
893   resource.tstyle.window =
894     readDatabaseTexture("toolbar.windowLabel", "black", style);
895   resource.tstyle.button =
896     readDatabaseTexture("toolbar.button", "white", style);
897   resource.tstyle.pressed =
898     readDatabaseTexture("toolbar.button.pressed", "black", style);
899   resource.tstyle.clock =
900     readDatabaseTexture("toolbar.clock", "black", style);
901   resource.tstyle.l_text =
902     readDatabaseColor("toolbar.label.textColor", "white", style);
903   resource.tstyle.w_text =
904     readDatabaseColor("toolbar.windowLabel.textColor", "white", style);
905   resource.tstyle.c_text =
906     readDatabaseColor("toolbar.clock.textColor", "white", style);
907   resource.tstyle.b_pic =
908     readDatabaseColor("toolbar.button.picColor", "black", style);
909
910   resource.tstyle.justify = LeftJustify;
911   if (style.getValue("toolbar.justify", s)) {
912     if (s == "right" || s == "Right")
913       resource.tstyle.justify = RightJustify;
914     else if (s == "center" || s == "Center")
915       resource.tstyle.justify = CenterJustify;
916   }
917
918   // load menu config
919   resource.mstyle.title =
920     readDatabaseTexture("menu.title", "white", style);
921   resource.mstyle.frame =
922     readDatabaseTexture("menu.frame", "black", style);
923   resource.mstyle.hilite =
924     readDatabaseTexture("menu.hilite", "white", style);
925   resource.mstyle.t_text =
926     readDatabaseColor("menu.title.textColor", "black", style);
927   resource.mstyle.f_text =
928     readDatabaseColor("menu.frame.textColor", "white", style);
929   resource.mstyle.d_text =
930     readDatabaseColor("menu.frame.disableColor", "black", style);
931   resource.mstyle.h_text =
932     readDatabaseColor("menu.hilite.textColor", "black", style);
933
934   resource.mstyle.t_justify = LeftJustify;
935   if (style.getValue("menu.title.justify", s)) {
936     if (s == "right" || s == "Right")
937       resource.mstyle.t_justify = RightJustify;
938     else if (s == "center" || s == "Center")
939       resource.mstyle.t_justify = CenterJustify;
940   }
941
942   resource.mstyle.f_justify = LeftJustify;
943   if (style.getValue("menu.frame.justify", s)) {
944     if (s == "right" || s == "Right")
945       resource.mstyle.f_justify = RightJustify;
946     else if (s == "center" || s == "Center")
947       resource.mstyle.f_justify = CenterJustify;
948   }
949
950   resource.mstyle.bullet = Basemenu::Triangle;
951   if (style.getValue("menu.bullet", s)) {
952     if (s == "empty" || s == "Empty")
953       resource.mstyle.bullet = Basemenu::Empty;
954     else if (s == "square" || s == "Square")
955       resource.mstyle.bullet = Basemenu::Square;
956     else if (s == "diamond" || s == "Diamond")
957       resource.mstyle.bullet = Basemenu::Diamond;
958   }
959
960   resource.mstyle.bullet_pos = Basemenu::Left;
961   if (style.getValue("menu.bullet.position", s)) {
962     if (s == "right" || s == "Right")
963       resource.mstyle.bullet_pos = Basemenu::Right;
964   }
965
966   resource.border_color =
967     readDatabaseColor("borderColor", "black", style);
968
969   // load bevel, border and handle widths
970   if (! style.getValue("handleWidth", resource.handle_width) ||
971       resource.handle_width > (getWidth() / 2) || resource.handle_width == 0)
972     resource.handle_width = 6;
973
974   if (! style.getValue("borderWidth", resource.border_width))
975     resource.border_width = 1;
976
977   if (! style.getValue("bevelWidth", resource.bevel_width) ||
978       resource.bevel_width > (getWidth() / 2) || resource.bevel_width == 0)
979     resource.bevel_width = 3;
980
981   if (! style.getValue("frameWidth", resource.frame_width) ||
982       resource.frame_width > (getWidth() / 2))
983     resource.frame_width = resource.bevel_width;
984
985   if (style.getValue("rootCommand", s))
986     bexec(s, displayString());
987 }
988
989
990 void BScreen::addIcon(BlackboxWindow *w) {
991   if (! w) return;
992
993   w->setWorkspace(BSENTINEL);
994   w->setWindowNumber(iconList.size());
995
996   iconList.push_back(w);
997
998   const char* title = w->getIconTitle();
999   iconmenu->insert(title);
1000   iconmenu->update();
1001 }
1002
1003
1004 void BScreen::removeIcon(BlackboxWindow *w) {
1005   if (! w) return;
1006
1007   iconList.remove(w);
1008
1009   iconmenu->remove(w->getWindowNumber());
1010   iconmenu->update();
1011
1012   BlackboxWindowList::iterator it = iconList.begin(),
1013     end = iconList.end();
1014   for (int i = 0; it != end; ++it)
1015     (*it)->setWindowNumber(i++);
1016 }
1017
1018
1019 BlackboxWindow *BScreen::getIcon(unsigned int index) {
1020   if (index < iconList.size()) {
1021     BlackboxWindowList::iterator it = iconList.begin();
1022     for (; index > 0; --index, ++it) ; /* increment to index */
1023     return *it;
1024   }
1025
1026   return (BlackboxWindow *) 0;
1027 }
1028
1029
1030 unsigned int BScreen::addWorkspace(void) {
1031   Workspace *wkspc = new Workspace(this, workspacesList.size());
1032   workspacesList.push_back(wkspc);
1033   saveWorkspaces(getWorkspaceCount());
1034   saveWorkspaceNames();
1035
1036   workspacemenu->insertWorkspace(wkspc);
1037   workspacemenu->update();
1038
1039   toolbar->reconfigure();
1040
1041   updateNetizenWorkspaceCount();
1042
1043   return workspacesList.size();
1044 }
1045
1046
1047 unsigned int BScreen::removeLastWorkspace(void) {
1048   if (workspacesList.size() == 1)
1049     return 1;
1050
1051   Workspace *wkspc = workspacesList.back();
1052
1053   if (current_workspace->getID() == wkspc->getID())
1054     changeWorkspaceID(current_workspace->getID() - 1);
1055
1056   wkspc->removeAll();
1057
1058   workspacemenu->removeWorkspace(wkspc);
1059   workspacemenu->update();
1060
1061   workspacesList.pop_back();
1062   delete wkspc;
1063
1064   saveWorkspaces(getWorkspaceCount());
1065   saveWorkspaceNames();
1066
1067   toolbar->reconfigure();
1068
1069   updateNetizenWorkspaceCount();
1070
1071   return workspacesList.size();
1072 }
1073
1074
1075 void BScreen::changeWorkspaceID(unsigned int id) {
1076   if (! current_workspace || id == current_workspace->getID()) return;
1077
1078   BlackboxWindow *focused = blackbox->getFocusedWindow();
1079   if (focused && focused->getScreen() == this) {
1080     assert(focused->isStuck() ||
1081            focused->getWorkspaceNumber() == current_workspace->getID());
1082
1083     current_workspace->setLastFocusedWindow(focused);
1084   } else {
1085     // if no window had focus, no need to store a last focus
1086     current_workspace->setLastFocusedWindow((BlackboxWindow *) 0);
1087   }
1088
1089   // when we switch workspaces, unfocus whatever was focused
1090   blackbox->setFocusedWindow((BlackboxWindow *) 0);
1091     
1092   current_workspace->hideAll();
1093   workspacemenu->setItemSelected(current_workspace->getID() + 2, False);
1094
1095   current_workspace = getWorkspace(id);
1096
1097   xatom->setValue(getRootWindow(), XAtom::net_current_desktop,
1098                   XAtom::cardinal, id);
1099
1100   workspacemenu->setItemSelected(current_workspace->getID() + 2, True);
1101   toolbar->redrawWorkspaceLabel(True);
1102
1103   current_workspace->showAll();
1104
1105   if (resource.focus_last && current_workspace->getLastFocusedWindow()) {
1106     XSync(blackbox->getXDisplay(), False);
1107     current_workspace->getLastFocusedWindow()->setInputFocus();
1108   }
1109
1110   updateNetizenCurrentWorkspace();
1111 }
1112
1113
1114 /*
1115  * Set the _NET_CLIENT_LIST root window property.
1116  */
1117 void BScreen::updateClientList(void) {
1118   if (windowList.size() > 0) {
1119     Window *windows = new Window[windowList.size()];
1120     Window *win_it = windows;
1121     BlackboxWindowList::iterator it = windowList.begin();
1122     const BlackboxWindowList::iterator end = windowList.end();
1123     for (; it != end; ++it, ++win_it)
1124       *win_it = (*it)->getClientWindow();
1125     xatom->setValue(getRootWindow(), XAtom::net_client_list, XAtom::window,
1126                     windows, windowList.size());
1127     delete [] windows;
1128   } else
1129     xatom->setValue(getRootWindow(), XAtom::net_client_list, XAtom::window,
1130                     0, 0);
1131
1132   updateStackingList();
1133 }
1134
1135
1136 /*
1137  * Set the _NET_CLIENT_LIST_STACKING root window property.
1138  */
1139 void BScreen::updateStackingList(void) {
1140
1141   BlackboxWindowList stack_order;
1142
1143   /*
1144    * Get the stacking order from all of the workspaces.
1145    * We start with the current workspace so that the sticky windows will be
1146    * in the right order on the current workspace.
1147    * XXX: Do we need to have sticky windows in the list once for each workspace?
1148    */
1149   getCurrentWorkspace()->appendStackOrder(stack_order);
1150   for (unsigned int i = 0; i < getWorkspaceCount(); ++i)
1151     if (i != getCurrentWorkspaceID())
1152       getWorkspace(i)->appendStackOrder(stack_order);
1153
1154   if (stack_order.size() > 0) {
1155     // set the client list atoms
1156     Window *windows = new Window[stack_order.size()];
1157     Window *win_it = windows;
1158     BlackboxWindowList::iterator it = stack_order.begin(),
1159                                  end = stack_order.end();
1160     for (; it != end; ++it, ++win_it)
1161       *win_it = (*it)->getClientWindow();
1162     xatom->setValue(getRootWindow(), XAtom::net_client_list_stacking,
1163                     XAtom::window, windows, stack_order.size());
1164     delete [] windows;
1165   } else
1166     xatom->setValue(getRootWindow(), XAtom::net_client_list_stacking,
1167                     XAtom::window, 0, 0);
1168 }
1169
1170
1171 void BScreen::addSystrayWindow(Window window) {
1172   systrayWindowList.push_back(window);
1173   xatom->setValue(getRootWindow(), XAtom::kde_net_system_tray_windows,
1174                   XAtom::window,
1175                   &systrayWindowList[0], systrayWindowList.size());
1176   blackbox->saveSystrayWindowSearch(window, this);
1177 }
1178
1179
1180 void BScreen::removeSystrayWindow(Window window) {
1181   WindowList::iterator it = systrayWindowList.begin();
1182   const WindowList::iterator end = systrayWindowList.end();
1183   for (; it != end; ++it)
1184     if (*it == window) {
1185       systrayWindowList.erase(it);
1186       xatom->setValue(getRootWindow(), XAtom::kde_net_system_tray_windows,
1187                       XAtom::window,
1188                       &systrayWindowList[0], systrayWindowList.size());
1189       blackbox->removeSystrayWindowSearch(window);
1190       break;
1191     }
1192 }
1193
1194
1195 void BScreen::manageWindow(Window w) {
1196   // is the window a KDE systray window?
1197   Window systray;
1198   if (xatom->getValue(w, XAtom::kde_net_wm_system_tray_window_for,
1199                       XAtom::window, systray) && systray) {
1200     addSystrayWindow(w);
1201     return;
1202   }
1203
1204   new BlackboxWindow(blackbox, w, this);
1205
1206   BlackboxWindow *win = blackbox->searchWindow(w);
1207   if (! win)
1208     return;
1209
1210
1211   if (win->isNormal()) {
1212     // don't list non-normal windows as managed windows
1213     windowList.push_back(win);
1214     updateClientList();
1215   } else if (win->isDesktop()) {
1216     desktopWindowList.push_back(win->getFrameWindow());
1217   }
1218
1219   XMapRequestEvent mre;
1220   mre.window = w;
1221   if (blackbox->isStartup() && win->isNormal()) win->restoreAttributes();
1222   win->mapRequestEvent(&mre);
1223 }
1224
1225
1226 void BScreen::unmanageWindow(BlackboxWindow *w, bool remap) {
1227   w->restore(remap);
1228
1229   if (w->getWorkspaceNumber() != BSENTINEL &&
1230       w->getWindowNumber() != BSENTINEL)
1231     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
1232   else if (w->isIconic())
1233     removeIcon(w);
1234
1235   if (w->isNormal()) {
1236     // we don't list non-normal windows as managed windows
1237     windowList.remove(w);
1238     updateClientList();
1239   } else if (w->isDesktop()) {
1240     WindowList::iterator it = desktopWindowList.begin();
1241     const WindowList::iterator end = desktopWindowList.end();
1242     for (; it != end; ++it)
1243       if (*it == w->getFrameWindow()) {
1244         desktopWindowList.erase(it);
1245         break;
1246       }
1247     assert(it != end);  // the window wasnt a desktop window?
1248   }
1249
1250   if (blackbox->getFocusedWindow() == w)
1251     blackbox->setFocusedWindow((BlackboxWindow *) 0);
1252
1253   removeNetizen(w->getClientWindow());
1254
1255   /*
1256     some managed windows can also be window group controllers.  when
1257     unmanaging such windows, we should also delete the window group.
1258   */
1259   BWindowGroup *group = blackbox->searchGroup(w->getClientWindow());
1260   delete group;
1261
1262   delete w;
1263 }
1264
1265
1266 void BScreen::addNetizen(Netizen *n) {
1267   netizenList.push_back(n);
1268
1269   n->sendWorkspaceCount();
1270   n->sendCurrentWorkspace();
1271
1272   WorkspaceList::iterator it = workspacesList.begin();
1273   const WorkspaceList::iterator end = workspacesList.end();
1274   for (; it != end; ++it)
1275     (*it)->sendWindowList(*n);
1276
1277   Window f = ((blackbox->getFocusedWindow()) ?
1278               blackbox->getFocusedWindow()->getClientWindow() : None);
1279   n->sendWindowFocus(f);
1280 }
1281
1282
1283 void BScreen::removeNetizen(Window w) {
1284   NetizenList::iterator it = netizenList.begin();
1285   for (; it != netizenList.end(); ++it) {
1286     if ((*it)->getWindowID() == w) {
1287       delete *it;
1288       netizenList.erase(it);
1289       break;
1290     }
1291   }
1292 }
1293
1294
1295 void BScreen::updateWorkArea(void) {
1296   if (workspacesList.size() > 0) {
1297     unsigned long *dims = new unsigned long[4 * workspacesList.size()];
1298     for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
1299       // XXX: this could be different for each workspace
1300       const Rect &area = availableArea();
1301       dims[(i * 4) + 0] = area.x();
1302       dims[(i * 4) + 1] = area.y();
1303       dims[(i * 4) + 2] = area.width();
1304       dims[(i * 4) + 3] = area.height();
1305     }
1306     xatom->setValue(getRootWindow(), XAtom::net_workarea, XAtom::cardinal,
1307                     dims, 4 * workspacesList.size());
1308     delete [] dims;
1309   } else
1310     xatom->setValue(getRootWindow(), XAtom::net_workarea, XAtom::cardinal,
1311                     0, 0);
1312 }
1313
1314
1315 void BScreen::updateNetizenCurrentWorkspace(void) {
1316   std::for_each(netizenList.begin(), netizenList.end(),
1317                 std::mem_fun(&Netizen::sendCurrentWorkspace));
1318 }
1319
1320
1321 void BScreen::updateNetizenWorkspaceCount(void) {
1322   xatom->setValue(getRootWindow(), XAtom::net_number_of_desktops,
1323                   XAtom::cardinal, workspacesList.size());
1324
1325   updateWorkArea();
1326   
1327   std::for_each(netizenList.begin(), netizenList.end(),
1328                 std::mem_fun(&Netizen::sendWorkspaceCount));
1329 }
1330
1331
1332 void BScreen::updateNetizenWindowFocus(void) {
1333   Window f = ((blackbox->getFocusedWindow()) ?
1334               blackbox->getFocusedWindow()->getClientWindow() : None);
1335
1336   xatom->setValue(getRootWindow(), XAtom::net_active_window,
1337                   XAtom::window, f);
1338
1339   NetizenList::iterator it = netizenList.begin();
1340   for (; it != netizenList.end(); ++it)
1341     (*it)->sendWindowFocus(f);
1342 }
1343
1344
1345 void BScreen::updateNetizenWindowAdd(Window w, unsigned long p) {
1346   NetizenList::iterator it = netizenList.begin();
1347   for (; it != netizenList.end(); ++it) {
1348     (*it)->sendWindowAdd(w, p);
1349   }
1350 }
1351
1352
1353 void BScreen::updateNetizenWindowDel(Window w) {
1354   NetizenList::iterator it = netizenList.begin();
1355   for (; it != netizenList.end(); ++it)
1356     (*it)->sendWindowDel(w);
1357 }
1358
1359
1360 void BScreen::updateNetizenWindowRaise(Window w) {
1361   NetizenList::iterator it = netizenList.begin();
1362   for (; it != netizenList.end(); ++it)
1363     (*it)->sendWindowRaise(w);
1364 }
1365
1366
1367 void BScreen::updateNetizenWindowLower(Window w) {
1368   NetizenList::iterator it = netizenList.begin();
1369   for (; it != netizenList.end(); ++it)
1370     (*it)->sendWindowLower(w);
1371 }
1372
1373
1374 void BScreen::updateNetizenConfigNotify(XEvent *e) {
1375   NetizenList::iterator it = netizenList.begin();
1376   for (; it != netizenList.end(); ++it)
1377     (*it)->sendConfigNotify(e);
1378 }
1379
1380
1381 void BScreen::raiseWindows(Window *workspace_stack, unsigned int num) {
1382   // the 13 represents the number of blackbox windows such as menus
1383   Window *session_stack = new
1384     Window[(num + workspacesList.size() + rootmenuList.size() + 13)];
1385   unsigned int i = 0, k = num;
1386
1387   XRaiseWindow(blackbox->getXDisplay(), iconmenu->getWindowID());
1388   *(session_stack + i++) = iconmenu->getWindowID();
1389
1390   WorkspaceList::iterator wit = workspacesList.begin();
1391   const WorkspaceList::iterator w_end = workspacesList.end();
1392   for (; wit != w_end; ++wit)
1393     *(session_stack + i++) = (*wit)->getMenu()->getWindowID();
1394
1395   *(session_stack + i++) = workspacemenu->getWindowID();
1396
1397   *(session_stack + i++) = configmenu->getFocusmenu()->getWindowID();
1398   *(session_stack + i++) = configmenu->getPlacementmenu()->getWindowID();
1399   *(session_stack + i++) = configmenu->getWindowID();
1400
1401   *(session_stack + i++) = slit->getMenu()->getDirectionmenu()->getWindowID();
1402   *(session_stack + i++) = slit->getMenu()->getPlacementmenu()->getWindowID();
1403   *(session_stack + i++) = slit->getMenu()->getWindowID();
1404
1405   *(session_stack + i++) =
1406     toolbar->getMenu()->getPlacementmenu()->getWindowID();
1407   *(session_stack + i++) = toolbar->getMenu()->getWindowID();
1408
1409   RootmenuList::iterator rit = rootmenuList.begin();
1410   for (; rit != rootmenuList.end(); ++rit)
1411     *(session_stack + i++) = (*rit)->getWindowID();
1412   *(session_stack + i++) = rootmenu->getWindowID();
1413
1414   if (toolbar->isOnTop())
1415     *(session_stack + i++) = toolbar->getWindowID();
1416
1417   if (slit->isOnTop())
1418     *(session_stack + i++) = slit->getWindowID();
1419
1420   while (k--)
1421     *(session_stack + i++) = *(workspace_stack + k);
1422
1423   XRestackWindows(blackbox->getXDisplay(), session_stack, i);
1424
1425   delete [] session_stack;
1426
1427   updateStackingList();
1428 }
1429
1430
1431 void BScreen::lowerWindows(Window *workspace_stack, unsigned int num) {
1432   assert(num > 0);  // this would cause trouble in the XRaiseWindow call
1433
1434   Window *session_stack = new Window[(num + desktopWindowList.size())];
1435   unsigned int i = 0, k = num;
1436
1437   XLowerWindow(blackbox->getXDisplay(), workspace_stack[0]);
1438
1439   while (k--)
1440     *(session_stack + i++) = *(workspace_stack + k);
1441
1442   WindowList::iterator dit = desktopWindowList.begin();
1443   const WindowList::iterator d_end = desktopWindowList.end();
1444   for (; dit != d_end; ++dit)
1445     *(session_stack + i++) = *dit;
1446
1447   XRestackWindows(blackbox->getXDisplay(), session_stack, i);
1448
1449   delete [] session_stack;
1450
1451   updateStackingList();
1452 }
1453
1454
1455 void BScreen::reassociateWindow(BlackboxWindow *w, unsigned int wkspc_id,
1456                                 bool ignore_sticky) {
1457   if (! w) return;
1458
1459   if (wkspc_id == BSENTINEL)
1460     wkspc_id = current_workspace->getID();
1461
1462   if (w->getWorkspaceNumber() == wkspc_id)
1463     return;
1464
1465   if (w->isIconic()) {
1466     removeIcon(w);
1467     getWorkspace(wkspc_id)->addWindow(w);
1468   } else if (ignore_sticky || ! w->isStuck()) {
1469     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
1470     getWorkspace(wkspc_id)->addWindow(w);
1471   }
1472   updateStackingList();
1473 }
1474
1475
1476 void BScreen::propagateWindowName(const BlackboxWindow *bw) {
1477   if (bw->isIconic()) {
1478     iconmenu->changeItemLabel(bw->getWindowNumber(), bw->getIconTitle());
1479     iconmenu->update();
1480   }
1481   else {
1482     Clientmenu *clientmenu = getWorkspace(bw->getWorkspaceNumber())->getMenu();
1483     clientmenu->changeItemLabel(bw->getWindowNumber(), bw->getTitle());
1484     clientmenu->update();
1485
1486     if (blackbox->getFocusedWindow() == bw)
1487       toolbar->redrawWindowLabel(True);
1488   }
1489 }
1490
1491
1492 void BScreen::nextFocus(void) {
1493   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1494     *next = focused;
1495
1496   if (focused) {
1497     // if window is not on this screen, ignore it
1498     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1499       focused = (BlackboxWindow*) 0;
1500   }
1501
1502   if (focused && current_workspace->getCount() > 1) {
1503     // next is the next window to recieve focus, current is a place holder
1504     BlackboxWindow *current;
1505     do {
1506       current = next;
1507       next = current_workspace->getNextWindowInList(current);
1508     } while(! next->setInputFocus() && next != focused);
1509
1510     if (next != focused)
1511       current_workspace->raiseWindow(next);
1512   } else if (current_workspace->getCount() >= 1) {
1513     next = current_workspace->getTopWindowOnStack();
1514
1515     current_workspace->raiseWindow(next);
1516     next->setInputFocus();
1517   }
1518 }
1519
1520
1521 void BScreen::prevFocus(void) {
1522   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1523     *next = focused;
1524
1525   if (focused) {
1526     // if window is not on this screen, ignore it
1527     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1528       focused = (BlackboxWindow*) 0;
1529   }
1530
1531   if (focused && current_workspace->getCount() > 1) {
1532     // next is the next window to recieve focus, current is a place holder
1533     BlackboxWindow *current;
1534     do {
1535       current = next;
1536       next = current_workspace->getPrevWindowInList(current);
1537     } while(! next->setInputFocus() && next != focused);
1538
1539     if (next != focused)
1540       current_workspace->raiseWindow(next);
1541   } else if (current_workspace->getCount() >= 1) {
1542     next = current_workspace->getTopWindowOnStack();
1543
1544     current_workspace->raiseWindow(next);
1545     next->setInputFocus();
1546   }
1547 }
1548
1549
1550 void BScreen::raiseFocus(void) {
1551   BlackboxWindow *focused = blackbox->getFocusedWindow();
1552   if (! focused)
1553     return;
1554
1555   // if on this Screen, raise it
1556   if (focused->getScreen()->getScreenNumber() == getScreenNumber()) {
1557     Workspace *workspace = getWorkspace(focused->getWorkspaceNumber());
1558     workspace->raiseWindow(focused);
1559   }
1560 }
1561
1562
1563 void BScreen::InitMenu(void) {
1564   if (rootmenu) {
1565     rootmenuList.clear();
1566
1567     while (rootmenu->getCount())
1568       rootmenu->remove(0);
1569   } else {
1570     rootmenu = new Rootmenu(this);
1571   }
1572   bool defaultMenu = True;
1573
1574   FILE *menu_file = (FILE *) 0;
1575   const char *menu_filename = blackbox->getMenuFilename();
1576
1577   if (menu_filename) 
1578     if (! (menu_file = fopen(menu_filename, "r")))
1579       perror(menu_filename);
1580   if (! menu_file) {     // opening the menu file failed, try the default menu
1581     menu_filename = DEFAULTMENU;
1582     if (! (menu_file = fopen(menu_filename, "r")))
1583       perror(menu_filename);
1584   } 
1585
1586   if (menu_file) {
1587     if (feof(menu_file)) {
1588       fprintf(stderr, i18n(ScreenSet, ScreenEmptyMenuFile,
1589                            "%s: Empty menu file"),
1590               menu_filename);
1591     } else {
1592       char line[1024], label[1024];
1593       memset(line, 0, 1024);
1594       memset(label, 0, 1024);
1595
1596       while (fgets(line, 1024, menu_file) && ! feof(menu_file)) {
1597         if (line[0] != '#') {
1598           int i, key = 0, index = -1, len = strlen(line);
1599
1600           for (i = 0; i < len; i++) {
1601             if (line[i] == '[') index = 0;
1602             else if (line[i] == ']') break;
1603             else if (line[i] != ' ')
1604               if (index++ >= 0)
1605                 key += tolower(line[i]);
1606           }
1607
1608           if (key == 517) { // [begin]
1609             index = -1;
1610             for (i = index; i < len; i++) {
1611               if (line[i] == '(') index = 0;
1612               else if (line[i] == ')') break;
1613               else if (index++ >= 0) {
1614                 if (line[i] == '\\' && i < len - 1) i++;
1615                 label[index - 1] = line[i];
1616               }
1617             }
1618
1619             if (index == -1) index = 0;
1620             label[index] = '\0';
1621
1622             rootmenu->setLabel(label);
1623             defaultMenu = parseMenuFile(menu_file, rootmenu);
1624             if (! defaultMenu)
1625               blackbox->addMenuTimestamp(menu_filename);
1626             break;
1627           }
1628         }
1629       }
1630     }
1631     fclose(menu_file);
1632   }
1633
1634   if (defaultMenu) {
1635     rootmenu->setInternalMenu();
1636     rootmenu->insert(i18n(ScreenSet, Screenxterm, "xterm"),
1637                      BScreen::Execute,
1638                      i18n(ScreenSet, Screenxterm, "xterm"));
1639     rootmenu->insert(i18n(ScreenSet, ScreenRestart, "Restart"),
1640                      BScreen::Restart);
1641     rootmenu->insert(i18n(ScreenSet, ScreenExit, "Exit"),
1642                      BScreen::Exit);
1643     rootmenu->setLabel(i18n(BasemenuSet, BasemenuBlackboxMenu,
1644                             "Openbox Menu"));
1645   }
1646 }
1647
1648
1649 bool BScreen::parseMenuFile(FILE *file, Rootmenu *menu) {
1650   char line[1024], label[1024], command[1024];
1651
1652   while (! feof(file)) {
1653     memset(line, 0, 1024);
1654     memset(label, 0, 1024);
1655     memset(command, 0, 1024);
1656
1657     if (fgets(line, 1024, file)) {
1658       if (line[0] != '#') {
1659         int i, key = 0, parse = 0, index = -1, line_length = strlen(line);
1660
1661         // determine the keyword
1662         for (i = 0; i < line_length; i++) {
1663           if (line[i] == '[') parse = 1;
1664           else if (line[i] == ']') break;
1665           else if (line[i] != ' ')
1666             if (parse)
1667               key += tolower(line[i]);
1668         }
1669
1670         // get the label enclosed in ()'s
1671         parse = 0;
1672
1673         for (i = 0; i < line_length; i++) {
1674           if (line[i] == '(') {
1675             index = 0;
1676             parse = 1;
1677           } else if (line[i] == ')') break;
1678           else if (index++ >= 0) {
1679             if (line[i] == '\\' && i < line_length - 1) i++;
1680             label[index - 1] = line[i];
1681           }
1682         }
1683
1684         if (parse) {
1685           label[index] = '\0';
1686         } else {
1687           label[0] = '\0';
1688         }
1689
1690         // get the command enclosed in {}'s
1691         parse = 0;
1692         index = -1;
1693         for (i = 0; i < line_length; i++) {
1694           if (line[i] == '{') {
1695             index = 0;
1696             parse = 1;
1697           } else if (line[i] == '}') break;
1698           else if (index++ >= 0) {
1699             if (line[i] == '\\' && i < line_length - 1) i++;
1700             command[index - 1] = line[i];
1701           }
1702         }
1703
1704         if (parse) {
1705           command[index] = '\0';
1706         } else {
1707           command[0] = '\0';
1708         }
1709
1710         switch (key) {
1711         case 311: // end
1712           return ((menu->getCount() == 0) ? True : False);
1713
1714           break;
1715
1716         case 333: // nop
1717           if (! *label)
1718             label[0] = '\0';
1719           menu->insert(label);
1720
1721           break;
1722
1723         case 421: // exec
1724           if ((! *label) && (! *command)) {
1725             fprintf(stderr, i18n(ScreenSet, ScreenEXECError,
1726                                  "BScreen::parseMenuFile: [exec] error, "
1727                                  "no menu label and/or command defined\n"));
1728             continue;
1729           }
1730
1731           menu->insert(label, BScreen::Execute, command);
1732
1733           break;
1734
1735         case 442: // exit
1736           if (! *label) {
1737             fprintf(stderr, i18n(ScreenSet, ScreenEXITError,
1738                                  "BScreen::parseMenuFile: [exit] error, "
1739                                  "no menu label defined\n"));
1740             continue;
1741           }
1742
1743           menu->insert(label, BScreen::Exit);
1744
1745           break;
1746
1747         case 561: // style
1748           {
1749             if ((! *label) || (! *command)) {
1750               fprintf(stderr,
1751                       i18n(ScreenSet, ScreenSTYLEError,
1752                            "BScreen::parseMenuFile: [style] error, "
1753                            "no menu label and/or filename defined\n"));
1754               continue;
1755             }
1756
1757             string style = expandTilde(command);
1758
1759             menu->insert(label, BScreen::SetStyle, style.c_str());
1760           }
1761
1762           break;
1763
1764         case 630: // config
1765           if (! *label) {
1766             fprintf(stderr, i18n(ScreenSet, ScreenCONFIGError,
1767                                  "BScreen::parseMenufile: [config] error, "
1768                                  "no label defined"));
1769             continue;
1770           }
1771
1772           menu->insert(label, configmenu);
1773
1774           break;
1775
1776         case 740: // include
1777           {
1778             if (! *label) {
1779               fprintf(stderr, i18n(ScreenSet, ScreenINCLUDEError,
1780                                    "BScreen::parseMenuFile: [include] error, "
1781                                    "no filename defined\n"));
1782               continue;
1783             }
1784
1785             string newfile = expandTilde(label);
1786             FILE *submenufile = fopen(newfile.c_str(), "r");
1787
1788             if (submenufile) {
1789               struct stat buf;
1790               if (fstat(fileno(submenufile), &buf) ||
1791                   (! S_ISREG(buf.st_mode))) {
1792                 fprintf(stderr,
1793                         i18n(ScreenSet, ScreenINCLUDEErrorReg,
1794                              "BScreen::parseMenuFile: [include] error: "
1795                              "'%s' is not a regular file\n"), newfile.c_str());
1796                 break;
1797               }
1798
1799               if (! feof(submenufile)) {
1800                 if (! parseMenuFile(submenufile, menu))
1801                   blackbox->addMenuTimestamp(newfile);
1802
1803                 fclose(submenufile);
1804               }
1805             } else {
1806               perror(newfile.c_str());
1807             }
1808           }
1809
1810           break;
1811
1812         case 767: // submenu
1813           {
1814             if (! *label) {
1815               fprintf(stderr, i18n(ScreenSet, ScreenSUBMENUError,
1816                                    "BScreen::parseMenuFile: [submenu] error, "
1817                                    "no menu label defined\n"));
1818               continue;
1819             }
1820
1821             Rootmenu *submenu = new Rootmenu(this);
1822
1823             if (*command)
1824               submenu->setLabel(command);
1825             else
1826               submenu->setLabel(label);
1827
1828             parseMenuFile(file, submenu);
1829             submenu->update();
1830             menu->insert(label, submenu);
1831             rootmenuList.push_back(submenu);
1832           }
1833
1834           break;
1835
1836         case 773: // restart
1837           {
1838             if (! *label) {
1839               fprintf(stderr, i18n(ScreenSet, ScreenRESTARTError,
1840                                    "BScreen::parseMenuFile: [restart] error, "
1841                                    "no menu label defined\n"));
1842               continue;
1843             }
1844
1845             if (*command)
1846               menu->insert(label, BScreen::RestartOther, command);
1847             else
1848               menu->insert(label, BScreen::Restart);
1849           }
1850
1851           break;
1852
1853         case 845: // reconfig
1854           {
1855             if (! *label) {
1856               fprintf(stderr,
1857                       i18n(ScreenSet, ScreenRECONFIGError,
1858                            "BScreen::parseMenuFile: [reconfig] error, "
1859                            "no menu label defined\n"));
1860               continue;
1861             }
1862
1863             menu->insert(label, BScreen::Reconfigure);
1864           }
1865
1866           break;
1867
1868         case 995: // stylesdir
1869         case 1113: // stylesmenu
1870           {
1871             bool newmenu = ((key == 1113) ? True : False);
1872
1873             if ((! *label) || ((! *command) && newmenu)) {
1874               fprintf(stderr,
1875                       i18n(ScreenSet, ScreenSTYLESDIRError,
1876                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1877                            " error, no directory defined\n"));
1878               continue;
1879             }
1880
1881             char *directory = ((newmenu) ? command : label);
1882
1883             string stylesdir = expandTilde(directory);
1884
1885             struct stat statbuf;
1886
1887             if (! stat(stylesdir.c_str(), &statbuf)) {
1888               if (S_ISDIR(statbuf.st_mode)) {
1889                 Rootmenu *stylesmenu;
1890
1891                 if (newmenu)
1892                   stylesmenu = new Rootmenu(this);
1893                 else
1894                   stylesmenu = menu;
1895
1896                 DIR *d = opendir(stylesdir.c_str());
1897                 struct dirent *p;
1898                 std::vector<string> ls;
1899
1900                 while((p = readdir(d)))
1901                   ls.push_back(p->d_name);
1902
1903                 closedir(d);
1904
1905                 std::sort(ls.begin(), ls.end());
1906
1907                 std::vector<string>::iterator it = ls.begin(),
1908                   end = ls.end();
1909                 for (; it != end; ++it) {
1910                   const string& fname = *it;
1911
1912                   if (fname[fname.size()-1] == '~')
1913                     continue;
1914
1915                   string style = stylesdir;
1916                   style += '/';
1917                   style += fname;
1918
1919                   if ((! stat(style.c_str(), &statbuf)) &&
1920                       S_ISREG(statbuf.st_mode))
1921                     stylesmenu->insert(fname, BScreen::SetStyle, style);
1922                 }
1923
1924                 stylesmenu->update();
1925
1926                 if (newmenu) {
1927                   stylesmenu->setLabel(label);
1928                   menu->insert(label, stylesmenu);
1929                   rootmenuList.push_back(stylesmenu);
1930                 }
1931
1932                 blackbox->addMenuTimestamp(stylesdir);
1933               } else {
1934                 fprintf(stderr,
1935                         i18n(ScreenSet, ScreenSTYLESDIRErrorNotDir,
1936                              "BScreen::parseMenuFile:"
1937                              " [stylesdir/stylesmenu] error, %s is not a"
1938                              " directory\n"), stylesdir.c_str());
1939               }
1940             } else {
1941               fprintf(stderr,
1942                       i18n(ScreenSet, ScreenSTYLESDIRErrorNoExist,
1943                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1944                            " error, %s does not exist\n"), stylesdir.c_str());
1945             }
1946             break;
1947           }
1948
1949         case 1090: // workspaces
1950           {
1951             if (! *label) {
1952               fprintf(stderr,
1953                       i18n(ScreenSet, ScreenWORKSPACESError,
1954                            "BScreen:parseMenuFile: [workspaces] error, "
1955                            "no menu label defined\n"));
1956               continue;
1957             }
1958
1959             menu->insert(label, workspacemenu);
1960
1961             break;
1962           }
1963         }
1964       }
1965     }
1966   }
1967
1968   return ((menu->getCount() == 0) ? True : False);
1969 }
1970
1971
1972 void BScreen::shutdown(void) {
1973   XSelectInput(blackbox->getXDisplay(), getRootWindow(), NoEventMask);
1974   XSync(blackbox->getXDisplay(), False);
1975
1976   while(! windowList.empty())
1977     unmanageWindow(windowList.front(), True);
1978
1979   slit->shutdown();
1980 }
1981
1982
1983 void BScreen::showPosition(int x, int y) {
1984   if (! geom_visible) {
1985     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
1986                       (getWidth() - geom_w) / 2,
1987                       (getHeight() - geom_h) / 2, geom_w, geom_h);
1988     XMapWindow(blackbox->getXDisplay(), geom_window);
1989     XRaiseWindow(blackbox->getXDisplay(), geom_window);
1990
1991     geom_visible = True;
1992   }
1993
1994   char label[1024];
1995
1996   sprintf(label, i18n(ScreenSet, ScreenPositionFormat,
1997                       "X: %4d x Y: %4d"), x, y);
1998
1999   XClearWindow(blackbox->getXDisplay(), geom_window);
2000
2001   resource.wstyle.font->drawString(geom_window,
2002                                    resource.bevel_width, resource.bevel_width,
2003                                    resource.wstyle.l_text_focus,
2004                                    label);
2005 }
2006
2007
2008 void BScreen::showGeometry(unsigned int gx, unsigned int gy) {
2009   if (! geom_visible) {
2010     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
2011                       (getWidth() - geom_w) / 2,
2012                       (getHeight() - geom_h) / 2, geom_w, geom_h);
2013     XMapWindow(blackbox->getXDisplay(), geom_window);
2014     XRaiseWindow(blackbox->getXDisplay(), geom_window);
2015
2016     geom_visible = True;
2017   }
2018
2019   char label[1024];
2020
2021   sprintf(label, i18n(ScreenSet, ScreenGeometryFormat,
2022                       "W: %4d x H: %4d"), gx, gy);
2023
2024   XClearWindow(blackbox->getXDisplay(), geom_window);
2025
2026   resource.wstyle.font->drawString(geom_window,
2027                                    resource.bevel_width, resource.bevel_width,
2028                                    resource.wstyle.l_text_focus,
2029                                    label);
2030 }
2031
2032
2033 void BScreen::hideGeometry(void) {
2034   if (geom_visible) {
2035     XUnmapWindow(blackbox->getXDisplay(), geom_window);
2036     geom_visible = False;
2037   }
2038 }
2039
2040
2041 void BScreen::addStrut(Strut *strut) {
2042   strutList.push_back(strut);
2043 }
2044
2045
2046 void BScreen::removeStrut(Strut *strut) {
2047   strutList.remove(strut);
2048 }
2049
2050
2051 const Rect& BScreen::availableArea(void) const {
2052   if (doFullMax())
2053     return getRect(); // return the full screen
2054   return usableArea;
2055 }
2056
2057
2058 void BScreen::updateAvailableArea(void) {
2059   Rect old_area = usableArea;
2060   usableArea = getRect(); // reset to full screen
2061
2062   /* these values represent offsets from the screen edge
2063    * we look for the biggest offset on each edge and then apply them
2064    * all at once
2065    * do not be confused by the similarity to the names of Rect's members
2066    */
2067   unsigned int current_left = 0, current_right = 0, current_top = 0,
2068     current_bottom = 0;
2069
2070   StrutList::const_iterator it = strutList.begin(), end = strutList.end();
2071
2072   for(; it != end; ++it) {
2073     Strut *strut = *it;
2074     if (strut->left > current_left)
2075       current_left = strut->left;
2076     if (strut->top > current_top)
2077       current_top = strut->top;
2078     if (strut->right > current_right)
2079       current_right = strut->right;
2080     if (strut->bottom > current_bottom)
2081       current_bottom = strut->bottom;
2082   }
2083
2084   usableArea.setPos(current_left, current_top);
2085   usableArea.setSize(usableArea.width() - (current_left + current_right),
2086                      usableArea.height() - (current_top + current_bottom));
2087
2088   if (old_area != usableArea) {
2089     BlackboxWindowList::iterator it = windowList.begin(),
2090       end = windowList.end();
2091     for (; it != end; ++it)
2092       if ((*it)->isMaximized()) (*it)->remaximize();
2093   }
2094
2095   updateWorkArea();  
2096 }
2097
2098
2099 Workspace* BScreen::getWorkspace(unsigned int index) {
2100   assert(index < workspacesList.size());
2101   return workspacesList[index];
2102 }
2103
2104
2105 void BScreen::buttonPressEvent(const XButtonEvent *xbutton) {
2106   if (xbutton->button == 1) {
2107     if (! isRootColormapInstalled())
2108       image_control->installRootColormap();
2109
2110     if (workspacemenu->isVisible())
2111       workspacemenu->hide();
2112
2113     if (rootmenu->isVisible())
2114       rootmenu->hide();
2115   } else if (xbutton->button == 2) {
2116     int mx = xbutton->x_root - (workspacemenu->getWidth() / 2);
2117     int my = xbutton->y_root - (workspacemenu->getTitleHeight() / 2);
2118
2119     if (mx < 0) mx = 0;
2120     if (my < 0) my = 0;
2121
2122     if (mx + workspacemenu->getWidth() > getWidth())
2123       mx = getWidth() - workspacemenu->getWidth() - getBorderWidth();
2124
2125     if (my + workspacemenu->getHeight() > getHeight())
2126       my = getHeight() - workspacemenu->getHeight() - getBorderWidth();
2127
2128     workspacemenu->move(mx, my);
2129
2130     if (! workspacemenu->isVisible()) {
2131       workspacemenu->removeParent();
2132       workspacemenu->show();
2133     }
2134   } else if (xbutton->button == 3) {
2135     int mx = xbutton->x_root - (rootmenu->getWidth() / 2);
2136     int my = xbutton->y_root - (rootmenu->getTitleHeight() / 2);
2137
2138     if (mx < 0) mx = 0;
2139     if (my < 0) my = 0;
2140
2141     if (mx + rootmenu->getWidth() > getWidth())
2142       mx = getWidth() - rootmenu->getWidth() - getBorderWidth();
2143
2144     if (my + rootmenu->getHeight() > getHeight())
2145       my = getHeight() - rootmenu->getHeight() - getBorderWidth();
2146
2147     rootmenu->move(mx, my);
2148
2149     if (! rootmenu->isVisible()) {
2150       blackbox->checkMenu();
2151       rootmenu->show();
2152     }
2153   // mouse wheel up
2154   } else if (xbutton->button == 4) {
2155     if (getCurrentWorkspaceID() >= getWorkspaceCount() - 1)
2156       changeWorkspaceID(0);
2157     else
2158       changeWorkspaceID(getCurrentWorkspaceID() + 1);
2159   // mouse wheel down
2160   } else if (xbutton->button == 5) {
2161     if (getCurrentWorkspaceID() == 0)
2162       changeWorkspaceID(getWorkspaceCount() - 1);
2163     else
2164       changeWorkspaceID(getCurrentWorkspaceID() - 1);
2165   }
2166 }
2167
2168
2169 void BScreen::propertyNotifyEvent(const XPropertyEvent *pe) {
2170   if (pe->atom == xatom->getAtom(XAtom::net_desktop_names)) {
2171     // _NET_WM_DESKTOP_NAMES
2172     WorkspaceList::iterator it = workspacesList.begin();
2173     const WorkspaceList::iterator end = workspacesList.end();
2174     for (; it != end; ++it) {
2175       (*it)->readName(); // re-read its name from the window property
2176       workspacemenu->changeWorkspaceLabel((*it)->getID(), (*it)->getName());
2177     }
2178     workspacemenu->update();
2179     toolbar->reconfigure();
2180     saveWorkspaceNames();
2181   }
2182 }
2183
2184
2185 void BScreen::toggleFocusModel(FocusModel model) {
2186   std::for_each(windowList.begin(), windowList.end(),
2187                 std::mem_fun(&BlackboxWindow::ungrabButtons));
2188
2189   if (model == SloppyFocus) {
2190     saveSloppyFocus(True);
2191   } else {
2192     // we're cheating here to save writing the config file 3 times
2193     resource.auto_raise = False;
2194     resource.click_raise = False;
2195     saveSloppyFocus(False);
2196   }
2197
2198   std::for_each(windowList.begin(), windowList.end(),
2199                 std::mem_fun(&BlackboxWindow::grabButtons));
2200 }
2201
2202
2203 BTexture BScreen::readDatabaseTexture(const string &rname,
2204                                       const string &default_color,
2205                                       const Configuration &style) {
2206   BTexture texture;
2207   string s;
2208
2209   if (style.getValue(rname, s))
2210     texture = BTexture(s);
2211   else
2212     texture.setTexture(BTexture::Solid | BTexture::Flat);
2213
2214   // associate this texture with this screen
2215   texture.setDisplay(getBaseDisplay(), getScreenNumber());
2216   texture.setImageControl(image_control);
2217
2218   if (texture.texture() & BTexture::Solid) {
2219     texture.setColor(readDatabaseColor(rname + ".color",
2220                                        default_color, style));
2221     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
2222                                          default_color, style));
2223   } else if (texture.texture() & BTexture::Gradient) {
2224     texture.setColor(readDatabaseColor(rname + ".color",
2225                                        default_color, style));
2226     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
2227                                          default_color, style));
2228   }
2229
2230   return texture;
2231 }
2232
2233
2234 BColor BScreen::readDatabaseColor(const string &rname,
2235                                   const string &default_color,
2236                                   const Configuration &style) {
2237   BColor color;
2238   string s;
2239   if (style.getValue(rname, s))
2240     color = BColor(s, getBaseDisplay(), getScreenNumber());
2241   else
2242     color = BColor(default_color, getBaseDisplay(), getScreenNumber());
2243   return color;
2244 }
2245
2246
2247 BFont *BScreen::readDatabaseFont(const string &rbasename,
2248                                  const Configuration &style) {
2249   string fontname;
2250
2251   string s;
2252
2253 #ifdef    XFT
2254   int i;
2255   if (style.getValue(rbasename + "xft.font", s) &&
2256       style.getValue(rbasename + "xft.size", i)) {
2257     string family = s;
2258     bool bold = False;
2259     bool italic = False;
2260     if (style.getValue(rbasename + "xft.flags", s)) {
2261       if (s.find("bold") != string::npos)
2262         bold = True;
2263       if (s.find("italic") != string::npos)
2264         italic = True;
2265     }
2266     
2267     BFont *b = new BFont(blackbox->getXDisplay(), this, family, i, bold,
2268                          italic, resource.aa_fonts);
2269     if (b->valid())
2270       return b;
2271     else
2272       delete b; // fall back to the normal X font stuff
2273   }
2274 #endif // XFT
2275
2276   style.getValue(rbasename + "font", s);
2277   // if this fails, a blank string will be used, which will cause the fallback
2278   // font to load.
2279
2280   BFont *b = new BFont(blackbox->getXDisplay(), this, s);
2281   if (! b->valid())
2282     exit(2);  // can't continue without a font
2283   return b;
2284 }