]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
make the menu headers show their correct text
[chaz/openbox] / openbox / menuframe.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3 menuframe.c for the Openbox window manager
4 Copyright (c) 2006 Mikael Magnusson
5 Copyright (c) 2003-2007 Dana Jansens
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "menuframe.h"
21 #include "client.h"
22 #include "menu.h"
23 #include "screen.h"
24 #include "actions.h"
25 #include "grab.h"
26 #include "openbox.h"
27 #include "mainloop.h"
28 #include "config.h"
29 #include "render/theme.h"
30
31 #define PADDING 2
32 #define SEPARATOR_HEIGHT 3
33 #define MAX_MENU_WIDTH 400
34
35 #define ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
36
37 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
38 LeaveWindowMask)
39 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
40 ButtonPressMask | ButtonReleaseMask)
41
42 GList *menu_frame_visible;
43
44 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
45 ObMenuFrame *frame);
46 static void menu_entry_frame_free(ObMenuEntryFrame *self);
47 static void menu_frame_update(ObMenuFrame *self);
48 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
49 static void menu_frame_hide(ObMenuFrame *self);
50
51 static Window createWindow(Window parent, gulong mask,
52 XSetWindowAttributes *attrib)
53 {
54 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
55 RrDepth(ob_rr_inst), InputOutput,
56 RrVisual(ob_rr_inst), mask, attrib);
57 }
58
59 GHashTable *menu_frame_map;
60
61 void menu_frame_startup(gboolean reconfig)
62 {
63 if (reconfig) return;
64
65 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
66 }
67
68 void menu_frame_shutdown(gboolean reconfig)
69 {
70 if (reconfig) return;
71
72 g_hash_table_destroy(menu_frame_map);
73 }
74
75 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
76 {
77 ObMenuFrame *self;
78 XSetWindowAttributes attr;
79
80 self = g_new0(ObMenuFrame, 1);
81 self->type = Window_Menu;
82 self->menu = menu;
83 self->selected = NULL;
84 self->client = client;
85 self->direction_right = TRUE;
86 self->show_from = show_from;
87
88 attr.event_mask = FRAME_EVENTMASK;
89 self->window = createWindow(RootWindow(ob_display, ob_screen),
90 CWEventMask, &attr);
91
92 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
93 XSetWindowBorder(ob_display, self->window,
94 RrColorPixel(ob_rr_theme->menu_border_color));
95
96 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
97
98 stacking_add(MENU_AS_WINDOW(self));
99
100 return self;
101 }
102
103 void menu_frame_free(ObMenuFrame *self)
104 {
105 if (self) {
106 while (self->entries) {
107 menu_entry_frame_free(self->entries->data);
108 self->entries = g_list_delete_link(self->entries, self->entries);
109 }
110
111 stacking_remove(MENU_AS_WINDOW(self));
112
113 RrAppearanceFree(self->a_items);
114
115 XDestroyWindow(ob_display, self->window);
116
117 g_free(self);
118 }
119 }
120
121 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
122 ObMenuFrame *frame)
123 {
124 ObMenuEntryFrame *self;
125 XSetWindowAttributes attr;
126
127 self = g_new0(ObMenuEntryFrame, 1);
128 self->entry = entry;
129 self->frame = frame;
130
131 menu_entry_ref(entry);
132
133 attr.event_mask = ENTRY_EVENTMASK;
134 self->window = createWindow(self->frame->window, CWEventMask, &attr);
135 self->text = createWindow(self->window, 0, NULL);
136 g_hash_table_insert(menu_frame_map, &self->window, self);
137 g_hash_table_insert(menu_frame_map, &self->text, self);
138 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
139 self->icon = createWindow(self->window, 0, NULL);
140 g_hash_table_insert(menu_frame_map, &self->icon, self);
141 }
142 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
143 self->bullet = createWindow(self->window, 0, NULL);
144 g_hash_table_insert(menu_frame_map, &self->bullet, self);
145 }
146
147 XMapWindow(ob_display, self->window);
148 XMapWindow(ob_display, self->text);
149
150 return self;
151 }
152
153 static void menu_entry_frame_free(ObMenuEntryFrame *self)
154 {
155 if (self) {
156 menu_entry_unref(self->entry);
157
158 XDestroyWindow(ob_display, self->text);
159 XDestroyWindow(ob_display, self->window);
160 g_hash_table_remove(menu_frame_map, &self->text);
161 g_hash_table_remove(menu_frame_map, &self->window);
162 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
163 XDestroyWindow(ob_display, self->icon);
164 g_hash_table_remove(menu_frame_map, &self->icon);
165 }
166 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
167 XDestroyWindow(ob_display, self->bullet);
168 g_hash_table_remove(menu_frame_map, &self->bullet);
169 }
170
171 g_free(self);
172 }
173 }
174
175 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
176 {
177 RECT_SET_POINT(self->area, x, y);
178 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
179 }
180
181 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
182 {
183 gint dx, dy;
184
185 if (config_menu_middle) {
186 gint myx;
187
188 myx = *x;
189 *y -= self->area.height / 2;
190
191 /* try to the right of the cursor */
192 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
193 self->direction_right = TRUE;
194 if (dx != 0) {
195 /* try to the left of the cursor */
196 myx = *x - self->area.width;
197 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
198 self->direction_right = FALSE;
199 }
200 if (dx != 0) {
201 /* if didnt fit on either side so just use what it says */
202 myx = *x;
203 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
204 self->direction_right = TRUE;
205 }
206 *x = myx + dx;
207 *y += dy;
208 } else {
209 gint myx, myy;
210
211 myx = *x;
212 myy = *y;
213
214 /* try to the bottom right of the cursor */
215 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
216 self->direction_right = TRUE;
217 if (dx != 0 || dy != 0) {
218 /* try to the bottom left of the cursor */
219 myx = *x - self->area.width;
220 myy = *y;
221 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
222 self->direction_right = FALSE;
223 }
224 if (dx != 0 || dy != 0) {
225 /* try to the top right of the cursor */
226 myx = *x;
227 myy = *y - self->area.height;
228 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
229 self->direction_right = TRUE;
230 }
231 if (dx != 0 || dy != 0) {
232 /* try to the top left of the cursor */
233 myx = *x - self->area.width;
234 myy = *y - self->area.height;
235 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
236 self->direction_right = FALSE;
237 }
238 if (dx != 0 || dy != 0) {
239 /* if didnt fit on either side so just use what it says */
240 myx = *x;
241 myy = *y;
242 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
243 self->direction_right = TRUE;
244 }
245 *x = myx + dx;
246 *y = myy + dy;
247 }
248 }
249
250 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
251 {
252 gint overlap;
253 gint bwidth;
254
255 overlap = ob_rr_theme->menu_overlap;
256 bwidth = ob_rr_theme->mbwidth;
257
258 if (self->direction_right)
259 *x = self->parent->area.x + self->parent->area.width -
260 overlap - bwidth;
261 else
262 *x = self->parent->area.x - self->area.width + overlap + bwidth;
263
264 *y = self->parent->area.y + self->parent_entry->area.y;
265 if (config_menu_middle)
266 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
267 else
268 *y += overlap;
269 }
270
271 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
272 gint *dx, gint *dy)
273 {
274 Rect *a = NULL;
275 gint pos, half;
276
277 *dx = *dy = 0;
278
279 a = screen_physical_area_monitor(self->monitor);
280
281 half = g_list_length(self->entries) / 2;
282 pos = g_list_index(self->entries, self->selected);
283
284 /* if in the bottom half then check this stuff first, will keep the bottom
285 edge of the menu visible */
286 if (pos > half) {
287 *dx = MAX(*dx, a->x - x);
288 *dy = MAX(*dy, a->y - y);
289 }
290 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
291 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
292 /* if in the top half then check this stuff last, will keep the top
293 edge of the menu visible */
294 if (pos <= half) {
295 *dx = MAX(*dx, a->x - x);
296 *dy = MAX(*dy, a->y - y);
297 }
298
299 g_free(a);
300 }
301
302 static void menu_entry_frame_render(ObMenuEntryFrame *self)
303 {
304 RrAppearance *item_a, *text_a;
305 gint th; /* temp */
306 ObMenu *sub;
307 ObMenuFrame *frame = self->frame;
308
309 switch (self->entry->type) {
310 case OB_MENU_ENTRY_TYPE_NORMAL:
311 case OB_MENU_ENTRY_TYPE_SUBMENU:
312 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
313 !self->entry->data.normal.enabled ?
314 /* disabled */
315 (self == self->frame->selected ?
316 ob_rr_theme->a_menu_disabled_selected :
317 ob_rr_theme->a_menu_disabled) :
318 /* enabled */
319 (self == self->frame->selected ?
320 ob_rr_theme->a_menu_selected :
321 ob_rr_theme->a_menu_normal));
322 th = ITEM_HEIGHT;
323 break;
324 case OB_MENU_ENTRY_TYPE_SEPARATOR:
325 if (self->entry->data.separator.label) {
326 item_a = ob_rr_theme->a_menu_title;
327 th = ob_rr_theme->menu_title_height;
328 } else {
329 item_a = ob_rr_theme->a_menu_normal;
330 th = SEPARATOR_HEIGHT + 2*PADDING;
331 }
332 break;
333 default:
334 g_assert_not_reached();
335 }
336 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
337 XResizeWindow(ob_display, self->window,
338 self->area.width, self->area.height);
339 item_a->surface.parent = self->frame->a_items;
340 item_a->surface.parentx = self->area.x;
341 item_a->surface.parenty = self->area.y;
342 RrPaint(item_a, self->window, self->area.width, self->area.height);
343
344 switch (self->entry->type) {
345 case OB_MENU_ENTRY_TYPE_NORMAL:
346 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
347 !self->entry->data.normal.enabled ?
348 /* disabled */
349 (self == self->frame->selected ?
350 ob_rr_theme->a_menu_text_disabled_selected :
351 ob_rr_theme->a_menu_text_disabled) :
352 /* enabled */
353 (self == self->frame->selected ?
354 ob_rr_theme->a_menu_text_selected :
355 ob_rr_theme->a_menu_text_normal));
356 text_a->texture[0].data.text.string = self->entry->data.normal.label;
357 if (self->entry->data.normal.shortcut &&
358 (self->frame->menu->show_all_shortcuts ||
359 self->entry->data.normal.shortcut_always_show ||
360 self->entry->data.normal.shortcut_position > 0))
361 {
362 text_a->texture[0].data.text.shortcut = TRUE;
363 text_a->texture[0].data.text.shortcut_pos =
364 self->entry->data.normal.shortcut_position;
365 } else
366 text_a->texture[0].data.text.shortcut = FALSE;
367 break;
368 case OB_MENU_ENTRY_TYPE_SUBMENU:
369 text_a = (self == self->frame->selected ?
370 ob_rr_theme->a_menu_text_selected :
371 ob_rr_theme->a_menu_text_normal);
372 sub = self->entry->data.submenu.submenu;
373 text_a->texture[0].data.text.string = sub ? sub->title : "";
374 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
375 sub->shortcut_always_show ||
376 sub->shortcut_position > 0))
377 {
378 text_a->texture[0].data.text.shortcut = TRUE;
379 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
380 } else
381 text_a->texture[0].data.text.shortcut = FALSE;
382 break;
383 case OB_MENU_ENTRY_TYPE_SEPARATOR:
384 if (self->entry->data.separator.label != NULL) {
385 text_a = ob_rr_theme->a_menu_text_title;
386 text_a->texture[0].data.text.string =
387 self->entry->data.separator.label;
388 }
389 else
390 text_a = ob_rr_theme->a_menu_text_normal;
391 break;
392 }
393
394 switch (self->entry->type) {
395 case OB_MENU_ENTRY_TYPE_NORMAL:
396 XMoveResizeWindow(ob_display, self->text,
397 self->frame->text_x, PADDING,
398 self->frame->text_w,
399 ITEM_HEIGHT - 2*PADDING);
400 text_a->surface.parent = item_a;
401 text_a->surface.parentx = self->frame->text_x;
402 text_a->surface.parenty = PADDING;
403 RrPaint(text_a, self->text, self->frame->text_w,
404 ITEM_HEIGHT - 2*PADDING);
405 break;
406 case OB_MENU_ENTRY_TYPE_SUBMENU:
407 XMoveResizeWindow(ob_display, self->text,
408 self->frame->text_x, PADDING,
409 self->frame->text_w - ITEM_HEIGHT,
410 ITEM_HEIGHT - 2*PADDING);
411 text_a->surface.parent = item_a;
412 text_a->surface.parentx = self->frame->text_x;
413 text_a->surface.parenty = PADDING;
414 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
415 ITEM_HEIGHT - 2*PADDING);
416 break;
417 case OB_MENU_ENTRY_TYPE_SEPARATOR:
418 if (self->entry->data.separator.label != NULL) {
419 /* labeled separator */
420 XMoveResizeWindow(ob_display, self->text,
421 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
422 self->area.width - 2*ob_rr_theme->paddingx,
423 ob_rr_theme->menu_title_height -
424 2*ob_rr_theme->paddingy);
425 text_a->surface.parent = item_a;
426 text_a->surface.parentx = ob_rr_theme->paddingx;
427 text_a->surface.parenty = ob_rr_theme->paddingy;
428 RrPaint(text_a, self->text,
429 self->area.width - 2*ob_rr_theme->paddingx,
430 ob_rr_theme->menu_title_height -
431 2*ob_rr_theme->paddingy);
432 } else {
433 RrAppearance *clear;
434
435 /* unlabeled separaator */
436 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
437 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
438
439 clear = ob_rr_theme->a_clear_tex;
440 clear->texture[0].type = RR_TEXTURE_LINE_ART;
441 clear->surface.parent = item_a;
442 clear->surface.parentx = PADDING;
443 clear->surface.parenty = PADDING;
444 clear->texture[0].data.lineart.color =
445 text_a->texture[0].data.text.color;
446 clear->texture[0].data.lineart.x1 = 2*PADDING;
447 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
448 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
449 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
450 RrPaint(clear, self->text,
451 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
452 }
453 break;
454 }
455
456 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
457 self->entry->data.normal.icon_data)
458 {
459 RrAppearance *clear;
460
461 XMoveResizeWindow(ob_display, self->icon,
462 PADDING, frame->item_margin.top,
463 ITEM_HEIGHT - frame->item_margin.top
464 - frame->item_margin.bottom,
465 ITEM_HEIGHT - frame->item_margin.top
466 - frame->item_margin.bottom);
467
468 clear = ob_rr_theme->a_clear_tex;
469 clear->texture[0].type = RR_TEXTURE_RGBA;
470 clear->texture[0].data.rgba.width =
471 self->entry->data.normal.icon_width;
472 clear->texture[0].data.rgba.height =
473 self->entry->data.normal.icon_height;
474 clear->texture[0].data.rgba.alpha =
475 self->entry->data.normal.icon_alpha;
476 clear->texture[0].data.rgba.data =
477 self->entry->data.normal.icon_data;
478 clear->surface.parent = item_a;
479 clear->surface.parentx = PADDING;
480 clear->surface.parenty = frame->item_margin.top;
481 RrPaint(clear, self->icon,
482 ITEM_HEIGHT - frame->item_margin.top
483 - frame->item_margin.bottom,
484 ITEM_HEIGHT - frame->item_margin.top
485 - frame->item_margin.bottom);
486 XMapWindow(ob_display, self->icon);
487 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
488 self->entry->data.normal.mask)
489 {
490 RrColor *c;
491 RrAppearance *clear;
492
493 XMoveResizeWindow(ob_display, self->icon,
494 PADDING, frame->item_margin.top,
495 ITEM_HEIGHT - frame->item_margin.top
496 - frame->item_margin.bottom,
497 ITEM_HEIGHT - frame->item_margin.top
498 - frame->item_margin.bottom);
499
500 clear = ob_rr_theme->a_clear_tex;
501 clear->texture[0].type = RR_TEXTURE_MASK;
502 clear->texture[0].data.mask.mask =
503 self->entry->data.normal.mask;
504
505 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
506 !self->entry->data.normal.enabled ?
507 /* disabled */
508 (self == self->frame->selected ?
509 self->entry->data.normal.mask_disabled_selected_color :
510 self->entry->data.normal.mask_disabled_color) :
511 /* enabled */
512 (self == self->frame->selected ?
513 self->entry->data.normal.mask_selected_color :
514 self->entry->data.normal.mask_normal_color));
515 clear->texture[0].data.mask.color = c;
516
517 clear->surface.parent = item_a;
518 clear->surface.parentx = PADDING;
519 clear->surface.parenty = frame->item_margin.top;
520 RrPaint(clear, self->icon,
521 ITEM_HEIGHT - frame->item_margin.top
522 - frame->item_margin.bottom,
523 ITEM_HEIGHT - frame->item_margin.top
524 - frame->item_margin.bottom);
525 XMapWindow(ob_display, self->icon);
526 } else
527 XUnmapWindow(ob_display, self->icon);
528
529 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
530 RrAppearance *bullet_a;
531 XMoveResizeWindow(ob_display, self->bullet,
532 self->frame->text_x + self->frame->text_w -
533 ITEM_HEIGHT + PADDING, PADDING,
534 ITEM_HEIGHT - 2*PADDING,
535 ITEM_HEIGHT - 2*PADDING);
536 bullet_a = (self == self->frame->selected ?
537 ob_rr_theme->a_menu_bullet_selected :
538 ob_rr_theme->a_menu_bullet_normal);
539 bullet_a->surface.parent = item_a;
540 bullet_a->surface.parentx =
541 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
542 bullet_a->surface.parenty = PADDING;
543 RrPaint(bullet_a, self->bullet,
544 ITEM_HEIGHT - 2*PADDING,
545 ITEM_HEIGHT - 2*PADDING);
546 XMapWindow(ob_display, self->bullet);
547 } else
548 XUnmapWindow(ob_display, self->bullet);
549
550 XFlush(ob_display);
551 }
552
553 /*! this code is taken from the menu_frame_render. if that changes, this won't
554 work.. */
555 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
556 gboolean first_entry,
557 gboolean last_entry)
558 {
559 ObMenuEntryType t;
560 gint h = 0;
561
562 h += 2*PADDING;
563
564 if (self)
565 t = self->entry->type;
566 else
567 /* this is the More... entry, it's NORMAL type */
568 t = OB_MENU_ENTRY_TYPE_NORMAL;
569
570 switch (t) {
571 case OB_MENU_ENTRY_TYPE_NORMAL:
572 case OB_MENU_ENTRY_TYPE_SUBMENU:
573 h += ob_rr_theme->menu_font_height;
574 break;
575 case OB_MENU_ENTRY_TYPE_SEPARATOR:
576 if (self->entry->data.separator.label != NULL) {
577 h += ob_rr_theme->menu_title_height +
578 (ob_rr_theme->mbwidth - PADDING) * 2;
579
580 /* if the first entry is a labeled separator, then make its border
581 overlap with the menu's outside border */
582 if (first_entry)
583 h -= ob_rr_theme->mbwidth;
584 /* if the last entry is a labeled separator, then make its border
585 overlap with the menu's outside border */
586 if (last_entry)
587 h -= ob_rr_theme->mbwidth;
588 } else {
589 h += SEPARATOR_HEIGHT;
590 }
591 break;
592 }
593
594 return h;
595 }
596
597 void menu_frame_render(ObMenuFrame *self)
598 {
599 gint w = 0, h = 0;
600 gint tw, th; /* temps */
601 GList *it;
602 gboolean has_icon = FALSE;
603 ObMenu *sub;
604 ObMenuEntryFrame *e;
605
606 /* find text dimensions */
607
608 STRUT_SET(self->item_margin, 0, 0, 0, 0);
609
610 if (self->entries) {
611 gint l, t, r, b;
612
613 e = self->entries->data;
614 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
615 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
616 tw += 2*PADDING;
617
618 th = ITEM_HEIGHT;
619
620 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
621 STRUT_SET(self->item_margin,
622 MAX(self->item_margin.left, l),
623 MAX(self->item_margin.top, t),
624 MAX(self->item_margin.right, r),
625 MAX(self->item_margin.bottom, b));
626 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
627 STRUT_SET(self->item_margin,
628 MAX(self->item_margin.left, l),
629 MAX(self->item_margin.top, t),
630 MAX(self->item_margin.right, r),
631 MAX(self->item_margin.bottom, b));
632 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
633 STRUT_SET(self->item_margin,
634 MAX(self->item_margin.left, l),
635 MAX(self->item_margin.top, t),
636 MAX(self->item_margin.right, r),
637 MAX(self->item_margin.bottom, b));
638 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
639 STRUT_SET(self->item_margin,
640 MAX(self->item_margin.left, l),
641 MAX(self->item_margin.top, t),
642 MAX(self->item_margin.right, r),
643 MAX(self->item_margin.bottom, b));
644 }
645
646 /* render the entries */
647
648 for (it = self->entries; it; it = g_list_next(it)) {
649 RrAppearance *text_a;
650 e = it->data;
651
652 /* if the first entry is a labeled separator, then make its border
653 overlap with the menu's outside border */
654 if (it == self->entries &&
655 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
656 e->entry->data.separator.label)
657 {
658 h -= ob_rr_theme->mbwidth;
659 }
660
661 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
662 e->entry->data.separator.label)
663 {
664 e->border = ob_rr_theme->mbwidth;
665 }
666
667 RECT_SET_POINT(e->area, 0, h+e->border);
668 XMoveWindow(ob_display, e->window,
669 e->area.x-e->border, e->area.y-e->border);
670 XSetWindowBorderWidth(ob_display, e->window, e->border);
671 XSetWindowBorder(ob_display, e->window,
672 RrColorPixel(ob_rr_theme->menu_border_color));
673
674
675 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
676 !e->entry->data.normal.enabled ?
677 /* disabled */
678 (e == self->selected ?
679 ob_rr_theme->a_menu_text_disabled_selected :
680 ob_rr_theme->a_menu_text_disabled) :
681 /* enabled */
682 (e == self->selected ?
683 ob_rr_theme->a_menu_text_selected :
684 ob_rr_theme->a_menu_text_normal));
685 switch (e->entry->type) {
686 case OB_MENU_ENTRY_TYPE_NORMAL:
687 text_a->texture[0].data.text.string = e->entry->data.normal.label;
688 tw = RrMinWidth(text_a);
689 tw = MIN(tw, MAX_MENU_WIDTH);
690 th = ob_rr_theme->menu_font_height;
691
692 if (e->entry->data.normal.icon_data ||
693 e->entry->data.normal.mask)
694 has_icon = TRUE;
695 break;
696 case OB_MENU_ENTRY_TYPE_SUBMENU:
697 sub = e->entry->data.submenu.submenu;
698 text_a->texture[0].data.text.string = sub ? sub->title : "";
699 tw = RrMinWidth(text_a);
700 tw = MIN(tw, MAX_MENU_WIDTH);
701 th = ob_rr_theme->menu_font_height;
702
703 if (e->entry->data.normal.icon_data ||
704 e->entry->data.normal.mask)
705 has_icon = TRUE;
706
707 tw += ITEM_HEIGHT - PADDING;
708 break;
709 case OB_MENU_ENTRY_TYPE_SEPARATOR:
710 if (e->entry->data.separator.label != NULL) {
711 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
712 e->entry->data.separator.label;
713 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
714 2*ob_rr_theme->paddingx;
715 tw = MIN(tw, MAX_MENU_WIDTH);
716 th = ob_rr_theme->menu_title_height +
717 (ob_rr_theme->mbwidth - PADDING) *2;
718 } else {
719 tw = 0;
720 th = SEPARATOR_HEIGHT;
721 }
722 break;
723 }
724 tw += 2*PADDING;
725 th += 2*PADDING;
726 w = MAX(w, tw);
727 h += th;
728 }
729
730 /* if the last entry is a labeled separator, then make its border
731 overlap with the menu's outside border */
732 it = g_list_last(self->entries);
733 e = it ? it->data : NULL;
734 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
735 e->entry->data.separator.label)
736 {
737 h -= ob_rr_theme->mbwidth;
738 }
739
740 self->text_x = PADDING;
741 self->text_w = w;
742
743 if (self->entries) {
744 if (has_icon) {
745 w += ITEM_HEIGHT + PADDING;
746 self->text_x += ITEM_HEIGHT + PADDING;
747 }
748 }
749
750 if (!w) w = 10;
751 if (!h) h = 3;
752
753 XResizeWindow(ob_display, self->window, w, h);
754
755 self->inner_w = w;
756
757 RrPaint(self->a_items, self->window, w, h);
758
759 for (it = self->entries; it; it = g_list_next(it))
760 menu_entry_frame_render(it->data);
761
762 w += ob_rr_theme->mbwidth * 2;
763 h += ob_rr_theme->mbwidth * 2;
764
765 RECT_SET_SIZE(self->area, w, h);
766
767 XFlush(ob_display);
768 }
769
770 static void menu_frame_update(ObMenuFrame *self)
771 {
772 GList *mit, *fit;
773 Rect *a;
774 gint h;
775
776 menu_pipe_execute(self->menu);
777 menu_find_submenus(self->menu);
778
779 self->selected = NULL;
780
781 /* start at show_from */
782 mit = g_list_nth(self->menu->entries, self->show_from);
783
784 /* go through the menu's and frame's entries and connect the frame entries
785 to the menu entries */
786 for (fit = self->entries; mit && fit;
787 mit = g_list_next(mit), fit = g_list_next(fit))
788 {
789 ObMenuEntryFrame *f = fit->data;
790 f->entry = mit->data;
791 }
792
793 /* if there are more menu entries than in the frame, add them */
794 while (mit) {
795 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
796 self->entries = g_list_append(self->entries, e);
797 mit = g_list_next(mit);
798 }
799
800 /* if there are more frame entries than menu entries then get rid of
801 them */
802 while (fit) {
803 GList *n = g_list_next(fit);
804 menu_entry_frame_free(fit->data);
805 self->entries = g_list_delete_link(self->entries, fit);
806 fit = n;
807 }
808
809 /* * make the menu fit on the screen */
810
811 /* calculate the height of the menu */
812 h = 0;
813 for (fit = self->entries; fit; fit = g_list_next(fit))
814 h += menu_entry_frame_get_height(fit->data,
815 fit == self->entries,
816 g_list_next(fit) == NULL);
817 /* add the border at the top and bottom */
818 h += ob_rr_theme->mbwidth * 2;
819
820 a = screen_physical_area_monitor(self->monitor);
821
822 if (h > a->height) {
823 GList *flast, *tmp;
824 gboolean last_entry = TRUE;
825
826 /* take the height of our More... entry into account */
827 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
828
829 /* start at the end of the entries */
830 flast = g_list_last(self->entries);
831
832 /* pull out all the entries from the frame that don't
833 fit on the screen, leaving at least 1 though */
834 while (h > a->height && g_list_previous(flast) != NULL) {
835 /* update the height, without this entry */
836 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
837
838 /* destroy the entry we're not displaying */
839 tmp = flast;
840 flast = g_list_previous(flast);
841 menu_entry_frame_free(tmp->data);
842 self->entries = g_list_delete_link(self->entries, tmp);
843
844 /* only the first one that we see is the last entry in the menu */
845 last_entry = FALSE;
846 };
847
848 {
849 ObMenuEntry *more_entry;
850 ObMenuEntryFrame *more_frame;
851 /* make the More... menu entry frame which will display in this
852 frame.
853 if self->menu->more_menu is NULL that means that this is already
854 More... menu, so just use ourself.
855 */
856 more_entry = menu_get_more((self->menu->more_menu ?
857 self->menu->more_menu :
858 self->menu),
859 /* continue where we left off */
860 self->show_from +
861 g_list_length(self->entries));
862 more_frame = menu_entry_frame_new(more_entry, self);
863 /* make it get deleted when the menu frame goes away */
864 menu_entry_unref(more_entry);
865
866 /* add our More... entry to the frame */
867 self->entries = g_list_append(self->entries, more_frame);
868 }
869 }
870
871 g_free(a);
872
873 menu_frame_render(self);
874 }
875
876 static gboolean menu_frame_is_visible(ObMenuFrame *self)
877 {
878 return !!(g_list_find(menu_frame_visible, self));
879 }
880
881 static gboolean menu_frame_show(ObMenuFrame *self)
882 {
883 GList *it;
884
885 /* determine if the underlying menu is already visible */
886 for (it = menu_frame_visible; it; it = g_list_next(it)) {
887 ObMenuFrame *f = it->data;
888 if (f->menu == self->menu)
889 break;
890 }
891 if (!it) {
892 if (self->menu->update_func)
893 if (!self->menu->update_func(self, self->menu->data))
894 return FALSE;
895 }
896
897 if (menu_frame_visible == NULL) {
898 /* no menus shown yet */
899
900 /* grab the pointer in such a way as to pass through "owner events"
901 so that we can get enter/leave notifies in the menu. */
902 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
903 return FALSE;
904 if (!grab_keyboard()) {
905 ungrab_pointer();
906 return FALSE;
907 }
908 }
909
910 menu_frame_update(self);
911
912 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
913
914 if (self->menu->show_func)
915 self->menu->show_func(self, self->menu->data);
916
917 return TRUE;
918 }
919
920 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
921 gboolean mouse)
922 {
923 gint px, py;
924 guint i;
925
926 if (menu_frame_is_visible(self))
927 return TRUE;
928 if (!menu_frame_show(self))
929 return FALSE;
930
931 /* find the monitor the menu is on */
932 for (i = 0; i < screen_num_monitors; ++i) {
933 Rect *a = screen_physical_area_monitor(i);
934 gboolean contains = RECT_CONTAINS(*a, x, y);
935 g_free(a);
936 if (contains) {
937 self->monitor = i;
938 break;
939 }
940 }
941
942 if (self->menu->place_func)
943 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
944 else
945 menu_frame_place_topmenu(self, &x, &y);
946
947 menu_frame_move(self, x, y);
948
949 XMapWindow(ob_display, self->window);
950
951 if (screen_pointer_pos(&px, &py)) {
952 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
953 if (e && e->frame == self)
954 e->ignore_enters++;
955 }
956
957 return TRUE;
958 }
959
960 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
961 ObMenuEntryFrame *parent_entry)
962 {
963 gint x, y, dx, dy;
964 gint px, py;
965
966 if (menu_frame_is_visible(self))
967 return TRUE;
968
969 self->monitor = parent->monitor;
970 self->parent = parent;
971 self->parent_entry = parent_entry;
972
973 /* set up parent's child to be us */
974 if (parent->child)
975 menu_frame_hide(parent->child);
976 parent->child = self;
977
978 if (!menu_frame_show(self))
979 return FALSE;
980
981 menu_frame_place_submenu(self, &x, &y);
982 menu_frame_move_on_screen(self, x, y, &dx, &dy);
983
984 if (dx != 0) {
985 /*try the other side */
986 self->direction_right = !self->direction_right;
987 menu_frame_place_submenu(self, &x, &y);
988 menu_frame_move_on_screen(self, x, y, &dx, &dy);
989 }
990 menu_frame_move(self, x + dx, y + dy);
991
992 XMapWindow(ob_display, self->window);
993
994 if (screen_pointer_pos(&px, &py)) {
995 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
996 if (e && e->frame == self)
997 e->ignore_enters++;
998 }
999
1000 return TRUE;
1001 }
1002
1003 static void menu_frame_hide(ObMenuFrame *self)
1004 {
1005 GList *it = g_list_find(menu_frame_visible, self);
1006
1007 if (!it)
1008 return;
1009
1010 if (self->menu->hide_func)
1011 self->menu->hide_func(self, self->menu->data);
1012
1013 if (self->child)
1014 menu_frame_hide(self->child);
1015
1016 if (self->parent)
1017 self->parent->child = NULL;
1018 self->parent = NULL;
1019 self->parent_entry = NULL;
1020
1021 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1022
1023 if (menu_frame_visible == NULL) {
1024 /* last menu shown */
1025 ungrab_pointer();
1026 ungrab_keyboard();
1027 }
1028
1029 XUnmapWindow(ob_display, self->window);
1030
1031 menu_frame_free(self);
1032 }
1033
1034 void menu_frame_hide_all(void)
1035 {
1036 GList *it;
1037
1038 if (config_submenu_show_delay) {
1039 /* remove any submenu open requests */
1040 ob_main_loop_timeout_remove(ob_main_loop,
1041 menu_entry_frame_submenu_timeout);
1042 }
1043 if ((it = g_list_last(menu_frame_visible)))
1044 menu_frame_hide(it->data);
1045 }
1046
1047 void menu_frame_hide_all_client(ObClient *client)
1048 {
1049 GList *it = g_list_last(menu_frame_visible);
1050 if (it) {
1051 ObMenuFrame *f = it->data;
1052 if (f->client == client) {
1053 if (config_submenu_show_delay) {
1054 /* remove any submenu open requests */
1055 ob_main_loop_timeout_remove(ob_main_loop,
1056 menu_entry_frame_submenu_timeout);
1057 }
1058 menu_frame_hide(f);
1059 }
1060 }
1061 }
1062
1063
1064 ObMenuFrame* menu_frame_under(gint x, gint y)
1065 {
1066 ObMenuFrame *ret = NULL;
1067 GList *it;
1068
1069 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1070 ObMenuFrame *f = it->data;
1071
1072 if (RECT_CONTAINS(f->area, x, y)) {
1073 ret = f;
1074 break;
1075 }
1076 }
1077 return ret;
1078 }
1079
1080 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1081 {
1082 ObMenuFrame *frame;
1083 ObMenuEntryFrame *ret = NULL;
1084 GList *it;
1085
1086 if ((frame = menu_frame_under(x, y))) {
1087 x -= ob_rr_theme->mbwidth + frame->area.x;
1088 y -= ob_rr_theme->mbwidth + frame->area.y;
1089
1090 for (it = frame->entries; it; it = g_list_next(it)) {
1091 ObMenuEntryFrame *e = it->data;
1092
1093 if (RECT_CONTAINS(e->area, x, y)) {
1094 ret = e;
1095 break;
1096 }
1097 }
1098 }
1099 return ret;
1100 }
1101
1102 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1103 {
1104 g_assert(menu_frame_visible);
1105 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1106 return FALSE;
1107 }
1108
1109 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1110 gboolean immediate)
1111 {
1112 ObMenuEntryFrame *old = self->selected;
1113 ObMenuFrame *oldchild = self->child;
1114
1115 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1116 entry = old;
1117
1118 if (old == entry) return;
1119
1120 if (config_submenu_show_delay) {
1121 /* remove any submenu open requests */
1122 ob_main_loop_timeout_remove(ob_main_loop,
1123 menu_entry_frame_submenu_timeout);
1124 }
1125
1126 self->selected = entry;
1127
1128 if (old)
1129 menu_entry_frame_render(old);
1130 if (oldchild)
1131 menu_frame_hide(oldchild);
1132
1133 if (self->selected) {
1134 menu_entry_frame_render(self->selected);
1135
1136 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1137 if (config_submenu_show_delay && !immediate) {
1138 /* initiate a new submenu open request */
1139 ob_main_loop_timeout_add(ob_main_loop,
1140 config_submenu_show_delay * 1000,
1141 menu_entry_frame_submenu_timeout,
1142 self->selected, g_direct_equal,
1143 NULL);
1144 } else {
1145 menu_entry_frame_show_submenu(self->selected);
1146 }
1147 }
1148 }
1149 }
1150
1151 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1152 {
1153 ObMenuFrame *f;
1154
1155 if (!self->entry->data.submenu.submenu) return;
1156
1157 f = menu_frame_new(self->entry->data.submenu.submenu,
1158 self->entry->data.submenu.show_from,
1159 self->frame->client);
1160 /* pass our direction on to our child */
1161 f->direction_right = self->frame->direction_right;
1162
1163 menu_frame_show_submenu(f, self->frame, self);
1164 }
1165
1166 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1167 {
1168 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1169 self->entry->data.normal.enabled)
1170 {
1171 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1172 gets freed */
1173 ObMenuEntry *entry = self->entry;
1174 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1175 gpointer data = self->frame->menu->data;
1176 GSList *acts = self->entry->data.normal.actions;
1177 ObClient *client = self->frame->client;
1178 ObMenuFrame *frame = self->frame;
1179
1180 /* release grabs before executing the shit */
1181 if (!(state & ControlMask)) {
1182 menu_frame_hide_all();
1183 frame = NULL;
1184 }
1185
1186 if (func)
1187 func(entry, frame, client, state, data);
1188 else
1189 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1190 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1191 }
1192 }
1193
1194 void menu_frame_select_previous(ObMenuFrame *self)
1195 {
1196 GList *it = NULL, *start;
1197
1198 if (self->entries) {
1199 start = it = g_list_find(self->entries, self->selected);
1200 while (TRUE) {
1201 ObMenuEntryFrame *e;
1202
1203 it = it ? g_list_previous(it) : g_list_last(self->entries);
1204 if (it == start)
1205 break;
1206
1207 if (it) {
1208 e = it->data;
1209 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1210 break;
1211 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1212 break;
1213 }
1214 }
1215 }
1216 menu_frame_select(self, it ? it->data : NULL, TRUE);
1217 }
1218
1219 void menu_frame_select_next(ObMenuFrame *self)
1220 {
1221 GList *it = NULL, *start;
1222
1223 if (self->entries) {
1224 start = it = g_list_find(self->entries, self->selected);
1225 while (TRUE) {
1226 ObMenuEntryFrame *e;
1227
1228 it = it ? g_list_next(it) : self->entries;
1229 if (it == start)
1230 break;
1231
1232 if (it) {
1233 e = it->data;
1234 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1235 break;
1236 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1237 break;
1238 }
1239 }
1240 }
1241 menu_frame_select(self, it ? it->data : NULL, TRUE);
1242 }
This page took 0.09616 seconds and 5 git commands to generate.