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