]> Dogcows Code - chaz/openbox/blob - src/Basemenu.cc
keep a minimum menu height so that the pixmaps (checkmarks) draw right in them
[chaz/openbox] / 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() < 9 ? 9 : style->f_font->height()) +
254 menu.bevel_w; // 9 for the menu pixmaps (checkmarks)
255 menu.title_h = style->t_font->height() + menu.bevel_w * 2;
256
257 if (title_vis) {
258 menu.item_w = screen->getMenuStyle()->t_font->measureString(menu.label) +
259 menu.bevel_w * 2;
260 } else {
261 menu.item_w = 1;
262 }
263
264 unsigned int ii = 0;
265 MenuItems::iterator it = menuitems.begin(), end = menuitems.end();
266 for (; it != end; ++it) {
267 ii = screen->getMenuStyle()->f_font->measureString((*it)->l) +
268 (menu.bevel_w * 2) + (menu.item_h * 2);
269
270 menu.item_w = ((menu.item_w < ii) ? ii : menu.item_w);
271 }
272
273 if (! menuitems.empty()) {
274 menu.sublevels = 1;
275
276 unsigned int menu_size = menuitems.size();
277 while (((menu.item_h * (menu_size + 1) / menu.sublevels)
278 + menu.title_h + screen->getBorderWidth()) >
279 screen->getHeight())
280 menu.sublevels++;
281
282 if (menu.sublevels < menu.minsub) menu.sublevels = menu.minsub;
283
284 menu.persub = menu_size / menu.sublevels;
285 if (menu_size % menu.sublevels) menu.persub++;
286 } else {
287 menu.sublevels = 0;
288 menu.persub = 0;
289 }
290
291 menu.width = (menu.sublevels * (menu.item_w));
292 if (! menu.width) menu.width = menu.item_w;
293
294 menu.frame_h = (menu.item_h * menu.persub);
295 menu.height = ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) +
296 menu.frame_h;
297 if (! menu.frame_h) menu.frame_h = 1;
298 if (menu.height < 1) menu.height = 1;
299
300 Pixmap tmp;
301 BTexture *texture;
302 if (title_vis) {
303 tmp = menu.title_pixmap;
304 texture = &(screen->getMenuStyle()->title);
305 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
306 menu.title_pixmap = None;
307 XSetWindowBackground(display, menu.title,
308 texture->color().pixel());
309 } else {
310 menu.title_pixmap =
311 image_ctrl->renderImage(menu.width, menu.title_h, *texture);
312 XSetWindowBackgroundPixmap(display, menu.title, menu.title_pixmap);
313 }
314 if (tmp) image_ctrl->removeImage(tmp);
315 XClearWindow(display, menu.title);
316 }
317
318 tmp = menu.frame_pixmap;
319 texture = &(screen->getMenuStyle()->frame);
320 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
321 menu.frame_pixmap = None;
322 XSetWindowBackground(display, menu.frame,
323 texture->color().pixel());
324 } else {
325 menu.frame_pixmap =
326 image_ctrl->renderImage(menu.width, menu.frame_h, *texture);
327 XSetWindowBackgroundPixmap(display, menu.frame, menu.frame_pixmap);
328 }
329 if (tmp) image_ctrl->removeImage(tmp);
330
331 tmp = menu.hilite_pixmap;
332 texture = &(screen->getMenuStyle()->hilite);
333 if (texture->texture() == (BTexture::Flat | BTexture::Solid)) {
334 menu.hilite_pixmap = None;
335 } else {
336 menu.hilite_pixmap =
337 image_ctrl->renderImage(menu.item_w, menu.item_h, *texture);
338 }
339 if (tmp) image_ctrl->removeImage(tmp);
340
341 XResizeWindow(display, menu.window, menu.width, menu.height);
342
343 if (title_vis)
344 XResizeWindow(display, menu.title, menu.width, menu.title_h);
345
346 XMoveResizeWindow(display, menu.frame, 0,
347 ((title_vis) ? menu.title_h +
348 screen->getBorderWidth() : 0), menu.width,
349 menu.frame_h);
350
351 XClearWindow(display, menu.window);
352 XClearWindow(display, menu.title);
353 XClearWindow(display, menu.frame);
354
355 if (title_vis && visible) redrawTitle();
356
357 const int menu_size = menuitems.size();
358 for (int i = 0; visible && i < menu_size; i++) {
359 if (i == which_sub) {
360 drawItem(i, True, 0);
361 drawSubmenu(i);
362 } else {
363 drawItem(i, False, 0);
364 }
365 }
366
367 if (parent && visible)
368 parent->drawSubmenu(parent->which_sub);
369
370 XMapSubwindows(display, menu.window);
371 }
372
373
374 void Basemenu::show(void) {
375 XMapSubwindows(display, menu.window);
376 XMapWindow(display, menu.window);
377 visible = True;
378
379 if (! parent) {
380 if (shown && (! shown->torn))
381 shown->hide();
382
383 shown = this;
384 }
385 }
386
387
388 void Basemenu::hide(void) {
389 if (! torn && hide_tree && parent && parent->isVisible()) {
390 Basemenu *p = parent;
391
392 while (p->isVisible() && ! p->torn && p->parent) p = p->parent;
393 p->internal_hide();
394 } else {
395 internal_hide();
396 }
397 }
398
399
400 void Basemenu::internal_hide(void) {
401 BasemenuItem *tmp = find(which_sub);
402 if (tmp)
403 tmp->submenu()->internal_hide();
404
405 if (parent && ! torn) {
406 parent->drawItem(parent->which_sub, False, True);
407
408 parent->which_sub = -1;
409 } else if (shown && shown->menu.window == menu.window) {
410 shown = (Basemenu *) 0;
411 }
412
413 torn = visible = False;
414 which_sub = which_press = which_sub = -1;
415
416 XUnmapWindow(display, menu.window);
417 }
418
419
420 void Basemenu::move(int x, int y) {
421 menu.x = x;
422 menu.y = y;
423 XMoveWindow(display, menu.window, x, y);
424 if (which_sub != -1)
425 drawSubmenu(which_sub);
426 }
427
428
429 void Basemenu::redrawTitle(void) {
430 const char *text = (! menu.label.empty()) ? getLabel() :
431 i18n(BasemenuSet, BasemenuBlackboxMenu, "Blackbox Menu");
432 int dx = menu.bevel_w;
433 unsigned int l;
434 MenuStyle *style = screen->getMenuStyle();
435
436 l = style->t_font->measureString(text) + menu.bevel_w * 2;
437
438 switch (screen->getMenuStyle()->t_justify) {
439 case RightJustify:
440 dx += menu.width - l;
441 break;
442
443 case CenterJustify:
444 dx += (menu.width - l) / 2;
445 break;
446
447 case LeftJustify:
448 default:
449 break;
450 }
451
452 style->t_font->drawString(menu.title, dx, menu.bevel_w,
453 style->t_text, text);
454 }
455
456
457 void Basemenu::drawSubmenu(int index) {
458 BasemenuItem *item = find(which_sub);
459 if (item && item->submenu() && ! item->submenu()->isTorn() &&
460 which_sub != index)
461 item->submenu()->internal_hide();
462
463 item = find(index);
464 if (! item)
465 return;
466 Basemenu *submenu = item->submenu();
467
468 if (submenu && visible && ! submenu->isTorn() && item->isEnabled()) {
469 if (submenu->parent != this) submenu->parent = this;
470 int sbl = index / menu.persub, i = index - (sbl * menu.persub),
471 x = menu.x + ((menu.item_w * (sbl + 1)) + screen->getBorderWidth()), y;
472
473 if (alignment == AlignTop) {
474 y = (((shifted) ? menu.y_shift : menu.y) +
475 ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) -
476 ((submenu->title_vis) ?
477 submenu->menu.title_h + screen->getBorderWidth() : 0));
478 } else {
479 y = (((shifted) ? menu.y_shift : menu.y) +
480 (menu.item_h * i) +
481 ((title_vis) ? menu.title_h + screen->getBorderWidth() : 0) -
482 ((submenu->title_vis) ?
483 submenu->menu.title_h + screen->getBorderWidth() : 0));
484 }
485
486 if (alignment == AlignBottom &&
487 (y + submenu->menu.height) > ((shifted) ? menu.y_shift :
488 menu.y) + menu.height)
489 y = (((shifted) ? menu.y_shift : menu.y) +
490 menu.height - submenu->menu.height);
491
492 if ((x + submenu->getWidth()) > screen->getWidth())
493 x = ((shifted) ? menu.x_shift : menu.x) -
494 submenu->getWidth() - screen->getBorderWidth();
495
496 if (x < 0) x = 0;
497
498 if ((y + submenu->getHeight()) > screen->getHeight())
499 y = screen->getHeight() - submenu->getHeight() -
500 (screen->getBorderWidth() * 2);
501 if (y < 0) y = 0;
502
503 submenu->move(x, y);
504 if (! moving) drawItem(index, True);
505
506 if (! submenu->isVisible())
507 submenu->show();
508 submenu->moving = moving;
509 which_sub = index;
510 } else {
511 which_sub = -1;
512 }
513 }
514
515
516 bool Basemenu::hasSubmenu(int index) {
517 BasemenuItem *item = find(index);
518 if (item && item->submenu())
519 return True;
520 return False;
521 }
522
523
524 void Basemenu::drawItem(int index, bool highlight, bool clear,
525 int x, int y, unsigned int w, unsigned int h) {
526 BasemenuItem *item = find(index);
527 if (! item) return;
528
529 bool dotext = True, dohilite = True, dosel = True;
530 const char *text = item->label();
531 int sbl = index / menu.persub, i = index - (sbl * menu.persub);
532 int item_x = (sbl * menu.item_w), item_y = (i * menu.item_h);
533 int hilite_x = item_x, hilite_y = item_y, hoff_x = 0, hoff_y = 0;
534 int text_x = 0, text_y = 0, sel_x = 0, sel_y = 0;
535 unsigned int hilite_w = menu.item_w, hilite_h = menu.item_h, text_w = 0,
536 text_h = 0;
537 unsigned int half_w = menu.item_h / 2, quarter_w = menu.item_h / 4;
538
539 if (text) {
540 text_w = screen->getMenuStyle()->f_font->measureString(text);
541 text_y = item_y + menu.bevel_w / 2;
542
543 switch(screen->getMenuStyle()->f_justify) {
544 case LeftJustify:
545 text_x = item_x + menu.bevel_w + menu.item_h + 1;
546 break;
547
548 case RightJustify:
549 text_x = item_x + menu.item_w - (menu.item_h + menu.bevel_w + text_w);
550 break;
551
552 case CenterJustify:
553 text_x = item_x + ((menu.item_w + 1 - text_w) / 2);
554 break;
555 }
556
557 text_h = menu.item_h - menu.bevel_w;
558 }
559
560 MenuStyle *style = screen->getMenuStyle();
561 BPen hipen(style->hilite.color());
562 // match the text color
563 BPen pen((highlight ? style->h_text :
564 (item->isEnabled() ? style->f_text :
565 style->d_text)));
566
567
568 sel_x = item_x;
569 if (screen->getMenuStyle()->bullet_pos == Right)
570 sel_x += (menu.item_w - menu.item_h - menu.bevel_w);
571 sel_x += quarter_w;
572 sel_y = item_y + quarter_w;
573
574 if (clear) {
575 XClearArea(display, menu.frame, item_x, item_y, menu.item_w, menu.item_h,
576 False);
577 } else if (! (x == y && y == -1 && w == h && h == 0)) {
578 // calculate the which part of the hilite to redraw
579 if (! (max(item_x, x) <= min<signed>(item_x + menu.item_w, x + w) &&
580 max(item_y, y) <= min<signed>(item_y + menu.item_h, y + h))) {
581 dohilite = False;
582 } else {
583 hilite_x = max(item_x, x);
584 hilite_y = max(item_y, y);
585 hilite_w = min(item_x + menu.item_w, x + w) - hilite_x;
586 hilite_h = min(item_y + menu.item_h, y + h) - hilite_y;
587 hoff_x = hilite_x % menu.item_w;
588 hoff_y = hilite_y % menu.item_h;
589 }
590
591 // check if we need to redraw the text
592 int text_ry = item_y + (menu.bevel_w / 2);
593 if (! (max(text_x, x) <= min<signed>(text_x + text_w, x + w) &&
594 max(text_ry, y) <= min<signed>(text_ry + text_h, y + h)))
595 dotext = False;
596
597 // check if we need to redraw the select pixmap/menu bullet
598 if (! (max(sel_x, x) <= min<signed>(sel_x + half_w, x + w) &&
599 max(sel_y, y) <= min<signed>(sel_y + half_w, y + h)))
600 dosel = False;
601 }
602
603 if (dohilite && highlight && (menu.hilite_pixmap != ParentRelative)) {
604 if (menu.hilite_pixmap)
605 XCopyArea(display, menu.hilite_pixmap, menu.frame,
606 hipen.gc(), hoff_x, hoff_y,
607 hilite_w, hilite_h, hilite_x, hilite_y);
608 else
609 XFillRectangle(display, menu.frame, hipen.gc(),
610 hilite_x, hilite_y, hilite_w, hilite_h);
611 }
612
613 if (dosel && item->isSelected()) {
614 XPoint pts[6];
615
616 // put the check mark on the opposite side of the menu
617 int x = sel_x;
618 if (screen->getMenuStyle()->bullet_pos == Right)
619 x -= (menu.item_w - menu.item_h - menu.bevel_w);
620 else
621 x += (menu.item_w - menu.item_h - menu.bevel_w);
622
623 pts[0].x = x + 0;
624 pts[0].y = sel_y + 2;
625
626 pts[1].x = 0;
627 pts[1].y = 3;
628
629 pts[2].x = 2;
630 pts[2].y = 3;
631
632 pts[3].x = 5;
633 pts[3].y = -5;
634
635
636 pts[4].x = 0;
637 pts[4].y = -3;
638
639 pts[5].x = -5;
640 pts[5].y = 5;
641
642 XFillPolygon(display, menu.frame, pen.gc(), pts, 6, Nonconvex,
643 CoordModePrevious);
644 }
645
646 if (dotext && text) {
647 style->f_font->drawString(menu.frame, text_x, text_y,
648 (highlight ? style->h_text :
649 (item->isEnabled() ? style->f_text :
650 style->d_text)),
651 text);
652 }
653
654 if (dosel && item->submenu()) {
655 const int bullet_size = 3;
656
657 switch (screen->getMenuStyle()->bullet) {
658 case Square:
659 XDrawRectangle(display, menu.frame, pen.gc(), sel_x, sel_y,
660 bullet_size * 2, bullet_size * 2);
661 break;
662
663 case Triangle:
664 XPoint tri[3];
665
666 if (screen->getMenuStyle()->bullet_pos == Right) {
667 tri[0].x = sel_x + quarter_w - bullet_size;
668 tri[0].y = sel_y + quarter_w - bullet_size;
669 tri[1].x = 2 * bullet_size;
670 tri[1].y = bullet_size;
671 tri[2].x = -(2 * bullet_size);
672 tri[2].y = bullet_size;
673 } else {
674 tri[0].x = sel_x + quarter_w - bullet_size;
675 tri[0].y = item_y + half_w;
676 tri[1].x = 2 * bullet_size;
677 tri[1].y = bullet_size;
678 tri[2].x = 0;
679 tri[2].y = -(2 * bullet_size);
680 }
681
682 XFillPolygon(display, menu.frame, pen.gc(), tri, 3, Convex,
683 CoordModePrevious);
684 break;
685
686 case Diamond:
687 XPoint dia[4];
688
689 dia[0].x = sel_x + quarter_w - bullet_size;
690 dia[0].y = item_y + half_w;
691 dia[1].x = bullet_size;
692 dia[1].y = -bullet_size;
693 dia[2].x = bullet_size;
694 dia[2].y = bullet_size;
695 dia[3].x = -bullet_size;
696 dia[3].y = bullet_size;
697
698 XFillPolygon(display, menu.frame, pen.gc(), dia, 4, Convex,
699 CoordModePrevious);
700 break;
701 }
702 }
703 }
704
705
706 void Basemenu::setLabel(const string& label) {
707 menu.label = label;
708 }
709
710
711 void Basemenu::setItemSelected(int index, bool sel) {
712 assert(index >= 0);
713 BasemenuItem *item = find(index);
714 if (! item) return;
715
716 item->setSelected(sel);
717 if (visible) drawItem(index, (index == which_sub), True);
718 }
719
720
721 bool Basemenu::isItemSelected(int index) {
722 assert(index >= 0);
723 BasemenuItem *item = find(index);
724 if (! item) return False;
725
726 return item->isSelected();
727 }
728
729
730 void Basemenu::setItemEnabled(int index, bool enable) {
731 assert(index >= 0);
732 BasemenuItem *item = find(index);
733 if (! item) return;
734
735 item->setEnabled(enable);
736 if (visible) drawItem(index, (index == which_sub), True);
737 }
738
739
740 bool Basemenu::isItemEnabled(int index) {
741 assert(index >= 0);
742 BasemenuItem *item = find(index);
743 if (! item) return False;
744
745 return item->isEnabled();
746 }
747
748
749 void Basemenu::buttonPressEvent(XButtonEvent *be) {
750 if (be->window == menu.frame) {
751 int sbl = (be->x / menu.item_w), i = (be->y / menu.item_h);
752 int w = (sbl * menu.persub) + i;
753
754 BasemenuItem *item = find(w);
755 if (item) {
756 which_press = i;
757 which_sbl = sbl;
758
759
760 if (item->submenu())
761 drawSubmenu(w);
762 else
763 drawItem(w, (item->isEnabled()), True);
764 }
765 } else {
766 menu.x_move = be->x_root - menu.x;
767 menu.y_move = be->y_root - menu.y;
768 }
769 }
770
771
772 void Basemenu::buttonReleaseEvent(XButtonEvent *re) {
773 if (re->window == menu.title) {
774 if (moving) {
775 moving = False;
776
777 if (which_sub != -1)
778 drawSubmenu(which_sub);
779 }
780
781 if (re->x >= 0 && re->x <= static_cast<signed>(menu.width) &&
782 re->y >= 0 && re->y <= static_cast<signed>(menu.title_h))
783 if (re->button == 3)
784 hide();
785 } else if (re->window == menu.frame &&
786 re->x >= 0 && re->x < static_cast<signed>(menu.width) &&
787 re->y >= 0 && re->y < static_cast<signed>(menu.frame_h)) {
788 if (re->button == 3) {
789 hide();
790 } else {
791 int sbl = (re->x / menu.item_w), i = (re->y / menu.item_h),
792 ix = sbl * menu.item_w, iy = i * menu.item_h,
793 w = (sbl * menu.persub) + i,
794 p = (which_sbl * menu.persub) + which_press;
795
796 if (w >= 0 && w < static_cast<signed>(menuitems.size())) {
797 drawItem(p, (p == which_sub), True);
798
799 if (p == w && isItemEnabled(w)) {
800 if (re->x > ix && re->x < static_cast<signed>(ix + menu.item_w) &&
801 re->y > iy && re->y < static_cast<signed>(iy + menu.item_h)) {
802 itemSelected(re->button, w);
803 }
804 }
805 } else {
806 drawItem(p, False, True);
807 }
808 }
809 }
810 }
811
812
813 void Basemenu::motionNotifyEvent(XMotionEvent *me) {
814 if (me->window == menu.title && (me->state & Button1Mask)) {
815 if (movable) {
816 if (! moving) {
817 if (parent && ! torn) {
818 parent->drawItem(parent->which_sub, False, True);
819 parent->which_sub = -1;
820 }
821
822 moving = torn = True;
823
824 if (which_sub != -1)
825 drawSubmenu(which_sub);
826 } else {
827 menu.x = me->x_root - menu.x_move,
828 menu.y = me->y_root - menu.y_move;
829
830 XMoveWindow(display, menu.window, menu.x, menu.y);
831
832 if (which_sub != -1)
833 drawSubmenu(which_sub);
834 }
835 }
836 } else if (! (me->state & Button1Mask) && me->window == menu.frame &&
837 me->x >= 0 && me->x < static_cast<signed>(menu.width) &&
838 me->y >= 0 && me->y < static_cast<signed>(menu.frame_h)) {
839 int sbl = (me->x / menu.item_w), i = (me->y / menu.item_h),
840 w = (sbl * menu.persub) + i;
841
842 if ((i != which_press || sbl != which_sbl) &&
843 (w >= 0 && w < static_cast<signed>(menuitems.size()))) {
844 if (which_press != -1 && which_sbl != -1) {
845 int p = (which_sbl * menu.persub) + which_press;
846 BasemenuItem *item = find(p);
847
848 drawItem(p, False, True);
849 if (item->submenu())
850 if (item->submenu()->isVisible() &&
851 ! item->submenu()->isTorn()) {
852 item->submenu()->internal_hide();
853 which_sub = -1;
854 }
855 }
856
857 which_press = i;
858 which_sbl = sbl;
859
860 BasemenuItem *itmp = find(w);
861
862 if (itmp->submenu())
863 drawSubmenu(w);
864 else
865 drawItem(w, (itmp->isEnabled()), True);
866 }
867 }
868 }
869
870
871 void Basemenu::exposeEvent(XExposeEvent *ee) {
872 if (ee->window == menu.title) {
873 redrawTitle();
874 } else if (ee->window == menu.frame) {
875 // this is a compilicated algorithm... lets do it step by step...
876 // first... we see in which sub level the expose starts... and how many
877 // items down in that sublevel
878
879 int sbl = (ee->x / menu.item_w), id = (ee->y / menu.item_h),
880 // next... figure out how many sublevels over the redraw spans
881 sbl_d = ((ee->x + ee->width) / menu.item_w),
882 // then we see how many items down to redraw
883 id_d = ((ee->y + ee->height) / menu.item_h);
884
885 if (id_d > menu.persub) id_d = menu.persub;
886
887 // draw the sublevels and the number of items the exposure spans
888 MenuItems::iterator it,
889 end = menuitems.end();
890 int i, ii;
891 for (i = sbl; i <= sbl_d; i++) {
892 // set the iterator to the first item in the sublevel needing redrawing
893 it = menuitems.begin() + (id + (i * menu.persub));
894 for (ii = id; ii <= id_d && it != end; ++it, ii++) {
895 int index = ii + (i * menu.persub);
896 // redraw the item
897 drawItem(index, (which_sub == index), False,
898 ee->x, ee->y, ee->width, ee->height);
899 }
900 }
901 }
902 }
903
904
905 void Basemenu::enterNotifyEvent(XCrossingEvent *ce) {
906 if (ce->window == menu.frame) {
907 menu.x_shift = menu.x, menu.y_shift = menu.y;
908 if (menu.x + menu.width > screen->getWidth()) {
909 menu.x_shift = screen->getWidth() - menu.width -
910 screen->getBorderWidth();
911 shifted = True;
912 } else if (menu.x < 0) {
913 menu.x_shift = -screen->getBorderWidth();
914 shifted = True;
915 }
916
917 if (menu.y + menu.height > screen->getHeight()) {
918 menu.y_shift = screen->getHeight() - menu.height -
919 screen->getBorderWidth();
920 shifted = True;
921 } else if (menu.y + static_cast<signed>(menu.title_h) < 0) {
922 menu.y_shift = -screen->getBorderWidth();
923 shifted = True;
924 }
925
926 if (shifted)
927 XMoveWindow(display, menu.window, menu.x_shift, menu.y_shift);
928
929 if (which_sub != -1) {
930 BasemenuItem *tmp = find(which_sub);
931 if (tmp->submenu()->isVisible()) {
932 int sbl = (ce->x / menu.item_w), i = (ce->y / menu.item_h),
933 w = (sbl * menu.persub) + i;
934
935 if (w != which_sub && ! tmp->submenu()->isTorn()) {
936 tmp->submenu()->internal_hide();
937
938 drawItem(which_sub, False, True);
939 which_sub = -1;
940 }
941 }
942 }
943 }
944 }
945
946
947 void Basemenu::leaveNotifyEvent(XCrossingEvent *ce) {
948 if (ce->window == menu.frame) {
949 if (which_press != -1 && which_sbl != -1 && menuitems.size() > 0) {
950 int p = (which_sbl * menu.persub) + which_press;
951
952 drawItem(p, (p == which_sub), True);
953
954 which_sbl = which_press = -1;
955 }
956
957 if (shifted) {
958 XMoveWindow(display, menu.window, menu.x, menu.y);
959 shifted = False;
960
961 if (which_sub != -1) drawSubmenu(which_sub);
962 }
963 }
964 }
965
966
967 void Basemenu::reconfigure(void) {
968 XSetWindowBackground(display, menu.window,
969 screen->getBorderColor()->pixel());
970 XSetWindowBorder(display, menu.window,
971 screen->getBorderColor()->pixel());
972 XSetWindowBorderWidth(display, menu.window, screen->getBorderWidth());
973
974 menu.bevel_w = screen->getBevelWidth();
975 update();
976 }
977
978
979 void Basemenu::changeItemLabel(unsigned int index, const string& label) {
980 BasemenuItem *item = find(index);
981 assert(item);
982 item->newLabel(label);
983 }
This page took 0.076832 seconds and 5 git commands to generate.