2
0

bclealcddisplay_editor.pas 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. unit BCLeaLCDDisplay_Editor;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, PropEdits, ComponentEditors,
  6. BCLeaLCDDisplay;
  7. type
  8. TBCLeaLCDDisplayCharDefsPropertyEditor = class(TPersistentPropertyEditor)
  9. public
  10. procedure Edit; override;
  11. procedure ExecuteVerb(Index: integer); override;
  12. function GetAttributes: TPropertyAttributes; override;
  13. function GetVerb(Index: integer): string; override;
  14. function GetVerbCount: integer; override;
  15. function BCLeaLCDDisplay: TBCLeaLCDDisplay;
  16. end;
  17. TBCLeaLCDDisplayComponentEditor = class(TComponentEditor)
  18. private
  19. procedure EditLines;
  20. public
  21. procedure Edit; override;
  22. procedure ExecuteVerb(Index: integer); override;
  23. function GetVerb(Index: integer): string; override;
  24. function GetVerbCount: integer; override;
  25. function BCLeaLCDDisplay: TBCLeaLCDDisplay;
  26. end;
  27. procedure EditCharDefs(ABCLeaLCDDisplay: TBCLeaLCDDisplay);
  28. implementation
  29. uses
  30. Controls, StdCtrls, Dialogs, ButtonPanel, Forms,
  31. BCLeaLCDDisplay_EditorForm;
  32. { Opens the char def editor. }
  33. procedure EditCharDefs(ABCLeaLCDDisplay: TBCLeaLCDDisplay);
  34. var
  35. F: TBCLeaLCDCharDefsEditor;
  36. begin
  37. F := TBCLeaLCDCharDefsEditor.Create(nil);
  38. try
  39. F.Position := poScreenCenter;
  40. F.BCLeaLCDDisplay := TBCLeaLCDDisplay(ABCLeaLCDDisplay);
  41. F.ShowModal; // Cancel has been handled by the editor form.
  42. finally
  43. F.Free;
  44. end;
  45. end;
  46. { Loads the char defs of the specified BCLeaLCDDisplay from an xml file. }
  47. procedure LoadCharDefsFromFile(ABCLeaLCDDisplay: TBCLeaLCDDisplay);
  48. var
  49. dlg: TOpenDialog;
  50. begin
  51. dlg := TOpenDialog.Create(nil);
  52. try
  53. dlg.FileName := '';
  54. dlg.Filter := 'XML files (*.xml)|*.xml';
  55. if dlg.Execute then
  56. begin
  57. ABCLeaLCDDisplay.CharDefs.LoadFromFile(dlg.FileName);
  58. ABCLeaLCDDisplay.Invalidate;
  59. end;
  60. finally
  61. dlg.Free;
  62. end;
  63. end;
  64. { Saves the chardefs of the specified BCLeaLCDDisplay to an xml file. }
  65. procedure SaveCharDefsToFile(ABCLeaLCDDisplay: TBCLeaLCDDisplay);
  66. var
  67. dlg: TOpenDialog;
  68. begin
  69. dlg := TSaveDialog.Create(nil);
  70. try
  71. dlg.FileName := '';
  72. dlg.Filter := 'XML files (*.xml)|*.xml';
  73. if dlg.Execute then
  74. ABCLeaLCDDisplay.CharDefs.SaveToFile(dlg.FileName);
  75. finally
  76. dlg.Free;
  77. end;
  78. end;
  79. { TBCLeaLCDDisplayCharDefsPropertyEditor }
  80. { Opens the chardefs editor. }
  81. procedure TBCLeaLCDDisplayCharDefsPropertyEditor.Edit;
  82. begin
  83. EditCharDefs(BCLeaLCDDisplay);
  84. end;
  85. { Executes the routines assigned to the CharDefs context menu }
  86. procedure TBCLeaLCDDisplayCharDefsPropertyEditor.ExecuteVerb(Index: integer);
  87. begin
  88. case Index of
  89. 0: Edit;
  90. 1: LoadCharDefsFromFile(BCLeaLCDDisplay);
  91. 2: SaveCharDefsToFile(BCLeaLCDDisplay);
  92. end;
  93. end;
  94. { The property editor should open the CharDefs editor. }
  95. function TBCLeaLCDDisplayCharDefsPropertyEditor.GetAttributes: TPropertyAttributes;
  96. begin
  97. Result := inherited GetAttributes + [paDialog];
  98. end;
  99. { Determines how many items will be added to the CharDefs context menu. }
  100. function TBCLeaLCDDisplayCharDefsPropertyEditor.GetVerbCount: integer;
  101. begin
  102. Result := 3;
  103. end;
  104. { Determines the menu item text for CharDefs context menu. }
  105. function TBCLeaLCDDisplayCharDefsPropertyEditor.GetVerb(Index: integer): string;
  106. begin
  107. case Index of
  108. 0: Result := 'Edit...';
  109. 1: Result := 'Load from file...';
  110. 2: Result := 'Save to file...';
  111. end;
  112. end;
  113. function TBCLeaLCDDisplayCharDefsPropertyEditor.BCLeaLCDDisplay: TBCLeaLCDDisplay;
  114. begin
  115. Result := TBCLeaLCDDisplay(GetComponent(0));
  116. end;
  117. { TBCLeaLCDDisplayComponentEditor }
  118. procedure TBCLeaLCDDisplayComponentEditor.Edit;
  119. begin
  120. ExecuteVerb(0);
  121. end;
  122. procedure TBCLeaLCDDisplayComponentEditor.EditLines;
  123. var
  124. F: TForm;
  125. Memo: TMemo;
  126. begin
  127. F := TForm.CreateNew(nil);
  128. try
  129. F.Caption := 'Edit BCLeaLCDDisplay text';
  130. F.Position := poScreenCenter;
  131. F.Width := 300;
  132. F.Height := 200;
  133. Memo := TMemo.Create(F);
  134. with Memo do
  135. begin
  136. Align := alClient;
  137. BorderSpacing.Around := 8;
  138. Parent := F;
  139. Lines.Assign(BCLeaLCDDisplay.Lines);
  140. end;
  141. with TButtonPanel.Create(F) do
  142. begin
  143. ShowButtons := [pbOK, pbCancel];
  144. Parent := F;
  145. end;
  146. if F.ShowModal = mrOk then
  147. begin
  148. BCLeaLCDDisplay.Lines.Assign(Memo.Lines);
  149. BCLeaLCDDisplay.Invalidate;
  150. end;
  151. finally
  152. F.Free;
  153. end;
  154. end;
  155. procedure TBCLeaLCDDisplayComponentEditor.ExecuteVerb(Index: integer);
  156. begin
  157. case Index of
  158. 0: EditLines;
  159. 1: EditCharDefs(BCLeaLCDDisplay);
  160. 2: LoadCharDefsFromFile(BCLeaLCDDisplay);
  161. 3: SaveCharDefsToFile(BCLeaLCDDisplay);
  162. end;
  163. end;
  164. { Determines how many items will be added to the BCLeaLCDDisplay context menu. }
  165. function TBCLeaLCDDisplayComponentEditor.GetVerbCount: integer;
  166. begin
  167. Result := 4;
  168. end;
  169. { Determines the menu item text for BCLeaLCDDisplay context menu. }
  170. function TBCLeaLCDDisplayComponentEditor.GetVerb(Index: integer): string;
  171. begin
  172. case Index of
  173. 0: Result := 'Lines text...';
  174. 1: Result := 'Edit character defs...';
  175. 2: Result := 'Load character defs from file...';
  176. 3: Result := 'Save character defs to file...';
  177. end;
  178. end;
  179. function TBCLeaLCDDisplayComponentEditor.BCLeaLCDDisplay: TBCLeaLCDDisplay;
  180. begin
  181. Result := TBCLeaLCDDisplay(GetComponent);
  182. end;
  183. end.