
/***************************************************************************
**
**    $Header:   D:/PICSLDV/SRC/LOG/VARFRM.CPP   1.14   09 Jan 1997 14:26:40   ZJRD  $
**
**    $Log:   D:/PICSLDV/SRC/LOG/VARFRM.CPP  $
** 
**    Rev 1.14   09 Jan 1997 14:26:40   ZJRD
** No change.
** 
**    Rev 1.13   13 Dec 1996 11:21:30   ZJRD
** No change.
** 
**    Rev 1.12   22 Nov 1996 11:02:30   ZJRD
** No change.
** 
**    Rev 1.11   11 Nov 1996 12:49:44   ZJRD
** No change.
** 
** 
**    Rev 1.0   13 Aug 1996 09:17:36   ZJRD
** Initial revision.
** 
****************************************************************************/

/////////////////////////////////////////////////////////////////////////////
//
//  File name: VARFRM.CPP
//
//  Description: The implementation file for the class: CVarFrame.
//
//  Author: Chen Jun
//
//  Date: 07/18/95
//
//  Modification:
//      1. 07/18/95, Initial version of the class: CVarFrame.
//
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// Include files.
#include "stdafx.h"
#include "resource.h"

//#include "flttool.h"
#include "mainfrm.h"
#include "uicom2.h"
#include "varpane.h"

#include "varfrm.h"
#include "vardoc.h"
#include "varview.h"

#include "varmodi.h"
#include "varsrch.h"
#include "varcast.h"
#include "varadd.h"
#include "varedit.h"

#include "symblsvr.h"

//#include "stkexp.h"
#include "varexp.h"
#include "stkinit.h"
#include "varinit.h"
#include "goapi.h"


/////////////////////////////////////////////////////////////////////////////
// Debug flags.
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif


/////////////////////////////////////////////////////////////////////////////
// Global variables.
CMultiDocTemplate* pVarDocTemplate;

extern CMDIChildWnd    * pVariableWnd;
extern CMDIChildWnd    * pStackWnd;

BOOL isVarSortByHistory = TRUE;
BOOL isVarDisplayVertically = TRUE;

BOOL isVarWindowActive = FALSE;


/////////////////////////////////////////////////////////////////////////////
// Global function prototypes.
void VarOpenWindow(CMDIFrameWnd* pParent);
void VarCloseWindow(void);

BOOL VarAllocateSpace(void);
void VarReleaseSpace(void);


/////////////////////////////////////////////////////////////////////////////
// External function prototypes.
void VarDeleteOneNode(void);
void VarDeleteAll(void);
BOOL VarIsEmpty(void);
void VarSearchNextToken(void);
void VarUpdateWholeWindow(void);


/////////////////////////////////////////////////////////////////////////////
// External variables.
extern CMDIChildWnd* pVariableWnd;      // Defined in UICOM2.H
extern BOOL isVariableOn;               

extern CStringList* pVarSearchPatternList;  // Defined in VARSRCH.CPP
extern CStringList* pVarTypeCastList;       // Defined in VARCAST.CPP
extern CStringList* pVarAddVariableList;    // Defined in VARADD.CPP
extern CStringList* pVarEditVariableList;   // Defined in VAREDIT.CPP

extern BOOL isStkLocalActive;



/////////////////////////////////////////////////////////////////////////////
// Public functions.

// Open the Variable window if it's now closed.
void VarOpenWindow(CMDIFrameWnd* /* pParent */)
{
    // Assertion of the input parameters.

    // Only one Variable window.
    if ( ::isVariableOn ) {
        return;
    }

    ::pVarDocTemplate->OpenDocumentFile(NULL);

    ::VarUpdateWholeWindow();
    
}   // End of VarOpenWindow().

// Close the Variable window if it's now opened.
void VarCloseWindow(void)
{
    // Assertion of the input parameters.
    ASSERT( ::pVariableWnd );
    ASSERT( TRUE == ::isVariableOn );

    // Send message to Variable window.
    ::pVariableWnd->SendMessage(WM_CLOSE);

}   // End of VarCloseWindow().

// Allocate the space for the variable list.
BOOL VarAllocateSpace(void)
{
    // Assertion of the input parameters.

    // Allocate ::pVarSearchPatternList.
    ::pVarSearchPatternList = new CStringList;
    ASSERT( ::pVarSearchPatternList );
    if ( !::pVarSearchPatternList ) {
        return (FALSE);
    }
    
    // Allocate ::pVarTypeCastList.
    ::pVarTypeCastList = new CStringList;
    ASSERT( ::pVarTypeCastList );
    if ( !::pVarTypeCastList ) {
        return (FALSE);
    }
    
    // Allocate ::pVarAddVariableList.
    ::pVarAddVariableList = new CStringList;
    ASSERT( ::pVarAddVariableList );
    if ( !::pVarAddVariableList ) {
        return (FALSE);
    }

    // Allocate ::pVarEditVariableList.
    ::pVarEditVariableList = new CStringList;
    ASSERT( ::pVarEditVariableList );
    if ( !::pVarEditVariableList ) {
        return (FALSE);
    }
    
    return (TRUE);
}


