|
|
Microsoft OS/2 SDK 2.0 05-30-1990
/*==============================================================*\
* User.c - routines for handling messages not processed
* by the standard message processing routine
* Created 1990, Microsoft, IBM Corp.
*--------------------------------------------------------------
*
* This module contains the code for processing messages sent
* to the standard window that the standard window does not
* process. The application developer need only modify this
* file in order to implement new menu items or process
* messages not handled by the standard message routine.
*
* This module also contains some routines that demonstate the
* various dialog box controls and message box types that can
* be used. The sample code should be deleted when this
* module is modified for an application. The demonstration
* code is identified by comments.
*
*--------------------------------------------------------------
*
* This source file contains the following functions:
*
* UserWndProc(hwnd, msg, mp1, mp2) - user window procedure
* UserCommand(mp1, mp2) - user WM_COMMAND processor
* SetForegroundColor(hwnd)
* SetBackgroundColor(MenuId)
* SetWindowText(hwnd)
*
\*==============================================================*/
/*--------------------------------------------------------------*\
* Include files, macros, defined constants, and externs
\*--------------------------------------------------------------*/
#define INCL_WINMENUS
#define INCL_WINWINDOWMGR
#define INCL_WINCLIPBOARD
#define INCL_WINMLE
#define INCL_WINSTDFONT
#define INCL_GPILCIDS
#define INCL_GPIPRIMITIVES
#include <os2.h>
#include "sty_main.h"
#include "sty_xtrn.h"
#include "sty_dlg.h"
#include <string.h>
// #define FONT_DLG_ENABLED
/*--------------------------------------------------------------*\
* Global variables
\*--------------------------------------------------------------*/
ULONG clrForeground = CLR_NEUTRAL; /* color for window text */
ULONG clrBackground = CLR_BACKGROUND; /* color for window background */
/*--------------------------------------------------------------*\
* Entry point declarations
\*--------------------------------------------------------------*/
VOID SetForegroundColor(VOID);
VOID SetBackgroundColor(SHORT idMenu);
VOID SetFont(VOID);
MRESULT EXPENTRY DemoDlgProc(HWND hwnd, USHORT msg,
MPARAM mp1, MPARAM mp2);
MRESULT EXPENTRY PresParamDemoDlgProc(HWND hwnd, USHORT msg,
MPARAM mp1, MPARAM mp2);
VOID ShowDemoDlg(SHORT idMenuItem);
VOID ShowDemoMsgBox(SHORT idMenuItem);
/****************************************************************\
* Non-standard window message processing routine
*--------------------------------------------------------------
*
* Name: UserWndProc(hwnd, msg, mp1, mp2)
*
* Purpose: Process any messages sent to hwndMain that
* are not processed by the standard window
* procedure
*
* Usage: Routine is called for each message MainWndProc
* does not process
*
* Method: A switch statement branches control based upon
* the message passed. Any messages not processed
* here must be passed onto WinDefWindowProc()
*
* Returns: Return value depended upon the message processed
\****************************************************************/
MRESULT UserWndProc(hwnd, msg, mp1, mp2)
HWND hwnd; /* handle of window */
USHORT msg; /* id of message */
MPARAM mp1; /* first message parameter */
MPARAM mp2; /* second message parameter */
{
switch(msg) {
/*--------------------------------------------------------------*\
* Add case statements for message ids you wish to process
\*--------------------------------------------------------------*/
case WM_SIZE:
/* re-size the MLE to be the same width and height as the
client window */
WinSetWindowPos(hwndMLE,
HWND_TOP,
0,
0,
SHORT1FROMMP(mp2),
SHORT2FROMMP(mp2),
SWP_SIZE);
break;
case WM_SETFOCUS:
if(SHORT1FROMMP(mp1))
WinPostMsg(hwnd, SM_SETFOCUS, NULL, NULL);
break;
case SM_SETFOCUS:
WinSetFocus(HWND_DESKTOP, hwndMLE);
break;
default: /* default must call WinDefWindowProc() */
return(WinDefWindowProc(hwnd, msg, mp1, mp2));
break;
}
return 0L;
} /* UserWndProc() */
/****************************************************************\
* Non-standard menu item command processing procedure
*--------------------------------------------------------------
*
* Name: UserCommand(mp1, mp2)
*
* Purpose: Process any WM_COMMAND messages send to hwndMain
* that are not processed by MainCommand
*
* Usage: Routine is called for each WM_COMMAND that is
* not posted by a standard menu item
*
* Method: A switch statement branches control based upon
* the id of the control which posted the message
*
* Returns:
\****************************************************************/
VOID UserCommand(mp1, mp2)
MPARAM mp1; /* first message parameter */
MPARAM mp2; /* second message parameter */
{
switch(SHORT1FROMMP(mp1)) {
/*--------------------------------------------------------------*\
* Add case statements for menuitem ids you wish to process
\*--------------------------------------------------------------*/
/*--------------------------------------------------------------*\
* The following code is for the demonstration code only. It
* should be removed when modifying this file for your
* application.
\*--------------------------------------------------------------*/
#ifdef PALETTE_DLG_ENABLED
case IDM_OPTIONSFORECOLOR:
SetForegroundColor(hwndMLE);
break;
#endif
case IDM_OPTIONSBACKCOLORPINK:
case IDM_OPTIONSBACKCOLORCYAN:
case IDM_OPTIONSBACKCOLORYELLOW:
case IDM_OPTIONSBACKCOLORDEFAULT:
SetBackgroundColor(SHORT1FROMMP(mp1));
break;
#ifdef FONT_DLG_ENABLED
case IDM_OPTIONSFONT:
SetFont();
break;
#endif
case IDM_DEMODLGBUTTONS:
case IDM_DEMODLGLISTBOXES:
case IDM_DEMODLGCOMBOBOXES:
case IDM_DEMODLGENTRYFIELDS:
case IDM_DEMODLGSTATIC:
ShowDemoDlg(SHORT1FROMMP(mp1));
break;
case IDM_DEMOMSGBOXOK:
case IDM_DEMOMSGBOXOKCANCEL:
case IDM_DEMOMSGBOXYESNO:
case IDM_DEMOMSGBOXYESNOCANCEL:
case IDM_DEMOMSGBOXRETRYCANCEL:
case IDM_DEMOMSGBOXABORT:
case IDM_DEMOMSGBOXENTER:
case IDM_DEMOMSGBOXENTERCANCEL:
case IDM_DEMOMSGBOXQUERY:
case IDM_DEMOMSGBOXWARNING:
case IDM_DEMOMSGBOXINFO:
case IDM_DEMOMSGBOXCRITICAL:
case IDM_DEMOMSGBOXAPP:
case IDM_DEMOMSGBOXSYS:
case IDM_DEMOMSGBOXMOVEABLE:
case IDM_DEMOMSGBOXHELP:
ShowDemoMsgBox(SHORT1FROMMP(mp1));
break;
case IDM_DEMODLGPP:
WinDlgBox(hwndMain,
hwndMain,
(PFNWP)PresParamDemoDlgProc,
NULL,
IDD_PPDEMODLG,
(PVOID)NULL);
break;
/*--------------------------------------------------------------*\
* End of demonstration code
\*--------------------------------------------------------------*/
default:
break;
}
/* This routine currently doesn't use the mp2 parameter but *\
* it is referenced here to prevent an 'Unreferenced Parameter'
\* warning at compile time. */
mp2;
} /* UserCommand() */
/****************************************************************\
* Menu item intialization routine
*--------------------------------------------------------------
*
* Name: InitMenu(mp1, mp2)
*
* Purpose: Processes the WM_INITMENU message for the main window,
* disabling any menus that are not active
*
* Usage: Routine is called each time a menu is dropped
*
* Method: A switch statement branches control based upon
* the id of the menu which is being displayed
*
* Returns:
\****************************************************************/
VOID InitMenu(mp1, mp2)
MPARAM mp1; /* first message parameter */
MPARAM mp2; /* second message parameter */
{
SHORT fsFmtInfo;
BOOL fEnable;
switch(SHORT1FROMMP(mp1)) {
case IDM_FILE:
/*
* The Print, Print Setup, and Page Setup menu items of the
* File menu will be enabled if printing is enabled, otherwise
* they will be disabled
*/
EnableMenuItem(HWNDFROMMP(mp2), IDM_FILEPRINT, fPrintEnabled);
EnableMenuItem(HWNDFROMMP(mp2), IDM_FILEPRINTSETUP, fPrintEnabled);
EnableMenuItem(HWNDFROMMP(mp2), IDM_FILEPAGESETUP, fPrintEnabled);
break;
case IDM_HELP:
/*
* Enable or disable the Help menu depending upon whether the
* help manager has been enabled
*/
EnableMenuItem(HWNDFROMMP(mp2),
IDM_HELPHELPFORHELP, fHelpEnabled);
EnableMenuItem(HWNDFROMMP(mp2),
IDM_HELPEXTENDED, fHelpEnabled);
EnableMenuItem(HWNDFROMMP(mp2),
IDM_HELPKEYS, fHelpEnabled);
EnableMenuItem(HWNDFROMMP(mp2),
IDM_HELPINDEX, fHelpEnabled);
/** REMEMBER: add a case for IDM_HELPTUTORIAL if you include
the menu item **/
break;
case IDM_EDIT:
/* if text is selected in the MLE, the enable the Cut, Copy,
and Clear menus. Otherwise, do not */
fEnable = WinSendMsg(hwndMLE,
MLM_QUERYSEL,
MPFROMSHORT(MLFQS_MINSEL),
NULL) != WinSendMsg(hwndMLE,
MLM_QUERYSEL,
MPFROMSHORT(MLFQS_MAXSEL),
NULL);
EnableMenuItem(HWNDFROMMP(mp2), IDM_EDITCUT, fEnable);
EnableMenuItem(HWNDFROMMP(mp2), IDM_EDITCOPY, fEnable);
EnableMenuItem(HWNDFROMMP(mp2), IDM_EDITCLEAR, fEnable);
/* determine if the MLE can Undo the last action. If it can't,
then disable he Undo menu */
fEnable =
HIUSHORT(WinSendMsg(hwndMLE, MLM_QUERYUNDO, NULL, NULL)) != 0;
EnableMenuItem(HWNDFROMMP(mp2), IDM_EDITUNDO, fEnable);
/* determine if the clipboard has some text on it. If it
doesn't, then disable the Paste menu */
if(WinOpenClipbrd(hab)) {
if(WinQueryClipbrdFmtInfo(hab, CF_TEXT, &fsFmtInfo))
fEnable = TRUE;
else
fEnable = FALSE;
WinCloseClipbrd(hab);
} else
fEnable = TRUE;
EnableMenuItem(HWNDFROMMP(mp2), IDM_EDITPASTE, fEnable);
break;
default:
break;
}
} /* InitMenu() */
/****************************************************************\
* Enables/Disables the menu item of the given menu
*--------------------------------------------------------------
*
* Name: EnableMenuItem(hwndMenu, idItem, fEnable)
*
* Purpose: Enables or disables the menu item
*
* Usage: Called whenever a menu item is to enabled or
* disabled
*
* Method: Sends a MM_SETITEMATTR to the menu with the
* given item id. Sets the MIA_DISABLED attribute
* flag if the item is to be disabled, clears the flag
* if enabling
*
* Returns:
*
\****************************************************************/
VOID EnableMenuItem(hwndMenu, idItem, fEnable)
HWND hwndMenu; /* Handle to the menu */
SHORT idItem; /* Id of the menu item to be enabled/disabled */
BOOL fEnable; /* flag to set enable or disable bit */
{
SHORT fsFlag;
if(fEnable)
fsFlag = 0;
else
fsFlag = MIA_DISABLED;
WinSendMsg(hwndMenu,
MM_SETITEMATTR,
MPFROM2SHORT(idItem, TRUE),
MPFROM2SHORT(MIA_DISABLED, fsFlag));
} /* EnableMenuItem() */
/****************************************************************\
* Displays the Demonstration dialog selected
*--------------------------------------------------------------
*
* Name: ShowDemoDlg(idMenuItem)
*
* Purpose: Displays the demonstration dialog for the menu
* id chosen.
*
* Usage: Called whenever a menu item from the Dialog Box
* menu of the Demo menu is selected.
*
* Method: Determines the id of the dialog template and
* creates a dialog of that template.
*
* Returns:
*
\****************************************************************/
VOID ShowDemoDlg(idMenuItem)
SHORT idMenuItem; /* Id of the menu item chosen for the dialog */
{
SHORT idDlg;
switch(idMenuItem) {
case IDM_DEMODLGBUTTONS:
idDlg = IDD_BUTTONSDLG;
break;
case IDM_DEMODLGLISTBOXES:
idDlg = IDD_LISTBOXDLG;
break;
case IDM_DEMODLGCOMBOBOXES:
idDlg = IDD_COMBOBOXDLG;
break;
case IDM_DEMODLGENTRYFIELDS:
idDlg = IDD_ENTRYFIELDDLG;
break;
case IDM_DEMODLGSTATIC:
idDlg = IDD_STATICDLG;
break;
default: /* unknown menu id */
return;
break;
}
WinDlgBox(hwndMain,
hwndMain,
(PFNWP)DemoDlgProc, /* all demos use DemoDlgProc*/
NULL,
idDlg, /* id of template */
(PVOID)idDlg); /* pass id as mp2 of WM_INITDLG */
} /* ShowDemoDlg() */
/****************************************************************\
* Displays the Demonstration message box selected
*--------------------------------------------------------------
*
* Name: ShowDemoMsgBox(idMenuItem)
*
* Purpose: Displays the demonstration message box for the menu
* id chosen.
*
* Usage: Called whenever a menu item from the Message Box
* menu of the Demo menu is selected.
*
* Method: Determines the options for the message box and then
* creates the box.
*
* Returns:
*
\****************************************************************/
VOID ShowDemoMsgBox(idMenuItem)
SHORT idMenuItem; /* Id of the menu item chosen for the dialog */
{
SHORT fsOptions, idText;
CHAR szText[MESSAGELEN];
switch(idMenuItem) {
case IDM_DEMOMSGBOXOK:
fsOptions = MB_OK;
idText = IDS_DEMOMSGBOXOK;
break;
case IDM_DEMOMSGBOXOKCANCEL:
fsOptions = MB_OKCANCEL;
idText = IDS_DEMOMSGBOXOKCANCEL;
break;
case IDM_DEMOMSGBOXYESNO:
fsOptions = MB_YESNO;
idText = IDS_DEMOMSGBOXYESNO;
break;
case IDM_DEMOMSGBOXYESNOCANCEL:
fsOptions = MB_YESNOCANCEL;
idText = IDS_DEMOMSGBOXYESNOCANCEL;
break;
case IDM_DEMOMSGBOXRETRYCANCEL:
fsOptions = MB_RETRYCANCEL;
idText = IDS_DEMOMSGBOXRETRYCANCEL;
break;
case IDM_DEMOMSGBOXABORT:
fsOptions = MB_ABORTRETRYIGNORE;
idText = IDS_DEMOMSGBOXABORT;
break;
case IDM_DEMOMSGBOXENTER:
fsOptions = MB_ENTER;
idText = IDS_DEMOMSGBOXENTER;
break;
case IDM_DEMOMSGBOXENTERCANCEL:
fsOptions = MB_ENTERCANCEL;
idText = IDS_DEMOMSGBOXENTERCANCEL;
break;
case IDM_DEMOMSGBOXQUERY:
fsOptions = MB_OK | MB_QUERY;
idText = IDS_DEMOMSGBOXQUERY;
break;
case IDM_DEMOMSGBOXWARNING:
fsOptions = MB_OK | MB_WARNING;
idText = IDS_DEMOMSGBOXWARNING;
break;
case IDM_DEMOMSGBOXINFO:
fsOptions = MB_OK | MB_INFORMATION;
idText = IDS_DEMOMSGBOXINFO;
break;
case IDM_DEMOMSGBOXCRITICAL:
fsOptions = MB_OK | MB_CRITICAL;
idText = IDS_DEMOMSGBOXCRITICAL;
break;
case IDM_DEMOMSGBOXAPP:
fsOptions = MB_OK | MB_APPLMODAL;
idText = IDS_DEMOMSGBOXAPP;
break;
case IDM_DEMOMSGBOXSYS:
fsOptions = MB_OK | MB_SYSTEMMODAL;
idText = IDS_DEMOMSGBOXSYS;
break;
case IDM_DEMOMSGBOXMOVEABLE:
fsOptions = MB_OK | MB_MOVEABLE;
idText = IDS_DEMOMSGBOXMOVEABLE;
break;
case IDM_DEMOMSGBOXHELP:
fsOptions = MB_OK | MB_HELP;
idText = IDS_DEMOMSGBOXHELP;
break;
default: /* unknown menu id */
return;
break;
}
/* get the text for the message box */
if(!WinLoadString(hab,
NULL,
idText,
MESSAGELEN,
(PSZ)szText)) {
MessageBox(hwndMain,
IDMSG_CANNOTLOADSTRING,
MB_OK | MB_ERROR,
FALSE);
return;
}
/* bring up the message box */
WinMessageBox(HWND_DESKTOP,
hwndMain,
szText,
szAppName,
IDD_DEMOMSGBOX,
fsOptions);
} /* ShowDemoMsgBox() */
/*--------------------------------------------------------------*\
* The following code is for the demonstration code only. It
* should be removed when modifying this file for your
* application.
\*--------------------------------------------------------------*/
#ifdef PALETTE_DLG_ENABLED
/****************************************************************\
* Sets the foreground color of the application window
*--------------------------------------------------------------
*
* Name: SetForegroundColor()
*
* Purpose: Allows the user to select a color for the text
* displayed in the MLE
*
* Usage: Routine is called each time the user selects the
* Foreground Color menu item from the Options menu
*
* Method: The standard color dialog is called with the
* default color table colors. If the user selects
* one, then the MLM_SETTEXTCOLOR message is sent
* to the MLE to changed its text to the color chosen
*
* Returns:
*
\****************************************************************/
VOID SetForegroundColor(VOID)
{
COLORDLG cd;
HPS hps;
/* Get the PS for the window */
hps = WinGetPS(hwndMLE);
if(!hps) {
MessageBox(hwnd, IDMSG_CANNOTGETHPS, TRUE);
return;
}
/* Initialize the COLORDLG structure */
cd.kgd.cbSize = sizeof(COLORDLG);
cd.kgd.flStyle = PGSS_CENTER;
cd.kgd.pszDlgTitle = "Foreground Color";
cd.kgd.pfnDlgProc = NULL;
cd.kgd.hmod = NULL;
cd.kgd.idDlg = 0;
cd.kgd.lReturn = 0L;
cd.kgd.x = 0;
cd.kgd.y = 0;
cd.pszButton = "Ok";
cd.fl = CDS_STATICPALETTE | CDS_CENTER | CDS_HELPBUTTON;
cd.pflFlags = NULL;
cd.iclrSel = clrForeground;
cd.cclrMax = 0;
cd.hps = hps;
if(KitColorDlg(hwndMain, (PCOLORDLG)&cd) == (HWND)NULL) {
MessageBox(hwndMain, IDMSG_CANNOTRUNCOLORDLG, TRUE);
WinReleasePS(hps);
return (HWND)NULL;
}
/* set the foreground color to the returned value */
WinSendMsg(hwndMLE, MLM_SETTEXTCOLOR, MPFROMLONG(cd.iclrSel), NULL);
clrForeground = cd.iclrSel;
WinReleasePS(hps);
} /* SetForegroundColor() */
#endif /* PALETTE_DLG_ENABLED */
/****************************************************************\
* Sets the background color of the given window
*--------------------------------------------------------------
*
* Name: SetBackgroundColor(nMenuId)
*
* Purpose: Allows the user to select a color for the window
* background
*
* Usage: Routine is called each time the user selects one
* of the Background colors listed in the Background
* Color submenu of the Options menu
*
* Method: A switch statement determines which menu item was
* chosen and then the appropriate color is placed
* into clrBackground.
*
* Returns:
*
\****************************************************************/
VOID SetBackgroundColor(idMenu)
SHORT idMenu; /* id of menu item selected */
{
switch(idMenu) {
case IDM_OPTIONSBACKCOLORPINK:
clrBackground = CLR_PINK;
break;
case IDM_OPTIONSBACKCOLORCYAN:
clrBackground = CLR_CYAN;
break;
case IDM_OPTIONSBACKCOLORYELLOW:
clrBackground = CLR_YELLOW;
break;
/*--------------------------------------------------------------*\
* For any others, including IDM_OPTIONSBACKCOLORDEFAULT, set
* the background color to the default back color
\*--------------------------------------------------------------*/
default:
clrBackground = CLR_BACKGROUND;
break;
}
WinSendMsg(hwndMLE, MLM_SETBACKCOLOR, MPFROMLONG(clrBackground), NULL);
} /* SetBackgroundColor() */
#ifdef FONT_DLG_ENABLED
/****************************************************************\
* Sets the font of the MLE
*--------------------------------------------------------------
*
* Name: SetFont()
*
* Purpose: Allows the user to select a font for the text
* displayed in the MLE
*
* Usage: Routine is called each time the user selects the
* Font menu item from the Options menu
*
* Method: The standard font dialog is called with the
* current available fonts. If the user selects
* one, then the MLM_SETFONT message is sent to the
* MLE to display its text to the font chosen
*
* Returns:
*
\****************************************************************/
VOID SetFont(VOID)
{
FONTDLG fntd;
HPS hps;
FONTMETRICS fm;
CHAR szTitle[MESSAGELEN];
/* get the current font attributes */
hps = WinGetPS(hwndMLE);
WinSendMsg(hwndMLE, MLM_QUERYFONT, MPFROMP((PFATTRS)&(fntd.fAttrs)), NULL);
GpiCreateLogFont(hps, (PSTR8)NULL, 1L, &(fntd.fAttrs));
GpiSetCharSet(hps, 1L);
GpiQueryFontMetrics(hps, sizeof(FONTMETRICS), &fm);
GpiSetCharSet(hps, LCID_DEFAULT);
GpiDeleteSetId(hps, 1L);
WinReleasePS(hps);
/* Initialize the FONTDLG structure with the current font */
fntd.cbSize = sizeof(FONTDLG);
fntd.hpsScreen = WinGetScreenPS(HWND_DESKTOP);
fntd.hpsPrinter = NULL;
if(!WinLoadString(hab, NULL, IDS_FONTDLGTITLE, MESSAGELEN, szTitle)) {
MessageBox(hwndMain, IDMSG_CANNOTLOADSTRING, MB_OK | MB_ERROR, TRUE);
return;
}
fntd.pszTitle = szTitle;
fntd.pszPreview = NULL;
fntd.pszPtSizeList = NULL;
fntd.pfnDlgProc = NULL;
strcpy(fntd.szFamilyname, fm.szFamilyname);
fntd.fxPointSize = MAKEFIXED(fm.sNominalPointSize, 0);
fntd.fl = FNTS_CENTER;
fntd.flFlags = NULL;
fntd.flType = NULL;
fntd.flTypeMask = NULL;
fntd.flStyle = NULL;
fntd.flStyleMask = NULL;
fntd.flCHSOptions = 0L;
fntd.flCHSMask = 0L;
fntd.clrFore = clrForeground;
fntd.clrBack = clrBackground;
fntd.lUser = 0L;
fntd.lReturn = 0L;
fntd.lEmHeight = 0L;
fntd.lXHeight = 0L;
fntd.lExternalLeading = 0L;
fntd.sNominalPointSize = 0L;
fntd.usWeight = fm.usWeightClass;
fntd.usWidth = fm.usWidthClass;
fntd.x = 0;
fntd.y = 0;
fntd.idDlg = IDD_FONT;
/* Bring up the standard Font Dialog */
if(!KitFontDialog(hwndMLE, &fntd)) {
WinReleasePS(fntd.hpsScreen);
return;
}
WinReleasePS(fntd.hpsScreen);
WinSendMsg(hwndMLE, MLM_SETFONT, MPFROMP(&(fntd.fAttrs)), NULL);
/* Set the new colors */
clrForeground = fntd.clrFore;
clrBackground = fntd.clrBack;
WinSendMsg(hwndMLE, MLM_SETTEXTCOLOR, MPFROMLONG(clrForeground), NULL);
WinSendMsg(hwndMLE, MLM_SETBACKCOLOR, MPFROMLONG(clrBackground), NULL);
} /* SetFont() */
#endif /* FONT_DLG_ENABLED */
/*--------------------------------------------------------------*\
* End of demonstration code
\*--------------------------------------------------------------*/
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.