1 // -*- mode: C++; indent-tabs-mode: nil; -*-
2 // Toolbar.cc for Blackbox - an X11 Window manager
3 // Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry <shaleh@debian.org>
4 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
6 // Permission is hereby granted, free of charge, to any person obtaining a
7 // copy of this software and associated documentation files (the "Software"),
8 // to deal in the Software without restriction, including without limitation
9 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 // and/or sell copies of the Software, and to permit persons to whom the
11 // Software is furnished to do so, subject to the following conditions:
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 // DEALINGS IN THE SOFTWARE.
25 # include "../config.h"
26 #endif // HAVE_CONFIG_H
29 #include <X11/keysym.h>
33 #endif // HAVE_STRING_H
37 #endif // HAVE_STDIO_H
39 #ifdef TIME_WITH_SYS_TIME
40 # include <sys/time.h>
42 #else // !TIME_WITH_SYS_TIME
43 # ifdef HAVE_SYS_TIME_H
44 # include <sys/time.h>
45 # else // !HAVE_SYS_TIME_H
47 # endif // HAVE_SYS_TIME_H
48 #endif // TIME_WITH_SYS_TIME
55 #include "blackbox.hh"
62 #include "Workspace.hh"
63 #include "Clientmenu.hh"
64 #include "Workspacemenu.hh"
68 static long aMinuteFromNow(void) {
70 gettimeofday(&now
, 0);
71 return ((60 - (now
.tv_sec
% 60)) * 1000);
75 Toolbar::Toolbar(BScreen
*scrn
) {
77 blackbox
= screen
->getBlackbox();
78 toolbarstr
= "session.screen" + itostring(screen
->getScreenNumber()) +
80 config
= blackbox
->getConfig();
84 // get the clock updating every minute
85 clock_timer
= new BTimer(blackbox
, this);
86 clock_timer
->setTimeout(aMinuteFromNow());
87 clock_timer
->recurring(True
);
89 frame
.minute
= frame
.hour
= -1;
91 hide_handler
.toolbar
= this;
92 hide_timer
= new BTimer(blackbox
, &hide_handler
);
93 hide_timer
->setTimeout(blackbox
->getAutoRaiseDelay());
98 toolbarmenu
= new Toolbarmenu(this);
100 display
= blackbox
->getXDisplay();
101 XSetWindowAttributes attrib
;
102 unsigned long create_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
103 CWColormap
| CWOverrideRedirect
| CWEventMask
;
104 attrib
.background_pixmap
= None
;
105 attrib
.background_pixel
= attrib
.border_pixel
=
106 screen
->getBorderColor()->pixel();
107 attrib
.colormap
= screen
->getColormap();
108 attrib
.override_redirect
= True
;
109 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
110 EnterWindowMask
| LeaveWindowMask
;
113 XCreateWindow(display
, screen
->getRootWindow(), 0, 0, 1, 1, 0,
114 screen
->getDepth(), InputOutput
, screen
->getVisual(),
115 create_mask
, &attrib
);
116 blackbox
->saveToolbarSearch(frame
.window
, this);
118 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
| ExposureMask
|
119 KeyPressMask
| EnterWindowMask
;
121 frame
.workspace_label
=
122 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
123 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
124 blackbox
->saveToolbarSearch(frame
.workspace_label
, this);
127 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
128 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
129 blackbox
->saveToolbarSearch(frame
.window_label
, this);
132 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
133 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
134 blackbox
->saveToolbarSearch(frame
.clock
, this);
137 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
138 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
139 blackbox
->saveToolbarSearch(frame
.psbutton
, this);
142 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
143 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
144 blackbox
->saveToolbarSearch(frame
.nsbutton
, this);
147 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
148 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
149 blackbox
->saveToolbarSearch(frame
.pwbutton
, this);
152 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
->getDepth(),
153 InputOutput
, screen
->getVisual(), create_mask
, &attrib
);
154 blackbox
->saveToolbarSearch(frame
.nwbutton
, this);
156 frame
.base
= frame
.label
= frame
.wlabel
= frame
.clk
= frame
.button
=
157 frame
.pbutton
= None
;
164 Toolbar::~Toolbar(void) {
167 if (frame
.base
) screen
->getImageControl()->removeImage(frame
.base
);
168 if (frame
.label
) screen
->getImageControl()->removeImage(frame
.label
);
169 if (frame
.wlabel
) screen
->getImageControl()->removeImage(frame
.wlabel
);
170 if (frame
.clk
) screen
->getImageControl()->removeImage(frame
.clk
);
171 if (frame
.button
) screen
->getImageControl()->removeImage(frame
.button
);
172 if (frame
.pbutton
) screen
->getImageControl()->removeImage(frame
.pbutton
);
174 blackbox
->removeToolbarSearch(frame
.window
);
175 blackbox
->removeToolbarSearch(frame
.workspace_label
);
176 blackbox
->removeToolbarSearch(frame
.window_label
);
177 blackbox
->removeToolbarSearch(frame
.clock
);
178 blackbox
->removeToolbarSearch(frame
.psbutton
);
179 blackbox
->removeToolbarSearch(frame
.nsbutton
);
180 blackbox
->removeToolbarSearch(frame
.pwbutton
);
181 blackbox
->removeToolbarSearch(frame
.nwbutton
);
183 XDestroyWindow(display
, frame
.workspace_label
);
184 XDestroyWindow(display
, frame
.window_label
);
185 XDestroyWindow(display
, frame
.clock
);
187 XDestroyWindow(display
, frame
.window
);
195 void Toolbar::mapToolbar() {
196 if (!screen
->doHideToolbar()) {
197 //not hidden, so windows should not maximize over the toolbar
198 XMapSubwindows(display
, frame
.window
);
199 XMapWindow(display
, frame
.window
);
201 screen
->addStrut(&strut
);
206 void Toolbar::unmapToolbar() {
207 if (toolbarmenu
->isVisible())
209 //hidden so we can maximize over the toolbar
210 screen
->removeStrut(&strut
);
211 screen
->updateAvailableArea();
213 XUnmapWindow(display
, frame
.window
);
218 void Toolbar::saveOnTop(bool b
) {
220 config
->setValue(toolbarstr
+ "onTop", on_top
);
224 void Toolbar::saveAutoHide(bool b
) {
226 config
->setValue(toolbarstr
+ "autoHide", do_auto_hide
);
230 void Toolbar::saveWidthPercent(unsigned int w
) {
232 config
->setValue(toolbarstr
+ "widthPercent", width_percent
);
236 void Toolbar::savePlacement(int p
) {
240 case TopLeft
: pname
= "TopLeft"; break;
241 case BottomLeft
: pname
= "BottomLeft"; break;
242 case TopCenter
: pname
= "TopCenter"; break;
243 case TopRight
: pname
= "TopRight"; break;
244 case BottomRight
: pname
= "BottomRight"; break;
245 case BottomCenter
: default: pname
= "BottomCenter"; break;
247 config
->setValue(toolbarstr
+ "placement", pname
);
251 void Toolbar::save_rc(void) {
253 saveAutoHide(do_auto_hide
);
254 saveWidthPercent(width_percent
);
255 savePlacement(placement
);
259 void Toolbar::load_rc(void) {
262 if (! config
->getValue(toolbarstr
+ "onTop", on_top
))
265 if (! config
->getValue(toolbarstr
+ "autoHide", do_auto_hide
))
266 do_auto_hide
= false;
267 hidden
= do_auto_hide
;
269 if (! config
->getValue(toolbarstr
+ "widthPercent", width_percent
) ||
270 width_percent
== 0 || width_percent
> 100)
273 if (config
->getValue(toolbarstr
+ "placement", s
)) {
276 else if (s
== "BottomLeft")
277 placement
= BottomLeft
;
278 else if (s
== "TopCenter")
279 placement
= TopCenter
;
280 else if (s
== "TopRight")
281 placement
= TopRight
;
282 else if (s
== "BottomRight")
283 placement
= BottomRight
;
284 else //if (s == "BottomCenter")
285 placement
= BottomCenter
;
287 placement
= BottomCenter
;
291 void Toolbar::reconfigure(void) {
292 unsigned int width
, height
;
294 width
= (screen
->getWidth() * width_percent
) / 100;
295 height
= screen
->getToolbarStyle()->font
->height();
297 frame
.bevel_w
= screen
->getBevelWidth();
298 frame
.button_w
= height
;
300 frame
.label_h
= height
;
301 height
+= (frame
.bevel_w
* 2);
303 frame
.rect
.setSize(width
, height
);
310 if (placement
== TopLeft
)
312 else if (placement
== TopRight
)
313 x
= screen
->getWidth() - frame
.rect
.width()
314 - (screen
->getBorderWidth() * 2);
316 x
= (screen
->getWidth() - frame
.rect
.width()) / 2;
321 frame
.y_hidden
= screen
->getBevelWidth() - screen
->getBorderWidth()
322 - frame
.rect
.height();
329 if (placement
== BottomLeft
)
331 else if (placement
== BottomRight
)
332 x
= screen
->getWidth() - frame
.rect
.width()
333 - (screen
->getBorderWidth() * 2);
335 x
= (screen
->getWidth() - frame
.rect
.width()) / 2;
337 y
= screen
->getHeight() - frame
.rect
.height()
338 - (screen
->getBorderWidth() * 2);
341 frame
.y_hidden
= screen
->getHeight() - screen
->getBevelWidth()
342 - screen
->getBorderWidth();
346 frame
.rect
.setPos(x
, y
);
351 time_t ttmp
= time(NULL
);
355 struct tm
*tt
= localtime(&ttmp
);
358 int len
= strftime(t
, 1024, screen
->getStrftimeFormat(), tt
);
359 if (len
== 0) { // invalid time format found
360 screen
->saveStrftimeFormat("%I:%M %p"); // so use the default
361 strftime(t
, 1024, screen
->getStrftimeFormat(), tt
);
363 // find the length of the rendered string and add room for two extra
364 // characters to it. This allows for variable width output of the fonts
365 BFont
*font
= screen
->getToolbarStyle()->font
;
366 frame
.clock_w
= font
->measureString(t
) + font
->maxCharWidth() * 2;
369 #else // !HAVE_STRFTIME
371 string s
= i18n(ToolbarSet
, ToolbarNoStrftimeLength
, "00:00000");
372 frame
.clock_w
= screen
->getToolbarStyle()->font
->measureString(s
);
374 #endif // HAVE_STRFTIME
376 frame
.workspace_label_w
= 0;
378 for (unsigned int i
= 0; i
< screen
->getWorkspaceCount(); i
++) {
379 const string
& workspace_name
= screen
->getWorkspace(i
)->getName();
380 width
= screen
->getToolbarStyle()->font
->measureString(workspace_name
);
381 if (width
> frame
.workspace_label_w
) frame
.workspace_label_w
= width
;
384 frame
.workspace_label_w
= frame
.clock_w
=
385 std::max(frame
.workspace_label_w
, frame
.clock_w
) + (frame
.bevel_w
* 4);
387 // XXX: where'd the +6 come from?
388 frame
.window_label_w
=
389 (frame
.rect
.width() - (frame
.clock_w
+ (frame
.button_w
* 4) +
390 frame
.workspace_label_w
+ (frame
.bevel_w
* 8) + 6));
393 XMoveResizeWindow(display
, frame
.window
, frame
.x_hidden
, frame
.y_hidden
,
394 frame
.rect
.width(), frame
.rect
.height());
396 XMoveResizeWindow(display
, frame
.window
, frame
.rect
.x(), frame
.rect
.y(),
397 frame
.rect
.width(), frame
.rect
.height());
400 XMoveResizeWindow(display
, frame
.workspace_label
, frame
.bevel_w
,
401 frame
.bevel_w
, frame
.workspace_label_w
,
403 XMoveResizeWindow(display
, frame
.psbutton
,
404 ((frame
.bevel_w
* 2) + frame
.workspace_label_w
+ 1),
405 frame
.bevel_w
+ 1, frame
.button_w
, frame
.button_w
);
406 XMoveResizeWindow(display
, frame
.nsbutton
,
407 ((frame
.bevel_w
* 3) + frame
.workspace_label_w
+
408 frame
.button_w
+ 2), frame
.bevel_w
+ 1, frame
.button_w
,
410 XMoveResizeWindow(display
, frame
.window_label
,
411 ((frame
.bevel_w
* 4) + (frame
.button_w
* 2) +
412 frame
.workspace_label_w
+ 3), frame
.bevel_w
,
413 frame
.window_label_w
, frame
.label_h
);
414 XMoveResizeWindow(display
, frame
.pwbutton
,
415 ((frame
.bevel_w
* 5) + (frame
.button_w
* 2) +
416 frame
.workspace_label_w
+ frame
.window_label_w
+ 4),
417 frame
.bevel_w
+ 1, frame
.button_w
, frame
.button_w
);
418 XMoveResizeWindow(display
, frame
.nwbutton
,
419 ((frame
.bevel_w
* 6) + (frame
.button_w
* 3) +
420 frame
.workspace_label_w
+ frame
.window_label_w
+ 5),
421 frame
.bevel_w
+ 1, frame
.button_w
, frame
.button_w
);
422 XMoveResizeWindow(display
, frame
.clock
,
423 frame
.rect
.width() - frame
.clock_w
- (frame
.bevel_w
* 2),
424 frame
.bevel_w
, frame
.clock_w
, frame
.label_h
);
426 ToolbarStyle
*style
= screen
->getToolbarStyle();
427 frame
.base
= style
->toolbar
.render(frame
.rect
.width(), frame
.rect
.height(),
430 XSetWindowBackground(display
, frame
.window
,
431 style
->toolbar
.color().pixel());
433 XSetWindowBackgroundPixmap(display
, frame
.window
, frame
.base
);
435 frame
.label
= style
->window
.render(frame
.window_label_w
, frame
.label_h
,
438 XSetWindowBackground(display
, frame
.window_label
,
439 style
->window
.color().pixel());
441 XSetWindowBackgroundPixmap(display
, frame
.window_label
, frame
.label
);
443 frame
.wlabel
= style
->label
.render(frame
.workspace_label_w
, frame
.label_h
,
446 XSetWindowBackground(display
, frame
.workspace_label
,
447 style
->label
.color().pixel());
449 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
451 frame
.clk
= style
->clock
.render(frame
.clock_w
, frame
.label_h
, frame
.clk
);
453 XSetWindowBackground(display
, frame
.clock
, style
->clock
.color().pixel());
455 XSetWindowBackgroundPixmap(display
, frame
.clock
, frame
.clk
);
457 frame
.button
= style
->button
.render(frame
.button_w
, frame
.button_w
,
459 if (! frame
.button
) {
460 frame
.button_pixel
= style
->button
.color().pixel();
461 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
462 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
463 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
464 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
466 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
467 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
468 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
469 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
472 frame
.pbutton
= style
->pressed
.render(frame
.button_w
, frame
.button_w
,
475 frame
.pbutton_pixel
= style
->pressed
.color().pixel();
477 XSetWindowBorder(display
, frame
.window
,
478 screen
->getBorderColor()->pixel());
479 XSetWindowBorderWidth(display
, frame
.window
, screen
->getBorderWidth());
481 XClearWindow(display
, frame
.window
);
482 XClearWindow(display
, frame
.workspace_label
);
483 XClearWindow(display
, frame
.window_label
);
484 XClearWindow(display
, frame
.clock
);
485 XClearWindow(display
, frame
.psbutton
);
486 XClearWindow(display
, frame
.nsbutton
);
487 XClearWindow(display
, frame
.pwbutton
);
488 XClearWindow(display
, frame
.nwbutton
);
491 redrawWorkspaceLabel();
492 redrawPrevWorkspaceButton();
493 redrawNextWorkspaceButton();
494 redrawPrevWindowButton();
495 redrawNextWindowButton();
498 toolbarmenu
->reconfigure();
502 void Toolbar::updateStrut(void) {
503 // left and right are always 0
504 strut
.top
= strut
.bottom
= 0;
506 // when hidden only one border is visible
507 unsigned int border_width
= screen
->getBorderWidth();
511 if (! screen
->doHideToolbar()) {
516 strut
.top
= getExposedHeight() + border_width
;
519 strut
.bottom
= getExposedHeight() + border_width
;
523 screen
->updateAvailableArea();
528 void Toolbar::checkClock(bool redraw
) {
529 #else // !HAVE_STRFTIME
530 void Toolbar::checkClock(bool redraw
, bool date
) {
531 #endif // HAVE_STRFTIME
535 if ((tmp
= time(NULL
)) != -1) {
536 if (! (tt
= localtime(&tmp
))) return;
537 if (tt
->tm_min
!= frame
.minute
|| tt
->tm_hour
!= frame
.hour
) {
538 frame
.hour
= tt
->tm_hour
;
539 frame
.minute
= tt
->tm_min
;
540 XClearWindow(display
, frame
.clock
);
548 if (! strftime(t
, 1024, screen
->getStrftimeFormat(), tt
))
550 #else // !HAVE_STRFTIME
553 // format the date... with special consideration for y2k ;)
554 if (screen
->getDateFormat() == Blackbox::B_EuropeanDate
)
555 sprintf(t
, 18n(ToolbarSet
, ToolbarNoStrftimeDateFormatEu
,
557 tt
->tm_mday
, tt
->tm_mon
+ 1,
558 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
560 sprintf(t
, i18n(ToolbarSet
, ToolbarNoStrftimeDateFormat
,
562 tt
->tm_mon
+ 1, tt
->tm_mday
,
563 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
565 if (screen
->isClock24Hour())
566 sprintf(t
, i18n(ToolbarSet
, ToolbarNoStrftimeTimeFormat24
,
568 frame
.hour
, frame
.minute
);
570 sprintf(t
, i18n(ToolbarSet
, ToolbarNoStrftimeTimeFormat12
,
572 ((frame
.hour
> 12) ? frame
.hour
- 12 :
573 ((frame
.hour
== 0) ? 12 : frame
.hour
)), frame
.minute
,
574 ((frame
.hour
>= 12) ?
575 i18n(ToolbarSet
, ToolbarNoStrftimeTimeFormatP
, "p") :
576 i18n(ToolbarSet
, ToolbarNoStrftimeTimeFormatA
, "a")));
578 #endif // HAVE_STRFTIME
580 ToolbarStyle
*style
= screen
->getToolbarStyle();
582 int pos
= frame
.bevel_w
* 2; // this is modified by doJustify()
583 style
->doJustify(t
, pos
, frame
.clock_w
, frame
.bevel_w
* 4);
584 style
->font
->drawString(frame
.clock
, pos
, 1, style
->c_text
, t
);
589 void Toolbar::redrawWindowLabel(bool redraw
) {
590 BlackboxWindow
*foc
= screen
->getBlackbox()->getFocusedWindow();
592 XClearWindow(display
, frame
.window_label
);
597 XClearWindow(display
, frame
.window_label
);
599 if (foc
->getScreen() != screen
) return;
601 const char *title
= foc
->getTitle();
602 ToolbarStyle
*style
= screen
->getToolbarStyle();
604 int pos
= frame
.bevel_w
* 2; // modified by doJustify()
605 style
->doJustify(title
, pos
, frame
.window_label_w
, frame
.bevel_w
* 4);
606 style
->font
->drawString(frame
.window_label
, pos
, 1, style
->w_text
, title
);
610 void Toolbar::redrawWorkspaceLabel(bool redraw
) {
611 const string
& name
= screen
->getCurrentWorkspace()->getName();
614 XClearWindow(display
, frame
.workspace_label
);
616 ToolbarStyle
*style
= screen
->getToolbarStyle();
618 int pos
= frame
.bevel_w
* 2;
619 style
->doJustify(name
.c_str(), pos
, frame
.workspace_label_w
,
621 style
->font
->drawString(frame
.workspace_label
, pos
, 1, style
->l_text
, name
);
625 void Toolbar::drawArrow(Drawable surface
, bool left
) const {
626 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
628 const int bullet_size
= 3;
631 pts
[0].x
= hw
- bullet_size
;
633 pts
[1].x
= 2 * bullet_size
;
634 pts
[1].y
= bullet_size
;
636 pts
[2].y
= -(2 * bullet_size
);
638 pts
[0].x
= hw
- bullet_size
;
639 pts
[0].y
= hh
- bullet_size
;
640 pts
[1].x
= (2 * bullet_size
);
641 pts
[1].y
= bullet_size
;
642 pts
[2].x
= -(2 * bullet_size
);
643 pts
[2].y
= bullet_size
;
646 BPen
pen(screen
->getToolbarStyle()->b_pic
);
647 XFillPolygon(display
, surface
, pen
.gc(), pts
, 3, Convex
, CoordModePrevious
);
651 void Toolbar::redrawPrevWorkspaceButton(bool pressed
, bool redraw
) {
655 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.pbutton
);
657 XSetWindowBackground(display
, frame
.psbutton
, frame
.pbutton_pixel
);
660 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
662 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
664 XClearWindow(display
, frame
.psbutton
);
667 drawArrow(frame
.psbutton
, True
);
671 void Toolbar::redrawNextWorkspaceButton(bool pressed
, bool redraw
) {
675 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.pbutton
);
677 XSetWindowBackground(display
, frame
.nsbutton
, frame
.pbutton_pixel
);
680 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
682 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
684 XClearWindow(display
, frame
.nsbutton
);
687 drawArrow(frame
.nsbutton
, False
);
691 void Toolbar::redrawPrevWindowButton(bool pressed
, bool redraw
) {
695 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.pbutton
);
697 XSetWindowBackground(display
, frame
.pwbutton
, frame
.pbutton_pixel
);
700 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
702 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
704 XClearWindow(display
, frame
.pwbutton
);
707 drawArrow(frame
.pwbutton
, True
);
711 void Toolbar::redrawNextWindowButton(bool pressed
, bool redraw
) {
715 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.pbutton
);
717 XSetWindowBackground(display
, frame
.nwbutton
, frame
.pbutton_pixel
);
720 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
722 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
724 XClearWindow(display
, frame
.nwbutton
);
727 drawArrow(frame
.nwbutton
, False
);
731 void Toolbar::edit(void) {
736 XGetInputFocus(display
, &window
, &foo
);
737 if (window
== frame
.workspace_label
)
740 XSetInputFocus(display
, frame
.workspace_label
,
741 RevertToPointerRoot
, CurrentTime
);
742 XClearWindow(display
, frame
.workspace_label
);
744 blackbox
->setNoFocus(True
);
745 if (blackbox
->getFocusedWindow())
746 blackbox
->getFocusedWindow()->setFocusFlag(False
);
748 ToolbarStyle
*style
= screen
->getToolbarStyle();
749 BPen
pen(style
->l_text
);
750 XDrawRectangle(display
, frame
.workspace_label
, pen
.gc(),
751 frame
.workspace_label_w
/ 2, 0, 1,
753 // change the background of the window to that of an active window label
754 BTexture
*texture
= &(screen
->getWindowStyle()->l_focus
);
755 frame
.wlabel
= texture
->render(frame
.workspace_label_w
, frame
.label_h
,
758 XSetWindowBackground(display
, frame
.workspace_label
,
759 texture
->color().pixel());
761 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
765 void Toolbar::buttonPressEvent(const XButtonEvent
*be
) {
766 if (be
->button
== 1) {
767 if (be
->window
== frame
.psbutton
)
768 redrawPrevWorkspaceButton(True
, True
);
769 else if (be
->window
== frame
.nsbutton
)
770 redrawNextWorkspaceButton(True
, True
);
771 else if (be
->window
== frame
.pwbutton
)
772 redrawPrevWindowButton(True
, True
);
773 else if (be
->window
== frame
.nwbutton
)
774 redrawNextWindowButton(True
, True
);
775 #ifndef HAVE_STRFTIME
776 else if (be
->window
== frame
.clock
) {
777 XClearWindow(display
, frame
.clock
);
778 checkClock(True
, True
);
780 #endif // HAVE_STRFTIME
782 Window w
[1] = { frame
.window
};
783 screen
->raiseWindows(w
, 1);
785 } else if (be
->button
== 2 && (! on_top
)) {
786 XLowerWindow(display
, frame
.window
);
787 } else if (be
->button
== 3) {
788 if (toolbarmenu
->isVisible()) {
793 x
= be
->x_root
- (toolbarmenu
->getWidth() / 2);
794 y
= be
->y_root
- (toolbarmenu
->getHeight() / 2);
798 else if (x
+ toolbarmenu
->getWidth() > screen
->getWidth())
799 x
= screen
->getWidth() - toolbarmenu
->getWidth();
803 else if (y
+ toolbarmenu
->getHeight() > screen
->getHeight())
804 y
= screen
->getHeight() - toolbarmenu
->getHeight();
806 toolbarmenu
->move(x
, y
);
814 void Toolbar::buttonReleaseEvent(const XButtonEvent
*re
) {
815 if (re
->button
== 1) {
816 if (re
->window
== frame
.psbutton
) {
817 redrawPrevWorkspaceButton(False
, True
);
819 if (re
->x
>= 0 && re
->x
< static_cast<signed>(frame
.button_w
) &&
820 re
->y
>= 0 && re
->y
< static_cast<signed>(frame
.button_w
))
821 if (screen
->getCurrentWorkspace()->getID() > 0)
822 screen
->changeWorkspaceID(screen
->getCurrentWorkspace()->
825 screen
->changeWorkspaceID(screen
->getWorkspaceCount() - 1);
826 } else if (re
->window
== frame
.nsbutton
) {
827 redrawNextWorkspaceButton(False
, True
);
829 if (re
->x
>= 0 && re
->x
< static_cast<signed>(frame
.button_w
) &&
830 re
->y
>= 0 && re
->y
< static_cast<signed>(frame
.button_w
))
831 if (screen
->getCurrentWorkspace()->getID() <
832 (screen
->getWorkspaceCount() - 1))
833 screen
->changeWorkspaceID(screen
->getCurrentWorkspace()->
836 screen
->changeWorkspaceID(0);
837 } else if (re
->window
== frame
.pwbutton
) {
838 redrawPrevWindowButton(False
, True
);
840 if (re
->x
>= 0 && re
->x
< static_cast<signed>(frame
.button_w
) &&
841 re
->y
>= 0 && re
->y
< static_cast<signed>(frame
.button_w
))
843 } else if (re
->window
== frame
.nwbutton
) {
844 redrawNextWindowButton(False
, True
);
846 if (re
->x
>= 0 && re
->x
< static_cast<signed>(frame
.button_w
) &&
847 re
->y
>= 0 && re
->y
< static_cast<signed>(frame
.button_w
))
849 } else if (re
->window
== frame
.window_label
)
850 screen
->raiseFocus();
851 #ifndef HAVE_STRFTIME
852 else if (re
->window
== frame
.clock
) {
853 XClearWindow(display
, frame
.clock
);
856 #endif // HAVE_STRFTIME
861 void Toolbar::enterNotifyEvent(const XCrossingEvent
*) {
866 if (! hide_timer
->isTiming()) hide_timer
->start();
868 if (hide_timer
->isTiming()) hide_timer
->stop();
872 void Toolbar::leaveNotifyEvent(const XCrossingEvent
*) {
877 if (hide_timer
->isTiming()) hide_timer
->stop();
878 } else if (! toolbarmenu
->isVisible()) {
879 if (! hide_timer
->isTiming()) hide_timer
->start();
884 void Toolbar::exposeEvent(const XExposeEvent
*ee
) {
885 if (ee
->window
== frame
.clock
) checkClock(True
);
886 else if (ee
->window
== frame
.workspace_label
&& (! editing
))
887 redrawWorkspaceLabel();
888 else if (ee
->window
== frame
.window_label
) redrawWindowLabel();
889 else if (ee
->window
== frame
.psbutton
) redrawPrevWorkspaceButton();
890 else if (ee
->window
== frame
.nsbutton
) redrawNextWorkspaceButton();
891 else if (ee
->window
== frame
.pwbutton
) redrawPrevWindowButton();
892 else if (ee
->window
== frame
.nwbutton
) redrawNextWindowButton();
896 void Toolbar::keyPressEvent(const XKeyEvent
*ke
) {
897 if (ke
->window
== frame
.workspace_label
&& editing
) {
898 if (new_workspace_name
.empty()) {
904 XLookupString(const_cast<XKeyEvent
*>(ke
), keychar
, 1, &ks
, 0);
906 // either we are told to end with a return or we hit 127 chars
907 if (ks
== XK_Return
|| new_name_pos
== 127) {
910 blackbox
->setNoFocus(False
);
911 if (blackbox
->getFocusedWindow())
912 blackbox
->getFocusedWindow()->setInputFocus();
914 blackbox
->setFocusedWindow(0);
916 // the toolbar will be reconfigured when the change to the workspace name
917 // gets caught in the PropertyNotify event handler
918 screen
->getCurrentWorkspace()->setName(new_workspace_name
);
920 new_workspace_name
.erase();
923 // reset the background to that of the workspace label (its normal
925 BTexture
*texture
= &(screen
->getToolbarStyle()->label
);
926 frame
.wlabel
= texture
->render(frame
.workspace_label_w
, frame
.label_h
,
929 XSetWindowBackground(display
, frame
.workspace_label
,
930 texture
->color().pixel());
932 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
,
934 } else if (! (ks
== XK_Shift_L
|| ks
== XK_Shift_R
||
935 ks
== XK_Control_L
|| ks
== XK_Control_R
||
936 ks
== XK_Caps_Lock
|| ks
== XK_Shift_Lock
||
937 ks
== XK_Meta_L
|| ks
== XK_Meta_R
||
938 ks
== XK_Alt_L
|| ks
== XK_Alt_R
||
939 ks
== XK_Super_L
|| ks
== XK_Super_R
||
940 ks
== XK_Hyper_L
|| ks
== XK_Hyper_R
)) {
941 if (ks
== XK_BackSpace
) {
942 if (new_name_pos
> 0) {
944 new_workspace_name
.erase(new_name_pos
);
946 new_workspace_name
.resize(0);
949 new_workspace_name
+= (*keychar
);
953 XClearWindow(display
, frame
.workspace_label
);
956 tw
= screen
->getToolbarStyle()->font
->measureString(new_workspace_name
);
957 x
= (frame
.workspace_label_w
- tw
) / 2;
959 if (x
< frame
.bevel_w
) x
= frame
.bevel_w
;
961 ToolbarStyle
*style
= screen
->getToolbarStyle();
962 style
->font
->drawString(frame
.workspace_label
, x
, 1, style
->l_text
,
964 BPen
pen(style
->l_text
);
965 XDrawRectangle(display
, frame
.workspace_label
, pen
.gc(), x
+ tw
, 0, 1,
972 void Toolbar::timeout(void) {
975 clock_timer
->setTimeout(aMinuteFromNow());
979 void Toolbar::HideHandler::timeout(void) {
980 toolbar
->hidden
= ! toolbar
->hidden
;
982 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
983 toolbar
->frame
.x_hidden
, toolbar
->frame
.y_hidden
);
985 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
986 toolbar
->frame
.rect
.x(), toolbar
->frame
.rect
.y());
990 void Toolbar::toggleAutoHide(void) {
991 saveAutoHide(! doAutoHide());
994 screen
->getSlit()->reposition();
996 if (do_auto_hide
== False
&& hidden
) {
997 // force the slit to be visible
998 if (hide_timer
->isTiming()) hide_timer
->stop();
999 hide_handler
.timeout();
1004 Toolbarmenu::Toolbarmenu(Toolbar
*tb
) : Basemenu(tb
->screen
) {
1007 setLabel(i18n(ToolbarSet
, ToolbarToolbarTitle
, "Toolbar"));
1010 placementmenu
= new Placementmenu(this);
1012 insert(i18n(CommonSet
, CommonPlacementTitle
, "Placement"),
1014 insert(i18n(CommonSet
, CommonAlwaysOnTop
, "Always on top"), 1);
1015 insert(i18n(CommonSet
, CommonAutoHide
, "Auto hide"), 2);
1016 insert(i18n(ToolbarSet
, ToolbarEditWkspcName
,
1017 "Edit current workspace name"), 3);
1024 void Toolbarmenu::setValues() {
1025 setItemSelected(1, toolbar
->isOnTop());
1026 setItemSelected(2, toolbar
->doAutoHide());
1030 Toolbarmenu::~Toolbarmenu(void) {
1031 delete placementmenu
;
1035 void Toolbarmenu::itemSelected(int button
, unsigned int index
) {
1039 BasemenuItem
*item
= find(index
);
1042 switch (item
->function()) {
1043 case 1: { // always on top
1044 toolbar
->saveOnTop(! toolbar
->isOnTop());
1045 setItemSelected(1, toolbar
->isOnTop());
1047 if (toolbar
->isOnTop()) getScreen()->raiseWindows((Window
*) 0, 0);
1051 case 2: { // auto hide
1052 toolbar
->toggleAutoHide();
1053 setItemSelected(2, toolbar
->doAutoHide());
1058 case 3: { // edit current workspace name
1068 void Toolbarmenu::internal_hide(void) {
1069 Basemenu::internal_hide();
1070 if (toolbar
->doAutoHide() && ! toolbar
->isEditing())
1071 toolbar
->hide_handler
.timeout();
1075 void Toolbarmenu::reconfigure(void) {
1077 placementmenu
->reconfigure();
1079 Basemenu::reconfigure();
1083 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu
*tm
)
1084 : Basemenu(tm
->toolbar
->screen
), toolbar(tm
->toolbar
) {
1085 setLabel(i18n(ToolbarSet
, ToolbarToolbarPlacement
, "Toolbar Placement"));
1087 setMinimumSublevels(3);
1089 insert(i18n(CommonSet
, CommonPlacementTopLeft
, "Top Left"),
1091 insert(i18n(CommonSet
, CommonPlacementBottomLeft
, "Bottom Left"),
1092 Toolbar::BottomLeft
);
1093 insert(i18n(CommonSet
, CommonPlacementTopCenter
, "Top Center"),
1094 Toolbar::TopCenter
);
1095 insert(i18n(CommonSet
, CommonPlacementBottomCenter
, "Bottom Center"),
1096 Toolbar::BottomCenter
);
1097 insert(i18n(CommonSet
, CommonPlacementTopRight
, "Top Right"),
1099 insert(i18n(CommonSet
, CommonPlacementBottomRight
, "Bottom Right"),
1100 Toolbar::BottomRight
);
1106 void Toolbarmenu::Placementmenu::setValues(void) {
1108 switch (toolbar
->getPlacement()) {
1109 case Toolbar::BottomRight
:
1111 case Toolbar::TopRight
:
1113 case Toolbar::BottomCenter
:
1115 case Toolbar::TopCenter
:
1117 case Toolbar::BottomLeft
:
1119 case Toolbar::TopLeft
:
1122 setItemSelected(0, 0 == place
);
1123 setItemSelected(1, 1 == place
);
1124 setItemSelected(2, 2 == place
);
1125 setItemSelected(3, 3 == place
);
1126 setItemSelected(4, 4 == place
);
1127 setItemSelected(5, 5 == place
);
1131 void Toolbarmenu::Placementmenu::reconfigure(void) {
1133 Basemenu::reconfigure();
1137 void Toolbarmenu::Placementmenu::itemSelected(int button
, unsigned int index
) {
1141 BasemenuItem
*item
= find(index
);
1144 toolbar
->savePlacement(item
->function());
1146 toolbar
->reconfigure();
1148 // reposition the slit as well to make sure it doesn't intersect the
1150 getScreen()->getSlit()->reposition();
1154 void ToolbarStyle::doJustify(const std::string
&text
, int &start_pos
,
1155 unsigned int max_length
,
1156 unsigned int modifier
) const {
1157 size_t text_len
= text
.size();
1158 unsigned int length
;
1161 length
= font
->measureString(string(text
, 0, text_len
)) + modifier
;
1162 } while (length
> max_length
&& text_len
-- > 0);
1166 start_pos
+= max_length
- length
;
1170 start_pos
+= (max_length
- length
) / 2;