// Release the space.
void VarReleaseSpace(void)
{
    // Assertion of the input parameters.

    // Release the allocated space.
    if ( ::pVarSearchPatternList ) {
        delete ::pVarSearchPatternList;
    }
    
    if ( ::pVarTypeCastList ) {
        delete ::pVarTypeCastList;
    }

    if ( ::pVarAddVariableList ) {
        delete ::pVarAddVariableList;
    }

    if ( ::pVarEditVariableList ) {
        delete ::pVarEditVariableList;
    }
}


/////////////////////////////////////////////////////////////////////////////
// CVarFrame

IMPLEMENT_DYNCREATE(CVarFrame, CMDIChildWnd)

// Specify the Window class.
BOOL CVarFrame::PreCreateWindow(CREATESTRUCT& cs)
{
    // Register a custom WndClass and create a window.
    // This must be done because CVarFrame has a custom icon.
    const char* pszVariableFrameClass =
        AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
            LoadCursor(NULL, IDC_ARROW),
            (HBRUSH) (COLOR_WINDOW+1),
            LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_VARIABLE)));
    
    // Set to CREATESTRUCT.
    ASSERT( NULL == cs.lpszClass );
    cs.lpszClass = pszVariableFrameClass;

    return TRUE;
}

// Construction.
CVarFrame::CVarFrame()
{
    // Set the Flag.
    ::isVariableOn = TRUE;
    ::pVariableWnd = this;
    
    // Initial.
    m_nErrorID = noError;
}

// Destruction.
CVarFrame::~CVarFrame()
{
    // Reset the flag.
    ::isVariableOn = FALSE;
}


/////////////////////////////////////////////////////////////////////////////
// Common function.

// Display the error message.
void CVarFrame::DisplayErrorMessage(void) const
{
    // Assertion of the input parameters.
    ASSERT( m_nErrorID > noError && m_nErrorID < maxError );
    
    // Create the error message table.
    char* pszErrorMessage[] = {
        "",
        "Insufficient memory.",
        "Cannot create bottom pane in Variable window."
    };
    
    // Show the error message.
    AfxMessageBox(pszErrorMessage[m_nErrorID]);
}


/////////////////////////////////////////////////////////////////////////////
// Message map

/*
    ON_COMMAND(ID_VIEW_SORTBYHISTORY, OnViewSortbyHistory)
    ON_UPDATE_COMMAND_UI(ID_VIEW_SORTBYHISTORY, OnUpdateViewSortbyHistory)
    ON_COMMAND(ID_VIEW_SORTBYNAME, OnViewSortbyName)
    ON_UPDATE_COMMAND_UI(ID_VIEW_SORTBYNAME, OnUpdateViewSortbyName)
    ON_COMMAND(ID_VIEW_DISPLAYHORIZONTALLY, OnViewDisplayHorizontally)
    ON_UPDATE_COMMAND_UI(ID_VIEW_DISPLAYHORIZONTALLY, OnUpdateViewDisplayHorizontally)
    ON_COMMAND(ID_VIEW_DISPLAYVERTICALLY, OnViewDisplayVertically)
    ON_UPDATE_COMMAND_UI(ID_VIEW_DISPLAYVERTICALLY, OnUpdateViewDisplayVertically)
    ON_COMMAND(ID_VIEW_TYPECAST, OnViewTypeCast)
    ON_UPDATE_COMMAND_UI(ID_VIEW_TYPECAST, OnUpdateViewTypecast)
    ON_COMMAND(ID_VARIABLE_UNDELETE, OnVariableUndelete)
*/
BEGIN_MESSAGE_MAP(CVarFrame, CMDIChildWnd)
    //{{AFX_MSG_MAP(CVarFrame)
    ON_CBN_KILLFOCUS(IDC_SERCOMBO, OnSrchComboKillFocus)
    ON_UPDATE_COMMAND_UI(IDC_SERCOMBO, OnUpdateSearchCombo)
    ON_WM_CREATE()
    ON_WM_SIZE()
    ON_WM_GETMINMAXINFO()
    ON_COMMAND(ID_EDIT_MODIFYVALUES, OnEditModifyValues)
    ON_COMMAND(ID_EDIT_SEARCH, OnEditSearch)
    ON_COMMAND(ID_EDIT_SEARCHNEXT, OnEditSearchNext)
    ON_UPDATE_COMMAND_UI(ID_EDIT_SEARCHNEXT, OnUpdateEditSearchNext)
    ON_COMMAND(ID_VARIABLE_ADD, OnVariableAdd)
    ON_COMMAND(ID_VARIABLE_DELETE, OnVariableDelete)
    ON_COMMAND(ID_VARIABLE_DELETEALL, OnVariableDeleteAll)
    ON_COMMAND(ID_VARIABLE_EDIT, OnVariableEdit)
    ON_WM_MDIACTIVATE()
    ON_UPDATE_COMMAND_UI(ID_EDIT_MODIFYVALUES, OnUpdateEditModifyvalues)
    ON_UPDATE_COMMAND_UI(ID_EDIT_SEARCH, OnUpdateEditSearch)
    ON_UPDATE_COMMAND_UI(ID_VARIABLE_DELETE, OnUpdateVariableDelete)
    ON_UPDATE_COMMAND_UI(ID_VARIABLE_DELETEALL, OnUpdateVariableDeleteall)
    ON_UPDATE_COMMAND_UI(ID_VARIABLE_EDIT, OnUpdateVariableEdit)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// CVarFrame message handlers

