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