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
;
90 menu
.sel_pixmap
= None
;
92 menu
.bevel_w
= screen
->getBevelWidth();
94 MenuStyle
*style
= screen
->getMenuStyle();
95 menu
.width
= menu
.title_h
= menu
.item_w
= menu
.frame_h
=
96 style
->t_font
->height() + (menu
.bevel_w
* 2);
102 menu
.item_h
= style
->f_font
->height() + menu
.bevel_w
;
104 menu
.height
= menu
.title_h
+ screen
->getBorderWidth() + menu
.frame_h
;
106 unsigned long attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
|
107 CWColormap
| CWOverrideRedirect
| CWEventMask
;
108 XSetWindowAttributes attrib
;
109 attrib
.background_pixmap
= None
;
110 attrib
.background_pixel
= attrib
.border_pixel
=
111 screen
->getBorderColor()->pixel();
112 attrib
.colormap
= screen
->getColormap();
113 attrib
.override_redirect
= True
;
114 attrib
.event_mask
= ButtonPressMask
| ButtonReleaseMask
|
115 ButtonMotionMask
| ExposureMask
;
118 XCreateWindow(display
, screen
->getRootWindow(),
119 menu
.x
, menu
.y
, menu
.width
, menu
.height
,
120 screen
->getBorderWidth(), screen
->getDepth(),
121 InputOutput
, screen
->getVisual(), attrib_mask
, &attrib
);
122 blackbox
->saveMenuSearch(menu
.window
, this);
124 attrib_mask
= CWBackPixmap
| CWBackPixel
| CWBorderPixel
| CWEventMask
;
125 attrib
.background_pixel
= screen
->getBorderColor()->pixel();
126 attrib
.event_mask
|= EnterWindowMask
| LeaveWindowMask
;
129 XCreateWindow(display
, menu
.window
, 0, 0, menu
.width
, menu
.height
, 0,
130 screen
->getDepth(), InputOutput
, screen
->getVisual(),
131 attrib_mask
, &attrib
);
132 blackbox
->saveMenuSearch(menu
.title
, this);
134 attrib
.event_mask
|= PointerMotionMask
;
135 menu
.frame
= XCreateWindow(display
, menu
.window
, 0,
136 menu
.title_h
+ screen
->getBorderWidth(),
137 menu
.width
, menu
.frame_h
, 0,
138 screen
->getDepth(), InputOutput
,
139 screen
->getVisual(), attrib_mask
, &attrib
);
140 blackbox
->saveMenuSearch(menu
.frame
, this);
142 // even though this is the end of the constructor the menu is still not
143 // completely created. items must be inserted and it must be update()'d
146 Basemenu::~Basemenu(void) {
147 XUnmapWindow(display
, menu
.window
);
149 if (shown
&& shown
->getWindowID() == getWindowID())
150 shown
= (Basemenu
*) 0;
152 MenuItems::const_iterator it
= menuitems
.begin();
153 while (it
!= menuitems
.end()) {
154 BasemenuItem
*item
= *it
;
155 if ((! internal_menu
)) {
156 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
158 if (! tmp
->internal_menu
) {
161 tmp
->internal_hide();
168 std::for_each(menuitems
.begin(), menuitems
.end(), PointerAssassin());
170 if (menu
.title_pixmap
)
171 image_ctrl
->removeImage(menu
.title_pixmap
);
173 if (menu
.frame_pixmap
)
174 image_ctrl
->removeImage(menu
.frame_pixmap
);
176 if (menu
.hilite_pixmap
)
177 image_ctrl
->removeImage(menu
.hilite_pixmap
);
180 image_ctrl
->removeImage(menu
.sel_pixmap
);
182 blackbox
->removeMenuSearch(menu
.title
);
183 XDestroyWindow(display
, menu
.title
);
185 blackbox
->removeMenuSearch(menu
.frame
);
186 XDestroyWindow(display
, menu
.frame
);
188 blackbox
->removeMenuSearch(menu
.window
);
189 XDestroyWindow(display
, menu
.window
);
193 BasemenuItem
*Basemenu::find(int index
) {
194 if (index
< 0 || index
>= static_cast<signed>(menuitems
.size()))
195 return (BasemenuItem
*) 0;
197 return menuitems
[index
];
201 int Basemenu::insert(BasemenuItem
*item
, int pos
) {
203 menuitems
.push_back(item
);
205 assert(pos
< static_cast<signed>(menuitems
.size()));
206 menuitems
.insert((menuitems
.begin() + pos
), item
);
208 return menuitems
.size();
212 int Basemenu::insert(const string
& label
, int function
,
213 const string
& exec
, int pos
) {
214 BasemenuItem
*item
= new BasemenuItem(label
, function
, exec
);
215 return insert(item
, pos
);
219 int Basemenu::insert(const string
& label
, Basemenu
*submenu
, int pos
) {
220 BasemenuItem
*item
= new BasemenuItem(label
, submenu
);
221 submenu
->parent
= this;
223 return insert(item
, pos
);
227 int Basemenu::remove(int index
) {
228 BasemenuItem
*item
= find(index
);
229 if (! item
) return -1;
231 if ((! internal_menu
)) {
232 Basemenu
*tmp
= (Basemenu
*) item
->submenu();
234 if (! tmp
->internal_menu
) {
237 tmp
->internal_hide();
244 if (which_sub
== index
)
246 else if (which_sub
> index
)
249 menuitems
.erase(menuitems
.begin() + index
);
251 return menuitems
.size();
255 void Basemenu::update(void) {
256 MenuStyle
*style
= screen
->getMenuStyle();
257 menu
.item_h
= style
->f_font
->height() + menu
.bevel_w
;
258 menu
.title_h
= style
->t_font
->height() + menu
.bevel_w
* 2;
261 menu
.item_w
= screen
->getMenuStyle()->t_font
->measureString(menu
.label
) +
268 MenuItems::iterator it
= menuitems
.begin(), end
= menuitems
.end();
269 for (; it
!= end
; ++it
) {
270 ii
= screen
->getMenuStyle()->f_font
->measureString((*it
)->l
) +
271 (menu
.bevel_w
* 2) + (menu
.item_h
* 2);
273 menu
.item_w
= ((menu
.item_w
< ii
) ? ii
: menu
.item_w
);
276 if (! menuitems
.empty()) {
279 unsigned int menu_size
= menuitems
.size();
280 while (((menu
.item_h
* (menu_size
+ 1) / menu
.sublevels
)
281 + menu
.title_h
+ screen
->getBorderWidth()) >
285 if (menu
.sublevels
< menu
.minsub
) menu
.sublevels
= menu
.minsub
;
287 menu
.persub
= menu_size
/ menu
.sublevels
;
288 if (menu_size
% menu
.sublevels
) menu
.persub
++;
294 menu
.width
= (menu
.sublevels
* (menu
.item_w
));
295 if (! menu
.width
) menu
.width
= menu
.item_w
;
297 menu
.frame_h
= (menu
.item_h
* menu
.persub
);
298 menu
.height
= ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) +
300 if (! menu
.frame_h
) menu
.frame_h
= 1;
301 if (menu
.height
< 1) menu
.height
= 1;
306 tmp
= menu
.title_pixmap
;
307 texture
= &(screen
->getMenuStyle()->title
);
308 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
309 menu
.title_pixmap
= None
;
310 XSetWindowBackground(display
, menu
.title
,
311 texture
->color().pixel());
314 image_ctrl
->renderImage(menu
.width
, menu
.title_h
, *texture
);
315 XSetWindowBackgroundPixmap(display
, menu
.title
, menu
.title_pixmap
);
317 if (tmp
) image_ctrl
->removeImage(tmp
);
318 XClearWindow(display
, menu
.title
);
321 tmp
= menu
.frame_pixmap
;
322 texture
= &(screen
->getMenuStyle()->frame
);
323 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
324 menu
.frame_pixmap
= None
;
325 XSetWindowBackground(display
, menu
.frame
,
326 texture
->color().pixel());
329 image_ctrl
->renderImage(menu
.width
, menu
.frame_h
, *texture
);
330 XSetWindowBackgroundPixmap(display
, menu
.frame
, menu
.frame_pixmap
);
332 if (tmp
) image_ctrl
->removeImage(tmp
);
334 tmp
= menu
.hilite_pixmap
;
335 texture
= &(screen
->getMenuStyle()->hilite
);
336 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
337 menu
.hilite_pixmap
= None
;
340 image_ctrl
->renderImage(menu
.item_w
, menu
.item_h
, *texture
);
342 if (tmp
) image_ctrl
->removeImage(tmp
);
344 tmp
= menu
.sel_pixmap
;
345 if (texture
->texture() == (BTexture::Flat
| BTexture::Solid
)) {
346 menu
.sel_pixmap
= None
;
348 int hw
= menu
.item_h
/ 2;
350 image_ctrl
->renderImage(hw
, hw
, *texture
);
352 if (tmp
) image_ctrl
->removeImage(tmp
);
354 XResizeWindow(display
, menu
.window
, menu
.width
, menu
.height
);
357 XResizeWindow(display
, menu
.title
, menu
.width
, menu
.title_h
);
359 XMoveResizeWindow(display
, menu
.frame
, 0,
360 ((title_vis
) ? menu
.title_h
+
361 screen
->getBorderWidth() : 0), menu
.width
,
364 XClearWindow(display
, menu
.window
);
365 XClearWindow(display
, menu
.title
);
366 XClearWindow(display
, menu
.frame
);
368 if (title_vis
&& visible
) redrawTitle();
370 const int menu_size
= menuitems
.size();
371 for (int i
= 0; visible
&& i
< menu_size
; i
++) {
372 if (i
== which_sub
) {
373 drawItem(i
, True
, 0);
376 drawItem(i
, False
, 0);
380 if (parent
&& visible
)
381 parent
->drawSubmenu(parent
->which_sub
);
383 XMapSubwindows(display
, menu
.window
);
387 void Basemenu::show(void) {
388 XMapSubwindows(display
, menu
.window
);
389 XMapWindow(display
, menu
.window
);
393 if (shown
&& (! shown
->torn
))
401 void Basemenu::hide(void) {
402 if ((! torn
) && hide_tree
&& parent
&& parent
->isVisible()) {
403 Basemenu
*p
= parent
;
405 while (p
->isVisible() && (! p
->torn
) && p
->parent
) p
= p
->parent
;
413 void Basemenu::internal_hide(void) {
414 BasemenuItem
*tmp
= find(which_sub
);
416 tmp
->submenu()->internal_hide();
418 if (parent
&& (! torn
)) {
419 parent
->drawItem(parent
->which_sub
, False
, True
);
421 parent
->which_sub
= -1;
422 } else if (shown
&& shown
->menu
.window
== menu
.window
) {
423 shown
= (Basemenu
*) 0;
426 torn
= visible
= False
;
427 which_sub
= which_press
= which_sub
= -1;
429 XUnmapWindow(display
, menu
.window
);
433 void Basemenu::move(int x
, int y
) {
436 XMoveWindow(display
, menu
.window
, x
, y
);
438 drawSubmenu(which_sub
);
442 void Basemenu::redrawTitle(void) {
443 const char *text
= (! menu
.label
.empty()) ? getLabel() :
444 i18n(BasemenuSet
, BasemenuBlackboxMenu
, "Blackbox Menu");
445 int dx
= menu
.bevel_w
;
447 MenuStyle
*style
= screen
->getMenuStyle();
449 l
= style
->t_font
->measureString(text
) + menu
.bevel_w
* 2;
451 switch (screen
->getMenuStyle()->t_justify
) {
453 dx
+= menu
.width
- l
;
457 dx
+= (menu
.width
- l
) / 2;
465 style
->t_font
->drawString(menu
.title
, dx
, menu
.bevel_w
,
466 style
->t_text
, text
);
470 void Basemenu::drawSubmenu(int index
) {
471 BasemenuItem
*item
= find(which_sub
);
472 if (item
&& item
->submenu() && ! item
->submenu()->isTorn() &&
474 item
->submenu()->internal_hide();
479 Basemenu
*submenu
= item
->submenu();
481 if (submenu
&& visible
&& ! submenu
->isTorn() && item
->isEnabled()) {
482 if (submenu
->parent
!= this) submenu
->parent
= this;
483 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
),
484 x
= menu
.x
+ ((menu
.item_w
* (sbl
+ 1)) + screen
->getBorderWidth()), y
;
486 if (alignment
== AlignTop
) {
487 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
488 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
489 ((submenu
->title_vis
) ?
490 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
492 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
494 ((title_vis
) ? menu
.title_h
+ screen
->getBorderWidth() : 0) -
495 ((submenu
->title_vis
) ?
496 submenu
->menu
.title_h
+ screen
->getBorderWidth() : 0));
499 if (alignment
== AlignBottom
&&
500 (y
+ submenu
->menu
.height
) > ((shifted
) ? menu
.y_shift
:
501 menu
.y
) + menu
.height
)
502 y
= (((shifted
) ? menu
.y_shift
: menu
.y
) +
503 menu
.height
- submenu
->menu
.height
);
505 if ((x
+ submenu
->getWidth()) > screen
->getWidth())
506 x
= ((shifted
) ? menu
.x_shift
: menu
.x
) -
507 submenu
->getWidth() - screen
->getBorderWidth();
511 if ((y
+ submenu
->getHeight()) > screen
->getHeight())
512 y
= screen
->getHeight() - submenu
->getHeight() -
513 (screen
->getBorderWidth() * 2);
517 if (! moving
) drawItem(index
, True
);
519 if (! submenu
->isVisible())
521 submenu
->moving
= moving
;
529 bool Basemenu::hasSubmenu(int index
) {
530 BasemenuItem
*item
= find(index
);
531 if (item
&& item
->submenu())
537 void Basemenu::drawItem(int index
, bool highlight
, bool clear
,
538 int x
, int y
, unsigned int w
, unsigned int h
) {
539 BasemenuItem
*item
= find(index
);
542 bool dotext
= True
, dohilite
= True
, dosel
= True
;
543 const char *text
= item
->label();
544 int sbl
= index
/ menu
.persub
, i
= index
- (sbl
* menu
.persub
);
545 int item_x
= (sbl
* menu
.item_w
), item_y
= (i
* menu
.item_h
);
546 int hilite_x
= item_x
, hilite_y
= item_y
, hoff_x
= 0, hoff_y
= 0;
547 int text_x
= 0, text_y
= 0, sel_x
= 0, sel_y
= 0;
548 unsigned int hilite_w
= menu
.item_w
, hilite_h
= menu
.item_h
, text_w
= 0,
550 unsigned int half_w
= menu
.item_h
/ 2, quarter_w
= menu
.item_h
/ 4;
553 text_w
= screen
->getMenuStyle()->f_font
->measureString(text
);
554 text_y
= item_y
+ menu
.bevel_w
/ 2;
556 switch(screen
->getMenuStyle()->f_justify
) {
558 text_x
= item_x
+ menu
.bevel_w
+ menu
.item_h
+ 1;
562 text_x
= item_x
+ menu
.item_w
- (menu
.item_h
+ menu
.bevel_w
+ text_w
);
566 text_x
= item_x
+ ((menu
.item_w
+ 1 - text_w
) / 2);
570 text_h
= menu
.item_h
- menu
.bevel_w
;
573 MenuStyle
*style
= screen
->getMenuStyle();
574 BPen
pen((highlight
|| item
->isSelected()) ? style
->h_text
: style
->f_text
),
575 hipen(style
->hilite
.color());
579 if (screen
->getMenuStyle()->bullet_pos
== Right
)
580 sel_x
+= (menu
.item_w
- menu
.item_h
- menu
.bevel_w
);
582 sel_y
= item_y
+ quarter_w
;
585 XClearArea(display
, menu
.frame
, item_x
, item_y
, menu
.item_w
, menu
.item_h
,
587 } else if (! (x
== y
&& y
== -1 && w
== h
&& h
== 0)) {
588 // calculate the which part of the hilite to redraw
589 if (! (max(item_x
, x
) <= min
<signed>(item_x
+ menu
.item_w
, x
+ w
) &&
590 max(item_y
, y
) <= min
<signed>(item_y
+ menu
.item_h
, y
+ h
))) {
593 hilite_x
= max(item_x
, x
);
594 hilite_y
= max(item_y
, y
);
595 hilite_w
= min(item_x
+ menu
.item_w
, x
+ w
) - hilite_x
;
596 hilite_h
= min(item_y
+ menu
.item_h
, y
+ h
) - hilite_y
;
597 hoff_x
= hilite_x
% menu
.item_w
;
598 hoff_y
= hilite_y
% menu
.item_h
;
601 // check if we need to redraw the text
602 int text_ry
= item_y
+ (menu
.bevel_w
/ 2);
603 if (! (max(text_x
, x
) <= min
<signed>(text_x
+ text_w
, x
+ w
) &&
604 max(text_ry
, y
) <= min
<signed>(text_ry
+ text_h
, y
+ h
)))
607 // check if we need to redraw the select pixmap/menu bullet
608 if (! (max(sel_x
, x
) <= min
<signed>(sel_x
+ half_w
, x
+ w
) &&
609 max(sel_y
, y
) <= min
<signed>(sel_y
+ half_w
, y
+ h
)))
613 if (dohilite
&& highlight
&& (menu
.hilite_pixmap
!= ParentRelative
)) {
614 if (menu
.hilite_pixmap
)
615 XCopyArea(display
, menu
.hilite_pixmap
, menu
.frame
,
616 hipen
.gc(), hoff_x
, hoff_y
,
617 hilite_w
, hilite_h
, hilite_x
, hilite_y
);
619 XFillRectangle(display
, menu
.frame
, hipen
.gc(),
620 hilite_x
, hilite_y
, hilite_w
, hilite_h
);
621 } else if (dosel
&& item
->isSelected() &&
622 (menu
.sel_pixmap
!= ParentRelative
)) {
624 XCopyArea(display
, menu
.sel_pixmap
, menu
.frame
, hipen
.gc(), 0, 0,
625 half_w
, half_w
, sel_x
, sel_y
);
627 XFillRectangle(display
, menu
.frame
, hipen
.gc(), sel_x
, sel_y
, half_w
, half_w
);
630 if (dotext
&& text
) {
631 style
->f_font
->drawString(menu
.frame
, text_x
, text_y
,
632 (highlight
? style
->h_text
:
633 (item
->isEnabled() ? style
->f_text
:
638 if (dosel
&& item
->submenu()) {
639 switch (screen
->getMenuStyle()->bullet
) {
641 XDrawRectangle(display
, menu
.frame
, pen
.gc(), sel_x
, sel_y
, half_w
, half_w
);
647 if (screen
->getMenuStyle()->bullet_pos
== Right
) {
648 tri
[0].x
= sel_x
+ quarter_w
- 2;
649 tri
[0].y
= sel_y
+ quarter_w
- 2;
655 tri
[0].x
= sel_x
+ quarter_w
- 2;
656 tri
[0].y
= item_y
+ half_w
;
663 XFillPolygon(display
, menu
.frame
, pen
.gc(), tri
, 3, Convex
,
670 dia
[0].x
= sel_x
+ quarter_w
- 3;
671 dia
[0].y
= item_y
+ half_w
;
679 XFillPolygon(display
, menu
.frame
, pen
.gc(), dia
, 4, Convex
,
687 void Basemenu::setLabel(const string
& label
) {
692 void Basemenu::setItemSelected(int index
, bool sel
) {
694 BasemenuItem
*item
= find(index
);
697 item
->setSelected(sel
);
698 if (visible
) drawItem(index
, (index
== which_sub
), True
);
702 bool Basemenu::isItemSelected(int index
) {
704 BasemenuItem
*item
= find(index
);
705 if (! item
) return False
;
707 return item
->isSelected();
711 void Basemenu::setItemEnabled(int index
, bool enable
) {
713 BasemenuItem
*item
= find(index
);
716 item
->setEnabled(enable
);
717 if (visible
) drawItem(index
, (index
== which_sub
), True
);
721 bool Basemenu::isItemEnabled(int index
) {
723 BasemenuItem
*item
= find(index
);
724 if (! item
) return False
;
726 return item
->isEnabled();
730 void Basemenu::buttonPressEvent(XButtonEvent
*be
) {
731 if (be
->window
== menu
.frame
) {
732 int sbl
= (be
->x
/ menu
.item_w
), i
= (be
->y
/ menu
.item_h
);
733 int w
= (sbl
* menu
.persub
) + i
;
735 BasemenuItem
*item
= find(w
);
744 drawItem(w
, (item
->isEnabled()), True
);
747 menu
.x_move
= be
->x_root
- menu
.x
;
748 menu
.y_move
= be
->y_root
- menu
.y
;
753 void Basemenu::buttonReleaseEvent(XButtonEvent
*re
) {
754 if (re
->window
== menu
.title
) {
759 drawSubmenu(which_sub
);
762 if (re
->x
>= 0 && re
->x
<= static_cast<signed>(menu
.width
) &&
763 re
->y
>= 0 && re
->y
<= static_cast<signed>(menu
.title_h
))
766 } else if (re
->window
== menu
.frame
&&
767 re
->x
>= 0 && re
->x
< static_cast<signed>(menu
.width
) &&
768 re
->y
>= 0 && re
->y
< static_cast<signed>(menu
.frame_h
)) {
769 if (re
->button
== 3) {
772 int sbl
= (re
->x
/ menu
.item_w
), i
= (re
->y
/ menu
.item_h
),
773 ix
= sbl
* menu
.item_w
, iy
= i
* menu
.item_h
,
774 w
= (sbl
* menu
.persub
) + i
,
775 p
= (which_sbl
* menu
.persub
) + which_press
;
777 if (w
>= 0 && w
< static_cast<signed>(menuitems
.size())) {
778 drawItem(p
, (p
== which_sub
), True
);
780 if (p
== w
&& isItemEnabled(w
)) {
781 if (re
->x
> ix
&& re
->x
< static_cast<signed>(ix
+ menu
.item_w
) &&
782 re
->y
> iy
&& re
->y
< static_cast<signed>(iy
+ menu
.item_h
)) {
783 itemSelected(re
->button
, w
);
787 drawItem(p
, False
, True
);
794 void Basemenu::motionNotifyEvent(XMotionEvent
*me
) {
795 if (me
->window
== menu
.title
&& (me
->state
& Button1Mask
)) {
798 if (parent
&& (! torn
)) {
799 parent
->drawItem(parent
->which_sub
, False
, True
);
800 parent
->which_sub
= -1;
803 moving
= torn
= True
;
806 drawSubmenu(which_sub
);
808 menu
.x
= me
->x_root
- menu
.x_move
,
809 menu
.y
= me
->y_root
- menu
.y_move
;
811 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
814 drawSubmenu(which_sub
);
817 } else if ((! (me
->state
& Button1Mask
)) && me
->window
== menu
.frame
&&
818 me
->x
>= 0 && me
->x
< static_cast<signed>(menu
.width
) &&
819 me
->y
>= 0 && me
->y
< static_cast<signed>(menu
.frame_h
)) {
820 int sbl
= (me
->x
/ menu
.item_w
), i
= (me
->y
/ menu
.item_h
),
821 w
= (sbl
* menu
.persub
) + i
;
823 if ((i
!= which_press
|| sbl
!= which_sbl
) &&
824 (w
>= 0 && w
< static_cast<signed>(menuitems
.size()))) {
825 if (which_press
!= -1 && which_sbl
!= -1) {
826 int p
= (which_sbl
* menu
.persub
) + which_press
;
827 BasemenuItem
*item
= find(p
);
829 drawItem(p
, False
, True
);
831 if (item
->submenu()->isVisible() &&
832 (! item
->submenu()->isTorn())) {
833 item
->submenu()->internal_hide();
841 BasemenuItem
*itmp
= find(w
);
846 drawItem(w
, (itmp
->isEnabled()), True
);
852 void Basemenu::exposeEvent(XExposeEvent
*ee
) {
853 if (ee
->window
== menu
.title
) {
855 } else if (ee
->window
== menu
.frame
) {
856 // this is a compilicated algorithm... lets do it step by step...
857 // first... we see in which sub level the expose starts... and how many
858 // items down in that sublevel
860 int sbl
= (ee
->x
/ menu
.item_w
), id
= (ee
->y
/ menu
.item_h
),
861 // next... figure out how many sublevels over the redraw spans
862 sbl_d
= ((ee
->x
+ ee
->width
) / menu
.item_w
),
863 // then we see how many items down to redraw
864 id_d
= ((ee
->y
+ ee
->height
) / menu
.item_h
);
866 if (id_d
> menu
.persub
) id_d
= menu
.persub
;
868 // draw the sublevels and the number of items the exposure spans
869 MenuItems::iterator it
,
870 end
= menuitems
.end();
872 for (i
= sbl
; i
<= sbl_d
; i
++) {
873 // set the iterator to the first item in the sublevel needing redrawing
874 it
= menuitems
.begin() + (id
+ (i
* menu
.persub
));
875 for (ii
= id
; ii
<= id_d
&& it
!= end
; ++it
, ii
++) {
876 int index
= ii
+ (i
* menu
.persub
);
878 drawItem(index
, (which_sub
== index
), False
,
879 ee
->x
, ee
->y
, ee
->width
, ee
->height
);
886 void Basemenu::enterNotifyEvent(XCrossingEvent
*ce
) {
887 if (ce
->window
== menu
.frame
) {
888 menu
.x_shift
= menu
.x
, menu
.y_shift
= menu
.y
;
889 if (menu
.x
+ menu
.width
> screen
->getWidth()) {
890 menu
.x_shift
= screen
->getWidth() - menu
.width
-
891 screen
->getBorderWidth();
893 } else if (menu
.x
< 0) {
894 menu
.x_shift
= -screen
->getBorderWidth();
898 if (menu
.y
+ menu
.height
> screen
->getHeight()) {
899 menu
.y_shift
= screen
->getHeight() - menu
.height
-
900 screen
->getBorderWidth();
902 } else if (menu
.y
+ static_cast<signed>(menu
.title_h
) < 0) {
903 menu
.y_shift
= -screen
->getBorderWidth();
908 XMoveWindow(display
, menu
.window
, menu
.x_shift
, menu
.y_shift
);
910 if (which_sub
!= -1) {
911 BasemenuItem
*tmp
= find(which_sub
);
912 if (tmp
->submenu()->isVisible()) {
913 int sbl
= (ce
->x
/ menu
.item_w
), i
= (ce
->y
/ menu
.item_h
),
914 w
= (sbl
* menu
.persub
) + i
;
916 if (w
!= which_sub
&& (! tmp
->submenu()->isTorn())) {
917 tmp
->submenu()->internal_hide();
919 drawItem(which_sub
, False
, True
);
928 void Basemenu::leaveNotifyEvent(XCrossingEvent
*ce
) {
929 if (ce
->window
== menu
.frame
) {
930 if (which_press
!= -1 && which_sbl
!= -1 && menuitems
.size() > 0) {
931 int p
= (which_sbl
* menu
.persub
) + which_press
;
933 drawItem(p
, (p
== which_sub
), True
);
935 which_sbl
= which_press
= -1;
939 XMoveWindow(display
, menu
.window
, menu
.x
, menu
.y
);
942 if (which_sub
!= -1) drawSubmenu(which_sub
);
948 void Basemenu::reconfigure(void) {
949 XSetWindowBackground(display
, menu
.window
,
950 screen
->getBorderColor()->pixel());
951 XSetWindowBorder(display
, menu
.window
,
952 screen
->getBorderColor()->pixel());
953 XSetWindowBorderWidth(display
, menu
.window
, screen
->getBorderWidth());
955 menu
.bevel_w
= screen
->getBevelWidth();
960 void Basemenu::changeItemLabel(unsigned int index
, const string
& label
) {
961 BasemenuItem
*item
= find(index
);
963 item
->newLabel(label
);