void CVarFrame::OnSrchComboKillFocus(void)
{   
    CMainFrame * pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
    CString str = pMain->m_wndToolBar.GetSrchEditText();    
    if(str != "") { 
        pMain->m_wndToolBar.AddSrchText(str);
        ::pVarSearchPatternList->AddTail(str);
    }
}

void CVarFrame::OnUpdateSearchCombo(CCmdUI* pCmdUI)
{
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;
    
    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}


int CVarFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
        return -1;
    
    // TODO: Add your specialized creation code here
    
    // Create the bottom pane.
    if ( !m_wndBottomPane.Create(this) ) {
        m_nErrorID = bottomPane;
        DisplayErrorMessage();
        ASSERT( FALSE );
        return -1;
    }

    return 0;
}

void CVarFrame::OnSize(UINT nType, int cx, int cy)
{
    CMDIChildWnd::OnSize(nType, cx, cy);
    
    // TODO: Add your message handler code here

    // Adjust the Bottom pane position.
    CRect rect(0, cy-bottomHeight+1, cx, cy);
    m_wndBottomPane.MoveWindow(rect);
}

void CVarFrame::OnGetMinMaxInfo(MINMAXINFO FAR* lpMMI)
{
    // TODO: Add your message handler code here and/or call default
    
    // Set the minimum range.
    lpMMI->ptMinTrackSize = CPoint(bottomHeight*10, bottomHeight*4);
    
    CMDIChildWnd::OnGetMinMaxInfo(lpMMI);
}

void CVarFrame::OnMDIActivate(BOOL bActivate, CWnd* pActivateWnd, CWnd* pDeactivateWnd)
{
    CMDIChildWnd::OnMDIActivate(bActivate, pActivateWnd, pDeactivateWnd);
    
    // TODO: Add your message handler code here
    
    // Update the Variable window.
//    if( bActivate && this == pActivateWnd ) {
//        ::VarUpdateWholeWindow();
//    }

    if(bActivate && pActivateWnd == this)
        GetMDIFrame()->SendMessage(XM_MDIACTIVE, WORD(bActivate), 
            LONG(WID_VARIABLE));
    else if(!bActivate && !pActivateWnd)
        GetMDIFrame()->SendMessage(XM_MDIACTIVE, WORD(bActivate)); 
}


void CVarFrame::OnEditModifyValues()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Open the Modify Variable dialog.
    CVarModifyVariableDialog dlg;
    dlg.DoModal();
}

void CVarFrame::OnUpdateEditModifyvalues(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    BOOL bEnable = TRUE;

    unsigned char    uch;

	GetCpuStatus(uch);
    if ( STATUS_HALT != uch && STATUS_GOMONI != uch ) {
//	if ( uch ) {
        bEnable = FALSE;
    }
    else if ( ::VarIsEmpty() ) {
        bEnable = FALSE;
    }
    else {                  
    	::Var_LineToNode(((CVarView*)GetActiveView())->m_nCurRow, 
    							 ((CVarView*)GetActiveView())->m_pVarNode);

        ((CVarView*)GetActiveView())->DetectVarFormat();
        
        if ( ((CVarView*)GetActiveView())->simpleSign != 
             ((CVarView*)GetActiveView())->m_nBmpStatus ) {
            bEnable = FALSE;
        }                   
        
        if ( Var_CanBeModify(((CVarView*)GetActiveView())->m_pVarNode) != 0 )
        	bEnable = FALSE;
    }
    
    pCmdUI->Enable(bEnable);
}

