/* t4main.c   (c)Copyright Sequiter Software Inc., 1990-1993.  All rights reserved. */
/*  Main test program for running CodeBase test code */

#ifdef S4OS2PM
#define  INCL_WINSYS
#define  INCL_WINWINDOWMGR
#define  INCL_DOSPROCESS
#define  INCL_GPIPRIMITIVES
#define  INCL_GPILCIDS
#endif

#include "d4all.h"
#ifdef __TURBOC__
#pragma hdrstop
#endif
#include "t4test.h"

#ifdef __ZTC__
#ifdef S4WINDOWS
#ifndef NULL
#define NULL 0
#endif
#endif
#endif

#ifdef __TURBOC__
extern unsigned _stklen = 28000;
#endif

D4DISPLAY display ;

#ifndef S4WINDOWS
#ifndef S4OS2PM
main( int argc, char **argv )
{
  d4parsestring_init( &display.parse_str, argc, argv ) ;
  return t4test( &display ) ;
}
#else
#include <process.h>
HWND hwndMainFrame = NULLHANDLE ;
HWND hwndMain ;
HDC  hdcMain ;
HAB  hab ;
HMQ  hmq ;
CHAR szAppName[MAXNAMEL] ;
CHAR szUntitled[MESSAGELEN] ;
VOID ExitProc(USHORT usTermCode)
{
  if(WinIsWindow(hab, hwndMainFrame))
    WinDestroyWindow(hwndMainFrame) ;
  WinDestroyMsgQueue(hmq) ;
  WinTerminate(hab) ;
  DosExitList(EXLST_EXIT, (PFNEXITLIST)NULL) ;
}

int on_val = 0 ;
int thread_on = 0 ;

VOID t4thread( void *disp )
{
  D4DISPLAY *display ;
  int i ;

  thread_on = 1 ;
  display = (D4DISPLAY *)disp ;

  /* Clear the window after marking that thread has started, but before test input started */
  WinInvalidateRegion( display->hWnd, (HRGN)NULL, FALSE );

  on_val = 1 ;

  t4test( display ) ;

  while ( display->display_set == 1 )   /* a display message not processed is waiting */
    ;

  display->display_set = 1 ;  /* mark a message as pending */
  display->do_box = 1 ;
  strcpy( display->str, "Test Thread Completed" ) ;
  display->len = strlen( display->str ) ;
  display->display_set = 1 ;
  WinInvalidateRegion( display->hWnd, (HRGN)NULL, FALSE );

  DosSleep( 1000L ) ;  /* delay to ensure on_val still true when box comes out */

  on_val = 0 ;
  thread_on = 0 ;
  _endthread() ;
}

VOID MainCommand(HWND hwnd, MPARAM mp1, MPARAM mp2)
{
  switch(SHORT1FROMMP(mp1))
  {
  case IDM_ABOUT:
    WinMessageBox(HWND_DESKTOP, hwnd, "CodeBase 5.0 Test Routines - version 1.0", "CB 5.0 OS/2", MSGBOXID, MB_OK ) ;
    break ;

  case EM_CODEBASE_ERROR:
    WinMessageBox(HWND_DESKTOP, display.hWnd, display.str, "CB 5.0 OS/2 ERROR", MSGBOXID, MB_OK ) ;
    display.display_set = 0 ;
    break ;

  case IDM_DO_TEST:
    if ( thread_on == 1 )   /* test already executing... */
    {
      WinMessageBox(HWND_DESKTOP, hwnd, "Test thread already started...", "CB 5.0 OS/2 TEST", MSGBOXID, MB_OK ) ;
      return;
    }
    if ( _beginthread( t4thread, 20480, (void *)&display ) == -1 )
    {
      WinMessageBox(HWND_DESKTOP, hwnd, "CodeBase 5.0 Test Failure- Couldn't start thread", "CB 5.0 OS/2", MSGBOXID, MB_OK ) ;
      return;
    }
    break ;
  }
  return;
}


