home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / STYLE.ZIP / STY_MAIN.C < prev    next >
C/C++ Source or Header  |  1992-03-30  |  12KB  |  363 lines

  1. /*************************************************************************
  2. *
  3. *  File Name   : Sty_main.c
  4. *
  5. *  Description : This PM sample application demonstates the
  6. *                standard menus, dialogs, and controls found
  7. *                in most PM applications.
  8. *
  9. *  Concepts    : PM window creation
  10. *                Menu Creation
  11. *                Dialog box creation
  12. *                Presentation parameters
  13. *                Initizalition and display of PM controls
  14. *                Help creation and initialization
  15. *
  16. *  API's       :
  17. *  -------------
  18. *  DosAllocMem              WinFontDlg             WinCreateStdWindow
  19. *  DosBeep                  WinGetMsg              WinCreateWindow
  20. *  DosClose                 WinGetPs               WinDefDlgProc
  21. *  DosExit                  WinInitialize          WinDefDlgProc
  22. *  DosExitList              WinIsWindow            WinDefWindowProc
  23. *  DosFreeMem               WinLoadMenu            WinDestroyHelpINstance
  24. *  DosOpen                  WinLoadString          WinDestroyWIndow
  25. *  DosRead                  WinMapWIndowPoints     WinDismissDlg
  26. *  DosWrite                 WinMessageBox          WinDispatchMsg
  27. *  GpiCreateLogFont         WinOpenClipbrd         WinDlgBox
  28. *  GpiDeleteSetID           WinPopupMenu           WinEndPaint
  29. *  GpiQueryFontMetrics      WinPostMsg             WinSendMsg
  30. *  GpiQueryFonts            WinQueryClipbrdFmtInfo WinSetDlgItemText
  31. *  GpiSetCharSet            WinQueryFocus          WinSetFocus
  32. *  WinSendDlgItemMsg        WinQueryTaskTitle      WinSetPresParms
  33. *  WinTerminate             WinQueryWindowPos      WinSetWindosPos
  34. *  WinAlarm                 WinQueryWindowRect     WinSetWindowText
  35. *  WinAssociateHelpInstance WinQueryWindowText     WinTerminate
  36. *  WinBeginPaint            WinQueryWindowUShort   WinWindosFormID
  37. *  WinCloseClipbrd          WinRegisterClass       WinDestroyMsgQueue
  38. *  WinCreateHelpInstance    WinReleasePS           WinFileDlg
  39. *  WinCreateMsgQueue        WinRemovePresParms     WinFillRect
  40. *
  41. *  Copyright (C) 1992 IBM Corporation
  42. *
  43. *      DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  44. *      sample code created by IBM Corporation. This sample code is not
  45. *      part of any standard or IBM product and is provided to you solely
  46. *      for  the purpose of assisting you in the development of your
  47. *      applications.  The code is provided "AS IS", without
  48. *      warranty of any kind.  IBM shall not be liable for any damages
  49. *      arising out of your use of the sample code, even if they have been
  50. *      advised of the possibility of such damages.                                                    *
  51. *
  52. ************************************************************************/
  53.  
  54. /* Include files, macros, defined constants, and externs                */
  55.  
  56. #define  INCL_WINHELP
  57.  
  58. #include <os2.h>
  59. #include "sty_main.h"
  60. #include "sty_xtrn.h"
  61. #include "sty_help.h"
  62.  
  63. #define RETURN_SUCCESS      0            /* successful return in DosExit */
  64. #define RETURN_ERROR        1            /* error return in DosExit      */
  65. #define BEEP_WARN_FREQ      60           /* frequency of warning beep    */
  66. #define BEEP_WARN_DUR      100           /* duration of warning beep     */
  67.  
  68.  
  69. /*  Global variables                                                     */
  70.  
  71. HWND hwndMainFrame;                  /* handle to the main frame window */
  72. HWND hwndMain;                       /* handle to the main client window */
  73. HDC  hdcMain;                        /* handle to the DC of the client */
  74. HAB hab;                             /* anchor block for the process */
  75. HMQ hmq;                             /* handle to the process' message queue */
  76. CHAR szAppName[MAXNAMEL];            /* buffer for application name string */
  77. CHAR szUntitled[MESSAGELEN];         /* buffer for "Untitled" string */
  78. BOOL fHelpEnabled;                   /* flag to determine if help is enabled */
  79.  
  80. /****************************************************************
  81.  *  Name:   main()
  82.  *
  83.  *  Description: Entry point of program.
  84.  *
  85.  *  Concepts: Obtains anchor block handle and creates message
  86.  *            queue.  Calls the initialization routine.
  87.  *            Creates the main frame window which creates the
  88.  *            main client window.  Polls the message queue
  89.  *            via Get/Dispatch Msg loop.  Upon exiting the
  90.  *            loop, exits.
  91.  *
  92.  *  API's   :  WinInitilize
  93.  *             DosBeep
  94.  *             WinCreateMsgQueue
  95.  *             WinTerminate
  96.  *             WinCreateStdWindow
  97.  *             WinSetWindowText
  98.  *             WinGetMsg
  99.  *             WinDispatchMsg
  100.  *
  101.  *  Parameters: None
  102.  *
  103.  *  Returns:
  104.  *          1 - if sucessful execution completed
  105.  *          0 - if error
  106. \****************************************************************/
  107. int main (VOID)
  108. {
  109.    QMSG qmsg;          /* message structure */
  110.    ULONG ulCtlData;      /* frame control data */
  111.  
  112.    hab = WinInitialize(0);
  113.    if(!hab)
  114.    {
  115.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  116.       return(RETURN_ERROR);
  117.    }
  118.  
  119.    hmq = WinCreateMsgQueue(hab, 0);
  120.    if(!hmq)
  121.    {
  122.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  123.       WinTerminate(hab);
  124.       return(RETURN_ERROR);
  125.    }
  126.  
  127.    if(!Init())
  128.    {
  129.       MessageBox(HWND_DESKTOP, IDMSG_INITFAILED, MB_OK | MB_ERROR, TRUE);
  130.       return(RETURN_ERROR);
  131.    }
  132.  
  133.    /* NOTE:  clean up from here is handled by the DosExitList processing */
  134.  
  135.    /* create the main window */
  136.    ulCtlData = FCF_STANDARD;
  137.  
  138.    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE,
  139.                                       (PVOID)&ulCtlData, (PSZ)szAppName,
  140.                                       (PSZ)NULL, WS_VISIBLE,
  141.                                       (HMODULE)NULL, IDR_MAIN,
  142.                                       (PHWND)&hwndMain);
  143.  
  144.    if(!hwndMainFrame)
  145.    {
  146.       MessageBox(HWND_DESKTOP, IDMSG_MAINWINCREATEFAILED, MB_OK | MB_ERROR,
  147.                   TRUE);
  148.       return(RETURN_ERROR);
  149.    }
  150.  
  151.    WinSetWindowText(hwndMainFrame, szAppName);
  152.  
  153.    InitHelp();
  154.  
  155.    /* Get/Dispatch Message loop                                  */
  156.    while(WinGetMsg(hmq, (PQMSG)&qmsg, 0, 0, 0))
  157.        WinDispatchMsg(hmq, (PQMSG)&qmsg);
  158.  
  159.    DestroyHelpInstance();
  160.  
  161.    return(RETURN_SUCCESS);
  162.  
  163. } /* End of main porcedure  */
  164.  
  165.  
  166.  
  167. /****************************************************************
  168.  *  Name:   MainWndProc
  169.  *
  170.  *  Description : Window procedure for the main clent window.
  171.  *
  172.  *  Concepts : Processes the messages sent to the main client
  173.  *             window.  This routine processes the basic
  174.  *             messages all client windows should process and
  175.  *             passes all others onto UserWndProc where the
  176.  *             developer can process any others.
  177.  *
  178.  *  API's : None
  179.  *
  180.  * Parameters   : hwnd - Window handle to which message is addressed
  181.  *                msg - Message type
  182.  *                mp1 - First message parameter
  183.  *                mp2 - Second message parameter
  184.  *
  185.  *  Returns:  Return values are determined by each message
  186.  *
  187.  ****************************************************************/
  188. MRESULT EXPENTRY MainWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  189. {
  190.    switch(msg)
  191.    {
  192.       case WM_CREATE:
  193.          return(InitMainWindow(hwnd, mp1, mp2));
  194.          break;
  195.  
  196.       case WM_PAINT:
  197.          MainPaint(hwnd);
  198.          break;
  199.  
  200.       case WM_COMMAND:
  201.          MainCommand(mp1, mp2);
  202.          break;
  203.  
  204.       case WM_INITMENU:
  205.          InitMenu(mp1, mp2);
  206.          break;
  207.  
  208.       case HM_QUERY_KEYS_HELP:
  209.          return (MRESULT)PANEL_HELPKEYS;     /* return id of key help panel */
  210.          break;
  211.  
  212.     /*
  213.      * Any messages not processed are passed on
  214.      * to the user's window proc.  It is
  215.      * responsible for passing any messages it
  216.      * doesn't handle onto WinDefWindowProc()
  217.      */
  218.       default:
  219.          return(UserWndProc(hwnd, msg, mp1, mp2));
  220.          break;
  221.  
  222.    }
  223.    return (MRESULT)0; /* all window procedures should return 0 as a default */
  224.  
  225. }   /* End of MainWndProc() */
  226.  
  227. /*********************************************************************
  228.  *  Name:   MessageBox
  229.  *
  230.  *  Description : Message Box procedure
  231.  *
  232.  *  Concepts : Displays the warning message box with the message
  233.  *             given in idMsg retrived from the message table
  234.  *             Called whever an error occurs and a message wishes
  235.  *             to be displayed to the user
  236.  *
  237.  *  API's : WinLoadString
  238.  *          WinAlarm
  239.  *          WinMessageBox
  240.  *
  241.  *  Parameters :  hwndOwner - window handle of the owner of the
  242.  *                            message box
  243.  *                idMsg     - id of message to be retrieved from
  244.  *                            resource file
  245.  *                fsStyle   - message box style
  246.  *                fBeep     - flag to sound alarm
  247.  *
  248.  *  Returns: SHORT
  249.  *
  250. \****************************************************************/
  251. SHORT MessageBox(HWND hwndOwner, SHORT idMsg, SHORT fsStyle, BOOL fBeep)
  252. {
  253.    CHAR szText[MESSAGELEN];
  254.  
  255.    if(!WinLoadMessage(hab, (HMODULE)NULL, idMsg, MESSAGELEN, (PSZ)szText))
  256.    {
  257.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  258.       return MBID_ERROR;
  259.    }
  260.  
  261.    if(fBeep)
  262.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  263.  
  264.    return(WinMessageBox(HWND_DESKTOP, hwndOwner, szText, (PSZ)NULL,
  265.           IDD_MSGBOX, fsStyle));
  266.  
  267. }   /* End of MessageBox() */
  268.  
  269. /****************************************************************\
  270.  *  Name:   MainCommand
  271.  *
  272.  *  Purpose: Main window WM_COMMAND processing procedure
  273.  *
  274.  *  Concepts : Routine is called whenever a WM_COMMAND message is
  275.  *             posted to the main window.  A switch statement
  276.  *             branches on the id of the menu item that posted the
  277.  *             message and the appropriate action for that item is
  278.  *             taken.  Any menu ids that are not part of the
  279.  *             standard menu set are passed onto the user defined
  280.  *             WM_COMMAND processing procedure.
  281.  *
  282.  *  API's : WinPostMsg
  283.  *
  284.  *  Parameters :  mp1 - First message parameter
  285.  *                mp2 - Second message parameter
  286.  *
  287.  *  Returns: VOID
  288.  *
  289. \****************************************************************/
  290. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  291. {
  292.    switch(SHORT1FROMMP(mp1))
  293.    {
  294.       case IDM_EXIT:
  295.          WinPostMsg( hwndMain, WM_QUIT, NULL, NULL );
  296.          break;
  297.  
  298.       case IDM_FILENEW:
  299.          FileNew(mp2);
  300.          break;
  301.  
  302.       case IDM_FILEOPEN:
  303.          FileOpen(mp2);
  304.          break;
  305.  
  306.       case IDM_FILESAVE:
  307.          FileSave(mp2);
  308.          break;
  309.  
  310.       case IDM_FILESAVEAS:
  311.          FileSaveAs(mp2);
  312.          break;
  313.  
  314.       case IDM_EDITUNDO:
  315.          EditUndo(mp2);
  316.          break;
  317.  
  318.       case IDM_EDITCUT:
  319.          EditCut(mp2);
  320.          break;
  321.  
  322.       case IDM_EDITCOPY:
  323.          EditCopy(mp2);
  324.           break;
  325.  
  326.       case IDM_EDITPASTE:
  327.          EditPaste(mp2);
  328.          break;
  329.  
  330.       case IDM_EDITCLEAR:
  331.          EditClear(mp2);
  332.          break;
  333.  
  334.       case IDM_HELPUSINGHELP:
  335.          HelpUsingHelp(mp2);
  336.          break;
  337.  
  338.       case IDM_HELPGENERAL:
  339.          HelpGeneral(mp2);
  340.          break;
  341.  
  342.       case IDM_HELPKEYS:
  343.          HelpKeys(mp2);
  344.          break;
  345.  
  346.       case IDM_HELPINDEX:
  347.          HelpIndex(mp2);
  348.          break;
  349.  
  350.       case IDM_HELPPRODINFO:
  351.          HelpProdInfo(mp2);
  352.          break;
  353.     /*
  354.      * User command processing routine is called
  355.      * here so any ids not procecessed here can be
  356.      * processed
  357.      */
  358.       default:
  359.          UserCommand(mp1, mp2);
  360.          break;
  361.    }
  362. }   /* MainCommand() */
  363.