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