/**************************************************************************
*
*  @@@BUILDINFO@@@ 64debugMenu-2.jsx 2.0.0.54  08-Feb-2007
*  Copyright 2006-2007 Adobe Systems Incorporated
*  All Rights Reserved.
*
* NOTICE:  All information contained herein is, and remains the property of
* Adobe Systems Incorporated  and its suppliers,  if any.  The intellectual 
* and technical concepts contained herein are proprietary to  Adobe Systems 
* Incorporated  and its suppliers  and may be  covered by U.S.  and Foreign 
* Patents,patents in process,and are protected by trade secret or copyright 
* law.  Dissemination of this  information or reproduction of this material
* is strictly  forbidden  unless prior written permission is  obtained from 
* Adobe Systems Incorporated.
**************************************************************************/

//////////////////////////////////////////////////////////////////////////
// The Debug menu.

menus.debug				= new MenuElement ("menu", "$$$/ESToolkit/Menu/Debug=&Debug", 
										   "at the end of menubar", "debug");
menus.debug.run			= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/Run=&Run",
										   "at the end of debug",  "debug/run");
menus.debug.stop		= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/Stop=&Stop",
										   "at the end of debug",  "debug/stop");
menus.debug.pause		= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/Break=&Break", 
										   "at the end of debug", "debug/break");
menus.debug.into		= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/StepInto=Step &Into",
										   "at the end of debug",  "debug/into");
menus.debug.over		= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/StepOver=Step &Over",
										   "at the end of debug",  "debug/over");
menus.debug.out			= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/StepOut=Step Ou&t",
										   "at the end of debug",  "debug/out");
menus.debug.bptoggle	= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/ToggleBP=To&ggle Breakpoint",
										   "--at the end of debug",  "debug/bptoggle");
menus.debug.bpclearall	= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/ClearAllBP=&Clear All Breakpoints",
										   "at the end of debug",  "debug/bpclearall");
menus.debug.noErrors	= new MenuElement ("command", "$$$/ESToolkit/Menu/Debug/DontBreakOnErrors=Do Not Break on Guarded &Exceptions", 
											"--at the end of debug", "debug/noerrors");

//											
// handle Debugger broadcasts
// /registered in 72debugger.jsx)
//
menus.debug.onNotify = function( reason, param01, param02, param03 )											
{
    switch( reason )
    {
        case 'shutdown':
            globalBroadcaster.unregisterClient( this );
            break;
            
        case 'state':   
            this.reflectState(); 
            break;
    }
}

// Disable the Debug menu altogether

menus.debug.disable = function()
{
	this.run.enabled		=
	this.into.enabled		=
	this.over.enabled		=
	this.out.enabled		=
	this.bptoggle.enabled	=
	this.bpclearall.enabled	=
	this.stop.enabled		=
	this.pause.enabled		= 
	this.noErrors.enabled   = false;
	
    if( document && document.window == workspace.activeDocument )
        document.window.updateDebugButtonStates( true );
}

// Set the state of the debugging options according to the document settings

menus.debug.reflectState = function()
{
    if( document && document.window == workspace.activeDocument && ( !targetMgr.getConnected( document.getCurrentTargetName() ) || document.canDebug() ) )
    {
		var running = false;
		var stopped = true;
		
		var dbg = Debugger.find( document.getCurrentTargetName(), document.getCurrentEngineName() );
		
		if( dbg )
		{
		    running = ( dbg.state == Debugger.RUNNING );
		    stopped = ( dbg.state == Debugger.STOPPED );
		}

		switch (document.status)
		{
			case "noexec":
				// not executable
				this.disable();
				break;

			case "exec":
				// standard executable
				this.run.enabled		=
				this.into.enabled		=
				this.over.enabled		= !running || stopped;
				this.out.enabled		= stopped;
				this.bptoggle.enabled	=
				this.bpclearall.enabled	= true;
				this.stop.enabled		= running || stopped;
				this.pause.enabled		= running;
				break;

			case "dynamic":
				// dynamic script, not saveable
				this.run.enabled		=
				this.into.enabled		=
				this.over.enabled		=
				this.out.enabled		=
				this.bptoggle.enabled	=
				this.bpclearall.enabled	=
				this.stop.enabled		= stopped;
				this.pause.enabled		= false;
				break;
		}
		
		document.window.updateDebugButtonStates();
		this.noErrors.enabled = !running;
    }
	else
	{
	    this.disable();
	}
}

