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()");
|
|
|
|
}
|
|
|
|
|
2015-04-21 17:44:31 -05:00
|
|
|
static void appendTextItem(HMENU menu, const char *text, UINT_PTR *id)
|
2015-04-20 19:26:21 -05:00
|
|
|
{
|
|
|
|
WCHAR *wtext;
|
|
|
|
|
|
|
|
wtext = toUTF16(text);
|
2015-04-21 17:44:31 -05:00
|
|
|
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);
|
2015-04-21 17:44:31 -05:00
|
|
|
(*id)++;
|
2015-04-20 19:26:21 -05:00
|
|
|
}
|
|
|
|
|
2015-04-21 17:44:31 -05:00
|
|
|
static void appendMenuItem(HMENU menu, const uiMenuItem *item, UINT_PTR *id)
|
2015-04-20 19:26:21 -05:00
|
|
|
{
|
2015-04-20 22:45:32 -05:00
|
|
|
switch (item->Type) {
|
|
|
|
case uiMenuItemTypeCommand:
|
|
|
|
case uiMenuItemTypeCheckbox:
|
2015-04-21 17:44:31 -05:00
|
|
|
appendTextItem(menu, item->Name, id);
|
2015-04-20 22:45:32 -05:00
|
|
|
return;
|
2015-04-20 19:26:21 -05:00
|
|
|
// TODO see if there are stock items for these three
|
2015-04-20 22:45:32 -05:00
|
|
|
case uiMenuItemTypeQuit:
|
|
|
|
// TODO verify name
|
2015-04-20 19:26:21 -05:00
|
|
|
appendSeparator(menu);
|
2015-04-21 17:44:31 -05:00
|
|
|
appendTextItem(menu, "Quit", id);
|
2015-04-20 19:26:21 -05:00
|
|
|
return;
|
2015-04-20 22:45:32 -05:00
|
|
|
case uiMenuItemTypePreferences:
|
|
|
|
// TODO verify name
|
2015-04-20 19:26:21 -05:00
|
|
|
appendSeparator(menu);
|
2015-04-21 17:44:31 -05:00
|
|
|
appendTextItem(menu, "Preferences", id);
|
2015-04-20 19:26:21 -05:00
|
|
|
return;
|
2015-04-20 22:45:32 -05:00
|
|
|
case uiMenuItemTypeAbout:
|
|
|
|
// TODO verify name
|
2015-04-20 19:26:21 -05:00
|
|
|
appendSeparator(menu);
|
2015-04-21 17:44:31 -05:00
|
|
|
appendTextItem(menu, "About", id);
|
2015-04-20 19:26:21 -05:00
|
|
|
return;
|
2015-04-20 22:45:32 -05:00
|
|
|
case uiMenuItemTypeSeparator:
|
|
|
|
// TODO verify name
|
2015-04-20 19:26:21 -05:00
|
|
|
appendSeparator(menu);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// TODO complain
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-04-21 17:44:31 -05:00
|
|
|
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()");
|
2015-04-20 22:45:32 -05:00
|
|
|
for (i = items; i->Type != 0; i++)
|
2015-04-21 17:44:31 -05:00
|
|
|
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;
|
2015-04-21 17:44:31 -05:00
|
|
|
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()");
|
|
|
|
|
2015-04-21 17:44:31 -05:00
|
|
|
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);
|
2015-04-21 17:44:31 -05:00
|
|
|
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;
|
|
|
|
}
|
2015-04-21 17:44:31 -05:00
|
|
|
|
|
|
|
// this is slow, but it will do for now
|
|
|
|
// TODO investigate faster options
|
|
|
|
|
2015-04-21 17:52:57 -05:00
|
|
|
static const uiMenuItem *lookupID(const uiMenuItem *items, UINT_PTR *cur, UINT_PTR id)
|
2015-04-21 17:44:31 -05:00
|
|
|
{
|
|
|
|
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++) {
|
2015-04-21 17:52:57 -05:00
|
|
|
item = lookupID(m->Items, &cur, id);
|
2015-04-21 17:44:31 -05:00
|
|
|
if (item != NULL)
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
// TODO complain
|
|
|
|
return NULL;
|
|
|
|
}
|