cms.c


/*--------------------------------------------------------
   CMS.C -- SPII MIC2 Calibrator Control Program
                L. R. Erickson, Apr. 1994
  --------------------------------------------------------*/

#include <windows.h>
#include <stdlib.h>
#include "cms.h"



  
       // File Globals
static  HANDLE statglobhCMSPrevInst ;   // Need for prev instance check
                                 // with message box.
static char   statglobszSetupClass  [] = "CMSSetupChild" ;
static char   statglobszDataAcqClass [] = "CMSDataAcqChild" ;
static char   statglobszOpsClass  [] = "CMSOpsChild" ;

//*********** Start of WinMain **************************************

int PASCAL WinMain (HANDLE hInstance, HANDLE hPrevInstance,
                    LPSTR lpszCmdLine, int nCmdShow)
     {


     static char   szFrameClass [] = "MdiFrame" ;

     HANDLE   hAccel ;
     HWND     hwndFrame, hwndClient ;
     MSG      msg ;
     WNDCLASS wndclass ;


     globhCMSInst = hInstance ;
     statglobhCMSPrevInst = hPrevInstance ;


     if (!hPrevInstance)   
     {
                    // Register the frame window class

     wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
     wndclass.lpfnWndProc   = FrameWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = 0 ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = COLOR_APPWORKSPACE + 1 ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = szFrameClass ;

     RegisterClass (&wndclass) ;

                    // Register the Setup child window class

     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
     wndclass.lpfnWndProc   = CMSSetupWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof (LOCALHANDLE) ; 
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = statglobszSetupClass ;

     RegisterClass (&wndclass) ;
                    // Register the Data Acquisiton child window class

     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
     wndclass.lpfnWndProc   = CMSDataAcqWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof (LOCALHANDLE) ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = statglobszDataAcqClass ;

     RegisterClass (&wndclass) ;

                    // Register the Operation child window class

     wndclass.style         = CS_HREDRAW | CS_VREDRAW | CS_NOCLOSE ;
     wndclass.lpfnWndProc   = CMSOpsWndProc ;
     wndclass.cbClsExtra    = 0 ;
     wndclass.cbWndExtra    = sizeof (LOCALHANDLE) ;
     wndclass.hInstance     = hInstance ;
     wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
     wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
     wndclass.hbrBackground = GetStockObject (WHITE_BRUSH) ;
     wndclass.lpszMenuName  = NULL ;
     wndclass.lpszClassName = statglobszOpsClass ;

     RegisterClass (&wndclass) ;

     }  // Endif !hPrevInstance

               // Load accelerator table

     hAccel = LoadAccelerators (globhCMSInst, "CMSAccel") ;

               // Create the frame window

     hwndFrame = CreateWindow (szFrameClass, "CMS Cal Control",
                               WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
                               CW_USEDEFAULT, CW_USEDEFAULT,
                               CW_USEDEFAULT, CW_USEDEFAULT,
                               NULL, NULL, hInstance, NULL) ;

     hwndClient = GetWindow (hwndFrame, GW_CHILD) ;

     ShowWindow (hwndFrame, SW_SHOWMAXIMIZED) ;
     UpdateWindow (hwndFrame) ;

               // Enter the MDI modified message loop

     while (GetMessage (&msg, NULL, 0, 0))
          {
          if (!TranslateMDISysAccel (hwndClient, &msg) &&
              !TranslateAccelerator (hwndFrame, hAccel, &msg))
               {
               TranslateMessage (&msg) ;
               DispatchMessage (&msg) ;
               }
          }

     return msg.wParam ;

     }

//*********** End of WinMain **************************************

//*********** Start of FrameWndProc **********************************

