]> icculus.org git repositories - mikachu/openbox.git/blob - src/Screen.cc
comment typo and function spacing fix
[mikachu/openbox.git] / src / Screen.cc
1 // Screen.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #include <X11/Xatom.h>
34 #include <X11/keysym.h>
35
36 #include "i18n.h"
37 #include "openbox.h"
38 #include "Clientmenu.h"
39 #include "Iconmenu.h"
40 #include "Image.h"
41 #include "Screen.h"
42
43 #ifdef    SLIT
44 #include "Slit.h"
45 #endif // SLIT
46
47 #include "Rootmenu.h"
48 #include "Toolbar.h"
49 #include "Window.h"
50 #include "Workspace.h"
51 #include "Workspacemenu.h"
52
53 #ifdef    HAVE_STDLIB_H
54 #  include <stdlib.h>
55 #endif // HAVE_STDLIB_H
56
57 #ifdef    HAVE_STRING_H
58 #  include <string.h>
59 #endif // HAVE_STRING_H
60
61 #ifdef    HAVE_SYS_TYPES_H
62 #  include <sys/types.h>
63 #endif // HAVE_SYS_TYPES_H
64
65 #ifdef    HAVE_CTYPE_H
66 #  include <ctype.h>
67 #endif // HAVE_CTYPE_H
68
69 #ifdef    HAVE_DIRENT_H
70 #  include <dirent.h>
71 #endif // HAVE_DIRENT_H
72
73 #ifdef    HAVE_LOCALE_H
74 #  include <locale.h>
75 #endif // HAVE_LOCALE_H
76
77 #ifdef    HAVE_UNISTD_H
78 #  include <sys/types.h>
79 #  include <unistd.h>
80 #endif // HAVE_UNISTD_H
81
82 #ifdef    HAVE_SYS_STAT_H
83 #  include <sys/stat.h>
84 #endif // HAVE_SYS_STAT_H
85
86 #ifdef    HAVE_STDARG_H
87 #  include <stdarg.h>
88 #endif // HAVE_STDARG_H
89
90 #ifndef    HAVE_SNPRINTF
91 #  include "bsd-snprintf.h"
92 #endif // !HAVE_SNPRINTF
93
94 #ifndef   MAXPATHLEN
95 #define   MAXPATHLEN 255
96 #endif // MAXPATHLEN
97
98 #ifndef   FONT_ELEMENT_SIZE
99 #define   FONT_ELEMENT_SIZE 50
100 #endif // FONT_ELEMENT_SIZE
101
102 #include <strstream>
103 #include <string>
104 #include <algorithm>
105
106 static Bool running = True;
107
108 static int anotherWMRunning(Display *display, XErrorEvent *) {
109   fprintf(stderr, i18n->getMessage(ScreenSet, ScreenAnotherWMRunning,
110      "BScreen::BScreen: an error occured while querying the X server.\n"
111              "  another window manager already running on display %s.\n"),
112           DisplayString(display));
113
114   running = False;
115
116   return(-1);
117 }
118
119 struct dcmp {
120   bool operator()(const char *one, const char *two) const {
121     return (strcmp(one, two) < 0) ? True : False;
122   }
123 };
124
125 #ifndef    HAVE_STRCASESTR
126 static const char * strcasestr(const char *str, const char *ptn) {
127   const char *s2, *p2;
128   for( ; *str; str++) {
129     for(s2=str,p2=ptn; ; s2++,p2++) {
130       if (!*p2) return str;
131       if (toupper(*s2) != toupper(*p2)) break;
132     }
133   }
134   return NULL;
135 }
136 #endif // HAVE_STRCASESTR
137
138 static const char *getFontElement(const char *pattern, char *buf, int bufsiz, ...) {
139   const char *p, *v;
140   char *p2;
141   va_list va;
142
143   va_start(va, bufsiz);
144   buf[bufsiz-1] = 0;
145   buf[bufsiz-2] = '*';
146   while((v = va_arg(va, char *)) != NULL) {
147     p = strcasestr(pattern, v);
148     if (p) {
149       strncpy(buf, p+1, bufsiz-2);
150       p2 = strchr(buf, '-');
151       if (p2) *p2=0;
152       va_end(va);
153       return p;
154     }
155   }
156   va_end(va);
157   strncpy(buf, "*", bufsiz);
158   return NULL;
159 }
160
161 static const char *getFontSize(const char *pattern, int *size) {
162   const char *p;
163   const char *p2=NULL;
164   int n=0;
165
166   for (p=pattern; 1; p++) {
167     if (!*p) {
168       if (p2!=NULL && n>1 && n<72) {
169         *size = n; return p2+1;
170       } else {
171         *size = 16; return NULL;
172       }
173     } else if (*p=='-') {
174       if (n>1 && n<72 && p2!=NULL) {
175         *size = n;
176         return p2+1;
177       }
178       p2=p; n=0;
179     } else if (*p>='0' && *p<='9' && p2!=NULL) {
180       n *= 10;
181       n += *p-'0';
182     } else {
183       p2=NULL; n=0;
184     }
185   }
186 }
187
188
189 BScreen::BScreen(Openbox &ob, int scrn, Resource &conf) : ScreenInfo(ob, scrn),
190   openbox(ob), config(conf)
191 {
192   // default values
193   resource.full_max = false;
194   resource.focus_new = false;
195   resource.focus_last = false;
196   resource.row_direction = LeftRight;
197   resource.col_direction = TopBottom;
198   resource.workspaces = 1;
199   resource.sloppy_focus = true;
200   resource.auto_raise = false;
201   resource.zones = 1;
202   resource.placement_policy = CascadePlacement;
203 #ifdef    HAVE_STRFTIME
204   resource.strftime_format = bstrdup("%I:%M %p");
205 #else // !have_strftime
206   resource.date_format = B_AmericanDate;
207   resource.clock24hour =  false;
208 #endif // HAVE_STRFTIME
209   resource.edge_snap_threshold = 4;
210   resource.image_dither = true;
211   resource.root_command = NULL;
212   resource.opaque_move = false;
213
214   event_mask = ColormapChangeMask | EnterWindowMask | PropertyChangeMask |
215                SubstructureRedirectMask | KeyPressMask | KeyReleaseMask |
216                ButtonPressMask | ButtonReleaseMask;
217
218   XErrorHandler old = XSetErrorHandler((XErrorHandler) anotherWMRunning);
219   XSelectInput(getBaseDisplay().getXDisplay(), getRootWindow(), event_mask);
220   XSync(getBaseDisplay().getXDisplay(), False);
221   XSetErrorHandler((XErrorHandler) old);
222
223   managed = running;
224   if (! managed) return;
225
226   fprintf(stderr, i18n->getMessage(ScreenSet, ScreenManagingScreen,
227                      "BScreen::BScreen: managing screen %d "
228                      "using visual 0x%lx, depth %d\n"),
229           getScreenNumber(), XVisualIDFromVisual(getVisual()),
230           getDepth());
231
232   rootmenu = 0;
233
234   resource.mstyle.t_fontset = resource.mstyle.f_fontset =
235     resource.tstyle.fontset = resource.wstyle.fontset = NULL;
236   resource.mstyle.t_font = resource.mstyle.f_font = resource.tstyle.font =
237     resource.wstyle.font = NULL;
238
239 #ifdef   SLIT
240   slit = NULL;
241 #endif // SLIT
242   toolbar = NULL;
243
244 #ifdef    HAVE_GETPID
245   pid_t bpid = getpid();
246
247   XChangeProperty(getBaseDisplay().getXDisplay(), getRootWindow(),
248                   openbox.getOpenboxPidAtom(), XA_CARDINAL,
249                   sizeof(pid_t) * 8, PropModeReplace,
250                   (unsigned char *) &bpid, 1);
251 #endif // HAVE_GETPID
252
253   XDefineCursor(getBaseDisplay().getXDisplay(), getRootWindow(),
254                 openbox.getSessionCursor());
255
256   workspaceNames = new LinkedList<char>;
257   workspacesList = new LinkedList<Workspace>;
258   rootmenuList = new LinkedList<Rootmenu>;
259   netizenList = new LinkedList<Netizen>;
260   iconList = new LinkedList<OpenboxWindow>;
261
262   image_control =
263     new BImageControl(openbox, *this, True, openbox.getColorsPerChannel(),
264                       openbox.getCacheLife(), openbox.getCacheMax());
265   image_control->installRootColormap();
266   root_colormap_installed = True;
267
268   image_control->setDither(resource.image_dither);
269
270   load();       // load config options from Resources
271   LoadStyle();
272
273   XGCValues gcv;
274   unsigned long gc_value_mask = GCForeground;
275   if (! i18n->multibyte()) gc_value_mask |= GCFont;
276
277   gcv.foreground = WhitePixel(getBaseDisplay().getXDisplay(),
278                               getScreenNumber())
279                  ^ BlackPixel(getBaseDisplay().getXDisplay(),
280                               getScreenNumber());
281   gcv.function = GXxor;
282   gcv.subwindow_mode = IncludeInferiors;
283   opGC = XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
284                    GCForeground | GCFunction | GCSubwindowMode, &gcv);
285
286   gcv.foreground = resource.wstyle.l_text_focus.getPixel();
287   if (resource.wstyle.font)
288     gcv.font = resource.wstyle.font->fid;
289   resource.wstyle.l_text_focus_gc =
290     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
291               gc_value_mask, &gcv);
292
293   gcv.foreground = resource.wstyle.l_text_unfocus.getPixel();
294   if (resource.wstyle.font)
295     gcv.font = resource.wstyle.font->fid;
296   resource.wstyle.l_text_unfocus_gc =
297     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
298               gc_value_mask, &gcv);
299
300   gcv.foreground = resource.wstyle.b_pic_focus.getPixel();
301   resource.wstyle.b_pic_focus_gc =
302     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
303               GCForeground, &gcv);
304
305   gcv.foreground = resource.wstyle.b_pic_unfocus.getPixel();
306   resource.wstyle.b_pic_unfocus_gc =
307     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
308               GCForeground, &gcv);
309
310   gcv.foreground = resource.mstyle.t_text.getPixel();
311   if (resource.mstyle.t_font)
312     gcv.font = resource.mstyle.t_font->fid;
313   resource.mstyle.t_text_gc =
314     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
315               gc_value_mask, &gcv);
316
317   gcv.foreground = resource.mstyle.f_text.getPixel();
318   if (resource.mstyle.f_font)
319     gcv.font = resource.mstyle.f_font->fid;
320   resource.mstyle.f_text_gc =
321     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
322               gc_value_mask, &gcv);
323
324   gcv.foreground = resource.mstyle.h_text.getPixel();
325   resource.mstyle.h_text_gc =
326     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
327               gc_value_mask, &gcv);
328
329   gcv.foreground = resource.mstyle.d_text.getPixel();
330   resource.mstyle.d_text_gc =
331     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
332               gc_value_mask, &gcv);
333
334   gcv.foreground = resource.mstyle.hilite.getColor()->getPixel();
335   resource.mstyle.hilite_gc =
336     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
337               gc_value_mask, &gcv);
338
339   gcv.foreground = resource.tstyle.l_text.getPixel();
340   if (resource.tstyle.font)
341     gcv.font = resource.tstyle.font->fid;
342   resource.tstyle.l_text_gc =
343     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
344               gc_value_mask, &gcv);
345
346   gcv.foreground = resource.tstyle.w_text.getPixel();
347   resource.tstyle.w_text_gc =
348     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
349               gc_value_mask, &gcv);
350
351   gcv.foreground = resource.tstyle.c_text.getPixel();
352   resource.tstyle.c_text_gc =
353     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
354               gc_value_mask, &gcv);
355
356   gcv.foreground = resource.tstyle.b_pic.getPixel();
357   resource.tstyle.b_pic_gc =
358     XCreateGC(getBaseDisplay().getXDisplay(), getRootWindow(),
359               gc_value_mask, &gcv);
360
361   const char *s =  i18n->getMessage(ScreenSet, ScreenPositionLength,
362                                     "0: 0000 x 0: 0000");
363   int l = strlen(s);
364
365   if (i18n->multibyte()) {
366     XRectangle ink, logical;
367     XmbTextExtents(resource.wstyle.fontset, s, l, &ink, &logical);
368     geom_w = logical.width;
369
370     geom_h = resource.wstyle.fontset_extents->max_ink_extent.height;
371   } else {
372     geom_h = resource.wstyle.font->ascent +
373              resource.wstyle.font->descent;
374
375     geom_w = XTextWidth(resource.wstyle.font, s, l);
376   }
377
378   geom_w += (resource.bevel_width * 2);
379   geom_h += (resource.bevel_width * 2);
380
381   XSetWindowAttributes attrib;
382   unsigned long mask = CWBorderPixel | CWColormap | CWSaveUnder;
383   attrib.border_pixel = getBorderColor()->getPixel();
384   attrib.colormap = getColormap();
385   attrib.save_under = True;
386
387   geom_window =
388     XCreateWindow(getBaseDisplay().getXDisplay(), getRootWindow(),
389                   0, 0, geom_w, geom_h, resource.border_width, getDepth(),
390                   InputOutput, getVisual(), mask, &attrib);
391   geom_visible = False;
392
393   if (resource.wstyle.l_focus.getTexture() & BImage_ParentRelative) {
394     if (resource.wstyle.t_focus.getTexture() ==
395                                       (BImage_Flat | BImage_Solid)) {
396       geom_pixmap = None;
397       XSetWindowBackground(getBaseDisplay().getXDisplay(), geom_window,
398                            resource.wstyle.t_focus.getColor()->getPixel());
399     } else {
400       geom_pixmap = image_control->renderImage(geom_w, geom_h,
401                                                &resource.wstyle.t_focus);
402       XSetWindowBackgroundPixmap(getBaseDisplay().getXDisplay(),
403                                  geom_window, geom_pixmap);
404     }
405   } else {
406     if (resource.wstyle.l_focus.getTexture() ==
407                                       (BImage_Flat | BImage_Solid)) {
408       geom_pixmap = None;
409       XSetWindowBackground(getBaseDisplay().getXDisplay(), geom_window,
410                            resource.wstyle.l_focus.getColor()->getPixel());
411     } else {
412       geom_pixmap = image_control->renderImage(geom_w, geom_h,
413                                                &resource.wstyle.l_focus);
414       XSetWindowBackgroundPixmap(getBaseDisplay().getXDisplay(),
415                                  geom_window, geom_pixmap);
416     }
417   }
418
419   workspacemenu = new Workspacemenu(*this);
420   iconmenu = new Iconmenu(*this);
421   configmenu = new Configmenu(*this);
422
423   Workspace *wkspc = NULL;
424   if (resource.workspaces != 0) {
425     for (int i = 0; i < resource.workspaces; ++i) {
426       wkspc = new Workspace(*this, workspacesList->count());
427       workspacesList->insert(wkspc);
428       saveWorkspaceNames();
429       workspacemenu->insert(wkspc->getName(), wkspc->getMenu());
430     }
431   } else {
432     wkspc = new Workspace(*this, workspacesList->count());
433     workspacesList->insert(wkspc);
434     saveWorkspaceNames();
435     workspacemenu->insert(wkspc->getName(), wkspc->getMenu());
436   }
437
438   workspacemenu->insert(i18n->getMessage(IconSet, IconIcons, "Icons"),
439                         iconmenu);
440   workspacemenu->update();
441
442   current_workspace = workspacesList->first();
443   workspacemenu->setItemSelected(2, True);
444
445   toolbar = new Toolbar(*this, config);
446
447 #ifdef    SLIT
448   slit = new Slit(*this, config);
449 #endif // SLIT
450
451   InitMenu();
452
453   raiseWindows(0, 0);
454   rootmenu->update();
455
456   changeWorkspaceID(0);
457
458   int i;
459   unsigned int nchild;
460   Window r, p, *children;
461   XQueryTree(getBaseDisplay().getXDisplay(), getRootWindow(), &r, &p,
462              &children, &nchild);
463
464   // preen the window list of all icon windows... for better dockapp support
465   for (i = 0; i < (int) nchild; i++) {
466     if (children[i] == None) continue;
467
468     XWMHints *wmhints = XGetWMHints(getBaseDisplay().getXDisplay(),
469                                     children[i]);
470
471     if (wmhints) {
472       if ((wmhints->flags & IconWindowHint) &&
473           (wmhints->icon_window != children[i]))
474         for (int j = 0; j < (int) nchild; j++)
475           if (children[j] == wmhints->icon_window) {
476             children[j] = None;
477
478             break;
479           }
480
481       XFree(wmhints);
482     }
483   }
484
485   // manage shown windows
486   for (i = 0; i < (int) nchild; ++i) {
487     if (children[i] == None || (! openbox.validateWindow(children[i])))
488       continue;
489
490     XWindowAttributes attrib;
491     if (XGetWindowAttributes(getBaseDisplay().getXDisplay(), children[i],
492                              &attrib)) {
493       if (attrib.override_redirect) continue;
494
495       if (attrib.map_state != IsUnmapped) {
496         new OpenboxWindow(openbox, children[i], this);
497
498         OpenboxWindow *win = openbox.searchWindow(children[i]);
499         if (win) {
500           XMapRequestEvent mre;
501           mre.window = children[i];
502           win->restoreAttributes();
503           win->mapRequestEvent(&mre);
504         }
505       }
506     }
507   }
508
509   if (! resource.sloppy_focus)
510     XSetInputFocus(getBaseDisplay().getXDisplay(), toolbar->getWindowID(),
511                    RevertToParent, CurrentTime);
512
513   XFree(children);
514   XFlush(getBaseDisplay().getXDisplay());
515 }
516
517
518 BScreen::~BScreen(void) {
519   if (! managed) return;
520
521   if (geom_pixmap != None)
522     image_control->removeImage(geom_pixmap);
523
524   if (geom_window != None)
525     XDestroyWindow(getBaseDisplay().getXDisplay(), geom_window);
526
527   removeWorkspaceNames();
528
529   while (workspacesList->count())
530     delete workspacesList->remove(0);
531
532   while (rootmenuList->count())
533     rootmenuList->remove(0);
534
535   while (iconList->count())
536     delete iconList->remove(0);
537
538   while (netizenList->count())
539     delete netizenList->remove(0);
540
541 #ifdef    HAVE_STRFTIME
542   if (resource.strftime_format)
543     delete [] resource.strftime_format;
544 #endif // HAVE_STRFTIME
545
546   delete rootmenu;
547   delete workspacemenu;
548   delete iconmenu;
549   delete configmenu;
550
551 #ifdef    SLIT
552   delete slit;
553 #endif // SLIT
554
555   delete toolbar;
556   delete image_control;
557
558   delete workspacesList;
559   delete workspaceNames;
560   delete rootmenuList;
561   delete iconList;
562   delete netizenList;
563
564   if (resource.wstyle.fontset)
565     XFreeFontSet(getBaseDisplay().getXDisplay(), resource.wstyle.fontset);
566   if (resource.mstyle.t_fontset)
567     XFreeFontSet(getBaseDisplay().getXDisplay(), resource.mstyle.t_fontset);
568   if (resource.mstyle.f_fontset)
569     XFreeFontSet(getBaseDisplay().getXDisplay(), resource.mstyle.f_fontset);
570   if (resource.tstyle.fontset)
571     XFreeFontSet(getBaseDisplay().getXDisplay(), resource.tstyle.fontset);
572
573   if (resource.wstyle.font)
574     XFreeFont(getBaseDisplay().getXDisplay(), resource.wstyle.font);
575   if (resource.mstyle.t_font)
576     XFreeFont(getBaseDisplay().getXDisplay(), resource.mstyle.t_font);
577   if (resource.mstyle.f_font)
578     XFreeFont(getBaseDisplay().getXDisplay(), resource.mstyle.f_font);
579   if (resource.tstyle.font)
580     XFreeFont(getBaseDisplay().getXDisplay(), resource.tstyle.font);
581   if (resource.root_command != NULL)
582     delete [] resource.root_command;
583
584   XFreeGC(getBaseDisplay().getXDisplay(), opGC);
585
586   XFreeGC(getBaseDisplay().getXDisplay(),
587           resource.wstyle.l_text_focus_gc);
588   XFreeGC(getBaseDisplay().getXDisplay(),
589           resource.wstyle.l_text_unfocus_gc);
590   XFreeGC(getBaseDisplay().getXDisplay(),
591           resource.wstyle.b_pic_focus_gc);
592   XFreeGC(getBaseDisplay().getXDisplay(),
593           resource.wstyle.b_pic_unfocus_gc);
594
595   XFreeGC(getBaseDisplay().getXDisplay(),
596           resource.mstyle.t_text_gc);
597   XFreeGC(getBaseDisplay().getXDisplay(),
598           resource.mstyle.f_text_gc);
599   XFreeGC(getBaseDisplay().getXDisplay(),
600           resource.mstyle.h_text_gc);
601   XFreeGC(getBaseDisplay().getXDisplay(),
602           resource.mstyle.d_text_gc);
603   XFreeGC(getBaseDisplay().getXDisplay(),
604           resource.mstyle.hilite_gc);
605
606   XFreeGC(getBaseDisplay().getXDisplay(),
607           resource.tstyle.l_text_gc);
608   XFreeGC(getBaseDisplay().getXDisplay(),
609           resource.tstyle.w_text_gc);
610   XFreeGC(getBaseDisplay().getXDisplay(),
611           resource.tstyle.c_text_gc);
612   XFreeGC(getBaseDisplay().getXDisplay(),
613           resource.tstyle.b_pic_gc);
614 }
615
616 void BScreen::readDatabaseTexture(const char *rname, const char *rclass,
617                                   BTexture *texture,
618                                   unsigned long default_pixel)
619 {
620   std::string s;
621   
622   if (resource.styleconfig.getValue(rname, rclass, s))
623     image_control->parseTexture(texture, s.c_str());
624   else
625     texture->setTexture(BImage_Solid | BImage_Flat);
626
627   if (texture->getTexture() & BImage_Solid) {
628     int clen = strlen(rclass) + 32, nlen = strlen(rname) + 32;
629
630     char *colorclass = new char[clen], *colorname = new char[nlen];
631
632     sprintf(colorclass, "%s.Color", rclass);
633     sprintf(colorname,  "%s.color", rname);
634
635     readDatabaseColor(colorname, colorclass, texture->getColor(),
636                       default_pixel);
637
638 #ifdef    INTERLACE
639     sprintf(colorclass, "%s.ColorTo", rclass);
640     sprintf(colorname,  "%s.colorTo", rname);
641
642     readDatabaseColor(colorname, colorclass, texture->getColorTo(),
643                       default_pixel);
644 #endif // INTERLACE
645
646     delete [] colorclass;
647     delete [] colorname;
648
649     if ((! texture->getColor()->isAllocated()) ||
650         (texture->getTexture() & BImage_Flat))
651       return;
652
653     XColor xcol;
654
655     xcol.red = (unsigned int) (texture->getColor()->getRed() +
656                                (texture->getColor()->getRed() >> 1));
657     if (xcol.red >= 0xff) xcol.red = 0xffff;
658     else xcol.red *= 0xff;
659     xcol.green = (unsigned int) (texture->getColor()->getGreen() +
660                                  (texture->getColor()->getGreen() >> 1));
661     if (xcol.green >= 0xff) xcol.green = 0xffff;
662     else xcol.green *= 0xff;
663     xcol.blue = (unsigned int) (texture->getColor()->getBlue() +
664                                 (texture->getColor()->getBlue() >> 1));
665     if (xcol.blue >= 0xff) xcol.blue = 0xffff;
666     else xcol.blue *= 0xff;
667
668     if (! XAllocColor(getBaseDisplay().getXDisplay(),
669                       getColormap(), &xcol))
670       xcol.pixel = 0;
671
672     texture->getHiColor()->setPixel(xcol.pixel);
673
674     xcol.red =
675       (unsigned int) ((texture->getColor()->getRed() >> 2) +
676                       (texture->getColor()->getRed() >> 1)) * 0xff;
677     xcol.green =
678       (unsigned int) ((texture->getColor()->getGreen() >> 2) +
679                       (texture->getColor()->getGreen() >> 1)) * 0xff;
680     xcol.blue =
681       (unsigned int) ((texture->getColor()->getBlue() >> 2) +
682                       (texture->getColor()->getBlue() >> 1)) * 0xff;
683
684     if (! XAllocColor(getBaseDisplay().getXDisplay(),
685                       getColormap(), &xcol))
686       xcol.pixel = 0;
687
688     texture->getLoColor()->setPixel(xcol.pixel);
689   } else if (texture->getTexture() & BImage_Gradient) {
690     int clen = strlen(rclass) + 10, nlen = strlen(rname) + 10;
691
692     char *colorclass = new char[clen], *colorname = new char[nlen],
693       *colortoclass = new char[clen], *colortoname = new char[nlen];
694
695     sprintf(colorclass, "%s.Color", rclass);
696     sprintf(colorname,  "%s.color", rname);
697
698     sprintf(colortoclass, "%s.ColorTo", rclass);
699     sprintf(colortoname,  "%s.colorTo", rname);
700
701     readDatabaseColor(colorname, colorclass, texture->getColor(),
702                       default_pixel);
703     readDatabaseColor(colortoname, colortoclass, texture->getColorTo(),
704                       default_pixel);
705
706     delete [] colorclass;
707     delete [] colorname;
708     delete [] colortoclass;
709     delete [] colortoname;
710   }
711 }
712
713
714 void BScreen::readDatabaseColor(const char *rname, const  char *rclass,
715                                 BColor *color, unsigned long default_pixel)
716 {
717   std::string s;
718   
719   if (resource.styleconfig.getValue(rname, rclass, s))
720     image_control->parseColor(color, s.c_str());
721   else {
722     // parsing with no color string just deallocates the color, if it has
723     // been previously allocated
724     image_control->parseColor(color);
725     color->setPixel(default_pixel);
726   }
727 }
728
729
730 void BScreen::readDatabaseFontSet(const char *rname, const char *rclass,
731                                   XFontSet *fontset) {
732   if (! fontset) return;
733
734   static char *defaultFont = "fixed";
735   bool load_default = false;
736   std::string s;
737
738   if (*fontset)
739     XFreeFontSet(getBaseDisplay().getXDisplay(), *fontset);
740
741   if (resource.styleconfig.getValue(rname, rclass, s)) {
742     if (! (*fontset = createFontSet(s.c_str())))
743       load_default = true;
744   } else
745     load_default = true;
746
747   if (load_default) {
748     *fontset = createFontSet(defaultFont);
749
750     if (! *fontset) {
751       fprintf(stderr, i18n->getMessage(ScreenSet, ScreenDefaultFontLoadFail,
752                        "BScreen::LoadStyle(): couldn't load default font.\n"));
753       exit(2);
754     }
755   }
756 }
757
758
759 void BScreen::readDatabaseFont(const char *rname, const char *rclass,
760                                XFontStruct **font) {
761   if (! font) return;
762
763   static char *defaultFont = "fixed";
764   bool load_default = false;
765   std::string s;
766
767   if (*font)
768     XFreeFont(getBaseDisplay().getXDisplay(), *font);
769
770   if (resource.styleconfig.getValue(rname, rclass, s)) {
771     if ((*font = XLoadQueryFont(getBaseDisplay().getXDisplay(),
772                                 s.c_str())) == NULL) {
773       fprintf(stderr, i18n->getMessage(ScreenSet, ScreenFontLoadFail,
774                          "BScreen::LoadStyle(): couldn't load font '%s'\n"),
775               s.c_str());
776       load_default = true;
777     }
778   } else
779     load_default = true;
780
781   if (load_default) {
782     if ((*font = XLoadQueryFont(getBaseDisplay().getXDisplay(),
783                                 defaultFont)) == NULL) {
784       fprintf(stderr, i18n->getMessage(ScreenSet, ScreenDefaultFontLoadFail,
785                  "BScreen::LoadStyle(): couldn't load default font.\n"));
786       exit(2);
787     }
788   }
789 }
790
791
792 XFontSet BScreen::createFontSet(const char *fontname) {
793   XFontSet fs;
794   char **missing, *def = "-";
795   int nmissing, pixel_size = 0, buf_size = 0;
796   char weight[FONT_ELEMENT_SIZE], slant[FONT_ELEMENT_SIZE];
797
798   fs = XCreateFontSet(getBaseDisplay().getXDisplay(),
799                       fontname, &missing, &nmissing, &def);
800   if (fs && (! nmissing)) return fs;
801
802 #ifdef    HAVE_SETLOCALE
803   if (! fs) {
804     if (nmissing) XFreeStringList(missing);
805
806     setlocale(LC_CTYPE, "C");
807     fs = XCreateFontSet(getBaseDisplay().getXDisplay(), fontname,
808                         &missing, &nmissing, &def);
809     setlocale(LC_CTYPE, "");
810   }
811 #endif // HAVE_SETLOCALE
812
813   if (fs) {
814     XFontStruct **fontstructs;
815     char **fontnames;
816     XFontsOfFontSet(fs, &fontstructs, &fontnames);
817     fontname = fontnames[0];
818   }
819
820   getFontElement(fontname, weight, FONT_ELEMENT_SIZE,
821                  "-medium-", "-bold-", "-demibold-", "-regular-", NULL);
822   getFontElement(fontname, slant, FONT_ELEMENT_SIZE,
823                  "-r-", "-i-", "-o-", "-ri-", "-ro-", NULL);
824   getFontSize(fontname, &pixel_size);
825
826   if (! strcmp(weight, "*")) strncpy(weight, "medium", FONT_ELEMENT_SIZE);
827   if (! strcmp(slant, "*")) strncpy(slant, "r", FONT_ELEMENT_SIZE);
828   if (pixel_size < 3) pixel_size = 3;
829   else if (pixel_size > 97) pixel_size = 97;
830
831   buf_size = strlen(fontname) + (FONT_ELEMENT_SIZE * 2) + 64;
832   char *pattern2 = new char[buf_size];
833   snprintf(pattern2, buf_size - 1,
834            "%s,"
835            "-*-*-%s-%s-*-*-%d-*-*-*-*-*-*-*,"
836            "-*-*-*-*-*-*-%d-*-*-*-*-*-*-*,*",
837            fontname, weight, slant, pixel_size, pixel_size);
838   fontname = pattern2;
839
840   if (nmissing) XFreeStringList(missing);
841   if (fs) XFreeFontSet(getBaseDisplay().getXDisplay(), fs);
842
843   fs = XCreateFontSet(getBaseDisplay().getXDisplay(), fontname,
844                       &missing, &nmissing, &def);
845   delete [] pattern2;
846
847   return fs;
848 }
849
850
851 void BScreen::setSloppyFocus(bool b) {
852   resource.sloppy_focus = b;
853   ostrstream s;
854   s << "session.screen" << getScreenNumber() << ".focusModel" << ends;
855   config.setValue(s.str(),
856                   (resource.sloppy_focus ?
857                   (resource.auto_raise ? "AutoRaiseSloppyFocus" : "SloppyFocus")
858                   : "ClickToFocus"));
859   s.rdbuf()->freeze(0);
860 }
861
862
863 void BScreen::setAutoRaise(bool a) {
864   resource.auto_raise = a;
865   ostrstream s;
866   s << "session.screen" << getScreenNumber() << ".focusModel" << ends;
867   config.setValue(s.str(),
868                   (resource.sloppy_focus ?
869                   (resource.auto_raise ? "AutoRaiseSloppyFocus" : "SloppyFocus")
870                   : "ClickToFocus"));
871   s.rdbuf()->freeze(0);
872 }
873
874
875 void BScreen::setImageDither(bool d) {
876   resource.image_dither = d;
877   ostrstream s;
878   s << "session.screen" << getScreenNumber() << ".imageDither" << ends;
879   config.setValue(s.str(), resource.image_dither);
880   s.rdbuf()->freeze(0);
881 }
882
883
884 void BScreen::setOpaqueMove(bool o) {
885   resource.opaque_move = o;
886   ostrstream s;
887   s << "session.screen" << getScreenNumber() << ".opaqueMove" << ends;
888   config.setValue(s.str(), resource.opaque_move);
889   s.rdbuf()->freeze(0);
890 }
891
892
893 void BScreen::setFullMax(bool f) {
894   resource.full_max = f;
895   ostrstream s;
896   s << "session.screen" << getScreenNumber() << ".fullMaximization" << ends;
897   config.setValue(s.str(), resource.full_max);
898   s.rdbuf()->freeze(0);
899 }
900
901
902 void BScreen::setFocusNew(bool f) {
903   resource.focus_new = f;
904   ostrstream s;
905   s << "session.screen" << getScreenNumber() << ".focusNewWindows" << ends;
906   config.setValue(s.str(), resource.focus_new);
907   s.rdbuf()->freeze(0);
908 }
909
910
911 void BScreen::setFocusLast(bool f) {
912   resource.focus_last = f;
913   ostrstream s;
914   s << "session.screen" << getScreenNumber() << ".focusLastWindow" << ends;
915   config.setValue(s.str(), resource.focus_last);
916   s.rdbuf()->freeze(0);
917 }
918
919
920 void BScreen::setWindowZones(int z) {
921   resource.zones = z;
922   ostrstream s;
923   s << "session.screen" << getScreenNumber() << ".windowZones" << ends;
924   config.setValue(s.str(), resource.zones);
925   s.rdbuf()->freeze(0);
926 }
927
928
929 void BScreen::setWorkspaceCount(int w) {
930   resource.workspaces = w;
931   ostrstream s;
932   s << "session.screen" << getScreenNumber() << ".workspaces" << ends;
933   config.setValue(s.str(), resource.workspaces);
934   s.rdbuf()->freeze(0);
935 }
936
937
938 void BScreen::setPlacementPolicy(int p) {
939   resource.placement_policy = p;
940   ostrstream s;
941   s << "session.screen" << getScreenNumber() << ".windowPlacement" << ends;
942   const char *placement;
943   switch (resource.placement_policy) {
944   case CascadePlacement: placement = "CascadePlacement"; break;
945   case BestFitPlacement: placement = "BestFitPlacement"; break;
946   case ColSmartPlacement: placement = "ColSmartPlacement"; break;
947   default:
948   case RowSmartPlacement: placement = "RowSmartPlacement"; break;
949   }
950   config.setValue(s.str(), placement);
951   s.rdbuf()->freeze(0);
952 }
953
954
955 void BScreen::setEdgeSnapThreshold(int t) {
956   resource.edge_snap_threshold = t;
957   ostrstream s;
958   s << "session.screen" << getScreenNumber() << ".edgeSnapThreshold" << ends;
959   config.setValue(s.str(), resource.edge_snap_threshold);
960   s.rdbuf()->freeze(0);
961 }
962
963
964 void BScreen::setRowPlacementDirection(int d) {
965   resource.row_direction = d;
966   ostrstream s;
967   s << "session.screen" << getScreenNumber() << ".rowPlacementDirection" <<
968     ends;
969   config.setValue(s.str(),
970                   resource.row_direction == LeftRight ?
971                   "LeftToRight" : "RightToLeft");
972   s.rdbuf()->freeze(0);
973 }
974
975
976 void BScreen::setColPlacementDirection(int d) {
977   resource.col_direction = d;
978   ostrstream s;
979   s << "session.screen" << getScreenNumber() << ".colPlacementDirection" <<
980     ends;
981   config.setValue(s.str(),
982                   resource.col_direction == TopBottom ?
983                   "TopToBottom" : "BottomToTop");
984   s.rdbuf()->freeze(0);
985 }
986
987
988 void BScreen::setRootCommand(const char *cmd) {
989 if (resource.root_command != NULL)
990     delete [] resource.root_command;
991   if (cmd != NULL)
992     resource.root_command = bstrdup(cmd);
993   else
994     resource.root_command = NULL;
995   // this doesn't save to the Resources config because it can't be changed
996   // inside Openbox, and this way we dont add an empty command which would over-
997   // ride the styles command when none has been specified
998 }
999
1000
1001 #ifdef    HAVE_STRFTIME
1002 void BScreen::setStrftimeFormat(const char *f) {
1003   if (resource.strftime_format != NULL)
1004     delete [] resource.strftime_format;
1005
1006   resource.strftime_format = bstrdup(f);
1007   ostrstream s;
1008   s << "session.screen" << getScreenNumber() << ".strftimeFormat" << ends;
1009   config.setValue(s.str(), resource.strftime_format);
1010   s.rdbuf()->freeze(0);
1011 }
1012
1013 #else // !HAVE_STRFTIME
1014 void BScreen::setDateFormat(int f) {
1015   resource.date_format = f;
1016   ostrstream s;
1017   s << "session.screen" << getScreenNumber() << ".dateFormat" << ends;
1018   config.setValue(s.str(), resource.date_format == B_EuropeanDate ?
1019                   "European" : "American");
1020   s.rdbuf()->freeze(0);
1021 }
1022
1023 void BScreen::setClock24Hour(Bool c) {
1024   resource.clock24hour = c;
1025   ostrstream s;
1026   s << "session.screen" << getScreenNumber() << ".clockFormat" << ends;
1027   config.setValue(s.str(), resource.clock24hour ? 24 : 12);
1028   s.rdbuf()->freeze(0);
1029 }
1030 #endif // HAVE_STRFTIME
1031
1032 void BScreen::setHideToolbar(bool b) {
1033   resource.hide_toolbar = b;
1034   if (resource.hide_toolbar)
1035     getToolbar()->unMapToolbar();
1036   else
1037     getToolbar()->mapToolbar();
1038   ostrstream s;
1039   s << "session.screen" << getScreenNumber() << ".hideToolbar" << ends;
1040   config.setValue(s.str(), resource.hide_toolbar ? "True" : "False");
1041   s.rdbuf()->freeze(0);
1042 }
1043
1044 void BScreen::saveWorkspaceNames() {
1045   ostrstream rc, names;
1046
1047   for (int i = 0; i < resource.workspaces; i++) {
1048     Workspace *w = getWorkspace(i);
1049     if (w != NULL) {
1050       names << w->getName();
1051       if (i < resource.workspaces-1)
1052         names << ',';
1053     }
1054   }
1055   names << ends;
1056
1057   rc << "session.screen" << getScreenNumber() << ".workspaceNames" << ends;
1058   config.setValue(rc.str(), names.str());
1059   rc.rdbuf()->freeze(0);
1060   names.rdbuf()->freeze(0);
1061 }
1062
1063 void BScreen::save() {
1064   setSloppyFocus(resource.sloppy_focus);
1065   setAutoRaise(resource.auto_raise);
1066   setImageDither(resource.image_dither);
1067   setOpaqueMove(resource.opaque_move);
1068   setFullMax(resource.full_max);
1069   setFocusNew(resource.focus_new);
1070   setFocusLast(resource.focus_last);
1071   setWindowZones(resource.zones);
1072   setWorkspaceCount(resource.workspaces);
1073   setPlacementPolicy(resource.placement_policy);
1074   setEdgeSnapThreshold(resource.edge_snap_threshold);
1075   setRowPlacementDirection(resource.row_direction);
1076   setColPlacementDirection(resource.col_direction);
1077   setRootCommand(resource.root_command);
1078 #ifdef    HAVE_STRFTIME
1079   // it deletes the current value before setting the new one, so we have to
1080   // duplicate the current value.
1081   std::string s = resource.strftime_format;
1082   setStrftimeFormat(s.c_str()); 
1083 #else // !HAVE_STRFTIME
1084   setDateFormat(resource.date_format);
1085   setClock24Hour(resource.clock24hour);
1086 #endif // HAVE_STRFTIME
1087   setHideToolbar(resource.hide_toolbar);
1088 }
1089
1090 void BScreen::load() {
1091   std::ostrstream rscreen, rname, rclass;
1092   std::string s;
1093   bool b;
1094   long l;
1095   rscreen << "session.screen" << getScreenNumber() << '.' << ends;
1096
1097   rname << rscreen.str() << "hideToolbar" << ends;
1098   rclass << rscreen.str() << "HideToolbar" << ends;
1099   if (config.getValue(rname.str(), rclass.str(), b))
1100     resource.hide_toolbar = b;
1101   Toolbar *t = getToolbar();
1102   if (t != NULL) {
1103     if (resource.hide_toolbar)
1104       t->unMapToolbar();
1105     else
1106       t->mapToolbar();
1107   }
1108
1109   rname.seekp(0); rclass.seekp(0);
1110   rname << rscreen.str() << "fullMaximization" << ends;
1111   rclass << rscreen.str() << "FullMaximization" << ends;
1112   if (config.getValue(rname.str(), rclass.str(), b))
1113     resource.full_max = b;
1114
1115   rname.seekp(0); rclass.seekp(0);
1116   rname << rscreen.str() << "focusNewWindows" << ends;
1117   rclass << rscreen.str() << "FocusNewWindows" << ends;
1118   if (config.getValue(rname.str(), rclass.str(), b))
1119     resource.focus_new = b;
1120
1121   rname.seekp(0); rclass.seekp(0);
1122   rname << rscreen.str() << "focusLastWindow" << ends;
1123   rclass << rscreen.str() << "FocusLastWindow" << ends;
1124   if (config.getValue(rname.str(), rclass.str(), b))
1125     resource.focus_last = b;
1126
1127   rname.seekp(0); rclass.seekp(0);
1128   rname << rscreen.str() << "rowPlacementDirection" << ends;
1129   rclass << rscreen.str() << "RowPlacementDirection" << ends;
1130   if (config.getValue(rname.str(), rclass.str(), s)) {
1131     if (0 == strncasecmp(s.c_str(), "RightToLeft", s.length()))
1132       resource.row_direction = RightLeft;
1133     else if (0 == strncasecmp(s.c_str(), "LeftToRight", s.length()))
1134       resource.row_direction = LeftRight;
1135   }
1136
1137   rname.seekp(0); rclass.seekp(0);
1138   rname << rscreen.str() << "colPlacementDirection" << ends;
1139   rclass << rscreen.str() << "ColPlacementDirection" << ends;
1140   if (config.getValue(rname.str(), rclass.str(), s)) {
1141     if (0 == strncasecmp(s.c_str(), "BottomToTop", s.length()))
1142       resource.col_direction = BottomTop;
1143     else if (0 == strncasecmp(s.c_str(), "TopToBottom", s.length()))
1144       resource.col_direction = TopBottom;
1145   }
1146
1147   rname.seekp(0); rclass.seekp(0);
1148   rname << rscreen.str() << "workspaces" << ends;
1149   rclass << rscreen.str() << "Workspaces" << ends;
1150   if (config.getValue(rname.str(), rclass.str(), l))
1151     resource.workspaces = l;
1152
1153   removeWorkspaceNames();
1154   rname.seekp(0); rclass.seekp(0);
1155   rname << rscreen.str() << "workspaceNames" << ends;
1156   rclass << rscreen.str() << "WorkspaceNames" << ends;
1157   if (config.getValue(rname.str(), rclass.str(), s)) {
1158     std::string::const_iterator it = s.begin(), end = s.end();
1159     while(1) {
1160       std::string::const_iterator tmp = it;// current string.begin()
1161       it = std::find(tmp, end, ',');       // look for comma between tmp and end
1162       std::string name(tmp, it);           // name = s[tmp:it]
1163       addWorkspaceName(name.c_str());
1164       if (it == end)
1165         break;
1166       ++it;
1167     }
1168   }
1169   
1170   rname.seekp(0); rclass.seekp(0);
1171   rname << rscreen.str() << "focusModel" << ends;
1172   rclass << rscreen.str() << "FocusModel" << ends;
1173   if (config.getValue(rname.str(), rclass.str(), s)) {
1174     if (0 == strncasecmp(s.c_str(), "ClickToFocus", s.length())) {
1175       resource.auto_raise = false;
1176       resource.sloppy_focus = false;
1177     } else if (0 == strncasecmp(s.c_str(), "AutoRaiseSloppyFocus",
1178                                 s.length())) {
1179       resource.sloppy_focus = true;
1180       resource.auto_raise = true;
1181     } else if (0 == strncasecmp(s.c_str(), "SloppyFocus", s.length())) {
1182       resource.sloppy_focus = true;
1183       resource.auto_raise = false;
1184     }
1185   }
1186
1187   rname.seekp(0); rclass.seekp(0);
1188   rname << rscreen.str() << "windowZones" << ends;
1189   rclass << rscreen.str() << "WindowZones" << ends;
1190   if (config.getValue(rname.str(), rclass.str(), l))
1191     resource.zones = (l == 1 || l == 2 || l == 4) ? l : 1;
1192
1193   rname.seekp(0); rclass.seekp(0);
1194   rname << rscreen.str() << "windowPlacement" << ends;
1195   rclass << rscreen.str() << "WindowPlacement" << ends;
1196   if (config.getValue(rname.str(), rclass.str(), s)) {
1197     if (0 == strncasecmp(s.c_str(), "RowSmartPlacement", s.length()))
1198       resource.placement_policy = RowSmartPlacement;
1199     else if (0 == strncasecmp(s.c_str(), "ColSmartPlacement", s.length()))
1200       resource.placement_policy = ColSmartPlacement;
1201     else if (0 == strncasecmp(s.c_str(), "BestFitPlacement", s.length()))
1202       resource.placement_policy = BestFitPlacement;
1203     else if (0 == strncasecmp(s.c_str(), "CascadePlacement", s.length()))
1204       resource.placement_policy = CascadePlacement;
1205   }
1206
1207 #ifdef    HAVE_STRFTIME
1208   rname.seekp(0); rclass.seekp(0);
1209   rname << rscreen.str() << "strftimeFormat" << ends;
1210   rclass << rscreen.str() << "StrftimeFormat" << ends;
1211
1212   if (resource.strftime_format != NULL){
1213     delete [] resource.strftime_format;
1214     resource.strftime_format=NULL;
1215   }
1216
1217   if (config.getValue(rname.str(), rclass.str(), s)) {
1218     resource.strftime_format = bstrdup(s.c_str());
1219   }
1220 #else // !HAVE_STRFTIME
1221   rname.seekp(0); rclass.seekp(0);
1222   rname << rscreen.str() << "dateFormat" << ends;
1223   rclass << rscreen.str() << "DateFormat" << ends;
1224   if (config.getValue(rname.str(), rclass.str(), s)) {
1225     if (strncasecmp(s.c_str(), "European", s.length()))
1226       resource.date_format = B_EuropeanDate;
1227     else if (strncasecmp(s.c_str(), "American", s.length()))
1228       resource.date_format = B_AmericanDate;
1229   }
1230
1231   rname.seekp(0); rclass.seekp(0);
1232   rname << rscreen.str() << "clockFormat" << ends;
1233   rclass << rscreen.str() << "ClockFormat" << ends;
1234   if (config.getValue(rname.str(), rclass.str(), l)) {
1235     if (clock == 24)
1236       resource.clock24hour = true;
1237     else if (clock == 12)
1238       resource.clock24hour =  false;
1239 #endif // HAVE_STRFTIME
1240
1241   rname.seekp(0); rclass.seekp(0);
1242   rname << rscreen.str() << "edgeSnapThreshold" << ends;
1243   rclass << rscreen.str() << "EdgeSnapThreshold" << ends;
1244   if (config.getValue(rname.str(), rclass.str(), l))
1245     resource.edge_snap_threshold = l;
1246
1247   rname.seekp(0); rclass.seekp(0);
1248   rname << rscreen.str() << "imageDither" << ends;
1249   rclass << rscreen.str() << "ImageDither" << ends;
1250   if (config.getValue(rname.str(), rclass.str(), b))
1251     resource.image_dither = b;
1252
1253   rname.seekp(0); rclass.seekp(0);
1254   rname << rscreen.str() << "rootCommand" << ends;
1255   rclass << rscreen.str() << "RootCommand" << ends;
1256
1257   if (resource.root_command != NULL){
1258     delete [] resource.root_command;
1259     resource.root_command=NULL;
1260   }
1261   if (config.getValue(rname.str(), rclass.str(), s)) {
1262     resource.root_command = bstrdup(s.c_str());
1263   }
1264
1265   rname.seekp(0); rclass.seekp(0);
1266   rname << rscreen.str() << "opaqueMove" << ends;
1267   rclass << rscreen.str() << "OpaqueMove" << ends;
1268   if (config.getValue(rname.str(), rclass.str(), b))
1269     resource.opaque_move = b;
1270   rscreen.rdbuf()->freeze(0);
1271   rname.rdbuf()->freeze(0);
1272   rclass.rdbuf()->freeze(0);
1273 }
1274
1275 void BScreen::reconfigure(void) {
1276   load();
1277   toolbar->load();
1278 #ifdef    SLIT
1279   slit->load();
1280 #endif // SLIT
1281   LoadStyle();
1282
1283   XGCValues gcv;
1284   unsigned long gc_value_mask = GCForeground;
1285   if (! i18n->multibyte()) gc_value_mask |= GCFont;
1286
1287   gcv.foreground = WhitePixel(getBaseDisplay().getXDisplay(),
1288                               getScreenNumber());
1289   gcv.function = GXinvert;
1290   gcv.subwindow_mode = IncludeInferiors;
1291   XChangeGC(getBaseDisplay().getXDisplay(), opGC,
1292             GCForeground | GCFunction | GCSubwindowMode, &gcv);
1293
1294   gcv.foreground = resource.wstyle.l_text_focus.getPixel();
1295   if (resource.wstyle.font)
1296     gcv.font = resource.wstyle.font->fid;
1297   XChangeGC(getBaseDisplay().getXDisplay(), resource.wstyle.l_text_focus_gc,
1298             gc_value_mask, &gcv);
1299
1300   gcv.foreground = resource.wstyle.l_text_unfocus.getPixel();
1301   XChangeGC(getBaseDisplay().getXDisplay(), resource.wstyle.l_text_unfocus_gc,
1302             gc_value_mask, &gcv);
1303
1304   gcv.foreground = resource.wstyle.b_pic_focus.getPixel();
1305   XChangeGC(getBaseDisplay().getXDisplay(), resource.wstyle.b_pic_focus_gc,
1306             GCForeground, &gcv);
1307
1308   gcv.foreground = resource.wstyle.b_pic_unfocus.getPixel();
1309   XChangeGC(getBaseDisplay().getXDisplay(), resource.wstyle.b_pic_unfocus_gc,
1310             GCForeground, &gcv);
1311
1312   gcv.foreground = resource.mstyle.t_text.getPixel();
1313   if (resource.mstyle.t_font)
1314     gcv.font = resource.mstyle.t_font->fid;
1315   XChangeGC(getBaseDisplay().getXDisplay(), resource.mstyle.t_text_gc,
1316             gc_value_mask, &gcv);
1317
1318   gcv.foreground = resource.mstyle.f_text.getPixel();
1319   if (resource.mstyle.f_font)
1320     gcv.font = resource.mstyle.f_font->fid;
1321   XChangeGC(getBaseDisplay().getXDisplay(), resource.mstyle.f_text_gc,
1322             gc_value_mask, &gcv);
1323
1324   gcv.foreground = resource.mstyle.h_text.getPixel();
1325   XChangeGC(getBaseDisplay().getXDisplay(), resource.mstyle.h_text_gc,
1326             gc_value_mask, &gcv);
1327
1328   gcv.foreground = resource.mstyle.d_text.getPixel();
1329   XChangeGC(getBaseDisplay().getXDisplay(), resource.mstyle.d_text_gc,
1330             gc_value_mask, &gcv);
1331
1332   gcv.foreground = resource.mstyle.hilite.getColor()->getPixel();
1333   XChangeGC(getBaseDisplay().getXDisplay(), resource.mstyle.hilite_gc,
1334             gc_value_mask, &gcv);
1335
1336   gcv.foreground = resource.tstyle.l_text.getPixel();
1337   if (resource.tstyle.font)
1338     gcv.font = resource.tstyle.font->fid;
1339   XChangeGC(getBaseDisplay().getXDisplay(), resource.tstyle.l_text_gc,
1340             gc_value_mask, &gcv);
1341
1342   gcv.foreground = resource.tstyle.w_text.getPixel();
1343   XChangeGC(getBaseDisplay().getXDisplay(), resource.tstyle.w_text_gc,
1344             gc_value_mask, &gcv);
1345
1346   gcv.foreground = resource.tstyle.c_text.getPixel();
1347   XChangeGC(getBaseDisplay().getXDisplay(), resource.tstyle.c_text_gc,
1348             gc_value_mask, &gcv);
1349
1350   gcv.foreground = resource.tstyle.b_pic.getPixel();
1351   XChangeGC(getBaseDisplay().getXDisplay(), resource.tstyle.b_pic_gc,
1352             gc_value_mask, &gcv);
1353
1354   const char *s = i18n->getMessage(ScreenSet, ScreenPositionLength,
1355                                    "0: 0000 x 0: 0000");
1356   int l = strlen(s);
1357
1358   if (i18n->multibyte()) {
1359     XRectangle ink, logical;
1360     XmbTextExtents(resource.wstyle.fontset, s, l, &ink, &logical);
1361     geom_w = logical.width;
1362
1363     geom_h = resource.wstyle.fontset_extents->max_ink_extent.height;
1364   } else {
1365     geom_w = XTextWidth(resource.wstyle.font, s, l);
1366
1367     geom_h = resource.wstyle.font->ascent +
1368              resource.wstyle.font->descent; 
1369   }
1370
1371   geom_w += (resource.bevel_width * 2);
1372   geom_h += (resource.bevel_width * 2);
1373
1374   Pixmap tmp = geom_pixmap;
1375   if (resource.wstyle.l_focus.getTexture() & BImage_ParentRelative) {
1376     if (resource.wstyle.t_focus.getTexture() ==
1377                                       (BImage_Flat | BImage_Solid)) {
1378       geom_pixmap = None;
1379       XSetWindowBackground(getBaseDisplay().getXDisplay(), geom_window,
1380                          resource.wstyle.t_focus.getColor()->getPixel());
1381     } else {
1382       geom_pixmap = image_control->renderImage(geom_w, geom_h,
1383                                                &resource.wstyle.t_focus);
1384       XSetWindowBackgroundPixmap(getBaseDisplay().getXDisplay(),
1385                                  geom_window, geom_pixmap);
1386     }
1387   } else {
1388     if (resource.wstyle.l_focus.getTexture() ==
1389                                       (BImage_Flat | BImage_Solid)) {
1390       geom_pixmap = None;
1391       XSetWindowBackground(getBaseDisplay().getXDisplay(), geom_window,
1392                          resource.wstyle.l_focus.getColor()->getPixel());
1393     } else {
1394       geom_pixmap = image_control->renderImage(geom_w, geom_h,
1395                                                &resource.wstyle.l_focus);
1396       XSetWindowBackgroundPixmap(getBaseDisplay().getXDisplay(),
1397                                  geom_window, geom_pixmap);
1398     }
1399   }
1400   if (tmp) image_control->removeImage(tmp);
1401
1402   XSetWindowBorderWidth(getBaseDisplay().getXDisplay(), geom_window,
1403                         resource.border_width);
1404   XSetWindowBorder(getBaseDisplay().getXDisplay(), geom_window,
1405                    resource.border_color.getPixel());
1406
1407   workspacemenu->reconfigure();
1408   iconmenu->reconfigure();
1409
1410   {
1411     int remember_sub = rootmenu->getCurrentSubmenu();
1412     InitMenu();
1413     raiseWindows(0, 0);
1414     rootmenu->reconfigure();
1415     rootmenu->drawSubmenu(remember_sub);
1416   }
1417
1418   configmenu->reconfigure();
1419
1420   toolbar->reconfigure();
1421
1422 #ifdef    SLIT
1423   slit->reconfigure();
1424 #endif // SLIT
1425
1426   LinkedListIterator<Workspace> wit(workspacesList);
1427   for (Workspace *w = wit.current(); w; wit++, w = wit.current())
1428     w->reconfigure();
1429
1430   LinkedListIterator<OpenboxWindow> iit(iconList);
1431   for (OpenboxWindow *bw = iit.current(); bw; iit++, bw = iit.current())
1432     if (bw->validateClient())
1433       bw->reconfigure();
1434
1435   image_control->timeout();
1436 }
1437
1438
1439 void BScreen::rereadMenu(void) {
1440   InitMenu();
1441   raiseWindows(0, 0);
1442
1443   rootmenu->reconfigure();
1444 }
1445
1446
1447 void BScreen::removeWorkspaceNames(void) {
1448   while (workspaceNames->count())
1449     delete [] workspaceNames->remove(0);
1450 }
1451
1452
1453 void BScreen::LoadStyle(void) {
1454   Resource &conf = resource.styleconfig;
1455   
1456   const char *sfile = openbox.getStyleFilename();
1457   bool loaded = false;
1458   if (sfile != NULL) {
1459     conf.setFile(sfile);
1460     loaded = conf.load();
1461   }
1462   if (!loaded) {
1463     conf.setFile(DEFAULTSTYLE);
1464     if (!conf.load()) {
1465       fprintf(stderr, i18n->getMessage(ScreenSet, ScreenDefaultStyleLoadFail,
1466                                        "BScreen::LoadStyle(): couldn't load "
1467                                        "default style.\n"));
1468       exit(2);
1469     }
1470   }
1471
1472   std::string s;
1473   long l;
1474   
1475   // load fonts/fontsets
1476
1477   if (i18n->multibyte()) {
1478     readDatabaseFontSet("window.font", "Window.Font",
1479                         &resource.wstyle.fontset);
1480     readDatabaseFontSet("toolbar.font", "Toolbar.Font",
1481                         &resource.tstyle.fontset);
1482     readDatabaseFontSet("menu.title.font", "Menu.Title.Font",
1483                         &resource.mstyle.t_fontset);
1484     readDatabaseFontSet("menu.frame.font", "Menu.Frame.Font",
1485                         &resource.mstyle.f_fontset);
1486
1487     resource.mstyle.t_fontset_extents =
1488       XExtentsOfFontSet(resource.mstyle.t_fontset);
1489     resource.mstyle.f_fontset_extents =
1490       XExtentsOfFontSet(resource.mstyle.f_fontset);
1491     resource.tstyle.fontset_extents =
1492       XExtentsOfFontSet(resource.tstyle.fontset);
1493     resource.wstyle.fontset_extents =
1494       XExtentsOfFontSet(resource.wstyle.fontset);
1495   } else {
1496     readDatabaseFont("window.font", "Window.Font",
1497                      &resource.wstyle.font);
1498     readDatabaseFont("menu.title.font", "Menu.Title.Font",
1499                      &resource.mstyle.t_font);
1500     readDatabaseFont("menu.frame.font", "Menu.Frame.Font",
1501                      &resource.mstyle.f_font);
1502     readDatabaseFont("toolbar.font", "Toolbar.Font",
1503                      &resource.tstyle.font);
1504   }
1505
1506   // load window config
1507   readDatabaseTexture("window.title.focus", "Window.Title.Focus",
1508                       &resource.wstyle.t_focus,
1509                       WhitePixel(getBaseDisplay().getXDisplay(),
1510                                  getScreenNumber()));
1511   readDatabaseTexture("window.title.unfocus", "Window.Title.Unfocus",
1512                       &resource.wstyle.t_unfocus,
1513                       BlackPixel(getBaseDisplay().getXDisplay(),
1514                                  getScreenNumber()));
1515   readDatabaseTexture("window.label.focus", "Window.Label.Focus",
1516                       &resource.wstyle.l_focus,
1517                       WhitePixel(getBaseDisplay().getXDisplay(),
1518                                  getScreenNumber()));
1519   readDatabaseTexture("window.label.unfocus", "Window.Label.Unfocus",
1520                       &resource.wstyle.l_unfocus,
1521                       BlackPixel(getBaseDisplay().getXDisplay(),
1522                                  getScreenNumber()));
1523   readDatabaseTexture("window.handle.focus", "Window.Handle.Focus",
1524                       &resource.wstyle.h_focus,
1525                       WhitePixel(getBaseDisplay().getXDisplay(),
1526                                  getScreenNumber()));
1527   readDatabaseTexture("window.handle.unfocus", "Window.Handle.Unfocus",
1528                       &resource.wstyle.h_unfocus,
1529                       BlackPixel(getBaseDisplay().getXDisplay(),
1530                                  getScreenNumber()));
1531   readDatabaseTexture("window.grip.focus", "Window.Grip.Focus",
1532                       &resource.wstyle.g_focus,
1533                       WhitePixel(getBaseDisplay().getXDisplay(),
1534                                  getScreenNumber()));
1535   readDatabaseTexture("window.grip.unfocus", "Window.Grip.Unfocus",
1536                       &resource.wstyle.g_unfocus,
1537                       BlackPixel(getBaseDisplay().getXDisplay(),
1538                                  getScreenNumber()));
1539   readDatabaseTexture("window.button.focus", "Window.Button.Focus",
1540                       &resource.wstyle.b_focus,
1541                       WhitePixel(getBaseDisplay().getXDisplay(),
1542                                  getScreenNumber()));
1543   readDatabaseTexture("window.button.unfocus", "Window.Button.Unfocus",
1544                       &resource.wstyle.b_unfocus,
1545                       BlackPixel(getBaseDisplay().getXDisplay(),
1546                                  getScreenNumber()));
1547   readDatabaseTexture("window.button.pressed", "Window.Button.Pressed",
1548                       &resource.wstyle.b_pressed,
1549                       BlackPixel(getBaseDisplay().getXDisplay(),
1550                                  getScreenNumber()));
1551   readDatabaseColor("window.frame.focusColor",
1552                     "Window.Frame.FocusColor",
1553                     &resource.wstyle.f_focus,
1554                     WhitePixel(getBaseDisplay().getXDisplay(),
1555                                getScreenNumber()));
1556   readDatabaseColor("window.frame.unfocusColor",
1557                     "Window.Frame.UnfocusColor",
1558                     &resource.wstyle.f_unfocus,
1559                     BlackPixel(getBaseDisplay().getXDisplay(),
1560                                getScreenNumber()));
1561   readDatabaseColor("window.label.focus.textColor",
1562                     "Window.Label.Focus.TextColor",
1563                     &resource.wstyle.l_text_focus,
1564                     BlackPixel(getBaseDisplay().getXDisplay(),
1565                                getScreenNumber()));
1566   readDatabaseColor("window.label.unfocus.textColor",
1567                     "Window.Label.Unfocus.TextColor",
1568                     &resource.wstyle.l_text_unfocus,
1569                     WhitePixel(getBaseDisplay().getXDisplay(),
1570                                getScreenNumber()));
1571   readDatabaseColor("window.button.focus.picColor",
1572                     "Window.Button.Focus.PicColor",
1573                     &resource.wstyle.b_pic_focus,
1574                     BlackPixel(getBaseDisplay().getXDisplay(),
1575                                getScreenNumber()));
1576   readDatabaseColor("window.button.unfocus.picColor",
1577                     "Window.Button.Unfocus.PicColor",
1578                     &resource.wstyle.b_pic_unfocus,
1579                     WhitePixel(getBaseDisplay().getXDisplay(),
1580                                getScreenNumber()));
1581
1582   if (conf.getValue("window.justify", "Window.Justify", s)) {
1583     if (0 == strncasecmp(s.c_str(), "right", s.length()))
1584       resource.wstyle.justify = BScreen::RightJustify;
1585     else if (0 == strncasecmp(s.c_str(), "center", s.length()))
1586       resource.wstyle.justify = BScreen::CenterJustify;
1587     else
1588       resource.wstyle.justify = BScreen::LeftJustify;
1589   } else
1590     resource.wstyle.justify = BScreen::LeftJustify;
1591
1592   // load toolbar config
1593   readDatabaseTexture("toolbar", "Toolbar",
1594                       &resource.tstyle.toolbar,
1595                       BlackPixel(getBaseDisplay().getXDisplay(),
1596                                  getScreenNumber()));
1597   readDatabaseTexture("toolbar.label", "Toolbar.Label",
1598                       &resource.tstyle.label,
1599                       BlackPixel(getBaseDisplay().getXDisplay(),
1600                                  getScreenNumber()));
1601   readDatabaseTexture("toolbar.windowLabel", "Toolbar.WindowLabel",
1602                       &resource.tstyle.window,
1603                       BlackPixel(getBaseDisplay().getXDisplay(),
1604                                  getScreenNumber()));
1605   readDatabaseTexture("toolbar.button", "Toolbar.Button",
1606                       &resource.tstyle.button,
1607                       WhitePixel(getBaseDisplay().getXDisplay(),
1608                                  getScreenNumber()));
1609   readDatabaseTexture("toolbar.button.pressed", "Toolbar.Button.Pressed",
1610                       &resource.tstyle.pressed,
1611                       BlackPixel(getBaseDisplay().getXDisplay(),
1612                                  getScreenNumber()));
1613   readDatabaseTexture("toolbar.clock", "Toolbar.Clock",
1614                       &resource.tstyle.clock,
1615                       BlackPixel(getBaseDisplay().getXDisplay(),
1616                                  getScreenNumber()));
1617   readDatabaseColor("toolbar.label.textColor", "Toolbar.Label.TextColor",
1618                     &resource.tstyle.l_text,
1619                     WhitePixel(getBaseDisplay().getXDisplay(),
1620                                getScreenNumber()));
1621   readDatabaseColor("toolbar.windowLabel.textColor",
1622                     "Toolbar.WindowLabel.TextColor",
1623                     &resource.tstyle.w_text,
1624                     WhitePixel(getBaseDisplay().getXDisplay(),
1625                                getScreenNumber()));
1626   readDatabaseColor("toolbar.clock.textColor", "Toolbar.Clock.TextColor",
1627                     &resource.tstyle.c_text,
1628                     WhitePixel(getBaseDisplay().getXDisplay(),
1629                                getScreenNumber()));
1630   readDatabaseColor("toolbar.button.picColor", "Toolbar.Button.PicColor",
1631                     &resource.tstyle.b_pic,
1632                     BlackPixel(getBaseDisplay().getXDisplay(),
1633                                getScreenNumber()));
1634
1635   if (conf.getValue("toolbar.justify", "Toolbar.Justify", s)) {
1636     if (0 == strncasecmp(s.c_str(), "right", s.length()))
1637       resource.tstyle.justify = BScreen::RightJustify;
1638     else if (0 == strncasecmp(s.c_str(), "center", s.length()))
1639       resource.tstyle.justify = BScreen::CenterJustify;
1640     else
1641       resource.tstyle.justify = BScreen::LeftJustify;
1642   } else
1643     resource.tstyle.justify = BScreen::LeftJustify;
1644
1645   // load menu config
1646   readDatabaseTexture("menu.title", "Menu.Title",
1647                       &resource.mstyle.title,
1648                       WhitePixel(getBaseDisplay().getXDisplay(),
1649                                  getScreenNumber()));
1650   readDatabaseTexture("menu.frame", "Menu.Frame",
1651                       &resource.mstyle.frame,
1652                       BlackPixel(getBaseDisplay().getXDisplay(),
1653                                  getScreenNumber()));
1654   readDatabaseTexture("menu.hilite", "Menu.Hilite",
1655                       &resource.mstyle.hilite,
1656                       WhitePixel(getBaseDisplay().getXDisplay(),
1657                                  getScreenNumber()));
1658   readDatabaseColor("menu.title.textColor", "Menu.Title.TextColor",
1659                     &resource.mstyle.t_text,
1660                     BlackPixel(getBaseDisplay().getXDisplay(),
1661                                getScreenNumber()));
1662   readDatabaseColor("menu.frame.textColor", "Menu.Frame.TextColor",
1663                     &resource.mstyle.f_text,
1664                     WhitePixel(getBaseDisplay().getXDisplay(),
1665                                getScreenNumber()));
1666   readDatabaseColor("menu.frame.disableColor", "Menu.Frame.DisableColor",
1667                     &resource.mstyle.d_text,
1668                     BlackPixel(getBaseDisplay().getXDisplay(),
1669                                getScreenNumber()));
1670   readDatabaseColor("menu.hilite.textColor", "Menu.Hilite.TextColor",
1671                     &resource.mstyle.h_text,
1672                     BlackPixel(getBaseDisplay().getXDisplay(),
1673                                getScreenNumber()));
1674
1675   if (conf.getValue("menu.title.justify", "Menu.Title.Justify", s)) {
1676     if (0 == strncasecmp(s.c_str(), "right", s.length()))
1677       resource.mstyle.t_justify = BScreen::RightJustify;
1678     else if (0 == strncasecmp(s.c_str(), "center", s.length()))
1679       resource.mstyle.t_justify = BScreen::CenterJustify;
1680     else
1681       resource.mstyle.t_justify = BScreen::LeftJustify;
1682   } else
1683     resource.mstyle.t_justify = BScreen::LeftJustify;
1684
1685   if (conf.getValue("menu.frame.justify", "Menu.Frame.Justify", s)) {
1686     if (0 == strncasecmp(s.c_str(), "right", s.length()))
1687       resource.mstyle.f_justify = BScreen::RightJustify;
1688     else if (0 == strncasecmp(s.c_str(), "center", s.length()))
1689       resource.mstyle.f_justify = BScreen::CenterJustify;
1690     else
1691       resource.mstyle.f_justify = BScreen::LeftJustify;
1692   } else
1693     resource.mstyle.f_justify = BScreen::LeftJustify;
1694
1695   if (conf.getValue("menu.bullet", "Menu.Bullet", s)) {
1696     if (0 == strncasecmp(s.c_str(), "empty", s.length()))
1697       resource.mstyle.bullet = Basemenu::Empty;
1698     else if (0 == strncasecmp(s.c_str(), "square", s.length()))
1699       resource.mstyle.bullet = Basemenu::Square;
1700     else if (0 == strncasecmp(s.c_str(), "diamond", s.length()))
1701       resource.mstyle.bullet = Basemenu::Diamond;
1702     else
1703       resource.mstyle.bullet = Basemenu::Triangle;
1704   } else
1705     resource.mstyle.bullet = Basemenu::Triangle;
1706
1707   if (conf.getValue("menu.bullet.position", "Menu.Bullet.Position", s)) {
1708     if (0 == strncasecmp(s.c_str(), "right", s.length()))
1709       resource.mstyle.bullet_pos = Basemenu::Right;
1710     else
1711       resource.mstyle.bullet_pos = Basemenu::Left;
1712   } else
1713     resource.mstyle.bullet_pos = Basemenu::Left;
1714
1715   readDatabaseColor("borderColor", "BorderColor", &resource.border_color,
1716                     BlackPixel(getBaseDisplay().getXDisplay(),
1717                                getScreenNumber()));
1718
1719   // load bevel, border and handle widths
1720   if (conf.getValue("handleWidth", "HandleWidth", l)) {
1721     if (l <= size().w() / 2 && l != 0)
1722       resource.handle_width = l;
1723     else
1724       resource.handle_width = 6;
1725   } else
1726     resource.handle_width = 6;
1727
1728   if (conf.getValue("borderWidth", "BorderWidth", l))
1729     resource.border_width = l;
1730   else
1731     resource.border_width = 1;
1732
1733   if (conf.getValue("bevelWidth", "BevelWidth", l)) {
1734     if (l <= size().w() / 2 && l != 0)
1735       resource.bevel_width = l;
1736     else
1737       resource.bevel_width = 3;
1738   } else
1739     resource.bevel_width = 3;
1740
1741   if (conf.getValue("frameWidth", "FrameWidth", l)) {
1742     if (l <= size().w() / 2)
1743       resource.frame_width = l;
1744     else
1745       resource.frame_width = resource.bevel_width;
1746   } else
1747     resource.frame_width = resource.bevel_width;
1748
1749   const char *cmd = resource.root_command;
1750   if (cmd != NULL || conf.getValue("rootCommand", "RootCommand", s)) {
1751     if (cmd == NULL)
1752       cmd = s.c_str(); // not specified by the screen, so use the one from the
1753                        // style file
1754 #ifndef    __EMX__
1755     char displaystring[MAXPATHLEN];
1756     sprintf(displaystring, "DISPLAY=%s",
1757             DisplayString(getBaseDisplay().getXDisplay()));
1758     sprintf(displaystring + strlen(displaystring) - 1, "%d",
1759             getScreenNumber());
1760
1761     bexec(cmd, displaystring);
1762 #else //   __EMX__
1763     spawnlp(P_NOWAIT, "cmd.exe", "cmd.exe", "/c", cmd, NULL);
1764 #endif // !__EMX__
1765   }
1766 }
1767
1768
1769 void BScreen::addIcon(OpenboxWindow *w) {
1770   if (! w) return;
1771
1772   w->setWorkspace(-1);
1773   w->setWindowNumber(iconList->count());
1774
1775   iconList->insert(w);
1776
1777   iconmenu->insert((const char **) w->getIconTitle());
1778   iconmenu->update();
1779 }
1780
1781
1782 void BScreen::removeIcon(OpenboxWindow *w) {
1783   if (! w) return;
1784
1785   iconList->remove(w->getWindowNumber());
1786
1787   iconmenu->remove(w->getWindowNumber());
1788   iconmenu->update();
1789
1790   LinkedListIterator<OpenboxWindow> it(iconList);
1791   OpenboxWindow *bw = it.current();
1792   for (int i = 0; bw; it++, bw = it.current())
1793     bw->setWindowNumber(i++);
1794 }
1795
1796
1797 OpenboxWindow *BScreen::getIcon(int index) {
1798   if (index >= 0 && index < iconList->count())
1799     return iconList->find(index);
1800
1801   return NULL;
1802 }
1803
1804
1805 int BScreen::addWorkspace(void) {
1806   Workspace *wkspc = new Workspace(*this, workspacesList->count());
1807   workspacesList->insert(wkspc);
1808   saveWorkspaceNames();
1809
1810   workspacemenu->insert(wkspc->getName(), wkspc->getMenu(),
1811                         wkspc->getWorkspaceID() + 2);
1812   workspacemenu->update();
1813
1814   toolbar->reconfigure();
1815
1816   updateNetizenWorkspaceCount();
1817
1818   return workspacesList->count();
1819 }
1820
1821
1822 int BScreen::removeLastWorkspace(void) {
1823   if (workspacesList->count() == 1)
1824     return 0;
1825
1826   Workspace *wkspc = workspacesList->last();
1827
1828   if (current_workspace->getWorkspaceID() == wkspc->getWorkspaceID())
1829     changeWorkspaceID(current_workspace->getWorkspaceID() - 1);
1830
1831   wkspc->removeAll();
1832
1833   workspacemenu->remove(wkspc->getWorkspaceID() + 2);
1834   workspacemenu->update();
1835
1836   workspacesList->remove(wkspc);
1837   delete wkspc;
1838
1839   toolbar->reconfigure();
1840
1841   updateNetizenWorkspaceCount();
1842
1843   return workspacesList->count();
1844 }
1845
1846
1847 void BScreen::changeWorkspaceID(int id) {
1848   if (! current_workspace) return;
1849
1850   if (id != current_workspace->getWorkspaceID()) {
1851     current_workspace->hideAll();
1852
1853     workspacemenu->setItemSelected(current_workspace->getWorkspaceID() + 2,
1854                                    False);
1855
1856     if (openbox.getFocusedWindow() &&
1857         openbox.getFocusedWindow()->getScreen() == this &&
1858         (! openbox.getFocusedWindow()->isStuck())) {
1859       current_workspace->setLastFocusedWindow(openbox.getFocusedWindow());
1860       openbox.setFocusedWindow(NULL);
1861     }
1862
1863     current_workspace = getWorkspace(id);
1864
1865     workspacemenu->setItemSelected(current_workspace->getWorkspaceID() + 2,
1866                                    True);
1867     toolbar->redrawWorkspaceLabel(True);
1868
1869     current_workspace->showAll();
1870
1871     if (resource.focus_last && current_workspace->getLastFocusedWindow()) {
1872       XSync(openbox.getXDisplay(), False);
1873       current_workspace->getLastFocusedWindow()->setInputFocus();
1874     }
1875   }
1876
1877   updateNetizenCurrentWorkspace();
1878 }
1879
1880
1881 void BScreen::addNetizen(Netizen *n) {
1882   netizenList->insert(n);
1883
1884   n->sendWorkspaceCount();
1885   n->sendCurrentWorkspace();
1886
1887   LinkedListIterator<Workspace> it(workspacesList);
1888   for (Workspace *w = it.current(); w; it++, w = it.current()) {
1889     for (int i = 0; i < w->getCount(); i++)
1890       n->sendWindowAdd(w->getWindow(i)->getClientWindow(),
1891                        w->getWorkspaceID());
1892   }
1893
1894   Window f = ((openbox.getFocusedWindow()) ?
1895               openbox.getFocusedWindow()->getClientWindow() : None);
1896   n->sendWindowFocus(f);
1897 }
1898
1899
1900 void BScreen::removeNetizen(Window w) {
1901   LinkedListIterator<Netizen> it(netizenList);
1902   int i = 0;
1903
1904   for (Netizen *n = it.current(); n; it++, i++, n = it.current())
1905     if (n->getWindowID() == w) {
1906       Netizen *tmp = netizenList->remove(i);
1907       delete tmp;
1908
1909       break;
1910     }
1911 }
1912
1913
1914 void BScreen::updateNetizenCurrentWorkspace(void) {
1915   LinkedListIterator<Netizen> it(netizenList);
1916   for (Netizen *n = it.current(); n; it++, n = it.current())
1917     n->sendCurrentWorkspace();
1918 }
1919
1920
1921 void BScreen::updateNetizenWorkspaceCount(void) {
1922   LinkedListIterator<Netizen> it(netizenList);
1923   for (Netizen *n = it.current(); n; it++, n = it.current())
1924     n->sendWorkspaceCount();
1925 }
1926
1927
1928 void BScreen::updateNetizenWindowFocus(void) {
1929   Window f = ((openbox.getFocusedWindow()) ?
1930               openbox.getFocusedWindow()->getClientWindow() : None);
1931   LinkedListIterator<Netizen> it(netizenList);
1932   for (Netizen *n = it.current(); n; it++, n = it.current())
1933     n->sendWindowFocus(f);
1934 }
1935
1936
1937 void BScreen::updateNetizenWindowAdd(Window w, unsigned long p) {
1938   LinkedListIterator<Netizen> it(netizenList);
1939   for (Netizen *n = it.current(); n; it++, n = it.current())
1940     n->sendWindowAdd(w, p);
1941 }
1942
1943
1944 void BScreen::updateNetizenWindowDel(Window w) {
1945   LinkedListIterator<Netizen> it(netizenList);
1946   for (Netizen *n = it.current(); n; it++, n = it.current())
1947     n->sendWindowDel(w);
1948 }
1949
1950
1951 void BScreen::updateNetizenWindowRaise(Window w) {
1952   LinkedListIterator<Netizen> it(netizenList);
1953   for (Netizen *n = it.current(); n; it++, n = it.current())
1954     n->sendWindowRaise(w);
1955 }
1956
1957
1958 void BScreen::updateNetizenWindowLower(Window w) {
1959   LinkedListIterator<Netizen> it(netizenList);
1960   for (Netizen *n = it.current(); n; it++, n = it.current())
1961     n->sendWindowLower(w);
1962 }
1963
1964
1965 void BScreen::updateNetizenConfigNotify(XEvent *e) {
1966   LinkedListIterator<Netizen> it(netizenList);
1967   for (Netizen *n = it.current(); n; it++, n = it.current())
1968     n->sendConfigNotify(e);
1969 }
1970
1971
1972 void BScreen::raiseWindows(Window *workspace_stack, int num) {
1973   Window *session_stack = new
1974     Window[(num + workspacesList->count() + rootmenuList->count() + 13)];
1975   int i = 0, k = num;
1976
1977   XRaiseWindow(getBaseDisplay().getXDisplay(), iconmenu->getWindowID());
1978   *(session_stack + i++) = iconmenu->getWindowID();
1979
1980   LinkedListIterator<Workspace> wit(workspacesList);
1981   for (Workspace *tmp = wit.current(); tmp; wit++, tmp = wit.current())
1982     *(session_stack + i++) = tmp->getMenu()->getWindowID();
1983
1984   *(session_stack + i++) = workspacemenu->getWindowID();
1985
1986   *(session_stack + i++) = configmenu->getFocusmenu()->getWindowID();
1987   *(session_stack + i++) = configmenu->getPlacementmenu()->getWindowID();
1988   *(session_stack + i++) = configmenu->getWindowID();
1989
1990 #ifdef    SLIT
1991   *(session_stack + i++) = slit->getMenu()->getDirectionmenu()->getWindowID();
1992   *(session_stack + i++) = slit->getMenu()->getPlacementmenu()->getWindowID();
1993   *(session_stack + i++) = slit->getMenu()->getWindowID();
1994 #endif // SLIT
1995
1996   *(session_stack + i++) =
1997     toolbar->getMenu()->getPlacementmenu()->getWindowID();
1998   *(session_stack + i++) = toolbar->getMenu()->getWindowID();
1999
2000   LinkedListIterator<Rootmenu> rit(rootmenuList);
2001   for (Rootmenu *tmp = rit.current(); tmp; rit++, tmp = rit.current())
2002     *(session_stack + i++) = tmp->getWindowID();
2003   *(session_stack + i++) = rootmenu->getWindowID();
2004
2005   if (toolbar->onTop())
2006     *(session_stack + i++) = toolbar->getWindowID();
2007
2008 #ifdef    SLIT
2009   if (slit->onTop())
2010     *(session_stack + i++) = slit->getWindowID();
2011 #endif // SLIT
2012
2013   while (k--)
2014     *(session_stack + i++) = *(workspace_stack + k);
2015
2016   XRestackWindows(getBaseDisplay().getXDisplay(), session_stack, i);
2017
2018   delete [] session_stack;
2019 }
2020
2021
2022 void BScreen::addWorkspaceName(const char *name) {
2023   workspaceNames->insert(bstrdup(name));
2024 }
2025
2026 char* BScreen::getNameOfWorkspace(int id) {
2027   char *name = NULL;
2028
2029   if (id >= 0 && id < workspaceNames->count()) {
2030     char *wkspc_name = workspaceNames->find(id);
2031
2032     if (wkspc_name)
2033       name = wkspc_name;
2034   }
2035   return name;
2036 }
2037
2038
2039 void BScreen::reassociateWindow(OpenboxWindow *w, int wkspc_id, Bool ignore_sticky) {
2040   if (! w) return;
2041
2042   if (wkspc_id == -1)
2043     wkspc_id = current_workspace->getWorkspaceID();
2044
2045   if (w->getWorkspaceNumber() == wkspc_id)
2046     return;
2047
2048   if (w->isIconic()) {
2049     removeIcon(w);
2050     getWorkspace(wkspc_id)->addWindow(w);
2051   } else if (ignore_sticky || ! w->isStuck()) {
2052     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
2053     getWorkspace(wkspc_id)->addWindow(w);
2054   }
2055 }
2056
2057
2058 void BScreen::nextFocus(void) {
2059   Bool have_focused = False;
2060   int focused_window_number = -1;
2061   OpenboxWindow *next;
2062
2063   if (openbox.getFocusedWindow()) {
2064     if (openbox.getFocusedWindow()->getScreen()->getScreenNumber() ==
2065         getScreenNumber()) {
2066       have_focused = True;
2067       focused_window_number = openbox.getFocusedWindow()->getWindowNumber();
2068     }
2069   }
2070
2071   if ((getCurrentWorkspace()->getCount() > 1) && have_focused) {
2072     int next_window_number = focused_window_number;
2073     do {
2074       if ((++next_window_number) >= getCurrentWorkspace()->getCount())
2075         next_window_number = 0;
2076
2077       next = getCurrentWorkspace()->getWindow(next_window_number);
2078     } while ((! next->setInputFocus()) && (next_window_number !=
2079                                            focused_window_number));
2080
2081     if (next_window_number != focused_window_number)
2082       getCurrentWorkspace()->raiseWindow(next);
2083   } else if (getCurrentWorkspace()->getCount() >= 1) {
2084     next = current_workspace->getWindow(0);
2085
2086     current_workspace->raiseWindow(next);
2087     next->setInputFocus();
2088   }
2089 }
2090
2091
2092 void BScreen::prevFocus(void) {
2093   Bool have_focused = False;
2094   int focused_window_number = -1;
2095   OpenboxWindow *prev;
2096
2097   if (openbox.getFocusedWindow()) {
2098     if (openbox.getFocusedWindow()->getScreen()->getScreenNumber() ==
2099         getScreenNumber()) {
2100       have_focused = True;
2101       focused_window_number = openbox.getFocusedWindow()->getWindowNumber();
2102     }
2103   }
2104
2105   if ((getCurrentWorkspace()->getCount() > 1) && have_focused) {
2106     int prev_window_number = focused_window_number;
2107     do {
2108       if ((--prev_window_number) < 0)
2109         prev_window_number = getCurrentWorkspace()->getCount() - 1;
2110
2111       prev = getCurrentWorkspace()->getWindow(prev_window_number);
2112     } while ((! prev->setInputFocus()) && (prev_window_number !=
2113                                            focused_window_number));
2114
2115     if (prev_window_number != focused_window_number)
2116       getCurrentWorkspace()->raiseWindow(prev);
2117   } else if (getCurrentWorkspace()->getCount() >= 1) {
2118     prev = current_workspace->getWindow(0);
2119
2120     current_workspace->raiseWindow(prev);
2121     prev->setInputFocus();
2122   }
2123 }
2124
2125
2126 void BScreen::raiseFocus(void) {
2127   Bool have_focused = False;
2128   int focused_window_number = -1;
2129
2130   if (openbox.getFocusedWindow()) {
2131     if (openbox.getFocusedWindow()->getScreen()->getScreenNumber() ==
2132         getScreenNumber()) {
2133       have_focused = True;
2134       focused_window_number = openbox.getFocusedWindow()->getWindowNumber();
2135     }
2136   }
2137
2138   if ((getCurrentWorkspace()->getCount() > 1) && have_focused)
2139     getWorkspace(openbox.getFocusedWindow()->getWorkspaceNumber())->
2140       raiseWindow(openbox.getFocusedWindow());
2141 }
2142
2143
2144 void BScreen::InitMenu(void) {
2145   if (rootmenu) {
2146     while (rootmenuList->count())
2147       rootmenuList->remove(0);
2148
2149     while (rootmenu->getCount())
2150       rootmenu->remove(0);
2151   } else {
2152     rootmenu = new Rootmenu(*this);
2153   }
2154   Bool defaultMenu = True;
2155
2156   if (openbox.getMenuFilename()) {
2157     FILE *menu_file = fopen(openbox.getMenuFilename(), "r");
2158
2159     if (!menu_file) {
2160       perror(openbox.getMenuFilename());
2161     } else {
2162       if (feof(menu_file)) {
2163         fprintf(stderr, i18n->getMessage(ScreenSet, ScreenEmptyMenuFile,
2164                                          "%s: Empty menu file"),
2165                 openbox.getMenuFilename());
2166       } else {
2167         char line[1024], label[1024];
2168         memset(line, 0, 1024);
2169         memset(label, 0, 1024);
2170
2171         while (fgets(line, 1024, menu_file) && ! feof(menu_file)) {
2172           if (line[0] != '#') {
2173             int i, key = 0, index = -1, len = strlen(line);
2174
2175             key = 0;
2176             for (i = 0; i < len; i++) {
2177               if (line[i] == '[') index = 0;
2178               else if (line[i] == ']') break;
2179               else if (line[i] != ' ')
2180                 if (index++ >= 0)
2181                   key += tolower(line[i]);
2182             }
2183
2184             if (key == 517) {
2185               index = -1;
2186               for (i = index; i < len; i++) {
2187                 if (line[i] == '(') index = 0;
2188                 else if (line[i] == ')') break;
2189                 else if (index++ >= 0) {
2190                   if (line[i] == '\\' && i < len - 1) i++;
2191                   label[index - 1] = line[i];
2192                 }
2193               }
2194
2195               if (index == -1) index = 0;
2196               label[index] = '\0';
2197
2198               rootmenu->setLabel(label);
2199               defaultMenu = parseMenuFile(menu_file, rootmenu);
2200               break;
2201             }
2202           }
2203         }
2204       }
2205       fclose(menu_file);
2206     }
2207   }
2208
2209   if (defaultMenu) {
2210     rootmenu->setInternalMenu();
2211     rootmenu->insert(i18n->getMessage(ScreenSet, Screenxterm, "xterm"),
2212                      BScreen::Execute,
2213                      i18n->getMessage(ScreenSet, Screenxterm, "xterm"));
2214     rootmenu->insert(i18n->getMessage(ScreenSet, ScreenRestart, "Restart"),
2215                      BScreen::Restart);
2216     rootmenu->insert(i18n->getMessage(ScreenSet, ScreenExit, "Exit"),
2217                      BScreen::Exit);
2218   } else {
2219     openbox.setMenuFilename(openbox.getMenuFilename());
2220   }
2221 }
2222
2223
2224 Bool BScreen::parseMenuFile(FILE *file, Rootmenu *menu) {
2225   char line[1024], label[1024], command[1024];
2226
2227   while (! feof(file)) {
2228     memset(line, 0, 1024);
2229     memset(label, 0, 1024);
2230     memset(command, 0, 1024);
2231
2232     if (fgets(line, 1024, file)) {
2233       if (line[0] != '#') {
2234         register int i, key = 0, parse = 0, index = -1,
2235           line_length = strlen(line),
2236           label_length = 0, command_length = 0;
2237
2238         // determine the keyword
2239         key = 0;
2240         for (i = 0; i < line_length; i++) {
2241           if (line[i] == '[') parse = 1;
2242           else if (line[i] == ']') break;
2243           else if (line[i] != ' ')
2244             if (parse)
2245               key += tolower(line[i]);
2246         }
2247
2248         // get the label enclosed in ()'s
2249         parse = 0;
2250
2251         for (i = 0; i < line_length; i++) {
2252           if (line[i] == '(') {
2253             index = 0;
2254             parse = 1;
2255           } else if (line[i] == ')') break;
2256           else if (index++ >= 0) {
2257             if (line[i] == '\\' && i < line_length - 1) i++;
2258             label[index - 1] = line[i];
2259           }
2260         }
2261
2262         if (parse) {
2263           label[index] = '\0';
2264           label_length = index;
2265         } else {
2266           label[0] = '\0';
2267           label_length = 0;
2268         }
2269
2270         // get the command enclosed in {}'s
2271         parse = 0;
2272         index = -1;
2273         for (i = 0; i < line_length; i++) {
2274           if (line[i] == '{') {
2275             index = 0;
2276             parse = 1;
2277           } else if (line[i] == '}') break;
2278           else if (index++ >= 0) {
2279             if (line[i] == '\\' && i < line_length - 1) i++;
2280             command[index - 1] = line[i];
2281           }
2282         }
2283
2284         if (parse) {
2285           command[index] = '\0';
2286           command_length = index;
2287         } else {
2288           command[0] = '\0';
2289           command_length = 0;
2290         }
2291
2292         switch (key) {
2293         case 311: //end
2294           return ((menu->getCount() == 0) ? True : False);
2295
2296           break;
2297
2298         case 333: // nop
2299           menu->insert(label);
2300
2301           break;
2302
2303         case 421: // exec
2304           if ((! *label) && (! *command)) {
2305             fprintf(stderr, i18n->getMessage(ScreenSet, ScreenEXECError,
2306                              "BScreen::parseMenuFile: [exec] error, "
2307                              "no menu label and/or command defined\n"));
2308             continue;
2309           }
2310
2311           menu->insert(label, BScreen::Execute, command);
2312
2313           break;
2314
2315         case 442: // exit
2316           if (! *label) {
2317             fprintf(stderr, i18n->getMessage(ScreenSet, ScreenEXITError,
2318                                      "BScreen::parseMenuFile: [exit] error, "
2319                                      "no menu label defined\n"));
2320             continue;
2321           }
2322
2323           menu->insert(label, BScreen::Exit);
2324
2325           break;
2326
2327         case 561: // style
2328           {
2329             if ((! *label) || (! *command)) {
2330               fprintf(stderr, i18n->getMessage(ScreenSet, ScreenSTYLEError,
2331                                  "BScreen::parseMenuFile: [style] error, "
2332                                  "no menu label and/or filename defined\n"));
2333               continue;
2334             }
2335
2336             char style[MAXPATHLEN];
2337
2338             // perform shell style ~ home directory expansion
2339             char *homedir = 0;
2340             int homedir_len = 0;
2341             if (*command == '~' && *(command + 1) == '/') {
2342               homedir = getenv("HOME");
2343               homedir_len = strlen(homedir);
2344             }
2345
2346             if (homedir && homedir_len != 0) {
2347               strncpy(style, homedir, homedir_len);
2348
2349               strncpy(style + homedir_len, command + 1,
2350                       command_length - 1);
2351               *(style + command_length + homedir_len - 1) = '\0';
2352             } else {
2353               strncpy(style, command, command_length);
2354               *(style + command_length) = '\0';
2355             }
2356
2357             menu->insert(label, BScreen::SetStyle, style);
2358           }
2359
2360           break;
2361
2362         case 630: // config
2363           if (! *label) {
2364             fprintf(stderr, i18n->getMessage(ScreenSet, ScreenCONFIGError,
2365                                "BScreen::parseMenufile: [config] error, "
2366                                "no label defined"));
2367             continue;
2368           }
2369
2370           menu->insert(label, configmenu);
2371
2372           break;
2373
2374         case 740: // include
2375           {
2376             if (! *label) {
2377               fprintf(stderr, i18n->getMessage(ScreenSet, ScreenINCLUDEError,
2378                                  "BScreen::parseMenuFile: [include] error, "
2379                                  "no filename defined\n"));
2380               continue;
2381             }
2382
2383             char newfile[MAXPATHLEN];
2384
2385             // perform shell style ~ home directory expansion
2386             char *homedir = 0;
2387             int homedir_len = 0;
2388             if (*label == '~' && *(label + 1) == '/') {
2389               homedir = getenv("HOME");
2390               homedir_len = strlen(homedir);
2391             }
2392
2393             if (homedir && homedir_len != 0) {
2394               strncpy(newfile, homedir, homedir_len);
2395
2396               strncpy(newfile + homedir_len, label + 1,
2397                       label_length - 1);
2398               *(newfile + label_length + homedir_len - 1) = '\0';
2399             } else {
2400               strncpy(newfile, label, label_length);
2401               *(newfile + label_length) = '\0';
2402             }
2403
2404             if (newfile) {
2405               FILE *submenufile = fopen(newfile, "r");
2406
2407               if (submenufile) {
2408                 struct stat buf;
2409                 if (fstat(fileno(submenufile), &buf) ||
2410                     (! S_ISREG(buf.st_mode))) {
2411                   fprintf(stderr,
2412                           i18n->getMessage(ScreenSet, ScreenINCLUDEErrorReg,
2413                              "BScreen::parseMenuFile: [include] error: "
2414                              "'%s' is not a regular file\n"), newfile);
2415                   break;
2416                 }
2417
2418                 if (! feof(submenufile)) {
2419                   if (! parseMenuFile(submenufile, menu))
2420                     openbox.setMenuFilename(newfile);
2421
2422                   fclose(submenufile);
2423                 }
2424               } else
2425                 perror(newfile);
2426             }
2427           }
2428
2429           break;
2430
2431         case 767: // submenu
2432           {
2433             if (! *label) {
2434               fprintf(stderr, i18n->getMessage(ScreenSet, ScreenSUBMENUError,
2435                                  "BScreen::parseMenuFile: [submenu] error, "
2436                                  "no menu label defined\n"));
2437               continue;
2438             }
2439
2440             Rootmenu *submenu = new Rootmenu(*this);
2441
2442             if (*command)
2443               submenu->setLabel(command);
2444             else
2445               submenu->setLabel(label);
2446
2447             parseMenuFile(file, submenu);
2448             submenu->update();
2449             menu->insert(label, submenu);
2450             rootmenuList->insert(submenu);
2451           }
2452
2453           break;
2454
2455         case 773: // restart
2456           {
2457             if (! *label) {
2458               fprintf(stderr, i18n->getMessage(ScreenSet, ScreenRESTARTError,
2459                                  "BScreen::parseMenuFile: [restart] error, "
2460                                  "no menu label defined\n"));
2461               continue;
2462             }
2463
2464             if (*command)
2465               menu->insert(label, BScreen::RestartOther, command);
2466             else
2467               menu->insert(label, BScreen::Restart);
2468           }
2469
2470           break;
2471
2472         case 845: // reconfig
2473           {
2474             if (! *label) {
2475               fprintf(stderr, i18n->getMessage(ScreenSet, ScreenRECONFIGError,
2476                                  "BScreen::parseMenuFile: [reconfig] error, "
2477                                  "no menu label defined\n"));
2478               continue;
2479             }
2480
2481             menu->insert(label, BScreen::Reconfigure);
2482           }
2483
2484           break;
2485
2486         case 995: // stylesdir
2487         case 1113: // stylesmenu
2488           {
2489             Bool newmenu = ((key == 1113) ? True : False);
2490
2491             if ((! *label) || ((! *command) && newmenu)) {
2492               fprintf(stderr,
2493                       i18n->getMessage(ScreenSet, ScreenSTYLESDIRError,
2494                          "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
2495                          " error, no directory defined\n"));
2496               continue;
2497             }
2498
2499             char stylesdir[MAXPATHLEN];
2500
2501             char *directory = ((newmenu) ? command : label);
2502             int directory_length = ((newmenu) ? command_length : label_length);
2503
2504             // perform shell style ~ home directory expansion
2505             char *homedir = 0;
2506             int homedir_len = 0;
2507
2508             if (*directory == '~' && *(directory + 1) == '/') {
2509               homedir = getenv("HOME");
2510               homedir_len = strlen(homedir);
2511             }
2512
2513             if (homedir && homedir_len != 0) {
2514               strncpy(stylesdir, homedir, homedir_len);
2515
2516               strncpy(stylesdir + homedir_len, directory + 1,
2517                       directory_length - 1);
2518               *(stylesdir + directory_length + homedir_len - 1) = '\0';
2519             } else {
2520               strncpy(stylesdir, directory, directory_length);
2521               *(stylesdir + directory_length) = '\0';
2522             }
2523
2524             struct stat statbuf;
2525
2526             if (! stat(stylesdir, &statbuf)) {
2527               if (S_ISDIR(statbuf.st_mode)) {
2528                 Rootmenu *stylesmenu;
2529
2530                 if (newmenu)
2531                   stylesmenu = new Rootmenu(*this);
2532                 else
2533                   stylesmenu = menu;
2534
2535                 DIR *d = opendir(stylesdir);
2536                 int entries = 0;
2537                 struct dirent *p;
2538
2539                 // get the total number of directory entries
2540                 while ((p = readdir(d))) entries++;
2541                 rewinddir(d);
2542
2543                 char **ls = new char* [entries];
2544                 int index = 0;
2545                 while ((p = readdir(d)))
2546                   ls[index++] = bstrdup(p->d_name);
2547
2548                 closedir(d);
2549
2550                 std::sort(ls, ls + entries, dcmp());
2551
2552                 int n, slen = strlen(stylesdir);
2553                 for (n = 0; n < entries; n++) {
2554                   if (ls[n][strlen(ls[n])-1] != '~') {
2555                     int nlen = strlen(ls[n]);
2556                     char style[MAXPATHLEN + 1];
2557
2558                     strncpy(style, stylesdir, slen);
2559                     *(style + slen) = '/';
2560                     strncpy(style + slen + 1, ls[n], nlen + 1);
2561
2562                     if ((! stat(style, &statbuf)) && S_ISREG(statbuf.st_mode))
2563                       stylesmenu->insert(ls[n], BScreen::SetStyle, style);
2564                   }
2565
2566                   delete [] ls[n];
2567                 }
2568
2569                 delete [] ls;
2570
2571                 stylesmenu->update();
2572
2573                 if (newmenu) {
2574                   stylesmenu->setLabel(label);
2575                   menu->insert(label, stylesmenu);
2576                   rootmenuList->insert(stylesmenu);
2577                 }
2578
2579                 openbox.setMenuFilename(stylesdir);
2580               } else {
2581                 fprintf(stderr, i18n->getMessage(ScreenSet,
2582                                                  ScreenSTYLESDIRErrorNotDir,
2583                                    "BScreen::parseMenuFile:"
2584                                    " [stylesdir/stylesmenu] error, %s is not a"
2585                                    " directory\n"), stylesdir);
2586               }
2587             } else {
2588               fprintf(stderr,
2589                       i18n->getMessage(ScreenSet, ScreenSTYLESDIRErrorNoExist,
2590                          "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
2591                          " error, %s does not exist\n"), stylesdir);
2592             }
2593
2594             break;
2595           }
2596
2597         case 1090: // workspaces
2598           {
2599             if (! *label) {
2600               fprintf(stderr,
2601                       i18n->getMessage(ScreenSet, ScreenWORKSPACESError,
2602                                "BScreen:parseMenuFile: [workspaces] error, "
2603                                "no menu label defined\n"));
2604               continue;
2605             }
2606
2607             menu->insert(label, workspacemenu);
2608
2609             break;
2610           }
2611         }
2612       }
2613     }
2614   }
2615
2616   return ((menu->getCount() == 0) ? True : False);
2617 }
2618
2619
2620 void BScreen::shutdown(void) {
2621   openbox.grab();
2622
2623   XSelectInput(getBaseDisplay().getXDisplay(), getRootWindow(), NoEventMask);
2624   XSync(getBaseDisplay().getXDisplay(), False);
2625
2626   LinkedListIterator<Workspace> it(workspacesList);
2627   for (Workspace *w = it.current(); w; it++, w = it.current())
2628     w->shutdown();
2629
2630   while (iconList->count()) {
2631     iconList->first()->restore();
2632     delete iconList->first();
2633   }
2634
2635 #ifdef    SLIT
2636   slit->shutdown();
2637 #endif // SLIT
2638
2639   openbox.ungrab();
2640 }
2641
2642
2643 void BScreen::showPosition(int x, int y) {
2644   if (! geom_visible) {
2645     XMoveResizeWindow(getBaseDisplay().getXDisplay(), geom_window,
2646                       (size().w() - geom_w) / 2,
2647                       (size().h() - geom_h) / 2, geom_w, geom_h);
2648     XMapWindow(getBaseDisplay().getXDisplay(), geom_window);
2649     XRaiseWindow(getBaseDisplay().getXDisplay(), geom_window);
2650
2651     geom_visible = True;
2652   }
2653
2654   char label[1024];
2655
2656   sprintf(label, i18n->getMessage(ScreenSet, ScreenPositionFormat,
2657                                   "X: %4d x Y: %4d"), x, y);
2658
2659   XClearWindow(getBaseDisplay().getXDisplay(), geom_window);
2660
2661   if (i18n->multibyte()) {
2662     XmbDrawString(getBaseDisplay().getXDisplay(), geom_window,
2663                   resource.wstyle.fontset, resource.wstyle.l_text_focus_gc,
2664                   resource.bevel_width, resource.bevel_width -
2665                   resource.wstyle.fontset_extents->max_ink_extent.y,
2666                   label, strlen(label));
2667   } else {
2668     XDrawString(getBaseDisplay().getXDisplay(), geom_window,
2669                 resource.wstyle.l_text_focus_gc,
2670                 resource.bevel_width,
2671                 resource.wstyle.font->ascent +
2672                 resource.bevel_width, label, strlen(label));
2673   }
2674 }
2675
2676
2677 void BScreen::showGeometry(unsigned int gx, unsigned int gy) {
2678   if (! geom_visible) {
2679     XMoveResizeWindow(getBaseDisplay().getXDisplay(), geom_window,
2680                       (size().w() - geom_w) / 2,
2681                       (size().h() - geom_h) / 2, geom_w, geom_h);
2682     XMapWindow(getBaseDisplay().getXDisplay(), geom_window);
2683     XRaiseWindow(getBaseDisplay().getXDisplay(), geom_window);
2684
2685     geom_visible = True;
2686   }
2687
2688   char label[1024];
2689
2690   sprintf(label, i18n->getMessage(ScreenSet, ScreenGeometryFormat,
2691                                   "W: %4d x H: %4d"), gx, gy);
2692
2693   XClearWindow(getBaseDisplay().getXDisplay(), geom_window);
2694
2695   if (i18n->multibyte()) {
2696     XmbDrawString(getBaseDisplay().getXDisplay(), geom_window,
2697                   resource.wstyle.fontset, resource.wstyle.l_text_focus_gc,
2698                   resource.bevel_width, resource.bevel_width -
2699                   resource.wstyle.fontset_extents->max_ink_extent.y,
2700                   label, strlen(label));
2701   } else {
2702     XDrawString(getBaseDisplay().getXDisplay(), geom_window,
2703                 resource.wstyle.l_text_focus_gc,
2704                 resource.bevel_width,
2705                 resource.wstyle.font->ascent +
2706                 resource.bevel_width, label, strlen(label));
2707   }
2708 }
2709
2710 void BScreen::hideGeometry(void) {
2711   if (geom_visible) {
2712     XUnmapWindow(getBaseDisplay().getXDisplay(), geom_window);
2713     geom_visible = False;
2714   }
2715 }