#include "cdrapp.h"

// sr.ApplyUniformFill CreateCMYKColor(0, 100, 100, 0)
bool fill_red(corel *cdr)
{
    auto sr = cdr->ActiveSelectionRange;
    auto red = cdr->CreateCMYKColor(0, 100, 100, 0);
    sr->ApplyUniformFill(red);

    return true;
}

bool cql_OutlineColor(corel *cdr)
{
    auto col = cdr->CreateCMYKColor(0, 100, 100, 0);
    auto s = cdr->ActiveShape;
    col-> CopyAssign(s->Outline->Color);
    col->ConvertToRGB();

    auto r = col->RGBRed;
    auto g = col->RGBGreen;
    auto b = col->RGBBlue;

    char buf[256] = { 0 };
    sprintf(buf, "@Outline.Color.rgb[.r='%d' And .g='%d' And .b='%d']", r, g, b);
    auto cql = _bstr_t(buf);
    // MessageBox(NULL, cql, "cql 轮廓颜色", MB_ICONSTOP);
    auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
    sr->CreateSelection();

    strcpy(infobuf , buf);
    return true;
}

bool cql_FillColor(corel *cdr)
{
    auto col = cdr->CreateCMYKColor(0, 100, 100, 0);
    auto s = cdr->ActiveShape;
    col-> CopyAssign(s->Fill->UniformColor);
    col->ConvertToRGB();

    auto r = col->RGBRed;
    auto g = col->RGBGreen;
    auto b = col->RGBBlue;

    char buf[256] = { 0 };
    sprintf(buf, "@Fill.Color.rgb[.r='%d' And .g='%d' And .b='%d']", r, g, b);
    auto cql = _bstr_t(buf);

    auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
    sr->CreateSelection();

    strcpy(infobuf , buf);
    return true;
}

bool cql_SameSize(corel *cdr, double exp)
{
    cdr->ActiveDocument->Unit = cdrMillimeter;
    auto s = cdr->ActiveShape;

    exp += 0.01;
    char buf[256] = { 0 };
    sprintf(buf, "(@com.SizeWidth - %lf).abs() <  %lf and (@com.SizeHeight - %lf).abs() <  %lf", s->SizeWidth, exp, s->SizeHeight, exp);
    auto cql = _bstr_t(buf);

    //  MessageBox(NULL, cql, "cql 尺寸相同", MB_ICONSTOP);
    auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
    sr->CreateSelection();

    strcpy(infobuf , buf);
    return true;
}

bool Shapes_Filp(corel *cdr)
{
    BeginOpt(cdr);
    auto sr = cdr->ActiveSelectionRange;
    // CorelDRAW Shapes 物件 Item 编号从1开始
    for (auto i = 0; i != sr->Count; i++)
        sr->Shapes->Item[i + 1]->Flip(VGCore::cdrFlipHorizontal);

    EndOpt(cdr);
    return true;
}

bool  Clear_Fill(corel *cdr)
{
    cdr->ActiveSelection->Fill->ApplyNoFill();
    return true;
}

//  1- A program that will copy a selected object 1000 times
bool Copy_1K_Objects(corel *cdr, double distance)
{
  BeginOpt(cdr);

  auto sr = cdr->ActiveSelectionRange; // Get the selection range
  double sw = sr->SizeWidth;
  double sh = sr->SizeHeight;
  int n = 32;
  auto dup = sr->StepAndRepeat(n - 1, sw + distance, 0.0 , cdrModeOffset, cdrRight, cdrModeOffset, cdrDown);
  sr->AddRange(dup);
  auto dup_all = sr->StepAndRepeat(n - 1, 0.0 , sh + distance, cdrModeOffset, cdrRight, cdrModeOffset, cdrDown);
  sr->AddRange(dup_all);
  sr->CreateSelection();

  EndOpt(cdr);
  return true;
}

// 最高版本 CorelDRAW 2020 保存  CDR X4 格式
bool SaveAs_X4CDR(corel *cdr, const char *filePath)
{
   
    auto SaveOptions = cdr->CreateStructSaveAsOptions();
    SaveOptions->EmbedVBAProject = cdrTrue;
    SaveOptions->Filter = cdrCDR;
    SaveOptions->IncludeCMXData = cdrFalse;
    SaveOptions->Range = cdrAllPages;
    SaveOptions->EmbedICCProfile = cdrFalse;
    SaveOptions->Version = cdrVersion14;

    cdr->ActiveDocument->SaveAs(_bstr_t(filePath), SaveOptions);
    return true;
}

// CorelDRAW CPG 统一中英文字体 C++ 源码
bool setFontChinese(corel *cdr) {
  auto sr = cdr->ActiveSelectionRange;
  auto s = sr->FirstShape;
  s->Text->Story->LanguageID = cdrSimplifiedChinese;
  s->Text->Story->Font = L"Microsoft YaHei";
  s->Text->Story->Size = 18;
  return true;
}
