// Copyright 1994, 1997 by Jon Dart.
// mainfrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "arasan.h"
#include "resource.h"
#include "mainfrm.h"
#include "arasavw.h"
#include "globals.h"
#include <afxcmn.h>

#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
        //{{AFX_MSG_MAP(CMainFrame)
                // NOTE - the ClassWizard will add and remove mapping macros here.
                //    DO NOT EDIT what you see in these blocks of generated code !
        ON_WM_CREATE()
        ON_WM_CLOSE()
	ON_UPDATE_COMMAND_UI(ID_VIEW_BESTLINEBAR, OnUpdateControlBarMenu)
	ON_COMMAND_EX(ID_VIEW_BESTLINEBAR, OnBarCheck)
                // Global help commands
        ON_COMMAND(ID_HELP_FINDER, CFrameWnd::OnHelpFinder)
        ON_COMMAND(ID_HELP_USING, CFrameWnd::OnHelpUsing)
        ON_COMMAND(ID_HELP, CFrameWnd::OnHelp)
        //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// arrays of IDs used to initialize control bars

// toolbar buttons - IDs are command buttons
static UINT BASED_CODE buttons[] =
{
        // same order as in the bitmap 'toolbar.bmp'
        ID_FILE_NEW,
        ID_FILE_OPEN,
        ID_FILE_SAVE,
                ID_SEPARATOR,
        ID_EDIT_CUT,
        ID_EDIT_COPY,
        ID_EDIT_PASTE,  
/** LATER        
                ID_SEPARATOR,
        ID_FILE_PRINT,
**/        
                        ID_SEPARATOR,
        ID_BROWSE_PREVIOUSGAME,
        ID_BEGIN_GAME,
        ID_TAKEBACK,
        ID_COMPUTE,
        ID_FORWARD,
        ID_GAME_ENDOFGAME,
        ID_BROWSE_NEXTGAME,
                        ID_SEPARATOR,
        ID_HINT
};

static UINT BASED_CODE indicators[] =
{
        ID_SEPARATOR,           // status line indicator
        ID_INDICATOR_PLYLABEL,
        ID_INDICATOR_PLY,
        ID_INDICATOR_SCORELABEL,
        ID_INDICATOR_SCORE,
        ID_INDICATOR_NODELABEL,
        ID_INDICATOR_NODES
};

static UINT BASED_CODE pv_indicators[] =
{
//        ID_SEPARATOR,           // status line indicator
        ID_INDICATOR_PVLABEL,
        ID_INDICATOR_PV
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
        // TODO: add member initialization code here
}

CMainFrame::~CMainFrame()
{
}

void CMainFrame::update(int ply, int nodes, float score,
           LPCSTR predicted)
{
   char text[20];
   sprintf(text,"%d",ply);
   updateSafe(1,2,text);
   sprintf(text,"%4.2f",score);
   updateSafe(1,4,text);
   sprintf(text,"%d",nodes);
   updateSafe(1,6,text);
   updateSafe(2,1,predicted);
}

void CMainFrame::update(int nodes)
{
    char text[20];
    sprintf(text,"%d",nodes);
    updateSafe(1,6,text);
}

void CMainFrame::clear_status_bar()
{
    updateSafe(1,2,"");
    updateSafe(1,4,"");
    updateSafe(1,6,"");
    updateSafe(2,1,"");
}

// MFC does not like CWnd-based classes to be shared between threads.
// This appears in fact to be quite safe but it will trigger asserts
// in the debug version of MFC.  So we use a lower-level API to 
// update the status bars.
void CMainFrame::updateSafe(int bar,int index, LPCSTR text)
{
    CStatusBarCtrl &ctrl = 
     (bar == 1 ? m_wndStatusBar.GetStatusBarCtrl() :
                 m_pvStatusBar.GetStatusBarCtrl());
    ctrl.SetText(text,index,0);
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT &cs)
{
    // Have to call the base version first because it sets the window
    // class.
    CFrameWnd::PreCreateWindow(cs);
    // Set the window size and position to where it was when we
    // last ran, or to some default otherwise.
    int x, y, w, h;
    global_options->get_app_geometry(&x,&y,&w,&h);
    cs.cx = w;
    cs.cy = h;
    cs.x = x;
    cs.y = y;
    return TRUE;
}

