]> icculus.org git repositories - mikachu/openbox.git/blob - src/Toolbar.cc
merge the sticky window fix from 1.2.
[mikachu/openbox.git] / src / Toolbar.cc
1 // Toolbar.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 "i18n.h"
34 #include "openbox.h"
35 #include "Clientmenu.h"
36 #include "Iconmenu.h"
37 #include "Rootmenu.h"
38 #include "Screen.h"
39 #include "Toolbar.h"
40 #include "Window.h"
41 #include "Workspace.h"
42 #include "Workspacemenu.h"
43
44 #include <X11/keysym.h>
45
46 #ifdef    HAVE_STRING_H
47 #  include <string.h>
48 #endif // HAVE_STRING_H
49
50 #ifdef    HAVE_STDIO_H
51 #  include <stdio.h>
52 #endif // HAVE_STDIO_H
53
54 #ifdef    TIME_WITH_SYS_TIME
55 # include <sys/time.h>
56 # include <time.h>
57 #else // !TIME_WITH_SYS_TIME
58 # ifdef    HAVE_SYS_TIME_H
59 #  include <sys/time.h>
60 # else // !HAVE_SYS_TIME_H
61 #  include <time.h>
62 # endif // HAVE_SYS_TIME_H
63 #endif // TIME_WITH_SYS_TIME
64
65 #include <strstream>
66 #include <string>
67 using std::ends;
68
69 Toolbar::Toolbar(BScreen &scrn, Resource &conf) : openbox(scrn.getOpenbox()),
70 screen(scrn), config(conf)
71 {
72   load();
73
74   // get the clock updating every minute
75   clock_timer = new BTimer(openbox, *this);
76   timeval now;
77   gettimeofday(&now, 0);
78   clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
79   clock_timer->start();
80
81   hide_handler.toolbar = this;
82   hide_timer = new BTimer(openbox, hide_handler);
83   // the time out is set in ::reconfigure()
84   hide_timer->fireOnce(True);
85
86   image_ctrl = screen.getImageControl();
87
88   m_editing = False;
89   new_workspace_name = (char *) 0;
90   new_name_pos = 0;
91   frame.grab_x = frame.grab_y = 0;
92
93   toolbarmenu = new Toolbarmenu(*this);
94
95   display = openbox.getXDisplay();
96   XSetWindowAttributes attrib;
97   unsigned long create_mask = CWBackPixmap | CWBackPixel | CWBorderPixel |
98     CWColormap | CWOverrideRedirect | CWEventMask;
99   attrib.background_pixmap = None;
100   attrib.background_pixel = attrib.border_pixel =
101     screen.getBorderColor()->getPixel();
102   attrib.colormap = screen.getColormap();
103   attrib.override_redirect = True;
104   attrib.event_mask = ButtonPressMask | ButtonReleaseMask |
105     EnterWindowMask | LeaveWindowMask;
106
107   frame.window =
108     XCreateWindow(display, screen.getRootWindow(), 0, 0, 1, 1, 0,
109                   screen.getDepth(), InputOutput, screen.getVisual(),
110                   create_mask, &attrib);
111   openbox.saveToolbarSearch(frame.window, this);
112
113   attrib.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask |
114     KeyPressMask | EnterWindowMask;
115
116   frame.workspace_label =
117     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
118                   InputOutput, screen.getVisual(), create_mask, &attrib);
119   openbox.saveToolbarSearch(frame.workspace_label, this);
120
121   frame.window_label =
122     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
123                   InputOutput, screen.getVisual(), create_mask, &attrib);
124   openbox.saveToolbarSearch(frame.window_label, this);
125
126   frame.clock =
127     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
128                   InputOutput, screen.getVisual(), create_mask, &attrib);
129   openbox.saveToolbarSearch(frame.clock, this);
130
131   frame.psbutton =
132     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
133                   InputOutput, screen.getVisual(), create_mask, &attrib);
134   openbox.saveToolbarSearch(frame.psbutton, this);
135
136   frame.nsbutton =
137     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
138                   InputOutput, screen.getVisual(), create_mask, &attrib);
139   openbox.saveToolbarSearch(frame.nsbutton, this);
140
141   frame.pwbutton =
142     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
143                   InputOutput, screen.getVisual(), create_mask, &attrib);
144   openbox.saveToolbarSearch(frame.pwbutton, this);
145
146   frame.nwbutton =
147     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
148                   InputOutput, screen.getVisual(), create_mask, &attrib);
149   openbox.saveToolbarSearch(frame.nwbutton, this);
150
151   frame.base = frame.label = frame.wlabel = frame.clk = frame.button =
152     frame.pbutton = None;
153
154   reconfigure();
155   mapToolbar();
156 }
157
158 Rect Toolbar::area() const {
159   int x = ((m_hidden) ? frame.x_hidden : frame.x);
160   int y;
161   if (screen.hideToolbar()) y = screen.size().h();
162   else if (m_hidden) y = frame.y_hidden;
163   else y = frame.y;
164   return Rect(x, y, frame.width, frame.height);
165 }
166
167 unsigned int Toolbar::getExposedHeight() const {
168   if (screen.hideToolbar()) return 0;
169   else if (m_autohide) return frame.bevel_w;
170   else return frame.height;
171 }
172
173 void Toolbar::mapToolbar(){
174   if (!screen.hideToolbar()) {
175     //not hidden, so windows should not maximize over the toolbar
176     XMapSubwindows(display, frame.window);
177     XMapWindow(display, frame.window);
178   }
179 }
180
181 void Toolbar::unMapToolbar(){
182   //hidden so we can maximize over the toolbar
183   XUnmapWindow(display, frame.window);
184 }
185
186 Toolbar::~Toolbar() {
187   unMapToolbar();
188   if (frame.base) image_ctrl->removeImage(frame.base);
189   if (frame.label) image_ctrl->removeImage(frame.label);
190   if (frame.wlabel) image_ctrl->removeImage(frame.wlabel);
191   if (frame.clk) image_ctrl->removeImage(frame.clk);
192   if (frame.button) image_ctrl->removeImage(frame.button);
193   if (frame.pbutton) image_ctrl->removeImage(frame.pbutton);
194
195   openbox.removeToolbarSearch(frame.window);
196   openbox.removeToolbarSearch(frame.workspace_label);
197   openbox.removeToolbarSearch(frame.window_label);
198   openbox.removeToolbarSearch(frame.clock);
199   openbox.removeToolbarSearch(frame.psbutton);
200   openbox.removeToolbarSearch(frame.nsbutton);
201   openbox.removeToolbarSearch(frame.pwbutton);
202   openbox.removeToolbarSearch(frame.nwbutton);
203
204   XDestroyWindow(display, frame.workspace_label);
205   XDestroyWindow(display, frame.window_label);
206   XDestroyWindow(display, frame.clock);
207
208   XDestroyWindow(display, frame.window);
209
210   delete hide_timer;
211   delete clock_timer;
212   delete toolbarmenu;
213 }
214
215
216 void Toolbar::setOnTop(bool b) {
217   m_ontop = b;
218   std::ostrstream s;
219   s << "session.screen" << screen.getScreenNumber() << ".toolbar.onTop" << ends;
220   config.setValue(s.str(), m_ontop ? "True" : "False");
221   s.rdbuf()->freeze(0);
222 }
223
224 void Toolbar::setAutoHide(bool b) {
225   m_autohide = b;
226   std::ostrstream s;
227   s << "session.screen" << screen.getScreenNumber() << ".toolbar.autoHide"
228     << ends;
229   config.setValue(s.str(), m_autohide ? "True" : "False");
230   s.rdbuf()->freeze(0);
231 }
232
233 void Toolbar::setWidthPercent(int w) {
234   m_width_percent = w;
235   std::ostrstream s;
236   s << "session.screen" << screen.getScreenNumber() << ".toolbar.widthPercent"
237     << ends;
238   config.setValue(s.str(), m_width_percent);
239   s.rdbuf()->freeze(0);
240 }
241
242 void Toolbar::setPlacement(int p) {
243   m_placement = p;
244   std::ostrstream s;
245   s << "session.screen" << screen.getScreenNumber() << ".toolbar.placement"
246     << ends;
247   const char *placement;
248   switch (m_placement) {
249   case TopLeft: placement = "TopLeft"; break;
250   case BottomLeft: placement = "BottomLeft"; break;
251   case TopCenter: placement = "TopCenter"; break;
252   case TopRight: placement = "TopRight"; break;
253   case BottomRight: placement = "BottomRight"; break;
254   case BottomCenter: default: placement = "BottomCenter"; break;
255   }
256   config.setValue(s.str(), placement);
257   s.rdbuf()->freeze(0);
258 }
259
260 void Toolbar::save() {
261   setOnTop(m_ontop);
262   setAutoHide(m_autohide);
263   setWidthPercent(m_width_percent);
264   setPlacement(m_placement);
265 }
266
267 void Toolbar::load() {
268   std::ostrstream rscreen, rname, rclass;
269   std::string s;
270   bool b;
271   long l;
272   rscreen << "session.screen" << screen.getScreenNumber() << '.' << ends;
273
274   rname << rscreen.str() << "toolbar.widthPercent" << ends;
275   rclass << rscreen.str() << "Toolbar.WidthPercent" << ends;
276   if (config.getValue(rname.str(), rclass.str(), l) && (l > 0 && l <= 100))
277     m_width_percent = l;
278   else
279     m_width_percent =66;
280
281   rname.seekp(0); rclass.seekp(0);
282   rname.rdbuf()->freeze(0); rclass.rdbuf()->freeze(0);
283   rname << rscreen.str() << "toolbar.placement" << ends;
284   rclass << rscreen.str() << "Toolbar.Placement" << ends;
285   if (config.getValue(rname.str(), rclass.str(), s)) {
286     if (0 == strncasecmp(s.c_str(), "TopLeft", s.length()))
287       m_placement = TopLeft;
288     else if (0 == strncasecmp(s.c_str(), "BottomLeft", s.length()))
289       m_placement = BottomLeft;
290     else if (0 == strncasecmp(s.c_str(), "TopCenter", s.length()))
291       m_placement = TopCenter;
292     else if (0 == strncasecmp(s.c_str(), "TopRight", s.length()))
293       m_placement = TopRight;
294     else if ( 0 == strncasecmp(s.c_str(), "BottomRight", s.length()))
295       m_placement = BottomRight;
296     else if ( 0 == strncasecmp(s.c_str(), "BottomCenter", s.length()))
297       m_placement = BottomCenter;
298   } else
299     m_placement = BottomCenter;
300
301   rname.seekp(0); rclass.seekp(0);
302   rname.rdbuf()->freeze(0); rclass.rdbuf()->freeze(0);
303   rname << rscreen.str() << "toolbar.onTop" << ends;
304   rclass << rscreen.str() << "Toolbar.OnTop" << ends;
305   if (config.getValue(rname.str(), rclass.str(), b))
306     m_ontop = b;
307   else
308     m_ontop = false;
309
310   rname.seekp(0); rclass.seekp(0);
311   rname.rdbuf()->freeze(0); rclass.rdbuf()->freeze(0);
312   rname << rscreen.str() << "toolbar.autoHide" << ends;
313   rclass << rscreen.str() << "Toolbar.AutoHide" << ends;
314   if (config.getValue(rname.str(), rclass.str(), b))
315     m_hidden = m_autohide = b;
316   else
317     m_hidden = m_autohide = false;
318
319   rscreen.rdbuf()->freeze(0);
320   rname.rdbuf()->freeze(0); rclass.rdbuf()->freeze(0);
321 }
322
323 void Toolbar::reconfigure() {
324   hide_timer->setTimeout(openbox.getAutoRaiseDelay());
325
326   frame.bevel_w = screen.getBevelWidth();
327   frame.width = screen.size().w() * m_width_percent / 100;
328
329   if (i18n.multibyte())
330     frame.height =
331       screen.getToolbarStyle()->fontset_extents->max_ink_extent.height;
332   else
333     frame.height = screen.getToolbarStyle()->font->ascent +
334       screen.getToolbarStyle()->font->descent;
335   frame.button_w = frame.height;
336   frame.height += 2;
337   frame.label_h = frame.height;
338   frame.height += (frame.bevel_w * 2);
339
340   switch (m_placement) {
341   case TopLeft:
342     frame.x = 0;
343     frame.y = 0;
344     frame.x_hidden = 0;
345     frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
346       - frame.height;
347     break;
348
349   case BottomLeft:
350     frame.x = 0;
351     frame.y = screen.size().h() - frame.height
352       - (screen.getBorderWidth() * 2);
353     frame.x_hidden = 0;
354     frame.y_hidden = screen.size().h() - screen.getBevelWidth()
355       - screen.getBorderWidth();
356     break;
357
358   case TopCenter:
359     frame.x = (screen.size().w() - frame.width) / 2;
360     frame.y = 0;
361     frame.x_hidden = frame.x;
362     frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
363       - frame.height;
364     break;
365
366   case BottomCenter:
367   default:
368     frame.x = (screen.size().w() - frame.width) / 2;
369     frame.y = screen.size().h() - frame.height
370       - (screen.getBorderWidth() * 2);
371     frame.x_hidden = frame.x;
372     frame.y_hidden = screen.size().h() - screen.getBevelWidth()
373       - screen.getBorderWidth();
374     break;
375
376   case TopRight:
377     frame.x = screen.size().w() - frame.width
378       - (screen.getBorderWidth() * 2);
379     frame.y = 0;
380     frame.x_hidden = frame.x;
381     frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
382       - frame.height;
383     break;
384
385   case BottomRight:
386     frame.x = screen.size().w() - frame.width
387       - (screen.getBorderWidth() * 2);
388     frame.y = screen.size().h() - frame.height
389       - (screen.getBorderWidth() * 2);
390     frame.x_hidden = frame.x;
391     frame.y_hidden = screen.size().h() - screen.getBevelWidth()
392       - screen.getBorderWidth();
393     break;
394   }
395
396 #ifdef    HAVE_STRFTIME
397   time_t ttmp = time(NULL);
398   struct tm *tt = 0;
399
400   if (ttmp != -1) {
401     tt = localtime(&ttmp);
402     if (tt) {
403       char t[1025], *time_string = (char *) 0;
404       int len = strftime(t, 1024, screen.strftimeFormat(), tt);
405       t[len++] = 'A';   // add size to the string for padding
406       t[len++] = 'A';   // add size to the string for padding
407       t[len] = '\0';
408
409       if (i18n.multibyte()) {
410         XRectangle ink, logical;
411         XmbTextExtents(screen.getToolbarStyle()->fontset, t, len, &ink,
412                        &logical);
413         frame.clock_w = logical.width;
414
415         // ben's additional solution to pad some space beside the numbers
416         //frame.clock_w +=
417         //  screen.getToolbarStyle()->fontset_extents->max_logical_extent.width *
418         //  4;
419
420         // brad's solution, which is currently buggy, too big
421         //frame.clock_w =
422         //  screen.getToolbarStyle()->fontset_extents->max_logical_extent.width
423         //  * len;
424       } else {
425         frame.clock_w = XTextWidth(screen.getToolbarStyle()->font, t, len);
426         // ben's additional solution to pad some space beside the numbers
427         //frame.clock_w += screen.getToolbarStyle()->font->max_bounds.width * 4;
428         // brad's solution again, too big
429         //frame.clock_w = screen.getToolbarStyle()->font->max_bounds.width * len;
430       }
431       frame.clock_w += (frame.bevel_w * 4);
432
433       delete [] time_string;
434     } else {
435       frame.clock_w = 0;
436     }
437   } else {
438     frame.clock_w = 0;
439   }
440 #else // !HAVE_STRFTIME
441   frame.clock_w =
442     XTextWidth(screen.getToolbarStyle()->font,
443                i18n(ToolbarSet, ToolbarNoStrftimeLength,
444                     "00:00000"),
445                strlen(i18n(ToolbarSet, ToolbarNoStrftimeLength,
446                            "00:00000"))) + (frame.bevel_w * 4);
447 #endif // HAVE_STRFTIME
448
449                            int i;
450                            unsigned int w = 0;
451                            frame.workspace_label_w = 0;
452
453                            for (i = 0; i < screen.getWorkspaceCount(); i++) {
454                              if (i18n.multibyte()) {
455                                XRectangle ink, logical;
456                                XmbTextExtents(screen.getToolbarStyle()->fontset,
457                                               screen.getWorkspace(i)->getName(),
458                                               strlen(screen.getWorkspace(i)->getName()),
459                                               &ink, &logical);
460                                w = logical.width;
461                              } else {
462                                w = XTextWidth(screen.getToolbarStyle()->font,
463                                               screen.getWorkspace(i)->getName(),
464                                               strlen(screen.getWorkspace(i)->getName()));
465                              }
466                              w += (frame.bevel_w * 4);
467
468                              if (w > frame.workspace_label_w) frame.workspace_label_w = w;
469                            }
470
471                            if (frame.workspace_label_w < frame.clock_w)
472                              frame.workspace_label_w = frame.clock_w;
473                            else if (frame.workspace_label_w > frame.clock_w)
474                              frame.clock_w = frame.workspace_label_w;
475
476                            frame.window_label_w =
477                              (frame.width - (frame.clock_w + (frame.button_w * 4) +
478                                              frame.workspace_label_w + (frame.bevel_w * 8) + 6));
479
480                            if (m_hidden) {
481                              XMoveResizeWindow(display, frame.window, frame.x_hidden, frame.y_hidden,
482                                                frame.width, frame.height);
483                            } else {
484                              XMoveResizeWindow(display, frame.window, frame.x, frame.y,
485                                                frame.width, frame.height);
486                            }
487
488                            XMoveResizeWindow(display, frame.workspace_label, frame.bevel_w,
489                                              frame.bevel_w, frame.workspace_label_w,
490                                              frame.label_h);
491                            XMoveResizeWindow(display, frame.psbutton, (frame.bevel_w * 2) +
492                                              frame.workspace_label_w + 1, frame.bevel_w + 1,
493                                              frame.button_w, frame.button_w);
494                            XMoveResizeWindow(display ,frame.nsbutton, (frame.bevel_w * 3) +
495                                              frame.workspace_label_w + frame.button_w + 2,
496                                              frame.bevel_w + 1, frame.button_w, frame.button_w);
497                            XMoveResizeWindow(display, frame.window_label, (frame.bevel_w * 4) +
498                                              (frame.button_w * 2) + frame.workspace_label_w + 3,
499                                              frame.bevel_w, frame.window_label_w, frame.label_h);
500                            XMoveResizeWindow(display, frame.pwbutton, (frame.bevel_w * 5) +
501                                              (frame.button_w * 2) + frame.workspace_label_w +
502                                              frame.window_label_w + 4, frame.bevel_w + 1,
503                                              frame.button_w, frame.button_w);
504                            XMoveResizeWindow(display, frame.nwbutton, (frame.bevel_w * 6) +
505                                              (frame.button_w * 3) + frame.workspace_label_w +
506                                              frame.window_label_w + 5, frame.bevel_w + 1,
507                                              frame.button_w, frame.button_w);
508                            XMoveResizeWindow(display, frame.clock, frame.width - frame.clock_w -
509                                              frame.bevel_w, frame.bevel_w, frame.clock_w,
510                                              frame.label_h);
511
512                            Pixmap tmp = frame.base;
513                            BTexture *texture = &(screen.getToolbarStyle()->toolbar);
514                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
515                              frame.base = None;
516                              XSetWindowBackground(display, frame.window,
517                                                   texture->getColor()->getPixel());
518                            } else {
519                              frame.base =
520                                image_ctrl->renderImage(frame.width, frame.height, texture);
521                              XSetWindowBackgroundPixmap(display, frame.window, frame.base);
522                            }
523                            if (tmp) image_ctrl->removeImage(tmp);
524
525                            tmp = frame.label;
526                            texture = &(screen.getToolbarStyle()->window);
527                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
528                              frame.label = None;
529                              XSetWindowBackground(display, frame.window_label,
530                                                   texture->getColor()->getPixel());
531                            } else {
532                              frame.label =
533                                image_ctrl->renderImage(frame.window_label_w, frame.label_h, texture);
534                              XSetWindowBackgroundPixmap(display, frame.window_label, frame.label);
535                            }
536                            if (tmp) image_ctrl->removeImage(tmp);
537
538                            tmp = frame.wlabel;
539                            texture = &(screen.getToolbarStyle()->label);
540                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
541                              frame.wlabel = None;
542                              XSetWindowBackground(display, frame.workspace_label,
543                                                   texture->getColor()->getPixel());
544                            } else {
545                              frame.wlabel =
546                                image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
547                              XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
548                            }
549                            if (tmp) image_ctrl->removeImage(tmp);
550
551                            tmp = frame.clk;
552                            texture = &(screen.getToolbarStyle()->clock);
553                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
554                              frame.clk = None;
555                              XSetWindowBackground(display, frame.clock,
556                                                   texture->getColor()->getPixel());
557                            } else {
558                              frame.clk =
559                                image_ctrl->renderImage(frame.clock_w, frame.label_h, texture);
560                              XSetWindowBackgroundPixmap(display, frame.clock, frame.clk);
561                            }
562                            if (tmp) image_ctrl->removeImage(tmp);
563
564                            tmp = frame.button;
565                            texture = &(screen.getToolbarStyle()->button);
566                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
567                              frame.button = None;
568
569                              frame.button_pixel = texture->getColor()->getPixel();
570                              XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
571                              XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
572                              XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
573                              XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
574                            } else {
575                              frame.button =
576                                image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
577
578                              XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
579                              XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
580                              XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
581                              XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
582                            }
583                            if (tmp) image_ctrl->removeImage(tmp);
584
585                            tmp = frame.pbutton;
586                            texture = &(screen.getToolbarStyle()->pressed);
587                            if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
588                              frame.pbutton = None;
589                              frame.pbutton_pixel = texture->getColor()->getPixel();
590                            } else {
591                              frame.pbutton =
592                                image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
593                            }
594                            if (tmp) image_ctrl->removeImage(tmp);
595
596                            XSetWindowBorder(display, frame.window,
597                                             screen.getBorderColor()->getPixel());
598                            XSetWindowBorderWidth(display, frame.window, screen.getBorderWidth());
599
600                            XClearWindow(display, frame.window);
601                            XClearWindow(display, frame.workspace_label);
602                            XClearWindow(display, frame.window_label);
603                            XClearWindow(display, frame.clock);
604                            XClearWindow(display, frame.psbutton);
605                            XClearWindow(display, frame.nsbutton);
606                            XClearWindow(display, frame.pwbutton);
607                            XClearWindow(display, frame.nwbutton);
608
609                            redrawWindowLabel();
610                            redrawWorkspaceLabel();
611                            redrawPrevWorkspaceButton();
612                            redrawNextWorkspaceButton();
613                            redrawPrevWindowButton();
614                            redrawNextWindowButton();
615                            checkClock(True);
616
617                            toolbarmenu->reconfigure();
618 }
619
620
621 #ifdef    HAVE_STRFTIME
622 void Toolbar::checkClock(Bool redraw) {
623 #else // !HAVE_STRFTIME
624   void Toolbar::checkClock(Bool redraw, Bool date) {
625 #endif // HAVE_STRFTIME
626     time_t tmp = 0;
627     struct tm *tt = 0;
628
629     if ((tmp = time(NULL)) != -1) {
630       if (! (tt = localtime(&tmp))) return;
631       if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
632         frame.hour = tt->tm_hour;
633         frame.minute = tt->tm_min;
634         XClearWindow(display, frame.clock);
635         redraw = True;
636       }
637     }
638
639     if (redraw) {
640 #ifdef    HAVE_STRFTIME
641       char t[1024];
642       if (! strftime(t, 1024, screen.strftimeFormat(), tt))
643         return;
644 #else // !HAVE_STRFTIME
645       char t[9];
646       if (date) {
647         // format the date... with special consideration for y2k ;)
648         if (screen.getDateFormat() == Openbox::B_EuropeanDate)
649           sprintf(t, 18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormatEu,
650                                      "%02d.%02d.%02d"),
651                   tt->tm_mday, tt->tm_mon + 1,
652                   (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
653         else
654           sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeDateFormat,
655                           "%02d/%02d/%02d"),
656                   tt->tm_mon + 1, tt->tm_mday,
657                   (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
658       } else {
659         if (screen.isClock24Hour())
660           sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeTimeFormat24,
661                           "  %02d:%02d "),
662                   frame.hour, frame.minute);
663         else
664           sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeTimeFormat12,
665                           "%02d:%02d %sm"),
666                   ((frame.hour > 12) ? frame.hour - 12 :
667                    ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
668                   ((frame.hour >= 12) ?
669                    i18n(ToolbarSet,
670                         ToolbarNoStrftimeTimeFormatP, "p") :
671                    i18n(ToolbarSet,
672                         ToolbarNoStrftimeTimeFormatA, "a")));
673       }
674 #endif // HAVE_STRFTIME
675
676       int dx = (frame.bevel_w * 2), dlen = strlen(t);
677       unsigned int l;
678
679       if (i18n.multibyte()) {
680         XRectangle ink, logical;
681         XmbTextExtents(screen.getToolbarStyle()->fontset,
682                        t, dlen, &ink, &logical);
683         l = logical.width;
684       } else {
685         l = XTextWidth(screen.getToolbarStyle()->font, t, dlen);
686       }
687
688       l += (frame.bevel_w * 4);
689
690       if (l > frame.clock_w) {
691         for (; dlen >= 0; dlen--) {
692           if (i18n.multibyte()) {
693             XRectangle ink, logical;
694             XmbTextExtents(screen.getToolbarStyle()->fontset,
695                            t, dlen, &ink, &logical);
696             l = logical.width;
697           } else {
698             l = XTextWidth(screen.getToolbarStyle()->font, t, dlen);
699           }
700           l+= (frame.bevel_w * 4);
701
702           if (l < frame.clock_w)
703             break;
704         }
705       }
706       switch (screen.getToolbarStyle()->justify) {
707       case BScreen::RightJustify:
708         dx += frame.clock_w - l;
709         break;
710
711       case BScreen::CenterJustify:
712         dx += (frame.clock_w - l) / 2;
713         break;
714       }
715
716       ToolbarStyle *style = screen.getToolbarStyle();
717       if (i18n.multibyte())
718         XmbDrawString(display, frame.clock, style->fontset, style->c_text_gc,
719                       dx, (1 - style->fontset_extents->max_ink_extent.y),
720                       t, dlen);
721       else
722         XDrawString(display, frame.clock, style->c_text_gc, dx,
723                     (style->font->ascent + 1), t, dlen);
724     }
725   }
726
727
728   void Toolbar::redrawWindowLabel(Bool redraw) {
729     OpenboxWindow *foc = screen.getOpenbox().focusedWindow();
730     if (foc == (OpenboxWindow *) 0) {
731       XClearWindow(display, frame.window_label);
732     } else {
733       if (redraw)
734         XClearWindow(display, frame.window_label);
735
736       if (foc->getScreen() != &screen) return;
737
738       int dx = (frame.bevel_w * 2), dlen = strlen(*foc->getTitle());
739       unsigned int l;
740
741       if (i18n.multibyte()) {
742         XRectangle ink, logical;
743         XmbTextExtents(screen.getToolbarStyle()->fontset, *foc->getTitle(),
744                        dlen, &ink, &logical);
745         l = logical.width;
746       } else {
747         l = XTextWidth(screen.getToolbarStyle()->font, *foc->getTitle(), dlen);
748       }
749       l += (frame.bevel_w * 4);
750
751       if (l > frame.window_label_w) {
752         for (; dlen >= 0; dlen--) {
753           if (i18n.multibyte()) {
754             XRectangle ink, logical;
755             XmbTextExtents(screen.getToolbarStyle()->fontset,
756                            *foc->getTitle(), dlen, &ink, &logical);
757             l = logical.width;
758           } else {
759             l = XTextWidth(screen.getToolbarStyle()->font,
760                            *foc->getTitle(), dlen);
761           }
762           l += (frame.bevel_w * 4);
763
764           if (l < frame.window_label_w)
765             break;
766         }
767       }
768       switch (screen.getToolbarStyle()->justify) {
769       case BScreen::RightJustify:
770         dx += frame.window_label_w - l;
771         break;
772
773       case BScreen::CenterJustify:
774         dx += (frame.window_label_w - l) / 2;
775         break;
776       }
777
778       ToolbarStyle *style = screen.getToolbarStyle();
779       if (i18n.multibyte())
780         XmbDrawString(display, frame.window_label, style->fontset,
781                       style->w_text_gc, dx,
782                       (1 - style->fontset_extents->max_ink_extent.y),
783                       *foc->getTitle(), dlen);
784       else
785         XDrawString(display, frame.window_label, style->w_text_gc, dx,
786                     (style->font->ascent + 1), *foc->getTitle(), dlen);
787     }
788   }
789
790
791   void Toolbar::redrawWorkspaceLabel(Bool redraw) {
792     if (screen.getCurrentWorkspace()->getName()) {
793       if (redraw)
794         XClearWindow(display, frame.workspace_label);
795
796       int dx = (frame.bevel_w * 2), dlen =
797         strlen(screen.getCurrentWorkspace()->getName());
798       unsigned int l;
799
800       if (i18n.multibyte()) {
801         XRectangle ink, logical;
802         XmbTextExtents(screen.getToolbarStyle()->fontset,
803                        screen.getCurrentWorkspace()->getName(), dlen,
804                        &ink, &logical);
805         l = logical.width;
806       } else {
807         l = XTextWidth(screen.getToolbarStyle()->font,
808                        screen.getCurrentWorkspace()->getName(), dlen);
809       }
810       l += (frame.bevel_w * 4);
811
812       if (l > frame.workspace_label_w) {
813         for (; dlen >= 0; dlen--) {
814           if (i18n.multibyte()) {
815             XRectangle ink, logical;
816             XmbTextExtents(screen.getToolbarStyle()->fontset,
817                            screen.getCurrentWorkspace()->getName(), dlen,
818                            &ink, &logical);
819             l = logical.width;
820           } else {
821             l = XTextWidth(screen.getWindowStyle()->font,
822                            screen.getCurrentWorkspace()->getName(), dlen);
823           }
824           l += (frame.bevel_w * 4);
825
826           if (l < frame.workspace_label_w)
827             break;
828         }
829       }
830       switch (screen.getToolbarStyle()->justify) {
831       case BScreen::RightJustify:
832         dx += frame.workspace_label_w - l;
833         break;
834
835       case BScreen::CenterJustify:
836         dx += (frame.workspace_label_w - l) / 2;
837         break;
838       }
839
840       ToolbarStyle *style = screen.getToolbarStyle();
841       if (i18n.multibyte())
842         XmbDrawString(display, frame.workspace_label, style->fontset,
843                       style->l_text_gc, dx,
844                       (1 - style->fontset_extents->max_ink_extent.y),
845                       (char *) screen.getCurrentWorkspace()->getName(), dlen);
846       else
847         XDrawString(display, frame.workspace_label, style->l_text_gc, dx,
848                     (style->font->ascent + 1),
849                     (char *) screen.getCurrentWorkspace()->getName(), dlen);
850     }
851   }
852
853
854   void Toolbar::redrawPrevWorkspaceButton(Bool pressed, Bool redraw) {
855     if (redraw) {
856       if (pressed) {
857         if (frame.pbutton)
858           XSetWindowBackgroundPixmap(display, frame.psbutton, frame.pbutton);
859         else
860           XSetWindowBackground(display, frame.psbutton, frame.pbutton_pixel);
861       } else {
862         if (frame.button)
863           XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
864         else
865           XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
866       }
867       XClearWindow(display, frame.psbutton);
868     }
869
870     int hh = frame.button_w / 2, hw = frame.button_w / 2;
871
872     XPoint pts[3];
873     pts[0].x = hw - 2; pts[0].y = hh;
874     pts[1].x = 4; pts[1].y = 2;
875     pts[2].x = 0; pts[2].y = -4;
876
877     XFillPolygon(display, frame.psbutton, screen.getToolbarStyle()->b_pic_gc,
878                  pts, 3, Convex, CoordModePrevious);
879   }
880
881
882   void Toolbar::redrawNextWorkspaceButton(Bool pressed, Bool redraw) {
883     if (redraw) {
884       if (pressed) {
885         if (frame.pbutton)
886           XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.pbutton);
887         else
888           XSetWindowBackground(display, frame.nsbutton, frame.pbutton_pixel);
889       } else {
890         if (frame.button)
891           XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
892         else
893           XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
894       }
895       XClearWindow(display, frame.nsbutton);
896     }
897
898     int hh = frame.button_w / 2, hw = frame.button_w / 2;
899
900     XPoint pts[3];
901     pts[0].x = hw - 2; pts[0].y = hh - 2;
902     pts[1].x = 4; pts[1].y =  2;
903     pts[2].x = -4; pts[2].y = 2;
904
905     XFillPolygon(display, frame.nsbutton, screen.getToolbarStyle()->b_pic_gc,
906                  pts, 3, Convex, CoordModePrevious);
907   }
908
909
910   void Toolbar::redrawPrevWindowButton(Bool pressed, Bool redraw) {
911     if (redraw) {
912       if (pressed) {
913         if (frame.pbutton)
914           XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.pbutton);
915         else
916           XSetWindowBackground(display, frame.pwbutton, frame.pbutton_pixel);
917       } else {
918         if (frame.button)
919           XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
920         else
921           XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
922       }
923       XClearWindow(display, frame.pwbutton);
924     }
925
926     int hh = frame.button_w / 2, hw = frame.button_w / 2;
927
928     XPoint pts[3];
929     pts[0].x = hw - 2; pts[0].y = hh;
930     pts[1].x = 4; pts[1].y = 2;
931     pts[2].x = 0; pts[2].y = -4;
932
933     XFillPolygon(display, frame.pwbutton, screen.getToolbarStyle()->b_pic_gc,
934                  pts, 3, Convex, CoordModePrevious);
935   }
936
937
938   void Toolbar::redrawNextWindowButton(Bool pressed, Bool redraw) {
939     if (redraw) {
940       if (pressed) {
941         if (frame.pbutton)
942           XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.pbutton);
943         else
944           XSetWindowBackground(display, frame.nwbutton, frame.pbutton_pixel);
945       } else {
946         if (frame.button)
947           XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
948         else
949           XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
950       }
951       XClearWindow(display, frame.nwbutton);
952     }
953
954     int hh = frame.button_w / 2, hw = frame.button_w / 2;
955
956     XPoint pts[3];
957     pts[0].x = hw - 2; pts[0].y = hh - 2;
958     pts[1].x = 4; pts[1].y =  2;
959     pts[2].x = -4; pts[2].y = 2;
960
961     XFillPolygon(display, frame.nwbutton, screen.getToolbarStyle()->b_pic_gc,
962                  pts, 3, Convex, CoordModePrevious);
963   }
964
965
966   void Toolbar::edit() {
967     Window window;
968     int foo;
969
970     m_editing = True;
971     if (XGetInputFocus(display, &window, &foo) &&
972         window == frame.workspace_label)
973       return;
974
975     XSetInputFocus(display, frame.workspace_label,
976                    RevertToPointerRoot, CurrentTime);
977     XClearWindow(display, frame.workspace_label);
978
979     openbox.setNoFocus(True);
980     if (openbox.focusedWindow())
981       openbox.focusedWindow()->setFocusFlag(False);
982
983     XDrawRectangle(display, frame.workspace_label,
984                    screen.getWindowStyle()->l_text_focus_gc,
985                    frame.workspace_label_w / 2, 0, 1,
986                    frame.label_h - 1);
987
988     // change the background of the window to that of an active window label
989     Pixmap tmp = frame.wlabel;
990     BTexture *texture = &(screen.getWindowStyle()->l_focus);
991     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
992       frame.wlabel = None;
993       XSetWindowBackground(display, frame.workspace_label,
994                            texture->getColor()->getPixel());
995     } else {
996       frame.wlabel =
997         image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
998       XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
999     }
1000     if (tmp) image_ctrl->removeImage(tmp);
1001   }
1002
1003
1004   void Toolbar::buttonPressEvent(XButtonEvent *be) {
1005     if (be->button == 1) {
1006       if (be->window == frame.psbutton)
1007         redrawPrevWorkspaceButton(True, True);
1008       else if (be->window == frame.nsbutton)
1009         redrawNextWorkspaceButton(True, True);
1010       else if (be->window == frame.pwbutton)
1011         redrawPrevWindowButton(True, True);
1012       else if (be->window == frame.nwbutton)
1013         redrawNextWindowButton(True, True);
1014 #ifndef   HAVE_STRFTIME
1015       else if (be->window == frame.clock) {
1016         XClearWindow(display, frame.clock);
1017         checkClock(True, True);
1018       }
1019 #endif // HAVE_STRFTIME
1020       else if (! m_ontop) {
1021         Window w[1] = { frame.window };
1022         screen.raiseWindows(w, 1);
1023       }
1024     } else if (be->button == 2 && (! m_ontop)) {
1025       XLowerWindow(display, frame.window);
1026     } else if (be->button == 3) {
1027       if (! toolbarmenu->isVisible()) {
1028         int x, y;
1029
1030         x = be->x_root - (toolbarmenu->getWidth() / 2);
1031         y = be->y_root - (toolbarmenu->getHeight() / 2);
1032
1033         if (x < 0)
1034           x = 0;
1035         else if (x + toolbarmenu->getWidth() > screen.size().w())
1036           x = screen.size().w() - toolbarmenu->getWidth();
1037
1038         if (y < 0)
1039           y = 0;
1040         else if (y + toolbarmenu->getHeight() > screen.size().h())
1041           y = screen.size().h() - toolbarmenu->getHeight();
1042
1043         toolbarmenu->move(x, y);
1044         toolbarmenu->show();
1045       } else
1046         toolbarmenu->hide();
1047     }
1048   }
1049
1050
1051
1052   void Toolbar::buttonReleaseEvent(XButtonEvent *re) {
1053     if (re->button == 1) {
1054       if (re->window == frame.psbutton) {
1055         redrawPrevWorkspaceButton(False, True);
1056
1057         if (re->x >= 0 && re->x < (signed) frame.button_w &&
1058             re->y >= 0 && re->y < (signed) frame.button_w)
1059           if (screen.getCurrentWorkspace()->getWorkspaceID() > 0)
1060             screen.changeWorkspaceID(screen.getCurrentWorkspace()->
1061                                      getWorkspaceID() - 1);
1062           else
1063             screen.changeWorkspaceID(screen.getWorkspaceCount() - 1);
1064       } else if (re->window == frame.nsbutton) {
1065         redrawNextWorkspaceButton(False, True);
1066
1067         if (re->x >= 0 && re->x < (signed) frame.button_w &&
1068             re->y >= 0 && re->y < (signed) frame.button_w)
1069           if (screen.getCurrentWorkspace()->getWorkspaceID() <
1070               screen.getWorkspaceCount() - 1)
1071             screen.changeWorkspaceID(screen.getCurrentWorkspace()->
1072                                      getWorkspaceID() + 1);
1073           else
1074             screen.changeWorkspaceID(0);
1075       } else if (re->window == frame.pwbutton) {
1076         redrawPrevWindowButton(False, True);
1077
1078         if (re->x >= 0 && re->x < (signed) frame.button_w &&
1079             re->y >= 0 && re->y < (signed) frame.button_w)
1080           screen.prevFocus();
1081       } else if (re->window == frame.nwbutton) {
1082         redrawNextWindowButton(False, True);
1083
1084         if (re->x >= 0 && re->x < (signed) frame.button_w &&
1085             re->y >= 0 && re->y < (signed) frame.button_w)
1086           screen.nextFocus();
1087       } else if (re->window == frame.window_label)
1088         screen.raiseFocus();
1089 #ifndef   HAVE_STRFTIME
1090       else if (re->window == frame.clock) {
1091         XClearWindow(display, frame.clock);
1092         checkClock(True);
1093       }
1094 #endif // HAVE_STRFTIME
1095     }
1096   }
1097
1098
1099   void Toolbar::enterNotifyEvent(XCrossingEvent *) {
1100     if (! m_autohide)
1101       return;
1102
1103     if (m_hidden) {
1104       if (! hide_timer->isTiming()) hide_timer->start();
1105     } else {
1106       if (hide_timer->isTiming()) hide_timer->stop();
1107     }
1108   }
1109
1110   void Toolbar::leaveNotifyEvent(XCrossingEvent *) {
1111     if (! m_autohide)
1112       return;
1113
1114     if (m_hidden) {
1115       if (hide_timer->isTiming()) hide_timer->stop();
1116     } else if (! toolbarmenu->isVisible()) {
1117       if (! hide_timer->isTiming()) hide_timer->start();
1118     }
1119   }
1120
1121
1122   void Toolbar::exposeEvent(XExposeEvent *ee) {
1123     if (ee->window == frame.clock) checkClock(True);
1124     else if (ee->window == frame.workspace_label && (! m_editing))
1125       redrawWorkspaceLabel();
1126     else if (ee->window == frame.window_label) redrawWindowLabel();
1127     else if (ee->window == frame.psbutton) redrawPrevWorkspaceButton();
1128     else if (ee->window == frame.nsbutton) redrawNextWorkspaceButton();
1129     else if (ee->window == frame.pwbutton) redrawPrevWindowButton();
1130     else if (ee->window == frame.nwbutton) redrawNextWindowButton();
1131   }
1132
1133
1134   void Toolbar::keyPressEvent(XKeyEvent *ke) {
1135     if (ke->window == frame.workspace_label && m_editing) {
1136       openbox.grab();
1137
1138       if (! new_workspace_name) {
1139         new_workspace_name = new char[128];
1140         new_name_pos = 0;
1141
1142         if (! new_workspace_name) return;
1143       }
1144
1145       KeySym ks;
1146       char keychar[1];
1147       XLookupString(ke, keychar, 1, &ks, 0);
1148
1149       // either we are told to end with a return or we hit the end of the buffer
1150       if (ks == XK_Return || new_name_pos == 127) {
1151         *(new_workspace_name + new_name_pos) = 0;
1152
1153         m_editing = False;
1154
1155         openbox.setNoFocus(False);
1156         if (openbox.focusedWindow()) {
1157           openbox.focusedWindow()->setInputFocus();
1158           openbox.focusedWindow()->setFocusFlag(True);
1159         } else
1160           openbox.focusWindow((OpenboxWindow *) 0);
1161
1162         // check to make sure that new_name[0] != 0... otherwise we have a null
1163         // workspace name which causes serious problems, especially for the
1164         // Openbox::LoadRC() method.
1165         if (*new_workspace_name) {
1166           screen.getCurrentWorkspace()->setName(new_workspace_name);
1167           screen.getCurrentWorkspace()->getMenu()->hide();
1168           screen.getWorkspacemenu()->
1169             remove(screen.getCurrentWorkspace()->getWorkspaceID() + 2);
1170           screen.getWorkspacemenu()->
1171             insert(screen.getCurrentWorkspace()->getName(),
1172                    screen.getCurrentWorkspace()->getMenu(),
1173                    screen.getCurrentWorkspace()->getWorkspaceID() + 2);
1174           screen.getWorkspacemenu()->update();
1175         }
1176
1177         delete [] new_workspace_name;
1178         new_workspace_name = (char *) 0;
1179         new_name_pos = 0;
1180
1181         // reset the background to that of the workspace label (its normal
1182         // setting)
1183         Pixmap tmp = frame.wlabel;
1184         BTexture *texture = &(screen.getToolbarStyle()->label);
1185         if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
1186           frame.wlabel = None;
1187           XSetWindowBackground(display, frame.workspace_label,
1188                                texture->getColor()->getPixel());
1189         } else {
1190           frame.wlabel =
1191             image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
1192           XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
1193         }
1194         if (tmp) image_ctrl->removeImage(tmp);
1195
1196         reconfigure();
1197       } else if (! (ks == XK_Shift_L || ks == XK_Shift_R ||
1198                     ks == XK_Control_L || ks == XK_Control_R ||
1199                     ks == XK_Caps_Lock || ks == XK_Shift_Lock ||
1200                     ks == XK_Meta_L || ks == XK_Meta_R ||
1201                     ks == XK_Alt_L || ks == XK_Alt_R ||
1202                     ks == XK_Super_L || ks == XK_Super_R ||
1203                     ks == XK_Hyper_L || ks == XK_Hyper_R)) {
1204         if (ks == XK_BackSpace) {
1205           if (new_name_pos > 0) {
1206             --new_name_pos;
1207             *(new_workspace_name + new_name_pos) = '\0';
1208           } else {
1209             *new_workspace_name = '\0';
1210           }
1211         } else {
1212           *(new_workspace_name + new_name_pos) = *keychar;
1213           ++new_name_pos;
1214           *(new_workspace_name + new_name_pos) = '\0';
1215         }
1216
1217         XClearWindow(display, frame.workspace_label);
1218         int l = strlen(new_workspace_name), tw, x;
1219
1220         if (i18n.multibyte()) {
1221           XRectangle ink, logical;
1222           XmbTextExtents(screen.getToolbarStyle()->fontset,
1223                          new_workspace_name, l, &ink, &logical);
1224           tw = logical.width;
1225         } else {
1226           tw = XTextWidth(screen.getToolbarStyle()->font,
1227                           new_workspace_name, l);
1228         }
1229         x = (frame.workspace_label_w - tw) / 2;
1230
1231         if (x < (signed) frame.bevel_w) x = frame.bevel_w;
1232
1233         WindowStyle *style = screen.getWindowStyle();
1234         if (i18n.multibyte())
1235           XmbDrawString(display, frame.workspace_label, style->fontset,
1236                         style->l_text_focus_gc, x,
1237                         (1 - style->fontset_extents->max_ink_extent.y),
1238                         new_workspace_name, l);
1239         else
1240           XDrawString(display, frame.workspace_label, style->l_text_focus_gc, x,
1241                       (style->font->ascent + 1),
1242                       new_workspace_name, l);
1243
1244         XDrawRectangle(display, frame.workspace_label,
1245                        screen.getWindowStyle()->l_text_focus_gc, x + tw, 0, 1,
1246                        frame.label_h - 1);
1247       }
1248
1249       openbox.ungrab();
1250     }
1251   }
1252
1253
1254   void Toolbar::timeout() {
1255     checkClock(True);
1256
1257     timeval now;
1258     gettimeofday(&now, 0);
1259     clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
1260   }
1261
1262
1263   void Toolbar::HideHandler::timeout() {
1264     toolbar->m_hidden = !toolbar->m_hidden;
1265     if (toolbar->m_hidden)
1266       XMoveWindow(toolbar->display, toolbar->frame.window,
1267                   toolbar->frame.x_hidden, toolbar->frame.y_hidden);
1268     else
1269       XMoveWindow(toolbar->display, toolbar->frame.window,
1270                   toolbar->frame.x, toolbar->frame.y);
1271   }
1272
1273
1274   Toolbarmenu::Toolbarmenu(Toolbar &tb) : Basemenu(tb.screen), toolbar(tb) {
1275     setLabel(i18n(ToolbarSet, ToolbarToolbarTitle, "Toolbar"));
1276     setInternalMenu();
1277
1278     placementmenu = new Placementmenu(*this);
1279
1280     insert(i18n(CommonSet, CommonPlacementTitle, "Placement"),
1281            placementmenu);
1282     insert(i18n(CommonSet, CommonAlwaysOnTop, "Always on top"), 1);
1283     insert(i18n(CommonSet, CommonAutoHide, "Auto hide"), 2);
1284     insert(i18n(ToolbarSet, ToolbarEditWkspcName,
1285                 "Edit current workspace name"), 3);
1286
1287     update();
1288
1289     setValues();
1290   }
1291
1292   void Toolbarmenu::setValues() {
1293     setItemSelected(1, toolbar.onTop());
1294     setItemSelected(2, toolbar.autoHide());
1295   }
1296
1297
1298   Toolbarmenu::~Toolbarmenu() {
1299     delete placementmenu;
1300   }
1301
1302
1303   void Toolbarmenu::itemSelected(int button, int index) {
1304     if (button != 1)
1305       return;
1306
1307     BasemenuItem *item = find(index);
1308     if (! item) return;
1309
1310     switch (item->function()) {
1311     case 1: { // always on top
1312       Bool change = ((toolbar.onTop()) ? False : True);
1313       toolbar.setOnTop(change);
1314       setItemSelected(1, change);
1315
1316       if (toolbar.onTop()) toolbar.screen.raiseWindows((Window *) 0, 0);
1317       break;
1318     }
1319
1320     case 2: { // auto hide
1321       Bool change = ((toolbar.autoHide()) ?  False : True);
1322       toolbar.setAutoHide(change);
1323       setItemSelected(2, change);
1324
1325 #ifdef    SLIT
1326       toolbar.screen.getSlit()->reposition();
1327 #endif // SLIT
1328       break;
1329     }
1330
1331     case 3: { // edit current workspace name
1332       toolbar.edit();
1333       hide();
1334
1335       break;
1336     }
1337     } // switch
1338   }
1339
1340
1341   void Toolbarmenu::internal_hide() {
1342     Basemenu::internal_hide();
1343     if (toolbar.autoHide() && ! toolbar.isEditing())
1344       toolbar.hide_handler.timeout();
1345   }
1346
1347
1348   void Toolbarmenu::reconfigure() {
1349     setValues();
1350     placementmenu->reconfigure();
1351
1352     Basemenu::reconfigure();
1353   }
1354
1355
1356   Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu &tm)
1357     : Basemenu(tm.toolbar.screen), toolbarmenu(tm) {
1358       setLabel(i18n(ToolbarSet, ToolbarToolbarPlacement,
1359                     "Toolbar Placement"));
1360       setInternalMenu();
1361       setMinimumSublevels(3);
1362
1363       insert(i18n(CommonSet, CommonPlacementTopLeft,
1364                   "Top Left"), Toolbar::TopLeft);
1365       insert(i18n(CommonSet, CommonPlacementBottomLeft,
1366                   "Bottom Left"), Toolbar::BottomLeft);
1367       insert(i18n(CommonSet, CommonPlacementTopCenter,
1368                   "Top Center"), Toolbar::TopCenter);
1369       insert(i18n(CommonSet, CommonPlacementBottomCenter,
1370                   "Bottom Center"), Toolbar::BottomCenter);
1371       insert(i18n(CommonSet, CommonPlacementTopRight,
1372                   "Top Right"), Toolbar::TopRight);
1373       insert(i18n(CommonSet, CommonPlacementBottomRight,
1374                   "Bottom Right"), Toolbar::BottomRight);
1375       update();
1376     }
1377
1378   void Toolbarmenu::Placementmenu::itemSelected(int button, int index) {
1379     if (button != 1)
1380       return;
1381
1382     BasemenuItem *item = find(index);
1383     if (! item) return;
1384
1385     toolbarmenu.toolbar.setPlacement(item->function());
1386     hide();
1387     toolbarmenu.toolbar.reconfigure();
1388
1389 #ifdef    SLIT
1390     // reposition the slit as well to make sure it doesn't intersect the
1391     // toolbar
1392     toolbarmenu.toolbar.screen.getSlit()->reposition();
1393 #endif // SLIT
1394   }