djjd47130

Font Button Control

Aug 12th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 40.15 KB | None | 0 0
  1. (*
  2.   TFontButton - custom control encapsulating a button with custom effects
  3.     Uses font (vector) glyphs instead of bitmaps or other image types
  4.     Allows for scalable lightweight buttons
  5.  
  6.   TODO:
  7.   - Fix design-time artifacts drawn in larger controls
  8.   - Arrange properties in appropriate order
  9.   - Finish component editor
  10.   - Properly scale image/caption
  11.     - DPI scaling when set to 125%
  12.   - Fix autosizing due to new rect calculations
  13.   - Fix centering when Margin = -1
  14.   - Implement centering when Spacing = -1
  15.   - Add option to not move text when hovering
  16.     - Actually "CaptionGrow" property needed
  17.   - Fix "ImageGrow" property (always enabled?)
  18.  
  19. *)
  20.  
  21. unit RMP.Ctrls.FontButton;
  22.  
  23. //DO NOT ENABLE!!!
  24. {    $DEFINE BUF_BMP}
  25.  
  26. interface
  27.  
  28. uses
  29.   Winapi.Windows, Winapi.Messages,
  30.   System.SysUtils, System.Classes, System.Types, System.UITypes,
  31.   Vcl.Graphics, Vcl.Controls, Vcl.Buttons, Vcl.StdCtrls, Vcl.ExtCtrls,
  32.   Vcl.Forms, Vcl.Styles, Vcl.Themes,
  33.   RMP.Graphics,
  34.   Vcl.Dialogs;
  35.  
  36. const
  37.   WM_COLORCHANGE = WM_USER + 42;
  38.  
  39. type
  40.   TFontButtonImgPosition = (fpImgTop, fpImgBottom, fpImgLeft, fpImgRight, fpImgOnly, fpImgNone);
  41.  
  42.   TFontButtonState = (fsDisabled, fsFocused, fsHot, fsNormal, fsPressed);
  43.  
  44.   TFontButtonStyleColor = (scCaption, scImage, scBack, scFrame);
  45.   TFontButtonStyleColors = set of TFontButtonStyleColor;
  46.  
  47.   TFontButtonDrawStyle = (fdThemed, fdTransparent, fdHybrid);
  48.  
  49.   TFontButtonKind = (fkCustom, fkOK, fkCancel, fkClose, fkClear, fkEdit, fkSave, fkAdd,
  50.     fkDelete, fkInfo, fkHelp, fkPrint, fkPrintTag, fkCalc, fkRefresh, fkView, fkMerge,
  51.     fkEmail, fkCloseX, fkUndo);
  52.  
  53.   TFontButtonColor = (fcNeutral, fcBlue, fcGreen, fcRed, fcYellow, fcOrange);
  54.  
  55.   TFontButtonColors = array[TFontButtonColor] of TColorRec;
  56.  
  57.   TColorManager = class(TObject)
  58.   private
  59.     FBaseColor: TColorRec;
  60.     FColors: TFontButtonColors;
  61.     FIsBaseDark: Boolean;
  62.     procedure SetBaseColor(const Value: TColorRec);
  63.     function GetColor(Clr: TFontButtonColor): TColorRec;
  64.     procedure SetColor(Clr: TFontButtonColor; const Value: TColorRec);
  65.   public
  66.     constructor Create;
  67.     destructor Destroy; override;
  68.     procedure Invalidate;
  69.     function IsBaseDark: Boolean;
  70.   public
  71.     property BaseColor: TColorRec read FBaseColor write SetBaseColor;
  72.     property Color[Clr: TFontButtonColor]: TColorRec read GetColor write SetColor;
  73.   end;
  74.  
  75.  
  76. const
  77.   DEFAULT_WIDTH = 100;
  78.   DEFAULT_HEIGHT = 30;
  79.   DEFAULT_COLOR = clBtnFace;
  80.   DEFAULT_IMAGE_POSITION = fpImgLeft;
  81.   DEFAULT_DOWN_SIZE = 3;
  82.   DEFAULT_STYLE_COLORS = [scCaption, scBack, scFrame];
  83.   DEFAULT_SHOW_FOCUS_RECT = False;
  84.   DEFAULT_SHOW_GUIDES = False;
  85.   DEFAULT_SPACING = 4;
  86.   DEFAULT_MARGIN = 4;
  87.   DEFAULT_MODAL_RESULT = mrNone;
  88.   DEFAULT_TAB_STOP = True;
  89.   DEFAULT_DEFAULT = False;
  90.   DEFAULT_CANCEL = False;
  91.   DEFAULT_DRAW_STYLE = fdThemed;
  92.   DEFAULT_ENABLED = True;
  93.   DEFAULT_KIND = fkCustom;
  94.  
  95.   DEFAULT_IMAGE_FONT = 'RMPicons';
  96.   DEFAULT_IMAGE_QUALITY = fqAntialiased;
  97.   DEFAULT_IMAGE_SIZE = 16;
  98.   DEFAULT_IMAGE_TEXT = '';
  99.   DEFAULT_IMAGE_AUTO_SIZE = False;
  100.   DEFAULT_IMAGE_GROW_SIZE = 3;
  101.   DEFAULT_IMAGE_STANDARD_COLOR = fcNeutral;
  102.   DEFAULT_IMAGE_USE_STANDARD_COLOR = True;
  103.  
  104.   DEFAULT_FRAME_SIZE = 1;
  105.   DEFAULT_FRAME_ROUND = 8;
  106.   DEFAULT_FRAME_COLOR = clBlack;
  107.   DEFAULT_FRAME_SHOW_HOVER = True;
  108.   DEFAULT_FRAME_SHOW_ALWAYS = True;
  109.  
  110. const
  111.   CLR_DK_BLUE = clNavy;
  112.   CLR_BLUE = clBlue;
  113.  
  114. type
  115.   TFontButtonImage = class;
  116.   TFontButton = class;
  117.  
  118.   TFontButtonImage = class(TPersistent)
  119.   private
  120.     FOwner: TFontButton;
  121.     FFont: TFont;
  122.     FText: TCaption;
  123.     FAutoSize: Boolean;
  124.     FGrowSize: Integer;
  125.     FStandardColor: TFontButtonColor;
  126.     FUseStandardColor: Boolean;
  127.     procedure FontChanged(Sender: TObject);
  128.     procedure SetText(const Value: TCaption);
  129.     procedure SetFont(const Value: TFont);
  130.     procedure SetAutoSize(const Value: Boolean);
  131.     procedure SetGrowSize(const Value: Integer);
  132.     procedure SetStandardColor(const Value: TFontButtonColor);
  133.     procedure SetUseStandardColor(const Value: Boolean);
  134.   public
  135.     constructor Create(AOwner: TFontButton);
  136.     destructor Destroy; override;
  137.     procedure Invalidate;
  138.   published
  139.     property AutoSize: Boolean read FAutoSize write SetAutoSize default True;
  140.     property Text: TCaption read FText write SetText;
  141.     property Font: TFont read FFont write SetFont;
  142.     property GrowSize: Integer read FGrowSize write SetGrowSize default 3;
  143.     property StandardColor: TFontButtonColor read FStandardColor write SetStandardColor default DEFAULT_IMAGE_STANDARD_COLOR;
  144.     property UseStandardColor: Boolean read FUseStandardColor write SetUseStandardColor default DEFAULT_IMAGE_USE_STANDARD_COLOR;
  145.   end;
  146.  
  147.   TFontButton = class(TCustomControl)
  148.   private
  149.     FTmp: TBitmap;
  150.     {$IFDEF BUF_BMP}
  151.     FBuf: TBitmap;
  152.     {$ENDIF}
  153.     FActive: Boolean;
  154.     FClicking: Boolean;
  155.     FHovering: Boolean;
  156.     FButtonTheme: THandle;
  157.     FCancel: Boolean;
  158.     FColor: TColor;
  159.     FImage: TFontButtonImage;
  160.     FDownSize: Integer;
  161.     FStyleColors: TFontButtonStyleColors;
  162.     FImagePosition: TFontButtonImgPosition;
  163.     FShowFocusRect: Boolean;
  164.     FModalResult: TModalResult;
  165.     FSpacing: Integer;
  166.     FMargin: Integer;
  167.     FDefault: Boolean;
  168.     FShowGuides: Boolean;
  169.     FDrawStyle: TFontButtonDrawStyle;
  170.     FKind: TFontButtonKind;
  171.     procedure SetDownSize(const Value: Integer);
  172.     procedure SetImagePosition(const Value: TFontButtonImgPosition);
  173.     procedure SetStyleColors(const Value: TFontButtonStyleColors);
  174.     procedure Reset;
  175.     function CaptionRect: TRect;
  176.     function ImageRect: TRect;
  177.     function CaptionColor: TColor;
  178.     function ImageColor: TColor;
  179.     function CaptionDims: TPoint;
  180.     function ImageDims: TPoint;
  181.     function CaptionFlags: Cardinal;
  182.     function ImageFlags: Cardinal;
  183.     function ContentRect: TRect;
  184.     function ImageFontSize: Integer;
  185.     procedure DrawBtn;
  186.     procedure DrawTheme;
  187.     procedure DrawBackground;
  188.     procedure DrawCaption;
  189.     procedure DrawImage;
  190.     procedure DrawFocus;
  191.     //Property Getters/Setters
  192.     procedure SetColor(Value: TColor);
  193.     procedure SetImage(const Value: TFontButtonImage);
  194.     procedure SetShowFocusRect(const Value: Boolean);
  195.     procedure SetMargin(const Value: Integer);
  196.     procedure SetSpacing(const Value: Integer);
  197.     procedure SetText(const Value: TCaption);
  198.     function GetText: TCaption;
  199.     procedure SetDefault(const Value: Boolean);
  200.     procedure SetCancel(const Value: Boolean);
  201.     procedure SetShowGuides(const Value: Boolean);
  202.     procedure SetDrawStyle(const Value: TFontButtonDrawStyle);
  203.     function GetFont: TFont;
  204.     procedure SetFont(const Value: TFont);
  205.     procedure SetKind(const Value: TFontButtonKind);
  206.     function ActiveCanvas: TCanvas;
  207.   protected
  208.     procedure Paint; override;
  209.     function GetEnabled: Boolean; reintroduce;
  210.     procedure SetEnabled(Value: Boolean); reintroduce;
  211.     procedure Loaded; override;
  212.     //Catch Windows Messages
  213.     procedure CMStyleChanged(var Message: TMessage); message CM_STYLECHANGED;
  214.     procedure CMMouseEnter(var Message: TMessage); message CM_MOUSEENTER;
  215.     procedure CMMouseLeave(var Message: TMessage); message CM_MOUSELEAVE;
  216.     procedure CMDialogChar(var Message: TCMDialogChar); message CM_DIALOGCHAR;
  217.     procedure CMFocusChanged(var Message: TCMFocusChanged); message CM_FOCUSCHANGED;
  218.     procedure CMDialogKey(var Message: TCMDialogKey); message CM_DIALOGKEY;
  219.     procedure WMLButtonDown(var Message: TWMLButtonDown); message WM_LBUTTONDOWN;
  220.     procedure WMRButtonDown(var Message: TWMRButtonDown); message WM_RBUTTONDOWN;
  221.     procedure WMMButtonDown(var Message: TWMMButtonDown); message WM_MBUTTONDOWN;
  222.     procedure WMLButtonUp(var Message: TWMLButtonUp); message WM_LBUTTONUP;
  223.     procedure WMRButtonUp(var Message: TWMRButtonUp); message WM_RBUTTONUP;
  224.     procedure WMMButtonUp(var Message: TWMMButtonUp); message WM_MBUTTONUP;
  225.     procedure WMFontChange(var Message: TMessage); message WM_FONTCHANGE;
  226.     procedure WMNCHitTest(var Message: TWMNCHitTest); message WM_NCHITTEST;
  227.     procedure WMKeyDown(var Message: TWMKeyDown); message WM_KEYDOWN;
  228.     procedure WMKeyUp(var Message: TWMKeyUp); message WM_KEYUP;
  229.     procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS;
  230.     procedure WMKillFocus(var Message: TWMKillFocus); message WM_KILLFOCUS;
  231.     procedure WMResize(var Message: TWMSize); message WM_SIZE;
  232.     procedure WMGetDlgCode(var Message: TWMGetDlgCode); message WM_GETDLGCODE;
  233.     procedure WMColorChange(var Message: TMessage); message WM_COLORCHANGE;
  234.   public
  235.     constructor Create(AOwner: TComponent); override;
  236.     destructor Destroy; override;
  237.     function State: TFontButtonState;
  238.     procedure Assign(Source: TPersistent); override;
  239.     procedure Click; override;
  240.     function BackColor: TColor;
  241.     procedure AfterConstruction; override;
  242.   published
  243.     property Align;
  244.     property Anchors;
  245.     property Cancel: Boolean read FCancel write SetCancel default DEFAULT_CANCEL;
  246.     property Color: TColor read FColor write SetColor default DEFAULT_COLOR;
  247.     property Cursor;
  248.     property CustomHint;
  249.     property Default: Boolean read FDefault write SetDefault default DEFAULT_DEFAULT;
  250.     property DoubleBuffered;
  251.     property DownSize: Integer read FDownSize write SetDownSize default DEFAULT_DOWN_SIZE;
  252.     property DrawStyle: TFontButtonDrawStyle read FDrawStyle write SetDrawStyle default DEFAULT_DRAW_STYLE;
  253.     property Enabled: Boolean read GetEnabled write SetEnabled default DEFAULT_ENABLED;
  254.     property Font: TFont read GetFont write SetFont;
  255.     property Hint;
  256.     property Image: TFontButtonImage read FImage write SetImage;
  257.     property ImagePosition: TFontButtonImgPosition read FImagePosition write SetImagePosition default DEFAULT_IMAGE_POSITION;
  258.     property Kind: TFontButtonKind read FKind write SetKind default DEFAULT_KIND;
  259.     property Margin: Integer read FMargin write SetMargin default DEFAULT_MARGIN;
  260.     property ModalResult: TModalResult read FModalResult write FModalResult default DEFAULT_MODAL_RESULT;
  261.     property ParentCustomHint;
  262.     property ParentDoubleBuffered;
  263.     property ShowFocusRect: Boolean read FShowFocusRect write SetShowFocusRect default DEFAULT_SHOW_FOCUS_RECT;
  264.     property ShowGuides: Boolean read FShowGuides write SetShowGuides default DEFAULT_SHOW_GUIDES;
  265.     property ShowHint;
  266.     property Spacing: Integer read FSpacing write SetSpacing default DEFAULT_SPACING;
  267.     property StyleColors: TFontButtonStyleColors read FStyleColors write SetStyleColors default DEFAULT_STYLE_COLORS;
  268.     property TabOrder;
  269.     property TabStop default DEFAULT_TAB_STOP;
  270.     property Text: TCaption read GetText write SetText;
  271.     property Visible;
  272.  
  273.     property OnClick;
  274.     property OnContextPopup;
  275.     property OnEnter;
  276.     property OnExit;
  277.     property OnKeyDown;
  278.     property OnKeyPress;
  279.     property OnKeyUp;
  280.     property OnMouseDown;
  281.     property OnMouseEnter;
  282.     property OnMouseLeave;
  283.     property OnMouseMove;
  284.     property OnMouseUp;
  285.   end;
  286.  
  287. //Used for drawing new Windows Themes (XP and up) - only if enabled
  288. function DrawThemeBackground(hTheme: THandle; hdc: HDC; iPartId, iStateId: Integer;
  289.   const pRect: TRect; pClipRect: PRECT): HRESULT; stdcall;
  290.   external 'uxtheme.dll';
  291. function OpenThemeData(hwnd: HWND; pszClassList: LPCWSTR): THandle; stdcall;
  292.   external 'uxtheme.dll';
  293. function CloseThemeData(hTheme: THandle): HRESULT; stdcall;
  294.   external 'uxtheme.dll';
  295.  
  296. function ColorManager: TColorManager;
  297.  
  298. implementation
  299.  
  300. uses
  301.   Math;
  302.  
  303. const
  304.   PBS_NORMAL = 1;
  305.   PBS_HOT = 2;
  306.   PBS_PRESSED = 3;
  307.   PBS_DISABLED = 4;
  308.   PBS_DEFAULTED = 5;
  309.   PBS_DEFAULTED_ANIMATING = 6;
  310.  
  311. type
  312.   TIsThemeActive = function: Bool; stdcall;
  313.  
  314. const
  315.   THEME_LIB = 'uxtheme.dll';
  316.  
  317. var
  318.   _ColorManager: TColorManager;
  319.  
  320. function ColorManager: TColorManager;
  321. begin
  322.   Result:= _ColorManager;
  323. end;
  324.  
  325. function IsThemeActive: Boolean;
  326. var
  327.   IsThemeActive: TIsThemeActive;
  328.   hUxTheme: HINST;
  329. begin
  330.   //Checks whether or not windows themes are enabled
  331.   Result := False;
  332.   if (Win32Platform = VER_PLATFORM_WIN32_NT) and
  333.     (((Win32MajorVersion = 5) and (Win32MinorVersion >= 1)) or
  334.     (Win32MajorVersion > 5)) then
  335.   begin
  336.     hUxTheme := LoadLibrary(THEME_LIB);
  337.     if hUxTheme <> 0 then begin
  338.       try
  339.         IsThemeActive := GetProcAddress(huxtheme, 'IsThemeActive');
  340.         Result := IsThemeActive;
  341.       finally
  342.         if hUxTheme > 0 then
  343.           FreeLibrary(huxtheme);
  344.       end;
  345.     end;
  346.   end;
  347. end;
  348.  
  349. function IntToButtonState(const Value: Integer): TFontButtonState;
  350. begin
  351.   //Converts an integer (from Winapi) to state enum
  352.   case Value of
  353.     PBS_NORMAL: Result:= fsNormal;
  354.     PBS_HOT: Result:= fsHot;
  355.     PBS_PRESSED: Result:= fsPressed;
  356.     PBS_DISABLED: Result:= fsDisabled;
  357.     PBS_DEFAULTED: Result:= fsFocused;
  358.     PBS_DEFAULTED_ANIMATING: Result:= fsFocused;
  359.     else Result:= fsNormal;
  360.   end;
  361. end;
  362.  
  363. function ButtonStateToInt(const Value: TFontButtonState): Integer;
  364. begin
  365.   //Converts a state enum to integer (from Winapi)
  366.   case Value of
  367.     fsDisabled: Result:= PBS_DISABLED;
  368.     fsFocused: Result:= PBS_DEFAULTED;
  369.     fsHot: Result:= PBS_HOT;
  370.     fsNormal: Result:= PBS_NORMAL;
  371.     fsPressed: Result:= PBS_PRESSED;
  372.     else Result:= PBS_NORMAL;
  373.   end;
  374. end;
  375.  
  376. { TFontButtonImage }
  377.  
  378. constructor TFontButtonImage.Create(AOwner: TFontButton);
  379. begin
  380.   FOwner:= AOwner;
  381.   FFont:= TFont.Create;
  382.  
  383.  
  384.   FFont.Quality:= DEFAULT_IMAGE_QUALITY;
  385.   FFont.Size:= DEFAULT_IMAGE_SIZE;
  386.   FFont.Name:= DEFAULT_IMAGE_FONT;
  387.   FFont.OnChange:= FontChanged;
  388.   FText:= DEFAULT_IMAGE_TEXT;
  389.   FAutoSize:= DEFAULT_IMAGE_AUTO_SIZE;
  390.   FGrowSize:= DEFAULT_IMAGE_GROW_SIZE;
  391.   FStandardColor:= DEFAULT_IMAGE_STANDARD_COLOR;
  392.   FUseStandardColor:= DEFAULT_IMAGE_USE_STANDARD_COLOR;
  393. end;
  394.  
  395. destructor TFontButtonImage.Destroy;
  396. begin
  397.   FFont.Free;
  398.   inherited;
  399. end;
  400.  
  401. procedure TFontButtonImage.FontChanged(Sender: TObject);
  402. begin
  403.   Invalidate;
  404. end;
  405.  
  406. procedure TFontButtonImage.Invalidate;
  407. begin
  408.   FOwner.Invalidate;
  409. end;
  410.  
  411. procedure TFontButtonImage.SetAutoSize(const Value: Boolean);
  412. begin
  413.   FAutoSize := Value;
  414.   Invalidate;
  415. end;
  416.  
  417. procedure TFontButtonImage.SetFont(const Value: TFont);
  418. begin
  419.   FFont.Assign(Value);
  420.   Invalidate;
  421. end;
  422.  
  423. procedure TFontButtonImage.SetText(const Value: TCaption);
  424. begin
  425.   FText:= Value;
  426.   FOwner.FKind:= fkCustom;
  427.   Invalidate;
  428. end;
  429.  
  430. procedure TFontButtonImage.SetUseStandardColor(const Value: Boolean);
  431. begin
  432.   FUseStandardColor := Value;
  433.   FOwner.FKind:= fkCustom;
  434.   Invalidate;
  435. end;
  436.  
  437. procedure TFontButtonImage.SetGrowSize(const Value: Integer);
  438. begin
  439.   FGrowSize := Value;
  440.   Invalidate;
  441. end;
  442.  
  443. procedure TFontButtonImage.SetStandardColor(const Value: TFontButtonColor);
  444. begin
  445.   FStandardColor := Value;
  446.   FOwner.FKind:= fkCustom;
  447.   Invalidate;
  448. end;
  449.  
  450. { TFontButton }
  451.  
  452. constructor TFontButton.Create(AOwner: TComponent);
  453. begin
  454.   inherited Create(AOwner);
  455.   FHovering:= False;
  456.   FClicking:= False;
  457.   FTmp:= TBitmap.Create;
  458.   {$IFDEF BUF_BMP}
  459.   FBuf:= TBitmap.Create;
  460.   {$ENDIF}
  461.   FImage:= TFontButtonImage.Create(Self);
  462.   //Font.OnChange:= FontChanged;
  463. end;
  464.  
  465. destructor TFontButton.Destroy;
  466. begin
  467.   FImage.Free;
  468.   {$IFDEF BUF_BMP}
  469.   FBuf.Free;
  470.   {$ENDIF}
  471.   FTmp.Free;
  472.   CloseThemeData(FButtonTheme);
  473.   inherited;
  474. end;
  475.  
  476. procedure TFontButton.AfterConstruction;
  477. begin
  478.   inherited;
  479.   //Set default values
  480.   FDrawStyle:= DEFAULT_DRAW_STYLE;
  481.   Width:= DEFAULT_WIDTH;
  482.   Height:= DEFAULT_HEIGHT;
  483.   Color:= DEFAULT_COLOR;
  484.   FImagePosition:= DEFAULT_IMAGE_POSITION;
  485.   FDownSize:= DEFAULT_DOWN_SIZE;
  486.   FStyleColors:= DEFAULT_STYLE_COLORS;
  487.   FShowFocusRect:= DEFAULT_SHOW_FOCUS_RECT;
  488.   FMargin:= DEFAULT_MARGIN;
  489.   FSpacing:= DEFAULT_SPACING;
  490.   FModalResult:= DEFAULT_MODAL_RESULT;
  491.   TabStop:= DEFAULT_TAB_STOP;
  492.   FDefault:= DEFAULT_DEFAULT;
  493.   FCancel:= DEFAULT_CANCEL;
  494.   FShowGuides:= DEFAULT_SHOW_GUIDES;
  495.   FKind:= DEFAULT_KIND;
  496.  
  497. end;
  498.  
  499. procedure TFontButton.Loaded;
  500. const
  501.   ButtonDataName: PWideChar = 'button';
  502. begin
  503.   inherited;
  504.  
  505.   FButtonTheme := OpenThemeData(Handle, ButtonDataName);
  506.  
  507. end;
  508.  
  509. procedure TFontButton.Assign(Source: TPersistent);
  510. var
  511.   S, D: TFontButton;
  512. begin
  513.   if Source is TFontButton then begin
  514.     S:= TFontButton(Source);
  515.     D:= Self;
  516.  
  517.     D.Width:= S.Width;
  518.     D.Height:= S.Height;
  519.     D.ImagePosition:= S.ImagePosition;
  520.     D.Font.Assign(S.Font);
  521.     D.Text:= S.Text;
  522.     D.Color:= S.Color;
  523.     D.Cursor:= S.Cursor;
  524.     D.DownSize:= S.DownSize;
  525.     D.Hint:= S.Hint;
  526.     D.Image.Font.Assign(S.Image.Font);
  527.     D.Image.AutoSize:= S.Image.AutoSize;
  528.     D.Image.GrowSize:= S.Image.GrowSize;
  529.     D.Image.Text:= S.Image.Text;
  530.     D.Margins.Assign(S.Margins);
  531.     D.ShowFocusRect:= S.ShowFocusRect;
  532.     D.ShowHint:= S.ShowHint;
  533.     D.StyleColors:= S.StyleColors;
  534.     D.TabStop:= S.TabStop;
  535.     D.Margin:= S.Margin;
  536.     D.Spacing:= S.Spacing;
  537.     D.DrawStyle:= S.DrawStyle;
  538.     D.Default:= S.Default;
  539.     D.Cancel:= S.Cancel;
  540.  
  541.  
  542.   end else
  543.     inherited;
  544. end;
  545.  
  546. function TFontButton.GetEnabled: Boolean;
  547. begin
  548.   Result:= inherited Enabled;
  549. end;
  550.  
  551. function TFontButton.GetFont: TFont;
  552. begin
  553.   Result:= inherited Font;
  554. end;
  555.  
  556. function TFontButton.GetText: TCaption;
  557. begin
  558.   Result:= inherited Caption;
  559. end;
  560.  
  561. procedure TFontButton.SetCancel(const Value: Boolean);
  562. begin
  563.   FCancel := Value;
  564.   Invalidate;
  565. end;
  566.  
  567. procedure TFontButton.SetText(const Value: TCaption);
  568. begin
  569.   inherited Text:= Value;
  570.   Invalidate;
  571. end;
  572.  
  573. procedure TFontButton.SetColor(Value: TColor);
  574. begin
  575.   FColor := Value;
  576.   Invalidate;
  577. end;
  578.  
  579. procedure TFontButton.SetEnabled(Value: Boolean);
  580. begin
  581.   inherited Enabled:= Value;
  582.   Invalidate;
  583. end;
  584.  
  585. procedure TFontButton.SetFont(const Value: TFont);
  586. begin
  587.   inherited Font.Assign(Value);
  588.   Invalidate;
  589. end;
  590.  
  591. procedure TFontButton.SetImage(const Value: TFontButtonImage);
  592. begin
  593.   FImage.Assign(Value);
  594.   Invalidate;
  595. end;
  596.  
  597. procedure TFontButton.SetShowFocusRect(const Value: Boolean);
  598. begin
  599.   FShowFocusRect := Value;
  600.   Invalidate;
  601. end;
  602.  
  603. procedure TFontButton.SetShowGuides(const Value: Boolean);
  604. begin
  605.   FShowGuides := Value;
  606.   Invalidate;
  607. end;
  608.  
  609. procedure TFontButton.SetSpacing(const Value: Integer);
  610. begin
  611.   FSpacing := Value;
  612.   Invalidate;
  613. end;
  614.  
  615. procedure TFontButton.SetStyleColors(const Value: TFontButtonStyleColors);
  616. begin
  617.   FStyleColors := Value;
  618.   Invalidate;
  619. end;
  620.  
  621. procedure TFontButton.SetDefault(const Value: Boolean);
  622. var
  623.   Form: TCustomForm;
  624. begin
  625.   FDefault := Value;
  626.   if HandleAllocated then begin
  627.     Form := GetParentForm(Self);
  628.     if Form <> nil then
  629.       Form.Perform(CM_FOCUSCHANGED, 0, LPARAM(Form.ActiveControl));
  630.   end;
  631.   Invalidate;
  632. end;
  633.  
  634. procedure TFontButton.SetDownSize(const Value: Integer);
  635. begin
  636.   if Value < 1 then begin
  637.     FDownSize:= 1;
  638.     Invalidate;
  639.     raise Exception.Create('Down size must be at least 1');
  640.   end;
  641.   FDownSize := Value;
  642.   Invalidate;
  643. end;
  644.  
  645. procedure TFontButton.SetDrawStyle(const Value: TFontButtonDrawStyle);
  646. begin
  647.   FDrawStyle := Value;
  648.   Invalidate;
  649. end;
  650.  
  651. procedure TFontButton.SetImagePosition(const Value: TFontButtonImgPosition);
  652. begin
  653.   FImagePosition := Value;
  654.   Invalidate;
  655. end;
  656.  
  657. procedure TFontButton.SetMargin(const Value: Integer);
  658. begin
  659.   FMargin := Value;
  660.   Invalidate;
  661. end;
  662.  
  663. function TFontButton.State: TFontButtonState;
  664. begin
  665.   if FClicking then
  666.     Result:= fsPressed
  667.   else if FHovering then
  668.     Result:= fsHot
  669.   else if Focused then
  670.     Result:= fsFocused
  671.   else if not Enabled then
  672.     Result:= fsDisabled
  673.   else
  674.     Result:= fsNormal;
  675. end;
  676.  
  677. procedure TFontButton.WMColorChange(var Message: TMessage);
  678. begin
  679.   Invalidate;
  680.   Repaint;
  681.   inherited;
  682. end;
  683.  
  684. procedure TFontButton.WMFontChange(var Message: TMessage);
  685. begin
  686.   Invalidate;
  687.   Repaint;
  688.   inherited;
  689. end;
  690.  
  691. procedure TFontButton.WMGetDlgCode(var Message: TWMGetDlgCode);
  692. begin
  693.   Message.Result:= DLGC_BUTTON;
  694.   inherited;
  695. end;
  696.  
  697. procedure TFontButton.WMKeyDown(var Message: TWMKeyDown);
  698. begin
  699.   if (Message.CharCode = vkReturn) or
  700.     (Message.CharCode = vkSpace) then
  701.   begin
  702.     FClicking:= True;
  703.   end;
  704.   Invalidate;
  705.   Repaint;
  706.   inherited;
  707. end;
  708.  
  709. procedure TFontButton.WMKeyUp(var Message: TWMKeyUp);
  710. begin
  711.   FClicking:= False;
  712.   Invalidate;
  713.   Repaint;
  714.   inherited;
  715. end;
  716.  
  717. procedure TFontButton.WMKillFocus(var Message: TWMKillFocus);
  718. begin
  719.   Invalidate;
  720.   Repaint;
  721.   inherited;
  722. end;
  723.  
  724. procedure TFontButton.WMLButtonDown(var Message: TWMLButtonDown);
  725. begin
  726.   FClicking:= True;
  727.   if TabStop then
  728.     SetFocus;
  729.   Invalidate;
  730.   inherited;
  731. end;
  732.  
  733. procedure TFontButton.WMLButtonUp(var Message: TWMLButtonUp);
  734. begin
  735.   FClicking:= False;
  736.   Invalidate;
  737.   inherited;
  738. end;
  739.  
  740. procedure TFontButton.WMMButtonDown(var Message: TWMMButtonDown);
  741. begin
  742.   FClicking:= True;
  743.   if TabStop then
  744.     SetFocus;
  745.   Invalidate;
  746.   inherited;
  747. end;
  748.  
  749. procedure TFontButton.WMMButtonUp(var Message: TWMMButtonUp);
  750. begin
  751.   FClicking:= False;
  752.   Invalidate;
  753.   inherited;
  754. end;
  755.  
  756. procedure TFontButton.WMNCHitTest(var Message: TWMNCHitTest);
  757. begin
  758.   Message.Result := HTCLIENT;
  759. end;
  760.  
  761. procedure TFontButton.WMRButtonDown(var Message: TWMRButtonDown);
  762. begin
  763.   FClicking:= True;
  764.   if TabStop then
  765.     SetFocus;
  766.   Invalidate;
  767.   inherited;
  768. end;
  769.  
  770. procedure TFontButton.WMRButtonUp(var Message: TWMRButtonUp);
  771. begin
  772.   FClicking:= False;
  773.   Invalidate;
  774.   inherited;
  775. end;
  776.  
  777. procedure TFontButton.WMResize(var Message: TWMSize);
  778. begin
  779.   Invalidate;
  780.   inherited;
  781. end;
  782.  
  783. procedure TFontButton.WMSetFocus(var Message: TWMSetFocus);
  784. begin
  785.   Invalidate;
  786.   Repaint;
  787.   inherited;
  788. end;
  789.  
  790. procedure TFontButton.CMDialogChar(var Message: TCMDialogChar);
  791. begin
  792.   if (Message.keydata and $20000000) <> 0 then begin
  793.     if IsAccel(Message.charcode, Text) then begin
  794.       Click;
  795.       Message.Result := 1;
  796.     end;
  797.   end;
  798.   inherited;
  799. end;
  800.  
  801. procedure TFontButton.CMMouseEnter(var Message: TMessage);
  802. begin
  803.   if Enabled then
  804.     FHovering:= True;
  805.   Invalidate;
  806.   inherited;
  807. end;
  808.  
  809. procedure TFontButton.CMMouseLeave(var Message: TMessage);
  810. begin
  811.   FHovering:= False;
  812.   Invalidate;
  813.   inherited;
  814. end;
  815.  
  816. procedure TFontButton.CMStyleChanged(var Message: TMessage);
  817. begin
  818.   Reset;
  819.   inherited;
  820. end;
  821.  
  822. procedure TFontButton.CMFocusChanged(var Message: TCMFocusChanged);
  823. begin
  824.   with Message do
  825.     if Sender is TFontButton then
  826.       FActive := Sender = Self
  827.     else
  828.       FActive := FDefault;
  829.   Invalidate;
  830.   inherited;
  831. end;
  832.  
  833. procedure TFontButton.CMDialogKey(var Message: TCMDialogKey);
  834. begin
  835.   with Message do
  836.     if  (((CharCode = VK_RETURN) and FActive) or
  837.       ((CharCode = VK_ESCAPE) and FCancel)) and
  838.       (KeyDataToShiftState(Message.KeyData) = []) and CanFocus then
  839.     begin
  840.       Click;
  841.       Result := 1;
  842.     end else
  843.       inherited;
  844.   Invalidate;
  845. end;
  846.  
  847. procedure TFontButton.Reset;
  848. begin
  849.   FHovering:= False;
  850.   FClicking:= False;
  851.   Invalidate;
  852. end;
  853.  
  854. procedure TFontButton.Click;
  855. var
  856.   Form: TCustomForm;
  857. begin
  858.   Form := GetParentForm(Self);
  859.   if Form <> nil then Form.ModalResult := ModalResult;
  860.   inherited Click;
  861. end;
  862.  
  863. function TFontButton.BackColor: TColor;
  864. begin
  865.   if scBack in StyleColors then begin
  866.     case State of
  867.       fsHot: begin
  868.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonHot)
  869.       end;
  870.       fsPressed: begin
  871.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonPressed)
  872.       end;
  873.       fsFocused: begin
  874.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonFocused)
  875.       end;
  876.       fsDisabled: begin
  877.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonDisabled)
  878.       end;
  879.       fsNormal: begin
  880.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonNormal);
  881.       end;
  882.       else begin
  883.         Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonNormal);
  884.       end;
  885.     end;
  886.   end else begin
  887.     if State = fsDisabled then
  888.       Result:= TStyleManager.ActiveStyle.GetStyleColor(scButtonDisabled)
  889.     else
  890.       Result:= TStyleManager.ActiveStyle.GetSystemColor(Self.Color);
  891.   end;
  892. end;
  893.  
  894. function TFontButton.CaptionColor: TColor;
  895. begin
  896.   if scCaption in StyleColors then begin
  897.     case State of
  898.       fsHot: begin
  899.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextHot);
  900.       end;
  901.       fsPressed: begin
  902.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextPressed);
  903.       end;
  904.       fsFocused: begin
  905.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextFocused);
  906.       end;
  907.       fsDisabled: begin
  908.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextDisabled);
  909.       end;
  910.       fsNormal: begin
  911.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextNormal);
  912.       end;
  913.       else begin
  914.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextNormal);
  915.       end;
  916.     end;
  917.   end else begin
  918.     if State = fsDisabled then
  919.       Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextDisabled)
  920.     else
  921.       Result:= TStyleManager.ActiveStyle.GetSystemColor(Font.Color);
  922.   end;
  923. end;
  924.  
  925. function TFontButton.ImageColor: TColor;
  926. begin
  927.   if scImage in StyleColors then begin
  928.     case State of
  929.       fsHot: begin
  930.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextHot);
  931.       end;
  932.       fsPressed: begin
  933.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextPressed);
  934.       end;
  935.       fsFocused: begin
  936.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextFocused);
  937.       end;
  938.       fsDisabled: begin
  939.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextDisabled);
  940.       end;
  941.       fsNormal: begin
  942.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextNormal);
  943.       end;
  944.       else begin
  945.         Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextNormal);
  946.       end;
  947.     end;
  948.   end else begin
  949.     if State = fsDisabled then
  950.       Result:= TStyleManager.ActiveStyle.GetStyleFontColor(sfButtonTextDisabled)
  951.     else begin
  952.       if Image.UseStandardColor then begin
  953.         Result:= ColorManager.Color[Image.StandardColor];
  954.       end else begin
  955.         Result:= TStyleManager.ActiveStyle.GetSystemColor(FImage.Font.Color);
  956.       end;
  957.     end;
  958.   end;
  959. end;
  960.  
  961. function TFontButton.ImageFontSize: Integer;
  962. const
  963.   RANGE_MIN = 6;
  964.   RANGE_MAX = 3000;
  965.   DIVIDE_BY = 1.7;
  966.   SHRINK_BY = 3;
  967. var
  968.   P: TPoint;
  969. begin
  970.   Result:= Image.Font.Size;
  971.   if Image.AutoSize then begin
  972.     P:= CaptionDims;
  973.     case FImagePosition of
  974.       fpImgTop, fpImgBottom: begin
  975.         Result:= IntRange(Trunc(ClientRect.Height - P.Y / DIVIDE_BY) - SHRINK_BY,
  976.           RANGE_MIN, RANGE_MAX);
  977.       end;
  978.       fpImgLeft, fpImgRight, fpImgOnly: begin
  979.         Result:= IntRange(Trunc(ClientRect.Height / DIVIDE_BY) - SHRINK_BY,
  980.           RANGE_MIN, RANGE_MAX);
  981.       end;
  982.       fpImgNone: begin
  983.  
  984.       end;
  985.     end;
  986.   end;
  987.   if FImage.FGrowSize > 0 then begin
  988.     if FHovering then begin
  989.       Result:= Result + FImage.FGrowSize;
  990.     end;
  991.   end;
  992. end;
  993.  
  994. function TFontButton.ImageDims: TPoint;
  995. begin
  996.   FTmp.Canvas.Font.Assign(Image.Font);
  997.   FTmp.Canvas.Font.Size:= ImageFontSize;
  998.   //Result.X:= Canvas.TextWidth(Image.Text);
  999.   Result.Y:= FTmp.Canvas.TextHeight(Image.Text);
  1000.   Result.X:= Result.Y;
  1001. end;
  1002.  
  1003. function TFontButton.CaptionDims: TPoint;
  1004. begin
  1005.   //Returns the dimensions of the caption text with given font
  1006.   FTmp.Canvas.Font.Assign(Font);
  1007.   Result.X:= FTmp.Canvas.TextWidth(Text);
  1008.   Result.Y:= FTmp.Canvas.TextHeight(Text);
  1009. end;
  1010.  
  1011. function TFontButton.ContentRect: TRect;
  1012. var
  1013.   Cap, Img, D: TPoint;
  1014.   W, H: Integer;
  1015. begin
  1016.   Cap:= CaptionDims;
  1017.   Img:= ImageDims;
  1018.  
  1019.   case FImagePosition of
  1020.     fpImgTop, fpImgBottom: begin
  1021.       if Img.X > Cap.X then D.X:= Img.X else D.X:= Cap.X;
  1022.       D.Y:= Img.Y + Cap.Y + FSpacing;
  1023.     end;
  1024.     fpImgLeft, fpImgRight: begin
  1025.       if Img.Y > Cap.Y then D.Y:= Img.Y else D.Y:= Cap.Y;
  1026.       D.X:= Img.X + Cap.X + FSpacing;
  1027.     end;
  1028.     fpImgOnly: begin
  1029.       D.X:= ClientRect.Width;
  1030.       D.Y:= ClientRect.Height;
  1031.     end;
  1032.     fpImgNone: begin
  1033.  
  1034.     end;
  1035.   end;
  1036.  
  1037.   //Make result D centered with ClientRect
  1038.   //If Margin = -1 then center else shift image and caption from image edge
  1039.   Result.Left:= (ClientRect.Width div 2) - (D.X div 2);
  1040.   Result.Right:= Result.Left + D.X;
  1041.   Result.Top:= (ClientRect.Height div 2) - (D.Y div 2);
  1042.   Result.Bottom:= Result.Top + D.Y;
  1043.  
  1044.   W:= Result.Width;
  1045.   H:= Result.Height;
  1046.  
  1047.   if FMargin <> -1 then begin
  1048.     case FImagePosition of
  1049.       fpImgTop: begin
  1050.         Result.Top:= FMargin;
  1051.         Result.Bottom:= FMargin + H;
  1052.       end;
  1053.       fpImgBottom: begin
  1054.         Result.Bottom:= ClientHeight - FMargin;
  1055.         Result.Top:= ClientHeight - FMargin - H;
  1056.       end;
  1057.       fpImgLeft: begin
  1058.         Result.Left:= FMargin;
  1059.         Result.Right:= FMargin + W;
  1060.       end;
  1061.       fpImgRight: begin
  1062.         Result.Right:= ClientWidth - FMargin;
  1063.         Result.Left:= ClientWidth - FMargin - W;
  1064.       end;
  1065.       fpImgOnly: begin
  1066.  
  1067.       end;
  1068.       fpImgNone: begin
  1069.  
  1070.       end;
  1071.     end;
  1072.   end;
  1073.  
  1074. end;
  1075.  
  1076. function TFontButton.CaptionRect: TRect;
  1077. var
  1078.   Dims: TPoint;
  1079. begin
  1080.   Result:= ContentRect;
  1081.   Dims:= CaptionDims;
  1082.  
  1083.   case ImagePosition of
  1084.     fpImgTop: begin
  1085.       //Text resized on bottom, image filled on top
  1086.       Result.Top:= Result.Bottom - Dims.Y;
  1087.     end;
  1088.     fpImgBottom: begin
  1089.       //Text resized on top, image filled on bottom
  1090.       Result.Bottom:= Result.Top + Dims.Y;
  1091.     end;
  1092.     fpImgLeft: begin
  1093.       //Image resized on left, text filled on right
  1094.       Result.Left:= Result.Right - Dims.X;
  1095.     end;
  1096.     fpImgRight: begin
  1097.       //Image resized on right, text filled on left
  1098.       Result.Right:= Result.Left + Dims.X;
  1099.     end;
  1100.     fpImgOnly: begin
  1101.       //Result means nothing here really
  1102.     end;
  1103.     fpImgNone: begin
  1104.       //No image, just caption
  1105.     end;
  1106.   end;
  1107. end;
  1108.  
  1109. function TFontButton.ImageRect: TRect;
  1110. var
  1111.   Dims: TPoint;
  1112. begin
  1113.   Result:= ContentRect;
  1114.   Dims:= ImageDims;
  1115.  
  1116.   case ImagePosition of
  1117.     fpImgTop: begin
  1118.       //Text resized on bottom, image filled on top
  1119.       Result.Bottom:= Result.Top + Dims.Y;
  1120.     end;
  1121.     fpImgBottom: begin
  1122.       //Text resized on top, image filled on bottom
  1123.       Result.Top:= Result.Bottom - Dims.Y;
  1124.     end;
  1125.     fpImgLeft: begin
  1126.       //Image resized on left, text filled on right
  1127.       Result.Right:= Result.Left + Dims.Y;
  1128.     end;
  1129.     fpImgRight: begin
  1130.       //Image resized on right, text filled on left
  1131.       Result.Left:= Result.Right - Dims.Y;
  1132.       //Result.Left:= ClientWidth - Result.Height;
  1133.       //Result.Right:= ClientWidth;
  1134.     end;
  1135.     fpImgOnly: begin
  1136.       //Keep result, fill full control with image
  1137.     end;
  1138.     fpImgNone: begin
  1139.       //No image, just caption, result means nothing here
  1140.     end;
  1141.   end;
  1142. end;
  1143.  
  1144. function TFontButton.CaptionFlags: Cardinal;
  1145. begin
  1146.   Result:= DT_SINGLELINE or DT_NOCLIP;
  1147.   case ImagePosition of
  1148.     fpImgTop: begin
  1149.       //Text resized on bottom, image filled on top
  1150.       Result:= Result or DT_CENTER or DT_TOP
  1151.     end;
  1152.     fpImgBottom: begin
  1153.       //Text resized on top, image filled on bottom
  1154.       Result:= Result or DT_CENTER or DT_BOTTOM
  1155.     end;
  1156.     fpImgLeft: begin
  1157.       //Image resized on left, text filled on right
  1158.       Result:= Result or DT_LEFT or DT_VCENTER
  1159.     end;
  1160.     fpImgRight: begin
  1161.       //Image resized on right, text filled on left
  1162.       Result:= Result or DT_RIGHT or DT_VCENTER
  1163.     end;
  1164.     fpImgOnly: begin
  1165.       //Ignore
  1166.     end;
  1167.     fpImgNone: begin
  1168.       //No image, just caption
  1169.       Result:= Result or DT_CENTER or DT_VCENTER
  1170.     end;
  1171.   end;
  1172. end;
  1173.  
  1174. function TFontButton.ImageFlags: Cardinal;
  1175. begin
  1176.   Result:= DT_SINGLELINE or DT_CENTER or DT_VCENTER or DT_NOCLIP;
  1177. end;
  1178.  
  1179. procedure TFontButton.DrawTheme;
  1180. const
  1181.   WordBreakFlag: array[Boolean] of Integer = (0, DT_WORDBREAK);
  1182. var
  1183.   Details:  TThemedElementDetails;
  1184.   DrawRect: TRect;
  1185. begin
  1186.   DrawRect := ClientRect;
  1187.   case State of
  1188.     fsDisabled: begin
  1189.       Details := StyleServices.GetElementDetails(tbPushButtonDisabled);
  1190.     end;
  1191.     fsFocused: begin
  1192.       Details := StyleServices.GetElementDetails(tbPushButtonDefaulted)
  1193.     end;
  1194.     fsHot: begin
  1195.       Details := StyleServices.GetElementDetails(tbPushButtonHot)
  1196.     end;
  1197.     fsNormal: begin
  1198.       if FDefault then
  1199.         Details := StyleServices.GetElementDetails(tbPushButtonDefaulted)
  1200.       else
  1201.         Details := StyleServices.GetElementDetails(tbPushButtonNormal);
  1202.     end;
  1203.     fsPressed: begin
  1204.       Details := StyleServices.GetElementDetails(tbPushButtonPressed)
  1205.     end;
  1206.   end;
  1207.   StyleServices.DrawElement(ActiveCanvas.Handle, Details, DrawRect);
  1208. end;
  1209.  
  1210. procedure TFontButton.DrawBtn;
  1211. var
  1212.   R: TRect;
  1213. begin
  1214.   {$IFDEF BUF_BMP}
  1215.   R:= FBuf.Canvas.ClipRect;
  1216.   {$ELSE}
  1217.   R:= Canvas.ClipRect;
  1218.   {$ENDIF}
  1219.   //Draw Windows Standard Button Background
  1220.   if IsThemeActive then begin
  1221.     //If Windows Themes are Enabled
  1222.     DrawThemeBackground(FButtonTheme, ActiveCanvas.Handle, 1, ButtonStateToInt(State), R, nil);
  1223.   end else begin
  1224.     //If Windows Themes are Disabled
  1225.     DrawFrameControl(ActiveCanvas.Handle, R, DFC_BUTTON, DFCS_BUTTONPUSH);
  1226.   end;
  1227. end;
  1228.  
  1229. function TFontButton.ActiveCanvas: TCanvas;
  1230. begin
  1231.   //Switches which canvas to draw to based on whether buffer is enabled or not
  1232.   {$IFDEF BUF_BMP}
  1233.   Result:= FBuf.Canvas;
  1234.   {$ELSE}
  1235.   Result:= Canvas;
  1236.   {$ENDIF}
  1237. end;
  1238.  
  1239. procedure TFontButton.DrawBackground;
  1240.   procedure DoDraw;
  1241.   begin
  1242.     if (StyleServices.Available) and
  1243.       (not SameText(TStyleManager.ActiveStyle.Name, 'Windows')) then
  1244.     begin
  1245.       DrawTheme;      //VCL style button
  1246.     end else begin
  1247.       DrawBtn;        //Windows themed button
  1248.     end;
  1249.   end;
  1250. begin
  1251.   ActiveCanvas.Brush.Style:= bsSolid;
  1252.   ActiveCanvas.Pen.Style:= psClear;
  1253.   DrawParentImage(Self, ActiveCanvas);
  1254.  
  1255.   case FDrawStyle of
  1256.     fdThemed: begin
  1257.       //Always draw themed background
  1258.       DoDraw;
  1259.     end;
  1260.     fdTransparent: begin
  1261.       //Draw nothing for background
  1262.     end;
  1263.     fdHybrid: begin
  1264.       //Only draw background if state is not in normal state or disabled state
  1265.       if (State <> fsNormal) and (State <> fsDisabled) then begin
  1266.         DoDraw;
  1267.       end;
  1268.     end;
  1269.   end;
  1270.  
  1271. end;
  1272.  
  1273. procedure TFontButton.DrawCaption;
  1274. var
  1275.   R: TRect;
  1276. begin
  1277.   //Caption
  1278.   if ImagePosition <> fpImgOnly then begin
  1279.     ActiveCanvas.Font.Assign(Font);
  1280.     R:= CaptionRect;
  1281.     ActiveCanvas.Brush.Style:= bsClear;
  1282.     ActiveCanvas.Pen.Style:= psClear;
  1283.     ActiveCanvas.Font.Color:= CaptionColor;
  1284.     if FClicking then begin
  1285.       R.Left:= R.Left + DownSize;
  1286.       R.Top:= R.Top + DownSize;
  1287.     end;
  1288.     if FShowGuides then begin
  1289.       ActiveCanvas.Pen.Style:= psSolid;
  1290.       ActiveCanvas.Pen.Color:= clBlack;
  1291.       ActiveCanvas.Pen.Width:= 1;
  1292.       ActiveCanvas.Rectangle(R);
  1293.     end;
  1294.     //DrawTextW(Canvas.Handle, Caption.Text, Length(Caption.Text), R, CaptionFlags or DT_CALCRECT);
  1295.     DrawTextW(ActiveCanvas.Handle, Text, Length(Text), R, CaptionFlags);
  1296.   end;
  1297. end;
  1298.  
  1299. procedure TFontButton.DrawImage;
  1300. var
  1301.   R: TRect;
  1302. begin
  1303.   //Image
  1304.   if ImagePosition <> fpImgNone then begin
  1305.     R:= ImageRect;
  1306.     ActiveCanvas.Font.Assign(FImage.Font);
  1307.     ActiveCanvas.Font.Size:= ImageFontSize;
  1308.     ActiveCanvas.Brush.Style:= bsClear;
  1309.     ActiveCanvas.Pen.Style:= psClear;
  1310.     ActiveCanvas.Font.Color:= ImageColor;
  1311.     if FClicking then begin
  1312.       R.Left:= R.Left + DownSize;
  1313.       R.Top:= R.Top + DownSize;
  1314.     end;
  1315.     if FShowGuides then begin
  1316.       ActiveCanvas.Pen.Style:= psSolid;
  1317.       ActiveCanvas.Pen.Color:= clBlack;
  1318.       ActiveCanvas.Pen.Width:= 1;
  1319.       ActiveCanvas.Rectangle(R);
  1320.     end;
  1321.     //DrawTextW(Canvas.Handle, FImage.Text, Length(FImage.Text), R, ImageFlags or DT_CALCRECT);
  1322.     DrawTextW(ActiveCanvas.Handle, FImage.Text, Length(FImage.Text), R, ImageFlags);
  1323.   end;
  1324. end;
  1325.  
  1326. procedure TFontButton.DrawFocus;
  1327. begin
  1328.   //Focus Frame
  1329.   if (Focused) and (FShowFocusRect) then begin
  1330.     DrawFocusRect(ActiveCanvas.Handle, ActiveCanvas.ClipRect);
  1331.   end;
  1332. end;
  1333.  
  1334. procedure TFontButton.Paint;
  1335. var
  1336.   PS: PAINTSTRUCT;
  1337. begin
  1338.   BeginPaint(Handle, PS);
  1339.   try
  1340.     {$IFDEF BUF_BMP}
  1341.     if FBuf.Width <> Width then
  1342.       FBuf.Width:= Width;
  1343.     if FBuf.Height <> Height then
  1344.       FBuf.Height:= Height;
  1345.     {$ENDIF}
  1346.     DrawBackground;
  1347.     DrawCaption;
  1348.     DrawImage;
  1349.     DrawFocus;
  1350.  
  1351.     {$IFDEF BUF_BMP}
  1352.     BitBlt(Canvas.Handle, 0, 0, Width, Height, FBuf.Handle, 0, 0, SRCCOPY);
  1353.     {$ENDIF}
  1354.   finally
  1355.     EndPaint(Handle, PS);
  1356.   end;
  1357. end;
  1358.  
  1359. procedure TFontButton.SetKind(const Value: TFontButtonKind);
  1360. begin
  1361.   if Value <> fkCustom then begin
  1362.     //Standard for all
  1363.     Image.Font.Name:= 'RMPicons';
  1364.     Image.UseStandardColor:= True;
  1365.     Image.StandardColor:= fcNeutral;
  1366.     StyleColors:= StyleColors - [scImage];
  1367.   end;
  1368.   case Value of
  1369.     fkCustom: begin
  1370.       //Do nothing
  1371.     end;
  1372.     fkOK: begin
  1373.       Image.StandardColor:= fcGreen;
  1374.       Image.Text:= ''; //Checkmark
  1375.     end;
  1376.     fkCancel: begin
  1377.       Image.StandardColor:= fcOrange;
  1378.       Image.Text:= ''; //X Extended
  1379.     end;
  1380.     fkUndo: begin
  1381.       Image.StandardColor:= fcOrange;
  1382.       Image.Text:= ''; //Loop left arrow
  1383.     end;
  1384.     fkClose: begin
  1385.       Image.StandardColor:= fcBlue;
  1386.       Image.Text:= ''; //Power
  1387.     end;
  1388.     fkClear: begin
  1389.       Image.StandardColor:= fcOrange;
  1390.       Image.Text:= ''; //X Extended
  1391.     end;
  1392.     fkEdit: begin
  1393.       Image.StandardColor:= fcOrange;
  1394.       Image.Text:= ''; //Pencil
  1395.     end;
  1396.     fkSave: begin
  1397.       Image.StandardColor:= fcBlue;
  1398.       Image.Text:= ''; //Disk
  1399.     end;
  1400.     fkAdd: begin
  1401.       Image.StandardColor:= fcGreen;
  1402.       Image.Text:= ''; //Plus
  1403.     end;
  1404.     fkDelete: begin
  1405.       Image.StandardColor:= fcRed;
  1406.       Image.Text:= ''; //X
  1407.     end;
  1408.     fkInfo: begin
  1409.       Image.StandardColor:= fcBlue;
  1410.       Image.Text:= ''; //i in circle
  1411.     end;
  1412.     fkHelp: begin
  1413.       Image.StandardColor:= fcBlue;
  1414.       Image.Text:= ''; //? in circle
  1415.     end;
  1416.     fkPrint: begin
  1417.       Image.StandardColor:= fcNeutral;
  1418.       Image.Text:= ''; //Printer
  1419.     end;
  1420.     fkPrintTag: begin
  1421.       Image.StandardColor:= fcNeutral;
  1422.       Image.Text:= ''; //Tag Printer
  1423.     end;
  1424.     fkCalc: begin
  1425.       Image.StandardColor:= fcNeutral;
  1426.       Image.Text:= ''; //Calculator
  1427.     end;
  1428.     fkRefresh: begin
  1429.       Image.StandardColor:= fcGreen;
  1430.       Image.Text:= ''; //Circular arrows
  1431.     end;
  1432.     fkView: begin
  1433.       Image.StandardColor:= fcNeutral;
  1434.       Image.Text:= ''; //Magnifier inside box
  1435.     end;
  1436.     fkMerge: begin
  1437.       Image.StandardColor:= fcNeutral;
  1438.       Image.Text:= ''; //Two lines combining into 1 arrow
  1439.     end;
  1440.     fkEmail: begin
  1441.       Image.StandardColor:= fcNeutral;
  1442.       Image.Text:= ''; //Envelope
  1443.     end;
  1444.     fkCloseX: begin
  1445.       Image.StandardColor:= fcRed;
  1446.       Image.Text:= ''; //X inside box
  1447.     end;
  1448.   end;
  1449.   FKind := Value;
  1450.   Invalidate;
  1451. end;
  1452.  
  1453. { TColorManager }
  1454.  
  1455. constructor TColorManager.Create;
  1456. begin
  1457.   FBaseColor:= clWhite;
  1458.   Invalidate;
  1459. end;
  1460.  
  1461. destructor TColorManager.Destroy;
  1462. begin
  1463.  
  1464.   inherited;
  1465. end;
  1466.  
  1467. function TColorManager.GetColor(Clr: TFontButtonColor): TColorRec;
  1468. begin
  1469.   Result:= FColors[Clr];
  1470. end;
  1471.  
  1472. procedure TColorManager.Invalidate;
  1473. var
  1474.   B: Byte;
  1475. begin
  1476.   B:= (FBaseColor.Red+FBaseColor.Green+FBaseColor.Blue) div 3;
  1477.   FIsBaseDark:= B < 100;
  1478.   if FIsBaseDark then begin
  1479.     FColors[fcNeutral]:= LIGHT_NEUTRAL;
  1480.     FColors[fcBlue]:= LIGHT_BLUE;
  1481.     FColors[fcGreen]:= LIGHT_GREEN;
  1482.     FColors[fcRed]:= LIGHT_RED;
  1483.     FColors[fcYellow]:= LIGHT_YELLOW;
  1484.     FColors[fcOrange]:= LIGHT_ORANGE;
  1485.   end else begin
  1486.     FColors[fcNeutral]:= DARK_NEUTRAL;
  1487.     FColors[fcBlue]:= DARK_BLUE;
  1488.     FColors[fcGreen]:= DARK_GREEN;
  1489.     FColors[fcRed]:= DARK_RED;
  1490.     FColors[fcYellow]:= DARK_YELLOW;
  1491.     FColors[fcOrange]:= DARK_ORANGE;
  1492.   end;
  1493. end;
  1494.  
  1495. function TColorManager.IsBaseDark: Boolean;
  1496. begin
  1497.   Result:= FIsBaseDark;
  1498. end;
  1499.  
  1500. procedure TColorManager.SetBaseColor(const Value: TColorRec);
  1501. begin
  1502.   FBaseColor:= TColor(Value);
  1503.   SendMessage(Application.Handle, WM_COLORCHANGE, 0, 0);
  1504.   //Invalidate;
  1505. end;
  1506.  
  1507. procedure TColorManager.SetColor(Clr: TFontButtonColor; const Value: TColorRec);
  1508. begin
  1509.   FColors[Clr]:= Value;
  1510.   SendMessage(Application.Handle, WM_COLORCHANGE, 0, 0);
  1511.   //Invalidate;
  1512. end;
  1513.  
  1514. initialization
  1515.   _ColorManager:= TColorManager.Create;
  1516. finalization
  1517.   _ColorManager.Free;
  1518. end.
Advertisement
Add Comment
Please, Sign In to add comment