unit U_MakeEditObjectIcons;

interface

uses
  Windows, U_LNG, U_BaseConstants, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxStyles, cxCustomData, cxGraphics, cxFilter, cxData, cxEdit,
  DB, cxDBData, cxGridLevel, cxClasses, cxControls, cxGridCustomView,
  cxGridCustomTableView, cxGridTableView, cxGridDBTableView, cxGrid,
  StdCtrls, U_BaseCommon, XPMenu, ComCtrls, ToolWin, ActnList, ActnMan,
  cxLookAndFeelPainters, cxButtons, cxEditRepositoryItems, ExtCtrls,
  cxDataStorage, cxImage, cxSpinEdit, cxCheckBox, cxCurrencyEdit,
  cxColorComboBox, cxDBLookupComboBox, cxTextEdit, RzPanel, cxContainer,
  kbmMemTable, siComp, siLngLnk, cxLookAndFeels, Menus;

type

  TIconParams = record
    Image: TcxImage;
    IconBlk: TStream;
    IconBmp: TStream;
    IsEmpty: Boolean;
  end;

  TF_MakeEditObjectIcons = class(TForm)
    Panel_OKCancel: TRzPanel;
    Button_OK: TcxButton;
    Button_Cancel: TcxButton;
    Panel_Main: TRzPanel;
    Label_Name: TLabel;
    Edit_Name: TcxTextEdit;
    RzGroupBox1: TRzGroupBox;
    Label_ProjBmp: TLabel;
    Label_ActiveBmp: TLabel;
    Image_ProjBmp: TcxImage;
    Button_ProjBmp: TcxButton;
    Image_ActiveBmp: TcxImage;
    Button_ActiveBmp: TcxButton;
    OpenDialog: TOpenDialog;
    lng_Forms: TsiLangLinked;
    btExchangeImg: TcxButton;
    procedure Panel_OKCancelResize(Sender: TObject);
    procedure Button_ProjBmpClick(Sender: TObject);
    procedure Button_ActiveBmpClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btExchangeImgClick(Sender: TObject);
  private
    GForm: TForm;

    FProjIconParams: TIconParams;
    FActiveIconParams: TIconParams;
  public
    GFormMode: TFormMode;
    GID: Integer;

    constructor Create(AOwner: TComponent; AForm: TForm);
    destructor  Destroy; override;

    function MakeEdit(AFormMode: TFormMode; AMemTable: TkbmMemTable;
                  ADefNewName: String = ''; ADefNewFileBmp: String = '';
                  ADefNewFileBlk: String = ''; ADefIconType: Integer = oitNone): Boolean;
    function LoadPictureFromFile(var AIconParams, ASecondIconParams: TIconParams;
              AFileIconBMP, AFileIconBLK: String): Boolean;
    function LoadPictureFromFileOne(var AIconParams: TIconParams;
              AFileIconBMP, AFileIconBLK: String): Boolean;
    procedure LoadPictureFromOpenFileDialog(var AIconParams, ASecondIconParams: TIconParams);
  end;

//var
  //F_ComponentIcons: TF_ComponentIcons;

implementation
Uses Unit_DM_SCS, U_Main, U_CaseForm;
{$R *.dfm}

{ TF_ComponentIcons }





// #############################################################################
// #############################################################################
constructor TF_MakeEditObjectIcons.Create(AOwner: TComponent; AForm: TForm);
begin
    GForm := AForm;
    inherited Create(AOwner);
end;

destructor TF_MakeEditObjectIcons.Destroy;
begin
  inherited;  //
end;

procedure TF_MakeEditObjectIcons.Panel_OKCancelResize(Sender: TObject);
begin
    SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;


function TF_MakeEditObjectIcons.MakeEdit(AFormMode: TFormMode; AMemTable: TkbmMemTable;
  ADefNewName: String = ''; ADefNewFileBmp: String = ''; ADefNewFileBlk: String = '';
  ADefIconType: Integer = oitNone): Boolean;
var
  ModRes: TModalResult;
  BlkStreamSize: Integer;
  BmpStreamSize: Integer;

  MakeEdit: TMakeEdit;
  ObjectIconID: Integer;
  ObjectIconInfo: TObjectIconInfo;
  FilePath, FileName, FileExt: string;
