bcsvgbutton.pas 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. // SPDX-License-Identifier: LGPL-3.0-linking-exception
  2. { A Graphic Button Control that uses SVG images as the button states
  3. for Normal,Hover and DOWN states.
  4. originally written in 2018 by User Josh on Lazarus Forum.
  5. You can use the SVGDOWNXML property to enter the SVG XML code to create the
  6. image or You can enter the full svg image file and pathname into the properties
  7. FileNameDown; it will then read in the File Information and place it in the
  8. SVGDownXML Property.
  9. This Component uses the BGRABITMAP and BGRACONTROLS Framework to implement
  10. the Button's Functionality
  11. }
  12. {******************************* CONTRIBUTOR(S) ******************************
  13. - Edivando S. Santos Brasil | [email protected]
  14. (Compatibility with delphi VCL 11/2018)
  15. ***************************** END CONTRIBUTOR(S) *****************************}
  16. unit BCSVGButton;
  17. {$I bgracontrols.inc}
  18. interface
  19. uses
  20. Classes, SysUtils, Forms, Controls, Graphics, Dialogs,
  21. {$IFDEF FPC}LResources, lazutils,{$ENDIF}
  22. {$IFNDEF FPC}Windows, Messages, BGRAGraphics, GraphType, FPImage, {$ENDIF}
  23. BCSVGViewer;
  24. type
  25. SVGButtonState = (MouseIn, MouseOut, Pressed);
  26. TBCSVGButton = class(TBCSVGViewer)
  27. private
  28. fsvgnormal:tstrings;
  29. fsvghover:tstrings;
  30. fsvgdown:tstrings;
  31. fdown:boolean;
  32. FState:SVGButtonState;
  33. FOwner: TComponent;
  34. FFileNameHover: String;
  35. FFileNameNormal: String;
  36. FFileNameDown: String;
  37. FPosition: Integer;
  38. FMax: Integer;
  39. FInfo1: String;
  40. FInfo2: String;
  41. // property OnPositionChange;
  42. procedure setdown(AValue: boolean);
  43. procedure ReadSVGFileAndSetString(fn:String;itm:Integer);
  44. procedure GenerateCompletedSVGImage(AValue: string);
  45. protected
  46. FOnPositionChange: TNotifyEvent;
  47. procedure MouseDown(Button: TMouseButton; Shift: TShiftState;
  48. MX, MY: integer); override;
  49. procedure MouseUp(Button: TMouseButton; Shift: TShiftState; MX, MY: integer); override;
  50. procedure MouseEnter; override;
  51. procedure MouseLeave; override;
  52. procedure setsvghoverxml(const AValue: tstrings);
  53. procedure setsvgnormalxml(const AValue: tstrings);
  54. procedure setsvgdownxml(const AValue: tstrings);
  55. procedure setFFileNameDown(const AValue: string);
  56. procedure setFFileNameHover(const AValue: string);
  57. procedure setFFileNameNormal(const AValue: string);
  58. procedure SetInfo1(const AValue:String);
  59. procedure SetInfo2(const AValue:String);
  60. procedure Setposition(const AValue:Integer);
  61. procedure SetMax(const AValue:Integer);
  62. procedure RedrawBitmapContent; override;
  63. public
  64. constructor Create(AOwner: TComponent); override;
  65. destructor Destroy; override;
  66. procedure paint; override;
  67. published
  68. property BorderSpacing;
  69. property Constraints;
  70. Property FileNameDown : String Read FFileNameDown Write setFFileNameDown;
  71. Property FileNameHover : String Read FFileNameHover Write setFFileNameHover;
  72. Property FileNameNormal : String Read FFileNameNormal Write setFFileNameNormal;
  73. property SVGNormalXML:tstrings read fsvgnormal write setsvgnormalxml;
  74. property SVGHoverXML:tstrings read fsvghover write setsvghoverxml;
  75. property SVGDownXML:tstrings read fsvgdown write setsvgdownxml;
  76. property Down:boolean read fdown write setdown default false;
  77. property Information1:string read FInfo1 write SetInfo1;
  78. property Information2:string read FInfo2 write SetInfo2;
  79. property Position:integer read fposition write SetPosition;
  80. property Maximum:integer read fmax write SetMax;
  81. property OnPositionChange: TNotifyEvent read FOnPositionChange write FOnPositionChange;
  82. end;
  83. {$IFDEF FPC}procedure Register;{$ENDIF}
  84. implementation
  85. procedure TBCSVGButton.Paint;
  86. begin
  87. inherited Paint;
  88. end;
  89. constructor TBCSVGButton.Create(AOwner: TComponent);
  90. begin
  91. inherited Create(AOwner);
  92. FOwner := AOwner;
  93. fsvgnormal := TStringList.Create;
  94. fsvghover := TStringList.Create;
  95. fsvgdown := TStringList.Create;
  96. FState := MouseOut;
  97. end;
  98. destructor TBCSVGButton.Destroy;
  99. begin
  100. fsvghover.Free;
  101. fsvghover := nil;
  102. fsvgnormal.Free;
  103. fsvgnormal := nil;
  104. fsvgdown.Free;
  105. fsvgdown := nil;
  106. inherited Destroy;
  107. end;
  108. //FSVG.CreateFromString(fsvgnormal.Text);
  109. procedure TBCSVGButton.GenerateCompletedSVGImage(AValue: string);
  110. begin
  111. FSVG.CreateFromString(AValue);
  112. end;
  113. procedure TBCSVGButton.ReadSVGFileAndSetString(fn:String;itm:Integer);
  114. var li,st: {$IFDEF FPC}ansistring{$ELSE}string{$ENDIF};
  115. F: {$IFDEF FPC}Text{$ELSE}TextFile{$ENDIF};
  116. begin
  117. li:='';
  118. st:='';
  119. if fileexists(fn) then
  120. begin
  121. AssignFile(F,fn);
  122. {$I-}
  123. Reset(F);
  124. {$I+}
  125. If (IoResult = 0) Then
  126. Begin
  127. While Not(EoF(F)) Do
  128. Begin
  129. ReadLn(F,Li);
  130. st:=st+li;
  131. If (EoF(F)) Then Break;
  132. End;
  133. End;
  134. CloseFile(F);
  135. end else showmessage('File Not Found');
  136. case itm of
  137. 0:begin
  138. if st<>'' then fsvgNormal.Text:=st;
  139. FFileNameNormal:='';
  140. end;
  141. 1:Begin
  142. if st<>'' then fsvgHover.Text:=st;
  143. FFileNameHover:='';
  144. End;
  145. 2:Begin
  146. if st<>'' then fsvgDown.Text:=st;
  147. FFileNameDown:='';
  148. ENd;
  149. end;
  150. if st<>'' then RedrawBitmap;
  151. End;
  152. procedure TBCSVGButton.SetInfo1(const AValue: string);
  153. begin
  154. If AValue<>'' then FInfo1:=AValue;
  155. end;
  156. procedure TBCSVGButton.SetInfo2(const AValue: string);
  157. begin
  158. If AValue<>'' then FInfo2:=AValue;
  159. end;
  160. procedure TBCSVGButton.setposition(const AValue: Integer);
  161. begin
  162. If AValue<>FPosition then
  163. begin
  164. FPosition:=AValue;
  165. if assigned(FOnPositionChange) then FOnPositionChange(self);
  166. end;
  167. end;
  168. procedure TBCSVGButton.setmax(const AValue: Integer);
  169. begin
  170. If AValue<>Fmax then Fmax:=AValue;
  171. end;
  172. procedure TBCSVGButton.setFFileNameNormal(const AValue: string);
  173. begin
  174. If AValue<>'' then ReadSVGFileAndSetString(AValue,0);
  175. end;
  176. procedure TBCSVGButton.setFFileNameHover(const AValue: string);
  177. begin
  178. If AValue<>'' then ReadSVGFileAndSetString(Avalue,1);
  179. end;
  180. procedure TBCSVGButton.setFFileNameDown(const AValue: string);
  181. begin
  182. If AValue<>'' then ReadSVGFileAndSetString(Avalue,2);
  183. End;
  184. procedure TBCSVGButton.setsvgnormalxml(const AValue: tstrings);
  185. begin
  186. if fsvgnormal.Text = AValue.Text then
  187. Exit;
  188. fsvgnormal.Assign(AValue);
  189. DiscardBitmap;
  190. if FDown=false then if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
  191. RedrawBitmap;
  192. // if not fdown then RedrawBitmap;
  193. end;
  194. procedure TBCSVGButton.setsvghoverxml(const AValue: tstrings);
  195. begin
  196. if fsvghover.Text = AValue.Text then
  197. Exit;
  198. fsvghover.Assign(AValue);
  199. DiscardBitmap;
  200. end;
  201. procedure TBCSVGButton.setsvgdownxml(const AValue: tstrings);
  202. begin
  203. if fsvgdown.Text = AValue.Text then
  204. Exit;
  205. fsvgdown.Assign(AValue);
  206. DiscardBitmap;
  207. if FDown then
  208. begin
  209. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
  210. RedrawBitmap;
  211. end;
  212. end;
  213. procedure TBCSVGButton.setdown(AValue: boolean);
  214. begin
  215. if fdown = AValue then
  216. Exit;
  217. fdown := AValue;
  218. if fdown=false then Fstate:=MouseOut;
  219. DiscardBitmap;
  220. if FDown then
  221. begin
  222. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
  223. end
  224. else
  225. begin
  226. if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
  227. end;
  228. RedrawBitmap;
  229. end;
  230. procedure TBCSVGButton.MouseDown(Button: TMouseButton; Shift: TShiftState;
  231. MX, MY: integer);
  232. begin
  233. inherited MouseDown(Button, Shift, MX, MY);
  234. if csDesigning in ComponentState then
  235. exit;
  236. if (Button = mbLeft) and Enabled then
  237. begin
  238. FState := Pressed;
  239. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text);
  240. // RedrawBitmapContent;
  241. RedrawBitmap;
  242. end;
  243. end;
  244. procedure TBCSVGButton.MouseUp(Button: TMouseButton; Shift: TShiftState;
  245. MX, MY: integer);
  246. begin
  247. inherited MouseUp(Button, Shift, MX, MY);
  248. if csDesigning in ComponentState then exit;
  249. if (Button = mbLeft) and Enabled then
  250. begin
  251. if FDown then
  252. begin
  253. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
  254. end
  255. else
  256. begin
  257. if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
  258. end;
  259. FState := MouseIn;
  260. // RedrawBitmapContent;
  261. RedrawBitmap;
  262. end;
  263. end;
  264. procedure TBCSVGButton.MouseEnter;
  265. begin
  266. if csDesigning in ComponentState then exit;
  267. inherited MouseEnter;
  268. if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
  269. FState := MouseIn;
  270. // RedrawBitmapContent;
  271. RedrawBitmap;
  272. end;
  273. procedure TBCSVGButton.MouseLeave;
  274. begin
  275. inherited MouseLeave;
  276. if csDesigning in ComponentState then
  277. exit;
  278. if FDown then
  279. begin
  280. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
  281. end
  282. else
  283. begin
  284. if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
  285. end;
  286. FState := MouseOut;
  287. // RedrawBitmapContent;
  288. RedrawBitmap;
  289. end;
  290. procedure TBCSVGButton.RedrawBitmapContent;
  291. begin
  292. if FDown then
  293. begin
  294. if fsvgdown.Text<>'' then GenerateCompletedSVGImage(fsvgdown.Text)
  295. end
  296. else
  297. begin
  298. case fstate of
  299. mousein :if fsvghover.Text<>'' then GenerateCompletedSVGImage(fsvghover.Text);
  300. mouseout:if fsvgnormal.Text<>'' then GenerateCompletedSVGImage(fsvgnormal.Text);
  301. end;
  302. end;
  303. inherited RedrawBitmapContent;
  304. end;
  305. {$IFDEF FPC}
  306. procedure Register;
  307. begin
  308. RegisterComponents('BGRA Button Controls',[TBCSVGButton]);
  309. end;
  310. {$ENDIF}
  311. end.