CnASHostServices.pas 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. {******************************************************************************}
  2. { CnPack For Delphi/C++Builder }
  3. { 中国人自己的开放源码第三方开发包 }
  4. { (C)Copyright 2001-2018 CnPack 开发组 }
  5. { ------------------------------------ }
  6. { }
  7. { 本开发包是开源的自由软件,您可以遵照 CnPack 的发布协议来修 }
  8. { 改和重新发布这一程序。 }
  9. { }
  10. { 发布这一开发包的目的是希望它有用,但没有任何担保。甚至没有 }
  11. { 适合特定目的而隐含的担保。更详细的情况请参阅 CnPack 发布协议。 }
  12. { }
  13. { 您应该已经和开发包一起收到一份 CnPack 发布协议的副本。如果 }
  14. { 还没有,可访问我们的网站: }
  15. { }
  16. { 网站地址:http://www.cnpack.org }
  17. { 电子邮件:master@cnpack.org }
  18. { }
  19. {******************************************************************************}
  20. unit CnASHostServices;
  21. {* |<PRE>
  22. ================================================================================
  23. * 软件名称:不可视工具组件包
  24. * 单元名称:ActiveScript Host 服务单元
  25. * 单元作者:周劲羽 (zjy@cnpack.org)
  26. * 备 注:该单元定义了供 ActiveScript 使用的 Host 公共服务
  27. * 开发平台:PWin2K SP3 + Delphi 7
  28. * 兼容测试:PWin9X/2000/XP + Delphi 6/7 C++Builder 6
  29. * 本 地 化:该单元中的字符串均符合本地化处理方式
  30. * 单元标识:$Id$
  31. * 修改记录:2003.10.31
  32. * 创建单元
  33. ================================================================================
  34. |</PRE>}
  35. interface
  36. {$I CnPack.inc}
  37. {$IFNDEF COMPILER6_UP}
  38. 'Error: This unit can used only for Delphi / C++Builder 6 or up.'
  39. {$ENDIF COMPILER6_UP}
  40. uses
  41. Windows, SysUtils, Classes, TypInfo, ComObj, CnASIDispatchProxy, CnCommon;
  42. type
  43. //==============================================================================
  44. // ActiveScript 公共服务基础类
  45. //==============================================================================
  46. { ICnASService }
  47. ICnASService = interface(IActiveScriptInvokable)
  48. ['{5640170F-9C03-400E-9E46-549A80F3ABDD}']
  49. end;
  50. { TCnASService }
  51. TCnASService = class(TInterfacedObject, IActiveScriptInvokable, ICnASService)
  52. {* ActiveScript 公共服务基础类,用来提供服务给脚本使用,
  53. 脚本可调用 HostServices.CreateObject('ServiceName', CreateParam) 来使用。}
  54. public
  55. constructor Create(const CreateParam: OleVariant); virtual;
  56. {* 类构造器,CreateParam 为脚本传入的参数}
  57. end;
  58. TCnASServiceClass = class of TCnASService;
  59. //==============================================================================
  60. // ActiveScript Host 服务类
  61. //==============================================================================
  62. { ICnASHostServices }
  63. ICnASHostServices = interface(IActiveScriptInvokable)
  64. ['{E5B6A915-69AE-4CB2-BFBE-9411B75F8E49}']
  65. // 对象服务
  66. function CreateObject(const ServiceName: string; CreateParam: OleVariant): IUnknown; stdcall;
  67. {* 构造并返回一个指定标识名称的对象接口,ServiceName 为对象名称,
  68. CreateParam 为相关的参数}
  69. // 对话框服务
  70. function MessageBox(const Text, Caption: string; Flags: Integer): Integer; stdcall;
  71. {* 消息对话框}
  72. procedure InfoDlg(const Text: string; const Caption: string = ''); stdcall;
  73. {* 显示提示窗口}
  74. function InfoOk(Text: string; Caption: string = ''): Boolean; stdcall;
  75. {* 显示提示确认窗口}
  76. procedure ErrorDlg(Text: string; Caption: string = ''); stdcall;
  77. {* 显示错误窗口}
  78. procedure WarningDlg(Text: string; Caption: string = ''); stdcall;
  79. {* 显示警告窗口}
  80. function QueryDlg(Text: string; DefaultNo: Boolean = False;
  81. Caption: string = ''): Boolean; stdcall;
  82. {* 显示询问对话框}
  83. function InputQuery(const Caption, Prompt: string): string; stdcall;
  84. {* 显示一个输入窗口,如果取消,返回空字符串}
  85. { TODO : 扩充其它 Host 公共服务功能。 }
  86. end;
  87. function GetCnASHostServices: IDispatch;
  88. {* 返回一个支持 ICnASHostServices 和 IDispatch 的接口。
  89. 可供 ActiveScriptSite.AddNamedItem 使用。}
  90. //==============================================================================
  91. // Host 服务类列表相关过程
  92. //==============================================================================
  93. procedure RegisterCnASService(const ServiceName: string;
  94. const AClass: TCnASServiceClass; IntfTypeInfo: PTypeInfo);
  95. {* 注册一个 TCnASService 服务类引用,每个服务类实现应在该单元的 initialization
  96. 节调用该过程注册相关服务类 }
  97. implementation
  98. //==============================================================================
  99. // Host 服务类列表相关过程
  100. //==============================================================================
  101. var
  102. CnASServiceClassList: TStrings = nil; // Host 服务类引用列表
  103. CnASServiceIntfTypeInfoList: TList = nil; // 实现的接口信息列表
  104. // 注册一个 TCnASServiceClass 服务类引用
  105. procedure RegisterCnASService(const ServiceName: string;
  106. const AClass: TCnASServiceClass; IntfTypeInfo: PTypeInfo);
  107. begin
  108. if CnASServiceClassList.IndexOf(ServiceName) < 0 then
  109. begin
  110. CnASServiceClassList.AddObject(ServiceName, TObject(AClass));
  111. CnASServiceIntfTypeInfoList.Add(IntfTypeInfo);
  112. end;
  113. end;
  114. // 根据服务类名取指定的服务类引用
  115. function GetCnASServiceClass(const ServiceName: string; var IntfTypeInfo:
  116. PTypeInfo): TCnASServiceClass;
  117. var
  118. Idx: Integer;
  119. begin
  120. Idx := CnASServiceClassList.IndexOf(ServiceName);
  121. if Idx >= 0 then
  122. begin
  123. Result := TCnASServiceClass(CnASServiceClassList.Objects[Idx]);
  124. IntfTypeInfo := CnASServiceIntfTypeInfoList[Idx];
  125. end
  126. else
  127. begin
  128. Result := nil;
  129. IntfTypeInfo := nil;
  130. end;
  131. end;
  132. //==============================================================================
  133. // ActiveScript 公共服务基础类
  134. //==============================================================================
  135. { TCnASService }
  136. constructor TCnASService.Create(const CreateParam: OleVariant);
  137. begin
  138. inherited Create;
  139. end;
  140. //==============================================================================
  141. // ActiveScript Host 服务类
  142. //==============================================================================
  143. { ICnASHostServices }
  144. type
  145. TCnASHostServices = class(TInterfacedObject, ICnASHostServices)
  146. public
  147. // 对象服务
  148. function CreateObject(const ServiceName: string; CreateParam: OleVariant): IUnknown; stdcall;
  149. // 对话框服务
  150. function MessageBox(const Text, Caption: string; Flags: Integer): Integer; stdcall;
  151. procedure InfoDlg(const Text: string; const Caption: string = ''); stdcall;
  152. function InfoOk(Text: string; Caption: string = ''): Boolean; stdcall;
  153. procedure ErrorDlg(Text: string; Caption: string = ''); stdcall;
  154. procedure WarningDlg(Text: string; Caption: string = ''); stdcall;
  155. function QueryDlg(Text: string; DefaultNo: Boolean = False;
  156. Caption: string = ''): Boolean; stdcall;
  157. function InputQuery(const Caption, Prompt: string): string; stdcall;
  158. end;
  159. // 返回一个支持 ICnASHostServices 和 IDispatch 的接口。
  160. function GetCnASHostServices: IDispatch;
  161. begin
  162. Result := GetIDispatchProxy(TCnASHostServices.Create, TypeInfo(ICnASHostServices));
  163. end;
  164. { TCnASHostServices }
  165. function TCnASHostServices.CreateObject(const ServiceName: string;
  166. CreateParam: OleVariant): IUnknown;
  167. var
  168. IntfTypeInfo: PTypeInfo;
  169. ServiceClass: TCnASServiceClass;
  170. begin
  171. ServiceClass := GetCnASServiceClass(ServiceName, IntfTypeInfo);
  172. if Assigned(ServiceClass) then
  173. Result := GetIDispatchProxy(ServiceClass.Create(CreateParam), IntfTypeInfo)
  174. else
  175. Result := nil;
  176. end;
  177. procedure TCnASHostServices.ErrorDlg(Text, Caption: string);
  178. begin
  179. CnCommon.ErrorDlg(Text, Caption);
  180. end;
  181. procedure TCnASHostServices.InfoDlg(const Text, Caption: string);
  182. begin
  183. CnCommon.InfoDlg(Text, Caption);
  184. end;
  185. function TCnASHostServices.InfoOk(Text, Caption: string): Boolean;
  186. begin
  187. Result := CnCommon.InfoOk(Text, Caption);
  188. end;
  189. function TCnASHostServices.InputQuery(const Caption,
  190. Prompt: string): string;
  191. begin
  192. CnCommon.CnInputQuery(Caption, Prompt, Result);
  193. end;
  194. function TCnASHostServices.MessageBox(const Text, Caption: string;
  195. Flags: Integer): Integer;
  196. begin
  197. Result := Windows.MessageBox(0, PChar(Text), PChar(Caption), Flags);
  198. end;
  199. function TCnASHostServices.QueryDlg(Text: string; DefaultNo: Boolean;
  200. Caption: string): Boolean;
  201. begin
  202. Result := CnCommon.QueryDlg(Text, DefaultNo, Caption);
  203. end;
  204. procedure TCnASHostServices.WarningDlg(Text, Caption: string);
  205. begin
  206. CnCommon.WarningDlg(Text, Caption);
  207. end;
  208. initialization
  209. CnASServiceClassList := TStringList.Create;
  210. CnASServiceIntfTypeInfoList := TList.Create;
  211. finalization
  212. FreeAndNil(CnASServiceClassList);
  213. FreeAndNil(CnASServiceIntfTypeInfoList);
  214. end.