{"id":1151,"date":"2014-04-22T22:03:12","date_gmt":"2014-04-22T21:03:12","guid":{"rendered":"http:\/\/tulip-ui.azurewebsites.net\/?page_id=1151"},"modified":"2014-04-22T23:13:56","modified_gmt":"2014-04-22T22:13:56","slug":"tulip-ui-forms-pas","status":"publish","type":"page","link":"http:\/\/tulip-ui.azurewebsites.net\/?page_id=1151","title":{"rendered":"Tulip.UI.Forms.pas"},"content":{"rendered":"<div style=\"background-color: #FFFFFF; width: 680px\">\n<p style=\"text-align: right;\"><a title=\"Source Files\" href=\"http:\/\/tulip-ui.azurewebsites.net\/?page_id=1091\">Source Files<\/a><\/p>\n<p><code><\/p>\n<pre>\r\n{******************************************************************************}\r\n{                                                                              }\r\n{                        Tulip - User Interface Library                        }\r\n{                                                                              }\r\n{         Copyright(c) 2012 - 2013 Marcos Gomes. All rights Reserved.          }\r\n{                                                                              }\r\n{  --------------------------------------------------------------------------  }\r\n{                                                                              }\r\n{  This product is based on Asphyre Sphinx (c) 2000 - 2012  Yuriy Kotsarenko.  }\r\n{       All rights reserved. Official web site: http:\/\/www.afterwarp.net       }\r\n{                                                                              }\r\n{******************************************************************************}\r\n{                                                                              }\r\n{  Important Notice:                                                           }\r\n{                                                                              }\r\n{  If you modify\/use this code or one of its parts either in original or       }\r\n{  modified form, you must comply with Mozilla Public License Version 2.0,     }\r\n{  including section 3, \"Responsibilities\". Failure to do so will result in    }\r\n{  the license breach, which will be resolved in the court. Remember that      }\r\n{  violating author's rights either accidentally or intentionally is           }\r\n{  considered a serious crime in many countries. Thank you!                    }\r\n{                                                                              }\r\n{  !! Please *read* Mozilla Public License 2.0 document located at:            }\r\n{  http:\/\/www.mozilla.org\/MPL\/                                                 }\r\n{                                                                              }\r\n{  --------------------------------------------------------------------------  }\r\n{                                                                              }\r\n{  The contents of this file are subject to the Mozilla Public License         }\r\n{  Version 2.0 (the \"License\"); you may not use this file except in            }\r\n{  compliance with the License. You may obtain a copy of the License at        }\r\n{  http:\/\/www.mozilla.org\/MPL\/                                                 }\r\n{                                                                              }\r\n{  Software distributed under the License is distributed on an \"AS IS\"         }\r\n{  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the     }\r\n{  License for the specific language governing rights and limitations          }\r\n{  under the License.                                                          }\r\n{                                                                              }\r\n{  The Original Code is Tulip.UI.Forms.pas.                                    }\r\n{                                                                              }\r\n{  The Initial Developer of the Original Code is Marcos Gomes.                 }\r\n{  Portions created by Marcos Gomes are Copyright (C) 2012, Marcos Gomes.      }\r\n{  All Rights Reserved.                                                        }\r\n{                                                                              }\r\n{******************************************************************************}\r\n{                                                                              }\r\n{  Tulip.UI.Forms.pas                                   Modified: 23-Mar-2013  }\r\n{  --------------------------------------------------------------------------  }\r\n{                                                                              }\r\n{                    Base Implementations for Form Controls                    }\r\n{                                                                              }\r\n{                                Version 1.03                                  }\r\n{                                                                              }\r\n{******************************************************************************}\r\n\r\nunit Tulip.UI.Forms;\r\n\r\ninterface\r\n\r\nuses\r\n  System.SysUtils, System.Types, System.Classes,\r\n  \/\/ Aspryre units\r\n  AsphyreTypes, AbstractCanvas, AsphyreFonts, AsphyreImages, AsphyreUtils,\r\n  Vectors2,\r\n  \/\/ Tulip UI units\r\n  Tulip.UI.Types, Tulip.UI.Classes, Tulip.UI.Controls, Tulip.UI.Utils,\r\n  Tulip.UI.Helpers;\r\n\r\ntype\r\n{$REGION 'TCustomAForm'}\r\n  TCustomAForm = class(TWControl)\r\n  private\r\n    FAntialiased: Boolean;\r\n    FBorder: TBorder;\r\n    FBoundToScreen: Boolean;\r\n    FCanMove: Boolean;\r\n    FCaption: String;\r\n    FColor: TFillColor;\r\n    FFont: TFormatedFont;\r\n    FImage: TImage;\r\n    FIsMoving: Boolean;\r\n    FMargin: Word;\r\n    FModal: Boolean;\r\n    FShadow: Boolean;\r\n    FWordWrap: Boolean;\r\n\r\n    procedure SetAntialiased(Value: Boolean);\r\n    procedure SetBorder(Value: TBorder);\r\n    procedure SetCaption(Value: String);\r\n    procedure SetColor(Color: TFillColor);\r\n    procedure SetImage(Value: TImage);\r\n    procedure SetMargin(Value: Word);\r\n    procedure SetCanMove(Value: Boolean);\r\n    procedure SetShadow(Value: Boolean);\r\n    procedure SetFont(Value: TFormatedFont);\r\n    procedure SetWordWrap(Value: Boolean);\r\n  protected\r\n    procedure AssignTo(Dest: TPersistent); override;\r\n    procedure Paint; override;\r\n\r\n    procedure SetLeft(Value: Integer); override;\r\n    procedure SetHeight(Value: Integer); override;\r\n    procedure SetTop(Value: Integer); override;\r\n    procedure SetWidth(Value: Integer); override;\r\n  public\r\n    procedure MouseDown(Button: TMouseButton; Shift: TShiftState;\r\n      X, Y: Integer); override;\r\n    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;\r\n    procedure MouseUp(Button: TMouseButton; Shift: TShiftState;\r\n      X, Y: Integer); override;\r\n  public\r\n    constructor Create(AOwner: TComponent); override;\r\n    destructor Destroy; override;\r\n\r\n    function IsModal: Boolean;\r\n\r\n    procedure Close;\r\n    procedure Show(Modal: Boolean = False);\r\n\r\n    property Antialiased: Boolean read FAntialiased write SetAntialiased;\r\n    property Border: TBorder read FBorder write SetBorder;\r\n    property BoundToScreen: Boolean read FBoundToScreen write FBoundToScreen;\r\n    property Caption: String read FCaption write SetCaption;\r\n    property CanMove: Boolean read FCanMove write SetCanMove;\r\n    property Color: TFillColor read FColor write SetColor;\r\n    property Font: TFormatedFont read FFont write SetFont;\r\n    property Image: TImage read FImage write SetImage;\r\n    property IsMoving: Boolean read FIsMoving write FIsMoving;\r\n    property Margin: Word read FMargin write SetMargin;\r\n    property Shadow: Boolean read FShadow write SetShadow;\r\n    property WordWrap: Boolean read FWordWrap write SetWordWrap;\r\n  end;\r\n{$ENDREGION}\r\n\r\nimplementation\r\n\r\n\/\/ ----------------------------------------------------------------------------\r\n\r\nvar\r\n  XOffSet, YOffSet: Integer;\r\n\r\n{$REGION 'TCustomAForm'}\r\n  { TCustomAForm }\r\n\r\nprocedure TCustomAForm.MouseDown(Button: TMouseButton; Shift: TShiftState;\r\n  X, Y: Integer);\r\nbegin\r\n  if (Button = mbLeft) and (FCanMove) then\r\n  begin\r\n    XOffSet := X - Left;\r\n    YOffSet := Y - Top;\r\n    FIsMoving := True;\r\n  end;\r\n\r\n  inherited MouseDown(Button, Shift, X, Y);\r\nend;\r\n\r\nprocedure TCustomAForm.MouseMove(Shift: TShiftState; X, Y: Integer);\r\nbegin\r\n  if FIsMoving = True then\r\n  begin\r\n    Left := X - XOffSet;\r\n    Top := Y - YOffSet;\r\n  end;\r\n\r\n  inherited MouseMove(Shift, X, Y);\r\nend;\r\n\r\nprocedure TCustomAForm.MouseUp(Button: TMouseButton; Shift: TShiftState;\r\n  X, Y: Integer);\r\nbegin\r\n  if (Button = mbLeft) and (FIsMoving) then\r\n    FIsMoving := False;\r\n\r\n  inherited MouseUp(Button, Shift, X, Y);\r\nend;\r\n\r\nprocedure TCustomAForm.AssignTo(Dest: TPersistent);\r\nbegin\r\n  ControlState := ControlState + [csReadingState];\r\n\r\n  inherited AssignTo(Dest);\r\n\r\n  if Dest is TCustomAForm then\r\n    with TCustomAForm(Dest) do\r\n    begin\r\n      Antialiased := Self.Antialiased;\r\n      Border := Self.Border;\r\n      BoundToScreen := Self.BoundToScreen;\r\n      Caption := Self.Caption;\r\n      Color := Self.Color;\r\n      Font := Self.Font;\r\n      Image := Self.Image;\r\n      CanMove := Self.CanMove;\r\n      Margin := Self.Margin;\r\n      Shadow := Self.Shadow;\r\n      WordWrap := Self.WordWrap;\r\n    end;\r\n\r\n  ControlState := ControlState - [csReadingState];\r\nend;\r\n\r\nprocedure TCustomAForm.Close;\r\nbegin\r\n  FModal := False;\r\n  Visible := False;\r\nend;\r\n\r\nconstructor TCustomAForm.Create(AOwner: TComponent);\r\nvar\r\n  Num: Integer;\r\nbegin\r\n  ControlState := ControlState + [csCreating];\r\n\r\n  inherited Create(AOwner);\r\n\r\n  if (AOwner <> nil) and (AOwner <> Self) and (AOwner is TWControl) then\r\n  begin\r\n    \/\/ Auto generate name\r\n    Num := 1;\r\n    while TWControl(AOwner).FindChildControl('Form' + IntToStr(Num),\r\n      True) <> nil do\r\n      Inc(Num);\r\n    Name := 'Form' + IntToStr(Num);\r\n  end;\r\n\r\n  \/\/ fields\r\n  FAntialiased := True;\r\n  FBorder := TBorder.Create;\r\n  FBorder.Color := $80000000;\r\n  FBoundToScreen := False;\r\n  FCanMove := True;\r\n  FCaption := '';\r\n  FColor := TFillColor.Create($FFA6CAF0, $FFA6CAF0, $FF4090F0, $FF4090F0);\r\n  FFont := TFormatedFont.Create;\r\n  FImage := TImage.Create;\r\n  FIsMoving := False;\r\n  FMargin := 3;\r\n  FModal := False;\r\n  FShadow := True;\r\n  FWordWrap := True;\r\n\r\n  \/\/ properties\r\n  Left := 0;\r\n  Top := 0;\r\n  Width := 320;\r\n  Height := 240;\r\n\r\n  ControlState := ControlState - [csCreating];\r\nend;\r\n\r\ndestructor TCustomAForm.Destroy;\r\nbegin\r\n  FBorder.Free;\r\n  FColor.Free;\r\n  FFont.Free;\r\n  FImage.Free;\r\n\r\n  inherited Destroy;\r\nend;\r\n\r\nfunction TCustomAForm.IsModal: Boolean;\r\nbegin\r\n  Result := FModal;\r\nend;\r\n\r\nprocedure TCustomAForm.Paint;\r\nvar\r\n  X, Y: Integer;\r\n  AFont: TAsphyreFont;\r\n  AImage: TAsphyreImage;\r\n  bTop, bBottom: TConstraintSize;\r\n  I: Integer;\r\n  ARect: TRect;\r\n  AHeight, AWidth: Integer;\r\n  vLeft, vTop: Integer;\r\nbegin\r\n  \/\/ Get size Canvas\r\n  ARect := ControlManager.Canvas.ClipRect;\r\n\r\n  \/\/ Set initial values\r\n  X := ClientLeft;\r\n  Y := ClientTop;\r\n\r\n  ControlManager.Canvas.Antialias := FAntialiased;\r\n\r\n  if FShadow then\r\n  begin\r\n    \/\/ Draw Shadow\r\n    I := 12;\r\n    while I > 5 do\r\n    begin\r\n      ControlManager.Canvas.FillRect(Rect(X - I, Y - I, X + Width + I,\r\n        Y + Height + I), cAlpha4(1), beShadow);\r\n      Dec(I, 2);\r\n    end;\r\n\r\n    I := 5;\r\n    while I > 3 do\r\n    begin\r\n      ControlManager.Canvas.FillRect(Rect(X - I, Y - I, X + Width + I,\r\n        Y + Height + I), cAlpha4(2), beShadow);\r\n      Dec(I, 1);\r\n    end;\r\n\r\n    I := 3;\r\n    while I > 0 do\r\n    begin\r\n      ControlManager.Canvas.FillRect(Rect(X - I, Y - I, X + Width + I,\r\n        Y + Height + I), cAlpha4(4), beShadow);\r\n      Dec(I, 1);\r\n    end;\r\n  end;\r\n\r\n  \/\/ Draw Background\r\n  AImage := ControlManager.Images.Image[FImage.Image];\r\n  if AImage <> nil then\r\n  begin\r\n    ControlManager.Canvas.UseImagePx(AImage, pRect4(FImage.Rect));\r\n    ControlManager.Canvas.TexMap(pRect4(Rect(X, Y, X + Width, Y + Height)),\r\n      cAlpha4(FColor), beNormal);\r\n  end\r\n  else\r\n  begin\r\n    ControlManager.Canvas.FillRect(Rect(X, Y, X + Width, Y + Height),\r\n      cColor4(FColor), beNormal);\r\n  end;\r\n\r\n  \/\/ Draw Border\r\n  if Border.Size > 0 then\r\n  begin\r\n    bTop := 0;\r\n    bBottom := 0;\r\n\r\n    if eTop in Border.Edges then\r\n    begin\r\n      ControlManager.Canvas.FillRect(Rect(X, Y, X + Width, Y + Border.Size),\r\n        Border.Color, beNormal);\r\n      bTop := Border.Size;\r\n    end;\r\n\r\n    if eBottom in Border.Edges then\r\n    begin\r\n      ControlManager.Canvas.FillRect(Rect(X, Y + Height - Border.Size,\r\n        X + Width, Y + Height), Border.Color, beNormal);\r\n      bBottom := Border.Size;\r\n    end;\r\n\r\n    if eLeft in Border.Edges then\r\n      ControlManager.Canvas.FillRect(Rect(X, Y + bTop, X + Border.Size,\r\n        Y + Height - bBottom), Border.Color, beNormal);\r\n\r\n    if eRight in Border.Edges then\r\n      ControlManager.Canvas.FillRect(Rect(X + Width - Border.Size, Y + bTop,\r\n        X + Width, Y + Height - bBottom), Border.Color, beNormal);\r\n  end;\r\n\r\n  \/\/ Draw DisplayText\r\n  AFont := ControlManager.Fonts.Font[FFont.Name];\r\n  if (AFont <> nil) and (FCaption <> '') then\r\n  begin\r\n    if WordWrap then\r\n    begin\r\n      AFont.TextRectEx(Point2(X + Border.Size + Margin, Y + Border.Size +\r\n        Margin + 1), Point2(Width - (Border.Size * 2) - (Margin * 2),\r\n        Height - (Border.Size * 2) - (Margin * 2)), FCaption,\r\n        cColor2(FFont.Color), 1.0, FFont.HorizontalAlign, FFont.VerticalAlign,\r\n        FFont.ParagraphLine);\r\n    end\r\n    else\r\n    begin\r\n      \/\/ Set Bounds\r\n      X := ClientLeft + Margin + Border.Size;\r\n      Y := ClientTop + Margin + Border.Size;\r\n      AWidth := Width - Margin * 2 - Border.Size * 2;\r\n      AHeight := Height - Margin * 2 - Border.Size * 2;\r\n\r\n      \/\/ Set Rect Canvas\r\n      ControlManager.Canvas.ClipRect :=\r\n        ShortRect(Rect(X - 1, Y, X + AWidth, Y + AHeight), ARect);\r\n\r\n      case Self.FFont.VerticalAlign of\r\n        aTop:\r\n          vTop := Y;\r\n        aMiddle:\r\n          vTop := Y + (AHeight div 2) - (AFont.TexHeight(FCaption) div 2);\r\n        aBottom:\r\n          vTop := Y + AHeight - AFont.TexHeight(FCaption);\r\n        else\r\n          vTop := X;\r\n      end;\r\n\r\n      case FFont.HorizontalAlign of\r\n        aLeft,\r\n        aJustify:\r\n          vLeft := X;\r\n        aCenter:\r\n          vLeft := X + (AWidth div 2) - Round(AFont.TextWidth(FCaption) \/ 2);\r\n        aRight:\r\n          vLeft := X + AWidth - Round(AFont.TextWidth(FCaption));\r\n        else\r\n          vLeft := X;\r\n      end;\r\n\r\n      AFont.TextOut(Point2(vLeft, vTop),FCaption, cColor2(FFont.Color), 1.0);\r\n\r\n      \/\/ Restore Rect Canvas\r\n      ControlManager.Canvas.ClipRect := ARect;\r\n    end;\r\n  end;\r\n\r\n  inherited Paint;\r\nend;\r\n\r\nprocedure TCustomAForm.SetAntialiased(Value: Boolean);\r\nbegin\r\n  FAntialiased := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.SetBorder(Value: TBorder);\r\nbegin\r\n  if Value <> nil then\r\n    FBorder.Assign(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetCanMove(Value: Boolean);\r\nbegin\r\n  FCanMove := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.SetCaption(Value: String);\r\nbegin\r\n  FCaption := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.SetColor(Color: TFillColor);\r\nbegin\r\n  if Color <> nil then\r\n    FColor.Assign(Color);\r\nend;\r\n\r\nprocedure TCustomAForm.SetHeight(Value: Integer);\r\nbegin\r\n  if (FBoundToScreen) then\r\n  begin\r\n    if Value < 0 then\r\n    begin\r\n      Value := 0;\r\n    end;\r\n\r\n    if Top + Value > Parent.Height then\r\n    begin\r\n      Value := Parent.Height - Top;\r\n    end;\r\n  end;\r\n\r\n  inherited SetHeight(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetFont(Value: TFormatedFont);\r\nbegin\r\n  if Value <> nil then\r\n    FFont.Assign(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetTop(Value: Integer);\r\nbegin\r\n  if (FBoundToScreen) then\r\n  begin\r\n    if Value < 0 then\r\n    begin\r\n      Value := 0;\r\n    end;\r\n\r\n    if Value + Height > Parent.Height then\r\n    begin\r\n      Value := Parent.Height - Height;\r\n    end;\r\n  end;\r\n\r\n  inherited SetTop(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetWidth(Value: Integer);\r\nbegin\r\n  if (FBoundToScreen) then\r\n  begin\r\n    if Value < 0 then\r\n    begin\r\n      Value := 0;\r\n    end;\r\n\r\n    if Left + Value > Parent.Width then\r\n    begin\r\n      Value := Parent.Width - Left;\r\n    end;\r\n  end;\r\n\r\n  inherited SetWidth(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetWordWrap(Value: Boolean);\r\nbegin\r\n  FWordWrap := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.SetImage(Value: TImage);\r\nbegin\r\n  if Value <> nil then\r\n    FImage.Assign(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetLeft(Value: Integer);\r\nbegin\r\n  if (FBoundToScreen) then\r\n  begin\r\n    if Value < 0 then\r\n    begin\r\n      Value := 0;\r\n    end;\r\n\r\n    if Value + Width > Parent.Width then\r\n    begin\r\n      Value := Parent.Width - Width;\r\n    end;\r\n  end;\r\n\r\n  inherited SetLeft(Value);\r\nend;\r\n\r\nprocedure TCustomAForm.SetMargin(Value: Word);\r\nbegin\r\n  if FMargin <> Value then\r\n    FMargin := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.SetShadow(Value: Boolean);\r\nbegin\r\n  FShadow := Value;\r\nend;\r\n\r\nprocedure TCustomAForm.Show(Modal: Boolean = False);\r\nbegin\r\n  FModal := Modal;\r\n  Visible := True;\r\n  BringToFront;\r\n  SetFocus;\r\n  SelectFirst;\r\nend;\r\n{$ENDREGION}\r\n\r\nend.\r\n<\/pre>\n<p><\/code>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Source Files {******************************************************************************} { } { Tulip &#8211; User Interface Library } { } { Copyright(c) 2012 &#8211; 2013 Marcos Gomes. All rights Reserved. } { } { &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; } <a class=\"more-link\" href=\"http:\/\/tulip-ui.azurewebsites.net\/?page_id=1151\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1091,"menu_order":0,"comment_status":"closed","ping_status":"open","template":"","meta":[],"_links":{"self":[{"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/pages\/1151"}],"collection":[{"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1151"}],"version-history":[{"count":2,"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/pages\/1151\/revisions"}],"predecessor-version":[{"id":1721,"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/pages\/1151\/revisions\/1721"}],"up":[{"embeddable":true,"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=\/wp\/v2\/pages\/1091"}],"wp:attachment":[{"href":"http:\/\/tulip-ui.azurewebsites.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}