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