/***************************************************************************
**    $Log:   S:/tbird/mt2_68k/trace/evtwdlg.c_v  $
** 
**    Rev 1.2   09 Mar 1998 15:51:38   Winky
** Modify EvtWndProc function.
** 
**    Rev 1.1   31 Mar 1997 09:29:46   gene
** 
**    Rev 1.0   04 Mar 1997 13:05:16   gene
** Initial revision.
** 
**    Rev 1.11   09 Jan 1997 09:51:56   cjchen
** 
**    Rev 1.10   19 Dec 1996 09:04:40   cjchen
** 
**    Rev 1.9   12 Dec 1996 11:25:10   cjchen
** 
**    Rev 1.8   22 Nov 1996 14:31:18   cjchen
** 
**    Rev 1.7   07 Nov 1996 18:21:36   cjchen
** 
**    Rev 1.6   29 Oct 1996 16:20:10   cjchen
** 
**    Rev 1.6   29 Oct 1996 16:19:24   cjchen
** No change.
** 
**    Rev 1.5   22 Oct 1996 18:17:32   cjchen
** 
**    Rev 1.4   18 Oct 1996 17:46:58   cjchen
** 
**    Rev 1.4   18 Oct 1996 17:46:30   cjchen
** 
**    Rev 1.3   17 Oct 1996 09:13:30   cjchen
** No change.
** 
**    Rev 1.2   11 Oct 1996 17:22:30   cjchen
** 
**    Rev 1.1   09 Oct 1996 14:43:12   cjchen
** 
**    Rev 1.0   06 Sep 1996 16:49:30   cjchen
** Initial revision.
** 
**    Rev 1.1   12 Aug 1996 08:48:22   cjchen
** 
**    Rev 1.0   24 Jul 1996 10:30:00   cjchen
** Initial revision.
 * 
 *    Rev 1.6   17 Jun 1996 15:19:00   cjchen
 * tune the tab space
**
**    Rev 1.5   14 Jun 1996 08:13:26   cjchen
** No change.
**
**    Rev 1.4   07 Jun 1996 16:29:38   cjchen
** almost complete version
**
**    Rev 1.3   24 May 1996 16:37:38   cjchen
** thie version is the same as version 1.2
**
**  $Header:   S:/tbird/mt2_68k/trace/evtwdlg.c_v   1.2   09 Mar 1998 15:51:38   Winky  $
*****************************************************************************/

                /************************
                *                       *
                *     INCLUDE FILE      *
                *                       *
                *************************/
#include <windows.h>
#include <commdlg.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#ifndef _BASEWIND_
#include "basewind.h"
#endif

#ifndef _EVTTMPLT_
#include "evttmplt.h"
#endif

#ifndef _EVENT_
#include "event.h"
#endif

#ifndef _TRIG_
#include "trig.h"
#endif

#ifndef _CLIULIB_
#include "cliulib.h"
#endif

#ifndef _EVTWDLG_
#include "evtwdlg.h"
#endif

#ifndef _HLPENTRY_
#include "hlpentry.h"
#endif

                /***********************
                *                      *
                *     DEFINITIONS      *
                *                      *
                ************************/
#define MAXLEN          100
#define BSWIDTH          20
#define BSHEIGHT         20
#define WINDOWWIDTH     520
#define WINDOWHEIGHT    280
#define INTERLEN         30
#define GROUPWIDTH       60

typedef struct {
   char szEachGroupName[10];
   int  nUmBs;
   int  nHeavyBs;
} GROUPATTRI;

               /************************
               *                       *
               *     EXTERNAL          *
               *                       *
               *************************/
extern HINSTANCE hLib;

               /************************
               *                       *
               *       LOCAL           *
               *                       *
               *************************/
CHAR szString[128];
CHAR szAppName[20];
HINSTANCE hInst;
HWND hWndEvt=NULL;
PRIVATE BOOLEAN registeredClasses = FALSE;
HWND hWndActiveEvent, hWndAddr, hWndData, hWndAddrMask,
         hWndDataMask, hWndEndAddr;
HWND hWndTextActiveEvent, hWndTextAddr, hWndTextData,
         hWndTextAddrMask, hWndTextDataMask, hWndTextStart;
HWND hBsEndAddr, hBsLength;
HWND hWndAddEvt,hWndDelEvt,hWndDelAll,hWndClrAll;
HWND hWndEachGroup[nUmGroup], hWndEachButton[nUmGroup][nUmBsEachGroup];
HWND hWndTrcEachButton[nUmGroup][3];
HANDLE nData, nAddrMask, nDataMask;
HANDLE nEndAddr;
FARPROC lpfnOldEdit, lpAddEventProc, lpfnOldButton,lpfnOldCombox ;
DESCRIPTOR theTemplate, theEvent;
U16 nUmEntry,mAxwidth,mAxheight;
char lpszSingleLine[80];
GROUPATTRI bsEachGroup[nUmGroup];
GROUPATTRI bsTrcEachGroup[nUmGroup];
int nDyBsGroup,nMaxRow,nLeft,nTop,nTrcMaxRow,nTrcBsGroup;
int cxAveChar,nComboList=0;
int statusY,widthX,heightY,cyHeight;

               /************************
               *                       *
               *       PROTOTYPE       *
               *                       *
               *************************/
