5 #include "render/theme.h"
10 static GHashTable
*menu_hash
= NULL
;
11 GHashTable
*menu_map
= NULL
;
13 #define FRAME_EVENTMASK (ButtonPressMask |ButtonMotionMask | EnterWindowMask | \
15 #define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
16 #define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
17 ButtonPressMask | ButtonReleaseMask)
19 void menu_control_show(Menu
*self
, int x
, int y
, Client
*client
);
21 void menu_destroy_hash_key(gpointer data
)
26 void menu_destroy_hash_value(Menu
*self
)
30 for (it
= self
->entries
; it
; it
= it
->next
)
31 menu_entry_free(it
->data
);
32 g_list_free(self
->entries
);
37 g_hash_table_remove(menu_map
, &self
->title
);
38 g_hash_table_remove(menu_map
, &self
->frame
);
39 g_hash_table_remove(menu_map
, &self
->items
);
41 stacking_remove(self
);
43 appearance_free(self
->a_title
);
44 XDestroyWindow(ob_display
, self
->title
);
45 XDestroyWindow(ob_display
, self
->frame
);
46 XDestroyWindow(ob_display
, self
->items
);
51 void menu_entry_free(MenuEntry
*self
)
54 action_free(self
->action
);
56 g_hash_table_remove(menu_map
, &self
->item
);
58 appearance_free(self
->a_item
);
59 appearance_free(self
->a_disabled
);
60 appearance_free(self
->a_hilite
);
61 XDestroyWindow(ob_display
, self
->item
);
73 menu_hash
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
74 menu_destroy_hash_key
,
75 (GDestroyNotify
)menu_destroy_hash_value
);
76 menu_map
= g_hash_table_new(g_int_hash
, g_int_equal
);
78 m
= menu_new(NULL
, "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
));
88 s
= menu_new("subsex menu", "submenu", m
);
89 a
= action_from_string("execute");
90 a
->data
.execute
.path
= g_strdup("xclock");
91 menu_add_entry(s
, menu_entry_new("xclock", a
));
93 menu_add_entry(m
, menu_entry_new_submenu("subz", s
));
95 t
= (Menu
*)plugin_create("timed_menu");
97 a
= action_from_string("execute");
98 a
->data
.execute
.path
= g_strdup("xeyes");
99 menu_add_entry(t
, menu_entry_new("xeyes", a
));
100 menu_add_entry(m
, menu_entry_new_submenu("timed", t
));
103 s
= menu_new("empty", "chub", m
);
104 menu_add_entry(m
, menu_entry_new_submenu("empty", s
));
106 s
= menu_new("", "s-club", m
);
107 menu_add_entry(m
, menu_entry_new_submenu("empty", s
));
109 s
= menu_new(NULL
, "h-club", m
);
110 menu_add_entry(m
, menu_entry_new_submenu("empty", s
));
112 s
= menu_new(NULL
, "g-club", m
);
114 a
= action_from_string("execute");
115 a
->data
.execute
.path
= g_strdup("xterm");
116 menu_add_entry(s
, menu_entry_new("xterm", a
));
117 a
= action_from_string("restart");
118 menu_add_entry(s
, menu_entry_new("restart", a
));
119 menu_add_entry(s
, menu_entry_new_separator("--"));
120 a
= action_from_string("exit");
121 menu_add_entry(s
, menu_entry_new("exit", a
));
123 menu_add_entry(m
, menu_entry_new_submenu("long", s
));
125 m
= menu_new(NULL
, "client", NULL
);
126 a
= action_from_string("iconify");
127 menu_add_entry(m
, menu_entry_new("iconify", a
));
128 a
= action_from_string("toggleshade");
129 menu_add_entry(m
, menu_entry_new("(un)shade", a
));
130 a
= action_from_string("togglemaximizefull");
131 menu_add_entry(m
, menu_entry_new("(un)maximize", a
));
132 a
= action_from_string("close");
133 menu_add_entry(m
, menu_entry_new("close", a
));
139 g_hash_table_destroy(menu_hash
);
140 g_hash_table_destroy(menu_map
);
143 static Window
createWindow(Window parent
, unsigned long mask
,
144 XSetWindowAttributes
*attrib
)
146 return XCreateWindow(ob_display
, parent
, 0, 0, 1, 1, 0,
147 render_depth
, InputOutput
, render_visual
,
152 Menu
*menu_new_full(char *label
, char *name
, Menu
*parent
,
153 menu_controller_show show
, menu_controller_update update
)
155 XSetWindowAttributes attrib
;
158 self
= g_new0(Menu
, 1);
159 self
->obwin
.type
= Window_Menu
;
160 self
->label
= g_strdup(label
);
161 self
->name
= g_strdup(name
);
162 self
->parent
= parent
;
163 self
->open_submenu
= NULL
;
165 self
->entries
= NULL
;
167 self
->invalid
= TRUE
;
169 /* default controllers */
172 self
->update
= update
;
173 self
->mouseover
= NULL
;
174 self
->selected
= NULL
;
177 self
->plugin_data
= NULL
;
179 attrib
.override_redirect
= TRUE
;
180 attrib
.event_mask
= FRAME_EVENTMASK
;
181 self
->frame
= createWindow(ob_root
, CWOverrideRedirect
|CWEventMask
, &attrib
);
182 attrib
.event_mask
= TITLE_EVENTMASK
;
183 self
->title
= createWindow(self
->frame
, CWEventMask
, &attrib
);
184 self
->items
= createWindow(self
->frame
, 0, &attrib
);
186 XSetWindowBorderWidth(ob_display
, self
->frame
, theme_bwidth
);
187 XSetWindowBackground(ob_display
, self
->frame
, theme_b_color
->pixel
);
188 XSetWindowBorderWidth(ob_display
, self
->title
, theme_bwidth
);
189 XSetWindowBorder(ob_display
, self
->frame
, theme_b_color
->pixel
);
190 XSetWindowBorder(ob_display
, self
->title
, theme_b_color
->pixel
);
192 XMapWindow(ob_display
, self
->title
);
193 XMapWindow(ob_display
, self
->items
);
195 self
->a_title
= appearance_copy(theme_a_menu_title
);
196 self
->a_items
= appearance_copy(theme_a_menu
);
198 g_hash_table_insert(menu_map
, &self
->frame
, self
);
199 g_hash_table_insert(menu_map
, &self
->title
, self
);
200 g_hash_table_insert(menu_map
, &self
->items
, self
);
201 g_hash_table_insert(menu_hash
, g_strdup(name
), self
);
204 stacking_raise(MENU_AS_WINDOW(self
));
209 void menu_free(char *name
)
211 g_hash_table_remove(menu_hash
, name
);
214 MenuEntry
*menu_entry_new_full(char *label
, Action
*action
,
215 MenuEntryRenderType render_type
,
218 MenuEntry
*menu_entry
= g_new0(MenuEntry
, 1);
220 menu_entry
->label
= g_strdup(label
);
221 menu_entry
->render_type
= render_type
;
222 menu_entry
->action
= action
;
224 menu_entry
->hilite
= FALSE
;
225 menu_entry
->enabled
= TRUE
;
227 menu_entry
->submenu
= submenu
;
232 void menu_entry_set_submenu(MenuEntry
*entry
, Menu
*submenu
)
234 g_assert(entry
!= NULL
);
236 entry
->submenu
= submenu
;
238 if(entry
->parent
!= NULL
)
239 entry
->parent
->invalid
= TRUE
;
242 void menu_add_entry(Menu
*menu
, MenuEntry
*entry
)
244 XSetWindowAttributes attrib
;
246 g_assert(menu
!= NULL
);
247 g_assert(entry
!= NULL
);
248 g_assert(entry
->item
== None
);
250 menu
->entries
= g_list_append(menu
->entries
, entry
);
251 entry
->parent
= menu
;
253 attrib
.event_mask
= ENTRY_EVENTMASK
;
254 entry
->item
= createWindow(menu
->items
, CWEventMask
, &attrib
);
255 XMapWindow(ob_display
, entry
->item
);
256 entry
->a_item
= appearance_copy(theme_a_menu_item
);
257 entry
->a_disabled
= appearance_copy(theme_a_menu_disabled
);
258 entry
->a_hilite
= appearance_copy(theme_a_menu_hilite
);
260 menu
->invalid
= TRUE
;
262 g_hash_table_insert(menu_map
, &entry
->item
, menu
);
265 void menu_show(char *name
, int x
, int y
, Client
*client
)
269 self
= g_hash_table_lookup(menu_hash
, name
);
271 g_warning("Attempted to show menu '%s' but it does not exist.",
276 menu_show_full(self
, x
, y
, client
);
279 void menu_show_full(Menu
*self
, int x
, int y
, Client
*client
)
281 g_assert(self
!= NULL
);
285 self
->client
= client
;
288 self
->show(self
, x
, y
, client
);
290 menu_control_show(self
, x
, y
, client
);
295 void menu_hide(Menu
*self
) {
297 XUnmapWindow(ob_display
, self
->frame
);
299 if (self
->open_submenu
)
300 menu_hide(self
->open_submenu
);
301 if (self
->parent
&& self
->parent
->open_submenu
== self
)
302 self
->parent
->open_submenu
= NULL
;
307 void menu_clear(Menu
*self
) {
310 for (it
= self
->entries
; it
; it
= it
->next
) {
311 MenuEntry
*entry
= it
->data
;
312 menu_entry_free(entry
);
314 self
->entries
= NULL
;
315 self
->invalid
= TRUE
;
319 MenuEntry
*menu_find_entry(Menu
*menu
, Window win
)
323 for (it
= menu
->entries
; it
; it
= it
->next
) {
324 MenuEntry
*entry
= it
->data
;
325 if (entry
->item
== win
)
331 void menu_entry_fire(MenuEntry
*self
)
336 self
->action
->data
.any
.c
= self
->parent
->client
;
337 self
->action
->func(&self
->action
->data
);
339 /* hide the whole thing */
341 while (m
->parent
) m
= m
->parent
;
347 Default menu controller action for showing.
350 void menu_control_show(Menu
*self
, int x
, int y
, Client
*client
) {
351 g_assert(!self
->invalid
);
353 XMoveWindow(ob_display
, self
->frame
,
354 MIN(x
, screen_physical_size
.width
- self
->size
.width
),
355 MIN(y
, screen_physical_size
.height
- self
->size
.height
));
356 POINT_SET(self
->location
,
357 MIN(x
, screen_physical_size
.width
- self
->size
.width
),
358 MIN(y
, screen_physical_size
.height
- self
->size
.height
));
361 XMapWindow(ob_display
, self
->frame
);
362 stacking_raise(MENU_AS_WINDOW(self
));
364 } else if (self
->shown
&& self
->open_submenu
) {
365 menu_hide(self
->open_submenu
);
369 void menu_control_mouseover(MenuEntry
*self
, gboolean enter
) {
371 self
->hilite
= enter
;
374 if (self
->parent
->open_submenu
&& self
->submenu
375 != self
->parent
->open_submenu
)
376 menu_hide(self
->parent
->open_submenu
);
379 self
->parent
->open_submenu
= self
->submenu
;
381 /* shouldn't be invalid since it must be displayed */
382 g_assert(!self
->parent
->invalid
);
383 /* TODO: I don't understand why these bevels should be here.
384 Something must be wrong in the width calculation */
385 x
= self
->parent
->location
.x
+ self
->parent
->size
.width
+
388 /* need to get the width. is this bad?*/
389 menu_render(self
->submenu
);
391 if (self
->submenu
->size
.width
+ x
> screen_physical_size
.width
)
392 x
= self
->parent
->location
.x
- self
->submenu
->size
.width
-
395 menu_show_full(self
->submenu
, x
,
396 self
->parent
->location
.y
+ self
->y
, NULL
);