/**************************************************************************
*
*  @@@BUILDINFO@@@ 23callstack-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.
**************************************************************************/

callstack = null;

app.createCallStack = function (where)
{
	callstack = new Window( 
		"dockpalette {												\
			text:'$$$/ESToolkit/Panes/Stack/Title=Call Stack',		\
			                                                        \
			properties:                                             \
			{                                                       \
			    defaultVisibility : true,							\
				name:'callstack',									\
				icon : '#PCallStack_N',	                            \
				rollover : '#PCallStack_R',"
				+ where + "                                         \
			},													    \
			                                                        \
			preferredSize: [100, 100],								\
			margins: 2,                                             \
			spacing: 2,                                             \
			                                                        \
			output: ListBox                                         \
			{										                \
				preferredSize: [10, 20],							\
				alignment: ['fill', 'fill' ]                        \
			}				                                        \
            infogrp : Group                                         \
            {                                                       \
                orientation : 'row',                                \
                margins      : 0,                                   \
                spacing      : 0,                                   \
                alignment	: ['fill','bottom'],				    \
                targetField: StaticText                             \
                {								                    \
                    alignment	: ['fill','bottom'],				\
                    characters : 40                                 \
                },													\
                sizeBoxSpacer    : Group                            \
                {                                                   \
                    alignment	: ['right','bottom'],				\
                    preferredSize        : [14,1]                   \
                }                                                   \
            }                                                       \
		}");
	
	callstack.id = "callstack";
	callstack.menuText = localize ("$$$/ESToolkit/Menu/Window/Stack/Title=&Call Stack");

	callstack.onNotify = function( reason )
	{
	    switch( reason )
	    {
	        case 'shutdown':
	            this.output.removeAll();
	            globalBroadcaster.unregisterClient( this );
	            break;
	            
	        case 'changeActiveTarget':
	        case 'changeActiveEngine':
	        {
	            this.enabled = targetMgr.getActiveTarget().getConnected();
	            
	            if( this.enabled )
	            {
	                var targetName = BridgeTalk.getDisplayName( targetMgr.getActiveTarget().targetName )   ;
	                this.infogrp.targetField.text = ( targetName ? targetName : targetMgr.getActiveTarget().targetName );
	            }
	            else
	                this.infogrp.targetField.text = '';
            }
            break;
	    }
	}
	
	globalBroadcaster.registerClient( callstack );
    targetMgr.registerClient( callstack );

	///////////////////////////////////////////////////////////////////////////////
	//
	// methods
	//
	
	//-----------------------------------------------------------------------------
	// 
	// callstack.onResize(...)
	// callstack.onResizeing(...)
	// 
	// Purpose: Handle resizing of palette
	// 
	//-----------------------------------------------------------------------------
	
	callstack.onResize = 
	callstack.onResizing = function ()
	{
		this.layout.resize();
	}
	
	//-----------------------------------------------------------------------------
	// 
	// callstack.erase(...)
	// 
	// Purpose: Erase call stack list.
	// 
	//-----------------------------------------------------------------------------
	
	callstack.erase = function()
	{
		var text = localize ("$$$/ESToolkit/Panes/Stack/NoStack=[no stack]");
		this.output.removeAll();
		
		if( text.length <= 0 ) text = ' ';	// ScriptUI might run into breakpoint with 0-length strings
		this.output.add ("item", text);
	}
	
	//-----------------------------------------------------------------------------
	// 
	// callstack.switchToBottom(...)
	// 
	// Purpose: Select first stack frame of list.
	// 
	//-----------------------------------------------------------------------------
	
	callstack.switchToBottom = function()
	{
		if (currentDebugger.state == Debugger.STOPPED)
			currentDebugger.switchFrame (0);
	}
	
	//-----------------------------------------------------------------------------
	// 
	// callstack.getFrames(...)
	// 
	// Purpose: Get number of stack frames
	// 
	//-----------------------------------------------------------------------------
	
	callstack.getFrames = function()
	{
		return this.output.length;
	}
	
	//-----------------------------------------------------------------------------
	// 
	// callstack.update(...)
	// 
	// Purpose: Update list.
	// 
	//-----------------------------------------------------------------------------
	
	callstack.update = function (stackTrace)
	{
		var frames = stackTrace.split ('\n');
		// if the last line is empty, discard it
		if (frames [frames.length-1].length == 0)
			frames.pop();
		var oldMax = this.output.items.length - 1;
		var newSize = frames.length;
		for (var frame = 0; frame < newSize; frame++)
		{
			var curFrame = frames [frame];
			if (curFrame == "[toplevel]")
				curFrame = localize ("$$$/ESToolkit/Panes/Stack/Toplevel=[Top Level]");
			$.bp (curFrame.length == 0);
			if (oldMax < frame)
			{
				if( curFrame.length <= 0 ) curFrame = ' ';	// ScriptUI might run into breakpoint with 0-length strings
				this.output.add ("item", curFrame);
			}
			else
				this.output.items [frame].text = curFrame;
		}
		// delete excess frames
		while (this.output.items.length > newSize)
			this.output.remove (this.output.items.length - 1);
		// Select the last frame
		this.output.selection = this.output.items.length - 1;
	}
	
    //-----------------------------------------------------------------------------
    // 
    // callstack.output.onChange(...)
    // 
    // Purpose: The callback for the output list box is activated when the user
	//          selects a different stack frame. In this case, the debugger displays
	//          the selected stack frame if it is halted. If not, nothing happens.
    // 
    //-----------------------------------------------------------------------------

	callstack.output.onChange = function()
	{
		if (this.selection && currentDebugger.state == Debugger.STOPPED)
			currentDebugger.switchFrame (this.items.length - 1 - this.selection.index);
	}

    //
    // show palette
    //	
	panes.push (callstack);
	
	callstack.erase();
	callstack.show();
}
