2015-04-24 13:16:37 -05:00
|
|
|
// 24 april 2015
|
2016-04-23 14:26:57 -05:00
|
|
|
#include "uipriv_windows.hpp"
|
2015-04-24 13:16:37 -05:00
|
|
|
|
2016-05-29 18:55:53 -05:00
|
|
|
// LONGTERM migrate to std::vector
|
2015-05-08 09:34:32 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static uiMenu **menus = NULL;
|
2015-04-24 14:26:37 -05:00
|
|
|
static uintmax_t len = 0;
|
|
|
|
static uintmax_t cap = 0;
|
|
|
|
static BOOL menusFinalized = FALSE;
|
2015-04-24 14:45:58 -05:00
|
|
|
static WORD curID = 100; // start somewhere safe
|
2015-04-30 17:18:20 -05:00
|
|
|
static BOOL hasQuit = FALSE;
|
|
|
|
static BOOL hasPreferences = FALSE;
|
|
|
|
static BOOL hasAbout = FALSE;
|
2015-04-24 14:26:37 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
struct uiMenu {
|
2015-04-24 14:26:37 -05:00
|
|
|
WCHAR *name;
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem **items;
|
2015-04-24 14:26:37 -05:00
|
|
|
uintmax_t len;
|
|
|
|
uintmax_t cap;
|
|
|
|
};
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
struct uiMenuItem {
|
2015-04-24 14:26:37 -05:00
|
|
|
WCHAR *name;
|
|
|
|
int type;
|
|
|
|
WORD id;
|
|
|
|
void (*onClicked)(uiMenuItem *, uiWindow *, void *);
|
|
|
|
void *onClickedData;
|
|
|
|
BOOL disabled; // template for new instances; kept in sync with everything else
|
|
|
|
BOOL checked;
|
|
|
|
HMENU *hmenus;
|
|
|
|
uintmax_t len;
|
|
|
|
uintmax_t cap;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum {
|
|
|
|
typeRegular,
|
|
|
|
typeCheckbox,
|
|
|
|
typeQuit,
|
|
|
|
typePreferences,
|
|
|
|
typeAbout,
|
|
|
|
typeSeparator,
|
|
|
|
};
|
|
|
|
|
|
|
|
#define grow 32
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static void sync(uiMenuItem *item)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
|
|
|
uintmax_t i;
|
|
|
|
MENUITEMINFOW mi;
|
|
|
|
|
|
|
|
ZeroMemory(&mi, sizeof (MENUITEMINFOW));
|
|
|
|
mi.cbSize = sizeof (MENUITEMINFOW);
|
|
|
|
mi.fMask = MIIM_STATE;
|
|
|
|
if (item->disabled)
|
|
|
|
mi.fState |= MFS_DISABLED;
|
|
|
|
if (item->checked)
|
|
|
|
mi.fState |= MFS_CHECKED;
|
|
|
|
|
|
|
|
for (i = 0; i < item->len; i++)
|
|
|
|
if (SetMenuItemInfo(item->hmenus[i], item->id, FALSE, &mi) == 0)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error synchronizing menu items");
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
static void defaultOnClicked(uiMenuItem *item, uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
|
2015-05-09 09:53:38 -05:00
|
|
|
static void onQuitClicked(uiMenuItem *item, uiWindow *w, void *data)
|
|
|
|
{
|
|
|
|
if (shouldQuit())
|
|
|
|
uiQuit();
|
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
void uiMenuItemEnable(uiMenuItem *i)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
i->disabled = FALSE;
|
|
|
|
sync(i);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
void uiMenuItemDisable(uiMenuItem *i)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
i->disabled = TRUE;
|
|
|
|
sync(i);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
void uiMenuItemOnClicked(uiMenuItem *i, void (*f)(uiMenuItem *, uiWindow *, void *), void *data)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
if (i->type == typeQuit)
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not call uiMenuItemOnClicked() on a Quit item; use uiOnShouldQuit() instead.");
|
2015-08-31 16:50:23 -05:00
|
|
|
i->onClicked = f;
|
|
|
|
i->onClickedData = data;
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
int uiMenuItemChecked(uiMenuItem *i)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 16:50:23 -05:00
|
|
|
return i->checked != FALSE;
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
void uiMenuItemSetChecked(uiMenuItem *i, int checked)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
|
|
|
// use explicit values
|
2015-08-31 16:50:23 -05:00
|
|
|
i->checked = FALSE;
|
2015-04-24 14:26:37 -05:00
|
|
|
if (checked)
|
2015-08-31 16:50:23 -05:00
|
|
|
i->checked = TRUE;
|
|
|
|
sync(i);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static uiMenuItem *newItem(uiMenu *m, int type, const char *name)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *item;
|
2015-04-24 14:26:37 -05:00
|
|
|
|
|
|
|
if (menusFinalized)
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not create a new menu item after menus have been finalized.");
|
2015-04-24 14:26:37 -05:00
|
|
|
|
|
|
|
if (m->len >= m->cap) {
|
|
|
|
m->cap += grow;
|
2015-08-31 14:14:02 -05:00
|
|
|
m->items = (uiMenuItem **) uiRealloc(m->items, m->cap * sizeof (uiMenuItem *), "uiMenuitem *[]");
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
2015-04-30 17:30:25 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
item = uiNew(uiMenuItem);
|
2015-05-17 18:07:07 -05:00
|
|
|
|
2015-04-30 17:30:25 -05:00
|
|
|
m->items[m->len] = item;
|
2015-04-24 14:26:37 -05:00
|
|
|
m->len++;
|
|
|
|
|
|
|
|
item->type = type;
|
|
|
|
switch (item->type) {
|
|
|
|
case typeQuit:
|
|
|
|
item->name = toUTF16("Quit");
|
|
|
|
break;
|
|
|
|
case typePreferences:
|
|
|
|
item->name = toUTF16("Preferences...");
|
|
|
|
break;
|
|
|
|
case typeAbout:
|
|
|
|
item->name = toUTF16("About");
|
|
|
|
break;
|
|
|
|
case typeSeparator:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
item->name = toUTF16(name);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->type != typeSeparator) {
|
2015-04-24 14:45:58 -05:00
|
|
|
item->id = curID;
|
|
|
|
curID++;
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2016-05-29 18:55:53 -05:00
|
|
|
if (item->type == typeQuit) {
|
|
|
|
// can't call uiMenuItemOnClicked() here
|
2015-05-09 09:53:38 -05:00
|
|
|
item->onClicked = onQuitClicked;
|
2016-05-29 18:55:53 -05:00
|
|
|
item->onClickedData = NULL;
|
|
|
|
} else
|
|
|
|
uiMenuItemOnClicked(item, defaultOnClicked, NULL);
|
2015-04-24 14:26:37 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
return item;
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *uiMenuAppendItem(uiMenu *m, const char *name)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
return newItem(m, typeRegular, name);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *uiMenuAppendCheckItem(uiMenu *m, const char *name)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
return newItem(m, typeCheckbox, name);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *uiMenuAppendQuitItem(uiMenu *m)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-04-30 17:18:20 -05:00
|
|
|
if (hasQuit)
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not have multiple Quit menu items in a program.");
|
2015-04-30 17:18:20 -05:00
|
|
|
hasQuit = TRUE;
|
2015-08-31 14:14:02 -05:00
|
|
|
newItem(m, typeSeparator, NULL);
|
|
|
|
return newItem(m, typeQuit, NULL);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *uiMenuAppendPreferencesItem(uiMenu *m)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-04-30 17:18:20 -05:00
|
|
|
if (hasPreferences)
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not have multiple Preferences menu items in a program.");
|
2015-04-30 17:18:20 -05:00
|
|
|
hasPreferences = TRUE;
|
2015-08-31 14:14:02 -05:00
|
|
|
newItem(m, typeSeparator, NULL);
|
|
|
|
return newItem(m, typePreferences, NULL);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 16:50:23 -05:00
|
|
|
uiMenuItem *uiMenuAppendAboutItem(uiMenu *m)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-04-30 17:18:20 -05:00
|
|
|
if (hasAbout)
|
2016-05-29 18:55:53 -05:00
|
|
|
// TODO place these userbug strings in a header
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not have multiple About menu items in a program.");
|
2015-04-30 17:18:20 -05:00
|
|
|
hasAbout = TRUE;
|
2015-08-31 14:14:02 -05:00
|
|
|
newItem(m, typeSeparator, NULL);
|
|
|
|
return newItem(m, typeAbout, NULL);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
void uiMenuAppendSeparator(uiMenu *m)
|
2015-04-24 14:26:37 -05:00
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
newItem(m, typeSeparator, NULL);
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
|
|
|
|
2015-04-24 13:16:37 -05:00
|
|
|
uiMenu *uiNewMenu(const char *name)
|
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenu *m;
|
2015-04-24 14:26:37 -05:00
|
|
|
|
|
|
|
if (menusFinalized)
|
2016-05-13 21:53:56 -05:00
|
|
|
userbug("You can not create a new menu after menus have been finalized.");
|
2015-04-24 14:26:37 -05:00
|
|
|
if (len >= cap) {
|
|
|
|
cap += grow;
|
2015-08-31 14:14:02 -05:00
|
|
|
menus = (uiMenu **) uiRealloc(menus, cap * sizeof (uiMenu *), "uiMenu *[]");
|
2015-04-24 14:26:37 -05:00
|
|
|
}
|
2015-04-30 17:30:25 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
m = uiNew(uiMenu);
|
2015-05-17 18:07:07 -05:00
|
|
|
|
2015-04-30 17:30:25 -05:00
|
|
|
menus[len] = m;
|
2015-04-24 14:26:37 -05:00
|
|
|
len++;
|
|
|
|
|
|
|
|
m->name = toUTF16(name);
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
return m;
|
2015-04-24 13:16:37 -05:00
|
|
|
}
|
2015-04-24 14:44:29 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static void appendMenuItem(HMENU menu, uiMenuItem *item)
|
2015-04-24 14:44:29 -05:00
|
|
|
{
|
|
|
|
UINT uFlags;
|
|
|
|
|
|
|
|
uFlags = MF_SEPARATOR;
|
|
|
|
if (item->type != typeSeparator) {
|
|
|
|
uFlags = MF_STRING;
|
|
|
|
if (item->disabled)
|
|
|
|
uFlags |= MF_DISABLED | MF_GRAYED;
|
|
|
|
if (item->checked)
|
|
|
|
uFlags |= MF_CHECKED;
|
|
|
|
}
|
|
|
|
if (AppendMenuW(menu, uFlags, item->id, item->name) == 0)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error appending menu item");
|
2015-04-24 14:44:29 -05:00
|
|
|
|
|
|
|
if (item->len >= item->cap) {
|
|
|
|
item->cap += grow;
|
2015-05-08 09:34:32 -05:00
|
|
|
item->hmenus = (HMENU *) uiRealloc(item->hmenus, item->cap * sizeof (HMENU), "HMENU[]");
|
2015-04-24 14:44:29 -05:00
|
|
|
}
|
|
|
|
item->hmenus[item->len] = menu;
|
|
|
|
item->len++;
|
|
|
|
}
|
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static HMENU makeMenu(uiMenu *m)
|
2015-04-24 14:44:29 -05:00
|
|
|
{
|
|
|
|
HMENU menu;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
menu = CreatePopupMenu();
|
|
|
|
if (menu == NULL)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error creating menu");
|
2015-04-24 14:44:29 -05:00
|
|
|
for (i = 0; i < m->len; i++)
|
2015-04-30 17:30:25 -05:00
|
|
|
appendMenuItem(menu, m->items[i]);
|
2015-04-24 14:44:29 -05:00
|
|
|
return menu;
|
|
|
|
}
|
|
|
|
|
|
|
|
HMENU makeMenubar(void)
|
|
|
|
{
|
|
|
|
HMENU menubar;
|
|
|
|
HMENU menu;
|
|
|
|
uintmax_t i;
|
|
|
|
|
|
|
|
menusFinalized = TRUE;
|
|
|
|
|
|
|
|
menubar = CreateMenu();
|
|
|
|
if (menubar == NULL)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error creating menubar");
|
2015-04-24 14:44:29 -05:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2015-04-30 17:30:25 -05:00
|
|
|
menu = makeMenu(menus[i]);
|
|
|
|
if (AppendMenuW(menubar, MF_POPUP | MF_STRING, (UINT_PTR) menu, menus[i]->name) == 0)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error appending menu to menubar");
|
2015-04-24 14:44:29 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return menubar;
|
|
|
|
}
|
2015-04-24 14:54:37 -05:00
|
|
|
|
|
|
|
void runMenuEvent(WORD id, uiWindow *w)
|
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenu *m;
|
|
|
|
uiMenuItem *item;
|
2015-04-24 14:54:37 -05:00
|
|
|
uintmax_t i, j;
|
|
|
|
|
2015-05-03 16:37:36 -05:00
|
|
|
// this isn't optimal, but it works, and it should be just fine for most cases
|
2015-04-24 14:54:37 -05:00
|
|
|
for (i = 0; i < len; i++) {
|
2015-04-30 17:30:25 -05:00
|
|
|
m = menus[i];
|
2015-04-24 14:54:37 -05:00
|
|
|
for (j = 0; j < m->len; j++) {
|
2015-04-30 17:30:25 -05:00
|
|
|
item = m->items[j];
|
2015-04-24 14:54:37 -05:00
|
|
|
if (item->id == id)
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// no match
|
2016-05-13 21:53:56 -05:00
|
|
|
implbug("unknown menu ID %hu in runMenuEvent()", id);
|
2015-04-24 14:54:37 -05:00
|
|
|
|
|
|
|
found:
|
|
|
|
// first toggle checkboxes, if any
|
|
|
|
if (item->type == typeCheckbox)
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItemSetChecked(item, !uiMenuItemChecked(item));
|
2015-04-24 14:54:37 -05:00
|
|
|
|
|
|
|
// then run the event
|
2015-08-31 14:14:02 -05:00
|
|
|
(*(item->onClicked))(item, w, item->onClickedData);
|
2015-04-24 14:54:37 -05:00
|
|
|
}
|
2015-04-30 16:05:41 -05:00
|
|
|
|
2015-08-31 14:14:02 -05:00
|
|
|
static void freeMenu(uiMenu *m, HMENU submenu)
|
2015-04-30 16:05:41 -05:00
|
|
|
{
|
|
|
|
uintmax_t i;
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenuItem *item;
|
2015-04-30 16:05:41 -05:00
|
|
|
uintmax_t j;
|
|
|
|
|
|
|
|
for (i = 0; i < m->len; i++) {
|
2015-04-30 17:30:25 -05:00
|
|
|
item = m->items[i];
|
2015-04-30 16:05:41 -05:00
|
|
|
for (j = 0; j < item->len; j++)
|
|
|
|
if (item->hmenus[j] == submenu)
|
|
|
|
break;
|
|
|
|
if (j >= item->len)
|
2016-05-13 21:53:56 -05:00
|
|
|
implbug("submenu handle %p not found in freeMenu()", submenu);
|
2015-04-30 16:05:41 -05:00
|
|
|
for (; j < item->len - 1; j++)
|
|
|
|
item->hmenus[j] = item->hmenus[j + 1];
|
|
|
|
item->hmenus[j] = NULL;
|
|
|
|
item->len--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void freeMenubar(HMENU menubar)
|
|
|
|
{
|
|
|
|
uintmax_t i;
|
|
|
|
MENUITEMINFOW mi;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
ZeroMemory(&mi, sizeof (MENUITEMINFOW));
|
|
|
|
mi.cbSize = sizeof (MENUITEMINFOW);
|
|
|
|
mi.fMask = MIIM_SUBMENU;
|
|
|
|
if (GetMenuItemInfoW(menubar, i, TRUE, &mi) == 0)
|
2016-04-23 14:26:57 -05:00
|
|
|
logLastError(L"error getting menu to delete item references from");
|
2015-04-30 17:30:25 -05:00
|
|
|
freeMenu(menus[i], mi.hSubMenu);
|
2015-04-30 16:05:41 -05:00
|
|
|
}
|
|
|
|
// no need to worry about destroying any menus; destruction of the window they're in will do it for us
|
|
|
|
}
|
2015-05-08 13:23:22 -05:00
|
|
|
|
|
|
|
void uninitMenus(void)
|
|
|
|
{
|
2015-08-31 14:14:02 -05:00
|
|
|
uiMenu *m;
|
|
|
|
uiMenuItem *item;
|
2015-05-08 13:23:22 -05:00
|
|
|
uintmax_t i, j;
|
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
m = menus[i];
|
|
|
|
uiFree(m->name);
|
|
|
|
for (j = 0; j < m->len; j++) {
|
|
|
|
item = m->items[j];
|
|
|
|
if (item->len != 0)
|
2016-05-29 18:55:53 -05:00
|
|
|
// LONGTERM userbug()?
|
2016-05-13 21:53:56 -05:00
|
|
|
implbug("menu item %p (%ws) still has uiWindows attached; did you forget to destroy some windows?", item, item->name);
|
2015-05-08 13:23:22 -05:00
|
|
|
if (item->name != NULL)
|
|
|
|
uiFree(item->name);
|
|
|
|
if (item->hmenus != NULL)
|
|
|
|
uiFree(item->hmenus);
|
|
|
|
uiFree(item);
|
|
|
|
}
|
|
|
|
if (m->items != NULL)
|
|
|
|
uiFree(m->items);
|
|
|
|
uiFree(m);
|
|
|
|
}
|
|
|
|
if (menus != NULL)
|
|
|
|
uiFree(menus);
|
|
|
|
}
|