]> Dogcows Code - chaz/openbox/blob - openbox/menuframe.c
themerc.xsd is not executable
[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 ITEM_HEIGHT (ob_rr_theme->menu_font_height + 2*PADDING)
35
36 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask |\
37 LeaveWindowMask)
38 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
39 ButtonPressMask | ButtonReleaseMask)
40
41 GList *menu_frame_visible;
42
43 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
44 ObMenuFrame *frame);
45 static void menu_entry_frame_free(ObMenuEntryFrame *self);
46 static void menu_frame_update(ObMenuFrame *self);
47 static gboolean menu_entry_frame_submenu_timeout(gpointer data);
48
49 static Window createWindow(Window parent, gulong mask,
50 XSetWindowAttributes *attrib)
51 {
52 return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
53 RrDepth(ob_rr_inst), InputOutput,
54 RrVisual(ob_rr_inst), mask, attrib);
55 }
56
57 GHashTable *menu_frame_map;
58
59 void menu_frame_startup(gboolean reconfig)
60 {
61 if (reconfig) return;
62
63 menu_frame_map = g_hash_table_new(g_int_hash, g_int_equal);
64 }
65
66 void menu_frame_shutdown(gboolean reconfig)
67 {
68 if (reconfig) return;
69
70 g_hash_table_destroy(menu_frame_map);
71 }
72
73 ObMenuFrame* menu_frame_new(ObMenu *menu, guint show_from, ObClient *client)
74 {
75 ObMenuFrame *self;
76 XSetWindowAttributes attr;
77
78 self = g_new0(ObMenuFrame, 1);
79 self->type = Window_Menu;
80 self->menu = menu;
81 self->selected = NULL;
82 self->client = client;
83 self->direction_right = TRUE;
84 self->show_from = show_from;
85
86 attr.event_mask = FRAME_EVENTMASK;
87 self->window = createWindow(RootWindow(ob_display, ob_screen),
88 CWEventMask, &attr);
89
90 XSetWindowBorderWidth(ob_display, self->window, ob_rr_theme->mbwidth);
91 XSetWindowBorder(ob_display, self->window,
92 RrColorPixel(ob_rr_theme->menu_border_color));
93
94 self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
95 self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
96
97 stacking_add(MENU_AS_WINDOW(self));
98
99 return self;
100 }
101
102 void menu_frame_free(ObMenuFrame *self)
103 {
104 if (self) {
105 while (self->entries) {
106 menu_entry_frame_free(self->entries->data);
107 self->entries = g_list_delete_link(self->entries, self->entries);
108 }
109
110 stacking_remove(MENU_AS_WINDOW(self));
111
112 XDestroyWindow(ob_display, self->window);
113
114 RrAppearanceFree(self->a_items);
115 RrAppearanceFree(self->a_title);
116
117 g_free(self);
118 }
119 }
120
121 static ObMenuEntryFrame* menu_entry_frame_new(ObMenuEntry *entry,
122 ObMenuFrame *frame)
123 {
124 ObMenuEntryFrame *self;
125 XSetWindowAttributes attr;
126
127 self = g_new0(ObMenuEntryFrame, 1);
128 self->entry = entry;
129 self->frame = frame;
130
131 menu_entry_ref(entry);
132
133 attr.event_mask = ENTRY_EVENTMASK;
134 self->window = createWindow(self->frame->window, CWEventMask, &attr);
135 self->text = createWindow(self->window, 0, NULL);
136 g_hash_table_insert(menu_frame_map, &self->window, self);
137 g_hash_table_insert(menu_frame_map, &self->text, self);
138 if (entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
139 self->icon = createWindow(self->window, 0, NULL);
140 g_hash_table_insert(menu_frame_map, &self->icon, self);
141 }
142 if (entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
143 self->bullet = createWindow(self->window, 0, NULL);
144 g_hash_table_insert(menu_frame_map, &self->bullet, self);
145 }
146
147 XMapWindow(ob_display, self->window);
148 XMapWindow(ob_display, self->text);
149
150 self->a_normal = RrAppearanceCopy(ob_rr_theme->a_menu_normal);
151 self->a_selected = RrAppearanceCopy(ob_rr_theme->a_menu_selected);
152 self->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
153 self->a_disabled_selected =
154 RrAppearanceCopy(ob_rr_theme->a_menu_disabled_selected);
155
156 if (entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR) {
157 self->a_separator = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
158 self->a_separator->texture[0].type = RR_TEXTURE_LINE_ART;
159 } else {
160 self->a_icon = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
161 self->a_icon->texture[0].type = RR_TEXTURE_RGBA;
162 self->a_mask = RrAppearanceCopy(ob_rr_theme->a_clear_tex);
163 self->a_mask->texture[0].type = RR_TEXTURE_MASK;
164 self->a_bullet_normal =
165 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_normal);
166 self->a_bullet_selected =
167 RrAppearanceCopy(ob_rr_theme->a_menu_bullet_selected);
168 }
169
170 self->a_text_normal =
171 RrAppearanceCopy(ob_rr_theme->a_menu_text_normal);
172 self->a_text_selected =
173 RrAppearanceCopy(ob_rr_theme->a_menu_text_selected);
174 self->a_text_disabled =
175 RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled);
176 self->a_text_disabled_selected =
177 RrAppearanceCopy(ob_rr_theme->a_menu_text_disabled_selected);
178 self->a_text_title =
179 RrAppearanceCopy(ob_rr_theme->a_menu_text_title);
180
181 return self;
182 }
183
184 static void menu_entry_frame_free(ObMenuEntryFrame *self)
185 {
186 if (self) {
187 menu_entry_unref(self->entry);
188
189 XDestroyWindow(ob_display, self->text);
190 XDestroyWindow(ob_display, self->window);
191 g_hash_table_remove(menu_frame_map, &self->text);
192 g_hash_table_remove(menu_frame_map, &self->window);
193 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL) {
194 XDestroyWindow(ob_display, self->icon);
195 g_hash_table_remove(menu_frame_map, &self->icon);
196 }
197 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
198 XDestroyWindow(ob_display, self->bullet);
199 g_hash_table_remove(menu_frame_map, &self->bullet);
200 }
201
202 RrAppearanceFree(self->a_normal);
203 RrAppearanceFree(self->a_selected);
204 RrAppearanceFree(self->a_disabled);
205 RrAppearanceFree(self->a_disabled_selected);
206
207 RrAppearanceFree(self->a_separator);
208 RrAppearanceFree(self->a_icon);
209 RrAppearanceFree(self->a_mask);
210 RrAppearanceFree(self->a_text_normal);
211 RrAppearanceFree(self->a_text_selected);
212 RrAppearanceFree(self->a_text_disabled);
213 RrAppearanceFree(self->a_text_disabled_selected);
214 RrAppearanceFree(self->a_text_title);
215 RrAppearanceFree(self->a_bullet_normal);
216 RrAppearanceFree(self->a_bullet_selected);
217
218 g_free(self);
219 }
220 }
221
222 void menu_frame_move(ObMenuFrame *self, gint x, gint y)
223 {
224 RECT_SET_POINT(self->area, x, y);
225 XMoveWindow(ob_display, self->window, self->area.x, self->area.y);
226 }
227
228 static void menu_frame_place_topmenu(ObMenuFrame *self, gint *x, gint *y)
229 {
230 gint dx, dy;
231
232 if (config_menu_middle) {
233 gint myx;
234
235 myx = *x;
236 *y -= self->area.height / 2;
237
238 /* try to the right of the cursor */
239 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
240 self->direction_right = TRUE;
241 if (dx != 0) {
242 /* try to the left of the cursor */
243 myx = *x - self->area.width;
244 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
245 self->direction_right = FALSE;
246 }
247 if (dx != 0) {
248 /* if didnt fit on either side so just use what it says */
249 myx = *x;
250 menu_frame_move_on_screen(self, myx, *y, &dx, &dy);
251 self->direction_right = TRUE;
252 }
253 *x = myx + dx;
254 *y += dy;
255 } else {
256 gint myx, myy;
257
258 myx = *x;
259 myy = *y;
260
261 /* try to the bottom right of the cursor */
262 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
263 self->direction_right = TRUE;
264 if (dx != 0 || dy != 0) {
265 /* try to the bottom left of the cursor */
266 myx = *x - self->area.width;
267 myy = *y;
268 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
269 self->direction_right = FALSE;
270 }
271 if (dx != 0 || dy != 0) {
272 /* try to the top right of the cursor */
273 myx = *x;
274 myy = *y - self->area.height;
275 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
276 self->direction_right = TRUE;
277 }
278 if (dx != 0 || dy != 0) {
279 /* try to the top left of the cursor */
280 myx = *x - self->area.width;
281 myy = *y - self->area.height;
282 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
283 self->direction_right = FALSE;
284 }
285 if (dx != 0 || dy != 0) {
286 /* if didnt fit on either side so just use what it says */
287 myx = *x;
288 myy = *y;
289 menu_frame_move_on_screen(self, myx, myy, &dx, &dy);
290 self->direction_right = TRUE;
291 }
292 *x = myx + dx;
293 *y = myy + dy;
294 }
295 }
296
297 static void menu_frame_place_submenu(ObMenuFrame *self, gint *x, gint *y)
298 {
299 gint overlap;
300 gint bwidth;
301
302 overlap = ob_rr_theme->menu_overlap;
303 bwidth = ob_rr_theme->mbwidth;
304
305 if (self->direction_right)
306 *x = self->parent->area.x + self->parent->area.width -
307 overlap - bwidth;
308 else
309 *x = self->parent->area.x - self->area.width + overlap + bwidth;
310
311 *y = self->parent->area.y + self->parent_entry->area.y;
312 if (config_menu_middle)
313 *y -= (self->area.height - (bwidth * 2) - ITEM_HEIGHT) / 2;
314 else
315 *y += overlap;
316 }
317
318 void menu_frame_move_on_screen(ObMenuFrame *self, gint x, gint y,
319 gint *dx, gint *dy)
320 {
321 Rect *a = NULL;
322 gint pos, half;
323
324 *dx = *dy = 0;
325
326 a = screen_physical_area_monitor(self->monitor);
327
328 half = g_list_length(self->entries) / 2;
329 pos = g_list_index(self->entries, self->selected);
330
331 /* if in the bottom half then check this stuff first, will keep the bottom
332 edge of the menu visible */
333 if (pos > half) {
334 *dx = MAX(*dx, a->x - x);
335 *dy = MAX(*dy, a->y - y);
336 }
337 *dx = MIN(*dx, (a->x + a->width) - (x + self->area.width));
338 *dy = MIN(*dy, (a->y + a->height) - (y + self->area.height));
339 /* if in the top half then check this stuff last, will keep the top
340 edge of the menu visible */
341 if (pos <= half) {
342 *dx = MAX(*dx, a->x - x);
343 *dy = MAX(*dy, a->y - y);
344 }
345 }
346
347 static void menu_entry_frame_render(ObMenuEntryFrame *self)
348 {
349 RrAppearance *item_a, *text_a;
350 gint th; /* temp */
351 ObMenu *sub;
352 ObMenuFrame *frame = self->frame;
353
354 switch (self->entry->type) {
355 case OB_MENU_ENTRY_TYPE_NORMAL:
356 case OB_MENU_ENTRY_TYPE_SUBMENU:
357 item_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
358 !self->entry->data.normal.enabled ?
359 /* disabled */
360 (self == self->frame->selected ?
361 self->a_disabled_selected : self->a_disabled) :
362 /* enabled */
363 (self == self->frame->selected ?
364 self->a_selected : self->a_normal));
365 th = ITEM_HEIGHT;
366 break;
367 case OB_MENU_ENTRY_TYPE_SEPARATOR:
368 if (self->entry->data.separator.label) {
369 item_a = self->frame->a_title;
370 th = ob_rr_theme->menu_title_height;
371 } else {
372 item_a = self->a_normal;
373 th = SEPARATOR_HEIGHT + 2*PADDING;
374 }
375 break;
376 default:
377 g_assert_not_reached();
378 }
379 RECT_SET_SIZE(self->area, self->frame->inner_w, th);
380 XResizeWindow(ob_display, self->window,
381 self->area.width, self->area.height);
382 item_a->surface.parent = self->frame->a_items;
383 item_a->surface.parentx = self->area.x;
384 item_a->surface.parenty = self->area.y;
385 RrPaint(item_a, self->window, self->area.width, self->area.height);
386
387 switch (self->entry->type) {
388 case OB_MENU_ENTRY_TYPE_NORMAL:
389 text_a = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
390 !self->entry->data.normal.enabled ?
391 /* disabled */
392 (self == self->frame->selected ?
393 self->a_text_disabled_selected : self->a_text_disabled) :
394 /* enabled */
395 (self == self->frame->selected ?
396 self->a_text_selected : self->a_text_normal));
397 text_a->texture[0].data.text.string = self->entry->data.normal.label;
398 if (self->entry->data.normal.shortcut &&
399 (self->frame->menu->show_all_shortcuts ||
400 self->entry->data.normal.shortcut_position > 0))
401 {
402 text_a->texture[0].data.text.shortcut = TRUE;
403 text_a->texture[0].data.text.shortcut_pos =
404 self->entry->data.normal.shortcut_position;
405 } else
406 text_a->texture[0].data.text.shortcut = FALSE;
407 break;
408 case OB_MENU_ENTRY_TYPE_SUBMENU:
409 text_a = (self == self->frame->selected ?
410 self->a_text_selected :
411 self->a_text_normal);
412 sub = self->entry->data.submenu.submenu;
413 text_a->texture[0].data.text.string = sub ? sub->title : "";
414 if (sub->shortcut && (self->frame->menu->show_all_shortcuts ||
415 sub->shortcut_position > 0))
416 {
417 text_a->texture[0].data.text.shortcut = TRUE;
418 text_a->texture[0].data.text.shortcut_pos = sub->shortcut_position;
419 } else
420 text_a->texture[0].data.text.shortcut = FALSE;
421 break;
422 case OB_MENU_ENTRY_TYPE_SEPARATOR:
423 if (self->entry->data.separator.label != NULL)
424 text_a = self->a_text_title;
425 else
426 text_a = self->a_text_normal;
427 break;
428 }
429
430 switch (self->entry->type) {
431 case OB_MENU_ENTRY_TYPE_NORMAL:
432 XMoveResizeWindow(ob_display, self->text,
433 self->frame->text_x, PADDING,
434 self->frame->text_w,
435 ITEM_HEIGHT - 2*PADDING);
436 text_a->surface.parent = item_a;
437 text_a->surface.parentx = self->frame->text_x;
438 text_a->surface.parenty = PADDING;
439 RrPaint(text_a, self->text, self->frame->text_w,
440 ITEM_HEIGHT - 2*PADDING);
441 break;
442 case OB_MENU_ENTRY_TYPE_SUBMENU:
443 XMoveResizeWindow(ob_display, self->text,
444 self->frame->text_x, PADDING,
445 self->frame->text_w - ITEM_HEIGHT,
446 ITEM_HEIGHT - 2*PADDING);
447 text_a->surface.parent = item_a;
448 text_a->surface.parentx = self->frame->text_x;
449 text_a->surface.parenty = PADDING;
450 RrPaint(text_a, self->text, self->frame->text_w - ITEM_HEIGHT,
451 ITEM_HEIGHT - 2*PADDING);
452 break;
453 case OB_MENU_ENTRY_TYPE_SEPARATOR:
454 if (self->entry->data.separator.label != NULL) {
455 /* labeled separator */
456 XMoveResizeWindow(ob_display, self->text,
457 ob_rr_theme->paddingx, ob_rr_theme->paddingy,
458 self->area.width - 2*ob_rr_theme->paddingx,
459 ob_rr_theme->menu_title_height -
460 2*ob_rr_theme->paddingy);
461 text_a->surface.parent = item_a;
462 text_a->surface.parentx = ob_rr_theme->paddingx;
463 text_a->surface.parenty = ob_rr_theme->paddingy;
464 RrPaint(text_a, self->text,
465 self->area.width - 2*ob_rr_theme->paddingx,
466 ob_rr_theme->menu_title_height -
467 2*ob_rr_theme->paddingy);
468 } else {
469 /* unlabeled separaator */
470 XMoveResizeWindow(ob_display, self->text, PADDING, PADDING,
471 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
472 self->a_separator->surface.parent = item_a;
473 self->a_separator->surface.parentx = PADDING;
474 self->a_separator->surface.parenty = PADDING;
475 self->a_separator->texture[0].data.lineart.color =
476 text_a->texture[0].data.text.color;
477 self->a_separator->texture[0].data.lineart.x1 = 2*PADDING;
478 self->a_separator->texture[0].data.lineart.y1 = SEPARATOR_HEIGHT/2;
479 self->a_separator->texture[0].data.lineart.x2 =
480 self->area.width - 4*PADDING;
481 self->a_separator->texture[0].data.lineart.y2 = SEPARATOR_HEIGHT/2;
482 RrPaint(self->a_separator, self->text,
483 self->area.width - 2*PADDING, SEPARATOR_HEIGHT);
484 }
485 break;
486 }
487
488 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
489 self->entry->data.normal.icon_data)
490 {
491 XMoveResizeWindow(ob_display, self->icon,
492 PADDING, frame->item_margin.top,
493 ITEM_HEIGHT - frame->item_margin.top
494 - frame->item_margin.bottom,
495 ITEM_HEIGHT - frame->item_margin.top
496 - frame->item_margin.bottom);
497 self->a_icon->texture[0].data.rgba.width =
498 self->entry->data.normal.icon_width;
499 self->a_icon->texture[0].data.rgba.height =
500 self->entry->data.normal.icon_height;
501 self->a_icon->texture[0].data.rgba.data =
502 self->entry->data.normal.icon_data;
503 self->a_icon->surface.parent = item_a;
504 self->a_icon->surface.parentx = PADDING;
505 self->a_icon->surface.parenty = frame->item_margin.top;
506 RrPaint(self->a_icon, self->icon,
507 ITEM_HEIGHT - frame->item_margin.top
508 - frame->item_margin.bottom,
509 ITEM_HEIGHT - frame->item_margin.top
510 - frame->item_margin.bottom);
511 XMapWindow(ob_display, self->icon);
512 } else if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
513 self->entry->data.normal.mask)
514 {
515 RrColor *c;
516
517 XMoveResizeWindow(ob_display, self->icon,
518 PADDING, frame->item_margin.top,
519 ITEM_HEIGHT - frame->item_margin.top
520 - frame->item_margin.bottom,
521 ITEM_HEIGHT - frame->item_margin.top
522 - frame->item_margin.bottom);
523 self->a_mask->texture[0].data.mask.mask =
524 self->entry->data.normal.mask;
525
526 c = (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
527 !self->entry->data.normal.enabled ?
528 /* disabled */
529 (self == self->frame->selected ?
530 self->entry->data.normal.mask_disabled_selected_color :
531 self->entry->data.normal.mask_disabled_color) :
532 /* enabled */
533 (self == self->frame->selected ?
534 self->entry->data.normal.mask_selected_color :
535 self->entry->data.normal.mask_normal_color));
536 self->a_mask->texture[0].data.mask.color = c;
537
538 self->a_mask->surface.parent = item_a;
539 self->a_mask->surface.parentx = PADDING;
540 self->a_mask->surface.parenty = frame->item_margin.top;
541 RrPaint(self->a_mask, self->icon,
542 ITEM_HEIGHT - frame->item_margin.top
543 - frame->item_margin.bottom,
544 ITEM_HEIGHT - frame->item_margin.top
545 - frame->item_margin.bottom);
546 XMapWindow(ob_display, self->icon);
547 } else
548 XUnmapWindow(ob_display, self->icon);
549
550 if (self->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
551 RrAppearance *bullet_a;
552 XMoveResizeWindow(ob_display, self->bullet,
553 self->frame->text_x + self->frame->text_w -
554 ITEM_HEIGHT + PADDING, PADDING,
555 ITEM_HEIGHT - 2*PADDING,
556 ITEM_HEIGHT - 2*PADDING);
557 bullet_a = (self == self->frame->selected ?
558 self->a_bullet_selected :
559 self->a_bullet_normal);
560 bullet_a->surface.parent = item_a;
561 bullet_a->surface.parentx =
562 self->frame->text_x + self->frame->text_w - ITEM_HEIGHT + PADDING;
563 bullet_a->surface.parenty = PADDING;
564 RrPaint(bullet_a, self->bullet,
565 ITEM_HEIGHT - 2*PADDING,
566 ITEM_HEIGHT - 2*PADDING);
567 XMapWindow(ob_display, self->bullet);
568 } else
569 XUnmapWindow(ob_display, self->bullet);
570
571 XFlush(ob_display);
572 }
573
574 /*! this code is taken from the menu_frame_render. if that changes, this won't
575 work.. */
576 static gint menu_entry_frame_get_height(ObMenuEntryFrame *self,
577 gboolean first_entry,
578 gboolean last_entry)
579 {
580 ObMenuEntryType t;
581 gint h = 0;
582
583 h += 2*PADDING;
584
585 if (self)
586 t = self->entry->type;
587 else
588 /* this is the More... entry, it's NORMAL type */
589 t = OB_MENU_ENTRY_TYPE_NORMAL;
590
591 switch (t) {
592 case OB_MENU_ENTRY_TYPE_NORMAL:
593 case OB_MENU_ENTRY_TYPE_SUBMENU:
594 h += ob_rr_theme->menu_font_height;
595 break;
596 case OB_MENU_ENTRY_TYPE_SEPARATOR:
597 if (self->entry->data.separator.label != NULL) {
598 h += ob_rr_theme->menu_title_height +
599 (ob_rr_theme->mbwidth - PADDING) * 2;
600
601 /* if the first entry is a labeled separator, then make its border
602 overlap with the menu's outside border */
603 if (first_entry)
604 h -= ob_rr_theme->mbwidth;
605 /* if the last entry is a labeled separator, then make its border
606 overlap with the menu's outside border */
607 if (last_entry)
608 h -= ob_rr_theme->mbwidth;
609 } else {
610 h += SEPARATOR_HEIGHT;
611 }
612 break;
613 }
614
615 return h;
616 }
617
618 void menu_frame_render(ObMenuFrame *self)
619 {
620 gint w = 0, h = 0;
621 gint tw, th; /* temps */
622 GList *it;
623 gboolean has_icon = FALSE;
624 ObMenu *sub;
625 ObMenuEntryFrame *e;
626
627 /* find text dimensions */
628
629 STRUT_SET(self->item_margin, 0, 0, 0, 0);
630
631 if (self->entries) {
632 ObMenuEntryFrame *e = self->entries->data;
633 gint l, t, r, b;
634
635 e->a_text_normal->texture[0].data.text.string = "";
636 tw = RrMinWidth(e->a_text_normal);
637 tw += 2*PADDING;
638
639 th = ITEM_HEIGHT;
640
641 RrMargins(e->a_normal, &l, &t, &r, &b);
642 STRUT_SET(self->item_margin,
643 MAX(self->item_margin.left, l),
644 MAX(self->item_margin.top, t),
645 MAX(self->item_margin.right, r),
646 MAX(self->item_margin.bottom, b));
647 RrMargins(e->a_selected, &l, &t, &r, &b);
648 STRUT_SET(self->item_margin,
649 MAX(self->item_margin.left, l),
650 MAX(self->item_margin.top, t),
651 MAX(self->item_margin.right, r),
652 MAX(self->item_margin.bottom, b));
653 RrMargins(e->a_disabled, &l, &t, &r, &b);
654 STRUT_SET(self->item_margin,
655 MAX(self->item_margin.left, l),
656 MAX(self->item_margin.top, t),
657 MAX(self->item_margin.right, r),
658 MAX(self->item_margin.bottom, b));
659 RrMargins(e->a_disabled_selected, &l, &t, &r, &b);
660 STRUT_SET(self->item_margin,
661 MAX(self->item_margin.left, l),
662 MAX(self->item_margin.top, t),
663 MAX(self->item_margin.right, r),
664 MAX(self->item_margin.bottom, b));
665 }
666
667 /* render the entries */
668
669 for (it = self->entries; it; it = g_list_next(it)) {
670 RrAppearance *text_a;
671 e = it->data;
672
673 /* if the first entry is a labeled separator, then make its border
674 overlap with the menu's outside border */
675 if (it == self->entries &&
676 e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
677 e->entry->data.separator.label)
678 {
679 h -= ob_rr_theme->mbwidth;
680 }
681
682 if (e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
683 e->entry->data.separator.label)
684 {
685 e->border = ob_rr_theme->mbwidth;
686 }
687
688 RECT_SET_POINT(e->area, 0, h+e->border);
689 XMoveWindow(ob_display, e->window, e->area.x-e->border, e->area.y-e->border);
690 XSetWindowBorderWidth(ob_display, e->window, e->border);
691 XSetWindowBorder(ob_display, e->window,
692 RrColorPixel(ob_rr_theme->menu_border_color));
693
694
695 text_a = (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
696 !e->entry->data.normal.enabled ?
697 /* disabled */
698 (e == self->selected ?
699 e->a_text_disabled_selected : e->a_text_disabled) :
700 /* enabled */
701 (e == self->selected ?
702 e->a_text_selected : e->a_text_normal));
703 switch (e->entry->type) {
704 case OB_MENU_ENTRY_TYPE_NORMAL:
705 text_a->texture[0].data.text.string = e->entry->data.normal.label;
706 tw = RrMinWidth(text_a);
707 tw = MIN(tw, MAX_MENU_WIDTH);
708 th = ob_rr_theme->menu_font_height;
709
710 if (e->entry->data.normal.icon_data ||
711 e->entry->data.normal.mask)
712 has_icon = TRUE;
713 break;
714 case OB_MENU_ENTRY_TYPE_SUBMENU:
715 sub = e->entry->data.submenu.submenu;
716 text_a->texture[0].data.text.string = sub ? sub->title : "";
717 tw = RrMinWidth(text_a);
718 tw = MIN(tw, MAX_MENU_WIDTH);
719 th = ob_rr_theme->menu_font_height;
720
721 if (e->entry->data.normal.icon_data ||
722 e->entry->data.normal.mask)
723 has_icon = TRUE;
724
725 tw += ITEM_HEIGHT - PADDING;
726 break;
727 case OB_MENU_ENTRY_TYPE_SEPARATOR:
728 if (e->entry->data.separator.label != NULL) {
729 e->a_text_title->texture[0].data.text.string =
730 e->entry->data.separator.label;
731 tw = RrMinWidth(e->a_text_title);
732 tw = MIN(tw, MAX_MENU_WIDTH);
733 th = ob_rr_theme->menu_title_height +
734 (ob_rr_theme->mbwidth - PADDING) *2;
735 } else {
736 tw = 0;
737 th = SEPARATOR_HEIGHT;
738 }
739 break;
740 }
741 tw += 2*PADDING;
742 th += 2*PADDING;
743 w = MAX(w, tw);
744 h += th;
745 }
746
747 /* if the last entry is a labeled separator, then make its border
748 overlap with the menu's outside border */
749 it = g_list_last(self->entries);
750 e = it ? it->data : NULL;
751 if (e && e->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR &&
752 e->entry->data.separator.label)
753 {
754 h -= ob_rr_theme->mbwidth;
755 }
756
757 self->text_x = PADDING;
758 self->text_w = w;
759
760 if (self->entries) {
761 if (has_icon) {
762 w += ITEM_HEIGHT + PADDING;
763 self->text_x += ITEM_HEIGHT + PADDING;
764 }
765 }
766
767 if (!w) w = 10;
768 if (!h) h = 3;
769
770 XResizeWindow(ob_display, self->window, w, h);
771
772 self->inner_w = w;
773
774 RrPaint(self->a_items, self->window, w, h);
775
776 for (it = self->entries; it; it = g_list_next(it))
777 menu_entry_frame_render(it->data);
778
779 w += ob_rr_theme->mbwidth * 2;
780 h += ob_rr_theme->mbwidth * 2;
781
782 RECT_SET_SIZE(self->area, w, h);
783
784 XFlush(ob_display);
785 }
786
787 static void menu_frame_update(ObMenuFrame *self)
788 {
789 GList *mit, *fit;
790 Rect *a;
791 gint h;
792
793 menu_pipe_execute(self->menu);
794 menu_find_submenus(self->menu);
795
796 self->selected = NULL;
797
798 /* start at show_from */
799 mit = g_list_nth(self->menu->entries, self->show_from);
800
801 /* go through the menu's and frame's entries and connect the frame entries
802 to the menu entries */
803 for (fit = self->entries; mit && fit;
804 mit = g_list_next(mit), fit = g_list_next(fit))
805 {
806 ObMenuEntryFrame *f = fit->data;
807 f->entry = mit->data;
808 }
809
810 /* if there are more menu entries than in the frame, add them */
811 while (mit) {
812 ObMenuEntryFrame *e = menu_entry_frame_new(mit->data, self);
813 self->entries = g_list_append(self->entries, e);
814 mit = g_list_next(mit);
815 }
816
817 /* if there are more frame entries than menu entries then get rid of
818 them */
819 while (fit) {
820 GList *n = g_list_next(fit);
821 menu_entry_frame_free(fit->data);
822 self->entries = g_list_delete_link(self->entries, fit);
823 fit = n;
824 }
825
826 /* * make the menu fit on the screen */
827
828 /* calculate the height of the menu */
829 h = 0;
830 for (fit = self->entries; fit; fit = g_list_next(fit))
831 h += menu_entry_frame_get_height(fit->data,
832 fit == self->entries,
833 g_list_next(fit) == NULL);
834 /* add the border at the top and bottom */
835 h += ob_rr_theme->mbwidth * 2;
836
837 a = screen_physical_area_monitor(self->monitor);
838
839 if (h > a->height) {
840 GList *flast, *tmp;
841 gboolean last_entry = TRUE;
842
843 /* take the height of our More... entry into account */
844 h += menu_entry_frame_get_height(NULL, FALSE, TRUE);
845
846 /* start at the end of the entries */
847 flast = g_list_last(self->entries);
848
849 /* pull out all the entries from the frame that don't
850 fit on the screen, leaving at least 1 though */
851 while (h > a->height && g_list_previous(flast) != NULL) {
852 /* update the height, without this entry */
853 h -= menu_entry_frame_get_height(flast->data, FALSE, last_entry);
854
855 /* destroy the entry we're not displaying */
856 tmp = flast;
857 flast = g_list_previous(flast);
858 menu_entry_frame_free(tmp->data);
859 self->entries = g_list_delete_link(self->entries, tmp);
860
861 /* only the first one that we see is the last entry in the menu */
862 last_entry = FALSE;
863 };
864
865 {
866 ObMenuEntry *more_entry;
867 ObMenuEntryFrame *more_frame;
868 /* make the More... menu entry frame which will display in this
869 frame.
870 if self->menu->more_menu is NULL that means that this is already
871 More... menu, so just use ourself.
872 */
873 more_entry = menu_get_more((self->menu->more_menu ?
874 self->menu->more_menu :
875 self->menu),
876 /* continue where we left off */
877 self->show_from +
878 g_list_length(self->entries));
879 more_frame = menu_entry_frame_new(more_entry, self);
880 /* make it get deleted when the menu frame goes away */
881 menu_entry_unref(more_entry);
882
883 /* add our More... entry to the frame */
884 self->entries = g_list_append(self->entries, more_frame);
885 }
886 }
887
888 menu_frame_render(self);
889 }
890
891 static gboolean menu_frame_is_visible(ObMenuFrame *self)
892 {
893 return !!(g_list_find(menu_frame_visible, self));
894 }
895
896 static gboolean menu_frame_show(ObMenuFrame *self)
897 {
898 GList *it;
899
900 /* determine if the underlying menu is already visible */
901 for (it = menu_frame_visible; it; it = g_list_next(it)) {
902 ObMenuFrame *f = it->data;
903 if (f->menu == self->menu)
904 break;
905 }
906 if (!it) {
907 if (self->menu->update_func)
908 if (!self->menu->update_func(self, self->menu->data))
909 return FALSE;
910 }
911
912 if (menu_frame_visible == NULL) {
913 /* no menus shown yet */
914
915 /* grab the pointer in such a way as to pass through "owner events"
916 so that we can get enter/leave notifies in the menu. */
917 if (!grab_pointer(TRUE, FALSE, OB_CURSOR_POINTER))
918 return FALSE;
919 if (!grab_keyboard()) {
920 ungrab_pointer();
921 return FALSE;
922 }
923 }
924
925 menu_frame_update(self);
926
927 menu_frame_visible = g_list_prepend(menu_frame_visible, self);
928
929 if (self->menu->show_func)
930 self->menu->show_func(self, self->menu->data);
931
932 return TRUE;
933 }
934
935 gboolean menu_frame_show_topmenu(ObMenuFrame *self, gint x, gint y,
936 gint button)
937 {
938 gint px, py;
939 guint i;
940
941 if (menu_frame_is_visible(self))
942 return TRUE;
943 if (!menu_frame_show(self))
944 return FALSE;
945
946 /* find the monitor the menu is on */
947 for (i = 0; i < screen_num_monitors; ++i) {
948 Rect *a = screen_physical_area_monitor(i);
949 if (RECT_CONTAINS(*a, x, y)) {
950 self->monitor = i;
951 break;
952 }
953 }
954
955 if (self->menu->place_func)
956 self->menu->place_func(self, &x, &y, button, self->menu->data);
957 else
958 menu_frame_place_topmenu(self, &x, &y);
959
960 menu_frame_move(self, x, y);
961
962 XMapWindow(ob_display, self->window);
963
964 if (screen_pointer_pos(&px, &py)) {
965 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
966 if (e && e->frame == self)
967 e->ignore_enters++;
968 }
969
970 return TRUE;
971 }
972
973 gboolean menu_frame_show_submenu(ObMenuFrame *self, ObMenuFrame *parent,
974 ObMenuEntryFrame *parent_entry)
975 {
976 gint x, y, dx, dy;
977 gint px, py;
978
979 if (menu_frame_is_visible(self))
980 return TRUE;
981
982 self->monitor = parent->monitor;
983 self->parent = parent;
984 self->parent_entry = parent_entry;
985
986 /* set up parent's child to be us */
987 if (parent->child)
988 menu_frame_hide(parent->child);
989 parent->child = self;
990
991 if (!menu_frame_show(self))
992 return FALSE;
993
994 menu_frame_place_submenu(self, &x, &y);
995 menu_frame_move_on_screen(self, x, y, &dx, &dy);
996
997 if (dx != 0) {
998 /*try the other side */
999 self->direction_right = !self->direction_right;
1000 menu_frame_place_submenu(self, &x, &y);
1001 menu_frame_move_on_screen(self, x, y, &dx, &dy);
1002 }
1003 menu_frame_move(self, x + dx, y + dy);
1004
1005 XMapWindow(ob_display, self->window);
1006
1007 if (screen_pointer_pos(&px, &py)) {
1008 ObMenuEntryFrame *e = menu_entry_frame_under(px, py);
1009 if (e && e->frame == self)
1010 e->ignore_enters++;
1011 }
1012
1013 return TRUE;
1014 }
1015
1016 void menu_frame_hide(ObMenuFrame *self)
1017 {
1018 GList *it = g_list_find(menu_frame_visible, self);
1019
1020 if (!it)
1021 return;
1022
1023 if (self->menu->hide_func)
1024 self->menu->hide_func(self, self->menu->data);
1025
1026 if (self->child)
1027 menu_frame_hide(self->child);
1028
1029 if (self->parent)
1030 self->parent->child = NULL;
1031 self->parent = NULL;
1032 self->parent_entry = NULL;
1033
1034 menu_frame_visible = g_list_delete_link(menu_frame_visible, it);
1035
1036 if (menu_frame_visible == NULL) {
1037 /* last menu shown */
1038 ungrab_pointer();
1039 ungrab_keyboard();
1040 }
1041
1042 XUnmapWindow(ob_display, self->window);
1043
1044 menu_frame_free(self);
1045 }
1046
1047 void menu_frame_hide_all()
1048 {
1049 GList *it;
1050
1051 if (config_submenu_show_delay) {
1052 /* remove any submenu open requests */
1053 ob_main_loop_timeout_remove(ob_main_loop,
1054 menu_entry_frame_submenu_timeout);
1055 }
1056 if ((it = g_list_last(menu_frame_visible)))
1057 menu_frame_hide(it->data);
1058 }
1059
1060 void menu_frame_hide_all_client(ObClient *client)
1061 {
1062 GList *it = g_list_last(menu_frame_visible);
1063 if (it) {
1064 ObMenuFrame *f = it->data;
1065 if (f->client == client)
1066 menu_frame_hide(f);
1067 }
1068 }
1069
1070
1071 ObMenuFrame* menu_frame_under(gint x, gint y)
1072 {
1073 ObMenuFrame *ret = NULL;
1074 GList *it;
1075
1076 for (it = menu_frame_visible; it; it = g_list_next(it)) {
1077 ObMenuFrame *f = it->data;
1078
1079 if (RECT_CONTAINS(f->area, x, y)) {
1080 ret = f;
1081 break;
1082 }
1083 }
1084 return ret;
1085 }
1086
1087 ObMenuEntryFrame* menu_entry_frame_under(gint x, gint y)
1088 {
1089 ObMenuFrame *frame;
1090 ObMenuEntryFrame *ret = NULL;
1091 GList *it;
1092
1093 if ((frame = menu_frame_under(x, y))) {
1094 x -= ob_rr_theme->mbwidth + frame->area.x;
1095 y -= ob_rr_theme->mbwidth + frame->area.y;
1096
1097 for (it = frame->entries; it; it = g_list_next(it)) {
1098 ObMenuEntryFrame *e = it->data;
1099
1100 if (RECT_CONTAINS(e->area, x, y)) {
1101 ret = e;
1102 break;
1103 }
1104 }
1105 }
1106 return ret;
1107 }
1108
1109 static gboolean menu_entry_frame_submenu_timeout(gpointer data)
1110 {
1111 menu_entry_frame_show_submenu((ObMenuEntryFrame*)data);
1112 return FALSE;
1113 }
1114
1115 void menu_frame_select(ObMenuFrame *self, ObMenuEntryFrame *entry,
1116 gboolean immediate)
1117 {
1118 ObMenuEntryFrame *old = self->selected;
1119 ObMenuFrame *oldchild = self->child;
1120
1121 if (entry && entry->entry->type == OB_MENU_ENTRY_TYPE_SEPARATOR)
1122 entry = old;
1123
1124 if (old == entry) return;
1125
1126 if (config_submenu_show_delay) {
1127 /* remove any submenu open requests */
1128 ob_main_loop_timeout_remove(ob_main_loop,
1129 menu_entry_frame_submenu_timeout);
1130 }
1131
1132 self->selected = entry;
1133
1134 if (old)
1135 menu_entry_frame_render(old);
1136 if (oldchild)
1137 menu_frame_hide(oldchild);
1138
1139 if (self->selected) {
1140 menu_entry_frame_render(self->selected);
1141
1142 if (self->selected->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU) {
1143 if (config_submenu_show_delay && !immediate) {
1144 /* initiate a new submenu open request */
1145 ob_main_loop_timeout_add(ob_main_loop,
1146 config_submenu_show_delay * 1000,
1147 menu_entry_frame_submenu_timeout,
1148 self->selected, g_direct_equal,
1149 NULL);
1150 } else {
1151 menu_entry_frame_show_submenu(self->selected);
1152 }
1153 }
1154 }
1155 }
1156
1157 void menu_entry_frame_show_submenu(ObMenuEntryFrame *self)
1158 {
1159 ObMenuFrame *f;
1160
1161 if (!self->entry->data.submenu.submenu) return;
1162
1163 f = menu_frame_new(self->entry->data.submenu.submenu,
1164 self->entry->data.submenu.show_from,
1165 self->frame->client);
1166 /* pass our direction on to our child */
1167 f->direction_right = self->frame->direction_right;
1168
1169 menu_frame_show_submenu(f, self->frame, self);
1170 }
1171
1172 void menu_entry_frame_execute(ObMenuEntryFrame *self, guint state, Time time)
1173 {
1174 if (self->entry->type == OB_MENU_ENTRY_TYPE_NORMAL &&
1175 self->entry->data.normal.enabled)
1176 {
1177 /* grab all this shizzle, cuz when the menu gets hidden, 'self'
1178 gets freed */
1179 ObMenuEntry *entry = self->entry;
1180 ObMenuExecuteFunc func = self->frame->menu->execute_func;
1181 gpointer data = self->frame->menu->data;
1182 GSList *acts = self->entry->data.normal.actions;
1183 ObClient *client = self->frame->client;
1184 ObMenuFrame *frame = self->frame;
1185
1186 /* release grabs before executing the shit */
1187 if (!(state & ControlMask)) {
1188 menu_frame_hide_all();
1189 frame = NULL;
1190 }
1191
1192 if (func)
1193 func(entry, frame, client, state, data, time);
1194 else
1195 action_run(acts, client, state, time);
1196 }
1197 }
1198
1199 void menu_frame_select_previous(ObMenuFrame *self)
1200 {
1201 GList *it = NULL, *start;
1202
1203 if (self->entries) {
1204 start = it = g_list_find(self->entries, self->selected);
1205 while (TRUE) {
1206 ObMenuEntryFrame *e;
1207
1208 it = it ? g_list_previous(it) : g_list_last(self->entries);
1209 if (it == start)
1210 break;
1211
1212 if (it) {
1213 e = it->data;
1214 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1215 break;
1216 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1217 break;
1218 }
1219 }
1220 }
1221 menu_frame_select(self, it ? it->data : NULL, TRUE);
1222 }
1223
1224 void menu_frame_select_next(ObMenuFrame *self)
1225 {
1226 GList *it = NULL, *start;
1227
1228 if (self->entries) {
1229 start = it = g_list_find(self->entries, self->selected);
1230 while (TRUE) {
1231 ObMenuEntryFrame *e;
1232
1233 it = it ? g_list_next(it) : self->entries;
1234 if (it == start)
1235 break;
1236
1237 if (it) {
1238 e = it->data;
1239 if (e->entry->type == OB_MENU_ENTRY_TYPE_SUBMENU)
1240 break;
1241 if (e->entry->type == OB_MENU_ENTRY_TYPE_NORMAL)
1242 break;
1243 }
1244 }
1245 }
1246 menu_frame_select(self, it ? it->data : NULL, TRUE);
1247 }
This page took 0.091278 seconds and 4 git commands to generate.