/*
 ******************************************************************************
 *  Copyright 1998 DeltaLink bv. All Rights Reserved.
 ******************************************************************************
 *
 * File: sapi_win.c
 *
 * This module contains the winmain function for the sapi driver.
 *
 */
           		
#define _OLE2_H_          			/* no ole */
#pragma warning( disable : 4121 )

#include  <stdio.h>
#include  <windows.h>
#include  <process.h>
#include  <signal.h>

#include  <protocol.h>

int fl_main( void *);

#pragma warning( disable : 4115 )
#include  <sapi_s7.h>								/*  Siemens SAPI definitions */
#pragma warning( default : 4115 )

int sapi_dispatch ( ord32  );

//#ifdef							ERROR
//#define							WINERROR     ERROR
//#undef							ERROR
//#endif

//#ifdef							ERROR
//#define							FLERROR     ERROR
//#undef							ERROR
//#endif

#include						"sw.h"
#include						"resource.h"

#define							XBUFSIZE 1024

// client controls
#define							IDC_TOOLBAR   10
#define							IDC_STATUSBAR 11
#define							IDC_CLIENT    20


HINSTANCE						hInst, hPrevInst;					// hInstance of application
HWND								hwframe;									// frame window handle
HWND								hwmodeless;								// modeless dialog window handle
HWND								Hwnd;											// hwnd handle for the callback changed from local to global
HACCEL							hAccTable;							
																						
int									debug;


int PASCAL          WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
BOOL                AppInit(HINSTANCE, HINSTANCE, LPSTR, int);
LRESULT CALLBACK    SDIFRAME1WndProc(HWND, UINT, WPARAM, LPARAM);


int PASCAL WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                                 LPSTR lpszCmdLine, int cmdShow)
{
	MSG  msg;

	if(!(AppInit(hInstance, hPrevInstance, lpszCmdLine, cmdShow)))
		return 0;
	
	if ( _beginthread(  fl_main,
											0, 
											NULL) == -1 )
		return 0;

	while ( GetMessage(&msg, NULL, 0, 0 ) )
	{
		if ( hwmodeless && IsDialogMessage(hwmodeless, &msg ) )
										;
		else 
			if ( !TranslateAccelerator(hwframe, hAccTable, &msg ) ) 
			{
				TranslateMessage ( &msg );
				DispatchMessage ( &msg );
			}
	}

  return ( msg.wParam );
}


#pragma warning(disable:4100)
BOOL AppInit ( HINSTANCE hInstance, HINSTANCE hPrevInstance,
                                 LPSTR lpszCmdLine, int cmdShow)
{
	WNDCLASS wClass;  


  hInst = hInstance;  
	hPrevInst = hPrevInstance;

  if(!hPrevInstance)
	{

    wClass.style         = CS_HREDRAW | CS_VREDRAW;
    wClass.lpfnWndProc   = (WNDPROC)SDIFRAME1WndProc;
    wClass.cbClsExtra    = 12;
    wClass.cbWndExtra    = 12;
    wClass.hInstance     = hInstance;
    wClass.hIcon         = LoadIcon(hInst, MAKEINTRESOURCE(IDI_ICON1));
    wClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wClass.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wClass.lpszClassName = "SDIFRAME1";
    wClass.lpszMenuName  = NULL;

		// register windows here
    if(!(RegisterClass(&wClass)))  
			return FALSE;
	}

  hAccTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(0));

  Hwnd = CreateWindow("SDIFRAME1",						 // window class name
                      "SDI Frame Application", // window title
                      WS_OVERLAPPEDWINDOW,     // window style
                      100, 50, 300, 150,       // left,top,width,height
                      NULL,                    // no parent for this window
                      NULL,                    // use the class menu
                      hInstance,               // who created this window
                      NULL                     // no parms to pass on
                     ); 
    

  UpdateWindow( Hwnd );

  return TRUE;
}
#pragma warning(default:4100)

/*-----------------------------------------------------------------------------
 * FUNCTION: SDIFRAME1WndProc
 *
 * PURPOSE: This is the program's window procedure which receives the events
 *          generated by the SAPI library. This routine checks if it is an SAPI
 *          or Windows event and calls the right function.
 *
 -----------------------------------------------------------------------------*/

LRESULT CALLBACK SDIFRAME1WndProc( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam )
{

  /*
   * check if this is a SAPI event
   */
	if ( ( message >= WM_USER ) && ( message < ( WM_USER + 600 ) ))
	{
    /* 
     * call the dispatch function to handle the event. The event received is built
     * up in <board id><cp decriptor>.
     */

/*
		sapi_dispatch ( ( message - WM_USER ) / 100, ( message - WM_USER ) % 100 );
*/
    
		sapi_dispatch ( message - WM_USER  );
  }
	else
		return DefWindowProc( hwnd, message, wParam, lParam );

  return 0L ;
}

/*-----------------------------------------------------------------------------
 * FUNCTION: int win_set_callback
 *
 * PURPOSE:
 *
 -----------------------------------------------------------------------------*/

int win_set_callback (	ord32 cp_descr )
{
	return s7_set_window_handle_msg (	cp_descr,
																		Hwnd,
																		WM_USER + cp_descr );
}

/*-----------------------------------------------------------------------------
 * FUNCTION: Pointer_up
 *
 * PURPOSE:  This function adds a cyclic read operation to the queue of the cyclic
 *          
 *
 -----------------------------------------------------------------------------*/

