home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / devnews / vol2 / sample3 / sty_main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-27  |  13.0 KB  |  431 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. BOOL fUseDeviceDriver = TRUE;
  80.  
  81. /****************************************************************
  82.  *  Name:   main()
  83.  *
  84.  *  Description: Entry point of program.
  85.  *
  86.  *  Concepts: Obtains anchor block handle and creates message
  87.  *            queue.  Calls the initialization routine.
  88.  *            Creates the main frame window which creates the
  89.  *            main client window.  Polls the message queue
  90.  *            via Get/Dispatch Msg loop.  Upon exiting the
  91.  *            loop, exits.
  92.  *
  93.  *  API's   :  WinInitilize
  94.  *             DosBeep
  95.  *             WinCreateMsgQueue
  96.  *             WinTerminate
  97.  *             WinCreateStdWindow
  98.  *             WinSetWindowText
  99.  *             WinGetMsg
  100.  *             WinDispatchMsg
  101.  *
  102.  *  Parameters: None
  103.  *
  104.  *  Returns:
  105.  *          1 - if sucessful execution completed
  106.  *          0 - if error
  107. \****************************************************************/
  108. INT main (SHORT sArgc , CHAR *ppArgv[])
  109. {
  110.    QMSG qmsg;          /* message structure */
  111.    ULONG ulCtlData;      /* frame control data */
  112.  
  113.    hab = WinInitialize(0);
  114.  
  115.    /*
  116.     *process the command line
  117.     */
  118.    if(ProcessCmdLine(sArgc,ppArgv) )
  119.    {
  120.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  121.       return(RETURN_ERROR);
  122.    }
  123.    if(!hab)
  124.    {
  125.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  126.       return(RETURN_ERROR);
  127.    }
  128.  
  129.    hmq = WinCreateMsgQueue(hab, 0);
  130.    if(!hmq)
  131.    {
  132.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  133.       WinTerminate(hab);
  134.       return(RETURN_ERROR);
  135.    }
  136.  
  137.    if(!Init())
  138.    {
  139.       MessageBox(HWND_DESKTOP, IDMSG_INITFAILED, MB_OK | MB_ERROR, TRUE);
  140.       return(RETURN_ERROR);
  141.    }
  142.  
  143.    /* NOTE:  clean up from here is handled by the DosExitList processing */
  144.  
  145.    /* create the main window */
  146.    ulCtlData = FCF_STANDARD;
  147.  
  148.    hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP, WS_VISIBLE,
  149.                                       (PVOID)&ulCtlData, (PSZ)szAppName,
  150.                                       (PSZ)NULL, WS_VISIBLE,
  151.                                       (HMODULE)NULL, IDR_MAIN,
  152.                                       (PHWND)&hwndMain);
  153.  
  154.    if(!hwndMainFrame)
  155.    {
  156.       MessageBox(HWND_DESKTOP, IDMSG_MAINWINCREATEFAILED, MB_OK | MB_ERROR,
  157.                   TRUE);
  158.       return(RETURN_ERROR);
  159.    }
  160.  
  161.    WinSetWindowText(hwndMainFrame, szAppName);
  162.  
  163.    InitHelp();
  164.  
  165.    /* Get/Dispatch Message loop                                  */
  166.    while(WinGetMsg(hab, (PQMSG)&qmsg, 0, 0, 0))
  167.        WinDispatchMsg(hab, (PQMSG)&qmsg);
  168.  
  169.    DestroyHelpInstance();
  170.  
  171.    return(RETURN_SUCCESS);
  172.  
  173. } /* End of main porcedure  */
  174.  
  175.  
  176.  
  177. /****************************************************************
  178.  *  Name:   MainWndProc
  179.  *
  180.  *  Description : Window procedure for the main clent window.
  181.  *
  182.  *  Concepts : Processes the messages sent to the main client
  183.  *             window.  This routine processes the basic
  184.  *             messages all client windows should process and
  185.  *             passes all others onto UserWndProc where the
  186.  *             developer can process any others.
  187.  *
  188.  *  API's : None
  189.  *
  190.  * Parameters   : hwnd - Window handle to which message is addressed
  191.  *                msg - Message type
  192.  *                mp1 - First message parameter
  193.  *                mp2 - Second message parameter
  194.  *
  195.  *  Returns:  Return values are determined by each message
  196.  *
  197.  ****************************************************************/
  198. MRESULT EXPENTRY MainWndProc(HWND hwnd, USHORT msg, MPARAM mp1, MPARAM mp2)
  199. {
  200.    switch(msg)
  201.    {
  202.       case WM_CREATE:
  203.          return(InitMainWindow(hwnd, mp1, mp2));
  204.          break;
  205.  
  206.       case WM_PAINT:
  207.          MainPaint(hwnd);
  208.          break;
  209.  
  210.       case WM_COMMAND:
  211.          MainCommand(mp1, mp2);
  212.          break;
  213.  
  214.       case WM_INITMENU:
  215.          InitMenu(mp1, mp2);
  216.          break;
  217.  
  218.       case HM_QUERY_KEYS_HELP:
  219.          return (MRESULT)PANEL_HELPKEYS;     /* return id of key help panel */
  220.          break;
  221.  
  222.     /*
  223.      * Any messages not processed are passed on
  224.      * to the user's window proc.  It is
  225.      * responsible for passing any messages it
  226.      * doesn't handle onto WinDefWindowProc()
  227.      */
  228.       default:
  229.          return(UserWndProc(hwnd, msg, mp1, mp2));
  230.          break;
  231.  
  232.    }
  233.    return (MRESULT)0; /* all window procedures should return 0 as a default */
  234.  
  235. }   /* End of MainWndProc() */
  236.  
  237. /*********************************************************************
  238.  *  Name:   MessageBox
  239.  *
  240.  *  Description : Message Box procedure
  241.  *
  242.  *  Concepts : Displays the warning message box with the message
  243.  *             given in idMsg retrived from the message table;
  244.  *             Called wherever an error occurs and a message should
  245.  *             be displayed to the user
  246.  *
  247.  *  API's : WinLoadString
  248.  *          WinLoadMessage
  249.  *          WinAlarm
  250.  *          WinMessageBox
  251.  *
  252.  *  Parameters :  hwndOwner - window handle of the owner of the
  253.  *                            message box
  254.  *                idMsg     - id of message to be retrieved from
  255.  *                            resource file
  256.  *                fsStyle   - message box style
  257.  *                fBeep     - flag to sound alarm
  258.  *
  259.  *  Returns: SHORT
  260.  *
  261. \****************************************************************/
  262. SHORT MessageBox(HWND hwndOwner, SHORT idMsg, SHORT fsStyle, BOOL fBeep)
  263. {
  264.    CHAR szText[MESSAGELEN];
  265.    CHAR szTitle[MESSAGELEN];
  266.    PSZ  pszTitle = NULL;
  267.  
  268.    if(!WinLoadMessage(hab, (HMODULE)NULL, idMsg, MESSAGELEN, (PSZ)szText))
  269.    {
  270.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  271.       return MBID_ERROR;
  272.    }
  273.  
  274.    if(fBeep)
  275.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  276.  
  277.    if ( (fsStyle & MB_WARNING)                        /* use warning title ? */
  278.        && WinLoadString(hab, (HMODULE)0, IDS_WARNING, MESSAGELEN, szTitle) )
  279.          pszTitle = szTitle;
  280.    else
  281.       pszTitle = NULL; /* default to error title */
  282.  
  283.    return(WinMessageBox(HWND_DESKTOP, hwndOwner, szText, pszTitle,
  284.           IDD_MSGBOX, fsStyle));
  285.  
  286. }   /* End of MessageBox() */
  287.  
  288. /****************************************************************\
  289.  *  Name:   MainCommand
  290.  *
  291.  *  Purpose: Main window WM_COMMAND processing procedure
  292.  *
  293.  *  Concepts : Routine is called whenever a WM_COMMAND message is
  294.  *             posted to the main window.  A switch statement
  295.  *             branches on the id of the menu item that posted the
  296.  *             message and the appropriate action for that item is
  297.  *             taken.  Any menu ids that are not part of the
  298.  *             standard menu set are passed onto the user defined
  299.  *             WM_COMMAND processing procedure.
  300.  *
  301.  *  API's : WinPostMsg
  302.  *
  303.  *  Parameters :  mp1 - First message parameter
  304.  *                mp2 - Second message parameter
  305.  *
  306.  *  Returns: VOID
  307.  *
  308. \****************************************************************/
  309. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  310. {
  311.    switch(SHORT1FROMMP(mp1))
  312.    {
  313.       case IDM_EXIT:
  314.          WinPostMsg( hwndMain, WM_QUIT, NULL, NULL );
  315.          break;
  316.  
  317.       case IDM_FILENEW:
  318.          FileNew(mp2);
  319.          break;
  320.  
  321.       case IDM_FILEOPEN:
  322.          FileOpen(mp2);
  323.          break;
  324.  
  325.       case IDM_FILESAVE:
  326.          FileSave(mp2);
  327.          break;
  328.  
  329.       case IDM_FILESAVEAS:
  330.          FileSaveAs(mp2);
  331.          break;
  332.  
  333.       case IDM_EDITUNDO:
  334.          EditUndo(mp2);
  335.          break;
  336.  
  337.       case IDM_EDITCUT:
  338.          EditCut(mp2);
  339.          break;
  340.  
  341.       case IDM_EDITCOPY:
  342.          EditCopy(mp2);
  343.           break;
  344.  
  345.       case IDM_EDITPASTE:
  346.          EditPaste(mp2);
  347.          break;
  348.  
  349.       case IDM_EDITCLEAR:
  350.          EditClear(mp2);
  351.          break;
  352.  
  353.       case IDM_HELPUSINGHELP:
  354.          HelpUsingHelp(mp2);
  355.          break;
  356.  
  357.       case IDM_HELPGENERAL:
  358.          HelpGeneral(mp2);
  359.          break;
  360.  
  361.       case IDM_HELPKEYS:
  362.          HelpKeys(mp2);
  363.          break;
  364.  
  365.       case IDM_HELPINDEX:
  366.          HelpIndex(mp2);
  367.          break;
  368.  
  369.       case IDM_HELPPRODINFO:
  370.          HelpProdInfo(mp2);
  371.          break;
  372.     /*
  373.      * User command processing routine is called
  374.      * here so any ids not procecessed here can be
  375.      * processed
  376.      */
  377.       default:
  378.          UserCommand(mp1, mp2);
  379.          break;
  380.    }
  381. }   /* MainCommand() */
  382.  
  383. /****************************************************************\
  384.  *
  385.  *--------------------------------------------------------------
  386.  *
  387.  *  Name:ProcessCmdLine()
  388.  *
  389.  *  Purpose:Parse arguments passed on the command line.
  390.  *
  391.  *
  392.  *
  393.  *  Usage:
  394.  *
  395.  *  Method:
  396.  *          -
  397.  *
  398.  *          -
  399.  *          -
  400.  *
  401.  *          -
  402.  *          -
  403.  *
  404.  *  Returns:
  405.  *        FALSE   - at present time
  406.  *
  407. \****************************************************************/
  408. BOOL ProcessCmdLine(SHORT sArgc,CHAR **ppArgv)
  409. {
  410.      SHORT sArgnum = 1;
  411.      BOOL  fError = FALSE;
  412.  
  413.      while(--sArgc )
  414.      {
  415.           if( *ppArgv[sArgnum] == '-'   ||
  416.               *ppArgv[sArgnum] == '/' )
  417.           {
  418.  
  419.                switch(*(ppArgv[sArgnum] + 1) )
  420.                {
  421.                case 't':
  422.                case 'T':
  423.                     fUseDeviceDriver = FALSE;
  424.                     break;
  425.                }
  426.           }
  427.           sArgnum++;
  428.      }
  429.      return(fError);
  430. }
  431.