libui/new/unix/menu.c

270 lines
6.9 KiB
C
Raw Normal View History

2015-04-23 12:11:36 -05:00
// 23 april 2015
#include "uipriv_unix.h"
static GArray *menus = NULL;
static gboolean menusFinalized = FALSE;
struct menu {
uiMenu m;
char *name;
GHashTable *gtkMenuItems; // map[GtkWindow]GtkMenuItem
GHashTable *gtkSubmenus; // map[GtkWindow]GtkMenu
GArray *items; // []struct menuItem
};
struct menuItem {
uiMenuItem mi;
char *name;
int type;
void (*onClicked)(uiMenuItem *, uiWindow *, void *);
void *onClickedData;
2015-04-23 14:30:37 -05:00
const char *signal;
GCallback signalFunc;
GtkWidget *baseItem; // template for new instances; kept in sync with everything else
2015-04-23 12:11:36 -05:00
GHashTable *uiWindows; // map[GtkMenuItem]uiWindow
2015-04-23 14:30:37 -05:00
GHashTable *checkedSignals; // map[GtkMenuItem]gulong
2015-04-23 12:11:36 -05:00
};
enum {
typeRegular,
typeCheckbox,
typeQuit,
typePreferences,
typeAbout,
typeSeparator,
};
#define NEWHASH() g_hash_table_new(g_direct_hash, g_direct_equal)
2015-04-23 14:30:37 -05:00
static void onClicked(GtkMenuItem *menuitem, gpointer data)
{
struct menuItem *item = (struct menuItem *) data;
uiWindow *w;
w = uiWindow(g_hash_table_lookup(item->uiWindows, menuitem));
(*(item->onClicked))(uiMenuItem(item), w, item->onClickedData);
}
static void onToggled(GtkCheckMenuItem *menuitem, gpointer data)
{
struct menuItem *item = (struct menuItem *) data;
gboolean checked;
GHashTableIter iter;
gpointer widget;
gulong signal;
// get the checked state of /the item that triggered the signal/
checked = gtk_check_menu_item_get_active(menuitem);
// sync it with our template
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(item->baseItem), checked);
// then sync everything else
g_hash_table_iter_init(&iter, item->uiWindows);
while (g_hash_table_iter_next(&iter, &widget, NULL)) {
signal = (gulong) g_hash_table_lookup(item->checkedSignals, widget);
// don't allow toggling to recursively trigger this signal
g_signal_handler_block(widget, signal);
gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(widget), checked);
g_signal_handler_unblock(widget, signal);
}
// and finally fire our signal
// once again, do it on /the item that triggered the signal/
onClicked(GTK_MENU_ITEM(menuitem), item);
}
static void defaultOnClicked(uiMenuItem *item, uiWindow *w, void *data)
{
// do nothing
}
static void menuItemEnableDisable(struct menuItem *item, gboolean enabled)
{
GHashTableIter iter;
gpointer widget;
gtk_widget_set_sensitive(item->baseItem, enabled);
g_hash_table_iter_init(&iter, item->uiWindows);
while (g_hash_table_iter_next(&iter, &widget, NULL))
gtk_widget_set_sensitive(GTK_WIDGET(widget), enabled);
}
static void menuItemEnable(uiMenuItem *ii)
{
struct menuItem *item = (struct menuItem *) ii;
menuItemEnableDisable(item, TRUE);
}
static void menuItemDisable(uiMenuItem *ii)
{
struct menuItem *item = (struct menuItem *) ii;
menuItemEnableDisable(item, FALSE);
}
static void menuItemOnClicked(uiMenuItem *ii, void (*f)(uiMenuItem *, uiWindow *, void *), void *data)
{
struct menuItem *item = (struct menuItem *) ii;
item->onClicked = f;
item->onClickedData = data;
}
2015-04-23 12:11:36 -05:00
static uiMenuItem *newItem(struct menu *m, int type, const char *name)
{
struct menuItem *item;
if (menusFinalized)
complain("attempt to create a new menu item after menus have been finalized");
g_array_set_size(m->items, m->items->len + 1);
item = &g_array_index(m->items, struct menuItem, m->items->len - 1);
item->type = type;
switch (item->type) {
case typeQuit:
item->name = g_strdup("Quit");
break;
case typePreferences:
item->name = g_strdup("Preferences...");
break;
case typeAbout:
item->name = g_strdup("About");
break;
case typeSeparator:
break;
default:
item->name = g_strdup(name);
break;
}
2015-04-23 14:30:37 -05:00
item->onClicked = defaultOnClicked;
2015-04-23 12:11:36 -05:00
switch (item->type) {
case typeCheckbox:
item->baseItem = gtk_check_menu_item_new_with_label(item->name);
2015-04-23 14:30:37 -05:00
item->signal = "toggled";
item->signalFunc = G_CALLBACK(onToggled);
2015-04-23 12:11:36 -05:00
break;
case typeSeparator:
item->baseItem = gtk_separator_menu_item_new();
break;
default:
item->baseItem = gtk_menu_item_new_with_label(item->name);
2015-04-23 14:30:37 -05:00
item->signal = "activate";
item->signalFunc = G_CALLBACK(onClicked);
2015-04-23 12:11:36 -05:00
break;
}
item->uiWindows = NEWHASH();
2015-04-23 14:30:37 -05:00
item->checkedSignals = NEWHASH();
2015-04-23 12:11:36 -05:00
2015-04-23 14:30:37 -05:00
uiMenuItem(item)->Enable = menuItemEnable;
uiMenuItem(item)->Disable = menuItemDisable;
uiMenuItem(item)->OnClicked = menuItemOnClicked;
// TODO checked/setchecked
2015-04-23 12:11:36 -05:00
return uiMenuItem(item);
}
uiMenuItem *menuAppendItem(uiMenu *mm, const char *name)
{
return newItem((struct menu *) mm, typeRegular, name);
}
uiMenuItem *menuAppendCheckItem(uiMenu *mm, const char *name)
{
return newItem((struct menu *) mm, typeCheckbox, name);
}
uiMenuItem *menuAppendQuitItem(uiMenu *mm)
{
// TODO check multiple quit items
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typeQuit, NULL);
}
uiMenuItem *menuAppendPreferencesItem(uiMenu *mm)
{
// TODO check multiple preferences items
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typePreferences, NULL);
}
uiMenuItem *menuAppendAboutItem(uiMenu *mm)
{
// TODO check multiple about items
// TODO conditionally add separator
newItem((struct menu *) mm, typeSeparator, NULL);
return newItem((struct menu *) mm, typeAbout, NULL);
}
void menuAppendSeparator(uiMenu *mm)
{
// TODO check multiple about items
newItem((struct menu *) mm, typeSeparator, NULL);
}
uiMenu *uiNewMenu(const char *name)
{
struct menu *m;
if (menusFinalized)
complain("attempt to create a new menu after menus have been finalized");
if (menus == NULL)
menus = g_array_new(FALSE, TRUE, sizeof (struct menu));
// thanks Company in irc.gimp.net/#gtk+
g_array_set_size(menus, menus->len + 1);
m = &g_array_index(menus, struct menu, menus->len - 1);
m->name = g_strdup(name);
m->gtkMenuItems = NEWHASH();
m->gtkSubmenus = NEWHASH();
m->items = g_array_new(FALSE, TRUE, sizeof (struct menuItem));
uiMenu(m)->AppendItem = menuAppendItem;
uiMenu(m)->AppendCheckItem = menuAppendCheckItem;
uiMenu(m)->AppendQuitItem = menuAppendQuitItem;
uiMenu(m)->AppendPreferencesItem = menuAppendPreferencesItem;
uiMenu(m)->AppendAboutItem = menuAppendAboutItem;
uiMenu(m)->AppendSeparator = menuAppendSeparator;
return uiMenu(m);
}
/*
void menuItemDestroy(struct menuItem *item)
{
2015-04-23 14:30:37 -05:00
// TODO check that item->checkedSignals is empty
g_hash_table_destroy(item->checkedSignals);
2015-04-23 12:11:36 -05:00
// TODO checck that item->uiWindows is empty
g_hash_table_destroy(item->uiWindows);
gtk_widget_destroy(item->baseItem);
if (item->name != NULL)
g_free(item->name);
}
void menuDestroy(void)
{
guint i, j;
struct menu *m;
for (i = 0; i < menus->len; i++) {
m = &g_array_index(menus, struct menu, i);
for (j = 0; j < m->items->len; j++)
menuItemDestroy(&g_array_index(m->items, struct menuItem, j));
g_array_free(m->items, TRUE);
// TODO check that m->gtkSubmenus is empty
g_hash_table_destroy(m->gtkSubmenus);
// TODO check that m->gtkMenuItem is empty
g_hash_table_destroy(m->gtkMenuItem);
g_free(m->name);
}
g_array_free(menus, TRUE);
}
*/