5 #include "render/theme.h"
10 GHashTable
*menu_hash
= NULL
;
12 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
14 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
15 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
16 ButtonPressMask | ButtonReleaseMask)
18 void menu_control_show(Menu
*self
, int x
, int y
, Client
*client
);
20 void menu_destroy_hash_key(Menu
*menu
)
25 void menu_destroy_hash_value(Menu
*self
)
29 for (it
= self
->entries
; it
; it
= it
->next
)
30 menu_entry_free(it
->data
);
31 g_list_free(self
->entries
);
36 g_hash_table_remove(window_map
, &self
->title
);
37 g_hash_table_remove(window_map
, &self
->frame
);
38 g_hash_table_remove(window_map
, &self
->items
);
40 stacking_remove(self
);
42 RrAppearanceFree(self
->a_title
);
43 XDestroyWindow(ob_display
, self
->title
);
44 XDestroyWindow(ob_display
, self
->frame
);
45 XDestroyWindow(ob_display
, self
->items
);
50 void menu_entry_free(MenuEntry
*self
)
53 action_free(self
->action
);
55 g_hash_table_remove(window_map
, &self
->item
);
57 RrAppearanceFree(self
->a_item
);
58 RrAppearanceFree(self
->a_disabled
);
59 RrAppearanceFree(self
->a_hilite
);
60 XDestroyWindow(ob_display
, self
->item
);
74 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
75 (GDestroyNotify
)menu_destroy_hash_key
,
76 (GDestroyNotify
)menu_destroy_hash_value
);
78 m
= menu_new("sex menu", "root", NULL
);
80 a
= action_from_string("execute");
81 a
->data
.execute
.path
= g_strdup("xterm");
82 menu_add_entry(m
, menu_entry_new("xterm", a
));
83 a
= action_from_string("restart");
84 menu_add_entry(m
, menu_entry_new("restart", a
));
85 menu_add_entry(m
, menu_entry_new_separator("--"));
86 a
= action_from_string("exit");
87 menu_add_entry(m
, menu_entry_new("exit", a
));
90 s = menu_new("subsex menu", "submenu", m);
91 a = action_from_string("execute");
92 a->data.execute.path = g_strdup("xclock");
93 menu_add_entry(s, menu_entry_new("xclock", a));
95 menu_add_entry(m, menu_entry_new_submenu("subz", s));
97 s = menu_new("empty", "chub", m);
98 menu_add_entry(m, menu_entry_new_submenu("empty", s));
100 s = menu_new("", "s-club", m);
101 menu_add_entry(m, menu_entry_new_submenu("empty", s));
103 s = menu_new(NULL, "h-club", m);
104 menu_add_entry(m, menu_entry_new_submenu("empty", s));
106 s = menu_new(NULL, "g-club", m);
108 a = action_from_string("execute");
109 a->data.execute.path = g_strdup("xterm");
110 menu_add_entry(s, menu_entry_new("xterm", a));
111 a = action_from_string("restart");
112 menu_add_entry(s, menu_entry_new("restart", a));
113 menu_add_entry(s, menu_entry_new_separator("--"));
114 a = action_from_string("exit");
115 menu_add_entry(s, menu_entry_new("exit", a));
117 menu_add_entry(m, menu_entry_new_submenu("long", s));
123 g_hash_table_destroy(menu_hash
);
126 static Window
createWindow(Window parent
, unsigned long mask
,
127 XSetWindowAttributes
*attrib
)
129 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
130 RrDepth(ob_rr_inst
), InputOutput
,
131 RrVisual(ob_rr_inst
), mask
, attrib
);
135 Menu
*menu_new_full(char *label
, char *name
, Menu
*parent
,
136 menu_controller_show show
, menu_controller_update update
)
138 XSetWindowAttributes attrib
;
141 self
= g_new0(Menu
, 1);
142 self
->obwin
.type
= Window_Menu
;
143 self
->label
= g_strdup(label
);
144 self
->name
= g_strdup(name
);
145 self
->parent
= parent
;
146 self
->open_submenu
= NULL
;
148 self
->entries
= NULL
;
150 self
->invalid
= TRUE
;
152 /* default controllers */
155 self
->update
= update
;
156 self
->mouseover
= NULL
;
157 self
->selected
= NULL
;
160 self
->plugin_data
= NULL
;
162 attrib
.override_redirect
= TRUE
;
163 attrib
.event_mask
= FRAME_EVENTMASK
;
164 self
->frame
= createWindow(ob_root
, CWOverrideRedirect
|CWEventMask
, &attrib
);
165 attrib
.event_mask
= TITLE_EVENTMASK
;
166 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
167 self
->items
= createWindow(self
->frame
, 0, &attrib
);
169 XSetWindowBorderWidth(ob_display
, self
->frame
, ob_rr_theme
->bwidth
);
170 XSetWindowBackground(ob_display
, self
->frame
, ob_rr_theme
->b_color
->pixel
);
171 XSetWindowBorderWidth(ob_display
, self
->title
, ob_rr_theme
->bwidth
);
172 XSetWindowBorder(ob_display
, self
->frame
, ob_rr_theme
->b_color
->pixel
);
173 XSetWindowBorder(ob_display
, self
->title
, ob_rr_theme
->b_color
->pixel
);
175 XMapWindow(ob_display
, self
->title
);
176 XMapWindow(ob_display
, self
->items
);
178 self
->a_title
= RrAppearanceCopy(ob_rr_theme
->a_menu_title
);
179 self
->a_items
= RrAppearanceCopy(ob_rr_theme
->a_menu
);
181 g_hash_table_insert(window_map
, &self
->frame
, self
);
182 g_hash_table_insert(window_map
, &self
->title
, self
);
183 g_hash_table_insert(window_map
, &self
->items
, self
);
184 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
186 stacking_add(MENU_AS_WINDOW(self
));
187 stacking_raise(MENU_AS_WINDOW(self
));
192 void menu_free(char *name
)
194 g_hash_table_remove(menu_hash
, name
);
197 MenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
198 MenuEntryRenderType render_type
,
201 MenuEntry
*menu_entry
= g_new0(MenuEntry
, 1);
203 menu_entry
->label
= g_strdup(label
);
204 menu_entry
->render_type
= render_type
;
205 menu_entry
->action
= action
;
207 menu_entry
->hilite
= FALSE
;
208 menu_entry
->enabled
= TRUE
;
210 menu_entry
->submenu
= submenu
;
215 void menu_entry_set_submenu(MenuEntry
*entry
, Menu
*submenu
)
217 g_assert(entry
!= NULL
);
219 entry
->submenu
= submenu
;
221 if(entry
->parent
!= NULL
)
222 entry
->parent
->invalid
= TRUE
;
225 void menu_add_entry(Menu
*menu
, MenuEntry
*entry
)
227 XSetWindowAttributes attrib
;
229 g_assert(menu
!= NULL
);
230 g_assert(entry
!= NULL
);
231 g_assert(entry
->item
== None
);
233 menu
->entries
= g_list_append(menu
->entries
, entry
);
234 entry
->parent
= menu
;
236 attrib
.event_mask
= ENTRY_EVENTMASK
;
237 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
238 XMapWindow(ob_display
, entry
->item
);
239 entry
->a_item
= RrAppearanceCopy(ob_rr_theme
->a_menu_item
);
240 entry
->a_disabled
= RrAppearanceCopy(ob_rr_theme
->a_menu_disabled
);
241 entry
->a_hilite
= RrAppearanceCopy(ob_rr_theme
->a_menu_hilite
);
243 menu
->invalid
= TRUE
;
245 g_hash_table_insert(window_map
, &entry
->item
, menu
);
248 void menu_show(char *name
, int x
, int y
, Client
*client
)
252 self
= g_hash_table_lookup(menu_hash
, name
);
254 g_warning("Attempted to show menu '%s' but it does not exist.",
259 menu_show_full(self
, x
, y
, client
);
262 void menu_show_full(Menu
*self
, int x
, int y
, Client
*client
)
264 g_assert(self
!= NULL
);
268 self
->client
= client
;
271 self
->show(self
, x
, y
, client
);
273 menu_control_show(self
, x
, y
, client
);
277 void menu_hide(Menu
*self
) {
279 XUnmapWindow(ob_display
, self
->frame
);
281 if (self
->open_submenu
)
282 menu_hide(self
->open_submenu
);
283 if (self
->parent
&& self
->parent
->open_submenu
== self
)
284 self
->parent
->open_submenu
= NULL
;
289 void menu_clear(Menu
*self
) {
292 for (it
= self
->entries
; it
; it
= it
->next
) {
293 MenuEntry
*entry
= it
->data
;
294 menu_entry_free(entry
);
296 self
->entries
= NULL
;
297 self
->invalid
= TRUE
;
301 MenuEntry
*menu_find_entry(Menu
*menu
, Window win
)
305 for (it
= menu
->entries
; it
; it
= it
->next
) {
306 MenuEntry
*entry
= it
->data
;
307 if (entry
->item
== win
)
313 void menu_entry_fire(MenuEntry
*self
)
318 self
->action
->data
.any
.c
= self
->parent
->client
;
319 self
->action
->func(&self
->action
->data
);
321 /* hide the whole thing */
323 while (m
->parent
) m
= m
->parent
;
329 Default menu controller action for showing.
332 void menu_control_show(Menu
*self
, int x
, int y
, Client
*client
) {
333 g_assert(!self
->invalid
);
335 XMoveWindow(ob_display
, self
->frame
,
336 MIN(x
, screen_physical_size
.width
- self
->size
.width
),
337 MIN(y
, screen_physical_size
.height
- self
->size
.height
));
338 POINT_SET(self
->location
,
339 MIN(x
, screen_physical_size
.width
- self
->size
.width
),
340 MIN(y
, screen_physical_size
.height
- self
->size
.height
));
343 XMapWindow(ob_display
, self
->frame
);
344 stacking_raise(MENU_AS_WINDOW(self
));
346 } else if (self
->shown
&& self
->open_submenu
) {
347 menu_hide(self
->open_submenu
);
351 void menu_control_mouseover(MenuEntry
*self
, gboolean enter
) {
353 self
->hilite
= enter
;
356 if (self
->parent
->open_submenu
&& self
->submenu
357 != self
->parent
->open_submenu
)
358 menu_hide(self
->parent
->open_submenu
);
361 self
->parent
->open_submenu
= self
->submenu
;
363 /* shouldn't be invalid since it must be displayed */
364 g_assert(!self
->parent
->invalid
);
365 /* TODO: I don't understand why these bevels should be here.
366 Something must be wrong in the width calculation */
367 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
370 /* need to get the width. is this bad?*/
371 menu_render(self
->submenu
);
373 if (self
->submenu
->size
.width
+ x
> screen_physical_size
.width
)
374 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
377 menu_show_full(self
->submenu
, x
,
378 self
->parent
->location
.y
+ self
->y
,
379 self
->parent
->client
);