0

The OpenTools API defines an interface for being notified before and after a compile:

IOTAIDENotifier = interface(IOTANotifier)
  ['{E052204F-ECE9-11D1-AB19-00C04FB16FB3}']
  procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);  // This procedure is called for many various file operations within the IDE
  procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload; // This function is called immediatately before the compiler is invoked. Set Cancel to True to cancel the compile
  procedure AfterCompile(Succeeded: Boolean); overload; // This procedure is called immediately following a compile.  Succeeded will be true if the compile was successful
end;

i've created a wizard that exports this interface:

TDBGExportWizard = class(TNotifierObject, IOTANotifier, IOTAIDENotifier, IOTAIDENotifier50, IOTAWizard)
public
   { IOTANotifier }
// procedure AfterSave; //This procedure is called immediately after the item is successfully saved. This is not called for IOTAWizards
// procedure BeforeSave; //This function is called immediately before the item is saved. This is not called for IOTAWizard
// procedure Destroyed; // The associated item is being destroyed so all references should be dropped. Exceptions are ignored.
// procedure Modified; // This associated item was modified in some way. This is not called for IOTAWizards

   { IOTAIDENotifier }
   procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
   procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
   procedure AfterCompile(Succeeded: Boolean); overload;

   { IOTAIDENotifier50 }
   procedure BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean); overload;
   procedure AfterCompile(Succeeded: Boolean; IsCodeInsight: Boolean); overload;

   { IOTAWizard }
   function GetIDString: string;
   function GetName: string;
   function GetState: TWizardState;
   procedure Execute;
end;

and register the wizard with:

procedure Register;
begin
   RegisterPackageWizard(TDBGExportWizard.Create);
end;

But neither:

  • IOTAIDENotifier.BeforeCompile
  • IOTAIDENotifier.AfterCompile
  • IOTAIDENotifier.FileNotification
  • IOTAIDENotifier50.BeforeCompile
  • IOTAIDENotifier50.AfterCompile

are called. What am i doing wrong?


The only thing ever called is

  • IOTAWizard.GetName
  • IOTAWizard.GetIDString

In that order.

Ian Boyd
  • 246,734
  • 253
  • 869
  • 1,219

1 Answers1

4

The following Is from a small piece of our software (I clipped out some code but it should work)

unit UnitSymbolInsert;

interface

procedure Register;

implementation

uses
  SysUtils, ToolsApi, Classes, Types, ActiveX, UnitSymbols;

type
  TCITNotifier = class(TNotifierObject, IOTANotifier, IOTAIDENotifier, IOTAIDENotifier50 )
  protected
    // IOTAIDENotifier
    procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
    procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
    procedure AfterCompile(Succeeded: Boolean); overload;
    // IOTAIDENotifier50
    procedure BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean); overload;
    procedure AfterCompile(Succeeded: Boolean; IsCodeInsight: Boolean); overload;
  end;

var
  NotifierIndex: Integer = -1;

procedure Register;
var
  Services: IOTAServices;
begin
  if not Supports(BorlandIDEServices, IOTAServices, Services) then Exit;
  NotifierIndex := Services.AddNotifier(TCITNotifier.Create);
end;

procedure UnRegister;
var
  Services: IOTAServices;
begin
  if (NotifierIndex < 0) or not Supports(BorlandIDEServices, IOTAServices, Services) then Exit;
  Services.RemoveNotifier(NotifierIndex);
end;

{ TCITNotifier }

procedure TCITNotifier.AfterCompile(Succeeded, IsCodeInsight: Boolean);
begin
  // Only when we have a succesfully build for a project.
  if not Succeeded or IsCodeInsight then Exit;

  // do something useful here!!!!
end;

procedure TCITNotifier.BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean);
begin
  // Not used
end;

procedure TCITNotifier.AfterCompile(Succeeded: Boolean);
begin
  // Not used
end;

procedure TCITNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
  // Not used
end;

procedure TCITNotifier.FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
begin
  // Not used
end;

initialization
  ;
finalization
  UnRegister;
end.

Hopefully I did not clip away too much. Build into a dpk and install it into the IDE.

Ritsaert Hornstra
  • 5,013
  • 1
  • 33
  • 51
  • 1
    Looks impressive, but in what way is this different from the class the OP posted? – GolezTrol Oct 31 '11 at 15:57
  • 1
    @GolezTrol Indeed they look the same. The difference is in the registration part. OP registers the class as a separate Wizard, but Ritsaert adds the class to the current IDE notifiers. The latter works. – NGLN Oct 31 '11 at 16:21
  • @GolezTrol the difference is that this is the way it's supposed to work. If you don't register your notifier the callback is never called. – Ondrej Kelle Oct 31 '11 at 16:33
  • [This one](http://cc.embarcadero.com/Item/19823) should still be working (as an example, at least). – Ondrej Kelle Oct 31 '11 at 17:07
  • i think you snipped a little bit too much. With the removal of `IOTAWizard` i now get nothing. Nevermind, i do get notifications. They're just being masked by Delphi crashing now. i think there's an incompatibility with DDevExtensions/DelphiSpeedUp – Ian Boyd Oct 31 '11 at 17:08
  • But asking special global variable for a certain interface during a specially named procedure was the magic ingredient. – Ian Boyd Oct 31 '11 at 17:44
  • @Ian: Glad you got it working. Perhaps you can repeat the solution in your own code above; note that I never added IOTAWizard in my code and it works fine that way (I do not register as a wizard). Perhaps you can opy your soulution to the question section for others to read/comment upon. – Ritsaert Hornstra Nov 01 '11 at 06:58
  • No need, Ritsaert - your code works perfectly. i didn't actually need to export `IOTAwizard`, since i'm not actually a wizard. i just just following every example i could find. And, honestly, i don't even know what a "wizard" adds (GetName, name of what. GetIDString, id string of what? GetState, what is my state, etc). And while i may expand the addin with a dialog box accessible from a menu item - my question was for how to receive notifications. Your code solves that exactly - right down to the calling `UnRegister` from `finalization` section, only if the register succeeded. Accepted and +1! – Ian Boyd Nov 01 '11 at 11:31