begin
  Result := false;
  MakeEdit := GetMakeEditByFormMode(AFormMode);

  GID := -1;
  ObjectIconID := 0;
  try
    with TF_Main(GForm).FNormBase.DM do
    begin
      GFormMode := AFormMode;

      FProjIconParams.Image := Image_ProjBmp;
      FProjIconParams.Image.Clear;
      FProjIconParams.Image.Picture.Bitmap.FreeImage;
      FProjIconParams.IconBlk := TMemoryStream.Create;
      FProjIconParams.IconBlk.Position := 0;
      FProjIconParams.IconBmp := TMemoryStream.Create;
      FProjIconParams.IconBmp.Position := 0;
      FProjIconParams.IsEmpty := true;

      FActiveIconParams.Image := Image_ActiveBmp;
      FActiveIconParams.Image.Clear;
      FActiveIconParams.Image.Picture.Bitmap.FreeImage;
      FActiveIconParams.IconBlk := TMemoryStream.Create;
      FActiveIconParams.IconBlk.Position := 0;
      FActiveIconParams.IconBmp := TMemoryStream.Create;
      FActiveIconParams.IconBmp.Position := 0;
      FActiveIconParams.IsEmpty := true;


      try
        if GFormMode = fmMake then
        begin
          Caption := cMakeEditObjectIcons_Msg1_1;
          Edit_Name.Text := ADefNewName;

          //if (ADefNewFileBmp <> '') and (ADefNewFileBlk <> '') then
          //  LoadPictureFromFile(FProjIconParams, FActiveIconParams, ADefNewFileBmp, ADefNewFileBlk);
        end
        else
        begin
          Caption := cMakeEditObjectIcons_Msg1_2;

          if AMemTable <> nil then
          begin
            ObjectIconID := AMemTable.FieldByName(fnID).AsInteger;
            Edit_Name.Text := AMemTable.FieldByName(fnName).AsString;
            TBlobField(AMemTable.FieldByName(fnProjBlk)).SaveToStream(FProjIconParams.IconBlk);
            TBlobField(AMemTable.FieldByName(fnProjBmp)).SaveToStream(FProjIconParams.IconBmp);
            TBlobField(AMemTable.FieldByName(fnActiveBlk)).SaveToStream(FActiveIconParams.IconBlk);
            TBlobField(AMemTable.FieldByName(fnActiveBmp)).SaveToStream(FActiveIconParams.IconBmp);
          end
          else
          begin
            ObjectIconID := DataSet_OBJECT_ICONS.FN(fnID).AsInteger;
            Edit_Name.Text := DataSet_OBJECT_ICONS.FN(fnName).AsString;
            TBlobField(DataSet_OBJECT_ICONS.FN(fnProjBlk)).SaveToStream(FProjIconParams.IconBlk);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnProjBmp)).SaveToStream(FProjIconParams.IconBmp);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnActiveBlk)).SaveToStream(FActiveIconParams.IconBlk);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnActiveBmp)).SaveToStream(FActiveIconParams.IconBmp);
          end;

          FProjIconParams.IconBlk.Position := 0;
          FProjIconParams.IconBmp.Position := 0;
          FActiveIconParams.IconBlk.Position := 0;
          FActiveIconParams.IconBmp.Position := 0;

          BlkStreamSize := FProjIconParams.IconBlk.Size;
          BmpStreamSize := FProjIconParams.IconBmp.Size;
          if (BlkStreamSize > 0) and (BmpStreamSize > 0) then
            FProjIconParams.IsEmpty := false;

          BlkStreamSize := FActiveIconParams.IconBlk.Size;
          BmpStreamSize := FActiveIconParams.IconBmp.Size;
          if (BlkStreamSize > 0) and (BmpStreamSize > 0) then
            FActiveIconParams.IsEmpty := false;

          Image_ProjBmp.Picture.Bitmap.LoadFromStream(FProjIconParams.IconBmp);
          Image_ActiveBmp.Picture.Bitmap.LoadFromStream(FActiveIconParams.IconBmp);
        end;

        //*** Загрузить УГО с листа
        if (ADefNewFileBmp <> '') and (ADefNewFileBlk <> '') and (ADefIconType <> oitNone) then
          if FileExists(ADefNewFileBmp) and FileExists(ADefNewFileBlk) then
          begin
            if (ADefIconType = oitProjectible) or (ADefIconType = oitAll) then
            begin
              if (ADefIconType = oitProjectible) then
                LoadPictureFromFile(FProjIconParams, FActiveIconParams, ADefNewFileBmp, ADefNewFileBlk)
              else
              begin
                FilePath := ExtractFileDir(ADefNewFileBmp);
                FileName := ExtractFileName(ADefNewFileBmp);
                FileExt := ExtractFileExt(ADefNewFileBmp);
                FileName := ReplaceStr(FileName, FileExt, '', False);
                if FileExists(FilePath + '\' + FileName + '_active' + FileExt) then
                begin
                  //LoadPictureFromFile(FProjIconParams, FActiveIconParams, ADefNewFileBmp, ADefNewFileBlk);
                  //LoadPictureFromFile(FActiveIconParams, FProjIconParams, FilePath + '\' + FileName + '_active' + FileExt,
                  //                    FilePath + '\' + FileName + '_active' + '.blk');
                  LoadPictureFromFileOne(FProjIconParams, ADefNewFileBmp, ADefNewFileBlk);
                  LoadPictureFromFileOne(FActiveIconParams, FilePath + '\' + FileName + '_active' + FileExt,
                                      FilePath + '\' + FileName + '_active' + '.blk');
                end
                else
                  LoadPictureFromFile(FProjIconParams, FActiveIconParams, ADefNewFileBmp, ADefNewFileBlk)
              end;
            end
            else
            if ADefIconType = oitActive then
              LoadPictureFromFile(FActiveIconParams, FProjIconParams, ADefNewFileBmp, ADefNewFileBlk);
          end;

        ModRes := ShowModal;
        if ModRes = mrOk then
        begin
          FProjIconParams.IconBlk.Position := 0;
          FProjIconParams.IconBmp.Position := 0;
          FActiveIconParams.IconBlk.Position := 0;
          FActiveIconParams.IconBmp.Position := 0;

          if AMemTable <> nil then
          begin
            if GFormMode = fmMake then
              AMemTable.Append;
            AMemTable.Edit;
            AMemTable.FieldByName(fnName).AsString := Edit_Name.Text;
            TBlobField(AMemTable.FieldByName(fnProjBlk)).LoadFromStream(FProjIconParams.IconBlk);
            TBlobField(AMemTable.FieldByName(fnProjBmp)).LoadFromStream(FProjIconParams.IconBmp);
            TBlobField(AMemTable.FieldByName(fnActiveBlk)).LoadFromStream(FActiveIconParams.IconBlk);
            TBlobField(AMemTable.FieldByName(fnActiveBmp)).LoadFromStream(FActiveIconParams.IconBmp);
            AMemTable.FieldByName(fnIsModified).AsBoolean := true;
            AMemTable.Post;
            GID := AMemTable.FieldByName(fnID).AsInteger;
          end
          else
          begin
            ZeroMemory(@ObjectIconInfo, SizeOf(TObjectIconInfo));
            ObjectIconInfo.ID := ObjectIconID;
            ObjectIconInfo.Name := Edit_Name.Text;
            ObjectIconInfo.ProjBlk := FProjIconParams.IconBlk;
            ObjectIconInfo.ProjBmp := FProjIconParams.IconBmp;
            ObjectIconInfo.ActiveBlk := FActiveIconParams.IconBlk;
            ObjectIconInfo.ActiveBmp := FActiveIconParams.IconBmp;
            SaveObjectIcon(MakeEdit, @ObjectIconInfo);
            if MakeEdit = meMake then
              GID := ObjectIconInfo.ID
            else
            if MakeEdit = meEdit then
              DataSet_OBJECT_ICONS.FullRefresh;

              
            {
            if GFormMode = fmMake then
              DataSet_OBJECT_ICONS.Append;
            DataSet_OBJECT_ICONS.Edit;
            DataSet_OBJECT_ICONS.FN(fnName).AsString := Edit_Name.Text;
            TBlobField(DataSet_OBJECT_ICONS.FN(fnProjBlk)).LoadFromStream(FProjIconParams.IconBlk);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnProjBmp)).LoadFromStream(FProjIconParams.IconBmp);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnActiveBlk)).LoadFromStream(FActiveIconParams.IconBlk);
            TBlobField(DataSet_OBJECT_ICONS.FN(fnActiveBmp)).LoadFromStream(FActiveIconParams.IconBmp);
            DataSet_OBJECT_ICONS.Post;
            GID := DataSet_OBJECT_ICONS.FN(fnID).AsInteger;}
          end;

          Result := true;
        end;
      finally
        FProjIconParams.IconBlk.Free;
        FProjIconParams.IconBmp.Free;
        FActiveIconParams.IconBlk.Free;
        FActiveIconParams.IconBmp.Free;
      end;
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditObjectIcons.MakeEdit: '+E.Message);
  end;