menus.debug.run.onSelect = function()
{
	if( currentDebugger.state == Debugger.STOPPED )
		currentDebugger.doContinue( document );
	else if( document )
		currentDebugger.start( document, 1, true );
}

menus.debug.into.onSelect = function()
{
	if( currentDebugger.state == Debugger.STOPPED )
		currentDebugger.execute( 'StepInto' );
	else if( document )
		currentDebugger.start( document, 2, true );
}

menus.debug.over.onSelect = function()
{
	if( currentDebugger.state == Debugger.STOPPED )
		currentDebugger.execute( 'StepOver' );
	else if( document )
		currentDebugger.start( document, 2, true );
}

menus.debug.out.onSelect = function()
{
	if( currentDebugger.state == Debugger.STOPPED )
		currentDebugger.execute( 'StepOut' );
}

menus.debug.pause.onSelect = function()
{
	if( currentDebugger.isActive() )
	{
		currentDebugger.execute( 'Stop' );
		menus.debug.reflectState();
	}
}

menus.debug.stop.onSelect = function()
{
	if (currentDebugger.isActive() )
	{
	    currentDebugger.stop();
		menus.debug.reflectState();
	}
}

menus.debug.bptoggle.onDisplay = 
menus.debug.bpclearall.onDisplay = function()
{
	this.enabled = (document && (document.window == workspace.activeDocument));
}

menus.debug.bptoggle.onSelect = function()
{
	var line = document.getSelection()[0];
	document.toggleBreakpoint (line);
}

menus.debug.bpclearall.onSelect = function()
{
	document.removeAllBreakpoints();
}

menus.debug.noErrors.onDisplay = function()
{
	this.checked = PrefUtils.getValue( 'prefs.debug.dontBreakOnErrors', 'Boolean' );
}

menus.debug.noErrors.onSelect = function()
{
	prefs.debug.dontBreakOnErrors = !PrefUtils.getValue( 'prefs.debug.dontBreakOnErrors', 'Boolean' );
}

//////////////////////////////////////////////////////////////////////////
// The Profile menu.
// Profile display mode: 0 - nothing, 1 - hits, 2 - time
// Profiling level:
// 0 - no profiling (default)
// 1 - function level profiling
// 2 - function level profiling with timing information
// 3 - line level profiling
// 4 - line level profiling with timing information.

menus.profile			= new MenuElement ("menu", "$$$/ESToolkit/Menu/Profile=&Profile",
										   "after debug", "profile");
menus.profile.off		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Off=&Off",
										   "at the end of profile", "profile/off");
menus.profile.functions	= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Functions=&Functions",
										   "at the end of profile", "profile/functions");
menus.profile.lines		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Lines=&Lines",
										   "at the end of profile", "profile/lines");
//menus.profile.timing	= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Timing=&Add Timing Info",
//										   "----at the end of profile", "profile/timing");
menus.profile.none		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/None=&No Profile Data",
										   "----at the end of profile", "profile/none");
menus.profile.hits		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Hits=Show &Hit Count",
										   "at the end of profile", "profile/hits");
menus.profile.time		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Time=Show &Timing",
										   "at the end of profile", "profile/time");
menus.profile.clear		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/Clear=&Clear Profile Data",
										   "----at the end of profile", "profile/clear");
menus.profile.save		= new MenuElement ("command", "$$$/ESToolkit/Menu/Profile/SaveAs=Save Profile Data &As...",
										   "----at the end of profile", "profile/save");

