1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 // Basemenu.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
31 #endif // HAVE_STDIO_H
35 #endif // HAVE_STDLIB_H
39 #endif // HAVE_STRING_H
47 #include "blackbox.hh"
48 #include "Basemenu.hh"
55 static Basemenu
*shown
= (Basemenu
*) 0;
57 Basemenu::Basemenu(BScreen
*scrn
) {
59 blackbox
= screen
->getBlackbox();
60 image_ctrl
= screen
->getImageControl();
61 display
= blackbox
->getXDisplay();
62 parent
= (Basemenu
*) 0;
63 alignment
= AlignDontCare
;
89 menu
.sel_pixmap
= None
;
91 menu
.bevel_w
= screen
->getBevelWidth();
94 menu
.width
= menu
.title_h
= menu
.item_w
= menu
.frame_h
=
95 screen
->getMenuStyle()->t_fontset_extents
->max_ink_extent
.height
+
98 menu
.width
= menu
.title_h
= menu
.item_w
= menu
.frame_h
=
99 screen
->getMenuStyle()->t_font
->ascent
+
100 screen
->getMenuStyle()->t_font
->descent
+ (menu
.bevel_w
* 2);
106 MenuStyle
*style
= screen
->getMenuStyle();
107 if (i18n
.multibyte()) {
108 menu
.item_h
= style
->f_fontset_extents
->max_ink_extent
.height
+
111 menu
.item_h
= style
->f_font
->ascent
+ style
->f_font
->descent
+
115 menu
.height
= menu
.title_h
+ screen
->getBorderWidth() + menu
.frame_h
;
117 unsigned long attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
118 CWColormap
| CWOverrideRedirect
| CWEventMask
;
119 XSetWindowAttributes attrib
;
120 attrib
.background_pixmap
= None
;
121 attrib
.background_pixel
= attrib
.border_pixel
=
122 screen
->getBorderColor()->pixel();
123 attrib
.colormap
= screen
->getColormap();
124 attrib
.override_redirect
= True
;
125 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
126 ButtonMotionMask
| ExposureMask
;
129 XCreateWindow(display
, screen
->getRootWindow(),
130 menu
.x
, menu
.y
, menu
.width
, menu
.height
,
131 screen
->getBorderWidth(), screen
->getDepth(),
132 InputOutput
, screen
->getVisual(), attrib_mask
, &attrib
);
133 blackbox
->saveMenuSearch(menu
.window
, this);
135 attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
| CWEventMask
;
136 attrib
.background_pixel
= screen
->getBorderColor()->pixel();
137 attrib
.event_mask
|= EnterWindowMask
| LeaveWindowMask
;
140 XCreateWindow(display
, menu
.window
, 0, 0, menu
.width
, menu
.height
, 0,
141 screen
->getDepth(), InputOutput
, screen
->getVisual(),
142 attrib_mask
, &attrib
);
143 blackbox
->saveMenuSearch(menu
.title
, this);
145 attrib
.event_mask
|= PointerMotionMask
;
146 menu
.frame
= XCreateWindow(display
, menu
.window
, 0,
147 menu
.title_h
+ screen
->getBorderWidth(),
148 menu
.width
, menu
.frame_h
, 0,
149 screen
->getDepth(), InputOutput
,
150 screen
->getVisual(), attrib_mask
, &attrib
);
151 blackbox
->saveMenuSearch(menu
.frame
, this);
153 // even though this is the end of the constructor the menu is still not
154 // completely created. items must be inserted and it must be update()'d
157 Basemenu::~Basemenu(void) {
158 XUnmapWindow(display
, menu
.window
);
160 if (shown
&& shown
->getWindowID() == getWindowID())
161 shown
= (Basemenu
*) 0;
163 MenuItems::const_iterator it
= menuitems
.begin();
164 while (it
!= menuitems
.end()) {
165 BasemenuItem
*item
= *it
;
166 if ((! internal_menu
)) {
167 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
169 if (! tmp
->internal_menu
) {
172 tmp
->internal_hide();
179 std::for_each(menuitems
.begin(), menuitems
.end(), PointerAssassin());
181 if (menu
.title_pixmap
)
182 image_ctrl
->removeImage(menu
.title_pixmap
);
184 if (menu
.frame_pixmap
)
185 image_ctrl
->removeImage(menu
.frame_pixmap
);
187 if (menu
.hilite_pixmap
)
188 image_ctrl
->removeImage(menu
.hilite_pixmap
);
191 image_ctrl
->removeImage(menu
.sel_pixmap
);
193 blackbox
->removeMenuSearch(menu
.title
);
194 XDestroyWindow(display
, menu
.title
);
196 blackbox
->removeMenuSearch(menu
.frame
);
197 XDestroyWindow(display
, menu
.frame
);
199 blackbox
->removeMenuSearch(menu
.window
);
200 XDestroyWindow(display
, menu
.window
);
204 BasemenuItem::~BasemenuItem(void) {}
207 BasemenuItem
*Basemenu::find(int index
) {
208 if (index
< 0 || index
> static_cast<signed>(menuitems
.size()))
209 return (BasemenuItem
*) 0;
211 return *(menuitems
.begin() + index
);
215 int Basemenu::insert(BasemenuItem
*item
, int pos
) {
217 menuitems
.push_back(item
);
219 assert(pos
< static_cast<signed>(menuitems
.size()));
220 menuitems
.insert((menuitems
.begin() + pos
), item
);
222 return menuitems
.size();
226 int Basemenu::insert(const string
& label
, int function
,
227 const string
& exec
, int pos
) {
228 BasemenuItem
*item
= new BasemenuItem(label
, function
, exec
);
229 return insert(item
, pos
);
233 int Basemenu::insert(const string
& label
, Basemenu
*submenu
, int pos
) {
234 BasemenuItem
*item
= new BasemenuItem(label
, submenu
);
235 submenu
->parent
= this;
237 return insert(item
, pos
);
241 int Basemenu::remove(int index
) {
242 BasemenuItem
*item
= find(index
);
243 if (! item
) return -1;
245 if ((! internal_menu
)) {
246 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
248 if (! tmp
->internal_menu
) {
251 tmp
->internal_hide();
258 if (which_sub
== index
)
260 else if (which_sub
> index
)
263 menuitems
.erase(menuitems
.begin() + index
);
265 return menuitems
.size();
269 void Basemenu::update(void) {
270 MenuStyle
*style
= screen
->getMenuStyle();
271 if (i18n
.multibyte()) {
272 menu
.item_h
= style
->f_fontset_extents
->max_ink_extent
.height
+
274 menu
.title_h
= style
->t_fontset_extents
->max_ink_extent
.height
+
277 menu
.item_h
= style
->f_font
->ascent
+ style
->f_font
->descent
+
279 menu
.title_h
= style
->t_font
->ascent
+ style
->t_font
->descent
+
284 const char *s
= getLabel();
287 if (i18n
.multibyte()) {
288 XRectangle ink
, logical
;
289 XmbTextExtents(screen
->getMenuStyle()->t_fontset
, s
, l
, &ink
, &logical
);
290 menu
.item_w
= logical
.width
;
292 menu
.item_w
= XTextWidth(screen
->getMenuStyle()->t_font
, s
, l
);
295 menu
.item_w
+= (menu
.bevel_w
* 2);
301 MenuItems::iterator it
= menuitems
.begin(), end
= menuitems
.end();
302 for (; it
!= end
; ++it
) {
303 BasemenuItem
*tmp
= *it
;
304 const char *s
= tmp
->l
.c_str();
307 if (i18n
.multibyte()) {
308 XRectangle ink
, logical
;
309 XmbTextExtents(screen
->getMenuStyle()->f_fontset
, s
, l
, &ink
, &logical
);
312 ii
= XTextWidth(screen
->getMenuStyle()->f_font
, s
, l
);
314 ii
+= (menu
.bevel_w
* 2) + (menu
.item_h
* 2);
316 menu
.item_w
= ((menu
.item_w
< ii
) ? ii
: menu
.item_w
);
319 if (! menuitems
.empty()) {
322 unsigned int menu_size
= menuitems
.size();
323 while (((menu
.item_h
* (menu_size
+ 1) / menu
.sublevels
)
324 + menu
.title_h
+ screen
->getBorderWidth()) >
328 if (menu
.sublevels
< menu
.minsub
) menu
.sublevels
= menu
.minsub
;
330 menu
.persub
= menu_size
/ menu
.sublevels
;
331 if (menu_size
% menu
.sublevels
) menu
.persub
++;
337 menu
.width
= (menu
.sublevels
* (menu
.item_w
));
338 if (! menu
.width
) menu
.width
= menu
.item_w
;
340 menu
.frame_h
= (menu
.item_h
* menu
.persub
);
341 menu
.height
= ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) +
343 if (! menu
.frame_h
) menu
.frame_h
= 1;
344 if (menu
.height
< 1) menu
.height
= 1;
349 tmp
= menu
.title_pixmap
;
350 texture
= &(screen
->getMenuStyle()->title
);
351 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
352 menu
.title_pixmap
= None
;
353 XSetWindowBackground(display
, menu
.title
,
354 texture
->color().pixel());
357 image_ctrl
->renderImage(menu
.width
, menu
.title_h
, *texture
);
358 XSetWindowBackgroundPixmap(display
, menu
.title
, menu
.title_pixmap
);
360 if (tmp
) image_ctrl
->removeImage(tmp
);
361 XClearWindow(display
, menu
.title
);
364 tmp
= menu
.frame_pixmap
;
365 texture
= &(screen
->getMenuStyle()->frame
);
366 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
367 menu
.frame_pixmap
= None
;
368 XSetWindowBackground(display
, menu
.frame
,
369 texture
->color().pixel());
372 image_ctrl
->renderImage(menu
.width
, menu
.frame_h
, *texture
);
373 XSetWindowBackgroundPixmap(display
, menu
.frame
, menu
.frame_pixmap
);
375 if (tmp
) image_ctrl
->removeImage(tmp
);
377 tmp
= menu
.hilite_pixmap
;
378 texture
= &(screen
->getMenuStyle()->hilite
);
379 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
380 menu
.hilite_pixmap
= None
;
383 image_ctrl
->renderImage(menu
.item_w
, menu
.item_h
, *texture
);
385 if (tmp
) image_ctrl
->removeImage(tmp
);
387 tmp
= menu
.sel_pixmap
;
388 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
389 menu
.sel_pixmap
= None
;
391 int hw
= menu
.item_h
/ 2;
393 image_ctrl
->renderImage(hw
, hw
, *texture
);
395 if (tmp
) image_ctrl
->removeImage(tmp
);
397 XResizeWindow(display
, menu
.window
, menu
.width
, menu
.height
);
400 XResizeWindow(display
, menu
.title
, menu
.width
, menu
.title_h
);
402 XMoveResizeWindow(display
, menu
.frame
, 0,
403 ((title_vis
) ? menu
.title_h
+
404 screen
->getBorderWidth() : 0), menu
.width
,
407 XClearWindow(display
, menu
.window
);
408 XClearWindow(display
, menu
.title
);
409 XClearWindow(display
, menu
.frame
);
411 if (title_vis
&& visible
) redrawTitle();
413 const int menu_size
= menuitems
.size();
414 for (int i
= 0; visible
&& i
< menu_size
; i
++) {
415 if (i
== which_sub
) {
416 drawItem(i
, True
, 0);
419 drawItem(i
, False
, 0);
423 if (parent
&& visible
)
424 parent
->drawSubmenu(parent
->which_sub
);
426 XMapSubwindows(display
, menu
.window
);
430 void Basemenu::show(void) {
431 XMapSubwindows(display
, menu
.window
);
432 XMapWindow(display
, menu
.window
);
436 if (shown
&& (! shown
->torn
))
444 void Basemenu::hide(void) {
445 if ((! torn
) && hide_tree
&& parent
&& parent
->isVisible()) {
446 Basemenu
*p
= parent
;
448 while (p
->isVisible() && (! p
->torn
) && p
->parent
) p
= p
->parent
;
456 void Basemenu::internal_hide(void) {
457 BasemenuItem
*tmp
= find(which_sub
);
459 tmp
->submenu()->internal_hide();
461 if (parent
&& (! torn
)) {
462 parent
->drawItem(parent
->which_sub
, False
, True
);
464 parent
->which_sub
= -1;
465 } else if (shown
&& shown
->menu
.window
== menu
.window
) {
466 shown
= (Basemenu
*) 0;
469 torn
= visible
= False
;
470 which_sub
= which_press
= which_sub
= -1;
472 XUnmapWindow(display
, menu
.window
);
476 void Basemenu::move(int x
, int y
) {
479 XMoveWindow(display
, menu
.window
, x
, y
);
481 drawSubmenu(which_sub
);
485 void Basemenu::redrawTitle(void) {
486 const char *text
= (! menu
.label
.empty()) ? getLabel() :
487 i18n(BasemenuSet
, BasemenuBlackboxMenu
, "Blackbox Menu");
488 int dx
= menu
.bevel_w
, len
= strlen(text
);
491 if (i18n
.multibyte()) {
492 XRectangle ink
, logical
;
493 XmbTextExtents(screen
->getMenuStyle()->t_fontset
, text
, len
,
497 l
= XTextWidth(screen
->getMenuStyle()->t_font
, text
, len
);
500 l
+= (menu
.bevel_w
* 2);
502 switch (screen
->getMenuStyle()->t_justify
) {
504 dx
+= menu
.width
- l
;
508 dx
+= (menu
.width
- l
) / 2;
516 MenuStyle
*style
= screen
->getMenuStyle();
517 BPen
pen(style
->t_text
, style
->t_font
);
518 if (i18n
.multibyte())
519 XmbDrawString(display
, menu
.title
, style
->t_fontset
, pen
.gc(), dx
,
520 (menu
.bevel_w
- style
->t_fontset_extents
->max_ink_extent
.y
),
523 XDrawString(display
, menu
.title
, pen
.gc(), dx
,
524 (style
->t_font
->ascent
+ menu
.bevel_w
), text
, len
);
528 void Basemenu::drawSubmenu(int index
) {
529 BasemenuItem
*item
= find(which_sub
);
530 if (item
&& item
->submenu() && ! item
->submenu()->isTorn() &&
532 item
->submenu()->internal_hide();
537 Basemenu
*submenu
= item
->submenu();
539 if (submenu
&& visible
&& ! submenu
->isTorn() && item
->isEnabled()) {
540 if (submenu
->parent
!= this) submenu
->parent
= this;
541 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
),
542 x
= menu
.x
+ ((menu
.item_w
* (sbl
+ 1)) + screen
->getBorderWidth()), y
;
544 if (alignment
== AlignTop
) {
545 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
546 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
547 ((submenu
->title_vis
) ?
548 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
550 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
552 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
553 ((submenu
->title_vis
) ?
554 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
557 if (alignment
== AlignBottom
&&
558 (y
+ submenu
->menu
.height
) > ((shifted
) ? menu
.y_shift
:
559 menu
.y
) + menu
.height
)
560 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
561 menu
.height
- submenu
->menu
.height
);
563 if ((x
+ submenu
->getWidth()) > screen
->getWidth())
564 x
= ((shifted
) ? menu
.x_shift
: menu
.x
) -
565 submenu
->getWidth() - screen
->getBorderWidth();
569 if ((y
+ submenu
->getHeight()) > screen
->getHeight())
570 y
= screen
->getHeight() - submenu
->getHeight() -
571 (screen
->getBorderWidth() * 2);
575 if (! moving
) drawItem(index
, True
);
577 if (! submenu
->isVisible())
579 submenu
->moving
= moving
;
587 bool Basemenu::hasSubmenu(int index
) {
588 BasemenuItem
*item
= find(index
);
589 if (item
&& item
->submenu())
595 void Basemenu::drawItem(int index
, bool highlight
, bool clear
,
596 int x
, int y
, unsigned int w
, unsigned int h
)
598 BasemenuItem
*item
= find(index
);
601 bool dotext
= True
, dohilite
= True
, dosel
= True
;
602 const char *text
= item
->label();
603 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
);
604 int item_x
= (sbl
* menu
.item_w
), item_y
= (i
* menu
.item_h
);
605 int hilite_x
= item_x
, hilite_y
= item_y
, hoff_x
= 0, hoff_y
= 0;
606 int text_x
= 0, text_y
= 0, len
= strlen(text
), sel_x
= 0, sel_y
= 0;
607 unsigned int hilite_w
= menu
.item_w
, hilite_h
= menu
.item_h
, text_w
= 0,
609 unsigned int half_w
= menu
.item_h
/ 2, quarter_w
= menu
.item_h
/ 4;
612 if (i18n
.multibyte()) {
613 XRectangle ink
, logical
;
614 XmbTextExtents(screen
->getMenuStyle()->f_fontset
,
615 text
, len
, &ink
, &logical
);
616 text_w
= logical
.width
;
617 text_y
= item_y
+ (menu
.bevel_w
/ 2) -
618 screen
->getMenuStyle()->f_fontset_extents
->max_ink_extent
.y
;
620 text_w
= XTextWidth(screen
->getMenuStyle()->f_font
, text
, len
);
622 screen
->getMenuStyle()->f_font
->ascent
+
626 switch(screen
->getMenuStyle()->f_justify
) {
628 text_x
= item_x
+ menu
.bevel_w
+ menu
.item_h
+ 1;
632 text_x
= item_x
+ menu
.item_w
- (menu
.item_h
+ menu
.bevel_w
+ text_w
);
636 text_x
= item_x
+ ((menu
.item_w
+ 1 - text_w
) / 2);
640 text_h
= menu
.item_h
- menu
.bevel_w
;
643 MenuStyle
*style
= screen
->getMenuStyle();
644 BPen
pen((highlight
|| item
->isSelected()) ? style
->h_text
: style
->f_text
),
645 textpen((highlight
) ? style
->h_text
:
646 item
->isEnabled() ? style
->f_text
: style
->d_text
, style
->f_font
),
647 hipen(style
->hilite
.color());
651 if (screen
->getMenuStyle()->bullet_pos
== Right
)
652 sel_x
+= (menu
.item_w
- menu
.item_h
- menu
.bevel_w
);
654 sel_y
= item_y
+ quarter_w
;
657 XClearArea(display
, menu
.frame
, item_x
, item_y
, menu
.item_w
, menu
.item_h
,
659 } else if (! (x
== y
&& y
== -1 && w
== h
&& h
== 0)) {
660 // calculate the which part of the hilite to redraw
661 if (! (max(item_x
, x
) <= min
<signed>(item_x
+ menu
.item_w
, x
+ w
) &&
662 max(item_y
, y
) <= min
<signed>(item_y
+ menu
.item_h
, y
+ h
))) {
665 hilite_x
= max(item_x
, x
);
666 hilite_y
= max(item_y
, y
);
667 hilite_w
= min(item_x
+ menu
.item_w
, x
+ w
) - hilite_x
;
668 hilite_h
= min(item_y
+ menu
.item_h
, y
+ h
) - hilite_y
;
669 hoff_x
= hilite_x
% menu
.item_w
;
670 hoff_y
= hilite_y
% menu
.item_h
;
673 // check if we need to redraw the text
674 int text_ry
= item_y
+ (menu
.bevel_w
/ 2);
675 if (! (max(text_x
, x
) <= min
<signed>(text_x
+ text_w
, x
+ w
) &&
676 max(text_ry
, y
) <= min
<signed>(text_ry
+ text_h
, y
+ h
)))
679 // check if we need to redraw the select pixmap/menu bullet
680 if (! (max(sel_x
, x
) <= min
<signed>(sel_x
+ half_w
, x
+ w
) &&
681 max(sel_y
, y
) <= min
<signed>(sel_y
+ half_w
, y
+ h
)))
685 if (dohilite
&& highlight
&& (menu
.hilite_pixmap
!= ParentRelative
)) {
686 if (menu
.hilite_pixmap
)
687 XCopyArea(display
, menu
.hilite_pixmap
, menu
.frame
,
688 hipen
.gc(), hoff_x
, hoff_y
,
689 hilite_w
, hilite_h
, hilite_x
, hilite_y
);
691 XFillRectangle(display
, menu
.frame
, hipen
.gc(),
692 hilite_x
, hilite_y
, hilite_w
, hilite_h
);
693 } else if (dosel
&& item
->isSelected() &&
694 (menu
.sel_pixmap
!= ParentRelative
)) {
696 XCopyArea(display
, menu
.sel_pixmap
, menu
.frame
, hipen
.gc(), 0, 0,
697 half_w
, half_w
, sel_x
, sel_y
);
699 XFillRectangle(display
, menu
.frame
, hipen
.gc(), sel_x
, sel_y
, half_w
, half_w
);
702 if (dotext
&& text
) {
703 if (i18n
.multibyte())
704 XmbDrawString(display
, menu
.frame
, screen
->getMenuStyle()->f_fontset
,
705 textpen
.gc(), text_x
, text_y
, text
, len
);
707 XDrawString(display
, menu
.frame
, textpen
.gc(), text_x
, text_y
, text
, len
);
710 if (dosel
&& item
->submenu()) {
711 switch (screen
->getMenuStyle()->bullet
) {
713 XDrawRectangle(display
, menu
.frame
, pen
.gc(), sel_x
, sel_y
, half_w
, half_w
);
719 if (screen
->getMenuStyle()->bullet_pos
== Right
) {
720 tri
[0].x
= sel_x
+ quarter_w
- 2;
721 tri
[0].y
= sel_y
+ quarter_w
- 2;
727 tri
[0].x
= sel_x
+ quarter_w
- 2;
728 tri
[0].y
= item_y
+ half_w
;
735 XFillPolygon(display
, menu
.frame
, pen
.gc(), tri
, 3, Convex
,
742 dia
[0].x
= sel_x
+ quarter_w
- 3;
743 dia
[0].y
= item_y
+ half_w
;
751 XFillPolygon(display
, menu
.frame
, pen
.gc(), dia
, 4, Convex
,
759 void Basemenu::setLabel(const string
& label
) {
764 void Basemenu::setItemSelected(int index
, bool sel
) {
766 BasemenuItem
*item
= find(index
);
769 item
->setSelected(sel
);
770 if (visible
) drawItem(index
, (index
== which_sub
), True
);
774 bool Basemenu::isItemSelected(int index
) {
776 BasemenuItem
*item
= find(index
);
777 if (! item
) return False
;
779 return item
->isSelected();
783 void Basemenu::setItemEnabled(int index
, bool enable
) {
785 BasemenuItem
*item
= find(index
);
788 item
->setEnabled(enable
);
789 if (visible
) drawItem(index
, (index
== which_sub
), True
);
793 bool Basemenu::isItemEnabled(int index
) {
795 BasemenuItem
*item
= find(index
);
796 if (! item
) return False
;
798 return item
->isEnabled();
802 void Basemenu::buttonPressEvent(XButtonEvent
*be
) {
803 if (be
->window
== menu
.frame
) {
804 int sbl
= (be
->x
/ menu
.item_w
), i
= (be
->y
/ menu
.item_h
);
805 int w
= (sbl
* menu
.persub
) + i
;
807 BasemenuItem
*item
= find(w
);
816 drawItem(w
, (item
->isEnabled()), True
);
819 menu
.x_move
= be
->x_root
- menu
.x
;
820 menu
.y_move
= be
->y_root
- menu
.y
;
825 void Basemenu::buttonReleaseEvent(XButtonEvent
*re
) {
826 if (re
->window
== menu
.title
) {
831 drawSubmenu(which_sub
);
834 if (re
->x
>= 0 && re
->x
<= static_cast<signed>(menu
.width
) &&
835 re
->y
>= 0 && re
->y
<= static_cast<signed>(menu
.title_h
))
838 } else if (re
->window
== menu
.frame
&&
839 re
->x
>= 0 && re
->x
< static_cast<signed>(menu
.width
) &&
840 re
->y
>= 0 && re
->y
< static_cast<signed>(menu
.frame_h
)) {
841 if (re
->button
== 3) {
844 int sbl
= (re
->x
/ menu
.item_w
), i
= (re
->y
/ menu
.item_h
),
845 ix
= sbl
* menu
.item_w
, iy
= i
* menu
.item_h
,
846 w
= (sbl
* menu
.persub
) + i
,
847 p
= (which_sbl
* menu
.persub
) + which_press
;
849 if (w
>= 0 && w
< static_cast<signed>(menuitems
.size())) {
850 drawItem(p
, (p
== which_sub
), True
);
852 if (p
== w
&& isItemEnabled(w
)) {
853 if (re
->x
> ix
&& re
->x
< static_cast<signed>(ix
+ menu
.item_w
) &&
854 re
->y
> iy
&& re
->y
< static_cast<signed>(iy
+ menu
.item_h
)) {
855 itemSelected(re
->button
, w
);
859 drawItem(p
, False
, True
);
866 void Basemenu::motionNotifyEvent(XMotionEvent
*me
) {
867 if (me
->window
== menu
.title
&& (me
->state
& Button1Mask
)) {
870 if (parent
&& (! torn
)) {
871 parent
->drawItem(parent
->which_sub
, False
, True
);
872 parent
->which_sub
= -1;
875 moving
= torn
= True
;
878 drawSubmenu(which_sub
);
880 menu
.x
= me
->x_root
- menu
.x_move
,
881 menu
.y
= me
->y_root
- menu
.y_move
;
883 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
886 drawSubmenu(which_sub
);
889 } else if ((! (me
->state
& Button1Mask
)) && me
->window
== menu
.frame
&&
890 me
->x
>= 0 && me
->x
< static_cast<signed>(menu
.width
) &&
891 me
->y
>= 0 && me
->y
< static_cast<signed>(menu
.frame_h
)) {
892 int sbl
= (me
->x
/ menu
.item_w
), i
= (me
->y
/ menu
.item_h
),
893 w
= (sbl
* menu
.persub
) + i
;
895 if ((i
!= which_press
|| sbl
!= which_sbl
) &&
896 (w
>= 0 && w
< static_cast<signed>(menuitems
.size()))) {
897 if (which_press
!= -1 && which_sbl
!= -1) {
898 int p
= (which_sbl
* menu
.persub
) + which_press
;
899 BasemenuItem
*item
= find(p
);
901 drawItem(p
, False
, True
);
903 if (item
->submenu()->isVisible() &&
904 (! item
->submenu()->isTorn())) {
905 item
->submenu()->internal_hide();
913 BasemenuItem
*itmp
= find(w
);
918 drawItem(w
, (itmp
->isEnabled()), True
);
924 void Basemenu::exposeEvent(XExposeEvent
*ee
) {
925 if (ee
->window
== menu
.title
) {
927 } else if (ee
->window
== menu
.frame
) {
928 // this is a compilicated algorithm... lets do it step by step...
929 // first... we see in which sub level the expose starts... and how many
930 // items down in that sublevel
932 int sbl
= (ee
->x
/ menu
.item_w
), id
= (ee
->y
/ menu
.item_h
),
933 // next... figure out how many sublevels over the redraw spans
934 sbl_d
= ((ee
->x
+ ee
->width
) / menu
.item_w
),
935 // then we see how many items down to redraw
936 id_d
= ((ee
->y
+ ee
->height
) / menu
.item_h
);
938 if (id_d
> menu
.persub
) id_d
= menu
.persub
;
940 // draw the sublevels and the number of items the exposure spans
941 MenuItems::iterator it
,
942 end
= menuitems
.end();
944 for (i
= sbl
; i
<= sbl_d
; i
++) {
945 // set the iterator to the first item in the sublevel needing redrawing
946 it
= menuitems
.begin() + (id
+ (i
* menu
.persub
));
947 for (ii
= id
; ii
<= id_d
&& it
!= end
; ++it
, ii
++) {
948 int index
= ii
+ (i
* menu
.persub
);
950 drawItem(index
, (which_sub
== index
), False
,
951 ee
->x
, ee
->y
, ee
->width
, ee
->height
);
958 void Basemenu::enterNotifyEvent(XCrossingEvent
*ce
) {
959 if (ce
->window
== menu
.frame
) {
960 menu
.x_shift
= menu
.x
, menu
.y_shift
= menu
.y
;
961 if (menu
.x
+ menu
.width
> screen
->getWidth()) {
962 menu
.x_shift
= screen
->getWidth() - menu
.width
-
963 screen
->getBorderWidth();
965 } else if (menu
.x
< 0) {
966 menu
.x_shift
= -screen
->getBorderWidth();
970 if (menu
.y
+ menu
.height
> screen
->getHeight()) {
971 menu
.y_shift
= screen
->getHeight() - menu
.height
-
972 screen
->getBorderWidth();
974 } else if (menu
.y
+ static_cast<signed>(menu
.title_h
) < 0) {
975 menu
.y_shift
= -screen
->getBorderWidth();
980 XMoveWindow(display
, menu
.window
, menu
.x_shift
, menu
.y_shift
);
982 if (which_sub
!= -1) {
983 BasemenuItem
*tmp
= find(which_sub
);
984 if (tmp
->submenu()->isVisible()) {
985 int sbl
= (ce
->x
/ menu
.item_w
), i
= (ce
->y
/ menu
.item_h
),
986 w
= (sbl
* menu
.persub
) + i
;
988 if (w
!= which_sub
&& (! tmp
->submenu()->isTorn())) {
989 tmp
->submenu()->internal_hide();
991 drawItem(which_sub
, False
, True
);
1000 void Basemenu::leaveNotifyEvent(XCrossingEvent
*ce
) {
1001 if (ce
->window
== menu
.frame
) {
1002 if (which_press
!= -1 && which_sbl
!= -1 && menuitems
.size() > 0) {
1003 int p
= (which_sbl
* menu
.persub
) + which_press
;
1005 drawItem(p
, (p
== which_sub
), True
);
1007 which_sbl
= which_press
= -1;
1011 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
1014 if (which_sub
!= -1) drawSubmenu(which_sub
);
1020 void Basemenu::reconfigure(void) {
1021 XSetWindowBackground(display
, menu
.window
,
1022 screen
->getBorderColor()->pixel());
1023 XSetWindowBorder(display
, menu
.window
,
1024 screen
->getBorderColor()->pixel());
1025 XSetWindowBorderWidth(display
, menu
.window
, screen
->getBorderWidth());
1027 menu
.bevel_w
= screen
->getBevelWidth();
1032 void Basemenu::changeItemLabel(unsigned int index
, const string
& label
) {
1033 BasemenuItem
*item
= find(index
);
1035 item
->newLabel(label
);