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

#include <vcl.h>
#include "ITSUtilF.h"
#include "ITSDbF.h"
#include "ITSLangTransF.h"
#pragma hdrstop

#include <jpeg.hpp>
#include <Dialogs.hpp>
#include "FRAME_ChartF.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "cxButtons"
#pragma link "cxCheckBox"
#pragma link "cxClasses"
#pragma link "cxContainer"
#pragma link "cxControls"
#pragma link "cxCustomData"
#pragma link "cxEdit"
#pragma link "cxGraphics"
#pragma link "cxGrid"
#pragma link "cxGridChartView"
#pragma link "cxGridCustomView"
#pragma link "cxGridLevel"
#pragma link "cxLabel"
#pragma link "cxLookAndFeelPainters"
#pragma link "cxLookAndFeels"
#pragma link "cxStyles"
#pragma link "cxTrackBar"
#pragma link "dxSkinBlack"
#pragma link "dxSkinBlue"
#pragma link "dxSkinsCore"
#pragma link "dxSkinscxPCPainter"
#pragma link "dxSkinMcSkin"
#pragma link "cxDropDownEdit"
#pragma link "cxMaskEdit"
#pragma link "cxTextEdit"
#pragma resource "*.dfm"
TFRAMEChart *FRAMEChart;
//---------------------------------------------------------------------------
__fastcall TFRAMEChart::TFRAMEChart(TComponent* Owner)
    : TFrame(Owner)
{
    LangTrans->Translate(this, ITSDb_GetConnection());
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEChart::SetChartLegend(String ACategory, String AValue)
{
    try {
        TvChart->DiagramArea->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramArea->AxisValue->Title->Text = AValue;

        TvChart->DiagramBar->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramBar->AxisValue->Title->Text = AValue;

        TvChart->DiagramColumn->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramColumn->AxisValue->Title->Text = AValue;

        TvChart->DiagramLine->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramLine->AxisValue->Title->Text = AValue;

        TvChart->DiagramStackedArea->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramStackedArea->AxisValue->Title->Text = AValue;

        TvChart->DiagramStackedBar->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramStackedBar->AxisValue->Title->Text = AValue;

        TvChart->DiagramStackedColumn->AxisCategory->Title->Text = ACategory;
        TvChart->DiagramStackedColumn->AxisValue->Title->Text = AValue;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEChart::ChartValueDisplay(bool FDisplayValue)
{
    try {
        TvChart->DiagramArea->Values->CaptionPosition          = FDisplayValue ? ldvcpAbove : ldvcpNone;
        TvChart->DiagramBar->Values->CaptionPosition           = FDisplayValue ? cdvcpOutsideEnd : cdvcpNone;
        TvChart->DiagramColumn->Values->CaptionPosition        = FDisplayValue ? cdvcpOutsideEnd : cdvcpNone;
        TvChart->DiagramLine->Values->CaptionPosition          = FDisplayValue ? ldvcpAbove : ldvcpNone;
        TvChart->DiagramPie->Values->CaptionPosition           = FDisplayValue ? pdvcpOutsideEndWithLeaderLines : pdvcpNone;
        TvChart->DiagramStackedArea->Values->CaptionPosition   = FDisplayValue ? ldvcpRight : ldvcpNone;
        TvChart->DiagramStackedBar->Values->CaptionPosition    = FDisplayValue ? cdvcpCenter : cdvcpNone;
        TvChart->DiagramStackedColumn->Values->CaptionPosition = FDisplayValue ? cdvcpCenter : cdvcpNone;
    } catch(...) {}
}
//---------------------------------------------------------------------------

void __fastcall TFRAMEChart::ChkDisplayValPropertiesChange(TObject *Sender)
{
    ChartValueDisplay(ChkDisplayVal->Checked);
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEChart::cxTrackBar1PropertiesChange(TObject *Sender)
{
    try {
        if (cxTrackBar1->Position == cxTrackBar1->Properties->Max)
        {
            LblPage->Caption = lblAll->Caption;//"전체 ";
        }
        else
        {
            LblPage->Caption = String(cxTrackBar1->Position) + " ";
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEChart::cxTrackBar1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y)
{
    try {
        if (cxTrackBar1->Position == cxTrackBar1->Properties->Max)
        {
            TvChart->OptionsView->CategoriesPerPage = 0;
            LblPage->Caption = "All ";
        }
        else
        {
            TvChart->OptionsView->CategoriesPerPage = cxTrackBar1->Position;
            LblPage->Caption = String(cxTrackBar1->Position) + " ";
        }
    } catch(...) {}
}
//---------------------------------------------------------------------------
void __fastcall TFRAMEChart::BtnGraphSaveClick(TObject *Sender)
{
    TSaveDialog *SaveDialog1 = NULL;
    String sNewFile, sDate;

    sDate    = Now().FormatString("YYYYMMDDhhnn");
    sNewFile = FTitle + "_" + sDate;

    int nFilterIndex = 0;

    try
    {
        SaveDialog1 = new TSaveDialog(Application);
        SaveDialog1->Title      = FTitle + lblImgSave->Caption;//" 차트 이미지 저장";
        //SaveDialog1->InitialDir = ExtractFilePath(Application->ExeName);
        SaveDialog1->Filter     = "BMP (*.bmp)|*.bmp|"
                                  "JPG (*.jpg)|*.jpg|"
                                  //"PNG (*.png)|*.png|"
                                  "WMF (*.wmf)|*.wmf|"
                                  "EMF (*.emf)|*.emf";

        SaveDialog1->FileName   = sNewFile;
        if (SaveDialog1->Execute() == false)
        {
            return ;
        }
        sNewFile = SaveDialog1->FileName;
        if (sNewFile == "")
        {
            return ;
        }

        nFilterIndex = SaveDialog1->FilterIndex;
    }
    __finally
    {
        if (SaveDialog1) delete SaveDialog1;
        ChDir(ExtractFilePath(Application->ExeName));
    }

    TGraphic *pGraphic  = NULL;
    TGraphic *pTmpImage = NULL;
    try
    {
        Application->ProcessMessages();

        try
        {
            bool bMetafile = false;
            switch (nFilterIndex)
            {
            case 1:  sNewFile = ChangeFileExt(sNewFile, ".bmp"); bMetafile = false; break;
            case 2:  sNewFile = ChangeFileExt(sNewFile, ".jpg"); bMetafile = false; break;
            //case 3:  sNewFile = ChangeFileExt(sNewFile, ".png"); bMetafile = false; break;
            case 3:  sNewFile = ChangeFileExt(sNewFile, ".wmf"); bMetafile = true;  break;
            case 4:  sNewFile = ChangeFileExt(sNewFile, ".emf"); bMetafile = true;  break;
            default: sNewFile = ChangeFileExt(sNewFile, ".bmp"); bMetafile = false; break;
            }

            // (TdxPNGImage)
            if (nFilterIndex == 3 || nFilterIndex == 4)
            {
                pGraphic = TvChart->CreateImage(__classid(Graphics::TMetafile));
                ((Graphics::TMetafile*)pGraphic)->Enhanced =  nFilterIndex == 4 ? true : false;
            }
            else
            {
                pGraphic = TvChart->CreateImage(__classid(Graphics::TBitmap));
            }

            //TPNGImage *pPng = new TPNGImage();
            switch (nFilterIndex)
            {
            case 2: pTmpImage = new TJPEGImage();  break;
            //case 3: pTmpImage = new TdxPNGImage(); break;
            }

            if (pTmpImage)
            {
                pTmpImage->Assign(pGraphic);
                pTmpImage->SaveToFile(sNewFile);
            }
            else
            {
                pGraphic->SaveToFile(sNewFile);
            }
        }
        catch(Exception & E)
        {
            String strMsg = lblSaveErr->Caption + "\r\n";//"파일로 저장하는 중에 에러가 발생했습니다.\r\n";
            strMsg += "[" + E.Message + "]\r\n";
            ShowMessage(strMsg);
            return ;
        }
    }
    __finally
    {
        if (pGraphic)
        {
            delete pGraphic;
        }
        if (pTmpImage)
        {
            delete pTmpImage;
        }
    }

    String sMsg = "";
    //sMsg = "[차트 이미지를\r\n" + sNewFile + "\r\n 로 생성 하였습니다.";
    sMsg = lblSaveOk->Caption + "\r\n[" + sNewFile + "]";
    MessageDlg(sMsg, mtInformation, TMsgDlgButtons() << mbOK, 0);

#if 0
WORD AFormat;
THandle AData;
HPALETTE APalette;
TGraphic *Image;

Image = ChartView->CreateImage(class TBitmap,0,0);
Image->SaveToClipboardFormat(AFormat,AData,APalette);
WORD AFormat;
THandle AData;
HPALETTE APalette;
TGraphic *Image;

Image = <ChartView>->CreateImage(__classid(Graphics::TBitmap));
Image->SaveToClipboardFormat(AFormat, AData, APalette);
Clipboard()->SetAsHandle(AFormat, AData);
#endif
}
//---------------------------------------------------------------------------