MRESULT EXPENTRY MainWndProc(HWND hwnd, ULONG msg, MPARAM mp1, MPARAM mp2)
{
  RECTL rclUpdate ;
  HPS hps ;
  RECTL  rect ;
  POINTL pt ;
  SIZEL size ;
  int i, height, width ;
  char blank_line[180] ;

  switch(msg)
  {
  case WM_CREATE:
    d4display_init( &display, hwnd ) ;
    break ;
  case WM_PAINT:
    if ( display.display_set == 0 )  /* not a CodeBase message, must be a general system message... */
    {
      hps = WinBeginPaint(hwnd, NULLHANDLE, &rclUpdate) ;
      WinFillRect(hps, &rclUpdate, SYSCLR_WINDOW) ;
      WinEndPaint(hps) ;
    }
    else
    {
      if ( display.do_box == 1 )  /* message box instead of regular output */
      {
        display.do_box = 0 ;
        WinMessageBox(HWND_DESKTOP, display.hWnd, display.str, "CB 5.0 OS/2 TEST", MSGBOXID, MB_OK ) ;
      }
      else
      {
        hps = WinBeginPaint( hwnd, (HPS)NULL, &rect );
        GpiSetColor( hps, CLR_NEUTRAL );         /* set text color */
        GpiSetBackColor( hps, CLR_BACKGROUND );  /* set text background */
        GpiSetBackMix( hps, BM_OVERPAINT );      /* set text mix */
        WinQueryWindowRect(hwnd,&rect);            /* get window bounds */

        memset(blank_line, 32, 179) ;
        blank_line[179] = '\0' ;

        GpiQueryDefCharBox( hps, &size ) ;
        height = size.cy + 1 ;
        width = size.cx ;

        if ( display.is_new_line )
        {
          display.x = 0 ;
          display.y += height + display.tm.lExternalLeading ;
        }

        if ( (display.y+height) > rect.yTop )
        {
          display.y = 0 ;
          WinInvalidateRect( display.hWnd, &rect, 1 ) ;
        }

        pt.x = display.x ;
        pt.y = rect.yTop - display.y ;
        GpiCharStringAt( hps, &pt, display.len, display.str ) ;

        if ( (display.y+(2*height)) > rect.yTop )
        {
          pt.x = 0 ;
          pt.y = rect.yTop ;
          GpiCharStringAt( hps, &pt, strlen( blank_line ), blank_line ) ;
        }
        else
        {
          pt.x = 0 ;
          pt.y = rect.yTop - ( display.y + height + display.tm.lExternalLeading ) ;
          GpiCharStringAt( hps, &pt, strlen( blank_line ), blank_line ) ;
        }

        WinEndPaint( hps );
        display.x += width ;
      }
      display.display_set = 0 ;
    }
    break ;
  case WM_COMMAND:
    MainCommand(hwnd, mp1, mp2);
    break ;
  default:
    return(WinDefWindowProc(hwnd, msg, mp1, mp2)) ;
  }
  return (MRESULT)0 ;
}

BOOL Init(VOID)
{
  if(DosExitList(EXLST_ADD, (PFNEXITLIST)ExitProc))
    DosExit(EXIT_PROCESS, 1) ;
  if(0 == WinLoadString(hab, (HMODULE)0, IDS_APPNAME, MAXNAMEL, szAppName))
    return 0 ;
  if(!WinLoadString(hab, (HMODULE)0, IDS_UNTITLED, MESSAGELEN, szUntitled))
    return 0 ;
  if(!WinRegisterClass( hab, (PSZ)szAppName, (PFNWP)MainWndProc, CS_SIZEREDRAW, 0L))
    return 0 ;
  return 1 ;
}

