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

#include <vcl.h>
#pragma hdrstop

#include "FrmCctvCtrlF.h"

//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
#ifndef ULONG_PTR
#define ULONG_PTR unsigned long *
#endif

#include <stdio.h>
#include <string.h>
#include <list>
#include <map>

#define STRICT
#include <windows.h>
#include <algorithm>
using std::min;
using std::max;
#include "gdiplus.h"
#include "GdiplusBase.h" //항상 넣어 둔다.

class CGdiPlusStarter
{
private:
	ULONG_PTR m_gpToken;

public:
	bool m_bSuccess;
	CGdiPlusStarter()
    {
        ::CoInitialize(NULL);
		Gdiplus::GdiplusStartupInput gpsi;
		m_bSuccess = (Gdiplus::GdiplusStartup(&m_gpToken, &gpsi, NULL) == Gdiplus::Ok);
	}
	~CGdiPlusStarter()
    {
		Gdiplus::GdiplusShutdown(m_gpToken);
        ::CoUninitialize();
	}
};
CGdiPlusStarter g_gps;


//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxHint"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxMaskEdit"
#pragma link "cxPC"
#pragma link "cxPCdxBarPopupMenu"
#pragma link "cxSpinEdit"
#pragma link "cxTextEdit"
#pragma link "dxBar"
#pragma link "dxCustomHint"
#pragma link "dxRibbon"
#pragma link "dxRibbonGallery"
#pragma link "dxRibbonSkins"
#pragma link "dxRibbonStatusBar"
#pragma link "dxScreenTip"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinCaramel"
#pragma link "dxSkinChooserGallery"
#pragma link "dxSkinCoffee"
#pragma link "dxSkinDarkRoom"
#pragma link "dxSkinDarkSide"
#pragma link "dxSkinFoggy"
#pragma link "dxSkinGlassOceans"
#pragma link "dxSkiniMaginary"
#pragma link "dxSkinLilian"
#pragma link "dxSkinLiquidSky"
#pragma link "dxSkinLondonLiquidSky"
#pragma link "dxSkinMcSkin"
#pragma link "dxSkinMoneyTwins"
#pragma link "dxSkinOffice2007Black"
#pragma link "dxSkinOffice2007Blue"
#pragma link "dxSkinOffice2007Green"
#pragma link "dxSkinOffice2007Pink"
#pragma link "dxSkinOffice2007Silver"
#pragma link "dxSkinOffice2010Black"
#pragma link "dxSkinOffice2010Blue"
#pragma link "dxSkinOffice2010Silver"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinsdxBarPainter"
#pragma link "dxSkinsdxRibbonPainter"
#pragma link "dxSkinSeven"
#pragma link "dxSkinsForm"
#pragma link "dxSkinSharp"
#pragma link "dxSkinSilver"
#pragma link "dxSkinStardust"
#pragma link "dxStatusBar"
#pragma link "dxTabbedMDI"
#pragma link "IdAntiFreeze"
#pragma link "IdAntiFreezeBase"
#pragma link "IdBaseComponent"
#pragma link "cxButtons"
#pragma link "cxGroupBox"
#pragma link "ImageBtn"
#pragma link "FrameSamsungF"
#pragma link "FrameFlexWatchF"
#pragma link "FrameHanwhaF"
#pragma link "FrameSamsungXnsActiveXF"
#pragma link "FrameStreamingAcesF"
#pragma link "FrameTruenHttpF"
#pragma link "AcesTechXPlayer2Lib_OCX"
#pragma resource "*.dfm"

TFrmCctvCtrl *FrmCctvCtrl = NULL;
//---------------------------------------------------------------------------

