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