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)
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:
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
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.
23 // stupid macros needed to access some functions in version 2 of the GNU C
30 # include "../config.h"
31 #endif // HAVE_CONFIG_H
35 #include "Clientmenu.h"
41 #include "Workspace.h"
42 #include "Workspacemenu.h"
44 #include <X11/Xutil.h>
45 #include <X11/keysym.h>
49 #endif // STDC_HEADERS
53 #endif // HAVE_STDIO_H
55 #ifdef TIME_WITH_SYS_TIME
56 # include <sys/time.h>
58 #else // !TIME_WITH_SYS_TIME
59 # ifdef HAVE_SYS_TIME_H
60 # include <sys/time.h>
61 # else // !HAVE_SYS_TIME_H
63 # endif // HAVE_SYS_TIME_H
64 #endif // TIME_WITH_SYS_TIME
67 Toolbar::Toolbar(BScreen *scrn) {
69 openbox = screen->getOpenbox();
71 // get the clock updating every minute
72 clock_timer = new BTimer(*openbox, *this);
74 gettimeofday(&now, 0);
75 clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
78 hide_handler.toolbar = this;
79 hide_timer = new BTimer(*openbox, hide_handler);
80 hide_timer->setTimeout(openbox->getAutoRaiseDelay());
81 hide_timer->fireOnce(True);
83 image_ctrl = screen->getImageControl();
85 on_top = screen->isToolbarOnTop();
86 hidden = do_auto_hide = screen->doToolbarAutoHide();
89 new_workspace_name = (char *) 0;
91 frame.grab_x = frame.grab_y = 0;
93 toolbarmenu = new Toolbarmenu(*this);
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;
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);
113 attrib.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask |
114 KeyPressMask | EnterWindowMask;
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);
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);
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);
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);
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);
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);
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);
151 frame.base = frame.label = frame.wlabel = frame.clk = frame.button =
152 frame.pbutton = None;
156 XMapSubwindows(display, frame.window);
157 XMapWindow(display, frame.window);
161 Toolbar::~Toolbar(void) {
162 XUnmapWindow(display, frame.window);
164 if (frame.base) image_ctrl->removeImage(frame.base);
165 if (frame.label) image_ctrl->removeImage(frame.label);
166 if (frame.wlabel) image_ctrl->removeImage(frame.wlabel);
167 if (frame.clk) image_ctrl->removeImage(frame.clk);
168 if (frame.button) image_ctrl->removeImage(frame.button);
169 if (frame.pbutton) image_ctrl->removeImage(frame.pbutton);
171 openbox->removeToolbarSearch(frame.window);
172 openbox->removeToolbarSearch(frame.workspace_label);
173 openbox->removeToolbarSearch(frame.window_label);
174 openbox->removeToolbarSearch(frame.clock);
175 openbox->removeToolbarSearch(frame.psbutton);
176 openbox->removeToolbarSearch(frame.nsbutton);
177 openbox->removeToolbarSearch(frame.pwbutton);
178 openbox->removeToolbarSearch(frame.nwbutton);
180 XDestroyWindow(display, frame.workspace_label);
181 XDestroyWindow(display, frame.window_label);
182 XDestroyWindow(display, frame.clock);
184 XDestroyWindow(display, frame.window);
192 void Toolbar::reconfigure(void) {
193 frame.bevel_w = screen->getBevelWidth();
194 frame.width = screen->getWidth() * screen->getToolbarWidthPercent() / 100;
196 if (i18n->multibyte())
198 screen->getToolbarStyle()->fontset_extents->max_ink_extent.height;
200 frame.height = screen->getToolbarStyle()->font->ascent +
201 screen->getToolbarStyle()->font->descent;
202 frame.button_w = frame.height;
204 frame.label_h = frame.height;
205 frame.height += (frame.bevel_w * 2);
207 switch (screen->getToolbarPlacement()) {
212 frame.y_hidden = screen->getBevelWidth() - screen->getBorderWidth()
218 frame.y = screen->getHeight() - frame.height
219 - (screen->getBorderWidth() * 2);
221 frame.y_hidden = screen->getHeight() - screen->getBevelWidth()
222 - screen->getBorderWidth();
226 frame.x = (screen->getWidth() - frame.width) / 2;
228 frame.x_hidden = frame.x;
229 frame.y_hidden = screen->getBevelWidth() - screen->getBorderWidth()
235 frame.x = (screen->getWidth() - frame.width) / 2;
236 frame.y = screen->getHeight() - frame.height
237 - (screen->getBorderWidth() * 2);
238 frame.x_hidden = frame.x;
239 frame.y_hidden = screen->getHeight() - screen->getBevelWidth()
240 - screen->getBorderWidth();
244 frame.x = screen->getWidth() - frame.width
245 - (screen->getBorderWidth() * 2);
247 frame.x_hidden = frame.x;
248 frame.y_hidden = screen->getBevelWidth() - screen->getBorderWidth()
253 frame.x = screen->getWidth() - frame.width
254 - (screen->getBorderWidth() * 2);
255 frame.y = screen->getHeight() - frame.height
256 - (screen->getBorderWidth() * 2);
257 frame.x_hidden = frame.x;
258 frame.y_hidden = screen->getHeight() - screen->getBevelWidth()
259 - screen->getBorderWidth();
264 time_t ttmp = time(NULL);
268 tt = localtime(&ttmp);
270 char t[1025], *time_string = (char *) 0;
271 int len = strftime(t, 1024, screen->getStrftimeFormat(), tt);
272 t[len++-1] = ' '; // add a space to the string for padding
275 if (i18n->multibyte()) {
276 XRectangle ink, logical;
277 XmbTextExtents(screen->getToolbarStyle()->fontset, t, len, &ink,
279 frame.clock_w = logical.width;
281 // ben's additional solution to pad some space beside the numbers
283 // screen->getToolbarStyle()->fontset_extents->max_logical_extent.width *
286 // brad's solution, which is currently buggy, too big
288 // screen->getToolbarStyle()->fontset_extents->max_logical_extent.width
291 frame.clock_w = XTextWidth(screen->getToolbarStyle()->font, t, len);
292 // ben's additional solution to pad some space beside the numbers
293 //frame.clock_w += screen->getToolbarStyle()->font->max_bounds.width * 4;
294 // brad's solution again, too big
295 //frame.clock_w = screen->getToolbarStyle()->font->max_bounds.width * len;
297 frame.clock_w += (frame.bevel_w * 4);
299 delete [] time_string;
306 #else // !HAVE_STRFTIME
308 XTextWidth(screen->getToolbarStyle()->font,
309 i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
311 strlen(i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
312 "00:00000"))) + (frame.bevel_w * 4);
313 #endif // HAVE_STRFTIME
317 frame.workspace_label_w = 0;
319 for (i = 0; i < screen->getWorkspaceCount(); i++) {
320 if (i18n->multibyte()) {
321 XRectangle ink, logical;
322 XmbTextExtents(screen->getToolbarStyle()->fontset,
323 screen->getWorkspace(i)->getName(),
324 strlen(screen->getWorkspace(i)->getName()),
328 w = XTextWidth(screen->getToolbarStyle()->font,
329 screen->getWorkspace(i)->getName(),
330 strlen(screen->getWorkspace(i)->getName()));
332 w += (frame.bevel_w * 4);
334 if (w > frame.workspace_label_w) frame.workspace_label_w = w;
337 if (frame.workspace_label_w < frame.clock_w)
338 frame.workspace_label_w = frame.clock_w;
339 else if (frame.workspace_label_w > frame.clock_w)
340 frame.clock_w = frame.workspace_label_w;
342 frame.window_label_w =
343 (frame.width - (frame.clock_w + (frame.button_w * 4) +
344 frame.workspace_label_w + (frame.bevel_w * 8) + 6));
347 XMoveResizeWindow(display, frame.window, frame.x_hidden, frame.y_hidden,
348 frame.width, frame.height);
350 XMoveResizeWindow(display, frame.window, frame.x, frame.y,
351 frame.width, frame.height);
354 XMoveResizeWindow(display, frame.workspace_label, frame.bevel_w,
355 frame.bevel_w, frame.workspace_label_w,
357 XMoveResizeWindow(display, frame.psbutton, (frame.bevel_w * 2) +
358 frame.workspace_label_w + 1, frame.bevel_w + 1,
359 frame.button_w, frame.button_w);
360 XMoveResizeWindow(display ,frame.nsbutton, (frame.bevel_w * 3) +
361 frame.workspace_label_w + frame.button_w + 2,
362 frame.bevel_w + 1, frame.button_w, frame.button_w);
363 XMoveResizeWindow(display, frame.window_label, (frame.bevel_w * 4) +
364 (frame.button_w * 2) + frame.workspace_label_w + 3,
365 frame.bevel_w, frame.window_label_w, frame.label_h);
366 XMoveResizeWindow(display, frame.pwbutton, (frame.bevel_w * 5) +
367 (frame.button_w * 2) + frame.workspace_label_w +
368 frame.window_label_w + 4, frame.bevel_w + 1,
369 frame.button_w, frame.button_w);
370 XMoveResizeWindow(display, frame.nwbutton, (frame.bevel_w * 6) +
371 (frame.button_w * 3) + frame.workspace_label_w +
372 frame.window_label_w + 5, frame.bevel_w + 1,
373 frame.button_w, frame.button_w);
374 XMoveResizeWindow(display, frame.clock, frame.width - frame.clock_w -
375 frame.bevel_w, frame.bevel_w, frame.clock_w,
378 Pixmap tmp = frame.base;
379 BTexture *texture = &(screen->getToolbarStyle()->toolbar);
380 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
382 XSetWindowBackground(display, frame.window,
383 texture->getColor()->getPixel());
386 image_ctrl->renderImage(frame.width, frame.height, texture);
387 XSetWindowBackgroundPixmap(display, frame.window, frame.base);
389 if (tmp) image_ctrl->removeImage(tmp);
392 texture = &(screen->getToolbarStyle()->window);
393 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
395 XSetWindowBackground(display, frame.window_label,
396 texture->getColor()->getPixel());
399 image_ctrl->renderImage(frame.window_label_w, frame.label_h, texture);
400 XSetWindowBackgroundPixmap(display, frame.window_label, frame.label);
402 if (tmp) image_ctrl->removeImage(tmp);
405 texture = &(screen->getToolbarStyle()->label);
406 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
408 XSetWindowBackground(display, frame.workspace_label,
409 texture->getColor()->getPixel());
412 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
413 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
415 if (tmp) image_ctrl->removeImage(tmp);
418 texture = &(screen->getToolbarStyle()->clock);
419 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
421 XSetWindowBackground(display, frame.clock,
422 texture->getColor()->getPixel());
425 image_ctrl->renderImage(frame.clock_w, frame.label_h, texture);
426 XSetWindowBackgroundPixmap(display, frame.clock, frame.clk);
428 if (tmp) image_ctrl->removeImage(tmp);
431 texture = &(screen->getToolbarStyle()->button);
432 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
435 frame.button_pixel = texture->getColor()->getPixel();
436 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
437 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
438 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
439 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
442 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
444 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
445 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
446 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
447 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
449 if (tmp) image_ctrl->removeImage(tmp);
452 texture = &(screen->getToolbarStyle()->pressed);
453 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
454 frame.pbutton = None;
455 frame.pbutton_pixel = texture->getColor()->getPixel();
458 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
460 if (tmp) image_ctrl->removeImage(tmp);
462 XSetWindowBorder(display, frame.window,
463 screen->getBorderColor()->getPixel());
464 XSetWindowBorderWidth(display, frame.window, screen->getBorderWidth());
466 XClearWindow(display, frame.window);
467 XClearWindow(display, frame.workspace_label);
468 XClearWindow(display, frame.window_label);
469 XClearWindow(display, frame.clock);
470 XClearWindow(display, frame.psbutton);
471 XClearWindow(display, frame.nsbutton);
472 XClearWindow(display, frame.pwbutton);
473 XClearWindow(display, frame.nwbutton);
476 redrawWorkspaceLabel();
477 redrawPrevWorkspaceButton();
478 redrawNextWorkspaceButton();
479 redrawPrevWindowButton();
480 redrawNextWindowButton();
483 toolbarmenu->reconfigure();
488 void Toolbar::checkClock(Bool redraw) {
489 #else // !HAVE_STRFTIME
490 void Toolbar::checkClock(Bool redraw, Bool date) {
491 #endif // HAVE_STRFTIME
495 if ((tmp = time(NULL)) != -1) {
496 if (! (tt = localtime(&tmp))) return;
497 if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
498 frame.hour = tt->tm_hour;
499 frame.minute = tt->tm_min;
500 XClearWindow(display, frame.clock);
508 if (! strftime(t, 1024, screen->getStrftimeFormat(), tt))
510 #else // !HAVE_STRFTIME
513 // format the date... with special consideration for y2k ;)
514 if (screen->getDateFormat() == Openbox::B_EuropeanDate)
515 sprintf(t, 18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormatEu,
517 tt->tm_mday, tt->tm_mon + 1,
518 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
520 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormat,
522 tt->tm_mon + 1, tt->tm_mday,
523 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
525 if (screen->isClock24Hour())
526 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat24,
528 frame.hour, frame.minute);
530 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat12,
532 ((frame.hour > 12) ? frame.hour - 12 :
533 ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
534 ((frame.hour >= 12) ?
535 i18n->getMessage(ToolbarSet,
536 ToolbarNoStrftimeTimeFormatP, "p") :
537 i18n->getMessage(ToolbarSet,
538 ToolbarNoStrftimeTimeFormatA, "a")));
540 #endif // HAVE_STRFTIME
542 int dx = (frame.bevel_w * 2), dlen = strlen(t);
545 if (i18n->multibyte()) {
546 XRectangle ink, logical;
547 XmbTextExtents(screen->getToolbarStyle()->fontset,
548 t, dlen, &ink, &logical);
551 l = XTextWidth(screen->getToolbarStyle()->font, t, dlen);
554 l += (frame.bevel_w * 4);
556 if (l > frame.clock_w) {
557 for (; dlen >= 0; dlen--) {
558 if (i18n->multibyte()) {
559 XRectangle ink, logical;
560 XmbTextExtents(screen->getToolbarStyle()->fontset,
561 t, dlen, &ink, &logical);
564 l = XTextWidth(screen->getToolbarStyle()->font, t, dlen);
566 l+= (frame.bevel_w * 4);
568 if (l < frame.clock_w)
572 switch (screen->getToolbarStyle()->justify) {
573 case BScreen::RightJustify:
574 dx += frame.clock_w - l;
577 case BScreen::CenterJustify:
578 dx += (frame.clock_w - l) / 2;
582 ToolbarStyle *style = screen->getToolbarStyle();
583 if (i18n->multibyte())
584 XmbDrawString(display, frame.clock, style->fontset, style->c_text_gc,
585 dx, (1 - style->fontset_extents->max_ink_extent.y),
588 XDrawString(display, frame.clock, style->c_text_gc, dx,
589 (style->font->ascent + 1), t, dlen);
594 void Toolbar::redrawWindowLabel(Bool redraw) {
595 if (screen->getOpenbox()->getFocusedWindow()) {
597 XClearWindow(display, frame.window_label);
599 OpenboxWindow *foc = screen->getOpenbox()->getFocusedWindow();
600 if (foc->getScreen() != screen) return;
602 int dx = (frame.bevel_w * 2), dlen = strlen(*foc->getTitle());
605 if (i18n->multibyte()) {
606 XRectangle ink, logical;
607 XmbTextExtents(screen->getToolbarStyle()->fontset, *foc->getTitle(),
608 dlen, &ink, &logical);
611 l = XTextWidth(screen->getToolbarStyle()->font, *foc->getTitle(), dlen);
613 l += (frame.bevel_w * 4);
615 if (l > frame.window_label_w) {
616 for (; dlen >= 0; dlen--) {
617 if (i18n->multibyte()) {
618 XRectangle ink, logical;
619 XmbTextExtents(screen->getToolbarStyle()->fontset,
620 *foc->getTitle(), dlen, &ink, &logical);
623 l = XTextWidth(screen->getToolbarStyle()->font,
624 *foc->getTitle(), dlen);
626 l += (frame.bevel_w * 4);
628 if (l < frame.window_label_w)
632 switch (screen->getToolbarStyle()->justify) {
633 case BScreen::RightJustify:
634 dx += frame.window_label_w - l;
637 case BScreen::CenterJustify:
638 dx += (frame.window_label_w - l) / 2;
642 ToolbarStyle *style = screen->getToolbarStyle();
643 if (i18n->multibyte())
644 XmbDrawString(display, frame.window_label, style->fontset,
645 style->w_text_gc, dx,
646 (1 - style->fontset_extents->max_ink_extent.y),
647 *foc->getTitle(), dlen);
649 XDrawString(display, frame.window_label, style->w_text_gc, dx,
650 (style->font->ascent + 1), *foc->getTitle(), dlen);
652 XClearWindow(display, frame.window_label);
657 void Toolbar::redrawWorkspaceLabel(Bool redraw) {
658 if (screen->getCurrentWorkspace()->getName()) {
660 XClearWindow(display, frame.workspace_label);
662 int dx = (frame.bevel_w * 2), dlen =
663 strlen(screen->getCurrentWorkspace()->getName());
666 if (i18n->multibyte()) {
667 XRectangle ink, logical;
668 XmbTextExtents(screen->getToolbarStyle()->fontset,
669 screen->getCurrentWorkspace()->getName(), dlen,
673 l = XTextWidth(screen->getToolbarStyle()->font,
674 screen->getCurrentWorkspace()->getName(), dlen);
676 l += (frame.bevel_w * 4);
678 if (l > frame.workspace_label_w) {
679 for (; dlen >= 0; dlen--) {
680 if (i18n->multibyte()) {
681 XRectangle ink, logical;
682 XmbTextExtents(screen->getToolbarStyle()->fontset,
683 screen->getCurrentWorkspace()->getName(), dlen,
687 l = XTextWidth(screen->getWindowStyle()->font,
688 screen->getCurrentWorkspace()->getName(), dlen);
690 l += (frame.bevel_w * 4);
692 if (l < frame.workspace_label_w)
696 switch (screen->getToolbarStyle()->justify) {
697 case BScreen::RightJustify:
698 dx += frame.workspace_label_w - l;
701 case BScreen::CenterJustify:
702 dx += (frame.workspace_label_w - l) / 2;
706 ToolbarStyle *style = screen->getToolbarStyle();
707 if (i18n->multibyte())
708 XmbDrawString(display, frame.workspace_label, style->fontset,
709 style->l_text_gc, dx,
710 (1 - style->fontset_extents->max_ink_extent.y),
711 (char *) screen->getCurrentWorkspace()->getName(), dlen);
713 XDrawString(display, frame.workspace_label, style->l_text_gc, dx,
714 (style->font->ascent + 1),
715 (char *) screen->getCurrentWorkspace()->getName(), dlen);
720 void Toolbar::redrawPrevWorkspaceButton(Bool pressed, Bool redraw) {
724 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.pbutton);
726 XSetWindowBackground(display, frame.psbutton, frame.pbutton_pixel);
729 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
731 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
733 XClearWindow(display, frame.psbutton);
736 int hh = frame.button_w / 2, hw = frame.button_w / 2;
739 pts[0].x = hw - 2; pts[0].y = hh;
740 pts[1].x = 4; pts[1].y = 2;
741 pts[2].x = 0; pts[2].y = -4;
743 XFillPolygon(display, frame.psbutton, screen->getToolbarStyle()->b_pic_gc,
744 pts, 3, Convex, CoordModePrevious);
748 void Toolbar::redrawNextWorkspaceButton(Bool pressed, Bool redraw) {
752 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.pbutton);
754 XSetWindowBackground(display, frame.nsbutton, frame.pbutton_pixel);
757 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
759 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
761 XClearWindow(display, frame.nsbutton);
764 int hh = frame.button_w / 2, hw = frame.button_w / 2;
767 pts[0].x = hw - 2; pts[0].y = hh - 2;
768 pts[1].x = 4; pts[1].y = 2;
769 pts[2].x = -4; pts[2].y = 2;
771 XFillPolygon(display, frame.nsbutton, screen->getToolbarStyle()->b_pic_gc,
772 pts, 3, Convex, CoordModePrevious);
776 void Toolbar::redrawPrevWindowButton(Bool pressed, Bool redraw) {
780 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.pbutton);
782 XSetWindowBackground(display, frame.pwbutton, frame.pbutton_pixel);
785 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
787 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
789 XClearWindow(display, frame.pwbutton);
792 int hh = frame.button_w / 2, hw = frame.button_w / 2;
795 pts[0].x = hw - 2; pts[0].y = hh;
796 pts[1].x = 4; pts[1].y = 2;
797 pts[2].x = 0; pts[2].y = -4;
799 XFillPolygon(display, frame.pwbutton, screen->getToolbarStyle()->b_pic_gc,
800 pts, 3, Convex, CoordModePrevious);
804 void Toolbar::redrawNextWindowButton(Bool pressed, Bool redraw) {
808 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.pbutton);
810 XSetWindowBackground(display, frame.nwbutton, frame.pbutton_pixel);
813 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
815 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
817 XClearWindow(display, frame.nwbutton);
820 int hh = frame.button_w / 2, hw = frame.button_w / 2;
823 pts[0].x = hw - 2; pts[0].y = hh - 2;
824 pts[1].x = 4; pts[1].y = 2;
825 pts[2].x = -4; pts[2].y = 2;
827 XFillPolygon(display, frame.nwbutton, screen->getToolbarStyle()->b_pic_gc,
828 pts, 3, Convex, CoordModePrevious);
832 void Toolbar::edit(void) {
837 if (XGetInputFocus(display, &window, &foo) &&
838 window == frame.workspace_label)
841 XSetInputFocus(display, frame.workspace_label,
842 ((screen->isSloppyFocus()) ? RevertToPointerRoot :
845 XClearWindow(display, frame.workspace_label);
847 openbox->setNoFocus(True);
848 if (openbox->getFocusedWindow())
849 openbox->getFocusedWindow()->setFocusFlag(False);
851 XDrawRectangle(display, frame.workspace_label,
852 screen->getWindowStyle()->l_text_focus_gc,
853 frame.workspace_label_w / 2, 0, 1,
856 // change the background of the window to that of an active window label
857 Pixmap tmp = frame.wlabel;
858 BTexture *texture = &(screen->getWindowStyle()->l_focus);
859 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
861 XSetWindowBackground(display, frame.workspace_label,
862 texture->getColor()->getPixel());
865 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
866 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
868 if (tmp) image_ctrl->removeImage(tmp);
872 void Toolbar::buttonPressEvent(XButtonEvent *be) {
873 if (be->button == 1) {
874 if (be->window == frame.psbutton)
875 redrawPrevWorkspaceButton(True, True);
876 else if (be->window == frame.nsbutton)
877 redrawNextWorkspaceButton(True, True);
878 else if (be->window == frame.pwbutton)
879 redrawPrevWindowButton(True, True);
880 else if (be->window == frame.nwbutton)
881 redrawNextWindowButton(True, True);
882 #ifndef HAVE_STRFTIME
883 else if (be->window == frame.clock) {
884 XClearWindow(display, frame.clock);
885 checkClock(True, True);
887 #endif // HAVE_STRFTIME
889 Window w[1] = { frame.window };
890 screen->raiseWindows(w, 1);
892 } else if (be->button == 2 && (! on_top)) {
893 XLowerWindow(display, frame.window);
894 } else if (be->button == 3) {
895 if (! toolbarmenu->isVisible()) {
898 x = be->x_root - (toolbarmenu->getWidth() / 2);
899 y = be->y_root - (toolbarmenu->getHeight() / 2);
903 else if (x + toolbarmenu->getWidth() > screen->getWidth())
904 x = screen->getWidth() - toolbarmenu->getWidth();
908 else if (y + toolbarmenu->getHeight() > screen->getHeight())
909 y = screen->getHeight() - toolbarmenu->getHeight();
911 toolbarmenu->move(x, y);
920 void Toolbar::buttonReleaseEvent(XButtonEvent *re) {
921 if (re->button == 1) {
922 if (re->window == frame.psbutton) {
923 redrawPrevWorkspaceButton(False, True);
925 if (re->x >= 0 && re->x < (signed) frame.button_w &&
926 re->y >= 0 && re->y < (signed) frame.button_w)
927 if (screen->getCurrentWorkspace()->getWorkspaceID() > 0)
928 screen->changeWorkspaceID(screen->getCurrentWorkspace()->
929 getWorkspaceID() - 1);
931 screen->changeWorkspaceID(screen->getWorkspaceCount() - 1);
932 } else if (re->window == frame.nsbutton) {
933 redrawNextWorkspaceButton(False, True);
935 if (re->x >= 0 && re->x < (signed) frame.button_w &&
936 re->y >= 0 && re->y < (signed) frame.button_w)
937 if (screen->getCurrentWorkspace()->getWorkspaceID() <
938 screen->getWorkspaceCount() - 1)
939 screen->changeWorkspaceID(screen->getCurrentWorkspace()->
940 getWorkspaceID() + 1);
942 screen->changeWorkspaceID(0);
943 } else if (re->window == frame.pwbutton) {
944 redrawPrevWindowButton(False, True);
946 if (re->x >= 0 && re->x < (signed) frame.button_w &&
947 re->y >= 0 && re->y < (signed) frame.button_w)
949 } else if (re->window == frame.nwbutton) {
950 redrawNextWindowButton(False, True);
952 if (re->x >= 0 && re->x < (signed) frame.button_w &&
953 re->y >= 0 && re->y < (signed) frame.button_w)
955 } else if (re->window == frame.window_label)
956 screen->raiseFocus();
957 #ifndef HAVE_STRFTIME
958 else if (re->window == frame.clock) {
959 XClearWindow(display, frame.clock);
962 #endif // HAVE_STRFTIME
967 void Toolbar::enterNotifyEvent(XCrossingEvent *) {
972 if (! hide_timer->isTiming()) hide_timer->start();
974 if (hide_timer->isTiming()) hide_timer->stop();
978 void Toolbar::leaveNotifyEvent(XCrossingEvent *) {
983 if (hide_timer->isTiming()) hide_timer->stop();
984 } else if (! toolbarmenu->isVisible()) {
985 if (! hide_timer->isTiming()) hide_timer->start();
990 void Toolbar::exposeEvent(XExposeEvent *ee) {
991 if (ee->window == frame.clock) checkClock(True);
992 else if (ee->window == frame.workspace_label && (! editing))
993 redrawWorkspaceLabel();
994 else if (ee->window == frame.window_label) redrawWindowLabel();
995 else if (ee->window == frame.psbutton) redrawPrevWorkspaceButton();
996 else if (ee->window == frame.nsbutton) redrawNextWorkspaceButton();
997 else if (ee->window == frame.pwbutton) redrawPrevWindowButton();
998 else if (ee->window == frame.nwbutton) redrawNextWindowButton();
1002 void Toolbar::keyPressEvent(XKeyEvent *ke) {
1003 if (ke->window == frame.workspace_label && editing) {
1006 if (! new_workspace_name) {
1007 new_workspace_name = new char[128];
1010 if (! new_workspace_name) return;
1015 XLookupString(ke, keychar, 1, &ks, 0);
1017 // either we are told to end with a return or we hit the end of the buffer
1018 if (ks == XK_Return || new_name_pos == 127) {
1019 *(new_workspace_name + new_name_pos) = 0;
1023 openbox->setNoFocus(False);
1024 if (openbox->getFocusedWindow()) {
1025 openbox->getFocusedWindow()->setInputFocus();
1026 openbox->getFocusedWindow()->setFocusFlag(True);
1028 XSetInputFocus(display, PointerRoot, None, CurrentTime);
1030 // check to make sure that new_name[0] != 0... otherwise we have a null
1031 // workspace name which causes serious problems, especially for the
1032 // Openbox::LoadRC() method.
1033 if (*new_workspace_name) {
1034 screen->getCurrentWorkspace()->setName(new_workspace_name);
1035 screen->getCurrentWorkspace()->getMenu()->hide();
1036 screen->getWorkspacemenu()->
1037 remove(screen->getCurrentWorkspace()->getWorkspaceID() + 2);
1038 screen->getWorkspacemenu()->
1039 insert(screen->getCurrentWorkspace()->getName(),
1040 screen->getCurrentWorkspace()->getMenu(),
1041 screen->getCurrentWorkspace()->getWorkspaceID() + 2);
1042 screen->getWorkspacemenu()->update();
1045 delete [] new_workspace_name;
1046 new_workspace_name = (char *) 0;
1049 // reset the background to that of the workspace label (its normal
1051 Pixmap tmp = frame.wlabel;
1052 BTexture *texture = &(screen->getToolbarStyle()->label);
1053 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
1054 frame.wlabel = None;
1055 XSetWindowBackground(display, frame.workspace_label,
1056 texture->getColor()->getPixel());
1059 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
1060 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
1062 if (tmp) image_ctrl->removeImage(tmp);
1065 } else if (! (ks == XK_Shift_L || ks == XK_Shift_R ||
1066 ks == XK_Control_L || ks == XK_Control_R ||
1067 ks == XK_Caps_Lock || ks == XK_Shift_Lock ||
1068 ks == XK_Meta_L || ks == XK_Meta_R ||
1069 ks == XK_Alt_L || ks == XK_Alt_R ||
1070 ks == XK_Super_L || ks == XK_Super_R ||
1071 ks == XK_Hyper_L || ks == XK_Hyper_R)) {
1072 if (ks == XK_BackSpace) {
1073 if (new_name_pos > 0) {
1075 *(new_workspace_name + new_name_pos) = '\0';
1077 *new_workspace_name = '\0';
1080 *(new_workspace_name + new_name_pos) = *keychar;
1082 *(new_workspace_name + new_name_pos) = '\0';
1085 XClearWindow(display, frame.workspace_label);
1086 int l = strlen(new_workspace_name), tw, x;
1088 if (i18n->multibyte()) {
1089 XRectangle ink, logical;
1090 XmbTextExtents(screen->getToolbarStyle()->fontset,
1091 new_workspace_name, l, &ink, &logical);
1094 tw = XTextWidth(screen->getToolbarStyle()->font,
1095 new_workspace_name, l);
1097 x = (frame.workspace_label_w - tw) / 2;
1099 if (x < (signed) frame.bevel_w) x = frame.bevel_w;
1101 WindowStyle *style = screen->getWindowStyle();
1102 if (i18n->multibyte())
1103 XmbDrawString(display, frame.workspace_label, style->fontset,
1104 style->l_text_focus_gc, x,
1105 (1 - style->fontset_extents->max_ink_extent.y),
1106 new_workspace_name, l);
1108 XDrawString(display, frame.workspace_label, style->l_text_focus_gc, x,
1109 (style->font->ascent + 1),
1110 new_workspace_name, l);
1112 XDrawRectangle(display, frame.workspace_label,
1113 screen->getWindowStyle()->l_text_focus_gc, x + tw, 0, 1,
1122 void Toolbar::timeout(void) {
1126 gettimeofday(&now, 0);
1127 clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
1131 void Toolbar::HideHandler::timeout(void) {
1132 toolbar->hidden = ! toolbar->hidden;
1133 if (toolbar->hidden)
1134 XMoveWindow(toolbar->display, toolbar->frame.window,
1135 toolbar->frame.x_hidden, toolbar->frame.y_hidden);
1137 XMoveWindow(toolbar->display, toolbar->frame.window,
1138 toolbar->frame.x, toolbar->frame.y);
1142 Toolbarmenu::Toolbarmenu(Toolbar &tb) : Basemenu(*tb.screen), toolbar(tb) {
1143 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarTitle, "Toolbar"));
1146 placementmenu = new Placementmenu(*this);
1148 insert(i18n->getMessage(CommonSet, CommonPlacementTitle, "Placement"),
1150 insert(i18n->getMessage(CommonSet, CommonAlwaysOnTop, "Always on top"), 1);
1151 insert(i18n->getMessage(CommonSet, CommonAutoHide, "Auto hide"), 2);
1152 insert(i18n->getMessage(ToolbarSet, ToolbarEditWkspcName,
1153 "Edit current workspace name"), 3);
1157 if (toolbar.isOnTop()) setItemSelected(1, True);
1158 if (toolbar.doAutoHide()) setItemSelected(2, True);
1162 Toolbarmenu::~Toolbarmenu(void) {
1163 delete placementmenu;
1167 void Toolbarmenu::itemSelected(int button, int index) {
1171 BasemenuItem *item = find(index);
1174 switch (item->function()) {
1175 case 1: { // always on top
1176 Bool change = ((toolbar.isOnTop()) ? False : True);
1177 toolbar.on_top = change;
1178 setItemSelected(1, change);
1180 if (toolbar.isOnTop()) toolbar.screen->raiseWindows((Window *) 0, 0);
1184 case 2: { // auto hide
1185 Bool change = ((toolbar.doAutoHide()) ? False : True);
1186 toolbar.do_auto_hide = change;
1187 setItemSelected(2, change);
1190 toolbar.screen->getSlit()->reposition();
1195 case 3: { // edit current workspace name
1205 void Toolbarmenu::internal_hide(void) {
1206 Basemenu::internal_hide();
1207 if (toolbar.doAutoHide() && ! toolbar.isEditing())
1208 toolbar.hide_handler.timeout();
1212 void Toolbarmenu::reconfigure(void) {
1213 placementmenu->reconfigure();
1215 Basemenu::reconfigure();
1219 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu &tm)
1220 : Basemenu(*tm.toolbar.screen), toolbarmenu(tm) {
1221 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarPlacement,
1222 "Toolbar Placement"));
1224 setMinimumSublevels(3);
1226 insert(i18n->getMessage(CommonSet, CommonPlacementTopLeft,
1227 "Top Left"), Toolbar::TopLeft);
1228 insert(i18n->getMessage(CommonSet, CommonPlacementBottomLeft,
1229 "Bottom Left"), Toolbar::BottomLeft);
1230 insert(i18n->getMessage(CommonSet, CommonPlacementTopCenter,
1231 "Top Center"), Toolbar::TopCenter);
1232 insert(i18n->getMessage(CommonSet, CommonPlacementBottomCenter,
1233 "Bottom Center"), Toolbar::BottomCenter);
1234 insert(i18n->getMessage(CommonSet, CommonPlacementTopRight,
1235 "Top Right"), Toolbar::TopRight);
1236 insert(i18n->getMessage(CommonSet, CommonPlacementBottomRight,
1237 "Bottom Right"), Toolbar::BottomRight);
1242 void Toolbarmenu::Placementmenu::itemSelected(int button, int index) {
1246 BasemenuItem *item = find(index);
1249 toolbarmenu.toolbar.screen->saveToolbarPlacement(item->function());
1251 toolbarmenu.toolbar.reconfigure();
1254 // reposition the slit as well to make sure it doesn't intersect the
1256 toolbarmenu.toolbar.screen->getSlit()->reposition();