end;

function TF_MakeEditObjectIcons.LoadPictureFromFile(var AIconParams, ASecondIconParams: TIconParams;
  AFileIconBMP, AFileIconBLK: String): Boolean;
var
  FileStream: TStream;
begin
  Result := false;
  if FileExists(AFileIconBMP) and FileExists(AFileIconBLK) then
  begin
    Result := true;
    FileStream := TFileStream.Create(AFileIconBLK, fmOpenRead);
    try
      FileStream.Position := 0;
      AIconParams.IconBlk.Position := 0;
      AIconParams.IconBlk.Size := 0;
      AIconParams.IconBlk.CopyFrom(FileStream, FileStream.Size);
      AIconParams.IconBlk.Position := 0;
    finally
      FileStream.Free;
    end;

    FileStream := TFileStream.Create(AFileIconBMP, fmOpenRead);
    try
      FileStream.Position := 0;
      AIconParams.IconBmp.Position := 0;
      AIconParams.IconBmp.Size := 0;
      AIconParams.IconBmp.CopyFrom(FileStream, FileStream.Size);
      AIconParams.IconBmp.Position := 0;
    finally
      FileStream.Free;
    end;

    AIconParams.Image.Clear;
    AIconParams.Image.Picture.Bitmap.FreeImage;
    AIconParams.Image.Picture.Bitmap.LoadFromStream(AIconParams.IconBmp);
    AIconParams.IsEmpty := false;

    if ASecondIconParams.IsEmpty then
    begin
      ASecondIconParams.Image.Picture.Bitmap.Assign(AIconParams.Image.Picture.Bitmap);

      AIconParams.IconBlk.Position := 0;
      ASecondIconParams.IconBlk.Position := 0;
      ASecondIconParams.IconBlk.CopyFrom(AIconParams.IconBlk, AIconParams.IconBlk.Size);
      ASecondIconParams.IconBlk.Position := 0;

      AIconParams.IconBmp.Position := 0;
      ASecondIconParams.IconBmp.Position := 0;
      ASecondIconParams.IconBmp.CopyFrom(AIconParams.IconBmp, AIconParams.IconBmp.Size);
      ASecondIconParams.IconBmp.Position := 0;

      ASecondIconParams.IsEmpty := false;
    end;
  end;
