/examples/widestringstreaming/mainunit.pas

http://github.com/graemeg/lazarus · Pascal · 279 lines · 201 code · 35 blank · 43 comment · 14 complexity · e8f4d5fa0c8f90ee2563b7c1c5c69fb4 MD5 · raw file

  1. {
  2. ***************************************************************************
  3. * *
  4. * This source is free software; you can redistribute it and/or modify *
  5. * it under the terms of the GNU General Public License as published by *
  6. * the Free Software Foundation; either version 2 of the License, or *
  7. * (at your option) any later version. *
  8. * *
  9. * This code is distributed in the hope that it will be useful, but *
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
  12. * General Public License for more details. *
  13. * *
  14. * A copy of the GNU General Public License is available on the World *
  15. * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
  16. * obtain it by writing to the Free Software Foundation, *
  17. * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. * *
  19. ***************************************************************************
  20. }
  21. unit MainUnit;
  22. {$mode objfpc}{$H+}
  23. interface
  24. uses
  25. Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics,
  26. Dialogs, StdCtrls, Buttons;
  27. type
  28. { TMyComponent }
  29. TMyComponent = class(TCheckBox)
  30. private
  31. FDefaultText: WideString;
  32. FInteger1: integer;
  33. FWideStr1: widestring;
  34. function Integer1IsStored: boolean;
  35. procedure SetDefaultText(const AValue: WideString);
  36. procedure SetInteger1(const AValue: integer);
  37. procedure SetWideStr1(const AValue: widestring);
  38. function WideStr1IsStored: boolean;
  39. procedure ReadText(Reader: TReader);
  40. procedure WriteText(Writer: TWriter);
  41. protected
  42. procedure DefineProperties(Filer: TFiler); override;
  43. public
  44. constructor Create(TheOwner: TComponent); override;
  45. published
  46. property WideStr1: widestring read FWideStr1 write SetWideStr1 stored WideStr1IsStored;
  47. property DefaultText: WideString read FDefaultText write SetDefaultText stored False;
  48. property Integer1: integer read FInteger1 write SetInteger1;
  49. end;
  50. { TStreamDemoForm }
  51. TStreamDemoForm = class(TForm)
  52. AGroupBox: TGroupBox;
  53. StreamAsLFMCheckBox: TCheckBox;
  54. Note2Label: TLabel;
  55. Note1Label: TLabel;
  56. ReadStreamButton: TButton;
  57. StreamMemo: TMemo;
  58. StreamGroupBox: TGroupBox;
  59. WriteToStreamButton: TButton;
  60. SourceGroupBox: TGroupBox;
  61. DestinationGroupBox: TGroupBox;
  62. procedure FormCreate(Sender: TObject);
  63. procedure ReadStreamButtonClick(Sender: TObject);
  64. procedure StreamAsLFMCheckBoxChange(Sender: TObject);
  65. procedure WriteToStreamButtonClick(Sender: TObject);
  66. public
  67. StreamAsString: string;
  68. procedure ShowStreamInMemo;
  69. procedure SaveStreamAsString(AStream: TStream);
  70. procedure ReadStreamFromString(AStream: TStream);
  71. function ReadStringFromStream(AStream: TStream): string;
  72. procedure ClearDestinationGroupBox;
  73. procedure OnFindClass(Reader: TReader; const AClassName: string;
  74. var ComponentClass: TComponentClass);
  75. end;
  76. var
  77. StreamDemoForm: TStreamDemoForm;
  78. implementation
  79. {$R mainunit.lfm}
  80. { TStreamDemoForm }
  81. procedure TStreamDemoForm.WriteToStreamButtonClick(Sender: TObject);
  82. var
  83. AStream: TMemoryStream;
  84. begin
  85. AStream:=TMemoryStream.Create;
  86. try
  87. WriteComponentAsBinaryToStream(AStream,AGroupBox);
  88. SaveStreamAsString(AStream);
  89. finally
  90. AStream.Free;
  91. end;
  92. end;
  93. procedure TStreamDemoForm.ReadStreamButtonClick(Sender: TObject);
  94. var
  95. NewComponent: TComponent;
  96. AStream: TMemoryStream;
  97. begin
  98. ClearDestinationGroupBox;
  99. AStream:=TMemoryStream.Create;
  100. try
  101. ReadStreamFromString(AStream);
  102. NewComponent:=nil;
  103. ReadComponentFromBinaryStream(AStream,NewComponent,
  104. @OnFindClass,DestinationGroupBox);
  105. if NewComponent is TControl then
  106. TControl(NewComponent).Parent:=DestinationGroupBox;
  107. finally
  108. AStream.Free;
  109. end;
  110. end;
  111. procedure TStreamDemoForm.FormCreate(Sender: TObject);
  112. var
  113. MyComponent: TMyComponent;
  114. begin
  115. // create a checkbox with Owner = AGroupBox
  116. // because TWriter writes all components owned by AGroupBox
  117. MyComponent:=TMyComponent.Create(AGroupBox);
  118. with MyComponent do begin
  119. Name:='MyComponent';
  120. Parent:=AGroupBox;
  121. end;
  122. end;
  123. procedure TStreamDemoForm.StreamAsLFMCheckBoxChange(Sender: TObject);
  124. begin
  125. ShowStreamInMemo;
  126. end;
  127. procedure TStreamDemoForm.ShowStreamInMemo;
  128. var
  129. LRSStream: TMemoryStream;
  130. LFMStream: TMemoryStream;
  131. begin
  132. if StreamAsLFMCheckBox.Checked then begin
  133. // convert the stream to LFM
  134. LRSStream:=TMemoryStream.Create;
  135. LFMStream:=TMemoryStream.Create;
  136. try
  137. ReadStreamFromString(LRSStream);
  138. LRSObjectBinaryToText(LRSStream,LFMStream);
  139. StreamMemo.Lines.Text:=ReadStringFromStream(LFMStream);
  140. finally
  141. LRSStream.Free;
  142. LFMStream.Free;
  143. end;
  144. end else begin
  145. // the stream is in binary format and contains characters, that can not be
  146. // shown in the memo. Convert all special characters to hexnumbers.
  147. StreamMemo.Lines.Text:=DbgStr(StreamAsString);
  148. end;
  149. end;
  150. procedure TStreamDemoForm.SaveStreamAsString(AStream: TStream);
  151. begin
  152. StreamAsString:=ReadStringFromStream(AStream);
  153. ShowStreamInMemo;
  154. end;
  155. procedure TStreamDemoForm.ReadStreamFromString(AStream: TStream);
  156. begin
  157. AStream.Size:=0;
  158. if StreamAsString<>'' then
  159. AStream.Write(StreamAsString[1],length(StreamAsString));
  160. AStream.Position:=0;
  161. end;
  162. function TStreamDemoForm.ReadStringFromStream(AStream: TStream): string;
  163. begin
  164. AStream.Position:=0;
  165. SetLength(Result,AStream.Size);
  166. if Result<>'' then
  167. AStream.Read(Result[1],length(Result));
  168. end;
  169. procedure TStreamDemoForm.ClearDestinationGroupBox;
  170. { free all components owned by DestinationGroupBox
  171. Do not confuse 'Owner' and 'Parent';
  172. The 'Owner' of a TComponent is responsible for freeing the component.
  173. All components owned by a component can be found in its 'Components'
  174. property.
  175. The 'Parent' of a TControl is the visible container. For example
  176. DestinationGroupBox has as Parent the form (StreamDemoForm).
  177. All controls with the same parent are gathered in Parent.Controls.
  178. In this simple example the created component has as Owner and Parent the
  179. DestinationGroupBox.
  180. }
  181. begin
  182. while DestinationGroupBox.ComponentCount>0 do
  183. DestinationGroupBox.Components[0].Free;
  184. end;
  185. procedure TStreamDemoForm.OnFindClass(Reader: TReader;
  186. const AClassName: string; var ComponentClass: TComponentClass);
  187. begin
  188. if CompareText(AClassName,'TGroupBox')=0 then
  189. ComponentClass:=TGroupBox
  190. else if CompareText(AClassName,'TCheckBox')=0 then
  191. ComponentClass:=TCheckBox
  192. else if CompareText(AClassName,'TMyComponent')=0 then
  193. ComponentClass:=TMyComponent;
  194. end;
  195. { TMyComponent }
  196. procedure TMyComponent.SetWideStr1(const AValue: widestring);
  197. begin
  198. if FWideStr1=AValue then exit;
  199. FWideStr1:=AValue;
  200. end;
  201. procedure TMyComponent.SetDefaultText(const AValue: WideString);
  202. begin
  203. if FDefaultText=AValue then exit;
  204. FDefaultText:=AValue;
  205. end;
  206. function TMyComponent.Integer1IsStored: boolean;
  207. begin
  208. Result:=FInteger1=3;
  209. end;
  210. procedure TMyComponent.SetInteger1(const AValue: integer);
  211. begin
  212. if FInteger1=AValue then exit;
  213. FInteger1:=AValue;
  214. end;
  215. function TMyComponent.WideStr1IsStored: boolean;
  216. begin
  217. Result:=WideStr1<>'Node';
  218. end;
  219. procedure TMyComponent.ReadText(Reader: TReader);
  220. begin
  221. case Reader.NextValue of
  222. vaLString, vaString:
  223. SetDefaultText(Reader.ReadString);
  224. else
  225. SetDefaultText(Reader.ReadWideString);
  226. end;
  227. end;
  228. procedure TMyComponent.WriteText(Writer: TWriter);
  229. begin
  230. Writer.WriteWideString(FDefaultText);
  231. end;
  232. procedure TMyComponent.DefineProperties(Filer: TFiler);
  233. begin
  234. inherited DefineProperties(Filer);
  235. Filer.DefineProperty('WideDefaultText', @ReadText, @WriteText, FDefaultText <> 'Node');
  236. end;
  237. constructor TMyComponent.Create(TheOwner: TComponent);
  238. begin
  239. inherited Create(TheOwner);
  240. FWideStr1:='';
  241. FInteger1:=3;
  242. end;
  243. end.