/************************************************************

	SampleTool.cp
	
	Copyright © 2001 Nemetschek North America
	All Rights Reserved.
************************************************************/

#include <VWSDK.h>
#include "GSString.h"
#if GS_WIN || GS_MAC_MACHO
extern "C" long GS_EXTERNAL_ENTRY plugin_main(long action, long message1, long /*message2*/, long &/*userData*/, CallBackPtr cbp)
#elif GS_MAC_CFM
extern "C" long GS_EXTERNAL_ENTRY main(long action, long message1, long /*message2*/, long &/*userData*/, CallBackPtr cbp)
#endif
{
	GSExternalSetup(cbp);

	long returnVal = 0L;
	
	switch (action) {
		case kToolDisplayContextualHelp:{
			
			TXString* helpIdentifier = (TXString*) message1;
			if (helpIdentifier) {
				*helpIdentifier = "com.yourcompany.help.tool.UNIQUENUMBERHERE";
				return kToolEventHandled;
			}
			break;
		}
		case kToolInitGlobals : {
		
			//	The workspace that contains this tool is being opened.  This action is called
			//	so the tool can allocate and initialize any persistent data.
			//
			//	Do not allocate data using operator new.  On Windows this memory is disposed when the DLL is unloaded between calls
			//	Use GS_NewPtr(CALLBACKPTR, size_t size) and GS_DisposePtr(CALLBACKPTR, void* p) to allocate
			//	and destroy persistent data.

			/* Example:

				void* pRawMemory = GS_NewPtr(gCBP, sizeof(FooClass));
				if (pRawMemory) {
					FooClass* pConstructedMemory = (FooClass*) new(pRawMemory) FooClass();	
					userData = (long) pConstructedMemory;
				}
				else {
					userData = nil;
				}
	
			*/

			break;
		}
		case kToolDisposeGlobals : {
			
			//	The workspace that contains this tool is being closed.  This action is 
			//	called so the tool can free any allocated memory.

			/* Example:

				if (userData) {
					FooClass* pData = (FooClass*) userData;
					pData->~FooClass();
					GS_DisposePtr(gCBP, pData);
					userData = nil;
				}
	
			*/
				
			break;
		}
		case kToolDoSetup : {
		
			//	This tool is being made the active tool.
			//	Check the message1 parameter for setUp context.  If message1 == kSetupSetdownPauseRestoreFlag then the
			//	tool is being restored from a temporary pause  If message1 == 0 then the tool is being initially
			//	set up. 
			break;
		}
		case kToolDoSetDown : {
	
			//	The tool is being deactivated. VectorWorks is switching the active tool.
			//	Check the message1 parameter for setUp context.  If message1 == kSetupSetdownPauseRestoreFlag then the
			//	tool is being temporarily paused.  If message1 == 0 then the tool is being set down for good.
			break;
		}
		case kToolGetCursor : {

			//	This action is called by the framework to get the cursor for the tool.
			 
			MCObjectHandle overObject = nil; 
			short overPart;
			long code;
			
			GS_TrackTool(cbp, overObject, overPart, code);
			if (overObject)
				returnVal = 147;
			else
				returnVal = 103;
			break;
		}
		case kToolGetStatus: {
			
			//	This action is called so the Tool can return to the system the current state 
			//	of the tool.  Return the result of one of the following callbacks:
			//return	GS_OnePointToolStatus(cbp);			// 	The tool is a one point tool.
			//return		GS_TwoPointToolStatus(cbp);		// 	The tool is a two point tool.
			//return		GS_ThreePointToolStatus(cbp);	// 	The tool is a three point tool.
			returnVal = GS_PolyToolStatus(cbp);				// 	The tool is a multi - point tool.
			
			//	A tool can also return one of the following constants:
			//	kToolCompleted, kToolCollectingPoints, or kToolWaitingForFirstPoint.
			
			break;
		}
		case kToolPointAdded: {
			
			//	This action is called when the tool gets a mouse down in the content of 
			//	the drawing.  This action is also called for a click drag mouse up.  A tool point
			//	has just been added.
			
			break;
		}
		case kToolPointRemoved: {
			
			//	This action is called whenever the tool framework removes a tool point for 
			//	any reason.  Ue this action to clean up any state that becomes stale with 
			//	the removal of a tool point.
			break;
		}		
		case kToolMouseMove: {
		
			//	This action is called when the tool gets a mouse move in the content of 
			//	the drawing.  If the tool has the screenBasedCursor flag set it gets 
			//	kToolMouseMove action calls whenever the mouse moves a pixel.  If the tool 
			//	does not set the screenBasedCursor flag, it gets called whenever the 
			//	smartcursor snaps to a new point.
			break;
		}
		case kToolDraw: {
			//	This action is called when the tool needs to draw.  The framework calls this draw 
			//	action handling expecting the tool to draw itself.  Tools use the GS_GetToolPt## 
			//	callbacks to determine the current state of the tool and draw itself appropriately.
			
			GS_Default2DToolDraw(cbp);
			break;
		}
		case kToolHandleComplete: {
		
			//	This action is called when the tool has signified, through a kToolGetStatus 
			//	return value, that the tool is complete.  This is where the tool makes changes 
			//	to the drawing.  kToolHandleComplete action handling is the most likely 
			//	location for UNDO callbacks.
			
			break;
		}
		case kToolDoModeEvent : {
		
			//	This action is called when the user invokes a mode bar event.
			
			break;
		}
		case kToolDrawingDoubleClick: {
		
			//	This action is called whenever the user double-clicks in the content of the 
			//	drawing window with more two or more Tool points.
			//
			//	Here we return the GS_PolyToolStatus result.  See kToolGetStatus above.
			if (GS_GetNumToolPts(cbp) == 1)
				returnVal = kToolSwitchToCursor;
			else
				returnVal = kToolCompleted;
			break;
		}
		case kToolDoDoubleClick : {
		
			//	This action is called when the user double-clicks on the tool icon in a 
			//	tool palette.  This is not the same as kToolDrawingDoubleClick above.
			break;
		}
	}

	GSExternalSetdown (0L);

	return returnVal;
}

