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 *Basemenu::find(int index) {
205 if (index < 0 || index >= static_cast<signed>(menuitems.size()))
206 return (BasemenuItem*) 0;
208 return menuitems[index];
212 int Basemenu::insert(BasemenuItem *item, int pos) {
214 menuitems.push_back(item);
216 assert(pos < static_cast<signed>(menuitems.size()));
217 menuitems.insert((menuitems.begin() + pos), item);
219 return menuitems.size();
223 int Basemenu::insert(const string& label, int function,
224 const string& exec, int pos) {
225 BasemenuItem *item = new BasemenuItem(label, function, exec);
226 return insert(item, pos);
230 int Basemenu::insert(const string& label, Basemenu *submenu, int pos) {
231 BasemenuItem *item = new BasemenuItem(label, submenu);
232 submenu->parent = this;
234 return insert(item, pos);
238 int Basemenu::remove(int index) {
239 BasemenuItem *item = find(index);
240 if (! item) return -1;
242 if ((! internal_menu)) {
243 Basemenu *tmp = (Basemenu *) item->submenu();
245 if (! tmp->internal_menu) {
248 tmp->internal_hide();
255 if (which_sub == index)
257 else if (which_sub > index)
260 menuitems.erase(menuitems.begin() + index);
262 return menuitems.size();
266 void Basemenu::update(void) {
267 MenuStyle *style = screen->getMenuStyle();
268 if (i18n.multibyte()) {
269 menu.item_h = style->f_fontset_extents->max_ink_extent.height +
271 menu.title_h = style->t_fontset_extents->max_ink_extent.height +
274 menu.item_h = style->f_font->ascent + style->f_font->descent +
276 menu.title_h = style->t_font->ascent + style->t_font->descent +
281 const char *s = getLabel();
284 if (i18n.multibyte()) {
285 XRectangle ink, logical;
286 XmbTextExtents(screen->getMenuStyle()->t_fontset, s, l, &ink, &logical);
287 menu.item_w = logical.width;
289 menu.item_w = XTextWidth(screen->getMenuStyle()->t_font, s, l);
292 menu.item_w += (menu.bevel_w * 2);
298 MenuItems::iterator it = menuitems.begin(), end = menuitems.end();
299 for (; it != end; ++it) {
300 BasemenuItem *tmp = *it;
301 const char *s = tmp->l.c_str();
304 if (i18n.multibyte()) {
305 XRectangle ink, logical;
306 XmbTextExtents(screen->getMenuStyle()->f_fontset, s, l, &ink, &logical);
309 ii = XTextWidth(screen->getMenuStyle()->f_font, s, l);
311 ii += (menu.bevel_w * 2) + (menu.item_h * 2);
313 menu.item_w = ((menu.item_w < ii) ? ii : menu.item_w);
316 if (! menuitems.empty()) {
319 unsigned int menu_size = menuitems.size();
320 while (((menu.item_h * (menu_size + 1) / menu.sublevels)
321 + menu.title_h + screen->getBorderWidth()) >
325 if (menu.sublevels < menu.minsub) menu.sublevels = menu.minsub;
327 menu.persub = menu_size / menu.sublevels;
328 if (menu_size % menu.sublevels) menu.persub++;
334 menu.width = (menu.sublevels * (menu.item_w));
335 if (! menu.width) menu.width = menu.item_w;
337 menu.frame_h = (menu.item_h * menu.persub);
338 menu.height = ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) +
340 if (! menu.frame_h) menu.frame_h = 1;
341 if (menu.height < 1) menu.height = 1;
346 tmp = menu.title_pixmap;
347 texture = &(screen->getMenuStyle()->title);
348 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
349 menu.title_pixmap = None;
350 XSetWindowBackground(display, menu.title,
351 texture->color().pixel());
354 image_ctrl->renderImage(menu.width, menu.title_h, *texture);
355 XSetWindowBackgroundPixmap(display, menu.title, menu.title_pixmap);
357 if (tmp) image_ctrl->removeImage(tmp);
358 XClearWindow(display, menu.title);
361 tmp = menu.frame_pixmap;
362 texture = &(screen->getMenuStyle()->frame);
363 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
364 menu.frame_pixmap = None;
365 XSetWindowBackground(display, menu.frame,
366 texture->color().pixel());
369 image_ctrl->renderImage(menu.width, menu.frame_h, *texture);
370 XSetWindowBackgroundPixmap(display, menu.frame, menu.frame_pixmap);
372 if (tmp) image_ctrl->removeImage(tmp);
374 tmp = menu.hilite_pixmap;
375 texture = &(screen->getMenuStyle()->hilite);
376 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
377 menu.hilite_pixmap = None;
380 image_ctrl->renderImage(menu.item_w, menu.item_h, *texture);
382 if (tmp) image_ctrl->removeImage(tmp);
384 tmp = menu.sel_pixmap;
385 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
386 menu.sel_pixmap = None;
388 int hw = menu.item_h / 2;
390 image_ctrl->renderImage(hw, hw, *texture);
392 if (tmp) image_ctrl->removeImage(tmp);
394 XResizeWindow(display, menu.window, menu.width, menu.height);
397 XResizeWindow(display, menu.title, menu.width, menu.title_h);
399 XMoveResizeWindow(display, menu.frame, 0,
400 ((title_vis) ? menu.title_h +
401 screen->getBorderWidth() : 0), menu.width,
404 XClearWindow(display, menu.window);
405 XClearWindow(display, menu.title);
406 XClearWindow(display, menu.frame);
408 if (title_vis && visible) redrawTitle();
410 const int menu_size = menuitems.size();
411 for (int i = 0; visible && i < menu_size; i++) {
412 if (i == which_sub) {
413 drawItem(i, True, 0);
416 drawItem(i, False, 0);
420 if (parent && visible)
421 parent->drawSubmenu(parent->which_sub);
423 XMapSubwindows(display, menu.window);
427 void Basemenu::show(void) {
428 XMapSubwindows(display, menu.window);
429 XMapWindow(display, menu.window);
433 if (shown && (! shown->torn))
441 void Basemenu::hide(void) {
442 if ((! torn) && hide_tree && parent && parent->isVisible()) {
443 Basemenu *p = parent;
445 while (p->isVisible() && (! p->torn) && p->parent) p = p->parent;
453 void Basemenu::internal_hide(void) {
454 BasemenuItem *tmp = find(which_sub);
456 tmp->submenu()->internal_hide();
458 if (parent && (! torn)) {
459 parent->drawItem(parent->which_sub, False, True);
461 parent->which_sub = -1;
462 } else if (shown && shown->menu.window == menu.window) {
463 shown = (Basemenu *) 0;
466 torn = visible = False;
467 which_sub = which_press = which_sub = -1;
469 XUnmapWindow(display, menu.window);
473 void Basemenu::move(int x, int y) {
476 XMoveWindow(display, menu.window, x, y);
478 drawSubmenu(which_sub);
482 void Basemenu::redrawTitle(void) {
483 const char *text = (! menu.label.empty()) ? getLabel() :
484 i18n(BasemenuSet, BasemenuBlackboxMenu, "Blackbox Menu");
485 int dx = menu.bevel_w, len = strlen(text);
488 if (i18n.multibyte()) {
489 XRectangle ink, logical;
490 XmbTextExtents(screen->getMenuStyle()->t_fontset, text, len,
494 l = XTextWidth(screen->getMenuStyle()->t_font, text, len);
497 l += (menu.bevel_w * 2);
499 switch (screen->getMenuStyle()->t_justify) {
501 dx += menu.width - l;
505 dx += (menu.width - l) / 2;
513 MenuStyle *style = screen->getMenuStyle();
514 BPen pen(style->t_text, style->t_font);
515 if (i18n.multibyte())
516 XmbDrawString(display, menu.title, style->t_fontset, pen.gc(), dx,
517 (menu.bevel_w - style->t_fontset_extents->max_ink_extent.y),
520 XDrawString(display, menu.title, pen.gc(), dx,
521 (style->t_font->ascent + menu.bevel_w), text, len);
525 void Basemenu::drawSubmenu(int index) {
526 BasemenuItem *item = find(which_sub);
527 if (item && item->submenu() && ! item->submenu()->isTorn() &&
529 item->submenu()->internal_hide();
534 Basemenu *submenu = item->submenu();
536 if (submenu && visible && ! submenu->isTorn() && item->isEnabled()) {
537 if (submenu->parent != this) submenu->parent = this;
538 int sbl = index / menu.persub, i = index - (sbl * menu.persub),
539 x = menu.x + ((menu.item_w * (sbl + 1)) + screen->getBorderWidth()), y;
541 if (alignment == AlignTop) {
542 y = (((shifted) ? menu.y_shift : menu.y) +
543 ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) -
544 ((submenu->title_vis) ?
545 submenu->menu.title_h + screen->getBorderWidth() : 0));
547 y = (((shifted) ? menu.y_shift : menu.y) +
549 ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) -
550 ((submenu->title_vis) ?
551 submenu->menu.title_h + screen->getBorderWidth() : 0));
554 if (alignment == AlignBottom &&
555 (y + submenu->menu.height) > ((shifted) ? menu.y_shift :
556 menu.y) + menu.height)
557 y = (((shifted) ? menu.y_shift : menu.y) +
558 menu.height - submenu->menu.height);
560 if ((x + submenu->getWidth()) > screen->getWidth())
561 x = ((shifted) ? menu.x_shift : menu.x) -
562 submenu->getWidth() - screen->getBorderWidth();
566 if ((y + submenu->getHeight()) > screen->getHeight())
567 y = screen->getHeight() - submenu->getHeight() -
568 (screen->getBorderWidth() * 2);
572 if (! moving) drawItem(index, True);
574 if (! submenu->isVisible())
576 submenu->moving = moving;
584 bool Basemenu::hasSubmenu(int index) {
585 BasemenuItem *item = find(index);
586 if (item && item->submenu())
592 void Basemenu::drawItem(int index, bool highlight, bool clear,
593 int x, int y, unsigned int w, unsigned int h) {
594 BasemenuItem *item = find(index);
597 bool dotext = True, dohilite = True, dosel = True;
598 const char *text = item->label();
599 int sbl = index / menu.persub, i = index - (sbl * menu.persub);
600 int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h);
601 int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0;
602 int text_x = 0, text_y = 0, len = strlen(text), sel_x = 0, sel_y = 0;
603 unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0,
605 unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4;
608 if (i18n.multibyte()) {
609 XRectangle ink, logical;
610 XmbTextExtents(screen->getMenuStyle()->f_fontset,
611 text, len, &ink, &logical);
612 text_w = logical.width;
613 text_y = item_y + (menu.bevel_w / 2) -
614 screen->getMenuStyle()->f_fontset_extents->max_ink_extent.y;
616 text_w = XTextWidth(screen->getMenuStyle()->f_font, text, len);
618 screen->getMenuStyle()->f_font->ascent +
622 switch(screen->getMenuStyle()->f_justify) {
624 text_x = item_x + menu.bevel_w + menu.item_h + 1;
628 text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w);
632 text_x = item_x + ((menu.item_w + 1 - text_w) / 2);
636 text_h = menu.item_h - menu.bevel_w;
639 MenuStyle *style = screen->getMenuStyle();
640 BPen pen((highlight || item->isSelected()) ? style->h_text : style->f_text),
641 textpen((highlight) ? style->h_text :
642 item->isEnabled() ? style->f_text : style->d_text, style->f_font),
643 hipen(style->hilite.color());
647 if (screen->getMenuStyle()->bullet_pos == Right)
648 sel_x += (menu.item_w - menu.item_h - menu.bevel_w);
650 sel_y = item_y + quarter_w;
653 XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h,
655 } else if (! (x == y && y == -1 && w == h && h == 0)) {
656 // calculate the which part of the hilite to redraw
657 if (! (max(item_x, x) <= min<signed>(item_x + menu.item_w, x + w) &&
658 max(item_y, y) <= min<signed>(item_y + menu.item_h, y + h))) {
661 hilite_x = max(item_x, x);
662 hilite_y = max(item_y, y);
663 hilite_w = min(item_x + menu.item_w, x + w) - hilite_x;
664 hilite_h = min(item_y + menu.item_h, y + h) - hilite_y;
665 hoff_x = hilite_x % menu.item_w;
666 hoff_y = hilite_y % menu.item_h;
669 // check if we need to redraw the text
670 int text_ry = item_y + (menu.bevel_w / 2);
671 if (! (max(text_x, x) <= min<signed>(text_x + text_w, x + w) &&
672 max(text_ry, y) <= min<signed>(text_ry + text_h, y + h)))
675 // check if we need to redraw the select pixmap/menu bullet
676 if (! (max(sel_x, x) <= min<signed>(sel_x + half_w, x + w) &&
677 max(sel_y, y) <= min<signed>(sel_y + half_w, y + h)))
681 if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) {
682 if (menu.hilite_pixmap)
683 XCopyArea(display, menu.hilite_pixmap, menu.frame,
684 hipen.gc(), hoff_x, hoff_y,
685 hilite_w, hilite_h, hilite_x, hilite_y);
687 XFillRectangle(display, menu.frame, hipen.gc(),
688 hilite_x, hilite_y, hilite_w, hilite_h);
689 } else if (dosel && item->isSelected() &&
690 (menu.sel_pixmap != ParentRelative)) {
692 XCopyArea(display, menu.sel_pixmap, menu.frame, hipen.gc(), 0, 0,
693 half_w, half_w, sel_x, sel_y);
695 XFillRectangle(display, menu.frame, hipen.gc(), sel_x, sel_y, half_w, half_w);
698 if (dotext && text) {
699 if (i18n.multibyte())
700 XmbDrawString(display, menu.frame, screen->getMenuStyle()->f_fontset,
701 textpen.gc(), text_x, text_y, text, len);
703 XDrawString(display, menu.frame, textpen.gc(), text_x, text_y, text, len);
706 if (dosel && item->submenu()) {
707 switch (screen->getMenuStyle()->bullet) {
709 XDrawRectangle(display, menu.frame, pen.gc(), sel_x, sel_y, half_w, half_w);
715 if (screen->getMenuStyle()->bullet_pos == Right) {
716 tri[0].x = sel_x + quarter_w - 2;
717 tri[0].y = sel_y + quarter_w - 2;
723 tri[0].x = sel_x + quarter_w - 2;
724 tri[0].y = item_y + half_w;
731 XFillPolygon(display, menu.frame, pen.gc(), tri, 3, Convex,
738 dia[0].x = sel_x + quarter_w - 3;
739 dia[0].y = item_y + half_w;
747 XFillPolygon(display, menu.frame, pen.gc(), dia, 4, Convex,
755 void Basemenu::setLabel(const string& label) {
760 void Basemenu::setItemSelected(int index, bool sel) {
762 BasemenuItem *item = find(index);
765 item->setSelected(sel);
766 if (visible) drawItem(index, (index == which_sub), True);
770 bool Basemenu::isItemSelected(int index) {
772 BasemenuItem *item = find(index);
773 if (! item) return False;
775 return item->isSelected();
779 void Basemenu::setItemEnabled(int index, bool enable) {
781 BasemenuItem *item = find(index);
784 item->setEnabled(enable);
785 if (visible) drawItem(index, (index == which_sub), True);
789 bool Basemenu::isItemEnabled(int index) {
791 BasemenuItem *item = find(index);
792 if (! item) return False;
794 return item->isEnabled();
798 void Basemenu::buttonPressEvent(XButtonEvent *be) {
799 if (be->window == menu.frame) {
800 int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h);
801 int w = (sbl * menu.persub) + i;
803 BasemenuItem *item = find(w);
812 drawItem(w, (item->isEnabled()), True);
815 menu.x_move = be->x_root - menu.x;
816 menu.y_move = be->y_root - menu.y;
821 void Basemenu::buttonReleaseEvent(XButtonEvent *re) {
822 if (re->window == menu.title) {
827 drawSubmenu(which_sub);
830 if (re->x >= 0 && re->x <= static_cast<signed>(menu.width) &&
831 re->y >= 0 && re->y <= static_cast<signed>(menu.title_h))
834 } else if (re->window == menu.frame &&
835 re->x >= 0 && re->x < static_cast<signed>(menu.width) &&
836 re->y >= 0 && re->y < static_cast<signed>(menu.frame_h)) {
837 if (re->button == 3) {
840 int sbl = (re->x / menu.item_w), i = (re->y / menu.item_h),
841 ix = sbl * menu.item_w, iy = i * menu.item_h,
842 w = (sbl * menu.persub) + i,
843 p = (which_sbl * menu.persub) + which_press;
845 if (w >= 0 && w < static_cast<signed>(menuitems.size())) {
846 drawItem(p, (p == which_sub), True);
848 if (p == w && isItemEnabled(w)) {
849 if (re->x > ix && re->x < static_cast<signed>(ix + menu.item_w) &&
850 re->y > iy && re->y < static_cast<signed>(iy + menu.item_h)) {
851 itemSelected(re->button, w);
855 drawItem(p, False, True);
862 void Basemenu::motionNotifyEvent(XMotionEvent *me) {
863 if (me->window == menu.title && (me->state & Button1Mask)) {
866 if (parent && (! torn)) {
867 parent->drawItem(parent->which_sub, False, True);
868 parent->which_sub = -1;
871 moving = torn = True;
874 drawSubmenu(which_sub);
876 menu.x = me->x_root - menu.x_move,
877 menu.y = me->y_root - menu.y_move;
879 XMoveWindow(display, menu.window, menu.x, menu.y);
882 drawSubmenu(which_sub);
885 } else if ((! (me->state & Button1Mask)) && me->window == menu.frame &&
886 me->x >= 0 && me->x < static_cast<signed>(menu.width) &&
887 me->y >= 0 && me->y < static_cast<signed>(menu.frame_h)) {
888 int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h),
889 w = (sbl * menu.persub) + i;
891 if ((i != which_press || sbl != which_sbl) &&
892 (w >= 0 && w < static_cast<signed>(menuitems.size()))) {
893 if (which_press != -1 && which_sbl != -1) {
894 int p = (which_sbl * menu.persub) + which_press;
895 BasemenuItem *item = find(p);
897 drawItem(p, False, True);
899 if (item->submenu()->isVisible() &&
900 (! item->submenu()->isTorn())) {
901 item->submenu()->internal_hide();
909 BasemenuItem *itmp = find(w);
914 drawItem(w, (itmp->isEnabled()), True);
920 void Basemenu::exposeEvent(XExposeEvent *ee) {
921 if (ee->window == menu.title) {
923 } else if (ee->window == menu.frame) {
924 // this is a compilicated algorithm... lets do it step by step...
925 // first... we see in which sub level the expose starts... and how many
926 // items down in that sublevel
928 int sbl = (ee->x / menu.item_w), id = (ee->y / menu.item_h),
929 // next... figure out how many sublevels over the redraw spans
930 sbl_d = ((ee->x + ee->width) / menu.item_w),
931 // then we see how many items down to redraw
932 id_d = ((ee->y + ee->height) / menu.item_h);
934 if (id_d > menu.persub) id_d = menu.persub;
936 // draw the sublevels and the number of items the exposure spans
937 MenuItems::iterator it,
938 end = menuitems.end();
940 for (i = sbl; i <= sbl_d; i++) {
941 // set the iterator to the first item in the sublevel needing redrawing
942 it = menuitems.begin() + (id + (i * menu.persub));
943 for (ii = id; ii <= id_d && it != end; ++it, ii++) {
944 int index = ii + (i * menu.persub);
946 drawItem(index, (which_sub == index), False,
947 ee->x, ee->y, ee->width, ee->height);
954 void Basemenu::enterNotifyEvent(XCrossingEvent *ce) {
955 if (ce->window == menu.frame) {
956 menu.x_shift = menu.x, menu.y_shift = menu.y;
957 if (menu.x + menu.width > screen->getWidth()) {
958 menu.x_shift = screen->getWidth() - menu.width -
959 screen->getBorderWidth();
961 } else if (menu.x < 0) {
962 menu.x_shift = -screen->getBorderWidth();
966 if (menu.y + menu.height > screen->getHeight()) {
967 menu.y_shift = screen->getHeight() - menu.height -
968 screen->getBorderWidth();
970 } else if (menu.y + static_cast<signed>(menu.title_h) < 0) {
971 menu.y_shift = -screen->getBorderWidth();
976 XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift);
978 if (which_sub != -1) {
979 BasemenuItem *tmp = find(which_sub);
980 if (tmp->submenu()->isVisible()) {
981 int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h),
982 w = (sbl * menu.persub) + i;
984 if (w != which_sub && (! tmp->submenu()->isTorn())) {
985 tmp->submenu()->internal_hide();
987 drawItem(which_sub, False, True);
996 void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) {
997 if (ce->window == menu.frame) {
998 if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) {
999 int p = (which_sbl * menu.persub) + which_press;
1001 drawItem(p, (p == which_sub), True);
1003 which_sbl = which_press = -1;
1007 XMoveWindow(display, menu.window, menu.x, menu.y);
1010 if (which_sub != -1) drawSubmenu(which_sub);
1016 void Basemenu::reconfigure(void) {
1017 XSetWindowBackground(display, menu.window,
1018 screen->getBorderColor()->pixel());
1019 XSetWindowBorder(display, menu.window,
1020 screen->getBorderColor()->pixel());
1021 XSetWindowBorderWidth(display, menu.window, screen->getBorderWidth());
1023 menu.bevel_w = screen->getBevelWidth();
1028 void Basemenu::changeItemLabel(unsigned int index, const string& label) {
1029 BasemenuItem *item = find(index);
1031 item->newLabel(label);