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