/* $Header$ */
#include <extcomp.he>
#include <extfval.he>
#include <hwnd.he>
#include <gdi.he>
#include "generic.he"

// Generic is a framework you can use to develop OMNIS external components.
// It does not support any extra calls. See other generic samples for extra support,
// such as properties, functions, events etc. 
//
// When this generic object is added to a window in window design mode, it will have
// only standard properties that apply to all window objects. e.g. left,top etc.

// Your generic object constructor
tqfGenericObject::tqfGenericObject( HWND pFieldHWnd )
{
	mHWnd = pFieldHWnd;	// we remember the objects child window for later drawing 
}

// Generic destruction
tqfGenericObject::~tqfGenericObject()
{
	// Insert any memory deletion code or general cleanup code
}

// You need to paint your control
qbool tqfGenericObject::paint()
{
	WNDpaintStruct paintStruct;
	WNDbeginPaint( mHWnd, &paintStruct );
	
	// You can paint your control in here
	// Use the paintStruct.hdc as the device to paint into
	// See generic2 for a simple paint example

	// If in design mode, then call drawNumber & drawMultiKnobs to draw design
	// numbers and multiknobs, if required.
	if ( ECOisDesign(mHWnd) )
	{
		ECOdrawNumber(mHWnd,paintStruct.hdc);
		ECOdrawMultiKnobs(mHWnd,paintStruct.hdc);
	}
	
	WNDendPaint( mHWnd, &paintStruct );	
	return qtrue;
}


// Component library entry point (name as declared in resource 31000 )
extern "C" LRESULT OMNISWNDPROC GenericWndProc(HWND hwnd, UINT Msg,WPARAM wParam,LPARAM lParam,EXTCompInfo* eci)
{
	 // Initialize callback tables - THIS MUST BE DONE 
   ECOsetupCallbacks(hwnd, eci);		
	 switch (Msg)
	 {
			// WM_PAINT - standard paint message
			case WM_PAINT:
			{
				// First find the object in the libraries chain of objects
				tqfGenericObject* object = (tqfGenericObject*)ECOfindObject( eci, hwnd );
				// and if its good, call the paint function
				if ( NULL!=object && object->paint() )
					return qtrue;
				break;
			}  
			
			// ECM_OBJCONSTRUCT - this is a message to create a new object.
			case ECM_OBJCONSTRUCT:				
			{
				// Allocate a new object
				tqfGenericObject* object = new tqfGenericObject( hwnd );
				// and insert into a chain of objects. The OMNIS library will maintain this chain
				ECOinsertObject( eci, hwnd, (void*)object );
				return qtrue;
			}    
			
			// ECM_OBJDESTRUCT - this is a message to inform you to delete the object
			case ECM_OBJDESTRUCT:					
			{
				// First find the object in the libraries chain of objects, 
				// this call if ok also removes the object from the chain.
				tqfGenericObject* object = (tqfGenericObject*)ECOremoveObject( eci, hwnd );
				if ( NULL!=object )
				{
					// Now you can delete the object you previous allocated
					// Note: The hwnd passed on ECM_OBJCONSTRUCT should not be deleted, as
					// it was created and will be destroyed by OMNIS
					delete object;
				}
				return qtrue;
			}
	 		
	 		// ECM_CONNECT - this message is sent once per OMNIS session and should not be confused 
	 		// with ECM_OBJCONSTRUCT which is sent once per object. ECM_CONNECT can be used if load other libraries
	 		// once or other general global actions that need to be done only once.
	 		//
	 		// For most components this can be removed - see other BLYTH component examples
	 		case ECM_CONNECT:
      {
        return ECOmakeConnectFlags(EXT_FLAG_LOADED); // Return external flags
      } 
      
	 		// ECM_DISCONNECT - this message is sent only once when the OMNIS session is ending and should not be confused
	 		// with ECM_OBJDESTRUCT which is sent once per object. ECM_DISCONNECT can be used to free other libraries
	 		// loaded on ECM_CONNECT or other general global actions that need to be done only once.
	 		// 
	 		// For most components this can be removed - see other BLYTH component examples
      case ECM_DISCONNECT:
      { 
        return qtrue;
      }

      // ECM_GETCOMPLIBINFO - this is sent by OMNIS to find out the name of the library, and
      // the number of components this library supports
      case ECM_GETCOMPLIBINFO:
      {
      	return ECOreturnCompInfo( gInstLib, eci, LIB_RES_NAME, OBJECT_COUNT );
      }

      // ECM_GETCOMPICON - this is sent by OMNIS to get an icon for the OMNIS component store and
      // external component browser. You need to always supply an icon in your resource file.
			case ECM_GETCOMPICON:
			{
				// OMNIS will call you once per component for an icon.
				// GENERIC_ICON is defined in the header and included in the resource file
				if ( eci->mCompId==OBJECT_ID1 ) return ECOreturnIcon( gInstLib, eci, GENERIC_ICON );
				return qfalse;
			}

			// ECM_GETCOMPID - this message is sent by OMNIS to get information about each component in this library
			// wParam is a number from 1 to the number of components return on the ECM_GETCOMPLIBINFO message.
			//
			// For each call you should return the internal object ID and the type of object it is.
			// The object id will be used for other calls such as ECM_GETCOMPICON 
			// 
			// The object type is for example : cObjType_Basic 		- a basic window object or 
			//																	cRepObjType_Basic	- a basic report object.
			// 																	There are others 	- see BLYTH examples and headers
			case ECM_GETCOMPID:
			{
				if ( wParam==1 )
					return ECOreturnCompID( gInstLib, eci, OBJECT_ID1, cObjType_Basic );
				return 0L;
			}
	 }

	 // As a final result this must ALWAYS be called. It handles all other messages that this component
	 // decides to ignore.
	 return WNDdefWindowProc(hwnd,Msg,wParam,lParam,eci);
}

	