end;

function TF_MakeEditObjectIcons.LoadPictureFromFileOne(var AIconParams: TIconParams;
  AFileIconBMP, AFileIconBLK: String): Boolean;
var
  FileStream: TStream;
begin
  Result := false;
  if FileExists(AFileIconBMP) and FileExists(AFileIconBLK) then
  begin
    Result := true;
    FileStream := TFileStream.Create(AFileIconBLK, fmOpenRead);
    try
      FileStream.Position := 0;
      AIconParams.IconBlk.Position := 0;
      AIconParams.IconBlk.Size := 0;
      AIconParams.IconBlk.CopyFrom(FileStream, FileStream.Size);
      AIconParams.IconBlk.Position := 0;
    finally
      FileStream.Free;
    end;

    FileStream := TFileStream.Create(AFileIconBMP, fmOpenRead);
    try
      FileStream.Position := 0;
      AIconParams.IconBmp.Position := 0;
      AIconParams.IconBmp.Size := 0;
      AIconParams.IconBmp.CopyFrom(FileStream, FileStream.Size);
      AIconParams.IconBmp.Position := 0;
    finally
      FileStream.Free;
    end;

    AIconParams.Image.Clear;
    AIconParams.Image.Picture.Bitmap.FreeImage;
    AIconParams.Image.Picture.Bitmap.LoadFromStream(AIconParams.IconBmp);
    AIconParams.IsEmpty := false;
  end;
