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