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