unit uInsertVideoInDocument;
{< form toadjust video setting before insert}

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.ExtCtrls, Vcl.DBCtrls, Vcl.Samples.Spin;

type
  TInsertVideoInDocument = class(TForm)
    Image: TImage;
    pPictureSettings: TPanel;
    bInsertInDocument: TBitBtn;
    DBFileName: TDBText;
    lOriginSize: TLabel;
    lSizeInDoc: TLabel;
    seWidth: TSpinEdit;
    seHeight: TSpinEdit;
    cbSizeTemplates: TComboBox;
    cbSaveProportions: TCheckBox;
    procedure bInsertInDocumentClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure cbSizeTemplatesSelect(Sender: TObject);
    procedure seWidthChange(Sender: TObject);
    procedure seHeightChange(Sender: TObject);
    procedure cbSaveProportionsClick(Sender: TObject);
  private
    { Private declarations }

    FWidth:integer;
    FHeight:integer;

    FOriginWidth:integer;
    FOriginHeight:integer;

  procedure CalcNewSize(

  APercentOfOrigin:integer;
  AOriginWidth:Integer;
  AOriginHeight:Integer;
  var NewWidth:Integer;
  var NewHeight:Integer

  );

  public
    { Public declarations }

    constructor Create(AOwner: TComponent); override;


  end;

//var
//  InsertPictureInDocument: TInsertPictureInDocument;

implementation

{$R *.dfm}

uses uVisualFrame;
var VisualFrame_HTTP:TVisualFrame_HTTP;



procedure TInsertVideoInDocument.bInsertInDocumentClick(Sender: TObject);
begin
ModalResult:=mrOk;
end;


procedure TInsertVideoInDocument.

CalcNewSize(

  APercentOfOrigin:integer;
  AOriginWidth:Integer;
  AOriginHeight:Integer;
  var NewWidth:Integer;
  var NewHeight:Integer

  );

begin
//

NewWidth:=Trunc( AOriginWidth*(APercentOfOrigin/100) );
NewHeight:=Trunc( AOriginHeight*(APercentOfOrigin/100) );

end;

procedure TInsertVideoInDocument.cbSaveProportionsClick(Sender: TObject);
var k:real;

begin

if cbSaveProportions.Checked then
seWidth.OnChange(Self);

end;

procedure TInsertVideoInDocument.cbSizeTemplatesSelect(Sender: TObject);
begin

 case cbSizeTemplates.ItemIndex of

  0:

  CalcNewSize(
  33, // 33% of OriginPictureSize
  FOriginWidth,
  FOriginHeight,
  FWidth,
  FHeight
  );

  1:
   CalcNewSize(
  66, // 66% of OriginPictureSize
  FOriginWidth,
  FOriginHeight,
  FWidth,
  FHeight
  );

  2:
   CalcNewSize(
  90, // 66% of OriginPictureSize
  FOriginWidth,
  FOriginHeight,
  FWidth,
  FHeight
  );


  3:
   CalcNewSize(
  100, // 100% of OriginPictureSize
  FOriginWidth,
  FOriginHeight,
  FWidth,
  FHeight
  );

 end;



 //off
 seWidth.OnChange:=nil;
 seHeight.OnChange:=nil;

 //setting
 seWidth.Value:=FWidth;
 seHeight.Value:=FHeight;

 //on
 seWidth.OnChange:=seWidthChange;
 seHeight.OnChange:=seHeightChange;



end;

constructor TInsertVideoInDocument.Create(AOwner: TComponent);
begin
  inherited;



    if (AOwner is TVisualFrame_HTTP)  then
    VisualFrame_HTTP:=(AOwner as TVisualFrame_HTTP);


end;

procedure TInsertVideoInDocument.FormShow(Sender: TObject);
begin



    if Assigned(VisualFrame_HTTP) then

    with VisualFrame_HTTP do begin

     begin

     DBFileName.DataSource:=DataSource;
     DBFileName.DataField:='name';

     end;



    end;


   // Origin Size

   FOriginWidth:=640;//Image.Picture.Bitmap.Width;
   FOriginHeight:=480;//Image.Picture.Bitmap.Height;

   lOriginSize.Caption:='Исходный размер '+
  // lOriginSize.Caption:=lOriginSize.Caption+' '+
   FOriginWidth.ToString()+
   ' x '+
   FOriginHeight.ToString();


   //SetSize

   if (FOriginWidth<100) or (FOriginHeight<100) then



   begin
   //Set FullSize
   Image.Stretch:=false;


   cbSizeTemplates.ItemIndex:=3;
   cbSizeTemplates.OnSelect(Self);
   end else

   begin
   //SetMiddleSize
   cbSizeTemplates.ItemIndex:=1;
   cbSizeTemplates.OnSelect(Self);
   Image.Stretch:=True;
   end;


end;

procedure TInsertVideoInDocument.seHeightChange(Sender: TObject);
var k:real;
begin

cbSizeTemplates.ItemIndex:=4;

if not cbSaveProportions.Checked then Exit;


// switching off onChange Handler to avoid cycle changes
seWidth.OnChange:=nil;


//calculating coeff
k:=(

 (Image.Picture.Bitmap.Width/Image.Picture.Bitmap.Height)

  );



seWidth.Value:=Trunc(seHeight.Value/k);

// switching ON onChange Handler to avoid cycle changes
seWidth.OnChange:=seWidthChange;

end;

procedure TInsertVideoInDocument.seWidthChange(Sender: TObject);
var k:real;
begin

cbSizeTemplates.ItemIndex:=4;

if not cbSaveProportions.Checked then Exit;

// switching off onChange Handler to avoid cycle changes
seHeight.OnChange:=nil;


//calculating coeff
k:=(

 (Image.Picture.Bitmap.Width/Image.Picture.Bitmap.Height)

  );



seHeight.Value:=Trunc(seWidth.Value/k);

// switching ON onChange Handler to avoid cycle changes
seHeight.OnChange:=seHeightChange;

end;

end.
