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