]> icculus.org git repositories - mikachu/openbox.git/blob - src/Basemenu.cc
new focus code! working better than ever!
[mikachu/openbox.git] / src / Basemenu.cc
1 // Basemenu.cc for Openbox
2 // Copyright (c) 2001 Sean 'Shaleh' Perry <shaleh@debian.org>
3 // Copyright (c) 1997 - 2000 Brad Hughes (bhughes@tcac.net)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a
6 // copy of this software and associated documentation files (the "Software"),
7 // to deal in the Software without restriction, including without limitation
8 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 // and/or sell copies of the Software, and to permit persons to whom the
10 // Software is furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18 // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 // DEALINGS IN THE SOFTWARE.
22
23 // stupid macros needed to access some functions in version 2 of the GNU C
24 // library
25 #ifndef   _GNU_SOURCE
26 #define   _GNU_SOURCE
27 #endif // _GNU_SOURCE
28
29 #ifdef    HAVE_CONFIG_H
30 #  include "../config.h"
31 #endif // HAVE_CONFIG_H
32
33 #ifdef    HAVE_STDIO_H
34 #  include <stdio.h>
35 #endif // HAVE_STDIO_H
36
37 #ifdef    HAVE_STDLIB_H
38 #  include <stdlib.h>
39 #endif // HAVE_STDLIB_H
40
41 #ifdef    HAVE_STRING_H
42 #  include <string.h>
43 #endif // HAVE_STRING_H
44
45 #include <algorithm>
46 using std::min;
47 using std::max;
48
49 #include "i18n.h"
50 #include "openbox.h"
51 #include "Basemenu.h"
52 #include "Screen.h"
53
54 static Basemenu *shown = (Basemenu *) 0;
55
56 Basemenu::Basemenu(BScreen &scrn) : openbox(scrn.getOpenbox()), screen(scrn) {
57   image_ctrl = screen.getImageControl();
58   display = openbox.getXDisplay();
59   parent = (Basemenu *) 0;
60   alignment = AlignDontCare;
61
62   title_vis =
63     movable =
64     hide_tree = True;
65
66   shifted =
67     internal_menu =
68     moving =
69     torn =
70     visible = False;
71
72   menu.x =
73     menu.y =
74     menu.x_shift =
75     menu.y_shift =
76     menu.x_move =
77     menu.y_move = 0;
78
79   which_sub =
80     which_press =
81     which_sbl = -1;
82
83   menu.frame_pixmap =
84     menu.title_pixmap =
85     menu.hilite_pixmap =
86     menu.sel_pixmap = None;
87
88   menu.bevel_w = screen.getBevelWidth();
89
90   if (i18n->multibyte())
91     menu.width = menu.title_h = menu.item_w = menu.frame_h =
92       screen.getMenuStyle()->t_fontset_extents->max_ink_extent.height +
93         (menu.bevel_w  * 2);
94   else
95     menu.width = menu.title_h = menu.item_w = menu.frame_h =
96       screen.getMenuStyle()->t_font->ascent +
97         screen.getMenuStyle()->t_font->descent + (menu.bevel_w * 2);
98   
99   menu.label = 0;
100   
101   menu.sublevels =
102     menu.persub =
103     menu.minsub = 0;
104   
105   MenuStyle *style = screen.getMenuStyle();
106   if (i18n->multibyte()) {
107     menu.item_h = style->f_fontset_extents->max_ink_extent.height +
108       (menu.bevel_w);
109   } else {
110     menu.item_h = style->f_font->ascent + style->f_font->descent +
111                   (menu.bevel_w);
112   }
113   
114   menu.height = menu.title_h + screen.getBorderWidth() + menu.frame_h;
115   
116   unsigned long attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel |
117                               CWColormap | CWOverrideRedirect | CWEventMask;
118   XSetWindowAttributes attrib;
119   attrib.background_pixmap = None;
120   attrib.background_pixel = attrib.border_pixel =
121                             screen.getBorderColor()->getPixel();
122   attrib.colormap = screen.getColormap();
123   attrib.override_redirect = True;
124   attrib.event_mask = ButtonPressMask | ButtonReleaseMask |
125                       ButtonMotionMask | ExposureMask;
126
127   menu.window =
128     XCreateWindow(display, screen.getRootWindow(), menu.x, menu.y, menu.width,
129                   menu.height, screen.getBorderWidth(), screen.getDepth(),
130                   InputOutput, screen.getVisual(), attrib_mask, &attrib);
131   openbox.saveMenuSearch(menu.window, this);
132
133   attrib_mask = CWBackPixmap | CWBackPixel | CWBorderPixel | CWEventMask;
134   attrib.background_pixel = screen.getBorderColor()->getPixel();
135   attrib.event_mask |= EnterWindowMask | LeaveWindowMask;
136
137   menu.title =
138     XCreateWindow(display, menu.window, 0, 0, menu.width, menu.height, 0,
139                   screen.getDepth(), InputOutput, screen.getVisual(),
140                   attrib_mask, &attrib);
141   openbox.saveMenuSearch(menu.title, this);
142
143   attrib.event_mask |= PointerMotionMask;
144   menu.frame = XCreateWindow(display, menu.window, 0,
145                              menu.title_h + screen.getBorderWidth(),
146                              menu.width, menu.frame_h, 0,
147                              screen.getDepth(), InputOutput,
148                              screen.getVisual(), attrib_mask, &attrib);
149   openbox.saveMenuSearch(menu.frame, this);
150
151   // even though this is the end of the constructor the menu is still not
152   // completely created.  items must be inserted and it must be update()'d
153 }
154
155
156 Basemenu::~Basemenu(void) {
157   XUnmapWindow(display, menu.window);
158
159   if (shown && shown->getWindowID() == getWindowID())
160     shown = (Basemenu *) 0;
161
162   while (!menuitems.empty())
163     remove(0);
164
165   if (menu.label)
166     delete [] menu.label;
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   if (menu.sel_pixmap)
178     image_ctrl->removeImage(menu.sel_pixmap);
179
180   openbox.removeMenuSearch(menu.title);
181   XDestroyWindow(display, menu.title);
182
183   openbox.removeMenuSearch(menu.frame);
184   XDestroyWindow(display, menu.frame);
185
186   openbox.removeMenuSearch(menu.window);
187   XDestroyWindow(display, menu.window);
188 }
189
190
191 int Basemenu::insert(const char *l, int function, const char *e, int pos) {
192   char *label = 0, *exec = 0;
193
194   if (l) label = bstrdup(l);
195   if (e) exec = bstrdup(e);
196
197   BasemenuItem *item = new BasemenuItem(label, function, exec);
198   if (pos == -1)
199     menuitems.push_back(item);
200   else
201     menuitems.insert(menuitems.begin() + pos, item);
202
203   return menuitems.size();
204 }
205
206
207 int Basemenu::insert(const char *l, Basemenu *submenu, int pos) {
208   char *label = 0;
209
210   if (l) label = bstrdup(l);
211
212   BasemenuItem *item = new BasemenuItem(label, submenu);
213   if (pos == -1)
214     menuitems.push_back(item);
215   else
216     menuitems.insert(menuitems.begin() + pos, item);
217
218   submenu->parent = this;
219
220   return menuitems.size();
221 }
222
223
224 int Basemenu::insert(const char **ulabel, int pos, int function) {
225   BasemenuItem *item = new BasemenuItem(ulabel, function);
226   if (pos == -1)
227     menuitems.push_back(item);
228   else
229     menuitems.insert(menuitems.begin() + pos, item);
230
231   return menuitems.size();
232 }
233
234
235 int Basemenu::remove(int index) {
236   if (index < 0 || index > menuitems.size()) return -1;
237
238   BasemenuItem *item = menuitems[index];
239   menuitems.erase(menuitems.begin() + index);
240
241   if (item) {
242     if ((!internal_menu) && (item->submenu())) {
243       Basemenu *tmp = (Basemenu *) item->submenu();
244
245       if (! tmp->internal_menu) {
246         delete tmp;
247       } else {
248         tmp->internal_hide();
249       }
250     }
251
252     if (item->label())
253       delete [] item->label();
254     
255     if (item->exec())
256       delete [] item->exec();
257
258     delete item;
259   }
260
261   if (which_sub == index)
262     which_sub = -1;
263   else if (which_sub > index)
264     which_sub--;
265
266   return menuitems.size();
267 }
268
269
270 void Basemenu::update(void) {
271   MenuStyle *style = screen.getMenuStyle();
272   if (i18n->multibyte()) {
273     menu.item_h = style->f_fontset_extents->max_ink_extent.height +
274                   menu.bevel_w;
275     menu.title_h = style->t_fontset_extents->max_ink_extent.height +
276                    (menu.bevel_w * 2);
277   } else {
278     menu.item_h = style->f_font->ascent + style->f_font->descent +
279                   menu.bevel_w;
280     menu.title_h = style->t_font->ascent + style->t_font->descent +
281                    (menu.bevel_w * 2);
282   }
283     
284   if (title_vis) {
285     const char *s = (menu.label) ? menu.label :
286                     i18n->getMessage(BasemenuSet, BasemenuOpenboxMenu,
287                                      "Openbox Menu");
288     int l = strlen(s);
289     
290
291     if (i18n->multibyte()) {
292       XRectangle ink, logical;
293       XmbTextExtents(screen.getMenuStyle()->t_fontset, s, l, &ink, &logical);
294       menu.item_w = logical.width;
295     } else {
296       menu.item_w = XTextWidth(screen.getMenuStyle()->t_font, s, l);
297     }
298     
299     menu.item_w += (menu.bevel_w * 2);
300   }  else {
301     menu.item_w = 1;
302   }
303
304   int ii = 0;
305   menuitemList::const_iterator it = menuitems.begin();
306   for (; it != menuitems.end(); it++) {
307     const char *s = (((*it)->u && *(*it)->u) ? *(*it)->u :
308                      (((*it)->l) ? (*it)->l : (const char *) 0));
309     int l = strlen(s);
310
311     if (i18n->multibyte()) {
312       XRectangle ink, logical;
313       XmbTextExtents(screen.getMenuStyle()->f_fontset, s, l, &ink, &logical);
314       ii = logical.width;
315     } else
316       ii = XTextWidth(screen.getMenuStyle()->f_font, s, l);
317
318     ii += (menu.bevel_w * 2) + (menu.item_h * 2);
319
320     menu.item_w = ((menu.item_w < (unsigned int) ii) ? ii : menu.item_w);
321   }
322
323   if (menuitems.size()) {
324     menu.sublevels = 1;
325
326     while (((menu.item_h * (menuitems.size() + 1) / menu.sublevels)
327             + menu.title_h + screen.getBorderWidth()) >
328            screen.size().h())
329       menu.sublevels++;
330
331     if (menu.sublevels < menu.minsub) menu.sublevels = menu.minsub;
332
333     menu.persub = menuitems.size() / menu.sublevels;
334     if (menuitems.size() % menu.sublevels) menu.persub++;
335   } else {
336     menu.sublevels = 0;
337     menu.persub = 0;
338   }
339
340   menu.width = (menu.sublevels * (menu.item_w));
341   if (! menu.width) menu.width = menu.item_w;
342
343   menu.frame_h = (menu.item_h * menu.persub);
344   menu.height = ((title_vis) ? menu.title_h + screen.getBorderWidth() : 0) +
345                 menu.frame_h;
346   if (! menu.frame_h) menu.frame_h = 1;
347   if (menu.height < 1) menu.height = 1;
348
349   Pixmap tmp;
350   BTexture *texture;
351   if (title_vis) {
352     tmp = menu.title_pixmap;
353     texture = &(screen.getMenuStyle()->title);
354     if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
355       menu.title_pixmap = None;
356       XSetWindowBackground(display, menu.title,
357                            texture->getColor()->getPixel());
358     } else {
359       menu.title_pixmap =
360         image_ctrl->renderImage(menu.width, menu.title_h, texture);
361       XSetWindowBackgroundPixmap(display, menu.title, menu.title_pixmap);
362     }
363     if (tmp) image_ctrl->removeImage(tmp);
364     XClearWindow(display, menu.title);
365   }
366
367   tmp = menu.frame_pixmap;
368   texture = &(screen.getMenuStyle()->frame);
369   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
370     menu.frame_pixmap = None;
371     XSetWindowBackground(display, menu.frame,
372                          texture->getColor()->getPixel());
373   } else {
374     menu.frame_pixmap =
375       image_ctrl->renderImage(menu.width, menu.frame_h, texture);
376     XSetWindowBackgroundPixmap(display, menu.frame, menu.frame_pixmap);
377   }
378   if (tmp) image_ctrl->removeImage(tmp);
379
380   tmp = menu.hilite_pixmap;
381   texture = &(screen.getMenuStyle()->hilite);
382   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
383     menu.hilite_pixmap = None;
384   } else {
385     menu.hilite_pixmap =
386       image_ctrl->renderImage(menu.item_w, menu.item_h, texture);
387   }
388   if (tmp) image_ctrl->removeImage(tmp);
389
390   tmp = menu.sel_pixmap;
391   if (texture->getTexture() == (BImage_Flat | BImage_Solid)) {
392     menu.sel_pixmap = None;
393   } else {
394     int hw = menu.item_h / 2;
395     menu.sel_pixmap =
396       image_ctrl->renderImage(hw, hw, texture);
397   }
398   if (tmp) image_ctrl->removeImage(tmp);
399
400   XResizeWindow(display, menu.window, menu.width, menu.height);
401
402   if (title_vis)
403     XResizeWindow(display, menu.title, menu.width, menu.title_h);
404
405   XMoveResizeWindow(display, menu.frame, 0,
406                     ((title_vis) ? menu.title_h +
407                      screen.getBorderWidth() : 0), menu.width,
408                     menu.frame_h);
409
410   XClearWindow(display, menu.window);
411   XClearWindow(display, menu.title);
412   XClearWindow(display, menu.frame);
413
414   if (title_vis && visible) redrawTitle();
415
416   for (int i = 0; visible && i < menuitems.size(); i++) {
417     if (i == which_sub) {
418       drawItem(i, True, 0);
419       drawSubmenu(i);
420     } else {
421       drawItem(i, False, 0);
422     }
423   }
424
425   if (parent && visible)
426     parent->drawSubmenu(parent->which_sub);
427
428   XMapSubwindows(display, menu.window);
429 }
430
431
432 void Basemenu::show(void) {
433   XMapSubwindows(display, menu.window);
434   XMapWindow(display, menu.window);
435   visible = True;
436
437   if (! parent) {
438     if (shown && (! shown->torn))
439        shown->hide();
440
441     shown = this;
442   }
443 }
444
445
446 void Basemenu::hide(void) {
447   if ((! torn) && hide_tree && parent && parent->isVisible()) {
448     Basemenu *p = parent;
449
450     while (p->isVisible() && (! p->torn) && p->parent) p = p->parent;
451     p->internal_hide();
452   } else {
453     internal_hide();
454   }
455 }
456
457
458 void Basemenu::internal_hide(void) {
459   if (which_sub != -1) {
460     BasemenuItem *tmp = menuitems[which_sub];
461     tmp->submenu()->internal_hide();
462   }
463
464   if (parent && (! torn)) {
465     parent->drawItem(parent->which_sub, False, True);
466
467     parent->which_sub = -1;
468   } else if (shown && shown->menu.window == menu.window) {
469     shown = (Basemenu *) 0;
470   }
471
472   torn = visible = False;
473   which_sub = which_press = which_sub = -1;
474
475   XUnmapWindow(display, menu.window);
476 }
477
478
479 void Basemenu::move(int x, int y) {
480   menu.x = x;
481   menu.y = y;
482   XMoveWindow(display, menu.window, x, y);
483   if (which_sub != -1)
484     drawSubmenu(which_sub);
485 }
486
487
488 void Basemenu::redrawTitle(void) {
489   char *text = (char *) ((menu.label) ? menu.label :
490                          i18n->getMessage(BasemenuSet, BasemenuOpenboxMenu,
491                                           "Openbox Menu"));
492   int dx = menu.bevel_w, len = strlen(text);
493   unsigned int l;
494
495   if (i18n->multibyte()) {
496     XRectangle ink, logical;
497     XmbTextExtents(screen.getMenuStyle()->t_fontset, text, len, &ink, &logical);
498     l = logical.width;
499   } else {
500     l = XTextWidth(screen.getMenuStyle()->t_font, text, len);
501   }
502
503   l +=  (menu.bevel_w * 2);
504
505   switch (screen.getMenuStyle()->t_justify) {
506   case BScreen::RightJustify:
507     dx += menu.width - l;
508     break;
509
510   case BScreen::CenterJustify:
511     dx += (menu.width - l) / 2;
512     break;
513   }
514
515   MenuStyle *style = screen.getMenuStyle();
516   if (i18n->multibyte())
517     XmbDrawString(display, menu.title, style->t_fontset, style->t_text_gc, dx,
518                   (menu.bevel_w - style->t_fontset_extents->max_ink_extent.y),
519                   text, len);
520   else
521     XDrawString(display, menu.title, style->t_text_gc, dx,
522                 (style->t_font->ascent + menu.bevel_w), text, len);
523 }
524
525
526 void Basemenu::drawSubmenu(int index) {
527   if (which_sub != -1 && which_sub != index) {
528     BasemenuItem *itmp = menuitems[which_sub];
529
530     if (! itmp->submenu()->isTorn())
531       itmp->submenu()->internal_hide();
532   }
533
534   if (index >= 0 && index < menuitems.size()) {
535     BasemenuItem *item = menuitems[index];
536     if (item->submenu() && visible && (! item->submenu()->isTorn()) &&
537         item->isEnabled()) {
538       if (item->submenu()->parent != this) item->submenu()->parent = this;
539       int sbl = index / menu.persub, i = index - (sbl * menu.persub),
540             x = menu.x +
541                 ((menu.item_w * (sbl + 1)) + screen.getBorderWidth()), y;
542     
543       if (alignment == AlignTop)
544         y = (((shifted) ? menu.y_shift : menu.y) +
545              ((title_vis) ? menu.title_h + screen.getBorderWidth() : 0) -
546              ((item->submenu()->title_vis) ?
547               item->submenu()->menu.title_h + screen.getBorderWidth() : 0));
548       else
549         y = (((shifted) ? menu.y_shift : menu.y) +
550              (menu.item_h * i) +
551              ((title_vis) ? menu.title_h + screen.getBorderWidth() : 0) -
552              ((item->submenu()->title_vis) ?
553               item->submenu()->menu.title_h + screen.getBorderWidth() : 0));
554
555       if (alignment == AlignBottom &&
556           (y + item->submenu()->menu.height) > ((shifted) ? menu.y_shift :
557                                                 menu.y) + menu.height)
558         y = (((shifted) ? menu.y_shift : menu.y) +
559              menu.height - item->submenu()->menu.height);
560
561       if ((x + item->submenu()->getWidth()) > screen.size().w()) {
562         x = ((shifted) ? menu.x_shift : menu.x) -
563             item->submenu()->getWidth() - screen.getBorderWidth();
564       }
565       
566       if (x < 0) x = 0;
567
568       if ((y + item->submenu()->getHeight()) > screen.size().h())
569         y = screen.size().h() - item->submenu()->getHeight() -
570             (screen.getBorderWidth() * 2);
571       if (y < 0) y = 0;
572       
573       item->submenu()->move(x, y);
574       if (! moving) drawItem(index, True);
575     
576       if (! item->submenu()->isVisible())
577         item->submenu()->show();
578       item->submenu()->moving = moving;
579       which_sub = index;
580     } else {
581       which_sub = -1;
582     }
583   }
584 }
585
586
587 Bool Basemenu::hasSubmenu(int index) {
588   if ((index >= 0) && (index < menuitems.size()))
589     if (menuitems[index]->submenu())
590       return True;
591
592   return False;
593 }
594
595
596 void Basemenu::drawItem(int index, Bool highlight, Bool clear,
597                         int x, int y, unsigned int w, unsigned int h)
598 {
599   if (index < 0 || index > menuitems.size()) return;
600
601   BasemenuItem *item = menuitems[index];
602   if (! item) return;
603   
604   Bool dotext = True, dohilite = True, dosel = True;
605   const char *text = (item->ulabel()) ? *item->ulabel() : item->label();
606   int sbl = index / menu.persub, i = index - (sbl * menu.persub);
607   int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h);
608   int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0;
609   int text_x = 0, text_y = 0, len = strlen(text), sel_x = 0, sel_y = 0;
610   unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0,
611     text_h = 0;
612   unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4;
613   
614   if (text) {
615     if (i18n->multibyte()) {
616       XRectangle ink, logical;
617       XmbTextExtents(screen.getMenuStyle()->f_fontset,
618                      text, len, &ink, &logical);
619       text_w = logical.width;
620       text_y = item_y + (menu.bevel_w / 2) -
621                screen.getMenuStyle()->f_fontset_extents->max_ink_extent.y;
622     } else {
623       text_w = XTextWidth(screen.getMenuStyle()->f_font, text, len);
624       text_y =  item_y +
625                 screen.getMenuStyle()->f_font->ascent +
626                 (menu.bevel_w / 2);
627     }
628     
629     switch(screen.getMenuStyle()->f_justify) {
630     case BScreen::LeftJustify:
631       text_x = item_x + menu.bevel_w + menu.item_h + 1;
632       break;
633       
634     case BScreen::RightJustify:
635       text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w);
636       break;
637       
638     case BScreen::CenterJustify:
639       text_x = item_x + ((menu.item_w + 1 - text_w) / 2);
640       break;
641     }
642     
643     text_h = menu.item_h - menu.bevel_w;
644   }
645   
646   GC gc =
647     ((highlight || item->isSelected()) ? screen.getMenuStyle()->h_text_gc :
648      screen.getMenuStyle()->f_text_gc),
649     tgc =
650     ((highlight) ? screen.getMenuStyle()->h_text_gc :
651      ((item->isEnabled()) ? screen.getMenuStyle()->f_text_gc :
652       screen.getMenuStyle()->d_text_gc));
653   
654   sel_x = item_x;
655   if (screen.getMenuStyle()->bullet_pos == Right)
656     sel_x += (menu.item_w - menu.item_h - menu.bevel_w);
657   sel_x += quarter_w;
658   sel_y = item_y + quarter_w;
659   
660   if (clear) {
661     XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h,
662                False);
663   } else if (! (x == y && y == -1 && w == h && h == 0)) {
664     // calculate the which part of the hilite to redraw
665     if (! (max(item_x, x) <= (signed) min(item_x + menu.item_w, x + w) &&
666            max(item_y, y) <= (signed) min(item_y + menu.item_h, y + h))) {
667       dohilite = False;
668     } else {
669       hilite_x = max(item_x, x);
670       hilite_y = max(item_y, y);
671       hilite_w = min(item_x + menu.item_w, x + w) - hilite_x;
672       hilite_h = min(item_y + menu.item_h, y + h) - hilite_y;
673       hoff_x = hilite_x % menu.item_w;
674       hoff_y = hilite_y % menu.item_h;
675     }
676     
677     // check if we need to redraw the text    
678     int text_ry = item_y + (menu.bevel_w / 2);
679     if (! (max(text_x, x) <= (signed) min(text_x + text_w, x + w) &&
680            max(text_ry, y) <= (signed) min(text_ry + text_h, y + h)))
681       dotext = False;
682     
683     // check if we need to redraw the select pixmap/menu bullet
684     if (! (max(sel_x, x) <= (signed) min(sel_x + half_w, x + w) &&
685            max(sel_y, y) <= (signed) min(sel_y + half_w, y + h)))
686       dosel = False;
687   }
688   
689   if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) {
690     if (menu.hilite_pixmap)
691       XCopyArea(display, menu.hilite_pixmap, menu.frame,
692                 screen.getMenuStyle()->hilite_gc, hoff_x, hoff_y,
693                 hilite_w, hilite_h, hilite_x, hilite_y);
694     else
695       XFillRectangle(display, menu.frame,
696                      screen.getMenuStyle()->hilite_gc,
697                      hilite_x, hilite_y, hilite_w, hilite_h);
698   } else if (dosel && item->isSelected() &&
699                          (menu.sel_pixmap != ParentRelative)) {
700     if (menu.sel_pixmap)
701       XCopyArea(display, menu.sel_pixmap, menu.frame,
702                 screen.getMenuStyle()->hilite_gc, 0, 0,
703                 half_w, half_w, sel_x, sel_y);
704     else
705       XFillRectangle(display, menu.frame,
706                      screen.getMenuStyle()->hilite_gc,
707                      sel_x, sel_y, half_w, half_w);
708   }
709   
710   if (dotext && text) {
711     if (i18n->multibyte())
712       XmbDrawString(display, menu.frame, screen.getMenuStyle()->f_fontset,
713                     tgc, text_x, text_y, text, len);
714     else
715       XDrawString(display, menu.frame, tgc, text_x, text_y, text, len);
716   }
717
718   if (dosel && item->submenu()) {
719     switch (screen.getMenuStyle()->bullet) {
720     case Square:
721       XDrawRectangle(display, menu.frame, gc, sel_x, sel_y, half_w, half_w);
722       break;
723
724     case Triangle:
725       XPoint tri[3];
726
727       if (screen.getMenuStyle()->bullet_pos == Right) {
728         tri[0].x = sel_x + quarter_w - 2;
729         tri[0].y = sel_y + quarter_w - 2;
730         tri[1].x = 4;
731         tri[1].y = 2;
732         tri[2].x = -4;
733         tri[2].y = 2;
734       } else {
735         tri[0].x = sel_x + quarter_w - 2;
736         tri[0].y = item_y + half_w;
737         tri[1].x = 4;
738         tri[1].y = 2;
739         tri[2].x = 0;
740         tri[2].y = -4;
741       }
742       
743       XFillPolygon(display, menu.frame, gc, tri, 3, Convex,
744                    CoordModePrevious);
745       break;
746       
747     case Diamond:
748       XPoint dia[4];
749
750       dia[0].x = sel_x + quarter_w - 3;
751       dia[0].y = item_y + half_w;
752       dia[1].x = 3;
753       dia[1].y = -3;
754       dia[2].x = 3;
755       dia[2].y = 3;
756       dia[3].x = -3;
757       dia[3].y = 3;
758
759       XFillPolygon(display, menu.frame, gc, dia, 4, Convex,
760                    CoordModePrevious);
761       break;
762     }
763   }
764 }
765
766
767 void Basemenu::setLabel(const char *l) {
768   if (menu.label)
769     delete [] menu.label;
770
771   if (l) menu.label = bstrdup(l);
772   else menu.label = 0;
773 }
774
775
776 void Basemenu::setItemSelected(int index, Bool sel) {
777   if (index < 0 || index >= menuitems.size()) return;
778
779   BasemenuItem *item = find(index);
780   if (! item) return;
781
782   item->setSelected(sel);
783   if (visible) drawItem(index, (index == which_sub), True);
784 }
785
786
787 Bool Basemenu::isItemSelected(int index) {
788   if (index < 0 || index >= menuitems.size()) return False;
789
790   BasemenuItem *item = find(index);
791   if (! item) return False;
792
793   return item->isSelected();
794 }
795
796
797 void Basemenu::setItemEnabled(int index, Bool enable) {
798   if (index < 0 || index >= menuitems.size()) return;
799
800   BasemenuItem *item = find(index);
801   if (! item) return;
802
803   item->setEnabled(enable);
804   if (visible) drawItem(index, (index == which_sub), True);
805 }
806
807
808 Bool Basemenu::isItemEnabled(int index) {
809   if (index < 0 || index >= menuitems.size()) return False;
810
811   BasemenuItem *item = find(index);
812   if (! item) return False;
813
814   return item->isEnabled();
815 }
816
817
818 void Basemenu::buttonPressEvent(XButtonEvent *be) {
819   if (be->window == menu.frame) {
820     int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h);
821     int w = (sbl * menu.persub) + i;
822
823     if (w < menuitems.size() && w >= 0) {
824       which_press = i;
825       which_sbl = sbl;
826
827       BasemenuItem *item = menuitems[w];
828
829       if (item->submenu())
830         drawSubmenu(w);
831       else
832         drawItem(w, (item->isEnabled()), True);
833     }
834   } else {
835     menu.x_move = be->x_root - menu.x;
836     menu.y_move = be->y_root - menu.y;
837   }
838 }
839
840
841 void Basemenu::buttonReleaseEvent(XButtonEvent *re) {
842   if (re->window == menu.title) {
843     if (moving) {
844       moving = False;
845       
846       if (which_sub != -1)
847         drawSubmenu(which_sub);
848     }
849     
850     if (re->x >= 0 && re->x <= (signed) menu.width &&
851         re->y >= 0 && re->y <= (signed) menu.title_h)
852       if (re->button == 3)
853         hide();
854   } else if (re->window == menu.frame &&
855              re->x >= 0 && re->x < (signed) menu.width &&
856              re->y >= 0 && re->y < (signed) menu.frame_h) {
857     if (re->button == 3) {
858       hide();
859     } else {
860       int sbl = (re->x / menu.item_w), i = (re->y / menu.item_h),
861            ix = sbl * menu.item_w, iy = i * menu.item_h,
862             w = (sbl * menu.persub) + i,
863             p = (which_sbl * menu.persub) + which_press;
864
865       if (w < menuitems.size() && w >= 0) {
866         drawItem(p, (p == which_sub), True);
867
868         if  (p == w && isItemEnabled(w)) {
869           if (re->x > ix && re->x < (signed) (ix + menu.item_w) &&
870               re->y > iy && re->y < (signed) (iy + menu.item_h)) {
871             itemSelected(re->button, w);
872           }
873         }
874       } else
875         drawItem(p, False, True);
876     }
877   }
878 }
879
880
881 void Basemenu::motionNotifyEvent(XMotionEvent *me) {
882   if (me->window == menu.title && (me->state & Button1Mask)) {
883     if (movable) {
884       if (! moving) {
885         if (parent && (! torn)) {
886           parent->drawItem(parent->which_sub, False, True);
887           parent->which_sub = -1;
888         }
889
890         moving = torn = True;
891
892         if (which_sub != -1)
893           drawSubmenu(which_sub);
894       } else {
895         menu.x = me->x_root - menu.x_move,
896         menu.y = me->y_root - menu.y_move;
897         
898         XMoveWindow(display, menu.window, menu.x, menu.y);
899           
900         if (which_sub != -1)
901           drawSubmenu(which_sub);
902       }
903     }
904   } else if ((! (me->state & Button1Mask)) && me->window == menu.frame &&
905              me->x >= 0 && me->x < (signed) menu.width &&
906              me->y >= 0 && me->y < (signed) menu.frame_h) {
907     int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h),
908           w = (sbl * menu.persub) + i;
909
910     if ((i != which_press || sbl != which_sbl) &&
911         (w < menuitems.size() && w >= 0)) {
912       if (which_press != -1 && which_sbl != -1) {
913         int p = (which_sbl * menu.persub) + which_press;
914         BasemenuItem *item = menuitems[p];
915
916         drawItem(p, False, True);
917         if (item->submenu())
918           if (item->submenu()->isVisible() &&
919               (! item->submenu()->isTorn())) {
920             item->submenu()->internal_hide();
921             which_sub = -1;
922           }
923       }
924
925       which_press = i;
926       which_sbl = sbl;
927
928       BasemenuItem *itmp = menuitems[w];
929
930       if (itmp->submenu())
931         drawSubmenu(w);
932       else
933         drawItem(w, (itmp->isEnabled()), True);
934     }
935   }
936 }
937
938
939 void Basemenu::exposeEvent(XExposeEvent *ee) {
940   if (ee->window == menu.title) {
941     redrawTitle();
942   } else if (ee->window == menu.frame) {
943     // this is a compilicated algorithm... lets do it step by step...
944     // first... we see in which sub level the expose starts... and how many
945     // items down in that sublevel
946
947     int sbl = (ee->x / menu.item_w), id = (ee->y / menu.item_h),
948       // next... figure out how many sublevels over the redraw spans
949       sbl_d = ((ee->x + ee->width) / menu.item_w),
950       // then we see how many items down to redraw
951       id_d = ((ee->y + ee->height) / menu.item_h);
952
953     if (id_d > menu.persub) id_d = menu.persub;
954
955     // draw the sublevels and the number of items the exposure spans
956     menuitemList::const_iterator it = menuitems.begin();
957     int i, ii;
958     for (i = sbl; i <= sbl_d; i++) {
959       // set the iterator to the first item in the sublevel needing redrawing
960       it = menuitems.begin() + (id + (i * menu.persub));
961       for (ii = id; ii <= id_d && it != menuitems.end(); it++, ii++) {
962         int index = ii + (i * menu.persub);
963         // redraw the item
964         drawItem(index, (which_sub == index), False,
965                  ee->x, ee->y, ee->width, ee->height);
966       }
967     }
968   }
969 }
970
971
972 void Basemenu::enterNotifyEvent(XCrossingEvent *ce) {
973   if (ce->window == menu.frame) {
974     menu.x_shift = menu.x, menu.y_shift = menu.y;
975     if (menu.x + menu.width > screen.size().w()) {
976       menu.x_shift = screen.size().w() - menu.width -
977         screen.getBorderWidth();
978       shifted = True;
979     } else if (menu.x < 0) {
980       menu.x_shift = -screen.getBorderWidth();
981       shifted = True;
982     }
983
984     if (menu.y + menu.height > screen.size().h()) {
985       menu.y_shift = screen.size().h() - menu.height -
986         screen.getBorderWidth();
987       shifted = True;
988     } else if (menu.y + (signed) menu.title_h < 0) {
989       menu.y_shift = -screen.getBorderWidth();
990       shifted = True;
991     }
992
993     if (shifted)
994       XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift);
995
996     if (which_sub != -1) {
997       BasemenuItem *tmp = menuitems[which_sub];
998       if (tmp->submenu()->isVisible()) {
999         int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h),
1000           w = (sbl * menu.persub) + i;
1001
1002         if (w != which_sub && (! tmp->submenu()->isTorn())) {
1003           tmp->submenu()->internal_hide();
1004
1005           drawItem(which_sub, False, True);
1006           which_sub = -1;
1007         }
1008       }
1009     }
1010   }
1011 }
1012
1013
1014 void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) {
1015   if (ce->window == menu.frame) {
1016     if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) {
1017       int p = (which_sbl * menu.persub) + which_press;
1018
1019       drawItem(p, (p == which_sub), True);
1020
1021       which_sbl = which_press = -1;
1022     }
1023
1024     if (shifted) {
1025       XMoveWindow(display, menu.window, menu.x, menu.y);
1026       shifted = False;
1027
1028       if (which_sub != -1) drawSubmenu(which_sub);
1029     }
1030   }
1031 }
1032
1033
1034 void Basemenu::reconfigure(void) {
1035   XSetWindowBackground(display, menu.window,
1036                        screen.getBorderColor()->getPixel());
1037   XSetWindowBorder(display, menu.window,
1038                    screen.getBorderColor()->getPixel());
1039   XSetWindowBorderWidth(display, menu.window, screen.getBorderWidth());
1040
1041   menu.bevel_w = screen.getBevelWidth();
1042   update();
1043 }