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

#include <vcl.h>
#pragma hdrstop

#include "MainUnit.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "ZipBuilder"
#pragma resource "*.dfm"
TMakeSFX *MakeSFX;
//---------------------------------------------------------------------------
__fastcall TMakeSFX::TMakeSFX(TComponent* Owner)
  : TForm(Owner)
{

}
//---------------------------------------------------------------------------
void TMakeSFX::UpdateControlsFromZipBuilder(void)
{ // let form's controls reflect TZipMaster's properties
  if(!FCanUpdate) return;
  CmdLineCB->Checked = ZipBuilder1->SFXOptions.Contains(SFXAskCmdLine);
  FileListCB->Checked = ZipBuilder1->SFXOptions.Contains(SFXAskFiles);
  HideOverWriteCB->Checked = ZipBuilder1->SFXOptions.Contains(SFXHideOverWriteBox);
  AutoRunCB->Checked = ZipBuilder1->SFXOptions.Contains(SFXAutoRun);
  NoMsgShowCB->Checked = ZipBuilder1->SFXOptions.Contains(SFXNoSuccessMsg);
  switch(ZipBuilder1->SFXOverWriteMode)
    {
      case OvrConfirm: DfltOverWriteGrp->ItemIndex = 0; break;
      case OvrAlways:  DfltOverWriteGrp->ItemIndex = 1; break;
      case OvrNever:   DfltOverWriteGrp->ItemIndex = 2;
    }
  CaptionEdit->Text = ZipBuilder1->SFXCaption;
  DirectoryEdit->Text = ZipBuilder1->SFXDefaultDir;
  CommandEdit->Text = ZipBuilder1->SFXCommandLine;
  if(ZipBuilder1->SFXMessage.Length() &&((ZipBuilder1->SFXMessage[1] == '\1') ||
                                          (ZipBuilder1->SFXMessage[1] == '\2')))
  {
    if(ZipBuilder1->SFXMessage[1] == '\1') GroupBox1->ItemIndex = 0;
    else GroupBox1->ItemIndex = 1;
    M_SFXMessage->Text = ZipBuilder1->SFXMessage.SubString(2, ZipBuilder1->SFXMessage.Length() -1);
  }
  else
  {
    M_SFXMessage->Text = ZipBuilder1->SFXMessage;
    GroupBox1->ItemIndex = 2;
  }
  if(ImageList1->Count == 2) ImageList1->Delete(1);
  if(IconEdit->Text == "")
  {
    if(ZipBuilder1->SFXIcon->Empty)
    {
      ImageList1->GetIcon(int(AutoRunCB->Checked) , Image1->Picture->Icon );
      ZipBuilder1->SFXIcon = Image1->Picture->Icon;
    }
    else
    {
      ImageList1->AddIcon(ZipBuilder1->SFXIcon );
      ImageList1->GetIcon(1, Image1->Picture->Icon);
      OrigIconRB->Checked = true;
    }
  }
  OrigIconRB->Enabled = (ImageList1->Count == 2) ? true : false;
}
//---------------------------------------------------------------------------
void TMakeSFX::UpdateZipBuilderFromControls(void)
{
  ZipBuilder1->SFXOptions.Clear();
  ZipBuilder1->SFXOptions << SFXCheckSize; // compatibility
  if(CmdLineCB->Checked) ZipBuilder1->SFXOptions = ZipBuilder1->SFXOptions << SFXAskCmdLine;
  if(FileListCB->Checked) ZipBuilder1->SFXOptions = ZipBuilder1->SFXOptions << SFXAskFiles;
  if(HideOverWriteCB->Checked) ZipBuilder1->SFXOptions = ZipBuilder1->SFXOptions << SFXHideOverWriteBox;
  if(AutoRunCB->Checked) ZipBuilder1->SFXOptions = ZipBuilder1->SFXOptions << SFXAutoRun;
  if(NoMsgShowCB->Checked) ZipBuilder1->SFXOptions = ZipBuilder1->SFXOptions << SFXNoSuccessMsg;
  switch(DfltOverWriteGrp->ItemIndex)
  {
    case 0: ZipBuilder1->SFXOverWriteMode = OvrConfirm; break;
    case 1: ZipBuilder1->SFXOverWriteMode = OvrAlways; break;
    case 2: ZipBuilder1->SFXOverWriteMode = OvrNever; break;
  }
  ZipBuilder1->SFXCaption = CaptionEdit->Text;
  ZipBuilder1->SFXDefaultDir = DirectoryEdit->Text;
  ZipBuilder1->SFXCommandLine = CommandEdit->Text;
  ZipBuilder1->SFXMessage = "";
  if(GroupBox1->ItemIndex == 1) ZipBuilder1->SFXMessage = "\1";
  if(GroupBox1->ItemIndex == 2) ZipBuilder1->SFXMessage = "\2";
  ZipBuilder1->SFXMessage += M_SFXMessage->Text;
}
//---------------------------------------------------------------------------
void TMakeSFX::LoadCustomIcon(AnsiString IconPath,int IconIndex)
{ // load a custom icon
  Image1->Picture->Icon->ReleaseHandle();
  ZipBuilder1->SFXIcon->ReleaseHandle();
  HICON IconHandle = ExtractIcon(HInstance, IconPath.c_str(), IconIndex);
  if((IconHandle != 0) && ((int)IconHandle != 1))
  {
    Image1->Picture->Icon->Handle = IconHandle;
    ZipBuilder1->SFXIcon->Handle = IconHandle;
  }
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::ExecButClick(TObject* /*Sender*/)
{                                                                                                
  // don't change forms's controls before conversion
  // manual modifications could be lost
  FCanUpdate = false;
  try
  {
    ZipBuilder1->ZipFileName = FileEdit->Text;
  }
  __finally
  {
    FCanUpdate = true;
  }
  // set sfx options from controls
  UpdateZipBuilderFromControls();
  if(FileExists(ZipBuilder1->ZipFileName))
  {
    int ConvertErr;
    if(UpperCase(ExtractFileExt(ZipBuilder1->ZipFileName)) == ".ZIP")
        ConvertErr = ZipBuilderSFX1->ConvertToSFX();
    else ConvertErr = ZipBuilderSFX1->ConvertToZip();
    if(!ConvertErr)
    {
      UpdateControlsFromZipBuilder();
      ShowMessage(RS_FilenameIsNow + ZipBuilder1->ZipFileName);
    }
    else
    {
      AnsiString Message;
      Message.printf(RS_ErrorDOccuredInConvertingFile, ConvertErr);
      ShowMessage(Message);
    }
  }
  else ExecBut->Enabled = false;  // file not found, so no conversion possible
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::CloseButClick(TObject* /*Sender*/)
{   // close that app
  Close();
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::BitBtn3Click(TObject* /*Sender*/)
{  // select the file to convert
  if(OpenDialog2->Execute())
  {
    if(FileExists(OpenDialog2->FileName)) FileEdit->Text = OpenDialog2->FileName;
  }
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::FileEditChange(TObject* /*Sender*/)
{ // filename has changed, check
  ExecBut->Enabled = FileExists(FileEdit->Text);
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::BitBtn1Click(TObject* /*Sender*/)
{ // select a default extraction directory
  AnsiString TempStr = DirectoryEdit->Text;
  if(SelectDirectory(TempStr,TSelectDirOpts()  ,0)) DirectoryEdit->Text = TempStr;
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::FormShow(TObject* /*Sender*/)
{ // set form's state to show TZipBuilder's properties
  FCanUpdate = true;
  UpdateControlsFromZipBuilder();
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::BitBtn2Click(TObject* /*Sender*/)
{  // load a different icon
  if(OpenDialog1->Execute())
  {
    IconEdit->Text = OpenDialog1->FileName;
    DefIconRB->Checked = false;
    if(UpperCase(ExtractFileExt(OpenDialog1->FileName)) != ".ICO")
    {
      LoadCustomIcon(OpenDialog1->FileName,StrToIntDef(IconIndexEdit->Text, 0));
      IconIndexLabel->Enabled = true;
      IconIndexEdit->Enabled = true;
      IconIndexUD->Enabled = true;
      IconIndexEdit->Color = clWindow;
    }
    else
    {
      Image1->Picture->Icon->LoadFromFile(OpenDialog1->FileName);
      IconIndexEdit->Text = "0";
      IconIndexLabel->Enabled = false;
      IconIndexEdit->Enabled = false;
      IconIndexUD->Enabled = false;
      IconIndexEdit->Color = clBtnFace;
      ZipBuilder1->SFXIcon = Image1->Picture->Icon;
    }
  }
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::DefIconRBClick(TObject* Sender)
{
  TRadioButton* button = dynamic_cast<TRadioButton*>(Sender);
  ImageList1->GetIcon(button->Tag, Image1->Picture->Icon);
  ZipBuilder1->SFXIcon = Image1->Picture->Icon;
  IconEdit->Text = "";
  IconIndexEdit->Text = "0";
  IconIndexLabel->Enabled = false;
  IconIndexEdit->Enabled = false;
  IconIndexUD->Enabled = false;
  IconIndexEdit->Color = clBtnFace;
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::IconIndexUDClick(TObject* /*Sender*/,
      TUDBtnType /*Button*/)
{
  LoadCustomIcon(IconEdit->Text, StrToIntDef(IconIndexEdit->Text, 0));
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::IconEditKeyPress(TObject* /*Sender*/, char& /*Key*/)
{
 if(FileExists(IconEdit->Text))
      LoadCustomIcon(IconEdit->Text, StrToIntDef(IconIndexEdit->Text, 0));
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::ZipBuilder1DirUpdate(TObject* /*Sender*/)
{
 if(!FCanUpdate) return;
 FileEdit->Text = ZipBuilder1->ZipFileName;
 UpdateControlsFromZipBuilder();
}
//---------------------------------------------------------------------------
void __fastcall TMakeSFX::M_SFXMessageChange(TObject* /*Sender*/)
{
  int i = M_SFXMessage->Text.Length();
  L_CharsCountSFXMessage->Caption = i;
  L_CharsCountSFXMessage->Color = (i > 255) ? clRed : clBtnFace;
}
//---------------------------------------------------------------------------