end;


procedure TF_MakeEditObjectIcons.LoadPictureFromOpenFileDialog(var AIconParams, ASecondIconParams: TIconParams);
const
  extBLK = '.blk';
  extBMP = '.bmp';
var FilePath: String;
    FileName: String;
    FileExt: String;
begin
  try
    if OpenDialog.Execute then
    begin
      FilePath := ExtractFilePath(OpenDialog.FileName);
      FileName := ExtractFileName(OpenDialog.FileName);
      FileExt := ExtractFileExt(OpenDialog.FileName);
      if LowerCase(FileExt) = LowerCase(extBMP) then
      begin
        //StrLen := Length(FileName);
        SetLength(FileName, Length(FileName) - Length(FileExt));
        if FileExists(FilePath + FileName + extBLK) then
        begin
          LoadPictureFromFile(AIconParams, ASecondIconParams,
            FilePath + FileName + extBMP,
            FilePath + FileName + extBLK);

          if Edit_Name.Text = '' then
            Edit_Name.Text := ExtractFileNameOnly(OpenDialog.FileName);
        end
        else
          ShowMessageByType(Self.Handle, smtDisplay, cMakeEditObjectIcons_Msg2_1+' "'+FileName + FileExt+'" '+cMakeEditObjectIcons_Msg2_2+' "'+FileName + extBLK+'".', Caption, MB_OK or MB_ICONINFORMATION);
      end
      else
        ShowMessageByType(Self.Handle, smtDisplay, cMakeEditObjectIcons_Msg3, Caption, MB_OK or MB_ICONINFORMATION);
    end;
  except
    on E: Exception do AddExceptionToLog('TF_MakeEditObjectIcons.LoadPictureFromFile: '+E.Message);
  end;
end;


procedure TF_MakeEditObjectIcons.Button_ProjBmpClick(Sender: TObject);
begin
  LoadPictureFromOpenFileDialog(FProjIconParams, FActiveIconParams);
end;

procedure TF_MakeEditObjectIcons.Button_ActiveBmpClick(Sender: TObject);
begin
  LoadPictureFromOpenFileDialog(FActiveIconParams, FProjIconParams);
end;

procedure TF_MakeEditObjectIcons.FormCreate(Sender: TObject);
begin
    OpenDialog.InitialDir := ExtractSaveDirSimple; //07.04.2009 ExtractFilePath(Application.ExeName);
end;

procedure TF_MakeEditObjectIcons.FormShow(Sender: TObject);
begin
  if GFormMode = fmMake then
    Edit_Name.SetFocus;
end;

procedure TF_MakeEditObjectIcons.btExchangeImgClick(Sender: TObject);
var
  TmpBool: Boolean;
begin
  ExchangeObjects(FActiveIconParams.IconBlk, FProjIconParams.IconBlk);
  ExchangeObjects(FActiveIconParams.IconBmp, FProjIconParams.IconBmp);

  TmpBool := FActiveIconParams.IsEmpty;
  FActiveIconParams.IsEmpty := FProjIconParams.IsEmpty;
  FProjIconParams.IsEmpty := TmpBool;

  FActiveIconParams.Image.Clear;
  FActiveIconParams.Image.Picture.Bitmap.FreeImage;
  FActiveIconParams.IconBmp.Position := 0;
  FActiveIconParams.Image.Picture.Bitmap.LoadFromStream(FActiveIconParams.IconBmp);

  FProjIconParams.Image.Clear;
  FProjIconParams.Image.Picture.Bitmap.FreeImage;
  FProjIconParams.IconBmp.Position := 0;
  FProjIconParams.Image.Picture.Bitmap.LoadFromStream(FProjIconParams.IconBmp);
end;

end.
