fptemplt.pas 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. {
  2. $Id$
  3. This file is part of the Free Pascal Integrated Development Environment
  4. Copyright (c) 1998 by Berczi Gabor
  5. Template support routines for the IDE
  6. See the file COPYING.FPC, included in this distribution,
  7. for details about the copyright.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. **********************************************************************}
  12. unit FPTemplt;
  13. interface
  14. uses FPViews;
  15. function GetTemplateCount: integer;
  16. function GetTemplateName(Index: integer): string;
  17. function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
  18. procedure InitTemplates;
  19. procedure DoneTemplates;
  20. implementation
  21. uses
  22. Dos,Objects,
  23. {$ifdef EDITORS}
  24. Editors,
  25. {$else}
  26. WEditor,
  27. {$endif}
  28. FPUtils;
  29. type
  30. PTemplate = ^TTemplate;
  31. TTemplate = record
  32. Name : PString;
  33. Path : PString;
  34. end;
  35. PTemplateCollection = ^TTemplateCollection;
  36. TTemplateCollection = object(TSortedCollection)
  37. function At(Index: Integer): PTemplate;
  38. procedure FreeItem(Item: Pointer); virtual;
  39. function Compare(Key1, Key2: Pointer): Sw_Integer; virtual;
  40. end;
  41. const Templates : PTemplateCollection = nil;
  42. function NewTemplate(const Name, Path: string): PTemplate;
  43. var P: PTemplate;
  44. begin
  45. New(P);
  46. FillChar(P^,SizeOf(P^),0);
  47. P^.Name:=NewStr(Name);
  48. P^.Path:=NewStr(Path);
  49. NewTemplate:=P;
  50. end;
  51. procedure DisposeTemplate(P: PTemplate);
  52. begin
  53. if assigned(P) then
  54. begin
  55. if assigned(P^.Name) then
  56. DisposeStr(P^.Name);
  57. if assigned(P^.Path) then
  58. DisposeStr(P^.Path);
  59. Dispose(P);
  60. end;
  61. end;
  62. function TTemplateCollection.At(Index: Integer): PTemplate;
  63. begin
  64. At:=inherited At(Index);
  65. end;
  66. procedure TTemplateCollection.FreeItem(Item: Pointer);
  67. begin
  68. if assigned(Item) then
  69. DisposeTemplate(Item);
  70. end;
  71. function TTemplateCollection.Compare(Key1, Key2: Pointer): Sw_Integer;
  72. var R: Sw_integer;
  73. K1: PTemplate absolute Key1;
  74. K2: PTemplate absolute Key2;
  75. begin
  76. if K1^.Name^<K2^.Name^ then R:=-1 else
  77. if K1^.Name^>K2^.Name^ then R:= 1 else
  78. R:=0;
  79. Compare:=R;
  80. end;
  81. function GetTemplateCount: integer;
  82. var Count: integer;
  83. begin
  84. if Templates=nil then Count:=0 else Count:=Templates^.Count;
  85. GetTemplateCount:=Count;
  86. end;
  87. function GetTemplateName(Index: integer): string;
  88. begin
  89. GetTemplateName:=Templates^.At(Index)^.Name^;
  90. end;
  91. function StartTemplate(Index: integer; Editor: PSourceEditor): boolean;
  92. var
  93. T: PTemplate;
  94. OK: boolean;
  95. E: PFileEditor;
  96. R: TRect;
  97. begin
  98. T:=Templates^.At(Index);
  99. R.Assign(0,0,0,0);
  100. New(E, Init(R,nil,nil,nil,T^.Path^));
  101. OK:=E<>nil;
  102. if OK then OK:=E^.LoadFile;
  103. if OK then
  104. begin
  105. E^.SelectAll(true);
  106. Editor^.InsertFrom(E);
  107. Editor^.SetCurPtr(0,0);
  108. Editor^.SelectAll(false);
  109. Dispose(E, Done);
  110. end;
  111. StartTemplate:=OK;
  112. end;
  113. {*****************************************************************************
  114. InitTemplates
  115. *****************************************************************************}
  116. procedure InitTemplates;
  117. procedure ScanDir(Dir: PathStr);
  118. var SR: SearchRec;
  119. S: string;
  120. begin
  121. if copy(Dir,length(Dir),1)<>DirSep then Dir:=Dir+DirSep;
  122. FindFirst(Dir+'*.pt',AnyFile,SR);
  123. while (DosError=0) do
  124. begin
  125. S:=NameOf(SR.Name);
  126. S:=LowerCaseStr(S);
  127. S[1]:=Upcase(S[1]);
  128. Templates^.Insert(NewTemplate(S,FExpand(Dir+SR.Name)));
  129. FindNext(SR);
  130. end;
  131. {$ifdef FPC}
  132. FindClose(SR);
  133. {$endif def FPC}
  134. end;
  135. begin
  136. New(Templates, Init(10,10));
  137. ScanDir('.');
  138. ScanDir(DirOf(ParamStr(0)));
  139. end;
  140. procedure DoneTemplates;
  141. begin
  142. if assigned(Templates) then
  143. begin
  144. Dispose(Templates, Done);
  145. Templates:=nil;
  146. end;
  147. end;
  148. END.
  149. {
  150. $Log$
  151. Revision 1.5 1999-02-18 13:44:35 peter
  152. * search fixed
  153. + backward search
  154. * help fixes
  155. * browser updates
  156. Revision 1.4 1999/02/16 17:13:56 pierre
  157. + findclose added for FPC
  158. Revision 1.3 1999/01/21 11:54:24 peter
  159. + tools menu
  160. + speedsearch in symbolbrowser
  161. * working run command
  162. Revision 1.2 1998/12/28 15:47:52 peter
  163. + Added user screen support, display & window
  164. + Implemented Editor,Mouse Options dialog
  165. + Added location of .INI and .CFG file
  166. + Option (INI) file managment implemented (see bottom of Options Menu)
  167. + Switches updated
  168. + Run program
  169. Revision 1.2 1998/12/22 10:39:51 peter
  170. + options are now written/read
  171. + find and replace routines
  172. }