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