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"
56 static Basemenu
*shown
= (Basemenu
*) 0;
58 Basemenu::Basemenu(BScreen
*scrn
) {
60 blackbox
= screen
->getBlackbox();
61 image_ctrl
= screen
->getImageControl();
62 display
= blackbox
->getXDisplay();
63 parent
= (Basemenu
*) 0;
64 alignment
= AlignDontCare
;
89 menu
.hilite_pixmap
= None
;
91 menu
.bevel_w
= screen
->getBevelWidth();
93 MenuStyle
*style
= screen
->getMenuStyle();
94 menu
.width
= menu
.title_h
= menu
.item_w
= menu
.frame_h
=
95 style
->t_font
->height() + (menu
.bevel_w
* 2);
101 menu
.item_h
= style
->f_font
->height() + menu
.bevel_w
;
103 menu
.height
= menu
.title_h
+ screen
->getBorderWidth() + menu
.frame_h
;
105 unsigned long attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
106 CWColormap
| CWOverrideRedirect
| CWEventMask
;
107 XSetWindowAttributes attrib
;
108 attrib
.background_pixmap
= None
;
109 attrib
.background_pixel
= attrib
.border_pixel
=
110 screen
->getBorderColor()->pixel();
111 attrib
.colormap
= screen
->getColormap();
112 attrib
.override_redirect
= True
;
113 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
114 ButtonMotionMask
| ExposureMask
;
117 XCreateWindow(display
, screen
->getRootWindow(),
118 menu
.x
, menu
.y
, menu
.width
, menu
.height
,
119 screen
->getBorderWidth(), screen
->getDepth(),
120 InputOutput
, screen
->getVisual(), attrib_mask
, &attrib
);
121 blackbox
->saveMenuSearch(menu
.window
, this);
123 attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
| CWEventMask
;
124 attrib
.background_pixel
= screen
->getBorderColor()->pixel();
125 attrib
.event_mask
|= EnterWindowMask
| LeaveWindowMask
;
128 XCreateWindow(display
, menu
.window
, 0, 0, menu
.width
, menu
.height
, 0,
129 screen
->getDepth(), InputOutput
, screen
->getVisual(),
130 attrib_mask
, &attrib
);
131 blackbox
->saveMenuSearch(menu
.title
, this);
133 attrib
.event_mask
|= PointerMotionMask
;
134 menu
.frame
= XCreateWindow(display
, menu
.window
, 0,
135 menu
.title_h
+ screen
->getBorderWidth(),
136 menu
.width
, menu
.frame_h
, 0,
137 screen
->getDepth(), InputOutput
,
138 screen
->getVisual(), attrib_mask
, &attrib
);
139 blackbox
->saveMenuSearch(menu
.frame
, this);
141 // even though this is the end of the constructor the menu is still not
142 // completely created. items must be inserted and it must be update()'d
145 Basemenu::~Basemenu(void) {
146 XUnmapWindow(display
, menu
.window
);
148 if (shown
&& shown
->getWindowID() == getWindowID())
149 shown
= (Basemenu
*) 0;
151 MenuItems::const_iterator it
= menuitems
.begin();
152 while (it
!= menuitems
.end()) {
153 BasemenuItem
*item
= *it
;
154 if (! internal_menu
) {
155 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
157 if (! tmp
->internal_menu
) {
160 tmp
->internal_hide();
167 std::for_each(menuitems
.begin(), menuitems
.end(), PointerAssassin());
169 if (menu
.title_pixmap
)
170 image_ctrl
->removeImage(menu
.title_pixmap
);
172 if (menu
.frame_pixmap
)
173 image_ctrl
->removeImage(menu
.frame_pixmap
);
175 if (menu
.hilite_pixmap
)
176 image_ctrl
->removeImage(menu
.hilite_pixmap
);
178 blackbox
->removeMenuSearch(menu
.title
);
179 XDestroyWindow(display
, menu
.title
);
181 blackbox
->removeMenuSearch(menu
.frame
);
182 XDestroyWindow(display
, menu
.frame
);
184 blackbox
->removeMenuSearch(menu
.window
);
185 XDestroyWindow(display
, menu
.window
);
189 BasemenuItem
*Basemenu::find(int index
) {
190 if (index
< 0 || index
>= static_cast<signed>(menuitems
.size()))
191 return (BasemenuItem
*) 0;
193 return menuitems
[index
];
197 int Basemenu::insert(BasemenuItem
*item
, int pos
) {
199 menuitems
.push_back(item
);
201 assert(pos
<= static_cast<signed>(menuitems
.size()));
202 menuitems
.insert((menuitems
.begin() + pos
), item
);
204 return menuitems
.size();
208 int Basemenu::insert(const string
& label
, int function
,
209 const string
& exec
, int pos
) {
210 BasemenuItem
*item
= new BasemenuItem(label
, function
, exec
);
211 return insert(item
, pos
);
215 int Basemenu::insert(const string
& label
, Basemenu
*submenu
, int pos
) {
216 BasemenuItem
*item
= new BasemenuItem(label
, submenu
);
217 submenu
->parent
= this;
219 return insert(item
, pos
);
223 int Basemenu::remove(int index
) {
224 BasemenuItem
*item
= find(index
);
225 if (! item
) return -1;
227 if (! internal_menu
) {
228 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
230 if (! tmp
->internal_menu
) {
233 tmp
->internal_hide();
240 if (which_sub
== index
)
242 else if (which_sub
> index
)
245 menuitems
.erase(menuitems
.begin() + index
);
247 return menuitems
.size();
251 void Basemenu::update(void) {
252 MenuStyle
*style
= screen
->getMenuStyle();
253 menu
.item_h
= (style
->f_font
->height() < 9 ? 9 : style
->f_font
->height()) +
254 menu
.bevel_w
; // 9 for the menu pixmaps (checkmarks)
255 menu
.title_h
= style
->t_font
->height() + menu
.bevel_w
* 2;
258 menu
.item_w
= screen
->getMenuStyle()->t_font
->measureString(menu
.label
) +
265 MenuItems::iterator it
= menuitems
.begin(), end
= menuitems
.end();
266 for (; it
!= end
; ++it
) {
267 ii
= screen
->getMenuStyle()->f_font
->measureString((*it
)->l
) +
268 (menu
.bevel_w
* 2) + (menu
.item_h
* 2);
270 menu
.item_w
= ((menu
.item_w
< ii
) ? ii
: menu
.item_w
);
273 if (! menuitems
.empty()) {
276 unsigned int menu_size
= menuitems
.size();
277 while (((menu
.item_h
* (menu_size
+ 1) / menu
.sublevels
)
278 + menu
.title_h
+ screen
->getBorderWidth()) >
282 if (menu
.sublevels
< menu
.minsub
) menu
.sublevels
= menu
.minsub
;
284 menu
.persub
= menu_size
/ menu
.sublevels
;
285 if (menu_size
% menu
.sublevels
) menu
.persub
++;
291 menu
.width
= (menu
.sublevels
* (menu
.item_w
));
292 if (! menu
.width
) menu
.width
= menu
.item_w
;
294 menu
.frame_h
= (menu
.item_h
* menu
.persub
);
295 menu
.height
= ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) +
297 if (! menu
.frame_h
) menu
.frame_h
= 1;
298 if (menu
.height
< 1) menu
.height
= 1;
303 tmp
= menu
.title_pixmap
;
304 texture
= &(screen
->getMenuStyle()->title
);
305 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
306 menu
.title_pixmap
= None
;
307 XSetWindowBackground(display
, menu
.title
,
308 texture
->color().pixel());
311 image_ctrl
->renderImage(menu
.width
, menu
.title_h
, *texture
);
312 XSetWindowBackgroundPixmap(display
, menu
.title
, menu
.title_pixmap
);
314 if (tmp
) image_ctrl
->removeImage(tmp
);
315 XClearWindow(display
, menu
.title
);
318 tmp
= menu
.frame_pixmap
;
319 texture
= &(screen
->getMenuStyle()->frame
);
320 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
321 menu
.frame_pixmap
= None
;
322 XSetWindowBackground(display
, menu
.frame
,
323 texture
->color().pixel());
326 image_ctrl
->renderImage(menu
.width
, menu
.frame_h
, *texture
);
327 XSetWindowBackgroundPixmap(display
, menu
.frame
, menu
.frame_pixmap
);
329 if (tmp
) image_ctrl
->removeImage(tmp
);
331 tmp
= menu
.hilite_pixmap
;
332 texture
= &(screen
->getMenuStyle()->hilite
);
333 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
334 menu
.hilite_pixmap
= None
;
337 image_ctrl
->renderImage(menu
.item_w
, menu
.item_h
, *texture
);
339 if (tmp
) image_ctrl
->removeImage(tmp
);
341 XResizeWindow(display
, menu
.window
, menu
.width
, menu
.height
);
344 XResizeWindow(display
, menu
.title
, menu
.width
, menu
.title_h
);
346 XMoveResizeWindow(display
, menu
.frame
, 0,
347 ((title_vis
) ? menu
.title_h
+
348 screen
->getBorderWidth() : 0), menu
.width
,
351 XClearWindow(display
, menu
.window
);
352 XClearWindow(display
, menu
.title
);
353 XClearWindow(display
, menu
.frame
);
355 if (title_vis
&& visible
) redrawTitle();
357 const int menu_size
= menuitems
.size();
358 for (int i
= 0; visible
&& i
< menu_size
; i
++) {
359 if (i
== which_sub
) {
360 drawItem(i
, True
, 0);
363 drawItem(i
, False
, 0);
367 if (parent
&& visible
)
368 parent
->drawSubmenu(parent
->which_sub
);
370 XMapSubwindows(display
, menu
.window
);
374 void Basemenu::show(void) {
375 XMapSubwindows(display
, menu
.window
);
376 XMapWindow(display
, menu
.window
);
380 if (shown
&& (! shown
->torn
))
388 void Basemenu::hide(void) {
389 if (! torn
&& hide_tree
&& parent
&& parent
->isVisible()) {
390 Basemenu
*p
= parent
;
392 while (p
->isVisible() && ! p
->torn
&& p
->parent
) p
= p
->parent
;
400 void Basemenu::internal_hide(void) {
401 BasemenuItem
*tmp
= find(which_sub
);
403 tmp
->submenu()->internal_hide();
405 if (parent
&& ! torn
) {
406 parent
->drawItem(parent
->which_sub
, False
, True
);
408 parent
->which_sub
= -1;
409 } else if (shown
&& shown
->menu
.window
== menu
.window
) {
410 shown
= (Basemenu
*) 0;
413 torn
= visible
= False
;
414 which_sub
= which_press
= which_sub
= -1;
416 XUnmapWindow(display
, menu
.window
);
420 void Basemenu::move(int x
, int y
) {
423 XMoveWindow(display
, menu
.window
, x
, y
);
425 drawSubmenu(which_sub
);
429 void Basemenu::redrawTitle(void) {
430 const char *text
= (! menu
.label
.empty()) ? getLabel() :
431 i18n(BasemenuSet
, BasemenuBlackboxMenu
, "Blackbox Menu");
432 int dx
= menu
.bevel_w
;
434 MenuStyle
*style
= screen
->getMenuStyle();
436 l
= style
->t_font
->measureString(text
) + menu
.bevel_w
* 2;
438 switch (screen
->getMenuStyle()->t_justify
) {
440 dx
+= menu
.width
- l
;
444 dx
+= (menu
.width
- l
) / 2;
452 style
->t_font
->drawString(menu
.title
, dx
, menu
.bevel_w
,
453 style
->t_text
, text
);
457 void Basemenu::drawSubmenu(int index
) {
458 BasemenuItem
*item
= find(which_sub
);
459 if (item
&& item
->submenu() && ! item
->submenu()->isTorn() &&
461 item
->submenu()->internal_hide();
466 Basemenu
*submenu
= item
->submenu();
468 if (submenu
&& visible
&& ! submenu
->isTorn() && item
->isEnabled()) {
469 if (submenu
->parent
!= this) submenu
->parent
= this;
470 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
),
471 x
= menu
.x
+ ((menu
.item_w
* (sbl
+ 1)) + screen
->getBorderWidth()), y
;
473 if (alignment
== AlignTop
) {
474 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
475 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
476 ((submenu
->title_vis
) ?
477 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
479 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
481 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
482 ((submenu
->title_vis
) ?
483 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
486 if (alignment
== AlignBottom
&&
487 (y
+ submenu
->menu
.height
) > ((shifted
) ? menu
.y_shift
:
488 menu
.y
) + menu
.height
)
489 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
490 menu
.height
- submenu
->menu
.height
);
492 if ((x
+ submenu
->getWidth()) > screen
->getWidth())
493 x
= ((shifted
) ? menu
.x_shift
: menu
.x
) -
494 submenu
->getWidth() - screen
->getBorderWidth();
498 if ((y
+ submenu
->getHeight()) > screen
->getHeight())
499 y
= screen
->getHeight() - submenu
->getHeight() -
500 (screen
->getBorderWidth() * 2);
504 if (! moving
) drawItem(index
, True
);
506 if (! submenu
->isVisible())
508 submenu
->moving
= moving
;
516 bool Basemenu::hasSubmenu(int index
) {
517 BasemenuItem
*item
= find(index
);
518 if (item
&& item
->submenu())
524 void Basemenu::drawItem(int index
, bool highlight
, bool clear
,
525 int x
, int y
, unsigned int w
, unsigned int h
) {
526 BasemenuItem
*item
= find(index
);
529 bool dotext
= True
, dohilite
= True
, dosel
= True
, dooppsel
= True
;
530 const char *text
= item
->label();
531 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
);
532 int item_x
= (sbl
* menu
.item_w
), item_y
= (i
* menu
.item_h
);
533 int hilite_x
= item_x
, hilite_y
= item_y
, hoff_x
= 0, hoff_y
= 0;
534 int text_x
= 0, text_y
= 0, sel_x
= 0, oppsel_x
= 0, sel_y
= 0;
535 unsigned int hilite_w
= menu
.item_w
, hilite_h
= menu
.item_h
, text_w
= 0,
537 unsigned int half_w
= menu
.item_h
/ 2, quarter_w
= menu
.item_h
/ 4;
540 text_w
= screen
->getMenuStyle()->f_font
->measureString(text
);
541 text_y
= item_y
+ menu
.bevel_w
/ 2;
543 switch(screen
->getMenuStyle()->f_justify
) {
545 text_x
= item_x
+ menu
.bevel_w
+ menu
.item_h
+ 1;
549 text_x
= item_x
+ menu
.item_w
- (menu
.item_h
+ menu
.bevel_w
+ text_w
);
553 text_x
= item_x
+ ((menu
.item_w
+ 1 - text_w
) / 2);
557 text_h
= menu
.item_h
- menu
.bevel_w
;
560 MenuStyle
*style
= screen
->getMenuStyle();
561 BPen
hipen(style
->hilite
.color());
562 // match the text color
563 BPen
pen((highlight
? style
->h_text
:
564 (item
->isEnabled() ? style
->f_text
:
569 if (screen
->getMenuStyle()->bullet_pos
== Right
)
570 sel_x
+= (menu
.item_w
- menu
.item_h
- menu
.bevel_w
);
573 if (screen
->getMenuStyle()->bullet_pos
== Right
)
574 oppsel_x
-= (menu
.item_w
- menu
.item_h
- menu
.bevel_w
);
576 oppsel_x
+= (menu
.item_w
- menu
.item_h
- menu
.bevel_w
);
577 sel_y
= item_y
+ quarter_w
;
580 XClearArea(display
, menu
.frame
, item_x
, item_y
, menu
.item_w
, menu
.item_h
,
582 } else if (! (x
== y
&& y
== -1 && w
== h
&& h
== 0)) {
583 // calculate the which part of the hilite to redraw
584 if (! (max(item_x
, x
) <= min
<signed>(item_x
+ menu
.item_w
, x
+ w
) &&
585 max(item_y
, y
) <= min
<signed>(item_y
+ menu
.item_h
, y
+ h
))) {
588 hilite_x
= max(item_x
, x
);
589 hilite_y
= max(item_y
, y
);
590 hilite_w
= min(item_x
+ menu
.item_w
, x
+ w
) - hilite_x
;
591 hilite_h
= min(item_y
+ menu
.item_h
, y
+ h
) - hilite_y
;
592 hoff_x
= hilite_x
% menu
.item_w
;
593 hoff_y
= hilite_y
% menu
.item_h
;
596 // check if we need to redraw the text
597 int text_ry
= item_y
+ (menu
.bevel_w
/ 2);
598 if (! (max(text_x
, x
) <= min
<signed>(text_x
+ text_w
, x
+ w
) &&
599 max(text_ry
, y
) <= min
<signed>(text_ry
+ text_h
, y
+ h
)))
602 // check if we need to redraw the select pixmap/menu bullet
603 if (! (max(sel_x
, x
) <= min
<signed>(sel_x
+ half_w
, x
+ w
) &&
604 max(sel_y
, y
) <= min
<signed>(sel_y
+ half_w
, y
+ h
)))
607 // check if we need to redraw the select pixmap/menu bullet
608 // on the opposite side of the menu
609 if (! (max(oppsel_x
, x
) <= min
<signed>(oppsel_x
+ half_w
, x
+ w
) &&
610 max(sel_y
, y
) <= min
<signed>(sel_y
+ half_w
, y
+ h
)))
614 if (dohilite
&& highlight
&& (menu
.hilite_pixmap
!= ParentRelative
)) {
615 if (menu
.hilite_pixmap
)
616 XCopyArea(display
, menu
.hilite_pixmap
, menu
.frame
,
617 hipen
.gc(), hoff_x
, hoff_y
,
618 hilite_w
, hilite_h
, hilite_x
, hilite_y
);
620 XFillRectangle(display
, menu
.frame
, hipen
.gc(),
621 hilite_x
, hilite_y
, hilite_w
, hilite_h
);
624 if (dooppsel
&& item
->isSelected()) {
627 pts
[0].x
= oppsel_x
+ 0;
628 pts
[0].y
= sel_y
+ 2;
646 XFillPolygon(display
, menu
.frame
, pen
.gc(), pts
, 6, Nonconvex
,
650 if (dotext
&& text
) {
651 style
->f_font
->drawString(menu
.frame
, text_x
, text_y
,
652 (highlight
? style
->h_text
:
653 (item
->isEnabled() ? style
->f_text
:
658 if (dosel
&& item
->submenu()) {
659 const int bullet_size
= 3;
661 switch (screen
->getMenuStyle()->bullet
) {
663 XDrawRectangle(display
, menu
.frame
, pen
.gc(), sel_x
, sel_y
,
664 bullet_size
* 2, bullet_size
* 2);
670 if (screen
->getMenuStyle()->bullet_pos
== Right
) {
671 tri
[0].x
= sel_x
+ quarter_w
- bullet_size
;
672 tri
[0].y
= sel_y
+ quarter_w
- bullet_size
;
673 tri
[1].x
= 2 * bullet_size
;
674 tri
[1].y
= bullet_size
;
675 tri
[2].x
= -(2 * bullet_size
);
676 tri
[2].y
= bullet_size
;
678 tri
[0].x
= sel_x
+ quarter_w
- bullet_size
;
679 tri
[0].y
= item_y
+ half_w
;
680 tri
[1].x
= 2 * bullet_size
;
681 tri
[1].y
= bullet_size
;
683 tri
[2].y
= -(2 * bullet_size
);
686 XFillPolygon(display
, menu
.frame
, pen
.gc(), tri
, 3, Convex
,
693 dia
[0].x
= sel_x
+ quarter_w
- bullet_size
;
694 dia
[0].y
= item_y
+ half_w
;
695 dia
[1].x
= bullet_size
;
696 dia
[1].y
= -bullet_size
;
697 dia
[2].x
= bullet_size
;
698 dia
[2].y
= bullet_size
;
699 dia
[3].x
= -bullet_size
;
700 dia
[3].y
= bullet_size
;
702 XFillPolygon(display
, menu
.frame
, pen
.gc(), dia
, 4, Convex
,
710 void Basemenu::setLabel(const string
& label
) {
715 void Basemenu::setItemSelected(int index
, bool sel
) {
717 BasemenuItem
*item
= find(index
);
720 item
->setSelected(sel
);
721 if (visible
) drawItem(index
, (index
== which_sub
), True
);
725 bool Basemenu::isItemSelected(int index
) {
727 BasemenuItem
*item
= find(index
);
728 if (! item
) return False
;
730 return item
->isSelected();
734 void Basemenu::setItemEnabled(int index
, bool enable
) {
736 BasemenuItem
*item
= find(index
);
739 item
->setEnabled(enable
);
740 if (visible
) drawItem(index
, (index
== which_sub
), True
);
744 bool Basemenu::isItemEnabled(int index
) {
746 BasemenuItem
*item
= find(index
);
747 if (! item
) return False
;
749 return item
->isEnabled();
753 void Basemenu::buttonPressEvent(XButtonEvent
*be
) {
754 if (be
->window
== menu
.frame
) {
755 int sbl
= (be
->x
/ menu
.item_w
), i
= (be
->y
/ menu
.item_h
);
756 int w
= (sbl
* menu
.persub
) + i
;
758 BasemenuItem
*item
= find(w
);
767 drawItem(w
, (item
->isEnabled()), True
);
770 menu
.x_move
= be
->x_root
- menu
.x
;
771 menu
.y_move
= be
->y_root
- menu
.y
;
776 void Basemenu::buttonReleaseEvent(XButtonEvent
*re
) {
777 if (re
->window
== menu
.title
) {
782 drawSubmenu(which_sub
);
785 if (re
->x
>= 0 && re
->x
<= static_cast<signed>(menu
.width
) &&
786 re
->y
>= 0 && re
->y
<= static_cast<signed>(menu
.title_h
))
789 } else if (re
->window
== menu
.frame
&&
790 re
->x
>= 0 && re
->x
< static_cast<signed>(menu
.width
) &&
791 re
->y
>= 0 && re
->y
< static_cast<signed>(menu
.frame_h
)) {
792 if (re
->button
== 3) {
795 int sbl
= (re
->x
/ menu
.item_w
), i
= (re
->y
/ menu
.item_h
),
796 ix
= sbl
* menu
.item_w
, iy
= i
* menu
.item_h
,
797 w
= (sbl
* menu
.persub
) + i
,
798 p
= (which_sbl
* menu
.persub
) + which_press
;
800 if (w
>= 0 && w
< static_cast<signed>(menuitems
.size())) {
801 drawItem(p
, (p
== which_sub
), True
);
803 if (p
== w
&& isItemEnabled(w
)) {
804 if (re
->x
> ix
&& re
->x
< static_cast<signed>(ix
+ menu
.item_w
) &&
805 re
->y
> iy
&& re
->y
< static_cast<signed>(iy
+ menu
.item_h
)) {
806 itemSelected(re
->button
, w
);
810 drawItem(p
, False
, True
);
817 void Basemenu::motionNotifyEvent(XMotionEvent
*me
) {
818 if (me
->window
== menu
.title
&& (me
->state
& Button1Mask
)) {
821 if (parent
&& ! torn
) {
822 parent
->drawItem(parent
->which_sub
, False
, True
);
823 parent
->which_sub
= -1;
826 moving
= torn
= True
;
829 drawSubmenu(which_sub
);
831 menu
.x
= me
->x_root
- menu
.x_move
,
832 menu
.y
= me
->y_root
- menu
.y_move
;
834 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
837 drawSubmenu(which_sub
);
840 } else if (! (me
->state
& Button1Mask
) && me
->window
== menu
.frame
&&
841 me
->x
>= 0 && me
->x
< static_cast<signed>(menu
.width
) &&
842 me
->y
>= 0 && me
->y
< static_cast<signed>(menu
.frame_h
)) {
843 int sbl
= (me
->x
/ menu
.item_w
), i
= (me
->y
/ menu
.item_h
),
844 w
= (sbl
* menu
.persub
) + i
;
846 if ((i
!= which_press
|| sbl
!= which_sbl
) &&
847 (w
>= 0 && w
< static_cast<signed>(menuitems
.size()))) {
848 if (which_press
!= -1 && which_sbl
!= -1) {
849 int p
= (which_sbl
* menu
.persub
) + which_press
;
850 BasemenuItem
*item
= find(p
);
852 drawItem(p
, False
, True
);
854 if (item
->submenu()->isVisible() &&
855 ! item
->submenu()->isTorn()) {
856 item
->submenu()->internal_hide();
864 BasemenuItem
*itmp
= find(w
);
869 drawItem(w
, (itmp
->isEnabled()), True
);
875 void Basemenu::exposeEvent(XExposeEvent
*ee
) {
876 if (ee
->window
== menu
.title
) {
878 } else if (ee
->window
== menu
.frame
) {
879 // this is a compilicated algorithm... lets do it step by step...
880 // first... we see in which sub level the expose starts... and how many
881 // items down in that sublevel
883 int sbl
= (ee
->x
/ menu
.item_w
), id
= (ee
->y
/ menu
.item_h
),
884 // next... figure out how many sublevels over the redraw spans
885 sbl_d
= ((ee
->x
+ ee
->width
) / menu
.item_w
),
886 // then we see how many items down to redraw
887 id_d
= ((ee
->y
+ ee
->height
) / menu
.item_h
);
889 if (id_d
> menu
.persub
) id_d
= menu
.persub
;
891 // draw the sublevels and the number of items the exposure spans
892 MenuItems::iterator it
,
893 end
= menuitems
.end();
895 for (i
= sbl
; i
<= sbl_d
; i
++) {
896 // set the iterator to the first item in the sublevel needing redrawing
897 it
= menuitems
.begin() + (id
+ (i
* menu
.persub
));
898 for (ii
= id
; ii
<= id_d
&& it
!= end
; ++it
, ii
++) {
899 int index
= ii
+ (i
* menu
.persub
);
901 drawItem(index
, (which_sub
== index
), False
,
902 ee
->x
, ee
->y
, ee
->width
, ee
->height
);
909 void Basemenu::enterNotifyEvent(XCrossingEvent
*ce
) {
910 if (ce
->window
== menu
.frame
) {
911 menu
.x_shift
= menu
.x
, menu
.y_shift
= menu
.y
;
912 if (menu
.x
+ menu
.width
> screen
->getWidth()) {
913 menu
.x_shift
= screen
->getWidth() - menu
.width
-
914 screen
->getBorderWidth();
916 } else if (menu
.x
< 0) {
917 menu
.x_shift
= -screen
->getBorderWidth();
921 if (menu
.y
+ menu
.height
> screen
->getHeight()) {
922 menu
.y_shift
= screen
->getHeight() - menu
.height
-
923 screen
->getBorderWidth();
925 } else if (menu
.y
+ static_cast<signed>(menu
.title_h
) < 0) {
926 menu
.y_shift
= -screen
->getBorderWidth();
931 XMoveWindow(display
, menu
.window
, menu
.x_shift
, menu
.y_shift
);
933 if (which_sub
!= -1) {
934 BasemenuItem
*tmp
= find(which_sub
);
935 if (tmp
->submenu()->isVisible()) {
936 int sbl
= (ce
->x
/ menu
.item_w
), i
= (ce
->y
/ menu
.item_h
),
937 w
= (sbl
* menu
.persub
) + i
;
939 if (w
!= which_sub
&& ! tmp
->submenu()->isTorn()) {
940 tmp
->submenu()->internal_hide();
942 drawItem(which_sub
, False
, True
);
951 void Basemenu::leaveNotifyEvent(XCrossingEvent
*ce
) {
952 if (ce
->window
== menu
.frame
) {
953 if (which_press
!= -1 && which_sbl
!= -1 && menuitems
.size() > 0) {
954 int p
= (which_sbl
* menu
.persub
) + which_press
;
956 drawItem(p
, (p
== which_sub
), True
);
958 which_sbl
= which_press
= -1;
962 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
965 if (which_sub
!= -1) drawSubmenu(which_sub
);
971 void Basemenu::reconfigure(void) {
972 XSetWindowBackground(display
, menu
.window
,
973 screen
->getBorderColor()->pixel());
974 XSetWindowBorder(display
, menu
.window
,
975 screen
->getBorderColor()->pixel());
976 XSetWindowBorderWidth(display
, menu
.window
, screen
->getBorderWidth());
978 menu
.bevel_w
= screen
->getBevelWidth();
983 void Basemenu::changeItemLabel(unsigned int index
, const string
& label
) {
984 BasemenuItem
*item
= find(index
);
986 item
->newLabel(label
);