[melonDS] add support for submenus
This commit is contained in:
parent
5bd78c1932
commit
476ce2e93a
1
ui.h
1
ui.h
|
@ -286,6 +286,7 @@ _UI_EXTERN uiMenuItem *uiMenuAppendCheckItem(uiMenu *m, const char *name);
|
|||
_UI_EXTERN uiMenuItem *uiMenuAppendQuitItem(uiMenu *m);
|
||||
_UI_EXTERN uiMenuItem *uiMenuAppendPreferencesItem(uiMenu *m);
|
||||
_UI_EXTERN uiMenuItem *uiMenuAppendAboutItem(uiMenu *m);
|
||||
_UI_EXTERN uiMenuItem *uiMenuAppendSubmenu(uiMenu *m, uiMenu* child);
|
||||
_UI_EXTERN void uiMenuAppendSeparator(uiMenu *m);
|
||||
_UI_EXTERN uiMenu *uiNewMenu(const char *name);
|
||||
|
||||
|
|
90
unix/menu.c
90
unix/menu.c
|
@ -2,6 +2,7 @@
|
|||
#include "uipriv_unix.h"
|
||||
|
||||
static GArray *menus = NULL;
|
||||
static guint nmenus = 0;
|
||||
static gboolean menusFinalized = FALSE;
|
||||
static gboolean hasQuit = FALSE;
|
||||
static gboolean hasPreferences = FALSE;
|
||||
|
@ -10,6 +11,8 @@ static gboolean hasAbout = FALSE;
|
|||
struct uiMenu {
|
||||
char *name;
|
||||
GArray *items; // []*uiMenuItem
|
||||
gboolean ischild;
|
||||
guint id;
|
||||
};
|
||||
|
||||
struct uiMenuItem {
|
||||
|
@ -21,6 +24,7 @@ struct uiMenuItem {
|
|||
gboolean disabled;
|
||||
gboolean checked;
|
||||
GHashTable *windows; // map[GtkMenuItem]*menuItemWindow
|
||||
uiMenu *popupchild;
|
||||
};
|
||||
|
||||
struct menuItemWindow {
|
||||
|
@ -35,6 +39,7 @@ enum {
|
|||
typePreferences,
|
||||
typeAbout,
|
||||
typeSeparator,
|
||||
typeSubmenu,
|
||||
};
|
||||
|
||||
// we do NOT want programmatic updates to raise an ::activated signal
|
||||
|
@ -179,6 +184,33 @@ static uiMenuItem *newItem(uiMenu *m, int type, const char *name)
|
|||
}
|
||||
|
||||
item->windows = g_hash_table_new(g_direct_hash, g_direct_equal);
|
||||
item->popupchild = NULL;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
uiMenuItem *uiMenuAppendSubmenu(uiMenu *m, uiMenu* child)
|
||||
{
|
||||
uiMenuItem *item;
|
||||
|
||||
if (menusFinalized)
|
||||
uiprivUserBug("You cannot create a new menu item after menus have been finalized.");
|
||||
|
||||
item = uiprivNew(uiMenuItem);
|
||||
|
||||
g_array_append_val(m->items, item);
|
||||
|
||||
item->type = typeSubmenu;
|
||||
item->name = child->name;
|
||||
|
||||
uiMenuItemOnClicked(item, defaultOnClicked, NULL);
|
||||
|
||||
// checkme
|
||||
item->gtype = GTK_TYPE_MENU_ITEM;
|
||||
|
||||
item->windows = g_hash_table_new(g_direct_hash, g_direct_equal);
|
||||
item->popupchild = child;
|
||||
child->ischild = TRUE;
|
||||
|
||||
return item;
|
||||
}
|
||||
|
@ -237,9 +269,12 @@ uiMenu *uiNewMenu(const char *name)
|
|||
m = uiprivNew(uiMenu);
|
||||
|
||||
g_array_append_val(menus, m);
|
||||
m->id = nmenus;
|
||||
nmenus++;
|
||||
|
||||
m->name = g_strdup(name);
|
||||
m->items = g_array_new(FALSE, TRUE, sizeof (uiMenuItem *));
|
||||
m->ischild = FALSE;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
@ -264,6 +299,18 @@ static void appendMenuItem(GtkMenuShell *submenu, uiMenuItem *item, uiWindow *w)
|
|||
ww->w = w;
|
||||
ww->signal = signal;
|
||||
g_hash_table_insert(item->windows, menuitem, ww);
|
||||
|
||||
if (item->popupchild != NULL) {
|
||||
int j;
|
||||
uiMenu* m;
|
||||
GtkWidget *submenu;
|
||||
|
||||
m = item->popupchild;
|
||||
submenu = gtk_menu_new();
|
||||
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
|
||||
for (j = 0; j < m->items->len; j++)
|
||||
appendMenuItem(GTK_MENU_SHELL(submenu), g_array_index(m->items, uiMenuItem *, j), w);
|
||||
}
|
||||
}
|
||||
|
||||
GtkWidget *uiprivMakeMenubar(uiWindow *w)
|
||||
|
@ -281,6 +328,7 @@ GtkWidget *uiprivMakeMenubar(uiWindow *w)
|
|||
if (menus != NULL)
|
||||
for (i = 0; i < menus->len; i++) {
|
||||
m = g_array_index(menus, uiMenu *, i);
|
||||
if (m->ischild) continue;
|
||||
menuitem = gtk_menu_item_new_with_label(m->name);
|
||||
submenu = gtk_menu_new();
|
||||
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuitem), submenu);
|
||||
|
@ -299,6 +347,8 @@ struct freeMenuItemData {
|
|||
guint i;
|
||||
};
|
||||
|
||||
static void freeMenu(GtkWidget *widget, gpointer data);
|
||||
|
||||
static void freeMenuItem(GtkWidget *widget, gpointer data)
|
||||
{
|
||||
struct freeMenuItemData *fmi = (struct freeMenuItemData *) data;
|
||||
|
@ -306,6 +356,8 @@ static void freeMenuItem(GtkWidget *widget, gpointer data)
|
|||
struct menuItemWindow *w;
|
||||
|
||||
item = g_array_index(fmi->items, uiMenuItem *, fmi->i);
|
||||
if (item->popupchild != NULL)
|
||||
freeMenu(widget, &item->popupchild->id);
|
||||
w = (struct menuItemWindow *) g_hash_table_lookup(item->windows, widget);
|
||||
if (g_hash_table_remove(item->windows, widget) == FALSE)
|
||||
uiprivImplBug("GtkMenuItem %p not in menu item's item/window map", widget);
|
||||
|
@ -339,28 +391,38 @@ void uiprivFreeMenubar(GtkWidget *mb)
|
|||
// no need to worry about destroying any widgets; destruction of the window they're in will do it for us
|
||||
}
|
||||
|
||||
void _freeMenu(uiMenu* m)
|
||||
{
|
||||
uiMenuItem *item;
|
||||
guint j;
|
||||
|
||||
g_free(m->name);
|
||||
for (j = 0; j < m->items->len; j++) {
|
||||
item = g_array_index(m->items, uiMenuItem *, j);
|
||||
if (item->popupchild != NULL)
|
||||
_freeMenu(item->popupchild);
|
||||
if (g_hash_table_size(item->windows) != 0)
|
||||
// TODO is this really a userbug()?
|
||||
uiprivImplBug("menu item %p (%s) still has uiWindows attached; did you forget to destroy some windows?", item, item->name);
|
||||
if (item->type != typeSubmenu) g_free(item->name);
|
||||
g_hash_table_destroy(item->windows);
|
||||
uiprivFree(item);
|
||||
}
|
||||
g_array_free(m->items, TRUE);
|
||||
uiprivFree(m);
|
||||
}
|
||||
|
||||
void uiprivUninitMenus(void)
|
||||
{
|
||||
uiMenu *m;
|
||||
uiMenuItem *item;
|
||||
guint i, j;
|
||||
guint i;
|
||||
|
||||
if (menus == NULL)
|
||||
return;
|
||||
for (i = 0; i < menus->len; i++) {
|
||||
m = g_array_index(menus, uiMenu *, i);
|
||||
g_free(m->name);
|
||||
for (j = 0; j < m->items->len; j++) {
|
||||
item = g_array_index(m->items, uiMenuItem *, j);
|
||||
if (g_hash_table_size(item->windows) != 0)
|
||||
// TODO is this really a uiprivUserBug()?
|
||||
uiprivImplBug("menu item %p (%s) still has uiWindows attached; did you forget to destroy some windows?", item, item->name);
|
||||
g_free(item->name);
|
||||
g_hash_table_destroy(item->windows);
|
||||
uiprivFree(item);
|
||||
}
|
||||
g_array_free(m->items, TRUE);
|
||||
uiprivFree(m);
|
||||
if (m->ischild) continue;
|
||||
_freeMenu(m);
|
||||
}
|
||||
g_array_free(menus, TRUE);
|
||||
}
|
||||
|
|
|
@ -14,7 +14,9 @@ static BOOL hasAbout = FALSE;
|
|||
|
||||
struct uiMenu {
|
||||
WCHAR *name;
|
||||
HMENU handle;
|
||||
uiMenuItem **items;
|
||||
BOOL ischild;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
};
|
||||
|
@ -28,6 +30,7 @@ struct uiMenuItem {
|
|||
BOOL disabled; // template for new instances; kept in sync with everything else
|
||||
BOOL checked;
|
||||
HMENU *hmenus;
|
||||
uiMenu* popupchild;
|
||||
size_t len;
|
||||
size_t cap;
|
||||
};
|
||||
|
@ -39,6 +42,7 @@ enum {
|
|||
typePreferences,
|
||||
typeAbout,
|
||||
typeSeparator,
|
||||
typeSubmenu,
|
||||
};
|
||||
|
||||
#define grow 32
|
||||
|
@ -140,6 +144,7 @@ static uiMenuItem *newItem(uiMenu *m, int type, const char *name)
|
|||
item->name = toUTF16(name);
|
||||
break;
|
||||
}
|
||||
item->popupchild = NULL;
|
||||
|
||||
if (item->type != typeSeparator) {
|
||||
item->id = curID;
|
||||
|
@ -156,6 +161,34 @@ static uiMenuItem *newItem(uiMenu *m, int type, const char *name)
|
|||
return item;
|
||||
}
|
||||
|
||||
uiMenuItem *uiMenuAppendSubmenu(uiMenu *m, uiMenu* child)
|
||||
{
|
||||
uiMenuItem *item;
|
||||
|
||||
if (menusFinalized)
|
||||
uiprivUserBug("You can not create a new menu item after menus have been finalized.");
|
||||
|
||||
if (m->len >= m->cap) {
|
||||
m->cap += grow;
|
||||
m->items = (uiMenuItem **) uiprivRealloc(m->items, m->cap * sizeof (uiMenuItem *), "uiMenuitem *[]");
|
||||
}
|
||||
|
||||
item = uiprivNew(uiMenuItem);
|
||||
|
||||
m->items[m->len] = item;
|
||||
m->len++;
|
||||
|
||||
item->type = typeSubmenu;
|
||||
item->name = child->name;
|
||||
|
||||
item->popupchild = child;
|
||||
child->ischild = TRUE;
|
||||
|
||||
uiMenuItemOnClicked(item, defaultOnClicked, NULL);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
uiMenuItem *uiMenuAppendItem(uiMenu *m, const char *name)
|
||||
{
|
||||
return newItem(m, typeRegular, name);
|
||||
|
@ -216,13 +249,17 @@ uiMenu *uiNewMenu(const char *name)
|
|||
len++;
|
||||
|
||||
m->name = toUTF16(name);
|
||||
m->ischild = FALSE;
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static HMENU makeMenu(uiMenu *m);
|
||||
|
||||
static void appendMenuItem(HMENU menu, uiMenuItem *item)
|
||||
{
|
||||
UINT uFlags;
|
||||
UINT_PTR id = item->id;
|
||||
|
||||
uFlags = MF_SEPARATOR;
|
||||
if (item->type != typeSeparator) {
|
||||
|
@ -231,8 +268,12 @@ static void appendMenuItem(HMENU menu, uiMenuItem *item)
|
|||
uFlags |= MF_DISABLED | MF_GRAYED;
|
||||
if (item->checked)
|
||||
uFlags |= MF_CHECKED;
|
||||
if (item->popupchild) {
|
||||
uFlags |= MF_POPUP;
|
||||
id = (UINT_PTR)makeMenu(item->popupchild);
|
||||
}
|
||||
}
|
||||
if (AppendMenuW(menu, uFlags, item->id, item->name) == 0)
|
||||
if (AppendMenuW(menu, uFlags, id, item->name) == 0)
|
||||
logLastError(L"error appending menu item");
|
||||
|
||||
if (item->len >= item->cap) {
|
||||
|
@ -251,6 +292,7 @@ static HMENU makeMenu(uiMenu *m)
|
|||
menu = CreatePopupMenu();
|
||||
if (menu == NULL)
|
||||
logLastError(L"error creating menu");
|
||||
m->handle = menu;
|
||||
for (i = 0; i < m->len; i++)
|
||||
appendMenuItem(menu, m->items[i]);
|
||||
return menu;
|
||||
|
@ -269,6 +311,7 @@ HMENU makeMenubar(void)
|
|||
logLastError(L"error creating menubar");
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (menus[i]->ischild) continue;
|
||||
menu = makeMenu(menus[i]);
|
||||
if (AppendMenuW(menubar, MF_POPUP | MF_STRING, (UINT_PTR) menu, menus[i]->name) == 0)
|
||||
logLastError(L"error appending menu to menubar");
|
||||
|
@ -321,21 +364,27 @@ static void freeMenu(uiMenu *m, HMENU submenu)
|
|||
item->hmenus[j] = item->hmenus[j + 1];
|
||||
item->hmenus[j] = NULL;
|
||||
item->len--;
|
||||
|
||||
if (item->popupchild)
|
||||
freeMenu(item->popupchild, item->popupchild->handle);
|
||||
}
|
||||
}
|
||||
|
||||
void freeMenubar(HMENU menubar)
|
||||
{
|
||||
size_t i;
|
||||
size_t j = 0;
|
||||
MENUITEMINFOW mi;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
if (menus[i]->ischild) continue;
|
||||
ZeroMemory(&mi, sizeof (MENUITEMINFOW));
|
||||
mi.cbSize = sizeof (MENUITEMINFOW);
|
||||
mi.fMask = MIIM_SUBMENU;
|
||||
if (GetMenuItemInfoW(menubar, i, TRUE, &mi) == 0)
|
||||
if (GetMenuItemInfoW(menubar, j, TRUE, &mi) == 0)
|
||||
logLastError(L"error getting menu to delete item references from");
|
||||
freeMenu(menus[i], mi.hSubMenu);
|
||||
j++;
|
||||
}
|
||||
// no need to worry about destroying any menus; destruction of the window they're in will do it for us
|
||||
}
|
||||
|
@ -354,7 +403,7 @@ void uninitMenus(void)
|
|||
if (item->len != 0)
|
||||
// LONGTERM uiprivUserBug()?
|
||||
uiprivImplBug("menu item %p (%ws) still has uiWindows attached; did you forget to destroy some windows?", item, item->name);
|
||||
if (item->name != NULL)
|
||||
if (item->type != typeSubmenu && item->name != NULL)
|
||||
uiprivFree(item->name);
|
||||
if (item->hmenus != NULL)
|
||||
uiprivFree(item->hmenus);
|
||||
|
|
Loading…
Reference in New Issue