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

	PurgeMenu.cpp
	
	This implements the "Purge..." menu command.
	It creates a menu item that deletes one or all
	of the following unused objects from a drawing:
	
		Symbol Definitions
		Layers
		Record Formats
		Classes
		Textures
		Hatches
		Gradients
		Image Fills
		Sketch Styles
		Render Background Images

	
	Copyright © Diehl Graphsoft, Inc  2000
	All Rights Reserved.

	JAK 4/12/00		Fix bug which corrupted Plug-in Objects which have more than one recordNode on their auxlists.
	SWF 4/21/00		Update to use the layout manager
	DLD 1/10/2001 	Don't purge texture record formats as part of format purging.
					XXX: My God, this code is densely packed, formatting-wise!  
						It's like the editor removed all line feeds!
						
	SAJ	12.10.01	Moved logic for determining special formats to the main Application.
					

	Bruce			Add new ones.

	JAK 5/6/02		Move the Class test below the SymDef test to fix bug 17243.

	SAJ	5.13.02		Remove unfinished logic for determining special formats to the main Application. 
	
	Drew 9/29/04 	Added in the Sketch Styles option.

	CJG 7/19/06		Completed support for Wall Styles.
						
************************************************************/

#include <VWSDK.h>
#include "TArray.h"
#include "PurgeDialog.h"
#include <iostream>


DEFINE_NOPOP_TLARGEARRAY(long);
DEFINE_NOPOP_TARRAY(Handle);


#if 0
static void AddTextureFormatsToRecordFormatTable(CallBackPtr cbp, Handle header, THandleArray* formatTable)
// We don't want to delete record formats used by texture resources,
// so we add them to the format table.  -DLD 1/10/2001
{
	if (header && cbp && formatTable) {

		// Traverse the header for all the texture resources
			
		// Get data node off of header, where textures reside
		Handle dataNode = GS_FindAuxObject(cbp, header, kDataNode);

		if (dataNode) {
		
			// Look for material resources
			Handle dataSubObj = GS_FirstMemberObj(cbp, dataNode);
			
			while (dataSubObj) {
			
				if (GS_GetObjectType(cbp, dataSubObj) == kMaterialNode) {
				
					// Found a material resource
				
					// Look for record nodes attached to material, add their
					// formats to the table
					Handle materialSubObj = GS_FindAuxObject(cbp, dataSubObj, kRecordNode);
					
					while (materialSubObj) {
						
						if (GS_GetObjectType(cbp, materialSubObj) == kRecordNode) {
							
							// Found a record node
							Handle format = GS_GetDefinition(cbp, materialSubObj);
							if (format && !FoundInTable(format, formatTable))
								formatTable->PushData(format);
						}

						materialSubObj = GS_NextObject(cbp, materialSubObj);
					}
				}
				
				dataSubObj = GS_NextObject(cbp, dataSubObj);
			}
		}		
	}
}
#endif

//===================================================================================

// Because we include MenuMain.cpp in the project, the low level setup operations
// are performed on our behalf by SDK code. The CallBackPtr is already stored in gCBP
// and can be used throughout this external

// See MiniCADHookIntf.h for a description

long MenuMain(long inAction, long message, long &ioUserData)
{
	switch (inAction) {
		case kMenuItemDisplayContextualHelp: {
			if (GS_DisplayContextualHelp(gCBP, "net.nemetschek.help.9BF6234A-C643-487F-8B51-D3B23F2F3691")) {
				return kMenuItemHasContextualHelp;
			}
			break;
		}
			
		case kMenuDoInterface: {
			PurgeOptionsType	purgeOptions = *((PurgeOptionsType *) &ioUserData);

			Boolean		texturesEnabled = false;
			GS_GetProgramVariable(gCBP, varRenderWorksEnabled, (void*) &texturesEnabled);
			
			if (PurgeDialog(gCBP, purgeOptions, texturesEnabled == true)) {

//				if (GS_NonUndoableActionOK(gCBP)) {
//					GS_SetUndoMethod(gCBP, kUndoNone);
//					DoPurgeObjects(gCBP, purgeOptions);
//				}
			}

			break;
		}
	}

	return 0L;
}


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