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