]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
a95e46fca4e1ccc9bd300037feb7ffed9ad6c196
[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 self->a_text_title =
167 RrAppearanceCopy(ob_rr_theme->a_menu_text_title);
168
169 return self;
170 }
171
172 static void menu_entry_frame_free(ObMenuEntryFrame *self)
173 {
174 if (self) {
175 XDestroyWindow(ob_display, self->text);
176 XDestroyWindow(ob_display, self->window);
177 g_hash_table_remove(menu_frame_map, &self->text);
178 g_hash_table_remove(menu_frame_map, &self->window);
179 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
180 XDestroyWindow(ob_display, self->icon);
181 g_hash_table_remove(menu_frame_map, &self->icon);
182 }
183 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
184 XDestroyWindow(ob_display, self->bullet);
185 g_hash_table_remove(menu_frame_map, &self->bullet);
186 }
187
188 RrAppearanceFree(self->a_normal);
189 RrAppearanceFree(self->a_disabled);
190 RrAppearanceFree(self->a_selected);
191
192 RrAppearanceFree(self->a_separator);
193 RrAppearanceFree(self->a_icon);
194 RrAppearanceFree(self->a_mask);
195 RrAppearanceFree(self->a_text_normal);
196 RrAppearanceFree(self->a_text_disabled);
197 RrAppearanceFree(self->a_text_selected);
198 RrAppearanceFree(self->a_text_title);
199 RrAppearanceFree(self->a_bullet_normal);
200 RrAppearanceFree(self->a_bullet_selected);
201
202 g_free(self);
203 }
204 }
205
206 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
207 {
208 RECT_SET_POINT(self->area, x, y);
209 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
210 }
211
212 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
213 {
214 gint dx, dy;
215
216 if (config_menu_middle) {
217 gint myx;
218
219 myx = *x;
220 *y -= self->area.height / 2;
221
222 /* try to the right of the cursor */
223 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
224 self->direction_right = TRUE;
225 if (dx != 0) {
226 /* try to the left of the cursor */
227 myx = *x - self->area.width;
228 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
229 self->direction_right = FALSE;
230 }
231 if (dx != 0) {
232 /* if didnt fit on either side so just use what it says */
233 myx = *x;
234 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
235 self->direction_right = TRUE;
236 }
237 *x = myx + dx;
238 *y += dy;
239 } else {
240 gint myx, myy;
241
242 myx = *x;
243 myy = *y;
244
245 /* try to the bottom right of the cursor */
246 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
247 self->direction_right = TRUE;
248 if (dx != 0 || dy != 0) {
249 /* try to the bottom left of the cursor */
250 myx = *x - self->area.width;
251 myy = *y;
252 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
253 self->direction_right = FALSE;
254 }
255 if (dx != 0 || dy != 0) {
256 /* try to the top right of the cursor */
257 myx = *x;
258 myy = *y - self->area.height;
259 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
260 self->direction_right = TRUE;
261 }
262 if (dx != 0 || dy != 0) {
263 /* try to the top left of the cursor */
264 myx = *x - self->area.width;
265 myy = *y - self->area.height;
266 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
267 self->direction_right = FALSE;
268 }
269 if (dx != 0 || dy != 0) {
270 /* if didnt fit on either side so just use what it says */
271 myx = *x;
272 myy = *y;
273 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
274 self->direction_right = TRUE;
275 }
276 *x = myx + dx;
277 *y = myy + dy;
278 }
279 }
280
281 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
282 {
283 gint overlap;
284 gint bwidth;
285
286 overlap = ob_rr_theme->menu_overlap;
287 bwidth = ob_rr_theme->mbwidth;
288
289 if (self->direction_right)
290 *x = self->parent->area.x + self->parent->area.width -
291 overlap - bwidth;
292 else
293 *x = self->parent->area.x - self->area.width + overlap + bwidth;
294
295 *y = self->parent->area.y + self->parent_entry->area.y;
296 if (config_menu_middle)
297 *y -= (self->area.height - (bwidth * 2) - self->item_h) / 2;
298 else
299 *y += overlap;
300 }
301
302 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
303 gint *dx, gint *dy)
304 {
305 Rect *a = NULL;
306 gint pos, half;
307
308 *dx = *dy = 0;
309
310 a = screen_physical_area_monitor(self->monitor);
311
312 half = g_list_length(self->entries) / 2;
313 pos = g_list_index(self->entries, self->selected);
314
315 /* if in the bottom half then check this stuff first, will keep the bottom
316 edge of the menu visible */
317 if (pos > half) {
318 *dx = MAX(*dx, a->x - x);
319 *dy = MAX(*dy, a->y - y);
320 }
321 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
322 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
323 /* if in the top half then check this stuff last, will keep the top
324 edge of the menu visible */
325 if (pos <= half) {
326 *dx = MAX(*dx, a->x - x);
327 *dy = MAX(*dy, a->y - y);
328 }
329 }
330
331 static void menu_entry_frame_render(ObMenuEntryFrame *self)
332 {
333 RrAppearance *item_a, *text_a;
334 gint th; /* temp */
335 ObMenu *sub;
336 ObMenuFrame *frame = self->frame;
337
338 switch (self->entry->type) {
339 case OB_MENU_ENTRY_TYPE_NORMAL:
340 case OB_MENU_ENTRY_TYPE_SUBMENU:
341 item_a = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
342 !self->entry->data.normal.enabled) ?
343 self->a_disabled :
344 (self == self->frame->selected ?
345 self->a_selected :
346 self->a_normal));
347 th = self->frame->item_h;
348 break;
349 case OB_MENU_ENTRY_TYPE_SEPARATOR:
350 if (self->entry->data.separator.label) {
351 item_a = self->frame->a_title;
352 th = ob_rr_theme->menu_title_height;
353 } else {
354 item_a = self->a_normal;
355 th = SEPARATOR_HEIGHT + 2*PADDING;
356 }
357 break;
358 default:
359 g_assert_not_reached();
360 }
361 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
362 XResizeWindow(ob_display, self->window,
363 self->area.width, self->area.height);
364 item_a->surface.parent = self->frame->a_items;
365 item_a->surface.parentx = self->area.x;
366 item_a->surface.parenty = self->area.y;
367 RrPaint(item_a, self->window, self->area.width, self->area.height);
368
369 switch (self->entry->type) {
370 case OB_MENU_ENTRY_TYPE_NORMAL:
371 text_a = (!self->entry->data.normal.enabled ?
372 self->a_text_disabled :
373 (self == self->frame->selected ?
374 self->a_text_selected :
375 self->a_text_normal));
376 text_a->texture[0].data.text.string = self->entry->data.normal.label;
377 if (self->entry->data.normal.shortcut &&
378 (self->frame->menu->show_all_shortcuts ||
379 self->entry->data.normal.shortcut_position > 0))
380 {
381 text_a->texture[0].data.text.shortcut = TRUE;
382 text_a->texture[0].data.text.shortcut_pos =
383 self->entry->data.normal.shortcut_position;
384 } else
385 text_a->texture[0].data.text.shortcut = FALSE;
386 break;
387 case OB_MENU_ENTRY_TYPE_SUBMENU:
388 text_a = (self == self->frame->selected ?
389 self->a_text_selected :
390 self->a_text_normal);
391 sub = self->entry->data.submenu.submenu;
392 text_a->texture[0].data.text.string = sub ? sub->title : "";
393 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
394 sub->shortcut_position > 0))
395 {
396 text_a->texture[0].data.text.shortcut = TRUE;
397 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
398 } else
399 text_a->texture[0].data.text.shortcut = FALSE;
400 break;
401 case OB_MENU_ENTRY_TYPE_SEPARATOR:
402 if (self->entry->data.separator.label != NULL)
403 text_a = self->a_text_title;
404 else
405 text_a = self->a_text_normal;
406 break;
407 }
408
409 switch (self->entry->type) {
410 case OB_MENU_ENTRY_TYPE_NORMAL:
411 XMoveResizeWindow(ob_display, self->text,
412 self->frame->text_x, PADDING,
413 self->frame->text_w,
414 self->frame->item_h - 2*PADDING);
415 text_a->surface.parent = item_a;
416 text_a->surface.parentx = self->frame->text_x;
417 text_a->surface.parenty = PADDING;
418 RrPaint(text_a, self->text, self->frame->text_w,
419 self->frame->item_h - 2*PADDING);
420 break;
421 case OB_MENU_ENTRY_TYPE_SUBMENU:
422 XMoveResizeWindow(ob_display, self->text,
423 self->frame->text_x, PADDING,
424 self->frame->text_w - self->frame->item_h,
425 self->frame->item_h - 2*PADDING);
426 text_a->surface.parent = item_a;
427 text_a->surface.parentx = self->frame->text_x;
428 text_a->surface.parenty = PADDING;
429 RrPaint(text_a, self->text, self->frame->text_w - self->frame->item_h,
430 self->frame->item_h - 2*PADDING);
431 break;
432 case OB_MENU_ENTRY_TYPE_SEPARATOR:
433 if (self->entry->data.separator.label != NULL) {
434 /* labeled separator */
435 XMoveResizeWindow(ob_display, self->text,
436 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
437 self->area.width - 2*ob_rr_theme->paddingx,
438 ob_rr_theme->menu_title_height -
439 2*ob_rr_theme->paddingy);
440 text_a->surface.parent = item_a;
441 text_a->surface.parentx = ob_rr_theme->paddingx;
442 text_a->surface.parenty = ob_rr_theme->paddingy;
443 RrPaint(text_a, self->text,
444 self->area.width - 2*ob_rr_theme->paddingx,
445 ob_rr_theme->menu_title_height -
446 2*ob_rr_theme->paddingy);
447 } else {
448 /* unlabeled separaator */
449 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
450 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
451 self->a_separator->surface.parent = item_a;
452 self->a_separator->surface.parentx = PADDING;
453 self->a_separator->surface.parenty = PADDING;
454 self->a_separator->texture[0].data.lineart.color =
455 text_a->texture[0].data.text.color;
456 self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
457 self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
458 self->a_separator->texture[0].data.lineart.x2 =
459 self->area.width - 4*PADDING;
460 self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
461 RrPaint(self->a_separator, self->text,
462 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
463 }
464 break;
465 }
466
467 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
468 self->entry->data.normal.icon_data)
469 {
470 XMoveResizeWindow(ob_display, self->icon,
471 PADDING, frame->item_margin.top,
472 self->frame->item_h - frame->item_margin.top
473 - frame->item_margin.bottom,
474 self->frame->item_h - frame->item_margin.top
475 - frame->item_margin.bottom);
476 self->a_icon->texture[0].data.rgba.width =
477 self->entry->data.normal.icon_width;
478 self->a_icon->texture[0].data.rgba.height =
479 self->entry->data.normal.icon_height;
480 self->a_icon->texture[0].data.rgba.data =
481 self->entry->data.normal.icon_data;
482 self->a_icon->surface.parent = item_a;
483 self->a_icon->surface.parentx = PADDING;
484 self->a_icon->surface.parenty = frame->item_margin.top;
485 RrPaint(self->a_icon, self->icon,
486 self->frame->item_h - frame->item_margin.top
487 - frame->item_margin.bottom,
488 self->frame->item_h - frame->item_margin.top
489 - frame->item_margin.bottom);
490 XMapWindow(ob_display, self->icon);
491 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
492 self->entry->data.normal.mask)
493 {
494 RrColor *c;
495
496 XMoveResizeWindow(ob_display, self->icon,
497 PADDING, frame->item_margin.top,
498 self->frame->item_h - frame->item_margin.top
499 - frame->item_margin.bottom,
500 self->frame->item_h - frame->item_margin.top
501 - frame->item_margin.bottom);
502 self->a_mask->texture[0].data.mask.mask =
503 self->entry->data.normal.mask;
504
505 c = ((self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
506 !self->entry->data.normal.enabled) ?
507 self->entry->data.normal.mask_disabled_color :
508 (self == self->frame->selected ?
509 self->entry->data.normal.mask_selected_color :
510 self->entry->data.normal.mask_normal_color));
511 self->a_mask->texture[0].data.mask.color = c;
512
513 self->a_mask->surface.parent = item_a;
514 self->a_mask->surface.parentx = PADDING;
515 self->a_mask->surface.parenty = frame->item_margin.top;
516 RrPaint(self->a_mask, self->icon,
517 self->frame->item_h - frame->item_margin.top
518 - frame->item_margin.bottom,
519 self->frame->item_h - frame->item_margin.top
520 - frame->item_margin.bottom);
521 XMapWindow(ob_display, self->icon);
522 } else
523 XUnmapWindow(ob_display, self->icon);
524
525 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
526 RrAppearance *bullet_a;
527 XMoveResizeWindow(ob_display, self->bullet,
528 self->frame->text_x + self->frame->text_w
529 - self->frame->item_h + PADDING, PADDING,
530 self->frame->item_h - 2*PADDING,
531 self->frame->item_h - 2*PADDING);
532 bullet_a = (self == self->frame->selected ?
533 self->a_bullet_selected :
534 self->a_bullet_normal);
535 bullet_a->surface.parent = item_a;
536 bullet_a->surface.parentx =
537 self->frame->text_x + self->frame->text_w - self->frame->item_h
538 + PADDING;
539 bullet_a->surface.parenty = PADDING;
540 RrPaint(bullet_a, self->bullet,
541 self->frame->item_h - 2*PADDING,
542 self->frame->item_h - 2*PADDING);
543 XMapWindow(ob_display, self->bullet);
544 } else
545 XUnmapWindow(ob_display, self->bullet);
546
547 XFlush(ob_display);
548 }
549
550 static void menu_frame_render(ObMenuFrame *self)
551 {
552 gint w = 0, h = 0;
553 gint tw, th; /* temps */
554 GList *it;
555 gboolean has_icon = FALSE;
556 ObMenu *sub;
557 ObMenuEntryFrame *e;
558
559 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
560 XSetWindowBorder(ob_display, self->window,
561 RrColorPixel(ob_rr_theme->menu_b_color));
562
563 /* find text dimensions */
564
565 STRUT_SET(self->item_margin, 0, 0, 0, 0);
566
567 if (self->entries) {
568 ObMenuEntryFrame *e = self->entries->data;
569 gint l, t, r, b;
570
571 e->a_text_normal->texture[0].data.text.string = "";
572 RrMinSize(e->a_text_normal, &tw, &th);
573 tw += 2*PADDING;
574 th += 2*PADDING;
575 self->item_h = th;
576
577 RrMargins(e->a_normal, &l, &t, &r, &b);
578 STRUT_SET(self->item_margin,
579 MAX(self->item_margin.left, l),
580 MAX(self->item_margin.top, t),
581 MAX(self->item_margin.right, r),
582 MAX(self->item_margin.bottom, b));
583 RrMargins(e->a_selected, &l, &t, &r, &b);
584 STRUT_SET(self->item_margin,
585 MAX(self->item_margin.left, l),
586 MAX(self->item_margin.top, t),
587 MAX(self->item_margin.right, r),
588 MAX(self->item_margin.bottom, b));
589 RrMargins(e->a_disabled, &l, &t, &r, &b);
590 STRUT_SET(self->item_margin,
591 MAX(self->item_margin.left, l),
592 MAX(self->item_margin.top, t),
593 MAX(self->item_margin.right, r),
594 MAX(self->item_margin.bottom, b));
595 } else
596 self->item_h = 0;
597
598 /* render the entries */
599
600 for (it = self->entries; it; it = g_list_next(it)) {
601 RrAppearance *text_a;
602 e = it->data;
603
604 /* if the first entry is a labeled separator, then make its border
605 overlap with the menu's outside border */
606 if (it == self->entries &&
607 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
608 e->entry->data.separator.label)
609 {
610 h -= ob_rr_theme->mbwidth;
611 }
612
613 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
614 e->entry->data.separator.label)
615 {
616 e->border = ob_rr_theme->mbwidth;
617 }
618
619 RECT_SET_POINT(e->area, 0, h+e->border);
620 XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
621 XSetWindowBorderWidth(ob_display, e->window, e->border);
622 XSetWindowBorder(ob_display, e->window,
623 RrColorPixel(ob_rr_theme->menu_b_color));
624
625 text_a = ((e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
626 !e->entry->data.normal.enabled) ?
627 e->a_text_disabled :
628 (e == self->selected ?
629 e->a_text_selected :
630 e->a_text_normal));
631 switch (e->entry->type) {
632 case OB_MENU_ENTRY_TYPE_NORMAL:
633 text_a->texture[0].data.text.string = e->entry->data.normal.label;
634 RrMinSize(text_a, &tw, &th);
635 tw = MIN(tw, MAX_MENU_WIDTH);
636
637 if (e->entry->data.normal.icon_data ||
638 e->entry->data.normal.mask)
639 has_icon = TRUE;
640 break;
641 case OB_MENU_ENTRY_TYPE_SUBMENU:
642 sub = e->entry->data.submenu.submenu;
643 text_a->texture[0].data.text.string = sub ? sub->title : "";
644 RrMinSize(text_a, &tw, &th);
645 tw = MIN(tw, MAX_MENU_WIDTH);
646
647 if (e->entry->data.normal.icon_data ||
648 e->entry->data.normal.mask)
649 has_icon = TRUE;
650
651 tw += self->item_h - PADDING;
652 break;
653 case OB_MENU_ENTRY_TYPE_SEPARATOR:
654 if (e->entry->data.separator.label != NULL) {
655 e->a_text_title->texture[0].data.text.string =
656 e->entry->data.separator.label;
657 RrMinSize(e->a_text_title, &tw, &th);
658 tw = MIN(tw, MAX_MENU_WIDTH);
659 th = ob_rr_theme->menu_title_height +
660 (ob_rr_theme->mbwidth - PADDING) *2;
661 } else {
662 tw = 0;
663 th = SEPARATOR_HEIGHT;
664 }
665 break;
666 }
667 tw += 2*PADDING;
668 th += 2*PADDING;
669 w = MAX(w, tw);
670 h += th;
671 }
672
673 /* if the last entry is a labeled separator, then make its border
674 overlap with the menu's outside border */
675 it = g_list_last(self->entries);
676 e = it ? it->data : NULL;
677 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
678 e->entry->data.separator.label)
679 {
680 h -= ob_rr_theme->mbwidth;
681 }
682
683 self->text_x = PADDING;
684 self->text_w = w;
685
686 if (self->entries) {
687 if (has_icon) {
688 w += self->item_h + PADDING;
689 self->text_x += self->item_h + PADDING;
690 }
691 }
692
693 if (!w) w = 10;
694 if (!h) h = 3;
695
696 XResizeWindow(ob_display, self->window, w, h);
697
698 self->inner_w = w;
699
700 RrPaint(self->a_items, self->window, w, h);
701
702 for (it = self->entries; it; it = g_list_next(it))
703 menu_entry_frame_render(it->data);
704
705 w += ob_rr_theme->mbwidth * 2;
706 h += ob_rr_theme->mbwidth * 2;
707
708 RECT_SET_SIZE(self->area, w, h);
709
710 XFlush(ob_display);
711 }
712
713 static void menu_frame_update(ObMenuFrame *self)
714 {
715 GList *mit, *fit;
716
717 menu_pipe_execute(self->menu);
718 menu_find_submenus(self->menu);
719
720 self->selected = NULL;
721
722 for (mit = self->menu->entries, fit = self->entries; mit && fit;
723 mit = g_list_next(mit), fit = g_list_next(fit))
724 {
725 ObMenuEntryFrame *f = fit->data;
726 f->entry = mit->data;
727 }
728
729 while (mit) {
730 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
731 self->entries = g_list_append(self->entries, e);
732 mit = g_list_next(mit);
733 }
734
735 while (fit) {
736 GList *n = g_list_next(fit);
737 menu_entry_frame_free(fit->data);
738 self->entries = g_list_delete_link(self->entries, fit);
739 fit = n;
740 }
741
742 menu_frame_render(self);
743 }
744
745 static gboolean menu_frame_is_visible(ObMenuFrame *self)
746 {
747 return !!(g_list_find(menu_frame_visible, self));
748 }
749
750 static gboolean menu_frame_show(ObMenuFrame *self)
751 {
752 GList *it;
753
754 /* determine if the underlying menu is already visible */
755 for (it = menu_frame_visible; it; it = g_list_next(it)) {
756 ObMenuFrame *f = it->data;
757 if (f->menu == self->menu)
758 break;
759 }
760 if (!it) {
761 if (self->menu->update_func)
762 if (!self->menu->update_func(self, self->menu->data))
763 return FALSE;
764 }
765
766 if (menu_frame_visible == NULL) {
767 /* no menus shown yet */
768 if (!grab_pointer(TRUE, TRUE, OB_CURSOR_POINTER))
769 return FALSE;
770 if (!grab_keyboard(TRUE)) {
771 grab_pointer(FALSE, TRUE, OB_CURSOR_POINTER);
772 return FALSE;
773 }
774 }
775
776 menu_frame_update(self);
777
778 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
779
780 return TRUE;
781 }
782
783 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
784 gint button)
785 {
786 guint i;
787
788 if (menu_frame_is_visible(self))
789 return TRUE;
790 if (!menu_frame_show(self))
791 return FALSE;
792
793 /* find the monitor the menu is on */
794 for (i = 0; i < screen_num_monitors; ++i) {
795 Rect *a = screen_physical_area_monitor(i);
796 if (RECT_CONTAINS(*a, x, y)) {
797 self->monitor = i;
798 break;
799 }
800 }
801
802 if (self->menu->place_func)
803 self->menu->place_func(self, &x, &y, button, self->menu->data);
804 else
805 menu_frame_place_topmenu(self, &x, &y);
806
807 menu_frame_move(self, x, y);
808
809 XMapWindow(ob_display, self->window);
810
811 return TRUE;
812 }
813
814 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
815 ObMenuEntryFrame *parent_entry)
816 {
817 ObMenuEntryFrame *e;
818 gint x, y, dx, dy;
819
820 if (menu_frame_is_visible(self))
821 return TRUE;
822
823 self->monitor = parent->monitor;
824 self->parent = parent;
825 self->parent_entry = parent_entry;
826
827 /* set up parent's child to be us */
828 if (parent->child)
829 menu_frame_hide(parent->child);
830 parent->child = self;
831
832 if (!menu_frame_show(self))
833 return FALSE;
834
835 menu_frame_place_submenu(self, &x, &y);
836 menu_frame_move_on_screen(self, x, y, &dx, &dy);
837
838 if (dx != 0) {
839 /*try the other side */
840 self->direction_right = !self->direction_right;
841 menu_frame_place_submenu(self, &x, &y);
842 menu_frame_move_on_screen(self, x, y, &dx, &dy);
843 }
844 menu_frame_move(self, x + dx, y + dy);
845
846 XMapWindow(ob_display, self->window);
847
848 if (screen_pointer_pos(&dx, &dy) && (e = menu_entry_frame_under(dx, dy)) &&
849 e->frame == self)
850 ++e->ignore_enters;
851
852 return TRUE;
853 }
854
855 void menu_frame_hide(ObMenuFrame *self)
856 {
857 GList *it = g_list_find(menu_frame_visible, self);
858
859 if (!it)
860 return;
861
862 if (self->child)
863 menu_frame_hide(self->child);
864
865 if (self->parent)
866 self->parent->child = NULL;
867 self->parent = NULL;
868 self->parent_entry = NULL;
869
870 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
871
872 if (menu_frame_visible == NULL) {
873 /* last menu shown */
874 grab_pointer(FALSE, TRUE, OB_CURSOR_NONE);
875 grab_keyboard(FALSE);
876 }
877
878 XUnmapWindow(ob_display, self->window);
879
880 menu_frame_free(self);
881 }
882
883 void menu_frame_hide_all()
884 {
885 GList *it;
886
887 if (config_submenu_show_delay) {
888 /* remove any submenu open requests */
889 ob_main_loop_timeout_remove(ob_main_loop,
890 menu_entry_frame_submenu_timeout);
891 }
892 if ((it = g_list_last(menu_frame_visible)))
893 menu_frame_hide(it->data);
894 }
895
896 void menu_frame_hide_all_client(ObClient *client)
897 {
898 GList *it = g_list_last(menu_frame_visible);
899 if (it) {
900 ObMenuFrame *f = it->data;
901 if (f->client == client)
902 menu_frame_hide(f);
903 }
904 }
905
906
907 ObMenuFrame* menu_frame_under(gint x, gint y)
908 {
909 ObMenuFrame *ret = NULL;
910 GList *it;
911
912 for (it = menu_frame_visible; it; it = g_list_next(it)) {
913 ObMenuFrame *f = it->data;
914
915 if (RECT_CONTAINS(f->area, x, y)) {
916 ret = f;
917 break;
918 }
919 }
920 return ret;
921 }
922
923 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
924 {
925 ObMenuFrame *frame;
926 ObMenuEntryFrame *ret = NULL;
927 GList *it;
928
929 if ((frame = menu_frame_under(x, y))) {
930 x -= ob_rr_theme->mbwidth + frame->area.x;
931 y -= ob_rr_theme->mbwidth + frame->area.y;
932
933 for (it = frame->entries; it; it = g_list_next(it)) {
934 ObMenuEntryFrame *e = it->data;
935
936 if (RECT_CONTAINS(e->area, x, y)) {
937 ret = e;
938 break;
939 }
940 }
941 }
942 return ret;
943 }
944
945 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
946 {
947 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
948 return FALSE;
949 }
950
951 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
952 gboolean immediate)
953 {
954 ObMenuEntryFrame *old = self->selected;
955 ObMenuFrame *oldchild = self->child;
956
957 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
958 entry = old;
959
960 if (old == entry) return;
961
962 if (config_submenu_show_delay) {
963 /* remove any submenu open requests */
964 ob_main_loop_timeout_remove(ob_main_loop,
965 menu_entry_frame_submenu_timeout);
966 }
967
968 self->selected = entry;
969
970 if (old)
971 menu_entry_frame_render(old);
972 if (oldchild)
973 menu_frame_hide(oldchild);
974
975 if (self->selected) {
976 menu_entry_frame_render(self->selected);
977
978 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
979 if (config_submenu_show_delay && !immediate) {
980 /* initiate a new submenu open request */
981 ob_main_loop_timeout_add(ob_main_loop,
982 config_submenu_show_delay * 1000,
983 menu_entry_frame_submenu_timeout,
984 self->selected, g_direct_equal,
985 NULL);
986 } else {
987 menu_entry_frame_show_submenu(self->selected);
988 }
989 }
990 }
991 }
992
993 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
994 {
995 ObMenuFrame *f;
996
997 if (!self->entry->data.submenu.submenu) return;
998
999 f = menu_frame_new(self->entry->data.submenu.submenu,
1000 self->frame->client);
1001 /* pass our direction on to our child */
1002 f->direction_right = self->frame->direction_right;
1003
1004 menu_frame_show_submenu(f, self->frame, self);
1005 }
1006
1007 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
1008 {
1009 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1010 self->entry->data.normal.enabled)
1011 {
1012 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1013 gets freed */
1014 ObMenuEntry *entry = self->entry;
1015 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1016 gpointer data = self->frame->menu->data;
1017 GSList *acts = self->entry->data.normal.actions;
1018 ObClient *client = self->frame->client;
1019
1020 /* release grabs before executing the shit */
1021 if (!(state & ControlMask))
1022 menu_frame_hide_all();
1023
1024 if (func)
1025 func(entry, state, data, time);
1026 else
1027 action_run(acts, client, state, time);
1028 }
1029 }
1030
1031 void menu_frame_select_previous(ObMenuFrame *self)
1032 {
1033 GList *it = NULL, *start;
1034
1035 if (self->entries) {
1036 start = it = g_list_find(self->entries, self->selected);
1037 while (TRUE) {
1038 ObMenuEntryFrame *e;
1039
1040 it = it ? g_list_previous(it) : g_list_last(self->entries);
1041 if (it == start)
1042 break;
1043
1044 if (it) {
1045 e = it->data;
1046 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1047 break;
1048 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1049 e->entry->data.normal.enabled)
1050 break;
1051 }
1052 }
1053 }
1054 menu_frame_select(self, it ? it->data : NULL, TRUE);
1055 }
1056
1057 void menu_frame_select_next(ObMenuFrame *self)
1058 {
1059 GList *it = NULL, *start;
1060
1061 if (self->entries) {
1062 start = it = g_list_find(self->entries, self->selected);
1063 while (TRUE) {
1064 ObMenuEntryFrame *e;
1065
1066 it = it ? g_list_next(it) : self->entries;
1067 if (it == start)
1068 break;
1069
1070 if (it) {
1071 e = it->data;
1072 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1073 break;
1074 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1075 e->entry->data.normal.enabled)
1076 break;
1077 }
1078 }
1079 }
1080 menu_frame_select(self, it ? it->data : NULL, TRUE);
1081 }
This page took 0.085982 seconds and 3 git commands to generate.