home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tolkit45.zip / os2tk45 / samples / os2 / npipe / svrpmain.c < prev    next >
C/C++ Source or Header  |  1999-05-11  |  15KB  |  491 lines

  1. /*==============================================================*\
  2.  *  svrpmain.c - Pipe Server end of Tic-Tac-Toe 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_DOSNMPIPES
  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 "svrpmain.h"
  41. #include "svrphelp.h"
  42. #include "svrpxtrn.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. BOOL   fHelpEnabled;                  /* flag to determine if help is enabled */
  56. SHORT  sTotalMsgs;
  57. CHAR   PipeMsgs[MAX_MESSAGES][MESSAGE_LEN];
  58. HPIPE  hpStartPipe;
  59. BOOL   fAppExit=FALSE;                /* application ending flag */
  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.  
  94.    hab = WinInitialize(0);
  95.  
  96.    if(!hab)
  97.    {
  98.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  99.       return(RETURN_ERROR);
  100.    }
  101.  
  102.    hmq = WinCreateMsgQueue(hab, 0);
  103.  
  104.    if(!hmq)
  105.    {
  106.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  107.       WinTerminate(hab);
  108.       return(RETURN_ERROR);
  109.    }
  110.  
  111.    if(!Init(argc, argv))
  112.    {
  113.       if(hwndMainFrame == NULLHANDLE)
  114.       {
  115.           MessageBox(HWND_DESKTOP,
  116.                      IDMSG_MAINWINCREATEFAILED,
  117.                      MB_OK | MB_ERROR,
  118.                      TRUE);
  119.       }
  120.  
  121.       else
  122.       {
  123.          MessageBox(HWND_DESKTOP,
  124.                     IDMSG_INITFAILED,
  125.                     MB_OK | MB_ERROR,
  126.                     TRUE);
  127.  
  128.       }
  129.       DosBeep(BEEP_WARN_FREQ, BEEP_WARN_DUR);
  130.       if (WinIsWindow(hab, hwndMainFrame))
  131.       {
  132.          WinDestroyWindow(hwndMainFrame);
  133.       }
  134.       WinDestroyMsgQueue(hmq);
  135.       WinTerminate(hab);
  136.       return(RETURN_ERROR);
  137.    }
  138.  
  139.    /* Get-Dispatch Message loop */
  140.    while(WinGetMsg(hab, (PQMSG)&qmsg, NULLHANDLE, 0, 0))
  141.       WinDispatchMsg(hab, (PQMSG)&qmsg);
  142.  
  143.    /* destroy the help instance */
  144.    DestroyHelpInstance();
  145.  
  146.    if (WinIsWindow(hab, hwndMainFrame))
  147.    {
  148.       WinDestroyWindow(hwndMainFrame);
  149.    }
  150.    WinDestroyMsgQueue(hmq);
  151.    WinTerminate(hab);
  152.    return(RETURN_SUCCESS);
  153.  
  154. }   /* main() */
  155.  
  156.  
  157. /****************************************************************\
  158.  *  Main client window procedure
  159.  *--------------------------------------------------------------
  160.  *
  161.  *  Name:   MainWndProc(hwnd, msg, mp1, mp2)
  162.  *
  163.  *  Purpose: Processes the messages sent to the main client
  164.  *              window.  This routine processes the basic
  165.  *              messages all client windows should process
  166.  *              and passes all others onto WinDefWindowProc.
  167.  *
  168.  *  Usage:  Called for each message placed in the main
  169.  *          window's message queue
  170.  *
  171.  *  Method: a switch statement branches to the routines to be
  172.  *          performed for each message processed.
  173.  *
  174.  *  Returns:  Return values are determined by each message
  175.  *
  176. \****************************************************************/
  177. MRESULT EXPENTRY MainWndProc(HWND   hwnd,      /* handle of window */
  178.                              ULONG  msg,     /* id of message */
  179.                              MPARAM mp1,     /* first message parameter */
  180.                              MPARAM mp2)     /* second message parameter */
  181. {
  182.    static int  iCloseCount=0;
  183.  
  184.    switch(msg)
  185.    {
  186.       case WM_CREATE:
  187.          return(InitMainWindow(hwnd, mp1, mp2));
  188.          break;
  189.  
  190.       case WM_PAINT:
  191.          MainPaint(hwnd, PipeMsgs, iIndex, sTotalMsgs);
  192.          break;
  193.  
  194.       case WM_SIZE:
  195.          MainSize(hwnd, sTotalMsgs, mp1, mp2);
  196.          break;
  197.  
  198.       case WM_HSCROLL:
  199.          MainHorizScroll(hwnd, mp2);
  200.          break;
  201.  
  202.       case WM_VSCROLL:
  203.          MainVertScroll(hwnd, mp2);
  204.          break;
  205.  
  206.       case WM_CHAR:
  207.          switch (SHORT2FROMMP(mp2))
  208.          {
  209.             case VK_LEFT:
  210.             case VK_RIGHT:
  211.                return MainCharHScroll (hwnd, msg, mp1, mp2) ;
  212.             case VK_UP:
  213.             case VK_DOWN:
  214.             case VK_PAGEUP:
  215.             case VK_PAGEDOWN:
  216.                return MainCharVScroll (hwnd, msg, mp1, mp2) ;
  217.          }
  218.          break ;
  219.  
  220.       case WM_COMMAND:
  221.          MainCommand(mp1, mp2);
  222.          break;
  223.  
  224.       case HM_QUERY_KEYS_HELP:
  225.          return (MRESULT)PANEL_HELPKEYS;   /* return id of key help panel */
  226.          break ;
  227.  
  228.       case WM_PIPEMSG:
  229.          /* copy message into circular array, with index being next free spot in array */
  230.          strcpy(PipeMsgs[iIndex], (CHAR *)PVOIDFROMMP(mp1));
  231.  
  232.          /* increment index, or reset to 0 as appropriate */
  233.          iIndex = (iIndex < MAX_MESSAGES-1) ? iIndex+1 : 0;
  234.  
  235.          /* increment total message count until buffer is filled */
  236.          sTotalMsgs = min(MAX_MESSAGES, sTotalMsgs+1);
  237.  
  238.          MainUpdateMsg(hwnd, PipeMsgs, iIndex, sTotalMsgs);
  239.          break;
  240.  
  241.       case WM_MSG:
  242.          /* display messages from threads w/o a message queue */
  243.          MessageBox(hwndMain, SHORT1FROMMP(mp1), MB_OK | MB_ICONEXCLAMATION, TRUE);
  244.          break;
  245.  
  246.       case WM_CLOSE:
  247.          fAppExit = TRUE;
  248.          DosClose(hpStartPipe);  /* break start pipe */
  249.  
  250.          /* give other thread a chance to exit */
  251.          /* but don't wait around forever for it */
  252.  
  253.          for (iCloseCount = 0; FALSE == fThrdsDead && iCloseCount < THRD_EXIT_TIMEOUT; iCloseCount++)
  254.          {
  255.             DosSleep(0);
  256.          }
  257.  
  258.          /* else process the WM_CLOSE */
  259.  
  260.    /*--------------------------------------------------*\
  261.     *      Any messages not processed are passed on
  262.     *      to WinDefWindowProc.
  263.    \*--------------------------------------------------*/
  264.  
  265.       default:
  266.          return(WinDefWindowProc(hwnd, msg, mp1, mp2));
  267.          break;
  268.    }
  269.  
  270.    return (MRFROMLONG(0));      /* all window procedures should return 0 as a default */
  271.  
  272. }   /* MainWndProc() */
  273.  
  274.  
  275. /****************************************************************\
  276.  *  Message Box procedure
  277.  *--------------------------------------------------------------
  278.  *
  279.  *  Name:   MessageBox(hwndOwner, nIdMsg, fsStyle, fBeep)
  280.  *
  281.  *  Purpose: Displays the message box with the message
  282.  *              given in idMsg retrieved from the message table
  283.  *              and using the style flags in fsStyle
  284.  *
  285.  *  Usage:  Called whenever a MessageBox is to be displayed
  286.  *
  287.  *  Method: - Message string is loaded from the process'
  288.  *              message table
  289.  *          - Alarm beep is sounded if desired
  290.  *          - Message box with the message is displayed
  291.  *          - WinMessageBox return value is returned
  292.  *
  293.  *  Returns: return value from WinMessageBox()
  294.  *
  295. \****************************************************************/
  296.  
  297. ULONG MessageBox(HWND  hwndOwner,  /* handle of the message box's owner */
  298.                  ULONG idMsg,      /* id if the message in the message table */
  299.                  ULONG fsStyle,    /* style of the message box */
  300.                  BOOL  fBeep)      /* if TRUE, beep before message box is displayed */
  301. {
  302.    CHAR szText[TITLE_LEN];
  303.  
  304.    if(!WinLoadMessage(hab,
  305.                      (HMODULE)NULL,
  306.                      idMsg,
  307.                      TITLE_LEN,
  308.                      (PSZ)szText))
  309.    {
  310.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  311.       return MBID_ERROR;
  312.    }
  313.  
  314.    if(fBeep)  {
  315.       WinAlarm(HWND_DESKTOP, WA_ERROR);
  316.    }
  317.  
  318.    return(WinMessageBox(HWND_DESKTOP,
  319.                         hwndOwner,
  320.                         szText,
  321.                         "Server Error",
  322.                         MSGBOXID,
  323.                         fsStyle));
  324.  
  325. }   /* MessageBox() */
  326.  
  327.  
  328. /****************************************************************\
  329.  *  Main window WM_COMMAND processing procedure
  330.  *--------------------------------------------------------------
  331.  *
  332.  *  Name:   MainCommand(mp1, mp2)
  333.  *
  334.  *  Purpose: Calls the appropriate procedures that deal with
  335.  *              the selected menu item.
  336.  *
  337.  *  Usage:  Routine is called whenever a WM_COMMAND message
  338.  *          is posted to the main window.
  339.  *
  340.  *  Method: a switch statement branches on the id of the
  341.  *          menu item that posted the message and the
  342.  *          appropriate action for that item is taken.
  343.  *
  344.  *  Returns:
  345.  *
  346. \****************************************************************/
  347. VOID MainCommand(MPARAM mp1, MPARAM mp2)
  348. {
  349.  
  350.    switch(SHORT1FROMMP(mp1))
  351.    {
  352.       case IDM_FILEEXIT:
  353.          FileExit();
  354.          break;
  355.  
  356.       case IDM_OPTIONCLEAR:
  357.          iIndex = sTotalMsgs = 0;
  358.          MainPurgeWindow();
  359.          break;
  360.  
  361.       case IDM_HELPHELPFORHELP:
  362.          HelpHelpForHelp(mp2);
  363.          break;
  364.  
  365.       case IDM_HELPEXTENDED:
  366.          HelpExtended(mp2);
  367.          break;
  368.  
  369.       case IDM_HELPKEYS:
  370.          HelpKeys(mp2);
  371.          break;
  372.  
  373.       case IDM_HELPINDEX:
  374.          HelpIndex(mp2);
  375.          break;
  376.  
  377.       case IDM_HELPTUTORIAL:
  378.          HelpTutorial(mp2);
  379.          break;
  380.  
  381.       case IDM_HELPABOUT:
  382.          HelpAbout(mp2);
  383.          break;
  384.  
  385.       default:
  386.          break;
  387.     }
  388.  
  389. }   /* MainCommand() */
  390.  
  391.  
  392. /****************************************************************\
  393.  *  Initialization routine
  394.  *--------------------------------------------------------------
  395.  *
  396.  *  Name:   Init()
  397.  *
  398.  *  Purpose: Performs initialization functions.
  399.  *
  400.  *  Usage:  Called once before the message queue is queried.
  401.  *
  402.  *  Method:
  403.  *          - starts processing thread
  404.  *          - registers all window classes
  405.  *
  406.  *  Returns:
  407.  *          TRUE - initialization is successful
  408.  *          FALSE - initialization failed
  409. \****************************************************************/
  410. BOOL Init(int argc, char *argv[])
  411. {
  412.    ULONG  flCtlData;    /* frame control data */
  413.    CHAR   pszBuff[CCHMAXPATH];
  414.    RESULTCODES  resc;
  415.  
  416.  
  417.  
  418.    /* load application name from resource file */
  419.    if(!WinLoadString(hab, 0, IDS_APPNAME, MAXNAMEL, szAppName))
  420.    {
  421.       return FALSE;
  422.    }
  423.  
  424.    /* register the main client window class */
  425.    if(!WinRegisterClass(hab,
  426.                         (PSZ)szAppName,
  427.                         MainWndProc,
  428.                         CS_SIZEREDRAW | CS_CLIPCHILDREN,
  429.                         0))
  430.    {
  431.       return FALSE;
  432.    }
  433.  
  434.    flCtlData = FCF_STANDARD | FCF_VERTSCROLL | FCF_HORZSCROLL;
  435.  
  436.    if (NULLHANDLE == (hwndMainFrame = WinCreateStdWindow(HWND_DESKTOP,
  437.                                                          WS_VISIBLE,
  438.                                                          (PULONG)&flCtlData,
  439.                                                          (PSZ)szAppName,
  440.                                                          NULL,
  441.                                                          WS_VISIBLE,
  442.                                                          (HMODULE)NULL,
  443.                                                          IDR_MAIN,
  444.                                                          (PHWND)&hwndMain)))
  445.       return FALSE;
  446.  
  447.    WinSetWindowText(hwndMainFrame, szAppName);
  448.  
  449.    if (!(BOOL)(hpStartPipe=InitThreads()))
  450.    {
  451.       return FALSE;
  452.    }
  453.  
  454.    if (argc > 1)
  455.    {
  456.       if ((BOOL)DosExecPgm(pszBuff, CCHMAXPATH, EXEC_ASYNC, NULL, NULL, &resc, argv[1]))
  457.       {
  458.          MessageBox(hwndMain, IDMSG_CANNOT_EXEC_CLIENT, MB_CUAWARNING | MB_OK, TRUE);
  459.       }
  460.    }
  461.  
  462.    InitHelp();
  463.  
  464.    return TRUE;
  465. }  /* Init() */
  466.  
  467.  
  468. /****************************************************************\
  469.  *  Exit routine
  470.  *--------------------------------------------------------------
  471.  *
  472.  *  Name:   FileExit(mp2)
  473.  *
  474.  *  Purpose: Processes the File menu's Exit item.
  475.  *
  476.  *  Usage:  called whenever Exit from the file menu is
  477.  *          selected
  478.  *
  479.  *  Method:  Routine posts a WM_CLOSE message to the main
  480.  *           application window.
  481.  *
  482.  *  Returns:
  483.  *
  484. \****************************************************************/
  485. VOID FileExit()
  486. {
  487.  
  488.    WinPostMsg(hwndMain, WM_CLOSE, (MPARAM)NULL, (MPARAM)NULL);
  489.  
  490. }   /* FileExit() */
  491.