//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "UnitMain.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
	: TForm(Owner)
{
    MemoList->Clear();
    ::PostMessage( Handle, TWM_ONSTART, 0, 0 );
}
//---------------------------------------------------------------------------
__fastcall TFormMain::~TFormMain()
{
    MemoLog->Lines->SaveToFile( ChangeFileExt(ParamStr(0), ".log")  );
}
//---------------------------------------------------------------------------
//! TWM_ONSTART
/*!
    @param[in] Msg
*/
void __fastcall TFormMain::TWMOnStart(TMessage & Msg)
{
    bool mode_auto = false;

    for( int i=0; i<ParamCount(); i++ ) {
        if( ParamStr(i+1) == "mode=auto" ) {
            mode_auto = true;
        }
        else {
            MemoList->Lines->Add( ParamStr(i+1) );
        }
    }
    if( mode_auto == true ) {
		LogOut("自動モード...Start");
        ButtonPrint->Click();
		LogOut("自動モード...End");
        Close();
    }
}
//---------------------------------------------------------------------------
//! エラー例外
/*!
    @param[in]
    @param[in]
*/
void __fastcall TFormMain::ApplicationEvents1Exception(TObject *Sender, Exception *E)
{
	LogOut("Error:" + E->Message);
}
//---------------------------------------------------------------------------
//! Log Out
/*!
    @param[in] msg
*/
void __fastcall TFormMain::LogOut( UnicodeString msg )
{
    UnicodeString header;
    header = FormatDateTime( "yyyy/mm/dd hh:nn:ss", Now() );

	MemoLog->Lines->Add(header + " " + msg);
}
//---------------------------------------------------------------------------
//! IE用ビジーウェイト
/*!
    @param[in] ie
    @param[in] timeout(sec)
*/
bool __fastcall TFormMain::IeBusyWait( Variant &ie, int timeout )
{
    bool ret = false;
    for( int i=0; i<timeout; i++ ) {
	    ::Sleep(1*1000);
	    if( (ie.OlePropertyGet("busy") == true ) || ( ie.OlePropertyGet("Document").OlePropertyGet("readyState") != "complete") ) {
	    }
	    else {
	        ret = true;
	        break;
        }
    }

    return ret;
}
//---------------------------------------------------------------------------
//! IEで印刷
/*!
    @param[in] fname
*/
void __fastcall TFormMain::PrintIE( UnicodeString fname )
{
	Variant ie  = Unassigned;

	try {
		LogOut("IE起動...");
		ie = Variant::CreateObject("InternetExplorer.Application");
		ie.OlePropertySet("Visible", false);
		LogOut("IE起動...OK");

		// ページロード
		LogOut("ページロード..." + fname);
		ie.OleProcedure("Navigate", WideString(fname) );
		if( IeBusyWait(ie, 60) == false ) {
			throw new Exception( UnicodeString().sprintf(L"ie.Navigate() TimeOut(%s)", fname) );
		}
		LogOut("ページロード...OK");

		// 印刷(ブロッキング)
		LogOut("印刷...");
		ie.OleProcedure("ExecWB", OLECMDID_PRINT, OLECMDEXECOPT_DONTPROMPTUSER, (short)3 );
		LogOut("印刷...OK");
	}
	__finally {
		if( VarIsEmpty(ie) != true ) ie.OleProcedure("Quit");
	  	ie = Unassigned;
	}
}
//---------------------------------------------------------------------------
//! 印刷ボタン
/*!
*/
void __fastcall TFormMain::ButtonPrintClick(TObject *Sender)
{
    for (int i=0; i <MemoList->Lines->Count; i++ ) {
		if( MemoList->Lines->Strings[i].Length() > 0 ) {
			try {
                PrintIE( MemoList->Lines->Strings[i] );
            }
            catch ( Exception &e ) {
            	LogOut("Error:" + e.Message);
            }
        }
    }
}
//---------------------------------------------------------------------------

