home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Hall of Fame
/
HallofFameCDROM.cdr
/
window
/
mewel12.lzh
/
MENUGEN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1989-07-21
|
33KB
|
1,183 lines
/*===========================================================================*/
/* */
/* File : MENUGEN.C */
/* */
/* Purpose : A simple menu editor designed for demo purposes. */
/* Menugen lets you design a pulldown menu system for your */
/* application. You can append, insert, change, and delete menubar */
/* or pulldown menu entries, and then save them in a .RC file. */
/* */
/* History : Created by Marc Adler 7/20/89 */
/* */
/* (C) Copyright 1989 Marc Adler/Magma Systems All Rights Reserved */
/*===========================================================================*/
#include <stdio.h>
#include <string.h>
#include "window.h"
#include "keys.h"
#include "rc.h"
#include "menugen.h"
/*
External functions
*/
extern int pascal MainWndProc();
extern int pascal MenuWindowWndProc();
extern BOOL pascal MenuItemDlgProc();
extern HWND pascal DialogCreateIndirect();
extern HMENU MenuList();
/*
Globals
*/
HWND hMain; /* handle of main window */
HWND hStatus; /* handle of status window */
HWND hWndMenu; /* handle to the window which holds the test menubar */
HDLG hItemDlg; /* handle to the menu editing dialog box */
HMENU hMenuBar = NULLHWND; /* handle of the test menubar */
char CurrFileName[65] = { '\0' };
#define MODE_MENUBAR 1
#define MODE_POPUP 2
WORD CurrMode = MODE_MENUBAR;
#define ID_ITEM 900
WORD idLastItem = ID_ITEM; /* keeps track of the last identifier given out */
/*
A structure to hold the current menu item status
*/
typedef struct menuitem
{
char szItemText[80];
WORD idItem;
WORD fMask;
HMENU hMenu;
} MENUITEM;
MENUITEM CurrItem;
/*
Menu attribute-to-string mapping table
*/
typedef struct attrinfo
{
WORD fBitMask;
char *szAttr;
} ATTRINFO;
ATTRINFO AttrInfo[] =
{
MF_CHECKED, "CHECKED",
MF_DISABLED, "DISABLED",
MF_SEPARATOR, "SEPARATOR",
MF_SHADOW, "SHADOW",
MF_HELP, "HELP",
};
main(argc, argv)
int argc;
char **argv;
{
WORD hRes;
HMENU hBar;
EVENT event;
HACCEL hAccel;
/*
Process any command line arguments
*/
if (argc >= 2)
{
int i;
for (i = 1; i < argc; i++)
{
char *arg = argv[i];
if (*arg == '-' || *arg == '/')
switch (*++arg)
{
case 'm' : /* -m => use mono attributes */
WinUseMonoMap(TRUE);
break;
}
else
break;
}
}
/*
Initialize the window system - this *must* be done once in every appl
*/
WinInit();
/*
Create the main window and set the main window procedure
*/
hMain = CreateWindow("Normal", /* class name */
"Menu Generator", /* window title */
WIN_HAS_BORDER, /* window flags */
0,0, 80,24, /* dimensions */
0x17, /* color */
1, /* id */
NULLHWND, /* hParent */
(HMENU) NULLHWND, /* menu */
0, /* HINST */
(DWORD) NULL); /* creation params */
WinSetWinProc(hMain, MainWndProc);
/*
Create the menu test window...
*/
hWndMenu = CreateWindow("Normal", /* class name */
"Current Menu", /* window title */
WIN_HAS_BORDER, /* window flags */
1,3, 78,5, /* dimensions */
0x57, /* color */
0, /* id */
hMain, /* hParent */
(HMENU) NULLHWND, /* menu */
0, /* HINST */
(DWORD) NULL); /* creation params */
WinSetWinProc(hWndMenu, MenuWindowWndProc);
/*
Create the status window...
*/
hStatus = CreateWindow("Normal", /* class name */
NULL, /* window title */
0x0000L, /* window flags */
0,24, 80,1, /* dimensions */
0x31, /* color */
0, /* id */
hMain, /* hParent */
(HMENU) NULLHWND, /* menu */
0, /* HINST */
(DWORD) NULL); /* creation params */
/*
Load the main menubar in from the external resource file MENUGEN.RES.
Alternatively, we can also do a bunch of CreateMenu()'s, but using
a resource file lets us modify our menus more easily.
*/
hRes = OpenResourceFile("menugen");
if (!hRes)
{
printf("Can't open menugen.res\n");
exit(1);
}
hBar = LoadMenu(hRes, ID_MENUBAR, hMain);
SetMenu(hMain, hBar);
CheckMenuItem(hBar, ID_MODE_BAR, MF_BYCOMMAND | MF_CHECKED);
CloseResourceFile(hRes);
/*
Set up the accelerator table and keys.
F1 invokes help and ALT-X exits.
*/
hAccel = AcceleratorCreate();
AcceleratorSetFocus(hAccel);
AcceleratorSetKey(ID_EXIT, VK_ALT_X);
AcceleratorSetKey(ID_HELP, VK_F1);
AcceleratorSetKey(ID_OPEN, VK_ALT_O);
AcceleratorSetWnd(hMain);
/*
Set the input focus to the main window. This will also draw the window.
*/
SetFocus(hMain);
/*
Read in the initial dialog box
*/
if (argc > 1)
{
char fname[80];
/*
Append a .RES extension
*/
strcpy(fname, argv[1]);
ChangeExtension(fname, ".res");
if ((hMenuBar = MenuList(fname)))
{
ChangeExtension(fname, ".rc");
strcpy(CurrFileName, fname);
SetMenu(hWndMenu, hMenuBar);
}
}
/*
If we have no initial menubar to work with, create a dummy one
with one menubar item and one associated pulldown item.
*/
if (hMenuBar == NULLHWND)
{
HMENU hPop;
hMenuBar = CreateMenu(hWndMenu);
hPop = CreateMenu(hMenuBar);
CreateDefaultPopup(hPop, 0x0000);
ChangeMenu(hMenuBar, 0, "~Dummy", hPop, MF_POPUP | MF_APPEND);
}
/*
Set the new menu bar and refresh the window
*/
SetMenu(hWndMenu, hMenuBar);
DrawMenuBar(hMenuBar);
/*
Main message loop
*/
for (;;)
{
GetMessage(&event);
TranslateAccelerator(&event);
/*
We trap the right-button-down messages before they get swallowed
up by the default menu window proc.
*/
if (event.message == WM_RBUTTONDOWN)
MenuWindowWndProc(event.hWnd, event.message, event.wParam, event.lParam);
else
DispatchMessage(&event);
}
}
/*
MainWndProc - window procedure for the main window
*/
pascal MainWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
switch (message)
{
case WM_COMMAND :
switch (wParam)
{
case ID_OPEN :
if (DlgOpenFile(NULLHWND, "*.res", CurrFileName))
{
if ((hMenuBar = MenuList(CurrFileName)))
{
ChangeExtension(CurrFileName, ".rc");
SetMenu(hWndMenu, hMenuBar);
DrawMenuBar(hMenuBar);
}
}
break;
case ID_SAVE :
if (CurrFileName[0])
{
GenerateRCCode(hMenuBar, CurrFileName);
break;
}
/* fall into ... */
case ID_SAVEAS :
SaveFileDlg();
break;
case ID_GENERATE :
GenerateRCCode(hMenuBar, "foo.c");
break;
case ID_HELP :
HelpDialog();
break;
case ID_EXIT :
exit(0);
}
break;
case WM_HELP :
HelpDialog();
break;
case WM_QUIT :
/* End the menu editor */
exit(0);
default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
return FALSE;
}
/*
MenuWindowWndProc - window procedure for the window which contains the
menubar we are building. The main purpose of this function is to receive
WM_COMMAND messages when we click on a menu item, and to receive
right button down messages in order to edit a menubar item.
*/
pascal MenuWindowWndProc(hWnd, message, wParam, lParam)
HWND hWnd;
WORD message;
WORD wParam;
DWORD lParam;
{
switch (message)
{
case WM_COMMAND :
{
WORD iSaveMode = CurrMode;
CurrMode = MODE_POPUP;
EditChangeItem(wParam);
CurrMode = iSaveMode;
break;
}
case WM_RBUTTONDOWN:
{
int mouserow = HIWORD(lParam);
int mousecol = LOWORD(lParam);
RECT rMenuBar;
/*
See if we clicked in the menubar
*/
rMenuBar = WinGetRect(hMenuBar);
if (RectContainsPoint(rMenuBar, mouserow, mousecol))
{
int iPos;
WORD iSaveMode = CurrMode;
CurrMode = MODE_MENUBAR;
if ((iPos = ColToItem(hMenuBar, mousecol)) >= 0)
{
/*
Determine which menubar item we clicked on, and fill the
CurrItem structure with that info.
*/
GetMenuString(hMenuBar, iPos, CurrItem.szItemText,
sizeof(CurrItem.szItemText), MF_BYPOSITION);
CurrItem.idItem = iPos;
CurrItem.fMask = GetMenuState(hMenuBar, iPos, MF_BYPOSITION);
EditDispatch();
}
CurrMode = iSaveMode;
}
SetFocus(hMain);
return TRUE;
}
case WM_HELP :
HelpDialog();
break;
case WM_QUIT :
/* End the dialog editor */
exit(0);
default :
/* Call the default window procedure for the main window */
return DefWinProc(hWnd, message, wParam, lParam);
}
return FALSE;
}
EditDispatch()
{
int rc;
/*
Put up the menu item dialog box and get the user's choice of action
*/
LoadMenugenDialog();
if ((rc = DialogBox(hItemDlg)) == IDCANCEL)
return TRUE;
if (rc == ID_EDIT_ADD || rc == ID_EDIT_INSERT || rc == ID_EDIT_CHANGE)
{
if (CurrMode == MODE_MENUBAR)
{
HMENU hPop;
if (rc == ID_EDIT_ADD || rc == ID_EDIT_INSERT)
{
WORD cmdmask = (rc == ID_EDIT_INSERT) ? MF_INSERT : MF_APPEND;
WORD idChgItem = (rc == ID_EDIT_INSERT) ? 0 : 0;
if (CurrItem.fMask & MF_POPUP)
CurrItem.idItem = hPop = CreateMenu(hMenuBar);
ChangeMenu(hMenuBar,
idChgItem,
CurrItem.szItemText,
CurrItem.idItem,
cmdmask | MF_STRING | (CurrItem.fMask & ~MF_SHADOW));
if (CurrItem.fMask & MF_POPUP)
CreateDefaultPopup(hPop, CurrItem.fMask & MF_SHADOW);
}
else /* rc == ID_EDIT_CHANGE */
{
ChangeMenu(hMenuBar, CurrItem.idItem, CurrItem.szItemText, 0,
MF_BYPOSITION | MF_CHANGE | MF_STRING |
(CurrItem.fMask & ~(MF_SHADOW|MF_POPUP)));
}
DrawMenuBar(hMenuBar);
}
else /* CurrMode == MF_POPUP */
{
if (rc == ID_EDIT_ADD || rc == ID_EDIT_INSERT)
{
WORD cmdmask = (rc == ID_EDIT_INSERT) ? MF_INSERT : MF_APPEND;
WORD idChgItem = (rc == ID_EDIT_INSERT) ? 0 : 0;
ChangeMenu(CurrItem.hMenu,
idChgItem,
CurrItem.szItemText,
CurrItem.idItem,
cmdmask | MF_STRING | CurrItem.fMask);
}
else /* rc == ID_EDIT_CHANGE */
{
ChangeMenu(CurrItem.hMenu, CurrItem.idItem, CurrItem.szItemText, 0,
MF_BYCOMMAND | MF_CHANGE | MF_STRING | CurrItem.fMask);
}
}
}
else if (rc == ID_EDIT_DELETE)
{
EditDeleteItem(CurrItem.idItem);
}
}
EditChangeItem(idItem)
{
int rc;
int pos;
HMENU hPop;
memset(&CurrItem, 0, sizeof(CurrItem));
CurrItem.idItem = idItem;
for (pos = 0; (hPop = GetSubMenu(hMenuBar, pos)); pos++)
{
if (GetMenuString(hPop, idItem, CurrItem.szItemText,
sizeof(CurrItem.szItemText), MF_BYCOMMAND) > 0)
{
CurrItem.fMask = GetMenuState(hPop, idItem, MF_BYCOMMAND);
break;
}
}
if ((CurrItem.hMenu = hPop) == NULLHWND)
{
MessageBox("Can't locate the item", NULL, NULL, "Error", MB_OK);
return FALSE;
}
EditDispatch();
}
EditDeleteItem(idItem)
{
int rc;
int pos;
HMENU hPop;
memset(&CurrItem, 0, sizeof(CurrItem));
CurrItem.idItem = idItem;
if (CurrMode == MODE_MENUBAR)
{
hPop = hMenuBar;
}
else
{
for (pos = 0; (hPop = GetSubMenu(hMenuBar, pos)); pos++)
if (GetMenuString(hPop, idItem, CurrItem.szItemText,
sizeof(CurrItem.szItemText), MF_BYCOMMAND) > 0)
{
CurrItem.fMask = GetMenuState(hPop, idItem, MF_BYCOMMAND);
break;
}
}
if ((CurrItem.hMenu = hPop) == NULLHWND)
{
MessageBox("Can't locate the item", NULL, NULL, "Error", MB_OK);
return FALSE;
}
if (MessageBox("Are you sure that you",
"want to delete the item",
CurrItem.szItemText, "Warning!", MB_YESNO) == IDNO)
return FALSE;
ChangeMenu(hPop, idItem, NULL, 0,
(CurrMode == MODE_MENUBAR) ? (MF_DELETE | MF_BYPOSITION)
: (MF_DELETE | MF_BYCOMMAND));
DrawMenuBar(hMenuBar);
return TRUE;
}
/***************************************************************************/
/* */
/* ColToItem() - given a menubar and an absolute column, return the */
/* index of the menubar item clicked on. */
/* */
/***************************************************************************/
ColToItem(hBar, mousecol)
HMENU hBar;
int mousecol;
{
RECT rBar;
int totalLen = 0;
int itemLen;
int iPos;
char szItem[80];
/*
Convert screen to window coordinates
*/
rBar = WinGetRect(hBar);
mousecol -= rBar.left;
/*
Go through all of the items in the menubar, totalling up the lengths
of the strings. When we find an item that intersects the column,
the return the index of the string.
*/
for (iPos = 0;
(itemLen = GetMenuString(hBar,iPos,szItem,sizeof(szItem),MF_BYPOSITION));
iPos++)
{
if (mousecol >= totalLen + 1 && mousecol < totalLen + itemLen + 1)
return iPos;
else
totalLen += itemLen + 2;
}
return -1;
}
/**************************************************************************/
/* */
/* CheckBox-to-mask mapping */
/* */
/**************************************************************************/
typedef struct checkbox_to_mask
{
WORD idCheckBox;
WORD fMaskBits;
} CHECKBOX_TO_MASK;
CHECKBOX_TO_MASK CheckBoxToMask[] =
{
ID_SEPARATOR, MF_SEPARATOR,
ID_SHADOW, MF_SHADOW,
ID_HAS_POPUP, MF_POPUP,
ID_WMHELP, MF_HELP,
ID_DISABLED, MF_DISABLED,
ID_CHECKED, MF_CHECKED
};
/**************************************************************************/
/* */
/* MenuItemDlgProc() */
/* Processes the mneu item information dialog box. */
/* */
/**************************************************************************/
BOOL pascal MenuItemDlgProc(hDlg, msg, wParam, lParam)
HWND hDlg;
WORD msg;
WORD wParam;
DWORD lParam;
{
int i;
switch (msg)
{
case WM_INITDIALOG :
/*
Transfer the data from CurrItem into the dialog controls
*/
SetDialogText(hDlg, ID_ITEMTEXT, CurrItem.szItemText);
SetDialogInt(hDlg, ID_ITEMID, CurrItem.idItem);
for (i = 0; i < sizeof(CheckBoxToMask)/sizeof(CheckBoxToMask[0]); i++)
CheckDialogItem(hDlg, CheckBoxToMask[i].idCheckBox,
(CurrItem.fMask & CheckBoxToMask[i].fMaskBits) ? TRUE : FALSE);
return TRUE;
case WM_QUIT :
EndDialog(hDlg, IDCANCEL);
break;
case WM_COMMAND :
if (wParam == IDCANCEL)
EndDialog(hDlg, IDCANCEL);
else if (wParam==ID_EDIT_ADD || wParam==ID_EDIT_INSERT || wParam==ID_EDIT_CHANGE)
{
/*
Transfer the data from the dialog controls into the CurrItem struct
*/
GetDlgItemText(hDlg, ID_ITEMTEXT,
CurrItem.szItemText, sizeof(CurrItem.szItemText));
GetDialogInt(hDlg, ID_ITEMID, &CurrItem.idItem);
/*
Build the attribute mask from the checkbox states
*/
CurrItem.fMask = 0x0000;
for (i = 0; i < sizeof(CheckBoxToMask)/sizeof(CheckBoxToMask[0]); i++)
if (IsDialogButtonChecked(hDlg, CheckBoxToMask[i].idCheckBox))
CurrItem.fMask |= CheckBoxToMask[i].fMaskBits;
EndDialog(hDlg, wParam);
}
else if (wParam == ID_EDIT_DELETE)
{
GetDialogInt(hDlg, ID_ITEMID, &CurrItem.idItem);
EndDialog(hDlg, ID_EDIT_DELETE);
}
return TRUE;
case WM_CHAR :
if (wParam == VK_ESC)
EndDialog(hDlg, IDCANCEL);
return FALSE;
default :
return FALSE;
}
}
/**************************************************************************/
/* */
/* CreateDefaultPopup() */
/* Attaches NUM_DEFAULT_ITEMS popup items to a menubar entry. */
/* */
/**************************************************************************/
#define NUM_DEFAULT_ITEMS 1
CreateDefaultPopup(hPop, hasShadow)
HMENU hPop;
WORD hasShadow;
{
int i;
char buf[32];
for (i = 0; i < NUM_DEFAULT_ITEMS; i++)
{
sprintf(buf, "Item%d", idLastItem);
ChangeMenu(hPop, 0, buf, idLastItem++, MF_APPEND | hasShadow);
}
}
LoadMenugenDialog()
{
WORD hRes = OpenResourceFile("menugen");
if (!hRes)
{
printf("Can't open menugen.res\n");
exit(1);
}
hItemDlg = LoadDialog(hRes, DLG_MENUITEM, hMain, MenuItemDlgProc);
CloseResourceFile(hRes);
}
/****************************************************************************/
/* */
/* CODE GENERATION FOR THE MENUS... */
/* */
/****************************************************************************/
#define ID_MENU 256
GenerateRCCode(hMenuBar, szFileName)
HMENU hMenuBar; /* handle to menubar */
char *szFileName; /* name of file to write the description to */
{
int indent = 0;
int nBarItems, nPopupItems;
int iBar, iItem;
WORD fFlags;
WORD id;
char szText[80];
HMENU hPop;
FILE *RCfp;
if ((nBarItems = GetMenuItemCount(hMenuBar)) <= 0)
return FALSE;
/*
Open the RC file
*/
if ((RCfp = fopen(szFileName, "w")) == NULL)
{
MessageBox("Cannot open the file", szFileName, NULL, "Error", MB_OK);
return FALSE;
}
/*
Print the menu header
*/
fprintf(RCfp, "%d MENU\nBEGIN\n", ID_MENU);
indent++;
for (iBar = 0; iBar < nBarItems; iBar++)
{
if ((hPop = GetSubMenu(hMenuBar, iBar)) != NULLHWND)
{
/*
Print a popup item
*/
GetMenuString(hMenuBar, iBar, szText, sizeof(szText), MF_BYPOSITION);
print_indent(RCfp, indent);
fprintf(RCfp, "POPUP \"%s\"\nBEGIN\n", szText);
indent++;
if ((nPopupItems = GetMenuItemCount(hPop)) > 0)
for (iItem = 0; iItem < nPopupItems; iItem++)
{
fFlags = GetMenuState(hPop, iItem, MF_BYPOSITION);
if (fFlags & MF_SEPARATOR)
{
print_indent(RCfp, indent);
fprintf(RCfp, "MENUITEM SEPARATOR\n");
continue;
}
id = GetMenuItemID(hPop, iItem);
GetMenuString(hPop, iItem, szText, sizeof(szText), MF_BYPOSITION);
print_indent(RCfp, indent);
fprintf(RCfp, "MENUITEM \"%s\", %d", szText, id);
print_flags(RCfp, fFlags);
fprintf(RCfp, "\n");
}
indent--;
print_indent(RCfp, indent);
fprintf(RCfp, "END\n");
}
else
{
/*
Print a menubar item
*/
fFlags = GetMenuState(hMenuBar, iBar, MF_BYPOSITION);
id = GetMenuItemID(hMenuBar, iBar);
GetMenuString(hMenuBar, iBar, szText, sizeof(szText), MF_BYPOSITION);
print_indent(RCfp, indent);
fprintf(RCfp, "MENUITEM \"%s\", %d");
print_flags(RCfp, fFlags);
fprintf(RCfp, "\n");
}
}
indent--;
fprintf(RCfp, "END\n\n");
fclose(RCfp);
return TRUE;
}
/****************************************************************************/
/* */
/* print_flags() - converts attr bitmask to English description for .RC */
/* */
/****************************************************************************/
print_flags(fp, flags)
FILE *fp;
WORD flags;
{
BOOL bNeedComma = FALSE;
ATTRINFO *a;
if (!flags)
return FALSE;
fprintf(fp, ", ");
for (a=AttrInfo; a < AttrInfo + sizeof(AttrInfo)/sizeof(AttrInfo[0]); a++)
if (flags & a->fBitMask)
{
if (bNeedComma)
fprintf(fp, ", ");
fprintf(fp, a->szAttr);
bNeedComma = TRUE;
}
return TRUE;
}
/****************************************************************************/
/* */
/* print_indent() - prints out the proper number of leading blanks */
/* */
/****************************************************************************/
#define INDENT_VAL 2
print_indent(fp, indent)
FILE *fp;
int indent;
{
for (indent *= INDENT_VAL; indent; indent--)
fprintf(fp, " ");
}
/****************************************************************************/
/* */
/* SAVING A MENU TO AN RC FILE..... */
/* */
/****************************************************************************/
SaveFileDlg()
{
HWND hDlg;
WORD hRes;
extern BOOL pascal DlgFnSave();
hRes = OpenResourceFile("menugen");
if (!hRes)
return FALSE;
hDlg = LoadDialog(hRes, DLG_MENUSAVE, hMain, DlgFnSave);
CloseResourceFile(hRes);
return (hDlg) ? DialogBox(hDlg) : FALSE;
}
BOOL pascal DlgFnSave(hDlg, msg, wParam, lParam)
HWND hDlg;
WORD msg;
WORD wParam;
DWORD lParam;
{
char szFileName[80];
switch (msg)
{
case WM_INITDIALOG :
return TRUE;
case WM_COMMAND :
if (wParam == IDCANCEL)
EndDialog(hDlg, IDCANCEL);
else if (wParam == IDOK)
{
GetDialogText(hDlg, ID_ITEMTEXT, (char far *) szFileName, 65);
if (access(szFileName, 0x0000))
{
if (MessageBox("The file", szFileName, "exists. Overwrite?", "Warning",
MB_YESNO) == IDNO)
return TRUE;
}
if (GenerateRCCode(hMenuBar, szFileName))
{
strcpy(CurrFileName, szFileName);
EndDialog(hDlg, IDOK);
}
}
return TRUE;
case WM_CHAR :
if (wParam == VK_ESC)
{
EndDialog(hDlg, VK_ESC);
}
return FALSE;
default :
return FALSE;
}
}
/***************************************************************************/
/* */
/* MenuList() - puts up a dialog box which shows a list of the menus */
/* in the given .RES file. */
/* */
/***************************************************************************/
WORD hModule;
HMENU MenuList(char *szFileName)
{
HDLG hDlg;
HMENU hMenu;
WORD hRes;
extern BOOL pascal DlgListDialogProc();
hRes = OpenResourceFile("menugen");
if (!hRes)
{
printf("Can't open menugen.res\n");
exit(1);
}
hDlg = LoadDialog(hRes, DLG_MENULIST, hMain, DlgListDialogProc);
CloseResourceFile(hRes);
if ((hModule = OpenResourceFile(szFileName)) == FALSE)
return NULLHWND;
hMenu = (HMENU) DialogBox(hDlg);
CloseResourceFile(hModule);
return hMenu;
}
BOOL pascal DlgListDialogProc(hDlg, msg, wParam, lParam)
HWND hDlg;
WORD msg;
WORD wParam;
DWORD lParam;
{
RESOURCE res;
HMENU hNewMenu;
WORD iSel;
char buf[33];
switch (msg)
{
case WM_INITDIALOG :
lseek(hModule, 0L, 0);
/*
Gather the names of all menu resources
*/
for (;;)
{
if (read(hModule, &res, sizeof(res)) <= 0)
break;
if (res.iResType == RT_MENU)
{
/*
Add it to the listbox
*/
sprintf(buf, "%u", res.iResID);
SendDlgItemMessage(hDlg, ID_MENULIST_LISTBOX, LB_ADDSTRING, 0,
(DWORD) (char far *) buf);
}
if (lseek(hModule, (long) res.nResBytes, 1) == -1)
break;
}
return TRUE;
case WM_COMMAND :
switch (wParam)
{
case IDOK :
/*
Did we choose a menu? If so, find out which one.
*/
iSel = SendDlgItemMessage(hDlg, ID_MENULIST_LISTBOX, LB_GETCURSEL, 0, 0L);
if (iSel == LB_ERR)
return TRUE;
/*
Get the id of the menu to load
*/
SendDlgItemMessage(hDlg, ID_MENULIST_LISTBOX, LB_GETTEXT, iSel,
(DWORD) (char far *) buf);
iSel = atoi(buf);
/*
Load the chosen menu and return the menu handle to the caller.
*/
if ((hNewMenu = LoadMenu(hModule, iSel, hWndMenu)))
EndDialog(hDlg, hNewMenu);
else
MessageBox("This menu could not be loaded", NULL, NULL, "ERROR", MB_OK);
return TRUE;
case IDCANCEL :
EndDialog(hDlg, FALSE);
return TRUE;
}
} /* switch */
return FALSE;
}
/***************************************************************************/
/* */
/* ChangeExtension() - changes a filename's extension to szExt */
/* */
/***************************************************************************/
ChangeExtension(fname, szExt)
char *fname, *szExt;
{
char *pExt = strchr(fname, '.');
if (pExt)
strcpy(pExt, szExt);
else
strcat(fname, szExt);
}
/***************************************************************************/
/* */
/* CtrlDisplayStatus() - displays the status message */
/* */
/***************************************************************************/
CtrlDisplayStatus(str)
char *str;
{
WinClear(hStatus);
WinPuts(hStatus, 0, 0, str, WinGetAttr(hStatus));
}
/***************************************************************************/
/* */
/* HELP ROUTINES */
/* */
/***************************************************************************/
HelpDialog()
{
HDLG hDlg;
WORD hRes;
extern BOOL pascal HelpDialogProc();
hRes = OpenResourceFile("menugen");
if (!hRes)
{
printf("Can't open menugen.res\n");
exit(1);
}
hDlg = LoadDialog(hRes, DLG_HELP, hMain, HelpDialogProc);
CloseResourceFile(hRes);
return (hDlg) ? DialogBox(hDlg) : FALSE;
}
BOOL pascal HelpDialogProc(hDlg, msg, wParam, lParam)
HWND hDlg;
WORD msg;
WORD wParam;
DWORD lParam;
{
char szBuf[80];
static FILE *fHelp;
HWND hLB;
HWND hTopics;
int iTopic;
static int bShowingTopics;
switch (msg)
{
case WM_INITDIALOG :
if ((fHelp = fopen("MENUGEN.HLP", "r")) == NULL)
{
MessageBox("Can't open MENUGEN.HLP", NULL, NULL, "Error", MB_OK);
EndDialog(hDlg, IDCANCEL);
return TRUE;
}
get_topics:
hLB = GetDlgItem(hDlg, ID_HELP_LB);
SendMessage(hLB, LB_RESETCONTENT, 0, 0L);
ListBoxSetRedraw(hLB, FALSE);
while (fgets(szBuf, sizeof(szBuf), fHelp) != NULL && *szBuf != '\f')
{
char *szEnd = szBuf + strlen(szBuf) - 1;
if (*szEnd == '\n')
*szEnd = '\0';
SendMessage(hLB, LB_ADDSTRING, 0, (DWORD) (char far *) szBuf);
}
ListBoxSetRedraw(hLB, TRUE);
SetFocus(hLB);
hTopics = GetDlgItem(hDlg, ID_TOPICS);
EnableWindow(hTopics, FALSE);
WinSetAttr(hTopics, 0x37);
bShowingTopics = TRUE;
return TRUE;
case WM_QUIT :
EndDialog(hDlg, IDCANCEL);
break;
case WM_COMMAND :
switch (wParam)
{
case IDCANCEL :
fclose(fHelp);
EndDialog(hDlg, IDCANCEL);
break;
case IDOK :
if (bShowingTopics)
{
/* Show a help item */
hLB = GetDlgItem(hDlg, ID_HELP_LB);
iTopic = SendMessage(hLB, LB_GETCURSEL, 0, 0L);
while (iTopic)
{
if (fgets(szBuf, sizeof(szBuf), fHelp) == NULL)
break;
if (*szBuf == '\f')
iTopic--;
}
hLB = GetDlgItem(hDlg, ID_HELP_LB);
SendMessage(hLB, LB_RESETCONTENT, 0, 0L);
ListBoxSetRedraw(hLB, FALSE);
while (fgets(szBuf, sizeof(szBuf), fHelp) != NULL && *szBuf != '\f')
{
char *szEnd = szBuf + strlen(szBuf) - 1;
if (*szEnd == '\n')
*szEnd = '\0';
SendMessage(hLB, LB_ADDSTRING, 0, (DWORD) (char far *) szBuf);
}
ListBoxSetRedraw(hLB, TRUE);
SetFocus(hLB);
bShowingTopics = FALSE;
hTopics = GetDlgItem(hDlg, ID_TOPICS);
EnableWindow(hTopics, TRUE);
WinSetAttr(hTopics, 0x31);
}
else
{
rewind(fHelp);
goto get_topics;
}
break;
case ID_TOPICS:
rewind(fHelp);
goto get_topics;
}
return TRUE;
case WM_CHAR :
if (wParam == VK_ESC)
{
fclose(fHelp);
EndDialog(hDlg, IDCANCEL);
}
return FALSE;
default :
return FALSE;
}
}