long FAR PASCAL _export FrameWndProc (HWND hwnd, UINT message, UINT wParam,
                                                               LONG lParam)
     {


     static HWND        hwndClient ;
     static cxChild ;
     static cyChild ;

     CLIENTCREATESTRUCT clientcreate ;
     FARPROC            lpfnEnum ;
     HWND               hwndChild ;
     MDICREATESTRUCT    mdicreate ;
     RECT               rect ;
     HDC                hdc ;
     TEXTMETRIC         tm ;

     switch (message)
          {
          case WM_CREATE:          // Create the client window

                 // Have to wait till here to bail out if other
                 // copy 'cause need an hwnd to pop a message box.
              if (statglobhCMSPrevInst)   
                {
                MessageBox (hwnd,
                   "Only one CMS program can run at a time!",
                   "CMS Calibrator Control",
                    MB_ICONEXCLAMATION | MB_OK) ;
                PostQuitMessage (0) ;
                return 0 ;
                }    

               clientcreate.hWindowMenu  = NULL ;
               clientcreate.idFirstChild = IDM_FIRSTCHILD ;

               hwndClient = CreateWindow ("MDICLIENT", NULL,
                              WS_CHILD | WS_CLIPCHILDREN | WS_VISIBLE,
                              0, 0, 0, 0, hwnd, 1, globhCMSInst,
                              (LPSTR) &clientcreate) ;

                        
                      // Get system fixed font size
              hdc = GetDC (hwnd) ;
              SelectObject (hdc, GetStockObject (SYSTEM_FIXED_FONT)) ;
              GetTextMetrics (hdc, &tm) ;
              globcxCharSpace = tm.tmAveCharWidth ;
              globcyCharSpace = tm.tmHeight + tm.tmExternalLeading ;
              ReleaseDC (hwnd, hdc) ;

                       //Set up time
              SetInternational () ;
                      //Start a timer
              SetTimer (hwnd, ID_CLOCKTIMER, 1000, NULL) ;

                 // Start Setup window
              PostMessage (hwnd, WM_COMMAND, ID_MAKESETUPWIN, 0L) ; 
               
                 // Find out how many data points.
              SetNumDataPts () ;

              return 0 ;

          case WM_COMMAND:
               switch (wParam)
                    {
                    case ID_MAKESETUPWIN:       // Create a Data Display 
                                                 // child window
                        // Get startup client size
                     GetClientRect (hwndClient, &rect) ;
                     cxChild = rect.right - (NUM_WINDOWS * 
                                    (3 * globcxCharSpace)) ;
                     cyChild = rect.bottom - (NUM_WINDOWS * 
                          (( 3 * globcyCharSpace) / 2)) ;

                     mdicreate.szClass = statglobszSetupClass ;
                     mdicreate.szTitle = "CMS Startup" ;
                     mdicreate.hOwner  = globhCMSInst ;
                     mdicreate.x       = (SETUP_WIN_NUM -1) * 
                             ( 2 * globcxCharSpace) ;
                     mdicreate.y       = (SETUP_WIN_NUM -1) * globcyCharSpace ;
                     mdicreate.cx      = cxChild ;
                     mdicreate.cy      = cyChild ;
                     mdicreate.style   = 0 ;
                     mdicreate.lParam  = NULL ;

                     hwndChild = (HWND) SendMessage (hwndClient,
                                    WM_MDICREATE, 0,
                                    (long) (LPMDICREATESTRUCT) &mdicreate) ;
                     return 0 ;

                    case ID_MAKEDATAWIN:       // Create a Data Display 
                                                 // child window

                     mdicreate.szClass = statglobszDataAcqClass ;
                     mdicreate.szTitle = "Data Display" ;
                     mdicreate.hOwner  = globhCMSInst ;
                     mdicreate.x     = (DATAACQ_WIN_NUM -1) * 
                                         (2 * globcxCharSpace) ;
                     mdicreate.y     = (DATAACQ_WIN_NUM -1) * globcyCharSpace ;
                     mdicreate.cx      = cxChild ;
                     mdicreate.cy      = cyChild ;
                     mdicreate.style   = 0 ;
                     mdicreate.lParam  = NULL ;

                     hwndChild = (HWND) SendMessage (hwndClient,
                                    WM_MDICREATE, 0,
                                    (long) (LPMDICREATESTRUCT) &mdicreate) ;
                     return 0 ;

                    case ID_MAKEOPSWIN:        // Create a Operation 
                                                // child window

                     mdicreate.szClass = statglobszOpsClass ;
                     mdicreate.szTitle = "Operation" ;
                     mdicreate.hOwner  = globhCMSInst ;
                     mdicreate.x       = (OPS_WIN_NUM - 1) * 
                                            (2 * globcxCharSpace) ;
                     mdicreate.y       = (OPS_WIN_NUM - 1) * globcyCharSpace ;
                     mdicreate.cx      = cxChild ;
                     mdicreate.cy      = cyChild ;
                     mdicreate.style   = 0 ;
                     mdicreate.lParam  = NULL ;

                     hwndChild = (HWND) SendMessage (hwndClient,
                                    WM_MDICREATE, 0,
                                    (long) (LPMDICREATESTRUCT) &mdicreate) ;
                     return 0 ;

                                   // Messages for arranging windows
                    case IDM_TILE:
                         SendMessage (hwndClient, WM_MDITILE, 
                            MDITILE_HORIZONTAL, 0L) ;
                         return 0 ;

                    case IDM_CASCADE:
                         SendMessage (hwndClient, WM_MDICASCADE, 0, 0L) ;
                         return 0 ;

                    case IDM_ARRANGE:
                         SendMessage (hwndClient, WM_MDIICONARRANGE, 0, 0L) ;
                         return 0 ;

                    default:            // Pass to active child

                         hwndChild = LOWORD (SendMessage (hwndClient,
                                                WM_MDIGETACTIVE, 0, 0L)) ;

                        if (IsWindow (hwndChild))
                             SendMessage (hwndChild, WM_COMMAND,
                                          wParam, lParam) ;

                        break ;        // and then to DefFrameProc
                    }
               break ;

          case WM_QUERYENDSESSION:
          case WM_CLOSE:                   
                  // See if really want to quit. 
               if (IDOK != MessageBox (hwnd, 
                                       "Really want to quit CMS program?",
                                       "CMS Calibrator Control", 
                                       MB_ICONQUESTION | MB_OKCANCEL
                                       | MB_DEFBUTTON2))
                    return 0 ;

                   // Give children a chance to clean up after themselves.
               lpfnEnum = MakeProcInstance ((FARPROC) CloseEnumProc,
                                                      globhCMSInst) ;
               EnumChildWindows (hwndClient, lpfnEnum, 0L) ;
               FreeProcInstance (lpfnEnum) ;

               if (NULL != GetWindow (hwndClient, GW_CHILD))
                    return 0 ;
                
               break ;   // ie, call DefFrameProc ;

          case WM_DESTROY :
               KillTimer (hwnd, ID_CLOCKTIMER) ;
               PostQuitMessage (0) ;
               return 0 ;

          case WM_TIMER :
               UpdateCMSTime (hwnd) ;
               return 0 ;                 

          }
               // Pass unprocessed messages to DefFrameProc (not DefWindowProc)

     return DefFrameProc (hwnd, hwndClient, message, wParam, lParam) ;
     }

//*********** End of FrameWndProc **********************************

//*********** Start of CloseEnumProc *************************************

BOOL FAR PASCAL _export CloseEnumProc (HWND hwnd, LONG lParam)
     {
     if (GetWindow (hwnd, GW_OWNER))         // check for icon title
          return 1 ;

     SendMessage (GetParent (hwnd), WM_MDIRESTORE, hwnd, 0L) ;

     if (!SendMessage (hwnd, WM_QUERYENDSESSION, 0, 0L))
          return 1 ;

     SendMessage (GetParent (hwnd), WM_MDIDESTROY, hwnd, 0L) ;
          return 1 ;
     }

//*********** End of CloseEnumProc *************************************