home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 September / Chip_2002-09_cd1.bin / zkuste / delphi / experti / d456 / BROMBSCT.ZIP / uCodeTemplateLib.pas < prev    next >
Pascal/Delphi Source File  |  2002-07-02  |  9KB  |  295 lines

  1. unit uCodeTemplateLib;
  2.  
  3. interface
  4. uses
  5.   Windows, Forms, Controls, Menus, Dialogs, Classes, ToolIntf, EditIntf,
  6.   ExptIntf, VirtIntf, SysUtils, Messages, Graphics, StdCtrls;
  7.  
  8. type
  9.   TCodeTemplateExpert = class(TIExpert)
  10.   private
  11.     menOptions: TIMenuItemIntf;
  12.     menEditSep: TIMenuItemIntf;
  13.     menEditSep2: TIMenuItemIntf;
  14.     menInsCode: TIMenuItemIntf;
  15.   public
  16.     constructor Create; virtual;
  17.     destructor Destroy; override;
  18.     function GetName: string; override;
  19.     function GetStyle: TExpertStyle; override;
  20.     function GetState: TExpertState; override;
  21.     function GetIDString: string; override;
  22.     procedure memOptionsClick(Sender: TIMenuItemIntf);
  23.     procedure memInsCodeClick(Senfer: TIMenuItemIntf);
  24.     procedure Execute; override;
  25.   end;
  26.  
  27. function InitExpert(ToolServices: TIToolServices; Registerproc: TExpertRegisterproc; var Terminate:
  28.   TExpertTerminateProc): Boolean; export; stdcall;
  29.  
  30. procedure Register;
  31.  
  32. var
  33.   CodeTemplateExpert: TCodeTemplateExpert = nil;
  34.  
  35.  
  36. implementation
  37. uses
  38.   uGlobCT, uMain, ULoadTemplate;
  39. const
  40.   LINEFEED = #10;
  41.   CRLF = #13#10;
  42.  
  43. procedure Register;
  44. begin
  45.   RegisterLibraryExpert(TCodeTemplateExpert.Create);
  46. end;
  47.  
  48.  
  49. { TCodeTemplateExpert }
  50.  
  51. constructor TCodeTemplateExpert.Create;
  52. var
  53.   menMain: TIMainMenuIntf;
  54.   menViewWinList: TIMenuItemIntf;
  55.   menViewMenu: TIMenuItemIntf;
  56. begin
  57.   strexpertpath := ExtractFileDir(Application.ExeName);
  58.   inherited Create;
  59.   menMain := ToolServices.GetMainMenu;
  60.   if Assigned(menMain) then
  61.   try
  62.     menViewWinList := menMain.FindMenuItem('ToolsOptionsItem');
  63.     if Assigned(menViewWinList) then
  64.     try
  65.       menViewMenu := menViewWinList.GetParent;
  66.       if Assigned(menViewMenu) then
  67.       try
  68.         menEditSep := menViewMenu.InsertItem(menViewWinList.GetIndex + 1, '-',
  69.           'EditExpertSeparator', '', 0, 0, 0, [mfVisible], nil);
  70.  
  71.         menOptions := menViewMenu.InsertItem(menEditSep.GetIndex + 1, '&Code template',
  72.           'EditExpertOptionsItem', '', 0, 0, 0, [mfEnabled, mfVisible], memOptionsClick);
  73.  
  74.         menInsCode := menViewMenu.InsertItem(menOptions.GetIndex + 1, 'Insert code', 'EditExpertInserCodeItem',
  75.           '', ShortCut(Word('H'), [ssCtrl]), 0, 0, [mfEnabled, mfVisible], memInsCodeClick);
  76.  
  77.         menEditSep2 := menViewMenu.InsertItem(menInsCode.GetIndex + 1, '-',
  78.           'EditExpertSeparator2', '', 0, 0, 0, [mfVisible], nil);
  79.       finally
  80.         menViewMenu.Free;
  81.       end;
  82.     finally
  83.       menViewWinList.Free;
  84.     end;
  85.   finally
  86.     menMain.Free;
  87.   end;
  88. end;
  89.  
  90. destructor TCodeTemplateExpert.Destroy;
  91. begin
  92.   menOptions.free;
  93.   menEditSep.free;
  94.   menEditSep2.free;
  95.   menInsCode.free;
  96.   inherited Destroy;
  97. end;
  98.  
  99. procedure TCodeTemplateExpert.Execute;
  100. begin
  101.  
  102. end;
  103.  
  104. function TCodeTemplateExpert.GetIDString: string;
  105. begin
  106.   Result := 'brombs.CodeTemplate';
  107. end;
  108.  
  109. function TCodeTemplateExpert.GetName: string;
  110. begin
  111.   Result := 'Code Template Expert';
  112. end;
  113.  
  114. function TCodeTemplateExpert.GetState: TExpertState;
  115. begin
  116.   Result := [esEnabled];
  117. end;
  118.  
  119. function TCodeTemplateExpert.GetStyle: TExpertStyle;
  120. begin
  121.   Result := esAddIn;
  122. end;
  123.  
  124. procedure TCodeTemplateExpert.memOptionsClick(Sender: TIMenuItemIntf);
  125. begin
  126.   with TfmMain.create(Application) do
  127.   begin
  128.     ShowModal;
  129.     Free;
  130.   end;
  131. end;
  132.  
  133. procedure TCodeTemplateExpert.memInsCodeClick(Senfer: TIMenuItemIntf);
  134. var
  135.   MoInt: TIModuleInterface; // module interface
  136.   EdInt: TIEditorInterface; // editor interface
  137.   EdView: TIEditView; // the editor view
  138.   EdWrite: TiEditWriter; // Edit Writer
  139.   EdRead: TIEditReader; // Edit Reader
  140.   Ep: TEditPos; // the editorposition
  141.   FName: string; // The File Name
  142.   Lf: array[0..1] of Char; // Local Buffer
  143.   EdPos: Integer; // Current Position in Editor Buffer
  144.   Eof: Boolean; // True if at end of Edit Buffer
  145.   FoundIt: Boolean; // True if Line Found
  146.   i: integer;
  147.  
  148.   function ReadEditorLine: string; // Read a return a line from the Editor
  149.   var
  150.     CharPos: Integer; // Current Position in the result string
  151.   begin
  152.     SetLength(Result, 1024); // Longest Line !!!!
  153.     CharPos := 1; // begin at the beginning
  154.     repeat
  155.       try
  156.               // Read a character
  157.         Eof := EdRead.GetText(EdPos,
  158.           @Result[CharPos], 1) <> 1;
  159.       except
  160.         begin
  161.           Eof := True; // Eof JIC
  162.           Result := ''; // No Result
  163.         end;
  164.       end;
  165.       Inc(EdPos);
  166.       if Result[CharPos] = LINEFEED then begin // was it a LineFeed and if so...
  167.         Result[CharPos] := #0; // Null the last char
  168.         SetLength(Result, CharPos - 1); // set the length
  169.         Exit; // bye bye
  170.       end;
  171.     until Eof; // repeat till end of file
  172.     Result := ''; // if we get here we are eof so ''
  173.   end;
  174.  
  175.   function GotoLine(LineNo: Integer): Boolean; // Goto line from file start
  176.   var
  177.     Lines: Integer; // Line count
  178.   begin
  179.     Result := False; // assume failure
  180.     if LineNo = 1 then
  181.       exit; // if first line we are there already
  182.     EdPos := 0; // zap editor pos
  183.     Lines := 1; // First line
  184.     while (not Result) do begin // while not done
  185.       try
  186.         Eof := EdRead.GetText(EdPos, Lf, 1) <> 1; // read a character
  187.       except
  188.         Eof := True; // assume EOF on exception
  189.       end;
  190.       Inc(EdPos); // Bump Editor position
  191.       if Lf[0] = LINEFEED then
  192.         Inc(Lines); // if EOL then bump count
  193.       if Lines = LineNo then begin // if this is the line we want
  194.         Eof := False; //  not EOF
  195.         Result := True; // got it
  196.       end;
  197.     end;
  198.   end;
  199.  
  200. begin
  201.   if Toolservices = nil then // No toolservices, no can do
  202.     exit;
  203.   Fname := Toolservices.GetCurrentFile; // Get the file name
  204.   if Pos('.DFM', UpperCase(FName)) > 0 then begin // if this is a DFM
  205.     ShowMessage('Pascal source must have focus'); // Tell em not to be stupid
  206.     Exit; // Bye Bye
  207.   end;
  208.  
  209.   if not Toolservices.IsFileOpen(FName) then begin // if file is not open
  210.     if not Toolservices.OpenFile(FName) then begin // try and open it
  211.       MessageDlg(Format('Can''t open %s', [FName]),
  212.         mtError, [mbOk], 0); // tell em we failed
  213.       Exit; // bye bye
  214.     end;
  215.   end;
  216.  
  217.   MoInt := Toolservices.GetModuleInterface(Fname); // get the module interface for the file
  218.   try
  219.     EdInt := MoInt.GetEditorInterface; // get the editor interface
  220.     try
  221.       EdView := EdInt.GetView(0); // get the view
  222.       try
  223.         Ep := EdView.CursorPos; // get the current cursor position
  224.       finally
  225.         EdView.Free; // free the view
  226.       end;
  227.  
  228.       EdRead := EdInt.CreateReader; // Create the Reader
  229.       try
  230.         FoundIt := GotoLine(Ep.Line); // jump to the required line
  231.       finally
  232.         EdRead.Free; // Free the buffer
  233.       end;
  234.       if not FoundIt then
  235.         exit; // bomb out if not found
  236.  
  237.       EdWrite := EdInt.CreateWriter; // get the vWriter
  238.       try
  239.         fmLoadTemplate := TfmLoadTemplate.Create(Application);
  240.         try
  241.           fmLoadTemplate.ShowModal;
  242.           with EdWrite do begin // with the writer
  243.             if fmLoadTemplate.ModalResult = mrOK then
  244.             begin
  245.               CopyTo(EdPos); // move down to the first char of the line
  246.               Insert(CRLF);
  247.               if fmLoadTemplate.Code.Count > 0 then
  248.                 for i := 0 to fmLoadTemplate.Code.Count - 1 do
  249.                   Insert(PChar(fmLoadTemplate.Code[i] + CRLF));
  250.               Insert(CRLF);
  251.             end;
  252.           end;
  253.         finally
  254.           fmLoadTemplate.Free;
  255.         end;
  256.       finally
  257.         EdWrite.Free; // free the writer
  258.       end;
  259.       EdView := EdInt.GetView(0); // get the view
  260.       try
  261.         Ep := EdView.CursorPos; // get the cursor position
  262.         Ep.Line := Ep.Line + 1; // We inserted line
  263.         EdView.CursorPos := Ep; // Set the cursor position
  264.       finally
  265.         EdView.Free; // free the view
  266.       end;
  267.     finally
  268.       EdInt.Free; // free the interface
  269.     end;
  270.   finally
  271.     MoInt.Free; // free the module
  272.   end;
  273. end;
  274.  
  275.  
  276. function InitExpert(
  277.   ToolServices: TIToolServices;
  278.   Registerproc: TExpertRegisterproc;
  279.   var Terminate: TExpertTerminateProc): Boolean; export; stdcall;
  280. begin
  281.   Result := ExptIntf.ToolServices = nil;
  282.   if not Result then
  283.     exit;
  284.   ExptIntf.ToolServices := ToolServices;
  285.   if Assigned(ToolServices) then
  286.     Application.Handle := ToolServices.GetParentHandle;
  287.   Terminate := nil;
  288.   CodeTemplateExpert := TCodeTemplateExpert.Create;
  289.   RegisterProc(CodeTemplateExpert);
  290. end;
  291.  
  292.  
  293. end.
  294.  
  295.