PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/Gedemin/Component/SynEdit/Source/SynHighlighterManager.pas

http://gedemin.googlecode.com/
Pascal | 479 lines | 302 code | 35 blank | 142 comment | 30 complexity | c7850a60d4197758cc772f3230a12826 MD5 | raw file
Possible License(s): AGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. {-------------------------------------------------------------------------------
  2. The contents of this file are subject to the Mozilla Public License
  3. Version 1.1 (the "License"); you may not use this file except in compliance
  4. with the License. You may obtain a copy of the License at
  5. http://www.mozilla.org/MPL/
  6. Software distributed under the License is distributed on an "AS IS" basis,
  7. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
  8. the specific language governing rights and limitations under the License.
  9. The Original Code is: SynHighlighterManager.pas, released 2000-04-14.
  10. The Original Code is based on mwHighlighterManager.pas by Primoz Gabrijelcic,
  11. part of the mwEdit component suite.
  12. All Rights Reserved.
  13. Contributors to the SynEdit and mwEdit projects are listed in the
  14. Contributors.txt file.
  15. Alternatively, the contents of this file may be used under the terms of the
  16. GNU General Public License Version 2 or later (the "GPL"), in which case
  17. the provisions of the GPL are applicable instead of those above.
  18. If you wish to allow use of your version of this file only under the terms
  19. of the GPL and not to allow others to use your version of this file
  20. under the MPL, indicate your decision by deleting the provisions above and
  21. replace them with the notice and other provisions required by the GPL.
  22. If you do not delete the provisions above, a recipient may use your version
  23. of this file under either the MPL or the GPL.
  24. $Id: SynHighlighterManager.pas,v 1.4 2001/08/27 09:47:34 plpolak Exp $
  25. You may retrieve the latest version of this file at the SynEdit home page,
  26. located at http://SynEdit.SourceForge.net
  27. Known Issues:
  28. - does not work when dropped on a frame in Delphi 5
  29. -------------------------------------------------------------------------------}
  30. {
  31. @abstract(Provides a component to manage many highlighters in a single project.)
  32. @author(Primoz Gabrijelcic)
  33. @created(1999, converted to SynEdit 2000-04-14)
  34. @lastmod(2000-04-14)
  35. Provides a component to manage many highlighters in a single project.
  36. }
  37. unit SynHighlighterManager;
  38. {$I SynEdit.inc}
  39. interface
  40. uses
  41. Classes;
  42. type
  43. {:Highlighter manager.<p>
  44. Design-only component, designed to simplify work with highlighter components.<p>
  45. When placed on the form, SynHighlighterManager scans the form for highlighter
  46. components (descendants of TSynCustomHighlighter). Next it presents the user with
  47. small form containing checkboxed list and some buttons. User can select (by
  48. checking/unchecking list items) highlighter that should be placed onto the
  49. form. After user clicks OK, SynHighlighterManager synchronises highlighter
  50. components on the form with required state.<p>
  51. Built-in tricks:<br>
  52. - SynHighlighterManager never covers existing TComponent with a highlighter.<br>
  53. - SynHighlighterManager scans the form for TSynCustomHighlighter descendants and
  54. uses topmost and leftmost component as a starting point for insertion. If
  55. no TSynCustomHighlighter components are found, first highlighter will be placed
  56. at coordinates (8,8).<p>
  57. Known issues:<br>
  58. - If you place TSynHighlighterManager by double-clicking its icon in
  59. component palette, it will function normally, except that when all is
  60. done, Delphi will disply small window with title "Error" and message
  61. "Operation aborted". Purely cosmetic issue for which there is no obvious
  62. workaround. Live with it.<p>
  63. Last change: 2000-01-21
  64. @author Primoz Gabrijelcic
  65. @version 0.1
  66. @component
  67. @see TSynEditHighlighter
  68. :}
  69. TSynHighlighterManager = class(TComponent)
  70. public
  71. constructor Create(AOwner: TComponent); override;
  72. published
  73. end;
  74. procedure Register;
  75. implementation
  76. uses
  77. SysUtils,
  78. {$IFDEF SYN_KYLIX}
  79. Qt,
  80. QForms,
  81. QControls,
  82. QStdCtrls,
  83. QCheckLst,
  84. DesignIntf,
  85. Types,
  86. {$ELSE}
  87. {$IFDEF SYN_COMPILER_6_UP}
  88. DesignIntf,
  89. {$ELSE}
  90. DsgnIntf,
  91. {$ENDIF}
  92. Windows,
  93. Forms,
  94. Controls,
  95. StdCtrls,
  96. CheckLst,
  97. {$ENDIF}
  98. SynEditStrConst,
  99. SynEditHighlighter;
  100. type
  101. TSynHighlighterForm = class(TForm)
  102. clbHighlighters: TCheckListBox;
  103. btnSelectAll: TButton;
  104. btnDeselectAll: TButton;
  105. btnOK: TButton;
  106. btnCancel: TButton;
  107. Highlight: TSynHighlighterList;
  108. constructor Create(highlighters: TSynHighlighterList);
  109. {$IFDEF SYN_COMPILER_4_UP}reintroduce;{$ENDIF}
  110. procedure LoadForm;
  111. procedure SelectAll(Sender: TObject);
  112. procedure DeselectAll(Sender: TObject);
  113. end;
  114. {$IFDEF SYN_COMPILER_4_UP}
  115. {$IFDEF SYN_COMPILER_6_UP}
  116. TDesignerClass = IDesigner;
  117. {$ELSE}
  118. TDesignerClass = IFormDesigner;
  119. {$ENDIF}
  120. {$ELSE}
  121. TDesignerClass = TFormDesigner;
  122. {$ENDIF}
  123. procedure Register;
  124. begin
  125. RegisterComponents(SYNS_ComponentsPage, [TSynHighlighterManager]);
  126. end;
  127. { TSynHighlighterManager }
  128. constructor TSynHighlighterManager.Create(AOwner: TComponent);
  129. var
  130. form: TCustomForm;
  131. dsgn: TDesignerClass;
  132. highlight: TSynHighlighterList;
  133. synForm: TSynHighlighterForm;
  134. procedure CheckExisting;
  135. var
  136. i: integer;
  137. j: integer;
  138. begin
  139. for i := 0 to form.ComponentCount-1 do begin
  140. j := highlight.FindByClass(form.Components[i]);
  141. if j >= 0 then begin
  142. j := synForm.clbHighlighters.Items.IndexOf(highlight[j].GetLanguageName);
  143. if j >= 0 then
  144. synForm.clbHighlighters.Checked[j] := true;
  145. end;
  146. end; //for
  147. end;
  148. function FindHighlighterComp(hlClass: TSynCustomHighlighterClass): integer;
  149. var
  150. i: integer;
  151. begin
  152. Result := -1;
  153. for i := 0 to form.ComponentCount-1 do begin
  154. if form.Components[i] is hlClass then begin
  155. Result := i;
  156. Exit;
  157. end;
  158. end; //for
  159. end;
  160. procedure PlaceNew;
  161. var
  162. i: integer;
  163. high: integer;
  164. comp: integer;
  165. xpos, ypos: integer;
  166. xstart: integer;
  167. procedure GetStartCoordinates;
  168. var
  169. compTop: integer;
  170. compLeft: integer;
  171. i: integer;
  172. begin
  173. xpos := -1;
  174. ypos := -1;
  175. for i := 0 to form.ComponentCount-1 do begin
  176. if form.Components[i] is TSynCustomHighlighterClass then begin
  177. compLeft := LongRec(form.Components[i].DesignInfo).Lo;
  178. compTop := LongRec(form.Components[i].DesignInfo).Hi;
  179. if (xpos < 0) or (compLeft < xpos) then
  180. xpos := compLeft;
  181. if (ypos < 0) or (compTop < ypos) then
  182. ypos := compTop;
  183. end;
  184. end; //for
  185. if xpos < 0 then
  186. xpos := 8;
  187. if ypos < 0 then
  188. ypos := 8;
  189. xstart := xpos;
  190. end;
  191. procedure IncCoordinates;
  192. begin
  193. Inc(xpos,32);
  194. if (xpos+32) >= form.ClientWidth then begin
  195. xpos := xstart;
  196. Inc(ypos,32);
  197. end;
  198. end;
  199. function CoordinatesTaken: boolean;
  200. var
  201. compTop: integer;
  202. compLeft: integer;
  203. compRect: TRect;
  204. testRect: TRect;
  205. interRect: TRect;
  206. i: integer;
  207. begin
  208. Result := false;
  209. testRect := Rect(xpos,ypos,xpos+31,ypos+31);
  210. for i := 0 to form.ComponentCount-1 do begin
  211. if (form.Components[i] <> self) and (not (form.Components[i] is TControl)) then begin
  212. compLeft := LongRec(form.Components[i].DesignInfo).Lo;
  213. compTop := LongRec(form.Components[i].DesignInfo).Hi;
  214. compRect := Rect(compLeft,compTop,compLeft+31,compTop+31);
  215. if IntersectRect(interRect,testRect,compRect) then begin
  216. Result := true;
  217. Exit;
  218. end;
  219. end;
  220. end; //for
  221. end;
  222. procedure GetFreeCoordinates;
  223. begin
  224. while CoordinatesTaken do
  225. IncCoordinates;
  226. end;
  227. begin
  228. GetStartCoordinates;
  229. // Iterate over TCheckListBox, not over GetPlaceableHighlighters to ensure
  230. // inserted highlighters to be sorted by name.
  231. // Iterate twice - delete highlighters in first pass (to make place), create
  232. // in second.
  233. for i := 0 to synForm.clbHighlighters.Items.Count-1 do begin
  234. if not synForm.clbHighlighters.Checked[i] then begin // unchecked - remove
  235. high := highlight.FindByName(synForm.clbHighlighters.Items[i]);
  236. if high >= 0 then begin
  237. comp := FindHighlighterComp(highlight[high]);
  238. if comp >= 0 then
  239. form.Components[comp].Free;
  240. end;
  241. end;
  242. end; //for
  243. for i := 0 to synForm.clbHighlighters.Items.Count-1 do begin
  244. if synForm.clbHighlighters.Checked[i] then begin // checked - add
  245. high := highlight.FindByName(synForm.clbHighlighters.Items[i]);
  246. if high >= 0 then begin
  247. if FindHighlighterComp(highlight[high]) < 0 then begin
  248. GetFreeCoordinates;
  249. dsgn.CreateComponent(highlight[high],AOwner,xpos,ypos,24,24);
  250. IncCoordinates;
  251. end;
  252. end;
  253. end;
  254. end; //for
  255. end;
  256. begin
  257. inherited;
  258. if (csDesigning in ComponentState) and (AOwner is TCustomForm) then begin
  259. form := TCustomForm(AOwner);
  260. dsgn := form.Designer as TDesignerClass;
  261. {$IFDEF SYN_KYLIX}
  262. dsgn := form.DesignerHook as TDesignerClass;
  263. {$ELSE}
  264. dsgn := form.Designer as TDesignerClass;
  265. {$ENDIF}
  266. highlight := GetPlaceableHighlighters;
  267. if highlight.Count = 0 then
  268. {$IFDEF SYN_KYLIX}
  269. Application.MessageBox('No highlighters found!','Highlighter Manager', [smbOK], smsWarning)
  270. {$ELSE}
  271. Application.MessageBox('No highlighters found!','Highlighter Manager', MB_OK or MB_ICONEXCLAMATION)
  272. {$ENDIF}
  273. else
  274. begin
  275. synForm := TSynHighlighterForm.Create(highlight);
  276. try
  277. CheckExisting;
  278. if synForm.ShowModal = mrOK then
  279. PlaceNew;
  280. finally
  281. synForm.Free;
  282. end;
  283. end;
  284. end;
  285. SysUtils.Abort;
  286. end;
  287. { TSynHighlighterForm }
  288. constructor TSynHighlighterForm.Create(highlighters: TSynHighlighterList);
  289. begin
  290. CreateNew(nil);
  291. Caption := 'Highlighter Manager';
  292. Width := 410;
  293. Height := 243;
  294. Position := poScreenCenter;
  295. {$IFDEF SYN_KYLIX}
  296. BorderStyle := fbsDialog;
  297. {$ELSE}
  298. BorderStyle := bsDialog;
  299. {$ENDIF}
  300. Highlight := highlighters;
  301. //object clbHighlighters: TCheckListBox
  302. // Left = 8
  303. // Top = 8
  304. // Width = 305
  305. // Height = 201
  306. // ItemHeight = 13
  307. // TabOrder = 0
  308. //end
  309. //object btnSelectAll: TButton
  310. // Left = 320
  311. // Top = 8
  312. // Width = 75
  313. // Height = 25
  314. // Caption = '&Select All'
  315. // TabOrder = 1
  316. //end
  317. //object btnDeselectAll: TButton
  318. // Left = 320
  319. // Top = 40
  320. // Width = 75
  321. // Height = 25
  322. // Caption = '&Deselect All'
  323. // TabOrder = 2
  324. //end
  325. //object btnOK: TButton
  326. // Left = 320
  327. // Top = 152
  328. // Width = 75
  329. // Height = 25
  330. // Caption = 'OK'
  331. // Default = True
  332. // ModalResult = 1
  333. // TabOrder = 3
  334. //end
  335. //object btnCancel: TButton
  336. // Left = 320
  337. // Top = 184
  338. // Width = 75
  339. // Height = 25
  340. // Caption = 'Cancel'
  341. // ModalResult = 2
  342. // TabOrder = 4
  343. //end
  344. clbHighlighters := TCheckListBox.Create(Self);
  345. btnSelectAll := TButton.Create(Self);
  346. btnDeselectAll := TButton.Create(Self);
  347. btnOK := TButton.Create(Self);
  348. btnCancel := TButton.Create(Self);
  349. with clbHighlighters do
  350. begin
  351. Name := 'clbHighlighters';
  352. Parent := Self;
  353. Left := 8;
  354. Top := 8;
  355. Width := 305;
  356. Height := 201;
  357. ItemHeight := 13;
  358. Sorted := true;
  359. TabOrder := 0;
  360. end;
  361. with btnSelectAll do
  362. begin
  363. Name := 'btnSelectAll';
  364. Parent := Self;
  365. Left := 320;
  366. Top := 8;
  367. Width := 75;
  368. Height := 25;
  369. Caption := '&Select All';
  370. TabOrder := 1;
  371. OnClick := SelectAll;
  372. end;
  373. with btnDeselectAll do
  374. begin
  375. Name := 'btnDeselectAll';
  376. Parent := Self;
  377. Left := 320;
  378. Top := 40;
  379. Width := 75;
  380. Height := 25;
  381. Caption := '&Deselect All';
  382. TabOrder := 2;
  383. OnClick := DeselectAll;
  384. end;
  385. with btnOK do
  386. begin
  387. Name := 'btnOK';
  388. Parent := Self;
  389. Left := 320;
  390. Top := 152;
  391. Width := 75;
  392. Height := 25;
  393. Caption := 'OK';
  394. Default := True;
  395. ModalResult := 1;
  396. TabOrder := 3;
  397. end;
  398. with btnCancel do
  399. begin
  400. Name := 'btnCancel';
  401. Parent := Self;
  402. Left := 320;
  403. Top := 184;
  404. Width := 75;
  405. Height := 25;
  406. Caption := 'Cancel';
  407. ModalResult := 2;
  408. TabOrder := 4;
  409. end;
  410. LoadForm;
  411. end;
  412. procedure TSynHighlighterForm.DeselectAll(Sender: TObject);
  413. var
  414. i: integer;
  415. begin
  416. for i := 0 to clbHighlighters.Items.Count-1 do
  417. clbHighlighters.Checked[i] := false;
  418. end;
  419. procedure TSynHighlighterForm.LoadForm;
  420. var
  421. i: integer;
  422. begin
  423. clbHighlighters.Clear;
  424. for i := 0 to Highlight.Count-1 do begin
  425. clbHighlighters.Items.Add(Highlight[i].GetLanguageName);
  426. end; //for
  427. end;
  428. procedure TSynHighlighterForm.SelectAll(Sender: TObject);
  429. var
  430. i: integer;
  431. begin
  432. for i := 0 to clbHighlighters.Items.Count-1 do
  433. clbHighlighters.Checked[i] := true;
  434. end;
  435. end.