2003-04-14 21:26:01 +00:00
|
|
|
#include "menu.h"
|
2003-04-15 18:29:55 +00:00
|
|
|
#include "openbox.h"
|
2003-04-16 05:36:51 +00:00
|
|
|
#include "stacking.h"
|
2003-04-16 18:09:11 +00:00
|
|
|
#include "grab.h"
|
2003-04-15 18:29:55 +00:00
|
|
|
#include "render/theme.h"
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
static GHashTable *menu_hash = NULL;
|
|
|
|
GHashTable *menu_map = NULL;
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 18:09:11 +00:00
|
|
|
#define FRAME_EVENTMASK (ButtonMotionMask | EnterWindowMask | LeaveWindowMask)
|
2003-04-16 05:36:51 +00:00
|
|
|
#define TITLE_EVENTMASK (ButtonPressMask | ButtonMotionMask)
|
2003-04-15 18:29:55 +00:00
|
|
|
#define ENTRY_EVENTMASK (EnterWindowMask | LeaveWindowMask | \
|
|
|
|
ButtonPressMask | ButtonReleaseMask)
|
|
|
|
|
2003-04-22 03:48:34 +00:00
|
|
|
void menu_control_show(Menu *self, int x, int y, Client *client);
|
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
void menu_destroy_hash_key(gpointer data)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
void menu_destroy_hash_value(Menu *self)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
|
|
|
GList *it;
|
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
for (it = self->entries; it; it = it->next)
|
|
|
|
menu_entry_free(it->data);
|
|
|
|
g_list_free(self->entries);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
g_free(self->label);
|
|
|
|
g_free(self->name);
|
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
g_hash_table_remove(menu_map, &self->title);
|
|
|
|
g_hash_table_remove(menu_map, &self->frame);
|
|
|
|
g_hash_table_remove(menu_map, &self->items);
|
2003-04-16 04:16:24 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
appearance_free(self->a_title);
|
|
|
|
XDestroyWindow(ob_display, self->title);
|
|
|
|
XDestroyWindow(ob_display, self->frame);
|
|
|
|
XDestroyWindow(ob_display, self->items);
|
2003-04-16 04:16:24 +00:00
|
|
|
|
|
|
|
g_free(self);
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
void menu_entry_free(MenuEntry *self)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
2003-04-16 04:16:24 +00:00
|
|
|
g_free(self->label);
|
|
|
|
action_free(self->action);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
g_hash_table_remove(menu_map, &self->item);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
appearance_free(self->a_item);
|
|
|
|
appearance_free(self->a_disabled);
|
|
|
|
appearance_free(self->a_hilite);
|
|
|
|
XDestroyWindow(ob_display, self->item);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 04:16:24 +00:00
|
|
|
g_free(self);
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void menu_startup()
|
|
|
|
{
|
2003-04-15 18:29:55 +00:00
|
|
|
Menu *m;
|
2003-04-16 18:09:11 +00:00
|
|
|
Action *a;
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-14 21:26:01 +00:00
|
|
|
menu_hash = g_hash_table_new_full(g_str_hash, g_str_equal,
|
|
|
|
menu_destroy_hash_key,
|
2003-04-16 04:16:24 +00:00
|
|
|
(GDestroyNotify)menu_destroy_hash_value);
|
|
|
|
menu_map = g_hash_table_new(g_int_hash, g_int_equal);
|
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
m = menu_new("sex menu", "root", NULL);
|
2003-04-16 18:09:11 +00:00
|
|
|
a = action_from_string("execute");
|
|
|
|
a->data.execute.path = g_strdup("xterm");
|
|
|
|
menu_add_entry(m, menu_entry_new("xterm", a));
|
|
|
|
a = action_from_string("restart");
|
|
|
|
menu_add_entry(m, menu_entry_new("restart", a));
|
|
|
|
menu_add_entry(m, menu_entry_new("--", NULL));
|
|
|
|
a = action_from_string("exit");
|
|
|
|
menu_add_entry(m, menu_entry_new("exit", a));
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void menu_shutdown()
|
|
|
|
{
|
|
|
|
g_hash_table_destroy(menu_hash);
|
2003-04-16 04:16:24 +00:00
|
|
|
g_hash_table_destroy(menu_map);
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
static Window createWindow(Window parent, unsigned long mask,
|
|
|
|
XSetWindowAttributes *attrib)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
2003-04-15 18:29:55 +00:00
|
|
|
return XCreateWindow(ob_display, parent, 0, 0, 1, 1, 0,
|
|
|
|
render_depth, InputOutput, render_visual,
|
|
|
|
mask, attrib);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2003-04-22 03:48:34 +00:00
|
|
|
Menu *menu_new_full(char *label, char *name, Menu *parent,
|
|
|
|
menu_controller_show show, menu_controller_update update)
|
2003-04-15 18:29:55 +00:00
|
|
|
{
|
|
|
|
XSetWindowAttributes attrib;
|
|
|
|
Menu *self;
|
|
|
|
|
|
|
|
self = g_new0(Menu, 1);
|
|
|
|
self->label = g_strdup(label);
|
|
|
|
self->name = g_strdup(name);
|
|
|
|
self->parent = parent;
|
|
|
|
|
|
|
|
self->entries = NULL;
|
|
|
|
self->shown = FALSE;
|
|
|
|
self->invalid = FALSE;
|
2003-04-14 21:26:01 +00:00
|
|
|
/* default controllers? */
|
2003-04-22 03:48:34 +00:00
|
|
|
|
|
|
|
self->show = show;
|
|
|
|
self->hide = NULL;
|
|
|
|
self->update = update;
|
|
|
|
self->mouseover = NULL;
|
|
|
|
self->selected = NULL;
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
attrib.override_redirect = TRUE;
|
2003-04-16 18:09:11 +00:00
|
|
|
attrib.event_mask = FRAME_EVENTMASK;
|
|
|
|
self->frame = createWindow(ob_root, CWOverrideRedirect|CWEventMask, &attrib);
|
2003-04-16 04:16:24 +00:00
|
|
|
attrib.event_mask = TITLE_EVENTMASK;
|
2003-04-16 05:36:51 +00:00
|
|
|
self->title = createWindow(self->frame, CWEventMask, &attrib);
|
|
|
|
self->items = createWindow(self->frame, 0, &attrib);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
XSetWindowBorderWidth(ob_display, self->frame, theme_bwidth);
|
|
|
|
XSetWindowBorderWidth(ob_display, self->title, theme_bwidth);
|
|
|
|
XSetWindowBorder(ob_display, self->frame, theme_b_color->pixel);
|
|
|
|
XSetWindowBorder(ob_display, self->title, theme_b_color->pixel);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
XMapWindow(ob_display, self->title);
|
|
|
|
XMapWindow(ob_display, self->items);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
self->a_title = appearance_copy(theme_a_menu_title);
|
|
|
|
self->a_items = appearance_copy(theme_a_menu);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
g_hash_table_insert(menu_map, &self->frame, self);
|
|
|
|
g_hash_table_insert(menu_map, &self->title, self);
|
|
|
|
g_hash_table_insert(menu_map, &self->items, self);
|
2003-04-15 18:29:55 +00:00
|
|
|
g_hash_table_insert(menu_hash, g_strdup(name), self);
|
|
|
|
return self;
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
void menu_free(char *name)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
|
|
|
g_hash_table_remove(menu_hash, name);
|
|
|
|
}
|
|
|
|
|
2003-04-15 18:29:55 +00:00
|
|
|
MenuEntry *menu_entry_new_full(char *label, Action *action,
|
|
|
|
MenuEntryRenderType render_type,
|
|
|
|
gpointer submenu)
|
2003-04-14 21:26:01 +00:00
|
|
|
{
|
2003-04-16 05:36:51 +00:00
|
|
|
MenuEntry *menu_entry = g_new0(MenuEntry, 1);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
|
|
|
menu_entry->label = g_strdup(label);
|
|
|
|
menu_entry->render_type = render_type;
|
2003-04-15 18:29:55 +00:00
|
|
|
menu_entry->action = action;
|
2003-04-14 21:26:01 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
menu_entry->hilite = FALSE;
|
|
|
|
menu_entry->enabled = TRUE;
|
|
|
|
|
2003-04-14 21:26:01 +00:00
|
|
|
menu_entry->submenu = submenu;
|
|
|
|
|
|
|
|
return menu_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_entry_set_submenu(MenuEntry *entry, Menu *submenu)
|
|
|
|
{
|
2003-04-15 18:29:55 +00:00
|
|
|
g_assert(entry != NULL);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
|
|
|
entry->submenu = submenu;
|
|
|
|
|
|
|
|
if(entry->parent != NULL)
|
|
|
|
entry->parent->invalid = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_add_entry(Menu *menu, MenuEntry *entry)
|
|
|
|
{
|
2003-04-15 18:29:55 +00:00
|
|
|
XSetWindowAttributes attrib;
|
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
g_assert(menu != NULL && entry != NULL && entry->item == None);
|
2003-04-14 21:26:01 +00:00
|
|
|
|
|
|
|
menu->entries = g_list_append(menu->entries, entry);
|
|
|
|
entry->parent = menu;
|
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
attrib.event_mask = ENTRY_EVENTMASK;
|
|
|
|
entry->item = createWindow(menu->items, CWEventMask, &attrib);
|
|
|
|
XMapWindow(ob_display, entry->item);
|
|
|
|
entry->a_item = appearance_copy(theme_a_menu_item);
|
|
|
|
entry->a_disabled = appearance_copy(theme_a_menu_disabled);
|
|
|
|
entry->a_hilite = appearance_copy(theme_a_menu_hilite);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-14 21:26:01 +00:00
|
|
|
menu->invalid = TRUE;
|
2003-04-16 04:16:24 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
g_hash_table_insert(menu_map, &entry->item, menu);
|
2003-04-14 21:26:01 +00:00
|
|
|
}
|
2003-04-15 18:29:55 +00:00
|
|
|
|
|
|
|
void menu_show(char *name, int x, int y, Client *client)
|
|
|
|
{
|
|
|
|
Menu *self;
|
|
|
|
|
|
|
|
self = g_hash_table_lookup(menu_hash, name);
|
|
|
|
if (!self) {
|
|
|
|
g_warning("Attempted to show menu '%s' but it does not exist.",
|
|
|
|
name);
|
|
|
|
return;
|
|
|
|
}
|
2003-04-16 05:36:51 +00:00
|
|
|
|
2003-04-22 03:48:34 +00:00
|
|
|
if (self->invalid) {
|
|
|
|
if (self->update) {
|
|
|
|
self->update(self);
|
|
|
|
} else {
|
|
|
|
menu_render(self);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-04-22 01:04:29 +00:00
|
|
|
self->client = client;
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-22 03:48:34 +00:00
|
|
|
if (self->show) {
|
|
|
|
self->show(self, x, y, client);
|
|
|
|
} else {
|
|
|
|
menu_control_show(self, x, y, client);
|
2003-04-16 18:09:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_hide(Menu *self) {
|
|
|
|
if (self->shown) {
|
|
|
|
XUnmapWindow(ob_display, self->frame);
|
|
|
|
self->shown = FALSE;
|
|
|
|
}
|
2003-04-16 05:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
MenuEntry *menu_find_entry(Menu *menu, Window win)
|
|
|
|
{
|
|
|
|
GList *it;
|
|
|
|
|
|
|
|
for (it = menu->entries; it; it = it->next) {
|
|
|
|
MenuEntry *entry = it->data;
|
|
|
|
if (entry->item == win)
|
|
|
|
return entry;
|
2003-04-15 18:29:55 +00:00
|
|
|
}
|
2003-04-16 05:36:51 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void menu_entry_render(MenuEntry *self)
|
|
|
|
{
|
|
|
|
Menu *menu = self->parent;
|
|
|
|
Appearance *a;
|
|
|
|
|
|
|
|
a = !self->enabled ? self->a_disabled :
|
2003-04-16 18:09:11 +00:00
|
|
|
(self->hilite && self->action ? self->a_hilite : self->a_item);
|
2003-04-16 05:36:51 +00:00
|
|
|
|
|
|
|
RECT_SET(a->area, 0, 0, menu->width,
|
|
|
|
menu->item_h);
|
|
|
|
RECT_SET(a->texture[0].position, menu->bullet_w,
|
|
|
|
0, menu->width - 2 * menu->bullet_w,
|
|
|
|
menu->item_h);
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
XMoveResizeWindow(ob_display, self->item, 0, self->y,
|
|
|
|
menu->width, menu->item_h);
|
|
|
|
a->surface.data.planar.parent = menu->a_items;
|
|
|
|
a->surface.data.planar.parentx = 0;
|
|
|
|
a->surface.data.planar.parenty = self->y;
|
2003-04-15 18:29:55 +00:00
|
|
|
|
2003-04-16 05:36:51 +00:00
|
|
|
paint(self->item, a);
|
2003-04-15 18:29:55 +00:00
|
|
|
}
|
2003-04-16 18:09:11 +00:00
|
|
|
|
|
|
|
void menu_entry_fire(MenuEntry *self)
|
|
|
|
{
|
|
|
|
Menu *m;
|
|
|
|
|
|
|
|
if (self->action) {
|
|
|
|
self->action->data.any.c = self->parent->client;
|
|
|
|
self->action->func(&self->action->data);
|
|
|
|
|
|
|
|
/* hide the whole thing */
|
|
|
|
m = self->parent;
|
|
|
|
while (m->parent) m = m->parent;
|
|
|
|
menu_hide(m);
|
|
|
|
}
|
|
|
|
}
|
2003-04-22 03:48:34 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
Default menu controller action for showing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void menu_control_show(Menu *self, int x, int y, Client *client) {
|
|
|
|
XMoveWindow(ob_display, self->frame, x, y);
|
|
|
|
|
|
|
|
if (!self->shown) {
|
|
|
|
stacking_raise_internal(self->frame);
|
|
|
|
XMapWindow(ob_display, self->frame);
|
|
|
|
self->shown = TRUE;
|
|
|
|
}
|
|
|
|
}
|