void CVarFrame::OnEditSearch()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Open the Search Dialog.
    CVarSearchDialog dlg;
    dlg.DoModal();
}

void CVarFrame::OnUpdateEditSearch(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}

void CVarFrame::OnEditSearchNext()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Search the token from the next line.
    ::VarSearchNextToken();
}

void CVarFrame::OnUpdateEditSearchNext(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable the menu item.
    if ( ::VarIsEmpty() ) {
        pCmdUI->Enable(FALSE);
    }
    else {
        pCmdUI->Enable(!::pVarSearchPatternList->IsEmpty());
    }
}

/*
void CVarFrame::OnViewSortbyHistory()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Check the global flag.
    if ( ::isVarSortByHistory ) {
        return;
    }
    
    // Sort by history.
    ::isVarSortByHistory = TRUE;

    // TODO:
    
}

void CVarFrame::OnUpdateViewSortbyHistory(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Set the radio dot or not.
    pCmdUI->SetRadio(::isVarSortByHistory);
}

void CVarFrame::OnViewSortbyName()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Check the global flag.
    if ( !::isVarSortByHistory ) {
        return;
    }
    
    // Sort by name.
    ::isVarSortByHistory = FALSE;

    // TODO:
    
}

void CVarFrame::OnUpdateViewSortbyName(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Set the radio dot or not.
    pCmdUI->SetRadio(!::isVarSortByHistory);
}

void CVarFrame::OnViewDisplayHorizontally()
{
    // TODO: Add your command handler code here

    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Check the global flag.
    if ( !::isVarDisplayVertically ) {
        return;
    }
    
    // Display horizontally.
    ::isVarDisplayVertically = FALSE;

    // TODO:

}

void CVarFrame::OnUpdateViewDisplayHorizontally(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Set the radio dot or not.
    pCmdUI->SetRadio(!::isVarDisplayVertically);
}

void CVarFrame::OnViewDisplayVertically()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Check the global flag.
    if ( ::isVarDisplayVertically ) {
        return;
    }
    
    // Display vertically.
    ::isVarDisplayVertically = TRUE;

    // TODO:

}

void CVarFrame::OnUpdateViewDisplayVertically(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here

    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Set the radio dot or not.
    pCmdUI->SetRadio(::isVarDisplayVertically);
}

void CVarFrame::OnUpdateViewTypecast(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}

void CVarFrame::OnViewTypeCast()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Open Type Cast dialog.
    CVarTypeCastDialog dlg;
    dlg.DoModal();
}
*/

void CVarFrame::OnVariableAdd()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Open Add Variable dialog.
    CVarAddVariableDialog dlg;
    dlg.DoModal();
}

void CVarFrame::OnUpdateVariableDelete(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}

void CVarFrame::OnVariableDelete()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Delete the boot variable node.
    ::VarDeleteOneNode();
}

void CVarFrame::OnUpdateVariableDeleteall(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}

void CVarFrame::OnVariableDeleteAll()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Delete all the variables in the Variable window.
    ::VarDeleteAll();
}

void CVarFrame::OnUpdateVariableEdit(CCmdUI* pCmdUI)
{
    // TODO: Add your command update UI handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Enable or disable according to if the Variable window is empty.
    pCmdUI->Enable(!::VarIsEmpty());
}

void CVarFrame::OnVariableEdit()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;

    // Open Edit Variable dialog.
    CVarEditVariableDialog dlg;
    dlg.DoModal();
}

void CVarFrame::OnVariableUndelete()
{
    // TODO: Add your command handler code here
    
    // Set the identify flag.
    ::isVarWindowActive = TRUE;
    ::isStkLocalActive = FALSE;
}

BOOL CVarFrame::PreTranslateMessage(MSG *pMsg)
{                   
  if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN)
  {                
    CMainFrame * pMain = (CMainFrame *)AfxGetApp()->m_pMainWnd;
    
//    if( pMain->m_wndToolBar.HasFocus() ) {
	if ( GetFocus() != this && ((CVarView *)GetActiveView())->m_nFocus==0 ) {
	// Added by Gates Hua    
	    CString str = pMain->m_wndToolBar.GetSrchEditText();    
	    if ( !str.IsEmpty() ) {                          
	    	if ( !::pVarSearchPatternList->IsEmpty() ) {
	    		if ( !(str == ::pVarSearchPatternList->GetTail()) ) {
			        ::pVarSearchPatternList->AddTail(str);
				}
			}    			              
			else 
		        ::pVarSearchPatternList->AddTail(str);
		}
       pVariableWnd->PostMessage(WM_COMMAND,ID_EDIT_SEARCHNEXT);
    }
  }
  return CMDIChildWnd::PreTranslateMessage(pMsg);
}

