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