X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=src%2FBasemenu.cc;h=90d5e458ebcdc8e21b56fd0e47ff1e0c9f2eb037;hb=23e92e110e467796437d981c0c5bc129c0e20b8e;hp=b0e9760623ba6141298d0b8e9091db7621c1cbb3;hpb=dfc5f034581f5a26cba5c4811500438f89f0634a;p=chaz%2Fopenbox diff --git a/src/Basemenu.cc b/src/Basemenu.cc index b0e97606..90d5e458 100644 --- a/src/Basemenu.cc +++ b/src/Basemenu.cc @@ -1,5 +1,6 @@ -// Basemenu.cc for Openbox -// Copyright (c) 2001 Sean 'Shaleh' Perry +// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- +// Basemenu.cc for Blackbox - an X11 Window manager +// Copyright (c) 2001 - 2002 Sean 'Shaleh' Perry // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net) // // Permission is hereby granted, free of charge, to any person obtaining a @@ -20,40 +21,45 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -// stupid macros needed to access some functions in version 2 of the GNU C -// library -#ifndef _GNU_SOURCE -#define _GNU_SOURCE -#endif // _GNU_SOURCE - #ifdef HAVE_CONFIG_H # include "../config.h" #endif // HAVE_CONFIG_H +extern "C" { #ifdef HAVE_STDIO_H # include #endif // HAVE_STDIO_H -#ifdef STDC_HEADERS +#ifdef HAVE_STDLIB_H # include +#endif // HAVE_STDLIB_H + +#ifdef HAVE_STRING_H # include -#endif // STDC_HEADERS +#endif // HAVE_STRING_H +} #include +#include using namespace std; -#include "i18n.h" -#include "openbox.h" -#include "Basemenu.h" -#include "Screen.h" +#include "i18n.hh" +#include "blackbox.hh" +#include "Basemenu.hh" +#include "Font.hh" +#include "GCCache.hh" +#include "Image.hh" +#include "Screen.hh" +#include "Util.hh" + static Basemenu *shown = (Basemenu *) 0; Basemenu::Basemenu(BScreen *scrn) { screen = scrn; - openbox = screen->getOpenbox(); + blackbox = screen->getBlackbox(); image_ctrl = screen->getImageControl(); - display = openbox->getXDisplay(); + display = blackbox->getXDisplay(); parent = (Basemenu *) 0; alignment = AlignDontCare; @@ -80,93 +86,85 @@ Basemenu::Basemenu(BScreen *scrn) { menu.frame_pixmap = menu.title_pixmap = - menu.hilite_pixmap = - menu.sel_pixmap = None; + menu.hilite_pixmap = None; menu.bevel_w = screen->getBevelWidth(); - if (i18n->multibyte()) - menu.width = menu.title_h = menu.item_w = menu.frame_h = - screen->getMenuStyle()->t_fontset_extents->max_ink_extent.height + - (menu.bevel_w * 2); - else - menu.width = menu.title_h = menu.item_w = menu.frame_h = - screen->getMenuStyle()->t_font->ascent + - screen->getMenuStyle()->t_font->descent + (menu.bevel_w * 2); - - menu.label = 0; - + MenuStyle *style = screen->getMenuStyle(); + menu.width = menu.title_h = menu.item_w = menu.frame_h = + style->t_font->height() + (menu.bevel_w * 2); + menu.sublevels = menu.persub = menu.minsub = 0; - - MenuStyle *style = screen->getMenuStyle(); - if (i18n->multibyte()) { - menu.item_h = style->f_fontset_extents->max_ink_extent.height + - (menu.bevel_w); - } else { - menu.item_h = style->f_font->ascent + style->f_font->descent + - (menu.bevel_w); - } - + + menu.item_h = style->f_font->height() + menu.bevel_w; + menu.height = menu.title_h + screen->getBorderWidth() + menu.frame_h; - + unsigned long attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel | - CWColormap | CWOverrideRedirect | CWEventMask; + CWColormap | CWOverrideRedirect | CWEventMask; XSetWindowAttributes attrib; attrib.background_pixmap = None; attrib.background_pixel = attrib.border_pixel = - screen->getBorderColor()->getPixel(); + screen->getBorderColor()->pixel(); attrib.colormap = screen->getColormap(); attrib.override_redirect = True; attrib.event_mask = ButtonPressMask | ButtonReleaseMask | - ButtonMotionMask | ExposureMask; + ButtonMotionMask | ExposureMask; menu.window = - XCreateWindow(display, screen->getRootWindow(), menu.x, menu.y, menu.width, - menu.height, screen->getBorderWidth(), screen->getDepth(), + XCreateWindow(display, screen->getRootWindow(), + menu.x, menu.y, menu.width, menu.height, + screen->getBorderWidth(), screen->getDepth(), InputOutput, screen->getVisual(), attrib_mask, &attrib); - openbox->saveMenuSearch(menu.window, this); + blackbox->saveMenuSearch(menu.window, this); attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel | CWEventMask; - attrib.background_pixel = screen->getBorderColor()->getPixel(); + attrib.background_pixel = screen->getBorderColor()->pixel(); attrib.event_mask |= EnterWindowMask | LeaveWindowMask; menu.title = XCreateWindow(display, menu.window, 0, 0, menu.width, menu.height, 0, - screen->getDepth(), InputOutput, screen->getVisual(), - attrib_mask, &attrib); - openbox->saveMenuSearch(menu.title, this); + screen->getDepth(), InputOutput, screen->getVisual(), + attrib_mask, &attrib); + blackbox->saveMenuSearch(menu.title, this); attrib.event_mask |= PointerMotionMask; menu.frame = XCreateWindow(display, menu.window, 0, - menu.title_h + screen->getBorderWidth(), - menu.width, menu.frame_h, 0, - screen->getDepth(), InputOutput, - screen->getVisual(), attrib_mask, &attrib); - openbox->saveMenuSearch(menu.frame, this); - - menuitems = new LinkedList; + menu.title_h + screen->getBorderWidth(), + menu.width, menu.frame_h, 0, + screen->getDepth(), InputOutput, + screen->getVisual(), attrib_mask, &attrib); + blackbox->saveMenuSearch(menu.frame, this); // even though this is the end of the constructor the menu is still not // completely created. items must be inserted and it must be update()'d } - Basemenu::~Basemenu(void) { XUnmapWindow(display, menu.window); if (shown && shown->getWindowID() == getWindowID()) shown = (Basemenu *) 0; - int n = menuitems->count(); - for (int i = 0; i < n; ++i) - remove(0); - - delete menuitems; + MenuItems::const_iterator it = menuitems.begin(); + while (it != menuitems.end()) { + BasemenuItem *item = *it; + if (! internal_menu) { + Basemenu *tmp = (Basemenu *) item->submenu(); + if (tmp) { + if (! tmp->internal_menu) { + delete tmp; + } else { + tmp->internal_hide(); + } + } + } + ++it; + } - if (menu.label) - delete [] menu.label; + std::for_each(menuitems.begin(), menuitems.end(), PointerAssassin()); if (menu.title_pixmap) image_ctrl->removeImage(menu.title_pixmap); @@ -177,154 +175,114 @@ Basemenu::~Basemenu(void) { if (menu.hilite_pixmap) image_ctrl->removeImage(menu.hilite_pixmap); - if (menu.sel_pixmap) - image_ctrl->removeImage(menu.sel_pixmap); - - openbox->removeMenuSearch(menu.title); + blackbox->removeMenuSearch(menu.title); XDestroyWindow(display, menu.title); - openbox->removeMenuSearch(menu.frame); + blackbox->removeMenuSearch(menu.frame); XDestroyWindow(display, menu.frame); - openbox->removeMenuSearch(menu.window); + blackbox->removeMenuSearch(menu.window); XDestroyWindow(display, menu.window); } -int Basemenu::insert(const char *l, int function, const char *e, int pos) { - char *label = 0, *exec = 0; +BasemenuItem *Basemenu::find(int index) { + if (index < 0 || index >= static_cast(menuitems.size())) + return (BasemenuItem*) 0; - if (l) label = bstrdup(l); - if (e) exec = bstrdup(e); + return menuitems[index]; +} - BasemenuItem *item = new BasemenuItem(label, function, exec); - menuitems->insert(item, pos); - return menuitems->count(); +int Basemenu::insert(BasemenuItem *item, int pos) { + if (pos < 0) { + menuitems.push_back(item); + } else { + assert(pos <= static_cast(menuitems.size())); + menuitems.insert((menuitems.begin() + pos), item); + } + return menuitems.size(); } -int Basemenu::insert(const char *l, Basemenu *submenu, int pos) { - char *label = 0; +int Basemenu::insert(const string& label, int function, + const string& exec, int pos) { + BasemenuItem *item = new BasemenuItem(label, function, exec); + return insert(item, pos); +} - if (l) label = bstrdup(l); +int Basemenu::insert(const string& label, Basemenu *submenu, int pos) { BasemenuItem *item = new BasemenuItem(label, submenu); - menuitems->insert(item, pos); - submenu->parent = this; - return menuitems->count(); -} - - -int Basemenu::insert(const char **ulabel, int pos, int function) { - BasemenuItem *item = new BasemenuItem(ulabel, function); - menuitems->insert(item, pos); - - return menuitems->count(); + return insert(item, pos); } int Basemenu::remove(int index) { - if (index < 0 || index > menuitems->count()) return -1; - - BasemenuItem *item = menuitems->remove(index); - - if (item) { - if ((! internal_menu) && (item->submenu())) { - Basemenu *tmp = (Basemenu *) item->submenu(); + BasemenuItem *item = find(index); + if (! item) return -1; + if (! internal_menu) { + Basemenu *tmp = (Basemenu *) item->submenu(); + if (tmp) { if (! tmp->internal_menu) { - delete tmp; + delete tmp; } else { - tmp->internal_hide(); + tmp->internal_hide(); } } - - if (item->label()) - delete [] item->label(); - - if (item->exec()) - delete [] item->exec(); - - delete item; } + delete item; + if (which_sub == index) which_sub = -1; else if (which_sub > index) which_sub--; - return menuitems->count(); + menuitems.erase(menuitems.begin() + index); + + return menuitems.size(); } void Basemenu::update(void) { MenuStyle *style = screen->getMenuStyle(); - if (i18n->multibyte()) { - menu.item_h = style->f_fontset_extents->max_ink_extent.height + - menu.bevel_w; - menu.title_h = style->t_fontset_extents->max_ink_extent.height + - (menu.bevel_w * 2); - } else { - menu.item_h = style->f_font->ascent + style->f_font->descent + - menu.bevel_w; - menu.title_h = style->t_font->ascent + style->t_font->descent + - (menu.bevel_w * 2); - } - - if (title_vis) { - const char *s = (menu.label) ? menu.label : - i18n->getMessage(BasemenuSet, BasemenuOpenboxMenu, - "Openbox Menu"); - int l = strlen(s); - + menu.item_h = (style->f_font->height() < 9 ? 9 : style->f_font->height()) + + menu.bevel_w; // 9 for the menu pixmaps (checkmarks) + menu.title_h = style->t_font->height() + menu.bevel_w * 2; - if (i18n->multibyte()) { - XRectangle ink, logical; - XmbTextExtents(screen->getMenuStyle()->t_fontset, s, l, &ink, &logical); - menu.item_w = logical.width; - } else { - menu.item_w = XTextWidth(screen->getMenuStyle()->t_font, s, l); - } - - menu.item_w += (menu.bevel_w * 2); + if (title_vis) { + menu.item_w = screen->getMenuStyle()->t_font->measureString(menu.label) + + menu.bevel_w * 2; } else { menu.item_w = 1; } - int ii = 0; - LinkedListIterator it(menuitems); - for (BasemenuItem *tmp = it.current(); tmp; it++, tmp = it.current()) { - const char *s = ((tmp->u && *tmp->u) ? *tmp->u : - ((tmp->l) ? tmp->l : (const char *) 0)); - int l = strlen(s); - - if (i18n->multibyte()) { - XRectangle ink, logical; - XmbTextExtents(screen->getMenuStyle()->f_fontset, s, l, &ink, &logical); - ii = logical.width; - } else - ii = XTextWidth(screen->getMenuStyle()->f_font, s, l); - - ii += (menu.bevel_w * 2) + (menu.item_h * 2); + unsigned int ii = 0; + MenuItems::iterator it = menuitems.begin(), end = menuitems.end(); + for (; it != end; ++it) { + ii = screen->getMenuStyle()->f_font->measureString((*it)->l) + + (menu.bevel_w * 2) + (menu.item_h * 2); - menu.item_w = ((menu.item_w < (unsigned int) ii) ? ii : menu.item_w); + menu.item_w = ((menu.item_w < ii) ? ii : menu.item_w); } - if (menuitems->count()) { + if (! menuitems.empty()) { menu.sublevels = 1; - while (((menu.item_h * (menuitems->count() + 1) / menu.sublevels) - + menu.title_h + screen->getBorderWidth()) > - screen->getHeight()) + unsigned int menu_size = menuitems.size(); + while (((menu.item_h * (menu_size + 1) / menu.sublevels) + + menu.title_h + screen->getBorderWidth()) > + screen->getHeight()) menu.sublevels++; if (menu.sublevels < menu.minsub) menu.sublevels = menu.minsub; - menu.persub = menuitems->count() / menu.sublevels; - if (menuitems->count() % menu.sublevels) menu.persub++; + menu.persub = menu_size / menu.sublevels; + if (menu_size % menu.sublevels) menu.persub++; } else { menu.sublevels = 0; menu.persub = 0; @@ -335,7 +293,7 @@ void Basemenu::update(void) { menu.frame_h = (menu.item_h * menu.persub); menu.height = ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) + - menu.frame_h; + menu.frame_h; if (! menu.frame_h) menu.frame_h = 1; if (menu.height < 1) menu.height = 1; @@ -344,13 +302,13 @@ void Basemenu::update(void) { if (title_vis) { tmp = menu.title_pixmap; texture = &(screen->getMenuStyle()->title); - if (texture->getTexture() == (BImage_Flat | BImage_Solid)) { + if (texture->texture() == (BTexture::Flat | BTexture::Solid)) { menu.title_pixmap = None; XSetWindowBackground(display, menu.title, - texture->getColor()->getPixel()); + texture->color().pixel()); } else { menu.title_pixmap = - image_ctrl->renderImage(menu.width, menu.title_h, texture); + image_ctrl->renderImage(menu.width, menu.title_h, *texture); XSetWindowBackgroundPixmap(display, menu.title, menu.title_pixmap); } if (tmp) image_ctrl->removeImage(tmp); @@ -359,34 +317,24 @@ void Basemenu::update(void) { tmp = menu.frame_pixmap; texture = &(screen->getMenuStyle()->frame); - if (texture->getTexture() == (BImage_Flat | BImage_Solid)) { + if (texture->texture() == (BTexture::Flat | BTexture::Solid)) { menu.frame_pixmap = None; XSetWindowBackground(display, menu.frame, - texture->getColor()->getPixel()); + texture->color().pixel()); } else { menu.frame_pixmap = - image_ctrl->renderImage(menu.width, menu.frame_h, texture); + image_ctrl->renderImage(menu.width, menu.frame_h, *texture); XSetWindowBackgroundPixmap(display, menu.frame, menu.frame_pixmap); } if (tmp) image_ctrl->removeImage(tmp); tmp = menu.hilite_pixmap; texture = &(screen->getMenuStyle()->hilite); - if (texture->getTexture() == (BImage_Flat | BImage_Solid)) { + if (texture->texture() == (BTexture::Flat | BTexture::Solid)) { menu.hilite_pixmap = None; } else { menu.hilite_pixmap = - image_ctrl->renderImage(menu.item_w, menu.item_h, texture); - } - if (tmp) image_ctrl->removeImage(tmp); - - tmp = menu.sel_pixmap; - if (texture->getTexture() == (BImage_Flat | BImage_Solid)) { - menu.sel_pixmap = None; - } else { - int hw = menu.item_h / 2; - menu.sel_pixmap = - image_ctrl->renderImage(hw, hw, texture); + image_ctrl->renderImage(menu.item_w, menu.item_h, *texture); } if (tmp) image_ctrl->removeImage(tmp); @@ -396,9 +344,9 @@ void Basemenu::update(void) { XResizeWindow(display, menu.title, menu.width, menu.title_h); XMoveResizeWindow(display, menu.frame, 0, - ((title_vis) ? menu.title_h + - screen->getBorderWidth() : 0), menu.width, - menu.frame_h); + ((title_vis) ? menu.title_h + + screen->getBorderWidth() : 0), menu.width, + menu.frame_h); XClearWindow(display, menu.window); XClearWindow(display, menu.title); @@ -406,7 +354,8 @@ void Basemenu::update(void) { if (title_vis && visible) redrawTitle(); - for (int i = 0; visible && i < menuitems->count(); i++) { + const int menu_size = menuitems.size(); + for (int i = 0; visible && i < menu_size; i++) { if (i == which_sub) { drawItem(i, True, 0); drawSubmenu(i); @@ -429,7 +378,7 @@ void Basemenu::show(void) { if (! parent) { if (shown && (! shown->torn)) - shown->hide(); + shown->hide(); shown = this; } @@ -437,10 +386,10 @@ void Basemenu::show(void) { void Basemenu::hide(void) { - if ((! torn) && hide_tree && parent && parent->isVisible()) { + if (! torn && hide_tree && parent && parent->isVisible()) { Basemenu *p = parent; - while (p->isVisible() && (! p->torn) && p->parent) p = p->parent; + while (p->isVisible() && ! p->torn && p->parent) p = p->parent; p->internal_hide(); } else { internal_hide(); @@ -449,12 +398,11 @@ void Basemenu::hide(void) { void Basemenu::internal_hide(void) { - if (which_sub != -1) { - BasemenuItem *tmp = menuitems->find(which_sub); + BasemenuItem *tmp = find(which_sub); + if (tmp) tmp->submenu()->internal_hide(); - } - if (parent && (! torn)) { + if (parent && ! torn) { parent->drawItem(parent->which_sub, False, True); parent->which_sub = -1; @@ -479,184 +427,162 @@ void Basemenu::move(int x, int y) { void Basemenu::redrawTitle(void) { - char *text = (char *) ((menu.label) ? menu.label : - i18n->getMessage(BasemenuSet, BasemenuOpenboxMenu, - "Openbox Menu")); - int dx = menu.bevel_w, len = strlen(text); + const char *text = (! menu.label.empty()) ? getLabel() : + i18n(BasemenuSet, BasemenuBlackboxMenu, "Blackbox Menu"); + int dx = menu.bevel_w; unsigned int l; + MenuStyle *style = screen->getMenuStyle(); - if (i18n->multibyte()) { - XRectangle ink, logical; - XmbTextExtents(screen->getMenuStyle()->t_fontset, text, len, &ink, &logical); - l = logical.width; - } else { - l = XTextWidth(screen->getMenuStyle()->t_font, text, len); - } - - l += (menu.bevel_w * 2); + l = style->t_font->measureString(text) + menu.bevel_w * 2; switch (screen->getMenuStyle()->t_justify) { - case BScreen::RightJustify: + case RightJustify: dx += menu.width - l; break; - case BScreen::CenterJustify: + case CenterJustify: dx += (menu.width - l) / 2; break; + + case LeftJustify: + default: + break; } - MenuStyle *style = screen->getMenuStyle(); - if (i18n->multibyte()) - XmbDrawString(display, menu.title, style->t_fontset, style->t_text_gc, dx, - (menu.bevel_w - style->t_fontset_extents->max_ink_extent.y), - text, len); - else - XDrawString(display, menu.title, style->t_text_gc, dx, - (style->t_font->ascent + menu.bevel_w), text, len); + style->t_font->drawString(menu.title, dx, menu.bevel_w, + style->t_text, text); } void Basemenu::drawSubmenu(int index) { - if (which_sub != -1 && which_sub != index) { - BasemenuItem *itmp = menuitems->find(which_sub); + BasemenuItem *item = find(which_sub); + if (item && item->submenu() && ! item->submenu()->isTorn() && + which_sub != index) + item->submenu()->internal_hide(); + + item = find(index); + if (! item) + return; + Basemenu *submenu = item->submenu(); + + if (submenu && visible && ! submenu->isTorn() && item->isEnabled()) { + if (submenu->parent != this) submenu->parent = this; + int sbl = index / menu.persub, i = index - (sbl * menu.persub), + x = menu.x + ((menu.item_w * (sbl + 1)) + screen->getBorderWidth()), y; + + if (alignment == AlignTop) { + y = (((shifted) ? menu.y_shift : menu.y) + + ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) - + ((submenu->title_vis) ? + submenu->menu.title_h + screen->getBorderWidth() : 0)); + } else { + y = (((shifted) ? menu.y_shift : menu.y) + + (menu.item_h * i) + + ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) - + ((submenu->title_vis) ? + submenu->menu.title_h + screen->getBorderWidth() : 0)); + } - if (! itmp->submenu()->isTorn()) - itmp->submenu()->internal_hide(); - } + if (alignment == AlignBottom && + (y + submenu->menu.height) > ((shifted) ? menu.y_shift : + menu.y) + menu.height) + y = (((shifted) ? menu.y_shift : menu.y) + + menu.height - submenu->menu.height); - if (index >= 0 && index < menuitems->count()) { - BasemenuItem *item = menuitems->find(index); - if (item->submenu() && visible && (! item->submenu()->isTorn()) && - item->isEnabled()) { - if (item->submenu()->parent != this) item->submenu()->parent = this; - int sbl = index / menu.persub, i = index - (sbl * menu.persub), - x = menu.x + - ((menu.item_w * (sbl + 1)) + screen->getBorderWidth()), y; - - if (alignment == AlignTop) - y = (((shifted) ? menu.y_shift : menu.y) + - ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) - - ((item->submenu()->title_vis) ? - item->submenu()->menu.title_h + screen->getBorderWidth() : 0)); - else - y = (((shifted) ? menu.y_shift : menu.y) + - (menu.item_h * i) + - ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) - - ((item->submenu()->title_vis) ? - item->submenu()->menu.title_h + screen->getBorderWidth() : 0)); - - if (alignment == AlignBottom && - (y + item->submenu()->menu.height) > ((shifted) ? menu.y_shift : - menu.y) + menu.height) - y = (((shifted) ? menu.y_shift : menu.y) + - menu.height - item->submenu()->menu.height); - - if ((x + item->submenu()->getWidth()) > screen->getWidth()) { - x = ((shifted) ? menu.x_shift : menu.x) - - item->submenu()->getWidth() - screen->getBorderWidth(); - } - - if (x < 0) x = 0; + if ((x + submenu->getWidth()) > screen->getWidth()) + x = ((shifted) ? menu.x_shift : menu.x) - + submenu->getWidth() - screen->getBorderWidth(); - if ((y + item->submenu()->getHeight()) > screen->getHeight()) - y = screen->getHeight() - item->submenu()->getHeight() - - (screen->getBorderWidth() * 2); - if (y < 0) y = 0; - - item->submenu()->move(x, y); - if (! moving) drawItem(index, True); - - if (! item->submenu()->isVisible()) - item->submenu()->show(); - item->submenu()->moving = moving; - which_sub = index; - } else { - which_sub = -1; - } + if (x < 0) x = 0; + + if ((y + submenu->getHeight()) > screen->getHeight()) + y = screen->getHeight() - submenu->getHeight() - + (screen->getBorderWidth() * 2); + if (y < 0) y = 0; + + submenu->move(x, y); + if (! moving) drawItem(index, True); + + if (! submenu->isVisible()) + submenu->show(); + submenu->moving = moving; + which_sub = index; + } else { + which_sub = -1; } } -Bool Basemenu::hasSubmenu(int index) { - if ((index >= 0) && (index < menuitems->count())) - if (menuitems->find(index)->submenu()) - return True; - +bool Basemenu::hasSubmenu(int index) { + BasemenuItem *item = find(index); + if (item && item->submenu()) + return True; return False; } -void Basemenu::drawItem(int index, Bool highlight, Bool clear, - int x, int y, unsigned int w, unsigned int h) -{ - if (index < 0 || index > menuitems->count()) return; - - BasemenuItem *item = menuitems->find(index); +void Basemenu::drawItem(int index, bool highlight, bool clear, + int x, int y, unsigned int w, unsigned int h) { + BasemenuItem *item = find(index); if (! item) return; - - Bool dotext = True, dohilite = True, dosel = True; - const char *text = (item->ulabel()) ? *item->ulabel() : item->label(); + + bool dotext = True, dohilite = True, dosel = True, dooppsel = True; + const char *text = item->label(); int sbl = index / menu.persub, i = index - (sbl * menu.persub); int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h); int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0; - int text_x = 0, text_y = 0, len = strlen(text), sel_x = 0, sel_y = 0; + int text_x = 0, text_y = 0, sel_x = 0, oppsel_x = 0, sel_y = 0; unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0, text_h = 0; unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4; - + if (text) { - if (i18n->multibyte()) { - XRectangle ink, logical; - XmbTextExtents(screen->getMenuStyle()->f_fontset, - text, len, &ink, &logical); - text_w = logical.width; - text_y = item_y + (menu.bevel_w / 2) - - screen->getMenuStyle()->f_fontset_extents->max_ink_extent.y; - } else { - text_w = XTextWidth(screen->getMenuStyle()->f_font, text, len); - text_y = item_y + - screen->getMenuStyle()->f_font->ascent + - (menu.bevel_w / 2); - } - + text_w = screen->getMenuStyle()->f_font->measureString(text); + text_y = item_y + menu.bevel_w / 2; + switch(screen->getMenuStyle()->f_justify) { - case BScreen::LeftJustify: + case LeftJustify: text_x = item_x + menu.bevel_w + menu.item_h + 1; break; - - case BScreen::RightJustify: + + case RightJustify: text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w); break; - - case BScreen::CenterJustify: + + case CenterJustify: text_x = item_x + ((menu.item_w + 1 - text_w) / 2); break; } - + text_h = menu.item_h - menu.bevel_w; } - - GC gc = - ((highlight || item->isSelected()) ? screen->getMenuStyle()->h_text_gc : - screen->getMenuStyle()->f_text_gc), - tgc = - ((highlight) ? screen->getMenuStyle()->h_text_gc : - ((item->isEnabled()) ? screen->getMenuStyle()->f_text_gc : - screen->getMenuStyle()->d_text_gc)); - + + MenuStyle *style = screen->getMenuStyle(); + BPen hipen(style->hilite.color()); + // match the text color + BPen pen((highlight ? style->h_text : + (item->isEnabled() ? style->f_text : + style->d_text))); + + sel_x = item_x; if (screen->getMenuStyle()->bullet_pos == Right) sel_x += (menu.item_w - menu.item_h - menu.bevel_w); sel_x += quarter_w; + oppsel_x = sel_x; + if (screen->getMenuStyle()->bullet_pos == Right) + oppsel_x -= (menu.item_w - menu.item_h - menu.bevel_w); + else + oppsel_x += (menu.item_w - menu.item_h - menu.bevel_w); sel_y = item_y + quarter_w; - + if (clear) { XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h, - False); + False); } else if (! (x == y && y == -1 && w == h && h == 0)) { // calculate the which part of the hilite to redraw - if (! (max(item_x, x) <= (signed) min(item_x + menu.item_w, x + w) && - max(item_y, y) <= (signed) min(item_y + menu.item_h, y + h))) { + if (! (max(item_x, x) <= min(item_x + menu.item_w, x + w) && + max(item_y, y) <= min(item_y + menu.item_h, y + h))) { dohilite = False; } else { hilite_x = max(item_x, x); @@ -666,90 +592,114 @@ void Basemenu::drawItem(int index, Bool highlight, Bool clear, hoff_x = hilite_x % menu.item_w; hoff_y = hilite_y % menu.item_h; } - - // check if we need to redraw the text + + // check if we need to redraw the text int text_ry = item_y + (menu.bevel_w / 2); - if (! (max(text_x, x) <= (signed) min(text_x + text_w, x + w) && - max(text_ry, y) <= (signed) min(text_ry + text_h, y + h))) + if (! (max(text_x, x) <= min(text_x + text_w, x + w) && + max(text_ry, y) <= min(text_ry + text_h, y + h))) dotext = False; - + // check if we need to redraw the select pixmap/menu bullet - if (! (max(sel_x, x) <= (signed) min(sel_x + half_w, x + w) && - max(sel_y, y) <= (signed) min(sel_y + half_w, y + h))) + if (! (max(sel_x, x) <= min(sel_x + half_w, x + w) && + max(sel_y, y) <= min(sel_y + half_w, y + h))) dosel = False; + + // check if we need to redraw the select pixmap/menu bullet + // on the opposite side of the menu + if (! (max(oppsel_x, x) <= min(oppsel_x + half_w, x + w) && + max(sel_y, y) <= min(sel_y + half_w, y + h))) + dooppsel = False; } - + if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) { if (menu.hilite_pixmap) XCopyArea(display, menu.hilite_pixmap, menu.frame, - screen->getMenuStyle()->hilite_gc, hoff_x, hoff_y, - hilite_w, hilite_h, hilite_x, hilite_y); - else - XFillRectangle(display, menu.frame, - screen->getMenuStyle()->hilite_gc, - hilite_x, hilite_y, hilite_w, hilite_h); - } else if (dosel && item->isSelected() && - (menu.sel_pixmap != ParentRelative)) { - if (menu.sel_pixmap) - XCopyArea(display, menu.sel_pixmap, menu.frame, - screen->getMenuStyle()->hilite_gc, 0, 0, - half_w, half_w, sel_x, sel_y); + hipen.gc(), hoff_x, hoff_y, + hilite_w, hilite_h, hilite_x, hilite_y); else - XFillRectangle(display, menu.frame, - screen->getMenuStyle()->hilite_gc, - sel_x, sel_y, half_w, half_w); + XFillRectangle(display, menu.frame, hipen.gc(), + hilite_x, hilite_y, hilite_w, hilite_h); } + if (dooppsel && item->isSelected()) { + XPoint pts[6]; + + pts[0].x = oppsel_x + 0; + pts[0].y = sel_y + 2; + + pts[1].x = 0; + pts[1].y = 3; + + pts[2].x = 2; + pts[2].y = 3; + + pts[3].x = 5; + pts[3].y = -5; + + + pts[4].x = 0; + pts[4].y = -3; + + pts[5].x = -5; + pts[5].y = 5; + + XFillPolygon(display, menu.frame, pen.gc(), pts, 6, Nonconvex, + CoordModePrevious); + } + if (dotext && text) { - if (i18n->multibyte()) - XmbDrawString(display, menu.frame, screen->getMenuStyle()->f_fontset, - tgc, text_x, text_y, text, len); - else - XDrawString(display, menu.frame, tgc, text_x, text_y, text, len); + style->f_font->drawString(menu.frame, text_x, text_y, + (highlight ? style->h_text : + (item->isEnabled() ? style->f_text : + style->d_text)), + text); } if (dosel && item->submenu()) { + const int bullet_size = 3; + switch (screen->getMenuStyle()->bullet) { case Square: - XDrawRectangle(display, menu.frame, gc, sel_x, sel_y, half_w, half_w); + XDrawRectangle(display, menu.frame, pen.gc(), sel_x, sel_y, + bullet_size * 2, bullet_size * 2); break; case Triangle: XPoint tri[3]; if (screen->getMenuStyle()->bullet_pos == Right) { - tri[0].x = sel_x + quarter_w - 2; - tri[0].y = sel_y + quarter_w - 2; - tri[1].x = 4; - tri[1].y = 2; - tri[2].x = -4; - tri[2].y = 2; + tri[0].x = sel_x + quarter_w - bullet_size; + tri[0].y = sel_y + quarter_w - bullet_size; + tri[1].x = 2 * bullet_size; + tri[1].y = bullet_size; + tri[2].x = -(2 * bullet_size); + tri[2].y = bullet_size; } else { - tri[0].x = sel_x + quarter_w - 2; - tri[0].y = item_y + half_w; - tri[1].x = 4; - tri[1].y = 2; + tri[0].x = sel_x + quarter_w - bullet_size; + tri[0].y = item_y + half_w; + tri[1].x = 2 * bullet_size; + tri[1].y = bullet_size; tri[2].x = 0; - tri[2].y = -4; + tri[2].y = -(2 * bullet_size); } - - XFillPolygon(display, menu.frame, gc, tri, 3, Convex, + + XFillPolygon(display, menu.frame, pen.gc(), tri, 3, Convex, CoordModePrevious); break; - + case Diamond: XPoint dia[4]; - dia[0].x = sel_x + quarter_w - 3; + dia[0].x = sel_x + quarter_w - bullet_size; dia[0].y = item_y + half_w; - dia[1].x = 3; - dia[1].y = -3; - dia[2].x = 3; - dia[2].y = 3; - dia[3].x = -3; - dia[3].y = 3; - - XFillPolygon(display, menu.frame, gc, dia, 4, Convex, + dia[1].x = bullet_size; + dia[1].y = -bullet_size; + dia[2].x = bullet_size; + dia[2].y = bullet_size; + dia[3].x = -bullet_size; + dia[3].y = bullet_size; + + XFillPolygon(display, menu.frame, pen.gc(), dia, 4, Convex, CoordModePrevious); break; } @@ -757,18 +707,13 @@ void Basemenu::drawItem(int index, Bool highlight, Bool clear, } -void Basemenu::setLabel(const char *l) { - if (menu.label) - delete [] menu.label; - - if (l) menu.label = bstrdup(l); - else menu.label = 0; +void Basemenu::setLabel(const string& label) { + menu.label = label; } -void Basemenu::setItemSelected(int index, Bool sel) { - if (index < 0 || index >= menuitems->count()) return; - +void Basemenu::setItemSelected(int index, bool sel) { + assert(index >= 0); BasemenuItem *item = find(index); if (! item) return; @@ -777,9 +722,8 @@ void Basemenu::setItemSelected(int index, Bool sel) { } -Bool Basemenu::isItemSelected(int index) { - if (index < 0 || index >= menuitems->count()) return False; - +bool Basemenu::isItemSelected(int index) { + assert(index >= 0); BasemenuItem *item = find(index); if (! item) return False; @@ -787,9 +731,8 @@ Bool Basemenu::isItemSelected(int index) { } -void Basemenu::setItemEnabled(int index, Bool enable) { - if (index < 0 || index >= menuitems->count()) return; - +void Basemenu::setItemEnabled(int index, bool enable) { + assert(index >= 0); BasemenuItem *item = find(index); if (! item) return; @@ -798,9 +741,8 @@ void Basemenu::setItemEnabled(int index, Bool enable) { } -Bool Basemenu::isItemEnabled(int index) { - if (index < 0 || index >= menuitems->count()) return False; - +bool Basemenu::isItemEnabled(int index) { + assert(index >= 0); BasemenuItem *item = find(index); if (! item) return False; @@ -813,16 +755,16 @@ void Basemenu::buttonPressEvent(XButtonEvent *be) { int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h); int w = (sbl * menu.persub) + i; - if (w < menuitems->count() && w >= 0) { + BasemenuItem *item = find(w); + if (item) { which_press = i; which_sbl = sbl; - BasemenuItem *item = menuitems->find(w); if (item->submenu()) - drawSubmenu(w); + drawSubmenu(w); else - drawItem(w, (item->isEnabled()), True); + drawItem(w, (item->isEnabled()), True); } } else { menu.x_move = be->x_root - menu.x; @@ -835,37 +777,38 @@ void Basemenu::buttonReleaseEvent(XButtonEvent *re) { if (re->window == menu.title) { if (moving) { moving = False; - + if (which_sub != -1) - drawSubmenu(which_sub); + drawSubmenu(which_sub); } - - if (re->x >= 0 && re->x <= (signed) menu.width && - re->y >= 0 && re->y <= (signed) menu.title_h) + + if (re->x >= 0 && re->x <= static_cast(menu.width) && + re->y >= 0 && re->y <= static_cast(menu.title_h)) if (re->button == 3) - hide(); + hide(); } else if (re->window == menu.frame && - re->x >= 0 && re->x < (signed) menu.width && - re->y >= 0 && re->y < (signed) menu.frame_h) { + re->x >= 0 && re->x < static_cast(menu.width) && + re->y >= 0 && re->y < static_cast(menu.frame_h)) { if (re->button == 3) { hide(); } else { int sbl = (re->x / menu.item_w), i = (re->y / menu.item_h), - ix = sbl * menu.item_w, iy = i * menu.item_h, - w = (sbl * menu.persub) + i, - p = (which_sbl * menu.persub) + which_press; + ix = sbl * menu.item_w, iy = i * menu.item_h, + w = (sbl * menu.persub) + i, + p = (which_sbl * menu.persub) + which_press; - if (w < menuitems->count() && w >= 0) { - drawItem(p, (p == which_sub), True); + if (w >= 0 && w < static_cast(menuitems.size())) { + drawItem(p, (p == which_sub), True); if (p == w && isItemEnabled(w)) { - if (re->x > ix && re->x < (signed) (ix + menu.item_w) && - re->y > iy && re->y < (signed) (iy + menu.item_h)) { - itemSelected(re->button, w); - } + if (re->x > ix && re->x < static_cast(ix + menu.item_w) && + re->y > iy && re->y < static_cast(iy + menu.item_h)) { + itemSelected(re->button, w); + } } - } else + } else { drawItem(p, False, True); + } } } } @@ -875,55 +818,55 @@ void Basemenu::motionNotifyEvent(XMotionEvent *me) { if (me->window == menu.title && (me->state & Button1Mask)) { if (movable) { if (! moving) { - if (parent && (! torn)) { - parent->drawItem(parent->which_sub, False, True); - parent->which_sub = -1; - } + if (parent && ! torn) { + parent->drawItem(parent->which_sub, False, True); + parent->which_sub = -1; + } moving = torn = True; - if (which_sub != -1) - drawSubmenu(which_sub); + if (which_sub != -1) + drawSubmenu(which_sub); } else { - menu.x = me->x_root - menu.x_move, - menu.y = me->y_root - menu.y_move; - - XMoveWindow(display, menu.window, menu.x, menu.y); - - if (which_sub != -1) - drawSubmenu(which_sub); + menu.x = me->x_root - menu.x_move, + menu.y = me->y_root - menu.y_move; + + XMoveWindow(display, menu.window, menu.x, menu.y); + + if (which_sub != -1) + drawSubmenu(which_sub); } } - } else if ((! (me->state & Button1Mask)) && me->window == menu.frame && - me->x >= 0 && me->x < (signed) menu.width && - me->y >= 0 && me->y < (signed) menu.frame_h) { + } else if (! (me->state & Button1Mask) && me->window == menu.frame && + me->x >= 0 && me->x < static_cast(menu.width) && + me->y >= 0 && me->y < static_cast(menu.frame_h)) { int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h), - w = (sbl * menu.persub) + i; + w = (sbl * menu.persub) + i; if ((i != which_press || sbl != which_sbl) && - (w < menuitems->count() && w >= 0)) { + (w >= 0 && w < static_cast(menuitems.size()))) { if (which_press != -1 && which_sbl != -1) { - int p = (which_sbl * menu.persub) + which_press; - BasemenuItem *item = menuitems->find(p); - - drawItem(p, False, True); - if (item->submenu()) - if (item->submenu()->isVisible() && - (! item->submenu()->isTorn())) { - item->submenu()->internal_hide(); - which_sub = -1; - } + int p = (which_sbl * menu.persub) + which_press; + BasemenuItem *item = find(p); + + drawItem(p, False, True); + if (item->submenu()) + if (item->submenu()->isVisible() && + ! item->submenu()->isTorn()) { + item->submenu()->internal_hide(); + which_sub = -1; + } } which_press = i; which_sbl = sbl; - BasemenuItem *itmp = menuitems->find(w); + BasemenuItem *itmp = find(w); if (itmp->submenu()) - drawSubmenu(w); + drawSubmenu(w); else - drawItem(w, (itmp->isEnabled()), True); + drawItem(w, (itmp->isEnabled()), True); } } } @@ -946,16 +889,17 @@ void Basemenu::exposeEvent(XExposeEvent *ee) { if (id_d > menu.persub) id_d = menu.persub; // draw the sublevels and the number of items the exposure spans - LinkedListIterator it(menuitems); + MenuItems::iterator it, + end = menuitems.end(); int i, ii; for (i = sbl; i <= sbl_d; i++) { // set the iterator to the first item in the sublevel needing redrawing - it.set(id + (i * menu.persub)); - for (ii = id; ii <= id_d && it.current(); it++, ii++) { - int index = ii + (i * menu.persub); - // redraw the item - drawItem(index, (which_sub == index), False, - ee->x, ee->y, ee->width, ee->height); + it = menuitems.begin() + (id + (i * menu.persub)); + for (ii = id; ii <= id_d && it != end; ++it, ii++) { + int index = ii + (i * menu.persub); + // redraw the item + drawItem(index, (which_sub == index), False, + ee->x, ee->y, ee->width, ee->height); } } } @@ -978,7 +922,7 @@ void Basemenu::enterNotifyEvent(XCrossingEvent *ce) { menu.y_shift = screen->getHeight() - menu.height - screen->getBorderWidth(); shifted = True; - } else if (menu.y + (signed) menu.title_h < 0) { + } else if (menu.y + static_cast(menu.title_h) < 0) { menu.y_shift = -screen->getBorderWidth(); shifted = True; } @@ -987,17 +931,17 @@ void Basemenu::enterNotifyEvent(XCrossingEvent *ce) { XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift); if (which_sub != -1) { - BasemenuItem *tmp = menuitems->find(which_sub); + BasemenuItem *tmp = find(which_sub); if (tmp->submenu()->isVisible()) { - int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h), - w = (sbl * menu.persub) + i; + int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h), + w = (sbl * menu.persub) + i; - if (w != which_sub && (! tmp->submenu()->isTorn())) { - tmp->submenu()->internal_hide(); + if (w != which_sub && ! tmp->submenu()->isTorn()) { + tmp->submenu()->internal_hide(); - drawItem(which_sub, False, True); - which_sub = -1; - } + drawItem(which_sub, False, True); + which_sub = -1; + } } } } @@ -1006,7 +950,7 @@ void Basemenu::enterNotifyEvent(XCrossingEvent *ce) { void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) { if (ce->window == menu.frame) { - if (which_press != -1 && which_sbl != -1 && menuitems->count() > 0) { + if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) { int p = (which_sbl * menu.persub) + which_press; drawItem(p, (p == which_sub), True); @@ -1026,11 +970,18 @@ void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) { void Basemenu::reconfigure(void) { XSetWindowBackground(display, menu.window, - screen->getBorderColor()->getPixel()); + screen->getBorderColor()->pixel()); XSetWindowBorder(display, menu.window, - screen->getBorderColor()->getPixel()); + screen->getBorderColor()->pixel()); XSetWindowBorderWidth(display, menu.window, screen->getBorderWidth()); menu.bevel_w = screen->getBevelWidth(); update(); } + + +void Basemenu::changeItemLabel(unsigned int index, const string& label) { + BasemenuItem *item = find(index); + if (item) + item->newLabel(label); +}