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