void CMainFrame::OnClose()
{
     // save the window size and position for the next time we are run.
    CRect rect;
    GetWindowRect(&rect);
    global_options->set_app_geometry(rect.left,rect.top,rect.Width(),rect.Height());
    SaveBarState("ControlBarState");
    CFrameWnd::OnClose();
}


int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

#ifndef CBRS_TOOLTIPS
     if (!m_wndToolBar.Create(this) ||
#else
    if (!m_wndToolBar.Create(this,WS_CHILD | WS_VISIBLE | CBRS_TOP | CBRS_TOOLTIPS) ||
#endif    
        !m_wndToolBar.LoadBitmap(IDR_MAINFRAME) ||
        !m_wndToolBar.SetButtons(buttons,
                                 sizeof(buttons)/sizeof(UINT)))
    {
        TRACE("Failed to create toolbar\n");
        return -1;      // fail to create
    }

    // Create the regular status bar
    if (!m_wndStatusBar.Create(this) ||
        !m_wndStatusBar.SetIndicators(indicators,
                                      sizeof(indicators)/sizeof(UINT)))
    {
       TRACE("Failed to create status bar 1\n");
       return -1;      // fail to create
    }
    // Create the p.v. status bar.  Note that the bar has the same
    // control ID as the menu item that controls it.
    if (!m_pvStatusBar.Create(this,WS_CHILD | WS_VISIBLE | CBRS_BOTTOM,
        ID_VIEW_BESTLINEBAR) ||
        !m_pvStatusBar.SetIndicators(pv_indicators,
                                      sizeof(pv_indicators)/sizeof(UINT)))
    {
       TRACE("Failed to create status bar 2\n");
       return -1;      // fail to create
    }
    m_wndStatusBar.SetPaneInfo(0,ID_SEPARATOR,SBPS_NOBORDERS |
    SBPS_STRETCH, 150);
    m_wndStatusBar.SetPaneInfo(1,ID_INDICATOR_PLYLABEL,SBPS_NOBORDERS |
    SBPS_NORMAL, 30);
    m_wndStatusBar.SetPaneInfo(2,ID_INDICATOR_PLY, SBPS_NORMAL, 30);
    m_wndStatusBar.SetPaneInfo(3,ID_INDICATOR_SCORELABEL,SBPS_NOBORDERS |
    SBPS_NORMAL, 30);
    m_wndStatusBar.SetPaneInfo(4,ID_INDICATOR_SCORE, SBPS_NORMAL, 40);
    m_wndStatusBar.SetPaneInfo(5,ID_INDICATOR_NODELABEL,SBPS_NOBORDERS |
    SBPS_NORMAL, 30);
    m_wndStatusBar.SetPaneInfo(6,ID_INDICATOR_NODES, SBPS_NORMAL, 60);
    m_pvStatusBar.SetPaneInfo(0,ID_INDICATOR_PVLABEL,SBPS_NOBORDERS |
    SBPS_NORMAL, 60);
    m_pvStatusBar.SetPaneInfo(1,ID_INDICATOR_PV, SBPS_STRETCH, 100);
    
    LoadBarState("ControlBarState");
    return 0;
}

BOOL CMainFrame::OnCommand(WPARAM wParam, LPARAM lParam)
{
    // forward this to the view class
    CArasanView *view = (CArasanView*)GetActiveView();
    if (view && view->IsKindOf(RUNTIME_CLASS(CArasanView)) )
        if (view->OnCommand(wParam, lParam))
            return TRUE;
    return CFrameWnd::OnCommand(wParam, lParam);
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
        CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
        CFrameWnd::Dump(dc);
}                                

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers
