]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
get rid of copying the rrappearances for each menu frame, except for the one that...
[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 else
387 text_a = ob_rr_theme->a_menu_text_normal;
388 break;
389 }
390
391 switch (self->entry->type) {
392 case OB_MENU_ENTRY_TYPE_NORMAL:
393 XMoveResizeWindow(ob_display, self->text,
394 self->frame->text_x, PADDING,
395 self->frame->text_w,
396 ITEM_HEIGHT - 2*PADDING);
397 text_a->surface.parent = item_a;
398 text_a->surface.parentx = self->frame->text_x;
399 text_a->surface.parenty = PADDING;
400 RrPaint(text_a, self->text, self->frame->text_w,
401 ITEM_HEIGHT - 2*PADDING);
402 break;
403 case OB_MENU_ENTRY_TYPE_SUBMENU:
404 XMoveResizeWindow(ob_display, self->text,
405 self->frame->text_x, PADDING,
406 self->frame->text_w - ITEM_HEIGHT,
407 ITEM_HEIGHT - 2*PADDING);
408 text_a->surface.parent = item_a;
409 text_a->surface.parentx = self->frame->text_x;
410 text_a->surface.parenty = PADDING;
411 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
412 ITEM_HEIGHT - 2*PADDING);
413 break;
414 case OB_MENU_ENTRY_TYPE_SEPARATOR:
415 if (self->entry->data.separator.label != NULL) {
416 /* labeled separator */
417 XMoveResizeWindow(ob_display, self->text,
418 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
419 self->area.width - 2*ob_rr_theme->paddingx,
420 ob_rr_theme->menu_title_height -
421 2*ob_rr_theme->paddingy);
422 text_a->surface.parent = item_a;
423 text_a->surface.parentx = ob_rr_theme->paddingx;
424 text_a->surface.parenty = ob_rr_theme->paddingy;
425 RrPaint(text_a, self->text,
426 self->area.width - 2*ob_rr_theme->paddingx,
427 ob_rr_theme->menu_title_height -
428 2*ob_rr_theme->paddingy);
429 } else {
430 RrAppearance *clear;
431
432 /* unlabeled separaator */
433 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
434 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
435
436 clear = ob_rr_theme->a_clear_tex;
437 clear->texture[0].type = RR_TEXTURE_LINE_ART;
438 clear->surface.parent = item_a;
439 clear->surface.parentx = PADDING;
440 clear->surface.parenty = PADDING;
441 clear->texture[0].data.lineart.color =
442 text_a->texture[0].data.text.color;
443 clear->texture[0].data.lineart.x1 = 2*PADDING;
444 clear->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
445 clear->texture[0].data.lineart.x2 = self->area.width - 4*PADDING;
446 clear->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
447 RrPaint(clear, self->text,
448 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
449 }
450 break;
451 }
452
453 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
454 self->entry->data.normal.icon_data)
455 {
456 RrAppearance *clear;
457
458 XMoveResizeWindow(ob_display, self->icon,
459 PADDING, frame->item_margin.top,
460 ITEM_HEIGHT - frame->item_margin.top
461 - frame->item_margin.bottom,
462 ITEM_HEIGHT - frame->item_margin.top
463 - frame->item_margin.bottom);
464
465 clear = ob_rr_theme->a_clear_tex;
466 clear->texture[0].type = RR_TEXTURE_RGBA;
467 clear->texture[0].data.rgba.width =
468 self->entry->data.normal.icon_width;
469 clear->texture[0].data.rgba.height =
470 self->entry->data.normal.icon_height;
471 clear->texture[0].data.rgba.alpha =
472 self->entry->data.normal.icon_alpha;
473 clear->texture[0].data.rgba.data =
474 self->entry->data.normal.icon_data;
475 clear->surface.parent = item_a;
476 clear->surface.parentx = PADDING;
477 clear->surface.parenty = frame->item_margin.top;
478 RrPaint(clear, self->icon,
479 ITEM_HEIGHT - frame->item_margin.top
480 - frame->item_margin.bottom,
481 ITEM_HEIGHT - frame->item_margin.top
482 - frame->item_margin.bottom);
483 XMapWindow(ob_display, self->icon);
484 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
485 self->entry->data.normal.mask)
486 {
487 RrColor *c;
488 RrAppearance *clear;
489
490 XMoveResizeWindow(ob_display, self->icon,
491 PADDING, frame->item_margin.top,
492 ITEM_HEIGHT - frame->item_margin.top
493 - frame->item_margin.bottom,
494 ITEM_HEIGHT - frame->item_margin.top
495 - frame->item_margin.bottom);
496
497 clear = ob_rr_theme->a_clear_tex;
498 clear->texture[0].type = RR_TEXTURE_MASK;
499 clear->texture[0].data.mask.mask =
500 self->entry->data.normal.mask;
501
502 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
503 !self->entry->data.normal.enabled ?
504 /* disabled */
505 (self == self->frame->selected ?
506 self->entry->data.normal.mask_disabled_selected_color :
507 self->entry->data.normal.mask_disabled_color) :
508 /* enabled */
509 (self == self->frame->selected ?
510 self->entry->data.normal.mask_selected_color :
511 self->entry->data.normal.mask_normal_color));
512 clear->texture[0].data.mask.color = c;
513
514 clear->surface.parent = item_a;
515 clear->surface.parentx = PADDING;
516 clear->surface.parenty = frame->item_margin.top;
517 RrPaint(clear, self->icon,
518 ITEM_HEIGHT - frame->item_margin.top
519 - frame->item_margin.bottom,
520 ITEM_HEIGHT - frame->item_margin.top
521 - frame->item_margin.bottom);
522 XMapWindow(ob_display, self->icon);
523 } else
524 XUnmapWindow(ob_display, self->icon);
525
526 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
527 RrAppearance *bullet_a;
528 XMoveResizeWindow(ob_display, self->bullet,
529 self->frame->text_x + self->frame->text_w -
530 ITEM_HEIGHT + PADDING, PADDING,
531 ITEM_HEIGHT - 2*PADDING,
532 ITEM_HEIGHT - 2*PADDING);
533 bullet_a = (self == self->frame->selected ?
534 ob_rr_theme->a_menu_bullet_selected :
535 ob_rr_theme->a_menu_bullet_normal);
536 bullet_a->surface.parent = item_a;
537 bullet_a->surface.parentx =
538 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
539 bullet_a->surface.parenty = PADDING;
540 RrPaint(bullet_a, self->bullet,
541 ITEM_HEIGHT - 2*PADDING,
542 ITEM_HEIGHT - 2*PADDING);
543 XMapWindow(ob_display, self->bullet);
544 } else
545 XUnmapWindow(ob_display, self->bullet);
546
547 XFlush(ob_display);
548 }
549
550 /*! this code is taken from the menu_frame_render. if that changes, this won't
551 work.. */
552 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
553 gboolean first_entry,
554 gboolean last_entry)
555 {
556 ObMenuEntryType t;
557 gint h = 0;
558
559 h += 2*PADDING;
560
561 if (self)
562 t = self->entry->type;
563 else
564 /* this is the More... entry, it's NORMAL type */
565 t = OB_MENU_ENTRY_TYPE_NORMAL;
566
567 switch (t) {
568 case OB_MENU_ENTRY_TYPE_NORMAL:
569 case OB_MENU_ENTRY_TYPE_SUBMENU:
570 h += ob_rr_theme->menu_font_height;
571 break;
572 case OB_MENU_ENTRY_TYPE_SEPARATOR:
573 if (self->entry->data.separator.label != NULL) {
574 h += ob_rr_theme->menu_title_height +
575 (ob_rr_theme->mbwidth - PADDING) * 2;
576
577 /* if the first entry is a labeled separator, then make its border
578 overlap with the menu's outside border */
579 if (first_entry)
580 h -= ob_rr_theme->mbwidth;
581 /* if the last entry is a labeled separator, then make its border
582 overlap with the menu's outside border */
583 if (last_entry)
584 h -= ob_rr_theme->mbwidth;
585 } else {
586 h += SEPARATOR_HEIGHT;
587 }
588 break;
589 }
590
591 return h;
592 }
593
594 void menu_frame_render(ObMenuFrame *self)
595 {
596 gint w = 0, h = 0;
597 gint tw, th; /* temps */
598 GList *it;
599 gboolean has_icon = FALSE;
600 ObMenu *sub;
601 ObMenuEntryFrame *e;
602
603 /* find text dimensions */
604
605 STRUT_SET(self->item_margin, 0, 0, 0, 0);
606
607 if (self->entries) {
608 gint l, t, r, b;
609
610 e = self->entries->data;
611 ob_rr_theme->a_menu_text_normal->texture[0].data.text.string = "";
612 tw = RrMinWidth(ob_rr_theme->a_menu_text_normal);
613 tw += 2*PADDING;
614
615 th = ITEM_HEIGHT;
616
617 RrMargins(ob_rr_theme->a_menu_normal, &l, &t, &r, &b);
618 STRUT_SET(self->item_margin,
619 MAX(self->item_margin.left, l),
620 MAX(self->item_margin.top, t),
621 MAX(self->item_margin.right, r),
622 MAX(self->item_margin.bottom, b));
623 RrMargins(ob_rr_theme->a_menu_selected, &l, &t, &r, &b);
624 STRUT_SET(self->item_margin,
625 MAX(self->item_margin.left, l),
626 MAX(self->item_margin.top, t),
627 MAX(self->item_margin.right, r),
628 MAX(self->item_margin.bottom, b));
629 RrMargins(ob_rr_theme->a_menu_disabled, &l, &t, &r, &b);
630 STRUT_SET(self->item_margin,
631 MAX(self->item_margin.left, l),
632 MAX(self->item_margin.top, t),
633 MAX(self->item_margin.right, r),
634 MAX(self->item_margin.bottom, b));
635 RrMargins(ob_rr_theme->a_menu_disabled_selected, &l, &t, &r, &b);
636 STRUT_SET(self->item_margin,
637 MAX(self->item_margin.left, l),
638 MAX(self->item_margin.top, t),
639 MAX(self->item_margin.right, r),
640 MAX(self->item_margin.bottom, b));
641 }
642
643 /* render the entries */
644
645 for (it = self->entries; it; it = g_list_next(it)) {
646 RrAppearance *text_a;
647 e = it->data;
648
649 /* if the first entry is a labeled separator, then make its border
650 overlap with the menu's outside border */
651 if (it == self->entries &&
652 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
653 e->entry->data.separator.label)
654 {
655 h -= ob_rr_theme->mbwidth;
656 }
657
658 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
659 e->entry->data.separator.label)
660 {
661 e->border = ob_rr_theme->mbwidth;
662 }
663
664 RECT_SET_POINT(e->area, 0, h+e->border);
665 XMoveWindow(ob_display, e->window,
666 e->area.x-e->border, e->area.y-e->border);
667 XSetWindowBorderWidth(ob_display, e->window, e->border);
668 XSetWindowBorder(ob_display, e->window,
669 RrColorPixel(ob_rr_theme->menu_border_color));
670
671
672 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
673 !e->entry->data.normal.enabled ?
674 /* disabled */
675 (e == self->selected ?
676 ob_rr_theme->a_menu_text_disabled_selected :
677 ob_rr_theme->a_menu_text_disabled) :
678 /* enabled */
679 (e == self->selected ?
680 ob_rr_theme->a_menu_text_selected :
681 ob_rr_theme->a_menu_text_normal));
682 switch (e->entry->type) {
683 case OB_MENU_ENTRY_TYPE_NORMAL:
684 text_a->texture[0].data.text.string = e->entry->data.normal.label;
685 tw = RrMinWidth(text_a);
686 tw = MIN(tw, MAX_MENU_WIDTH);
687 th = ob_rr_theme->menu_font_height;
688
689 if (e->entry->data.normal.icon_data ||
690 e->entry->data.normal.mask)
691 has_icon = TRUE;
692 break;
693 case OB_MENU_ENTRY_TYPE_SUBMENU:
694 sub = e->entry->data.submenu.submenu;
695 text_a->texture[0].data.text.string = sub ? sub->title : "";
696 tw = RrMinWidth(text_a);
697 tw = MIN(tw, MAX_MENU_WIDTH);
698 th = ob_rr_theme->menu_font_height;
699
700 if (e->entry->data.normal.icon_data ||
701 e->entry->data.normal.mask)
702 has_icon = TRUE;
703
704 tw += ITEM_HEIGHT - PADDING;
705 break;
706 case OB_MENU_ENTRY_TYPE_SEPARATOR:
707 if (e->entry->data.separator.label != NULL) {
708 ob_rr_theme->a_menu_text_title->texture[0].data.text.string =
709 e->entry->data.separator.label;
710 tw = RrMinWidth(ob_rr_theme->a_menu_text_title) +
711 2*ob_rr_theme->paddingx;
712 tw = MIN(tw, MAX_MENU_WIDTH);
713 th = ob_rr_theme->menu_title_height +
714 (ob_rr_theme->mbwidth - PADDING) *2;
715 } else {
716 tw = 0;
717 th = SEPARATOR_HEIGHT;
718 }
719 break;
720 }
721 tw += 2*PADDING;
722 th += 2*PADDING;
723 w = MAX(w, tw);
724 h += th;
725 }
726
727 /* if the last entry is a labeled separator, then make its border
728 overlap with the menu's outside border */
729 it = g_list_last(self->entries);
730 e = it ? it->data : NULL;
731 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
732 e->entry->data.separator.label)
733 {
734 h -= ob_rr_theme->mbwidth;
735 }
736
737 self->text_x = PADDING;
738 self->text_w = w;
739
740 if (self->entries) {
741 if (has_icon) {
742 w += ITEM_HEIGHT + PADDING;
743 self->text_x += ITEM_HEIGHT + PADDING;
744 }
745 }
746
747 if (!w) w = 10;
748 if (!h) h = 3;
749
750 XResizeWindow(ob_display, self->window, w, h);
751
752 self->inner_w = w;
753
754 RrPaint(self->a_items, self->window, w, h);
755
756 for (it = self->entries; it; it = g_list_next(it))
757 menu_entry_frame_render(it->data);
758
759 w += ob_rr_theme->mbwidth * 2;
760 h += ob_rr_theme->mbwidth * 2;
761
762 RECT_SET_SIZE(self->area, w, h);
763
764 XFlush(ob_display);
765 }
766
767 static void menu_frame_update(ObMenuFrame *self)
768 {
769 GList *mit, *fit;
770 Rect *a;
771 gint h;
772
773 menu_pipe_execute(self->menu);
774 menu_find_submenus(self->menu);
775
776 self->selected = NULL;
777
778 /* start at show_from */
779 mit = g_list_nth(self->menu->entries, self->show_from);
780
781 /* go through the menu's and frame's entries and connect the frame entries
782 to the menu entries */
783 for (fit = self->entries; mit && fit;
784 mit = g_list_next(mit), fit = g_list_next(fit))
785 {
786 ObMenuEntryFrame *f = fit->data;
787 f->entry = mit->data;
788 }
789
790 /* if there are more menu entries than in the frame, add them */
791 while (mit) {
792 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
793 self->entries = g_list_append(self->entries, e);
794 mit = g_list_next(mit);
795 }
796
797 /* if there are more frame entries than menu entries then get rid of
798 them */
799 while (fit) {
800 GList *n = g_list_next(fit);
801 menu_entry_frame_free(fit->data);
802 self->entries = g_list_delete_link(self->entries, fit);
803 fit = n;
804 }
805
806 /* * make the menu fit on the screen */
807
808 /* calculate the height of the menu */
809 h = 0;
810 for (fit = self->entries; fit; fit = g_list_next(fit))
811 h += menu_entry_frame_get_height(fit->data,
812 fit == self->entries,
813 g_list_next(fit) == NULL);
814 /* add the border at the top and bottom */
815 h += ob_rr_theme->mbwidth * 2;
816
817 a = screen_physical_area_monitor(self->monitor);
818
819 if (h > a->height) {
820 GList *flast, *tmp;
821 gboolean last_entry = TRUE;
822
823 /* take the height of our More... entry into account */
824 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
825
826 /* start at the end of the entries */
827 flast = g_list_last(self->entries);
828
829 /* pull out all the entries from the frame that don't
830 fit on the screen, leaving at least 1 though */
831 while (h > a->height && g_list_previous(flast) != NULL) {
832 /* update the height, without this entry */
833 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
834
835 /* destroy the entry we're not displaying */
836 tmp = flast;
837 flast = g_list_previous(flast);
838 menu_entry_frame_free(tmp->data);
839 self->entries = g_list_delete_link(self->entries, tmp);
840
841 /* only the first one that we see is the last entry in the menu */
842 last_entry = FALSE;
843 };
844
845 {
846 ObMenuEntry *more_entry;
847 ObMenuEntryFrame *more_frame;
848 /* make the More... menu entry frame which will display in this
849 frame.
850 if self->menu->more_menu is NULL that means that this is already
851 More... menu, so just use ourself.
852 */
853 more_entry = menu_get_more((self->menu->more_menu ?
854 self->menu->more_menu :
855 self->menu),
856 /* continue where we left off */
857 self->show_from +
858 g_list_length(self->entries));
859 more_frame = menu_entry_frame_new(more_entry, self);
860 /* make it get deleted when the menu frame goes away */
861 menu_entry_unref(more_entry);
862
863 /* add our More... entry to the frame */
864 self->entries = g_list_append(self->entries, more_frame);
865 }
866 }
867
868 g_free(a);
869
870 menu_frame_render(self);
871 }
872
873 static gboolean menu_frame_is_visible(ObMenuFrame *self)
874 {
875 return !!(g_list_find(menu_frame_visible, self));
876 }
877
878 static gboolean menu_frame_show(ObMenuFrame *self)
879 {
880 GList *it;
881
882 /* determine if the underlying menu is already visible */
883 for (it = menu_frame_visible; it; it = g_list_next(it)) {
884 ObMenuFrame *f = it->data;
885 if (f->menu == self->menu)
886 break;
887 }
888 if (!it) {
889 if (self->menu->update_func)
890 if (!self->menu->update_func(self, self->menu->data))
891 return FALSE;
892 }
893
894 if (menu_frame_visible == NULL) {
895 /* no menus shown yet */
896
897 /* grab the pointer in such a way as to pass through "owner events"
898 so that we can get enter/leave notifies in the menu. */
899 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
900 return FALSE;
901 if (!grab_keyboard()) {
902 ungrab_pointer();
903 return FALSE;
904 }
905 }
906
907 menu_frame_update(self);
908
909 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
910
911 if (self->menu->show_func)
912 self->menu->show_func(self, self->menu->data);
913
914 return TRUE;
915 }
916
917 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
918 gboolean mouse)
919 {
920 gint px, py;
921 guint i;
922
923 if (menu_frame_is_visible(self))
924 return TRUE;
925 if (!menu_frame_show(self))
926 return FALSE;
927
928 /* find the monitor the menu is on */
929 for (i = 0; i < screen_num_monitors; ++i) {
930 Rect *a = screen_physical_area_monitor(i);
931 gboolean contains = RECT_CONTAINS(*a, x, y);
932 g_free(a);
933 if (contains) {
934 self->monitor = i;
935 break;
936 }
937 }
938
939 if (self->menu->place_func)
940 self->menu->place_func(self, &x, &y, mouse, self->menu->data);
941 else
942 menu_frame_place_topmenu(self, &x, &y);
943
944 menu_frame_move(self, x, y);
945
946 XMapWindow(ob_display, self->window);
947
948 if (screen_pointer_pos(&px, &py)) {
949 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
950 if (e && e->frame == self)
951 e->ignore_enters++;
952 }
953
954 return TRUE;
955 }
956
957 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
958 ObMenuEntryFrame *parent_entry)
959 {
960 gint x, y, dx, dy;
961 gint px, py;
962
963 if (menu_frame_is_visible(self))
964 return TRUE;
965
966 self->monitor = parent->monitor;
967 self->parent = parent;
968 self->parent_entry = parent_entry;
969
970 /* set up parent's child to be us */
971 if (parent->child)
972 menu_frame_hide(parent->child);
973 parent->child = self;
974
975 if (!menu_frame_show(self))
976 return FALSE;
977
978 menu_frame_place_submenu(self, &x, &y);
979 menu_frame_move_on_screen(self, x, y, &dx, &dy);
980
981 if (dx != 0) {
982 /*try the other side */
983 self->direction_right = !self->direction_right;
984 menu_frame_place_submenu(self, &x, &y);
985 menu_frame_move_on_screen(self, x, y, &dx, &dy);
986 }
987 menu_frame_move(self, x + dx, y + dy);
988
989 XMapWindow(ob_display, self->window);
990
991 if (screen_pointer_pos(&px, &py)) {
992 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
993 if (e && e->frame == self)
994 e->ignore_enters++;
995 }
996
997 return TRUE;
998 }
999
1000 static void menu_frame_hide(ObMenuFrame *self)
1001 {
1002 GList *it = g_list_find(menu_frame_visible, self);
1003
1004 if (!it)
1005 return;
1006
1007 if (self->menu->hide_func)
1008 self->menu->hide_func(self, self->menu->data);
1009
1010 if (self->child)
1011 menu_frame_hide(self->child);
1012
1013 if (self->parent)
1014 self->parent->child = NULL;
1015 self->parent = NULL;
1016 self->parent_entry = NULL;
1017
1018 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1019
1020 if (menu_frame_visible == NULL) {
1021 /* last menu shown */
1022 ungrab_pointer();
1023 ungrab_keyboard();
1024 }
1025
1026 XUnmapWindow(ob_display, self->window);
1027
1028 menu_frame_free(self);
1029 }
1030
1031 void menu_frame_hide_all(void)
1032 {
1033 GList *it;
1034
1035 if (config_submenu_show_delay) {
1036 /* remove any submenu open requests */
1037 ob_main_loop_timeout_remove(ob_main_loop,
1038 menu_entry_frame_submenu_timeout);
1039 }
1040 if ((it = g_list_last(menu_frame_visible)))
1041 menu_frame_hide(it->data);
1042 }
1043
1044 void menu_frame_hide_all_client(ObClient *client)
1045 {
1046 GList *it = g_list_last(menu_frame_visible);
1047 if (it) {
1048 ObMenuFrame *f = it->data;
1049 if (f->client == client) {
1050 if (config_submenu_show_delay) {
1051 /* remove any submenu open requests */
1052 ob_main_loop_timeout_remove(ob_main_loop,
1053 menu_entry_frame_submenu_timeout);
1054 }
1055 menu_frame_hide(f);
1056 }
1057 }
1058 }
1059
1060
1061 ObMenuFrame* menu_frame_under(gint x, gint y)
1062 {
1063 ObMenuFrame *ret = NULL;
1064 GList *it;
1065
1066 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1067 ObMenuFrame *f = it->data;
1068
1069 if (RECT_CONTAINS(f->area, x, y)) {
1070 ret = f;
1071 break;
1072 }
1073 }
1074 return ret;
1075 }
1076
1077 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1078 {
1079 ObMenuFrame *frame;
1080 ObMenuEntryFrame *ret = NULL;
1081 GList *it;
1082
1083 if ((frame = menu_frame_under(x, y))) {
1084 x -= ob_rr_theme->mbwidth + frame->area.x;
1085 y -= ob_rr_theme->mbwidth + frame->area.y;
1086
1087 for (it = frame->entries; it; it = g_list_next(it)) {
1088 ObMenuEntryFrame *e = it->data;
1089
1090 if (RECT_CONTAINS(e->area, x, y)) {
1091 ret = e;
1092 break;
1093 }
1094 }
1095 }
1096 return ret;
1097 }
1098
1099 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1100 {
1101 g_assert(menu_frame_visible);
1102 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1103 return FALSE;
1104 }
1105
1106 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1107 gboolean immediate)
1108 {
1109 ObMenuEntryFrame *old = self->selected;
1110 ObMenuFrame *oldchild = self->child;
1111
1112 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1113 entry = old;
1114
1115 if (old == entry) return;
1116
1117 if (config_submenu_show_delay) {
1118 /* remove any submenu open requests */
1119 ob_main_loop_timeout_remove(ob_main_loop,
1120 menu_entry_frame_submenu_timeout);
1121 }
1122
1123 self->selected = entry;
1124
1125 if (old)
1126 menu_entry_frame_render(old);
1127 if (oldchild)
1128 menu_frame_hide(oldchild);
1129
1130 if (self->selected) {
1131 menu_entry_frame_render(self->selected);
1132
1133 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1134 if (config_submenu_show_delay && !immediate) {
1135 /* initiate a new submenu open request */
1136 ob_main_loop_timeout_add(ob_main_loop,
1137 config_submenu_show_delay * 1000,
1138 menu_entry_frame_submenu_timeout,
1139 self->selected, g_direct_equal,
1140 NULL);
1141 } else {
1142 menu_entry_frame_show_submenu(self->selected);
1143 }
1144 }
1145 }
1146 }
1147
1148 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1149 {
1150 ObMenuFrame *f;
1151
1152 if (!self->entry->data.submenu.submenu) return;
1153
1154 f = menu_frame_new(self->entry->data.submenu.submenu,
1155 self->entry->data.submenu.show_from,
1156 self->frame->client);
1157 /* pass our direction on to our child */
1158 f->direction_right = self->frame->direction_right;
1159
1160 menu_frame_show_submenu(f, self->frame, self);
1161 }
1162
1163 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
1164 {
1165 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1166 self->entry->data.normal.enabled)
1167 {
1168 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1169 gets freed */
1170 ObMenuEntry *entry = self->entry;
1171 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1172 gpointer data = self->frame->menu->data;
1173 GSList *acts = self->entry->data.normal.actions;
1174 ObClient *client = self->frame->client;
1175 ObMenuFrame *frame = self->frame;
1176
1177 /* release grabs before executing the shit */
1178 if (!(state & ControlMask)) {
1179 menu_frame_hide_all();
1180 frame = NULL;
1181 }
1182
1183 if (func)
1184 func(entry, frame, client, state, data);
1185 else
1186 actions_run_acts(acts, OB_USER_ACTION_MENU_SELECTION,
1187 state, -1, -1, 0, OB_FRAME_CONTEXT_NONE, client);
1188 }
1189 }
1190
1191 void menu_frame_select_previous(ObMenuFrame *self)
1192 {
1193 GList *it = NULL, *start;
1194
1195 if (self->entries) {
1196 start = it = g_list_find(self->entries, self->selected);
1197 while (TRUE) {
1198 ObMenuEntryFrame *e;
1199
1200 it = it ? g_list_previous(it) : g_list_last(self->entries);
1201 if (it == start)
1202 break;
1203
1204 if (it) {
1205 e = it->data;
1206 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1207 break;
1208 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1209 break;
1210 }
1211 }
1212 }
1213 menu_frame_select(self, it ? it->data : NULL, TRUE);
1214 }
1215
1216 void menu_frame_select_next(ObMenuFrame *self)
1217 {
1218 GList *it = NULL, *start;
1219
1220 if (self->entries) {
1221 start = it = g_list_find(self->entries, self->selected);
1222 while (TRUE) {
1223 ObMenuEntryFrame *e;
1224
1225 it = it ? g_list_next(it) : self->entries;
1226 if (it == start)
1227 break;
1228
1229 if (it) {
1230 e = it->data;
1231 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1232 break;
1233 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1234 break;
1235 }
1236 }
1237 }
1238 menu_frame_select(self, it ? it->data : NULL, TRUE);
1239 }
This page took 0.0899990000000001 seconds and 5 git commands to generate.