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
) : screen(scrn
), openbox(scrn
.getOpenbox()) {
68 // get the clock updating every minute
69 clock_timer
= new BTimer(openbox
, *this);
71 gettimeofday(&now
, 0);
72 clock_timer
->setTimeout((60 - (now
.tv_sec
% 60)) * 1000);
75 hide_handler
.toolbar
= this;
76 hide_timer
= new BTimer(openbox
, hide_handler
);
77 hide_timer
->setTimeout(openbox
.getAutoRaiseDelay());
78 hide_timer
->fireOnce(True
);
80 image_ctrl
= screen
.getImageControl();
82 on_top
= screen
.isToolbarOnTop();
83 hidden
= do_auto_hide
= screen
.doToolbarAutoHide();
86 new_workspace_name
= (char *) 0;
88 frame
.grab_x
= frame
.grab_y
= 0;
90 toolbarmenu
= new Toolbarmenu(*this);
92 display
= openbox
.getXDisplay();
93 XSetWindowAttributes attrib
;
94 unsigned long create_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
95 CWColormap
| CWOverrideRedirect
| CWEventMask
;
96 attrib
.background_pixmap
= None
;
97 attrib
.background_pixel
= attrib
.border_pixel
=
98 screen
.getBorderColor()->getPixel();
99 attrib
.colormap
= screen
.getColormap();
100 attrib
.override_redirect
= True
;
101 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
102 EnterWindowMask
| LeaveWindowMask
;
105 XCreateWindow(display
, screen
.getRootWindow(), 0, 0, 1, 1, 0,
106 screen
.getDepth(), InputOutput
, screen
.getVisual(),
107 create_mask
, &attrib
);
108 openbox
.saveToolbarSearch(frame
.window
, this);
110 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
| ExposureMask
|
111 KeyPressMask
| EnterWindowMask
;
113 frame
.workspace_label
=
114 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
115 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
116 openbox
.saveToolbarSearch(frame
.workspace_label
, this);
119 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
120 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
121 openbox
.saveToolbarSearch(frame
.window_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
.clock
, this);
129 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
130 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
131 openbox
.saveToolbarSearch(frame
.psbutton
, this);
134 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
135 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
136 openbox
.saveToolbarSearch(frame
.nsbutton
, this);
139 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
140 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
141 openbox
.saveToolbarSearch(frame
.pwbutton
, this);
144 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
145 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
146 openbox
.saveToolbarSearch(frame
.nwbutton
, this);
148 frame
.base
= frame
.label
= frame
.wlabel
= frame
.clk
= frame
.button
=
149 frame
.pbutton
= None
;
153 XMapSubwindows(display
, frame
.window
);
154 XMapWindow(display
, frame
.window
);
158 Toolbar::~Toolbar(void) {
159 XUnmapWindow(display
, frame
.window
);
161 if (frame
.base
) image_ctrl
->removeImage(frame
.base
);
162 if (frame
.label
) image_ctrl
->removeImage(frame
.label
);
163 if (frame
.wlabel
) image_ctrl
->removeImage(frame
.wlabel
);
164 if (frame
.clk
) image_ctrl
->removeImage(frame
.clk
);
165 if (frame
.button
) image_ctrl
->removeImage(frame
.button
);
166 if (frame
.pbutton
) image_ctrl
->removeImage(frame
.pbutton
);
168 openbox
.removeToolbarSearch(frame
.window
);
169 openbox
.removeToolbarSearch(frame
.workspace_label
);
170 openbox
.removeToolbarSearch(frame
.window_label
);
171 openbox
.removeToolbarSearch(frame
.clock
);
172 openbox
.removeToolbarSearch(frame
.psbutton
);
173 openbox
.removeToolbarSearch(frame
.nsbutton
);
174 openbox
.removeToolbarSearch(frame
.pwbutton
);
175 openbox
.removeToolbarSearch(frame
.nwbutton
);
177 XDestroyWindow(display
, frame
.workspace_label
);
178 XDestroyWindow(display
, frame
.window_label
);
179 XDestroyWindow(display
, frame
.clock
);
181 XDestroyWindow(display
, frame
.window
);
189 void Toolbar::reconfigure(void) {
190 frame
.bevel_w
= screen
.getBevelWidth();
191 frame
.width
= screen
.getWidth() * screen
.getToolbarWidthPercent() / 100;
193 if (i18n
->multibyte())
195 screen
.getToolbarStyle()->fontset_extents
->max_ink_extent
.height
;
197 frame
.height
= screen
.getToolbarStyle()->font
->ascent
+
198 screen
.getToolbarStyle()->font
->descent
;
199 frame
.button_w
= frame
.height
;
201 frame
.label_h
= frame
.height
;
202 frame
.height
+= (frame
.bevel_w
* 2);
204 switch (screen
.getToolbarPlacement()) {
209 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
215 frame
.y
= screen
.getHeight() - frame
.height
216 - (screen
.getBorderWidth() * 2);
218 frame
.y_hidden
= screen
.getHeight() - screen
.getBevelWidth()
219 - screen
.getBorderWidth();
223 frame
.x
= (screen
.getWidth() - frame
.width
) / 2;
225 frame
.x_hidden
= frame
.x
;
226 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
232 frame
.x
= (screen
.getWidth() - frame
.width
) / 2;
233 frame
.y
= screen
.getHeight() - frame
.height
234 - (screen
.getBorderWidth() * 2);
235 frame
.x_hidden
= frame
.x
;
236 frame
.y_hidden
= screen
.getHeight() - screen
.getBevelWidth()
237 - screen
.getBorderWidth();
241 frame
.x
= screen
.getWidth() - frame
.width
242 - (screen
.getBorderWidth() * 2);
244 frame
.x_hidden
= frame
.x
;
245 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
250 frame
.x
= screen
.getWidth() - frame
.width
251 - (screen
.getBorderWidth() * 2);
252 frame
.y
= screen
.getHeight() - frame
.height
253 - (screen
.getBorderWidth() * 2);
254 frame
.x_hidden
= frame
.x
;
255 frame
.y_hidden
= screen
.getHeight() - screen
.getBevelWidth()
256 - screen
.getBorderWidth();
261 time_t ttmp
= time(NULL
);
265 tt
= localtime(&ttmp
);
267 char t
[1025], *time_string
= (char *) 0;
268 int len
= strftime(t
, 1024, screen
.getStrftimeFormat(), tt
);
269 t
[len
++-1] = ' '; // add a space to the string for padding
272 if (i18n
->multibyte()) {
273 XRectangle ink
, logical
;
274 XmbTextExtents(screen
.getToolbarStyle()->fontset
, t
, len
, &ink
,
276 frame
.clock_w
= logical
.width
;
278 // ben's additional solution to pad some space beside the numbers
280 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width *
283 // brad's solution, which is currently buggy, too big
285 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width
288 frame
.clock_w
= XTextWidth(screen
.getToolbarStyle()->font
, t
, len
);
289 // ben's additional solution to pad some space beside the numbers
290 //frame.clock_w += screen.getToolbarStyle()->font->max_bounds.width * 4;
291 // brad's solution again, too big
292 //frame.clock_w = screen.getToolbarStyle()->font->max_bounds.width * len;
294 frame
.clock_w
+= (frame
.bevel_w
* 4);
296 delete [] time_string
;
303 #else // !HAVE_STRFTIME
305 XTextWidth(screen
.getToolbarStyle()->font
,
306 i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeLength
,
308 strlen(i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeLength
,
309 "00:00000"))) + (frame
.bevel_w
* 4);
310 #endif // HAVE_STRFTIME
314 frame
.workspace_label_w
= 0;
316 for (i
= 0; i
< screen
.getWorkspaceCount(); i
++) {
317 if (i18n
->multibyte()) {
318 XRectangle ink
, logical
;
319 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
320 screen
.getWorkspace(i
)->getName(),
321 strlen(screen
.getWorkspace(i
)->getName()),
325 w
= XTextWidth(screen
.getToolbarStyle()->font
,
326 screen
.getWorkspace(i
)->getName(),
327 strlen(screen
.getWorkspace(i
)->getName()));
329 w
+= (frame
.bevel_w
* 4);
331 if (w
> frame
.workspace_label_w
) frame
.workspace_label_w
= w
;
334 if (frame
.workspace_label_w
< frame
.clock_w
)
335 frame
.workspace_label_w
= frame
.clock_w
;
336 else if (frame
.workspace_label_w
> frame
.clock_w
)
337 frame
.clock_w
= frame
.workspace_label_w
;
339 frame
.window_label_w
=
340 (frame
.width
- (frame
.clock_w
+ (frame
.button_w
* 4) +
341 frame
.workspace_label_w
+ (frame
.bevel_w
* 8) + 6));
344 XMoveResizeWindow(display
, frame
.window
, frame
.x_hidden
, frame
.y_hidden
,
345 frame
.width
, frame
.height
);
347 XMoveResizeWindow(display
, frame
.window
, frame
.x
, frame
.y
,
348 frame
.width
, frame
.height
);
351 XMoveResizeWindow(display
, frame
.workspace_label
, frame
.bevel_w
,
352 frame
.bevel_w
, frame
.workspace_label_w
,
354 XMoveResizeWindow(display
, frame
.psbutton
, (frame
.bevel_w
* 2) +
355 frame
.workspace_label_w
+ 1, frame
.bevel_w
+ 1,
356 frame
.button_w
, frame
.button_w
);
357 XMoveResizeWindow(display
,frame
.nsbutton
, (frame
.bevel_w
* 3) +
358 frame
.workspace_label_w
+ frame
.button_w
+ 2,
359 frame
.bevel_w
+ 1, frame
.button_w
, frame
.button_w
);
360 XMoveResizeWindow(display
, frame
.window_label
, (frame
.bevel_w
* 4) +
361 (frame
.button_w
* 2) + frame
.workspace_label_w
+ 3,
362 frame
.bevel_w
, frame
.window_label_w
, frame
.label_h
);
363 XMoveResizeWindow(display
, frame
.pwbutton
, (frame
.bevel_w
* 5) +
364 (frame
.button_w
* 2) + frame
.workspace_label_w
+
365 frame
.window_label_w
+ 4, frame
.bevel_w
+ 1,
366 frame
.button_w
, frame
.button_w
);
367 XMoveResizeWindow(display
, frame
.nwbutton
, (frame
.bevel_w
* 6) +
368 (frame
.button_w
* 3) + frame
.workspace_label_w
+
369 frame
.window_label_w
+ 5, frame
.bevel_w
+ 1,
370 frame
.button_w
, frame
.button_w
);
371 XMoveResizeWindow(display
, frame
.clock
, frame
.width
- frame
.clock_w
-
372 frame
.bevel_w
, frame
.bevel_w
, frame
.clock_w
,
375 Pixmap tmp
= frame
.base
;
376 BTexture
*texture
= &(screen
.getToolbarStyle()->toolbar
);
377 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
379 XSetWindowBackground(display
, frame
.window
,
380 texture
->getColor()->getPixel());
383 image_ctrl
->renderImage(frame
.width
, frame
.height
, texture
);
384 XSetWindowBackgroundPixmap(display
, frame
.window
, frame
.base
);
386 if (tmp
) image_ctrl
->removeImage(tmp
);
389 texture
= &(screen
.getToolbarStyle()->window
);
390 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
392 XSetWindowBackground(display
, frame
.window_label
,
393 texture
->getColor()->getPixel());
396 image_ctrl
->renderImage(frame
.window_label_w
, frame
.label_h
, texture
);
397 XSetWindowBackgroundPixmap(display
, frame
.window_label
, frame
.label
);
399 if (tmp
) image_ctrl
->removeImage(tmp
);
402 texture
= &(screen
.getToolbarStyle()->label
);
403 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
405 XSetWindowBackground(display
, frame
.workspace_label
,
406 texture
->getColor()->getPixel());
409 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
410 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
412 if (tmp
) image_ctrl
->removeImage(tmp
);
415 texture
= &(screen
.getToolbarStyle()->clock
);
416 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
418 XSetWindowBackground(display
, frame
.clock
,
419 texture
->getColor()->getPixel());
422 image_ctrl
->renderImage(frame
.clock_w
, frame
.label_h
, texture
);
423 XSetWindowBackgroundPixmap(display
, frame
.clock
, frame
.clk
);
425 if (tmp
) image_ctrl
->removeImage(tmp
);
428 texture
= &(screen
.getToolbarStyle()->button
);
429 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
432 frame
.button_pixel
= texture
->getColor()->getPixel();
433 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
434 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
435 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
436 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
439 image_ctrl
->renderImage(frame
.button_w
, frame
.button_w
, texture
);
441 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
442 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
443 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
444 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
446 if (tmp
) image_ctrl
->removeImage(tmp
);
449 texture
= &(screen
.getToolbarStyle()->pressed
);
450 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
451 frame
.pbutton
= None
;
452 frame
.pbutton_pixel
= texture
->getColor()->getPixel();
455 image_ctrl
->renderImage(frame
.button_w
, frame
.button_w
, texture
);
457 if (tmp
) image_ctrl
->removeImage(tmp
);
459 XSetWindowBorder(display
, frame
.window
,
460 screen
.getBorderColor()->getPixel());
461 XSetWindowBorderWidth(display
, frame
.window
, screen
.getBorderWidth());
463 XClearWindow(display
, frame
.window
);
464 XClearWindow(display
, frame
.workspace_label
);
465 XClearWindow(display
, frame
.window_label
);
466 XClearWindow(display
, frame
.clock
);
467 XClearWindow(display
, frame
.psbutton
);
468 XClearWindow(display
, frame
.nsbutton
);
469 XClearWindow(display
, frame
.pwbutton
);
470 XClearWindow(display
, frame
.nwbutton
);
473 redrawWorkspaceLabel();
474 redrawPrevWorkspaceButton();
475 redrawNextWorkspaceButton();
476 redrawPrevWindowButton();
477 redrawNextWindowButton();
480 toolbarmenu
->reconfigure();
485 void Toolbar::checkClock(Bool redraw
) {
486 #else // !HAVE_STRFTIME
487 void Toolbar::checkClock(Bool redraw
, Bool date
) {
488 #endif // HAVE_STRFTIME
492 if ((tmp
= time(NULL
)) != -1) {
493 if (! (tt
= localtime(&tmp
))) return;
494 if (tt
->tm_min
!= frame
.minute
|| tt
->tm_hour
!= frame
.hour
) {
495 frame
.hour
= tt
->tm_hour
;
496 frame
.minute
= tt
->tm_min
;
497 XClearWindow(display
, frame
.clock
);
505 if (! strftime(t
, 1024, screen
.getStrftimeFormat(), tt
))
507 #else // !HAVE_STRFTIME
510 // format the date... with special consideration for y2k ;)
511 if (screen
.getDateFormat() == Openbox::B_EuropeanDate
)
512 sprintf(t
, 18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeDateFormatEu
,
514 tt
->tm_mday
, tt
->tm_mon
+ 1,
515 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
517 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeDateFormat
,
519 tt
->tm_mon
+ 1, tt
->tm_mday
,
520 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
522 if (screen
.isClock24Hour())
523 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeTimeFormat24
,
525 frame
.hour
, frame
.minute
);
527 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeTimeFormat12
,
529 ((frame
.hour
> 12) ? frame
.hour
- 12 :
530 ((frame
.hour
== 0) ? 12 : frame
.hour
)), frame
.minute
,
531 ((frame
.hour
>= 12) ?
532 i18n
->getMessage(ToolbarSet
,
533 ToolbarNoStrftimeTimeFormatP
, "p") :
534 i18n
->getMessage(ToolbarSet
,
535 ToolbarNoStrftimeTimeFormatA
, "a")));
537 #endif // HAVE_STRFTIME
539 int dx
= (frame
.bevel_w
* 2), dlen
= strlen(t
);
542 if (i18n
->multibyte()) {
543 XRectangle ink
, logical
;
544 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
545 t
, dlen
, &ink
, &logical
);
548 l
= XTextWidth(screen
.getToolbarStyle()->font
, t
, dlen
);
551 l
+= (frame
.bevel_w
* 4);
553 if (l
> frame
.clock_w
) {
554 for (; dlen
>= 0; dlen
--) {
555 if (i18n
->multibyte()) {
556 XRectangle ink
, logical
;
557 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
558 t
, dlen
, &ink
, &logical
);
561 l
= XTextWidth(screen
.getToolbarStyle()->font
, t
, dlen
);
563 l
+= (frame
.bevel_w
* 4);
565 if (l
< frame
.clock_w
)
569 switch (screen
.getToolbarStyle()->justify
) {
570 case BScreen::RightJustify
:
571 dx
+= frame
.clock_w
- l
;
574 case BScreen::CenterJustify
:
575 dx
+= (frame
.clock_w
- l
) / 2;
579 ToolbarStyle
*style
= screen
.getToolbarStyle();
580 if (i18n
->multibyte())
581 XmbDrawString(display
, frame
.clock
, style
->fontset
, style
->c_text_gc
,
582 dx
, (1 - style
->fontset_extents
->max_ink_extent
.y
),
585 XDrawString(display
, frame
.clock
, style
->c_text_gc
, dx
,
586 (style
->font
->ascent
+ 1), t
, dlen
);
591 void Toolbar::redrawWindowLabel(Bool redraw
) {
592 if (screen
.getOpenbox().getFocusedWindow()) {
594 XClearWindow(display
, frame
.window_label
);
596 OpenboxWindow
*foc
= screen
.getOpenbox().getFocusedWindow();
597 if (foc
->getScreen() != &screen
) return;
599 int dx
= (frame
.bevel_w
* 2), dlen
= strlen(*foc
->getTitle());
602 if (i18n
->multibyte()) {
603 XRectangle ink
, logical
;
604 XmbTextExtents(screen
.getToolbarStyle()->fontset
, *foc
->getTitle(),
605 dlen
, &ink
, &logical
);
608 l
= XTextWidth(screen
.getToolbarStyle()->font
, *foc
->getTitle(), dlen
);
610 l
+= (frame
.bevel_w
* 4);
612 if (l
> frame
.window_label_w
) {
613 for (; dlen
>= 0; dlen
--) {
614 if (i18n
->multibyte()) {
615 XRectangle ink
, logical
;
616 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
617 *foc
->getTitle(), dlen
, &ink
, &logical
);
620 l
= XTextWidth(screen
.getToolbarStyle()->font
,
621 *foc
->getTitle(), dlen
);
623 l
+= (frame
.bevel_w
* 4);
625 if (l
< frame
.window_label_w
)
629 switch (screen
.getToolbarStyle()->justify
) {
630 case BScreen::RightJustify
:
631 dx
+= frame
.window_label_w
- l
;
634 case BScreen::CenterJustify
:
635 dx
+= (frame
.window_label_w
- l
) / 2;
639 ToolbarStyle
*style
= screen
.getToolbarStyle();
640 if (i18n
->multibyte())
641 XmbDrawString(display
, frame
.window_label
, style
->fontset
,
642 style
->w_text_gc
, dx
,
643 (1 - style
->fontset_extents
->max_ink_extent
.y
),
644 *foc
->getTitle(), dlen
);
646 XDrawString(display
, frame
.window_label
, style
->w_text_gc
, dx
,
647 (style
->font
->ascent
+ 1), *foc
->getTitle(), dlen
);
649 XClearWindow(display
, frame
.window_label
);
654 void Toolbar::redrawWorkspaceLabel(Bool redraw
) {
655 if (screen
.getCurrentWorkspace()->getName()) {
657 XClearWindow(display
, frame
.workspace_label
);
659 int dx
= (frame
.bevel_w
* 2), dlen
=
660 strlen(screen
.getCurrentWorkspace()->getName());
663 if (i18n
->multibyte()) {
664 XRectangle ink
, logical
;
665 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
666 screen
.getCurrentWorkspace()->getName(), dlen
,
670 l
= XTextWidth(screen
.getToolbarStyle()->font
,
671 screen
.getCurrentWorkspace()->getName(), dlen
);
673 l
+= (frame
.bevel_w
* 4);
675 if (l
> frame
.workspace_label_w
) {
676 for (; dlen
>= 0; dlen
--) {
677 if (i18n
->multibyte()) {
678 XRectangle ink
, logical
;
679 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
680 screen
.getCurrentWorkspace()->getName(), dlen
,
684 l
= XTextWidth(screen
.getWindowStyle()->font
,
685 screen
.getCurrentWorkspace()->getName(), dlen
);
687 l
+= (frame
.bevel_w
* 4);
689 if (l
< frame
.workspace_label_w
)
693 switch (screen
.getToolbarStyle()->justify
) {
694 case BScreen::RightJustify
:
695 dx
+= frame
.workspace_label_w
- l
;
698 case BScreen::CenterJustify
:
699 dx
+= (frame
.workspace_label_w
- l
) / 2;
703 ToolbarStyle
*style
= screen
.getToolbarStyle();
704 if (i18n
->multibyte())
705 XmbDrawString(display
, frame
.workspace_label
, style
->fontset
,
706 style
->l_text_gc
, dx
,
707 (1 - style
->fontset_extents
->max_ink_extent
.y
),
708 (char *) screen
.getCurrentWorkspace()->getName(), dlen
);
710 XDrawString(display
, frame
.workspace_label
, style
->l_text_gc
, dx
,
711 (style
->font
->ascent
+ 1),
712 (char *) screen
.getCurrentWorkspace()->getName(), dlen
);
717 void Toolbar::redrawPrevWorkspaceButton(Bool pressed
, Bool redraw
) {
721 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.pbutton
);
723 XSetWindowBackground(display
, frame
.psbutton
, frame
.pbutton_pixel
);
726 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
728 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
730 XClearWindow(display
, frame
.psbutton
);
733 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
736 pts
[0].x
= hw
- 2; pts
[0].y
= hh
;
737 pts
[1].x
= 4; pts
[1].y
= 2;
738 pts
[2].x
= 0; pts
[2].y
= -4;
740 XFillPolygon(display
, frame
.psbutton
, screen
.getToolbarStyle()->b_pic_gc
,
741 pts
, 3, Convex
, CoordModePrevious
);
745 void Toolbar::redrawNextWorkspaceButton(Bool pressed
, Bool redraw
) {
749 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.pbutton
);
751 XSetWindowBackground(display
, frame
.nsbutton
, frame
.pbutton_pixel
);
754 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
756 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
758 XClearWindow(display
, frame
.nsbutton
);
761 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
764 pts
[0].x
= hw
- 2; pts
[0].y
= hh
- 2;
765 pts
[1].x
= 4; pts
[1].y
= 2;
766 pts
[2].x
= -4; pts
[2].y
= 2;
768 XFillPolygon(display
, frame
.nsbutton
, screen
.getToolbarStyle()->b_pic_gc
,
769 pts
, 3, Convex
, CoordModePrevious
);
773 void Toolbar::redrawPrevWindowButton(Bool pressed
, Bool redraw
) {
777 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.pbutton
);
779 XSetWindowBackground(display
, frame
.pwbutton
, frame
.pbutton_pixel
);
782 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
784 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
786 XClearWindow(display
, frame
.pwbutton
);
789 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
792 pts
[0].x
= hw
- 2; pts
[0].y
= hh
;
793 pts
[1].x
= 4; pts
[1].y
= 2;
794 pts
[2].x
= 0; pts
[2].y
= -4;
796 XFillPolygon(display
, frame
.pwbutton
, screen
.getToolbarStyle()->b_pic_gc
,
797 pts
, 3, Convex
, CoordModePrevious
);
801 void Toolbar::redrawNextWindowButton(Bool pressed
, Bool redraw
) {
805 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.pbutton
);
807 XSetWindowBackground(display
, frame
.nwbutton
, frame
.pbutton_pixel
);
810 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
812 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
814 XClearWindow(display
, frame
.nwbutton
);
817 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
820 pts
[0].x
= hw
- 2; pts
[0].y
= hh
- 2;
821 pts
[1].x
= 4; pts
[1].y
= 2;
822 pts
[2].x
= -4; pts
[2].y
= 2;
824 XFillPolygon(display
, frame
.nwbutton
, screen
.getToolbarStyle()->b_pic_gc
,
825 pts
, 3, Convex
, CoordModePrevious
);
829 void Toolbar::edit(void) {
834 if (XGetInputFocus(display
, &window
, &foo
) &&
835 window
== frame
.workspace_label
)
838 XSetInputFocus(display
, frame
.workspace_label
,
839 ((screen
.isSloppyFocus()) ? RevertToPointerRoot
:
842 XClearWindow(display
, frame
.workspace_label
);
844 openbox
.setNoFocus(True
);
845 if (openbox
.getFocusedWindow())
846 openbox
.getFocusedWindow()->setFocusFlag(False
);
848 XDrawRectangle(display
, frame
.workspace_label
,
849 screen
.getWindowStyle()->l_text_focus_gc
,
850 frame
.workspace_label_w
/ 2, 0, 1,
853 // change the background of the window to that of an active window label
854 Pixmap tmp
= frame
.wlabel
;
855 BTexture
*texture
= &(screen
.getWindowStyle()->l_focus
);
856 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
858 XSetWindowBackground(display
, frame
.workspace_label
,
859 texture
->getColor()->getPixel());
862 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
863 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
865 if (tmp
) image_ctrl
->removeImage(tmp
);
869 void Toolbar::buttonPressEvent(XButtonEvent
*be
) {
870 if (be
->button
== 1) {
871 if (be
->window
== frame
.psbutton
)
872 redrawPrevWorkspaceButton(True
, True
);
873 else if (be
->window
== frame
.nsbutton
)
874 redrawNextWorkspaceButton(True
, True
);
875 else if (be
->window
== frame
.pwbutton
)
876 redrawPrevWindowButton(True
, True
);
877 else if (be
->window
== frame
.nwbutton
)
878 redrawNextWindowButton(True
, True
);
879 #ifndef HAVE_STRFTIME
880 else if (be
->window
== frame
.clock
) {
881 XClearWindow(display
, frame
.clock
);
882 checkClock(True
, True
);
884 #endif // HAVE_STRFTIME
886 Window w
[1] = { frame
.window
};
887 screen
.raiseWindows(w
, 1);
889 } else if (be
->button
== 2 && (! on_top
)) {
890 XLowerWindow(display
, frame
.window
);
891 } else if (be
->button
== 3) {
892 if (! toolbarmenu
->isVisible()) {
895 x
= be
->x_root
- (toolbarmenu
->getWidth() / 2);
896 y
= be
->y_root
- (toolbarmenu
->getHeight() / 2);
900 else if (x
+ toolbarmenu
->getWidth() > screen
.getWidth())
901 x
= screen
.getWidth() - toolbarmenu
->getWidth();
905 else if (y
+ toolbarmenu
->getHeight() > screen
.getHeight())
906 y
= screen
.getHeight() - toolbarmenu
->getHeight();
908 toolbarmenu
->move(x
, y
);
917 void Toolbar::buttonReleaseEvent(XButtonEvent
*re
) {
918 if (re
->button
== 1) {
919 if (re
->window
== frame
.psbutton
) {
920 redrawPrevWorkspaceButton(False
, True
);
922 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
923 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
924 if (screen
.getCurrentWorkspace()->getWorkspaceID() > 0)
925 screen
.changeWorkspaceID(screen
.getCurrentWorkspace()->
926 getWorkspaceID() - 1);
928 screen
.changeWorkspaceID(screen
.getWorkspaceCount() - 1);
929 } else if (re
->window
== frame
.nsbutton
) {
930 redrawNextWorkspaceButton(False
, True
);
932 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
933 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
934 if (screen
.getCurrentWorkspace()->getWorkspaceID() <
935 screen
.getWorkspaceCount() - 1)
936 screen
.changeWorkspaceID(screen
.getCurrentWorkspace()->
937 getWorkspaceID() + 1);
939 screen
.changeWorkspaceID(0);
940 } else if (re
->window
== frame
.pwbutton
) {
941 redrawPrevWindowButton(False
, True
);
943 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
944 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
946 } else if (re
->window
== frame
.nwbutton
) {
947 redrawNextWindowButton(False
, True
);
949 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
950 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
952 } else if (re
->window
== frame
.window_label
)
954 #ifndef HAVE_STRFTIME
955 else if (re
->window
== frame
.clock
) {
956 XClearWindow(display
, frame
.clock
);
959 #endif // HAVE_STRFTIME
964 void Toolbar::enterNotifyEvent(XCrossingEvent
*) {
969 if (! hide_timer
->isTiming()) hide_timer
->start();
971 if (hide_timer
->isTiming()) hide_timer
->stop();
975 void Toolbar::leaveNotifyEvent(XCrossingEvent
*) {
980 if (hide_timer
->isTiming()) hide_timer
->stop();
981 } else if (! toolbarmenu
->isVisible()) {
982 if (! hide_timer
->isTiming()) hide_timer
->start();
987 void Toolbar::exposeEvent(XExposeEvent
*ee
) {
988 if (ee
->window
== frame
.clock
) checkClock(True
);
989 else if (ee
->window
== frame
.workspace_label
&& (! editing
))
990 redrawWorkspaceLabel();
991 else if (ee
->window
== frame
.window_label
) redrawWindowLabel();
992 else if (ee
->window
== frame
.psbutton
) redrawPrevWorkspaceButton();
993 else if (ee
->window
== frame
.nsbutton
) redrawNextWorkspaceButton();
994 else if (ee
->window
== frame
.pwbutton
) redrawPrevWindowButton();
995 else if (ee
->window
== frame
.nwbutton
) redrawNextWindowButton();
999 void Toolbar::keyPressEvent(XKeyEvent
*ke
) {
1000 if (ke
->window
== frame
.workspace_label
&& editing
) {
1003 if (! new_workspace_name
) {
1004 new_workspace_name
= new char[128];
1007 if (! new_workspace_name
) return;
1012 XLookupString(ke
, keychar
, 1, &ks
, 0);
1014 // either we are told to end with a return or we hit the end of the buffer
1015 if (ks
== XK_Return
|| new_name_pos
== 127) {
1016 *(new_workspace_name
+ new_name_pos
) = 0;
1020 openbox
.setNoFocus(False
);
1021 if (openbox
.getFocusedWindow()) {
1022 openbox
.getFocusedWindow()->setInputFocus();
1023 openbox
.getFocusedWindow()->setFocusFlag(True
);
1025 XSetInputFocus(display
, PointerRoot
, None
, CurrentTime
);
1027 // check to make sure that new_name[0] != 0... otherwise we have a null
1028 // workspace name which causes serious problems, especially for the
1029 // Openbox::LoadRC() method.
1030 if (*new_workspace_name
) {
1031 screen
.getCurrentWorkspace()->setName(new_workspace_name
);
1032 screen
.getCurrentWorkspace()->getMenu()->hide();
1033 screen
.getWorkspacemenu()->
1034 remove(screen
.getCurrentWorkspace()->getWorkspaceID() + 2);
1035 screen
.getWorkspacemenu()->
1036 insert(screen
.getCurrentWorkspace()->getName(),
1037 screen
.getCurrentWorkspace()->getMenu(),
1038 screen
.getCurrentWorkspace()->getWorkspaceID() + 2);
1039 screen
.getWorkspacemenu()->update();
1042 delete [] new_workspace_name
;
1043 new_workspace_name
= (char *) 0;
1046 // reset the background to that of the workspace label (its normal
1048 Pixmap tmp
= frame
.wlabel
;
1049 BTexture
*texture
= &(screen
.getToolbarStyle()->label
);
1050 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
1051 frame
.wlabel
= None
;
1052 XSetWindowBackground(display
, frame
.workspace_label
,
1053 texture
->getColor()->getPixel());
1056 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
1057 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
1059 if (tmp
) image_ctrl
->removeImage(tmp
);
1062 } else if (! (ks
== XK_Shift_L
|| ks
== XK_Shift_R
||
1063 ks
== XK_Control_L
|| ks
== XK_Control_R
||
1064 ks
== XK_Caps_Lock
|| ks
== XK_Shift_Lock
||
1065 ks
== XK_Meta_L
|| ks
== XK_Meta_R
||
1066 ks
== XK_Alt_L
|| ks
== XK_Alt_R
||
1067 ks
== XK_Super_L
|| ks
== XK_Super_R
||
1068 ks
== XK_Hyper_L
|| ks
== XK_Hyper_R
)) {
1069 if (ks
== XK_BackSpace
) {
1070 if (new_name_pos
> 0) {
1072 *(new_workspace_name
+ new_name_pos
) = '\0';
1074 *new_workspace_name
= '\0';
1077 *(new_workspace_name
+ new_name_pos
) = *keychar
;
1079 *(new_workspace_name
+ new_name_pos
) = '\0';
1082 XClearWindow(display
, frame
.workspace_label
);
1083 int l
= strlen(new_workspace_name
), tw
, x
;
1085 if (i18n
->multibyte()) {
1086 XRectangle ink
, logical
;
1087 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
1088 new_workspace_name
, l
, &ink
, &logical
);
1091 tw
= XTextWidth(screen
.getToolbarStyle()->font
,
1092 new_workspace_name
, l
);
1094 x
= (frame
.workspace_label_w
- tw
) / 2;
1096 if (x
< (signed) frame
.bevel_w
) x
= frame
.bevel_w
;
1098 WindowStyle
*style
= screen
.getWindowStyle();
1099 if (i18n
->multibyte())
1100 XmbDrawString(display
, frame
.workspace_label
, style
->fontset
,
1101 style
->l_text_focus_gc
, x
,
1102 (1 - style
->fontset_extents
->max_ink_extent
.y
),
1103 new_workspace_name
, l
);
1105 XDrawString(display
, frame
.workspace_label
, style
->l_text_focus_gc
, x
,
1106 (style
->font
->ascent
+ 1),
1107 new_workspace_name
, l
);
1109 XDrawRectangle(display
, frame
.workspace_label
,
1110 screen
.getWindowStyle()->l_text_focus_gc
, x
+ tw
, 0, 1,
1119 void Toolbar::timeout(void) {
1123 gettimeofday(&now
, 0);
1124 clock_timer
->setTimeout((60 - (now
.tv_sec
% 60)) * 1000);
1128 void Toolbar::HideHandler::timeout(void) {
1129 toolbar
->hidden
= ! toolbar
->hidden
;
1130 if (toolbar
->hidden
)
1131 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
1132 toolbar
->frame
.x_hidden
, toolbar
->frame
.y_hidden
);
1134 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
1135 toolbar
->frame
.x
, toolbar
->frame
.y
);
1139 Toolbarmenu::Toolbarmenu(Toolbar
&tb
) : Basemenu(tb
.screen
), toolbar(tb
) {
1140 setLabel(i18n
->getMessage(ToolbarSet
, ToolbarToolbarTitle
, "Toolbar"));
1143 placementmenu
= new Placementmenu(*this);
1145 insert(i18n
->getMessage(CommonSet
, CommonPlacementTitle
, "Placement"),
1147 insert(i18n
->getMessage(CommonSet
, CommonAlwaysOnTop
, "Always on top"), 1);
1148 insert(i18n
->getMessage(CommonSet
, CommonAutoHide
, "Auto hide"), 2);
1149 insert(i18n
->getMessage(ToolbarSet
, ToolbarEditWkspcName
,
1150 "Edit current workspace name"), 3);
1154 if (toolbar
.isOnTop()) setItemSelected(1, True
);
1155 if (toolbar
.doAutoHide()) setItemSelected(2, True
);
1159 Toolbarmenu::~Toolbarmenu(void) {
1160 delete placementmenu
;
1164 void Toolbarmenu::itemSelected(int button
, int index
) {
1168 BasemenuItem
*item
= find(index
);
1171 switch (item
->function()) {
1172 case 1: { // always on top
1173 Bool change
= ((toolbar
.isOnTop()) ? False
: True
);
1174 toolbar
.on_top
= change
;
1175 setItemSelected(1, change
);
1177 if (toolbar
.isOnTop()) toolbar
.screen
.raiseWindows((Window
*) 0, 0);
1181 case 2: { // auto hide
1182 Bool change
= ((toolbar
.doAutoHide()) ? False
: True
);
1183 toolbar
.do_auto_hide
= change
;
1184 setItemSelected(2, change
);
1187 toolbar
.screen
.getSlit()->reposition();
1192 case 3: { // edit current workspace name
1202 void Toolbarmenu::internal_hide(void) {
1203 Basemenu::internal_hide();
1204 if (toolbar
.doAutoHide() && ! toolbar
.isEditing())
1205 toolbar
.hide_handler
.timeout();
1209 void Toolbarmenu::reconfigure(void) {
1210 placementmenu
->reconfigure();
1212 Basemenu::reconfigure();
1216 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu
&tm
)
1217 : Basemenu(tm
.toolbar
.screen
), toolbarmenu(tm
) {
1218 setLabel(i18n
->getMessage(ToolbarSet
, ToolbarToolbarPlacement
,
1219 "Toolbar Placement"));
1221 setMinimumSublevels(3);
1223 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopLeft
,
1224 "Top Left"), Toolbar::TopLeft
);
1225 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomLeft
,
1226 "Bottom Left"), Toolbar::BottomLeft
);
1227 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopCenter
,
1228 "Top Center"), Toolbar::TopCenter
);
1229 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomCenter
,
1230 "Bottom Center"), Toolbar::BottomCenter
);
1231 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopRight
,
1232 "Top Right"), Toolbar::TopRight
);
1233 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomRight
,
1234 "Bottom Right"), Toolbar::BottomRight
);
1239 void Toolbarmenu::Placementmenu::itemSelected(int button
, int index
) {
1243 BasemenuItem
*item
= find(index
);
1246 toolbarmenu
.toolbar
.screen
.saveToolbarPlacement(item
->function());
1248 toolbarmenu
.toolbar
.reconfigure();
1251 // reposition the slit as well to make sure it doesn't intersect the
1253 toolbarmenu
.toolbar
.screen
.getSlit()->reposition();