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