]> icculus.org git repositories - mikachu/openbox.git/blob - src/Basemenu.cc
Fix XFT redraw of text. Still needs slight work for the shadow in Basemenu
[mikachu/openbox.git] / src / Basemenu.cc
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)
5 //
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:
12 //
13 // The above copyright notice and this permission notice shall be included in
14 // all copies or substantial portions of the Software.
15 //
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.
23
24 #ifdef    HAVE_CONFIG_H
25 #  include "../config.h"
26 #endif // HAVE_CONFIG_H
27
28 extern "C" {
29 #ifdef    HAVE_STDIO_H
30 #  include <stdio.h>
31 #endif // HAVE_STDIO_H
32
33 #ifdef HAVE_STDLIB_H
34 #  include <stdlib.h>
35 #endif // HAVE_STDLIB_H
36
37 #ifdef HAVE_STRING_H
38 #  include <string.h>
39 #endif // HAVE_STRING_H
40 }
41
42 #include <algorithm>
43 #include <assert.h>
44 using namespace std;
45
46 #include "i18n.hh"
47 #include "blackbox.hh"
48 #include "Basemenu.hh"
49 #include "Font.hh"
50 #include "GCCache.hh"
51 #include "Image.hh"
52 #include "Screen.hh"
53 #include "Util.hh"
54
55
56 static Basemenu *shown = (Basemenu *) 0;
57
58 Basemenu::Basemenu(BScreen *scrn) {
59   screen = scrn;
60   blackbox = screen->getBlackbox();
61   image_ctrl = screen->getImageControl();
62   display = blackbox->getXDisplay();
63   parent = (Basemenu *) 0;
64   alignment = AlignDontCare;
65
66   title_vis =
67     movable =
68     hide_tree = True;
69
70   shifted =
71     internal_menu =
72     moving =
73     torn =
74     visible = False;
75
76   menu.x =
77     menu.y =
78     menu.x_shift =
79     menu.y_shift =
80     menu.x_move =
81     menu.y_move = 0;
82
83   which_sub =
84     which_press =
85     which_sbl = -1;
86
87   menu.sublevels =
88     menu.persub =
89     menu.minsub = 0;
90
91   menu.frame_pixmap =
92     menu.title_pixmap =
93     menu.hilite_pixmap = None;
94
95   menu.bevel_w = screen->getBevelWidth();
96
97   const MenuStyle* const style = screen->getMenuStyle();
98   menu.width = menu.title_h = menu.item_w = menu.frame_h =
99     style->t_font->height() + (menu.bevel_w  * 2);
100
101   menu.item_h = style->f_font->height() + menu.bevel_w;
102
103   menu.height = menu.title_h + screen->getBorderWidth() + menu.frame_h;
104
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;
115
116   menu.window =
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);
122
123   attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel | CWEventMask;
124   attrib.background_pixel = screen->getBorderColor()->pixel();
125   attrib.event_mask |= EnterWindowMask | LeaveWindowMask;
126
127   menu.title =
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);
132
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);
140
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
143 }
144
145
146 Basemenu::~Basemenu(void) {
147   XUnmapWindow(display, menu.window);
148
149   if (shown && shown->getWindowID() == getWindowID())
150     shown = (Basemenu *) 0;
151
152   MenuItems::const_iterator it = menuitems.begin(),
153     end = menuitems.end();
154   for (; it != end; ++it) {
155     if (! internal_menu) {
156       Basemenu *tmp = (*it)->submenu();
157       if (tmp) {
158         if (! tmp->internal_menu)
159           delete tmp;
160         else
161           tmp->internal_hide();
162       }
163     }
164   }
165
166   std::for_each(menuitems.begin(), menuitems.end(), PointerAssassin());
167
168   if (menu.title_pixmap)
169     image_ctrl->removeImage(menu.title_pixmap);
170
171   if (menu.frame_pixmap)
172     image_ctrl->removeImage(menu.frame_pixmap);
173
174   if (menu.hilite_pixmap)
175     image_ctrl->removeImage(menu.hilite_pixmap);
176
177   blackbox->removeMenuSearch(menu.title);
178   XDestroyWindow(display, menu.title);
179
180   blackbox->removeMenuSearch(menu.frame);
181   XDestroyWindow(display, menu.frame);
182
183   blackbox->removeMenuSearch(menu.window);
184   XDestroyWindow(display, menu.window);
185 }
186
187
188 BasemenuItem *Basemenu::find(int index) {
189   if (index < 0 || index >= static_cast<signed>(menuitems.size()))
190     return (BasemenuItem*) 0;
191
192   return menuitems[index];
193 }
194
195
196 int Basemenu::insert(BasemenuItem *item, int pos) {
197   if (pos < 0) {
198     menuitems.push_back(item);
199   } else {
200     assert(pos <= static_cast<signed>(menuitems.size()));
201     menuitems.insert((menuitems.begin() + pos), item);
202   }
203   return menuitems.size();
204 }
205
206
207 int Basemenu::insert(const string& label, int function,
208                      const string& exec, int pos) {
209   BasemenuItem *item = new BasemenuItem(label, function, exec);
210   return insert(item, pos);
211 }
212
213
214 int Basemenu::insert(const string& label, Basemenu *submenu, int pos) {
215   BasemenuItem *item = new BasemenuItem(label, submenu);
216   submenu->parent = this;
217
218   return insert(item, pos);
219 }
220
221
222 int Basemenu::remove(int index) {
223   BasemenuItem *item = find(index);
224   if (! item) return -1;
225
226   if (! internal_menu) {
227     Basemenu *tmp = item->submenu();
228     if (tmp) {
229       if (! tmp->internal_menu)
230         delete tmp;
231       else
232         tmp->internal_hide();
233     }
234   }
235
236   delete item;
237
238   if (which_sub == index)
239     which_sub = -1;
240   else if (which_sub > index)
241     which_sub--;
242
243   menuitems.erase(menuitems.begin() + index);
244
245   return menuitems.size();
246 }
247
248
249 void Basemenu::update(void) {
250   const MenuStyle* const style = screen->getMenuStyle();
251   menu.item_h = (style->f_font->height() < 9 ? 9 : style->f_font->height()) +
252                 menu.bevel_w;  // 9 for the menu pixmaps (checkmarks)
253   menu.title_h = style->t_font->height() + menu.bevel_w * 2;
254
255   if (title_vis) {
256     menu.item_w = screen->getMenuStyle()->t_font->measureString(menu.label) +
257       menu.bevel_w * 2;
258   }  else {
259     menu.item_w = 1;
260   }
261
262   unsigned int ii = 0;
263   MenuItems::iterator it = menuitems.begin(), end = menuitems.end();
264   for (; it != end; ++it) {
265     ii = screen->getMenuStyle()->f_font->measureString((*it)->l) +
266       (menu.bevel_w * 2) + (menu.item_h * 2);
267
268     menu.item_w = ((menu.item_w < ii) ? ii : menu.item_w);
269   }
270
271   if (! menuitems.empty()) {
272     menu.sublevels = 1;
273
274     const unsigned int menu_size = menuitems.size();
275     while (((menu.item_h * (menu_size + 1) / menu.sublevels)
276             + menu.title_h + screen->getBorderWidth()) >
277            screen->getHeight())
278       menu.sublevels++;
279
280     if (menu.sublevels < menu.minsub) menu.sublevels = menu.minsub;
281
282     menu.persub = menu_size / menu.sublevels;
283     if (menu_size % menu.sublevels) menu.persub++;
284   } else {
285     menu.sublevels = 0;
286     menu.persub = 0;
287   }
288
289   menu.width = (menu.sublevels * (menu.item_w));
290   if (! menu.width) menu.width = menu.item_w;
291
292   menu.frame_h = (menu.item_h * menu.persub);
293   menu.height = ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) +
294     menu.frame_h;
295   if (! menu.frame_h) menu.frame_h = 1;
296   if (menu.height < 1) menu.height = 1;
297
298   Pixmap tmp;
299   BTexture *texture;
300   if (title_vis) {
301     tmp = menu.title_pixmap;
302     texture = &(screen->getMenuStyle()->title);
303     if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
304       menu.title_pixmap = None;
305       XSetWindowBackground(display, menu.title,
306                            texture->color().pixel());
307     } else {
308       menu.title_pixmap =
309         image_ctrl->renderImage(menu.width, menu.title_h, *texture);
310       XSetWindowBackgroundPixmap(display, menu.title, menu.title_pixmap);
311     }
312     if (tmp) image_ctrl->removeImage(tmp);
313     XClearWindow(display, menu.title);
314   }
315
316   tmp = menu.frame_pixmap;
317   texture = &(screen->getMenuStyle()->frame);
318   if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
319     menu.frame_pixmap = None;
320     XSetWindowBackground(display, menu.frame,
321                          texture->color().pixel());
322   } else {
323     menu.frame_pixmap =
324       image_ctrl->renderImage(menu.width, menu.frame_h, *texture);
325     XSetWindowBackgroundPixmap(display, menu.frame, menu.frame_pixmap);
326   }
327   if (tmp) image_ctrl->removeImage(tmp);
328
329   tmp = menu.hilite_pixmap;
330   texture = &(screen->getMenuStyle()->hilite);
331   if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
332     menu.hilite_pixmap = None;
333   } else {
334     menu.hilite_pixmap =
335       image_ctrl->renderImage(menu.item_w, menu.item_h, *texture);
336   }
337   if (tmp) image_ctrl->removeImage(tmp);
338
339   XResizeWindow(display, menu.window, menu.width, menu.height);
340
341   if (title_vis)
342     XResizeWindow(display, menu.title, menu.width, menu.title_h);
343
344   XMoveResizeWindow(display, menu.frame, 0,
345                     ((title_vis) ? menu.title_h +
346                      screen->getBorderWidth() : 0), menu.width,
347                     menu.frame_h);
348
349   XClearWindow(display, menu.window);
350   XClearWindow(display, menu.title);
351   XClearWindow(display, menu.frame);
352
353   if (title_vis && visible) redrawTitle();
354
355   const int menu_size = menuitems.size();
356   for (int i = 0; visible && i < menu_size; i++) {
357     if (i == which_sub) {
358       drawItem(i, True, 0);
359       drawSubmenu(i);
360     } else {
361       drawItem(i, False, 0);
362     }
363   }
364
365   if (parent && visible)
366     parent->drawSubmenu(parent->which_sub);
367
368   XMapSubwindows(display, menu.window);
369 }
370
371
372 void Basemenu::show(void) {
373   XMapSubwindows(display, menu.window);
374   XMapWindow(display, menu.window);
375   visible = True;
376
377   if (! parent) {
378     if (shown && (! shown->torn))
379       shown->hide();
380
381     shown = this;
382   }
383 }
384
385
386 void Basemenu::hide(void) {
387   if (! torn && hide_tree && parent && parent->isVisible()) {
388     Basemenu *p = parent;
389
390     while (p->isVisible() && ! p->torn && p->parent) p = p->parent;
391     p->internal_hide();
392   } else {
393     internal_hide();
394   }
395 }
396
397
398 void Basemenu::internal_hide(void) {
399   BasemenuItem *tmp = find(which_sub);
400   if (tmp)
401     tmp->submenu()->internal_hide();
402
403   if (parent && ! torn) {
404     parent->drawItem(parent->which_sub, False, True);
405
406     parent->which_sub = -1;
407   } else if (shown && shown->menu.window == menu.window) {
408     shown = (Basemenu *) 0;
409   }
410
411   torn = visible = False;
412   which_sub = which_press = which_sub = -1;
413
414   XUnmapWindow(display, menu.window);
415 }
416
417
418 void Basemenu::move(int x, int y) {
419   menu.x = x;
420   menu.y = y;
421   XMoveWindow(display, menu.window, x, y);
422   if (which_sub != -1)
423     drawSubmenu(which_sub);
424 }
425
426
427 void Basemenu::redrawTitle(void) {
428   const char *text = (! menu.label.empty()) ? getLabel() :
429     i18n(BasemenuSet, BasemenuBlackboxMenu, "Blackbox Menu");
430   int dx = menu.bevel_w;
431   unsigned int l;
432   const MenuStyle* const style = screen->getMenuStyle();
433
434   l = style->t_font->measureString(text) + menu.bevel_w * 2;
435
436   switch (screen->getMenuStyle()->t_justify) {
437   case RightJustify:
438     dx += menu.width - l;
439     break;
440
441   case CenterJustify:
442     dx += (menu.width - l) / 2;
443     break;
444
445   case LeftJustify:
446   default:
447     break;
448   }
449
450   XClearWindow(display, menu.title);
451   style->t_font->drawString(menu.title, dx, menu.bevel_w,
452                             style->t_text, text);
453 }
454
455
456 void Basemenu::drawSubmenu(int index) {
457   BasemenuItem *item = find(which_sub);
458   if (item && item->submenu() && ! item->submenu()->isTorn() &&
459       which_sub != index)
460     item->submenu()->internal_hide();
461
462   item = find(index);
463   if (! item)
464     return;
465
466   Basemenu *submenu = item->submenu();
467
468   if (submenu && visible && ! submenu->isTorn() && item->isEnabled()) {
469     if (submenu->parent != this) submenu->parent = this;
470     const int sbl = index / menu.persub, i = index - (sbl * menu.persub);
471     int x = menu.x + ((menu.item_w * (sbl + 1)) + screen->getBorderWidth()), y;
472
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));
478     } else {
479       y = (((shifted) ? menu.y_shift : menu.y) +
480            (menu.item_h * i) +
481            ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) -
482            ((submenu->title_vis) ?
483             submenu->menu.title_h + screen->getBorderWidth() : 0));
484     }
485
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);
491
492     if ((x + submenu->getWidth()) > screen->getWidth())
493       x = ((shifted) ? menu.x_shift : menu.x) -
494         submenu->getWidth() - screen->getBorderWidth();
495
496     if (x < 0) x = 0;
497
498     if ((y + submenu->getHeight()) > screen->getHeight())
499       y = screen->getHeight() - submenu->getHeight() -
500         (screen->getBorderWidth() * 2);
501     if (y < 0) y = 0;
502
503     submenu->move(x, y);
504     if (! moving) drawItem(index, True);
505
506     if (! submenu->isVisible())
507       submenu->show();
508     submenu->moving = moving;
509     which_sub = index;
510   } else {
511     which_sub = -1;
512   }
513 }
514
515
516 bool Basemenu::hasSubmenu(int index) {
517   BasemenuItem *item = find(index);
518   if (item && item->submenu())
519     return True;
520   return False;
521 }
522
523
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);
527   if (! item) return;
528
529   bool dotext = True, dohilite = True, dosel = True, dooppsel = True;
530   const char *text = item->label();
531   const int sbl = index / menu.persub, i = index - (sbl * menu.persub);
532   const unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4;
533   int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h);
534   int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0;
535   int text_x = 0, text_y = 0, sel_x = 0, oppsel_x = 0, sel_y = 0;
536   unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0,
537     text_h = 0;
538
539   if (text) {
540     text_w = screen->getMenuStyle()->f_font->measureString(text);
541     text_y = item_y + menu.bevel_w / 2;
542
543     switch(screen->getMenuStyle()->f_justify) {
544     case LeftJustify:
545       text_x = item_x + menu.bevel_w + menu.item_h + 1;
546       break;
547
548     case RightJustify:
549       text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w);
550       break;
551
552     case CenterJustify:
553       text_x = item_x + ((menu.item_w + 1 - text_w) / 2);
554       break;
555     }
556
557     text_h = menu.item_h - menu.bevel_w;
558   }
559
560   const MenuStyle* const style = screen->getMenuStyle();
561   const BPen hipen(style->hilite.color());
562   // match the text color
563   const BPen pen((highlight ? style->h_text :
564                   (item->isEnabled() ? style->f_text :
565                    style->d_text)));
566
567
568   sel_x = item_x;
569   if (screen->getMenuStyle()->bullet_pos == Right)
570     sel_x += (menu.item_w - menu.item_h - menu.bevel_w);
571   sel_x += quarter_w;
572   oppsel_x = sel_x;
573   if (screen->getMenuStyle()->bullet_pos == Right)
574     oppsel_x -= (menu.item_w - menu.item_h - menu.bevel_w);
575   else
576     oppsel_x += (menu.item_w - menu.item_h - menu.bevel_w);
577   sel_y = item_y + quarter_w;
578
579   if (clear) {
580     XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h,
581                False);
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))) {
586       dohilite = False;
587     } else {
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;
594     }
595
596     // check if we need to redraw the text
597     const 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)))
600       dotext = False;
601
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)))
605       dosel = False;
606     
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)))
611       dooppsel = False;
612   }
613
614   if (dotext)
615     XClearArea(display, menu.frame, text_x, text_y, text_w, text_h,
616                False);
617
618   if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) {
619     if (menu.hilite_pixmap)
620       XCopyArea(display, menu.hilite_pixmap, menu.frame,
621                 hipen.gc(), hoff_x, hoff_y,
622                 hilite_w, hilite_h, hilite_x, hilite_y);
623     else
624       XFillRectangle(display, menu.frame, hipen.gc(),
625                      hilite_x, hilite_y, hilite_w, hilite_h);
626   }
627
628   if (dooppsel && item->isSelected()) {
629 #ifdef    BITMAPBUTTONS
630     if ( style->tick_image.mask != None) {
631       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
632                      oppsel_x, item_y + menu.item_h/2 - style->tick_image.h/2);
633       XSetClipMask(blackbox->getXDisplay(), pen.gc(),
634                    style->tick_image.mask);
635
636       XFillRectangle(blackbox->getXDisplay(), menu.frame, pen.gc(),
637                      oppsel_x, item_y + menu.item_h/2 - style->tick_image.h/2,
638                      style->tick_image.w,
639                      style->tick_image.h);
640
641       XSetClipMask(blackbox->getXDisplay(), pen.gc(), None);
642       
643       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
644                      0, 0);
645     } else {
646 #endif // BITMAPBUTTONS
647       XPoint pts[6];
648
649       pts[0].x = oppsel_x + 0;
650       pts[0].y = sel_y + 2;
651       
652       pts[1].x = 0;
653       pts[1].y = 3;
654       
655       pts[2].x = 2;
656       pts[2].y = 3;
657       
658       pts[3].x = 5;
659       pts[3].y = -5;
660       
661       
662       pts[4].x = 0;
663       pts[4].y = -3;
664       
665       pts[5].x = -5;
666       pts[5].y = 5;
667
668       XFillPolygon(display, menu.frame, pen.gc(), pts, 6, Nonconvex,
669                    CoordModePrevious);
670 #ifdef    BITMAPBUTTONS
671     }
672 #endif // BITMAPBUTTONS
673   }
674
675   if (dotext && text) {
676     style->f_font->drawString(menu.frame, text_x, text_y,
677                               (highlight ? style->h_text :
678                                (item->isEnabled() ? style->f_text :
679                                 style->d_text)),
680                               text);
681   }
682
683   if (dosel && item->submenu()) {
684 #ifdef    BITMAPBUTTONS
685     if ( style->bullet_image.mask != None) {
686       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
687                      sel_x, item_y + menu.item_h/2 - style->bullet_image.h/2);
688       XSetClipMask(blackbox->getXDisplay(), pen.gc(),
689                    style->bullet_image.mask);
690
691       XFillRectangle(blackbox->getXDisplay(), menu.frame, pen.gc(),
692                      sel_x, item_y + menu.item_h/2 - style->bullet_image.h/2,
693                      style->bullet_image.w, style->bullet_image.h);
694
695       XSetClipMask(blackbox->getXDisplay(), pen.gc(), None);
696       
697       XSetClipOrigin(blackbox->getXDisplay(), pen.gc(),
698                      0, 0);
699     } else {
700 #endif // BITMAPBUTTONS
701       const int bullet_size = 3;
702
703       switch (screen->getMenuStyle()->bullet) {
704       case Square:
705         XDrawRectangle(display, menu.frame, pen.gc(), sel_x, sel_y,
706                        bullet_size * 2, bullet_size * 2);
707         break;
708
709       case Triangle:
710         XPoint tri[3];
711
712         if (screen->getMenuStyle()->bullet_pos == Right) {
713           tri[0].x = sel_x + quarter_w - bullet_size;
714           tri[0].y = sel_y + quarter_w - bullet_size;
715           tri[1].x = 2 * bullet_size;
716           tri[1].y = bullet_size;
717           tri[2].x = -(2 * bullet_size);
718           tri[2].y = bullet_size;
719         } else {
720           tri[0].x = sel_x + quarter_w - bullet_size;
721           tri[0].y = item_y + half_w;
722           tri[1].x = 2 * bullet_size;
723           tri[1].y = bullet_size;
724           tri[2].x = 0;
725           tri[2].y = -(2 * bullet_size);
726         }
727
728         XFillPolygon(display, menu.frame, pen.gc(), tri, 3, Convex,
729                      CoordModePrevious);
730         break;
731
732       case Diamond:
733         XPoint dia[4];
734
735         dia[0].x = sel_x + quarter_w - bullet_size;
736         dia[0].y = item_y + half_w;
737         dia[1].x = bullet_size;
738         dia[1].y = -bullet_size;
739         dia[2].x = bullet_size;
740         dia[2].y = bullet_size;
741         dia[3].x = -bullet_size;
742         dia[3].y = bullet_size;
743
744         XFillPolygon(display, menu.frame, pen.gc(), dia, 4, Convex,
745                      CoordModePrevious);
746         break;
747       }
748 #ifdef    BITMAPBUTTONS
749     }
750 #endif // BITMAPBUTTONS
751   }
752 }
753
754
755 void Basemenu::setLabel(const string& label) {
756   menu.label = label;
757 }
758
759
760 void Basemenu::setItemSelected(int index, bool sel) {
761   assert(index >= 0);
762   BasemenuItem *item = find(index);
763   if (! item) return;
764
765   item->setSelected(sel);
766   if (visible) drawItem(index, (index == which_sub), True);
767 }
768
769
770 bool Basemenu::isItemSelected(int index) {
771   assert(index >= 0);
772   BasemenuItem *item = find(index);
773   if (! item) return False;
774
775   return item->isSelected();
776 }
777
778
779 void Basemenu::setItemEnabled(int index, bool enable) {
780   assert(index >= 0);
781   BasemenuItem *item = find(index);
782   if (! item) return;
783
784   item->setEnabled(enable);
785   if (visible) drawItem(index, (index == which_sub), True);
786 }
787
788
789 bool Basemenu::isItemEnabled(int index) {
790   assert(index >= 0);
791   BasemenuItem *item = find(index);
792   if (! item) return False;
793
794   return item->isEnabled();
795 }
796
797
798 void Basemenu::buttonPressEvent(XButtonEvent *be) {
799   if (be->window == menu.frame) {
800     const int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h),
801       w = (sbl * menu.persub) + i;
802     
803     BasemenuItem *item = find(w);
804     if (item) {
805       which_press = i;
806       which_sbl = sbl;
807
808
809       if (item->submenu())
810         drawSubmenu(w);
811       else
812         drawItem(w, (item->isEnabled()), True);
813     }
814   } else {
815     menu.x_move = be->x_root - menu.x;
816     menu.y_move = be->y_root - menu.y;
817   }
818 }
819
820
821 void Basemenu::buttonReleaseEvent(XButtonEvent *re) {
822   if (re->window == menu.title) {
823     if (moving) {
824       moving = False;
825
826       if (which_sub != -1)
827         drawSubmenu(which_sub);
828     }
829
830     if (re->x >= 0 && re->x <= static_cast<signed>(menu.width) &&
831         re->y >= 0 && re->y <= static_cast<signed>(menu.title_h))
832       if (re->button == 3)
833         hide();
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) {
838       hide();
839     } else {
840       const 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;
844
845       if (w >= 0 && w < static_cast<signed>(menuitems.size())) {
846         drawItem(p, (p == which_sub), True);
847
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);
852           }
853         }
854       } else {
855         drawItem(p, False, True);
856       }
857     }
858   }
859 }
860
861
862 void Basemenu::motionNotifyEvent(XMotionEvent *me) {
863   if (me->window == menu.title && (me->state & Button1Mask)) {
864     if (movable) {
865       if (! moving) {
866         if (parent && ! torn) {
867           parent->drawItem(parent->which_sub, False, True);
868           parent->which_sub = -1;
869         }
870
871         moving = torn = True;
872
873         if (which_sub != -1)
874           drawSubmenu(which_sub);
875       } else {
876         menu.x = me->x_root - menu.x_move,
877           menu.y = me->y_root - menu.y_move;
878
879         XMoveWindow(display, menu.window, menu.x, menu.y);
880
881         if (which_sub != -1)
882           drawSubmenu(which_sub);
883       }
884     }
885   } else if (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     const int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h),
889       w = (sbl * menu.persub) + i;
890
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         const int p = (which_sbl * menu.persub) + which_press;
895         BasemenuItem *item = find(p);
896         if (! item) return;
897
898         drawItem(p, False, True);
899         if (item->submenu() &&
900             item->submenu()->isVisible() &&
901             ! item->submenu()->isTorn()) {
902           item->submenu()->internal_hide();
903           which_sub = -1;
904         }
905       }
906
907       which_press = i;
908       which_sbl = sbl;
909
910       BasemenuItem *itmp = find(w);
911       if (! itmp) return;
912       
913       if (itmp->submenu())
914         drawSubmenu(w);
915       else
916         drawItem(w, (itmp->isEnabled()), True);
917     }
918   }
919 }
920
921
922 void Basemenu::exposeEvent(XExposeEvent *ee) {
923   if (ee->window == menu.title) {
924     redrawTitle();
925   } else if (ee->window == menu.frame) {
926     // this is a complicated algorithm... lets do it step by step...
927     // first... we see in which sub level the expose starts... and how many
928     // items down in that sublevel
929
930     const int sbl = (ee->x / menu.item_w), id = (ee->y / menu.item_h),
931       // next... figure out how many sublevels over the redraw spans
932       sbl_d = ((ee->x + ee->width) / menu.item_w);
933       // then we see how many items down to redraw
934     int id_d = ((ee->y + ee->height) / menu.item_h);
935
936     if (id_d > menu.persub) id_d = menu.persub;
937
938     // draw the sublevels and the number of items the exposure spans
939     MenuItems::iterator it,
940       end = menuitems.end();
941     for (int i = sbl; i <= sbl_d; i++) {
942       // set the iterator to the first item in the sublevel needing redrawing
943       it = menuitems.begin() + (id + (i * menu.persub));
944       for (int ii = id; ii <= id_d && it != end; ++it, ii++) {
945         const int index = ii + (i * menu.persub);
946         // redraw the item
947         drawItem(index, (which_sub == index), False,
948                  ee->x, ee->y, ee->width, ee->height);
949       }
950     }
951   }
952 }
953
954
955 void Basemenu::enterNotifyEvent(XCrossingEvent *ce) {
956   if (ce->window == menu.frame) {
957     menu.x_shift = menu.x, menu.y_shift = menu.y;
958     if (menu.x + menu.width > screen->getWidth()) {
959       menu.x_shift = screen->getWidth() - menu.width -
960         screen->getBorderWidth();
961       shifted = True;
962     } else if (menu.x < 0) {
963       menu.x_shift = -screen->getBorderWidth();
964       shifted = True;
965     }
966
967     if (menu.y + menu.height > screen->getHeight()) {
968       menu.y_shift = screen->getHeight() - menu.height -
969         screen->getBorderWidth();
970       shifted = True;
971     } else if (menu.y + static_cast<signed>(menu.title_h) < 0) {
972       menu.y_shift = -screen->getBorderWidth();
973       shifted = True;
974     }
975
976     if (shifted)
977       XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift);
978
979     if (which_sub != -1) {
980       BasemenuItem *tmp = find(which_sub);
981       if (tmp->submenu()->isVisible()) {
982         const int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h),
983           w = (sbl * menu.persub) + i;
984
985         if (w != which_sub && ! tmp->submenu()->isTorn()) {
986           tmp->submenu()->internal_hide();
987
988           drawItem(which_sub, False, True);
989           which_sub = -1;
990         }
991       }
992     }
993   }
994 }
995
996
997 void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) {
998   if (ce->window == menu.frame) {
999     if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) {
1000       const int p = (which_sbl * menu.persub) + which_press;
1001
1002       drawItem(p, (p == which_sub), True);
1003
1004       which_sbl = which_press = -1;
1005     }
1006
1007     if (shifted) {
1008       XMoveWindow(display, menu.window, menu.x, menu.y);
1009       shifted = False;
1010
1011       if (which_sub != -1) drawSubmenu(which_sub);
1012     }
1013   }
1014 }
1015
1016
1017 void Basemenu::reconfigure(void) {
1018   XSetWindowBackground(display, menu.window,
1019                        screen->getBorderColor()->pixel());
1020   XSetWindowBorder(display, menu.window,
1021                    screen->getBorderColor()->pixel());
1022   XSetWindowBorderWidth(display, menu.window, screen->getBorderWidth());
1023
1024   menu.bevel_w = screen->getBevelWidth();
1025   update();
1026 }
1027
1028
1029 void Basemenu::changeItemLabel(unsigned int index, const string& label) {
1030   BasemenuItem *item = find(index);
1031   if (item)
1032     item->newLabel(label);
1033 }