#include <idate.hpp>
#include <imsgbox.hpp>

#include "ChFile.xih"
#include "stockdlg.h"
#include "stockdlg.hpp"

//----------------------- Destructor -----------------------
// Remove this program from the list of programs handling
// events for this frame window.
//----------------------------------------------------------
StockDlg::~StockDlg()
{
    ICommandHandler::stopHandlingEventsFor(this);
}
//----------------------- Contructor -----------------------
//
// Note that the frame window is constructed from the PM
// window handle. This tells IFrameWindow that it is a
// wrapper for a PM generated Frame Window.
//----------------------------------------------------------
StockDlg::StockDlg (ChartFile *PChartFile,
                    const IWindowHandle& Hwnd)
        : IFrameWindow(Hwnd)
        , pChartFile(PChartFile)
        , stockSymbol(DID_TICKER_SYMBOL, this)
        , lastMonth(DID_MM, this)
        , lastDay(DID_DD, this)
        , lastYear(DID_YYYY, this)
        , exchange(DID_EXCHANGE, this)
{

// Limit the characters that can be entered in each field

   lastMonth.setLimit(2);
   lastDay.setLimit(2);
   lastYear.setLimit(4);
   stockSymbol.setLimit(8);

// Populate the "Exchange" drop-down box

   exchange.setText(" ");
   exchange.addAsLast("NYSE");
   exchange.addAsLast("AMEX");
   exchange.addAsLast("NASDAQ");
   exchange.addAsLast("INDEX");

// Save the SOM Environment. It is used through out this
// program.

   pSomEnv = somGetGlobalEnvironment();

// Set the fields on the screen to their current values.

   restoreState();

// Put this program on top of the list of programs handling
// events for this window.

   ICommandHandler::handleEventsFor(this);
}

//----------------------------------------------------------
// Override ICommandHandler::command to know when the user
// clicks on the UNDO button on this settings page.
//----------------------------------------------------------
Boolean StockDlg::command(ICommandEvent& cmdEvent)
{
    switch (cmdEvent.commandId())
          {
           case DID_UNDO:
                restoreState();
                return true;

           default:
                break;
          }

    return false;
}

//----------------------------------------------------------
// Override IHandler::dispatchHandlerEvent to intercept PM
// messages so that when the settings notebook is closed,
// the current state of the object can be saved.
//----------------------------------------------------------
Boolean StockDlg::dispatchHandlerEvent(IEvent& evt)
{
   if (evt.eventId() == WM_DESTROY)
       saveState();

   return ICommandHandler::dispatchHandlerEvent(evt);
}

//---------------------------------------------------------
// Set the screen fields to the values stored in the
// ChartFile object
//---------------------------------------------------------
void StockDlg::restoreState()
{
   IString tStr; short tShort;

   tStr = pChartFile->getStockSymbol(pSomEnv);
   stockSymbol.setText(tStr);

   tStr = pChartFile->getExchange(pSomEnv);
   exchange.setText(tStr);

   tShort = pChartFile->getLastDD(pSomEnv);
   tStr = IString(tShort);
   lastDay.setText(tStr);

   tShort = pChartFile->getLastMM(pSomEnv);
   tStr = IString(tShort);
   lastMonth.setText(tStr);

   tShort = pChartFile->getLastYYYY(pSomEnv);
   tStr = IString(tShort);
   lastYear.setText(tStr);

   stockSymbol.setFocus();

   return;
}

//-------------------------------------------------
// Copy data from the screen to the WPS object
//-------------------------------------------------
void StockDlg::saveState()
{
     IString tStr;
     short   tShort;

     tStr = stockSymbol.text();
     pChartFile->setStockSymbol(pSomEnv, tStr);

     tStr = exchange.text();
     pChartFile->setExchange(pSomEnv, tStr);

     tShort = lastDay.text().asInt();
     pChartFile->setLastDD(pSomEnv, tShort);

     tShort = lastMonth.text().asInt();
     pChartFile->setLastMM(pSomEnv, tShort);

     tShort = lastYear.text().asInt();
     pChartFile->setLastYYYY(pSomEnv, tShort);

// Tell the ChartFile object that stock data has been
// updated and call wpSaveDeferred to save this data
// during idle or shut down time.

     pChartFile->setStockDataSaved(pSomEnv, 1);
     pChartFile->wpSaveDeferred();

     return;
}

//-------------Create a pop-up menu resource----------------
//
// This is how the pop-up menu items that are added to the
// object's pop-up menu look in ChFile.rc. Note that the old
// dialog editor (not the visual builder) is used to create
// the settings page's dialog window.
//----------------------------------------------------------
