//
//
//	SampleLayout.cpp
//	
//	Copyright © 2000 Nemetschek North America 
//	All Rights Reserved.
//
//	This is sample code to demonstrate the cross-platform user interface
//	features of the Layout Manager.
//

#if _WINDOWS
#include <Windows.h>
#include "GSWinSDK.h"	// Graphsoft Required.
#else
#include "StdHeaders.h"
#endif

#include <MiniCadCallBacks.h>
#include <MiniCadExternals.h>
//#include "CustomObjects.h"

#include <GSString.h>
#include <RecordHandler.h>





//===========================================
// this is a snippet from another source file of the Plug-in Object....
#include <MiniCadHookIntf.h>
#include <MCCoordTypes.h>
#include <MCVector.h>
#include <MCMatrix.h>

static void SampleLayoutHandleEvent(long dlogID, long& itemHit, void* data, void* env);
bool DoSampleLayoutDialog();



//////////////////////////CALL LAYOUTMANAGER DIALOG BOX/////////////////////////////////

struct SampleData
{
	long            dlogID;        // Layout manage dialog required
	double			scaleAngle;
	double			scaleHeight;
};


// dialog constants must match the constants used in VS layout code (in the TEXT rsrc)
const short kAngleHeightDlog	= 129;
const short kOk					= 1;
const short kCancel				= 2;
const short kHeightEdit			= 8;
const short kAngleEdit			= 9;




//-----------------------------------------------------------------------------------------
void SampleLayoutHandleEvent(long dlogID, long& itemHit, void* message, void* env)
//
// Function for handling events.  
// This fuction is pased in to the RunLayoutDialog routine.
// This function will be called by the Layout Manager to handle events while the dialog is up.
//
{
	SampleData*     myenv = (SampleData*)env;
    MCObjectHandle tempPath  = NULL;

	Str255 sAngle, sHeight;
	double_gs scaleAngle, scaleHeight;

	if (VERIFYPOINTER(kJeff, myenv, sizeof(SampleData))) {
		switch (itemHit) {
				
			case kSetupMessage: {
				//I need to get the initial values for height and angle
				GS_CoordToDimString(gCBP, myenv->scaleHeight, sHeight);
				GS_AngleToStringN(gCBP, myenv->scaleAngle, sAngle); 
				GS_SetItemText(gCBP, dlogID, kHeightEdit, sHeight); 
				GS_SetItemText(gCBP, dlogID, kAngleEdit, sAngle); 
				break;
			}			
			case kSetdownMessage: {
				// dialog is no longer visible
				// last chance to get data out of dialog items
				break;
			}
            case kOk: {
				GS_GetItemText(gCBP, dlogID, kHeightEdit, sHeight);
				GS_GetItemText(gCBP, dlogID, kAngleEdit, sAngle);
				if (!GS_DimStringToCoord(gCBP, sHeight, scaleHeight) || !GS_StringToAngleN(gCBP, sAngle, scaleAngle))
					break;
				myenv->scaleAngle = scaleAngle;
				myenv->scaleHeight = scaleHeight;
				break;				
			}	
			case kCancel: {

				break;
			}
			default :
				break;
		}
	}
}

//-----------------------------------------------------------------------------------------
bool DoSampleLayoutDialog()
//
//	Displays a sample layout manger dialog.
//	The platform independent description of the dialog is in a TEXT resource.
//	The Layout Manager will display a dialog which is appropriate for whatever platform we are running on.
//
{		
	SampleData env;
	double_gs angleValue = 0.0;
	WorldCoord heightValue = 0.0;
	env.scaleAngle = angleValue;
	env.scaleHeight = heightValue;
	// Read in the 'TEXT' resource which describes the dialog.  Creates the dialog. Returns 0 for failure.
	env.dlogID = GS_GetLayoutFromRsrc(gCBP, kAngleHeightDlog);
	// Verify that all items were created on the dialog.
	if (GS_VerifyLayout(gCBP, env.dlogID)) {
		// Display the dialog. Will call the Event handling routine specified.  
		// Returns the id of the button used to close the dialog: 1 for default button, 2 for cancel button.
		long result = GS_CALLBACK GS_RunLayoutDialog(gCBP, env.dlogID, SampleLayoutHandleEvent, (void*) &env);
		if (result == kOk) 
			return true;
	}

	return false;
}



//-----------------------------------------------------------------------------------------
long MenuMain(long action, long message, long &userData)
//
//	Main entry point for parametric object regeneration external code resource.
//	See MiniCadHookIntf.h for constants.
//
{

	message;
	
	long	reply = kParametricNoErr;


	switch (action) {
		case kMenuItemDisplayContextualHelp: {
			TXString* helpIdentifier = (TXString*) message;
			if (helpIdentifier) {
				*helpIdentifier = "com.yourcompany.help.menuitem.????";
				return kMenuHandledEvent;
			}
			break;
		}
			
		case kMenuInitGlobals : {
				//
				//	This action is called when an workspace is activated and serves the 
				//	primary purpose of allowing the plug-in to define any 
				//	defaults and global information and to initialize data space that 
				//	may be stored in userData.
				//

				// Construct explicitly with 'placement new'. Get memory from the app because
				// memory allocated with normal 'new' disappears when the dll unloads.

			/* Example:

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

				break;
			}

		case kMenuDisposeGlobals : {
				//
				//	This action is called when a workspace is deactivated and serves the 
				//	primary purpose of allowing the external function to dispose data 
				//	space that may be stored in userData.
				//

				// Destruct explicitly when using 'placement new'

			/* Example:

				if (userData) {
					FooClass* pData = (FooClass*) userData;
					pData->~FooClass();
					GS_DisposePtr(gCBP, pData);
					userData = nil;
				}
	
			*/
				
				break;
			}

		case kMenuDoInterface: {				
				// ********************** Do the Layout Mgr Dialog HERE *****************
				DoSampleLayoutDialog();

				break;
			}

	}

	return reply;
}

//--------------------------------------------------------------------------------------
long DangerousMenuMain(long /*action*/, long /*message1*/, long & /*userData*/)
{
	// This entry point gets kMenuCheckHilite & kMenuNotify messages, which must be handled very carefully
	return 0L;
}
