| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- // SPDX-License-Identifier: LGPL-3.0-linking-exception
- { A Graphic Button Control that uses SVG images as the button states
- for Normal,Hover and DOWN states.
- originally written in 2018 by User Josh on Lazarus Forum.
- You can use the SVGDOWNXML property to enter the SVG XML code to create the
- image or You can enter the full svg image file and pathname into the properties
- FileNameDown; it will then read in the File Information and place it in the
- SVGDownXML Property.
- This Component uses the BGRABITMAP and BGRACONTROLS Framework to implement
- the Button's Functionality
- }
- {******************************* CONTRIBUTOR(S) ******************************
- - Edivando S. Santos Brasil | [email protected]
- (Compatibility with delphi VCL 11/2018)
- ***************************** END CONTRIBUTOR(S) *****************************}
- unit BCSVGButton;
- {$I bgracontrols.inc}
- interface
- uses
- Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
- {$IFDEF FPC}LResources, lazutils,{$ENDIF}
- {$IFNDEF FPC}Windows, Messages, BGRAGraphics, GraphType, FPImage, {$ENDIF}
- BCSVGViewer;
- type
- SVGButtonState = (MouseIn, MouseOut, Pressed);
- TBCSVGButton = class(TBCSVGViewer)
- private
- fsvgnormal:tstrings;
- fsvghover:tstrings;
- fsvgdown:tstrings;
- fdown:boolean;
- FState:SVGButtonState;
- FOwner: TComponent;
- FFileNameHover: String;
- FFileNameNormal: String;
- FFileNameDown: String;
- FPosition: Integer;
- FMax: Integer;
- FInfo1: String;
- FInfo2: String;
- // property OnPositionChange;
- procedure setdown(AValue: boolean);
- procedure ReadSVGFileAndSetString(fn:String;itm:Integer);
- procedure GenerateCompletedSVGImage(AValue: string);
- protected
- FOnPositionChange: TNotifyEvent;
- procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
- MX, MY: integer); override;
- procedure MouseUp(Button: TMouseButton; Shift: TShiftState; MX, MY: integer); override;
- procedure MouseEnter; override;
- procedure MouseLeave; override;
- procedure setsvghoverxml(const AValue: tstrings);
- procedure setsvgnormalxml(const AValue: tstrings);
- procedure setsvgdownxml(const AValue: tstrings);
- procedure setFFileNameDown(const AValue: string);
- procedure setFFileNameHover(const AValue: string);
- procedure setFFileNameNormal(const AValue: string);
- procedure SetInfo1(const AValue:String);
- procedure SetInfo2(const AValue:String);
- procedure Setposition(const AValue:Integer);
- procedure SetMax(const AValue:Integer);
- procedure RedrawBitmapContent; override;
- public
- constructor Create(AOwner: TComponent); override;
- destructor Destroy; override;
- procedure paint; override;
- published
- property BorderSpacing;
- property Constraints;
- Property FileNameDown : String Read FFileNameDown Write setFFileNameDown;
- Property FileNameHover : String Read FFileNameHover Write setFFileNameHover;
- Property FileNameNormal : String Read FFileNameNormal Write setFFileNameNormal;
- property SVGNormalXML:tstrings read fsvgnormal write setsvgnormalxml;
- property SVGHoverXML:tstrings read fsvghover write setsvghoverxml;
- property SVGDownXML:tstrings read fsvgdown write setsvgdownxml;
- property Down:boolean read fdown write setdown default false;
- property Information1:string read FInfo1 write SetInfo1;
- property Information2:string read FInfo2 write SetInfo2;
- property Position:integer read fposition write SetPosition;
- property Maximum:integer read fmax write SetMax;
- property OnPositionChange: TNotifyEvent read FOnPositionChange write FOnPositionChange;
- end;
- {$IFDEF FPC}procedure Register;{$ENDIF}
- implementation
- procedure TBCSVGButton.Paint;
- begin
- inherited Paint;
- end;
- constructor TBCSVGButton.Create(AOwner: TComponent);
- begin
- inherited Create(AOwner);
- FOwner := AOwner;
- fsvgnormal := TStringList.Create;
- fsvghover := TStringList.Create;
- fsvgdown := TStringList.Create;
- FState := MouseOut;
- end;
- destructor TBCSVGButton.Destroy;
- begin
- fsvghover.Free;
- fsvghover := nil;
- fsvgnormal.Free;
- fsvgnormal := nil;
- fsvgdown.Free;
- fsvgdown := nil;
- inherited Destroy;
- end;
- //FSVG.CreateFromString(fsvgnormal.Text);
- procedure TBCSVGButton.GenerateCompletedSVGImage(AValue: string);
- begin
- FSVG.CreateFromString(AValue);
- end;
- procedure TBCSVGButton.ReadSVGFileAndSetString(fn:String;itm:Integer);
- var li,st: {$IFDEF FPC}ansistring{$ELSE}string{$ENDIF};
- F: {$IFDEF FPC}Text{$ELSE}TextFile{$ENDIF};
- begin
- li:='';
- st:='';
- if fileexists(fn) then
- begin
- AssignFile(F,fn);
- {$I-}
- Reset(F);
- {$I+}
- If (IoResult = 0) Then
- Begin
- While Not(EoF(F)) Do
- Begin
- ReadLn(F,Li);
- st:=st+li;
- If (EoF(F)) Then Break;
- End;
- End;
- CloseFile(F);
- end else showmessage('File Not Found');
- case itm of
- 0:begin
- if st<>'' then fsvgNormal.Text:=st;
- FFileNameNormal:='';
- end;
- 1:Begin
- if st<>'' then fsvgHover.Text:=st;
- FFileNameHover:='';
- End;
- 2:Begin
- if st<>'' then fsvgDown.Text:=st;
- FFileNameDown:='';
- ENd;
- end;
- if st<>'' then RedrawBitmap;
- End;
- procedure TBCSVGButton.SetInfo1(const AValue: string);
- begin
- If AValue<>'' then FInfo1:=AValue;
- end;
- procedure TBCSVGButton.SetInfo2(const AValue: string);
- begin
- If AValue<>'' then FInfo2:=AValue;
- end;
- procedure TBCSVGButton.setposition(const AValue: Integer);
- begin
- If AValue<>FPosition then
- begin
- FPosition:=AValue;
- if assigned(FOnPositionChange) then FOnPositionChange(self);
- end;
- end;
- procedure TBCSVGButton.setmax(const AValue: Integer);
- begin
- If AValue<>Fmax then Fmax:=AValue;
- end;
- procedure TBCSVGButton.setFFileNameNormal(const AValue: string);
- begin
- If AValue<>'' then ReadSVGFileAndSetString(AValue,0);
- end;
- procedure TBCSVGButton.setFFileNameHover(const AValue: string);
- begin
- If AValue<>'' then ReadSVGFileAndSetString(Avalue,1);
- end;
- procedure TBCSVGButton.setFFileNameDown(const AValue: string);
- begin
- If AValue<>'' then ReadSVGFileAndSetString(Avalue,2);
- End;
- procedure TBCSVGButton.setsvgnormalxml(const AValue: tstrings);
- begin
- if fsvgnormal.Text = AValue.Text then
- Exit;
- fsvgnormal.Assign(AValue);
- DiscardBitmap;
- if FDown=false then if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
- RedrawBitmap;
- // if not fdown then RedrawBitmap;
- end;
- procedure TBCSVGButton.setsvghoverxml(const AValue: tstrings);
- begin
- if fsvghover.Text = AValue.Text then
- Exit;
- fsvghover.Assign(AValue);
- DiscardBitmap;
- end;
- procedure TBCSVGButton.setsvgdownxml(const AValue: tstrings);
- begin
- if fsvgdown.Text = AValue.Text then
- Exit;
- fsvgdown.Assign(AValue);
- DiscardBitmap;
- if FDown then
- begin
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
- RedrawBitmap;
- end;
- end;
- procedure TBCSVGButton.setdown(AValue: boolean);
- begin
- if fdown = AValue then
- Exit;
- fdown := AValue;
- if fdown=false then Fstate:=MouseOut;
- DiscardBitmap;
- if FDown then
- begin
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
- end
- else
- begin
- if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
- end;
- RedrawBitmap;
- end;
- procedure TBCSVGButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
- MX, MY: integer);
- begin
- inherited MouseDown(Button, Shift, MX, MY);
- if csDesigning in ComponentState then
- exit;
- if (Button = mbLeft) and Enabled then
- begin
- FState := Pressed;
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
- // RedrawBitmapContent;
- RedrawBitmap;
- end;
- end;
- procedure TBCSVGButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
- MX, MY: integer);
- begin
- inherited MouseUp(Button, Shift, MX, MY);
- if csDesigning in ComponentState then exit;
- if (Button = mbLeft) and Enabled then
- begin
- if FDown then
- begin
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
- end
- else
- begin
- if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
- end;
- FState := MouseIn;
- // RedrawBitmapContent;
- RedrawBitmap;
- end;
- end;
- procedure TBCSVGButton.MouseEnter;
- begin
- if csDesigning in ComponentState then exit;
- inherited MouseEnter;
- if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
- FState := MouseIn;
- // RedrawBitmapContent;
- RedrawBitmap;
- end;
- procedure TBCSVGButton.MouseLeave;
- begin
- inherited MouseLeave;
- if csDesigning in ComponentState then
- exit;
- if FDown then
- begin
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
- end
- else
- begin
- if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
- end;
- FState := MouseOut;
- // RedrawBitmapContent;
- RedrawBitmap;
- end;
- procedure TBCSVGButton.RedrawBitmapContent;
- begin
- if FDown then
- begin
- if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
- end
- else
- begin
- case fstate of
- mousein :if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
- mouseout:if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
- end;
- end;
- inherited RedrawBitmapContent;
- end;
- {$IFDEF FPC}
- procedure Register;
- begin
- RegisterComponents('BGRA Button Controls',[TBCSVGButton]);
- end;
- {$ENDIF}
- end.
|