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