__fastcall TFrmCctvCtrl::TFrmCctvCtrl(TComponent* Owner)
    : TForm(Owner)//TdxCustomRibbonForm(Owner)//TForm(Owner)
{
    //DoubleBuffered = true;
    Application->OnException = OnAppException;

    ShpTitle->Left = 4;
    ShpTitle->Top  = 4;
    ShpTitle->Anchors = TAnchors() << akLeft << akTop << akRight;
    ShpTitle->Brush->Style = bsClear;


    ::CoInitialize(NULL);
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::FormCreate(TObject *Sender)
{
    /* 레지스터리 등록 IE 최신버전 */
    String sExeName = ExtractFileName(Application->ExeName);
    UpdateExplorerEmulatorVersion(sExeName);

    Application->ShowMainForm = false;

    try {
        Application->Icon->LoadFromResourceName(((unsigned int)HInstance), "MAINICON");
    } catch(...) { ShowMessage("LoadFromResourceName failed"); }

    Application->ShowMainForm = true;
    Application->OnHint       = OnAppHint;
}
//---------------------------------------------------------------------------

#include <Registry.hpp>
bool __fastcall TFrmCctvCtrl::UpdateExplorerEmulatorVersion(String AAppName)
{
    TRegistry *reg = new TRegistry(KEY_READ|KEY_WRITE|KEY_WOW64_64KEY);
    if (reg == NULL) return false;

    String sKey = "SOFTWARE\\Wow6432Node\\Microsoft\\Internet Explorer\\MAIN\\FeatureControl\\FEATURE_BROWSER_EMULATION";

    reg->RootKey = HKEY_LOCAL_MACHINE;
    if (!reg->KeyExists(sKey))
    {
        //MessageDlg("Key not found! Created now.", mtInformation, TMsgDlgButtons() << mbOK, 0);
        bool bOpenKey = reg->CreateKey(sKey);
        //return false;
    }
    reg->Access = KEY_WRITE;

    bool openResult = reg->OpenKey(sKey, true);
    if(!openResult)
    {
        //MessageDlg("Unable to create key! Exiting.", mtError, TMsgDlgButtons() << mbOK, 0);
        return false;
    }

    if (!reg->KeyExists(AAppName))
    {
        reg->WriteInteger(AAppName, 10000);
    }

    reg->CloseKey();
    reg->Free();

    return true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::FormShow(TObject *Sender)
{
    Refresh();
    Application->ProcessMessages();

	TmrOnShow->Enabled = true;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::TmrOnShowTimer(TObject *Sender)
{
    Refresh();
	TmrOnShow->Enabled = false;
	Application->ProcessMessages();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::FormClose(TObject *Sender, TCloseAction &Action)
{
    CommClose();
    Action = caFree;
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::CommClose()
{
    try
    {
        FrameSamsungXnsActiveX1->CloseStream();
        FrameHanwha1->CloseStream();
        FrameTruenHttp1->CloseStream();
        FrameFlexWatch1->CloseStream();
        FrameStreamingAces1->CloseStream();

        ::CoUninitialize();
    }
    catch(...)
    {
    }
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::OnAppHint(TObject *Sender)
{
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::OnAppException(TObject *Sender, Exception *exception)
{
    String sClassName = (NULL != Sender)        ? Sender->ClassName() : String("Unknown_Class");
    String sErrMsg    = (NULL != exception)     ? exception->Message  : String("Unknown_Error");
    String sError     = sClassName + " : " + sErrMsg;

    //Application->ShowException(&exception);

    Application->NormalizeTopMosts();
    Application->MessageBox(sError.c_str(), L"System Error", MB_OK|MB_ICONERROR|MB_APPLMODAL);
    Application->RestoreTopMosts();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::ApplicationEvents1SettingChange(TObject *Sender, int Flag,
          const UnicodeString Section, int &Result)
{
    DateSeparator = '-';
    TimeSeparator = ':';
    ShortDateFormat = "yyyy-MM-dd";
    ShortTimeFormat = "HH:NN:SS";
    LongTimeFormat  = "HH:NN:SS";

    cxFormatController()->BeginUpdate();
    cxFormatController()->UseDelphiDateTimeFormats = true;
    cxFormatController()->EndUpdate();
    cxFormatController()->GetFormats();
    cxFormatController()->NotifyListeners();
}
//---------------------------------------------------------------------------

void __fastcall TFrmCctvCtrl::BtnCloseClick(TObject *Sender)
{
    Close();
}
//---------------------------------------------------------------------------


