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