]> icculus.org git repositories - mikachu/openbox.git/blob - src/Basemenu.cc
added the X classes to the build process though they aren't sued by anything else...
[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 > (signed)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 < (signed)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 < (signed)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 >= (signed)menuitems.size())
589     return false;
590   return (menuitems[index]->submenu());
591 }
592
593
594 void Basemenu::drawItem(int index, Bool highlight, Bool clear,
595                         int x, int y, unsigned int w, unsigned int h)
596 {
597   if (index < 0 || index > (signed)menuitems.size()) return;
598
599   BasemenuItem *item = menuitems[index];
600   if (! item) return;
601   
602   Bool dotext = True, dohilite = True, dosel = True;
603   const char *text = (item->ulabel()) ? *item->ulabel() : item->label();
604   int sbl = index / menu.persub, i = index - (sbl * menu.persub);
605   int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h);
606   int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0;
607   int text_x = 0, text_y = 0, len = strlen(text), sel_x = 0, sel_y = 0;
608   unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0,
609     text_h = 0;
610   unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4;
611   
612   if (text) {
613     if (i18n->multibyte()) {
614       XRectangle ink, logical;
615       XmbTextExtents(screen.getMenuStyle()->f_fontset,
616                      text, len, &ink, &logical);
617       text_w = logical.width;
618       text_y = item_y + (menu.bevel_w / 2) -
619                screen.getMenuStyle()->f_fontset_extents->max_ink_extent.y;
620     } else {
621       text_w = XTextWidth(screen.getMenuStyle()->f_font, text, len);
622       text_y =  item_y +
623                 screen.getMenuStyle()->f_font->ascent +
624                 (menu.bevel_w / 2);
625     }
626     
627     switch(screen.getMenuStyle()->f_justify) {
628     case BScreen::LeftJustify:
629       text_x = item_x + menu.bevel_w + menu.item_h + 1;
630       break;
631       
632     case BScreen::RightJustify:
633       text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w);
634       break;
635       
636     case BScreen::CenterJustify:
637       text_x = item_x + ((menu.item_w + 1 - text_w) / 2);
638       break;
639     }
640     
641     text_h = menu.item_h - menu.bevel_w;
642   }
643   
644   GC gc =
645     ((highlight || item->isSelected()) ? screen.getMenuStyle()->h_text_gc :
646      screen.getMenuStyle()->f_text_gc),
647     tgc =
648     ((highlight) ? screen.getMenuStyle()->h_text_gc :
649      ((item->isEnabled()) ? screen.getMenuStyle()->f_text_gc :
650       screen.getMenuStyle()->d_text_gc));
651   
652   sel_x = item_x;
653   if (screen.getMenuStyle()->bullet_pos == Right)
654     sel_x += (menu.item_w - menu.item_h - menu.bevel_w);
655   sel_x += quarter_w;
656   sel_y = item_y + quarter_w;
657   
658   if (clear) {
659     XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h,
660                False);
661   } else if (! (x == y && y == -1 && w == h && h == 0)) {
662     // calculate the which part of the hilite to redraw
663     if (! (max(item_x, x) <= (signed) min(item_x + menu.item_w, x + w) &&
664            max(item_y, y) <= (signed) min(item_y + menu.item_h, y + h))) {
665       dohilite = False;
666     } else {
667       hilite_x = max(item_x, x);
668       hilite_y = max(item_y, y);
669       hilite_w = min(item_x + menu.item_w, x + w) - hilite_x;
670       hilite_h = min(item_y + menu.item_h, y + h) - hilite_y;
671       hoff_x = hilite_x % menu.item_w;
672       hoff_y = hilite_y % menu.item_h;
673     }
674     
675     // check if we need to redraw the text    
676     int text_ry = item_y + (menu.bevel_w / 2);
677     if (! (max(text_x, x) <= (signed) min(text_x + text_w, x + w) &&
678            max(text_ry, y) <= (signed) min(text_ry + text_h, y + h)))
679       dotext = False;
680     
681     // check if we need to redraw the select pixmap/menu bullet
682     if (! (max(sel_x, x) <= (signed) min(sel_x + half_w, x + w) &&
683            max(sel_y, y) <= (signed) min(sel_y + half_w, y + h)))
684       dosel = False;
685   }
686   
687   if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) {
688     if (menu.hilite_pixmap)
689       XCopyArea(display, menu.hilite_pixmap, menu.frame,
690                 screen.getMenuStyle()->hilite_gc, hoff_x, hoff_y,
691                 hilite_w, hilite_h, hilite_x, hilite_y);
692     else
693       XFillRectangle(display, menu.frame,
694                      screen.getMenuStyle()->hilite_gc,
695                      hilite_x, hilite_y, hilite_w, hilite_h);
696   } else if (dosel && item->isSelected() &&
697                          (menu.sel_pixmap != ParentRelative)) {
698     if (menu.sel_pixmap)
699       XCopyArea(display, menu.sel_pixmap, menu.frame,
700                 screen.getMenuStyle()->hilite_gc, 0, 0,
701                 half_w, half_w, sel_x, sel_y);
702     else
703       XFillRectangle(display, menu.frame,
704                      screen.getMenuStyle()->hilite_gc,
705                      sel_x, sel_y, half_w, half_w);
706   }
707   
708   if (dotext && text) {
709     if (i18n->multibyte())
710       XmbDrawString(display, menu.frame, screen.getMenuStyle()->f_fontset,
711                     tgc, text_x, text_y, text, len);
712     else
713       XDrawString(display, menu.frame, tgc, text_x, text_y, text, len);
714   }
715
716   if (dosel && item->submenu()) {
717     switch (screen.getMenuStyle()->bullet) {
718     case Square:
719       XDrawRectangle(display, menu.frame, gc, sel_x, sel_y, half_w, half_w);
720       break;
721
722     case Triangle:
723       XPoint tri[3];
724
725       if (screen.getMenuStyle()->bullet_pos == Right) {
726         tri[0].x = sel_x + quarter_w - 2;
727         tri[0].y = sel_y + quarter_w - 2;
728         tri[1].x = 4;
729         tri[1].y = 2;
730         tri[2].x = -4;
731         tri[2].y = 2;
732       } else {
733         tri[0].x = sel_x + quarter_w - 2;
734         tri[0].y = item_y + half_w;
735         tri[1].x = 4;
736         tri[1].y = 2;
737         tri[2].x = 0;
738         tri[2].y = -4;
739       }
740       
741       XFillPolygon(display, menu.frame, gc, tri, 3, Convex,
742                    CoordModePrevious);
743       break;
744       
745     case Diamond:
746       XPoint dia[4];
747
748       dia[0].x = sel_x + quarter_w - 3;
749       dia[0].y = item_y + half_w;
750       dia[1].x = 3;
751       dia[1].y = -3;
752       dia[2].x = 3;
753       dia[2].y = 3;
754       dia[3].x = -3;
755       dia[3].y = 3;
756
757       XFillPolygon(display, menu.frame, gc, dia, 4, Convex,
758                    CoordModePrevious);
759       break;
760     }
761   }
762 }
763
764
765 void Basemenu::setLabel(const char *l) {
766   if (menu.label)
767     delete [] menu.label;
768
769   if (l) menu.label = bstrdup(l);
770   else menu.label = 0;
771 }
772
773
774 void Basemenu::setItemSelected(int index, bool sel) {
775   if (index < 0 || index >= (signed)menuitems.size()) return;
776
777   BasemenuItem *item = find(index);
778   if (! item) return;
779
780   item->setSelected(sel);
781   if (visible) drawItem(index, (index == which_sub), True);
782 }
783
784
785 bool Basemenu::isItemSelected(int index) {
786   if (index < 0 || index >= (signed)menuitems.size()) return false;
787
788   BasemenuItem *item = find(index);
789   if (! item) return false;
790
791   return item->isSelected();
792 }
793
794
795 void Basemenu::setItemEnabled(int index, bool enable) {
796   if (index < 0 || index >= (signed)menuitems.size()) return;
797
798   BasemenuItem *item = find(index);
799   if (! item) return;
800
801   item->setEnabled(enable);
802   if (visible) drawItem(index, (index == which_sub), True);
803 }
804
805
806 bool Basemenu::isItemEnabled(int index) {
807   if (index < 0 || index >= (signed)menuitems.size()) return false;
808
809   BasemenuItem *item = find(index);
810   if (! item) return False;
811
812   return item->isEnabled();
813 }
814
815
816 void Basemenu::buttonPressEvent(XButtonEvent *be) {
817   if (be->window == menu.frame) {
818     int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h);
819     int w = (sbl * menu.persub) + i;
820
821     if (w < (signed)menuitems.size() && w >= 0) {
822       which_press = i;
823       which_sbl = sbl;
824
825       BasemenuItem *item = menuitems[w];
826
827       if (item->submenu())
828         drawSubmenu(w);
829       else
830         drawItem(w, (item->isEnabled()), True);
831     }
832   } else {
833     menu.x_move = be->x_root - menu.x;
834     menu.y_move = be->y_root - menu.y;
835   }
836 }
837
838
839 void Basemenu::buttonReleaseEvent(XButtonEvent *re) {
840   if (re->window == menu.title) {
841     if (moving) {
842       moving = False;
843       
844       if (which_sub != -1)
845         drawSubmenu(which_sub);
846     }
847     
848     if (re->x >= 0 && re->x <= (signed) menu.width &&
849         re->y >= 0 && re->y <= (signed) menu.title_h)
850       if (re->button == 3)
851         hide();
852   } else if (re->window == menu.frame &&
853              re->x >= 0 && re->x < (signed) menu.width &&
854              re->y >= 0 && re->y < (signed) menu.frame_h) {
855     if (re->button == 3) {
856       hide();
857     } else {
858       int sbl = (re->x / menu.item_w), i = (re->y / menu.item_h),
859            ix = sbl * menu.item_w, iy = i * menu.item_h,
860             w = (sbl * menu.persub) + i,
861             p = (which_sbl * menu.persub) + which_press;
862
863       if (w < (signed)menuitems.size() && w >= 0) {
864         drawItem(p, (p == which_sub), True);
865
866         if  (p == w && isItemEnabled(w)) {
867           if (re->x > ix && re->x < (signed) (ix + menu.item_w) &&
868               re->y > iy && re->y < (signed) (iy + menu.item_h)) {
869             itemSelected(re->button, w);
870           }
871         }
872       } else
873         drawItem(p, False, True);
874     }
875   }
876 }
877
878
879 void Basemenu::motionNotifyEvent(XMotionEvent *me) {
880   if (me->window == menu.title && (me->state & Button1Mask)) {
881     if (movable) {
882       if (! moving) {
883         if (parent && (! torn)) {
884           parent->drawItem(parent->which_sub, False, True);
885           parent->which_sub = -1;
886         }
887
888         moving = torn = True;
889
890         if (which_sub != -1)
891           drawSubmenu(which_sub);
892       } else {
893         menu.x = me->x_root - menu.x_move,
894         menu.y = me->y_root - menu.y_move;
895         
896         XMoveWindow(display, menu.window, menu.x, menu.y);
897           
898         if (which_sub != -1)
899           drawSubmenu(which_sub);
900       }
901     }
902   } else if ((! (me->state & Button1Mask)) && me->window == menu.frame &&
903              me->x >= 0 && me->x < (signed) menu.width &&
904              me->y >= 0 && me->y < (signed) menu.frame_h) {
905     int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h),
906           w = (sbl * menu.persub) + i;
907
908     if ((i != which_press || sbl != which_sbl) &&
909         (w < (signed)menuitems.size() && w >= 0)) {
910       if (which_press != -1 && which_sbl != -1) {
911         int p = (which_sbl * menu.persub) + which_press;
912         BasemenuItem *item = menuitems[p];
913
914         drawItem(p, False, True);
915         if (item->submenu())
916           if (item->submenu()->isVisible() &&
917               (! item->submenu()->isTorn())) {
918             item->submenu()->internal_hide();
919             which_sub = -1;
920           }
921       }
922
923       which_press = i;
924       which_sbl = sbl;
925
926       BasemenuItem *itmp = menuitems[w];
927
928       if (itmp->submenu())
929         drawSubmenu(w);
930       else
931         drawItem(w, (itmp->isEnabled()), True);
932     }
933   }
934 }
935
936
937 void Basemenu::exposeEvent(XExposeEvent *ee) {
938   if (ee->window == menu.title) {
939     redrawTitle();
940   } else if (ee->window == menu.frame) {
941     // this is a compilicated algorithm... lets do it step by step...
942     // first... we see in which sub level the expose starts... and how many
943     // items down in that sublevel
944
945     int sbl = (ee->x / menu.item_w), id = (ee->y / menu.item_h),
946       // next... figure out how many sublevels over the redraw spans
947       sbl_d = ((ee->x + ee->width) / menu.item_w),
948       // then we see how many items down to redraw
949       id_d = ((ee->y + ee->height) / menu.item_h);
950
951     if (id_d > menu.persub) id_d = menu.persub;
952
953     // draw the sublevels and the number of items the exposure spans
954     menuitemList::const_iterator it = menuitems.begin();
955     int i, ii;
956     for (i = sbl; i <= sbl_d; i++) {
957       // set the iterator to the first item in the sublevel needing redrawing
958       it = menuitems.begin() + (id + (i * menu.persub));
959       for (ii = id; ii <= id_d && it != menuitems.end(); it++, ii++) {
960         int index = ii + (i * menu.persub);
961         // redraw the item
962         drawItem(index, (which_sub == index), False,
963                  ee->x, ee->y, ee->width, ee->height);
964       }
965     }
966   }
967 }
968
969
970 void Basemenu::enterNotifyEvent(XCrossingEvent *ce) {
971   if (ce->window == menu.frame) {
972     menu.x_shift = menu.x, menu.y_shift = menu.y;
973     if (menu.x + menu.width > screen.size().w()) {
974       menu.x_shift = screen.size().w() - menu.width -
975         screen.getBorderWidth();
976       shifted = True;
977     } else if (menu.x < 0) {
978       menu.x_shift = -screen.getBorderWidth();
979       shifted = True;
980     }
981
982     if (menu.y + menu.height > screen.size().h()) {
983       menu.y_shift = screen.size().h() - menu.height -
984         screen.getBorderWidth();
985       shifted = True;
986     } else if (menu.y + (signed) menu.title_h < 0) {
987       menu.y_shift = -screen.getBorderWidth();
988       shifted = True;
989     }
990
991     if (shifted)
992       XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift);
993
994     if (which_sub != -1) {
995       BasemenuItem *tmp = menuitems[which_sub];
996       if (tmp->submenu()->isVisible()) {
997         int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h),
998           w = (sbl * menu.persub) + i;
999
1000         if (w != which_sub && (! tmp->submenu()->isTorn())) {
1001           tmp->submenu()->internal_hide();
1002
1003           drawItem(which_sub, False, True);
1004           which_sub = -1;
1005         }
1006       }
1007     }
1008   }
1009 }
1010
1011
1012 void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) {
1013   if (ce->window == menu.frame) {
1014     if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) {
1015       int p = (which_sbl * menu.persub) + which_press;
1016
1017       drawItem(p, (p == which_sub), True);
1018
1019       which_sbl = which_press = -1;
1020     }
1021
1022     if (shifted) {
1023       XMoveWindow(display, menu.window, menu.x, menu.y);
1024       shifted = False;
1025
1026       if (which_sub != -1) drawSubmenu(which_sub);
1027     }
1028   }
1029 }
1030
1031
1032 void Basemenu::reconfigure(void) {
1033   XSetWindowBackground(display, menu.window,
1034                        screen.getBorderColor()->getPixel());
1035   XSetWindowBorder(display, menu.window,
1036                    screen.getBorderColor()->getPixel());
1037   XSetWindowBorderWidth(display, menu.window, screen.getBorderWidth());
1038
1039   menu.bevel_w = screen.getBevelWidth();
1040   update();
1041 }