]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
maybe this makes everyone happy, only select first entry in menus if it's not a subme...
[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) 2004 Mikael Magnusson
5 Copyright (c) 2003 Ben 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 "grab.h"
25 #include "openbox.h"
26 #include "config.h"
27 #include "render/theme.h"
28
29 #define PADDING 2
30 #define SEPARATOR_HEIGHT 3
31 #define MAX_MENU_WIDTH 400
32
33 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
34 LeaveWindowMask)
35 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
36 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
37 ButtonPressMask | ButtonReleaseMask)
38
39 GList *menu_frame_visible;
40
41 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
42 ObMenuFrame *frame);
43 static void menu_entry_frame_free(ObMenuEntryFrame *self);
44 static void menu_frame_render(ObMenuFrame *self);
45 static void menu_frame_update(ObMenuFrame *self);
46 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
47
48 static Window createWindow(Window parent, gulong mask,
49 XSetWindowAttributes *attrib)
50 {
51 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
52 RrDepth(ob_rr_inst), InputOutput,
53 RrVisual(ob_rr_inst), mask, attrib);
54 }
55
56 ObMenuFrame* menu_frame_new(ObMenu *menu, ObClient *client)
57 {
58 ObMenuFrame *self;
59 XSetWindowAttributes attr;
60
61 self = g_new0(ObMenuFrame, 1);
62 self->type = Window_Menu;
63 self->menu = menu;
64 self->selected = NULL;
65 self->show_title = TRUE;
66 self->client = client;
67
68 attr.event_mask = FRAME_EVENTMASK;
69 self->window = createWindow(RootWindow(ob_display, ob_screen),
70 CWEventMask, &attr);
71 attr.event_mask = TITLE_EVENTMASK;
72 self->title = createWindow(self->window, CWEventMask, &attr);
73 self->items = createWindow(self->window, 0, NULL);
74
75 XMapWindow(ob_display, self->items);
76
77 self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
78 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
79
80 stacking_add(MENU_AS_WINDOW(self));
81
82 return self;
83 }
84
85 void menu_frame_free(ObMenuFrame *self)
86 {
87 if (self) {
88 while (self->entries) {
89 menu_entry_frame_free(self->entries->data);
90 self->entries = g_list_delete_link(self->entries, self->entries);
91 }
92
93 stacking_remove(MENU_AS_WINDOW(self));
94
95 XDestroyWindow(ob_display, self->items);
96 XDestroyWindow(ob_display, self->title);
97 XDestroyWindow(ob_display, self->window);
98
99 RrAppearanceFree(self->a_items);
100 RrAppearanceFree(self->a_title);
101
102 g_free(self);
103 }
104 }
105
106 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
107 ObMenuFrame *frame)
108 {
109 ObMenuEntryFrame *self;
110 XSetWindowAttributes attr;
111
112 self = g_new0(ObMenuEntryFrame, 1);
113 self->entry = entry;
114 self->frame = frame;
115
116 attr.event_mask = ENTRY_EVENTMASK;
117 self->window = createWindow(self->frame->items, CWEventMask, &attr);
118 self->text = createWindow(self->window, 0, NULL);
119 if (entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
120 self->icon = createWindow(self->window, 0, NULL);
121 self->bullet = createWindow(self->window, 0, NULL);
122 }
123
124 XMapWindow(ob_display, self->window);
125 XMapWindow(ob_display, self->text);
126
127 self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
128 self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
129 self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
130
131 if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
132 self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
133 self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
134 } else {
135 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
136 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
137 self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
138 self->a_mask->texture[0].type = RR_TEXTURE_MASK;
139 self->a_bullet_normal =
140 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
141 self->a_bullet_selected =
142 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
143 }
144
145 self->a_text_normal =
146 RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
147 self->a_text_disabled =
148 RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
149 self->a_text_selected =
150 RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
151
152 return self;
153 }
154
155 static void menu_entry_frame_free(ObMenuEntryFrame *self)
156 {
157 if (self) {
158 XDestroyWindow(ob_display, self->text);
159 XDestroyWindow(ob_display, self->window);
160 if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR) {
161 XDestroyWindow(ob_display, self->icon);
162 XDestroyWindow(ob_display, self->bullet);
163 }
164
165 RrAppearanceFree(self->a_normal);
166 RrAppearanceFree(self->a_disabled);
167 RrAppearanceFree(self->a_selected);
168
169 RrAppearanceFree(self->a_separator);
170 RrAppearanceFree(self->a_icon);
171 RrAppearanceFree(self->a_mask);
172 RrAppearanceFree(self->a_text_normal);
173 RrAppearanceFree(self->a_text_disabled);
174 RrAppearanceFree(self->a_text_selected);
175 RrAppearanceFree(self->a_bullet_normal);
176 RrAppearanceFree(self->a_bullet_selected);
177
178 g_free(self);
179 }
180 }
181
182 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
183 {
184 RECT_SET_POINT(self->area, x, y);
185 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
186 }
187
188 void menu_frame_move_on_screen(ObMenuFrame *self)
189 {
190 Rect *a = NULL;
191 guint i;
192 gint dx = 0, dy = 0;
193 gint pos, half;
194
195 a = screen_physical_area_monitor(self->monitor);
196
197 half = g_list_length(self->entries) / 2;
198 pos = g_list_index(self->entries, self->selected);
199
200 /* if in the bottom half then check this shit first, will keep the bottom
201 edge of the menu visible */
202 if (pos > half) {
203 dx = MAX(dx, a->x - self->area.x);
204 dy = MAX(dy, a->y - self->area.y);
205 }
206 dx = MIN(dx, (a->x + a->width) - (self->area.x + self->area.width));
207 dy = MIN(dy, (a->y + a->height) - (self->area.y + self->area.height));
208 /* if in the top half then check this shit last, will keep the top
209 edge of the menu visible */
210 if (pos <= half) {
211 dx = MAX(dx, a->x - self->area.x);
212 dy = MAX(dy, a->y - self->area.y);
213 }
214
215 if (dx || dy) {
216 ObMenuFrame *f;
217
218 /* move the current menu frame to fit, but dont touch parents yet */
219 menu_frame_move(self, self->area.x + dx, self->area.y + dy);
220 if (!config_menu_xorstyle)
221 dy = 0; /* if we want to be like xor, move parents in y- *
222 * and x-direction, otherwise just in x-dir */
223 for (f = self->parent; f; f = f->parent)
224 menu_frame_move(f, f->area.x + dx, f->area.y + dy);
225 for (f = self->child; f; f = f->child)
226 menu_frame_move(f, f->area.x + dx, f->area.y + dy);
227 if (config_menu_warppointer)
228 XWarpPointer(ob_display, None, None, 0, 0, 0, 0, dx, dy);
229 }
230 }
231
232 static void menu_entry_frame_render(ObMenuEntryFrame *self)
233 {
234 RrAppearance *item_a, *text_a;
235 gint th; /* temp */
236 ObMenu *sub;
237 ObMenuFrame *frame = self->frame;
238
239 item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
240 !self->entry->data.normal.enabled) ?
241 self->a_disabled :
242 (self == self->frame->selected ?
243 self->a_selected :
244 self->a_normal));
245 switch (self->entry->type) {
246 case OB_MENU_ENTRY_TYPE_NORMAL:
247 case OB_MENU_ENTRY_TYPE_SUBMENU:
248 th = self->frame->item_h;
249 break;
250 case OB_MENU_ENTRY_TYPE_SEPARATOR:
251 th = SEPARATOR_HEIGHT + 2*PADDING;
252 break;
253 }
254 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
255 XResizeWindow(ob_display, self->window,
256 self->area.width, self->area.height);
257 item_a->surface.parent = self->frame->a_items;
258 item_a->surface.parentx = self->area.x;
259 item_a->surface.parenty = self->area.y;
260 RrPaint(item_a, self->window, self->area.width, self->area.height);
261
262 text_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
263 !self->entry->data.normal.enabled) ?
264 self->a_text_disabled :
265 (self == self->frame->selected ?
266 self->a_text_selected :
267 self->a_text_normal));
268 switch (self->entry->type) {
269 case OB_MENU_ENTRY_TYPE_NORMAL:
270 text_a->texture[0].data.text.string = self->entry->data.normal.label;
271 break;
272 case OB_MENU_ENTRY_TYPE_SUBMENU:
273 sub = self->entry->data.submenu.submenu;
274 text_a->texture[0].data.text.string = sub ? sub->title : "";
275 break;
276 case OB_MENU_ENTRY_TYPE_SEPARATOR:
277 break;
278 }
279
280 switch (self->entry->type) {
281 case OB_MENU_ENTRY_TYPE_NORMAL:
282 XMoveResizeWindow(ob_display, self->text,
283 self->frame->text_x, PADDING,
284 self->frame->text_w,
285 self->frame->item_h - 2*PADDING);
286 text_a->surface.parent = item_a;
287 text_a->surface.parentx = self->frame->text_x;
288 text_a->surface.parenty = PADDING;
289 RrPaint(text_a, self->text, self->frame->text_w,
290 self->frame->item_h - 2*PADDING);
291 break;
292 case OB_MENU_ENTRY_TYPE_SUBMENU:
293 XMoveResizeWindow(ob_display, self->text,
294 self->frame->text_x, PADDING,
295 self->frame->text_w - self->frame->item_h,
296 self->frame->item_h - 2*PADDING);
297 text_a->surface.parent = item_a;
298 text_a->surface.parentx = self->frame->text_x;
299 text_a->surface.parenty = PADDING;
300 RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
301 self->frame->item_h - 2*PADDING);
302 break;
303 case OB_MENU_ENTRY_TYPE_SEPARATOR:
304 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
305 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
306 self->a_separator->surface.parent = item_a;
307 self->a_separator->surface.parentx = PADDING;
308 self->a_separator->surface.parenty = PADDING;
309 self->a_separator->texture[0].data.lineart.color =
310 text_a->texture[0].data.text.color;
311 self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
312 self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT / 2;
313 self->a_separator->texture[0].data.lineart.x2 =
314 self->area.width - 4*PADDING;
315 self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT / 2;
316 RrPaint(self->a_separator, self->text,
317 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
318 break;
319 }
320
321 if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
322 self->entry->data.normal.icon_data)
323 {
324 XMoveResizeWindow(ob_display, self->icon,
325 PADDING, frame->item_margin.top,
326 self->frame->item_h - frame->item_margin.top
327 - frame->item_margin.bottom,
328 self->frame->item_h - frame->item_margin.top
329 - frame->item_margin.bottom);
330 self->a_icon->texture[0].data.rgba.width =
331 self->entry->data.normal.icon_width;
332 self->a_icon->texture[0].data.rgba.height =
333 self->entry->data.normal.icon_height;
334 self->a_icon->texture[0].data.rgba.data =
335 self->entry->data.normal.icon_data;
336 self->a_icon->surface.parent = item_a;
337 self->a_icon->surface.parentx = PADDING;
338 self->a_icon->surface.parenty = frame->item_margin.top;
339 RrPaint(self->a_icon, self->icon,
340 self->frame->item_h - frame->item_margin.top
341 - frame->item_margin.bottom,
342 self->frame->item_h - frame->item_margin.top
343 - frame->item_margin.bottom);
344 XMapWindow(ob_display, self->icon);
345 } else if (self->entry->type != OB_MENU_ENTRY_TYPE_SEPARATOR &&
346 self->entry->data.normal.mask)
347 {
348 RrColor *c;
349
350 XMoveResizeWindow(ob_display, self->icon,
351 PADDING, frame->item_margin.top,
352 self->frame->item_h - frame->item_margin.top
353 - frame->item_margin.bottom,
354 self->frame->item_h - frame->item_margin.top
355 - frame->item_margin.bottom);
356 self->a_mask->texture[0].data.mask.mask =
357 self->entry->data.normal.mask;
358
359 c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
360 !self->entry->data.normal.enabled) ?
361 self->entry->data.normal.mask_disabled_color :
362 (self == self->frame->selected ?
363 self->entry->data.normal.mask_selected_color :
364 self->entry->data.normal.mask_normal_color));
365 self->a_mask->texture[0].data.mask.color = c;
366
367 self->a_mask->surface.parent = item_a;
368 self->a_mask->surface.parentx = PADDING;
369 self->a_mask->surface.parenty = frame->item_margin.top;
370 RrPaint(self->a_mask, self->icon,
371 self->frame->item_h - frame->item_margin.top
372 - frame->item_margin.bottom,
373 self->frame->item_h - frame->item_margin.top
374 - frame->item_margin.bottom);
375 XMapWindow(ob_display, self->icon);
376 } else
377 XUnmapWindow(ob_display, self->icon);
378
379 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
380 RrAppearance *bullet_a;
381 XMoveResizeWindow(ob_display, self->bullet,
382 self->frame->text_x + self->frame->text_w
383 - self->frame->item_h + PADDING, PADDING,
384 self->frame->item_h - 2*PADDING,
385 self->frame->item_h - 2*PADDING);
386 bullet_a = (self == self->frame->selected ?
387 self->a_bullet_selected :
388 self->a_bullet_normal);
389 bullet_a->surface.parent = item_a;
390 bullet_a->surface.parentx =
391 self->frame->text_x + self->frame->text_w - self->frame->item_h
392 + PADDING;
393 bullet_a->surface.parenty = PADDING;
394 RrPaint(bullet_a, self->bullet,
395 self->frame->item_h - 2*PADDING,
396 self->frame->item_h - 2*PADDING);
397 XMapWindow(ob_display, self->bullet);
398 } else
399 XUnmapWindow(ob_display, self->bullet);
400
401 XFlush(ob_display);
402 }
403
404 static void menu_frame_render(ObMenuFrame *self)
405 {
406 gint w = 0, h = 0;
407 gint allitems_h = 0;
408 gint tw, th; /* temps */
409 GList *it;
410 gboolean has_icon = FALSE;
411 ObMenu *sub;
412
413 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
414 XSetWindowBorder(ob_display, self->window,
415 RrColorPixel(ob_rr_theme->b_color));
416
417 if (!self->parent && self->show_title) {
418 XMoveWindow(ob_display, self->title,
419 -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
420
421 self->a_title->texture[0].data.text.string = self->menu->title;
422 RrMinsize(self->a_title, &tw, &th);
423 tw = MIN(tw, MAX_MENU_WIDTH) + ob_rr_theme->padding * 2;
424 w = MAX(w, tw);
425
426 th = ob_rr_theme->menu_title_height;
427 h += (self->title_h = th + ob_rr_theme->bwidth);
428
429 XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
430 XSetWindowBorder(ob_display, self->title,
431 RrColorPixel(ob_rr_theme->b_color));
432 }
433
434 XMoveWindow(ob_display, self->items, 0, h);
435
436 STRUT_SET(self->item_margin, 0, 0, 0, 0);
437
438 if (self->entries) {
439 ObMenuEntryFrame *e = self->entries->data;
440 gint l, t, r, b;
441
442 e->a_text_normal->texture[0].data.text.string = "";
443 RrMinsize(e->a_text_normal, &tw, &th);
444 tw += 2*PADDING;
445 th += 2*PADDING;
446 self->item_h = th;
447
448 RrMargins(e->a_normal, &l, &t, &r, &b);
449 STRUT_SET(self->item_margin,
450 MAX(self->item_margin.left, l),
451 MAX(self->item_margin.top, t),
452 MAX(self->item_margin.right, r),
453 MAX(self->item_margin.bottom, b));
454 RrMargins(e->a_selected, &l, &t, &r, &b);
455 STRUT_SET(self->item_margin,
456 MAX(self->item_margin.left, l),
457 MAX(self->item_margin.top, t),
458 MAX(self->item_margin.right, r),
459 MAX(self->item_margin.bottom, b));
460 RrMargins(e->a_disabled, &l, &t, &r, &b);
461 STRUT_SET(self->item_margin,
462 MAX(self->item_margin.left, l),
463 MAX(self->item_margin.top, t),
464 MAX(self->item_margin.right, r),
465 MAX(self->item_margin.bottom, b));
466 } else
467 self->item_h = 0;
468
469 for (it = self->entries; it; it = g_list_next(it)) {
470 RrAppearance *text_a;
471 ObMenuEntryFrame *e = it->data;
472
473 RECT_SET_POINT(e->area, 0, allitems_h);
474 XMoveWindow(ob_display, e->window, 0, e->area.y);
475
476 text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
477 !e->entry->data.normal.enabled) ?
478 e->a_text_disabled :
479 (e == self->selected ?
480 e->a_text_selected :
481 e->a_text_normal));
482 switch (e->entry->type) {
483 case OB_MENU_ENTRY_TYPE_NORMAL:
484 text_a->texture[0].data.text.string = e->entry->data.normal.label;
485 RrMinsize(text_a, &tw, &th);
486 tw = MIN(tw, MAX_MENU_WIDTH);
487
488 if (e->entry->data.normal.icon_data ||
489 e->entry->data.normal.mask)
490 has_icon = TRUE;
491 break;
492 case OB_MENU_ENTRY_TYPE_SUBMENU:
493 sub = e->entry->data.submenu.submenu;
494 text_a->texture[0].data.text.string = sub ? sub->title : "";
495 RrMinsize(text_a, &tw, &th);
496 tw = MIN(tw, MAX_MENU_WIDTH);
497
498 if (e->entry->data.normal.icon_data ||
499 e->entry->data.normal.mask)
500 has_icon = TRUE;
501
502 tw += self->item_h - PADDING;
503 break;
504 case OB_MENU_ENTRY_TYPE_SEPARATOR:
505 tw = 0;
506 th = SEPARATOR_HEIGHT;
507 break;
508 }
509 tw += 2*PADDING;
510 th += 2*PADDING;
511 w = MAX(w, tw);
512 h += th;
513 allitems_h += th;
514 }
515
516 self->text_x = PADDING;
517 self->text_w = w;
518
519 if (self->entries) {
520 if (has_icon) {
521 w += self->item_h + PADDING;
522 self->text_x += self->item_h + PADDING;
523 }
524 }
525
526 if (!w) w = 10;
527 if (!allitems_h) {
528 allitems_h = 3;
529 h += 3;
530 }
531
532 XResizeWindow(ob_display, self->window, w, h);
533 XResizeWindow(ob_display, self->items, w, allitems_h);
534
535 self->inner_w = w;
536
537 if (!self->parent && self->show_title) {
538 XResizeWindow(ob_display, self->title,
539 w, self->title_h - ob_rr_theme->bwidth);
540 RrPaint(self->a_title, self->title,
541 w, self->title_h - ob_rr_theme->bwidth);
542 XMapWindow(ob_display, self->title);
543 } else
544 XUnmapWindow(ob_display, self->title);
545
546 RrPaint(self->a_items, self->items, w, allitems_h);
547
548 for (it = self->entries; it; it = g_list_next(it))
549 menu_entry_frame_render(it->data);
550
551 w += ob_rr_theme->bwidth * 2;
552 h += ob_rr_theme->bwidth * 2;
553
554 RECT_SET_SIZE(self->area, w, h);
555
556 XFlush(ob_display);
557 }
558
559 static void menu_frame_update(ObMenuFrame *self)
560 {
561 GList *mit, *fit;
562
563 menu_pipe_execute(self->menu);
564 menu_find_submenus(self->menu);
565
566 self->selected = NULL;
567
568 for (mit = self->menu->entries, fit = self->entries; mit && fit;
569 mit = g_list_next(mit), fit = g_list_next(fit))
570 {
571 ObMenuEntryFrame *f = fit->data;
572 f->entry = mit->data;
573 }
574
575 while (mit) {
576 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
577 self->entries = g_list_append(self->entries, e);
578 mit = g_list_next(mit);
579 }
580
581 while (fit) {
582 GList *n = g_list_next(fit);
583 menu_entry_frame_free(fit->data);
584 self->entries = g_list_delete_link(self->entries, fit);
585 fit = n;
586 }
587
588 menu_frame_render(self);
589 }
590
591 gboolean menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
592 {
593 GList *it;
594
595 if (g_list_find(menu_frame_visible, self))
596 return TRUE;
597
598 if (menu_frame_visible == NULL) {
599 /* no menus shown yet */
600 if (!grab_pointer(TRUE, OB_CURSOR_NONE))
601 return FALSE;
602 if (!grab_keyboard(TRUE)) {
603 grab_pointer(FALSE, OB_CURSOR_NONE);
604 return FALSE;
605 }
606 }
607
608 if (parent) {
609 self->monitor = parent->monitor;
610 if (parent->child)
611 menu_frame_hide(parent->child);
612 parent->child = self;
613 }
614 self->parent = parent;
615
616 /* determine if the underlying menu is already visible */
617 for (it = menu_frame_visible; it; it = g_list_next(it)) {
618 ObMenuFrame *f = it->data;
619 if (f->menu == self->menu)
620 break;
621 }
622 if (!it) {
623 if (self->menu->update_func)
624 self->menu->update_func(self, self->menu->data);
625 }
626
627 menu_frame_update(self);
628
629 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
630
631 menu_frame_move_on_screen(self);
632
633 XMapWindow(ob_display, self->window);
634
635 return TRUE;
636 }
637
638 void menu_frame_hide(ObMenuFrame *self)
639 {
640 GList *it = g_list_find(menu_frame_visible, self);
641
642 if (!it)
643 return;
644
645 if (self->child)
646 menu_frame_hide(self->child);
647
648 if (self->parent)
649 self->parent->child = NULL;
650 self->parent = NULL;
651
652 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
653
654 if (menu_frame_visible == NULL) {
655 /* last menu shown */
656 grab_pointer(FALSE, OB_CURSOR_NONE);
657 grab_keyboard(FALSE);
658 }
659
660 XUnmapWindow(ob_display, self->window);
661
662 menu_frame_free(self);
663 }
664
665 void menu_frame_hide_all()
666 {
667 if (config_submenu_show_delay) {
668 /* remove any submenu open requests */
669 ob_main_loop_timeout_remove(ob_main_loop,
670 menu_entry_frame_submenu_timeout);
671 }
672 GList *it = g_list_last(menu_frame_visible);
673 if (it)
674 menu_frame_hide(it->data);
675 }
676
677 void menu_frame_hide_all_client(ObClient *client)
678 {
679 GList *it = g_list_last(menu_frame_visible);
680 if (it) {
681 ObMenuFrame *f = it->data;
682 if (f->client == client)
683 menu_frame_hide(f);
684 }
685 }
686
687
688 ObMenuFrame* menu_frame_under(gint x, gint y)
689 {
690 ObMenuFrame *ret = NULL;
691 GList *it;
692
693 for (it = menu_frame_visible; it; it = g_list_next(it)) {
694 ObMenuFrame *f = it->data;
695
696 if (RECT_CONTAINS(f->area, x, y)) {
697 ret = f;
698 break;
699 }
700 }
701 return ret;
702 }
703
704 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
705 {
706 ObMenuFrame *frame;
707 ObMenuEntryFrame *ret = NULL;
708 GList *it;
709
710 if ((frame = menu_frame_under(x, y))) {
711 x -= ob_rr_theme->bwidth + frame->area.x;
712 y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
713
714 for (it = frame->entries; it; it = g_list_next(it)) {
715 ObMenuEntryFrame *e = it->data;
716
717 if (RECT_CONTAINS(e->area, x, y)) {
718 ret = e;
719 break;
720 }
721 }
722 }
723 return ret;
724 }
725
726 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
727 {
728 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
729 return FALSE;
730 }
731
732 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
733 {
734 ObMenuEntryFrame *old = self->selected;
735 ObMenuFrame *oldchild = self->child;
736
737 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
738 entry = old;
739
740 if (old == entry) return;
741
742 if (config_submenu_show_delay) {
743 /* remove any submenu open requests */
744 ob_main_loop_timeout_remove(ob_main_loop,
745 menu_entry_frame_submenu_timeout);
746 }
747
748 self->selected = entry;
749
750 if (old)
751 menu_entry_frame_render(old);
752 if (oldchild)
753 menu_frame_hide(oldchild);
754
755 if (self->selected) {
756 menu_entry_frame_render(self->selected);
757
758 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
759 if (config_submenu_show_delay) {
760 /* initiate a new submenu open request */
761 ob_main_loop_timeout_add(ob_main_loop,
762 config_submenu_show_delay * 1000,
763 menu_entry_frame_submenu_timeout,
764 self->selected,
765 NULL);
766 } else {
767 menu_entry_frame_show_submenu(self->selected);
768 }
769 }
770 }
771 }
772
773 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
774 {
775 ObMenuFrame *f;
776
777 if (!self->entry->data.submenu.submenu) return;
778
779 f = menu_frame_new(self->entry->data.submenu.submenu,
780 self->frame->client);
781 menu_frame_move(f,
782 self->frame->area.x + self->frame->area.width
783 - ob_rr_theme->menu_overlap - ob_rr_theme->bwidth,
784 self->frame->area.y + self->frame->title_h +
785 self->area.y + ob_rr_theme->menu_overlap);
786 menu_frame_show(f, self->frame);
787 }
788
789 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
790 {
791 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
792 self->entry->data.normal.enabled)
793 {
794 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
795 gets freed */
796 ObMenuEntry *entry = self->entry;
797 ObMenuExecuteFunc func = self->frame->menu->execute_func;
798 gpointer data = self->frame->menu->data;
799 GSList *acts = self->entry->data.normal.actions;
800 ObClient *client = self->frame->client;
801
802 /* release grabs before executing the shit */
803 if (!(state & ControlMask))
804 menu_frame_hide_all();
805
806 if (func)
807 func(entry, state, data);
808 else
809 action_run(acts, client, state);
810 }
811 }
812
813 void menu_frame_select_previous(ObMenuFrame *self)
814 {
815 GList *it = NULL, *start;
816
817 if (self->entries) {
818 start = it = g_list_find(self->entries, self->selected);
819 while (TRUE) {
820 ObMenuEntryFrame *e;
821
822 it = it ? g_list_previous(it) : g_list_last(self->entries);
823 if (it == start)
824 break;
825
826 if (it) {
827 e = it->data;
828 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
829 break;
830 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
831 e->entry->data.normal.enabled)
832 break;
833 }
834 }
835 }
836 menu_frame_select(self, it ? it->data : NULL);
837 }
838
839 void menu_frame_select_next(ObMenuFrame *self)
840 {
841 GList *it = NULL, *start;
842
843 if (self->entries) {
844 start = it = g_list_find(self->entries, self->selected);
845 while (TRUE) {
846 ObMenuEntryFrame *e;
847
848 it = it ? g_list_next(it) : self->entries;
849 if (it == start)
850 break;
851
852 if (it) {
853 e = it->data;
854 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
855 break;
856 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
857 e->entry->data.normal.enabled)
858 break;
859 }
860 }
861 }
862 menu_frame_select(self, it ? it->data : NULL);
863 }
This page took 0.081358 seconds and 4 git commands to generate.