]> icculus.org git repositories - mikachu/openbox.git/blob - src/Screen.cc
import from bb-cvs
[mikachu/openbox.git] / src / Screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Screen.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <X11/Xatom.h>
30 #include <X11/keysym.h>
31
32 // for strcasestr()
33 #ifndef _GNU_SOURCE
34 #  define   _GNU_SOURCE
35 #endif // _GNU_SOURCE
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 <algorithm>
72 #include <functional>
73 using std::string;
74
75 #include "i18n.hh"
76 #include "blackbox.hh"
77 #include "Clientmenu.hh"
78 #include "GCCache.hh"
79 #include "Iconmenu.hh"
80 #include "Image.hh"
81 #include "Screen.hh"
82 #include "Slit.hh"
83 #include "Rootmenu.hh"
84 #include "Toolbar.hh"
85 #include "Util.hh"
86 #include "Window.hh"
87 #include "Workspace.hh"
88 #include "Workspacemenu.hh"
89
90 #ifndef   FONT_ELEMENT_SIZE
91 #define   FONT_ELEMENT_SIZE 50
92 #endif // FONT_ELEMENT_SIZE
93
94
95 static bool running = True;
96
97 static int anotherWMRunning(Display *display, XErrorEvent *) {
98   fprintf(stderr, i18n(ScreenSet, ScreenAnotherWMRunning,
99           "BScreen::BScreen: an error occured while querying the X server.\n"
100           "  another window manager already running on display %s.\n"),
101           DisplayString(display));
102
103   running = False;
104
105   return(-1);
106 }
107
108
109 BScreen::BScreen(Blackbox *bb, unsigned int scrn) : ScreenInfo(bb, scrn) {
110   blackbox = bb;
111
112   event_mask = ColormapChangeMask | EnterWindowMask | PropertyChangeMask |
113     SubstructureRedirectMask | ButtonPressMask | ButtonReleaseMask;
114
115   XErrorHandler old = XSetErrorHandler((XErrorHandler) anotherWMRunning);
116   XSelectInput(getBaseDisplay()->getXDisplay(), getRootWindow(), event_mask);
117   XSync(getBaseDisplay()->getXDisplay(), False);
118   XSetErrorHandler((XErrorHandler) old);
119
120   managed = running;
121   if (! managed) return;
122
123   fprintf(stderr, i18n(ScreenSet, ScreenManagingScreen,
124                        "BScreen::BScreen: managing screen %d "
125                        "using visual 0x%lx, depth %d\n"),
126           getScreenNumber(), XVisualIDFromVisual(getVisual()),
127           getDepth());
128
129   rootmenu = 0;
130   resource.stylerc = 0;
131
132   resource.mstyle.t_fontset = resource.mstyle.f_fontset =
133     resource.tstyle.fontset = resource.wstyle.fontset = (XFontSet) 0;
134   resource.mstyle.t_font = resource.mstyle.f_font = resource.tstyle.font =
135     resource.wstyle.font = (XFontStruct *) 0;
136
137 #ifdef    HAVE_GETPID
138   pid_t bpid = getpid();
139
140   XChangeProperty(blackbox->getXDisplay(), getRootWindow(),
141                   blackbox->getBlackboxPidAtom(), XA_CARDINAL,
142                   sizeof(pid_t) * 8, PropModeReplace,
143                   (unsigned char *) &bpid, 1);
144 #endif // HAVE_GETPID
145
146   XDefineCursor(blackbox->getXDisplay(), getRootWindow(),
147                 blackbox->getSessionCursor());
148
149   // start off full screen, top left.
150   usableArea.setSize(getWidth(), getHeight());
151
152   image_control =
153     new BImageControl(blackbox, this, True, blackbox->getColorsPerChannel(),
154                       blackbox->getCacheLife(), blackbox->getCacheMax());
155   image_control->installRootColormap();
156   root_colormap_installed = True;
157
158   blackbox->load_rc(this);
159
160   image_control->setDither(resource.image_dither);
161
162   LoadStyle();
163
164   XGCValues gcv;
165   unsigned long gc_value_mask = GCForeground;
166   if (! i18n.multibyte()) gc_value_mask |= GCFont;
167
168   gcv.foreground = WhitePixel(blackbox->getXDisplay(), getScreenNumber())
169     ^ BlackPixel(blackbox->getXDisplay(), getScreenNumber());
170   gcv.function = GXxor;
171   gcv.subwindow_mode = IncludeInferiors;
172   opGC = XCreateGC(blackbox->getXDisplay(), getRootWindow(),
173                    GCForeground | GCFunction | GCSubwindowMode, &gcv);
174
175   const char *s =  i18n(ScreenSet, ScreenPositionLength,
176                         "0: 0000 x 0: 0000");
177   int l = strlen(s);
178
179   if (i18n.multibyte()) {
180     XRectangle ink, logical;
181     XmbTextExtents(resource.wstyle.fontset, s, l, &ink, &logical);
182     geom_w = logical.width;
183
184     geom_h = resource.wstyle.fontset_extents->max_ink_extent.height;
185   } else {
186     geom_h = resource.wstyle.font->ascent +
187       resource.wstyle.font->descent;
188
189     geom_w = XTextWidth(resource.wstyle.font, s, l);
190   }
191
192   geom_w += (resource.bevel_width * 2);
193   geom_h += (resource.bevel_width * 2);
194
195   XSetWindowAttributes attrib;
196   unsigned long mask = CWBorderPixel | CWColormap | CWSaveUnder;
197   attrib.border_pixel = getBorderColor()->pixel();
198   attrib.colormap = getColormap();
199   attrib.save_under = True;
200
201   geom_window = XCreateWindow(blackbox->getXDisplay(), getRootWindow(),
202                               0, 0, geom_w, geom_h, resource.border_width,
203                               getDepth(), InputOutput, getVisual(),
204                               mask, &attrib);
205   geom_visible = False;
206
207   BTexture* texture = &(resource.wstyle.l_focus);
208   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
209   if (! geom_pixmap)
210     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
211                          texture->color().pixel());
212   else
213     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
214                                geom_window, geom_pixmap);
215
216   workspacemenu = new Workspacemenu(this);
217   iconmenu = new Iconmenu(this);
218   configmenu = new Configmenu(this);
219
220   Workspace *wkspc = (Workspace *) 0;
221   if (resource.workspaces != 0) {
222     for (unsigned int i = 0; i < resource.workspaces; ++i) {
223       wkspc = new Workspace(this, workspacesList.size());
224       workspacesList.push_back(wkspc);
225       workspacemenu->insert(wkspc->getName(), wkspc->getMenu());
226     }
227   } else {
228     wkspc = new Workspace(this, workspacesList.size());
229     workspacesList.push_back(wkspc);
230     workspacemenu->insert(wkspc->getName(), wkspc->getMenu());
231   }
232
233   workspacemenu->insert(i18n(IconSet, IconIcons, "Icons"), iconmenu);
234   workspacemenu->update();
235
236   current_workspace = workspacesList.front();
237   workspacemenu->setItemSelected(2, True);
238
239   toolbar = new Toolbar(this);
240
241   slit = new Slit(this);
242
243   InitMenu();
244
245   raiseWindows(0, 0);
246   rootmenu->update();
247
248   updateAvailableArea();
249
250   changeWorkspaceID(0);
251
252   unsigned int i, j, nchild;
253   Window r, p, *children;
254   XQueryTree(blackbox->getXDisplay(), getRootWindow(), &r, &p,
255              &children, &nchild);
256
257   // preen the window list of all icon windows... for better dockapp support
258   for (i = 0; i < nchild; i++) {
259     if (children[i] == None) continue;
260
261     XWMHints *wmhints = XGetWMHints(blackbox->getXDisplay(),
262                                     children[i]);
263
264     if (wmhints) {
265       if ((wmhints->flags & IconWindowHint) &&
266           (wmhints->icon_window != children[i])) {
267         for (j = 0; j < nchild; j++) {
268           if (children[j] == wmhints->icon_window) {
269             children[j] = None;
270             break;
271           }
272         }
273       }
274
275       XFree(wmhints);
276     }
277   }
278
279   // manage shown windows
280   for (i = 0; i < nchild; ++i) {
281     if (children[i] == None || (! blackbox->validateWindow(children[i])))
282       continue;
283
284     XWindowAttributes attrib;
285     if (XGetWindowAttributes(blackbox->getXDisplay(), children[i], &attrib)) {
286       if (attrib.override_redirect) continue;
287
288       if (attrib.map_state != IsUnmapped) {
289         manageWindow(children[i]);
290       }
291     }
292   }
293
294   XFree(children);
295
296   // call this again just in case a window we found updates the Strut list
297   updateAvailableArea();
298 }
299
300
301 BScreen::~BScreen(void) {
302   if (! managed) return;
303
304   if (geom_pixmap != None)
305     image_control->removeImage(geom_pixmap);
306
307   if (geom_window != None)
308     XDestroyWindow(blackbox->getXDisplay(), geom_window);
309
310   std::for_each(workspacesList.begin(), workspacesList.end(),
311                 PointerAssassin());
312
313   std::for_each(iconList.begin(), iconList.end(), PointerAssassin());
314
315   std::for_each(netizenList.begin(), netizenList.end(), PointerAssassin());
316
317   delete rootmenu;
318   delete workspacemenu;
319   delete iconmenu;
320   delete configmenu;
321   delete slit;
322   delete toolbar;
323   delete image_control;
324
325   if (resource.wstyle.fontset)
326     XFreeFontSet(blackbox->getXDisplay(), resource.wstyle.fontset);
327   if (resource.mstyle.t_fontset)
328     XFreeFontSet(blackbox->getXDisplay(), resource.mstyle.t_fontset);
329   if (resource.mstyle.f_fontset)
330     XFreeFontSet(blackbox->getXDisplay(), resource.mstyle.f_fontset);
331   if (resource.tstyle.fontset)
332     XFreeFontSet(blackbox->getXDisplay(), resource.tstyle.fontset);
333
334   if (resource.wstyle.font)
335     XFreeFont(blackbox->getXDisplay(), resource.wstyle.font);
336   if (resource.mstyle.t_font)
337     XFreeFont(blackbox->getXDisplay(), resource.mstyle.t_font);
338   if (resource.mstyle.f_font)
339     XFreeFont(blackbox->getXDisplay(), resource.mstyle.f_font);
340   if (resource.tstyle.font)
341     XFreeFont(blackbox->getXDisplay(), resource.tstyle.font);
342
343   XFreeGC(blackbox->getXDisplay(), opGC);
344 }
345
346
347 void BScreen::removeWorkspaceNames(void) {
348   workspaceNames.clear();
349 }
350
351
352 void BScreen::reconfigure(void) {
353   LoadStyle();
354
355   XGCValues gcv;
356   unsigned long gc_value_mask = GCForeground;
357   if (! i18n.multibyte()) gc_value_mask |= GCFont;
358
359   gcv.foreground = WhitePixel(blackbox->getXDisplay(),
360                               getScreenNumber());
361   gcv.function = GXinvert;
362   gcv.subwindow_mode = IncludeInferiors;
363   XChangeGC(blackbox->getXDisplay(), opGC,
364             GCForeground | GCFunction | GCSubwindowMode, &gcv);
365
366   const char *s = i18n(ScreenSet, ScreenPositionLength,
367                        "0: 0000 x 0: 0000");
368   int l = strlen(s);
369
370   if (i18n.multibyte()) {
371     XRectangle ink, logical;
372     XmbTextExtents(resource.wstyle.fontset, s, l, &ink, &logical);
373     geom_w = logical.width;
374
375     geom_h = resource.wstyle.fontset_extents->max_ink_extent.height;
376   } else {
377     geom_w = XTextWidth(resource.wstyle.font, s, l);
378
379     geom_h = resource.wstyle.font->ascent + resource.wstyle.font->descent;
380   }
381
382   geom_w += (resource.bevel_width * 2);
383   geom_h += (resource.bevel_width * 2);
384
385   BTexture* texture = &(resource.wstyle.l_focus);
386   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
387   if (! geom_pixmap)
388     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
389                          texture->color().pixel());
390   else
391     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
392                                geom_window, geom_pixmap);
393
394   XSetWindowBorderWidth(blackbox->getXDisplay(), geom_window,
395                         resource.border_width);
396   XSetWindowBorder(blackbox->getXDisplay(), geom_window,
397                    resource.border_color.pixel());
398
399   workspacemenu->reconfigure();
400   iconmenu->reconfigure();
401
402   int remember_sub = rootmenu->getCurrentSubmenu();
403   InitMenu();
404   raiseWindows(0, 0);
405   rootmenu->reconfigure();
406   rootmenu->drawSubmenu(remember_sub);
407
408   configmenu->reconfigure();
409
410   toolbar->reconfigure();
411
412   slit->reconfigure();
413
414   std::for_each(workspacesList.begin(), workspacesList.end(),
415                 std::mem_fun(&Workspace::reconfigure));
416
417   BlackboxWindowList::iterator iit = iconList.begin();
418   for (; iit != iconList.end(); ++iit) {
419     BlackboxWindow *bw = *iit;
420     if (bw->validateClient())
421       bw->reconfigure();
422   }
423
424   image_control->timeout();
425 }
426
427
428 void BScreen::rereadMenu(void) {
429   InitMenu();
430   raiseWindows(0, 0);
431
432   rootmenu->reconfigure();
433 }
434
435
436 void BScreen::LoadStyle(void) {
437   resource.stylerc = XrmGetFileDatabase(blackbox->getStyleFilename());
438   if (! resource.stylerc)
439     resource.stylerc = XrmGetFileDatabase(DEFAULTSTYLE);
440
441   XrmValue value;
442   char *value_type;
443
444   // load fonts/fontsets
445   if (resource.wstyle.fontset)
446     XFreeFontSet(blackbox->getXDisplay(), resource.wstyle.fontset);
447   if (resource.tstyle.fontset)
448     XFreeFontSet(blackbox->getXDisplay(), resource.tstyle.fontset);
449   if (resource.mstyle.f_fontset)
450     XFreeFontSet(blackbox->getXDisplay(), resource.mstyle.f_fontset);
451   if (resource.mstyle.t_fontset)
452     XFreeFontSet(blackbox->getXDisplay(), resource.mstyle.t_fontset);
453   resource.wstyle.fontset = 0;
454   resource.tstyle.fontset = 0;
455   resource.mstyle.f_fontset = 0;
456   resource.mstyle.t_fontset = 0;
457   if (resource.wstyle.font)
458     XFreeFont(blackbox->getXDisplay(), resource.wstyle.font);
459   if (resource.tstyle.font)
460     XFreeFont(blackbox->getXDisplay(), resource.tstyle.font);
461   if (resource.mstyle.f_font)
462     XFreeFont(blackbox->getXDisplay(), resource.mstyle.f_font);
463   if (resource.mstyle.t_font)
464     XFreeFont(blackbox->getXDisplay(), resource.mstyle.t_font);
465   resource.wstyle.font = 0;
466   resource.tstyle.font = 0;
467   resource.mstyle.f_font = 0;
468   resource.mstyle.t_font = 0;
469
470   if (i18n.multibyte()) {
471     resource.wstyle.fontset =
472       readDatabaseFontSet("window.font", "Window.Font");
473     resource.tstyle.fontset =
474       readDatabaseFontSet("toolbar.font", "Toolbar.Font");
475     resource.mstyle.t_fontset =
476       readDatabaseFontSet("menu.title.font", "Menu.Title.Font");
477     resource.mstyle.f_fontset =
478       readDatabaseFontSet("menu.frame.font", "Menu.Frame.Font");
479
480     resource.mstyle.t_fontset_extents =
481       XExtentsOfFontSet(resource.mstyle.t_fontset);
482     resource.mstyle.f_fontset_extents =
483       XExtentsOfFontSet(resource.mstyle.f_fontset);
484     resource.tstyle.fontset_extents =
485       XExtentsOfFontSet(resource.tstyle.fontset);
486     resource.wstyle.fontset_extents =
487       XExtentsOfFontSet(resource.wstyle.fontset);
488   } else {
489     resource.wstyle.font =
490       readDatabaseFont("window.font", "Window.Font");
491     resource.tstyle.font =
492       readDatabaseFont("toolbar.font", "Toolbar.Font");
493     resource.mstyle.t_font =
494       readDatabaseFont("menu.title.font", "Menu.Title.Font");
495     resource.mstyle.f_font =
496       readDatabaseFont("menu.frame.font", "Menu.Frame.Font");
497   }
498
499   // load window config
500   resource.wstyle.t_focus =
501     readDatabaseTexture("window.title.focus", "Window.Title.Focus", "white");
502   resource.wstyle.t_unfocus =
503     readDatabaseTexture("window.title.unfocus",
504                         "Window.Title.Unfocus", "black");
505   resource.wstyle.l_focus =
506     readDatabaseTexture("window.label.focus", "Window.Label.Focus", "white" );
507   resource.wstyle.l_unfocus =
508     readDatabaseTexture("window.label.unfocus", "Window.Label.Unfocus",
509                         "black");
510   resource.wstyle.h_focus =
511     readDatabaseTexture("window.handle.focus", "Window.Handle.Focus", "white");
512   resource.wstyle.h_unfocus =
513     readDatabaseTexture("window.handle.unfocus",
514                         "Window.Handle.Unfocus", "black");
515   resource.wstyle.g_focus =
516     readDatabaseTexture("window.grip.focus", "Window.Grip.Focus", "white");
517   resource.wstyle.g_unfocus =
518     readDatabaseTexture("window.grip.unfocus", "Window.Grip.Unfocus", "black");
519   resource.wstyle.b_focus =
520     readDatabaseTexture("window.button.focus", "Window.Button.Focus", "white");
521   resource.wstyle.b_unfocus =
522     readDatabaseTexture("window.button.unfocus",
523                         "Window.Button.Unfocus", "black");
524   resource.wstyle.b_pressed =
525     readDatabaseTexture("window.button.pressed",
526                         "Window.Button.Pressed", "black");
527   resource.wstyle.f_focus =
528     readDatabaseColor("window.frame.focusColor",
529                       "Window.Frame.FocusColor", "white");
530   resource.wstyle.f_unfocus =
531     readDatabaseColor("window.frame.unfocusColor",
532                       "Window.Frame.UnfocusColor", "black");
533   resource.wstyle.l_text_focus =
534     readDatabaseColor("window.label.focus.textColor",
535                       "Window.Label.Focus.TextColor", "black");
536   resource.wstyle.l_text_unfocus =
537     readDatabaseColor("window.label.unfocus.textColor",
538                       "Window.Label.Unfocus.TextColor", "white");
539   resource.wstyle.b_pic_focus =
540     readDatabaseColor("window.button.focus.picColor",
541                       "Window.Button.Focus.PicColor", "black");
542   resource.wstyle.b_pic_unfocus =
543     readDatabaseColor("window.button.unfocus.picColor",
544                       "Window.Button.Unfocus.PicColor", "white");
545
546   resource.wstyle.justify = LeftJustify;
547   if (XrmGetResource(resource.stylerc, "window.justify", "Window.Justify",
548                      &value_type, &value)) {
549     if (strstr(value.addr, "right") || strstr(value.addr, "Right"))
550       resource.wstyle.justify = RightJustify;
551     else if (strstr(value.addr, "center") || strstr(value.addr, "Center"))
552       resource.wstyle.justify = CenterJustify;
553   }
554
555   // load toolbar config
556   resource.tstyle.toolbar =
557     readDatabaseTexture("toolbar", "Toolbar", "black");
558   resource.tstyle.label =
559     readDatabaseTexture("toolbar.label", "Toolbar.Label", "black");
560   resource.tstyle.window =
561     readDatabaseTexture("toolbar.windowLabel", "Toolbar.WindowLabel", "black");
562   resource.tstyle.button =
563     readDatabaseTexture("toolbar.button", "Toolbar.Button", "white");
564   resource.tstyle.pressed =
565     readDatabaseTexture("toolbar.button.pressed",
566                         "Toolbar.Button.Pressed", "black");
567   resource.tstyle.clock =
568     readDatabaseTexture("toolbar.clock", "Toolbar.Clock", "black");
569   resource.tstyle.l_text =
570     readDatabaseColor("toolbar.label.textColor",
571                       "Toolbar.Label.TextColor", "white");
572   resource.tstyle.w_text =
573     readDatabaseColor("toolbar.windowLabel.textColor",
574                       "Toolbar.WindowLabel.TextColor", "white");
575   resource.tstyle.c_text =
576     readDatabaseColor("toolbar.clock.textColor",
577                       "Toolbar.Clock.TextColor", "white");
578   resource.tstyle.b_pic =
579     readDatabaseColor("toolbar.button.picColor",
580                       "Toolbar.Button.PicColor", "black");
581
582   resource.tstyle.justify = LeftJustify;
583   if (XrmGetResource(resource.stylerc, "toolbar.justify",
584                      "Toolbar.Justify", &value_type, &value)) {
585     if (strstr(value.addr, "right") || strstr(value.addr, "Right"))
586       resource.tstyle.justify = RightJustify;
587     else if (strstr(value.addr, "center") || strstr(value.addr, "Center"))
588       resource.tstyle.justify = CenterJustify;
589   }
590
591   // load menu config
592   resource.mstyle.title =
593     readDatabaseTexture("menu.title", "Menu.Title", "white");
594   resource.mstyle.frame =
595     readDatabaseTexture("menu.frame", "Menu.Frame", "black");
596   resource.mstyle.hilite =
597     readDatabaseTexture("menu.hilite", "Menu.Hilite", "white");
598   resource.mstyle.t_text =
599     readDatabaseColor("menu.title.textColor", "Menu.Title.TextColor", "black");
600   resource.mstyle.f_text =
601     readDatabaseColor("menu.frame.textColor", "Menu.Frame.TextColor", "white");
602   resource.mstyle.d_text =
603     readDatabaseColor("menu.frame.disableColor",
604                       "Menu.Frame.DisableColor", "black");
605   resource.mstyle.h_text =
606     readDatabaseColor("menu.hilite.textColor",
607                       "Menu.Hilite.TextColor", "black");
608
609   resource.mstyle.t_justify = LeftJustify;
610   if (XrmGetResource(resource.stylerc, "menu.title.justify",
611                      "Menu.Title.Justify",
612                      &value_type, &value)) {
613     if (strstr(value.addr, "right") || strstr(value.addr, "Right"))
614       resource.mstyle.t_justify = RightJustify;
615     else if (strstr(value.addr, "center") || strstr(value.addr, "Center"))
616       resource.mstyle.t_justify = CenterJustify;
617   }
618
619   resource.mstyle.f_justify = LeftJustify;
620   if (XrmGetResource(resource.stylerc, "menu.frame.justify",
621                      "Menu.Frame.Justify",
622                      &value_type, &value)) {
623     if (strstr(value.addr, "right") || strstr(value.addr, "Right"))
624       resource.mstyle.f_justify = RightJustify;
625     else if (strstr(value.addr, "center") || strstr(value.addr, "Center"))
626       resource.mstyle.f_justify = CenterJustify;
627   }
628
629   resource.mstyle.bullet = Basemenu::Triangle;
630   if (XrmGetResource(resource.stylerc, "menu.bullet", "Menu.Bullet",
631                      &value_type, &value)) {
632     if (! strncasecmp(value.addr, "empty", value.size))
633       resource.mstyle.bullet = Basemenu::Empty;
634     else if (! strncasecmp(value.addr, "square", value.size))
635       resource.mstyle.bullet = Basemenu::Square;
636     else if (! strncasecmp(value.addr, "diamond", value.size))
637       resource.mstyle.bullet = Basemenu::Diamond;
638   }
639
640   resource.mstyle.bullet_pos = Basemenu::Left;
641   if (XrmGetResource(resource.stylerc, "menu.bullet.position",
642                      "Menu.Bullet.Position", &value_type, &value)) {
643     if (! strncasecmp(value.addr, "right", value.size))
644       resource.mstyle.bullet_pos = Basemenu::Right;
645   }
646
647   resource.border_color =
648     readDatabaseColor("borderColor", "BorderColor", "black");
649
650   unsigned int uint_value;
651
652   // load bevel, border and handle widths
653   resource.handle_width = 6;
654   if (XrmGetResource(resource.stylerc, "handleWidth", "HandleWidth",
655                      &value_type, &value) &&
656       sscanf(value.addr, "%u", &uint_value) == 1 &&
657       uint_value <= (getWidth() / 2) && uint_value != 0) {
658     resource.handle_width = uint_value;
659   }
660
661   resource.border_width = 1;
662   if (XrmGetResource(resource.stylerc, "borderWidth", "BorderWidth",
663                      &value_type, &value) &&
664       sscanf(value.addr, "%u", &uint_value) == 1) {
665     resource.border_width = uint_value;
666   }
667
668   resource.bevel_width = 3;
669   if (XrmGetResource(resource.stylerc, "bevelWidth", "BevelWidth",
670                      &value_type, &value) &&
671       sscanf(value.addr, "%u", &uint_value) == 1 &&
672       uint_value <= (getWidth() / 2) && uint_value != 0) {
673     resource.bevel_width = uint_value;
674   }
675
676   resource.frame_width = resource.bevel_width;
677   if (XrmGetResource(resource.stylerc, "frameWidth", "FrameWidth",
678                      &value_type, &value) &&
679       sscanf(value.addr, "%u", &uint_value) == 1 &&
680       uint_value <= (getWidth() / 2)) {
681     resource.frame_width = uint_value;
682   }
683
684   if (XrmGetResource(resource.stylerc, "rootCommand", "RootCommand",
685                      &value_type, &value)) {
686     bexec(value.addr, displayString());
687   }
688
689   XrmDestroyDatabase(resource.stylerc);
690 }
691
692
693 void BScreen::addIcon(BlackboxWindow *w) {
694   if (! w) return;
695
696   w->setWorkspace(BSENTINEL);
697   w->setWindowNumber(iconList.size());
698
699   iconList.push_back(w);
700
701   const char* title = w->getIconTitle();
702   iconmenu->insert(title);
703   iconmenu->update();
704 }
705
706
707 void BScreen::removeIcon(BlackboxWindow *w) {
708   if (! w) return;
709
710   iconList.remove(w);
711
712   iconmenu->remove(w->getWindowNumber());
713   iconmenu->update();
714
715   BlackboxWindowList::iterator it = iconList.begin(),
716     end = iconList.end();
717   for (int i = 0; it != end; ++it)
718     (*it)->setWindowNumber(i++);
719 }
720
721
722 BlackboxWindow *BScreen::getIcon(unsigned int index) {
723   if (index < iconList.size()) {
724     BlackboxWindowList::iterator it = iconList.begin();
725     for (; index > 0; --index, ++it) ; /* increment to index */
726     return *it;
727   }
728
729   return (BlackboxWindow *) 0;
730 }
731
732
733 unsigned int BScreen::addWorkspace(void) {
734   Workspace *wkspc = new Workspace(this, workspacesList.size());
735   workspacesList.push_back(wkspc);
736
737   workspacemenu->insert(wkspc->getName(), wkspc->getMenu(),
738                         wkspc->getID() + 2);
739   workspacemenu->update();
740
741   toolbar->reconfigure();
742
743   updateNetizenWorkspaceCount();
744
745   return workspacesList.size();
746 }
747
748
749 unsigned int BScreen::removeLastWorkspace(void) {
750   if (workspacesList.size() == 1)
751     return 1;
752
753   Workspace *wkspc = workspacesList.back();
754
755   if (current_workspace->getID() == wkspc->getID())
756     changeWorkspaceID(current_workspace->getID() - 1);
757
758   wkspc->removeAll();
759
760   workspacemenu->remove(wkspc->getID() + 2);
761   workspacemenu->update();
762
763   workspacesList.pop_back();
764   delete wkspc;
765
766   toolbar->reconfigure();
767
768   updateNetizenWorkspaceCount();
769
770   return workspacesList.size();
771 }
772
773
774 void BScreen::changeWorkspaceID(unsigned int id) {
775   if (! current_workspace) return;
776
777   if (id != current_workspace->getID()) {
778     current_workspace->hideAll();
779
780     workspacemenu->setItemSelected(current_workspace->getID() + 2, False);
781
782     if (blackbox->getFocusedWindow() &&
783         blackbox->getFocusedWindow()->getScreen() == this &&
784         (! blackbox->getFocusedWindow()->isStuck())) {
785       current_workspace->setLastFocusedWindow(blackbox->getFocusedWindow());
786       blackbox->setFocusedWindow((BlackboxWindow *) 0);
787     }
788
789     current_workspace = getWorkspace(id);
790
791     workspacemenu->setItemSelected(current_workspace->getID() + 2, True);
792     toolbar->redrawWorkspaceLabel(True);
793
794     current_workspace->showAll();
795
796     if (resource.focus_last && current_workspace->getLastFocusedWindow()) {
797       XSync(blackbox->getXDisplay(), False);
798       current_workspace->getLastFocusedWindow()->setInputFocus();
799     }
800   }
801
802   updateNetizenCurrentWorkspace();
803 }
804
805
806 void BScreen::manageWindow(Window w) {
807   new BlackboxWindow(blackbox, w, this);
808
809   BlackboxWindow *win = blackbox->searchWindow(w);
810   if (! win)
811     return;
812
813   windowList.push_back(win);
814
815   XMapRequestEvent mre;
816   mre.window = w;
817   win->restoreAttributes();
818   win->mapRequestEvent(&mre);
819 }
820
821
822 void BScreen::unmanageWindow(BlackboxWindow *w, bool remap) {
823   w->restore(remap);
824
825   if (w->getWorkspaceNumber() != BSENTINEL &&
826       w->getWindowNumber() != BSENTINEL)
827     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
828   else if (w->isIconic())
829     removeIcon(w);
830
831   windowList.remove(w);
832
833   if (blackbox->getFocusedWindow() == w)
834     blackbox->setFocusedWindow((BlackboxWindow *) 0);
835
836   removeNetizen(w->getClientWindow());
837
838   delete w;
839 }
840
841
842 void BScreen::addNetizen(Netizen *n) {
843   netizenList.push_back(n);
844
845   n->sendWorkspaceCount();
846   n->sendCurrentWorkspace();
847
848   WorkspaceList::iterator it = workspacesList.begin();
849   const WorkspaceList::iterator end = workspacesList.end();
850   for (; it != end; ++it)
851     (*it)->sendWindowList(*n);
852
853   Window f = ((blackbox->getFocusedWindow()) ?
854               blackbox->getFocusedWindow()->getClientWindow() : None);
855   n->sendWindowFocus(f);
856 }
857
858
859 void BScreen::removeNetizen(Window w) {
860   NetizenList::iterator it = netizenList.begin();
861   for (; it != netizenList.end(); ++it) {
862     if ((*it)->getWindowID() == w) {
863       delete *it;
864       netizenList.erase(it);
865       break;
866     }
867   }
868 }
869
870
871 void BScreen::updateNetizenCurrentWorkspace(void) {
872   std::for_each(netizenList.begin(), netizenList.end(),
873                 std::mem_fun(&Netizen::sendCurrentWorkspace));
874 }
875
876
877 void BScreen::updateNetizenWorkspaceCount(void) {
878   std::for_each(netizenList.begin(), netizenList.end(),
879                 std::mem_fun(&Netizen::sendWorkspaceCount));
880 }
881
882
883 void BScreen::updateNetizenWindowFocus(void) {
884   Window f = ((blackbox->getFocusedWindow()) ?
885               blackbox->getFocusedWindow()->getClientWindow() : None);
886   NetizenList::iterator it = netizenList.begin();
887   for (; it != netizenList.end(); ++it)
888     (*it)->sendWindowFocus(f);
889 }
890
891
892 void BScreen::updateNetizenWindowAdd(Window w, unsigned long p) {
893   NetizenList::iterator it = netizenList.begin();
894   for (; it != netizenList.end(); ++it) {
895     (*it)->sendWindowAdd(w, p);
896   }
897 }
898
899
900 void BScreen::updateNetizenWindowDel(Window w) {
901   NetizenList::iterator it = netizenList.begin();
902   for (; it != netizenList.end(); ++it)
903     (*it)->sendWindowDel(w);
904 }
905
906
907 void BScreen::updateNetizenWindowRaise(Window w) {
908   NetizenList::iterator it = netizenList.begin();
909   for (; it != netizenList.end(); ++it)
910     (*it)->sendWindowRaise(w);
911 }
912
913
914 void BScreen::updateNetizenWindowLower(Window w) {
915   NetizenList::iterator it = netizenList.begin();
916   for (; it != netizenList.end(); ++it)
917     (*it)->sendWindowLower(w);
918 }
919
920
921 void BScreen::updateNetizenConfigNotify(XEvent *e) {
922   NetizenList::iterator it = netizenList.begin();
923   for (; it != netizenList.end(); ++it)
924     (*it)->sendConfigNotify(e);
925 }
926
927
928 void BScreen::raiseWindows(Window *workspace_stack, unsigned int num) {
929   // XXX: why 13??
930   Window *session_stack = new
931     Window[(num + workspacesList.size() + rootmenuList.size() + 13)];
932   unsigned int i = 0, k = num;
933
934   XRaiseWindow(blackbox->getXDisplay(), iconmenu->getWindowID());
935   *(session_stack + i++) = iconmenu->getWindowID();
936
937   WorkspaceList::iterator wit = workspacesList.begin();
938   const WorkspaceList::iterator w_end = workspacesList.end();
939   for (; wit != w_end; ++wit)
940     *(session_stack + i++) = (*wit)->getMenu()->getWindowID();
941
942   *(session_stack + i++) = workspacemenu->getWindowID();
943
944   *(session_stack + i++) = configmenu->getFocusmenu()->getWindowID();
945   *(session_stack + i++) = configmenu->getPlacementmenu()->getWindowID();
946   *(session_stack + i++) = configmenu->getWindowID();
947
948   *(session_stack + i++) = slit->getMenu()->getDirectionmenu()->getWindowID();
949   *(session_stack + i++) = slit->getMenu()->getPlacementmenu()->getWindowID();
950   *(session_stack + i++) = slit->getMenu()->getWindowID();
951
952   *(session_stack + i++) =
953     toolbar->getMenu()->getPlacementmenu()->getWindowID();
954   *(session_stack + i++) = toolbar->getMenu()->getWindowID();
955
956   RootmenuList::iterator rit = rootmenuList.begin();
957   for (; rit != rootmenuList.end(); ++rit)
958     *(session_stack + i++) = (*rit)->getWindowID();
959   *(session_stack + i++) = rootmenu->getWindowID();
960
961   if (toolbar->isOnTop())
962     *(session_stack + i++) = toolbar->getWindowID();
963
964   if (slit->isOnTop())
965     *(session_stack + i++) = slit->getWindowID();
966
967   while (k--)
968     *(session_stack + i++) = *(workspace_stack + k);
969
970   XRestackWindows(blackbox->getXDisplay(), session_stack, i);
971
972   delete [] session_stack;
973 }
974
975
976 #ifdef    HAVE_STRFTIME
977 void BScreen::saveStrftimeFormat(const string& format) {
978   resource.strftime_format = format;
979 }
980 #endif // HAVE_STRFTIME
981
982
983 void BScreen::addWorkspaceName(const string& name) {
984   workspaceNames.push_back(name);
985 }
986
987
988 /*
989  * I would love to kill this function and the accompanying workspaceNames
990  * list.  However, we have a chicken and egg situation.  The names are read
991  * in during load_rc() which happens before the workspaces are created.
992  * The current solution is to read the names into a list, then use the list
993  * later for constructing the workspaces.  It is only used during initial
994  * BScreen creation.
995  */
996 const string BScreen::getNameOfWorkspace(unsigned int id) {
997   if (id < workspaceNames.size())
998     return workspaceNames[id];
999   return string("");
1000 }
1001
1002
1003 void BScreen::reassociateWindow(BlackboxWindow *w, unsigned int wkspc_id,
1004                                 bool ignore_sticky) {
1005   if (! w) return;
1006
1007   if (wkspc_id == BSENTINEL)
1008     wkspc_id = current_workspace->getID();
1009
1010   if (w->getWorkspaceNumber() == wkspc_id)
1011     return;
1012
1013   if (w->isIconic()) {
1014     removeIcon(w);
1015     getWorkspace(wkspc_id)->addWindow(w);
1016   } else if (ignore_sticky || ! w->isStuck()) {
1017     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
1018     getWorkspace(wkspc_id)->addWindow(w);
1019   }
1020 }
1021
1022
1023 void BScreen::propagateWindowName(const BlackboxWindow *bw) {
1024   if (bw->isIconic()) {
1025     iconmenu->changeItemLabel(bw->getWindowNumber(), bw->getIconTitle());
1026     iconmenu->update();
1027   }
1028   else {
1029     Clientmenu *clientmenu = getWorkspace(bw->getWorkspaceNumber())->getMenu();
1030     clientmenu->changeItemLabel(bw->getWindowNumber(), bw->getTitle());
1031     clientmenu->update();
1032
1033     if (blackbox->getFocusedWindow() == bw)
1034       toolbar->redrawWindowLabel(True);
1035   }
1036 }
1037
1038
1039 void BScreen::nextFocus(void) {
1040   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1041     *next = focused;
1042
1043   if (focused) {
1044     // if window is not on this screen, ignore it
1045     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1046       focused = (BlackboxWindow*) 0;
1047   }
1048
1049   if (focused && current_workspace->getCount() > 1) {
1050     // next is the next window to recieve focus, current is a place holder
1051     BlackboxWindow *current;
1052     do {
1053       current = next;
1054       next = current_workspace->getNextWindowInList(current);
1055     } while(!next->setInputFocus() && next != focused);
1056
1057     if (next != focused)
1058       current_workspace->raiseWindow(next);
1059   } else if (current_workspace->getCount() >= 1) {
1060     next = current_workspace->getTopWindowOnStack();
1061
1062     current_workspace->raiseWindow(next);
1063     next->setInputFocus();
1064   }
1065 }
1066
1067
1068 void BScreen::prevFocus(void) {
1069   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1070     *next = focused;
1071
1072   if (focused) {
1073     // if window is not on this screen, ignore it
1074     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1075       focused = (BlackboxWindow*) 0;
1076   }
1077
1078   if (focused && current_workspace->getCount() > 1) {
1079     // next is the next window to recieve focus, current is a place holder
1080     BlackboxWindow *current;
1081     do {
1082       current = next;
1083       next = current_workspace->getPrevWindowInList(current);
1084     } while(!next->setInputFocus() && next != focused);
1085
1086     if (next != focused)
1087       current_workspace->raiseWindow(next);
1088   } else if (current_workspace->getCount() >= 1) {
1089     next = current_workspace->getTopWindowOnStack();
1090
1091     current_workspace->raiseWindow(next);
1092     next->setInputFocus();
1093   }
1094 }
1095
1096
1097 void BScreen::raiseFocus(void) {
1098   BlackboxWindow *focused = blackbox->getFocusedWindow();
1099   if (! focused)
1100     return;
1101
1102   // if on this Screen, raise it
1103   if (focused->getScreen()->getScreenNumber() == getScreenNumber()) {
1104     Workspace *workspace = getWorkspace(focused->getWorkspaceNumber());
1105     workspace->raiseWindow(focused);
1106   }
1107 }
1108
1109
1110 void BScreen::InitMenu(void) {
1111   if (rootmenu) {
1112     rootmenuList.clear();
1113
1114     while (rootmenu->getCount())
1115       rootmenu->remove(0);
1116   } else {
1117     rootmenu = new Rootmenu(this);
1118   }
1119   bool defaultMenu = True;
1120
1121   if (blackbox->getMenuFilename()) {
1122     FILE *menu_file = fopen(blackbox->getMenuFilename(), "r");
1123
1124     if (!menu_file) {
1125       perror(blackbox->getMenuFilename());
1126     } else {
1127       if (feof(menu_file)) {
1128         fprintf(stderr, i18n(ScreenSet, ScreenEmptyMenuFile,
1129                              "%s: Empty menu file"),
1130                 blackbox->getMenuFilename());
1131       } else {
1132         char line[1024], label[1024];
1133         memset(line, 0, 1024);
1134         memset(label, 0, 1024);
1135
1136         while (fgets(line, 1024, menu_file) && ! feof(menu_file)) {
1137           if (line[0] != '#') {
1138             int i, key = 0, index = -1, len = strlen(line);
1139
1140             for (i = 0; i < len; i++) {
1141               if (line[i] == '[') index = 0;
1142               else if (line[i] == ']') break;
1143               else if (line[i] != ' ')
1144                 if (index++ >= 0)
1145                   key += tolower(line[i]);
1146             }
1147
1148             if (key == 517) { // [begin]
1149               index = -1;
1150               for (i = index; i < len; i++) {
1151                 if (line[i] == '(') index = 0;
1152                 else if (line[i] == ')') break;
1153                 else if (index++ >= 0) {
1154                   if (line[i] == '\\' && i < len - 1) i++;
1155                   label[index - 1] = line[i];
1156                 }
1157               }
1158
1159               if (index == -1) index = 0;
1160               label[index] = '\0';
1161
1162               rootmenu->setLabel(label);
1163               defaultMenu = parseMenuFile(menu_file, rootmenu);
1164               break;
1165             }
1166           }
1167         }
1168       }
1169       fclose(menu_file);
1170     }
1171   }
1172
1173   if (defaultMenu) {
1174     rootmenu->setInternalMenu();
1175     rootmenu->insert(i18n(ScreenSet, Screenxterm, "xterm"),
1176                      BScreen::Execute,
1177                      i18n(ScreenSet, Screenxterm, "xterm"));
1178     rootmenu->insert(i18n(ScreenSet, ScreenRestart, "Restart"),
1179                      BScreen::Restart);
1180     rootmenu->insert(i18n(ScreenSet, ScreenExit, "Exit"),
1181                      BScreen::Exit);
1182     rootmenu->setLabel(i18n(BasemenuSet, BasemenuBlackboxMenu,
1183                             "Blackbox Menu"));
1184   } else {
1185     blackbox->saveMenuFilename(blackbox->getMenuFilename());
1186   }
1187 }
1188
1189
1190 bool BScreen::parseMenuFile(FILE *file, Rootmenu *menu) {
1191   char line[1024], label[1024], command[1024];
1192
1193   while (! feof(file)) {
1194     memset(line, 0, 1024);
1195     memset(label, 0, 1024);
1196     memset(command, 0, 1024);
1197
1198     if (fgets(line, 1024, file)) {
1199       if (line[0] != '#') {
1200         int i, key = 0, parse = 0, index = -1, line_length = strlen(line);
1201
1202         // determine the keyword
1203         for (i = 0; i < line_length; i++) {
1204           if (line[i] == '[') parse = 1;
1205           else if (line[i] == ']') break;
1206           else if (line[i] != ' ')
1207             if (parse)
1208               key += tolower(line[i]);
1209         }
1210
1211         // get the label enclosed in ()'s
1212         parse = 0;
1213
1214         for (i = 0; i < line_length; i++) {
1215           if (line[i] == '(') {
1216             index = 0;
1217             parse = 1;
1218           } else if (line[i] == ')') break;
1219           else if (index++ >= 0) {
1220             if (line[i] == '\\' && i < line_length - 1) i++;
1221             label[index - 1] = line[i];
1222           }
1223         }
1224
1225         if (parse) {
1226           label[index] = '\0';
1227         } else {
1228           label[0] = '\0';
1229         }
1230
1231         // get the command enclosed in {}'s
1232         parse = 0;
1233         index = -1;
1234         for (i = 0; i < line_length; i++) {
1235           if (line[i] == '{') {
1236             index = 0;
1237             parse = 1;
1238           } else if (line[i] == '}') break;
1239           else if (index++ >= 0) {
1240             if (line[i] == '\\' && i < line_length - 1) i++;
1241             command[index - 1] = line[i];
1242           }
1243         }
1244
1245         if (parse) {
1246           command[index] = '\0';
1247         } else {
1248           command[0] = '\0';
1249         }
1250
1251         switch (key) {
1252         case 311: // end
1253           return ((menu->getCount() == 0) ? True : False);
1254
1255           break;
1256
1257         case 333: // nop
1258           if (! *label)
1259             label[0] = '\0';
1260           menu->insert(label);
1261
1262           break;
1263
1264         case 421: // exec
1265           if ((! *label) && (! *command)) {
1266             fprintf(stderr, i18n(ScreenSet, ScreenEXECError,
1267                                  "BScreen::parseMenuFile: [exec] error, "
1268                                  "no menu label and/or command defined\n"));
1269             continue;
1270           }
1271
1272           menu->insert(label, BScreen::Execute, command);
1273
1274           break;
1275
1276         case 442: // exit
1277           if (! *label) {
1278             fprintf(stderr, i18n(ScreenSet, ScreenEXITError,
1279                                  "BScreen::parseMenuFile: [exit] error, "
1280                                  "no menu label defined\n"));
1281             continue;
1282           }
1283
1284           menu->insert(label, BScreen::Exit);
1285
1286           break;
1287
1288         case 561: // style
1289           {
1290             if ((! *label) || (! *command)) {
1291               fprintf(stderr,
1292                       i18n(ScreenSet, ScreenSTYLEError,
1293                            "BScreen::parseMenuFile: [style] error, "
1294                            "no menu label and/or filename defined\n"));
1295               continue;
1296             }
1297
1298             string style = expandTilde(command);
1299
1300             menu->insert(label, BScreen::SetStyle, style.c_str());
1301           }
1302
1303           break;
1304
1305         case 630: // config
1306           if (! *label) {
1307             fprintf(stderr, i18n(ScreenSet, ScreenCONFIGError,
1308                                  "BScreen::parseMenufile: [config] error, "
1309                                  "no label defined"));
1310             continue;
1311           }
1312
1313           menu->insert(label, configmenu);
1314
1315           break;
1316
1317         case 740: // include
1318           {
1319             if (! *label) {
1320               fprintf(stderr, i18n(ScreenSet, ScreenINCLUDEError,
1321                                    "BScreen::parseMenuFile: [include] error, "
1322                                    "no filename defined\n"));
1323               continue;
1324             }
1325
1326             string newfile = expandTilde(label);
1327             FILE *submenufile = fopen(newfile.c_str(), "r");
1328
1329             if (submenufile) {
1330               struct stat buf;
1331               if (fstat(fileno(submenufile), &buf) ||
1332                   (! S_ISREG(buf.st_mode))) {
1333                 fprintf(stderr,
1334                         i18n(ScreenSet, ScreenINCLUDEErrorReg,
1335                              "BScreen::parseMenuFile: [include] error: "
1336                              "'%s' is not a regular file\n"), newfile.c_str());
1337                 break;
1338               }
1339
1340               if (! feof(submenufile)) {
1341                 if (! parseMenuFile(submenufile, menu))
1342                   blackbox->saveMenuFilename(newfile);
1343
1344                 fclose(submenufile);
1345               }
1346             } else {
1347               perror(newfile.c_str());
1348             }
1349           }
1350
1351           break;
1352
1353         case 767: // submenu
1354           {
1355             if (! *label) {
1356               fprintf(stderr, i18n(ScreenSet, ScreenSUBMENUError,
1357                                    "BScreen::parseMenuFile: [submenu] error, "
1358                                    "no menu label defined\n"));
1359               continue;
1360             }
1361
1362             Rootmenu *submenu = new Rootmenu(this);
1363
1364             if (*command)
1365               submenu->setLabel(command);
1366             else
1367               submenu->setLabel(label);
1368
1369             parseMenuFile(file, submenu);
1370             submenu->update();
1371             menu->insert(label, submenu);
1372             rootmenuList.push_back(submenu);
1373           }
1374
1375           break;
1376
1377         case 773: // restart
1378           {
1379             if (! *label) {
1380               fprintf(stderr, i18n(ScreenSet, ScreenRESTARTError,
1381                                    "BScreen::parseMenuFile: [restart] error, "
1382                                    "no menu label defined\n"));
1383               continue;
1384             }
1385
1386             if (*command)
1387               menu->insert(label, BScreen::RestartOther, command);
1388             else
1389               menu->insert(label, BScreen::Restart);
1390           }
1391
1392           break;
1393
1394         case 845: // reconfig
1395           {
1396             if (! *label) {
1397               fprintf(stderr,
1398                       i18n(ScreenSet, ScreenRECONFIGError,
1399                            "BScreen::parseMenuFile: [reconfig] error, "
1400                            "no menu label defined\n"));
1401               continue;
1402             }
1403
1404             menu->insert(label, BScreen::Reconfigure);
1405           }
1406
1407           break;
1408
1409         case 995: // stylesdir
1410         case 1113: // stylesmenu
1411           {
1412             bool newmenu = ((key == 1113) ? True : False);
1413
1414             if ((! *label) || ((! *command) && newmenu)) {
1415               fprintf(stderr,
1416                       i18n(ScreenSet, ScreenSTYLESDIRError,
1417                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1418                            " error, no directory defined\n"));
1419               continue;
1420             }
1421
1422             char *directory = ((newmenu) ? command : label);
1423
1424             string stylesdir = expandTilde(directory);
1425
1426             struct stat statbuf;
1427
1428             if (! stat(stylesdir.c_str(), &statbuf)) {
1429               if (S_ISDIR(statbuf.st_mode)) {
1430                 Rootmenu *stylesmenu;
1431
1432                 if (newmenu)
1433                   stylesmenu = new Rootmenu(this);
1434                 else
1435                   stylesmenu = menu;
1436
1437                 DIR *d = opendir(stylesdir.c_str());
1438                 struct dirent *p;
1439                 std::vector<string> ls;
1440
1441                 while((p = readdir(d)))
1442                   ls.push_back(p->d_name);
1443
1444                 closedir(d);
1445
1446                 std::sort(ls.begin(), ls.end());
1447
1448                 std::vector<string>::iterator it = ls.begin(),
1449                   end = ls.end();
1450                 for (; it != end; ++it) {
1451                   const string& fname = *it;
1452
1453                   if (fname[fname.size()-1] == '~')
1454                     continue;
1455
1456                   string style = stylesdir;
1457                   style += '/';
1458                   style += fname;
1459
1460                   if ((! stat(style.c_str(), &statbuf)) &&
1461                       S_ISREG(statbuf.st_mode))
1462                     stylesmenu->insert(fname, BScreen::SetStyle, style);
1463                 }
1464
1465                 stylesmenu->update();
1466
1467                 if (newmenu) {
1468                   stylesmenu->setLabel(label);
1469                   menu->insert(label, stylesmenu);
1470                   rootmenuList.push_back(stylesmenu);
1471                 }
1472
1473                 blackbox->saveMenuFilename(stylesdir);
1474               } else {
1475                 fprintf(stderr,
1476                         i18n(ScreenSet, ScreenSTYLESDIRErrorNotDir,
1477                              "BScreen::parseMenuFile:"
1478                              " [stylesdir/stylesmenu] error, %s is not a"
1479                              " directory\n"), stylesdir.c_str());
1480               }
1481             } else {
1482               fprintf(stderr,
1483                       i18n(ScreenSet, ScreenSTYLESDIRErrorNoExist,
1484                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1485                            " error, %s does not exist\n"), stylesdir.c_str());
1486             }
1487             break;
1488           }
1489
1490         case 1090: // workspaces
1491           {
1492             if (! *label) {
1493               fprintf(stderr,
1494                       i18n(ScreenSet, ScreenWORKSPACESError,
1495                            "BScreen:parseMenuFile: [workspaces] error, "
1496                            "no menu label defined\n"));
1497               continue;
1498             }
1499
1500             menu->insert(label, workspacemenu);
1501
1502             break;
1503           }
1504         }
1505       }
1506     }
1507   }
1508
1509   return ((menu->getCount() == 0) ? True : False);
1510 }
1511
1512
1513 void BScreen::shutdown(void) {
1514   XSelectInput(blackbox->getXDisplay(), getRootWindow(), NoEventMask);
1515   XSync(blackbox->getXDisplay(), False);
1516
1517   while(! windowList.empty())
1518     unmanageWindow(windowList.front(), True);
1519
1520   slit->shutdown();
1521 }
1522
1523
1524 void BScreen::showPosition(int x, int y) {
1525   if (! geom_visible) {
1526     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
1527                       (getWidth() - geom_w) / 2,
1528                       (getHeight() - geom_h) / 2, geom_w, geom_h);
1529     XMapWindow(blackbox->getXDisplay(), geom_window);
1530     XRaiseWindow(blackbox->getXDisplay(), geom_window);
1531
1532     geom_visible = True;
1533   }
1534
1535   char label[1024];
1536
1537   sprintf(label, i18n(ScreenSet, ScreenPositionFormat,
1538                       "X: %4d x Y: %4d"), x, y);
1539
1540   XClearWindow(blackbox->getXDisplay(), geom_window);
1541
1542   BPen pen(resource.wstyle.l_text_focus, resource.wstyle.font);
1543   if (i18n.multibyte()) {
1544     XmbDrawString(blackbox->getXDisplay(), geom_window,
1545                   resource.wstyle.fontset, pen.gc(),
1546                   resource.bevel_width, resource.bevel_width -
1547                   resource.wstyle.fontset_extents->max_ink_extent.y,
1548                   label, strlen(label));
1549   } else {
1550     XDrawString(blackbox->getXDisplay(), geom_window,
1551                 pen.gc(), resource.bevel_width,
1552                 resource.wstyle.font->ascent + resource.bevel_width,
1553                 label, strlen(label));
1554   }
1555 }
1556
1557
1558 void BScreen::showGeometry(unsigned int gx, unsigned int gy) {
1559   if (! geom_visible) {
1560     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
1561                       (getWidth() - geom_w) / 2,
1562                       (getHeight() - geom_h) / 2, geom_w, geom_h);
1563     XMapWindow(blackbox->getXDisplay(), geom_window);
1564     XRaiseWindow(blackbox->getXDisplay(), geom_window);
1565
1566     geom_visible = True;
1567   }
1568
1569   char label[1024];
1570
1571   sprintf(label, i18n(ScreenSet, ScreenGeometryFormat,
1572                       "W: %4d x H: %4d"), gx, gy);
1573
1574   XClearWindow(blackbox->getXDisplay(), geom_window);
1575
1576   BPen pen(resource.wstyle.l_text_focus, resource.wstyle.font);
1577   if (i18n.multibyte()) {
1578     XmbDrawString(blackbox->getXDisplay(), geom_window,
1579                   resource.wstyle.fontset, pen.gc(),
1580                   resource.bevel_width, resource.bevel_width -
1581                   resource.wstyle.fontset_extents->max_ink_extent.y,
1582                   label, strlen(label));
1583   } else {
1584     XDrawString(blackbox->getXDisplay(), geom_window,
1585                 pen.gc(), resource.bevel_width,
1586                 resource.wstyle.font->ascent +
1587                 resource.bevel_width, label, strlen(label));
1588   }
1589 }
1590
1591
1592 void BScreen::hideGeometry(void) {
1593   if (geom_visible) {
1594     XUnmapWindow(blackbox->getXDisplay(), geom_window);
1595     geom_visible = False;
1596   }
1597 }
1598
1599
1600 void BScreen::addStrut(Strut *strut) {
1601   strutList.push_back(strut);
1602 }
1603
1604
1605 void BScreen::removeStrut(Strut *strut) {
1606   strutList.remove(strut);
1607 }
1608
1609
1610 const Rect& BScreen::availableArea(void) const {
1611   if (doFullMax())
1612     return getRect(); // return the full screen
1613   return usableArea;
1614 }
1615
1616
1617 void BScreen::updateAvailableArea(void) {
1618   Rect old_area = usableArea;
1619   usableArea = getRect(); // reset to full screen
1620
1621   /* these values represent offsets from the screen edge
1622    * we look for the biggest offset on each edge and then apply them
1623    * all at once
1624    * do not be confused by the similarity to the names of Rect's members
1625    */
1626   unsigned int current_left = 0, current_right = 0, current_top = 0,
1627     current_bottom = 0;
1628
1629   StrutList::const_iterator it = strutList.begin(), end = strutList.end();
1630
1631   for(; it != end; ++it) {
1632     Strut *strut = *it;
1633     if (strut->left > current_left)
1634       current_left = strut->left;
1635     if (strut->top > current_top)
1636       current_top = strut->top;
1637     if (strut->right > current_right)
1638       current_right = strut->right;
1639     if (strut->bottom > current_bottom)
1640       current_bottom = strut->bottom;
1641   }
1642
1643   usableArea.setPos(current_left, current_top);
1644   usableArea.setSize(usableArea.width() - (current_left + current_right),
1645                      usableArea.height() - (current_top + current_bottom));
1646
1647   if (old_area != usableArea) {
1648     BlackboxWindowList::iterator it = windowList.begin(),
1649       end = windowList.end();
1650     for (; it != end; ++it)
1651       if ((*it)->isMaximized()) (*it)->remaximize();
1652   }
1653 }
1654
1655
1656 Workspace* BScreen::getWorkspace(unsigned int index) {
1657   assert(index < workspacesList.size());
1658   return workspacesList[index];
1659 }
1660
1661
1662 void BScreen::buttonPressEvent(XButtonEvent *xbutton) {
1663   if (xbutton->button == 1) {
1664     if (! isRootColormapInstalled())
1665       image_control->installRootColormap();
1666
1667     if (workspacemenu->isVisible())
1668       workspacemenu->hide();
1669
1670     if (rootmenu->isVisible())
1671       rootmenu->hide();
1672   } else if (xbutton->button == 2) {
1673     int mx = xbutton->x_root - (workspacemenu->getWidth() / 2);
1674     int my = xbutton->y_root - (workspacemenu->getTitleHeight() / 2);
1675
1676     if (mx < 0) mx = 0;
1677     if (my < 0) my = 0;
1678
1679     if (mx + workspacemenu->getWidth() > getWidth())
1680       mx = getWidth() - workspacemenu->getWidth() - getBorderWidth();
1681
1682     if (my + workspacemenu->getHeight() > getHeight())
1683       my = getHeight() - workspacemenu->getHeight() - getBorderWidth();
1684
1685     workspacemenu->move(mx, my);
1686
1687     if (! workspacemenu->isVisible()) {
1688       workspacemenu->removeParent();
1689       workspacemenu->show();
1690     }
1691   } else if (xbutton->button == 3) {
1692     int mx = xbutton->x_root - (rootmenu->getWidth() / 2);
1693     int my = xbutton->y_root - (rootmenu->getTitleHeight() / 2);
1694
1695     if (mx < 0) mx = 0;
1696     if (my < 0) my = 0;
1697
1698     if (mx + rootmenu->getWidth() > getWidth())
1699       mx = getWidth() - rootmenu->getWidth() - getBorderWidth();
1700
1701     if (my + rootmenu->getHeight() > getHeight())
1702       my = getHeight() - rootmenu->getHeight() - getBorderWidth();
1703
1704     rootmenu->move(mx, my);
1705
1706     if (! rootmenu->isVisible()) {
1707       blackbox->checkMenu();
1708       rootmenu->show();
1709     }
1710   }
1711 }
1712
1713
1714 void BScreen::toggleFocusModel(FocusModel model) {
1715   if (model == SloppyFocus) {
1716     saveSloppyFocus(True);
1717   } else {
1718     saveSloppyFocus(False);
1719     saveAutoRaise(False);
1720     saveClickRaise(False);
1721   }
1722
1723   updateFocusModel();
1724 }
1725
1726
1727 void BScreen::updateFocusModel()
1728 {
1729   std::for_each(workspacesList.begin(), workspacesList.end(),
1730                 std::mem_fun(&Workspace::updateFocusModel));
1731 }
1732
1733
1734 BTexture BScreen::readDatabaseTexture(const string &rname,
1735                                       const string &rclass,
1736                                       const string &default_color) {
1737   BTexture texture;
1738   XrmValue value;
1739   char *value_type;
1740
1741   if (XrmGetResource(resource.stylerc, rname.c_str(), rclass.c_str(),
1742                      &value_type, &value))
1743     texture = BTexture(value.addr);
1744   else
1745     texture.setTexture(BTexture::Solid | BTexture::Flat);
1746
1747   // associate this texture with this screen
1748   texture.setDisplay(getBaseDisplay(), getScreenNumber());
1749   texture.setImageControl(image_control);
1750
1751   if (texture.texture() & BTexture::Solid) {
1752     texture.setColor(readDatabaseColor(rname + ".color",
1753                                        rclass + ".Color",
1754                                        default_color));
1755     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
1756                                          rclass + ".ColorTo",
1757                                          default_color));
1758   } else if (texture.texture() & BTexture::Gradient) {
1759     texture.setColor(readDatabaseColor(rname + ".color",
1760                                        rclass + ".Color",
1761                                        default_color));
1762     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
1763                                          rclass + ".ColorTo",
1764                                          default_color));
1765   }
1766
1767   return texture;
1768 }
1769
1770
1771 BColor BScreen::readDatabaseColor(const string &rname, const string &rclass,
1772                                   const string &default_color) {
1773   BColor color;
1774   XrmValue value;
1775   char *value_type;
1776   if (XrmGetResource(resource.stylerc, rname.c_str(), rclass.c_str(),
1777                      &value_type, &value))
1778     color = BColor(value.addr, getBaseDisplay(), getScreenNumber());
1779   else
1780     color = BColor(default_color, getBaseDisplay(), getScreenNumber());
1781   return color;
1782 }
1783
1784
1785 XFontSet BScreen::readDatabaseFontSet(const string &rname,
1786                                       const string &rclass) {
1787   char *defaultFont = "fixed";
1788
1789   bool load_default = True;
1790   XrmValue value;
1791   char *value_type;
1792   XFontSet fontset = 0;
1793   if (XrmGetResource(resource.stylerc, rname.c_str(), rclass.c_str(),
1794                      &value_type, &value) &&
1795       (fontset = createFontSet(value.addr))) {
1796     load_default = False;
1797   }
1798
1799   if (load_default) {
1800     fontset = createFontSet(defaultFont);
1801
1802     if (! fontset) {
1803       fprintf(stderr,
1804               i18n(ScreenSet, ScreenDefaultFontLoadFail,
1805                    "BScreen::setCurrentStyle(): couldn't load default font.\n"));
1806       exit(2);
1807     }
1808   }
1809
1810   return fontset;
1811 }
1812
1813
1814 XFontStruct *BScreen::readDatabaseFont(const string &rname,
1815                                        const string &rclass) {
1816   char *defaultFont = "fixed";
1817
1818   bool load_default = False;
1819   XrmValue value;
1820   char *value_type;
1821   XFontStruct *font = 0;
1822   if (XrmGetResource(resource.stylerc, rname.c_str(), rclass.c_str(),
1823                      &value_type, &value)) {
1824     if ((font = XLoadQueryFont(blackbox->getXDisplay(), value.addr)) == NULL) {
1825       fprintf(stderr,
1826               i18n(ScreenSet, ScreenFontLoadFail,
1827                    "BScreen::setCurrentStyle(): couldn't load font '%s'\n"),
1828               value.addr);
1829
1830       load_default = True;
1831     }
1832   } else {
1833     load_default = True;
1834   }
1835
1836   if (load_default) {
1837     font = XLoadQueryFont(blackbox->getXDisplay(), defaultFont);
1838     if (font == NULL) {
1839       fprintf(stderr,
1840               i18n(ScreenSet, ScreenDefaultFontLoadFail,
1841                    "BScreen::setCurrentStyle(): couldn't load default font.\n"));
1842       exit(2);
1843     }
1844   }
1845
1846   return font;
1847 }
1848
1849
1850 #ifndef    HAVE_STRCASESTR
1851 static const char * strcasestr(const char *str, const char *ptn) {
1852   const char *s2, *p2;
1853   for(; *str; str++) {
1854     for(s2=str,p2=ptn; ; s2++,p2++) {
1855       if (!*p2) return str;
1856       if (toupper(*s2) != toupper(*p2)) break;
1857     }
1858   }
1859   return NULL;
1860 }
1861 #endif // HAVE_STRCASESTR
1862
1863
1864 static const char *getFontElement(const char *pattern, char *buf,
1865                                   int bufsiz, ...) {
1866   const char *p, *v;
1867   char *p2;
1868   va_list va;
1869
1870   va_start(va, bufsiz);
1871   buf[bufsiz-1] = 0;
1872   buf[bufsiz-2] = '*';
1873   while((v = va_arg(va, char *)) != NULL) {
1874     p = strcasestr(pattern, v);
1875     if (p) {
1876       strncpy(buf, p+1, bufsiz-2);
1877       p2 = strchr(buf, '-');
1878       if (p2) *p2=0;
1879       va_end(va);
1880       return p;
1881     }
1882   }
1883   va_end(va);
1884   strncpy(buf, "*", bufsiz);
1885   return NULL;
1886 }
1887
1888
1889 static const char *getFontSize(const char *pattern, int *size) {
1890   const char *p;
1891   const char *p2=NULL;
1892   int n=0;
1893
1894   for (p=pattern; 1; p++) {
1895     if (!*p) {
1896       if (p2!=NULL && n>1 && n<72) {
1897         *size = n; return p2+1;
1898       } else {
1899         *size = 16; return NULL;
1900       }
1901     } else if (*p=='-') {
1902       if (n>1 && n<72 && p2!=NULL) {
1903         *size = n;
1904         return p2+1;
1905       }
1906       p2=p; n=0;
1907     } else if (*p>='0' && *p<='9' && p2!=NULL) {
1908       n *= 10;
1909       n += *p-'0';
1910     } else {
1911       p2=NULL; n=0;
1912     }
1913   }
1914 }
1915
1916
1917 XFontSet BScreen::createFontSet(const string &fontname) {
1918   XFontSet fs;
1919   char **missing, *def = "-";
1920   int nmissing, pixel_size = 0, buf_size = 0;
1921   char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE];
1922
1923   fs = XCreateFontSet(blackbox->getXDisplay(),
1924                       fontname.c_str(), &missing, &nmissing, &def);
1925   if (fs && (! nmissing))
1926     return fs;
1927
1928   const char *nfontname = fontname.c_str();
1929 #ifdef    HAVE_SETLOCALE
1930   if (! fs) {
1931     if (nmissing) XFreeStringList(missing);
1932
1933     setlocale(LC_CTYPE, "C");
1934     fs = XCreateFontSet(blackbox->getXDisplay(), fontname.c_str(),
1935                         &missing, &nmissing, &def);
1936     setlocale(LC_CTYPE, "");
1937   }
1938 #endif // HAVE_SETLOCALE
1939
1940   if (fs) {
1941     XFontStruct **fontstructs;
1942     char **fontnames;
1943     XFontsOfFontSet(fs, &fontstructs, &fontnames);
1944     nfontname = fontnames[0];
1945   }
1946
1947   getFontElement(nfontname, weight, FONT_ELEMENT_SIZE,
1948                  "-medium-", "-bold-", "-demibold-", "-regular-", NULL);
1949   getFontElement(nfontname, slant, FONT_ELEMENT_SIZE,
1950                  "-r-", "-i-", "-o-", "-ri-", "-ro-", NULL);
1951   getFontSize(nfontname, &pixel_size);
1952
1953   if (! strcmp(weight, "*"))
1954     strncpy(weight, "medium", FONT_ELEMENT_SIZE);
1955   if (! strcmp(slant, "*"))
1956     strncpy(slant, "r", FONT_ELEMENT_SIZE);
1957   if (pixel_size < 3)
1958     pixel_size = 3;
1959   else if (pixel_size > 97)
1960     pixel_size = 97;
1961
1962   buf_size = strlen(nfontname) + (FONT_ELEMENT_SIZE * 2) + 64;
1963   char *pattern2 = new char[buf_size];
1964   sprintf(pattern2,
1965            "%s,"
1966            "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
1967            "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*",
1968            nfontname, weight, slant, pixel_size, pixel_size);
1969   nfontname = pattern2;
1970
1971   if (nmissing)
1972     XFreeStringList(missing);
1973   if (fs)
1974     XFreeFontSet(blackbox->getXDisplay(), fs);
1975
1976   fs = XCreateFontSet(blackbox->getXDisplay(), nfontname, &missing,
1977                       &nmissing, &def);
1978
1979   delete [] pattern2;
1980
1981   return fs;
1982 }