home *** CD-ROM | disk | FTP | other *** search
/ Delphi Anthology / aDELPHI.iso / Runimage / Delphi50 / Lib / ddereg.pas < prev    next >
Pascal/Delphi Source File  |  1999-08-11  |  9KB  |  347 lines

  1. unit DDEReg;
  2.  
  3. interface
  4.  
  5. uses Windows, SysUtils, Classes, Graphics, Forms, Controls, Buttons, TypInfo,
  6.   DsgnIntf, DdeMan, Messages, StdCtrls, ExtCtrls;
  7.  
  8. {  TComponentEditors }
  9.  
  10. type
  11.   TSrvrConvEdit = class(TDefaultEditor)
  12.   protected
  13.     procedure EditProperty(PropertyEditor: TPropertyEditor;
  14.       var Continue, FreeEditor: Boolean); override;
  15.   end;
  16.  
  17.   TCliConvEdit = class(TDefaultEditor)
  18.   protected
  19.     procedure EditProperty(PropertyEditor: TPropertyEditor;
  20.       var Continue, FreeEditor: Boolean); override;
  21.   end;
  22.  
  23.   TSrvrItemEdit = class(TDefaultEditor)
  24.   public
  25.     procedure Copy; override;
  26.   end;
  27.  
  28.  
  29. {  TForms }
  30. type
  31.   TDdeLinkDlg = class(TForm)
  32.     TopicEdit: TEdit;
  33.     AppEdit: TEdit;
  34.     Label1: TLabel;
  35.     Label2: TLabel;
  36.     OK: TButton;
  37.     CancelBtn: TButton;
  38.     PasteBtn: TButton;
  39.     HelpBtn: TButton;
  40.     procedure doPasteLink(Sender: TObject);
  41.     procedure DoPasteCheck(Sender: TObject);
  42.     procedure FormCreate(Sender: TObject);
  43.     procedure HelpBtnClick(Sender: TObject);
  44.   private
  45.     procedure WMActivate(var Message: TWMActivate); message WM_ACTIVATE;
  46.   end;
  47.  
  48.  
  49.   TDdeLinkInfoProperty = class(TPropertyEditor)
  50.   public
  51.     function GetAttributes: TPropertyAttributes; override;
  52.     procedure Edit; override;
  53.     function  GetValue: string; override;
  54.   end;
  55.  
  56.   TDdeClientItemProperty = class(TStringProperty)
  57.   public
  58.     function GetAttributes: TPropertyAttributes; override;
  59.     procedure GetValues(Proc: TGetStrProc); override;
  60.   end;
  61.  
  62. procedure Register;
  63.  
  64. implementation
  65.  
  66. uses Dialogs, LibHelp, ExptIntf, ToolIntf, EditIntf, Dsnconst;
  67.  
  68. {$R *.DFM}
  69. {---------------------------------------}
  70.  
  71. procedure TSrvrConvEdit.EditProperty(PropertyEditor: TPropertyEditor;
  72.   var Continue, FreeEditor: Boolean);
  73. var
  74.   PropName: string;
  75. begin
  76.   PropName := PropertyEditor.GetName;
  77.   if (CompareText(PropName, 'ONEXECUTEMACRO') = 0) then
  78.   begin
  79.     PropertyEditor.Edit;
  80.     Continue := False;
  81.   end
  82.   else
  83.     inherited EditProperty(PropertyEditor, Continue, FreeEditor);
  84. end;
  85.  
  86.  
  87. procedure TCliConvEdit.EditProperty(PropertyEditor: TPropertyEditor;
  88.   var Continue, FreeEditor: Boolean);
  89. var
  90.   PropName: string;
  91. begin
  92.   PropName := PropertyEditor.GetName;
  93.   if (CompareText(PropName, 'ONOPEN') = 0) then
  94.   begin
  95.     PropertyEditor.Edit;
  96.     Continue := False;
  97.   end
  98.   else
  99.     inherited EditProperty(PropertyEditor, Continue, FreeEditor);
  100. end;
  101.  
  102. procedure TSrvrItemEdit.Copy;
  103. begin
  104.   TDdeServerItem(Component).CopyToClipboard;
  105. end;
  106.  
  107.  
  108. {---------------------------------------}
  109. { TDdeLinkInfoProperty }
  110.  
  111. function TDdeLinkInfoProperty.GetAttributes: TPropertyAttributes;
  112. begin
  113.   Result := inherited GetAttributes + [paDialog, paReadOnly]
  114.                       - [paMultiSelect, paSubProperties];
  115. end;
  116.  
  117. function TDdeLinkInfoProperty.GetValue: string;
  118. begin
  119.   if GetStrValue = '' then Result := SDdeEmptyConnection
  120.   else Result := Format ('(%s)', [GetStrValue]);
  121. end;
  122.  
  123. procedure TDdeLinkInfoProperty.Edit;
  124. var
  125.   DdeCli      : TDdeClientConv;
  126.   LinkDlg     : TDdeLinkDlg;
  127. begin
  128.   DdeCli := TDdeClientConv (GetComponent(0));
  129.   LinkDlg := TDdeLinkDlg.Create(Application);
  130.   try
  131.     LinkDlg.AppEdit.Text    :=   DdeCli.DdeService;
  132.     LinkDlg.TopicEdit.Text  :=   DdeCli.DdeTopic;
  133.  
  134.     if LinkDlg.ShowModal = idOk then
  135.     begin
  136.       if Not DdeCli.SetLink(LinkDlg.AppEdit.Text,
  137.                             LinkDlg.TopicEdit.Text) then
  138.         MessageDlg(SDdeNoConnect, mtError, [mbOK], 0);
  139.       Modified;
  140.     end;
  141.   finally
  142.     LinkDlg.Free;
  143.   end;
  144. end;
  145.  
  146. {---------------------------------------}
  147.  
  148. procedure TDdeLinkDlg.doPasteLink(Sender: TObject);
  149. var
  150.   Service, Topic, Item : String;
  151. begin
  152.   if  GetPasteLinkInfo (Service, Topic, Item) = True  then
  153.   begin
  154.     AppEdit.Text   := Service;
  155.     TopicEdit.Text := Topic;
  156.   end
  157.   else
  158.   begin
  159.     AppEdit.Text   := EmptyStr;
  160.     TopicEdit.Text := EmptyStr;
  161.   end;
  162. end;
  163.  
  164. procedure TDdeLinkDlg.DoPasteCheck(Sender: TObject);
  165. var
  166.   enable : Boolean;
  167.   Service, Topic, Item : String;
  168. begin
  169.   enable := False;
  170.   if  GetPasteLinkInfo (Service, Topic, Item) = True  then
  171.     enable := True;
  172.   if enable <> PasteBtn.Enabled then
  173.     PasteBtn.Enabled := enable;
  174. end;
  175.  
  176. procedure TDdeLinkDlg.WMActivate(var Message: TWMActivate);
  177. begin
  178.   inherited;
  179.   if Message.Active <> WA_INACTIVE then DoPasteCheck(Self);
  180. end;
  181.  
  182. procedure TDdeLinkDlg.FormCreate(Sender: TObject);
  183. begin
  184.   HelpContext := hcDDDEInfo;
  185. end;
  186.  
  187. procedure TDdeLinkDlg.HelpBtnClick(Sender: TObject);
  188. begin
  189.   Application.HelpContext(HelpContext);
  190. end;
  191.  
  192. {-------}
  193. { TDdeClientItemProperty }
  194.  
  195. function TDdeClientItemProperty.GetAttributes: TPropertyAttributes;
  196. begin
  197.   Result := inherited GetAttributes + [paValueList] - [paMultiSelect,
  198.     paSubProperties];
  199. end;
  200.  
  201. procedure TDdeClientItemProperty.GetValues(Proc: TGetStrProc);
  202. var
  203.   Cli  : TDdeClientConv;
  204.   Service, Topic, Item : String;
  205. begin
  206.   Cli := TDdeClientConv(TDdeClientItem (GetComponent(0)).DdeConv);
  207.   if Cli <> Nil then
  208.   begin
  209.     if  GetPasteLinkInfo (Service, Topic, Item) = True  then
  210.     begin
  211.       if (Service = Cli.DdeService) and
  212.          (Topic = Cli.DdeTopic) then
  213.         Proc (Item);
  214.     end;
  215.   end;
  216. end;
  217.  
  218. type
  219.   TDDEProjectNotifier = class(TIAddInNotifier)
  220.     procedure FileNotification(NotifyCode: TFileNotification;
  221.       const FileName: string; var Cancel: Boolean); override;
  222.     procedure EventNotification(NotifyCode: TEventNotification;
  223.       var Cancel: Boolean); override;
  224.   end;
  225.  
  226.   TDDEModuleNotifier = class(TIModuleNotifier)
  227.   private
  228.     ModuleInterface: TIModuleInterface;
  229.   public
  230.     constructor Create(AModuleInterface: TIModuleInterface);
  231.     destructor Destroy; override;
  232.     procedure Notify(NotifyCode: TNotifyCode); override;
  233.     procedure ComponentRenamed(ComponentHandle: Pointer;
  234.       const OldName, NewName: string); override;
  235.   end;
  236.  
  237. var
  238.   ProjHooks: TList;
  239.   ProjHook: TDDEProjectNotifier;
  240.  
  241. procedure TDDEProjectNotifier.FileNotification;
  242. var
  243.   MI: TIModuleInterface;
  244.   Hook: TDDEModuleNotifier;
  245. begin
  246.   case NotifyCode of
  247.     fnProjectOpened:
  248.       begin
  249.         DDEMgr.AppName := ChangeFileExt(ExtractFileName(FileName),'');
  250.         MI := ToolServices.GetModuleInterface(FileName);
  251.         if MI <> nil then
  252.         begin
  253.           Hook := TDDEModuleNotifier.Create(MI);
  254.           ProjHooks.Add(Hook);
  255.           MI.AddNotifier(Hook);
  256.           MI.Release;
  257.         end;
  258.       end;
  259.     fnProjectClosing: DDEMgr.AppName := '';
  260.   end;
  261. end;
  262.  
  263. procedure TDDEProjectNotifier.EventNotification;
  264. begin
  265. end;
  266.  
  267. constructor TDDEModuleNotifier.Create(AModuleInterface: TIModuleInterface);
  268. begin
  269.   inherited Create;
  270.   ModuleInterface := AModuleInterface;
  271.   ModuleInterface.AddRef;
  272. end;
  273.  
  274. destructor TDDEModuleNotifier.Destroy;
  275. begin
  276.   ProjHooks.Remove(Self);
  277.   ModuleInterface.Release;
  278.   inherited Destroy;
  279. end;
  280.  
  281. procedure TDDEModuleNotifier.Notify(NotifyCode: TNotifyCode);
  282. var
  283.   EditInterface: TIEditorInterface;
  284. begin
  285.   case NotifyCode of
  286.     ncModuleDeleted:
  287.       begin
  288.         ModuleInterface.RemoveNotifier(Self);
  289.         Release;
  290.       end;
  291.     ncModuleRenamed:
  292.       begin
  293.         EditInterface := ModuleInterface.GetEditorInterface;
  294.         if EditInterface <> nil then
  295.         try
  296.           DDEMgr.AppName := ChangeFileExt(ExtractFileName(EditInterface.FileName),'');
  297.         finally
  298.           EditInterface.Free;
  299.         end;
  300.       end;
  301.   end;
  302. end;
  303.  
  304. procedure TDDEModuleNotifier.ComponentRenamed;
  305. begin
  306. end;
  307.  
  308. procedure Register;
  309. begin
  310.   RegisterComponents(srSystem, [TDdeClientConv, TDdeClientItem, TDdeServerConv,
  311.     TDdeServerItem]);
  312.   RegisterPropertyEditor(TypeInfo(string), TDdeClientConv, 'DdeService', TDdeLinkInfoProperty);
  313.   RegisterPropertyEditor(TypeInfo(string), TDdeClientConv, 'DdeTopic', TDdeLinkInfoProperty);
  314.   RegisterPropertyEditor(TypeInfo(string), TDdeClientItem, 'DdeItem', TDdeClientItemProperty);
  315.   RegisterComponentEditor(TDdeServerItem, TSrvrItemEdit);
  316.   RegisterComponentEditor(TDdeServerConv, TSrvrConvEdit);
  317.   RegisterComponentEditor(TDdeClientConv, TCliConvEdit);
  318.   RegisterPropertiesInCategory(TLocalizableCategory, TDdeServerItem, ['Lines']); { Text property displays Lines[0] }
  319.   RegisterPropertiesInCategory(TLocalizableCategory, TDdeClientItem, ['Lines']); { Text property displays Lines[0] }
  320.   ProjHook := TDDEProjectNotifier.Create;
  321.   ToolServices.AddNotifier(ProjHook);
  322. end;
  323.  
  324. procedure FreeProjFileHooks;
  325. var
  326.   I: Integer;
  327. begin
  328.   if (ToolServices = nil) or (ProjHooks = nil) then Exit;
  329.   I := ProjHooks.Count - 1;
  330.   while I > -1 do
  331.   begin
  332.     TDDEModuleNotifier(ProjHooks[I]).Notify(ncModuleDeleted);
  333.     Dec(I);
  334.   end;
  335.   ToolServices.RemoveNotifier(ProjHook);
  336.   ProjHook.Release;
  337.   ProjHook := nil;
  338. end;
  339.  
  340. initialization
  341.   ProjHooks := TList.Create;
  342. finalization
  343.   FreeProjFileHooks;
  344.   ProjHooks.Free;
  345.   ProjHooks := nil;
  346. end.
  347.