menus grab the keyboard and pointer, thus making only one menu visible at a time...
[dana/openbox.git] / openbox / menu_render.c
1 /* Functions for default rendering of menus. Might become pluginnable */
2
3 #include "menu.h"
4 #include "openbox.h"
5 #include "render/theme.h"
6
7 void menu_render_full(Menu *self);
8
9 void menu_render(Menu *self) {
10     if (self->invalid) {
11         if (self->update) {
12             self->update(self);
13         } else {
14             menu_render_full(self);
15         }
16     }
17 }
18             
19
20 void menu_render_full(Menu *self) {
21     GList *it;
22     int items_h = 0;
23     int nitems = 0; /* each item, only one is used */
24     int item_y;
25
26     self->size.width = 1;
27     self->item_h = 1;
28
29     if (self->a_title == NULL) {
30         XSetWindowBorderWidth(ob_display, self->frame, ob_rr_theme->bwidth);
31         XSetWindowBackground(ob_display, self->frame,
32                              ob_rr_theme->b_color->pixel);
33         XSetWindowBorderWidth(ob_display, self->title, ob_rr_theme->bwidth);
34         XSetWindowBorder(ob_display, self->frame, ob_rr_theme->b_color->pixel);
35         XSetWindowBorder(ob_display, self->title, ob_rr_theme->b_color->pixel);
36
37         self->a_title = RrAppearanceCopy(ob_rr_theme->a_menu_title);
38         self->a_items = RrAppearanceCopy(ob_rr_theme->a_menu);
39     }
40     
41     /* set texture data and size them mofos out */
42     if (self->label) {
43         self->a_title->texture[0].data.text.string = self->label;
44         RrMinsize(self->a_title, &self->title_min_w, &self->title_h);
45         self->title_min_w += ob_rr_theme->bevel * 2;
46         self->title_h += ob_rr_theme->bevel * 2;
47         self->size.width = MAX(self->size.width, self->title_min_w);
48     }
49
50     for (it = self->entries; it; it = it->next) {
51         MenuEntry *e = it->data;
52         int h;
53
54         if (e->a_item == NULL) {
55             e->a_item = RrAppearanceCopy(ob_rr_theme->a_menu_item);
56             e->a_disabled = RrAppearanceCopy(ob_rr_theme->a_menu_disabled);
57             e->a_hilite = RrAppearanceCopy(ob_rr_theme->a_menu_hilite);
58         }
59
60         e->a_item->texture[0].data.text.string = e->label;
61         RrMinsize(e->a_item, &e->min_w, &self->item_h);
62         self->size.width = MAX(self->size.width, e->min_w);
63
64         e->a_disabled->texture[0].data.text.string = e->label;
65         RrMinsize(e->a_disabled, &e->min_w, &h);
66         self->item_h = MAX(self->item_h, h);
67         self->size.width = MAX(self->size.width, e->min_w);
68         
69         e->a_hilite->texture[0].data.text.string = e->label;
70         RrMinsize(e->a_hilite, &e->min_w, &h);
71         self->item_h = MAX(self->item_h, h);
72         self->size.width = MAX(self->size.width, e->min_w);
73
74         e->min_w += ob_rr_theme->bevel * 2;
75         ++nitems;
76     }
77     self->bullet_w = self->item_h + ob_rr_theme->bevel;
78     self->size.width += 2 * self->bullet_w + 2 * ob_rr_theme->bevel;
79     self->item_h += ob_rr_theme->bevel * 2;
80     items_h = self->item_h * MAX(nitems, 1);
81
82     self->size.height = MAX(self->title_h + items_h + ob_rr_theme->bwidth, 1);
83     XResizeWindow(ob_display, self->frame, self->size.width,self->size.height);
84     if (self->label)
85         XMoveResizeWindow(ob_display, self->title, -ob_rr_theme->bwidth,
86                           -ob_rr_theme->bwidth,
87                           self->size.width, self->title_h);
88
89     XMoveResizeWindow(ob_display, self->items, 0, 
90                       self->title_h + ob_rr_theme->bwidth, self->size.width, 
91                       items_h);
92
93     if (self->label)
94         RrPaint(self->a_title, self->title, self->size.width, self->title_h);
95     RrPaint(self->a_items, self->items, self->size.width, items_h);
96
97     item_y = 0;
98     for (it = self->entries; it; it = it->next) {
99         ((MenuEntry*)it->data)->y = item_y;
100         menu_entry_render(it->data);
101         item_y += self->item_h;
102     }
103     
104     self->invalid = FALSE;
105 }
106
107 void menu_entry_render(MenuEntry *self)
108 {
109     Menu *menu = self->parent;
110     RrAppearance *a;
111     
112     switch (self->render_type) {
113     case MenuEntryRenderType_Submenu:
114         /* TODO: submenu mask */
115     case MenuEntryRenderType_Boolean:
116         /* TODO: boolean check */
117         a = self->enabled ? (self->hilite ? self->a_hilite : self->a_item) 
118             : self->a_disabled;
119         break;
120     case MenuEntryRenderType_None:
121         a = self->enabled ? (self->hilite ? self->a_hilite : self->a_item )
122             : self->a_disabled;
123         break;
124     case MenuEntryRenderType_Separator:
125         a = self->a_item;
126         break;
127
128     default:
129         g_message("unhandled render_type");
130         a = !self->enabled ? self->a_disabled :
131         (self->hilite && 
132          (self->action || self->render_type == MenuEntryRenderType_Submenu) ? 
133          self->a_hilite : self->a_item);
134         break;
135     }
136     g_message ("%s %d", self->label, self->hilite);
137
138     XMoveResizeWindow(ob_display, self->item, 0, self->y,
139                       menu->size.width, menu->item_h);
140
141     a->surface.parent = menu->a_items;
142     a->surface.parentx = 0;
143     a->surface.parenty = self->y;
144
145     RrPaint(a, self->item, menu->size.width, menu->item_h);
146 }