void ClearEventPanel(void);
void SetupEvent(void);
void DisablePanel(void);
void DeleteBlank(LPSTR, LPSTR);
void StatusWindow(HWND hWnd);
void SetupListBox(HWND);
long FAR PASCAL WndProc (HWND, UINT, UINT, LONG);
long FAR PASCAL ButtonProc(HWND, UINT, UINT, LONG);
long FAR PASCAL EditProc(HWND, UINT, UINT, LONG);
long FAR PASCAL ComboxProc(HWND, UINT, UINT, LONG);
BOOL FAR PASCAL AddDlgProc (HWND ,UINT ,UINT ,LONG);
RETCODE CreateEvtPresenter(HINSTANCE, S16, S16, S16, S16, HWND);
RETCODE CheckIsEventInUseForTrig(char *);
RETCODE ErrMessageBoxOkL(char *);
RETCODE ErrMessageBoxYesNoL(char *,int *);
RETCODE ClearTriggerForSomeEvent(char *);
int nCwRegisterClasses(void);
static HANDLE hInst;
VOID ToolBarMsg(WPARAM WParam, LPARAM LParam);
EVENT_COND ConvEvtCond(int);
ACTION_MASK ConvEvtAction(int);
/*****************************************************************************
**
**  CreateEvtPresenter
**
*****************************************************************************/
RETCODE CreateEvtPresenter(HINSTANCE hInstance, S16 left, S16 top,
                              S16 width, S16 height,HWND hWnd) {
   MSG msg;  // MSG structure to store your messages        
   int nRc;  // return value from Register Classes          

   nLeft=left;
   nTop=top;
   strcpy(szAppName, "Event");
   hInst = hInstance;
   if(!registeredClasses) {
      /* register window classes if first instance of application */
      if ((nRc = nCwRegisterClasses()) == -1) {
         // TODO: Replace this with ErrDisplayFormattedError
         strcpy(szString, "Error registering virtual listbox or peripheral");
         MessageBox(NULL, szString, NULL, MB_ICONEXCLAMATION);
         return nRc;
      }
      registeredClasses = TRUE;
   }

   //initialize theEvent
   EvtOpenEvent(&theEvent,"default");

   if (!left && !width) {
      left = CW_USEDEFAULT;
      width = CW_USEDEFAULT;
   }
   
   /* create peripheral window */
   /*
   lpfnEventDlgProc=MakeProcInstance(EventDlgProc,hInst);
   hWndEvt=DialogBox(hInst,"EVENTDLG",hWnd,lpfnEventDlgProc);
   */
   hWndEvt = CreateWindow((LPSTR) szAppName,
                           (LPSTR)"Event",
                           (WS_CAPTION     |
                           WS_SYSMENU      |
                           WS_MINIMIZEBOX  |
                           WS_CLIPCHILDREN |
                           WS_OVERLAPPED),
                           left, top,         
                           500, 400,
                           NULL,
                           NULL,
                           hInst,
                           NULL);
   ShowWindow (hWndEvt,SW_SHOWNORMAL);
   UpdateWindow (hWndEvt);
   if (nComboList==0)
      SendMessage(hWndEvt,WM_COMMAND,IDM_EVTEADD,0);
   /*
   while (GetMessage(&msg, NULL, 0, 0)) {
      TranslateMessage(&msg);
      DispatchMessage(&msg);
    }
   return msg.wParam;
   */
   return GOOD;
}
/*****************************************************************************
**
**  ToolBarMsg
**
*****************************************************************************/
VOID ToolBarMsg(WPARAM WParam, LPARAM LParam)   {
   HWND hToolBar;
   // Pass a message off to the toolbar window
   if (GetToolBarHandle((HANDLE FAR *) &hToolBar) == GOOD) {
      SendMessage(hToolBar, WM_COMMAND, WParam, LParam);
   }
}
/*****************************************************************
*                                                                *
*         EvtWndProc                                             *
*                                                                *
******************************************************************/
long FAR PASCAL EvtWndProc (HWND hWnd, UINT message,
         UINT wParam, LONG lParam) {
HDC            hDc; 
PAINTSTRUCT    ps;
TEXTMETRIC     tm;
HMENU          hMenu;

static short   cyInterLine;
static short   cxChar, cyChar, cxLength;
static short   cx05Length, cx15Length, cx10Length,
               cx20Length, cx30Length;
//for interprocess use
char           tt[3],ss[5];
int            nIndex, i, j, nSel,ii,result;
U16            num,buttonID;
char           szString01[]="0   1   X";
char           szString02[]="start";
char           tmp[3],str[MAXLEN],DMA;
RETCODE        err;
//for commdlg
static OPENFILENAME ofn;
static char lpszFileName[_MAX_PATH],lpszTitleName[_MAX_FNAME+_MAX_EXT];
//for template boutt group
int nRow;
int nMaxTitleLen,nCurPos,empty;
// use temperaryly
static char  szActiveEvent[15]="0";
static char  szAddr[MAXLEN]="0",szEndAddr[MAXLEN]="0";
static char  szAddrMask[MAXLEN]="0",szData[MAXLEN]="0",
             szDataMask[MAXLEN]="0";
HWND tempHandle;
WORD tempControlId;
/* Winky 3/9/98 */
CHAR cWorkDirBuf[1024];
CHAR *cWorkDir;
int  cWorkDisk;
/* EOF Winky */

  switch (message) {
     case WM_CREATE:
        hDc = GetDC(hWnd);
        GetTextMetrics(hDc,&tm);
        cxChar = tm.tmMaxCharWidth;
        cxAveChar=tm.tmAveCharWidth;
        cyChar = tm.tmHeight + 3 * tm.tmExternalLeading ;
        cyInterLine = 0.5 * tm.tmHeight;
        cxLength = 10*cxChar;
        cx05Length =  4 * cxChar;
        cx10Length =  8 * cxChar;
        cx15Length = 12 * cxChar;
        cx20Length = 16 * cxChar;
        cx30Length = 24 * cxChar;
        cyHeight = 1.5 * cyChar;
        ReleaseDC(hWnd,hDc);

        hMenu=LoadMenu(hInst,
                 "DialogMenu");
        SetMenu(hWnd,hMenu);

        RegisterHelpEntry(3,hWnd,HE_EVENT);

        CreateWindow( "static","Active E&vent:",
           WS_CHILD | WS_VISIBLE,
           5+cx05Length+1*cxChar,cyChar,
           15*cxAveChar, cyHeight,
           hWnd, 1,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndActiveEvent = CreateWindow( "combobox",NULL,
           WS_CHILD | WS_VISIBLE | WS_VSCROLL
           | CBS_DROPDOWNLIST |CBS_SORT,
           5+cx15Length, cyChar,
           cx20Length, 5*cyHeight,
           hWnd, IDC_ACTIVEEVENT,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        //change combox message procedure
        lpfnOldCombox = (FARPROC)GetWindowLong(
           hWndActiveEvent,GWL_WNDPROC);
        SetWindowLong(hWndActiveEvent,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)ComboxProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        hBsEndAddr = CreateWindow( "button", "E&nd Addr",
           WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
           5+cx15Length+5, 2*cyChar+1*cyHeight,
           cx10Length-2*cxChar, cyHeight,
           hWnd, IDB_END,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);
        SendMessage(hBsEndAddr,BM_SETCHECK,TRUE,0);

        hBsLength = CreateWindow( "button", "&Length",
           WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
           5+cx30Length-5*cxChar, 2*cyChar+1*cyHeight,
           cx10Length, cyHeight,
           hWnd, IDB_LENGTH,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);
        SendMessage(hBsLength,BM_SETCHECK,FALSE,0);

        CreateWindow( "static","&mask",
           WS_CHILD | WS_VISIBLE,
           5+cx30Length+3*cxChar, 2*cyChar+cyHeight,
           5*cxAveChar, cyHeight,
           hWnd, 1,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        CreateWindow( "static", "&addr:",
           WS_CHILD | WS_VISIBLE,
           5+1*cxChar, 3*cyChar+1*cyHeight+cyInterLine,
           5*cxAveChar, cyHeight,
           hWnd, 2,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndAddr = CreateWindow( "edit", " ",
           WS_CHILD | WS_VISIBLE | WS_BORDER |WS_CLIPSIBLINGS
           |ES_AUTOHSCROLL,
           5+cx05Length, 3*cyChar+1*cyHeight+cyInterLine,
           cx10Length, cyHeight,
           hWnd, IDE_ADDR,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndEndAddr = CreateWindow( "edit", " ",
           WS_CHILD | WS_VISIBLE | WS_BORDER
           | WS_CLIPSIBLINGS|ES_AUTOHSCROLL,
           5+cx15Length+5, 3*cyChar+1*cyHeight+cyInterLine,
           cx15Length, cyHeight,
           hWnd, IDE_ENDADDR,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndAddrMask = CreateWindow( "edit", " ",
           WS_CHILD | WS_VISIBLE | WS_BORDER
           | WS_CLIPSIBLINGS|ES_AUTOHSCROLL,
           5+cx30Length+10, 3*cyChar+1*cyHeight+cyInterLine,
           cx10Length,cyHeight,
           hWnd, IDE_ADDRMASK,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        CreateWindow( "static", "mas&k",
           WS_CHILD | WS_VISIBLE,
           5+cx30Length+3*cxChar,
           3*cyChar+2*cyHeight+cyInterLine,
           5*cxAveChar, cyHeight,
           hWnd, 3,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        CreateWindow( "static", "&data:",
           WS_CHILD | WS_VISIBLE,
           5+1*cxChar, 4*cyChar+2*cyHeight+2*cyInterLine,
           5*cxAveChar, cyHeight,
           hWnd, 3,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndData = CreateWindow( "edit", " ",
           WS_CHILD | WS_VISIBLE | WS_BORDER
           | WS_CLIPSIBLINGS|ES_AUTOHSCROLL,
           5+cx05Length, 4*cyChar+2*cyHeight+2*cyInterLine,
           cx10Length, cyHeight,
           hWnd, IDE_DATA,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        hWndDataMask = CreateWindow( "edit", " ",
           WS_CHILD | WS_VISIBLE | WS_BORDER
           | WS_CLIPSIBLINGS|ES_AUTOHSCROLL,
           5+cx30Length+10, 4*cyChar+2*cyHeight+2*cyInterLine,
           cx10Length,cyHeight,
           hWnd, IDE_DATAMASK,
           GetWindowWord(hWnd,GWW_HINSTANCE), NULL);

        statusY=4*cyChar+2*cyHeight+2*cyInterLine+3*cyHeight;
        widthX=5+cx30Length+10+cx10Length+10;
        //change edit message procedure
        lpfnOldEdit = (FARPROC)GetWindowLong(
           hWndAddr,GWL_WNDPROC);
        SetWindowLong(hWndAddr,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)EditProc,
            GetWindowWord(hWnd,GWW_HINSTANCE)));

        SetWindowLong(hWndEndAddr,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)EditProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));
                           
        SetWindowLong(hWndAddrMask,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)EditProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        SetWindowLong(hWndData,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)EditProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        SetWindowLong(hWndDataMask,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)EditProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        // initialize the OPENFILENAME
        ofn.lStructSize=sizeof(OPENFILENAME);
        ofn.hwndOwner=hWnd;
        ofn.hInstance=NULL;
        ofn.lpstrFilter="EVENT Files (*.EVT)";
        ofn.lpstrCustomFilter=NULL;
        ofn.nMaxCustFilter=0;
        ofn.nFilterIndex=0;
        ofn.lpstrFile=NULL;
        ofn.nMaxFile=_MAX_PATH;
        ofn.lpstrFileTitle=NULL;
        ofn.nMaxFileTitle=_MAX_FNAME+_MAX_EXT;
        ofn.lpstrInitialDir=NULL;
        ofn.lpstrTitle=NULL;
        ofn.Flags=0;
        ofn.nFileOffset=0;
        ofn.lpstrDefExt="*.evt";
        ofn.lCustData=0l;
        ofn.lpfnHook=NULL;
        ofn.lpTemplateName=NULL;

        StatusWindow(hWnd);

        //change status button message proc
        lpfnOldButton = (FARPROC)GetWindowLong(
           hBsEndAddr,GWL_WNDPROC);
        for (i=0;i<nDyBsGroup;i++)
           for(j=0;j<nUmBsEachGroup;j++)
              SetWindowLong(hWndEachButton[i][j],GWL_WNDPROC,
                 (LONG)MakeProcInstance((FARPROC)ButtonProc,
                 GetWindowWord(hWnd,GWW_HINSTANCE)));

        SetWindowLong(hBsEndAddr,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)ButtonProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        SetWindowLong(hBsLength,GWL_WNDPROC,
           (LONG)MakeProcInstance((FARPROC)ButtonProc,
           GetWindowWord(hWnd,GWW_HINSTANCE)));

        //setuplistbox
        SetupListBox(hWnd);
        return 0;
     case WM_KEYDOWN:
        case VK_HOME:
           i=1;
           break;

     case WM_SYSKEYDOWN:
        switch(wParam) {
           case 0x56:
              if ((HIWORD(lParam) && 0x2fff) >0 )
                 SetFocus(hWndActiveEvent);
              break;
           case 0x41:
              if ((HIWORD(lParam) && 0x2fff) > 0)
                 SetFocus(hWndAddr);
              break;
           case 0x44:
              if ((HIWORD(lParam) && 0x2fff) > 0)
                 SetFocus(hWndData);
              break;
           case 0x4E:
              if ((HIWORD(lParam) && 0x2fff) > 0 )
                 SetFocus(hBsEndAddr);
              break;
           case 0x4c:
              if ((HIWORD(lParam) && 0x2fff) > 0)
                 SetFocus(hBsLength);
              break;
           case 0x4D:
              if ((HIWORD(lParam) && 0x2fff) > 0)
                 SetFocus(hWndAddrMask);
              break;
           case 0x4B:
              if ((HIWORD(lParam) && 0x2fff) > 0)
                 SetFocus(hWndDataMask);
              break;
        }
        break;
     case WM_COMMAND:
        if (wParam >= 500 && wParam < 703 && nComboList>0) {
           //button group command
           if (HIWORD(lParam)==BN_CLICKED) {
              strncpy(tt,itoa(wParam,ss,10),2);
              strcpy(tmp,itoa(wParam,ss,10));
              tt[2]='\0';
              nIndex=atoi(tt)-50;
              SendMessage(hWndTrcEachButton[nIndex][0],BM_SETCHECK,FALSE,0);
              SendMessage(hWndTrcEachButton[nIndex][1],BM_SETCHECK,FALSE,0);
              SendMessage(hWndTrcEachButton[nIndex][2],BM_SETCHECK,FALSE,0);
              SendMessage(LOWORD(lParam),BM_SETCHECK,TRUE,0);
              if (tmp[2]=='0')
                 strcpy(tmp,"0");
              else if (tmp[2]=='1')
                 strcpy(tmp,"1");
              else if (tmp[2]=='2')
                 strcpy(tmp,"X");
              EvtSetActiveField(theEvent,bsTrcEachGroup[nIndex].szEachGroupName);
              EvtSetStartAndMask(theEvent,tmp);
              break;
           }
        }
        else {
           if (nComboList >0) {
              switch(wParam) {
              //button end and length command
              case 800:
              case 801:
                 if (HIWORD(lParam)==BN_CLICKED) {
                    SendMessage(hBsEndAddr,BM_SETCHECK,FALSE,0);
                    SendMessage(hBsLength,BM_SETCHECK,FALSE,0);
                    SendMessage(LOWORD(lParam),BM_SETCHECK,TRUE,0);
                    if (wParam==800)
                       EvtSetEndFunction(theEvent,END_ADDR_FUNC);
                    else
                       EvtSetEndFunction(theEvent,END_LEN_FUNC);
                    EvtSetActiveField(theEvent, "address");
                    GetWindowText(hWndEndAddr,szEndAddr,MAXLEN);
                    err=EvtSetEnd(theEvent,szEndAddr);
                    if (err!=0) {
                       ErrDisplayFormattedError(err, FORCE_POPUP,
                          NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                       SetFocus(hWndEndAddr);
                    }
                    EvtGetEnd(theEvent,szEndAddr,MAXLEN);
                    SetWindowText(hWndEndAddr,szEndAddr);
                 }
                 return 0;
              case IDE_ADDR:
                 if (HIWORD(lParam)==EN_KILLFOCUS) {
                       EvtSetActiveField(theEvent, "address");
                       GetWindowText(hWndAddr,szAddr,MAXLEN);
                       err=EvtSetStart(theEvent,szAddr);
                       if (err!=0)
                          ErrDisplayFormattedError(err, FORCE_POPUP,
                             NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                       EvtSetActiveField(theEvent, "address");
                       EvtGetStart(theEvent,szAddr,MAXLEN);
                       SetWindowText(hWndAddr,szAddr);
                       strcpy(szEndAddr,"");
                       EvtGetEnd(theEvent,szEndAddr,MAXLEN);
                       SetWindowText(hWndEndAddr,szEndAddr);
                       EvtGetMask(theEvent,szAddrMask,MAXLEN);
                       SetWindowText(hWndAddrMask,szAddrMask);
                   }
                   return 0;
              case IDE_ENDADDR:
                 if (HIWORD(lParam)==EN_KILLFOCUS) {
                    EvtSetActiveField(theEvent, "address");
                    GetWindowText(hWndEndAddr,szEndAddr,MAXLEN);
                    err=EvtSetEnd(theEvent,szEndAddr);
                    if (err!=0)
                       ErrDisplayFormattedError(err, FORCE_POPUP,
                          NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                    EvtSetActiveField(theEvent, "address");
                    EvtGetStart(theEvent,szAddr,MAXLEN);
                    SetWindowText(hWndAddr,szAddr);
                    strcpy(szEndAddr,"");
                    EvtGetEnd(theEvent,szEndAddr,MAXLEN);
                    SetWindowText(hWndEndAddr,szEndAddr);
                    EvtGetMask(theEvent,szAddrMask,MAXLEN);
                    SetWindowText(hWndAddrMask,szAddrMask);
                 }
                 return 0;
              case IDE_ADDRMASK:
                 if (HIWORD(lParam)==EN_KILLFOCUS) {
                    EvtSetActiveField(theEvent, "address");
                    GetWindowText(hWndAddrMask,szAddrMask,MAXLEN);
                    err=EvtSetMask(theEvent,szAddrMask);
                    if (err!=0)
                       ErrDisplayFormattedError(err, FORCE_POPUP,
                          NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                    EvtSetActiveField(theEvent, "address");
                    EvtGetStart(theEvent,szAddr,MAXLEN);
                    SetWindowText(hWndAddr,szAddr);
                    strcpy(szEndAddr,"");
                    EvtGetEnd(theEvent,szEndAddr,MAXLEN);
                    SetWindowText(hWndEndAddr,szEndAddr);
                    EvtGetMask(theEvent,szAddrMask,MAXLEN);
                    SetWindowText(hWndAddrMask,szAddrMask);
                 }
                 return 0;
              case IDE_DATA:
                 if (HIWORD(lParam)==EN_KILLFOCUS) {
                    EvtSetActiveField(theEvent, "data");
                    GetWindowText(hWndData,szData,MAXLEN);
                    DeleteBlank(szData,szData);
                    err=EvtSetStart(theEvent,szData);
                    if (err!=0)
                       ErrDisplayFormattedError(err, FORCE_POPUP,
                          NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                    EvtSetActiveField(theEvent, "data");
                    EvtGetStart(theEvent,szData,MAXLEN);
                    SetWindowText(hWndData,szData);
                    EvtGetMask(theEvent,szDataMask,MAXLEN);
                    SetWindowText(hWndDataMask,szDataMask);
                 }
                 return 0;
              case IDE_DATAMASK:
                 if (HIWORD(lParam)==EN_KILLFOCUS) {
                    EvtSetActiveField(theEvent, "data");
                    GetWindowText(hWndDataMask,szDataMask,MAXLEN);
                    err=EvtSetMask(theEvent,szDataMask);
                    if (err!=0)
                      ErrDisplayFormattedError(err, FORCE_POPUP,
                        NULL,NULL,NULL,0,(S16 FAR *) &buttonID);
                    EvtSetActiveField(theEvent, "data");
                    EvtGetStart(theEvent,szData,MAXLEN);
                    SetWindowText(hWndData,szData);
                    EvtGetMask(theEvent,szDataMask,MAXLEN);
                    SetWindowText(hWndDataMask,szDataMask);
                 }
                 return 0;
              } //end of switch
           }// end of if
           switch(wParam) {
              //menu command
              case IDM_EVTFSAVE:
                 ofn.hwndOwner=hWnd;
                 ofn.lpstrFile=lpszFileName;
                 ofn.lpstrFileTitle=lpszTitleName;
                 ofn.Flags=OFN_SHOWHELP|OFN_OVERWRITEPROMPT;
                 if (GetSaveFileName(&ofn)) {
                    EvtSaveAsEvents(lpszFileName);
                    /*
                    MessageBox(hWnd,lpszFileName,"File Message",
                         MB_OK|MB_ICONSTOP);
                    */
                 }
                 else
                    CommDlgExtendedError();
                 return 0;
              case IDM_EVTFRESTORE:
                 strcpy(str,"All triggers and events will be deleted. Still restore?");
                 ErrMessageBoxYesNoL(str,&result);
                 if (result==IDNO)
                    return 0;

                 /* Winky 3/9/98 -- Save current directory */
                 cWorkDir = getcwd(cWorkDirBuf, 1024);
                 cWorkDisk = getdisk();
                 /* EOF Winky */

                 ofn.hwndOwner=hWnd;
                 ofn.lpstrFile=lpszFileName;
                 ofn.lpstrFileTitle=lpszTitleName;
                 ofn.Flags=OFN_CREATEPROMPT;
                 if (GetOpenFileName(&ofn)) {
                    /* Winky 3/9/98 -- Restore previous directory */
                    setdisk(cWorkDisk);
                    chdir(cWorkDirBuf);
                    /* EOF Winky */
                    ClearTriggerForSomeEvent(NULL);
                    EvtDeleteAllEvents();
                    EvtRestoreEvents(lpszFileName);
                    SendMessage(hWndActiveEvent,CB_RESETCONTENT,0,0);
                    /*
                    i=(int)SendMessage(hWndActiveEvent,CB_GETCOUNT,0,0);
                    if (i>0)
                       for (j=0;j<i;j++) {
                          SendMessage(hWndActiveEvent,CB_DELETESTRING,
                             0,0);
                          nComboList=nComboList-1;
                       }
                    */
                    SetupListBox(hWnd);
                 }
                 else
                    CommDlgExtendedError();
                 return 0;
              case IDM_EVTFEXIT:
                 //SendMessage(hWnd,WM_CLOSE,0,0);
                 DestroyWindow(hWnd);
                 return 0;
              case IDM_EVTEADD:
                 lpAddEventProc=MakeProcInstance((FARPROC)AddDlgProc,
                    GetWindowWord(hWnd,GWW_HINSTANCE));
                 if (nComboList==8) {
                    strcpy(str,"total event number can't exceed eight.");
                    ErrMessageBoxOkL(str);
                    return 0;
                 }
                 if (DialogBox(hInst,"ADDEVENT",hWnd,lpAddEventProc))
                    if ((j=lstrlen(lpszSingleLine))>0) {
                       for (i=0;i<j;i++)
                          lpszSingleLine[i]=toupper(lpszSingleLine[i]);
                       wsprintf(str,"Event: %s",lpszSingleLine);
                       SetWindowText(hWnd,str);
                       if ((SendMessage(hWndActiveEvent,CB_SELECTSTRING,
                               0,(DWORD)lpszSingleLine))==CB_ERR) {
                          SendMessage(hWndActiveEvent,CB_ADDSTRING,
                             0,(DWORD)lpszSingleLine);
                          nComboList=nComboList+1;
                          SendMessage(hWndActiveEvent,CB_SELECTSTRING,
                             0,(DWORD)lpszSingleLine);
                       }
                       if (stricmp(lpszSingleLine,"")!=0) {
                          EvtSetActiveEvent(theEvent,lpszSingleLine);
                          SetupEvent();
                       }
                       else
                          DisablePanel();
                    }
                 return 0;
              case IDM_EVTEDELETE:
                 nSel=(int)SendMessage(hWndActiveEvent,CB_GETCURSEL,0,0l);
                 SendMessage(hWndActiveEvent,CB_GETLBTEXT,nSel,
                    (DWORD)szActiveEvent);
                 if ((err=CheckIsEventInUseForTrig(szActiveEvent))!=GOOD){
                    strcpy(str,"Be used for some trigger. If the event is deleted, some triggers will be clear. Still deletae?");
                    ErrMessageBoxYesNoL(str,&result);
                    if (result==IDNO)
                       return 0;
                 }
                 SendMessage(hWndActiveEvent,CB_DELETESTRING,nSel,0l);
                 nComboList=nComboList-1;
                 i=(int)SendMessage(hWndActiveEvent,CB_GETCOUNT,0,0);
                 EvtDeleteEvent(theEvent);
                 ClearTriggerForSomeEvent(szActiveEvent);
                 if (nSel==i)
                    nSel=nSel-1;
                 SendMessage(hWndActiveEvent,CB_SETCURSEL,nSel,0l);
                 SendMessage(hWndActiveEvent,CB_GETLBTEXT,nSel,
                    (DWORD)szActiveEvent);
                 if (nComboList>0) {
                    EvtSetActiveEvent(theEvent,szActiveEvent);
                    SetWindowText(hWnd,szActiveEvent);
                    SetupEvent();
                 }
                 else
                    DisablePanel();
                 return 0;
              case IDM_EVTECLEAR:
                 if (nComboList > 0) {
                    EvtClearEvent(theEvent);
                    SetupEvent();
                 }
                 return 0;
              case IDM_EVTEDELETEALL:
                 strcpy(str,"Warning, Delete All Events?");
                 ErrMessageBoxYesNoL(str,&result);
                 if (result==IDYES) {
                    for (i=0;i<nComboList;i++) {
                       SendMessage(hWndActiveEvent,CB_GETLBTEXT,i,
                          (DWORD)szActiveEvent);
                       if ((err=CheckIsEventInUseForTrig(szActiveEvent))
                          !=GOOD){
                          strcpy(str,"Be used for some trigger. If events are deleted, triggers will be cleared. Still delete all?");
                          ErrMessageBoxYesNoL(str,&result);
                          if (result==IDNO)
                             return 0;
                          else
                             break;
                       }
                    }
                    EvtDeleteAllEvents();
                    ClearTriggerForSomeEvent(NULL);
                    SendMessage(hWndActiveEvent,CB_RESETCONTENT,0,0);
                    DisablePanel();
                    nComboList=0;
                 }
              return 0;
              case IDM_EVTWBKPT:
                 ToolBarMsg(MENU_OFFSET + BREAKPOINT_WINDOW, lParam);
                 break;
              case IDM_EVTWCPU:
                 ToolBarMsg(MENU_OFFSET + CPU_WINDOW, lParam);
                 break;
              case IDM_EVTWMEMORY:
                 ToolBarMsg(MENU_OFFSET + MEMORY_WINDOW, lParam);
                 break;
              case IDM_EVTWPERIPHERAL:
                 ToolBarMsg(MENU_OFFSET + PERIPHERAL_WINDOW, lParam);
                 break;
              case IDM_EVTWSHELL:
                 ToolBarMsg(MENU_OFFSET + SHELL_WINDOW, lParam);
                 break;
              case IDM_EVTWSOURCE:
                 ToolBarMsg(MENU_OFFSET + SOURCE_WINDOW, lParam);
                 break;
              case IDM_EVTWSTACK:
                 ToolBarMsg(MENU_OFFSET + STACK_WINDOW, lParam);
                 break;
              case IDM_EVTWSTATUS:
                 ToolBarMsg(MENU_OFFSET + STATUS_WINDOW, lParam);
                 break;
              case IDM_EVTWSPA:
                 ToolBarMsg(MENU_OFFSET + SPA_WINDOW, lParam);
                 break;
              case IDM_EVTWTOOLBAR:
                 ToolBarMsg(MENU_OFFSET + TOOLBAR_WINDOW, lParam);
                 break;
              case IDM_EVTWTRACE:
                 ToolBarMsg(MENU_OFFSET + TRACE_WINDOW, lParam);
                 break;
              case IDM_EVTWTRIGGER:
                 ToolBarMsg(MENU_OFFSET + TRIGGER_WINDOW, lParam);
                 break;
              case IDM_EVTWVARIABLE:
                 ToolBarMsg(MENU_OFFSET + VARIABLE_WINDOW, lParam);
                 break;
              /*
              case IDM_EVTACCEPT:
                 TrigTriggerOpen(&theTrigger,"","default");
                 TrigTriggerAccept(theTrigger);
                 return 0;
              */
              case IDM_EVTHINDEX:
                 WinHelp(hWnd, "PWRVIEWS.HLP", HELP_CONTEXT, HE_CONTENTS);
                 return 0;
              case IDM_EVTHHELP:
                 WinHelp(hWnd, "PWRVIEWS.HLP", HELP_HELPONHELP, HE_CONTENTS);
                 return 0;
              case IDM_EVTHEVENT:
                 WinHelp(hWnd, "PWRVIEWS.HLP", HELP_CONTEXT, HE_EVENT);
                 return 0;
              //combo command
              case IDC_ACTIVEEVENT:
                 if (HIWORD(lParam)==CBN_SELCHANGE) {
                    nSel=(WORD)SendMessage(hWndActiveEvent,
                       CB_GETCURSEL, 0, 0L);
                    SendMessage(hWndActiveEvent, CB_GETLBTEXT,
                       nSel, (DWORD)szActiveEvent);
                    if (stricmp(szActiveEvent,"")!=0) {
                       EvtSetActiveEvent(theEvent,szActiveEvent);
                       wsprintf(str,"Event: %s",szActiveEvent);
                       SetWindowText(hWnd,str);
                       SetupEvent();
                   }
                   else
                      DisablePanel();
                 }
                 return 0;
                 //edit command
              case VK_TAB:
                 switch(HIWORD(lParam)) {
                    case 701:
                       SetFocus(hWndAddr);
                       break;
                    case 702:
                       SetFocus(hWndEndAddr);
                       break;
                    case 703:
                       SetFocus(hWndAddrMask);
                       break;
                    case 704:
                       SetFocus(hWndData);
                       break;
                    case 705:
                       SetFocus(hWndDataMask);
                       break;
                 }
                 return 0;
           }  //end of switch
        }  //end of else
        break;
     case WM_MOVE:
     case WM_LBUTTONDOWN:
        tempHandle=GetFocus();
        tempControlId=GetWindowWord(tempHandle,GWW_ID);
        if (tempControlId==IDC_ACTIVEEVENT)
           SendMessage(tempHandle,CB_SHOWDROPDOWN,FALSE,0);
        break;
     case WM_PAINT :
        hDc=BeginPaint(hWnd,&ps);
        //edit, combox control name
        TextOut(hDc,5+cx05Length+3*cxChar, 2*cyChar+1*cyHeight,
           szString02,lstrlen(szString02));
        //template button group name
        ii=0;
        nRow=0;
        nMaxTitleLen=0;
        nCurPos=30;
        while (!(ii>=nTrcBsGroup)) {
           if (nRow==0) {
              TextOut(hDc,nCurPos+nMaxTitleLen*cxAveChar,statusY-cyHeight,
                 szString01,lstrlen(szString01));
              nCurPos=nCurPos+GROUPWIDTH+nMaxTitleLen*cxAveChar+INTERLEN;
              nMaxTitleLen=0;
           }
           if ((nRow<nTrcMaxRow) && (ii<nTrcBsGroup)) {
              j=lstrlen(bsTrcEachGroup[ii].szEachGroupName);
              TextOut(hDc,nCurPos-25,statusY+10+20*nRow,
                 bsTrcEachGroup[ii].szEachGroupName,j);
              if (j>nMaxTitleLen)
                 nMaxTitleLen=j;
              ii++;
           }
           nRow=nRow+1;
           if (nRow==(nMaxRow+nTrcMaxRow)) {
              nRow=0;
           }
        }
        MoveTo(hDc,0,statusY);
        LineTo(hDc,mAxwidth,statusY);
        EndPaint(hWnd,&ps);
        return 0;
     case WM_CLOSE:
        DestroyWindow(hWnd);
        hWndEvt=NULL;
        break;
     case WM_DESTROY :
        hWndEvt=NULL;
        //PostQuitMessage (0);
        break;
  }
  return DefWindowProc (hWnd, message, wParam, lParam);
}

/************************************************************************
**
**          EditProc
**
*************************************************************************/
long FAR PASCAL EditProc(HWND hWnd, UINT message,
                         UINT wParam, LONG lParam) {
short n;

   n=GetWindowWord(hWnd,GWW_ID);
   if (message == WM_KEYUP) {
      if (wParam==VK_HOME)
         SendMessage(hWnd,WM_KEYDOWN,VK_HOME,lParam);
      if (wParam==VK_END)
         SendMessage(hWnd,WM_KEYDOWN,VK_END,lParam);
      if (wParam==VK_DELETE)
         SendMessage(hWnd,WM_KEYDOWN,VK_DELETE,lParam);
   }
   if (message == WM_KEYDOWN) {
      if (wParam==VK_TAB) {
         if (n==705)
            n=701;
         else
            n=n+1;
         SendMessage(GetParent(hWnd),WM_COMMAND,VK_TAB,
            MAKELONG(hWnd,n));
      }
      if (wParam==VK_RETURN) {
         SetFocus(hWndEvt);
         SetFocus(hWnd);
         /*
         SendMessage(GetParent(hWnd),WM_COMMAND,
            n,MAKELONG(hWnd,EN_KILLFOCUS));
         */
      }
   }
   if (message == WM_SYSKEYDOWN)
      SendMessage(GetParent(hWnd),WM_SYSKEYDOWN,
         wParam,lParam);
   return CallWindowProc(lpfnOldEdit,hWnd,message,wParam,lParam);        
}       

/************************************************************************
**
**          ButtonProc
**
*************************************************************************/
long FAR PASCAL ButtonProc(HWND hWnd, UINT message,
                         UINT wParam, LONG lParam) {
   if (message == WM_SYSKEYDOWN) {
      SendMessage(GetParent(hWnd),WM_SYSKEYDOWN,wParam,
          lParam);
   }
   return CallWindowProc(lpfnOldButton,hWnd,message,wParam,lParam);
}

/************************************************************************
**
**          ComboxProc
**
*************************************************************************/
long FAR PASCAL ComboxProc(HWND hWnd, UINT message,
                         UINT wParam, LONG lParam) {
   if (message == WM_SYSKEYDOWN) {
      SendMessage(GetParent(hWnd),WM_SYSKEYDOWN,wParam,
          lParam);
   }
   return CallWindowProc(lpfnOldCombox,hWnd,message,wParam,lParam);
}

/**********************************************************************
**
**      AddDlgProc
**
***********************************************************************/
BOOL FAR PASCAL AddDlgProc (HWND hDlg, UINT message,
                UINT wParam, LONG lParam) {
   switch(message) {
      case WM_INITDIALOG:
         return TRUE;
      case WM_COMMAND:
         switch(wParam) {
            case IDD_OKAY:
               GetDlgItemText(hDlg,IDD_ADDEVENT,
                  lpszSingleLine,MAXLEN);
               EndDialog(hDlg,TRUE);   
               return 0;
            case IDD_CANCEL:
               EndDialog(hDlg,FALSE);
               return 0;
            case IDD_HELP:
               WinHelp(hDlg, "PWRVIEWS.HLP", HELP_CONTEXT, 2001);
               return 0;
         }
         break;
   }
   return FALSE;
}

/**********************************************************
**
**           ClearEventPanel()
**
**********************************************************/
void ClearEventPanel() {
int i;

    SetWindowText(hWndActiveEvent," ");
    SetWindowText(hWndAddr," ");
    SetWindowText(hWndEndAddr," ");
    SetWindowText(hWndAddrMask," ");
    SetWindowText(hWndData," ");
    SetWindowText(hWndDataMask," ");
    SendMessage(hBsEndAddr,BM_SETCHECK,TRUE,0);
    SendMessage(hBsLength,BM_SETCHECK,FALSE,0);
    for (i=0;i<nDyBsGroup;i++) { 
       SendMessage(hWndEachButton[i][0],BM_SETCHECK,0,0);
       SendMessage(hWndEachButton[i][1],BM_SETCHECK,0,0);
       SendMessage(hWndEachButton[i][2],BM_SETCHECK,1,0);
    }
}

/**********************************************************
**
**           DisablePanel()
**
**********************************************************/
void DisablePanel() {
int i;
char str[MAXLEN];

    SetWindowText(hWndActiveEvent," ");
    SetWindowText(hWndAddr," ");
    SetWindowText(hWndEndAddr," ");
    SetWindowText(hWndAddrMask," ");
    SetWindowText(hWndData," ");
    SetWindowText(hWndDataMask," ");
    SendMessage(hBsEndAddr,BM_SETCHECK,0,0);
    SendMessage(hBsLength,BM_SETCHECK,0,0);
    for (i=0;i<nDyBsGroup;i++)
       SendMessage(hWndEachButton[i][0],BM_SETCHECK,0,0);
    for (i=0;i<nTrcBsGroup;i++) {
       SendMessage(hWndTrcEachButton[i][0],BM_SETCHECK,0,0);
       SendMessage(hWndTrcEachButton[i][1],BM_SETCHECK,0,0);
       SendMessage(hWndTrcEachButton[i][2],BM_SETCHECK,0,0);
    }
    wsprintf(str,"%s","Event: No Event Selected");
    SetWindowText(hWndEvt,str);
}

/*************************************************************
**
**               SetupEvent
**
**************************************************************/
void SetupEvent() {
char start[MAXLEN],end[MAXLEN];
END_FUNC func;
int i,j;

   //EvtGetActiveEvent(theEvent,szEvtName,lenEvtName);
   EvtSetActiveField(theEvent,"address");
   EvtGetStart(theEvent,start,MAXLEN);
   SetWindowText(hWndAddr,start);

   EvtGetEndFunction(theEvent,&func);
   if (func==END_ADDR_FUNC) {
      SendMessage(hBsEndAddr,BM_SETCHECK,1,0);
      SendMessage(hBsLength,BM_SETCHECK,0,0);
   }
   else {
      SendMessage(hBsEndAddr,BM_SETCHECK,0,0);
      SendMessage(hBsLength,BM_SETCHECK,1,0);
   }
   EvtGetEnd(theEvent,end,MAXLEN);
   SetWindowText(hWndEndAddr,end);

   EvtGetMask(theEvent,end,MAXLEN);
   SetWindowText(hWndAddrMask,end);

   EvtSetActiveField(theEvent,"data");
   EvtGetStart(theEvent,start,MAXLEN);
   SetWindowText(hWndData,start);

   EvtGetMask(theEvent,end,MAXLEN);
   SetWindowText(hWndDataMask,end);

   //set up status list
   for (i=0;i<nDyBsGroup;i++) {
      EvtSetActiveField(theEvent,bsEachGroup[i].szEachGroupName);
      EvtGetStartAndMask(theEvent,start,MAXLEN);
      SendMessage(hWndEachButton[i][0], BM_SETCHECK, 0, 0);
      //SendMessage(hWndEachButton[i][1], BM_SETCHECK, 0, 0);
      //SendMessage(hWndEachButton[i][2], BM_SETCHECK, 0, 0);
      /*
      if (stricmp(start,"0")==0)
         j = 0;
      else if (stricmp(start,"1")==0)
         j = 1;
      else if (stricmp(start,"X")==0 || stricmp(start,"x")==0)
         j = 2;
      */
      if (stricmp(start,"1")==0)
          SendMessage(hWndEachButton[i][0], BM_SETCHECK, 1, 0);
   }
   for (i=0;i<nTrcBsGroup;i++) {
      EvtSetActiveField(theEvent,bsTrcEachGroup[i].szEachGroupName);
      EvtGetStartAndMask(theEvent,start,MAXLEN);
      SendMessage(hWndTrcEachButton[i][0], BM_SETCHECK, 0, 0);
      SendMessage(hWndTrcEachButton[i][1], BM_SETCHECK, 0, 0);
      SendMessage(hWndTrcEachButton[i][2], BM_SETCHECK, 0, 0);
      if (stricmp(start,"0")==0)
         j = 0;
      else if (stricmp(start,"1")==0)
         j = 1;
      else if (stricmp(start,"X")==0 || stricmp(start,"x")==0)
         j = 2;
      SendMessage(hWndTrcEachButton[i][j], BM_SETCHECK, 1, 0);
   }
}

/*****************************************************************************
**
**  nCwRegisterClasses
**
*****************************************************************************/
int nCwRegisterClasses(VOID) {
   WNDCLASS   wndclass;    /* struct to define a window class */
   HINSTANCE hInstance;
   HWND hToolBar;
   //HINSTANCE hInstance;
   memset(&wndclass, 0x00, sizeof(WNDCLASS));

   if (GetToolBarHandle((HANDLE FAR *) &hToolBar) == GOOD) {
      hInstance = ((HINSTANCE) GetWindowWord(hToolBar, GWW_HINSTANCE));
      wndclass.hIcon = LoadIcon(hInstance, "EVENT");
   }
   /* load WNDCLASS with window's characteristics */
   wndclass.style = CS_HREDRAW | CS_VREDRAW; // | CS_BYTEALIGNWINDOW;
   wndclass.lpfnWndProc = EvtWndProc;

   /* Extra storage for Class and Window objects  */
   wndclass.cbClsExtra = 0;
   wndclass.cbWndExtra = 0;
   wndclass.hInstance = hInst;
   wndclass.hCursor = LoadCursor(NULL, IDC_ARROW);

   /* Create brush for erasing background */
   wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
   wndclass.lpszMenuName = "peri_menu";   /* Menu Name is App Name */
   wndclass.lpszClassName = szAppName; /* Class Name is App Name */
   if (!RegisterClass(&wndclass))
      return -1;
   return(0);
}

/*****************************************************************************
**
**  EvtOpenWindow
**
*****************************************************************************/
RETCODE EXPORT EvtOpenWindow(HWND parent,int left,int top,int width,
     int height,DWORD dwStyle,DWORD dwExStyle,HWND *evtWindow) {
RETCODE err=GOOD;

   if (hWndEvt == NULL) {
      err = CreateEvtPresenter(hLib, left, top,
               width, height,parent);
      *evtWindow = hWndEvt;
    }
    return err;
}

/*****************************************************************************
**
**  EvtCloseWindow
**
*****************************************************************************/
RETCODE EXPORT EvtCloseWindow() {
   if (hWndEvt != NULL) {
      hWndEvt = NULL;
   }
   return GOOD;
}

/*****************************************************************************
**
**  EvtExistsWindow
**
*****************************************************************************/
RETCODE EXPORT EvtExistsWindow(HWND *EvtHandle) {
   *EvtHandle = hWndEvt;
   return (GOOD);
}

/***********************************************************
**
**              DeleteBlank
**
************************************************************/
void DeleteBlank(LPSTR lpszin, LPSTR lpszout) {
int i,j;

   j=0;
   for (i=0;i<lstrlen(lpszin);i++)
      if (lpszin[i]!=' ') {
         lpszout[j]=lpszin[i];
         j=j+1;
      }
   lpszout[j]='\0';
}

/*********************************************************
**
**           StatusWindow
**
**********************************************************/
void StatusWindow(HWND hWnd) {
int nRow;
int nMaxTitleLen;
int i,ii,j,k;
int width,height,nCurPos;
char szTitle[MAXLEN];
BOOLEAN visible;
FIELD_TYPE fieldType;
ACTIVE_STATE activeState;
RECT winRect,cliRect;
int height1,height2;

   nRow=0;
   TmpltOpenTemplate(&theTemplate);
   TmpltSetTemplateName(theTemplate,"default");
   TmpltGetNumOfEntries(theTemplate,&nUmEntry);
   j=0;
   ii=0;
   for (i=0;i<nUmEntry;i++) {
      /*different from mp306 in fieldnum*/
      TmpltGetNameGivenFieldNumber(theTemplate,i+1,szTitle,MAXLEN);
      TmpltGetVisible(theTemplate,&visible);
      TmpltGetFieldType(theTemplate,&fieldType);
      TmpltGetActiveState(theTemplate,&activeState);
      if ((fieldType==0) && (visible==1)) {
         strcpy(bsTrcEachGroup[ii].szEachGroupName,szTitle);
         ii=ii+1;
      }
   }
   nRow=0;
   nDyBsGroup=j;
   nTrcBsGroup=ii;
   nTrcMaxRow=ceil(nTrcBsGroup/4.0);
   nMaxTitleLen=0;
   nCurPos=30;
   ii=0;
   while (!(ii>=nTrcBsGroup)) {
      if ((nRow<nTrcMaxRow) && (ii<nTrcBsGroup)) {
         if ((k=lstrlen(bsTrcEachGroup[ii].szEachGroupName))>nMaxTitleLen)
            nMaxTitleLen=k;
         for (j=0;j<3;j++) {
            hWndTrcEachButton[ii][j]=CreateWindow("button",
               NULL,WS_CHILD|WS_VISIBLE|BS_RADIOBUTTON,
               nCurPos+20*j,statusY+10+20*nRow,20,20,
               hWnd,500+10*ii+j,
               GetWindowWord(hWnd,GWW_HINSTANCE),NULL);
         }
         ii++;
      }
      nRow=nRow+1;
      if (nRow==nTrcMaxRow)
          nRow=0;
      if (nRow==0) {
         nCurPos=nCurPos+GROUPWIDTH+nMaxTitleLen*cxAveChar+INTERLEN;
         nMaxTitleLen=0;
      }
   }
   GetWindowRect(hWnd,&winRect);
   GetClientRect(hWnd,&cliRect);
   height1=winRect.bottom-winRect.top;
   height2=cliRect.bottom-cliRect.top;
   height=statusY+20*(nMaxRow+1)+(height1-height2)+20*nTrcMaxRow+cyHeight;
   if (nRow!=0)
      nCurPos=nCurPos+GROUPWIDTH+nMaxTitleLen*cxAveChar;
   if (nCurPos>widthX)
      width=nCurPos+20;
   else
      width=widthX+10;
   MoveWindow(hWnd,nLeft,nTop,width,height,1);
   mAxwidth=width;
   mAxheight=height;
}

/*************************************************************
**
**               SetupListBox
**
**************************************************************/
void SetupListBox(HWND hWnd) {
int i;
U16 num;
char szActiveEvent[MAXLEN];
char str[MAXLEN];

   nComboList=0;
   EvtNumDefinedEvents(theEvent,&num);
   for(i=0;i<num;i++) {
      EvtGetNextEvent(theEvent,szActiveEvent,MAXLEN);
      if (stricmp(szActiveEvent,"")!=0) {
         SendMessage(hWndActiveEvent,CB_ADDSTRING,
            0,(DWORD)szActiveEvent);
         nComboList=nComboList+1;
      }
   }
   SendMessage(hWndActiveEvent,CB_SETCURSEL,0,0);
   SendMessage(hWndActiveEvent,CB_GETLBTEXT,0,
      (DWORD)szActiveEvent);
   if (nComboList > 0) {
      EvtSetActiveEvent(theEvent,szActiveEvent);
      wsprintf(str,"Event: %s",szActiveEvent);
      SetWindowText(hWnd,str);
      SetupEvent();
   }
   else {
      wsprintf(str,"%s","Event: No Event Selected");
      SetWindowText(hWnd,str);
      DisablePanel();
   }
}
/*********************************************************
**
**           CheckIsEventInUseForTrig
**
**********************************************************/
RETCODE CheckIsEventInUseForTrig(char *str) {
U8 i,j;
DESCRIPTOR theTrigger;
CHAR evtName[MAXLEN];
RETCODE err;
U16 origLevel;
   if ((err=TrigTriggerOpen(&theTrigger,"","default"))!=GOOD) {
      ErrDisplayError(err,FORCE_POPUP);
      return 1; /*error*/
   }
   TrigSeqLevelGet(theTrigger,&origLevel);
   for (i=0;i<4;i++) {
      TrigSeqLevelSet(theTrigger,i);
      for (j=0;j<8;j++) {
         TrigEventSet(theTrigger,ConvEvtCond(j));
         TrigEventNameGet(theTrigger,evtName,MAXLEN);
         if (strcmp(evtName,str)==0) {
            TrigSeqLevelSet(theTrigger,origLevel);
            return 1; /*error*/
         }
      }
   }
   TrigSeqLevelSet(theTrigger,origLevel);
   return GOOD;
}
/*********************************************************
**
**           ClearTriggerForSomeEvent
**
**********************************************************/
RETCODE ClearTriggerForSomeEvent(char *str) {
U8 i,j,k;
DESCRIPTOR theTrigger;
CHAR evtName[MAXLEN];
RETCODE err;
HWND hWndTrig;
U16 wParam,origLevel;

   if ((err=TrigTriggerOpen(&theTrigger,"","default"))!=GOOD) {
      ErrDisplayError(err,FORCE_POPUP);
      return 1; /*error*/
   }
   TrigExistsWindow(&hWndTrig);
   TrigSeqLevelGet(theTrigger,&origLevel);
   for (i=0;i<4;i++) {
      TrigSeqLevelSet(theTrigger,i);
      for (j=0;j<8;j++) {
         TrigEventSet(theTrigger,ConvEvtCond(j));
         TrigEventNameGet(theTrigger,evtName,MAXLEN);
         wParam=200+j*20;
         if (str==NULL || (strcmp(evtName,str)==0)) {
            if (origLevel==i) {
               SendMessage(hWndTrig,WM_COMMAND,wParam,0);
               TrigClearTrigNameEnableAction();
            }
            else {
               TrigEventNameSet(theTrigger,"\0");
               TrigEventEnableSet(theTrigger,0);
               for (k=0;k<16;k++)
                  TrigActionClear(theTrigger,ConvEvtAction(k));
               TrigActionSet(theTrigger,i);
            }
         }
      }
   }
   TrigSeqLevelSet(theTrigger,origLevel);
   return GOOD;
}
/************************************************************************
**
**          ErrMessageBoxOkL
**
*************************************************************************/
RETCODE ErrMessageBoxOkL(char *displayString) {
static U16 callcount=0;

   if (callcount>0)
      return GOOD;
   callcount++;
   MessageBox(hWndEvt,displayString,"MP/SLD",MB_OK);
   callcount--;
   return GOOD;
}
/************************************************************************
**
**          ErrMessageBoxYesNoL
**
*************************************************************************/
RETCODE ErrMessageBoxYesNoL(char *displayString,int *result) {
static U16 callcount=0;

   if (callcount>0)
      return GOOD;
   callcount++;
   *result=MessageBox(hWndEvt,displayString,"MP/SLD",MB_YESNO);
   callcount--;
   return GOOD;
}
/***************************************************************
**
**            ConvEvtCond
**
****************************************************************/
EVENT_COND ConvEvtCond(int line){
   switch(line) {
      case 0:  return EV0_COND;
      case 1:  return EV1_COND;
      case 2:  return EV2_COND;
      case 3:  return EV3_COND;
      case 4:  return EV4_COND;
      case 5:  return EV5_COND;
      case 6:  return EV6_COND;
      case 7:  return EV7_COND;
      case 8:  return TC0_COND;
      case 9:  return TC1_COND;
      case 10: return EXT_COND;
   }
   return 0;
}
/*****************************************************************
**
**           ConvEvtAction
**
******************************************************************/
ACTION_MASK ConvEvtAction(int col) {
   switch(col) {
      case 0: return BRK_MASK;
      case 1: return TRC_QUAL_MASK;
      case 2: return TRC_DIS_MASK;
      case 3: return TRC_START_MASK;
      case 4: return TRC_STOP_MASK;
      case 5: return TMR0_START_MASK;
      case 6: return TMR0_STOP_MASK;
      case 7: return TC0_RST_MASK;
      case 8: return TMR1_START_MASK;
      case 9: return TMR1_STOP_MASK;
      case 10: return TC1_RST_MASK;
      case 11: return TRIG_OUT_MASK;
      case 12: return CNT0_INC_MASK;
      case 13: return TC0_RST_MASK;
      case 14: return CNT1_INC_MASK;
      case 15: return TC1_RST_MASK;
   }
   return 0;
}
//************************** E O F ****************************************/
