How to hook a method to the Edit event in Delphi 7 IDE?
Asked Answered
C

2

35

I'd like to automatically checkout a file when I start to edit it in Delphi 7 IDE.

ClearCase is my version control system and I really hate the need to checkout a file before starting to edit. It always breaks my thought flow: I'm trying to solve a problem, find where I need to change, try to edit it, fail because the file is read only, open clearcase, search the file, finally checkout, try to edit the file again, fail because the IDE still thinks it is readonly, tell the IDE that isn't readonly. When I finally go back to code, I forgot what I was trying do do.

I've found this nice and simple ClearCase IDE integration code. It works, but uses the deprecated ToolIntf unit. I've added a couple of shortcuts. Here is a simplified version of it (didn't try to compile):

unit clearcase;

interface
uses ToolsApi, ToolIntf;

implementation
uses
  Windows, Dialogs, Classes, ExptIntf, Menus, ShellApi, SysUtils;

type
  TDelphiClearcase = class
  private
    FClearcaseMenu,
    FDoCheckOutPasDfm,
    FDoCheckInPasDfm : TIMenuItemIntf;

    procedure ExecCommand(const command: string; path: PChar = nil);
  public
    destructor Destroy;override;
    procedure DoClick(Sender: TIMenuItemIntf);
    property ClearcaseMenu: TIMenuItemIntf read FClearcaseMenu write FClearcaseMenu;
    property DoCheckOutPasDfm:TIMenuItemIntf write FDoCheckOutPasDfm;
    property DoCheckInPasDfm: TIMenuItemIntf write FDoCheckInPasDfm;
  end;

var
  dcc: TDelphiClearcase = nil;

{ TDelphiClearcase }

destructor TDelphiClearcase.Destroy;
  procedure Remove(item: TIMenuItemIntf);
  begin
    if( item = nil )then
      Exit;
    item.DestroyMenuItem;
    FreeAndNil(item);
  end;
begin
  Remove(FDoCheckOutPasDfm);
  Remove(FDoCheckInPasDfm);
  inherited;
end;

procedure TDelphiClearcase.DoClick(Sender: TIMenuItemIntf);
  function GetPasDfm(const f: string): string;
  var
    aux: string;
  begin
    aux := Copy(f, 1, Length(f) - 4);
    Result := aux + '.pas' + ' ' + aux + '.dfm'
  end;

var
  command, fileName  : string;
begin
  fileName := ToolServices.GetCurrentFile;

  if( Sender = FDoCheckOutPasDfm )then
    command := 'cleartool co ' + GetPasDfm(fileName)
  else if( Sender = FDoCheckInPasDfm )then
    command := 'cleartool ci ' + GetPasDfm(fileName);

  ExecCommand(command);

  ToolServices.ReloadFile(fileName);

end;

procedure TDelphiClearcase.ExecCommand(const command: string; path: PChar);
var
  pi  : TProcessInformation;
  stinfo : TStartupInfo;
begin
  FillChar(stinfo, SizeOf(stinfo), 0);
  stinfo.cb := SizeOf(stinfo);

  if( CreateProcess(nil, PChar(command), nil, nil, True, CREATE_NEW_CONSOLE,
      nil, path, stinfo, pi) )then begin
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(pi.hProcess)
  end
end;

procedure CreateMenus;
var
  services: TIToolServices;
begin
  if( BorlandIDEServices = nil )then
    Exit;
  services := ToolServices;

  if( services = nil )then
    Exit;

  dcc := TDelphiClearcase.Create;

  dcc.ClearcaseMenu := services.GetMainMenu.GetMenuItems.InsertItem(6,
    'C&learcase', 'ClearcaseMenu', 'ClearcaseTools', 0, 0, 0,
    [mfEnabled, mfVisible], nil);


  dcc.DoCheckOutPasDfm := dcc.ClearcaseMenu.InsertItem(2,
    'Check Out pas and dfm', 'DoCheckOutPasDfm', 'Undo the check outs', ShortCut(Ord('>'),
    [ssCtrl]), 0, 2,
    [mfEnabled, mfVisible], dcc.DoClick);
  dcc.DoCheckInPasDfm:= dcc.ClearcaseMenu.InsertItem(4,
    'Check In pas and dfm', 'DoCheckInPasDfm', 'Check in current files', ShortCut(Ord('<'),
    [ssCtrl]), 0, 2,
    [mfEnabled, mfVisible], dcc.DoClick);

end;

procedure DestroyMenus;
begin
  FreeAndNil(dcc);
end;

initialization
  CreateMenus;

finalization
  DestroyMenus
end.

I'd like to checkout the file when I first start editing it and it is read only. I have no idea how to hook a function to the IDE edit event of a file. Any pointers are welcome.

Cressida answered 12/7, 2011 at 20:23 Comment(8)
One possible workaround might be to have your projects managed in git, make your changes locally and push your changes at the end of the day into your Clearcase repository. No need to checkout files during the day and no need to change readonly flags. You could take a look at following question for more relevant information #2342631Poppycock
Holy sh*t, just found this question: #909442Cressida
First of all you shouldn't be using a version control system that forces you to check files out, simply because there are alternatives. Try Suversion, Git, Mercurial. If you really are stuck with Clearcase, you really don't want to accidentally check files out, so you shouldn't try to check them out automatically.Sweat
@Cosmin: I agree with you that you shouldn't try to check stuff out automatically (I actually prefer vcs's that force you to check things out). That's why we have created a "simple" menu expert. Checking out a file is now a matter of a keyboard short-cut to the menu item. The expert recognizes when it is in a dpr or a pas file with a dfm and it will check out both dfm and pas, or both dpr and dproj as applicable. @neves: The shortcut key is engrained in my muscle memory so checking out a file no longer interrupts my flow of thought.Quinnquinol
@Cosmin: Other vc systems are out of question :-(Cressida
@Marjan: We are using UCM and each developer will work in his own stream. Checkout is just a nuisance.Cressida
This probably doesn't apply to Delphi 7, but may still be interesting: sourceforge.net/projects/radstudioverins It's the Version Insight project for Delphi XE.Azerbaijan
I have to use ClearCase. We use it only for integration; for the day-to-day work we use Mercurial.Cockloft
D
1

Aternative to writing API or the like is to simply use snapshot views and automatically write files using "Highjack" functionality ...then just check'em in later.

Diagram answered 30/8, 2011 at 4:6 Comment(1)
This isn't the answer to what I asked, but you are right. Hijacked files are the only reasonable way to work with ClearCase.Cressida
H
0

Alternatively you can use the open ToolsAPI to listen for changes in the editor and checkout the file when the user has changed any of the content in the file.

Hereld answered 27/12, 2011 at 13:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.