libui/windows/menu.c

125 lines
2.7 KiB
C
Raw Normal View History

2015-04-20 19:26:21 -05:00
// 20 april 2015
#include "uipriv_windows.h"
static void appendSeparator(HMENU menu)
{
if (AppendMenuW(menu, MF_SEPARATOR, 0, NULL) == 0)
logLastError("error appending separator in appendSeparator()");
}
static void appendTextItem(HMENU menu, const char *text, UINT_PTR *id)
2015-04-20 19:26:21 -05:00
{
WCHAR *wtext;
wtext = toUTF16(text);
if (AppendMenuW(menu, MF_STRING, *id, wtext) == 0)
2015-04-20 19:26:21 -05:00
logLastError("error appending menu item in appendTextItem()");
uiFree(wtext);
(*id)++;
2015-04-20 19:26:21 -05:00
}
static void appendMenuItem(HMENU menu, const uiMenuItem *item, UINT_PTR *id)
2015-04-20 19:26:21 -05:00
{
switch (item->Type) {
case uiMenuItemTypeCommand:
case uiMenuItemTypeCheckbox:
appendTextItem(menu, item->Name, id);
return;
2015-04-20 19:26:21 -05:00
// TODO see if there are stock items for these three
case uiMenuItemTypeQuit:
// TODO verify name
2015-04-20 19:26:21 -05:00
appendSeparator(menu);
appendTextItem(menu, "Quit", id);
2015-04-20 19:26:21 -05:00
return;
case uiMenuItemTypePreferences:
// TODO verify name
2015-04-20 19:26:21 -05:00
appendSeparator(menu);
appendTextItem(menu, "Preferences", id);
2015-04-20 19:26:21 -05:00
return;
case uiMenuItemTypeAbout:
// TODO verify name
2015-04-20 19:26:21 -05:00
appendSeparator(menu);
appendTextItem(menu, "About", id);
2015-04-20 19:26:21 -05:00
return;
case uiMenuItemTypeSeparator:
// TODO verify name
2015-04-20 19:26:21 -05:00
appendSeparator(menu);
return;
}
// TODO complain
}
static HMENU makeMenu(uiMenuItem *items, UINT_PTR *id)
2015-04-20 19:26:21 -05:00
{
HMENU menu;
const uiMenuItem *i;
menu = CreatePopupMenu();
if (menu == NULL)
logLastError("error creating menu in makeMenu()");
for (i = items; i->Type != 0; i++)
appendMenuItem(menu, i, id);
2015-04-20 19:26:21 -05:00
return menu;
}
HMENU makeMenubar(void)
{
HMENU menubar;
const uiMenu *m;
WCHAR *wname;
HMENU menu;
UINT_PTR id;
2015-04-20 19:26:21 -05:00
if (options.Menu == NULL)
complain("asked to give uiWindow a menubar but didn't specify a menu in uiInitOptions");
menubar = CreateMenu();
if (menubar == NULL)
logLastError("error creating menubar in makeMenubar()");
id = 100; // start at a safe number
2015-04-20 19:26:21 -05:00
for (m = options.Menu; m->Name != NULL; m++) {
wname = toUTF16(m->Name);
menu = makeMenu(m->Items, &id);
2015-04-20 19:26:21 -05:00
if (AppendMenuW(menubar, MF_POPUP | MF_STRING, (UINT_PTR) menu, wname) == 0)
logLastError("error appending menu to menubar in makeMenubar()");
uiFree(wname);
}
return menubar;
}
// this is slow, but it will do for now
// TODO investigate faster options
static const uiMenuItem *lookupID(const uiMenuItem *items, UINT_PTR *cur, UINT_PTR id)
{
const uiMenuItem *i;
for (i = items; i->Type != 0; i++) {
if (i->Type == uiMenuItemTypeSeparator)
continue;
if (*cur == id)
return i;
(*cur)++;
}
return NULL;
}
const uiMenuItem *menuIDToItem(UINT_PTR id)
{
UINT_PTR cur;
const uiMenu *m;
const uiMenuItem *item;
cur = 100;
for (m = options.Menu; m->Name != NULL; m++) {
item = lookupID(m->Items, &cur, id);
if (item != NULL)
return item;
}
// TODO complain
return NULL;
}