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
273 t[len++-1] = ' '; // add a space to the string for padding
276 if (i18n->multibyte()) {
277 XRectangle ink, logical;
278 XmbTextExtents(screen->getToolbarStyle()->fontset, t, len, &ink,
280 frame.clock_w = logical.width;
282 // ben's additional solution to pad some space beside the numbers
284 // screen->getToolbarStyle()->fontset_extents->max_logical_extent.width *
287 // brad's solution, which is currently buggy, too big
289 // screen->getToolbarStyle()->fontset_extents->max_logical_extent.width
292 frame.clock_w = XTextWidth(screen->getToolbarStyle()->font, t, len);
293 // ben's additional solution to pad some space beside the numbers
294 //frame.clock_w += screen->getToolbarStyle()->font->max_bounds.width * 4;
295 // brad's solution again, too big
296 //frame.clock_w = screen->getToolbarStyle()->font->max_bounds.width * len;
298 frame.clock_w += (frame.bevel_w * 4);
300 delete [] time_string;
307 #else // !HAVE_STRFTIME
309 XTextWidth(screen->getToolbarStyle()->font,
310 i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
312 strlen(i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
313 "00:00000"))) + (frame.bevel_w * 4);
314 #endif // HAVE_STRFTIME
318 frame.workspace_label_w = 0;
320 for (i = 0; i < screen->getWorkspaceCount(); i++) {
321 if (i18n->multibyte()) {
322 XRectangle ink, logical;
323 XmbTextExtents(screen->getToolbarStyle()->fontset,
324 screen->getWorkspace(i)->getName(),
325 strlen(screen->getWorkspace(i)->getName()),
329 w = XTextWidth(screen->getToolbarStyle()->font,
330 screen->getWorkspace(i)->getName(),
331 strlen(screen->getWorkspace(i)->getName()));
333 w += (frame.bevel_w * 4);
335 if (w > frame.workspace_label_w) frame.workspace_label_w = w;
338 if (frame.workspace_label_w < frame.clock_w)
339 frame.workspace_label_w = frame.clock_w;
340 else if (frame.workspace_label_w > frame.clock_w)
341 frame.clock_w = frame.workspace_label_w;
343 frame.window_label_w =
344 (frame.width - (frame.clock_w + (frame.button_w * 4) +
345 frame.workspace_label_w + (frame.bevel_w * 8) + 6));
348 XMoveResizeWindow(display, frame.window, frame.x_hidden, frame.y_hidden,
349 frame.width, frame.height);
351 XMoveResizeWindow(display, frame.window, frame.x, frame.y,
352 frame.width, frame.height);
355 XMoveResizeWindow(display, frame.workspace_label, frame.bevel_w,
356 frame.bevel_w, frame.workspace_label_w,
358 XMoveResizeWindow(display, frame.psbutton, (frame.bevel_w * 2) +
359 frame.workspace_label_w + 1, frame.bevel_w + 1,
360 frame.button_w, frame.button_w);
361 XMoveResizeWindow(display ,frame.nsbutton, (frame.bevel_w * 3) +
362 frame.workspace_label_w + frame.button_w + 2,
363 frame.bevel_w + 1, frame.button_w, frame.button_w);
364 XMoveResizeWindow(display, frame.window_label, (frame.bevel_w * 4) +
365 (frame.button_w * 2) + frame.workspace_label_w + 3,
366 frame.bevel_w, frame.window_label_w, frame.label_h);
367 XMoveResizeWindow(display, frame.pwbutton, (frame.bevel_w * 5) +
368 (frame.button_w * 2) + frame.workspace_label_w +
369 frame.window_label_w + 4, frame.bevel_w + 1,
370 frame.button_w, frame.button_w);
371 XMoveResizeWindow(display, frame.nwbutton, (frame.bevel_w * 6) +
372 (frame.button_w * 3) + frame.workspace_label_w +
373 frame.window_label_w + 5, frame.bevel_w + 1,
374 frame.button_w, frame.button_w);
375 XMoveResizeWindow(display, frame.clock, frame.width - frame.clock_w -
376 frame.bevel_w, frame.bevel_w, frame.clock_w,
379 Pixmap tmp = frame.base;
380 BTexture *texture = &(screen->getToolbarStyle()->toolbar);
381 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
383 XSetWindowBackground(display, frame.window,
384 texture->getColor()->getPixel());
387 image_ctrl->renderImage(frame.width, frame.height, texture);
388 XSetWindowBackgroundPixmap(display, frame.window, frame.base);
390 if (tmp) image_ctrl->removeImage(tmp);
393 texture = &(screen->getToolbarStyle()->window);
394 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
396 XSetWindowBackground(display, frame.window_label,
397 texture->getColor()->getPixel());
400 image_ctrl->renderImage(frame.window_label_w, frame.label_h, texture);
401 XSetWindowBackgroundPixmap(display, frame.window_label, frame.label);
403 if (tmp) image_ctrl->removeImage(tmp);
406 texture = &(screen->getToolbarStyle()->label);
407 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
409 XSetWindowBackground(display, frame.workspace_label,
410 texture->getColor()->getPixel());
413 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
414 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
416 if (tmp) image_ctrl->removeImage(tmp);
419 texture = &(screen->getToolbarStyle()->clock);
420 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
422 XSetWindowBackground(display, frame.clock,
423 texture->getColor()->getPixel());
426 image_ctrl->renderImage(frame.clock_w, frame.label_h, texture);
427 XSetWindowBackgroundPixmap(display, frame.clock, frame.clk);
429 if (tmp) image_ctrl->removeImage(tmp);
432 texture = &(screen->getToolbarStyle()->button);
433 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
436 frame.button_pixel = texture->getColor()->getPixel();
437 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
438 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
439 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
440 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
443 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
445 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
446 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
447 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
448 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
450 if (tmp) image_ctrl->removeImage(tmp);
453 texture = &(screen->getToolbarStyle()->pressed);
454 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
455 frame.pbutton = None;
456 frame.pbutton_pixel = texture->getColor()->getPixel();
459 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
461 if (tmp) image_ctrl->removeImage(tmp);
463 XSetWindowBorder(display, frame.window,
464 screen->getBorderColor()->getPixel());
465 XSetWindowBorderWidth(display, frame.window, screen->getBorderWidth());
467 XClearWindow(display, frame.window);
468 XClearWindow(display, frame.workspace_label);
469 XClearWindow(display, frame.window_label);
470 XClearWindow(display, frame.clock);
471 XClearWindow(display, frame.psbutton);
472 XClearWindow(display, frame.nsbutton);
473 XClearWindow(display, frame.pwbutton);
474 XClearWindow(display, frame.nwbutton);
477 redrawWorkspaceLabel();
478 redrawPrevWorkspaceButton();
479 redrawNextWorkspaceButton();
480 redrawPrevWindowButton();
481 redrawNextWindowButton();
484 toolbarmenu->reconfigure();
489 void Toolbar::checkClock(Bool redraw) {
490 #else // !HAVE_STRFTIME
491 void Toolbar::checkClock(Bool redraw, Bool date) {
492 #endif // HAVE_STRFTIME
496 if ((tmp = time(NULL)) != -1) {
497 if (! (tt = localtime(&tmp))) return;
498 if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
499 frame.hour = tt->tm_hour;
500 frame.minute = tt->tm_min;
501 XClearWindow(display, frame.clock);
509 if (! strftime(t, 1024, screen->getStrftimeFormat(), tt))
511 #else // !HAVE_STRFTIME
514 // format the date... with special consideration for y2k ;)
515 if (screen->getDateFormat() == Openbox::B_EuropeanDate)
516 sprintf(t, 18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormatEu,
518 tt->tm_mday, tt->tm_mon + 1,
519 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
521 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormat,
523 tt->tm_mon + 1, tt->tm_mday,
524 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
526 if (screen->isClock24Hour())
527 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat24,
529 frame.hour, frame.minute);
531 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat12,
533 ((frame.hour > 12) ? frame.hour - 12 :
534 ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
535 ((frame.hour >= 12) ?
536 i18n->getMessage(ToolbarSet,
537 ToolbarNoStrftimeTimeFormatP, "p") :
538 i18n->getMessage(ToolbarSet,
539 ToolbarNoStrftimeTimeFormatA, "a")));
541 #endif // HAVE_STRFTIME
543 int dx = (frame.bevel_w * 2), dlen = strlen(t);
546 if (i18n->multibyte()) {
547 XRectangle ink, logical;
548 XmbTextExtents(screen->getToolbarStyle()->fontset,
549 t, dlen, &ink, &logical);
552 l = XTextWidth(screen->getToolbarStyle()->font, t, dlen);
555 l += (frame.bevel_w * 4);
557 if (l > frame.clock_w) {
558 for (; dlen >= 0; dlen--) {
559 if (i18n->multibyte()) {
560 XRectangle ink, logical;
561 XmbTextExtents(screen->getToolbarStyle()->fontset,
562 t, dlen, &ink, &logical);
565 l = XTextWidth(screen->getToolbarStyle()->font, t, dlen);
567 l+= (frame.bevel_w * 4);
569 if (l < frame.clock_w)
573 switch (screen->getToolbarStyle()->justify) {
574 case BScreen::RightJustify:
575 dx += frame.clock_w - l;
578 case BScreen::CenterJustify:
579 dx += (frame.clock_w - l) / 2;
583 ToolbarStyle *style = screen->getToolbarStyle();
584 if (i18n->multibyte())
585 XmbDrawString(display, frame.clock, style->fontset, style->c_text_gc,
586 dx, (1 - style->fontset_extents->max_ink_extent.y),
589 XDrawString(display, frame.clock, style->c_text_gc, dx,
590 (style->font->ascent + 1), t, dlen);
595 void Toolbar::redrawWindowLabel(Bool redraw) {
596 if (screen->getOpenbox()->getFocusedWindow()) {
598 XClearWindow(display, frame.window_label);
600 OpenboxWindow *foc = screen->getOpenbox()->getFocusedWindow();
601 if (foc->getScreen() != screen) return;
603 int dx = (frame.bevel_w * 2), dlen = strlen(*foc->getTitle());
606 if (i18n->multibyte()) {
607 XRectangle ink, logical;
608 XmbTextExtents(screen->getToolbarStyle()->fontset, *foc->getTitle(),
609 dlen, &ink, &logical);
612 l = XTextWidth(screen->getToolbarStyle()->font, *foc->getTitle(), dlen);
614 l += (frame.bevel_w * 4);
616 if (l > frame.window_label_w) {
617 for (; dlen >= 0; dlen--) {
618 if (i18n->multibyte()) {
619 XRectangle ink, logical;
620 XmbTextExtents(screen->getToolbarStyle()->fontset,
621 *foc->getTitle(), dlen, &ink, &logical);
624 l = XTextWidth(screen->getToolbarStyle()->font,
625 *foc->getTitle(), dlen);
627 l += (frame.bevel_w * 4);
629 if (l < frame.window_label_w)
633 switch (screen->getToolbarStyle()->justify) {
634 case BScreen::RightJustify:
635 dx += frame.window_label_w - l;
638 case BScreen::CenterJustify:
639 dx += (frame.window_label_w - l) / 2;
643 ToolbarStyle *style = screen->getToolbarStyle();
644 if (i18n->multibyte())
645 XmbDrawString(display, frame.window_label, style->fontset,
646 style->w_text_gc, dx,
647 (1 - style->fontset_extents->max_ink_extent.y),
648 *foc->getTitle(), dlen);
650 XDrawString(display, frame.window_label, style->w_text_gc, dx,
651 (style->font->ascent + 1), *foc->getTitle(), dlen);
653 XClearWindow(display, frame.window_label);
658 void Toolbar::redrawWorkspaceLabel(Bool redraw) {
659 if (screen->getCurrentWorkspace()->getName()) {
661 XClearWindow(display, frame.workspace_label);
663 int dx = (frame.bevel_w * 2), dlen =
664 strlen(screen->getCurrentWorkspace()->getName());
667 if (i18n->multibyte()) {
668 XRectangle ink, logical;
669 XmbTextExtents(screen->getToolbarStyle()->fontset,
670 screen->getCurrentWorkspace()->getName(), dlen,
674 l = XTextWidth(screen->getToolbarStyle()->font,
675 screen->getCurrentWorkspace()->getName(), dlen);
677 l += (frame.bevel_w * 4);
679 if (l > frame.workspace_label_w) {
680 for (; dlen >= 0; dlen--) {
681 if (i18n->multibyte()) {
682 XRectangle ink, logical;
683 XmbTextExtents(screen->getToolbarStyle()->fontset,
684 screen->getCurrentWorkspace()->getName(), dlen,
688 l = XTextWidth(screen->getWindowStyle()->font,
689 screen->getCurrentWorkspace()->getName(), dlen);
691 l += (frame.bevel_w * 4);
693 if (l < frame.workspace_label_w)
697 switch (screen->getToolbarStyle()->justify) {
698 case BScreen::RightJustify:
699 dx += frame.workspace_label_w - l;
702 case BScreen::CenterJustify:
703 dx += (frame.workspace_label_w - l) / 2;
707 ToolbarStyle *style = screen->getToolbarStyle();
708 if (i18n->multibyte())
709 XmbDrawString(display, frame.workspace_label, style->fontset,
710 style->l_text_gc, dx,
711 (1 - style->fontset_extents->max_ink_extent.y),
712 (char *) screen->getCurrentWorkspace()->getName(), dlen);
714 XDrawString(display, frame.workspace_label, style->l_text_gc, dx,
715 (style->font->ascent + 1),
716 (char *) screen->getCurrentWorkspace()->getName(), dlen);
721 void Toolbar::redrawPrevWorkspaceButton(Bool pressed, Bool redraw) {
725 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.pbutton);
727 XSetWindowBackground(display, frame.psbutton, frame.pbutton_pixel);
730 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
732 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
734 XClearWindow(display, frame.psbutton);
737 int hh = frame.button_w / 2, hw = frame.button_w / 2;
740 pts[0].x = hw - 2; pts[0].y = hh;
741 pts[1].x = 4; pts[1].y = 2;
742 pts[2].x = 0; pts[2].y = -4;
744 XFillPolygon(display, frame.psbutton, screen->getToolbarStyle()->b_pic_gc,
745 pts, 3, Convex, CoordModePrevious);
749 void Toolbar::redrawNextWorkspaceButton(Bool pressed, Bool redraw) {
753 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.pbutton);
755 XSetWindowBackground(display, frame.nsbutton, frame.pbutton_pixel);
758 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
760 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
762 XClearWindow(display, frame.nsbutton);
765 int hh = frame.button_w / 2, hw = frame.button_w / 2;
768 pts[0].x = hw - 2; pts[0].y = hh - 2;
769 pts[1].x = 4; pts[1].y = 2;
770 pts[2].x = -4; pts[2].y = 2;
772 XFillPolygon(display, frame.nsbutton, screen->getToolbarStyle()->b_pic_gc,
773 pts, 3, Convex, CoordModePrevious);
777 void Toolbar::redrawPrevWindowButton(Bool pressed, Bool redraw) {
781 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.pbutton);
783 XSetWindowBackground(display, frame.pwbutton, frame.pbutton_pixel);
786 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
788 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
790 XClearWindow(display, frame.pwbutton);
793 int hh = frame.button_w / 2, hw = frame.button_w / 2;
796 pts[0].x = hw - 2; pts[0].y = hh;
797 pts[1].x = 4; pts[1].y = 2;
798 pts[2].x = 0; pts[2].y = -4;
800 XFillPolygon(display, frame.pwbutton, screen->getToolbarStyle()->b_pic_gc,
801 pts, 3, Convex, CoordModePrevious);
805 void Toolbar::redrawNextWindowButton(Bool pressed, Bool redraw) {
809 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.pbutton);
811 XSetWindowBackground(display, frame.nwbutton, frame.pbutton_pixel);
814 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
816 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
818 XClearWindow(display, frame.nwbutton);
821 int hh = frame.button_w / 2, hw = frame.button_w / 2;
824 pts[0].x = hw - 2; pts[0].y = hh - 2;
825 pts[1].x = 4; pts[1].y = 2;
826 pts[2].x = -4; pts[2].y = 2;
828 XFillPolygon(display, frame.nwbutton, screen->getToolbarStyle()->b_pic_gc,
829 pts, 3, Convex, CoordModePrevious);
833 void Toolbar::edit(void) {
838 if (XGetInputFocus(display, &window, &foo) &&
839 window == frame.workspace_label)
842 XSetInputFocus(display, frame.workspace_label,
843 ((screen->isSloppyFocus()) ? RevertToPointerRoot :
846 XClearWindow(display, frame.workspace_label);
848 openbox->setNoFocus(True);
849 if (openbox->getFocusedWindow())
850 openbox->getFocusedWindow()->setFocusFlag(False);
852 XDrawRectangle(display, frame.workspace_label,
853 screen->getWindowStyle()->l_text_focus_gc,
854 frame.workspace_label_w / 2, 0, 1,
857 // change the background of the window to that of an active window label
858 Pixmap tmp = frame.wlabel;
859 BTexture *texture = &(screen->getWindowStyle()->l_focus);
860 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
862 XSetWindowBackground(display, frame.workspace_label,
863 texture->getColor()->getPixel());
866 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
867 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
869 if (tmp) image_ctrl->removeImage(tmp);
873 void Toolbar::buttonPressEvent(XButtonEvent *be) {
874 if (be->button == 1) {
875 if (be->window == frame.psbutton)
876 redrawPrevWorkspaceButton(True, True);
877 else if (be->window == frame.nsbutton)
878 redrawNextWorkspaceButton(True, True);
879 else if (be->window == frame.pwbutton)
880 redrawPrevWindowButton(True, True);
881 else if (be->window == frame.nwbutton)
882 redrawNextWindowButton(True, True);
883 #ifndef HAVE_STRFTIME
884 else if (be->window == frame.clock) {
885 XClearWindow(display, frame.clock);
886 checkClock(True, True);
888 #endif // HAVE_STRFTIME
890 Window w[1] = { frame.window };
891 screen->raiseWindows(w, 1);
893 } else if (be->button == 2 && (! on_top)) {
894 XLowerWindow(display, frame.window);
895 } else if (be->button == 3) {
896 if (! toolbarmenu->isVisible()) {
899 x = be->x_root - (toolbarmenu->getWidth() / 2);
900 y = be->y_root - (toolbarmenu->getHeight() / 2);
904 else if (x + toolbarmenu->getWidth() > screen->getWidth())
905 x = screen->getWidth() - toolbarmenu->getWidth();
909 else if (y + toolbarmenu->getHeight() > screen->getHeight())
910 y = screen->getHeight() - toolbarmenu->getHeight();
912 toolbarmenu->move(x, y);
921 void Toolbar::buttonReleaseEvent(XButtonEvent *re) {
922 if (re->button == 1) {
923 if (re->window == frame.psbutton) {
924 redrawPrevWorkspaceButton(False, True);
926 if (re->x >= 0 && re->x < (signed) frame.button_w &&
927 re->y >= 0 && re->y < (signed) frame.button_w)
928 if (screen->getCurrentWorkspace()->getWorkspaceID() > 0)
929 screen->changeWorkspaceID(screen->getCurrentWorkspace()->
930 getWorkspaceID() - 1);
932 screen->changeWorkspaceID(screen->getWorkspaceCount() - 1);
933 } else if (re->window == frame.nsbutton) {
934 redrawNextWorkspaceButton(False, True);
936 if (re->x >= 0 && re->x < (signed) frame.button_w &&
937 re->y >= 0 && re->y < (signed) frame.button_w)
938 if (screen->getCurrentWorkspace()->getWorkspaceID() <
939 screen->getWorkspaceCount() - 1)
940 screen->changeWorkspaceID(screen->getCurrentWorkspace()->
941 getWorkspaceID() + 1);
943 screen->changeWorkspaceID(0);
944 } else if (re->window == frame.pwbutton) {
945 redrawPrevWindowButton(False, True);
947 if (re->x >= 0 && re->x < (signed) frame.button_w &&
948 re->y >= 0 && re->y < (signed) frame.button_w)
950 } else if (re->window == frame.nwbutton) {
951 redrawNextWindowButton(False, True);
953 if (re->x >= 0 && re->x < (signed) frame.button_w &&
954 re->y >= 0 && re->y < (signed) frame.button_w)
956 } else if (re->window == frame.window_label)
957 screen->raiseFocus();
958 #ifndef HAVE_STRFTIME
959 else if (re->window == frame.clock) {
960 XClearWindow(display, frame.clock);
963 #endif // HAVE_STRFTIME
968 void Toolbar::enterNotifyEvent(XCrossingEvent *) {
973 if (! hide_timer->isTiming()) hide_timer->start();
975 if (hide_timer->isTiming()) hide_timer->stop();
979 void Toolbar::leaveNotifyEvent(XCrossingEvent *) {
984 if (hide_timer->isTiming()) hide_timer->stop();
985 } else if (! toolbarmenu->isVisible()) {
986 if (! hide_timer->isTiming()) hide_timer->start();
991 void Toolbar::exposeEvent(XExposeEvent *ee) {
992 if (ee->window == frame.clock) checkClock(True);
993 else if (ee->window == frame.workspace_label && (! editing))
994 redrawWorkspaceLabel();
995 else if (ee->window == frame.window_label) redrawWindowLabel();
996 else if (ee->window == frame.psbutton) redrawPrevWorkspaceButton();
997 else if (ee->window == frame.nsbutton) redrawNextWorkspaceButton();
998 else if (ee->window == frame.pwbutton) redrawPrevWindowButton();
999 else if (ee->window == frame.nwbutton) redrawNextWindowButton();
1003 void Toolbar::keyPressEvent(XKeyEvent *ke) {
1004 if (ke->window == frame.workspace_label && editing) {
1007 if (! new_workspace_name) {
1008 new_workspace_name = new char[128];
1011 if (! new_workspace_name) return;
1016 XLookupString(ke, keychar, 1, &ks, 0);
1018 // either we are told to end with a return or we hit the end of the buffer
1019 if (ks == XK_Return || new_name_pos == 127) {
1020 *(new_workspace_name + new_name_pos) = 0;
1024 openbox->setNoFocus(False);
1025 if (openbox->getFocusedWindow()) {
1026 openbox->getFocusedWindow()->setInputFocus();
1027 openbox->getFocusedWindow()->setFocusFlag(True);
1029 XSetInputFocus(display, PointerRoot, None, CurrentTime);
1031 // check to make sure that new_name[0] != 0... otherwise we have a null
1032 // workspace name which causes serious problems, especially for the
1033 // Openbox::LoadRC() method.
1034 if (*new_workspace_name) {
1035 screen->getCurrentWorkspace()->setName(new_workspace_name);
1036 screen->getCurrentWorkspace()->getMenu()->hide();
1037 screen->getWorkspacemenu()->
1038 remove(screen->getCurrentWorkspace()->getWorkspaceID() + 2);
1039 screen->getWorkspacemenu()->
1040 insert(screen->getCurrentWorkspace()->getName(),
1041 screen->getCurrentWorkspace()->getMenu(),
1042 screen->getCurrentWorkspace()->getWorkspaceID() + 2);
1043 screen->getWorkspacemenu()->update();
1046 delete [] new_workspace_name;
1047 new_workspace_name = (char *) 0;
1050 // reset the background to that of the workspace label (its normal
1052 Pixmap tmp = frame.wlabel;
1053 BTexture *texture = &(screen->getToolbarStyle()->label);
1054 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
1055 frame.wlabel = None;
1056 XSetWindowBackground(display, frame.workspace_label,
1057 texture->getColor()->getPixel());
1060 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
1061 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
1063 if (tmp) image_ctrl->removeImage(tmp);
1066 } else if (! (ks == XK_Shift_L || ks == XK_Shift_R ||
1067 ks == XK_Control_L || ks == XK_Control_R ||
1068 ks == XK_Caps_Lock || ks == XK_Shift_Lock ||
1069 ks == XK_Meta_L || ks == XK_Meta_R ||
1070 ks == XK_Alt_L || ks == XK_Alt_R ||
1071 ks == XK_Super_L || ks == XK_Super_R ||
1072 ks == XK_Hyper_L || ks == XK_Hyper_R)) {
1073 if (ks == XK_BackSpace) {
1074 if (new_name_pos > 0) {
1076 *(new_workspace_name + new_name_pos) = '\0';
1078 *new_workspace_name = '\0';
1081 *(new_workspace_name + new_name_pos) = *keychar;
1083 *(new_workspace_name + new_name_pos) = '\0';
1086 XClearWindow(display, frame.workspace_label);
1087 int l = strlen(new_workspace_name), tw, x;
1089 if (i18n->multibyte()) {
1090 XRectangle ink, logical;
1091 XmbTextExtents(screen->getToolbarStyle()->fontset,
1092 new_workspace_name, l, &ink, &logical);
1095 tw = XTextWidth(screen->getToolbarStyle()->font,
1096 new_workspace_name, l);
1098 x = (frame.workspace_label_w - tw) / 2;
1100 if (x < (signed) frame.bevel_w) x = frame.bevel_w;
1102 WindowStyle *style = screen->getWindowStyle();
1103 if (i18n->multibyte())
1104 XmbDrawString(display, frame.workspace_label, style->fontset,
1105 style->l_text_focus_gc, x,
1106 (1 - style->fontset_extents->max_ink_extent.y),
1107 new_workspace_name, l);
1109 XDrawString(display, frame.workspace_label, style->l_text_focus_gc, x,
1110 (style->font->ascent + 1),
1111 new_workspace_name, l);
1113 XDrawRectangle(display, frame.workspace_label,
1114 screen->getWindowStyle()->l_text_focus_gc, x + tw, 0, 1,
1123 void Toolbar::timeout(void) {
1127 gettimeofday(&now, 0);
1128 clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
1132 void Toolbar::HideHandler::timeout(void) {
1133 toolbar->hidden = ! toolbar->hidden;
1134 if (toolbar->hidden)
1135 XMoveWindow(toolbar->display, toolbar->frame.window,
1136 toolbar->frame.x_hidden, toolbar->frame.y_hidden);
1138 XMoveWindow(toolbar->display, toolbar->frame.window,
1139 toolbar->frame.x, toolbar->frame.y);
1143 Toolbarmenu::Toolbarmenu(Toolbar &tb) : Basemenu(*tb.screen), toolbar(tb) {
1144 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarTitle, "Toolbar"));
1147 placementmenu = new Placementmenu(*this);
1149 insert(i18n->getMessage(CommonSet, CommonPlacementTitle, "Placement"),
1151 insert(i18n->getMessage(CommonSet, CommonAlwaysOnTop, "Always on top"), 1);
1152 insert(i18n->getMessage(CommonSet, CommonAutoHide, "Auto hide"), 2);
1153 insert(i18n->getMessage(ToolbarSet, ToolbarEditWkspcName,
1154 "Edit current workspace name"), 3);
1158 if (toolbar.isOnTop()) setItemSelected(1, True);
1159 if (toolbar.doAutoHide()) setItemSelected(2, True);
1163 Toolbarmenu::~Toolbarmenu(void) {
1164 delete placementmenu;
1168 void Toolbarmenu::itemSelected(int button, int index) {
1172 BasemenuItem *item = find(index);
1175 switch (item->function()) {
1176 case 1: { // always on top
1177 Bool change = ((toolbar.isOnTop()) ? False : True);
1178 toolbar.on_top = change;
1179 setItemSelected(1, change);
1181 if (toolbar.isOnTop()) toolbar.screen->raiseWindows((Window *) 0, 0);
1185 case 2: { // auto hide
1186 Bool change = ((toolbar.doAutoHide()) ? False : True);
1187 toolbar.do_auto_hide = change;
1188 setItemSelected(2, change);
1191 toolbar.screen->getSlit()->reposition();
1196 case 3: { // edit current workspace name
1206 void Toolbarmenu::internal_hide(void) {
1207 Basemenu::internal_hide();
1208 if (toolbar.doAutoHide() && ! toolbar.isEditing())
1209 toolbar.hide_handler.timeout();
1213 void Toolbarmenu::reconfigure(void) {
1214 placementmenu->reconfigure();
1216 Basemenu::reconfigure();
1220 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu &tm)
1221 : Basemenu(*tm.toolbar.screen), toolbarmenu(tm) {
1222 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarPlacement,
1223 "Toolbar Placement"));
1225 setMinimumSublevels(3);
1227 insert(i18n->getMessage(CommonSet, CommonPlacementTopLeft,
1228 "Top Left"), Toolbar::TopLeft);
1229 insert(i18n->getMessage(CommonSet, CommonPlacementBottomLeft,
1230 "Bottom Left"), Toolbar::BottomLeft);
1231 insert(i18n->getMessage(CommonSet, CommonPlacementTopCenter,
1232 "Top Center"), Toolbar::TopCenter);
1233 insert(i18n->getMessage(CommonSet, CommonPlacementBottomCenter,
1234 "Bottom Center"), Toolbar::BottomCenter);
1235 insert(i18n->getMessage(CommonSet, CommonPlacementTopRight,
1236 "Top Right"), Toolbar::TopRight);
1237 insert(i18n->getMessage(CommonSet, CommonPlacementBottomRight,
1238 "Bottom Right"), Toolbar::BottomRight);
1243 void Toolbarmenu::Placementmenu::itemSelected(int button, int index) {
1247 BasemenuItem *item = find(index);
1250 toolbarmenu.toolbar.screen->saveToolbarPlacement(item->function());
1252 toolbarmenu.toolbar.reconfigure();
1255 // reposition the slit as well to make sure it doesn't intersect the
1257 toolbarmenu.toolbar.screen->getSlit()->reposition();