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