]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
save the new glist!
[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 XFlush(ob_display);
366 }
367
368 static void menu_frame_render(ObMenuFrame *self)
369 {
370 gint w = 0, h = 0;
371 gint allitems_h = 0;
372 gint tw, th; /* temps */
373 GList *it;
374 gboolean has_icon = FALSE;
375 ObMenu *sub;
376
377 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->bwidth);
378 XSetWindowBorder(ob_display, self->window,
379 RrColorPixel(ob_rr_theme->b_color));
380
381 if (!self->parent && self->show_title) {
382 XMoveWindow(ob_display, self->title,
383 -ob_rr_theme->bwidth, h - ob_rr_theme->bwidth);
384
385 self->a_title->texture[0].data.text.string = self->menu->title;
386 RrMinsize(self->a_title, &tw, &th);
387 tw = MIN(tw, MAX_MENU_WIDTH);
388 tw += 2*PADDING;
389 th += 2*PADDING;
390 w = MAX(w, tw);
391 h += (self->title_h = th + ob_rr_theme->bwidth);
392
393 XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
394 XSetWindowBorder(ob_display, self->title,
395 RrColorPixel(ob_rr_theme->b_color));
396 }
397
398 XMoveWindow(ob_display, self->items, 0, h);
399
400 STRUT_SET(self->item_margin, 0, 0, 0, 0);
401
402 if (self->entries) {
403 ObMenuEntryFrame *e = self->entries->data;
404 gint l, t, r, b;
405
406 e->a_text_normal->texture[0].data.text.string = "";
407 RrMinsize(e->a_text_normal, &tw, &th);
408 tw += 2*PADDING;
409 th += 2*PADDING;
410 self->item_h = th;
411
412 RrMargins(e->a_normal, &l, &t, &r, &b);
413 STRUT_SET(self->item_margin,
414 MAX(self->item_margin.left, l),
415 MAX(self->item_margin.top, t),
416 MAX(self->item_margin.right, r),
417 MAX(self->item_margin.bottom, b));
418 RrMargins(e->a_selected, &l, &t, &r, &b);
419 STRUT_SET(self->item_margin,
420 MAX(self->item_margin.left, l),
421 MAX(self->item_margin.top, t),
422 MAX(self->item_margin.right, r),
423 MAX(self->item_margin.bottom, b));
424 RrMargins(e->a_disabled, &l, &t, &r, &b);
425 STRUT_SET(self->item_margin,
426 MAX(self->item_margin.left, l),
427 MAX(self->item_margin.top, t),
428 MAX(self->item_margin.right, r),
429 MAX(self->item_margin.bottom, b));
430 } else
431 self->item_h = 0;
432
433 for (it = self->entries; it; it = g_list_next(it)) {
434 RrAppearance *text_a;
435 ObMenuEntryFrame *e = it->data;
436
437 RECT_SET_POINT(e->area, 0, allitems_h);
438 XMoveWindow(ob_display, e->window, 0, e->area.y);
439
440 text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
441 !e->entry->data.normal.enabled) ?
442 e->a_text_disabled :
443 (e == self->selected ?
444 e->a_text_selected :
445 e->a_text_normal));
446 switch (e->entry->type) {
447 case OB_MENU_ENTRY_TYPE_NORMAL:
448 text_a->texture[0].data.text.string = e->entry->data.normal.label;
449 RrMinsize(text_a, &tw, &th);
450 tw = MIN(tw, MAX_MENU_WIDTH);
451
452 if (e->entry->data.normal.icon_data ||
453 e->entry->data.normal.mask)
454 has_icon = TRUE;
455 break;
456 case OB_MENU_ENTRY_TYPE_SUBMENU:
457 sub = e->entry->data.submenu.submenu;
458 text_a->texture[0].data.text.string = sub ? sub->title : "";
459 RrMinsize(text_a, &tw, &th);
460 tw = MIN(tw, MAX_MENU_WIDTH);
461
462 if (e->entry->data.normal.icon_data ||
463 e->entry->data.normal.mask)
464 has_icon = TRUE;
465
466 tw += self->item_h - PADDING;
467 break;
468 case OB_MENU_ENTRY_TYPE_SEPARATOR:
469 tw = 0;
470 th = SEPARATOR_HEIGHT;
471 break;
472 }
473 tw += 2*PADDING;
474 th += 2*PADDING;
475 w = MAX(w, tw);
476 h += th;
477 allitems_h += th;
478 }
479
480 self->text_x = PADDING;
481 self->text_w = w;
482
483 if (self->entries) {
484 if (has_icon) {
485 w += self->item_h + PADDING;
486 self->text_x += self->item_h + PADDING;
487 }
488 }
489
490 if (!w) w = 10;
491 if (!allitems_h) {
492 allitems_h = 3;
493 h += 3;
494 }
495
496 XResizeWindow(ob_display, self->window, w, h);
497 XResizeWindow(ob_display, self->items, w, allitems_h);
498
499 self->inner_w = w;
500
501 if (!self->parent && self->show_title) {
502 XResizeWindow(ob_display, self->title,
503 w, self->title_h - ob_rr_theme->bwidth);
504 RrPaint(self->a_title, self->title,
505 w, self->title_h - ob_rr_theme->bwidth);
506 XMapWindow(ob_display, self->title);
507 } else
508 XUnmapWindow(ob_display, self->title);
509
510 RrPaint(self->a_items, self->items, w, allitems_h);
511
512 for (it = self->entries; it; it = g_list_next(it))
513 menu_entry_frame_render(it->data);
514
515 w += ob_rr_theme->bwidth * 2;
516 h += ob_rr_theme->bwidth * 2;
517
518 RECT_SET_SIZE(self->area, w, h);
519
520 XFlush(ob_display);
521 }
522
523 static void menu_frame_update(ObMenuFrame *self)
524 {
525 GList *mit, *fit;
526
527 menu_pipe_execute(self->menu);
528 menu_find_submenus(self->menu);
529
530 self->selected = NULL;
531
532 for (mit = self->menu->entries, fit = self->entries; mit && fit;
533 mit = g_list_next(mit), fit = g_list_next(fit))
534 {
535 ObMenuEntryFrame *f = fit->data;
536 f->entry = mit->data;
537 }
538
539 while (mit) {
540 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
541 self->entries = g_list_append(self->entries, e);
542 mit = g_list_next(mit);
543 }
544
545 while (fit) {
546 GList *n = g_list_next(fit);
547 menu_entry_frame_free(fit->data);
548 self->entries = g_list_delete_link(self->entries, fit);
549 fit = n;
550 }
551
552 menu_frame_render(self);
553 }
554
555 void menu_frame_show(ObMenuFrame *self, ObMenuFrame *parent)
556 {
557 GList *it;
558
559 if (g_list_find(menu_frame_visible, self))
560 return;
561
562 if (parent) {
563 if (parent->child)
564 menu_frame_hide(parent->child);
565 parent->child = self;
566 }
567 self->parent = parent;
568
569 if (menu_frame_visible == NULL) {
570 /* no menus shown yet */
571 grab_pointer(TRUE, OB_CURSOR_NONE);
572 grab_keyboard(TRUE);
573 }
574
575 /* determine if the underlying menu is already visible */
576 for (it = menu_frame_visible; it; it = g_list_next(it)) {
577 ObMenuFrame *f = it->data;
578 if (f->menu == self->menu)
579 break;
580 }
581 if (!it) {
582 if (self->menu->update_func)
583 self->menu->update_func(self, self->menu->data);
584 }
585
586 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
587 menu_frame_update(self);
588
589 menu_frame_move_on_screen(self);
590
591 XMapWindow(ob_display, self->window);
592 }
593
594 void menu_frame_hide(ObMenuFrame *self)
595 {
596 GList *it = g_list_find(menu_frame_visible, self);
597
598 if (!it)
599 return;
600
601 if (self->child)
602 menu_frame_hide(self->child);
603
604 if (self->parent)
605 self->parent->child = NULL;
606 self->parent = NULL;
607
608 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
609
610 if (menu_frame_visible == NULL) {
611 /* last menu shown */
612 grab_pointer(FALSE, OB_CURSOR_NONE);
613 grab_keyboard(FALSE);
614 }
615
616 XUnmapWindow(ob_display, self->window);
617
618 menu_frame_free(self);
619 }
620
621 void menu_frame_hide_all()
622 {
623 GList *it = g_list_last(menu_frame_visible);
624 if (it)
625 menu_frame_hide(it->data);
626 }
627
628 void menu_frame_hide_all_client(ObClient *client)
629 {
630 GList *it = g_list_last(menu_frame_visible);
631 if (it) {
632 ObMenuFrame *f = it->data;
633 if (f->client == client)
634 menu_frame_hide(f);
635 }
636 }
637
638
639 ObMenuFrame* menu_frame_under(gint x, gint y)
640 {
641 ObMenuFrame *ret = NULL;
642 GList *it;
643
644 for (it = menu_frame_visible; it; it = g_list_next(it)) {
645 ObMenuFrame *f = it->data;
646
647 if (RECT_CONTAINS(f->area, x, y)) {
648 ret = f;
649 break;
650 }
651 }
652 return ret;
653 }
654
655 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
656 {
657 ObMenuFrame *frame;
658 ObMenuEntryFrame *ret = NULL;
659 GList *it;
660
661 if ((frame = menu_frame_under(x, y))) {
662 x -= ob_rr_theme->bwidth + frame->area.x;
663 y -= frame->title_h + ob_rr_theme->bwidth + frame->area.y;
664
665 for (it = frame->entries; it; it = g_list_next(it)) {
666 ObMenuEntryFrame *e = it->data;
667
668 if (RECT_CONTAINS(e->area, x, y)) {
669 ret = e;
670 break;
671 }
672 }
673 }
674 return ret;
675 }
676
677 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry)
678 {
679 ObMenuEntryFrame *old = self->selected;
680 ObMenuFrame *oldchild = self->child;
681
682 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
683 entry = old;
684
685 if (old == entry) return;
686
687 self->selected = entry;
688
689 if (old)
690 menu_entry_frame_render(old);
691 if (oldchild)
692 menu_frame_hide(oldchild);
693
694 if (self->selected) {
695 menu_entry_frame_render(self->selected);
696
697 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
698 menu_entry_frame_show_submenu(self->selected);
699 }
700 }
701
702 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
703 {
704 ObMenuFrame *f;
705
706 if (!self->entry->data.submenu.submenu) return;
707
708 f = menu_frame_new(self->entry->data.submenu.submenu,
709 self->frame->client);
710 menu_frame_move(f,
711 self->frame->area.x + self->frame->area.width
712 - ob_rr_theme->menu_overlap - ob_rr_theme->bwidth,
713 self->frame->area.y + self->frame->title_h +
714 self->area.y + ob_rr_theme->menu_overlap);
715 menu_frame_show(f, self->frame);
716 }
717
718 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state)
719 {
720 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
721 self->entry->data.normal.enabled)
722 {
723 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
724 gets freed */
725 ObMenuEntry *entry = self->entry;
726 ObMenuExecuteFunc func = self->frame->menu->execute_func;
727 gpointer data = self->frame->menu->data;
728 GSList *acts = self->entry->data.normal.actions;
729 ObClient *client = self->frame->client;
730
731 /* release grabs before executing the shit */
732 if (!(state & ControlMask))
733 menu_frame_hide_all();
734
735 if (func)
736 func(entry, state, data);
737 else {
738 GSList *it;
739
740 for (it = acts; it; it = g_slist_next(it))
741 action_run(it->data, client, state);
742 }
743 }
744 }
745
746 void menu_frame_select_previous(ObMenuFrame *self)
747 {
748 GList *it = NULL, *start;
749
750 if (self->entries) {
751 start = it = g_list_find(self->entries, self->selected);
752 while (TRUE) {
753 ObMenuEntryFrame *e;
754
755 it = it ? g_list_previous(it) : g_list_last(self->entries);
756 if (it == start)
757 break;
758
759 if (it) {
760 e = it->data;
761 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
762 break;
763 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
764 e->entry->data.normal.enabled)
765 break;
766 }
767 }
768 }
769 menu_frame_select(self, it ? it->data : NULL);
770 }
771
772 void menu_frame_select_next(ObMenuFrame *self)
773 {
774 GList *it = NULL, *start;
775
776 if (self->entries) {
777 start = it = g_list_find(self->entries, self->selected);
778 while (TRUE) {
779 ObMenuEntryFrame *e;
780
781 it = it ? g_list_next(it) : self->entries;
782 if (it == start)
783 break;
784
785 if (it) {
786 e = it->data;
787 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
788 break;
789 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
790 e->entry->data.normal.enabled)
791 break;
792 }
793 }
794 }
795 menu_frame_select(self, it ? it->data : NULL);
796 }
This page took 0.071804 seconds and 4 git commands to generate.