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
66 Toolbar::Toolbar(BScreen
&scrn
) : screen(scrn
), openbox(scrn
.getOpenbox()) {
67 // get the clock updating every minute
68 clock_timer
= new BTimer(openbox
, *this);
70 gettimeofday(&now
, 0);
71 clock_timer
->setTimeout((60 - (now
.tv_sec
% 60)) * 1000);
74 hide_handler
.toolbar
= this;
75 hide_timer
= new BTimer(openbox
, hide_handler
);
76 hide_timer
->setTimeout(openbox
.getAutoRaiseDelay());
77 hide_timer
->fireOnce(True
);
79 image_ctrl
= screen
.getImageControl();
81 on_top
= screen
.isToolbarOnTop();
82 hidden
= do_auto_hide
= screen
.doToolbarAutoHide();
85 new_workspace_name
= (char *) 0;
87 frame
.grab_x
= frame
.grab_y
= 0;
89 toolbarmenu
= new Toolbarmenu(*this);
91 display
= openbox
.getXDisplay();
92 XSetWindowAttributes attrib
;
93 unsigned long create_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
94 CWColormap
| CWOverrideRedirect
| CWEventMask
;
95 attrib
.background_pixmap
= None
;
96 attrib
.background_pixel
= attrib
.border_pixel
=
97 screen
.getBorderColor()->getPixel();
98 attrib
.colormap
= screen
.getColormap();
99 attrib
.override_redirect
= True
;
100 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
101 EnterWindowMask
| LeaveWindowMask
;
104 XCreateWindow(display
, screen
.getRootWindow(), 0, 0, 1, 1, 0,
105 screen
.getDepth(), InputOutput
, screen
.getVisual(),
106 create_mask
, &attrib
);
107 openbox
.saveToolbarSearch(frame
.window
, this);
109 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
| ExposureMask
|
110 KeyPressMask
| EnterWindowMask
;
112 frame
.workspace_label
=
113 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
114 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
115 openbox
.saveToolbarSearch(frame
.workspace_label
, this);
118 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
119 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
120 openbox
.saveToolbarSearch(frame
.window_label
, this);
123 XCreateWindow(display
, frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
124 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
125 openbox
.saveToolbarSearch(frame
.clock
, this);
128 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
129 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
130 openbox
.saveToolbarSearch(frame
.psbutton
, this);
133 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
134 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
135 openbox
.saveToolbarSearch(frame
.nsbutton
, this);
138 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
139 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
140 openbox
.saveToolbarSearch(frame
.pwbutton
, this);
143 XCreateWindow(display
,frame
.window
, 0, 0, 1, 1, 0, screen
.getDepth(),
144 InputOutput
, screen
.getVisual(), create_mask
, &attrib
);
145 openbox
.saveToolbarSearch(frame
.nwbutton
, this);
147 frame
.base
= frame
.label
= frame
.wlabel
= frame
.clk
= frame
.button
=
148 frame
.pbutton
= None
;
152 XMapSubwindows(display
, frame
.window
);
153 XMapWindow(display
, frame
.window
);
157 Toolbar::~Toolbar(void) {
158 XUnmapWindow(display
, frame
.window
);
160 if (frame
.base
) image_ctrl
->removeImage(frame
.base
);
161 if (frame
.label
) image_ctrl
->removeImage(frame
.label
);
162 if (frame
.wlabel
) image_ctrl
->removeImage(frame
.wlabel
);
163 if (frame
.clk
) image_ctrl
->removeImage(frame
.clk
);
164 if (frame
.button
) image_ctrl
->removeImage(frame
.button
);
165 if (frame
.pbutton
) image_ctrl
->removeImage(frame
.pbutton
);
167 openbox
.removeToolbarSearch(frame
.window
);
168 openbox
.removeToolbarSearch(frame
.workspace_label
);
169 openbox
.removeToolbarSearch(frame
.window_label
);
170 openbox
.removeToolbarSearch(frame
.clock
);
171 openbox
.removeToolbarSearch(frame
.psbutton
);
172 openbox
.removeToolbarSearch(frame
.nsbutton
);
173 openbox
.removeToolbarSearch(frame
.pwbutton
);
174 openbox
.removeToolbarSearch(frame
.nwbutton
);
176 XDestroyWindow(display
, frame
.workspace_label
);
177 XDestroyWindow(display
, frame
.window_label
);
178 XDestroyWindow(display
, frame
.clock
);
180 XDestroyWindow(display
, frame
.window
);
188 void Toolbar::reconfigure(void) {
189 frame
.bevel_w
= screen
.getBevelWidth();
190 frame
.width
= screen
.size().w() * screen
.getToolbarWidthPercent() / 100;
192 if (i18n
->multibyte())
194 screen
.getToolbarStyle()->fontset_extents
->max_ink_extent
.height
;
196 frame
.height
= screen
.getToolbarStyle()->font
->ascent
+
197 screen
.getToolbarStyle()->font
->descent
;
198 frame
.button_w
= frame
.height
;
200 frame
.label_h
= frame
.height
;
201 frame
.height
+= (frame
.bevel_w
* 2);
203 switch (screen
.getToolbarPlacement()) {
208 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
214 frame
.y
= screen
.size().h() - frame
.height
215 - (screen
.getBorderWidth() * 2);
217 frame
.y_hidden
= screen
.size().h() - screen
.getBevelWidth()
218 - screen
.getBorderWidth();
222 frame
.x
= (screen
.size().w() - frame
.width
) / 2;
224 frame
.x_hidden
= frame
.x
;
225 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
231 frame
.x
= (screen
.size().w() - frame
.width
) / 2;
232 frame
.y
= screen
.size().h() - frame
.height
233 - (screen
.getBorderWidth() * 2);
234 frame
.x_hidden
= frame
.x
;
235 frame
.y_hidden
= screen
.size().h() - screen
.getBevelWidth()
236 - screen
.getBorderWidth();
240 frame
.x
= screen
.size().w() - frame
.width
241 - (screen
.getBorderWidth() * 2);
243 frame
.x_hidden
= frame
.x
;
244 frame
.y_hidden
= screen
.getBevelWidth() - screen
.getBorderWidth()
249 frame
.x
= screen
.size().w() - frame
.width
250 - (screen
.getBorderWidth() * 2);
251 frame
.y
= screen
.size().h() - frame
.height
252 - (screen
.getBorderWidth() * 2);
253 frame
.x_hidden
= frame
.x
;
254 frame
.y_hidden
= screen
.size().h() - screen
.getBevelWidth()
255 - screen
.getBorderWidth();
260 time_t ttmp
= time(NULL
);
264 tt
= localtime(&ttmp
);
266 char t
[1025], *time_string
= (char *) 0;
267 int len
= strftime(t
, 1024, screen
.getStrftimeFormat(), tt
);
268 t
[len
++-1] = ' '; // add a space to the string for padding
271 if (i18n
->multibyte()) {
272 XRectangle ink
, logical
;
273 XmbTextExtents(screen
.getToolbarStyle()->fontset
, t
, len
, &ink
,
275 frame
.clock_w
= logical
.width
;
277 // ben's additional solution to pad some space beside the numbers
279 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width *
282 // brad's solution, which is currently buggy, too big
284 // screen.getToolbarStyle()->fontset_extents->max_logical_extent.width
287 frame
.clock_w
= XTextWidth(screen
.getToolbarStyle()->font
, t
, len
);
288 // ben's additional solution to pad some space beside the numbers
289 //frame.clock_w += screen.getToolbarStyle()->font->max_bounds.width * 4;
290 // brad's solution again, too big
291 //frame.clock_w = screen.getToolbarStyle()->font->max_bounds.width * len;
293 frame
.clock_w
+= (frame
.bevel_w
* 4);
295 delete [] time_string
;
302 #else // !HAVE_STRFTIME
304 XTextWidth(screen
.getToolbarStyle()->font
,
305 i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeLength
,
307 strlen(i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeLength
,
308 "00:00000"))) + (frame
.bevel_w
* 4);
309 #endif // HAVE_STRFTIME
313 frame
.workspace_label_w
= 0;
315 for (i
= 0; i
< screen
.getWorkspaceCount(); i
++) {
316 if (i18n
->multibyte()) {
317 XRectangle ink
, logical
;
318 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
319 screen
.getWorkspace(i
)->getName(),
320 strlen(screen
.getWorkspace(i
)->getName()),
324 w
= XTextWidth(screen
.getToolbarStyle()->font
,
325 screen
.getWorkspace(i
)->getName(),
326 strlen(screen
.getWorkspace(i
)->getName()));
328 w
+= (frame
.bevel_w
* 4);
330 if (w
> frame
.workspace_label_w
) frame
.workspace_label_w
= w
;
333 if (frame
.workspace_label_w
< frame
.clock_w
)
334 frame
.workspace_label_w
= frame
.clock_w
;
335 else if (frame
.workspace_label_w
> frame
.clock_w
)
336 frame
.clock_w
= frame
.workspace_label_w
;
338 frame
.window_label_w
=
339 (frame
.width
- (frame
.clock_w
+ (frame
.button_w
* 4) +
340 frame
.workspace_label_w
+ (frame
.bevel_w
* 8) + 6));
343 XMoveResizeWindow(display
, frame
.window
, frame
.x_hidden
, frame
.y_hidden
,
344 frame
.width
, frame
.height
);
346 XMoveResizeWindow(display
, frame
.window
, frame
.x
, frame
.y
,
347 frame
.width
, frame
.height
);
350 XMoveResizeWindow(display
, frame
.workspace_label
, frame
.bevel_w
,
351 frame
.bevel_w
, frame
.workspace_label_w
,
353 XMoveResizeWindow(display
, frame
.psbutton
, (frame
.bevel_w
* 2) +
354 frame
.workspace_label_w
+ 1, frame
.bevel_w
+ 1,
355 frame
.button_w
, frame
.button_w
);
356 XMoveResizeWindow(display
,frame
.nsbutton
, (frame
.bevel_w
* 3) +
357 frame
.workspace_label_w
+ frame
.button_w
+ 2,
358 frame
.bevel_w
+ 1, frame
.button_w
, frame
.button_w
);
359 XMoveResizeWindow(display
, frame
.window_label
, (frame
.bevel_w
* 4) +
360 (frame
.button_w
* 2) + frame
.workspace_label_w
+ 3,
361 frame
.bevel_w
, frame
.window_label_w
, frame
.label_h
);
362 XMoveResizeWindow(display
, frame
.pwbutton
, (frame
.bevel_w
* 5) +
363 (frame
.button_w
* 2) + frame
.workspace_label_w
+
364 frame
.window_label_w
+ 4, frame
.bevel_w
+ 1,
365 frame
.button_w
, frame
.button_w
);
366 XMoveResizeWindow(display
, frame
.nwbutton
, (frame
.bevel_w
* 6) +
367 (frame
.button_w
* 3) + frame
.workspace_label_w
+
368 frame
.window_label_w
+ 5, frame
.bevel_w
+ 1,
369 frame
.button_w
, frame
.button_w
);
370 XMoveResizeWindow(display
, frame
.clock
, frame
.width
- frame
.clock_w
-
371 frame
.bevel_w
, frame
.bevel_w
, frame
.clock_w
,
374 Pixmap tmp
= frame
.base
;
375 BTexture
*texture
= &(screen
.getToolbarStyle()->toolbar
);
376 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
378 XSetWindowBackground(display
, frame
.window
,
379 texture
->getColor()->getPixel());
382 image_ctrl
->renderImage(frame
.width
, frame
.height
, texture
);
383 XSetWindowBackgroundPixmap(display
, frame
.window
, frame
.base
);
385 if (tmp
) image_ctrl
->removeImage(tmp
);
388 texture
= &(screen
.getToolbarStyle()->window
);
389 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
391 XSetWindowBackground(display
, frame
.window_label
,
392 texture
->getColor()->getPixel());
395 image_ctrl
->renderImage(frame
.window_label_w
, frame
.label_h
, texture
);
396 XSetWindowBackgroundPixmap(display
, frame
.window_label
, frame
.label
);
398 if (tmp
) image_ctrl
->removeImage(tmp
);
401 texture
= &(screen
.getToolbarStyle()->label
);
402 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
404 XSetWindowBackground(display
, frame
.workspace_label
,
405 texture
->getColor()->getPixel());
408 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
409 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
411 if (tmp
) image_ctrl
->removeImage(tmp
);
414 texture
= &(screen
.getToolbarStyle()->clock
);
415 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
417 XSetWindowBackground(display
, frame
.clock
,
418 texture
->getColor()->getPixel());
421 image_ctrl
->renderImage(frame
.clock_w
, frame
.label_h
, texture
);
422 XSetWindowBackgroundPixmap(display
, frame
.clock
, frame
.clk
);
424 if (tmp
) image_ctrl
->removeImage(tmp
);
427 texture
= &(screen
.getToolbarStyle()->button
);
428 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
431 frame
.button_pixel
= texture
->getColor()->getPixel();
432 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
433 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
434 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
435 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
438 image_ctrl
->renderImage(frame
.button_w
, frame
.button_w
, texture
);
440 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
441 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
442 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
443 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
445 if (tmp
) image_ctrl
->removeImage(tmp
);
448 texture
= &(screen
.getToolbarStyle()->pressed
);
449 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
450 frame
.pbutton
= None
;
451 frame
.pbutton_pixel
= texture
->getColor()->getPixel();
454 image_ctrl
->renderImage(frame
.button_w
, frame
.button_w
, texture
);
456 if (tmp
) image_ctrl
->removeImage(tmp
);
458 XSetWindowBorder(display
, frame
.window
,
459 screen
.getBorderColor()->getPixel());
460 XSetWindowBorderWidth(display
, frame
.window
, screen
.getBorderWidth());
462 XClearWindow(display
, frame
.window
);
463 XClearWindow(display
, frame
.workspace_label
);
464 XClearWindow(display
, frame
.window_label
);
465 XClearWindow(display
, frame
.clock
);
466 XClearWindow(display
, frame
.psbutton
);
467 XClearWindow(display
, frame
.nsbutton
);
468 XClearWindow(display
, frame
.pwbutton
);
469 XClearWindow(display
, frame
.nwbutton
);
472 redrawWorkspaceLabel();
473 redrawPrevWorkspaceButton();
474 redrawNextWorkspaceButton();
475 redrawPrevWindowButton();
476 redrawNextWindowButton();
479 toolbarmenu
->reconfigure();
484 void Toolbar::checkClock(Bool redraw
) {
485 #else // !HAVE_STRFTIME
486 void Toolbar::checkClock(Bool redraw
, Bool date
) {
487 #endif // HAVE_STRFTIME
491 if ((tmp
= time(NULL
)) != -1) {
492 if (! (tt
= localtime(&tmp
))) return;
493 if (tt
->tm_min
!= frame
.minute
|| tt
->tm_hour
!= frame
.hour
) {
494 frame
.hour
= tt
->tm_hour
;
495 frame
.minute
= tt
->tm_min
;
496 XClearWindow(display
, frame
.clock
);
504 if (! strftime(t
, 1024, screen
.getStrftimeFormat(), tt
))
506 #else // !HAVE_STRFTIME
509 // format the date... with special consideration for y2k ;)
510 if (screen
.getDateFormat() == Openbox::B_EuropeanDate
)
511 sprintf(t
, 18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeDateFormatEu
,
513 tt
->tm_mday
, tt
->tm_mon
+ 1,
514 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
516 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeDateFormat
,
518 tt
->tm_mon
+ 1, tt
->tm_mday
,
519 (tt
->tm_year
>= 100) ? tt
->tm_year
- 100 : tt
->tm_year
);
521 if (screen
.isClock24Hour())
522 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeTimeFormat24
,
524 frame
.hour
, frame
.minute
);
526 sprintf(t
, i18n
->getMessage(ToolbarSet
, ToolbarNoStrftimeTimeFormat12
,
528 ((frame
.hour
> 12) ? frame
.hour
- 12 :
529 ((frame
.hour
== 0) ? 12 : frame
.hour
)), frame
.minute
,
530 ((frame
.hour
>= 12) ?
531 i18n
->getMessage(ToolbarSet
,
532 ToolbarNoStrftimeTimeFormatP
, "p") :
533 i18n
->getMessage(ToolbarSet
,
534 ToolbarNoStrftimeTimeFormatA
, "a")));
536 #endif // HAVE_STRFTIME
538 int dx
= (frame
.bevel_w
* 2), dlen
= strlen(t
);
541 if (i18n
->multibyte()) {
542 XRectangle ink
, logical
;
543 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
544 t
, dlen
, &ink
, &logical
);
547 l
= XTextWidth(screen
.getToolbarStyle()->font
, t
, dlen
);
550 l
+= (frame
.bevel_w
* 4);
552 if (l
> frame
.clock_w
) {
553 for (; dlen
>= 0; dlen
--) {
554 if (i18n
->multibyte()) {
555 XRectangle ink
, logical
;
556 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
557 t
, dlen
, &ink
, &logical
);
560 l
= XTextWidth(screen
.getToolbarStyle()->font
, t
, dlen
);
562 l
+= (frame
.bevel_w
* 4);
564 if (l
< frame
.clock_w
)
568 switch (screen
.getToolbarStyle()->justify
) {
569 case BScreen::RightJustify
:
570 dx
+= frame
.clock_w
- l
;
573 case BScreen::CenterJustify
:
574 dx
+= (frame
.clock_w
- l
) / 2;
578 ToolbarStyle
*style
= screen
.getToolbarStyle();
579 if (i18n
->multibyte())
580 XmbDrawString(display
, frame
.clock
, style
->fontset
, style
->c_text_gc
,
581 dx
, (1 - style
->fontset_extents
->max_ink_extent
.y
),
584 XDrawString(display
, frame
.clock
, style
->c_text_gc
, dx
,
585 (style
->font
->ascent
+ 1), t
, dlen
);
590 void Toolbar::redrawWindowLabel(Bool redraw
) {
591 if (screen
.getOpenbox().getFocusedWindow()) {
593 XClearWindow(display
, frame
.window_label
);
595 OpenboxWindow
*foc
= screen
.getOpenbox().getFocusedWindow();
596 if (foc
->getScreen() != &screen
) return;
598 int dx
= (frame
.bevel_w
* 2), dlen
= strlen(*foc
->getTitle());
601 if (i18n
->multibyte()) {
602 XRectangle ink
, logical
;
603 XmbTextExtents(screen
.getToolbarStyle()->fontset
, *foc
->getTitle(),
604 dlen
, &ink
, &logical
);
607 l
= XTextWidth(screen
.getToolbarStyle()->font
, *foc
->getTitle(), dlen
);
609 l
+= (frame
.bevel_w
* 4);
611 if (l
> frame
.window_label_w
) {
612 for (; dlen
>= 0; dlen
--) {
613 if (i18n
->multibyte()) {
614 XRectangle ink
, logical
;
615 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
616 *foc
->getTitle(), dlen
, &ink
, &logical
);
619 l
= XTextWidth(screen
.getToolbarStyle()->font
,
620 *foc
->getTitle(), dlen
);
622 l
+= (frame
.bevel_w
* 4);
624 if (l
< frame
.window_label_w
)
628 switch (screen
.getToolbarStyle()->justify
) {
629 case BScreen::RightJustify
:
630 dx
+= frame
.window_label_w
- l
;
633 case BScreen::CenterJustify
:
634 dx
+= (frame
.window_label_w
- l
) / 2;
638 ToolbarStyle
*style
= screen
.getToolbarStyle();
639 if (i18n
->multibyte())
640 XmbDrawString(display
, frame
.window_label
, style
->fontset
,
641 style
->w_text_gc
, dx
,
642 (1 - style
->fontset_extents
->max_ink_extent
.y
),
643 *foc
->getTitle(), dlen
);
645 XDrawString(display
, frame
.window_label
, style
->w_text_gc
, dx
,
646 (style
->font
->ascent
+ 1), *foc
->getTitle(), dlen
);
648 XClearWindow(display
, frame
.window_label
);
653 void Toolbar::redrawWorkspaceLabel(Bool redraw
) {
654 if (screen
.getCurrentWorkspace()->getName()) {
656 XClearWindow(display
, frame
.workspace_label
);
658 int dx
= (frame
.bevel_w
* 2), dlen
=
659 strlen(screen
.getCurrentWorkspace()->getName());
662 if (i18n
->multibyte()) {
663 XRectangle ink
, logical
;
664 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
665 screen
.getCurrentWorkspace()->getName(), dlen
,
669 l
= XTextWidth(screen
.getToolbarStyle()->font
,
670 screen
.getCurrentWorkspace()->getName(), dlen
);
672 l
+= (frame
.bevel_w
* 4);
674 if (l
> frame
.workspace_label_w
) {
675 for (; dlen
>= 0; dlen
--) {
676 if (i18n
->multibyte()) {
677 XRectangle ink
, logical
;
678 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
679 screen
.getCurrentWorkspace()->getName(), dlen
,
683 l
= XTextWidth(screen
.getWindowStyle()->font
,
684 screen
.getCurrentWorkspace()->getName(), dlen
);
686 l
+= (frame
.bevel_w
* 4);
688 if (l
< frame
.workspace_label_w
)
692 switch (screen
.getToolbarStyle()->justify
) {
693 case BScreen::RightJustify
:
694 dx
+= frame
.workspace_label_w
- l
;
697 case BScreen::CenterJustify
:
698 dx
+= (frame
.workspace_label_w
- l
) / 2;
702 ToolbarStyle
*style
= screen
.getToolbarStyle();
703 if (i18n
->multibyte())
704 XmbDrawString(display
, frame
.workspace_label
, style
->fontset
,
705 style
->l_text_gc
, dx
,
706 (1 - style
->fontset_extents
->max_ink_extent
.y
),
707 (char *) screen
.getCurrentWorkspace()->getName(), dlen
);
709 XDrawString(display
, frame
.workspace_label
, style
->l_text_gc
, dx
,
710 (style
->font
->ascent
+ 1),
711 (char *) screen
.getCurrentWorkspace()->getName(), dlen
);
716 void Toolbar::redrawPrevWorkspaceButton(Bool pressed
, Bool redraw
) {
720 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.pbutton
);
722 XSetWindowBackground(display
, frame
.psbutton
, frame
.pbutton_pixel
);
725 XSetWindowBackgroundPixmap(display
, frame
.psbutton
, frame
.button
);
727 XSetWindowBackground(display
, frame
.psbutton
, frame
.button_pixel
);
729 XClearWindow(display
, frame
.psbutton
);
732 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
735 pts
[0].x
= hw
- 2; pts
[0].y
= hh
;
736 pts
[1].x
= 4; pts
[1].y
= 2;
737 pts
[2].x
= 0; pts
[2].y
= -4;
739 XFillPolygon(display
, frame
.psbutton
, screen
.getToolbarStyle()->b_pic_gc
,
740 pts
, 3, Convex
, CoordModePrevious
);
744 void Toolbar::redrawNextWorkspaceButton(Bool pressed
, Bool redraw
) {
748 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.pbutton
);
750 XSetWindowBackground(display
, frame
.nsbutton
, frame
.pbutton_pixel
);
753 XSetWindowBackgroundPixmap(display
, frame
.nsbutton
, frame
.button
);
755 XSetWindowBackground(display
, frame
.nsbutton
, frame
.button_pixel
);
757 XClearWindow(display
, frame
.nsbutton
);
760 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
763 pts
[0].x
= hw
- 2; pts
[0].y
= hh
- 2;
764 pts
[1].x
= 4; pts
[1].y
= 2;
765 pts
[2].x
= -4; pts
[2].y
= 2;
767 XFillPolygon(display
, frame
.nsbutton
, screen
.getToolbarStyle()->b_pic_gc
,
768 pts
, 3, Convex
, CoordModePrevious
);
772 void Toolbar::redrawPrevWindowButton(Bool pressed
, Bool redraw
) {
776 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.pbutton
);
778 XSetWindowBackground(display
, frame
.pwbutton
, frame
.pbutton_pixel
);
781 XSetWindowBackgroundPixmap(display
, frame
.pwbutton
, frame
.button
);
783 XSetWindowBackground(display
, frame
.pwbutton
, frame
.button_pixel
);
785 XClearWindow(display
, frame
.pwbutton
);
788 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
791 pts
[0].x
= hw
- 2; pts
[0].y
= hh
;
792 pts
[1].x
= 4; pts
[1].y
= 2;
793 pts
[2].x
= 0; pts
[2].y
= -4;
795 XFillPolygon(display
, frame
.pwbutton
, screen
.getToolbarStyle()->b_pic_gc
,
796 pts
, 3, Convex
, CoordModePrevious
);
800 void Toolbar::redrawNextWindowButton(Bool pressed
, Bool redraw
) {
804 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.pbutton
);
806 XSetWindowBackground(display
, frame
.nwbutton
, frame
.pbutton_pixel
);
809 XSetWindowBackgroundPixmap(display
, frame
.nwbutton
, frame
.button
);
811 XSetWindowBackground(display
, frame
.nwbutton
, frame
.button_pixel
);
813 XClearWindow(display
, frame
.nwbutton
);
816 int hh
= frame
.button_w
/ 2, hw
= frame
.button_w
/ 2;
819 pts
[0].x
= hw
- 2; pts
[0].y
= hh
- 2;
820 pts
[1].x
= 4; pts
[1].y
= 2;
821 pts
[2].x
= -4; pts
[2].y
= 2;
823 XFillPolygon(display
, frame
.nwbutton
, screen
.getToolbarStyle()->b_pic_gc
,
824 pts
, 3, Convex
, CoordModePrevious
);
828 void Toolbar::edit(void) {
833 if (XGetInputFocus(display
, &window
, &foo
) &&
834 window
== frame
.workspace_label
)
837 XSetInputFocus(display
, frame
.workspace_label
,
838 ((screen
.isSloppyFocus()) ? RevertToPointerRoot
:
841 XClearWindow(display
, frame
.workspace_label
);
843 openbox
.setNoFocus(True
);
844 if (openbox
.getFocusedWindow())
845 openbox
.getFocusedWindow()->setFocusFlag(False
);
847 XDrawRectangle(display
, frame
.workspace_label
,
848 screen
.getWindowStyle()->l_text_focus_gc
,
849 frame
.workspace_label_w
/ 2, 0, 1,
852 // change the background of the window to that of an active window label
853 Pixmap tmp
= frame
.wlabel
;
854 BTexture
*texture
= &(screen
.getWindowStyle()->l_focus
);
855 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
857 XSetWindowBackground(display
, frame
.workspace_label
,
858 texture
->getColor()->getPixel());
861 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
862 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
864 if (tmp
) image_ctrl
->removeImage(tmp
);
868 void Toolbar::buttonPressEvent(XButtonEvent
*be
) {
869 if (be
->button
== 1) {
870 if (be
->window
== frame
.psbutton
)
871 redrawPrevWorkspaceButton(True
, True
);
872 else if (be
->window
== frame
.nsbutton
)
873 redrawNextWorkspaceButton(True
, True
);
874 else if (be
->window
== frame
.pwbutton
)
875 redrawPrevWindowButton(True
, True
);
876 else if (be
->window
== frame
.nwbutton
)
877 redrawNextWindowButton(True
, True
);
878 #ifndef HAVE_STRFTIME
879 else if (be
->window
== frame
.clock
) {
880 XClearWindow(display
, frame
.clock
);
881 checkClock(True
, True
);
883 #endif // HAVE_STRFTIME
885 Window w
[1] = { frame
.window
};
886 screen
.raiseWindows(w
, 1);
888 } else if (be
->button
== 2 && (! on_top
)) {
889 XLowerWindow(display
, frame
.window
);
890 } else if (be
->button
== 3) {
891 if (! toolbarmenu
->isVisible()) {
894 x
= be
->x_root
- (toolbarmenu
->getWidth() / 2);
895 y
= be
->y_root
- (toolbarmenu
->getHeight() / 2);
899 else if (x
+ toolbarmenu
->getWidth() > screen
.size().w())
900 x
= screen
.size().w() - toolbarmenu
->getWidth();
904 else if (y
+ toolbarmenu
->getHeight() > screen
.size().h())
905 y
= screen
.size().h() - toolbarmenu
->getHeight();
907 toolbarmenu
->move(x
, y
);
916 void Toolbar::buttonReleaseEvent(XButtonEvent
*re
) {
917 if (re
->button
== 1) {
918 if (re
->window
== frame
.psbutton
) {
919 redrawPrevWorkspaceButton(False
, True
);
921 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
922 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
923 if (screen
.getCurrentWorkspace()->getWorkspaceID() > 0)
924 screen
.changeWorkspaceID(screen
.getCurrentWorkspace()->
925 getWorkspaceID() - 1);
927 screen
.changeWorkspaceID(screen
.getWorkspaceCount() - 1);
928 } else if (re
->window
== frame
.nsbutton
) {
929 redrawNextWorkspaceButton(False
, True
);
931 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
932 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
933 if (screen
.getCurrentWorkspace()->getWorkspaceID() <
934 screen
.getWorkspaceCount() - 1)
935 screen
.changeWorkspaceID(screen
.getCurrentWorkspace()->
936 getWorkspaceID() + 1);
938 screen
.changeWorkspaceID(0);
939 } else if (re
->window
== frame
.pwbutton
) {
940 redrawPrevWindowButton(False
, True
);
942 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
943 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
945 } else if (re
->window
== frame
.nwbutton
) {
946 redrawNextWindowButton(False
, True
);
948 if (re
->x
>= 0 && re
->x
< (signed) frame
.button_w
&&
949 re
->y
>= 0 && re
->y
< (signed) frame
.button_w
)
951 } else if (re
->window
== frame
.window_label
)
953 #ifndef HAVE_STRFTIME
954 else if (re
->window
== frame
.clock
) {
955 XClearWindow(display
, frame
.clock
);
958 #endif // HAVE_STRFTIME
963 void Toolbar::enterNotifyEvent(XCrossingEvent
*) {
968 if (! hide_timer
->isTiming()) hide_timer
->start();
970 if (hide_timer
->isTiming()) hide_timer
->stop();
974 void Toolbar::leaveNotifyEvent(XCrossingEvent
*) {
979 if (hide_timer
->isTiming()) hide_timer
->stop();
980 } else if (! toolbarmenu
->isVisible()) {
981 if (! hide_timer
->isTiming()) hide_timer
->start();
986 void Toolbar::exposeEvent(XExposeEvent
*ee
) {
987 if (ee
->window
== frame
.clock
) checkClock(True
);
988 else if (ee
->window
== frame
.workspace_label
&& (! editing
))
989 redrawWorkspaceLabel();
990 else if (ee
->window
== frame
.window_label
) redrawWindowLabel();
991 else if (ee
->window
== frame
.psbutton
) redrawPrevWorkspaceButton();
992 else if (ee
->window
== frame
.nsbutton
) redrawNextWorkspaceButton();
993 else if (ee
->window
== frame
.pwbutton
) redrawPrevWindowButton();
994 else if (ee
->window
== frame
.nwbutton
) redrawNextWindowButton();
998 void Toolbar::keyPressEvent(XKeyEvent
*ke
) {
999 if (ke
->window
== frame
.workspace_label
&& editing
) {
1002 if (! new_workspace_name
) {
1003 new_workspace_name
= new char[128];
1006 if (! new_workspace_name
) return;
1011 XLookupString(ke
, keychar
, 1, &ks
, 0);
1013 // either we are told to end with a return or we hit the end of the buffer
1014 if (ks
== XK_Return
|| new_name_pos
== 127) {
1015 *(new_workspace_name
+ new_name_pos
) = 0;
1019 openbox
.setNoFocus(False
);
1020 if (openbox
.getFocusedWindow()) {
1021 openbox
.getFocusedWindow()->setInputFocus();
1022 openbox
.getFocusedWindow()->setFocusFlag(True
);
1024 XSetInputFocus(display
, PointerRoot
, None
, CurrentTime
);
1026 // check to make sure that new_name[0] != 0... otherwise we have a null
1027 // workspace name which causes serious problems, especially for the
1028 // Openbox::LoadRC() method.
1029 if (*new_workspace_name
) {
1030 screen
.getCurrentWorkspace()->setName(new_workspace_name
);
1031 screen
.getCurrentWorkspace()->getMenu()->hide();
1032 screen
.getWorkspacemenu()->
1033 remove(screen
.getCurrentWorkspace()->getWorkspaceID() + 2);
1034 screen
.getWorkspacemenu()->
1035 insert(screen
.getCurrentWorkspace()->getName(),
1036 screen
.getCurrentWorkspace()->getMenu(),
1037 screen
.getCurrentWorkspace()->getWorkspaceID() + 2);
1038 screen
.getWorkspacemenu()->update();
1041 delete [] new_workspace_name
;
1042 new_workspace_name
= (char *) 0;
1045 // reset the background to that of the workspace label (its normal
1047 Pixmap tmp
= frame
.wlabel
;
1048 BTexture
*texture
= &(screen
.getToolbarStyle()->label
);
1049 if (texture
->getTexture() == (BImage_Flat
| BImage_Solid
)) {
1050 frame
.wlabel
= None
;
1051 XSetWindowBackground(display
, frame
.workspace_label
,
1052 texture
->getColor()->getPixel());
1055 image_ctrl
->renderImage(frame
.workspace_label_w
, frame
.label_h
, texture
);
1056 XSetWindowBackgroundPixmap(display
, frame
.workspace_label
, frame
.wlabel
);
1058 if (tmp
) image_ctrl
->removeImage(tmp
);
1061 } else if (! (ks
== XK_Shift_L
|| ks
== XK_Shift_R
||
1062 ks
== XK_Control_L
|| ks
== XK_Control_R
||
1063 ks
== XK_Caps_Lock
|| ks
== XK_Shift_Lock
||
1064 ks
== XK_Meta_L
|| ks
== XK_Meta_R
||
1065 ks
== XK_Alt_L
|| ks
== XK_Alt_R
||
1066 ks
== XK_Super_L
|| ks
== XK_Super_R
||
1067 ks
== XK_Hyper_L
|| ks
== XK_Hyper_R
)) {
1068 if (ks
== XK_BackSpace
) {
1069 if (new_name_pos
> 0) {
1071 *(new_workspace_name
+ new_name_pos
) = '\0';
1073 *new_workspace_name
= '\0';
1076 *(new_workspace_name
+ new_name_pos
) = *keychar
;
1078 *(new_workspace_name
+ new_name_pos
) = '\0';
1081 XClearWindow(display
, frame
.workspace_label
);
1082 int l
= strlen(new_workspace_name
), tw
, x
;
1084 if (i18n
->multibyte()) {
1085 XRectangle ink
, logical
;
1086 XmbTextExtents(screen
.getToolbarStyle()->fontset
,
1087 new_workspace_name
, l
, &ink
, &logical
);
1090 tw
= XTextWidth(screen
.getToolbarStyle()->font
,
1091 new_workspace_name
, l
);
1093 x
= (frame
.workspace_label_w
- tw
) / 2;
1095 if (x
< (signed) frame
.bevel_w
) x
= frame
.bevel_w
;
1097 WindowStyle
*style
= screen
.getWindowStyle();
1098 if (i18n
->multibyte())
1099 XmbDrawString(display
, frame
.workspace_label
, style
->fontset
,
1100 style
->l_text_focus_gc
, x
,
1101 (1 - style
->fontset_extents
->max_ink_extent
.y
),
1102 new_workspace_name
, l
);
1104 XDrawString(display
, frame
.workspace_label
, style
->l_text_focus_gc
, x
,
1105 (style
->font
->ascent
+ 1),
1106 new_workspace_name
, l
);
1108 XDrawRectangle(display
, frame
.workspace_label
,
1109 screen
.getWindowStyle()->l_text_focus_gc
, x
+ tw
, 0, 1,
1118 void Toolbar::timeout(void) {
1122 gettimeofday(&now
, 0);
1123 clock_timer
->setTimeout((60 - (now
.tv_sec
% 60)) * 1000);
1127 void Toolbar::HideHandler::timeout(void) {
1128 toolbar
->hidden
= ! toolbar
->hidden
;
1129 if (toolbar
->hidden
)
1130 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
1131 toolbar
->frame
.x_hidden
, toolbar
->frame
.y_hidden
);
1133 XMoveWindow(toolbar
->display
, toolbar
->frame
.window
,
1134 toolbar
->frame
.x
, toolbar
->frame
.y
);
1138 Toolbarmenu::Toolbarmenu(Toolbar
&tb
) : Basemenu(tb
.screen
), toolbar(tb
) {
1139 setLabel(i18n
->getMessage(ToolbarSet
, ToolbarToolbarTitle
, "Toolbar"));
1142 placementmenu
= new Placementmenu(*this);
1144 insert(i18n
->getMessage(CommonSet
, CommonPlacementTitle
, "Placement"),
1146 insert(i18n
->getMessage(CommonSet
, CommonAlwaysOnTop
, "Always on top"), 1);
1147 insert(i18n
->getMessage(CommonSet
, CommonAutoHide
, "Auto hide"), 2);
1148 insert(i18n
->getMessage(ToolbarSet
, ToolbarEditWkspcName
,
1149 "Edit current workspace name"), 3);
1153 if (toolbar
.isOnTop()) setItemSelected(1, True
);
1154 if (toolbar
.doAutoHide()) setItemSelected(2, True
);
1158 Toolbarmenu::~Toolbarmenu(void) {
1159 delete placementmenu
;
1163 void Toolbarmenu::itemSelected(int button
, int index
) {
1167 BasemenuItem
*item
= find(index
);
1170 switch (item
->function()) {
1171 case 1: { // always on top
1172 Bool change
= ((toolbar
.isOnTop()) ? False
: True
);
1173 toolbar
.on_top
= change
;
1174 setItemSelected(1, change
);
1176 if (toolbar
.isOnTop()) toolbar
.screen
.raiseWindows((Window
*) 0, 0);
1180 case 2: { // auto hide
1181 Bool change
= ((toolbar
.doAutoHide()) ? False
: True
);
1182 toolbar
.do_auto_hide
= change
;
1183 setItemSelected(2, change
);
1186 toolbar
.screen
.getSlit()->reposition();
1191 case 3: { // edit current workspace name
1201 void Toolbarmenu::internal_hide(void) {
1202 Basemenu::internal_hide();
1203 if (toolbar
.doAutoHide() && ! toolbar
.isEditing())
1204 toolbar
.hide_handler
.timeout();
1208 void Toolbarmenu::reconfigure(void) {
1209 placementmenu
->reconfigure();
1211 Basemenu::reconfigure();
1215 Toolbarmenu::Placementmenu::Placementmenu(Toolbarmenu
&tm
)
1216 : Basemenu(tm
.toolbar
.screen
), toolbarmenu(tm
) {
1217 setLabel(i18n
->getMessage(ToolbarSet
, ToolbarToolbarPlacement
,
1218 "Toolbar Placement"));
1220 setMinimumSublevels(3);
1222 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopLeft
,
1223 "Top Left"), Toolbar::TopLeft
);
1224 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomLeft
,
1225 "Bottom Left"), Toolbar::BottomLeft
);
1226 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopCenter
,
1227 "Top Center"), Toolbar::TopCenter
);
1228 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomCenter
,
1229 "Bottom Center"), Toolbar::BottomCenter
);
1230 insert(i18n
->getMessage(CommonSet
, CommonPlacementTopRight
,
1231 "Top Right"), Toolbar::TopRight
);
1232 insert(i18n
->getMessage(CommonSet
, CommonPlacementBottomRight
,
1233 "Bottom Right"), Toolbar::BottomRight
);
1238 void Toolbarmenu::Placementmenu::itemSelected(int button
, int index
) {
1242 BasemenuItem
*item
= find(index
);
1245 toolbarmenu
.toolbar
.screen
.saveToolbarPlacement(item
->function());
1247 toolbarmenu
.toolbar
.reconfigure();
1250 // reposition the slit as well to make sure it doesn't intersect the
1252 toolbarmenu
.toolbar
.screen
.getSlit()->reposition();