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