]> icculus.org git repositories - dana/openbox.git/blob - src/Toolbar.cc
merged bitmap buttons
[dana/openbox.git] / src / Toolbar.cc
1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // Toolbar.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/keysym.h>
30
31 #ifdef HAVE_STRING_H
32 #  include <string.h>
33 #endif // HAVE_STRING_H
34
35 #ifdef    HAVE_STDIO_H
36 #  include <stdio.h>
37 #endif // HAVE_STDIO_H
38
39 #ifdef    TIME_WITH_SYS_TIME
40 # include <sys/time.h>
41 # include <time.h>
42 #else // !TIME_WITH_SYS_TIME
43 # ifdef    HAVE_SYS_TIME_H
44 #  include <sys/time.h>
45 # else // !HAVE_SYS_TIME_H
46 #  include <time.h>
47 # endif // HAVE_SYS_TIME_H
48 #endif // TIME_WITH_SYS_TIME
49 }
50
51 #include <string>
52 using std::string;
53
54 #include "i18n.hh"
55 #include "blackbox.hh"
56 #include "Font.hh"
57 #include "GCCache.hh"
58 #include "Image.hh"
59 #include "Screen.hh"
60 #include "Toolbar.hh"
61 #include "Window.hh"
62 #include "Workspace.hh"
63 #include "Clientmenu.hh"
64 #include "Workspacemenu.hh"
65 #include "Slit.hh"
66
67
68 static long aMinuteFromNow(void) {
69   timeval now;
70   gettimeofday(&now, 0);
71   return ((60 - (now.tv_sec % 60)) * 1000);
72 }
73
74
75 Toolbar::Toolbar(BScreen *scrn) {
76   screen = scrn;
77   blackbox = screen->getBlackbox();
78   toolbarstr = "session.screen" + itostring(screen->getScreenNumber()) +
79     ".toolbar.";
80   config = blackbox->getConfig();
81
82   load_rc();
83
84   // get the clock updating every minute
85   clock_timer = new BTimer(blackbox, this);
86   clock_timer->setTimeout(aMinuteFromNow());
87   clock_timer->recurring(True);
88   clock_timer->start();
89   frame.minute = frame.hour = -1;
90
91   hide_handler.toolbar = this;
92   hide_timer = new BTimer(blackbox, &hide_handler);
93   hide_timer->setTimeout(blackbox->getAutoRaiseDelay());
94
95   editing = False;
96   new_name_pos = 0;
97
98   toolbarmenu = new Toolbarmenu(this);
99
100   display = blackbox->getXDisplay();
101   XSetWindowAttributes attrib;
102   unsigned long create_mask = CWBackPixmap | CWBackPixel | CWBorderPixel |
103                               CWColormap | CWOverrideRedirect | CWEventMask;
104   attrib.background_pixmap = None;
105   attrib.background_pixel = attrib.border_pixel =
106     screen->getBorderColor()->pixel();
107   attrib.colormap = screen->getColormap();
108   attrib.override_redirect = True;
109   attrib.event_mask = ButtonPressMask | ButtonReleaseMask |
110                       EnterWindowMask | LeaveWindowMask;
111
112   frame.window =
113     XCreateWindow(display, screen->getRootWindow(), 0, 0, 1, 1, 0,
114                   screen->getDepth(), InputOutput, screen->getVisual(),
115                   create_mask, &attrib);
116   blackbox->saveToolbarSearch(frame.window, this);
117
118   attrib.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask |
119                       KeyPressMask | EnterWindowMask;
120
121   frame.workspace_label =
122     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
123                   InputOutput, screen->getVisual(), create_mask, &attrib);
124   blackbox->saveToolbarSearch(frame.workspace_label, this);
125
126   frame.window_label =
127     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
128                   InputOutput, screen->getVisual(), create_mask, &attrib);
129   blackbox->saveToolbarSearch(frame.window_label, this);
130
131   frame.clock =
132     XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
133                   InputOutput, screen->getVisual(), create_mask, &attrib);
134   blackbox->saveToolbarSearch(frame.clock, this);
135
136   frame.psbutton =
137     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
138                   InputOutput, screen->getVisual(), create_mask, &attrib);
139   blackbox->saveToolbarSearch(frame.psbutton, this);
140
141   frame.nsbutton =
142     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
143                   InputOutput, screen->getVisual(), create_mask, &attrib);
144   blackbox->saveToolbarSearch(frame.nsbutton, this);
145
146   frame.pwbutton =
147     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
148                   InputOutput, screen->getVisual(), create_mask, &attrib);
149   blackbox->saveToolbarSearch(frame.pwbutton, this);
150
151   frame.nwbutton =
152     XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen->getDepth(),
153                   InputOutput, screen->getVisual(), create_mask, &attrib);
154   blackbox->saveToolbarSearch(frame.nwbutton, this);
155
156   frame.base = frame.label = frame.wlabel = frame.clk = frame.button =
157     frame.pbutton = None;
158
159   reconfigure();
160   mapToolbar();
161 }
162
163
164 Toolbar::~Toolbar(void) {
165   unmapToolbar();
166
167   if (frame.base) screen->getImageControl()->removeImage(frame.base);
168   if (frame.label) screen->getImageControl()->removeImage(frame.label);
169   if (frame.wlabel) screen->getImageControl()->removeImage(frame.wlabel);
170   if (frame.clk) screen->getImageControl()->removeImage(frame.clk);
171   if (frame.button) screen->getImageControl()->removeImage(frame.button);
172   if (frame.pbutton) screen->getImageControl()->removeImage(frame.pbutton);
173
174   blackbox->removeToolbarSearch(frame.window);
175   blackbox->removeToolbarSearch(frame.workspace_label);
176   blackbox->removeToolbarSearch(frame.window_label);
177   blackbox->removeToolbarSearch(frame.clock);
178   blackbox->removeToolbarSearch(frame.psbutton);
179   blackbox->removeToolbarSearch(frame.nsbutton);
180   blackbox->removeToolbarSearch(frame.pwbutton);
181   blackbox->removeToolbarSearch(frame.nwbutton);
182
183   XDestroyWindow(display, frame.workspace_label);
184   XDestroyWindow(display, frame.window_label);
185   XDestroyWindow(display, frame.clock);
186
187   XDestroyWindow(display, frame.window);
188
189   delete hide_timer;
190   delete clock_timer;
191   delete toolbarmenu;
192 }
193
194
195 void Toolbar::mapToolbar() {
196   if (!screen->doHideToolbar()) {
197     //not hidden, so windows should not maximize over the toolbar
198     XMapSubwindows(display, frame.window);
199     XMapWindow(display, frame.window);
200   }
201   screen->addStrut(&strut);
202   updateStrut();
203 }
204
205
206 void Toolbar::unmapToolbar() {
207   if (toolbarmenu->isVisible())
208     toolbarmenu->hide();
209   //hidden so we can maximize over the toolbar
210   screen->removeStrut(&strut);
211   screen->updateAvailableArea();
212
213   XUnmapWindow(display, frame.window);
214   updateStrut();
215 }
216
217
218 void Toolbar::saveOnTop(bool b) {
219   on_top = b;
220   config->setValue(toolbarstr + "onTop", on_top);
221 }
222
223
224 void Toolbar::saveAutoHide(bool b) {
225   do_auto_hide = b;
226   config->setValue(toolbarstr + "autoHide", do_auto_hide);
227 }
228
229
230 void Toolbar::saveWidthPercent(unsigned int w) {
231   width_percent = w;
232   config->setValue(toolbarstr + "widthPercent", width_percent);
233 }
234
235
236 void Toolbar::savePlacement(int p) {
237   placement = p;
238   const char *pname;
239   switch (placement) {
240   case TopLeft: pname = "TopLeft"; break;
241   case BottomLeft: pname = "BottomLeft"; break;
242   case TopCenter: pname = "TopCenter"; break;
243   case TopRight: pname = "TopRight"; break;
244   case BottomRight: pname = "BottomRight"; break;
245   case BottomCenter: default: pname = "BottomCenter"; break;
246   }
247   config->setValue(toolbarstr + "placement", pname);
248 }
249
250
251 void Toolbar::save_rc(void) {
252   saveOnTop(on_top);
253   saveAutoHide(do_auto_hide);
254   saveWidthPercent(width_percent);
255   savePlacement(placement);
256 }
257
258
259 void Toolbar::load_rc(void) {
260   string s;
261   
262   if (! config->getValue(toolbarstr + "onTop", on_top))
263     on_top = false;
264
265   if (! config->getValue(toolbarstr + "autoHide", do_auto_hide))
266     do_auto_hide = false;
267   hidden = do_auto_hide;
268
269   if (! config->getValue(toolbarstr + "widthPercent", width_percent) ||
270       width_percent == 0 || width_percent > 100)
271     width_percent = 66;
272
273   if (config->getValue(toolbarstr + "placement", s)) {
274     if (s == "TopLeft")
275       placement = TopLeft;
276     else if (s == "BottomLeft")
277       placement = BottomLeft;
278     else if (s == "TopCenter")
279       placement = TopCenter;
280     else if (s == "TopRight")
281       placement = TopRight;
282     else if (s == "BottomRight")
283       placement = BottomRight;
284     else //if (s == "BottomCenter")
285       placement = BottomCenter;
286   } else
287     placement = BottomCenter;
288 }
289
290
291 void Toolbar::reconfigure(void) {
292   unsigned int width, height;
293  
294   width = (screen->getWidth() * width_percent) / 100;
295   height = screen->getToolbarStyle()->font->height();
296
297   frame.bevel_w = screen->getBevelWidth();
298   frame.button_w = height;
299   height += 2;
300   frame.label_h = height;
301   height += (frame.bevel_w * 2);
302
303   frame.rect.setSize(width, height);
304
305   int x, y;
306   switch (placement) {
307   case TopLeft:
308   case TopRight:
309   case TopCenter:
310     if (placement == TopLeft)
311       x = 0;
312     else if (placement == TopRight)
313       x = screen->getWidth() - frame.rect.width()
314         - (screen->getBorderWidth() * 2);
315     else
316       x = (screen->getWidth() - frame.rect.width()) / 2;
317
318     y = 0;
319
320     frame.x_hidden = x;
321     frame.y_hidden = screen->getBevelWidth() - screen->getBorderWidth()
322                      - frame.rect.height();
323     break;
324
325   case BottomLeft:
326   case BottomRight:
327   case BottomCenter:
328   default:
329     if (placement == BottomLeft)
330       x = 0;
331     else if (placement == BottomRight)
332       x = screen->getWidth() - frame.rect.width()
333         - (screen->getBorderWidth() * 2);
334     else
335       x = (screen->getWidth() - frame.rect.width()) / 2;
336
337     y = screen->getHeight() - frame.rect.height()
338       - (screen->getBorderWidth() * 2);
339
340     frame.x_hidden = x;
341     frame.y_hidden = screen->getHeight() - screen->getBevelWidth()
342                      - screen->getBorderWidth();
343     break;
344   }
345
346   frame.rect.setPos(x, y);
347
348   updateStrut();
349
350 #ifdef    HAVE_STRFTIME
351   time_t ttmp = time(NULL);
352
353   frame.clock_w = 0;
354   if (ttmp != -1) {
355     struct tm *tt = localtime(&ttmp);
356     if (tt) {
357       char t[1024];
358       int len = strftime(t, 1024, screen->getStrftimeFormat(), tt);
359       if (len == 0) { // invalid time format found
360         screen->saveStrftimeFormat("%I:%M %p"); // so use the default
361         strftime(t, 1024, screen->getStrftimeFormat(), tt);
362       }
363       // find the length of the rendered string and add room for two extra
364       // characters to it.  This allows for variable width output of the fonts
365       BFont *font = screen->getToolbarStyle()->font;
366       frame.clock_w = font->measureString(t) + font->maxCharWidth() * 2;
367     }
368   }
369 #else // !HAVE_STRFTIME
370   {
371     string s = i18n(ToolbarSet, ToolbarNoStrftimeLength, "00:00000");
372     frame.clock_w = screen->getToolbarStyle()->font->measureString(s);
373   }
374 #endif // HAVE_STRFTIME
375
376   frame.workspace_label_w = 0;
377
378   for (unsigned int i = 0; i < screen->getWorkspaceCount(); i++) {
379     const string& workspace_name = screen->getWorkspace(i)->getName();
380     width = screen->getToolbarStyle()->font->measureString(workspace_name);
381     if (width > frame.workspace_label_w) frame.workspace_label_w = width;
382   }
383
384   frame.workspace_label_w = frame.clock_w =
385     std::max(frame.workspace_label_w, frame.clock_w) + (frame.bevel_w * 4);
386
387   // XXX: where'd the +6 come from?
388   frame.window_label_w =
389     (frame.rect.width() - (frame.clock_w + (frame.button_w * 4) +
390                            frame.workspace_label_w + (frame.bevel_w * 8) + 6));
391
392   if (hidden) {
393     XMoveResizeWindow(display, frame.window, frame.x_hidden, frame.y_hidden,
394                       frame.rect.width(), frame.rect.height());
395   } else {
396     XMoveResizeWindow(display, frame.window, frame.rect.x(), frame.rect.y(),
397                       frame.rect.width(), frame.rect.height());
398   }
399
400   XMoveResizeWindow(display, frame.workspace_label, frame.bevel_w,
401                     frame.bevel_w, frame.workspace_label_w,
402                     frame.label_h);
403   XMoveResizeWindow(display, frame.psbutton,
404                     ((frame.bevel_w * 2) + frame.workspace_label_w + 1),
405                     frame.bevel_w + 1, frame.button_w, frame.button_w);
406   XMoveResizeWindow(display, frame.nsbutton,
407                     ((frame.bevel_w * 3) + frame.workspace_label_w +
408                      frame.button_w + 2), frame.bevel_w + 1, frame.button_w,
409                     frame.button_w);
410   XMoveResizeWindow(display, frame.window_label,
411                     ((frame.bevel_w * 4) + (frame.button_w * 2) +
412                      frame.workspace_label_w + 3), frame.bevel_w,
413                     frame.window_label_w, frame.label_h);
414   XMoveResizeWindow(display, frame.pwbutton,
415                     ((frame.bevel_w * 5) + (frame.button_w * 2) +
416                      frame.workspace_label_w + frame.window_label_w + 4),
417                     frame.bevel_w + 1, frame.button_w, frame.button_w);
418   XMoveResizeWindow(display, frame.nwbutton,
419                     ((frame.bevel_w * 6) + (frame.button_w * 3) +
420                      frame.workspace_label_w + frame.window_label_w + 5),
421                     frame.bevel_w + 1, frame.button_w, frame.button_w);
422   XMoveResizeWindow(display, frame.clock,
423                     frame.rect.width() - frame.clock_w - (frame.bevel_w * 2),
424                     frame.bevel_w, frame.clock_w, frame.label_h);
425
426   ToolbarStyle *style = screen->getToolbarStyle();
427   frame.base = style->toolbar.render(frame.rect.width(), frame.rect.height(),
428                                      frame.base);
429   if (! frame.base)
430     XSetWindowBackground(display, frame.window,
431                          style->toolbar.color().pixel());
432   else
433     XSetWindowBackgroundPixmap(display, frame.window, frame.base);
434
435   frame.label = style->window.render(frame.window_label_w, frame.label_h,
436                                      frame.label);
437   if (! frame.label)
438     XSetWindowBackground(display, frame.window_label,
439                          style->window.color().pixel());
440   else
441     XSetWindowBackgroundPixmap(display, frame.window_label, frame.label);
442
443   frame.wlabel = style->label.render(frame.workspace_label_w, frame.label_h,
444                                 frame.wlabel);
445   if (! frame.wlabel)
446     XSetWindowBackground(display, frame.workspace_label,
447                          style->label.color().pixel());
448   else
449     XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
450
451   frame.clk = style->clock.render(frame.clock_w, frame.label_h, frame.clk);
452   if (! frame.clk)
453     XSetWindowBackground(display, frame.clock, style->clock.color().pixel());
454   else
455     XSetWindowBackgroundPixmap(display, frame.clock, frame.clk);
456
457   frame.button = style->button.render(frame.button_w, frame.button_w,
458                                 frame.button);
459   if (! frame.button) {
460     frame.button_pixel = style->button.color().pixel();
461     XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
462     XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
463     XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
464     XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
465   } else {
466     XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
467     XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
468     XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
469     XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
470   }
471
472   frame.pbutton = style->pressed.render(frame.button_w, frame.button_w,
473                                         frame.pbutton);
474   if (! frame.pbutton)
475     frame.pbutton_pixel = style->pressed.color().pixel();
476
477   XSetWindowBorder(display, frame.window,
478                    screen->getBorderColor()->pixel());
479   XSetWindowBorderWidth(display, frame.window, screen->getBorderWidth());
480
481   XClearWindow(display, frame.window);
482   XClearWindow(display, frame.workspace_label);
483   XClearWindow(display, frame.window_label);
484   XClearWindow(display, frame.clock);
485   XClearWindow(display, frame.psbutton);
486   XClearWindow(display, frame.nsbutton);
487   XClearWindow(display, frame.pwbutton);
488   XClearWindow(display, frame.nwbutton);
489
490   redrawWindowLabel();
491   redrawWorkspaceLabel();
492   redrawPrevWorkspaceButton();
493   redrawNextWorkspaceButton();
494   redrawPrevWindowButton();
495   redrawNextWindowButton();
496   checkClock(True);
497
498   toolbarmenu->reconfigure();
499 }
500
501
502 void Toolbar::updateStrut(void) {
503   // left and right are always 0
504   strut.top = strut.bottom = 0;
505
506   // when hidden only one border is visible
507   unsigned int border_width = screen->getBorderWidth();
508   if (! do_auto_hide)
509     border_width *= 2;
510
511   if (! screen->doHideToolbar()) {
512     switch(placement) {
513     case TopLeft:
514     case TopCenter:
515     case TopRight:
516       strut.top = getExposedHeight() + border_width;
517       break;
518     default:
519       strut.bottom = getExposedHeight() + border_width;
520     }
521   }
522
523   screen->updateAvailableArea();
524 }
525
526
527 #ifdef    HAVE_STRFTIME
528 void Toolbar::checkClock(bool redraw) {
529 #else // !HAVE_STRFTIME
530 void Toolbar::checkClock(bool redraw, bool date) {
531 #endif // HAVE_STRFTIME
532   time_t tmp = 0;
533   struct tm *tt = 0;
534
535   if ((tmp = time(NULL)) != -1) {
536     if (! (tt = localtime(&tmp))) return;
537     if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
538       frame.hour = tt->tm_hour;
539       frame.minute = tt->tm_min;
540       XClearWindow(display, frame.clock);
541       redraw = True;
542     }
543   }
544
545   if (redraw) {
546 #ifdef    HAVE_STRFTIME
547     char t[1024];
548     if (! strftime(t, 1024, screen->getStrftimeFormat(), tt))
549       return;
550 #else // !HAVE_STRFTIME
551     char t[9];
552     if (date) {
553       // format the date... with special consideration for y2k ;)
554       if (screen->getDateFormat() == Blackbox::B_EuropeanDate)
555         sprintf(t, 18n(ToolbarSet, ToolbarNoStrftimeDateFormatEu,
556                        "%02d.%02d.%02d"),
557                 tt->tm_mday, tt->tm_mon + 1,
558                 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
559       else
560         sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeDateFormat,
561                         "%02d/%02d/%02d"),
562                 tt->tm_mon + 1, tt->tm_mday,
563                 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
564     } else {
565       if (screen->isClock24Hour())
566         sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeTimeFormat24,
567                         "  %02d:%02d "),
568                 frame.hour, frame.minute);
569       else
570         sprintf(t, i18n(ToolbarSet, ToolbarNoStrftimeTimeFormat12,
571                         "%02d:%02d %sm"),
572                 ((frame.hour > 12) ? frame.hour - 12 :
573                  ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
574                 ((frame.hour >= 12) ?
575                  i18n(ToolbarSet, ToolbarNoStrftimeTimeFormatP, "p") :
576                  i18n(ToolbarSet, ToolbarNoStrftimeTimeFormatA, "a")));
577     }
578 #endif // HAVE_STRFTIME
579
580     ToolbarStyle *style = screen->getToolbarStyle();
581
582     int pos = frame.bevel_w * 2; // this is modified by doJustify()
583     style->doJustify(t, pos, frame.clock_w, frame.bevel_w * 4);
584     style->font->drawString(frame.clock, pos, 1, style->c_text, t);
585   }
586 }
587
588
589 void Toolbar::redrawWindowLabel(bool redraw) {
590   BlackboxWindow *foc = screen->getBlackbox()->getFocusedWindow();
591   if (! foc) {
592     XClearWindow(display, frame.window_label);
593     return;
594   }
595
596   if (redraw)
597     XClearWindow(display, frame.window_label);
598
599   if (foc->getScreen() != screen) return;
600
601   const char *title = foc->getTitle();
602   ToolbarStyle *style = screen->getToolbarStyle();
603
604   int pos = frame.bevel_w * 2; // modified by doJustify()
605   style->doJustify(title, pos, frame.window_label_w, frame.bevel_w * 4);
606   style->font->drawString(frame.window_label, pos, 1, style->w_text, title);
607 }
608
609
610 void Toolbar::redrawWorkspaceLabel(bool redraw) {
611   const string& name = screen->getCurrentWorkspace()->getName();
612
613   if (redraw)
614     XClearWindow(display, frame.workspace_label);
615
616   ToolbarStyle *style = screen->getToolbarStyle();
617
618   int pos = frame.bevel_w * 2;
619   style->doJustify(name.c_str(), pos, frame.workspace_label_w,
620                    frame.bevel_w * 4);
621   style->font->drawString(frame.workspace_label, pos, 1, style->l_text, name);
622 }
623
624
625 void Toolbar::drawArrow(Drawable surface, bool left) const {
626   ToolbarStyle *style = screen->getToolbarStyle();
627
628   BPen pen(style->b_pic);
629
630   int hh = frame.button_w / 2, hw = frame.button_w / 2;
631   XPoint pts[3];
632   const int bullet_size = 3;
633
634
635   if (left) {
636     if (style->left_button.mask == None) {
637       pts[0].x = hw - bullet_size;
638       pts[0].y = hh;
639       pts[1].x = 2 * bullet_size;
640       pts[1].y = bullet_size;
641       pts[2].x = 0;
642       pts[2].y = -(2 * bullet_size);
643       XFillPolygon(display, surface, pen.gc(), pts, 3, Convex,
644                    CoordModePrevious);
645     } else {
646       XSetClipMask(blackbox->getXDisplay(), pen.gc(), style->left_button.mask);
647       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
648                      (frame.button_w - style->left_button.w)/2,
649                      (frame.button_w - style->left_button.h)/2);
650
651       XFillRectangle(blackbox->getXDisplay(), surface, pen.gc(),
652                      (frame.button_w - style->left_button.w)/2,
653                      (frame.button_w - style->left_button.h)/2,
654                      (frame.button_w + style->left_button.w)/2,
655                      (frame.button_w + style->left_button.h)/2);
656
657       XSetClipMask(blackbox->getXDisplay(), pen.gc(), None);
658       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(), 0, 0);
659     }
660   } else {
661     if (style->right_button.mask == None) {
662       pts[0].x = hw - bullet_size;
663       pts[0].y = hh - bullet_size;
664       pts[1].x = (2 * bullet_size);
665       pts[1].y =  bullet_size;
666       pts[2].x = -(2 * bullet_size);
667       pts[2].y = bullet_size;
668       XFillPolygon(display, surface, pen.gc(), pts, 3, Convex,
669                    CoordModePrevious);
670     } else {
671       XSetClipMask(blackbox->getXDisplay(), pen.gc(),
672                    style->right_button.mask);
673       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
674                      (frame.button_w - style->right_button.w)/2,
675                      (frame.button_w - style->right_button.h)/2);
676
677       XFillRectangle(blackbox->getXDisplay(), surface, pen.gc(),
678                      (frame.button_w - style->right_button.w)/2,
679                      (frame.button_w - style->right_button.h)/2,
680                      (frame.button_w + style->right_button.w)/2,
681                      (frame.button_w + style->right_button.h)/2);
682
683       XSetClipMask(blackbox->getXDisplay(), pen.gc(), None);
684       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(), 0, 0);
685     }
686   }
687 }
688
689
690 void Toolbar::redrawPrevWorkspaceButton(bool pressed, bool redraw) {
691   if (redraw) {
692     if (pressed) {
693       if (frame.pbutton)
694         XSetWindowBackgroundPixmap(display, frame.psbutton, frame.pbutton);
695       else
696         XSetWindowBackground(display, frame.psbutton, frame.pbutton_pixel);
697     } else {
698       if (frame.button)
699         XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
700       else
701         XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
702     }
703     XClearWindow(display, frame.psbutton);
704   }
705
706   drawArrow(frame.psbutton, True);
707 }
708
709
710 void Toolbar::redrawNextWorkspaceButton(bool pressed, bool redraw) {
711   if (redraw) {
712     if (pressed) {
713       if (frame.pbutton)
714         XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.pbutton);
715       else
716         XSetWindowBackground(display, frame.nsbutton, frame.pbutton_pixel);
717     } else {
718       if (frame.button)
719         XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
720       else
721         XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
722     }
723     XClearWindow(display, frame.nsbutton);
724   }
725
726   drawArrow(frame.nsbutton, False);
727 }
728
729
730 void Toolbar::redrawPrevWindowButton(bool pressed, bool redraw) {
731   if (redraw) {
732     if (pressed) {
733       if (frame.pbutton)
734         XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.pbutton);
735       else
736         XSetWindowBackground(display, frame.pwbutton, frame.pbutton_pixel);
737     } else {
738       if (frame.button)
739         XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
740       else
741         XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
742     }
743     XClearWindow(display, frame.pwbutton);
744   }
745
746   drawArrow(frame.pwbutton, True);
747 }
748
749
750 void Toolbar::redrawNextWindowButton(bool pressed, bool redraw) {
751   if (redraw) {
752     if (pressed) {
753       if (frame.pbutton)
754         XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.pbutton);
755       else
756         XSetWindowBackground(display, frame.nwbutton, frame.pbutton_pixel);
757     } else {
758       if (frame.button)
759         XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
760       else
761         XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
762     }
763     XClearWindow(display, frame.nwbutton);
764   }
765
766   drawArrow(frame.nwbutton, False);
767 }
768
769
770 void Toolbar::edit(void) {
771   Window window;
772   int foo;
773
774   editing = True;
775   XGetInputFocus(display, &window, &foo);
776   if (window == frame.workspace_label)
777     return;
778
779   XSetInputFocus(display, frame.workspace_label,
780                  RevertToPointerRoot, CurrentTime);
781   XClearWindow(display, frame.workspace_label);
782
783   blackbox->setNoFocus(True);
784   if (blackbox->getFocusedWindow())
785     blackbox->getFocusedWindow()->setFocusFlag(False);
786
787   ToolbarStyle *style = screen->getToolbarStyle();
788   BPen pen(style->l_text);
789   XDrawRectangle(display, frame.workspace_label, pen.gc(),
790                  frame.workspace_label_w / 2, 0, 1,
791                  frame.label_h - 1);
792   // change the background of the window to that of an active window label
793   BTexture *texture = &(screen->getWindowStyle()->l_focus);
794   frame.wlabel = texture->render(frame.workspace_label_w, frame.label_h,
795                                  frame.wlabel);
796   if (! frame.wlabel)
797     XSetWindowBackground(display, frame.workspace_label,
798                          texture->color().pixel());
799   else
800     XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
801 }
802
803
804 void Toolbar::buttonPressEvent(const XButtonEvent *be) {
805   if (be->button == 1) {
806     if (be->window == frame.psbutton)
807       redrawPrevWorkspaceButton(True, True);
808     else if (be->window == frame.nsbutton)
809       redrawNextWorkspaceButton(True, True);
810     else if (be->window == frame.pwbutton)
811       redrawPrevWindowButton(True, True);
812     else if (be->window == frame.nwbutton)
813       redrawNextWindowButton(True, True);
814 #ifndef   HAVE_STRFTIME
815     else if (be->window == frame.clock) {
816       XClearWindow(display, frame.clock);
817       checkClock(True, True);
818     }
819 #endif // HAVE_STRFTIME
820     else if (! on_top) {
821       Window w[1] = { frame.window };
822       screen->raiseWindows(w, 1);
823     }
824   } else if (be->button == 2 && (! on_top)) {
825     XLowerWindow(display, frame.window);
826   } else if (be->button == 3) {
827     if (toolbarmenu->isVisible()) {
828       toolbarmenu->hide();
829     } else {
830       int x, y;
831
832       x = be->x_root - (toolbarmenu->getWidth() / 2);
833       y = be->y_root - (toolbarmenu->getHeight() / 2);
834
835       if (x < 0)
836         x = 0;
837       else if (x + toolbarmenu->getWidth() > screen->getWidth())
838         x = screen->getWidth() - toolbarmenu->getWidth();
839
840       if (y < 0)
841         y = 0;
842       else if (y + toolbarmenu->getHeight() > screen->getHeight())
843         y = screen->getHeight() - toolbarmenu->getHeight();
844
845       toolbarmenu->move(x, y);
846       toolbarmenu->show();
847     }
848   }
849 }
850
851
852
853 void Toolbar::buttonReleaseEvent(const XButtonEvent *re) {
854   if (re->button == 1) {
855     if (re->window == frame.psbutton) {
856       redrawPrevWorkspaceButton(False, True);
857
858       if (re->x >= 0 && re->x < static_cast<signed>(frame.button_w) &&
859           re->y >= 0 && re->y < static_cast<signed>(frame.button_w))
860        if (screen->getCurrentWorkspace()->getID() > 0)
861           screen->changeWorkspaceID(screen->getCurrentWorkspace()->
862                                     getID() - 1);
863         else
864           screen->changeWorkspaceID(screen->getWorkspaceCount() - 1);
865     } else if (re->window == frame.nsbutton) {
866       redrawNextWorkspaceButton(False, True);
867
868       if (re->x >= 0 && re->x < static_cast<signed>(frame.button_w) &&
869           re->y >= 0 && re->y < static_cast<signed>(frame.button_w))
870         if (screen->getCurrentWorkspace()->getID() <
871             (screen->getWorkspaceCount() - 1))
872           screen->changeWorkspaceID(screen->getCurrentWorkspace()->
873                                     getID() + 1);
874         else
875           screen->changeWorkspaceID(0);
876     } else if (re->window == frame.pwbutton) {
877       redrawPrevWindowButton(False, True);
878
879       if (re->x >= 0 && re->x < static_cast<signed>(frame.button_w) &&
880           re->y >= 0 && re->y < static_cast<signed>(frame.button_w))
881         screen->prevFocus();
882     } else if (re->window == frame.nwbutton) {
883       redrawNextWindowButton(False, True);
884
885       if (re->x >= 0 && re->x < static_cast<signed>(frame.button_w) &&
886           re->y >= 0 && re->y < static_cast<signed>(frame.button_w))
887         screen->nextFocus();
888     } else if (re->window == frame.window_label)
889       screen->raiseFocus();
890 #ifndef   HAVE_STRFTIME
891     else if (re->window == frame.clock) {
892       XClearWindow(display, frame.clock);
893       checkClock(True);
894     }
895 #endif // HAVE_STRFTIME
896   }
897 }
898
899
900 void Toolbar::enterNotifyEvent(const XCrossingEvent *) {
901   if (! do_auto_hide)
902     return;
903
904   if (hidden) {
905     if (! hide_timer->isTiming()) hide_timer->start();
906   } else {
907     if (hide_timer->isTiming()) hide_timer->stop();
908   }
909 }
910
911 void Toolbar::leaveNotifyEvent(const XCrossingEvent *) {
912   if (! do_auto_hide)
913     return;
914
915   if (hidden) {
916     if (hide_timer->isTiming()) hide_timer->stop();
917   } else if (! toolbarmenu->isVisible()) {
918     if (! hide_timer->isTiming()) hide_timer->start();
919   }
920 }
921
922
923 void Toolbar::exposeEvent(const XExposeEvent *ee) {
924   if (ee->window == frame.clock) checkClock(True);
925   else if (ee->window == frame.workspace_label && (! editing))
926     redrawWorkspaceLabel();
927   else if (ee->window == frame.window_label) redrawWindowLabel();
928   else if (ee->window == frame.psbutton) redrawPrevWorkspaceButton();
929   else if (ee->window == frame.nsbutton) redrawNextWorkspaceButton();
930   else if (ee->window == frame.pwbutton) redrawPrevWindowButton();
931   else if (ee->window == frame.nwbutton) redrawNextWindowButton();
932 }
933
934
935 void Toolbar::keyPressEvent(const XKeyEvent *ke) {
936   if (ke->window == frame.workspace_label && editing) {
937     if (new_workspace_name.empty()) {
938       new_name_pos = 0;
939     }
940
941     KeySym ks;
942     char keychar[1];
943     XLookupString(const_cast<XKeyEvent*>(ke), keychar, 1, &ks, 0);
944
945     // either we are told to end with a return or we hit 127 chars
946     if (ks == XK_Return || new_name_pos == 127) {
947       editing = False;
948
949       blackbox->setNoFocus(False);
950       if (blackbox->getFocusedWindow())
951         blackbox->getFocusedWindow()->setInputFocus();
952       else
953         blackbox->setFocusedWindow(0);
954
955       // the toolbar will be reconfigured when the change to the workspace name
956       // gets caught in the PropertyNotify event handler
957       screen->getCurrentWorkspace()->setName(new_workspace_name);
958
959       new_workspace_name.erase();
960       new_name_pos = 0;
961
962       // reset the background to that of the workspace label (its normal
963       // setting)
964       BTexture *texture = &(screen->getToolbarStyle()->label);
965       frame.wlabel = texture->render(frame.workspace_label_w, frame.label_h,
966                                      frame.wlabel);
967       if (! frame.wlabel)
968         XSetWindowBackground(display, frame.workspace_label,
969                              texture->color().pixel());
970       else
971         XSetWindowBackgroundPixmap(display, frame.workspace_label,
972                                    frame.wlabel);
973     } else if (! (ks == XK_Shift_L || ks == XK_Shift_R ||
974                   ks == XK_Control_L || ks == XK_Control_R ||
975                   ks == XK_Caps_Lock || ks == XK_Shift_Lock ||
976                   ks == XK_Meta_L || ks == XK_Meta_R ||
977                   ks == XK_Alt_L || ks == XK_Alt_R ||
978                   ks == XK_Super_L || ks == XK_Super_R ||
979                   ks == XK_Hyper_L || ks == XK_Hyper_R)) {
980       if (ks == XK_BackSpace) {
981         if (new_name_pos > 0) {
982           --new_name_pos;
983           new_workspace_name.erase(new_name_pos);
984         } else {
985           new_workspace_name.resize(0);
986         }
987       } else {
988         new_workspace_name += (*keychar);
989         ++new_name_pos;
990       }
991
992       XClearWindow(display, frame.workspace_label);
993       unsigned int tw, x;
994
995       tw = screen->getToolbarStyle()->font->measureString(new_workspace_name);
996       x = (frame.workspace_label_w - tw) / 2;
997
998       if (x < frame.bevel_w) x = frame.bevel_w;
999
1000       ToolbarStyle *style = screen->getToolbarStyle();
1001       style->font->drawString(frame.workspace_label, x, 1, style->l_text,
1002                               new_workspace_name);
1003       BPen pen(style->l_text);
1004       XDrawRectangle(display, frame.workspace_label, pen.gc(), x + tw, 0, 1,
1005                      frame.label_h - 1);
1006     }
1007   }
1008 }
1009
1010
1011 void Toolbar::timeout(void) {
1012   checkClock(True);
1013
1014   clock_timer->setTimeout(aMinuteFromNow());
1015 }
1016
1017
1018 void Toolbar::HideHandler::timeout(void) {
1019   toolbar->hidden = ! toolbar->hidden;
1020   if (toolbar->hidden)
1021     XMoveWindow(toolbar->display, toolbar->frame.window,
1022                 toolbar->frame.x_hidden, toolbar->frame.y_hidden);
1023   else
1024     XMoveWindow(toolbar->display, toolbar->frame.window,
1025                 toolbar->frame.rect.x(), toolbar->frame.rect.y());
1026 }
1027
1028
1029 void Toolbar::toggleAutoHide(void) {
1030   saveAutoHide(! doAutoHide());
1031
1032   updateStrut();
1033   screen->getSlit()->reposition();
1034
1035   if (do_auto_hide == False && hidden) {
1036     // force the slit to be visible
1037     if (hide_timer->isTiming()) hide_timer->stop();
1038     hide_handler.timeout();
1039   }
1040 }
1041
1042
1043 Toolbarmenu::Toolbarmenu(Toolbar *tb) : Basemenu(tb->screen) {
1044   toolbar = tb;
1045
1046   setLabel(i18n(ToolbarSet, ToolbarToolbarTitle, "Toolbar"));
1047   setInternalMenu();
1048
1049   placementmenu = new Placementmenu(this);
1050
1051   insert(i18n(CommonSet, CommonPlacementTitle, "Placement"),
1052          placementmenu);
1053   insert(i18n(CommonSet, CommonAlwaysOnTop, "Always on top"), 1);
1054   insert(i18n(CommonSet, CommonAutoHide, "Auto hide"), 2);
1055   insert(i18n(ToolbarSet, ToolbarEditWkspcName,
1056               "Edit current workspace name"), 3);
1057
1058   update();
1059   setValues();
1060 }
1061
1062
1063 void Toolbarmenu::setValues() {
1064   setItemSelected(1, toolbar->isOnTop());
1065   setItemSelected(2, toolbar->doAutoHide());
1066 }
1067
1068
1069 Toolbarmenu::~Toolbarmenu(void) {
1070   delete placementmenu;
1071 }
1072
1073
1074 void Toolbarmenu::itemSelected(int button, unsigned int index) {
1075   if (button != 1)
1076     return;
1077
1078   BasemenuItem *item = find(index);
1079   if (! item) return;
1080
1081   switch (item->function()) {
1082   case 1: { // always on top
1083     toolbar->saveOnTop(! toolbar->isOnTop());
1084     setItemSelected(1, toolbar->isOnTop());
1085
1086     if (toolbar->isOnTop()) getScreen()->raiseWindows((Window *) 0, 0);
1087     break;
1088   }
1089
1090   case 2: { // auto hide
1091     toolbar->toggleAutoHide();
1092     setItemSelected(2, toolbar->doAutoHide());
1093
1094     break;
1095   }
1096
1097   case 3: { // edit current workspace name
1098     toolbar->edit();
1099     hide();
1100
1101     break;
1102   }
1103   } // switch
1104 }
1105
1106
1107 void Toolbarmenu::internal_hide(void) {
1108   Basemenu::internal_hide();
1109   if (toolbar->doAutoHide() && ! toolbar->isEditing())
1110     toolbar->hide_handler.timeout();
1111 }
1112
1113
1114 void Toolbarmenu::reconfigure(void) {
1115   setValues();
1116   placementmenu->reconfigure();
1117
1118   Basemenu::reconfigure();
1119 }
1120
1121
1122 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu *tm)
1123   : Basemenu(tm->toolbar->screen), toolbar(tm->toolbar) {
1124   setLabel(i18n(ToolbarSet, ToolbarToolbarPlacement, "Toolbar Placement"));
1125   setInternalMenu();
1126   setMinimumSublevels(3);
1127
1128   insert(i18n(CommonSet, CommonPlacementTopLeft, "Top Left"),
1129          Toolbar::TopLeft);
1130   insert(i18n(CommonSet, CommonPlacementBottomLeft, "Bottom Left"),
1131          Toolbar::BottomLeft);
1132   insert(i18n(CommonSet, CommonPlacementTopCenter, "Top Center"),
1133          Toolbar::TopCenter);
1134   insert(i18n(CommonSet, CommonPlacementBottomCenter, "Bottom Center"),
1135          Toolbar::BottomCenter);
1136   insert(i18n(CommonSet, CommonPlacementTopRight, "Top Right"),
1137          Toolbar::TopRight);
1138   insert(i18n(CommonSet, CommonPlacementBottomRight, "Bottom Right"),
1139          Toolbar::BottomRight);
1140   update();
1141   setValues();
1142 }
1143
1144
1145 void Toolbarmenu::Placementmenu::setValues(void) {
1146   int place = 0;
1147   switch (toolbar->getPlacement()) {
1148   case Toolbar::BottomRight:
1149     place++;
1150   case Toolbar::TopRight:
1151     place++;
1152   case Toolbar::BottomCenter:
1153     place++;
1154   case Toolbar::TopCenter:
1155     place++;
1156   case Toolbar::BottomLeft:
1157     place++;
1158   case Toolbar::TopLeft:
1159     break;
1160   }
1161   setItemSelected(0, 0 == place);
1162   setItemSelected(1, 1 == place);
1163   setItemSelected(2, 2 == place);
1164   setItemSelected(3, 3 == place);
1165   setItemSelected(4, 4 == place);
1166   setItemSelected(5, 5 == place);
1167 }
1168
1169
1170 void Toolbarmenu::Placementmenu::reconfigure(void) {
1171   setValues();
1172   Basemenu::reconfigure();
1173 }
1174
1175
1176 void Toolbarmenu::Placementmenu::itemSelected(int button, unsigned int index) {
1177   if (button != 1)
1178     return;
1179
1180   BasemenuItem *item = find(index);
1181   if (! item) return;
1182
1183   toolbar->savePlacement(item->function());
1184   hide();
1185   toolbar->reconfigure();
1186
1187   // reposition the slit as well to make sure it doesn't intersect the
1188   // toolbar
1189   getScreen()->getSlit()->reposition();
1190 }
1191
1192
1193 void ToolbarStyle::doJustify(const std::string &text, int &start_pos,
1194                              unsigned int max_length,
1195                              unsigned int modifier) const {
1196   size_t text_len = text.size();
1197   unsigned int length;
1198
1199   do {
1200     length = font->measureString(string(text, 0, text_len)) + modifier;
1201   } while (length > max_length && text_len-- > 0);
1202
1203   switch (justify) {
1204   case RightJustify:
1205     start_pos += max_length - length;
1206     break;
1207
1208   case CenterJustify:
1209     start_pos += (max_length - length) / 2;
1210     break;
1211
1212   case LeftJustify:
1213   default:
1214     break;
1215   }
1216 }