PageRenderTime 15ms CodeModel.GetById 11ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/components/codetools/examples/addeventmethod.lpr

http://github.com/graemeg/lazarus
Unknown | 57 lines | 49 code | 8 blank | 0 comment | 0 complexity | aeb64e60fbac06a0ce128f488778402e MD5 | raw file
 1{
 2 ***************************************************************************
 3 *                                                                         *
 4 *   This source is free software; you can redistribute it and/or modify   *
 5 *   it under the terms of the GNU General Public License as published by  *
 6 *   the Free Software Foundation; either version 2 of the License, or     *
 7 *   (at your option) any later version.                                   *
 8 *                                                                         *
 9 *   This code is distributed in the hope that it will be useful, but      *
10 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
11 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
12 *   General Public License for more details.                              *
13 *                                                                         *
14 *   A copy of the GNU General Public License is available on the World    *
15 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
16 *   obtain it by writing to the Free Software Foundation,                 *
17 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
18 *                                                                         *
19 ***************************************************************************
20
21  Author: Mattias Gaertner
22
23  Abstract:
24    Demonstrating how to add a method to a class and extending the uses section.
25}
26program AddEventMethod;
27
28{$mode objfpc}{$H+}
29
30uses
31  Classes, SysUtils, CodeCache, CodeToolManager;
32  
33const
34  ConfigFilename = 'codetools.config';
35var
36  Filename: string;
37  Code: TCodeBuffer;
38begin
39  CodeToolBoss.SimpleInit(ConfigFilename);
40
41  // load the file
42  Filename:=ExpandFileName('scanexamples/addeventexample.pas');
43  Code:=CodeToolBoss.LoadFile(Filename,false,false);
44  if Code=nil then
45    raise Exception.Create('loading failed '+Filename);
46
47  // Example 1: add a method compatible to TNotifyEvent
48  if CodeToolBoss.CreatePublishedMethod(Code,'TForm1','NewMethod',
49    typeinfo(TNotifyEvent),false,'Classes') then
50  begin
51    writeln('Method added: ');
52    writeln(Code.Source);
53  end else begin
54    raise Exception.Create('Adding method failed');
55  end;
56end.
57