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/keysym.h>
48 #endif // HAVE_STRING_H
52 #endif // HAVE_STDIO_H
54 #ifdef TIME_WITH_SYS_TIME
55 # include <sys/time.h>
57 #else // !TIME_WITH_SYS_TIME
58 # ifdef HAVE_SYS_TIME_H
59 # include <sys/time.h>
60 # else // !HAVE_SYS_TIME_H
62 # endif // HAVE_SYS_TIME_H
63 #endif // TIME_WITH_SYS_TIME
67 #endif // HAVE_SSTREAM
71 Toolbar::Toolbar(BScreen &scrn, Resource &conf) : screen(scrn),
72 openbox(scrn.getOpenbox()), config(conf)
76 // get the clock updating every minute
77 clock_timer = new BTimer(openbox, *this);
79 gettimeofday(&now, 0);
80 clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
83 hide_handler.toolbar = this;
84 hide_timer = new BTimer(openbox, hide_handler);
85 hide_timer->setTimeout(openbox.getAutoRaiseDelay());
86 hide_timer->fireOnce(True);
88 image_ctrl = screen.getImageControl();
91 new_workspace_name = (char *) 0;
93 frame.grab_x = frame.grab_y = 0;
95 toolbarmenu = new Toolbarmenu(*this);
97 display = openbox.getXDisplay();
98 XSetWindowAttributes attrib;
99 unsigned long create_mask = CWBackPixmap | CWBackPixel | CWBorderPixel |
100 CWColormap | CWOverrideRedirect | CWEventMask;
101 attrib.background_pixmap = None;
102 attrib.background_pixel = attrib.border_pixel =
103 screen.getBorderColor()->getPixel();
104 attrib.colormap = screen.getColormap();
105 attrib.override_redirect = True;
106 attrib.event_mask = ButtonPressMask | ButtonReleaseMask |
107 EnterWindowMask | LeaveWindowMask;
110 XCreateWindow(display, screen.getRootWindow(), 0, 0, 1, 1, 0,
111 screen.getDepth(), InputOutput, screen.getVisual(),
112 create_mask, &attrib);
113 openbox.saveToolbarSearch(frame.window, this);
115 attrib.event_mask = ButtonPressMask | ButtonReleaseMask | ExposureMask |
116 KeyPressMask | EnterWindowMask;
118 frame.workspace_label =
119 XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
120 InputOutput, screen.getVisual(), create_mask, &attrib);
121 openbox.saveToolbarSearch(frame.workspace_label, this);
124 XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
125 InputOutput, screen.getVisual(), create_mask, &attrib);
126 openbox.saveToolbarSearch(frame.window_label, this);
129 XCreateWindow(display, frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
130 InputOutput, screen.getVisual(), create_mask, &attrib);
131 openbox.saveToolbarSearch(frame.clock, this);
134 XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
135 InputOutput, screen.getVisual(), create_mask, &attrib);
136 openbox.saveToolbarSearch(frame.psbutton, this);
139 XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
140 InputOutput, screen.getVisual(), create_mask, &attrib);
141 openbox.saveToolbarSearch(frame.nsbutton, this);
144 XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
145 InputOutput, screen.getVisual(), create_mask, &attrib);
146 openbox.saveToolbarSearch(frame.pwbutton, this);
149 XCreateWindow(display ,frame.window, 0, 0, 1, 1, 0, screen.getDepth(),
150 InputOutput, screen.getVisual(), create_mask, &attrib);
151 openbox.saveToolbarSearch(frame.nwbutton, this);
153 frame.base = frame.label = frame.wlabel = frame.clk = frame.button =
154 frame.pbutton = None;
160 Rect Toolbar::area() const {
161 int x = ((m_hidden) ? frame.x_hidden : frame.x);
163 if (screen.hideToolbar()) y = screen.size().h();
164 else if (m_hidden) y = frame.y_hidden;
166 return Rect(x, y, frame.width, frame.height);
169 unsigned int Toolbar::getExposedHeight() const {
170 if (screen.hideToolbar()) return 0;
171 else if (m_autohide) return frame.bevel_w;
172 else return frame.height;
175 void Toolbar::mapToolbar(){
176 if (!screen.hideToolbar()) {
177 //not hidden, so windows should not maximize over the toolbar
178 XMapSubwindows(display, frame.window);
179 XMapWindow(display, frame.window);
183 void Toolbar::unMapToolbar(){
184 //hidden so we can maximize over the toolbar
185 XUnmapWindow(display, frame.window);
188 Toolbar::~Toolbar() {
190 if (frame.base) image_ctrl->removeImage(frame.base);
191 if (frame.label) image_ctrl->removeImage(frame.label);
192 if (frame.wlabel) image_ctrl->removeImage(frame.wlabel);
193 if (frame.clk) image_ctrl->removeImage(frame.clk);
194 if (frame.button) image_ctrl->removeImage(frame.button);
195 if (frame.pbutton) image_ctrl->removeImage(frame.pbutton);
197 openbox.removeToolbarSearch(frame.window);
198 openbox.removeToolbarSearch(frame.workspace_label);
199 openbox.removeToolbarSearch(frame.window_label);
200 openbox.removeToolbarSearch(frame.clock);
201 openbox.removeToolbarSearch(frame.psbutton);
202 openbox.removeToolbarSearch(frame.nsbutton);
203 openbox.removeToolbarSearch(frame.pwbutton);
204 openbox.removeToolbarSearch(frame.nwbutton);
206 XDestroyWindow(display, frame.workspace_label);
207 XDestroyWindow(display, frame.window_label);
208 XDestroyWindow(display, frame.clock);
210 XDestroyWindow(display, frame.window);
218 void Toolbar::setOnTop(bool b) {
220 std::ostringstream s;
221 s << "session.screen" << screen.getScreenNumber() << ".toolbar.onTop";
222 config.setValue(s.str(), m_ontop ? "True" : "False");
225 void Toolbar::setAutoHide(bool b) {
227 std::ostringstream s;
228 s << "session.screen" << screen.getScreenNumber() << ".toolbar.autoHide";
229 config.setValue(s.str(), m_autohide ? "True" : "False");
232 void Toolbar::setWidthPercent(int w) {
234 std::ostringstream s;
235 s << "session.screen" << screen.getScreenNumber() << ".toolbar.widthPercent"
237 config.setValue(s.str(), m_width_percent);
240 void Toolbar::setPlacement(int p) {
242 std::ostringstream s;
243 s << "session.screen" << screen.getScreenNumber() << ".toolbar.placement";
244 const char *placement;
245 switch (m_placement) {
246 case TopLeft: placement = "TopLeft"; break;
247 case BottomLeft: placement = "BottomLeft"; break;
248 case TopCenter: placement = "TopCenter"; break;
249 case TopRight: placement = "TopRight"; break;
250 case BottomRight: placement = "BottomRight"; break;
251 case BottomCenter: default: placement = "BottomCenter"; break;
253 config.setValue(s.str(), placement);
256 void Toolbar::save() {
258 setAutoHide(m_autohide);
259 setWidthPercent(m_width_percent);
260 setPlacement(m_placement);
263 void Toolbar::load() {
264 std::ostringstream rscreen, rname, rclass;
268 rscreen << "session.screen" << screen.getScreenNumber() << '.';
270 rname << rscreen.str() << "toolbar.widthPercent";
271 rclass << rscreen.str() << "Toolbar.WidthPercent";
272 if (config.getValue(rname.str(), rclass.str(), l) && (l > 0 && l <= 100))
277 rname.seekp(0); rclass.seekp(0);
278 rname << rscreen.str() << "toolbar.placement";
279 rclass << rscreen.str() << "Toolbar.Placement";
280 if (config.getValue(rname.str(), rclass.str(), s)) {
281 if (0 == strncasecmp(s.c_str(), "TopLeft", s.length()))
282 m_placement = TopLeft;
283 else if (0 == strncasecmp(s.c_str(), "BottomLeft", s.length()))
284 m_placement = BottomLeft;
285 else if (0 == strncasecmp(s.c_str(), "TopCenter", s.length()))
286 m_placement = TopCenter;
287 else if (0 == strncasecmp(s.c_str(), "TopRight", s.length()))
288 m_placement = TopRight;
289 else if ( 0 == strncasecmp(s.c_str(), "BottomRight", s.length()))
290 m_placement = BottomRight;
291 else if ( 0 == strncasecmp(s.c_str(), "BottomCenter", s.length()))
292 m_placement = BottomCenter;
294 m_placement = BottomCenter;
296 rname.seekp(0); rclass.seekp(0);
297 rname << rscreen.str() << "toolbar.onTop";
298 rclass << rscreen.str() << "Toolbar.OnTop";
299 if (config.getValue(rname.str(), rclass.str(), b))
304 rname.seekp(0); rclass.seekp(0);
305 rname << rscreen.str() << "toolbar.autoHide";
306 rclass << rscreen.str() << "Toolbar.AutoHide";
307 if (config.getValue(rname.str(), rclass.str(), b))
308 m_hidden = m_autohide = b;
310 m_hidden = m_autohide = false;
313 void Toolbar::reconfigure() {
314 frame.bevel_w = screen.getBevelWidth();
315 frame.width = screen.size().w() * m_width_percent / 100;
317 if (i18n->multibyte())
319 screen.getToolbarStyle()->fontset_extents->max_ink_extent.height;
321 frame.height = screen.getToolbarStyle()->font->ascent +
322 screen.getToolbarStyle()->font->descent;
323 frame.button_w = frame.height;
325 frame.label_h = frame.height;
326 frame.height += (frame.bevel_w * 2);
328 switch (m_placement) {
333 frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
339 frame.y = screen.size().h() - frame.height
340 - (screen.getBorderWidth() * 2);
342 frame.y_hidden = screen.size().h() - screen.getBevelWidth()
343 - screen.getBorderWidth();
347 frame.x = (screen.size().w() - frame.width) / 2;
349 frame.x_hidden = frame.x;
350 frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
356 frame.x = (screen.size().w() - frame.width) / 2;
357 frame.y = screen.size().h() - frame.height
358 - (screen.getBorderWidth() * 2);
359 frame.x_hidden = frame.x;
360 frame.y_hidden = screen.size().h() - screen.getBevelWidth()
361 - screen.getBorderWidth();
365 frame.x = screen.size().w() - frame.width
366 - (screen.getBorderWidth() * 2);
368 frame.x_hidden = frame.x;
369 frame.y_hidden = screen.getBevelWidth() - screen.getBorderWidth()
374 frame.x = screen.size().w() - frame.width
375 - (screen.getBorderWidth() * 2);
376 frame.y = screen.size().h() - frame.height
377 - (screen.getBorderWidth() * 2);
378 frame.x_hidden = frame.x;
379 frame.y_hidden = screen.size().h() - screen.getBevelWidth()
380 - screen.getBorderWidth();
385 time_t ttmp = time(NULL);
389 tt = localtime(&ttmp);
391 char t[1025], *time_string = (char *) 0;
392 int len = strftime(t, 1024, screen.strftimeFormat(), tt);
393 t[len++-1] = ' '; // add a space to the string for padding
396 if (i18n->multibyte()) {
397 XRectangle ink, logical;
398 XmbTextExtents(screen.getToolbarStyle()->fontset, t, len, &ink,
400 frame.clock_w = logical.width;
402 // ben's additional solution to pad some space beside the numbers
404 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width *
407 // brad's solution, which is currently buggy, too big
409 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width
412 frame.clock_w = XTextWidth(screen.getToolbarStyle()->font, t, len);
413 // ben's additional solution to pad some space beside the numbers
414 //frame.clock_w += screen.getToolbarStyle()->font->max_bounds.width * 4;
415 // brad's solution again, too big
416 //frame.clock_w = screen.getToolbarStyle()->font->max_bounds.width * len;
418 frame.clock_w += (frame.bevel_w * 4);
420 delete [] time_string;
427 #else // !HAVE_STRFTIME
429 XTextWidth(screen.getToolbarStyle()->font,
430 i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
432 strlen(i18n->getMessage(ToolbarSet, ToolbarNoStrftimeLength,
433 "00:00000"))) + (frame.bevel_w * 4);
434 #endif // HAVE_STRFTIME
438 frame.workspace_label_w = 0;
440 for (i = 0; i < screen.getWorkspaceCount(); i++) {
441 if (i18n->multibyte()) {
442 XRectangle ink, logical;
443 XmbTextExtents(screen.getToolbarStyle()->fontset,
444 screen.getWorkspace(i)->getName(),
445 strlen(screen.getWorkspace(i)->getName()),
449 w = XTextWidth(screen.getToolbarStyle()->font,
450 screen.getWorkspace(i)->getName(),
451 strlen(screen.getWorkspace(i)->getName()));
453 w += (frame.bevel_w * 4);
455 if (w > frame.workspace_label_w) frame.workspace_label_w = w;
458 if (frame.workspace_label_w < frame.clock_w)
459 frame.workspace_label_w = frame.clock_w;
460 else if (frame.workspace_label_w > frame.clock_w)
461 frame.clock_w = frame.workspace_label_w;
463 frame.window_label_w =
464 (frame.width - (frame.clock_w + (frame.button_w * 4) +
465 frame.workspace_label_w + (frame.bevel_w * 8) + 6));
468 XMoveResizeWindow(display, frame.window, frame.x_hidden, frame.y_hidden,
469 frame.width, frame.height);
471 XMoveResizeWindow(display, frame.window, frame.x, frame.y,
472 frame.width, frame.height);
475 XMoveResizeWindow(display, frame.workspace_label, frame.bevel_w,
476 frame.bevel_w, frame.workspace_label_w,
478 XMoveResizeWindow(display, frame.psbutton, (frame.bevel_w * 2) +
479 frame.workspace_label_w + 1, frame.bevel_w + 1,
480 frame.button_w, frame.button_w);
481 XMoveResizeWindow(display ,frame.nsbutton, (frame.bevel_w * 3) +
482 frame.workspace_label_w + frame.button_w + 2,
483 frame.bevel_w + 1, frame.button_w, frame.button_w);
484 XMoveResizeWindow(display, frame.window_label, (frame.bevel_w * 4) +
485 (frame.button_w * 2) + frame.workspace_label_w + 3,
486 frame.bevel_w, frame.window_label_w, frame.label_h);
487 XMoveResizeWindow(display, frame.pwbutton, (frame.bevel_w * 5) +
488 (frame.button_w * 2) + frame.workspace_label_w +
489 frame.window_label_w + 4, frame.bevel_w + 1,
490 frame.button_w, frame.button_w);
491 XMoveResizeWindow(display, frame.nwbutton, (frame.bevel_w * 6) +
492 (frame.button_w * 3) + frame.workspace_label_w +
493 frame.window_label_w + 5, frame.bevel_w + 1,
494 frame.button_w, frame.button_w);
495 XMoveResizeWindow(display, frame.clock, frame.width - frame.clock_w -
496 frame.bevel_w, frame.bevel_w, frame.clock_w,
499 Pixmap tmp = frame.base;
500 BTexture *texture = &(screen.getToolbarStyle()->toolbar);
501 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
503 XSetWindowBackground(display, frame.window,
504 texture->getColor()->getPixel());
507 image_ctrl->renderImage(frame.width, frame.height, texture);
508 XSetWindowBackgroundPixmap(display, frame.window, frame.base);
510 if (tmp) image_ctrl->removeImage(tmp);
513 texture = &(screen.getToolbarStyle()->window);
514 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
516 XSetWindowBackground(display, frame.window_label,
517 texture->getColor()->getPixel());
520 image_ctrl->renderImage(frame.window_label_w, frame.label_h, texture);
521 XSetWindowBackgroundPixmap(display, frame.window_label, frame.label);
523 if (tmp) image_ctrl->removeImage(tmp);
526 texture = &(screen.getToolbarStyle()->label);
527 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
529 XSetWindowBackground(display, frame.workspace_label,
530 texture->getColor()->getPixel());
533 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
534 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
536 if (tmp) image_ctrl->removeImage(tmp);
539 texture = &(screen.getToolbarStyle()->clock);
540 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
542 XSetWindowBackground(display, frame.clock,
543 texture->getColor()->getPixel());
546 image_ctrl->renderImage(frame.clock_w, frame.label_h, texture);
547 XSetWindowBackgroundPixmap(display, frame.clock, frame.clk);
549 if (tmp) image_ctrl->removeImage(tmp);
552 texture = &(screen.getToolbarStyle()->button);
553 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
556 frame.button_pixel = texture->getColor()->getPixel();
557 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
558 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
559 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
560 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
563 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
565 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
566 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
567 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
568 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
570 if (tmp) image_ctrl->removeImage(tmp);
573 texture = &(screen.getToolbarStyle()->pressed);
574 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
575 frame.pbutton = None;
576 frame.pbutton_pixel = texture->getColor()->getPixel();
579 image_ctrl->renderImage(frame.button_w, frame.button_w, texture);
581 if (tmp) image_ctrl->removeImage(tmp);
583 XSetWindowBorder(display, frame.window,
584 screen.getBorderColor()->getPixel());
585 XSetWindowBorderWidth(display, frame.window, screen.getBorderWidth());
587 XClearWindow(display, frame.window);
588 XClearWindow(display, frame.workspace_label);
589 XClearWindow(display, frame.window_label);
590 XClearWindow(display, frame.clock);
591 XClearWindow(display, frame.psbutton);
592 XClearWindow(display, frame.nsbutton);
593 XClearWindow(display, frame.pwbutton);
594 XClearWindow(display, frame.nwbutton);
597 redrawWorkspaceLabel();
598 redrawPrevWorkspaceButton();
599 redrawNextWorkspaceButton();
600 redrawPrevWindowButton();
601 redrawNextWindowButton();
604 toolbarmenu->reconfigure();
609 void Toolbar::checkClock(Bool redraw) {
610 #else // !HAVE_STRFTIME
611 void Toolbar::checkClock(Bool redraw, Bool date) {
612 #endif // HAVE_STRFTIME
616 if ((tmp = time(NULL)) != -1) {
617 if (! (tt = localtime(&tmp))) return;
618 if (tt->tm_min != frame.minute || tt->tm_hour != frame.hour) {
619 frame.hour = tt->tm_hour;
620 frame.minute = tt->tm_min;
621 XClearWindow(display, frame.clock);
629 if (! strftime(t, 1024, screen.strftimeFormat(), tt))
631 #else // !HAVE_STRFTIME
634 // format the date... with special consideration for y2k ;)
635 if (screen.getDateFormat() == Openbox::B_EuropeanDate)
636 sprintf(t, 18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormatEu,
638 tt->tm_mday, tt->tm_mon + 1,
639 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
641 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeDateFormat,
643 tt->tm_mon + 1, tt->tm_mday,
644 (tt->tm_year >= 100) ? tt->tm_year - 100 : tt->tm_year);
646 if (screen.isClock24Hour())
647 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat24,
649 frame.hour, frame.minute);
651 sprintf(t, i18n->getMessage(ToolbarSet, ToolbarNoStrftimeTimeFormat12,
653 ((frame.hour > 12) ? frame.hour - 12 :
654 ((frame.hour == 0) ? 12 : frame.hour)), frame.minute,
655 ((frame.hour >= 12) ?
656 i18n->getMessage(ToolbarSet,
657 ToolbarNoStrftimeTimeFormatP, "p") :
658 i18n->getMessage(ToolbarSet,
659 ToolbarNoStrftimeTimeFormatA, "a")));
661 #endif // HAVE_STRFTIME
663 int dx = (frame.bevel_w * 2), dlen = strlen(t);
666 if (i18n->multibyte()) {
667 XRectangle ink, logical;
668 XmbTextExtents(screen.getToolbarStyle()->fontset,
669 t, dlen, &ink, &logical);
672 l = XTextWidth(screen.getToolbarStyle()->font, t, dlen);
675 l += (frame.bevel_w * 4);
677 if (l > frame.clock_w) {
678 for (; dlen >= 0; dlen--) {
679 if (i18n->multibyte()) {
680 XRectangle ink, logical;
681 XmbTextExtents(screen.getToolbarStyle()->fontset,
682 t, dlen, &ink, &logical);
685 l = XTextWidth(screen.getToolbarStyle()->font, t, dlen);
687 l+= (frame.bevel_w * 4);
689 if (l < frame.clock_w)
693 switch (screen.getToolbarStyle()->justify) {
694 case BScreen::RightJustify:
695 dx += frame.clock_w - l;
698 case BScreen::CenterJustify:
699 dx += (frame.clock_w - l) / 2;
703 ToolbarStyle *style = screen.getToolbarStyle();
704 if (i18n->multibyte())
705 XmbDrawString(display, frame.clock, style->fontset, style->c_text_gc,
706 dx, (1 - style->fontset_extents->max_ink_extent.y),
709 XDrawString(display, frame.clock, style->c_text_gc, dx,
710 (style->font->ascent + 1), t, dlen);
715 void Toolbar::redrawWindowLabel(Bool redraw) {
716 if (screen.getOpenbox().getFocusedWindow()) {
718 XClearWindow(display, frame.window_label);
720 OpenboxWindow *foc = screen.getOpenbox().getFocusedWindow();
721 if (foc->getScreen() != &screen) return;
723 int dx = (frame.bevel_w * 2), dlen = strlen(*foc->getTitle());
726 if (i18n->multibyte()) {
727 XRectangle ink, logical;
728 XmbTextExtents(screen.getToolbarStyle()->fontset, *foc->getTitle(),
729 dlen, &ink, &logical);
732 l = XTextWidth(screen.getToolbarStyle()->font, *foc->getTitle(), dlen);
734 l += (frame.bevel_w * 4);
736 if (l > frame.window_label_w) {
737 for (; dlen >= 0; dlen--) {
738 if (i18n->multibyte()) {
739 XRectangle ink, logical;
740 XmbTextExtents(screen.getToolbarStyle()->fontset,
741 *foc->getTitle(), dlen, &ink, &logical);
744 l = XTextWidth(screen.getToolbarStyle()->font,
745 *foc->getTitle(), dlen);
747 l += (frame.bevel_w * 4);
749 if (l < frame.window_label_w)
753 switch (screen.getToolbarStyle()->justify) {
754 case BScreen::RightJustify:
755 dx += frame.window_label_w - l;
758 case BScreen::CenterJustify:
759 dx += (frame.window_label_w - l) / 2;
763 ToolbarStyle *style = screen.getToolbarStyle();
764 if (i18n->multibyte())
765 XmbDrawString(display, frame.window_label, style->fontset,
766 style->w_text_gc, dx,
767 (1 - style->fontset_extents->max_ink_extent.y),
768 *foc->getTitle(), dlen);
770 XDrawString(display, frame.window_label, style->w_text_gc, dx,
771 (style->font->ascent + 1), *foc->getTitle(), dlen);
773 XClearWindow(display, frame.window_label);
778 void Toolbar::redrawWorkspaceLabel(Bool redraw) {
779 if (screen.getCurrentWorkspace()->getName()) {
781 XClearWindow(display, frame.workspace_label);
783 int dx = (frame.bevel_w * 2), dlen =
784 strlen(screen.getCurrentWorkspace()->getName());
787 if (i18n->multibyte()) {
788 XRectangle ink, logical;
789 XmbTextExtents(screen.getToolbarStyle()->fontset,
790 screen.getCurrentWorkspace()->getName(), dlen,
794 l = XTextWidth(screen.getToolbarStyle()->font,
795 screen.getCurrentWorkspace()->getName(), dlen);
797 l += (frame.bevel_w * 4);
799 if (l > frame.workspace_label_w) {
800 for (; dlen >= 0; dlen--) {
801 if (i18n->multibyte()) {
802 XRectangle ink, logical;
803 XmbTextExtents(screen.getToolbarStyle()->fontset,
804 screen.getCurrentWorkspace()->getName(), dlen,
808 l = XTextWidth(screen.getWindowStyle()->font,
809 screen.getCurrentWorkspace()->getName(), dlen);
811 l += (frame.bevel_w * 4);
813 if (l < frame.workspace_label_w)
817 switch (screen.getToolbarStyle()->justify) {
818 case BScreen::RightJustify:
819 dx += frame.workspace_label_w - l;
822 case BScreen::CenterJustify:
823 dx += (frame.workspace_label_w - l) / 2;
827 ToolbarStyle *style = screen.getToolbarStyle();
828 if (i18n->multibyte())
829 XmbDrawString(display, frame.workspace_label, style->fontset,
830 style->l_text_gc, dx,
831 (1 - style->fontset_extents->max_ink_extent.y),
832 (char *) screen.getCurrentWorkspace()->getName(), dlen);
834 XDrawString(display, frame.workspace_label, style->l_text_gc, dx,
835 (style->font->ascent + 1),
836 (char *) screen.getCurrentWorkspace()->getName(), dlen);
841 void Toolbar::redrawPrevWorkspaceButton(Bool pressed, Bool redraw) {
845 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.pbutton);
847 XSetWindowBackground(display, frame.psbutton, frame.pbutton_pixel);
850 XSetWindowBackgroundPixmap(display, frame.psbutton, frame.button);
852 XSetWindowBackground(display, frame.psbutton, frame.button_pixel);
854 XClearWindow(display, frame.psbutton);
857 int hh = frame.button_w / 2, hw = frame.button_w / 2;
860 pts[0].x = hw - 2; pts[0].y = hh;
861 pts[1].x = 4; pts[1].y = 2;
862 pts[2].x = 0; pts[2].y = -4;
864 XFillPolygon(display, frame.psbutton, screen.getToolbarStyle()->b_pic_gc,
865 pts, 3, Convex, CoordModePrevious);
869 void Toolbar::redrawNextWorkspaceButton(Bool pressed, Bool redraw) {
873 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.pbutton);
875 XSetWindowBackground(display, frame.nsbutton, frame.pbutton_pixel);
878 XSetWindowBackgroundPixmap(display, frame.nsbutton, frame.button);
880 XSetWindowBackground(display, frame.nsbutton, frame.button_pixel);
882 XClearWindow(display, frame.nsbutton);
885 int hh = frame.button_w / 2, hw = frame.button_w / 2;
888 pts[0].x = hw - 2; pts[0].y = hh - 2;
889 pts[1].x = 4; pts[1].y = 2;
890 pts[2].x = -4; pts[2].y = 2;
892 XFillPolygon(display, frame.nsbutton, screen.getToolbarStyle()->b_pic_gc,
893 pts, 3, Convex, CoordModePrevious);
897 void Toolbar::redrawPrevWindowButton(Bool pressed, Bool redraw) {
901 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.pbutton);
903 XSetWindowBackground(display, frame.pwbutton, frame.pbutton_pixel);
906 XSetWindowBackgroundPixmap(display, frame.pwbutton, frame.button);
908 XSetWindowBackground(display, frame.pwbutton, frame.button_pixel);
910 XClearWindow(display, frame.pwbutton);
913 int hh = frame.button_w / 2, hw = frame.button_w / 2;
916 pts[0].x = hw - 2; pts[0].y = hh;
917 pts[1].x = 4; pts[1].y = 2;
918 pts[2].x = 0; pts[2].y = -4;
920 XFillPolygon(display, frame.pwbutton, screen.getToolbarStyle()->b_pic_gc,
921 pts, 3, Convex, CoordModePrevious);
925 void Toolbar::redrawNextWindowButton(Bool pressed, Bool redraw) {
929 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.pbutton);
931 XSetWindowBackground(display, frame.nwbutton, frame.pbutton_pixel);
934 XSetWindowBackgroundPixmap(display, frame.nwbutton, frame.button);
936 XSetWindowBackground(display, frame.nwbutton, frame.button_pixel);
938 XClearWindow(display, frame.nwbutton);
941 int hh = frame.button_w / 2, hw = frame.button_w / 2;
944 pts[0].x = hw - 2; pts[0].y = hh - 2;
945 pts[1].x = 4; pts[1].y = 2;
946 pts[2].x = -4; pts[2].y = 2;
948 XFillPolygon(display, frame.nwbutton, screen.getToolbarStyle()->b_pic_gc,
949 pts, 3, Convex, CoordModePrevious);
953 void Toolbar::edit() {
958 if (XGetInputFocus(display, &window, &foo) &&
959 window == frame.workspace_label)
962 XSetInputFocus(display, frame.workspace_label,
963 ((screen.sloppyFocus()) ? RevertToPointerRoot :
966 XClearWindow(display, frame.workspace_label);
968 openbox.setNoFocus(True);
969 if (openbox.getFocusedWindow())
970 openbox.getFocusedWindow()->setFocusFlag(False);
972 XDrawRectangle(display, frame.workspace_label,
973 screen.getWindowStyle()->l_text_focus_gc,
974 frame.workspace_label_w / 2, 0, 1,
977 // change the background of the window to that of an active window label
978 Pixmap tmp = frame.wlabel;
979 BTexture *texture = &(screen.getWindowStyle()->l_focus);
980 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
982 XSetWindowBackground(display, frame.workspace_label,
983 texture->getColor()->getPixel());
986 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
987 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
989 if (tmp) image_ctrl->removeImage(tmp);
993 void Toolbar::buttonPressEvent(XButtonEvent *be) {
994 if (be->button == 1) {
995 if (be->window == frame.psbutton)
996 redrawPrevWorkspaceButton(True, True);
997 else if (be->window == frame.nsbutton)
998 redrawNextWorkspaceButton(True, True);
999 else if (be->window == frame.pwbutton)
1000 redrawPrevWindowButton(True, True);
1001 else if (be->window == frame.nwbutton)
1002 redrawNextWindowButton(True, True);
1003 #ifndef HAVE_STRFTIME
1004 else if (be->window == frame.clock) {
1005 XClearWindow(display, frame.clock);
1006 checkClock(True, True);
1008 #endif // HAVE_STRFTIME
1009 else if (! m_ontop) {
1010 Window w[1] = { frame.window };
1011 screen.raiseWindows(w, 1);
1013 } else if (be->button == 2 && (! m_ontop)) {
1014 XLowerWindow(display, frame.window);
1015 } else if (be->button == 3) {
1016 if (! toolbarmenu->isVisible()) {
1019 x = be->x_root - (toolbarmenu->getWidth() / 2);
1020 y = be->y_root - (toolbarmenu->getHeight() / 2);
1024 else if (x + toolbarmenu->getWidth() > screen.size().w())
1025 x = screen.size().w() - toolbarmenu->getWidth();
1029 else if (y + toolbarmenu->getHeight() > screen.size().h())
1030 y = screen.size().h() - toolbarmenu->getHeight();
1032 toolbarmenu->move(x, y);
1033 toolbarmenu->show();
1035 toolbarmenu->hide();
1041 void Toolbar::buttonReleaseEvent(XButtonEvent *re) {
1042 if (re->button == 1) {
1043 if (re->window == frame.psbutton) {
1044 redrawPrevWorkspaceButton(False, True);
1046 if (re->x >= 0 && re->x < (signed) frame.button_w &&
1047 re->y >= 0 && re->y < (signed) frame.button_w)
1048 if (screen.getCurrentWorkspace()->getWorkspaceID() > 0)
1049 screen.changeWorkspaceID(screen.getCurrentWorkspace()->
1050 getWorkspaceID() - 1);
1052 screen.changeWorkspaceID(screen.getWorkspaceCount() - 1);
1053 } else if (re->window == frame.nsbutton) {
1054 redrawNextWorkspaceButton(False, True);
1056 if (re->x >= 0 && re->x < (signed) frame.button_w &&
1057 re->y >= 0 && re->y < (signed) frame.button_w)
1058 if (screen.getCurrentWorkspace()->getWorkspaceID() <
1059 screen.getWorkspaceCount() - 1)
1060 screen.changeWorkspaceID(screen.getCurrentWorkspace()->
1061 getWorkspaceID() + 1);
1063 screen.changeWorkspaceID(0);
1064 } else if (re->window == frame.pwbutton) {
1065 redrawPrevWindowButton(False, True);
1067 if (re->x >= 0 && re->x < (signed) frame.button_w &&
1068 re->y >= 0 && re->y < (signed) frame.button_w)
1070 } else if (re->window == frame.nwbutton) {
1071 redrawNextWindowButton(False, True);
1073 if (re->x >= 0 && re->x < (signed) frame.button_w &&
1074 re->y >= 0 && re->y < (signed) frame.button_w)
1076 } else if (re->window == frame.window_label)
1077 screen.raiseFocus();
1078 #ifndef HAVE_STRFTIME
1079 else if (re->window == frame.clock) {
1080 XClearWindow(display, frame.clock);
1083 #endif // HAVE_STRFTIME
1088 void Toolbar::enterNotifyEvent(XCrossingEvent *) {
1093 if (! hide_timer->isTiming()) hide_timer->start();
1095 if (hide_timer->isTiming()) hide_timer->stop();
1099 void Toolbar::leaveNotifyEvent(XCrossingEvent *) {
1104 if (hide_timer->isTiming()) hide_timer->stop();
1105 } else if (! toolbarmenu->isVisible()) {
1106 if (! hide_timer->isTiming()) hide_timer->start();
1111 void Toolbar::exposeEvent(XExposeEvent *ee) {
1112 if (ee->window == frame.clock) checkClock(True);
1113 else if (ee->window == frame.workspace_label && (! m_editing))
1114 redrawWorkspaceLabel();
1115 else if (ee->window == frame.window_label) redrawWindowLabel();
1116 else if (ee->window == frame.psbutton) redrawPrevWorkspaceButton();
1117 else if (ee->window == frame.nsbutton) redrawNextWorkspaceButton();
1118 else if (ee->window == frame.pwbutton) redrawPrevWindowButton();
1119 else if (ee->window == frame.nwbutton) redrawNextWindowButton();
1123 void Toolbar::keyPressEvent(XKeyEvent *ke) {
1124 if (ke->window == frame.workspace_label && m_editing) {
1127 if (! new_workspace_name) {
1128 new_workspace_name = new char[128];
1131 if (! new_workspace_name) return;
1136 XLookupString(ke, keychar, 1, &ks, 0);
1138 // either we are told to end with a return or we hit the end of the buffer
1139 if (ks == XK_Return || new_name_pos == 127) {
1140 *(new_workspace_name + new_name_pos) = 0;
1144 openbox.setNoFocus(False);
1145 if (openbox.getFocusedWindow()) {
1146 openbox.getFocusedWindow()->setInputFocus();
1147 openbox.getFocusedWindow()->setFocusFlag(True);
1149 XSetInputFocus(display, PointerRoot, None, CurrentTime);
1151 // check to make sure that new_name[0] != 0... otherwise we have a null
1152 // workspace name which causes serious problems, especially for the
1153 // Openbox::LoadRC() method.
1154 if (*new_workspace_name) {
1155 screen.getCurrentWorkspace()->setName(new_workspace_name);
1156 screen.getCurrentWorkspace()->getMenu()->hide();
1157 screen.getWorkspacemenu()->
1158 remove(screen.getCurrentWorkspace()->getWorkspaceID() + 2);
1159 screen.getWorkspacemenu()->
1160 insert(screen.getCurrentWorkspace()->getName(),
1161 screen.getCurrentWorkspace()->getMenu(),
1162 screen.getCurrentWorkspace()->getWorkspaceID() + 2);
1163 screen.getWorkspacemenu()->update();
1166 delete [] new_workspace_name;
1167 new_workspace_name = (char *) 0;
1170 // reset the background to that of the workspace label (its normal
1172 Pixmap tmp = frame.wlabel;
1173 BTexture *texture = &(screen.getToolbarStyle()->label);
1174 if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
1175 frame.wlabel = None;
1176 XSetWindowBackground(display, frame.workspace_label,
1177 texture->getColor()->getPixel());
1180 image_ctrl->renderImage(frame.workspace_label_w, frame.label_h, texture);
1181 XSetWindowBackgroundPixmap(display, frame.workspace_label, frame.wlabel);
1183 if (tmp) image_ctrl->removeImage(tmp);
1186 } else if (! (ks == XK_Shift_L || ks == XK_Shift_R ||
1187 ks == XK_Control_L || ks == XK_Control_R ||
1188 ks == XK_Caps_Lock || ks == XK_Shift_Lock ||
1189 ks == XK_Meta_L || ks == XK_Meta_R ||
1190 ks == XK_Alt_L || ks == XK_Alt_R ||
1191 ks == XK_Super_L || ks == XK_Super_R ||
1192 ks == XK_Hyper_L || ks == XK_Hyper_R)) {
1193 if (ks == XK_BackSpace) {
1194 if (new_name_pos > 0) {
1196 *(new_workspace_name + new_name_pos) = '\0';
1198 *new_workspace_name = '\0';
1201 *(new_workspace_name + new_name_pos) = *keychar;
1203 *(new_workspace_name + new_name_pos) = '\0';
1206 XClearWindow(display, frame.workspace_label);
1207 int l = strlen(new_workspace_name), tw, x;
1209 if (i18n->multibyte()) {
1210 XRectangle ink, logical;
1211 XmbTextExtents(screen.getToolbarStyle()->fontset,
1212 new_workspace_name, l, &ink, &logical);
1215 tw = XTextWidth(screen.getToolbarStyle()->font,
1216 new_workspace_name, l);
1218 x = (frame.workspace_label_w - tw) / 2;
1220 if (x < (signed) frame.bevel_w) x = frame.bevel_w;
1222 WindowStyle *style = screen.getWindowStyle();
1223 if (i18n->multibyte())
1224 XmbDrawString(display, frame.workspace_label, style->fontset,
1225 style->l_text_focus_gc, x,
1226 (1 - style->fontset_extents->max_ink_extent.y),
1227 new_workspace_name, l);
1229 XDrawString(display, frame.workspace_label, style->l_text_focus_gc, x,
1230 (style->font->ascent + 1),
1231 new_workspace_name, l);
1233 XDrawRectangle(display, frame.workspace_label,
1234 screen.getWindowStyle()->l_text_focus_gc, x + tw, 0, 1,
1243 void Toolbar::timeout() {
1247 gettimeofday(&now, 0);
1248 clock_timer->setTimeout((60 - (now.tv_sec % 60)) * 1000);
1252 void Toolbar::HideHandler::timeout() {
1253 toolbar->m_hidden = !toolbar->m_hidden;
1254 if (toolbar->m_hidden)
1255 XMoveWindow(toolbar->display, toolbar->frame.window,
1256 toolbar->frame.x_hidden, toolbar->frame.y_hidden);
1258 XMoveWindow(toolbar->display, toolbar->frame.window,
1259 toolbar->frame.x, toolbar->frame.y);
1263 Toolbarmenu::Toolbarmenu(Toolbar &tb) : Basemenu(tb.screen), toolbar(tb) {
1264 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarTitle, "Toolbar"));
1267 placementmenu = new Placementmenu(*this);
1269 insert(i18n->getMessage(CommonSet, CommonPlacementTitle, "Placement"),
1271 insert(i18n->getMessage(CommonSet, CommonAlwaysOnTop, "Always on top"), 1);
1272 insert(i18n->getMessage(CommonSet, CommonAutoHide, "Auto hide"), 2);
1273 insert(i18n->getMessage(ToolbarSet, ToolbarEditWkspcName,
1274 "Edit current workspace name"), 3);
1281 void Toolbarmenu::setValues() {
1282 setItemSelected(1, toolbar.onTop());
1283 setItemSelected(2, toolbar.autoHide());
1287 Toolbarmenu::~Toolbarmenu() {
1288 delete placementmenu;
1292 void Toolbarmenu::itemSelected(int button, int index) {
1296 BasemenuItem *item = find(index);
1299 switch (item->function()) {
1300 case 1: { // always on top
1301 Bool change = ((toolbar.onTop()) ? False : True);
1302 toolbar.setOnTop(change);
1303 setItemSelected(1, change);
1305 if (toolbar.onTop()) toolbar.screen.raiseWindows((Window *) 0, 0);
1309 case 2: { // auto hide
1310 Bool change = ((toolbar.autoHide()) ? False : True);
1311 toolbar.setAutoHide(change);
1312 setItemSelected(2, change);
1315 toolbar.screen.getSlit()->reposition();
1320 case 3: { // edit current workspace name
1330 void Toolbarmenu::internal_hide() {
1331 Basemenu::internal_hide();
1332 if (toolbar.autoHide() && ! toolbar.isEditing())
1333 toolbar.hide_handler.timeout();
1337 void Toolbarmenu::reconfigure() {
1339 placementmenu->reconfigure();
1341 Basemenu::reconfigure();
1345 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu &tm)
1346 : Basemenu(tm.toolbar.screen), toolbarmenu(tm) {
1347 setLabel(i18n->getMessage(ToolbarSet, ToolbarToolbarPlacement,
1348 "Toolbar Placement"));
1350 setMinimumSublevels(3);
1352 insert(i18n->getMessage(CommonSet, CommonPlacementTopLeft,
1353 "Top Left"), Toolbar::TopLeft);
1354 insert(i18n->getMessage(CommonSet, CommonPlacementBottomLeft,
1355 "Bottom Left"), Toolbar::BottomLeft);
1356 insert(i18n->getMessage(CommonSet, CommonPlacementTopCenter,
1357 "Top Center"), Toolbar::TopCenter);
1358 insert(i18n->getMessage(CommonSet, CommonPlacementBottomCenter,
1359 "Bottom Center"), Toolbar::BottomCenter);
1360 insert(i18n->getMessage(CommonSet, CommonPlacementTopRight,
1361 "Top Right"), Toolbar::TopRight);
1362 insert(i18n->getMessage(CommonSet, CommonPlacementBottomRight,
1363 "Bottom Right"), Toolbar::BottomRight);
1367 void Toolbarmenu::Placementmenu::itemSelected(int button, int index) {
1371 BasemenuItem *item = find(index);
1374 toolbarmenu.toolbar.setPlacement(item->function());
1376 toolbarmenu.toolbar.reconfigure();
1379 // reposition the slit as well to make sure it doesn't intersect the
1381 toolbarmenu.toolbar.screen.getSlit()->reposition();