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