menus.profile.off.onDisplay = function()
{
	this.checked = PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' ) == 0;
}

menus.profile.off.onSelect = function()
{
	menus.profile.setLevel (0);
}

menus.profile.functions.onDisplay = function()
{
    var level = PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' );
	this.checked = (level == 1 || level == 2);
}

menus.profile.functions.onSelect = function()
{
	//menus.profile.setLevel( ( PrefUtils.getValue( 'prefs.profiling.profileTiming', 'Boolean' ) ? 2 : 1 ) );
	menus.profile.setLevel(2);
}

menus.profile.lines.onDisplay = function()
{
    var level = PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' );
	this.checked = (level == 3 || level == 4);
}

menus.profile.lines.onSelect = function()
{
	//menus.profile.setLevel( ( PrefUtils.getValue( 'prefs.profiling.profileTiming', 'Boolean' ) ? 4 : 3 ) );
	menus.profile.setLevel(4);
}

//menus.profile.timing.onDisplay = function()
//{
//    var level = PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' );
//	this.enabled = (level != 0);
//	this.checked = (level == 2 || level == 4);
//}

//menus.profile.timing.onSelect = function()
//{
//    var level           = PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' );
//    var profileTiming   = !PrefUtils.getValue( 'prefs.profiling.profileTiming', 'Boolean' );
//    
//    prefs.profiling.profileTiming = profileTiming;
//    
//	if( level > 0 )
//	{
//		if( profileTiming )
//			menus.profile.setLevel( level == 1 ? 2 : 4 );
//		else
//		{
//			menus.profile.setLevel( level == 2 ? 1 : 3 );
//			
//			//
//			// reset display mode to Hit Count if Times was selected
//			//
//			if (PrefUtils.getValue( 'prefs.profiling.profDisplayMode', 'Number' ) == 2)
//				prefs.profiling.profDisplayMode = 1;
//		}
//	}
//}

menus.profile.none.onDisplay = function()
{
	this.enabled = (PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' ) != 0 && (!document || !document.composing));
	this.checked = (PrefUtils.getValue( 'prefs.profiling.profDisplayMode', 'Number' ) == 0);
}

menus.profile.none.onSelect = function()
{
	prefs.profiling.profDisplayMode = 0;
	Document.updateProfData();
}

menus.profile.hits.onDisplay = function()
{
	this.enabled = (PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' ) != 0 && (!document || !document.composing));
	this.checked = (PrefUtils.getValue( 'prefs.profiling.profDisplayMode', 'Number' ) == 1);
}

menus.profile.hits.onSelect = function()
{
	prefs.profiling.profDisplayMode = 1;
	Document.updateProfData();
}

menus.profile.time.onDisplay = function()
{
//	this.enabled = (PrefUtils.getValue( 'prefs.profiling.profileTiming', 'Boolean' ) && (!document || !document.composing));
	this.enabled = (PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' ) > 0 ) && (!document || !document.composing);
	this.checked = (PrefUtils.getValue( 'prefs.profiling.profDisplayMode', 'Number' ) == 2);
}

menus.profile.time.onSelect = function()
{
	prefs.profiling.profDisplayMode = 2;
	Document.updateProfData();
}

menus.profile.clear.onDisplay = function()
{
	this.enabled = (!document || !document.composing);
}

menus.profile.clear.onSelect = function()
{
	Document.clearProfileData();
}

menus.profile.save.onDisplay = function()
{
	this.enabled = (PrefUtils.getValue( 'prefs.profiling.profileLevel', 'Number' ) > 0);
}

menus.profile.save.onSelect = function()
{
	Document.saveProfData();
}

menus.profile.setLevel = function (level)
{
    if( PrefUtils.getValue( 'prefs.profiling.profDisplayMode', 'Number' ) > 0 )
        Document.setProfiling(level);
	
	prefs.profiling.profileLevel = level;
	
	if( currentDebugger.state == Debugger.STOPPED )
		currentDebugger.profileLevel = level;
}
