home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / VSCPPv8.zip / VACPP / IBMCPP / samples / TOOLKIT / OS2 / QUEUES / SVRQMAIN.C < prev    next >
C/C++ Source or Header  |  1994-11-17  |  15KB  |  497 lines

  1. /*==============================================================*\
  2.  *  Svrqmain.c - Queue Server sample app
  3.  *      Copyright 1992 IBM Corp.
  4.  *--------------------------------------------------------------
  5.  *
  6.  *  DISCLAIMER OF WARRANTIES.  The following [enclosed] code is
  7.  *  sample code created by IBM Corporation. This sample code is not
  8.  *  part of any standard or IBM product and is provided to you solely
  9.  *  for  the purpose of assisting you in the development of your
  10.  *  applications.  The code is provided "AS IS", without
  11.  *  warranty of any kind.  IBM shall not be liable for any damages
  12.  *  arising out of your use of the sample code, even if they have been
  13.  *  advised of the possibility of   such damages.
  14.  *
  15.  *--------------------------------------------------------------
  16.  *
  17.  *  This c file contains the window creation routines and
  18.  *  manages the PM message queue.
  19.  *
  20. \*==============================================================*/
  21.  
  22.  
  23.  
  24. /*--------------------------------------------------------------*\
  25.  *  Include files, macros, defined constants, and externs
  26. \*--------------------------------------------------------------*/
  27. #define INCL_WINHELP
  28. #define INCL_WININPUT
  29. #define INCL_DOSPROCESS
  30. #define INCL_DOSQUEUES
  31. #define INCL_WINMESSAGEMGR
  32. #define INCL_WINWINDOWMGR
  33. #define INCL_WINMENUS
  34. #define INCL_WINFRAMEMGR
  35.  
  36. #include <os2.h>
  37. #include <string.h>
  38. #include <stdlib.h>
  39. #include <stdio.h>
  40. #include "svrqmain.h"
  41. #include "svrqhelp.h"
  42. #include "svrqxtrn.h"
  43.  
  44. #define  THRD_EXIT_TIMEOUT  10
  45.  
  46. /*--------------------------------------------------------------*\
  47.  *  Global variables
  48. \*--------------------------------------------------------------*/
  49.  
  50. HWND   hwndMainFrame = NULLHANDLE;    /* handle to the main frame window */
  51. HWND   hwndMain;                      /* handle to the main client window */
  52. HAB    hab;                           /* anchor block for the process */
  53. HMQ    hmq;                           /* handle to the process' message queue */
  54. CHAR   szAppName[MAXNAMEL];           /* buffer for application name string */
  55. CHAR   szUntitled[TITLE_LEN];         /* buffer for "Untitled" string */
  56. BOOL   fHelpEnabled;                  /* flag to determine if help is enabled */
  57. HQUEUE hqQ;
  58. SHORT  sTotalMsgs;
  59. CHAR   SvrQMsgs[MAX_MESSAGES][MESSAGE_LEN+38];
  60. BOOL   fThrdsDead=FALSE;              /* notification of thread exitting */
  61.  
  62. unsigned int  iIndex=0;               /* beginning of circular buffer */
  63.  
  64.  
  65. /****************************************************************\
  66.  *  Main routine
  67.  *--------------------------------------------------------------
  68.  *
  69.  *  Name:   main()
  70.  *
  71.  *  Purpose: Initializes the PM environment, calls the
  72.  *              initialization routine, creates the main
  73.  *              window,  and polls the message queue
  74.  *
  75.  *  Usage:
  76.  *
  77.  *  Method:
  78.  *          - obtains anchor block handle and creates message
  79.  *              queue
  80.  *          - calls the initialization routine
  81.  *          - creates the main frame window which creates the
  82.  *              main client window
  83.  *          - polls the message queue via Get/Dispatch Msg loop
  84.  *          - upon exiting the loop, exits
  85.  *
  86.  *  Returns:
  87.  *          0 - if successful execution completed
  88.  *          1 - if error
  89. \****************************************************************/
  90. INT main(int argc, char *argv[])
  91. {
  92.    QMSG        qmsg;          /* message structure */
  93.    static int  iTimeOut=THRD_EXIT_TIMEOUT;
  94.  
  95.    hab = WinInitialize(0);
  96.  
  97.    if(!hab)
  98.    {
  99.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  100.       return(RETURN_ERROR);
  101.    }
  102.  
  103.    hmq = WinCreateMsgQueue(hab, 0);
  104.  
  105.    if(!hmq)
  106.    {
  107.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  108.       WinTerminate(hab);
  109.       return(RETURN_ERROR);
  110.    }
  111.  
  112.    if(!Init(argc, argv))
  113.    {
  114.       if(hwndMainFrame == NULLHANDLE)
  115.       {
  116.           MessageBox(HWND_DESKTOP,
  117.                      IDMSG_MAINWINCREATEFAILED,
  118.                      MB_OK | MB_ERROR,
  119.                      TRUE);
  120.       }
  121.  
  122.       else
  123.       {
  124.          MessageBox(HWND_DESKTOP,
  125.                     IDMSG_INITFAILED,
  126.                     MB_OK | MB_ERROR,
  127.                     TRUE);
  128.  
  129.       }
  130.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  131.       if (WinIsWindow(hab, hwndMainFrame))
  132.       {
  133.          WinDestroyWindow(hwndMainFrame);
  134.       }
  135.       WinDestroyMsgQueue(hmq);
  136.       WinTerminate(hab);
  137.       return(RETURN_ERROR);
  138.    }
  139.  
  140.    /* Get-Dispatch Message loop */
  141.    while(WinGetMsg(hab, (PQMSG)&qmsg, NULLHANDLE, 0, 0))
  142.       WinDispatchMsg(hab, (PQMSG)&qmsg);
  143.  
  144.  
  145.    /* destroy the help instance */
  146.    DestroyHelpInstance();
  147.  
  148.    if (WinIsWindow(hab, hwndMainFrame))
  149.       WinDestroyWindow(hwndMainFrame);
  150.    WinDestroyMsgQueue(hmq);
  151.  
  152.    /* give other thread a chance to exit */
  153.    while (!fThrdsDead && iTimeOut--)
  154.    {
  155.       DosSleep(0);
  156.    }
  157.  
  158.    WinTerminate(hab);
  159.    return(RETURN_SUCCESS);
  160.  
  161. }   /* main() */
  162.  
  163.  
  164. /****************************************************************\
  165.  *  Main client window procedure
  166.  *--------------------------------------------------------------
  167.  *
  168.  *  Name:   MainWndProc(hwnd, msg, mp1, mp2)
  169.  *
  170.  *  Purpose: Processes the messages sent to the main client
  171.  *              window.  This routine processes the basic
  172.  *              messages all client windows should process
  173.  *              and passes all others onto WinDefWindowProc.
  174.  *
  175.  *  Usage:  Called for each message placed in the main
  176.  *          window's message queue
  177.  *
  178.  *  Method: a switch statement branches to the routines to be
  179.  *          performed for each message processed.
  180.  *
  181.  *  Returns:  Return values are determined by each message
  182.  *
  183. \****************************************************************/
  184. MRESULT EXPENTRY MainWndProc(HWND   hwnd,    /* handle of window */
  185.                              ULONG  msg,     /* id of message */
  186.                              MPARAM mp1,     /* first message parameter */
  187.                              MPARAM mp2)     /* second message parameter */
  188. {
  189.    QDATA       *pqdataQ;
  190.  
  191.    switch(msg)
  192.    {
  193.       case WM_CREATE:
  194.          return(InitMainWindow(hwnd, mp1, mp2));
  195.          break;
  196.  
  197.       case WM_PAINT:
  198.          MainPaint(hwnd, SvrQMsgs, iIndex, sTotalMsgs);
  199.          break;
  200.  
  201.       case WM_SIZE:
  202.          MainSize(hwnd, sTotalMsgs, mp1, mp2);
  203.          break;
  204.  
  205.       case WM_HSCROLL:
  206.          MainHorizScroll(hwnd, mp2);
  207.          break;
  208.  
  209.       case WM_VSCROLL:
  210.          MainVertScroll(hwnd, mp2);
  211.          break;
  212.  
  213.       case WM_CHAR:
  214.          switch (SHORT2FROMMP(mp2))
  215.          {
  216.             case VK_LEFT:
  217.             case VK_RIGHT:
  218.                return MainCharHScroll (hwnd, msg, mp1, mp2) ;
  219.             case VK_UP:
  220.             case VK_DOWN:
  221.             case VK_PAGEUP:
  222.             case VK_PAGEDOWN:
  223.                return MainCharVScroll (hwnd, msg, mp1, mp2) ;
  224.          }
  225.          break ;
  226.  
  227.       case WM_COMMAND:
  228.          MainCommand(mp1, mp2);
  229.          break;
  230.  
  231.       case HM_QUERY_KEYS_HELP:
  232.          return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
  233.          break ;
  234.  
  235.       case WM_NEWQMSG:
  236.          /* copy message into circular array, with index being next free spot in array */
  237.          pqdataQ = (QDATA *)PVOIDFROMMP(mp1);
  238.          sprintf(SvrQMsgs[iIndex],
  239.                  "Process ID: %03lu, Priority: %02hu,  Message: %s",
  240.                  pqdataQ->pidProcess,
  241.                  pqdataQ->usPriority,
  242.                  pqdataQ->pszMsg);
  243.  
  244.          /* increment index, or reset to 0 as appropriate */
  245.          iIndex = (iIndex < MAX_MESSAGES-1) ? iIndex+1 : 0;
  246.  
  247.          /* increment total message count until buffer is filled */
  248.          sTotalMsgs = min(MAX_MESSAGES, sTotalMsgs+1);
  249.  
  250.          MainUpdateMsg(hwnd, SvrQMsgs, iIndex, sTotalMsgs);
  251.          break;
  252.  
  253.       case WM_MSG:
  254.          MessageBox(hwndMain, (ULONG)SHORT1FROMMP(mp1), MB_OK | MB_ICONEXCLAMATION | MB_APPLMODAL, TRUE);
  255.          break;
  256.  
  257.       case WM_CLOSE:
  258.          DosWriteQueue(hqQ, WM_CLOSE, 0, NULL, 15);
  259.  
  260.          /* process the WM_CLOSE */
  261.  
  262.    /*--------------------------------------------------*\
  263.     *      Any messages not processed are passed on
  264.     *      to WinDefWindowProc.
  265.    \*--------------------------------------------------*/
  266.  
  267.       default:
  268.          return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  269.          break;
  270.    }
  271.  
  272.    return (MRFROMLONG(0));      /* all window procedures should return 0 as a default */
  273.  
  274. }   /* MainWndProc() */
  275.  
  276.  
  277. /****************************************************************\
  278.  *  Message Box procedure
  279.  *--------------------------------------------------------------
  280.  *
  281.  *  Name:   MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
  282.  *
  283.  *  Purpose: Displays the message box with the message
  284.  *              given in idMsg retrieved from the message table
  285.  *              and using the style flags in fsStyle
  286.  *
  287.  *  Usage:  Called whenever a MessageBox is to be displayed
  288.  *
  289.  *  Method: - Message string is loaded from the process'
  290.  *              message table
  291.  *          - Alarm beep is sounded if desired
  292.  *          - Message box with the message is displayed
  293.  *          - WinMessageBox return value is returned
  294.  *
  295.  *  Returns: return value from WinMessageBox()
  296.  *
  297. \****************************************************************/
  298.  
  299. ULONG MessageBox(HWND hwndOwner,  /* handle of the message box's owner */
  300.                  ULONG idMsg,     /* id if the message in the message table */
  301.                  ULONG fsStyle,   /* style of the message box */
  302.                  BOOL fBeep)      /* if TRUE, beep before message box is displayed */
  303. {
  304.    CHAR szText[TITLE_LEN];
  305.  
  306.    if(!WinLoadMessage(hab,
  307.                      (HMODULE)NULL,
  308.                      idMsg,
  309.                      TITLE_LEN,
  310.                      (PSZ)szText))
  311.    {
  312.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  313.       return MBID_ERROR;
  314.    }
  315.  
  316.    if(fBeep)  {
  317.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  318.    }
  319.  
  320.    return(WinMessageBox(HWND_DESKTOP,
  321.                         hwndOwner,
  322.                         szText,
  323.                         (PSZ)NULL,
  324.                         MSGBOXID,
  325.                         fsStyle));
  326.  
  327. }   /* MessageBox() */
  328.  
  329.  
  330. /****************************************************************\
  331.  *  Main window WM_COMMAND processing procedure
  332.  *--------------------------------------------------------------
  333.  *
  334.  *  Name:   MainCommand(mp1, mp2)
  335.  *
  336.  *  Purpose: Calls the appropriate procedures that deal with
  337.  *              the selected menu item.
  338.  *
  339.  *  Usage:  Routine is called whenever a WM_COMMAND message
  340.  *          is posted to the main window.
  341.  *
  342.  *  Method: a switch statement branches on the id of the
  343.  *          menu item that posted the message and the
  344.  *          appropriate action for that item is taken.
  345.  *
  346.  *  Returns:
  347.  *
  348. \****************************************************************/
  349. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  350. {
  351.  
  352.    switch(SHORT1FROMMP(mp1))
  353.    {
  354.       case IDM_FILEEXIT:
  355.          FileExit();
  356.          break;
  357.  
  358.       case IDM_OPTIONPURGE:
  359.          iIndex = sTotalMsgs = 0;
  360.  
  361.          /* get memory thread, write priority 15 message to tell thread to purge Q */
  362.          DosWriteQueue(hqQ, IDM_OPTIONPURGE, 0, NULL, 15);
  363.          MainPurgeWindow();
  364.          break;
  365.  
  366.       case IDM_HELPHELPFORHELP:
  367.          HelpHelpForHelp(mp2);
  368.          break;
  369.  
  370.       case IDM_HELPEXTENDED:
  371.          HelpExtended(mp2);
  372.          break;
  373.  
  374.       case IDM_HELPKEYS:
  375.          HelpKeys(mp2);
  376.          break;
  377.  
  378.       case IDM_HELPINDEX:
  379.          HelpIndex(mp2);
  380.          break;
  381.  
  382.       case IDM_HELPTUTORIAL:
  383.          HelpTutorial(mp2);
  384.          break;
  385.  
  386.       case IDM_HELPABOUT:
  387.          HelpAbout(mp2);
  388.          break;
  389.  
  390.       default:
  391.          break;
  392.     }
  393.  
  394. }   /* MainCommand() */
  395.  
  396.  
  397. /****************************************************************\
  398.  *  Initialization routine
  399.  *--------------------------------------------------------------
  400.  *
  401.  *  Name:   Init()
  402.  *
  403.  *  Purpose: Performs initialization functions.
  404.  *
  405.  *  Usage:  Called once before the message queue is queried.
  406.  *
  407.  *  Method:
  408.  *          - starts processing thread
  409.  *          - registers all window classes
  410.  *
  411.  *  Returns:
  412.  *          TRUE - initialization is successful
  413.  *          FALSE - initialization failed
  414. \****************************************************************/
  415. BOOL Init(int argc, char *argv[])
  416. {
  417.    PTIB   ptibDummy;
  418.    PIB    *ppibProcess;
  419.    ULONG  flCtlData;    /* frame control data */
  420.    CHAR   pszBuff[CCHMAXPATH];
  421.    RESULTCODES  resc;
  422.  
  423.  
  424.    /* load application name from resource file */
  425.    if(!WinLoadString(hab, 0, IDS_APPNAME, MAXNAMEL, szAppName))
  426.       return FALSE;
  427.  
  428.    /* register the main client window class */
  429.    if(!WinRegisterClass(hab,
  430.                         (PSZ)szAppName,
  431.                         MainWndProc,
  432.                         CS_SIZEREDRAW | CS_CLIPCHILDREN,
  433.                         0))
  434.    {
  435.       return FALSE;
  436.    }
  437.  
  438.    flCtlData = FCF_STANDARD | FCF_VERTSCROLL | FCF_HORZSCROLL;
  439.  
  440.    if (NULLHANDLE == (hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
  441.                                                          WS_VISIBLE,
  442.                                                          (PULONG)&flCtlData,
  443.                                                          (PSZ)szAppName,
  444.                                                          (PSZ)NULL,
  445.                                                          WS_VISIBLE,
  446.                                                          (HMODULE)NULL,
  447.                                                          IDR_MAIN,
  448.                                                          &hwndMain)))
  449.       return FALSE;
  450.  
  451.    WinSetWindowText(hwndMainFrame, szAppName);
  452.  
  453.    if (DosGetInfoBlocks(&ptibDummy, &ppibProcess) ||
  454.        !(hqQ = InitQ((*ppibProcess).pib_ulpid,  /* proccess that will send WM_CLOSE command */
  455.                      hwndMainFrame)))           /* window to post to */
  456.       return FALSE;
  457.  
  458.    if (argc > 1)
  459.    {
  460.       if ((BOOL)DosExecPgm(pszBuff, CCHMAXPATH, EXEC_ASYNC, NULL, NULL, &resc, argv[1]))
  461.       {
  462.          MessageBox(hwndMain, IDMSG_CANNOT_EXEC_CLIENT, MB_CUAWARNING | MB_OK, TRUE);
  463.       }
  464.    }
  465.  
  466.    InitHelp();
  467.  
  468.    return TRUE;
  469. }  /* Init() */
  470.  
  471.  
  472. /****************************************************************\
  473.  *  Exit routine
  474.  *--------------------------------------------------------------
  475.  *
  476.  *  Name:   FileExit(mp2)
  477.  *
  478.  *  Purpose: Processes the File menu's Exit item.
  479.  *
  480.  *  Usage:  called whenever Exit from the file menu is
  481.  *          selected
  482.  *
  483.  *  Method:  Routine posts a WM_CLOSE message to the main
  484.  *           application window.
  485.  *
  486.  *  Returns:
  487.  *
  488. \****************************************************************/
  489. VOID FileExit()
  490. {
  491.  
  492.    WinPostMsg(hwndMain, WM_CLOSE, (MPARAM)NULL, (MPARAM)NULL);
  493.  
  494. }   /* FileExit() */
  495.  
  496.  
  497.