]> icculus.org git repositories - mikachu/openbox.git/blob - src/Screen.cc
add ifdefs for config.h
[mikachu/openbox.git] / src / Screen.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Screen.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
5 //
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
23
24 #ifdef    HAVE_CONFIG_H
25 #include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #include <X11/Xatom.h>
30 #include <X11/keysym.h>
31
32 #ifdef HAVE_STDLIB_H
33 #  include <stdlib.h>
34 #endif // HAVE_STDLIB_H
35
36 #ifdef HAVE_STRING_H
37 #  include <string.h>
38 #endif // HAVE_STRING_H
39
40 #ifdef    HAVE_CTYPE_H
41 #  include <ctype.h>
42 #endif // HAVE_CTYPE_H
43
44 #ifdef    HAVE_UNISTD_H
45 #  include <sys/types.h>
46 #  include <unistd.h>
47 #endif // HAVE_UNISTD_H
48
49 #ifdef    HAVE_DIRENT_H
50 #  include <dirent.h>
51 #endif // HAVE_DIRENT_H
52
53 #ifdef    HAVE_LOCALE_H
54 #  include <locale.h>
55 #endif // HAVE_LOCALE_H
56
57 #ifdef    HAVE_SYS_STAT_H
58 #  include <sys/stat.h>
59 #endif // HAVE_SYS_STAT_H
60
61 #ifdef    HAVE_STDARG_H
62 #  include <stdarg.h>
63 #endif // HAVE_STDARG_H
64 }
65
66 #include <assert.h>
67
68 #include <algorithm>
69 #include <functional>
70 #include <string>
71 using std::string;
72
73 #include "i18n.hh"
74 #include "blackbox.hh"
75 #include "Clientmenu.hh"
76 #include "Font.hh"
77 #include "GCCache.hh"
78 #include "Iconmenu.hh"
79 #include "Image.hh"
80 #include "Screen.hh"
81 #include "Slit.hh"
82 #include "Rootmenu.hh"
83 #include "Toolbar.hh"
84 #include "Util.hh"
85 #include "Window.hh"
86 #include "Workspace.hh"
87 #include "Workspacemenu.hh"
88 #include "XAtom.hh"
89
90 #ifndef   FONT_ELEMENT_SIZE
91 #define   FONT_ELEMENT_SIZE 50
92 #endif // FONT_ELEMENT_SIZE
93
94
95 static bool running = True;
96
97 static int anotherWMRunning(Display *display, XErrorEvent *) {
98   fprintf(stderr, i18n(ScreenSet, ScreenAnotherWMRunning,
99           "BScreen::BScreen: an error occured while querying the X server.\n"
100           "  another window manager already running on display %s.\n"),
101           DisplayString(display));
102
103   running = False;
104
105   return(-1);
106 }
107
108
109 BScreen::BScreen(Blackbox *bb, unsigned int scrn) : ScreenInfo(bb, scrn) {
110   blackbox = bb;
111   screenstr = "session.screen" + itostring(scrn) + '.';
112   config = blackbox->getConfig();
113   xatom = blackbox->getXAtom();
114
115   event_mask = ColormapChangeMask | EnterWindowMask | PropertyChangeMask |
116     SubstructureRedirectMask | ButtonPressMask | ButtonReleaseMask;
117
118   XErrorHandler old = XSetErrorHandler((XErrorHandler) anotherWMRunning);
119   XSelectInput(getBaseDisplay()->getXDisplay(), getRootWindow(), event_mask);
120   XSync(getBaseDisplay()->getXDisplay(), False);
121   XSetErrorHandler((XErrorHandler) old);
122
123   managed = running;
124   if (! managed) return;
125
126   fprintf(stderr, i18n(ScreenSet, ScreenManagingScreen,
127                        "BScreen::BScreen: managing screen %d "
128                        "using visual 0x%lx, depth %d\n"),
129           getScreenNumber(), XVisualIDFromVisual(getVisual()),
130           getDepth());
131
132   rootmenu = 0;
133
134   resource.mstyle.t_font = resource.mstyle.f_font = resource.tstyle.font =
135     resource.wstyle.font = (BFont *) 0;
136
137   geom_pixmap = None;
138
139   xatom->setSupported(this);    // set-up netwm support
140 #ifdef    HAVE_GETPID
141   xatom->setValue(getRootWindow(), XAtom::blackbox_pid, XAtom::cardinal,
142                   (unsigned long) getpid());
143 #endif // HAVE_GETPID
144   unsigned long geometry[] = { getWidth(),
145                                getHeight()};
146   xatom->setValue(getRootWindow(), XAtom::net_desktop_geometry,
147                   XAtom::cardinal, geometry, 2);
148   unsigned long viewport[] = {0,0};
149   xatom->setValue(getRootWindow(), XAtom::net_desktop_viewport,
150                   XAtom::cardinal, viewport, 2);
151                   
152
153   XDefineCursor(blackbox->getXDisplay(), getRootWindow(),
154                 blackbox->getSessionCursor());
155
156   // start off full screen, top left.
157   usableArea.setSize(getWidth(), getHeight());
158
159   image_control =
160     new BImageControl(blackbox, this, True, blackbox->getColorsPerChannel(),
161                       blackbox->getCacheLife(), blackbox->getCacheMax());
162   image_control->installRootColormap();
163   root_colormap_installed = True;
164
165   load_rc();
166   LoadStyle();
167
168   XGCValues gcv;
169   gcv.foreground = WhitePixel(blackbox->getXDisplay(), getScreenNumber())
170     ^ BlackPixel(blackbox->getXDisplay(), getScreenNumber());
171   gcv.function = GXxor;
172   gcv.subwindow_mode = IncludeInferiors;
173   opGC = XCreateGC(blackbox->getXDisplay(), getRootWindow(),
174                    GCForeground | GCFunction | GCSubwindowMode, &gcv);
175
176   const char *s =  i18n(ScreenSet, ScreenPositionLength,
177                         "0: 0000 x 0: 0000");
178   geom_w = resource.wstyle.font->measureString(s) + resource.bevel_width * 2;
179   geom_h = resource.wstyle.font->height() + resource.bevel_width * 2;
180
181   XSetWindowAttributes attrib;
182   unsigned long mask = CWBorderPixel | CWColormap | CWSaveUnder;
183   attrib.border_pixel = getBorderColor()->pixel();
184   attrib.colormap = getColormap();
185   attrib.save_under = True;
186
187   geom_window = XCreateWindow(blackbox->getXDisplay(), getRootWindow(),
188                               0, 0, geom_w, geom_h, resource.border_width,
189                               getDepth(), InputOutput, getVisual(),
190                               mask, &attrib);
191   geom_visible = False;
192
193   BTexture* texture = &(resource.wstyle.l_focus);
194   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
195   if (geom_pixmap == ParentRelative) {
196     texture = &(resource.wstyle.t_focus);
197     geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
198   }
199   if (! geom_pixmap)
200     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
201                          texture->color().pixel());
202   else
203     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
204                                geom_window, geom_pixmap);
205
206   workspacemenu = new Workspacemenu(this);
207   iconmenu = new Iconmenu(this);
208   configmenu = new Configmenu(this);
209
210   if (resource.workspaces != 0) {
211     for (unsigned int i = 0; i < resource.workspaces; ++i) {
212       Workspace *wkspc = new Workspace(this, workspacesList.size());
213       workspacesList.push_back(wkspc);
214       workspacemenu->insertWorkspace(wkspc);
215       workspacemenu->update();
216
217     }
218   } else {
219     Workspace *wkspc = new Workspace(this, workspacesList.size());
220     workspacesList.push_back(wkspc);
221     workspacemenu->insertWorkspace(wkspc);
222     workspacemenu->update();
223   }
224   saveWorkspaceNames();
225
226   updateNetizenWorkspaceCount();
227
228   workspacemenu->insert(i18n(IconSet, IconIcons, "Icons"), iconmenu);
229   workspacemenu->update();
230
231   current_workspace = workspacesList.front();
232   
233   xatom->setValue(getRootWindow(), XAtom::net_current_desktop,
234                   XAtom::cardinal, 0); //first workspace
235
236   workspacemenu->setItemSelected(2, True);
237
238   toolbar = new Toolbar(this);
239
240   slit = new Slit(this);
241
242   InitMenu();
243
244   raiseWindows(0, 0);     // this also initializes the empty stacking list
245   rootmenu->update();
246
247   updateClientList();     // initialize the client lists, which will be empty
248   updateAvailableArea();
249
250   changeWorkspaceID(0);
251
252   unsigned int i, j, nchild;
253   Window r, p, *children;
254   XQueryTree(blackbox->getXDisplay(), getRootWindow(), &r, &p,
255              &children, &nchild);
256
257   // preen the window list of all icon windows... for better dockapp support
258   for (i = 0; i < nchild; i++) {
259     if (children[i] == None) continue;
260
261     XWMHints *wmhints = XGetWMHints(blackbox->getXDisplay(),
262                                     children[i]);
263
264     if (wmhints) {
265       if ((wmhints->flags & IconWindowHint) &&
266           (wmhints->icon_window != children[i])) {
267         for (j = 0; j < nchild; j++) {
268           if (children[j] == wmhints->icon_window) {
269             children[j] = None;
270             break;
271           }
272         }
273       }
274
275       XFree(wmhints);
276     }
277   }
278
279   // manage shown windows
280   for (i = 0; i < nchild; ++i) {
281     if (children[i] == None || (! blackbox->validateWindow(children[i])))
282       continue;
283
284     XWindowAttributes attrib;
285     if (XGetWindowAttributes(blackbox->getXDisplay(), children[i], &attrib)) {
286       if (attrib.override_redirect) continue;
287
288       if (attrib.map_state != IsUnmapped) {
289         manageWindow(children[i]);
290       }
291     }
292   }
293
294   XFree(children);
295
296   // call this again just in case a window we found updates the Strut list
297   updateAvailableArea();
298 }
299
300
301 BScreen::~BScreen(void) {
302   if (! managed) return;
303
304   if (geom_pixmap != None)
305     image_control->removeImage(geom_pixmap);
306
307   if (geom_window != None)
308     XDestroyWindow(blackbox->getXDisplay(), geom_window);
309
310   std::for_each(workspacesList.begin(), workspacesList.end(),
311                 PointerAssassin());
312
313   std::for_each(iconList.begin(), iconList.end(), PointerAssassin());
314
315   std::for_each(netizenList.begin(), netizenList.end(), PointerAssassin());
316
317   while (! systrayWindowList.empty())
318     removeSystrayWindow(systrayWindowList[0]);
319
320   delete rootmenu;
321   delete workspacemenu;
322   delete iconmenu;
323   delete configmenu;
324   delete slit;
325   delete toolbar;
326   delete image_control;
327
328   if (resource.wstyle.font)
329     delete resource.wstyle.font;
330   if (resource.mstyle.t_font)
331     delete resource.mstyle.t_font;
332   if (resource.mstyle.f_font)
333     delete resource.mstyle.f_font;
334   if (resource.tstyle.font)
335     delete resource.tstyle.font;
336
337   XFreeGC(blackbox->getXDisplay(), opGC);
338 }
339
340
341 void BScreen::saveSloppyFocus(bool s) {
342   resource.sloppy_focus = s;
343
344   string fmodel;
345   if (resource.sloppy_focus) {
346     fmodel = "SloppyFocus";
347     if (resource.auto_raise) fmodel += " AutoRaise";
348     if (resource.click_raise) fmodel += " ClickRaise";
349   } else {
350     fmodel = "ClickToFocus";
351   }
352   config->setValue(screenstr + "focusModel", fmodel);
353 }
354
355
356 void BScreen::saveAutoRaise(bool a) {
357   resource.auto_raise = a;
358   saveSloppyFocus(resource.sloppy_focus);
359 }
360
361
362 void BScreen::saveClickRaise(bool c) {
363   resource.click_raise = c;
364   saveSloppyFocus(resource.sloppy_focus);
365 }
366
367
368 void BScreen::saveImageDither(bool d) {
369   image_control->setDither(d);
370   config->setValue(screenstr + "imageDither", doImageDither());
371 }
372
373
374 void BScreen::saveOpaqueMove(bool o) {
375   resource.opaque_move = o;
376   config->setValue(screenstr + "opaqueMove", resource.opaque_move);
377 }
378
379
380 void BScreen::saveFullMax(bool f) {
381   resource.full_max = f;
382   config->setValue(screenstr + "fullMaximization", resource.full_max);
383 }
384
385
386 void BScreen::saveFocusNew(bool f) {
387   resource.focus_new = f;
388   config->setValue(screenstr + "focusNewWindows", resource.focus_new);
389 }
390
391
392 void BScreen::saveFocusLast(bool f) {
393   resource.focus_last = f;
394   config->setValue(screenstr + "focusLastWindow", resource.focus_last);
395 }
396
397
398 void BScreen::saveAAFonts(bool f) {
399   resource.aa_fonts = f;
400   reconfigure();
401   config->setValue(screenstr + "antialiasFonts", resource.aa_fonts);
402 }
403
404
405 void BScreen::saveHideToolbar(bool h) {
406   resource.hide_toolbar = h;
407   if (resource.hide_toolbar)
408     toolbar->unmapToolbar();
409   else
410     toolbar->mapToolbar();
411   config->setValue(screenstr + "hideToolbar", resource.hide_toolbar);
412 }
413
414
415 void BScreen::saveWindowToWindowSnap(bool s) {
416   resource.window_to_window_snap = s;
417   config->setValue(screenstr + "windowToWindowSnap",
418                    resource.window_to_window_snap);
419 }
420
421
422 void BScreen::saveResizeZones(unsigned int z) {
423   resource.resize_zones = z;
424   config->setValue(screenstr + "resizeZones", resource.resize_zones);
425 }
426
427
428 void BScreen::saveWindowCornerSnap(bool s) {
429   resource.window_corner_snap = s;
430   config->setValue(screenstr + "windowCornerSnap",
431                    resource.window_corner_snap);
432 }
433
434
435 void BScreen::saveWorkspaces(unsigned int w) {
436   resource.workspaces = w;
437   config->setValue(screenstr + "workspaces", resource.workspaces);
438 }
439
440
441 void BScreen::savePlacementPolicy(int p) {
442   resource.placement_policy = p; 
443   const char *placement;
444   switch (resource.placement_policy) {
445   case CascadePlacement: placement = "CascadePlacement"; break;
446   case UnderMousePlacement: placement = "UnderMousePlacement"; break;
447   case ClickMousePlacement: placement = "ClickMousePlacement"; break;
448   case ColSmartPlacement: placement = "ColSmartPlacement"; break;
449   case RowSmartPlacement: default: placement = "RowSmartPlacement"; break;
450   }
451   config->setValue(screenstr + "windowPlacement", placement);
452 }
453
454
455 void BScreen::saveEdgeSnapThreshold(int t) {
456   resource.edge_snap_threshold = t;
457   config->setValue(screenstr + "edgeSnapThreshold",
458                    resource.edge_snap_threshold);
459 }
460
461
462 void BScreen::saveRowPlacementDirection(int d) {
463   resource.row_direction = d;
464   config->setValue(screenstr + "rowPlacementDirection",
465                    resource.row_direction == LeftRight ?
466                    "LeftToRight" : "RightToLeft");
467 }
468
469
470 void BScreen::saveColPlacementDirection(int d) {
471   resource.col_direction = d;
472   config->setValue(screenstr + "colPlacementDirection",
473                    resource.col_direction == TopBottom ?
474                    "TopToBottom" : "BottomToTop");
475 }
476
477
478 #ifdef    HAVE_STRFTIME
479 void BScreen::saveStrftimeFormat(const std::string& format) {
480   resource.strftime_format = format;
481   config->setValue(screenstr + "strftimeFormat", resource.strftime_format);
482 }
483
484 #else // !HAVE_STRFTIME
485
486 void BScreen::saveDateFormat(int f) {
487   resource.date_format = f;
488   config->setValue(screenstr + "dateFormat",
489                    resource.date_format == B_EuropeanDate ?
490                    "European" : "American");
491 }
492
493
494 void BScreen::saveClock24Hour(bool c) {
495   resource.clock24hour = c;
496   config->setValue(screenstr + "clockFormat", resource.clock24hour ? 24 : 12);
497 }
498 #endif // HAVE_STRFTIME
499
500
501 void BScreen::saveWorkspaceNames() {
502   string names;
503  
504   for (unsigned int i = 0; i < workspacesList.size(); ++i) {
505     names += workspacesList[i]->getName();
506     if (i < workspacesList.size() - 1)
507       names += ',';
508   }
509
510   config->setValue(screenstr + "workspaceNames", names);
511 }
512
513
514 void BScreen::savePlaceIgnoreShaded(bool i) {
515   resource.ignore_shaded = i;
516   config->setValue(screenstr + "placementIgnoreShaded",
517                    resource.ignore_shaded);
518 }
519
520
521 void BScreen::savePlaceIgnoreMaximized(bool i) {
522   resource.ignore_maximized = i;
523   config->setValue(screenstr + "placementIgnoreMaximized",
524                    resource.ignore_maximized);
525 }
526
527
528 void BScreen::save_rc(void) {
529   saveSloppyFocus(resource.sloppy_focus);
530   saveAutoRaise(resource.auto_raise);
531   saveImageDither(doImageDither());
532   saveAAFonts(resource.aa_fonts);
533   saveResizeZones(resource.resize_zones);
534   saveOpaqueMove(resource.opaque_move);
535   saveFullMax(resource.full_max);
536   saveFocusNew(resource.focus_new);
537   saveFocusLast(resource.focus_last);
538   saveHideToolbar(resource.hide_toolbar);
539   saveWindowToWindowSnap(resource.window_to_window_snap);
540   saveWindowCornerSnap(resource.window_corner_snap);
541   saveWorkspaces(resource.workspaces);
542   savePlacementPolicy(resource.placement_policy);
543   saveEdgeSnapThreshold(resource.edge_snap_threshold);
544   saveRowPlacementDirection(resource.row_direction);
545   saveColPlacementDirection(resource.col_direction);
546 #ifdef    HAVE_STRFTIME
547   saveStrftimeFormat(resource.strftime_format); 
548 #else // !HAVE_STRFTIME
549   saveDateFormat(resource.date_format);
550   savwClock24Hour(resource.clock24hour);
551 #endif // HAVE_STRFTIME
552   savePlaceIgnoreShaded(resource.ignore_shaded);
553   savePlaceIgnoreMaximized(resource.ignore_maximized);
554
555   toolbar->save_rc();
556   slit->save_rc();
557 }
558
559
560 void BScreen::load_rc(void) {
561   std::string s;
562   bool b;
563
564   if (! config->getValue(screenstr + "fullMaximization", resource.full_max))
565     resource.full_max = false;
566
567   if (! config->getValue(screenstr + "focusNewWindows", resource.focus_new))
568     resource.focus_new = false;
569
570   if (! config->getValue(screenstr + "focusLastWindow", resource.focus_last))
571     resource.focus_last = false;
572
573   if (! config->getValue(screenstr + "workspaces", resource.workspaces))
574     resource.workspaces = 1;
575
576   if (! config->getValue(screenstr + "opaqueMove", resource.opaque_move))
577     resource.opaque_move = false;
578
579   if (! config->getValue(screenstr + "antialiasFonts", resource.aa_fonts))
580     resource.aa_fonts = true;
581
582   if (! config->getValue(screenstr + "resizeZones", resource.resize_zones) ||
583       (resource.resize_zones != 1 && resource.resize_zones != 2 &&
584        resource.resize_zones != 4))
585       resource.resize_zones = 4;
586
587   if (! config->getValue(screenstr + "hideToolbar", resource.hide_toolbar))
588     resource.hide_toolbar = false;
589
590   if (! config->getValue(screenstr + "windowToWindowSnap",
591                          resource.window_to_window_snap))
592     resource.window_to_window_snap = true;
593
594   if (! config->getValue(screenstr + "windowCornerSnap",
595                          resource.window_corner_snap))
596     resource.window_corner_snap = true;
597
598   if (! config->getValue(screenstr + "imageDither", b))
599     b = true;
600   image_control->setDither(b);
601
602   if (! config->getValue(screenstr + "edgeSnapThreshold",
603                         resource.edge_snap_threshold))
604     resource.edge_snap_threshold = 4;
605   
606   if (config->getValue(screenstr + "rowPlacementDirection", s) &&
607       s == "RightToLeft")
608     resource.row_direction = RightLeft;
609   else
610     resource.row_direction = LeftRight;
611
612   if (config->getValue(screenstr + "colPlacementDirection", s) &&
613       s == "BottomToTop")
614     resource.col_direction = BottomTop;
615   else
616     resource.col_direction = TopBottom;
617
618   if (config->getValue(screenstr + "workspaceNames", s)) {
619     XAtom::StringVect workspaceNames;
620
621     string::const_iterator it = s.begin(), end = s.end();
622     while(1) {
623       string::const_iterator tmp = it;     // current string.begin()
624       it = std::find(tmp, end, ',');       // look for comma between tmp and end
625       workspaceNames.push_back(string(tmp, it)); // s[tmp:it]
626       if (it == end)
627         break;
628       ++it;
629     }
630
631     xatom->setValue(getRootWindow(), XAtom::net_desktop_names, XAtom::utf8,
632                     workspaceNames);
633   }
634
635   resource.sloppy_focus = true;
636   resource.auto_raise = false;
637   resource.click_raise = false;
638   if (config->getValue(screenstr + "focusModel", s)) {
639     if (s.find("ClickToFocus") != string::npos) {
640       resource.sloppy_focus = false;
641     } else {
642       // must be sloppy
643       if (s.find("AutoRaise") != string::npos)
644         resource.auto_raise = true;
645       if (s.find("ClickRaise") != string::npos)
646         resource.click_raise = true;
647     }
648   }
649
650   if (config->getValue(screenstr + "windowPlacement", s)) {
651     if (s == "CascadePlacement")
652       resource.placement_policy = CascadePlacement;
653     else if (s == "UnderMousePlacement")
654       resource.placement_policy = UnderMousePlacement;
655     else if (s == "ClickMousePlacement")
656       resource.placement_policy = ClickMousePlacement;
657     else if (s == "ColSmartPlacement")
658       resource.placement_policy = ColSmartPlacement;
659     else //if (s == "RowSmartPlacement")
660       resource.placement_policy = RowSmartPlacement;
661   } else
662     resource.placement_policy = RowSmartPlacement;
663
664 #ifdef    HAVE_STRFTIME
665   if (! config->getValue(screenstr + "strftimeFormat",
666                          resource.strftime_format))
667     resource.strftime_format = "%I:%M %p";
668 #else // !HAVE_STRFTIME
669   long l;
670
671   if (config->getValue(screenstr + "dateFormat", s) && s == "European")
672     resource.date_format = B_EuropeanDate;
673  else
674     resource.date_format = B_AmericanDate;
675
676   if (! config->getValue(screenstr + "clockFormat", l))
677     l = 12;
678   resource.clock24hour = l == 24;
679 #endif // HAVE_STRFTIME
680   
681   if (! config->getValue(screenstr + "placementIgnoreShaded",
682                          resource.ignore_shaded))
683     resource.ignore_shaded = true;
684
685   if (! config->getValue(screenstr + "placementIgnoreMaximized",
686                          resource.ignore_maximized))
687     resource.ignore_maximized = true;
688 }
689
690
691 void BScreen::changeWorkspaceCount(unsigned int new_count) {
692   assert(new_count > 0);
693
694   if (new_count < workspacesList.size()) {
695     // shrink
696     for (unsigned int i = workspacesList.size(); i > new_count; --i)
697       removeLastWorkspace();
698     // removeLast already sets the current workspace to the 
699     // last available one.
700   } else if (new_count > workspacesList.size()) {
701     // grow
702     for(unsigned int i = workspacesList.size(); i < new_count; ++i)
703       addWorkspace();
704   }
705 }
706
707
708 void BScreen::reconfigure(void) {
709   // don't reconfigure while saving the initial rc file, it's a waste and it
710   // breaks somethings (workspace names)
711   if (blackbox->isStartup()) return;
712
713   load_rc();
714   toolbar->load_rc();
715   slit->load_rc();
716   LoadStyle();
717
718   // we need to do this explicitly, because just loading this value from the rc
719   // does nothing
720   changeWorkspaceCount(resource.workspaces);
721
722   XGCValues gcv;
723   gcv.foreground = WhitePixel(blackbox->getXDisplay(),
724                               getScreenNumber());
725   gcv.function = GXinvert;
726   gcv.subwindow_mode = IncludeInferiors;
727   XChangeGC(blackbox->getXDisplay(), opGC,
728             GCForeground | GCFunction | GCSubwindowMode, &gcv);
729
730   const char *s = i18n(ScreenSet, ScreenPositionLength,
731                        "0: 0000 x 0: 0000");
732
733   geom_w = resource.wstyle.font->measureString(s) + resource.bevel_width * 2;
734   geom_h = resource.wstyle.font->height() + resource.bevel_width * 2;
735
736   BTexture* texture = &(resource.wstyle.l_focus);
737   geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
738   if (geom_pixmap == ParentRelative) {
739     texture = &(resource.wstyle.t_focus);
740     geom_pixmap = texture->render(geom_w, geom_h, geom_pixmap);
741   }
742   if (! geom_pixmap)
743     XSetWindowBackground(blackbox->getXDisplay(), geom_window,
744                          texture->color().pixel());
745   else
746     XSetWindowBackgroundPixmap(blackbox->getXDisplay(),
747                                geom_window, geom_pixmap);
748
749   XSetWindowBorderWidth(blackbox->getXDisplay(), geom_window,
750                         resource.border_width);
751   XSetWindowBorder(blackbox->getXDisplay(), geom_window,
752                    resource.border_color.pixel());
753
754   workspacemenu->reconfigure();
755   iconmenu->reconfigure();
756
757   typedef std::vector<int> SubList;
758   SubList remember_subs;
759
760   // save the current open menus
761   Basemenu *menu = rootmenu;
762   int submenu;
763   while ((submenu = menu->getCurrentSubmenu()) >= 0) {
764     remember_subs.push_back(submenu);
765     menu = menu->find(submenu)->submenu();
766     assert(menu);
767   }
768   
769   InitMenu();
770   raiseWindows(0, 0);
771   rootmenu->reconfigure();
772
773   // reopen the saved menus
774   menu = rootmenu;
775   const SubList::iterator subs_end = remember_subs.end();
776   for (SubList::iterator it = remember_subs.begin(); it != subs_end; ++it) {
777     menu->drawSubmenu(*it);
778     menu = menu->find(*it)->submenu();
779     if (! menu)
780       break;
781   }
782
783   configmenu->reconfigure();
784
785   toolbar->reconfigure();
786
787   slit->reconfigure();
788
789   std::for_each(workspacesList.begin(), workspacesList.end(),
790                 std::mem_fun(&Workspace::reconfigure));
791
792   BlackboxWindowList::iterator iit = iconList.begin();
793   for (; iit != iconList.end(); ++iit) {
794     BlackboxWindow *bw = *iit;
795     if (bw->validateClient())
796       bw->reconfigure();
797   }
798
799   image_control->timeout();
800 }
801
802
803 void BScreen::rereadMenu(void) {
804   InitMenu();
805   raiseWindows(0, 0);
806
807   rootmenu->reconfigure();
808 }
809
810
811 void BScreen::LoadStyle(void) {
812   Configuration style(False);
813
814   const char *sfile = blackbox->getStyleFilename();
815   if (sfile != NULL) {
816     style.setFile(sfile);
817     if (! style.load()) {
818       style.setFile(DEFAULTSTYLE);
819       if (! style.load())
820         style.create();  // hardcoded default values will be used.
821     }
822   }
823
824   // merge in the rc file
825   style.merge(config->file(), True);
826
827   string s;
828
829   // load fonts/fontsets
830   if (resource.wstyle.font)
831     delete resource.wstyle.font;
832   if (resource.tstyle.font)
833     delete resource.tstyle.font;
834   if (resource.mstyle.f_font)
835     delete resource.mstyle.f_font;
836   if (resource.mstyle.t_font)
837     delete resource.mstyle.t_font;
838   resource.wstyle.font = resource.tstyle.font = resource.mstyle.f_font =
839     resource.mstyle.t_font = (BFont *) 0;
840
841   resource.wstyle.font = readDatabaseFont("window.", style);
842   resource.tstyle.font = readDatabaseFont("toolbar.", style);
843   resource.mstyle.t_font = readDatabaseFont("menu.title.", style);
844   resource.mstyle.f_font = readDatabaseFont("menu.frame.", style);
845
846   // load window config
847   resource.wstyle.t_focus =
848     readDatabaseTexture("window.title.focus", "white", style);
849   resource.wstyle.t_unfocus =
850     readDatabaseTexture("window.title.unfocus", "black", style);
851   resource.wstyle.l_focus =
852     readDatabaseTexture("window.label.focus", "white", style);
853   resource.wstyle.l_unfocus =
854     readDatabaseTexture("window.label.unfocus", "black", style);
855   resource.wstyle.h_focus =
856     readDatabaseTexture("window.handle.focus", "white", style);
857   resource.wstyle.h_unfocus =
858     readDatabaseTexture("window.handle.unfocus", "black", style);
859   resource.wstyle.g_focus =
860     readDatabaseTexture("window.grip.focus", "white", style);
861   resource.wstyle.g_unfocus =
862     readDatabaseTexture("window.grip.unfocus", "black", style);
863   resource.wstyle.b_focus =
864     readDatabaseTexture("window.button.focus", "white", style);
865   resource.wstyle.b_unfocus =
866     readDatabaseTexture("window.button.unfocus", "black", style);
867   resource.wstyle.b_pressed =
868     readDatabaseTexture("window.button.pressed", "black", style);
869   resource.wstyle.f_focus =
870     readDatabaseColor("window.frame.focusColor", "white", style);
871   resource.wstyle.f_unfocus =
872     readDatabaseColor("window.frame.unfocusColor", "black", style);
873   resource.wstyle.l_text_focus =
874     readDatabaseColor("window.label.focus.textColor", "black", style);
875   resource.wstyle.l_text_unfocus =
876     readDatabaseColor("window.label.unfocus.textColor", "white", style);
877   resource.wstyle.b_pic_focus =
878     readDatabaseColor("window.button.focus.picColor", "black", style);
879   resource.wstyle.b_pic_unfocus =
880     readDatabaseColor("window.button.unfocus.picColor", "white", style);
881
882   resource.wstyle.justify = LeftJustify;
883   if (style.getValue("window.justify", s)) {
884     if (s == "right" || s == "Right")
885       resource.wstyle.justify = RightJustify;
886     else if (s == "center" || s == "Center")
887       resource.wstyle.justify = CenterJustify;
888   }
889
890   // load toolbar config
891   resource.tstyle.toolbar =
892     readDatabaseTexture("toolbar", "black", style);
893   resource.tstyle.label =
894     readDatabaseTexture("toolbar.label", "black", style);
895   resource.tstyle.window =
896     readDatabaseTexture("toolbar.windowLabel", "black", style);
897   resource.tstyle.button =
898     readDatabaseTexture("toolbar.button", "white", style);
899   resource.tstyle.pressed =
900     readDatabaseTexture("toolbar.button.pressed", "black", style);
901   resource.tstyle.clock =
902     readDatabaseTexture("toolbar.clock", "black", style);
903   resource.tstyle.l_text =
904     readDatabaseColor("toolbar.label.textColor", "white", style);
905   resource.tstyle.w_text =
906     readDatabaseColor("toolbar.windowLabel.textColor", "white", style);
907   resource.tstyle.c_text =
908     readDatabaseColor("toolbar.clock.textColor", "white", style);
909   resource.tstyle.b_pic =
910     readDatabaseColor("toolbar.button.picColor", "black", style);
911
912   resource.tstyle.justify = LeftJustify;
913   if (style.getValue("toolbar.justify", s)) {
914     if (s == "right" || s == "Right")
915       resource.tstyle.justify = RightJustify;
916     else if (s == "center" || s == "Center")
917       resource.tstyle.justify = CenterJustify;
918   }
919
920   // load menu config
921   resource.mstyle.title =
922     readDatabaseTexture("menu.title", "white", style);
923   resource.mstyle.frame =
924     readDatabaseTexture("menu.frame", "black", style);
925   resource.mstyle.hilite =
926     readDatabaseTexture("menu.hilite", "white", style);
927   resource.mstyle.t_text =
928     readDatabaseColor("menu.title.textColor", "black", style);
929   resource.mstyle.f_text =
930     readDatabaseColor("menu.frame.textColor", "white", style);
931   resource.mstyle.d_text =
932     readDatabaseColor("menu.frame.disableColor", "black", style);
933   resource.mstyle.h_text =
934     readDatabaseColor("menu.hilite.textColor", "black", style);
935
936   resource.mstyle.t_justify = LeftJustify;
937   if (style.getValue("menu.title.justify", s)) {
938     if (s == "right" || s == "Right")
939       resource.mstyle.t_justify = RightJustify;
940     else if (s == "center" || s == "Center")
941       resource.mstyle.t_justify = CenterJustify;
942   }
943
944   resource.mstyle.f_justify = LeftJustify;
945   if (style.getValue("menu.frame.justify", s)) {
946     if (s == "right" || s == "Right")
947       resource.mstyle.f_justify = RightJustify;
948     else if (s == "center" || s == "Center")
949       resource.mstyle.f_justify = CenterJustify;
950   }
951
952   resource.mstyle.bullet = Basemenu::Triangle;
953   if (style.getValue("menu.bullet", s)) {
954     if (s == "empty" || s == "Empty")
955       resource.mstyle.bullet = Basemenu::Empty;
956     else if (s == "square" || s == "Square")
957       resource.mstyle.bullet = Basemenu::Square;
958     else if (s == "diamond" || s == "Diamond")
959       resource.mstyle.bullet = Basemenu::Diamond;
960   }
961
962   resource.mstyle.bullet_pos = Basemenu::Left;
963   if (style.getValue("menu.bullet.position", s)) {
964     if (s == "right" || s == "Right")
965       resource.mstyle.bullet_pos = Basemenu::Right;
966   }
967
968   resource.border_color =
969     readDatabaseColor("borderColor", "black", style);
970
971   // load bevel, border and handle widths
972   if (! style.getValue("handleWidth", resource.handle_width) ||
973       resource.handle_width > (getWidth() / 2) || resource.handle_width == 0)
974     resource.handle_width = 6;
975
976   if (! style.getValue("borderWidth", resource.border_width))
977     resource.border_width = 1;
978
979   if (! style.getValue("bevelWidth", resource.bevel_width) ||
980       resource.bevel_width > (getWidth() / 2) || resource.bevel_width == 0)
981     resource.bevel_width = 3;
982
983   if (! style.getValue("frameWidth", resource.frame_width) ||
984       resource.frame_width > (getWidth() / 2))
985     resource.frame_width = resource.bevel_width;
986
987   if (style.getValue("rootCommand", s))
988     bexec(s, displayString());
989 }
990
991
992 void BScreen::addIcon(BlackboxWindow *w) {
993   if (! w) return;
994
995   w->setWorkspace(BSENTINEL);
996   w->setWindowNumber(iconList.size());
997
998   iconList.push_back(w);
999
1000   const char* title = w->getIconTitle();
1001   iconmenu->insert(title);
1002   iconmenu->update();
1003 }
1004
1005
1006 void BScreen::removeIcon(BlackboxWindow *w) {
1007   if (! w) return;
1008
1009   iconList.remove(w);
1010
1011   iconmenu->remove(w->getWindowNumber());
1012   iconmenu->update();
1013
1014   BlackboxWindowList::iterator it = iconList.begin(),
1015     end = iconList.end();
1016   for (int i = 0; it != end; ++it)
1017     (*it)->setWindowNumber(i++);
1018 }
1019
1020
1021 BlackboxWindow *BScreen::getIcon(unsigned int index) {
1022   if (index < iconList.size()) {
1023     BlackboxWindowList::iterator it = iconList.begin();
1024     for (; index > 0; --index, ++it) ; /* increment to index */
1025     return *it;
1026   }
1027
1028   return (BlackboxWindow *) 0;
1029 }
1030
1031
1032 unsigned int BScreen::addWorkspace(void) {
1033   Workspace *wkspc = new Workspace(this, workspacesList.size());
1034   workspacesList.push_back(wkspc);
1035   saveWorkspaces(getWorkspaceCount());
1036   saveWorkspaceNames();
1037
1038   workspacemenu->insertWorkspace(wkspc);
1039   workspacemenu->update();
1040
1041   toolbar->reconfigure();
1042
1043   updateNetizenWorkspaceCount();
1044
1045   return workspacesList.size();
1046 }
1047
1048
1049 unsigned int BScreen::removeLastWorkspace(void) {
1050   if (workspacesList.size() == 1)
1051     return 1;
1052
1053   Workspace *wkspc = workspacesList.back();
1054
1055   if (current_workspace->getID() == wkspc->getID())
1056     changeWorkspaceID(current_workspace->getID() - 1);
1057
1058   wkspc->removeAll();
1059
1060   workspacemenu->removeWorkspace(wkspc);
1061   workspacemenu->update();
1062
1063   workspacesList.pop_back();
1064   delete wkspc;
1065
1066   saveWorkspaces(getWorkspaceCount());
1067   saveWorkspaceNames();
1068
1069   toolbar->reconfigure();
1070
1071   updateNetizenWorkspaceCount();
1072
1073   return workspacesList.size();
1074 }
1075
1076
1077 void BScreen::changeWorkspaceID(unsigned int id) {
1078   if (! current_workspace || id == current_workspace->getID()) return;
1079
1080   BlackboxWindow *focused = blackbox->getFocusedWindow();
1081   if (focused && focused->getScreen() == this) {
1082     assert(focused->isStuck() ||
1083            focused->getWorkspaceNumber() == current_workspace->getID());
1084
1085     current_workspace->setLastFocusedWindow(focused);
1086   } else {
1087     // if no window had focus, no need to store a last focus
1088     current_workspace->setLastFocusedWindow((BlackboxWindow *) 0);
1089   }
1090
1091   // when we switch workspaces, unfocus whatever was focused
1092   blackbox->setFocusedWindow((BlackboxWindow *) 0);
1093     
1094   current_workspace->hideAll();
1095   workspacemenu->setItemSelected(current_workspace->getID() + 2, False);
1096
1097   current_workspace = getWorkspace(id);
1098
1099   xatom->setValue(getRootWindow(), XAtom::net_current_desktop,
1100                   XAtom::cardinal, id);
1101
1102   workspacemenu->setItemSelected(current_workspace->getID() + 2, True);
1103   toolbar->redrawWorkspaceLabel(True);
1104
1105   current_workspace->showAll();
1106
1107   if (resource.focus_last && current_workspace->getLastFocusedWindow()) {
1108     XSync(blackbox->getXDisplay(), False);
1109     current_workspace->getLastFocusedWindow()->setInputFocus();
1110   }
1111
1112   updateNetizenCurrentWorkspace();
1113 }
1114
1115
1116 /*
1117  * Set the _NET_CLIENT_LIST root window property.
1118  */
1119 void BScreen::updateClientList(void) {
1120   if (windowList.size() > 0) {
1121     Window *windows = new Window[windowList.size()];
1122     Window *win_it = windows;
1123     BlackboxWindowList::iterator it = windowList.begin();
1124     const BlackboxWindowList::iterator end = windowList.end();
1125     for (; it != end; ++it, ++win_it)
1126       *win_it = (*it)->getClientWindow();
1127     xatom->setValue(getRootWindow(), XAtom::net_client_list, XAtom::window,
1128                     windows, windowList.size());
1129     delete [] windows;
1130   } else
1131     xatom->setValue(getRootWindow(), XAtom::net_client_list, XAtom::window,
1132                     0, 0);
1133
1134   updateStackingList();
1135 }
1136
1137
1138 /*
1139  * Set the _NET_CLIENT_LIST_STACKING root window property.
1140  */
1141 void BScreen::updateStackingList(void) {
1142
1143   BlackboxWindowList stack_order;
1144
1145   /*
1146    * Get the stacking order from all of the workspaces.
1147    * We start with the current workspace so that the sticky windows will be
1148    * in the right order on the current workspace.
1149    * XXX: Do we need to have sticky windows in the list once for each workspace?
1150    */
1151   getCurrentWorkspace()->appendStackOrder(stack_order);
1152   for (unsigned int i = 0; i < getWorkspaceCount(); ++i)
1153     if (i != getCurrentWorkspaceID())
1154       getWorkspace(i)->appendStackOrder(stack_order);
1155
1156   if (stack_order.size() > 0) {
1157     // set the client list atoms
1158     Window *windows = new Window[stack_order.size()];
1159     Window *win_it = windows;
1160     BlackboxWindowList::iterator it = stack_order.begin(),
1161                                  end = stack_order.end();
1162     for (; it != end; ++it, ++win_it)
1163       *win_it = (*it)->getClientWindow();
1164     xatom->setValue(getRootWindow(), XAtom::net_client_list_stacking,
1165                     XAtom::window, windows, stack_order.size());
1166     delete [] windows;
1167   } else
1168     xatom->setValue(getRootWindow(), XAtom::net_client_list_stacking,
1169                     XAtom::window, 0, 0);
1170 }
1171
1172
1173 void BScreen::addSystrayWindow(Window window) {
1174   systrayWindowList.push_back(window);
1175   xatom->setValue(getRootWindow(), XAtom::kde_net_system_tray_windows,
1176                   XAtom::window,
1177                   &systrayWindowList[0], systrayWindowList.size());
1178   blackbox->saveSystrayWindowSearch(window, this);
1179 }
1180
1181
1182 void BScreen::removeSystrayWindow(Window window) {
1183   WindowList::iterator it = systrayWindowList.begin();
1184   const WindowList::iterator end = systrayWindowList.end();
1185   for (; it != end; ++it)
1186     if (*it == window) {
1187       systrayWindowList.erase(it);
1188       xatom->setValue(getRootWindow(), XAtom::kde_net_system_tray_windows,
1189                       XAtom::window,
1190                       &systrayWindowList[0], systrayWindowList.size());
1191       blackbox->removeSystrayWindowSearch(window);
1192       break;
1193     }
1194 }
1195
1196
1197 void BScreen::manageWindow(Window w) {
1198   // is the window a KDE systray window?
1199   Window systray;
1200   if (xatom->getValue(w, XAtom::kde_net_wm_system_tray_window_for,
1201                       XAtom::window, systray) && systray) {
1202     addSystrayWindow(w);
1203     return;
1204   }
1205
1206   new BlackboxWindow(blackbox, w, this);
1207
1208   BlackboxWindow *win = blackbox->searchWindow(w);
1209   if (! win)
1210     return;
1211
1212
1213   if (win->isNormal()) {
1214     // don't list non-normal windows as managed windows
1215     windowList.push_back(win);
1216     updateClientList();
1217   } else if (win->isDesktop()) {
1218     desktopWindowList.push_back(win->getFrameWindow());
1219   }
1220
1221   XMapRequestEvent mre;
1222   mre.window = w;
1223   if (blackbox->isStartup() && win->isNormal()) win->restoreAttributes();
1224   win->mapRequestEvent(&mre);
1225 }
1226
1227
1228 void BScreen::unmanageWindow(BlackboxWindow *w, bool remap) {
1229   w->restore(remap);
1230
1231   if (w->getWorkspaceNumber() != BSENTINEL &&
1232       w->getWindowNumber() != BSENTINEL)
1233     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
1234   else if (w->isIconic())
1235     removeIcon(w);
1236
1237   if (w->isNormal()) {
1238     // we don't list non-normal windows as managed windows
1239     windowList.remove(w);
1240     updateClientList();
1241   } else if (w->isDesktop()) {
1242     WindowList::iterator it = desktopWindowList.begin();
1243     const WindowList::iterator end = desktopWindowList.end();
1244     for (; it != end; ++it)
1245       if (*it == w->getFrameWindow()) {
1246         desktopWindowList.erase(it);
1247         break;
1248       }
1249     assert(it != end);  // the window wasnt a desktop window?
1250   }
1251
1252   if (blackbox->getFocusedWindow() == w)
1253     blackbox->setFocusedWindow((BlackboxWindow *) 0);
1254
1255   removeNetizen(w->getClientWindow());
1256
1257   /*
1258     some managed windows can also be window group controllers.  when
1259     unmanaging such windows, we should also delete the window group.
1260   */
1261   BWindowGroup *group = blackbox->searchGroup(w->getClientWindow());
1262   delete group;
1263
1264   delete w;
1265 }
1266
1267
1268 void BScreen::addNetizen(Netizen *n) {
1269   netizenList.push_back(n);
1270
1271   n->sendWorkspaceCount();
1272   n->sendCurrentWorkspace();
1273
1274   WorkspaceList::iterator it = workspacesList.begin();
1275   const WorkspaceList::iterator end = workspacesList.end();
1276   for (; it != end; ++it)
1277     (*it)->sendWindowList(*n);
1278
1279   Window f = ((blackbox->getFocusedWindow()) ?
1280               blackbox->getFocusedWindow()->getClientWindow() : None);
1281   n->sendWindowFocus(f);
1282 }
1283
1284
1285 void BScreen::removeNetizen(Window w) {
1286   NetizenList::iterator it = netizenList.begin();
1287   for (; it != netizenList.end(); ++it) {
1288     if ((*it)->getWindowID() == w) {
1289       delete *it;
1290       netizenList.erase(it);
1291       break;
1292     }
1293   }
1294 }
1295
1296
1297 void BScreen::updateWorkArea(void) {
1298   if (workspacesList.size() > 0) {
1299     unsigned long *dims = new unsigned long[4 * workspacesList.size()];
1300     for (unsigned int i = 0, m = workspacesList.size(); i < m; ++i) {
1301       // XXX: this could be different for each workspace
1302       const Rect &area = availableArea();
1303       dims[(i * 4) + 0] = area.x();
1304       dims[(i * 4) + 1] = area.y();
1305       dims[(i * 4) + 2] = area.width();
1306       dims[(i * 4) + 3] = area.height();
1307     }
1308     xatom->setValue(getRootWindow(), XAtom::net_workarea, XAtom::cardinal,
1309                     dims, 4 * workspacesList.size());
1310     delete [] dims;
1311   } else
1312     xatom->setValue(getRootWindow(), XAtom::net_workarea, XAtom::cardinal,
1313                     0, 0);
1314 }
1315
1316
1317 void BScreen::updateNetizenCurrentWorkspace(void) {
1318   std::for_each(netizenList.begin(), netizenList.end(),
1319                 std::mem_fun(&Netizen::sendCurrentWorkspace));
1320 }
1321
1322
1323 void BScreen::updateNetizenWorkspaceCount(void) {
1324   xatom->setValue(getRootWindow(), XAtom::net_number_of_desktops,
1325                   XAtom::cardinal, workspacesList.size());
1326
1327   updateWorkArea();
1328   
1329   std::for_each(netizenList.begin(), netizenList.end(),
1330                 std::mem_fun(&Netizen::sendWorkspaceCount));
1331 }
1332
1333
1334 void BScreen::updateNetizenWindowFocus(void) {
1335   Window f = ((blackbox->getFocusedWindow()) ?
1336               blackbox->getFocusedWindow()->getClientWindow() : None);
1337
1338   xatom->setValue(getRootWindow(), XAtom::net_active_window,
1339                   XAtom::window, f);
1340
1341   NetizenList::iterator it = netizenList.begin();
1342   for (; it != netizenList.end(); ++it)
1343     (*it)->sendWindowFocus(f);
1344 }
1345
1346
1347 void BScreen::updateNetizenWindowAdd(Window w, unsigned long p) {
1348   NetizenList::iterator it = netizenList.begin();
1349   for (; it != netizenList.end(); ++it) {
1350     (*it)->sendWindowAdd(w, p);
1351   }
1352 }
1353
1354
1355 void BScreen::updateNetizenWindowDel(Window w) {
1356   NetizenList::iterator it = netizenList.begin();
1357   for (; it != netizenList.end(); ++it)
1358     (*it)->sendWindowDel(w);
1359 }
1360
1361
1362 void BScreen::updateNetizenWindowRaise(Window w) {
1363   NetizenList::iterator it = netizenList.begin();
1364   for (; it != netizenList.end(); ++it)
1365     (*it)->sendWindowRaise(w);
1366 }
1367
1368
1369 void BScreen::updateNetizenWindowLower(Window w) {
1370   NetizenList::iterator it = netizenList.begin();
1371   for (; it != netizenList.end(); ++it)
1372     (*it)->sendWindowLower(w);
1373 }
1374
1375
1376 void BScreen::updateNetizenConfigNotify(XEvent *e) {
1377   NetizenList::iterator it = netizenList.begin();
1378   for (; it != netizenList.end(); ++it)
1379     (*it)->sendConfigNotify(e);
1380 }
1381
1382
1383 void BScreen::raiseWindows(Window *workspace_stack, unsigned int num) {
1384   // the 13 represents the number of blackbox windows such as menus
1385   Window *session_stack = new
1386     Window[(num + workspacesList.size() + rootmenuList.size() + 13)];
1387   unsigned int i = 0, k = num;
1388
1389   XRaiseWindow(blackbox->getXDisplay(), iconmenu->getWindowID());
1390   *(session_stack + i++) = iconmenu->getWindowID();
1391
1392   WorkspaceList::iterator wit = workspacesList.begin();
1393   const WorkspaceList::iterator w_end = workspacesList.end();
1394   for (; wit != w_end; ++wit)
1395     *(session_stack + i++) = (*wit)->getMenu()->getWindowID();
1396
1397   *(session_stack + i++) = workspacemenu->getWindowID();
1398
1399   *(session_stack + i++) = configmenu->getFocusmenu()->getWindowID();
1400   *(session_stack + i++) = configmenu->getPlacementmenu()->getWindowID();
1401   *(session_stack + i++) = configmenu->getWindowID();
1402
1403   *(session_stack + i++) = slit->getMenu()->getDirectionmenu()->getWindowID();
1404   *(session_stack + i++) = slit->getMenu()->getPlacementmenu()->getWindowID();
1405   *(session_stack + i++) = slit->getMenu()->getWindowID();
1406
1407   *(session_stack + i++) =
1408     toolbar->getMenu()->getPlacementmenu()->getWindowID();
1409   *(session_stack + i++) = toolbar->getMenu()->getWindowID();
1410
1411   RootmenuList::iterator rit = rootmenuList.begin();
1412   for (; rit != rootmenuList.end(); ++rit)
1413     *(session_stack + i++) = (*rit)->getWindowID();
1414   *(session_stack + i++) = rootmenu->getWindowID();
1415
1416   if (toolbar->isOnTop())
1417     *(session_stack + i++) = toolbar->getWindowID();
1418
1419   if (slit->isOnTop())
1420     *(session_stack + i++) = slit->getWindowID();
1421
1422   while (k--)
1423     *(session_stack + i++) = *(workspace_stack + k);
1424
1425   XRestackWindows(blackbox->getXDisplay(), session_stack, i);
1426
1427   delete [] session_stack;
1428
1429   updateStackingList();
1430 }
1431
1432
1433 void BScreen::lowerWindows(Window *workspace_stack, unsigned int num) {
1434   assert(num > 0);  // this would cause trouble in the XRaiseWindow call
1435
1436   Window *session_stack = new Window[(num + desktopWindowList.size())];
1437   unsigned int i = 0, k = num;
1438
1439   XLowerWindow(blackbox->getXDisplay(), workspace_stack[0]);
1440
1441   while (k--)
1442     *(session_stack + i++) = *(workspace_stack + k);
1443
1444   WindowList::iterator dit = desktopWindowList.begin();
1445   const WindowList::iterator d_end = desktopWindowList.end();
1446   for (; dit != d_end; ++dit)
1447     *(session_stack + i++) = *dit;
1448
1449   XRestackWindows(blackbox->getXDisplay(), session_stack, i);
1450
1451   delete [] session_stack;
1452
1453   updateStackingList();
1454 }
1455
1456
1457 void BScreen::reassociateWindow(BlackboxWindow *w, unsigned int wkspc_id,
1458                                 bool ignore_sticky) {
1459   if (! w) return;
1460
1461   if (wkspc_id == BSENTINEL)
1462     wkspc_id = current_workspace->getID();
1463
1464   if (w->getWorkspaceNumber() == wkspc_id)
1465     return;
1466
1467   if (w->isIconic()) {
1468     removeIcon(w);
1469     getWorkspace(wkspc_id)->addWindow(w);
1470   } else if (ignore_sticky || ! w->isStuck()) {
1471     getWorkspace(w->getWorkspaceNumber())->removeWindow(w);
1472     getWorkspace(wkspc_id)->addWindow(w);
1473   }
1474   updateStackingList();
1475 }
1476
1477
1478 void BScreen::propagateWindowName(const BlackboxWindow *bw) {
1479   if (bw->isIconic()) {
1480     iconmenu->changeItemLabel(bw->getWindowNumber(), bw->getIconTitle());
1481     iconmenu->update();
1482   }
1483   else {
1484     Clientmenu *clientmenu = getWorkspace(bw->getWorkspaceNumber())->getMenu();
1485     clientmenu->changeItemLabel(bw->getWindowNumber(), bw->getTitle());
1486     clientmenu->update();
1487
1488     if (blackbox->getFocusedWindow() == bw)
1489       toolbar->redrawWindowLabel(True);
1490   }
1491 }
1492
1493
1494 void BScreen::nextFocus(void) {
1495   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1496     *next = focused;
1497
1498   if (focused) {
1499     // if window is not on this screen, ignore it
1500     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1501       focused = (BlackboxWindow*) 0;
1502   }
1503
1504   if (focused && current_workspace->getCount() > 1) {
1505     // next is the next window to recieve focus, current is a place holder
1506     BlackboxWindow *current;
1507     do {
1508       current = next;
1509       next = current_workspace->getNextWindowInList(current);
1510     } while(! next->setInputFocus() && next != focused);
1511
1512     if (next != focused)
1513       current_workspace->raiseWindow(next);
1514   } else if (current_workspace->getCount() >= 1) {
1515     next = current_workspace->getTopWindowOnStack();
1516
1517     current_workspace->raiseWindow(next);
1518     next->setInputFocus();
1519   }
1520 }
1521
1522
1523 void BScreen::prevFocus(void) {
1524   BlackboxWindow *focused = blackbox->getFocusedWindow(),
1525     *next = focused;
1526
1527   if (focused) {
1528     // if window is not on this screen, ignore it
1529     if (focused->getScreen()->getScreenNumber() != getScreenNumber())
1530       focused = (BlackboxWindow*) 0;
1531   }
1532
1533   if (focused && current_workspace->getCount() > 1) {
1534     // next is the next window to recieve focus, current is a place holder
1535     BlackboxWindow *current;
1536     do {
1537       current = next;
1538       next = current_workspace->getPrevWindowInList(current);
1539     } while(! next->setInputFocus() && next != focused);
1540
1541     if (next != focused)
1542       current_workspace->raiseWindow(next);
1543   } else if (current_workspace->getCount() >= 1) {
1544     next = current_workspace->getTopWindowOnStack();
1545
1546     current_workspace->raiseWindow(next);
1547     next->setInputFocus();
1548   }
1549 }
1550
1551
1552 void BScreen::raiseFocus(void) {
1553   BlackboxWindow *focused = blackbox->getFocusedWindow();
1554   if (! focused)
1555     return;
1556
1557   // if on this Screen, raise it
1558   if (focused->getScreen()->getScreenNumber() == getScreenNumber()) {
1559     Workspace *workspace = getWorkspace(focused->getWorkspaceNumber());
1560     workspace->raiseWindow(focused);
1561   }
1562 }
1563
1564
1565 void BScreen::InitMenu(void) {
1566   if (rootmenu) {
1567     rootmenuList.clear();
1568
1569     while (rootmenu->getCount())
1570       rootmenu->remove(0);
1571   } else {
1572     rootmenu = new Rootmenu(this);
1573   }
1574   bool defaultMenu = True;
1575
1576   FILE *menu_file = (FILE *) 0;
1577   const char *menu_filename = blackbox->getMenuFilename();
1578
1579   if (menu_filename) 
1580     if (! (menu_file = fopen(menu_filename, "r")))
1581       perror(menu_filename);
1582   if (! menu_file) {     // opening the menu file failed, try the default menu
1583     menu_filename = DEFAULTMENU;
1584     if (! (menu_file = fopen(menu_filename, "r")))
1585       perror(menu_filename);
1586   } 
1587
1588   if (menu_file) {
1589     if (feof(menu_file)) {
1590       fprintf(stderr, i18n(ScreenSet, ScreenEmptyMenuFile,
1591                            "%s: Empty menu file"),
1592               menu_filename);
1593     } else {
1594       char line[1024], label[1024];
1595       memset(line, 0, 1024);
1596       memset(label, 0, 1024);
1597
1598       while (fgets(line, 1024, menu_file) && ! feof(menu_file)) {
1599         if (line[0] != '#') {
1600           int i, key = 0, index = -1, len = strlen(line);
1601
1602           for (i = 0; i < len; i++) {
1603             if (line[i] == '[') index = 0;
1604             else if (line[i] == ']') break;
1605             else if (line[i] != ' ')
1606               if (index++ >= 0)
1607                 key += tolower(line[i]);
1608           }
1609
1610           if (key == 517) { // [begin]
1611             index = -1;
1612             for (i = index; i < len; i++) {
1613               if (line[i] == '(') index = 0;
1614               else if (line[i] == ')') break;
1615               else if (index++ >= 0) {
1616                 if (line[i] == '\\' && i < len - 1) i++;
1617                 label[index - 1] = line[i];
1618               }
1619             }
1620
1621             if (index == -1) index = 0;
1622             label[index] = '\0';
1623
1624             rootmenu->setLabel(label);
1625             defaultMenu = parseMenuFile(menu_file, rootmenu);
1626             if (! defaultMenu)
1627               blackbox->addMenuTimestamp(menu_filename);
1628             break;
1629           }
1630         }
1631       }
1632     }
1633     fclose(menu_file);
1634   }
1635
1636   if (defaultMenu) {
1637     rootmenu->setInternalMenu();
1638     rootmenu->insert(i18n(ScreenSet, Screenxterm, "xterm"),
1639                      BScreen::Execute,
1640                      i18n(ScreenSet, Screenxterm, "xterm"));
1641     rootmenu->insert(i18n(ScreenSet, ScreenRestart, "Restart"),
1642                      BScreen::Restart);
1643     rootmenu->insert(i18n(ScreenSet, ScreenExit, "Exit"),
1644                      BScreen::Exit);
1645     rootmenu->setLabel(i18n(BasemenuSet, BasemenuBlackboxMenu,
1646                             "Openbox Menu"));
1647   }
1648 }
1649
1650
1651 bool BScreen::parseMenuFile(FILE *file, Rootmenu *menu) {
1652   char line[1024], label[1024], command[1024];
1653
1654   while (! feof(file)) {
1655     memset(line, 0, 1024);
1656     memset(label, 0, 1024);
1657     memset(command, 0, 1024);
1658
1659     if (fgets(line, 1024, file)) {
1660       if (line[0] != '#') {
1661         int i, key = 0, parse = 0, index = -1, line_length = strlen(line);
1662
1663         // determine the keyword
1664         for (i = 0; i < line_length; i++) {
1665           if (line[i] == '[') parse = 1;
1666           else if (line[i] == ']') break;
1667           else if (line[i] != ' ')
1668             if (parse)
1669               key += tolower(line[i]);
1670         }
1671
1672         // get the label enclosed in ()'s
1673         parse = 0;
1674
1675         for (i = 0; i < line_length; i++) {
1676           if (line[i] == '(') {
1677             index = 0;
1678             parse = 1;
1679           } else if (line[i] == ')') break;
1680           else if (index++ >= 0) {
1681             if (line[i] == '\\' && i < line_length - 1) i++;
1682             label[index - 1] = line[i];
1683           }
1684         }
1685
1686         if (parse) {
1687           label[index] = '\0';
1688         } else {
1689           label[0] = '\0';
1690         }
1691
1692         // get the command enclosed in {}'s
1693         parse = 0;
1694         index = -1;
1695         for (i = 0; i < line_length; i++) {
1696           if (line[i] == '{') {
1697             index = 0;
1698             parse = 1;
1699           } else if (line[i] == '}') break;
1700           else if (index++ >= 0) {
1701             if (line[i] == '\\' && i < line_length - 1) i++;
1702             command[index - 1] = line[i];
1703           }
1704         }
1705
1706         if (parse) {
1707           command[index] = '\0';
1708         } else {
1709           command[0] = '\0';
1710         }
1711
1712         switch (key) {
1713         case 311: // end
1714           return ((menu->getCount() == 0) ? True : False);
1715
1716           break;
1717
1718         case 333: // nop
1719           if (! *label)
1720             label[0] = '\0';
1721           menu->insert(label);
1722
1723           break;
1724
1725         case 421: // exec
1726           if ((! *label) && (! *command)) {
1727             fprintf(stderr, i18n(ScreenSet, ScreenEXECError,
1728                                  "BScreen::parseMenuFile: [exec] error, "
1729                                  "no menu label and/or command defined\n"));
1730             continue;
1731           }
1732
1733           menu->insert(label, BScreen::Execute, command);
1734
1735           break;
1736
1737         case 442: // exit
1738           if (! *label) {
1739             fprintf(stderr, i18n(ScreenSet, ScreenEXITError,
1740                                  "BScreen::parseMenuFile: [exit] error, "
1741                                  "no menu label defined\n"));
1742             continue;
1743           }
1744
1745           menu->insert(label, BScreen::Exit);
1746
1747           break;
1748
1749         case 561: // style
1750           {
1751             if ((! *label) || (! *command)) {
1752               fprintf(stderr,
1753                       i18n(ScreenSet, ScreenSTYLEError,
1754                            "BScreen::parseMenuFile: [style] error, "
1755                            "no menu label and/or filename defined\n"));
1756               continue;
1757             }
1758
1759             string style = expandTilde(command);
1760
1761             menu->insert(label, BScreen::SetStyle, style.c_str());
1762           }
1763
1764           break;
1765
1766         case 630: // config
1767           if (! *label) {
1768             fprintf(stderr, i18n(ScreenSet, ScreenCONFIGError,
1769                                  "BScreen::parseMenufile: [config] error, "
1770                                  "no label defined"));
1771             continue;
1772           }
1773
1774           menu->insert(label, configmenu);
1775
1776           break;
1777
1778         case 740: // include
1779           {
1780             if (! *label) {
1781               fprintf(stderr, i18n(ScreenSet, ScreenINCLUDEError,
1782                                    "BScreen::parseMenuFile: [include] error, "
1783                                    "no filename defined\n"));
1784               continue;
1785             }
1786
1787             string newfile = expandTilde(label);
1788             FILE *submenufile = fopen(newfile.c_str(), "r");
1789
1790             if (submenufile) {
1791               struct stat buf;
1792               if (fstat(fileno(submenufile), &buf) ||
1793                   (! S_ISREG(buf.st_mode))) {
1794                 fprintf(stderr,
1795                         i18n(ScreenSet, ScreenINCLUDEErrorReg,
1796                              "BScreen::parseMenuFile: [include] error: "
1797                              "'%s' is not a regular file\n"), newfile.c_str());
1798                 break;
1799               }
1800
1801               if (! feof(submenufile)) {
1802                 if (! parseMenuFile(submenufile, menu))
1803                   blackbox->addMenuTimestamp(newfile);
1804
1805                 fclose(submenufile);
1806               }
1807             } else {
1808               perror(newfile.c_str());
1809             }
1810           }
1811
1812           break;
1813
1814         case 767: // submenu
1815           {
1816             if (! *label) {
1817               fprintf(stderr, i18n(ScreenSet, ScreenSUBMENUError,
1818                                    "BScreen::parseMenuFile: [submenu] error, "
1819                                    "no menu label defined\n"));
1820               continue;
1821             }
1822
1823             Rootmenu *submenu = new Rootmenu(this);
1824
1825             if (*command)
1826               submenu->setLabel(command);
1827             else
1828               submenu->setLabel(label);
1829
1830             parseMenuFile(file, submenu);
1831             submenu->update();
1832             menu->insert(label, submenu);
1833             rootmenuList.push_back(submenu);
1834           }
1835
1836           break;
1837
1838         case 773: // restart
1839           {
1840             if (! *label) {
1841               fprintf(stderr, i18n(ScreenSet, ScreenRESTARTError,
1842                                    "BScreen::parseMenuFile: [restart] error, "
1843                                    "no menu label defined\n"));
1844               continue;
1845             }
1846
1847             if (*command)
1848               menu->insert(label, BScreen::RestartOther, command);
1849             else
1850               menu->insert(label, BScreen::Restart);
1851           }
1852
1853           break;
1854
1855         case 845: // reconfig
1856           {
1857             if (! *label) {
1858               fprintf(stderr,
1859                       i18n(ScreenSet, ScreenRECONFIGError,
1860                            "BScreen::parseMenuFile: [reconfig] error, "
1861                            "no menu label defined\n"));
1862               continue;
1863             }
1864
1865             menu->insert(label, BScreen::Reconfigure);
1866           }
1867
1868           break;
1869
1870         case 995: // stylesdir
1871         case 1113: // stylesmenu
1872           {
1873             bool newmenu = ((key == 1113) ? True : False);
1874
1875             if ((! *label) || ((! *command) && newmenu)) {
1876               fprintf(stderr,
1877                       i18n(ScreenSet, ScreenSTYLESDIRError,
1878                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1879                            " error, no directory defined\n"));
1880               continue;
1881             }
1882
1883             char *directory = ((newmenu) ? command : label);
1884
1885             string stylesdir = expandTilde(directory);
1886
1887             struct stat statbuf;
1888
1889             if (! stat(stylesdir.c_str(), &statbuf)) {
1890               if (S_ISDIR(statbuf.st_mode)) {
1891                 Rootmenu *stylesmenu;
1892
1893                 if (newmenu)
1894                   stylesmenu = new Rootmenu(this);
1895                 else
1896                   stylesmenu = menu;
1897
1898                 DIR *d = opendir(stylesdir.c_str());
1899                 struct dirent *p;
1900                 std::vector<string> ls;
1901
1902                 while((p = readdir(d)))
1903                   ls.push_back(p->d_name);
1904
1905                 closedir(d);
1906
1907                 std::sort(ls.begin(), ls.end());
1908
1909                 std::vector<string>::iterator it = ls.begin(),
1910                   end = ls.end();
1911                 for (; it != end; ++it) {
1912                   const string& fname = *it;
1913
1914                   if (fname[fname.size()-1] == '~')
1915                     continue;
1916
1917                   string style = stylesdir;
1918                   style += '/';
1919                   style += fname;
1920
1921                   if ((! stat(style.c_str(), &statbuf)) &&
1922                       S_ISREG(statbuf.st_mode))
1923                     stylesmenu->insert(fname, BScreen::SetStyle, style);
1924                 }
1925
1926                 stylesmenu->update();
1927
1928                 if (newmenu) {
1929                   stylesmenu->setLabel(label);
1930                   menu->insert(label, stylesmenu);
1931                   rootmenuList.push_back(stylesmenu);
1932                 }
1933
1934                 blackbox->addMenuTimestamp(stylesdir);
1935               } else {
1936                 fprintf(stderr,
1937                         i18n(ScreenSet, ScreenSTYLESDIRErrorNotDir,
1938                              "BScreen::parseMenuFile:"
1939                              " [stylesdir/stylesmenu] error, %s is not a"
1940                              " directory\n"), stylesdir.c_str());
1941               }
1942             } else {
1943               fprintf(stderr,
1944                       i18n(ScreenSet, ScreenSTYLESDIRErrorNoExist,
1945                            "BScreen::parseMenuFile: [stylesdir/stylesmenu]"
1946                            " error, %s does not exist\n"), stylesdir.c_str());
1947             }
1948             break;
1949           }
1950
1951         case 1090: // workspaces
1952           {
1953             if (! *label) {
1954               fprintf(stderr,
1955                       i18n(ScreenSet, ScreenWORKSPACESError,
1956                            "BScreen:parseMenuFile: [workspaces] error, "
1957                            "no menu label defined\n"));
1958               continue;
1959             }
1960
1961             menu->insert(label, workspacemenu);
1962
1963             break;
1964           }
1965         }
1966       }
1967     }
1968   }
1969
1970   return ((menu->getCount() == 0) ? True : False);
1971 }
1972
1973
1974 void BScreen::shutdown(void) {
1975   XSelectInput(blackbox->getXDisplay(), getRootWindow(), NoEventMask);
1976   XSync(blackbox->getXDisplay(), False);
1977
1978   while(! windowList.empty())
1979     unmanageWindow(windowList.front(), True);
1980
1981   slit->shutdown();
1982 }
1983
1984
1985 void BScreen::showPosition(int x, int y) {
1986   if (! geom_visible) {
1987     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
1988                       (getWidth() - geom_w) / 2,
1989                       (getHeight() - geom_h) / 2, geom_w, geom_h);
1990     XMapWindow(blackbox->getXDisplay(), geom_window);
1991     XRaiseWindow(blackbox->getXDisplay(), geom_window);
1992
1993     geom_visible = True;
1994   }
1995
1996   char label[1024];
1997
1998   sprintf(label, i18n(ScreenSet, ScreenPositionFormat,
1999                       "X: %4d x Y: %4d"), x, y);
2000
2001   XClearWindow(blackbox->getXDisplay(), geom_window);
2002
2003   resource.wstyle.font->drawString(geom_window,
2004                                    resource.bevel_width, resource.bevel_width,
2005                                    resource.wstyle.l_text_focus,
2006                                    label);
2007 }
2008
2009
2010 void BScreen::showGeometry(unsigned int gx, unsigned int gy) {
2011   if (! geom_visible) {
2012     XMoveResizeWindow(blackbox->getXDisplay(), geom_window,
2013                       (getWidth() - geom_w) / 2,
2014                       (getHeight() - geom_h) / 2, geom_w, geom_h);
2015     XMapWindow(blackbox->getXDisplay(), geom_window);
2016     XRaiseWindow(blackbox->getXDisplay(), geom_window);
2017
2018     geom_visible = True;
2019   }
2020
2021   char label[1024];
2022
2023   sprintf(label, i18n(ScreenSet, ScreenGeometryFormat,
2024                       "W: %4d x H: %4d"), gx, gy);
2025
2026   XClearWindow(blackbox->getXDisplay(), geom_window);
2027
2028   resource.wstyle.font->drawString(geom_window,
2029                                    resource.bevel_width, resource.bevel_width,
2030                                    resource.wstyle.l_text_focus,
2031                                    label);
2032 }
2033
2034
2035 void BScreen::hideGeometry(void) {
2036   if (geom_visible) {
2037     XUnmapWindow(blackbox->getXDisplay(), geom_window);
2038     geom_visible = False;
2039   }
2040 }
2041
2042
2043 void BScreen::addStrut(Strut *strut) {
2044   strutList.push_back(strut);
2045 }
2046
2047
2048 void BScreen::removeStrut(Strut *strut) {
2049   strutList.remove(strut);
2050 }
2051
2052
2053 const Rect& BScreen::availableArea(void) const {
2054   if (doFullMax())
2055     return getRect(); // return the full screen
2056   return usableArea;
2057 }
2058
2059
2060 void BScreen::updateAvailableArea(void) {
2061   Rect old_area = usableArea;
2062   usableArea = getRect(); // reset to full screen
2063
2064   /* these values represent offsets from the screen edge
2065    * we look for the biggest offset on each edge and then apply them
2066    * all at once
2067    * do not be confused by the similarity to the names of Rect's members
2068    */
2069   unsigned int current_left = 0, current_right = 0, current_top = 0,
2070     current_bottom = 0;
2071
2072   StrutList::const_iterator it = strutList.begin(), end = strutList.end();
2073
2074   for(; it != end; ++it) {
2075     Strut *strut = *it;
2076     if (strut->left > current_left)
2077       current_left = strut->left;
2078     if (strut->top > current_top)
2079       current_top = strut->top;
2080     if (strut->right > current_right)
2081       current_right = strut->right;
2082     if (strut->bottom > current_bottom)
2083       current_bottom = strut->bottom;
2084   }
2085
2086   usableArea.setPos(current_left, current_top);
2087   usableArea.setSize(usableArea.width() - (current_left + current_right),
2088                      usableArea.height() - (current_top + current_bottom));
2089
2090   if (old_area != usableArea) {
2091     BlackboxWindowList::iterator it = windowList.begin(),
2092       end = windowList.end();
2093     for (; it != end; ++it)
2094       if ((*it)->isMaximized()) (*it)->remaximize();
2095   }
2096
2097   updateWorkArea();  
2098 }
2099
2100
2101 Workspace* BScreen::getWorkspace(unsigned int index) {
2102   assert(index < workspacesList.size());
2103   return workspacesList[index];
2104 }
2105
2106
2107 void BScreen::buttonPressEvent(const XButtonEvent *xbutton) {
2108   if (xbutton->button == 1) {
2109     if (! isRootColormapInstalled())
2110       image_control->installRootColormap();
2111
2112     if (workspacemenu->isVisible())
2113       workspacemenu->hide();
2114
2115     if (rootmenu->isVisible())
2116       rootmenu->hide();
2117   } else if (xbutton->button == 2) {
2118     int mx = xbutton->x_root - (workspacemenu->getWidth() / 2);
2119     int my = xbutton->y_root - (workspacemenu->getTitleHeight() / 2);
2120
2121     if (mx < 0) mx = 0;
2122     if (my < 0) my = 0;
2123
2124     if (mx + workspacemenu->getWidth() > getWidth())
2125       mx = getWidth() - workspacemenu->getWidth() - getBorderWidth();
2126
2127     if (my + workspacemenu->getHeight() > getHeight())
2128       my = getHeight() - workspacemenu->getHeight() - getBorderWidth();
2129
2130     workspacemenu->move(mx, my);
2131
2132     if (! workspacemenu->isVisible()) {
2133       workspacemenu->removeParent();
2134       workspacemenu->show();
2135     }
2136   } else if (xbutton->button == 3) {
2137     int mx = xbutton->x_root - (rootmenu->getWidth() / 2);
2138     int my = xbutton->y_root - (rootmenu->getTitleHeight() / 2);
2139
2140     if (mx < 0) mx = 0;
2141     if (my < 0) my = 0;
2142
2143     if (mx + rootmenu->getWidth() > getWidth())
2144       mx = getWidth() - rootmenu->getWidth() - getBorderWidth();
2145
2146     if (my + rootmenu->getHeight() > getHeight())
2147       my = getHeight() - rootmenu->getHeight() - getBorderWidth();
2148
2149     rootmenu->move(mx, my);
2150
2151     if (! rootmenu->isVisible()) {
2152       blackbox->checkMenu();
2153       rootmenu->show();
2154     }
2155   // mouse wheel up
2156   } else if (xbutton->button == 4) {
2157     if (getCurrentWorkspaceID() >= getWorkspaceCount() - 1)
2158       changeWorkspaceID(0);
2159     else
2160       changeWorkspaceID(getCurrentWorkspaceID() + 1);
2161   // mouse wheel down
2162   } else if (xbutton->button == 5) {
2163     if (getCurrentWorkspaceID() == 0)
2164       changeWorkspaceID(getWorkspaceCount() - 1);
2165     else
2166       changeWorkspaceID(getCurrentWorkspaceID() - 1);
2167   }
2168 }
2169
2170
2171 void BScreen::propertyNotifyEvent(const XPropertyEvent *pe) {
2172   if (pe->atom == xatom->getAtom(XAtom::net_desktop_names)) {
2173     // _NET_WM_DESKTOP_NAMES
2174     WorkspaceList::iterator it = workspacesList.begin();
2175     const WorkspaceList::iterator end = workspacesList.end();
2176     for (; it != end; ++it) {
2177       (*it)->readName(); // re-read its name from the window property
2178       workspacemenu->changeWorkspaceLabel((*it)->getID(), (*it)->getName());
2179     }
2180     workspacemenu->update();
2181     toolbar->reconfigure();
2182     saveWorkspaceNames();
2183   }
2184 }
2185
2186
2187 void BScreen::toggleFocusModel(FocusModel model) {
2188   std::for_each(windowList.begin(), windowList.end(),
2189                 std::mem_fun(&BlackboxWindow::ungrabButtons));
2190
2191   if (model == SloppyFocus) {
2192     saveSloppyFocus(True);
2193   } else {
2194     // we're cheating here to save writing the config file 3 times
2195     resource.auto_raise = False;
2196     resource.click_raise = False;
2197     saveSloppyFocus(False);
2198   }
2199
2200   std::for_each(windowList.begin(), windowList.end(),
2201                 std::mem_fun(&BlackboxWindow::grabButtons));
2202 }
2203
2204
2205 BTexture BScreen::readDatabaseTexture(const string &rname,
2206                                       const string &default_color,
2207                                       const Configuration &style) {
2208   BTexture texture;
2209   string s;
2210
2211   if (style.getValue(rname, s))
2212     texture = BTexture(s);
2213   else
2214     texture.setTexture(BTexture::Solid | BTexture::Flat);
2215
2216   // associate this texture with this screen
2217   texture.setDisplay(getBaseDisplay(), getScreenNumber());
2218   texture.setImageControl(image_control);
2219
2220   if (texture.texture() & BTexture::Solid) {
2221     texture.setColor(readDatabaseColor(rname + ".color",
2222                                        default_color, style));
2223     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
2224                                          default_color, style));
2225   } else if (texture.texture() & BTexture::Gradient) {
2226     texture.setColor(readDatabaseColor(rname + ".color",
2227                                        default_color, style));
2228     texture.setColorTo(readDatabaseColor(rname + ".colorTo",
2229                                          default_color, style));
2230   }
2231
2232   return texture;
2233 }
2234
2235
2236 BColor BScreen::readDatabaseColor(const string &rname,
2237                                   const string &default_color,
2238                                   const Configuration &style) {
2239   BColor color;
2240   string s;
2241   if (style.getValue(rname, s))
2242     color = BColor(s, getBaseDisplay(), getScreenNumber());
2243   else
2244     color = BColor(default_color, getBaseDisplay(), getScreenNumber());
2245   return color;
2246 }
2247
2248
2249 BFont *BScreen::readDatabaseFont(const string &rbasename,
2250                                  const Configuration &style) {
2251   string fontname;
2252
2253   string s;
2254
2255 #ifdef    XFT
2256   int i;
2257   if (style.getValue(rbasename + "xft.font", s) &&
2258       style.getValue(rbasename + "xft.size", i)) {
2259     string family = s;
2260     bool bold = False;
2261     bool italic = False;
2262     if (style.getValue(rbasename + "xft.flags", s)) {
2263       if (s.find("bold") != string::npos)
2264         bold = True;
2265       if (s.find("italic") != string::npos)
2266         italic = True;
2267     }
2268     
2269     BFont *b = new BFont(blackbox->getXDisplay(), this, family, i, bold,
2270                          italic, resource.aa_fonts);
2271     if (b->valid())
2272       return b;
2273     else
2274       delete b; // fall back to the normal X font stuff
2275   }
2276 #endif // XFT
2277
2278   style.getValue(rbasename + "font", s);
2279   // if this fails, a blank string will be used, which will cause the fallback
2280   // font to load.
2281
2282   BFont *b = new BFont(blackbox->getXDisplay(), this, s);
2283   if (! b->valid())
2284     exit(2);  // can't continue without a font
2285   return b;
2286 }