/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. Author: Mattias Gaertner
  21. Abstract:
  22. Demonstrating how to add a method to a class and extending the uses section.
  23. }
  24. program AddEventMethod;
  25. {$mode objfpc}{$H+}
  26. uses
  27. Classes, SysUtils, CodeCache, CodeToolManager;
  28. const
  29. ConfigFilename = 'codetools.config';
  30. var
  31. Filename: string;
  32. Code: TCodeBuffer;
  33. begin
  34. CodeToolBoss.SimpleInit(ConfigFilename);
  35. // load the file
  36. Filename:=ExpandFileName('scanexamples/addeventexample.pas');
  37. Code:=CodeToolBoss.LoadFile(Filename,false,false);
  38. if Code=nil then
  39. raise Exception.Create('loading failed '+Filename);
  40. // Example 1: add a method compatible to TNotifyEvent
  41. if CodeToolBoss.CreatePublishedMethod(Code,'TForm1','NewMethod',
  42. typeinfo(TNotifyEvent),false,'Classes') then
  43. begin
  44. writeln('Method added: ');
  45. writeln(Code.Source);
  46. end else begin
  47. raise Exception.Create('Adding method failed');
  48. end;
  49. end.