TmpltForm.pas 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. {
  2. TIGCC IDE
  3. Copyright (C) 2004 Fréderic Bour
  4. Copyright (C) 2004 Sebastian Reichelt
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software Foundation,
  15. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. }
  17. unit TmpltForm;
  18. interface
  19. uses
  20. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  21. Dialogs, StdCtrls, MemoComponentUnit;
  22. type
  23. TTemplate = class(TObject)
  24. public
  25. Text: string;
  26. procedure Apply(Dest: TMemoComponent);
  27. end;
  28. TTemplateForm = class(TForm)
  29. TmpltList: TListBox;
  30. procedure FormDeactivate(Sender: TObject);
  31. procedure TmpltListData(Control: TWinControl; Index: Integer;
  32. var Data: string);
  33. procedure FormCreate(Sender: TObject);
  34. procedure FormDestroy(Sender: TObject);
  35. procedure TmpltListDblClick(Sender: TObject);
  36. procedure TmpltListKeyDown(Sender: TObject; var Key: Word;
  37. Shift: TShiftState);
  38. procedure FormActivate(Sender: TObject);
  39. public
  40. { Déclarations publiques }
  41. Templates: TStringList;
  42. procedure Select;
  43. end;
  44. var
  45. TemplateForm: TTemplateForm;
  46. implementation
  47. {$R *.dfm}
  48. uses MasterUnit, UtilsDos, CodeCompletion, CompletionForm;
  49. { TTemplate }
  50. procedure TTemplate.Apply(Dest: TMemoComponent);
  51. var
  52. St: Integer;
  53. i, p: Integer;
  54. S, Before: string;
  55. Lst: TStringList;
  56. T: TTextCell;
  57. begin
  58. // Indent template
  59. T := Dest.Selection.StartRowCol;
  60. Before := Copy(Dest.Lines[T.Row - 1], 1, T.Col - 1);
  61. Lst := TStringList.Create;
  62. Lst.Text := Text;
  63. for i := 1 to Lst.Count - 1 do
  64. Lst[i] := Before + Lst[i];
  65. S := Lst.Text;
  66. if (Copy(S, Length(S) - 1, 2) = #13#10) and (Copy(Text, Length(Text) - 1, 2) <> #13#10) then
  67. Delete(S, Length(S) - 1, 2);
  68. Lst.Free;
  69. p := Pos('|', S);
  70. st := Dest.Selection.RStart;
  71. if p = 0 then
  72. Dest.Selection.Text := S
  73. else
  74. begin
  75. Delete(S, p, 1);
  76. Dest.Selection.Text := S;
  77. Inc(St, p - 1);
  78. end;
  79. Dest.Selection.RStart := st;
  80. Dest.Selection.RLength := 0;
  81. end;
  82. { TTemplateForm }
  83. procedure TTemplateForm.FormDeactivate(Sender: TObject);
  84. begin
  85. Hide;
  86. end;
  87. procedure TTemplateForm.TmpltListData(Control: TWinControl; Index: Integer;
  88. var Data: string);
  89. begin
  90. if (Index >= 0) and (Index < Templates.Count) then
  91. Data := Templates[Index];
  92. end;
  93. procedure TTemplateForm.FormCreate(Sender: TObject);
  94. var
  95. T: TTemplate;
  96. F: TStream;
  97. S: string;
  98. begin
  99. Templates := TStringList.Create;
  100. Templates.Sorted := True;
  101. S := WithBackslash(TIGCCFolder) + TemplatesLocation + 'templates.dat';
  102. if FileExists(S) then
  103. try
  104. F := TFileStream.Create(S, fmOpenRead);
  105. try
  106. while ReadString(F, S) do
  107. begin
  108. T := TTemplate.Create;
  109. ReadString(F, T.Text);
  110. Templates.AddObject(S, T);
  111. end;
  112. finally
  113. F.Free;
  114. end;
  115. except
  116. end;
  117. end;
  118. procedure TTemplateForm.FormDestroy(Sender: TObject);
  119. var
  120. i: Integer;
  121. F: TStream;
  122. T: TTemplate;
  123. begin
  124. try
  125. F := TFileStream.Create(WithBackslash(TIGCCFolder) + TemplatesLocation + 'templates.dat', fmCreate);
  126. try
  127. for i := 0 to Templates.Count - 1 do
  128. begin
  129. T := TTemplate(Templates.Objects[i]);
  130. if Assigned(T) then
  131. begin
  132. WriteString(F, Templates[i]);
  133. WriteString(F, T.Text);
  134. end;
  135. end;
  136. finally
  137. F.Free;
  138. end;
  139. except
  140. end;
  141. for i := 0 to Templates.Count - 1 do
  142. Templates.Objects[i].Free;
  143. Templates.Free;
  144. end;
  145. procedure TTemplateForm.TmpltListDblClick(Sender: TObject);
  146. begin
  147. Select;
  148. end;
  149. procedure TTemplateForm.TmpltListKeyDown(Sender: TObject; var Key: Word;
  150. Shift: TShiftState);
  151. begin
  152. case Key of
  153. VK_RETURN:
  154. Select;
  155. VK_ESCAPE, VK_CLEAR, VK_BACK, VK_DELETE:
  156. Hide;
  157. end;
  158. end;
  159. procedure TTemplateForm.Select;
  160. var
  161. Idx: Integer;
  162. M: TMemoComponent;
  163. T: TTemplate;
  164. begin
  165. M := CompForm.Editor;
  166. if Assigned(M) then
  167. begin
  168. Idx := TmpltList.ItemIndex;
  169. if (Idx >= 0) and (Idx < Templates.Count) then
  170. begin
  171. T := TTemplate(Templates.Objects[Idx]);
  172. if Assigned(T) then
  173. T.Apply(M);
  174. end;
  175. end;
  176. Hide;
  177. end;
  178. procedure TTemplateForm.FormActivate(Sender: TObject);
  179. var
  180. M: TMemoComponent;
  181. P: TPoint;
  182. begin
  183. TmpltList.Count := Templates.Count;
  184. M := CompForm.Editor;
  185. if Assigned(M) then
  186. begin
  187. TmpltList.ItemIndex := 0;
  188. P := M.ClientToScreen(M.Selection.StartPoint);
  189. Left := P.X + 4;
  190. Top := P.Y + 20;
  191. end;
  192. end;
  193. end.