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