main( int argc, char **argv )
{
  QMSG qmsg ;
  ULONG flCtlData ;
  CODE4 cb ;

  d4init( &cb ) ;

  hab = WinInitialize(0) ;
  if ( hab == NULLHANDLE )
    return 1 ;

  hmq = WinCreateMsgQueue(hab, 0) ;

  if ( hmq == NULLHANDLE )
  {
    WinTerminate(hab) ;
    return 1 ;
  }

  if(!Init())
    return 1 ;

  flCtlData = FCF_STANDARD & ~FCF_SHELLPOSITION ;
  hwndMainFrame = WinCreateStdWindow( HWND_DESKTOP, 0, &flCtlData, (PSZ)szAppName, "", 0, (HMODULE)NULL, IDR_MAIN, (PHWND)&hwndMain) ;
  if ( hwndMainFrame == NULLHANDLE )
    return 1 ;
  WinSetWindowText(hwndMainFrame, "CodeBase 5.0 Test");
  if (!WinSetWindowPos( hwndMainFrame,
                       HWND_TOP,
                       20, 100, 600, 300,
                       SWP_SIZE | SWP_MOVE | SWP_ACTIVATE | SWP_SHOW
                       ))
    d4parsestring_init( &display.parse_str, argc, argv ) ;
  while(WinGetMsg(hmq, (PQMSG)&qmsg, (HWND)NULL, (ULONG)NULL, (ULONG)NULL))
    WinDispatchMsg(hmq, (PQMSG)&qmsg) ;
  return 0 ;
}
#endif
#else
int PASCAL WinMain(HANDLE, HANDLE, LPSTR, int);
BOOL InitApplication(HANDLE);
BOOL InitInstance(HANDLE, int);

LRESULT CALLBACK MainWndProc(HWND, UINT, UINT, LONG);


int PASCAL WinMain( HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpCmdLine,
                   int nCmdShow)
{
  if (!hPrevInstance)
    if (!InitApplication(hInstance))
      return (FALSE);

  if (!InitInstance(hInstance, nCmdShow))
    return (FALSE);

  d4parsestring_init( &display.parse_str, lpCmdLine ) ;

  t4test( &display ) ;

  PostQuitMessage(0) ;

  for (;;)
  {
    if (d4display_quit( &display ) )
      return (display.msg.wParam) ;
  }
}

BOOL InitApplication(HANDLE hInstance)
{
  WNDCLASS  wc;

  wc.style = (UINT) NULL;
  wc.lpfnWndProc = MainWndProc;

  wc.cbClsExtra = 0;
  wc.cbWndExtra = 0;
  wc.hInstance = hInstance;
  wc.hIcon = LoadIcon( (HINSTANCE) NULL, IDI_APPLICATION);
  wc.hCursor = LoadCursor( (HINSTANCE) NULL, IDC_ARROW);
  wc.hbrBackground = GetStockObject(WHITE_BRUSH);
  wc.lpszMenuName =  "TestMenu";
  wc.lpszClassName = "TestWClass";

  return (RegisterClass(&wc));
}


BOOL InitInstance( HANDLE hInstance, int nCmdShow)
{
  HWND   hWnd;

  hWnd = CreateWindow(
    "TestWClass",
    "Test CodeBase",
    WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    CW_USEDEFAULT,
    (HWND) NULL,
    (HMENU) NULL,
    hInstance,
    NULL
    );

  if ( !hWnd )
    return (FALSE);

  SetTimer( hWnd, (UINT) hWnd, 1000, (TIMERPROC) NULL ) ;

  d4display_init( &display, hWnd ) ;
  display.hInst = hInstance ;     /* for t4filter.c */

  ShowWindow(hWnd, nCmdShow);
  UpdateWindow(hWnd);
  return (TRUE);
}

LRESULT CALLBACK MainWndProc( HWND hWnd, UINT message, UINT wParam,
                             LONG lParam)
{
  switch (message)
  {
  case WM_DESTROY:
    PostQuitMessage(0);
    break;

  case WM_CLOSE:
    display.did_close =  1 ;
    return (DefWindowProc(hWnd, message, wParam, lParam));

  default:
    return (DefWindowProc(hWnd, message, wParam, lParam));
  }
  return (LONG) NULL ;
}
#endif


