PngComponentEditors.pas 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. unit PngComponentEditors;
  2. interface
  3. uses
  4. Windows, SysUtils, Forms, Classes, Controls, PngImageList, TypInfo,
  5. DesignIntf, DesignEditors, ColnEdit;
  6. type
  7. TPngImageListEditor = class(TComponentEditor)
  8. public
  9. procedure Edit; override;
  10. procedure ExecuteVerb(Index: Integer); override;
  11. function GetVerb(Index: Integer): string; override;
  12. function GetVerbCount: Integer; override;
  13. end;
  14. TPngImageCollectionEditor = class(TComponentEditor)
  15. public
  16. procedure Edit; override;
  17. procedure ExecuteVerb(Index: Integer); override;
  18. function GetVerb(Index: Integer): string; override;
  19. function GetVerbCount: Integer; override;
  20. end;
  21. TPngButtonEditor = class(TComponentEditor)
  22. public
  23. procedure Edit; override;
  24. procedure ExecuteVerb(Index: Integer); override;
  25. function GetVerb(Index: Integer): string; override;
  26. function GetVerbCount: Integer; override;
  27. end;
  28. TPngImageListImagesEditor = class(TStringProperty)
  29. public
  30. procedure Edit; override;
  31. function GetAttributes: TPropertyAttributes; override;
  32. function GetValue: string; override;
  33. end;
  34. TPngImageCollectionItemsEditor = class(TPngImageListImagesEditor)
  35. public
  36. procedure Edit; override;
  37. end;
  38. TEditProperty = class
  39. private
  40. FPropery: string;
  41. procedure EnumProperty(const Prop: IProperty);
  42. public
  43. constructor Create(Component: TComponent; const Prop: string; Designer: IDesigner);
  44. end;
  45. implementation
  46. uses
  47. PngImageListEditor;
  48. resourcestring
  49. SEditImage = '&Edit image...';
  50. SRecreateImages = '&Recreate images...';
  51. SEditImages = '&Edit images...';
  52. SEditing = 'Editing %s.%s';
  53. SPNGObjectsHaveBeenCopied = 'The PNG objects have been copied to the internal imagelist.';
  54. //This type is neccesary to be able to call CopyPngs without having to make it
  55. //public in the TPngImageList class.
  56. type
  57. TPngImageListAccess = class(TPngImageList);
  58. procedure EditProperty(Component: TComponent; const Prop: string; Designer: IDesigner);
  59. begin
  60. TEditProperty.Create(Component, Prop, Designer).Free;
  61. end;
  62. { TPngImageListEditor }
  63. procedure TPngImageListEditor.Edit;
  64. var
  65. Component: TPngImageList;
  66. begin
  67. Component := GetComponent as TPngImageList;
  68. EditProperty(Component, 'PngImages', Designer); // do not localize
  69. end;
  70. procedure TPngImageListEditor.ExecuteVerb(Index: Integer);
  71. begin
  72. case Index of
  73. 0: Edit;
  74. 1: begin
  75. TPngImageListAccess(GetComponent).CopyPngs;
  76. MessageBox(0, PChar(SPNGObjectsHaveBeenCopied),
  77. PChar(string(GetComponent.ClassName)), MB_ICONINFORMATION or MB_OK);
  78. end;
  79. end;
  80. end;
  81. function TPngImageListEditor.GetVerb(Index: Integer): string;
  82. begin
  83. case Index of
  84. 0: Result := SEditImages;
  85. 1: Result := SRecreateImages;
  86. end;
  87. end;
  88. function TPngImageListEditor.GetVerbCount: Integer;
  89. begin
  90. Result := 2;
  91. end;
  92. { TPngImageCollectionEditor }
  93. procedure TPngImageCollectionEditor.Edit;
  94. var
  95. Component: TPngImageCollection;
  96. begin
  97. Component := GetComponent as TPngImageCollection;
  98. EditProperty(Component, 'Items', Designer); // do not localize
  99. end;
  100. procedure TPngImageCollectionEditor.ExecuteVerb(Index: Integer);
  101. begin
  102. Edit;
  103. end;
  104. function TPngImageCollectionEditor.GetVerb(Index: Integer): string;
  105. begin
  106. Result := SEditImages;
  107. end;
  108. function TPngImageCollectionEditor.GetVerbCount: Integer;
  109. begin
  110. Result := 1;
  111. end;
  112. procedure TPngButtonEditor.Edit;
  113. begin
  114. EditProperty(GetComponent, 'OnClick', Designer); // do not localize
  115. end;
  116. { TPngButtonEditor }
  117. procedure TPngButtonEditor.ExecuteVerb(Index: Integer);
  118. begin
  119. EditProperty(GetComponent, 'PngImage', Designer); // do not localize
  120. end;
  121. function TPngButtonEditor.GetVerb(Index: Integer): string;
  122. begin
  123. Result := SEditImage;
  124. end;
  125. function TPngButtonEditor.GetVerbCount: Integer;
  126. begin
  127. Result := 1;
  128. end;
  129. { TEditProperty }
  130. constructor TEditProperty.Create(Component: TComponent; const Prop: string; Designer: IDesigner);
  131. var
  132. Components: IDesignerSelections;
  133. begin
  134. inherited Create;
  135. FPropery := Prop;
  136. Components := TDesignerSelections.Create;
  137. Components.Add(Component);
  138. GetComponentProperties(Components, tkAny, Designer, EnumProperty);
  139. end;
  140. procedure TEditProperty.EnumProperty(const Prop: IProperty);
  141. begin
  142. if Prop.GetName = FPropery then
  143. Prop.Edit;
  144. end;
  145. { TPngImageListImagesEditor }
  146. procedure TPngImageListImagesEditor.Edit;
  147. var
  148. ImageList: TPngImageList;
  149. dlg: TPngImageListEditorDlg;
  150. begin
  151. dlg := TPngImageListEditorDlg.Create(nil);
  152. try
  153. ImageList := GetComponent(0) as TPngImageList;
  154. dlg.Caption := Format(SEditing, [ImageList.Name, GetName]);
  155. dlg.Images.Items.Assign(ImageList.PngImages);
  156. dlg.ImageWidth := ImageList.Width;
  157. dlg.ImageHeight := ImageList.Height;
  158. if dlg.ShowModal = mrOK then begin
  159. ImageList.Width := dlg.ImageWidth;
  160. ImageList.Height := dlg.ImageHeight;
  161. ImageList.PngImages.Assign(dlg.Images.Items);
  162. Designer.Modified;
  163. end;
  164. finally
  165. dlg.Free;
  166. end;
  167. end;
  168. function TPngImageListImagesEditor.GetAttributes: TPropertyAttributes;
  169. begin
  170. Result := inherited GetAttributes + [paDialog, paReadOnly];
  171. end;
  172. function TPngImageListImagesEditor.GetValue: string;
  173. begin
  174. Result := '(PNG images)'; // do not localize
  175. end;
  176. { TPngImageCollectionItemsEditor }
  177. procedure TPngImageCollectionItemsEditor.Edit;
  178. var
  179. Collection: TPngImageCollection;
  180. dlg: TPngImageListEditorDlg;
  181. begin
  182. Collection := GetComponent(0) as TPngImageCollection;
  183. dlg := TPngImageListEditorDlg.Create(nil);
  184. try
  185. dlg.Caption := Format(SEditing, [Collection.Name, GetName]);
  186. dlg.Images.Items.Assign(Collection.Items);
  187. if dlg.ShowModal = mrOK then begin
  188. Collection.Items.Assign(dlg.Images.Items);
  189. Designer.Modified;
  190. end;
  191. finally
  192. dlg.Free;
  193. end;
  194. end;
  195. end.