Module

fTemplateEditor

Path

C:\CPRS\CPRS30\Templates\fTemplateEditor.pas

Last Modified

7/15/2014 3:26:44 PM

Comments

The OwnerScan conditional compile varaible was created because there were too
 many things that needed to be done to incorporate the viewing of other user's
 personal templates by clinical coordinators.  These include:
   Changing the Personal tree.
   expanding entirely new personal list when personal list changes
   when click on stop editing shared button, and personal is for someone else,
     need to resync to personal list.

HOT HEYS NOT YET ASSIGNED:
JFKQRUVZ

Units Used in Interface

Name Comments
fBase508Form -
uTemplates -

Units Used in Implementation

Name Comments
dShared -
fDrawers -
fFindingTemplates -
fIconLegend -
fTemplateAutoGen -
fTemplateFieldEditor -
fTemplateFields -
fTemplateObjects -
fTemplateView -
rConsults -
rCore -
rEventHooks -
rMisc -
rTemplates -
uConst -
uCore -
uReminders -
uSpell -

Classes

Name Comments
TfrmTemplateEditor -

Procedures

Name Owner Declaration Scope Comments
Adjust - procedure Adjust(Control: TWinControl); Local -
AdjustControls4FontChange TfrmTemplateEditor procedure AdjustControls4FontChange; Protected -
AutoLongLines TfrmTemplateEditor procedure AutoLongLines(Sender: TObject); Protected -
btnApplyClick TfrmTemplateEditor procedure btnApplyClick(Sender: TObject); Public/Published -
btnCancelClick TfrmTemplateEditor procedure btnCancelClick(Sender: TObject); Public/Published -
btnFindClick TfrmTemplateEditor procedure btnFindClick(Sender: TObject); Public/Published S1,S2: string;
btnNewClick TfrmTemplateEditor procedure btnNewClick(Sender: TObject); Public/Published -
btnOKClick TfrmTemplateEditor procedure btnOKClick(Sender: TObject); Public/Published -
cbActiveClick TfrmTemplateEditor procedure cbActiveClick(Sender: TObject); Public/Published -
cbClick TfrmTemplateEditor procedure cbClick(Sender: TCheckBox; Index: integer); Protected -
cbDisplayOnlyClick TfrmTemplateEditor procedure cbDisplayOnlyClick(Sender: TObject); Public/Published -
cbEditSharedClick TfrmTemplateEditor procedure cbEditSharedClick(Sender: TObject); Public/Published -
cbExcludeClick TfrmTemplateEditor procedure cbExcludeClick(Sender: TObject); Public/Published -
cbFirstLineClick TfrmTemplateEditor procedure cbFirstLineClick(Sender: TObject); Public/Published -
cbHideDlgItemsClick TfrmTemplateEditor procedure cbHideDlgItemsClick(Sender: TObject); Public/Published -
cbHideItemsClick TfrmTemplateEditor procedure cbHideItemsClick(Sender: TObject); Public/Published -
cbIndentClick TfrmTemplateEditor procedure cbIndentClick(Sender: TObject); Public/Published -
cbLockClick TfrmTemplateEditor procedure cbLockClick(Sender: TObject); Public/Published -
cbLongLinesClick TfrmTemplateEditor procedure cbLongLinesClick(Sender: TObject); Public/Published -
cbNotesClick TfrmTemplateEditor procedure cbNotesClick(Sender: TObject); Public/Published -
cbOneItemOnlyClick TfrmTemplateEditor procedure cbOneItemOnlyClick(Sender: TObject); Public/Published -
cboOwnerChange TfrmTemplateEditor procedure cboOwnerChange(Sender: TObject); Public/Published -
cboOwnerNeedData TfrmTemplateEditor procedure cboOwnerNeedData(Sender: TObject; const StartFrom: string; Direction, InsertAt: Integer); Public/Published -
cbPerFindOptionClick TfrmTemplateEditor procedure cbPerFindOptionClick(Sender: TObject); Public/Published -
cbPerHideClick TfrmTemplateEditor procedure cbPerHideClick(Sender: TObject); Public/Published -
cbShFindOptionClick TfrmTemplateEditor procedure cbShFindOptionClick(Sender: TObject); Public/Published -
cbShHideClick TfrmTemplateEditor procedure cbShHideClick(Sender: TObject); Public/Published -
cbxCOMObjChange TfrmTemplateEditor procedure cbxCOMObjChange(Sender: TObject); Public/Published -
cbxLinkExit TfrmTemplateEditor procedure cbxLinkExit(Sender: TObject); Public/Published -
cbxLinkNeedData TfrmTemplateEditor procedure cbxLinkNeedData(Sender: TObject; const StartFrom: string; Direction, InsertAt: Integer); Public/Published -
cbxRemDlgsChange TfrmTemplateEditor procedure cbxRemDlgsChange(Sender: TObject); Public/Published -
cbxTypeChange TfrmTemplateEditor procedure cbxTypeChange(Sender: TObject); Public/Published -
cbxTypeDrawItem TfrmTemplateEditor procedure cbxTypeDrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); Public/Published -
ClearFields - procedure ClearFields; Local -
DisplayBoilerplate TfrmTemplateEditor procedure DisplayBoilerplate(Node: TTreeNode); Protected -
EditTemplates - procedure EditTemplates(Form: TForm; NewTemplate: boolean = FALSE; CopiedText: string = ''; Shared: boolean = FALSE); Interfaced -
edtCOMParamChange TfrmTemplateEditor procedure edtCOMParamChange(Sender: TObject); Public/Published -
edtGapChange TfrmTemplateEditor procedure edtGapChange(Sender: TObject); Public/Published -
edtGapKeyPress TfrmTemplateEditor procedure edtGapKeyPress(Sender: TObject; var Key: Char); Public/Published -
edtNameExit TfrmTemplateEditor procedure edtNameExit(Sender: TObject); Public/Published -
edtNameOldChange TfrmTemplateEditor procedure edtNameOldChange(Sender: TObject); Public/Published -
edtPerSearchEnter TfrmTemplateEditor procedure edtPerSearchEnter(Sender: TObject); Public/Published -
edtPerSearchExit TfrmTemplateEditor procedure edtPerSearchExit(Sender: TObject); Public/Published -
edtSearchChange TfrmTemplateEditor procedure edtSearchChange(Sender: TObject); Public/Published -
edtShSearchEnter TfrmTemplateEditor procedure edtShSearchEnter(Sender: TObject); Public/Published -
edtShSearchExit TfrmTemplateEditor procedure edtShSearchExit(Sender: TObject); Public/Published -
EnableControls TfrmTemplateEditor procedure EnableControls(ok, Root: boolean); Protected -
EnableNavControls TfrmTemplateEditor procedure EnableNavControls; Protected -
FormClose TfrmTemplateEditor procedure FormClose(Sender: TObject; var Action: TCloseAction); Public/Published -
FormCloseQuery TfrmTemplateEditor procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean); Public/Published -
FormCreate TfrmTemplateEditor procedure FormCreate(Sender: TObject); Public/Published -
FormDestroy TfrmTemplateEditor procedure FormDestroy(Sender: TObject); Public/Published -
FormShow TfrmTemplateEditor procedure FormShow(Sender: TObject); Public/Published -
HideControls TfrmTemplateEditor procedure HideControls; Protected -
InitTrees TfrmTemplateEditor procedure InitTrees; Protected -
mbMainResize TfrmTemplateEditor procedure mbMainResize(Sender: TObject); Public/Published -
mnuAutoGenClick TfrmTemplateEditor procedure mnuAutoGenClick(Sender: TObject); Public/Published -
mnuBPCheckGrammarClick TfrmTemplateEditor procedure mnuBPCheckGrammarClick(Sender: TObject); Public/Published -
mnuBPCopyClick TfrmTemplateEditor procedure mnuBPCopyClick(Sender: TObject); Public/Published -
mnuBPCutClick TfrmTemplateEditor procedure mnuBPCutClick(Sender: TObject); Public/Published -
mnuBPErrorCheckClick TfrmTemplateEditor procedure mnuBPErrorCheckClick(Sender: TObject); Public/Published -
mnuBPInsertFieldClick TfrmTemplateEditor procedure mnuBPInsertFieldClick(Sender: TObject); Public/Published -
mnuBPInsertObjectClick TfrmTemplateEditor procedure mnuBPInsertObjectClick(Sender: TObject); Public/Published -
mnuBPPasteClick TfrmTemplateEditor procedure mnuBPPasteClick(Sender: TObject); Public/Published -
mnuBPSelectAllClick TfrmTemplateEditor procedure mnuBPSelectAllClick(Sender: TObject); Public/Published -
mnuBPSpellCheckClick TfrmTemplateEditor procedure mnuBPSpellCheckClick(Sender: TObject); Public/Published -
mnuBPTryClick TfrmTemplateEditor procedure mnuBPTryClick(Sender: TObject); Public/Published -
mnuBPUndoClick TfrmTemplateEditor procedure mnuBPUndoClick(Sender: TObject); Public/Published -
mnuCollapseTreeClick TfrmTemplateEditor procedure mnuCollapseTreeClick(Sender: TObject); Public/Published -
mnuEditClick TfrmTemplateEditor procedure mnuEditClick(Sender: TObject); Public/Published -
mnuEditTemplateFieldsClick TfrmTemplateEditor procedure mnuEditTemplateFieldsClick(Sender: TObject); Public/Published -
mnuExportTemplateClick TfrmTemplateEditor procedure mnuExportTemplateClick(Sender: TObject); Public/Published -
mnuFindPersonalClick TfrmTemplateEditor procedure mnuFindPersonalClick(Sender: TObject); Public/Published -
mnuFindSharedClick TfrmTemplateEditor procedure mnuFindSharedClick(Sender: TObject); Public/Published -
mnuFindTemplatesClick TfrmTemplateEditor procedure mnuFindTemplatesClick(Sender: TObject); Public/Published -
mnuGroupBoilerplateClick TfrmTemplateEditor procedure mnuGroupBoilerplateClick(Sender: TObject); Public/Published -
mnuGroupBPCopyClick TfrmTemplateEditor procedure mnuGroupBPCopyClick(Sender: TObject); Public/Published -
mnuGroupBPSelectAllClick TfrmTemplateEditor procedure mnuGroupBPSelectAllClick(Sender: TObject); Public/Published -
mnuImportTemplateClick TfrmTemplateEditor procedure mnuImportTemplateClick(Sender: TObject); Public/Published -
mnuNodeCopyClick TfrmTemplateEditor procedure mnuNodeCopyClick(Sender: TObject); Public/Published -
mnuNodeDeleteClick TfrmTemplateEditor procedure mnuNodeDeleteClick(Sender: TObject); Public/Published -
mnuNodePasteClick TfrmTemplateEditor procedure mnuNodePasteClick(Sender: TObject); Public/Published -
mnuNotesCopyClick TfrmTemplateEditor procedure mnuNotesCopyClick(Sender: TObject); Public/Published -
mnuNotesCutClick TfrmTemplateEditor procedure mnuNotesCutClick(Sender: TObject); Public/Published -
mnuNotesGrammarClick TfrmTemplateEditor procedure mnuNotesGrammarClick(Sender: TObject); Public/Published -
mnuNotesPasteClick TfrmTemplateEditor procedure mnuNotesPasteClick(Sender: TObject); Public/Published -
mnuNotesSelectAllClick TfrmTemplateEditor procedure mnuNotesSelectAllClick(Sender: TObject); Public/Published -
mnuNotesSpellingClick TfrmTemplateEditor procedure mnuNotesSpellingClick(Sender: TObject); Public/Published -
mnuNotesUndoClick TfrmTemplateEditor procedure mnuNotesUndoClick(Sender: TObject); Public/Published -
mnuPerCollapseClick TfrmTemplateEditor procedure mnuPerCollapseClick(Sender: TObject); Public/Published -
mnuRefreshClick TfrmTemplateEditor procedure mnuRefreshClick(Sender: TObject); Public/Published -
mnuShCollapseClick TfrmTemplateEditor procedure mnuShCollapseClick(Sender: TObject); Public/Published -
mnuSortClick TfrmTemplateEditor procedure mnuSortClick(Sender: TObject); Public/Published -
mnuTemplateClick TfrmTemplateEditor procedure mnuTemplateClick(Sender: TObject); Public/Published -
mnuTemplateIconLegendClick TfrmTemplateEditor procedure mnuTemplateIconLegendClick(Sender: TObject); Public/Published -
mnuToolsClick TfrmTemplateEditor procedure mnuToolsClick(Sender: TObject); Public/Published -
MoveCopyButtons TfrmTemplateEditor procedure MoveCopyButtons; Protected -
NewPersonalUser TfrmTemplateEditor procedure NewPersonalUser(UsrIEN: Int64); Protected -
pnlBoilerplateCanResize TfrmTemplateEditor procedure pnlBoilerplateCanResize(Sender: TObject; var NewWidth, NewHeight: Integer; var Resize: Boolean); Public/Published -
pnlBoilerplateResize TfrmTemplateEditor procedure pnlBoilerplateResize(Sender: TObject); Public/Published -
pnlPerSearchResize TfrmTemplateEditor procedure pnlPerSearchResize(Sender: TObject); Public/Published -
pnlPropertiesResize TfrmTemplateEditor procedure pnlPropertiesResize(Sender: TObject); Public/Published -
pnlShSearchResize TfrmTemplateEditor procedure pnlShSearchResize(Sender: TObject); Public/Published -
popBoilerplatePopup TfrmTemplateEditor procedure popBoilerplatePopup(Sender: TObject); Public/Published -
popGroupPopup TfrmTemplateEditor procedure popGroupPopup(Sender: TObject); Public/Published -
popNotesPopup TfrmTemplateEditor procedure popNotesPopup(Sender: TObject); Public/Published -
popTemplatesPopup TfrmTemplateEditor procedure popTemplatesPopup(Sender: TObject); Public/Published -
reBoilChange TfrmTemplateEditor procedure reBoilChange(Sender: TObject); Public/Published -
reBoilKeyDown TfrmTemplateEditor procedure reBoilKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); Public/Published -
reBoilKeyPress TfrmTemplateEditor procedure reBoilKeyPress(Sender: TObject; var Key: Char); Public/Published -
reBoilKeyUp TfrmTemplateEditor procedure reBoilKeyUp(Sender: TObject; var Key: Word; Shift: TShiftState); Public/Published -
reBoilSelectionChange TfrmTemplateEditor procedure reBoilSelectionChange(Sender: TObject); Public/Published -
RefreshData TfrmTemplateEditor procedure RefreshData; Protected -
reGroupBPSelectionChange TfrmTemplateEditor procedure reGroupBPSelectionChange(Sender: TObject); Public/Published -
reNotesChange TfrmTemplateEditor procedure reNotesChange(Sender: TObject); Public/Published -
reResizeRequest TfrmTemplateEditor procedure reResizeRequest(Sender: TObject; Rect: TRect); Public/Published -
Resync TfrmTemplateEditor procedure Resync(const Templates: array of TTemplate); Protected -
sbCopyLeftClick TfrmTemplateEditor procedure sbCopyLeftClick(Sender: TObject); Public/Published -
sbCopyRightClick TfrmTemplateEditor procedure sbCopyRightClick(Sender: TObject); Public/Published -
sbDeleteClick TfrmTemplateEditor procedure sbDeleteClick(Sender: TObject); Public/Published -
sbMoveDownClick TfrmTemplateEditor procedure sbMoveDownClick(Sender: TObject); Public/Published -
sbMoveUpClick TfrmTemplateEditor procedure sbMoveUpClick(Sender: TObject); Public/Published -
ScanTree - procedure ScanTree(Tree: TTreeView); Local -
SetFindNext TfrmTemplateEditor procedure SetFindNext(const Tree: TTreeView; const Value: boolean); Protected -
SharedEditing TfrmTemplateEditor procedure SharedEditing; Protected -
ShowBoilerPlate TfrmTemplateEditor procedure ShowBoilerPlate(Hide: Boolean); Protected -
ShowGroupBoilerplate TfrmTemplateEditor procedure ShowGroupBoilerplate(Visible: boolean); Protected -
ShowInfo TfrmTemplateEditor procedure ShowInfo(Node: TTreeNode); Protected -
ShowTemplateType TfrmTemplateEditor procedure ShowTemplateType(Template: TTemplate); Protected -
splBoilMoved TfrmTemplateEditor procedure splBoilMoved(Sender: TObject); Public/Published -
splMainCanResize TfrmTemplateEditor procedure splMainCanResize(Sender: TObject; var NewSize: Integer; var Accept: Boolean); Public/Published -
splMainMoved TfrmTemplateEditor procedure splMainMoved(Sender: TObject); Public/Published -
TemplateLocked TfrmTemplateEditor procedure TemplateLocked(Sender: TObject); Protected -
tmrAutoScrollTimer TfrmTemplateEditor procedure tmrAutoScrollTimer(Sender: TObject); Public/Published -
tvPersonalExpanding TfrmTemplateEditor procedure tvPersonalExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean); Public/Published -
tvSharedExpanding TfrmTemplateEditor procedure tvSharedExpanding(Sender: TObject; Node: TTreeNode; var AllowExpansion: Boolean); Public/Published -
tvTreeChange TfrmTemplateEditor procedure tvTreeChange(Sender: TObject; Node: TTreeNode); Public/Published -
tvTreeDragDrop TfrmTemplateEditor procedure tvTreeDragDrop(Sender, Source: TObject; X, Y: Integer); Public/Published -
tvTreeDragging TfrmTemplateEditor procedure tvTreeDragging(Sender: TObject; Node: TTreeNode; var CanDrag: Boolean); Public/Published -
tvTreeDragOver TfrmTemplateEditor procedure tvTreeDragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); Public/Published -
tvTreeEndDrag TfrmTemplateEditor procedure tvTreeEndDrag(Sender, Target: TObject; X, Y: Integer); Public/Published -
tvTreeEnter TfrmTemplateEditor procedure tvTreeEnter(Sender: TObject); Public/Published -
tvTreeGetImageIndex TfrmTemplateEditor procedure tvTreeGetImageIndex(Sender: TObject; Node: TTreeNode); Public/Published -
tvTreeGetSelectedIndex TfrmTemplateEditor procedure tvTreeGetSelectedIndex(Sender: TObject; Node: TTreeNode); Public/Published -
tvTreeKeyDown TfrmTemplateEditor procedure tvTreeKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); Public/Published -
tvTreeNodeEdited TfrmTemplateEditor procedure tvTreeNodeEdited(Sender: TObject; Node: TTreeNode; var S: string); Public/Published -
tvTreeStartDrag TfrmTemplateEditor procedure tvTreeStartDrag(Sender: TObject; var DragObject: TDragObject); Public/Published -
UpdateApply TfrmTemplateEditor procedure UpdateApply(Template: TTemplate); Protected Procedure UpdatePersonalObjects;
UpdateInsertsDialogs TfrmTemplateEditor procedure UpdateInsertsDialogs; Protected -
UpdateXY TfrmTemplateEditor procedure UpdateXY(re: TRichEdit; lblX, lblY: TLabel); Protected -

Functions

Name Owner Declaration Scope Comments
AllowMove TfrmTemplateEditor function AllowMove(ADropNode, ADragNode: TTreeNode): boolean; Protected -
AutoDel TfrmTemplateEditor function AutoDel(Template: TTemplate): boolean; Protected -
CanClone TfrmTemplateEditor function CanClone(const Node: TTreeNode): boolean; Protected
Returns TRUE if Cloning is not needed or if Cloning is needed and
  the top personal Node in the tree is locked.
ChangeTree TfrmTemplateEditor function ChangeTree(NewTree: TTreeView): boolean; Protected -
Clone TfrmTemplateEditor function Clone(Node: TTreeNode): boolean; Protected -
GetFirstPersonalNode - function GetFirstPersonalNode(Node: TTreeNode): TTreeNode; Local -
GetLinkType TfrmTemplateEditor function GetLinkType(const ANode: TTreeNode): TTemplateLinkType; Protected -
GetTree TfrmTemplateEditor function GetTree: TTreeView; Protected -
IsTemplateLocked TfrmTemplateEditor function IsTemplateLocked(Node: TTreeNode): boolean; Protected -
PasteOK TfrmTemplateEditor function PasteOK: boolean; Protected -
ScanNames TfrmTemplateEditor function ScanNames: boolean; Protected -

Global Variables

Name Type Declaration Comments
frmTemplateFields TfrmTemplateFields frmTemplateFields: TfrmTemplateFields = nil; -
frmTemplateObjects TfrmTemplateObjects frmTemplateObjects: TfrmTemplateObjects = nil; -
tmplEditorSplitterBoil Integer tmplEditorSplitterBoil: integer = 0; -
tmplEditorSplitterMain Integer tmplEditorSplitterMain: integer = 0; -
tmplEditorSplitterMiddle Integer tmplEditorSplitterMiddle: integer = 0; -
tmplEditorSplitterNotes Integer tmplEditorSplitterNotes: integer = 0; -
tmplEditorSplitterProperties Integer tmplEditorSplitterProperties: integer = 0; -

Constants

Name Declaration Scope Comments
BPDisplayOnlyFld 0 Global GroupTag = 5;
BPFirstLineFld 1 Global -
BPHideDlgItemsFld 3 Global -
BPHideItemsFld 4 Global -
BPIndentFld 5 Global -
BPLockFld 6 Global -
BPOneItemOnlyFld 2 Global -
ForcedIdx array[boolean, TTypeIndex] of integer = ((0, 1, 2, 3, 4, 5), (-1, 0, 1, 2, -1, -1)) Global -
IdxForced array[boolean, 0..5] of TTypeIndex = ((tiTemplate, tiFolder, tiGroup, tiDialog, tiRemDlg, tiCOMObj), Global -
iMessage 'This template has one or more new fields, and you are not authorized to create new fields. ' + Global -
iMessage2 'The imported template fields had XML errors. ' Global -
iMessage3 'No Fields were imported.' Global -
NoIE5 'You must have Internet Explorer 5 or better installed to %s Templates' Global -
NoIE5Header 'Need Internet Explorer 5' Global -
PropText ' Template Properties ' Global -
tcCopy TTemplateTreeControl Interfaced -
tcDel TTemplateTreeControl Interfaced -
tcDown TTemplateTreeControl Interfaced -
tcLbl TTemplateTreeControl Interfaced -
tcUp TTemplateTreeControl Interfaced -
TemplateEditorSplitters 'frmTempEditSplitters' Interfaced -
TemplateEditorSplitters2 'frmTempEditSplitters2' Interfaced -
tiCOMObj TTypeIndex Global -
tiDialog TTypeIndex Global -
tiFolder TTypeIndex Global -
tiGroup TTypeIndex Global -
tiNone TTypeIndex(-1) Global -
tiRemDlg TTypeIndex Global -
tiTemplate TTypeIndex Global -
ttDialog TTemplateType(-ord(ttGroup)) Global TypeTag: array[TTypeIndex] of integer = (7, 6, 8, -8, 7, 7);
ttPersonal TTemplateTreeType Interfaced -
ttShared TTemplateTreeType Interfaced -
TypeTag array[TTypeIndex] of TTemplateType = (ttDoc, ttClass, ttGroup, ttDialog, ttDoc, ttDoc) Global -
VK_A Ord('A') Global -
VK_C Ord('C') Global -
VK_E Ord('E') Global -
VK_F Ord('F') Global -
VK_G Ord('G') Global -
VK_I Ord('I') Global -
VK_S Ord('S') Global -
VK_T Ord('T') Global -
VK_V Ord('V') Global -
VK_X Ord('X') Global -
VK_Z Ord('Z') Global -


Module Source

1     unit fTemplateEditor;
2     {The OwnerScan conditional compile varaible was created because there were too
3      many things that needed to be done to incorporate the viewing of other user's
4      personal templates by clinical coordinators.  These include:
5        Changing the Personal tree.
6        expanding entirely new personal list when personal list changes
7        when click on stop editing shared button, and personal is for someone else,
8          need to resync to personal list.
9     
10    HOT HEYS NOT YET ASSIGNED:
11    JFKQRUVZ
12    }
13    {$DEFINE OwnerScan}
14    {$UNDEF OwnerScan}
15    interface
16    
17    uses
18      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
19      ExtCtrls, StdCtrls, ComCtrls, ORCtrls, Buttons, Mask, ORFn, ORNet,
20      uTemplates, Menus, ImgList, Clipbrd, ToolWin, MenuBar, TypInfo, MSXML_TLB, fBase508Form,
21      VA508AccessibilityManager, VA508ImageListLabeler;
22    
23    type
24      TTemplateTreeControl = (tcDel, tcUp, tcDown, tcLbl, tcCopy);
25      TTemplateTreeType = (ttShared, ttPersonal);
26    
27      TfrmTemplateEditor = class(TfrmBase508Form)
28        splMain: TSplitter;
29        pnlBottom: TPanel;
30        btnApply: TButton;
31        btnCancel: TButton;
32        btnOK: TButton;
33        pnlTop: TPanel;
34        pnlRightTop: TPanel;
35        splProperties: TSplitter;
36        pnlCopyBtns: TPanel;
37        sbCopyLeft: TBitBtn;
38        sbCopyRight: TBitBtn;
39        lblCopy: TLabel;
40        splMiddle: TSplitter;
41        pnlShared: TPanel;
42        lblShared: TLabel;
43        tvShared: TORTreeView;
44        pnlSharedBottom: TPanel;
45        sbShUp: TBitBtn;
46        sbShDown: TBitBtn;
47        sbShDelete: TBitBtn;
48        cbShHide: TCheckBox;
49        pnlSharedGap: TPanel;
50        pnlPersonal: TPanel;
51        lblPersonal: TLabel;
52        tvPersonal: TORTreeView;
53        pnlPersonalBottom: TPanel;
54        sbPerUp: TBitBtn;
55        sbPerDown: TBitBtn;
56        sbPerDelete: TBitBtn;
57        cbPerHide: TCheckBox;
58        pnlPersonalGap: TPanel;
59        popTemplates: TPopupMenu;
60        mnuCollapseTree: TMenuItem;
61        mnuFindTemplates: TMenuItem;
62        popBoilerplate: TPopupMenu;
63        mnuBPInsertObject: TMenuItem;
64        mnuBPErrorCheck: TMenuItem;
65        mnuBPSpellCheck: TMenuItem;
66        tmrAutoScroll: TTimer;
67        popGroup: TPopupMenu;
68        mnuGroupBPCopy: TMenuItem;
69        mnuBPCut: TMenuItem;
70        N2: TMenuItem;
71        mnuBPCopy: TMenuItem;
72        mnuBPPaste: TMenuItem;
73        N4: TMenuItem;
74        mnuGroupBPSelectAll: TMenuItem;
75        mnuBPSelectAll: TMenuItem;
76        N6: TMenuItem;
77        mnuNodeCopy: TMenuItem;
78        mnuNodePaste: TMenuItem;
79        mnuNodeDelete: TMenuItem;
80        N8: TMenuItem;
81        mnuBPUndo: TMenuItem;
82        cbEditShared: TCheckBox;
83        pnlProperties: TPanel;
84        gbProperties: TGroupBox;
85        lblName: TLabel;
86        lblLines: TLabel;
87        cbExclude: TORCheckBox;
88        cbActive: TCheckBox;
89        edtGap: TCaptionEdit;
90        udGap: TUpDown;
91        edtName: TCaptionEdit;
92        mnuMain: TMainMenu;
93        mnuEdit: TMenuItem;
94        mnuUndo: TMenuItem;
95        N9: TMenuItem;
96        mnuCut: TMenuItem;
97        mnuCopy: TMenuItem;
98        mnuPaste: TMenuItem;
99        mnuSelectAll: TMenuItem;
100       N11: TMenuItem;
101       mnuInsertObject: TMenuItem;
102       mnuErrorCheck: TMenuItem;
103       mnuSpellCheck: TMenuItem;
104       N13: TMenuItem;
105       mnuGroupBoilerplate: TMenuItem;
106       mnuGroupCopy: TMenuItem;
107       mnuGroupSelectAll: TMenuItem;
108       mnuTemplate: TMenuItem;
109       mnuTCopy: TMenuItem;
110       mnuTPaste: TMenuItem;
111       mnuTDelete: TMenuItem;
112       N12: TMenuItem;
113       pnlShSearch: TPanel;
114       btnShFind: TORAlignButton;
115       edtShSearch: TCaptionEdit;
116       cbShMatchCase: TCheckBox;
117       cbShWholeWords: TCheckBox;
118       pnlPerSearch: TPanel;
119       btnPerFind: TORAlignButton;
120       edtPerSearch: TCaptionEdit;
121       cbPerMatchCase: TCheckBox;
122       cbPerWholeWords: TCheckBox;
123       mnuFindShared: TMenuItem;
124       mnuFindPersonal: TMenuItem;
125       N3: TMenuItem;
126       mnuShCollapse: TMenuItem;
127       mnuPerCollapse: TMenuItem;
128       pnlMenuBar: TPanel;
129       lblPerOwner: TLabel;
130       cboOwner: TORComboBox;
131       btnNew: TORAlignButton;
132       pnlMenu: TPanel;
133       mbMain: TMenuBar;
134       mnuNewTemplate: TMenuItem;
135       Bevel1: TBevel;
136       mnuNodeNew: TMenuItem;
137       mnuBPCheckGrammar: TMenuItem;
138       mnuCheckGrammar: TMenuItem;
139       N1: TMenuItem;
140       N7: TMenuItem;
141       N14: TMenuItem;
142       mnuSort: TMenuItem;
143       N15: TMenuItem;
144       mnuNodeSort: TMenuItem;
145       mnuTry: TMenuItem;
146       mnuBPTry: TMenuItem;
147       mnuAutoGen: TMenuItem;
148       mnuNodeAutoGen: TMenuItem;
149       popNotes: TPopupMenu;
150       mnuNotesUndo: TMenuItem;
151       MenuItem2: TMenuItem;
152       mnuNotesCut: TMenuItem;
153       mnuNotesCopy: TMenuItem;
154       mnuNotesPaste: TMenuItem;
155       MenuItem6: TMenuItem;
156       mnuNotesSelectAll: TMenuItem;
157       MenuItem8: TMenuItem;
158       mnuNotesGrammar: TMenuItem;
159       mnuNotesSpelling: TMenuItem;
160       cbNotes: TCheckBox;
161       gbDialogProps: TGroupBox;
162       cbDisplayOnly: TCheckBox;
163       cbOneItemOnly: TCheckBox;
164       cbHideItems: TORCheckBox;
165       cbFirstLine: TCheckBox;
166       cbHideDlgItems: TCheckBox;
167       cbIndent: TCheckBox;
168       mnuTools: TMenuItem;
169       mnuEditTemplateFields: TMenuItem;
170       N16: TMenuItem;
171       mnuImportTemplate: TMenuItem;
172       mnuExportTemplate: TMenuItem;
173       mnuBPInsertField: TMenuItem;
174       mnuInsertField: TMenuItem;
175       cbEditUser: TCheckBox;
176       dlgImport: TOpenDialog;
177       dlgExport: TSaveDialog;
178       cbxType: TCaptionComboBox;
179       lblType: TLabel;
180       cbxRemDlgs: TORComboBox;
181       lblRemDlg: TLabel;
182       N17: TMenuItem;
183       mnuTemplateIconLegend: TMenuItem;
184       cbLock: TORCheckBox;
185       mnuRefresh: TMenuItem;
186       pnlCOM: TPanel;
187       lblCOMParam: TLabel;
188       edtCOMParam: TCaptionEdit;
189       cbxCOMObj: TORComboBox;
190       lblCOMObj: TLabel;
191       pnlLink: TPanel;
192       cbxLink: TORComboBox;
193       lblLink: TLabel;
194       imgLblTemplates: TVA508ImageListLabeler;
195       Panel1: TPanel;
196       pnlBoilerplate: TPanel;
197       splBoil: TSplitter;
198       splNotes: TSplitter;
199       reBoil: TRichEdit;
200       pnlGroupBP: TPanel;
201       lblGroupBP: TLabel;
202       lblGroupRow: TLabel;
203       lblGroupCol: TLabel;
204       reGroupBP: TRichEdit;
205       pnlGroupBPGap: TPanel;
206       pnlBP: TPanel;
207       lblBoilerplate: TLabel;
208       lblBoilRow: TLabel;
209       lblBoilCol: TLabel;
210       cbLongLines: TCheckBox;
211       pnlNotes: TPanel;
212       lblNotes: TLabel;
213       reNotes: TRichEdit;
214       procedure btnNewClick(Sender: TObject);
215       procedure btnApplyClick(Sender: TObject);
216       procedure FormCreate(Sender: TObject);
217       procedure cboOwnerNeedData(Sender: TObject; const StartFrom: string;
218         Direction, InsertAt: Integer);
219       procedure cboOwnerChange(Sender: TObject);
220       procedure tvPersonalExpanding(Sender: TObject; Node: TTreeNode;
221         var AllowExpansion: Boolean);
222       procedure tvSharedExpanding(Sender: TObject; Node: TTreeNode;
223         var AllowExpansion: Boolean);
224       procedure tvTreeGetImageIndex(Sender: TObject; Node: TTreeNode);
225       procedure tvTreeGetSelectedIndex(Sender: TObject; Node: TTreeNode);
226       procedure tvTreeChange(Sender: TObject; Node: TTreeNode);
227       procedure splMainMoved(Sender: TObject);
228       procedure pnlBoilerplateResize(Sender: TObject);
229       procedure edtNameOldChange(Sender: TObject);
230       procedure cbActiveClick(Sender: TObject);
231       procedure cbExcludeClick(Sender: TObject);
232       procedure edtGapChange(Sender: TObject);
233       procedure tvTreeEnter(Sender: TObject);
234       procedure tvTreeNodeEdited(Sender: TObject; Node: TTreeNode;
235         var S: string);
236       procedure cbShHideClick(Sender: TObject);
237       procedure cbPerHideClick(Sender: TObject);
238       procedure FormDestroy(Sender: TObject);
239       procedure sbMoveUpClick(Sender: TObject);
240       procedure sbMoveDownClick(Sender: TObject);
241       procedure sbDeleteClick(Sender: TObject);
242       procedure tvTreeDragging(Sender: TObject; Node: TTreeNode;
243         var CanDrag: Boolean);
244       procedure tvTreeDragOver(Sender, Source: TObject; X, Y: Integer;
245         State: TDragState; var Accept: Boolean);
246       procedure tvTreeDragDrop(Sender, Source: TObject; X, Y: Integer);
247       procedure sbCopyLeftClick(Sender: TObject);
248       procedure sbCopyRightClick(Sender: TObject);
249       procedure reBoilChange(Sender: TObject);
250       procedure cbEditSharedClick(Sender: TObject);
251       procedure popTemplatesPopup(Sender: TObject);
252       procedure mnuCollapseTreeClick(Sender: TObject);
253       procedure mnuFindTemplatesClick(Sender: TObject);
254       procedure btnFindClick(Sender: TObject);
255       procedure edtSearchChange(Sender: TObject);
256       procedure edtShSearchEnter(Sender: TObject);
257       procedure edtShSearchExit(Sender: TObject);
258       procedure edtPerSearchEnter(Sender: TObject);
259       procedure edtPerSearchExit(Sender: TObject);
260       procedure btnOKClick(Sender: TObject);
261       procedure FormShow(Sender: TObject);
262       procedure mnuBPInsertObjectClick(Sender: TObject);
263       procedure mnuBPErrorCheckClick(Sender: TObject);
264       procedure popBoilerplatePopup(Sender: TObject);
265       procedure btnCancelClick(Sender: TObject);
266       procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
267       procedure mnuBPSpellCheckClick(Sender: TObject);
268       procedure splBoilMoved(Sender: TObject);
269       procedure edtGapKeyPress(Sender: TObject; var Key: Char);
270       procedure edtNameExit(Sender: TObject);
271       procedure tmrAutoScrollTimer(Sender: TObject);
272       procedure tvTreeStartDrag(Sender: TObject;
273         var DragObject: TDragObject);
274       procedure tvTreeEndDrag(Sender, Target: TObject; X, Y: Integer);
275       procedure mnuGroupBPCopyClick(Sender: TObject);
276       procedure popGroupPopup(Sender: TObject);
277       procedure mnuBPCutClick(Sender: TObject);
278       procedure mnuBPCopyClick(Sender: TObject);
279       procedure mnuBPPasteClick(Sender: TObject);
280       procedure mnuGroupBPSelectAllClick(Sender: TObject);
281       procedure mnuBPSelectAllClick(Sender: TObject);
282       procedure mnuNodeDeleteClick(Sender: TObject);
283       procedure mnuNodeCopyClick(Sender: TObject);
284       procedure mnuNodePasteClick(Sender: TObject);
285       procedure mnuBPUndoClick(Sender: TObject);
286       procedure tvTreeKeyDown(Sender: TObject; var Key: Word;
287         Shift: TShiftState);
288       procedure mnuEditClick(Sender: TObject);
289       procedure mnuGroupBoilerplateClick(Sender: TObject);
290       procedure cbShFindOptionClick(Sender: TObject);
291       procedure cbPerFindOptionClick(Sender: TObject);
292       procedure mnuTemplateClick(Sender: TObject);
293       procedure mnuFindSharedClick(Sender: TObject);
294       procedure mnuFindPersonalClick(Sender: TObject);
295       procedure mnuShCollapseClick(Sender: TObject);
296       procedure mnuPerCollapseClick(Sender: TObject);
297       procedure pnlShSearchResize(Sender: TObject);
298       procedure pnlPerSearchResize(Sender: TObject);
299       procedure pnlPropertiesResize(Sender: TObject);
300       procedure mbMainResize(Sender: TObject);
301       procedure mnuBPCheckGrammarClick(Sender: TObject);
302       procedure mnuSortClick(Sender: TObject);
303       procedure pnlBoilerplateCanResize(Sender: TObject; var NewWidth,
304         NewHeight: Integer; var Resize: Boolean);
305       procedure mnuBPTryClick(Sender: TObject);
306       procedure mnuAutoGenClick(Sender: TObject);
307       procedure reNotesChange(Sender: TObject);
308       procedure mnuNotesUndoClick(Sender: TObject);
309       procedure mnuNotesCutClick(Sender: TObject);
310       procedure mnuNotesCopyClick(Sender: TObject);
311       procedure mnuNotesPasteClick(Sender: TObject);
312       procedure mnuNotesSelectAllClick(Sender: TObject);
313       procedure mnuNotesGrammarClick(Sender: TObject);
314       procedure mnuNotesSpellingClick(Sender: TObject);
315       procedure popNotesPopup(Sender: TObject);
316       procedure cbNotesClick(Sender: TObject);
317       procedure cbDisplayOnlyClick(Sender: TObject);
318       procedure cbFirstLineClick(Sender: TObject);
319       procedure cbOneItemOnlyClick(Sender: TObject);
320       procedure cbHideDlgItemsClick(Sender: TObject);
321       procedure cbHideItemsClick(Sender: TObject);
322       procedure cbIndentClick(Sender: TObject);
323       procedure mnuToolsClick(Sender: TObject);
324       procedure mnuEditTemplateFieldsClick(Sender: TObject);
325       procedure mnuBPInsertFieldClick(Sender: TObject);
326       procedure mnuExportTemplateClick(Sender: TObject);
327       procedure mnuImportTemplateClick(Sender: TObject);
328       procedure cbxTypeDrawItem(Control: TWinControl; Index: Integer;
329         Rect: TRect; State: TOwnerDrawState);
330       procedure cbxTypeChange(Sender: TObject);
331       procedure cbxRemDlgsChange(Sender: TObject);
332       procedure mnuTemplateIconLegendClick(Sender: TObject);
333       procedure cbLongLinesClick(Sender: TObject);
334       procedure cbLockClick(Sender: TObject);
335       procedure mnuRefreshClick(Sender: TObject);
336       procedure reResizeRequest(Sender: TObject; Rect: TRect);
337       procedure reBoilSelectionChange(Sender: TObject);
338       procedure reGroupBPSelectionChange(Sender: TObject);
339       procedure cbxCOMObjChange(Sender: TObject);
340       procedure edtCOMParamChange(Sender: TObject);
341       procedure cbxLinkNeedData(Sender: TObject; const StartFrom: string;
342         Direction, InsertAt: Integer);
343       procedure cbxLinkExit(Sender: TObject);
344       procedure reBoilKeyUp(Sender: TObject; var Key: Word;
345         Shift: TShiftState);
346       procedure reBoilKeyPress(Sender: TObject; var Key: Char);
347       procedure reBoilKeyDown(Sender: TObject; var Key: Word;
348         Shift: TShiftState);
349       procedure FormClose(Sender: TObject; var Action: TCloseAction);
350       procedure splMainCanResize(Sender: TObject; var NewSize: Integer;
351         var Accept: Boolean);
352     private
353       FLastRect: TRect;
354       FForceContainer: boolean;
355       FSavePause: integer;
356       FCopyNode: TTreeNode;
357       FPasteNode: TTreeNode;
358       FCopying: boolean;
359       FDropNode: TTreeNode;
360       FDropInto: boolean;
361       FDragNode: TTreeNode;
362       FPersonalEmptyNodeCount: integer;
363       FSharedEmptyNodeCount: integer;
364   //    FOldPersonalTemplate: TTemplate;
365       FCurrentPersonalUser: Int64;
366       FCanEditPersonal: boolean;
367       FCanEditShared: boolean;
368       FUpdating: boolean;
369       FCurTree: TTreeView;
370       FTreeControl: array[TTemplateTreeType, TTemplateTreeControl] of TControl;
371       FInternalHiddenExpand: boolean;
372       FFindShOn: boolean;
373       FFindShNext: boolean;
374       FLastFoundShNode: TTreeNode;
375       FFindPerOn: boolean;
376       FFindPerNext: boolean;
377       FLastFoundPerNode: TTreeNode;
378       FFirstShow: boolean;
379       FFocusName: boolean;
380       FOK2Close: boolean;
381       FBtnNewNode: TTreeNode;
382       FLastDropNode: TTreeNode;
383       FFromMainMenu: boolean;
384       FMainMenuTree: TTreeView;
385       FDragOverCount: integer;
386       FBPOK: boolean;
387       FImportingFromXML: boolean;
388       FXMLTemplateElement: IXMLDOMNode;
389       FXMLFieldElement: IXMLDOMNode;
390       FCanDoReminders: boolean;
391       FCanDoCOMObjects: boolean;
392       //FPersonalObjects: TStringList;
393       FShowingTemplate: TTemplate;
394       FConsultServices: TStringList;
395       FNavigatingTab: boolean;
396     protected
397       procedure UpdateXY(re: TRichEdit; lblX, lblY: TLabel);
398       function IsTemplateLocked(Node: TTreeNode): boolean;
399       procedure RefreshData;
400       procedure ShowTemplateType(Template: TTemplate);
401       procedure DisplayBoilerplate(Node: TTreeNode);
402       procedure NewPersonalUser(UsrIEN: Int64);
403       procedure HideControls;
404       procedure EnableControls(ok, Root: boolean);
405       procedure EnableNavControls;
406       procedure MoveCopyButtons;
407       procedure ShowInfo(Node: TTreeNode);
408       function ChangeTree(NewTree: TTreeView): boolean;
409       procedure Resync(const Templates: array of TTemplate);
410       function AllowMove(ADropNode, ADragNode: TTreeNode): boolean;
411       function CanClone(const Node: TTreeNode): boolean;
412       function Clone(Node: TTreeNode): boolean;
413       procedure SharedEditing;
414       function GetTree: TTreeView;
415       procedure SetFindNext(const Tree: TTreeView; const Value: boolean);
416       function ScanNames: boolean;
417       function PasteOK: boolean;
418       function AutoDel(Template: TTemplate): boolean;
419       procedure cbClick(Sender: TCheckBox; Index: integer);
420       procedure UpdateInsertsDialogs;
421       procedure AutoLongLines(Sender: TObject);
422       //procedure UpdatePersonalObjects;
423       procedure UpdateApply(Template: TTemplate);
424       procedure TemplateLocked(Sender: TObject);
425       procedure InitTrees;
426       procedure AdjustControls4FontChange;
427       procedure ShowGroupBoilerplate(Visible: boolean);
428       procedure ShowBoilerPlate(Hide: Boolean);
429       function GetLinkType(const ANode: TTreeNode): TTemplateLinkType;
430     end;
431   
432   procedure EditTemplates(Form: TForm; NewTemplate: boolean = FALSE; CopiedText: string = ''; Shared: boolean = FALSE);
433   
434   const
435     TemplateEditorSplitters = 'frmTempEditSplitters';
436     TemplateEditorSplitters2 = 'frmTempEditSplitters2';
437   
438   var
439     tmplEditorSplitterMiddle: integer = 0;
440     tmplEditorSplitterProperties: integer = 0;
441     tmplEditorSplitterMain: integer = 0;
442     tmplEditorSplitterBoil: integer = 0;
443     tmplEditorSplitterNotes: integer = 0;
444   
445   implementation
446   
447   {$R *.DFM}
448   
449   uses dShared, uCore, rTemplates, fTemplateObjects, uSpell, fTemplateView,
450     fTemplateAutoGen, fDrawers, fTemplateFieldEditor, fTemplateFields, XMLUtils,
451     fIconLegend, uReminders, uConst, rCore, rEventHooks, rConsults, VAUtils,
452     rMisc, fFindingTemplates;
453   
454   const
455     PropText = ' Template Properties ';
456   //  GroupTag = 5;
457     BPDisplayOnlyFld = 0;
458     BPFirstLineFld = 1;
459     BPOneItemOnlyFld = 2;
460     BPHideDlgItemsFld = 3;
461     BPHideItemsFld = 4;
462     BPIndentFld = 5;
463     BPLockFld = 6;
464     NoIE5 = 'You must have Internet Explorer 5 or better installed to %s Templates';
465     NoIE5Header = 'Need Internet Explorer 5';
466     VK_A = Ord('A');
467     VK_C = Ord('C');
468     VK_E = Ord('E');
469     VK_F = Ord('F');
470     VK_G = Ord('G');
471     VK_I = Ord('I');
472     VK_S = Ord('S');
473     VK_T = Ord('T');
474     VK_V = Ord('V');
475     VK_X = Ord('X');
476     VK_Z = Ord('Z');
477   
478   type
479     TTypeIndex = (tiTemplate, tiFolder, tiGroup, tiDialog, tiRemDlg, tiCOMObj);
480   
481   const
482     tiNone = TTypeIndex(-1);
483   //  TypeTag: array[TTypeIndex] of integer = (7, 6, 8, -8, 7, 7);
484     ttDialog = TTemplateType(-ord(ttGroup));
485   
486     TypeTag: array[TTypeIndex] of TTemplateType = (ttDoc, ttClass, ttGroup, ttDialog, ttDoc, ttDoc);
487     ForcedIdx: array[boolean, TTypeIndex] of integer = ((0, 1, 2, 3, 4, 5), (-1, 0, 1, 2, -1, -1));
488     IdxForced: array[boolean, 0..5] of TTypeIndex = ((tiTemplate, tiFolder, tiGroup, tiDialog, tiRemDlg, tiCOMObj),
489       (tiFolder, tiGroup, tiDialog, tiNone, tiNone, tiNone));
490     iMessage = 'This template has one or more new fields, and you are not authorized to create new fields.  ' +
491       'If you continue, the program will import the new template without the new fields.  Do you wish ' +
492       'to do this?';
493     iMessage2 = 'The imported template fields had XML errors.  ';
494     iMessage3 = 'No Fields were imported.';
495   
496   var
497     frmTemplateObjects: TfrmTemplateObjects = nil;
498     frmTemplateFields: TfrmTemplateFields = nil;
499   
500   procedure EditTemplates(Form: TForm; NewTemplate: boolean = FALSE;
501     CopiedText: string = ''; Shared: boolean = FALSE);
502   var
503     frmTemplateEditor: TfrmTemplateEditor;
504     Drawers: TfrmDrawers;
505     ExpandStr, SelectStr: string;
506     SelNode: TTreeNode;
507     SelShared: boolean;
508   
509   begin
510     if (UserTemplateAccessLevel in [taReadOnly, taNone]) then exit;
511   
512     ExpandStr := '';
513     SelectStr := '';
514     Drawers := nil;
515     if (not NewTemplate) and (CopiedText = '') then
516     begin
517       if Form is TfrmDrawers then
518         Drawers := TfrmDrawers(Form)
519       else
520         if IsPublishedProp(Form, DrawersProperty) then
521           Drawers := TfrmDrawers(GetOrdProp(Form, DrawersProperty));
522     end;
523   
524     if assigned(Drawers) then
525     begin
526       ExpandStr := Drawers.tvTemplates.GetExpandedIDStr(1, ';');
527       SelectStr := Drawers.tvTemplates.GetNodeID(TORTreeNode(Drawers.tvTemplates.Selected), 1, ';');
528     end;
529   
530     frmTemplateEditor := TfrmTemplateEditor.Create(Application);
531     try
532       with frmTemplateEditor do
533       begin
534         Font := Form.Font;
535         reBoil.Font.Size := Form.Font.Size;
536         reGroupBP.Font.Size := Form.Font.Size;
537         reNotes.Font.Size := Form.Font.Size;
538         dmodShared.ExpandTree(tvShared, ExpandStr, FSharedEmptyNodeCount);
539         SelNode := tvShared.FindPieceNode(SelectStr, 1, ';');
540         SelShared := assigned(SelNode);
541         dmodShared.ExpandTree(tvPersonal, ExpandStr, FPersonalEmptyNodeCount);
542         if not SelShared then
543           SelNode := tvPersonal.FindPieceNode(SelectStr, 1, ';');
544   
545         if (SelShared and (not Shared)) then
546           Shared := TRUE;
547   
548         if (Shared and (UserTemplateAccessLevel = taEditor)) then
549         begin
550           cbEditShared.Checked := TRUE;
551           ActiveControl := tvShared;
552           if SelShared then
553             tvShared.Selected := SelNode
554           else
555             tvShared.Selected := tvShared.Items.GetFirstNode;
556         end
557         else
558           if (not SelShared) and (assigned(SelNode)) then
559             tvPersonal.Selected := SelNode;
560         if (NewTemplate) then
561         begin
562           btnNewClick(frmTemplateEditor);
563           if (CopiedText <> '') then
564           begin
565             TTemplate(FBtnNewNode.Data).Boilerplate := CopiedText;
566             ShowInfo(FBtnNewNode);
567           end;
568         end;
569         ShowModal;
570       end;
571     finally
572       frmTemplateEditor.Free;
573     end;
574   end;
575   
576   procedure TfrmTemplateEditor.btnNewClick(Sender: TObject);
577   var
578     idx: integer;
579     Tmp, Owner: TTemplate;
580     Node, PNode: TTreeNode;
581     ownr: string;
582     ok: boolean;
583     ACheckBox: TCheckBox;
584   
585   begin
586     if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
587     begin
588       if (FCurTree = tvShared) and (FCanEditShared) then
589         ok := TRUE
590       else
591         if (FCurTree = tvPersonal) and (FCanEditPersonal) then
592           ok := TRUE
593         else
594           ok := FALSE;
595       if (ok) then
596       begin
597         Node := FCurTree.Selected;
598         PNode := Node;
599         if (TTemplate(Node.Data).RealType = ttDoc) then
600           PNode := Node.Parent;
601         if CanClone(PNode) then
602         begin
603           Clone(PNode);
604           Owner := TTemplate(PNode.Data);
605           if assigned(Owner) and Owner.CanModify then
606           begin
607             if Node = PNode then
608               idx := 0
609             else
610               idx := Owner.Items.IndexOf(Node.Data) + 1;
611             if (FCurTree = tvShared) then
612             begin
613               ownr := '';
614               ACheckBox := cbShHide;
615             end
616             else
617             begin
618               ownr := IntToStr(User.DUZ);
619               ACheckBox := cbPerHide;
620             end;
621             if FImportingFromXML then
622             begin
623               Tmp := TTemplate.CreateFromXML(FXMLTemplateElement, ownr);
624               ACheckBox.Checked := ACheckBox.Checked and Tmp.Active;
625             end
626             else
627             begin
628               Tmp := TTemplate.Create('0^T^A^' + NewTemplateName + '^^^' + ownr);
629               Tmp.BackupItems;
630               Templates.AddObject(Tmp.ID, Tmp);
631             end;
632             btnApply.Enabled := TRUE;
633             if (idx >= Owner.Items.Count) then
634               Owner.Items.Add(Tmp)
635             else
636               Owner.Items.Insert(idx, Tmp);
637             Resync([Owner]);
638             Node := FCurTree.Selected;
639             if (Node.Data <> Tmp) then
640             begin
641               if (TTemplate(Node.Data).RealType = ttDoc) then
642                 Node := Node.GetNextSibling
643               else
644               begin
645                 Node.Expand(FALSE);
646                 Node := Node.GetFirstChild;
647               end;
648               FCurTree.Selected := Node;
649             end;
650             FBtnNewNode := Node;
651             if (FFirstShow) then
652               FFocusName := TRUE
653             else
654             begin
655               edtName.SetFocus;
656               edtName.SelectAll;
657             end;
658           end;
659         end;
660       end;
661     end;
662   end;
663   
664   procedure TfrmTemplateEditor.btnApplyClick(Sender: TObject);
665   begin
666     if (ScanNames) then
667     begin
668       SaveAllTemplates;
669       BtnApply.Enabled := BackupDiffers;
670       if not BtnApply.Enabled then
671         UnlockAllTemplates;
672     end;
673   end;
674   
675   procedure TfrmTemplateEditor.FormCreate(Sender: TObject);
676   begin
677     SetFormPosition(Self);
678     ResizeAnchoredFormToFont(self);
679     //Now fix everything the resize messed up
680     lblLines.Width := cbLock.Left - lblLines.Left - 15;
681     sbPerDelete.Left := pnlPersonalBottom.ClientWidth - sbPerDelete.Width - 1;
682     sbPerDown.Left := sbPerDelete.Left - sbPerDown.Width - 2;
683     sbPerUp.Left := sbPerDown.Left - sbPerUp.Width - 2;
684     cbPerHide.Width := sbPerUp.Left - 3;
685     btnPerFind.Left := pnlPerSearch.ClientWidth - btnPerFind.Width;
686   
687     FSavePause := Application.HintHidePause;
688     Application.HintHidePause := FSavePause * 2;
689     if InteractiveRemindersActive then
690     begin
691       QuickCopy(GetTemplateAllowedReminderDialogs, cbxRemDlgs.Items);
692       FCanDoReminders := (cbxRemDlgs.Items.Count > 0);
693     end
694     else
695       FCanDoReminders := FALSE;
696   
697     QuickCopy(GetAllActiveCOMObjects, cbxCOMObj.Items);
698     FCanDoCOMObjects := (cbxCOMObj.Items.Count > 0);
699   
700     FUpdating := TRUE;
701     FFirstShow := TRUE;
702   
703     FTreeControl[ttShared, tcDel] := sbShDelete;
704     FTreeControl[ttShared, tcUp] := sbShUp;
705     FTreeControl[ttShared, tcDown] := sbShDown;
706     FTreeControl[ttShared, tcLbl] := lblCopy;
707     FTreeControl[ttShared, tcCopy] := sbCopyRight;
708     FTreeControl[ttPersonal, tcDel] := sbPerDelete;
709     FTreeControl[ttPersonal, tcUp] := sbPerUp;
710     FTreeControl[ttPersonal, tcDown] := sbPerDown;
711     FTreeControl[ttPersonal, tcLbl] := lblCopy;
712     FTreeControl[ttPersonal, tcCopy] := sbCopyLeft;
713     dmodShared.InEditor := TRUE;
714     dmodShared.OnTemplateLock := TemplateLocked;
715   
716     gbProperties.Caption := PropText;
717     pnlShSearch.Visible := FALSE;
718     pnlPerSearch.Visible := FALSE;
719     FCanEditPersonal := TRUE;
720   
721   { Don't mess with the order of the following commands! }
722     InitTrees;
723   
724     tvPersonal.Selected := tvPersonal.Items.GetFirstNode;
725   
726     ClearBackup;
727   
728     cboOwner.SelText := MixedCase(User.Name);
729     NewPersonalUser(User.DUZ);
730   
731     cbEditShared.Visible := (UserTemplateAccessLevel = taEditor);
732     FCanEditShared := FALSE;
733     SharedEditing;
734   
735     HideControls;
736   
737     lblCopy.AutoSize := TRUE;
738     lblCopy.AutoSize := FALSE; // resets height based on font
739     lblCopy.Width := pnlCopyBtns.Width + splMiddle.Width;
740     MoveCopyButtons;
741   
742     cbShHide.Checked := TRUE;
743     cbPerHide.Checked := TRUE;
744   
745     BtnApply.Enabled := BackupDiffers;
746     //SetFormPosition(Self);
747   end;
748   
749   procedure TfrmTemplateEditor.HideControls;
750   begin
751     sbCopyRight.Visible := FCanEditPersonal;
752     if (not FCanEditPersonal) then
753       cbPerHide.Checked := TRUE;
754     cbPerHide.Visible := FCanEditPersonal;
755     sbPerDelete.Visible := FCanEditPersonal;
756     sbPerUp.Visible := FCanEditPersonal;
757     sbPerDown.Visible := FCanEditPersonal;
758     tvPersonal.ReadOnly := not FCanEditPersonal;
759     MoveCopyButtons;
760   end;
761   
762   procedure TfrmTemplateEditor.cboOwnerNeedData(Sender: TObject;
763     const StartFrom: string; Direction, InsertAt: Integer);
764   begin
765   //  cboOwner.ForDataUse(SubSetOfTemplateOwners(StartFrom, Direction));
766   end;
767   
768   procedure TfrmTemplateEditor.cboOwnerChange(Sender: TObject);
769   begin
770     NewPersonalUser(cboOwner.ItemIEN);
771   end;
772   
773   procedure TfrmTemplateEditor.NewPersonalUser(UsrIEN: Int64);
774   var
775     NewEdit: boolean;
776   
777   begin
778     FCurrentPersonalUser := UsrIEN;
779     NewEdit := (FCurrentPersonalUser = User.DUZ);
780     if (FCanEditPersonal <> NewEdit) then
781     begin
782       FCanEditPersonal := NewEdit;
783       HideControls;
784     end;
785   end;
786   
787   procedure TfrmTemplateEditor.tvPersonalExpanding(Sender: TObject;
788     Node: TTreeNode; var AllowExpansion: Boolean);
789   begin
790     AllowExpansion := dmodShared.ExpandNode(tvPersonal, Node,
791       FPersonalEmptyNodeCount, not cbPerHide.Checked);
792     if (AllowExpansion and FInternalHiddenExpand) then
793       AllowExpansion := FALSE;
794   end;
795   
796   procedure TfrmTemplateEditor.tvSharedExpanding(Sender: TObject;
797     Node: TTreeNode; var AllowExpansion: Boolean);
798   begin
799     AllowExpansion := dmodShared.ExpandNode(tvShared, Node,
800       FSharedEmptyNodeCount, not cbShHide.Checked);
801     if (AllowExpansion and FInternalHiddenExpand) then
802       AllowExpansion := FALSE;
803   end;
804   
805   procedure TfrmTemplateEditor.tvTreeGetImageIndex(Sender: TObject;
806     Node: TTreeNode);
807   begin
808     Node.ImageIndex := dmodShared.ImgIdx(Node);
809   end;
810   
811   procedure TfrmTemplateEditor.tvTreeGetSelectedIndex(Sender: TObject;
812     Node: TTreeNode);
813   begin
814     Node.SelectedIndex := dmodShared.ImgIdx(Node);
815   end;
816   
817   function TfrmTemplateEditor.IsTemplateLocked(Node: TTreeNode): boolean;
818   var
819     Template: TTemplate;
820   
821   begin
822     Result := FALSE;
823     if assigned(Node) then
824     begin
825       Template := TTemplate(Node.Data);
826       if Template.AutoLock then
827         Result := TRUE
828       else
829         if (Template.PersonalOwner = 0) then
830         begin
831           if RootTemplate.IsLocked then
832             Result := TRUE
833           else
834           begin
835             Result := TTemplate(Node.Data).IsLocked;
836             if (not Result) and assigned(Node.Parent) and
837               (TTemplate(Node.Parent).PersonalOwner = 0) then
838               Result := IsTemplateLocked(Node.Parent);
839           end;
840         end;
841     end;
842   end;
843   
844   procedure TfrmTemplateEditor.tvTreeChange(Sender: TObject;
845     Node: TTreeNode);
846   var
847     ok, Something: boolean;
848     Template: TTemplate;
849   
850   begin
851     ChangeTree(TTreeView(Sender));
852     Something := assigned(Node);
853     if Something then
854     begin
855       Template := TTemplate(Node.Data);
856       Something := assigned(Template);
857       if Something then
858       begin
859         if (Sender = tvPersonal) then
860         begin
861           ok := FCanEditPersonal;
862           if ok and (Template.PersonalOwner = 0) and IsTemplateLocked(Node) then
863             ok := FALSE;
864         end
865         else
866           ok := FCanEditShared;
867         EnableControls(ok, (Template.RealType in AllTemplateRootTypes));
868         ShowInfo(Node);
869       end;
870     end;
871     if not Something then
872     begin
873       gbProperties.Caption := PropText;
874       EnableControls(FALSE, FALSE);
875       ShowInfo(nil);
876     end;
877   end;
878   
879   procedure TfrmTemplateEditor.EnableControls(ok, Root: boolean);
880   begin
881     cbLock.Enabled := ok and (FCurTree = tvShared);
882     if (ok and Root) then
883     begin
884       ok := FALSE;
885       lblName.Enabled := TRUE;
886       edtName.Enabled := TRUE;
887       reNotes.ReadOnly := FALSE;
888     end
889     else
890     begin
891       lblName.Enabled := ok;
892       edtName.Enabled := ok;
893       reNotes.ReadOnly := not ok;
894     end;
895     lblNotes.Enabled := (not reNotes.ReadOnly);
896     UpdateReadOnlyColorScheme(reNotes, reNotes.ReadOnly);
897     cbxType.Enabled := ok;
898     lblType.Enabled := ok;
899     lblRemDlg.Enabled := ok;
900     cbxRemDlgs.Enabled := ok and FCanDoReminders;
901     cbActive.Enabled := ok;
902     cbExclude.Enabled := ok;
903     cbDisplayOnly.Enabled := ok;
904     cbFirstLine.Enabled := ok;
905     cbOneItemOnly.Enabled := ok;
906     cbHideDlgItems.Enabled := ok;
907     cbHideItems.Enabled := ok;
908     cbIndent.Enabled := ok;
909     edtGap.Enabled := ok;
910     udGap.Enabled := ok;
911     udGap.Invalidate;
912     lblLines.Enabled := ok;
913     reBoil.ReadOnly := not ok;
914     UpdateReadOnlyColorScheme(reBoil, not ok);
915     lblLink.Enabled := ok;
916     cbxLink.Enabled := ok;
917     ok := ok and FCanDoCOMObjects;
918     cbxCOMObj.Enabled := ok;
919     lblCOMObj.Enabled := ok;
920     edtCOMParam.Enabled := ok;
921     lblCOMParam.Enabled := ok;
922     UpdateInsertsDialogs;
923     EnableNavControls;
924   end;
925   
926   procedure TfrmTemplateEditor.MoveCopyButtons;
927   var
928     tmpHeight: integer;
929   
930   begin
931     tmpHeight := tvShared.Height;
932     dec(tmpHeight, lblCopy.Height);
933     if (sbCopyLeft.Visible) then
934       dec(tmpHeight, sbCopyLeft.Height + 5);
935     if (sbCopyRight.Visible) then
936       dec(tmpHeight, sbCopyRight.Height + 5);
937     tmpHeight := (tmpHeight div 2) + tvShared.Top;
938     lblCopy.Top := tmpHeight;
939     inc(tmpHeight, lblCopy.height + 5);
940     if (sbCopyLeft.Visible) then
941     begin
942       sbCopyLeft.Top := tmpHeight;
943       inc(tmpHeight, sbCopyLeft.Height + 5);
944     end;
945     if (sbCopyRight.Visible) then
946       sbCopyRight.Top := tmpHeight;
947   end;
948   
949   procedure TfrmTemplateEditor.splMainCanResize(Sender: TObject;
950     var NewSize: Integer; var Accept: Boolean);
951   begin
952     inherited;
953     if (pnlGroupBP.Height - (NewSize - splMain.Top) <= pnlGroupBP.Constraints.MinHeight) and (NewSize > splMain.Top) then begin
954       //Moving Down
955       Accept := False;
956       NewSize := splMain.Top;
957     end else Accept := True; //Moving Up
958   end;
959   
960   procedure TfrmTemplateEditor.splMainMoved(Sender: TObject);
961   begin
962     MoveCopyButtons;
963   end;
964   
965   procedure TfrmTemplateEditor.ShowGroupBoilerplate(Visible: boolean);
966   begin
967     pnlGroupBP.Visible := Visible;
968     splBoil.Visible := Visible;
969     if Visible then
970     begin
971       reBoil.Align := alTop;
972       pnlGroupBP.Align := alClient;
973       reBoil.Height := tmplEditorSplitterBoil;
974       splBoil.Top := pnlGroupBP.Top - splBoil.Height;
975     end
976     else
977     begin
978       pnlGroupBP.Align := alBottom;
979       reBoil.Align := alClient;
980       pnlNotes.Constraints.MaxHeight := 0;
981     end;
982   end;
983   
984   procedure TfrmTemplateEditor.ShowBoilerPlate(Hide: Boolean);
985   begin
986    LockWindowUpdate(Self.Handle);
987    PnlBP.Visible := Hide;
988   
989    lblBoilerplate.Visible := Hide;
990    reBoil.Visible := Hide;
991    LockWindowUpdate(0);
992   end;
993   
994   
995   procedure TfrmTemplateEditor.ShowInfo(Node: TTreeNode);
996   var
997     OldUpdating, ClearName, ClearRB, ClearAll: boolean;
998     Idx: TTypeIndex;
999     CanDoCOM: boolean;
1000    lt: TTemplateLinkType;
1001    lts: string;
1002  
1003  begin
1004    OldUpdating := FUpdating;
1005    FUpdating := TRUE;
1006    try
1007      if (assigned(Node)) then
1008      begin
1009        FShowingTemplate := TTemplate(Node.Data);
1010        with FShowingTemplate do
1011        begin
1012          ClearName := FALSE;
1013          ClearRB := FALSE;
1014          ClearAll := FALSE;
1015          ShowTemplateType(TTemplate(Node.Data));
1016          lt := GetLinkType(Node);
1017          if (lt = ltNone) or (IsReminderDialog and (not (lt in [ltNone, ltTitle]))) then
1018            pnlLink.Visible := FALSE
1019          else
1020          begin
1021            pnlLink.Visible := TRUE;
1022            pnlLink.Tag := ord(lt);
1023            case lt of
1024              ltTitle: lts := 'Title';
1025              ltConsult: lts := 'Consult Service';
1026              ltProcedure: lts := 'Procedure';
1027            else lts := '';
1028            end;
1029            cbxLink.Clear;
1030            if lt = ltConsult then
1031            begin
1032              cbxLink.LongList := FALSE;
1033              if not assigned(FConsultServices) then
1034              begin
1035                FConsultServices := TStringList.Create;
1036                FastAssign(LoadServiceListWithSynonyms(1), FConsultServices);
1037                SortByPiece(FConsultServices, U, 2);
1038              end;
1039              FastAssign(FConsultServices, cbxLink.Items);
1040            end
1041            else
1042            begin
1043              cbxLink.LongList := TRUE;
1044              cbxLink.HideSynonyms := TRUE;
1045              cbxLink.InitLongList(LinkName);
1046            end;
1047            cbxLink.SelectByID(LinkIEN);
1048            lblLink.Caption := ' Associated ' + lts + ': ';
1049            cbxLink.Caption := lblLink.Caption;
1050          end;
1051  
1052          edtName.Text := PrintName;
1053          reNotes.Lines.Text := Description;
1054          if (PersonalOwner = 0) and (FCurTree = tvShared) and (cbEditShared.Checked) then
1055          begin
1056            cbLock.Checked := IsLocked;
1057            if AutoLock then
1058              cbLock.Enabled := FALSE;
1059          end
1060          else
1061          begin
1062            cbLock.Checked := IsTemplateLocked(Node);
1063            cbLock.Enabled := FALSE;
1064          end;
1065          CanDoCom := FCanDoCOMObjects and (PersonalOwner = 0);
1066          if (RealType in AllTemplateRootTypes) then
1067          begin
1068            ClearRB := TRUE;
1069            ClearAll := TRUE;
1070          end
1071          else
1072          begin
1073            case RealType of
1074              ttDoc: begin
1075                  if IsReminderDialog then
1076                    Idx := tiRemDlg
1077                  else
1078                    if IsCOMObject then
1079                      Idx := tiCOMObj
1080                    else
1081                      Idx := tiTemplate;
1082                end;
1083              ttGroup: begin
1084                  if (Dialog) then
1085                    Idx := tiDialog
1086                  else
1087                    Idx := tiGroup;
1088                end;
1089              ttClass: Idx := tiFolder;
1090            else Idx := tiNone;
1091            end;
1092            FForceContainer := ((RealType in [ttGroup, ttClass]) and (Children <> tcNone));
1093            cbxType.Items.Clear;
1094            if not FForceContainer then
1095              cbxType.Items.Add('Template');
1096            cbxType.Items.Add('Folder');
1097            cbxType.Items.Add('Group Template');
1098            cbxType.Items.Add('Dialog');
1099            if (not FForceContainer) then
1100            begin
1101              if (FCanDoReminders or CanDoCOM) then
1102                cbxType.Items.Add('Reminder Dialog');
1103              if (CanDoCOM) then
1104                cbxType.Items.Add('COM Object');
1105            end;
1106            cbxType.ItemIndex := ForcedIdx[FForceContainer, Idx];
1107            if (Idx = tiRemDlg) and FCanDoReminders then
1108              cbxRemDlgs.SelectByID(ReminderDialogIEN)
1109            else
1110            begin
1111              lblRemDlg.Enabled := FALSE;
1112              cbxRemDlgs.Enabled := FALSE;
1113              cbxRemDlgs.ItemIndex := -1;
1114            end;
1115            if (Idx = tiCOMObj) and CanDoCOM then
1116            begin
1117              pnlCOM.Visible := TRUE;
1118              cbxCOMObj.SelectByIEN(COMObject);
1119              edtCOMParam.Text := COMParam;
1120            end
1121            else
1122            begin
1123              pnlCOM.Visible := FALSE;
1124              cbxCOMObj.ItemIndex := -1;
1125              edtCOMParam.Text := '';
1126            end;
1127            cbActive.Checked := Active;
1128            if (RealType in [ttClass, ttGroup]) then
1129              cbHideItems.Checked := HideItems
1130            else
1131            begin
1132              cbHideItems.Checked := FALSE;
1133              cbHideItems.Enabled := FALSE;
1134            end;
1135            if ((RealType in [ttDoc, ttGroup]) and (assigned(Node.Parent)) and
1136              (TTemplate(Node.Parent.Data).RealType = ttGroup) and
1137              (not IsReminderDialog) and (not IsCOMObject)) then
1138              cbExclude.Checked := Exclude
1139            else
1140            begin
1141              cbExclude.Checked := FALSE;
1142              cbExclude.Enabled := FALSE;
1143            end;
1144            if dmodShared.InDialog(Node) and (not IsReminderDialog) and (not IsCOMObject) then
1145            begin
1146              cbDisplayOnly.Checked := DisplayOnly;
1147              cbFirstLine.Checked := FirstLine;
1148            end
1149            else
1150            begin
1151              cbDisplayOnly.Checked := FALSE;
1152              cbDisplayOnly.Enabled := FALSE;
1153              cbFirstLine.Checked := FALSE;
1154              cbFirstLine.Enabled := FALSE;
1155            end;
1156            if (RealType in [ttGroup, ttClass]) and (Children <> tcNone) and
1157              (dmodShared.InDialog(Node)) then
1158            begin
1159              cbOneItemOnly.Checked := OneItemOnly;
1160              cbIndent.Checked := IndentItems;
1161              if (RealType = ttGroup) and (Boilerplate <> '') then
1162              begin
1163                cbHideDlgItems.Checked := HideDlgItems;
1164              end
1165              else
1166              begin
1167                cbHideDlgItems.Checked := FALSE;
1168                cbHideDlgItems.Enabled := FALSE;
1169              end;
1170            end
1171            else
1172            begin
1173              cbOneItemOnly.Checked := FALSE;
1174              cbOneItemOnly.Enabled := FALSE;
1175              cbHideDlgItems.Checked := FALSE;
1176              cbHideDlgItems.Enabled := FALSE;
1177              cbIndent.Checked := FALSE;
1178              cbIndent.Enabled := FALSE;
1179            end;
1180            if (RealType = ttGroup) then
1181              edtGap.Text := IntToStr(Gap)
1182            else
1183            begin
1184              edtGap.Text := '0';
1185              edtGap.Enabled := FALSE;
1186              udGap.Enabled := FALSE;
1187              udGap.Invalidate;
1188              lblLines.Enabled := FALSE;
1189            end;
1190            DisplayBoilerplate(Node);
1191          end;
1192        end;
1193      end
1194      else
1195      begin
1196        ClearAll := TRUE;
1197        ClearRB := TRUE;
1198        ClearName := TRUE;
1199        gbProperties.Caption := PropText;
1200      end;
1201      if (ClearName) then
1202      begin
1203        edtName.Text := '';
1204        reNotes.Clear;
1205      end;
1206      if (ClearRB) then
1207      begin
1208        cbxType.ItemIndex := Ord(tiNone);
1209      end;
1210      if (ClearAll) then
1211      begin
1212        cbActive.Checked := FALSE;
1213        cbExclude.Checked := FALSE;
1214        cbDisplayOnly.Checked := FALSE;
1215        cbFirstLine.Checked := FALSE;
1216        cbOneItemOnly.Checked := FALSE;
1217        cbHideDlgItems.Checked := FALSE;
1218        cbHideItems.Checked := FALSE;
1219        cbIndent.Checked := FALSE;
1220        edtGap.Text := '0';
1221        reBoil.Clear;
1222        ShowGroupBoilerplate(False);
1223        pnlBoilerplateResize(Self);
1224        pnlCOM.Visible := FALSE;
1225        pnlLink.Visible := FALSE;
1226      end;
1227      if cbDisplayOnly.Enabled or
1228        cbFirstLine.Enabled or
1229        cbIndent.Enabled or
1230        cbOneItemOnly.Enabled or
1231        cbHideDlgItems.Enabled then
1232        gbDialogProps.Font.Color := clWindowText
1233      else
1234        gbDialogProps.Font.Color := clInactiveCaption;
1235    finally
1236      FUpdating := OldUpdating;
1237    end;
1238  end;
1239  
1240  procedure TfrmTemplateEditor.pnlBoilerplateResize(Sender: TObject);
1241  var
1242    Max: integer;
1243  
1244  begin
1245    if (pnlGroupBP.Visible) and (pnlGroupBP.Height > (pnlBoilerplate.Height - 29)) then
1246    begin
1247      pnlGroupBP.Height := pnlBoilerplate.Height - 29;
1248    end;
1249    if cbLongLines.checked then
1250      Max := 240
1251    else
1252      Max := MAX_ENTRY_WIDTH - 1;
1253    LimitEditWidth(reBoil, Max);
1254    LimitEditWidth(reNotes, MAX_ENTRY_WIDTH);
1255  end;
1256  
1257  procedure TfrmTemplateEditor.edtNameOldChange(Sender: TObject);
1258  var
1259    i: integer;
1260    Template: TTemplate;
1261    DoRefresh: boolean;
1262  
1263  begin
1264    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
1265    begin
1266      if CanClone(FCurTree.Selected) then
1267      begin
1268        DoRefresh := Clone(FCurTree.Selected);
1269        Template := TTemplate(FCurTree.Selected.Data);
1270        if assigned(Template) and Template.CanModify then
1271        begin
1272          Template.PrintName := edtName.Text;
1273          UpdateApply(Template);
1274          for i := 0 to Template.Nodes.Count - 1 do
1275            TTreeNode(Template.Nodes.Objects[i]).Text := Template.PrintName;
1276          if (DoRefresh) then
1277          begin
1278            tvShared.Invalidate;
1279            tvPersonal.Invalidate;
1280          end;
1281        end;
1282      end;
1283    end;
1284  end;
1285  
1286  procedure TfrmTemplateEditor.cbActiveClick(Sender: TObject);
1287  var
1288    i: integer;
1289    Template: TTemplate;
1290    Node: TTreeNode;
1291  
1292  begin
1293    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
1294    begin
1295      if CanClone(FCurTree.Selected) then
1296      begin
1297        Clone(FCurTree.Selected);
1298        Template := TTemplate(FCurTree.Selected.Data);
1299        if assigned(Template) and Template.CanModify then
1300        begin
1301          Template.Active := cbActive.Checked;
1302          UpdateApply(Template);
1303          for i := 0 to Template.Nodes.Count - 1 do
1304          begin
1305            Node := TTreeNode(Template.Nodes.Objects[i]);
1306            Node.Cut := not Template.Active;
1307          end;
1308          if (FCurTree = tvShared) then
1309          begin
1310            cbPerHideClick(Sender);
1311            cbShHideClick(Sender);
1312          end
1313          else
1314          begin
1315            cbShHideClick(Sender);
1316            cbPerHideClick(Sender);
1317          end;
1318          tvTreeChange(FCurTree, FCurTree.Selected);
1319          EnableNavControls;
1320          if cbActive.CanFocus then
1321            cbActive.SetFocus;
1322        end;
1323      end;
1324    end;
1325  end;
1326  
1327  procedure TfrmTemplateEditor.cbExcludeClick(Sender: TObject);
1328  var
1329    i: integer;
1330    Template: TTemplate;
1331    Node: TTreeNode;
1332  
1333  begin
1334    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
1335    begin
1336      if CanClone(FCurTree.Selected) then
1337      begin
1338        Clone(FCurTree.Selected);
1339        Template := TTemplate(FCurTree.Selected.Data);
1340        if assigned(Template) and Template.CanModify then
1341        begin
1342          Template.Exclude := cbExclude.Checked;
1343          UpdateApply(Template);
1344          for i := 0 to Template.Nodes.Count - 1 do
1345          begin
1346            Node := TTreeNode(Template.Nodes.Objects[i]);
1347            Node.ImageIndex := dmodShared.ImgIdx(Node);
1348            Node.SelectedIndex := dmodShared.ImgIdx(Node);
1349          end;
1350          tvShared.Invalidate;
1351          tvPersonal.Invalidate;
1352        end;
1353      end;
1354    end;
1355  end;
1356  
1357  procedure TfrmTemplateEditor.edtGapChange(Sender: TObject);
1358  var
1359    DoRefresh: boolean;
1360    Template: TTemplate;
1361  
1362  begin
1363    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
1364    begin
1365      if CanClone(FCurTree.Selected) then
1366      begin
1367        DoRefresh := Clone(FCurTree.Selected);
1368        Template := TTemplate(FCurTree.Selected.Data);
1369        if assigned(Template) and Template.CanModify then
1370        begin
1371          Template.Gap := StrToIntDef(edtGap.Text, 0);
1372          UpdateApply(Template);
1373          DisplayBoilerplate(FCurTree.Selected);
1374          if (DoRefresh) then
1375          begin
1376            tvShared.Invalidate;
1377            tvPersonal.Invalidate;
1378          end;
1379        end;
1380      end;
1381    end;
1382  end;
1383  
1384  function TfrmTemplateEditor.ChangeTree(NewTree: TTreeView): boolean;
1385  var
1386    i: TTemplateTreeControl;
1387  
1388  begin
1389    Result := FALSE;
1390    tvShared.HideSelection := TRUE;
1391    tvPersonal.HideSelection := TRUE;
1392    if (NewTree <> FCurTree) then
1393    begin
1394      Result := TRUE;
1395      if (assigned(FCurTree)) then
1396      begin
1397        for i := low(TTemplateTreeControl) to high(TTemplateTreeControl) do
1398          FTreeControl[TTemplateTreeType(FCurTree.Tag), i].Enabled := FALSE;
1399      end;
1400      FCurTree := NewTree;
1401    end;
1402    if (assigned(FCurTree)) then
1403    begin
1404      FCurTree.HideSelection := FALSE;
1405      if (FCurTree = tvPersonal) and (Screen.ActiveControl = tvShared) then
1406        tvPersonal.SetFocus
1407      else
1408        if (FCurTree = tvShared) and (Screen.ActiveControl = tvPersonal) then
1409          tvShared.SetFocus;
1410    end;
1411  end;
1412  
1413  procedure TfrmTemplateEditor.tvTreeEnter(Sender: TObject);
1414  begin
1415    if ((Sender is TTreeView) and (ChangeTree(TTreeView(Sender)))) then
1416      tvTreeChange(Sender, TTreeView(Sender).Selected);
1417  end;
1418  
1419  procedure TfrmTemplateEditor.tvTreeNodeEdited(Sender: TObject;
1420    Node: TTreeNode; var S: string);
1421  begin
1422    FUpdating := TRUE;
1423    try
1424      edtName.Text := S;
1425    finally
1426      FUpdating := FALSE;
1427    end;
1428    edtNameOldChange(edtName);
1429  end;
1430  
1431  procedure TfrmTemplateEditor.cbShHideClick(Sender: TObject);
1432  var
1433    Node: TTreeNode;
1434  
1435  begin
1436    Node := tvShared.Items.GetFirstNode;
1437    while assigned(Node) do
1438    begin
1439      dmodShared.Resync(Node, not cbShHide.Checked, FSharedEmptyNodeCount);
1440      Node := Node.getNextSibling;
1441    end;
1442    tvTreeChange(tvShared, tvShared.Selected);
1443    EnableNavControls;
1444  end;
1445  
1446  procedure TfrmTemplateEditor.cbPerHideClick(Sender: TObject);
1447  begin
1448    dmodShared.Resync(tvPersonal.Items.GetFirstNode, not cbPerHide.Checked,
1449      FPersonalEmptyNodeCount);
1450    tvTreeChange(tvPersonal, tvPersonal.Selected);
1451    EnableNavControls;
1452  end;
1453  
1454  procedure TfrmTemplateEditor.DisplayBoilerplate(Node: TTreeNode);
1455  var
1456    OldUpdating, ItemOK, BPOK, LongLines: boolean;
1457    i: integer;
1458    TmpSL: TStringList;
1459  
1460  begin
1461    OldUpdating := FUpdating;
1462    FUpdating := TRUE;
1463    try
1464      pnlBoilerplateResize(pnlBoilerplate);
1465      reBoil.Clear;
1466      ItemOK := FALSE;
1467      BPOK := TRUE;
1468      with Node, TTemplate(Node.Data) do
1469      begin
1470        if (RealType in [ttDoc, ttGroup]) then
1471        begin
1472          TmpSL := TStringList.Create;
1473          try
1474            if (RealType = ttGroup) and (not reBoil.ReadOnly) then
1475            begin
1476              ItemOK := TRUE;
1477              TmpSL.Text := Boilerplate;
1478              reGroupBP.Clear;
1479              reGroupBP.SelText := FullBoilerplate;
1480            end
1481            else
1482              TmpSL.Text := FullBoilerplate;
1483            LongLines := FALSE;
1484            for i := 0 to TmpSL.Count - 1 do
1485            begin
1486              if length(TmpSL[i]) > MAX_ENTRY_WIDTH then
1487              begin
1488                LongLines := TRUE;
1489                break;
1490              end;
1491            end;
1492            cbLongLines.Checked := LongLines;
1493            reBoil.SelText := TmpSL.Text;
1494          finally
1495            TmpSL.Free;
1496          end;
1497        end
1498        else
1499        begin
1500          reBoil.ReadOnly := TRUE;
1501          UpdateReadOnlyColorScheme(reBoil, TRUE);
1502          UpdateInsertsDialogs;
1503        end;
1504  
1505        if (not ItemOK) and (IsReminderDialog or IsCOMObject) then
1506          BPOK := FALSE;
1507  
1508        ShowBoilerPlate(BPOK);
1509        ShowGroupBoilerplate(ItemOK);
1510  
1511        pnlBoilerplateResize(Self);
1512        pnlCOM.Visible := (not BPOK) and IsCOMObject;
1513      end;
1514    finally
1515      FUpdating := OldUpdating;
1516    end;
1517  end;
1518  
1519  procedure TfrmTemplateEditor.FormDestroy(Sender: TObject);
1520  begin
1521    KillObj(@FConsultServices);
1522    Application.HintHidePause := FSavePause;
1523    if (assigned(frmTemplateObjects)) then
1524    begin
1525      frmTemplateObjects.Free;
1526      frmTemplateObjects := nil;
1527    end;
1528    if (assigned(frmTemplateFields)) then
1529    begin
1530      frmTemplateFields.Free;
1531      frmTemplateFields := nil;
1532    end;
1533    //---------- CQ #8665 - RV --------
1534    //KillObj(@FPersonalObjects);
1535    if (assigned(uPersonalObjects)) then
1536    begin
1537      KillObj(@uPersonalObjects);
1538      uPersonalObjects.Free;
1539      uPersonalObjects := nil;
1540    end;
1541    // ----  end CQ #8665 -------------
1542    dmodShared.OnTemplateLock := nil;
1543    dmodShared.InEditor := FALSE;
1544    RemoveAllNodes;
1545    ClearBackup;
1546    UnlockAllTemplates;
1547    dmodShared.Reload;
1548  end;
1549  
1550  procedure TfrmTemplateEditor.sbMoveUpClick(Sender: TObject);
1551  var
1552    idx: integer;
1553    ChangeLevel: boolean;
1554    ParentsParent, ParentNode, Node: TTreeNode;
1555    NodeTemplate, ParentTemplate, Template: TTemplate;
1556    Hide, First, ok: boolean;
1557  
1558  begin
1559    if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
1560      (assigned(FCurTree.Selected.Parent))) then
1561    begin
1562      Node := FCurTree.Selected;
1563      NodeTemplate := TTemplate(Node.Data);
1564      ParentNode := Node.Parent;
1565      Template := TTemplate(ParentNode.Data);
1566      idx := Template.Items.IndexOf(NodeTemplate);
1567      ChangeLevel := (idx < 1);
1568      if (not ChangeLevel) then
1569      begin
1570        if (TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
1571          Hide := cbShHide.Checked
1572        else
1573          Hide := cbPerHide.Checked;
1574        First := TRUE;
1575        while (idx > 0) do
1576        begin
1577          if First then
1578          begin
1579            ok := FALSE;
1580            First := FALSE;
1581            if CanClone(ParentNode) then
1582            begin
1583              if (Clone(ParentNode)) then
1584                Template := TTemplate(ParentNode.Data);
1585              if Template.CanModify then
1586                ok := TRUE;
1587            end;
1588          end
1589          else
1590            ok := TRUE;
1591          if ok then
1592          begin
1593            Template.Items.Exchange(idx - 1, idx);
1594            if (Hide and (not TTemplate(Template.Items[idx]).Active)) then
1595            begin
1596              dec(idx);
1597              ChangeLevel := (idx < 1);
1598            end
1599            else
1600              idx := 0;
1601          end
1602          else
1603            idx := 0;
1604        end;
1605      end;
1606      if (ChangeLevel) then
1607      begin
1608        ParentsParent := ParentNode.Parent;
1609        if (assigned(ParentsParent)) then
1610        begin
1611          ParentTemplate := TTemplate(ParentsParent.Data);
1612          if (ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
1613            InfoBox(ParentsParent.Text + ' already contains the ' +
1614              NodeTemplate.PrintName + ' template.',
1615              'Error', MB_OK or MB_ICONERROR)
1616          else
1617          begin
1618            if CanClone(ParentNode) then
1619            begin
1620              if (Clone(ParentNode)) then
1621                Template := TTemplate(ParentNode.Data);
1622              if Template.CanModify and CanClone(ParentsParent) then
1623              begin
1624                if (Clone(ParentsParent)) then
1625                  ParentTemplate := TTemplate(ParentsParent.Data);
1626                if ParentTemplate.CanModify then
1627                begin
1628                  Template.Items.Delete(idx);
1629                  idx := ParentTemplate.Items.IndexOf(Template);
1630                  if (idx >= 0) then
1631                  begin
1632                    ParentTemplate.Items.Insert(idx, NodeTemplate);
1633                    Resync([ParentTemplate, Template]);
1634                    btnApply.Enabled := TRUE;
1635                  end;
1636                end;
1637              end;
1638            end;
1639          end;
1640        end;
1641      end
1642      else
1643      begin
1644        Resync([Template]);
1645        btnApply.Enabled := TRUE;
1646      end;
1647    end;
1648  end;
1649  
1650  procedure TfrmTemplateEditor.sbMoveDownClick(Sender: TObject);
1651  var
1652    max, idx: integer;
1653    ChangeLevel: boolean;
1654    ParentsParent, ParentNode, Node: TTreeNode;
1655    NodeTemplate, ParentTemplate, Template: TTemplate;
1656    Hide, First, ok: boolean;
1657  
1658  begin
1659    if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
1660      (assigned(FCurTree.Selected.Parent))) then
1661    begin
1662      Node := FCurTree.Selected;
1663      NodeTemplate := TTemplate(Node.Data);
1664      ParentNode := Node.Parent;
1665      Template := TTemplate(ParentNode.Data);
1666      idx := Template.Items.IndexOf(NodeTemplate);
1667      max := Template.Items.Count - 1;
1668      ChangeLevel := (idx >= max);
1669      if (not ChangeLevel) then
1670      begin
1671        if (TTemplateTreeType(TBitBtn(Sender).Tag) = ttShared) then
1672          Hide := cbShHide.Checked
1673        else
1674          Hide := cbPerHide.Checked;
1675        First := TRUE;
1676        while (idx < max) do
1677        begin
1678          if First then
1679          begin
1680            ok := FALSE;
1681            First := FALSE;
1682            if CanClone(ParentNode) then
1683            begin
1684              if (Clone(ParentNode)) then
1685                Template := TTemplate(ParentNode.Data);
1686              if Template.CanModify then
1687                ok := TRUE;
1688            end;
1689          end
1690          else
1691            ok := TRUE;
1692          if ok then
1693          begin
1694            Template.Items.Exchange(idx, idx + 1);
1695            if (Hide and (not TTemplate(Template.Items[idx]).Active)) then
1696            begin
1697              inc(idx);
1698              ChangeLevel := (idx >= max);
1699            end
1700            else
1701              idx := max;
1702          end
1703          else
1704            idx := max;
1705        end;
1706      end;
1707      if (ChangeLevel) then
1708      begin
1709        ParentsParent := ParentNode.Parent;
1710        if (assigned(ParentsParent)) then
1711        begin
1712          ParentTemplate := TTemplate(ParentsParent.Data);
1713          if (ParentTemplate.Items.IndexOf(NodeTemplate) >= 0) then
1714            InfoBox(ParentsParent.Text + ' already contains the ' +
1715              NodeTemplate.PrintName + ' template.',
1716              'Error', MB_OK or MB_ICONERROR)
1717          else
1718          begin
1719            if CanClone(ParentNode) then
1720            begin
1721              if (Clone(ParentNode)) then
1722                Template := TTemplate(ParentNode.Data);
1723              if Template.CanModify and CanClone(ParentsParent) then
1724              begin
1725                if (Clone(ParentsParent)) then
1726                  ParentTemplate := TTemplate(ParentsParent.Data);
1727                if ParentTemplate.CanModify then
1728                begin
1729                  Template.Items.Delete(idx);
1730                  idx := ParentTemplate.Items.IndexOf(Template);
1731                  if (idx >= 0) then
1732                  begin
1733                    if (idx = (ParentTemplate.Items.Count - 1)) then
1734                      ParentTemplate.Items.Add(NodeTemplate)
1735                    else
1736                      ParentTemplate.Items.Insert(idx + 1, NodeTemplate);
1737                    Resync([ParentTemplate, Template]);
1738                    btnApply.Enabled := TRUE;
1739                  end;
1740                end;
1741              end;
1742            end;
1743          end;
1744        end;
1745      end
1746      else
1747      begin
1748        Resync([Template]);
1749        btnApply.Enabled := TRUE;
1750      end;
1751    end;
1752  end;
1753  
1754  procedure TfrmTemplateEditor.sbDeleteClick(Sender: TObject);
1755  var
1756    PNode, Node: TTreeNode;
1757    Template, Parent: TTemplate;
1758    DoIt: boolean;
1759    Answer: Word;
1760  
1761  begin
1762    if ((assigned(FCurTree)) and (assigned(FCurTree.Selected)) and
1763      (assigned(FCurTree.Selected.Parent))) then
1764    begin
1765      Node := FCurTree.Selected;
1766      Template := TTemplate(Node.Data);
1767      PNode := Node.Parent;
1768      Parent := TTemplate(PNode.Data);
1769      if (AutoDel(Template)) then
1770        DoIt := TRUE
1771      else
1772        if (Template.Active) and (cbActive.Checked) then
1773        begin
1774          DoIt := FALSE;
1775          Answer := MessageDlg('Once you delete a template you may not be able to retrieve it.' + CRLF +
1776            'Rather than deleting, you may want to inactivate a template instead.' + CRLF +
1777            'You may inactivate this template by pressing the Ignore button now.' + CRLF +
1778            'Are you sure you want to delete the "' + Node.Text +
1779            '" Template?', mtConfirmation, [mbYes, mbNo, mbIgnore], 0);
1780          if (Answer = mrYes) then
1781            DoIt := TRUE
1782          else
1783            if (Answer = mrIgnore) then
1784              cbActive.Checked := FALSE;
1785        end
1786        else
1787          DoIt := InfoBox('Are you sure you want to delete the "' + Node.Text +
1788            '" Template?', 'Confirmation', MB_YESNO or MB_ICONQUESTION) = IDYES;
1789      if (DoIt and CanClone(PNode)) then
1790      begin
1791        if (Clone(PNode)) then
1792          Parent := TTemplate(PNode.Data);
1793        if assigned(Parent) and Parent.CanModify then
1794        begin
1795          btnApply.Enabled := TRUE;
1796          Parent.RemoveChild(Template);
1797          MarkDeleted(Template);
1798          Resync([Parent]);
1799          tvTreeChange(FCurTree, FCurTree.Selected);
1800        end;
1801      end;
1802    end;
1803  end;
1804  
1805  procedure TfrmTemplateEditor.EnableNavControls;
1806  var
1807    i: TTemplateTreeControl;
1808    AllowUp, AllowDown, AllowSet: boolean;
1809    Node: TTreeNode;
1810    Tree: TTemplateTreeType;
1811    Curok: boolean;
1812    OldActiveControl: TControl;
1813  begin
1814    if (Assigned(FCurTree)) then
1815    begin
1816      Tree := TTemplateTreeType(FCurTree.Tag);
1817      Node := FCurTree.Selected;
1818      if (Assigned(Node)) then
1819        Curok := (TTemplate(Node.Data).RealType in [ttDoc, ttGroup, ttClass])
1820      else
1821        Curok := FALSE;
1822      if (Curok) then
1823      begin
1824        OldActiveControl := ActiveControl;
1825        FTreeControl[Tree, tcDel].Enabled := TRUE;
1826        AllowSet := FALSE;
1827        if (Node.Index > 0) then
1828          AllowUp := TRUE
1829        else
1830        begin
1831          AllowUp := AllowMove(Node.Parent.Parent, Node);
1832          AllowSet := TRUE;
1833        end;
1834        FTreeControl[Tree, tcUp].Enabled := AllowUp;
1835        AllowDown := AllowUp;
1836        if (Node.Index < (Node.Parent.Count - 1)) then
1837          AllowDown := TRUE
1838        else
1839        begin
1840          if (not AllowSet) then
1841            AllowDown := AllowMove(Node.Parent.Parent, Node);
1842        end;
1843        FTreeControl[Tree, tcDown].Enabled := AllowDown;
1844        if not AllowUp and (OldActiveControl = FTreeControl[Tree, tcUp]) then
1845          (FTreeControl[Tree, tcDown] as TWinControl).SetFocus;
1846        if not AllowDown and (OldActiveControl = FTreeControl[Tree, tcDown]) then
1847          (FTreeControl[Tree, tcUp] as TWinControl).SetFocus;
1848        FTreeControl[Tree, tcCopy].Enabled := FTreeControl[TTemplateTreeType(1 - ord(Tree)), tcDel].Visible;
1849        if (FTreeControl[Tree, tcCopy].Enabled) then
1850        begin
1851          if (Tree = ttShared) then
1852            Node := tvPersonal.Selected
1853          else
1854            Node := tvShared.Selected;
1855          if (assigned(Node)) then
1856          begin
1857            if (TTemplate(Node.Data).RealType = ttDoc) then
1858              Node := Node.Parent;
1859            FTreeControl[Tree, tcCopy].Enabled := AllowMove(Node, FCurTree.Selected);
1860          end
1861          else
1862            FTreeControl[Tree, tcCopy].Enabled := FALSE;
1863        end;
1864        FTreeControl[Tree, tcLbl].Enabled := FTreeControl[Tree, tcCopy].Enabled;
1865      end
1866      else
1867      begin
1868        for i := low(TTemplateTreeControl) to high(TTemplateTreeControl) do
1869          FTreeControl[Tree, i].Enabled := FALSE;
1870      end;
1871      if (FCurTree = tvShared) and (FCanEditShared) then
1872        btnNew.Enabled := TRUE
1873      else
1874        if (FCurTree = tvPersonal) and (FCanEditPersonal) then
1875          btnNew.Enabled := TRUE
1876        else
1877          btnNew.Enabled := FALSE;
1878    end
1879    else
1880      btnNew.Enabled := FALSE;
1881  end;
1882  
1883  procedure TfrmTemplateEditor.tvTreeDragging(Sender: TObject;
1884    Node: TTreeNode; var CanDrag: Boolean);
1885  
1886  begin
1887    CanDrag := (TTemplate(Node.Data).RealType in [ttDoc, ttGroup, ttClass]);
1888    if (CanDrag) then
1889      FDragNode := Node
1890    else
1891      FDragNode := nil;
1892  end;
1893  
1894  procedure TfrmTemplateEditor.tvTreeDragOver(Sender, Source: TObject; X,
1895    Y: Integer; State: TDragState; var Accept: Boolean);
1896  var
1897    TmpNode: TTreeNode;
1898    Tree: TTreeView;
1899  
1900  begin
1901    FDropNode := nil;
1902    Accept := FALSE;
1903    if (Source is TTreeView) and (assigned(FDragNode)) then
1904    begin
1905      Tree := TTreeView(Sender);
1906      FDropNode := Tree.GetNodeAt(X, Y);
1907      if (((Tree = tvShared) and (FCanEditShared)) or
1908        ((Tree = tvPersonal) and (FCanEditPersonal))) then
1909      begin
1910        if (assigned(FDropNode)) then
1911        begin
1912          FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
1913          if (FDropInto) then
1914            TmpNode := FDropNode
1915          else
1916            TmpNode := FDropNode.Parent;
1917          Accept := AllowMove(TmpNode, FDragNode);
1918        end;
1919      end;
1920    end;
1921  end;
1922  
1923  procedure TfrmTemplateEditor.tvTreeDragDrop(Sender, Source: TObject; X,
1924    Y: Integer);
1925  var
1926    Src, Template, Item: TTemplate;
1927    SIdx, idx: integer;
1928    TmpNode: TTreeNode;
1929  
1930  begin
1931    if (assigned(FDragNode)) and (assigned(FDropNode)) and (FDragNode <> FDropNode) then
1932    begin
1933      Item := TTemplate(FDragNode.Data);
1934      if (FDropInto) then
1935      begin
1936        TmpNode := FDropNode;
1937        idx := 0;
1938      end
1939      else
1940      begin
1941        TmpNode := FDropNode.Parent;
1942        idx := TTemplate(FDropNode.Parent.Data).Items.IndexOf(FDropNode.Data);
1943      end;
1944      if (AllowMove(TmpNode, FDragNode) and (idx >= 0)) then
1945      begin
1946        Template := TTemplate(TmpNode.Data);
1947        if (Template <> FDragNode.Parent.Data) and
1948          (Template.Items.IndexOf(Item) >= 0) then
1949          InfoBox(Template.PrintName + ' already contains the ' +
1950            Item.PrintName + ' template.',
1951            'Error', MB_OK or MB_ICONERROR)
1952        else
1953        begin
1954          Src := TTemplate(FDragNode.Parent.Data);
1955          Sidx := Src.Items.IndexOf(Item);
1956          if CanClone(TmpNode) then
1957          begin
1958            if (Clone(TmpNode)) then
1959              Template := TTemplate(TmpNode.Data);
1960            if assigned(Template) and Template.CanModify then
1961            begin
1962              if (Sidx >= 0) and (FDragNode.TreeView = FDropNode.TreeView) and
1963                (not FCopying) then // if same tree delete source
1964              begin
1965                if CanClone(FDragNode.Parent) then
1966                begin
1967                  if (Clone(FDragNode.Parent)) then
1968                    Src := TTemplate(FDragNode.Parent.Data);
1969                  if assigned(Src) and Src.CanModify then
1970                  begin
1971                    Src.Items.Delete(Sidx);
1972                    if (Template = Src) then
1973                      Src := nil;
1974                  end
1975                  else
1976                    Src := nil;
1977                end
1978                else
1979                  Src := nil;
1980              end
1981              else
1982                Src := nil;
1983              if (idx > 0) then
1984                idx := TTemplate(FDropNode.Parent.Data).Items.IndexOf(FDropNode.Data);
1985              Template.Items.Insert(idx, Item);
1986              if (TTreeView(FDropNode.TreeView) = tvShared) then
1987              begin
1988                Item.PersonalOwner := 0;
1989                tvPersonal.Invalidate;
1990              end;
1991              TTreeView(FDragNode.TreeView).Selected := FDragNode;
1992              TTreeView(FDragNode.TreeView).SetFocus;
1993              Resync([Src, Template]);
1994              btnApply.Enabled := TRUE;
1995            end;
1996          end;
1997        end;
1998      end;
1999    end;
2000  end;
2001  
2002  procedure TfrmTemplateEditor.Resync(const Templates: array of TTemplate);
2003  var
2004    i, j: integer;
2005    NodeList: TStringList;
2006    TemplateList: TStringList;
2007    Node: TTreeNode;
2008    tmpl: TTemplate;
2009    NodeID: string;
2010  
2011  begin
2012    NodeList := TStringList.Create;
2013    try
2014      TemplateList := TStringList.Create;
2015      try
2016        for i := low(Templates) to high(Templates) do
2017        begin
2018          tmpl := Templates[i];
2019          if (assigned(tmpl)) then
2020          begin
2021            for j := 0 to tmpl.Nodes.Count - 1 do
2022            begin
2023              Node := TTreeNode(tmpl.Nodes.Objects[j]);
2024              if (NodeList.IndexOfObject(Node) < 0) then
2025              begin
2026                NodeID := IntToStr(Node.Level);
2027                NodeID := copy('000', 1, 4 - length(NodeID)) + NodeID + U + tmpl.Nodes[j];
2028                TemplateList.AddObject(NodeID, tmpl);
2029                NodeList.AddObject(NodeId, Node);
2030              end;
2031            end;
2032          end;
2033        end;
2034  
2035      { By Sorting by Node Level, we prevent a Resync
2036        of nodes deeper within the heirchary }
2037  
2038        NodeList.Sort;
2039  
2040        for i := 0 to NodeList.Count - 1 do
2041        begin
2042          NodeID := NodeList[i];
2043          Node := TTreeNode(NodeList.Objects[i]);
2044          j := TemplateList.IndexOf(NodeID);
2045          if (j >= 0) then
2046          begin
2047            tmpl := TTemplate(TemplateList.Objects[j]);
2048            NodeID := Piece(NodeID, U, 2);
2049            if (tmpl.Nodes.IndexOf(NodeID) >= 0) then
2050            begin
2051              if (Node.TreeView = tvShared) then
2052                dmodShared.Resync(Node, not cbShHide.Checked, FSharedEmptyNodeCount)
2053              else
2054                if (Node.TreeView = tvPersonal) then
2055                  dmodShared.Resync(Node, not cbPerHide.Checked, FPersonalEmptyNodeCount);
2056            end;
2057          end;
2058        end;
2059      finally
2060        TemplateList.Free;
2061      end;
2062    finally
2063      NodeList.Free;
2064    end;
2065    EnableNavControls;
2066    if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
2067      tvTreeChange(FCurTree, FCurTree.Selected)
2068    else
2069      tvPersonal.Selected := tvPersonal.Items.GetFirstNode;
2070    FCopyNode := nil;
2071  end;
2072  
2073  procedure TfrmTemplateEditor.sbCopyLeftClick(Sender: TObject);
2074  begin
2075    if (assigned(tvPersonal.Selected)) then
2076    begin
2077      if (not assigned(tvShared.Selected)) then
2078        tvShared.Selected := tvShared.Items.GetFirstNode;
2079      FDragNode := tvPersonal.Selected;
2080      FDropNode := tvShared.Selected;
2081      FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
2082      tvTreeDragDrop(tvPersonal, tvShared, 0, 0);
2083    end;
2084  end;
2085  
2086  procedure TfrmTemplateEditor.sbCopyRightClick(Sender: TObject);
2087  begin
2088    if (assigned(tvShared.Selected)) then
2089    begin
2090      if (not assigned(tvPersonal.Selected)) then
2091        tvPersonal.Selected := tvPersonal.Items.GetFirstNode;
2092      FDragNode := tvShared.Selected;
2093      FDropNode := tvPersonal.Selected;
2094      FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
2095      tvTreeDragDrop(tvShared, tvPersonal, 0, 0);
2096    end;
2097  end;
2098  
2099  procedure TfrmTemplateEditor.AdjustControls4FontChange;
2100  var
2101    x: integer;
2102  
2103    procedure Adjust(Control: TWinControl);
2104    begin
2105      x := x - Control.Width - 2;
2106      Control.Left := x;
2107    end;
2108  
2109  begin
2110    if FCanEditShared then
2111    begin
2112      x := pnlSharedBottom.Width;
2113      Adjust(sbSHDelete);
2114      Adjust(sbSHDown);
2115      Adjust(sbSHUp);
2116      cbSHHide.Width := x;
2117    end;
2118    x := pnlBottom.Width;
2119    Adjust(btnApply);
2120    Adjust(btnCancel);
2121    Adjust(btnOK);
2122    cbEditShared.Width := TextWidthByFont(cbEditShared.Font.Handle, cbEditShared.Caption) + 25;
2123    cbNotes.Left := cbEditShared.Left + cbEditShared.Width + 60;
2124    cbNotes.Width := TextWidthByFont(cbNotes.Font.Handle, cbNotes.Caption) + 25;
2125  end;
2126  
2127  function TfrmTemplateEditor.AllowMove(ADropNode, ADragNode: TTreeNode): boolean;
2128  var
2129    i: integer;
2130    TmpNode: TTreeNode;
2131    DragTemplate, DropTemplate: TTemplate;
2132  
2133  begin
2134    if (assigned(ADropNode) and assigned(ADragNode)) then
2135    begin
2136      DropTemplate := TTemplate(ADropNode.Data);
2137      DragTemplate := TTemplate(ADragNode.Data);
2138      if IsTemplateLocked(ADropNode) then
2139        Result := FALSE
2140      else
2141        Result := (DragTemplate.RealType in [ttDoc, ttGroup, ttClass]);
2142      if (Result) then
2143      begin
2144        if (FCopying) then
2145        begin
2146          if (DropTemplate.Items.IndexOf(DragTemplate) >= 0) then
2147            Result := FALSE;
2148        end
2149        else
2150          if ((assigned(ADragNode.Parent)) and (ADropNode <> ADragNode.Parent) and
2151            (DropTemplate.Items.IndexOf(DragTemplate) >= 0)) then
2152            Result := FALSE;
2153      end;
2154      if (Result) then
2155      begin
2156        for i := 0 to DropTemplate.Nodes.Count - 1 do
2157        begin
2158          TmpNode := TTreeNode(DropTemplate.Nodes.Objects[i]);
2159          while (Result and (assigned(TmpNode.Parent))) do
2160          begin
2161            if (TmpNode.Data = DragTemplate) then
2162              Result := FALSE
2163            else
2164              TmpNode := TmpNode.Parent;
2165          end;
2166          if (not Result) then break;
2167        end;
2168      end;
2169    end
2170    else
2171      Result := FALSE;
2172  end;
2173  
2174  function TfrmTemplateEditor.Clone(Node: TTreeNode): boolean;
2175  var
2176    idx: integer;
2177    Prnt, OldT, NewT: TTemplate;
2178    PNode: TTreeNode;
2179    ok: boolean;
2180  
2181  begin
2182    Result := FALSE;
2183    if ((assigned(Node)) and (TTreeView(Node.TreeView) = tvPersonal)) then
2184    begin
2185      OldT := TTemplate(Node.Data);
2186      if (OldT.PersonalOwner <> User.DUZ) then
2187      begin
2188        PNode := Node.Parent;
2189        Prnt := nil;
2190        if (assigned(PNode)) then
2191        begin
2192          ok := CanClone(PNode);
2193          if ok then
2194          begin
2195            Clone(PNode);
2196            Prnt := TTemplate(PNode.Data);
2197            ok := Prnt.CanModify;
2198          end;
2199        end
2200        else
2201          ok := TRUE;
2202        if ok then
2203        begin
2204          BtnApply.Enabled := TRUE;
2205          Result := TRUE;
2206          NewT := OldT.Clone(User.DUZ);
2207          OldT.RemoveNode(Node);
2208          MarkDeleted(OldT);
2209          Node.Data := NewT;
2210          NewT.AddNode(Node);
2211          if (assigned(Prnt)) then
2212          begin
2213            idx := Prnt.Items.IndexOf(OldT);
2214            if (idx >= 0) then
2215              Prnt.Items[idx] := NewT;
2216          end;
2217          tvPersonal.Invalidate;
2218          ShowTemplateType(NewT);
2219        end;
2220      end;
2221    end;
2222  end;
2223  
2224  procedure TfrmTemplateEditor.reBoilChange(Sender: TObject);
2225  var
2226    DoInfo, DoRefresh: boolean;
2227    TmpBPlate: string;
2228    Template: TTemplate;
2229    x: integer;
2230  
2231  begin
2232    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
2233    begin
2234      Template := TTemplate(FCurTree.Selected.Data);
2235      TmpBPlate := reBoil.Lines.Text;
2236      if (Template.Boilerplate <> TmpBPlate) then
2237      begin
2238        if CanClone(FCurTree.Selected) then
2239        begin
2240          DoRefresh := Clone(FCurTree.Selected);
2241          if (DoRefresh) then
2242            Template := TTemplate(FCurTree.Selected.Data);
2243          if assigned(Template) and Template.CanModify then
2244          begin
2245            DoInfo := FALSE;
2246            if (Template.Boilerplate = '') or (TmpBPlate = '') then
2247              DoInfo := TRUE;
2248            Template.Boilerplate := TmpBPlate;
2249            TTemplate(FCurTree.Selected.Data).Gap := StrToIntDef(edtGap.Text, 0);
2250            if (Template.RealType = ttGroup) then
2251            begin
2252              reGroupBP.Text := Template.FullBoilerplate;
2253            end;
2254            if (DoRefresh) then
2255            begin
2256              tvShared.Invalidate;
2257              tvPersonal.Invalidate;
2258            end;
2259            if (DoInfo) then
2260            begin
2261              x := reBoil.SelStart;
2262              ShowInfo(FCurTree.Selected);
2263              reBoil.SelStart := x;
2264            end;
2265          end;
2266        end;
2267        btnApply.Enabled := TRUE;
2268  //        reBoil.Lines.Text := Template.Boilerplate;
2269      end;
2270    end;
2271  end;
2272  
2273  procedure TfrmTemplateEditor.SharedEditing;
2274  begin
2275  {$IFDEF OwnerScan}
2276    lblPerOwner.Visible := FCanEditShared;
2277    cboOwner.Visible := FCanEditShared;
2278  {$ELSE}
2279    lblPerOwner.Visible := FALSE;
2280    cboOwner.Visible := FALSE;
2281  {$ENDIF}
2282    sbCopyLeft.Visible := FCanEditShared;
2283    if (not FCanEditShared) then
2284      cbShHide.Checked := TRUE;
2285    cbShHide.Visible := FCanEditShared;
2286    sbShDelete.Visible := FCanEditShared;
2287    sbShUp.Visible := FCanEditShared;
2288    sbShDown.Visible := FCanEditShared;
2289    tvShared.ReadOnly := not FCanEditShared;
2290    MoveCopyButtons;
2291    tvTreeChange(FCurTree, FCurTree.Selected);
2292    if FCanEditShared then
2293      AdjustControls4FontChange;
2294  end;
2295  
2296  procedure TfrmTemplateEditor.cbEditSharedClick(Sender: TObject);
2297  begin
2298    FCanEditShared := cbEditShared.Checked;
2299    SharedEditing;
2300  end;
2301  
2302  procedure TfrmTemplateEditor.popTemplatesPopup(Sender: TObject);
2303  var
2304    Tree: TTreeView;
2305    Node: TTreeNode;
2306    FindOn: boolean;
2307    Txt: string;
2308  
2309  begin
2310    FFromMainMenu := FALSE;
2311    Tree := GetTree;
2312    Node := Tree.Selected;
2313    Tree.Selected := Node; // This line prevents selected from changing after menu closes
2314    mnuCollapseTree.Enabled := dmodShared.NeedsCollapsing(Tree);
2315    if (Tree = tvShared) then
2316    begin
2317      Txt := 'Shared';
2318      FindOn := FFindShOn;
2319      mnuNodeDelete.Enabled := ((sbShDelete.Visible) and (sbShDelete.Enabled));
2320    end
2321    else
2322    begin
2323      Txt := 'Personal';
2324      FindOn := FFindPerOn;
2325      mnuNodeDelete.Enabled := ((sbPerDelete.Visible) and (sbPerDelete.Enabled));
2326    end;
2327    mnuFindTemplates.Checked := FindOn;
2328    mnuCollapseTree.Caption := 'Collapse ' + Txt + ' &Tree';
2329    mnuFindTemplates.Caption := '&Find ' + Txt + ' Templates';
2330  
2331    if (assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
2332    begin
2333      mnuNodeCopy.Enabled := (TTemplate(Tree.Selected.Data).RealType in [ttDoc, ttGroup, ttClass]);
2334      mnuNodeSort.Enabled := (TTemplate(Tree.Selected.Data).RealType in AllTemplateFolderTypes) and
2335        (Tree.Selected.HasChildren) and
2336        (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
2337    end
2338    else
2339    begin
2340      mnuNodeCopy.Enabled := FALSE;
2341      mnuNodeSort.Enabled := FALSE;
2342    end;
2343    FPasteNode := Tree.Selected;
2344    mnuNodePaste.Enabled := PasteOK;
2345    mnuNodeNew.Enabled := btnNew.Enabled;
2346    mnuNodeAutoGen.Enabled := btnNew.Enabled;
2347  end;
2348  
2349  procedure TfrmTemplateEditor.mnuCollapseTreeClick(Sender: TObject);
2350  begin
2351    if (GetTree = tvShared) then
2352    begin
2353      tvShared.Selected := tvShared.Items.GetFirstNode;
2354      tvShared.FullCollapse;
2355    end
2356    else
2357    begin
2358      tvPersonal.Selected := tvShared.Items.GetFirstNode;
2359      tvPersonal.FullCollapse;
2360    end;
2361  end;
2362  
2363  procedure TfrmTemplateEditor.mnuFindTemplatesClick(Sender: TObject);
2364  var
2365    Tree: TTreeView;
2366  
2367  begin
2368    Tree := GetTree;
2369    if (Tree = tvShared) then
2370    begin
2371      FFindShOn := not FFindShOn;
2372      pnlShSearch.Visible := FFindShOn;
2373      if (FFindShOn) then
2374      begin
2375        edtShSearch.SetFocus;
2376        btnShFind.Enabled := (edtShSearch.Text <> '');
2377      end;
2378    end
2379    else
2380    begin
2381      FFindPerOn := not FFindPerOn;
2382      pnlPerSearch.Visible := FFindPerOn;
2383      if (FFindPerOn) then
2384      begin
2385        edtPerSearch.SetFocus;
2386        btnPerFind.Enabled := (edtPerSearch.Text <> '');
2387      end;
2388    end;
2389    SetFindNext(Tree, FALSE);
2390  end;
2391  
2392  procedure TfrmTemplateEditor.ShowTemplateType(Template: TTemplate);
2393  begin
2394    if (Template.PersonalOwner > 0) then
2395      gbProperties.Caption := 'Personal'
2396    else
2397      gbProperties.Caption := 'Shared';
2398    gbProperties.Caption := gbProperties.Caption + PropText;
2399  end;
2400  
2401  function TfrmTemplateEditor.GetTree: TTreeView;
2402  begin
2403    if (FFromMainMenu) then
2404      Result := FMainMenuTree
2405    else
2406    begin
2407      if (TTemplateTreeType(PopupComponent(popTemplates, popTemplates).Tag) = ttShared) then
2408        Result := tvShared
2409      else
2410        Result := tvPersonal;
2411    end;
2412  end;
2413  
2414  procedure TfrmTemplateEditor.btnFindClick(Sender: TObject);
2415  var
2416    Found: TTreeNode;
2417    edtSearch: TEdit;
2418    IsNext: boolean;
2419    FindNext: boolean;
2420    FindWholeWords: boolean;
2421    FindCase: boolean;
2422    Tree: TTreeView;
2423    LastFoundNode, TmpNode: TTreeNode;
2424  //  S1,S2: string;
2425  
2426  begin
2427    if (TTemplateTreeType(TButton(Sender).Tag) = ttShared) then
2428    begin
2429      Tree := tvShared;
2430      edtSearch := edtShSearch;
2431      FindNext := FFindShNext;
2432      FindWholeWords := cbShWholeWords.Checked;
2433      FindCase := cbShMatchCase.Checked;
2434      LastFoundNode := FLastFoundShNode;
2435    end
2436    else
2437    begin
2438      Tree := tvPersonal;
2439      edtSearch := edtPerSearch;
2440      FindNext := FFindPerNext;
2441      FindWholeWords := cbPerWholeWords.Checked;
2442      FindCase := cbPerMatchCase.Checked;
2443      LastFoundNode := FLastFoundPerNode;
2444    end;
2445    if (edtSearch.text <> '') then
2446    begin
2447      IsNext := ((FindNext) and assigned(LastFoundNode));
2448      if IsNext then
2449  
2450        TmpNode := LastFoundNode
2451      else
2452        TmpNode := Tree.Items.GetFirstNode;
2453      FInternalHiddenExpand := TRUE;
2454      try
2455        Found := FindTemplate(edtSearch.Text, Tree, Self, TmpNode,
2456          IsNext, not FindCase, FindWholeWords);
2457      finally
2458        FInternalHiddenExpand := FALSE;
2459      end;
2460      if Assigned(Found) then
2461      begin
2462        Tree.Selected := Found;
2463        if (Tree = tvShared) then
2464          FLastFoundShNode := Found
2465        else
2466          FLastFoundPerNode := Found;
2467        SetFindNext(Tree, TRUE);
2468      end;
2469    end;
2470    edtSearch.SetFocus;
2471  end;
2472  
2473  procedure TfrmTemplateEditor.edtSearchChange(Sender: TObject);
2474  begin
2475    if (TTemplateTreeType(TEdit(Sender).Tag) = ttShared) then
2476    begin
2477      btnShFind.Enabled := (edtShSearch.Text <> '');
2478      SetFindNext(tvShared, FALSE);
2479    end
2480    else
2481    begin
2482      btnPerFind.Enabled := (edtPerSearch.Text <> '');
2483      SetFindNext(tvPersonal, FALSE);
2484    end;
2485  end;
2486  
2487  procedure TfrmTemplateEditor.SetFindNext(const Tree: TTreeView; const Value: boolean);
2488  begin
2489    if (Tree = tvShared) then
2490    begin
2491      if (FFindShNext <> Value) then
2492      begin
2493        FFindShNext := Value;
2494        if (FFindShNext) then btnShFind.Caption := 'Find Next'
2495        else btnShFind.Caption := 'Find';
2496      end;
2497    end
2498    else
2499    begin
2500      if (FFindPerNext <> Value) then
2501      begin
2502        FFindPerNext := Value;
2503        if (FFindPerNext) then btnPerFind.Caption := 'Find Next'
2504        else btnPerFind.Caption := 'Find';
2505      end;
2506    end;
2507  end;
2508  
2509  procedure TfrmTemplateEditor.edtShSearchEnter(Sender: TObject);
2510  begin
2511    btnShFind.Default := TRUE;
2512  end;
2513  
2514  procedure TfrmTemplateEditor.edtShSearchExit(Sender: TObject);
2515  begin
2516    btnShFind.Default := FALSE;
2517  end;
2518  
2519  procedure TfrmTemplateEditor.edtPerSearchEnter(Sender: TObject);
2520  begin
2521    btnPerFind.Default := TRUE;
2522  end;
2523  
2524  procedure TfrmTemplateEditor.edtPerSearchExit(Sender: TObject);
2525  begin
2526    btnPerFind.Default := FALSE;
2527  end;
2528  
2529  procedure TfrmTemplateEditor.btnOKClick(Sender: TObject);
2530  begin
2531    if (ScanNames) then
2532    begin
2533      if (SaveAllTemplates) then
2534      begin
2535        FOK2Close := TRUE;
2536        ModalResult := mrOK;
2537      end
2538      else
2539        BtnApply.Enabled := BackupDiffers;
2540    end;
2541  end;
2542  
2543  procedure TfrmTemplateEditor.FormShow(Sender: TObject);
2544  begin
2545    if (FFirstShow) then
2546    begin
2547      FUpdating := FALSE;
2548      FFirstShow := FALSE;
2549      if (FFocusName) then
2550      begin
2551        edtName.SetFocus;
2552        edtName.SelectAll;
2553      end;
2554      pnlBoilerplateResize(Self);
2555      AdjustControls4FontChange;
2556      MoveCopyButtons;
2557    end;
2558  end;
2559  
2560  procedure TfrmTemplateEditor.mnuBPInsertObjectClick(Sender: TObject);
2561  var
2562    i: integer;
2563    DoIt: boolean;
2564  
2565  begin
2566    if (not assigned(frmTemplateObjects)) then
2567    begin
2568      dmodShared.LoadTIUObjects;
2569      frmTemplateObjects := TfrmTemplateObjects.Create(Self);
2570      DoIt := TRUE;
2571      if (UserTemplateAccessLevel <> taEditor) then
2572      begin
2573        UpdatePersonalObjects;
2574        if uPersonalObjects.Count > 0 then // -------- CQ #8665 - RV ------------
2575        begin
2576          DoIt := FALSE;
2577          for i := 0 to dmodShared.TIUObjects.Count - 1 do
2578            if uPersonalObjects.IndexOf(Piece(dmodShared.TIUObjects[i], U, 2)) >= 0 then // -------- CQ #8665 - RV ------------
2579              frmTemplateObjects.cboObjects.Items.Add(dmodShared.TIUObjects[i]);
2580        end;
2581      end;
2582      if DoIt then
2583        FastAssign(dmodShared.TIUObjects, frmTemplateObjects.cboObjects.Items);
2584      frmTemplateObjects.Font := Font;
2585      frmTemplateObjects.re := reBoil;
2586      frmTemplateObjects.AutoLongLines := AutoLongLines;
2587    end;
2588    frmTemplateObjects.Show;
2589  end;
2590  
2591  procedure TfrmTemplateEditor.mnuBPErrorCheckClick(Sender: TObject);
2592  begin
2593    FBPOK := FALSE;
2594    if (reBoil.Lines.Count > 0) then
2595    begin
2596      if (dmodShared.TemplateOK(FCurTree.Selected.Data, 'OK')) then
2597      begin
2598        TestBoilerplate(reBoil.Lines);
2599        if (RPCBrokerV.Results.Count > 0) then
2600          InfoBox('Boilerplate Contains Errors:' + CRLF + CRLF +
2601            RPCBrokerV.Results.Text, 'Error', MB_OK or MB_ICONERROR)
2602        else
2603        begin
2604          FBPOK := TRUE;
2605          if (assigned(Sender)) then
2606            InfoBox('No Errors Found in Boilerplate.', 'Information', MB_OK or MB_ICONINFORMATION);
2607        end;
2608      end;
2609    end;
2610  end;
2611  
2612  procedure TfrmTemplateEditor.popBoilerplatePopup(Sender: TObject);
2613  var
2614    tryOK, ok: boolean;
2615  
2616  begin
2617    ok := not reBoil.ReadOnly;
2618    mnuBPInsertObject.Enabled := ok;
2619    mnuBPInsertField.Enabled := ok;
2620  
2621    mnuBPPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
2622    if (ok) then
2623      ok := (reBoil.Lines.Count > 0);
2624    tryOK := (reBoil.Lines.Count > 0) or ((pnlGroupBP.Visible) and (reGroupBP.Lines.Count > 0));
2625    mnuBPErrorCheck.Enabled := tryOK;
2626    mnuBPTry.Enabled := tryOK;
2627    mnuBPSpellCheck.Enabled := ok and SpellCheckAvailable;
2628    mnuBPCheckGrammar.Enabled := ok and SpellCheckAvailable;
2629  
2630    mnuBPCopy.Enabled := (reBoil.SelLength > 0);
2631    mnuBPCut.Enabled := (ok and (reBoil.SelLength > 0));
2632    mnuBPSelectAll.Enabled := (reBoil.Lines.Count > 0);
2633    mnuBPUndo.Enabled := (reBoil.Perform(EM_CANUNDO, 0, 0) <> 0);
2634  end;
2635  
2636  function TfrmTemplateEditor.ScanNames: boolean;
2637  var
2638    Errors: TList;
2639    msg: string;
2640    i: integer;
2641    Node: TTreeNode;
2642  
2643    procedure ScanTree(Tree: TTreeView);
2644    begin
2645      Node := Tree.Items.GetFirstNode;
2646      while (assigned(Node)) do
2647      begin
2648        if (Node.Text <> EmptyNodeText) and (assigned(Node.Data)) then
2649        begin
2650          if (BadTemplateName(Node.Text)) then
2651            Errors.Add(Node);
2652        end;
2653        Node := Node.GetNext;
2654      end;
2655    end;
2656  
2657  begin
2658    Errors := TList.Create;
2659    try
2660      ScanTree(tvShared);
2661      ScanTree(tvPersonal);
2662      if (Errors.Count > 0) then
2663      begin
2664        if (Errors.Count > 1) then
2665          msg := IntToStr(Errors.Count) + ' Templates have invalid names'
2666        else
2667          msg := 'Template has an invalid name';
2668        msg := msg + ': ';
2669        for i := 0 to Errors.Count - 1 do
2670        begin
2671          if (i > 0) then msg := msg + ', ';
2672          Node := TTreeNode(Errors[i]);
2673          msg := msg + Node.Text;
2674          Node.MakeVisible;
2675        end;
2676        msg := msg + '.' + BadNameText;
2677        InfoBox(msg, 'Error', MB_OK or MB_ICONERROR);
2678        TTreeView(Node.TreeView).Selected := TTreeNode(Errors[0]);
2679      end;
2680    finally
2681      Result := (Errors.Count = 0);
2682      Errors.Free;
2683    end;
2684  end;
2685  
2686  procedure TfrmTemplateEditor.btnCancelClick(Sender: TObject);
2687  begin
2688    FOK2Close := TRUE;
2689  end;
2690  
2691  procedure TfrmTemplateEditor.FormClose(Sender: TObject;
2692    var Action: TCloseAction);
2693  begin
2694    SaveUserBounds(Self);
2695  end;
2696  
2697  procedure TfrmTemplateEditor.FormCloseQuery(Sender: TObject;
2698    var CanClose: Boolean);
2699  var
2700    ans: word;
2701  
2702  begin
2703    if (not FOK2Close) and (BackupDiffers) then
2704    begin
2705      ans := InfoBox('Save Changes?', 'Confirmation', MB_YESNOCANCEL or MB_ICONQUESTION);
2706      if (ans = IDCANCEL) then
2707        CanClose := FALSE
2708      else
2709        if (ans = IDYES) then
2710        begin
2711          CanClose := FALSE;
2712          if (ScanNames) then
2713          begin
2714            if (SaveAllTemplates) then
2715              CanClose := TRUE
2716            else
2717              BtnApply.Enabled := BackupDiffers;
2718          end;
2719        end;
2720    end;
2721  end;
2722  
2723  procedure TfrmTemplateEditor.mnuBPSpellCheckClick(Sender: TObject);
2724  begin
2725    SpellCheckForControl(reBoil);
2726  end;
2727  
2728  procedure TfrmTemplateEditor.splBoilMoved(Sender: TObject);
2729  begin
2730    if reboil.Visible and pnlGroupBP.Visible then
2731      tmplEditorSplitterBoil := reBoil.Height;
2732    if pnlNotes.Visible then
2733      tmplEditorSplitterNotes := pnlNotes.Height;
2734    pnlBoilerplateResize(Self);
2735    if not PnlNotes.visible and (PnlNotes.Top <= pnlGroupBP.Top - pnlGroupBP.Constraints.MinHeight) then
2736      tmplEditorSplitterNotes := (pnlGroupBP.Height - pnlGroupBP.Constraints.MinHeight) - splNotes.Height - 10;
2737    if not pnlGroupBP.visible and (pnlGroupBP.Top >= pnlNotes.Top + pnlGroupBP.Constraints.MinHeight) then
2738      tmplEditorSplitterBoil := reBoil.Height - pnlGroupBP.Constraints.MinHeight - 10;
2739  end;
2740  
2741  procedure TfrmTemplateEditor.edtGapKeyPress(Sender: TObject;
2742    var Key: Char);
2743  begin
2744    if (not (Key in ['0', '1', '2', '3'])) then Key := #0;
2745  end;
2746  
2747  procedure TfrmTemplateEditor.edtNameExit(Sender: TObject);
2748  var
2749    Warn: boolean;
2750  
2751  begin
2752    Warn := (ActiveControl <> btnCancel) and (BadTemplateName(edtName.Text));
2753    if (Warn and ((ActiveControl = sbShDelete) or (ActiveControl = sbPerDelete))) then
2754    begin
2755      if ((assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
2756        Warn := not AutoDel(TTemplate(FCurTree.Selected.Data));
2757    end;
2758    if (Warn) then
2759    begin
2760      InfoBox('Template has an invalid name: ' + edtName.Text + '.' + BadNameText, 'Error', MB_OK or MB_ICONERROR);
2761      edtName.SetFocus;
2762    end;
2763  end;
2764  
2765  procedure TfrmTemplateEditor.tmrAutoScrollTimer(Sender: TObject);
2766  const
2767    EdgeScroll = 16;
2768  
2769  var
2770    TopNode: TTreeNode;
2771    Redraw: boolean;
2772    TmpPt: TPoint;
2773    ht: THitTests;
2774    HPos, RMax: integer;
2775  
2776  begin
2777    if (assigned(FDropNode)) then
2778    begin
2779      TopNode := FDropNode.TreeView.TopItem;
2780      Redraw := FALSE;
2781      TmpPt := FDropNode.TreeView.ScreenToClient(Mouse.CursorPos);
2782      if (TopNode = FDropNode) and (TopNode <> TTreeView(FDropNode.TreeView).Items.GetFirstNode) then
2783      begin
2784        FDropNode.TreeView.TopItem := TopNode.GetPrevVisible;
2785        Redraw := TRUE;
2786      end
2787      else
2788      begin
2789        RMax := FDropNode.TreeView.ClientHeight - EdgeScroll;
2790        if ((TmpPt.Y > RMax) and (FDropNode.GetNextVisible <> nil)) then
2791        begin
2792          TORTreeView(FDropNode.TreeView).VertScrollPos :=
2793            TORTreeView(FDropNode.TreeView).VertScrollPos + 1;
2794          Redraw := TRUE;
2795        end;
2796      end;
2797      if (FLastDropNode <> FDropNode) then
2798      begin
2799        if ((assigned(FDropNode)) and (FDropNode.GetNext = nil)) then
2800          Redraw := TRUE
2801        else
2802          if ((assigned(FLastDropNode)) and (FLastDropNode.GetNext = nil)) then
2803            Redraw := TRUE;
2804        FLastDropNode := FDropNode;
2805        FDragOverCount := 0;
2806      end
2807      else
2808      begin
2809        if (FDropNode.HasChildren) and (not FDropNode.Expanded) then
2810        begin
2811          ht := FDropNode.TreeView.GetHitTestInfoAt(TmpPt.X, TmpPt.Y);
2812          if (htOnButton in ht) then
2813          begin
2814            inc(FDragOverCount);
2815            if (FDragOverCount > 4) then
2816            begin
2817              TopNode := FDropNode.TreeView.TopItem;
2818              FDropNode.Expand(FALSE);
2819              FDropNode.TreeView.TopItem := TopNode;
2820              FDragOverCount := 0;
2821              Redraw := TRUE;
2822            end;
2823          end
2824          else
2825            FDragOverCount := 0;
2826        end;
2827        if (not Redraw) then
2828        begin
2829          HPos := TORTreeView(FDropNode.TreeView).HorzScrollPos;
2830          if (HPos > 0) and (TmpPt.X < EdgeScroll) then
2831          begin
2832            TORTreeView(FDropNode.TreeView).HorzScrollPos :=
2833              TORTreeView(FDropNode.TreeView).HorzScrollPos - EdgeScroll;
2834            Redraw := TRUE;
2835          end
2836          else
2837          begin
2838            RMax := FDropNode.TreeView.ClientWidth - EdgeScroll;
2839            if (TmpPt.X > RMax) then
2840            begin
2841              TORTreeView(FDropNode.TreeView).HorzScrollPos :=
2842                TORTreeView(FDropNode.TreeView).HorzScrollPos + EdgeScroll;
2843              Redraw := TRUE;
2844            end;
2845          end;
2846        end;
2847      end;
2848      if (Redraw) then
2849      begin
2850        TmpPt := Mouse.CursorPos; // Wiggling the mouse causes needed windows messages to fire
2851        inc(TmpPt.X);
2852        Mouse.CursorPos := TmpPt;
2853        dec(TmpPt.X);
2854        Mouse.CursorPos := TmpPt;
2855        FDropNode.TreeView.Invalidate;
2856      end;
2857    end;
2858  end;
2859  
2860  procedure TfrmTemplateEditor.tvTreeStartDrag(Sender: TObject;
2861    var DragObject: TDragObject);
2862  begin
2863    FDropNode := nil;
2864    FLastDropNode := nil;
2865    FDragOverCount := 0;
2866    tmrAutoScroll.Enabled := TRUE;
2867  end;
2868  
2869  procedure TfrmTemplateEditor.tvTreeEndDrag(Sender, Target: TObject; X,
2870    Y: Integer);
2871  begin
2872    tmrAutoScroll.Enabled := FALSE;
2873  end;
2874  
2875  procedure TfrmTemplateEditor.mnuGroupBPCopyClick(Sender: TObject);
2876  begin
2877    reGroupBP.CopyToClipboard;
2878  end;
2879  
2880  procedure TfrmTemplateEditor.popGroupPopup(Sender: TObject);
2881  begin
2882    mnuGroupBPCopy.Enabled := (pnlGroupBP.Visible and (reGroupBP.SelLength > 0));
2883    mnuGroupBPSelectAll.Enabled := (pnlGroupBP.Visible and (reGroupBP.Lines.Count > 0));
2884  end;
2885  
2886  procedure TfrmTemplateEditor.mnuBPCutClick(Sender: TObject);
2887  begin
2888    reBoil.CutToClipboard;
2889  end;
2890  
2891  procedure TfrmTemplateEditor.mnuBPCopyClick(Sender: TObject);
2892  begin
2893    reBoil.CopyToClipboard;
2894  end;
2895  
2896  procedure TfrmTemplateEditor.mnuBPPasteClick(Sender: TObject);
2897  begin
2898    reBoil.SelText := Clipboard.AsText;
2899  end;
2900  
2901  procedure TfrmTemplateEditor.mnuGroupBPSelectAllClick(Sender: TObject);
2902  begin
2903    reGroupBP.SelectAll;
2904  end;
2905  
2906  procedure TfrmTemplateEditor.mnuBPSelectAllClick(Sender: TObject);
2907  begin
2908    reBoil.SelectAll;
2909  end;
2910  
2911  procedure TfrmTemplateEditor.mnuNodeDeleteClick(Sender: TObject);
2912  begin
2913    if (FCurTree = tvShared) and (sbShDelete.Visible) and (sbShDelete.Enabled) then
2914      sbDeleteClick(sbShDelete)
2915    else
2916      if (FCurTree = tvPersonal) and (sbPerDelete.Visible) and (sbPerDelete.Enabled) then
2917        sbDeleteClick(sbPerDelete);
2918  end;
2919  
2920  procedure TfrmTemplateEditor.mnuNodeCopyClick(Sender: TObject);
2921  begin
2922    if (assigned(FCurTree)) then
2923      FCopyNode := FCurTree.Selected
2924    else
2925      FCopyNode := nil;
2926  end;
2927  
2928  procedure TfrmTemplateEditor.mnuNodePasteClick(Sender: TObject);
2929  begin
2930    if (PasteOK) then
2931    begin
2932      FDragNode := FCopyNode;
2933      FDropNode := FPasteNode;
2934      FDropInto := (TTemplate(FDropNode.Data).RealType in AllTemplateFolderTypes);
2935      FCopying := TRUE;
2936      try
2937        tvTreeDragDrop(tvShared, tvPersonal, 0, 0);
2938      finally
2939        FCopying := FALSE;
2940      end;
2941    end;
2942    FCopyNode := nil;
2943  end;
2944  
2945  function TfrmTemplateEditor.PasteOK: boolean;
2946  var
2947    OldCopying: boolean;
2948    Node: TTreeNode;
2949  
2950  begin
2951    Result := assigned(FCopyNode) and assigned(FPasteNode);
2952    if (Result) then
2953      Result := (FTreeControl[TTemplateTreeType(FPasteNode.TreeView.Tag), tcDel].Visible);
2954    if (Result) then
2955    begin
2956      OldCopying := FCopying;
2957      FCopying := TRUE;
2958      try
2959        Node := FPasteNode;
2960        if (TTemplate(Node.Data).RealType = ttDoc) then
2961          Node := Node.Parent;
2962        Result := AllowMove(Node, FCopyNode);
2963      finally
2964        FCopying := OldCopying;
2965      end;
2966    end;
2967  end;
2968  
2969  procedure TfrmTemplateEditor.mnuBPUndoClick(Sender: TObject);
2970  begin
2971    reBoil.Perform(EM_UNDO, 0, 0);
2972  end;
2973  
2974  procedure TfrmTemplateEditor.tvTreeKeyDown(Sender: TObject;
2975    var Key: Word; Shift: TShiftState);
2976  begin
2977  
2978    if (Key = VK_DELETE) then
2979    begin
2980      if (Sender = tvShared) then
2981      begin
2982        if (sbShDelete.Visible and sbShDelete.Enabled) then
2983          sbDeleteClick(sbShDelete);
2984      end
2985      else
2986      begin
2987        if (sbPerDelete.Visible and sbPerDelete.Enabled) then
2988          sbDeleteClick(sbPerDelete);
2989      end;
2990    end;
2991     //Code Added to provide CTRL Key access for 508 compliance  GRE 3/03
2992    if (ssCtrl in Shift) and (Key = VK_A) then
2993      reBoil.SelectAll
2994    else
2995      if (ssCtrl in Shift) and (Key = VK_C) then
2996        reBoil.CopyToClipboard
2997      else
2998        if (ssCtrl in Shift) and (Key = VK_E) then
2999          mnuBPErrorCheckClick(Self)
3000        else
3001          if (ssCtrl in Shift) and (Key = VK_F) then
3002            mnuBPInsertFieldClick(Self)
3003          else
3004            if (ssCtrl in Shift) and (Key = VK_G) then
3005              GrammarCheckForControl(reBoil)
3006            else
3007              if (ssCtrl in Shift) and (Key = VK_I) then
3008                mnuBPInsertObjectClick(Self)
3009              else
3010                if (ssCtrl in Shift) and (Key = VK_S) then
3011                  SpellCheckForControl(reBoil)
3012                else
3013                  if (ssCtrl in Shift) and (Key = VK_T) then
3014                    mnuBPTryClick(Self)
3015                  else
3016                    if (ssCtrl in Shift) and (Key = VK_V) then
3017                      reBoil.SelText := Clipboard.AsText
3018                    else
3019                      if (ssCtrl in Shift) and (Key = VK_X) then
3020                        reBoil.CutToClipboard
3021                      else
3022                        if (ssCtrl in Shift) and (Key = VK_Z) then
3023                          reBoil.Perform(EM_UNDO, 0, 0);
3024    //End of ---- Code Added to provide CTRL Key access for 508 compliance  GRE 3/03
3025  end;
3026  
3027  procedure TfrmTemplateEditor.mnuEditClick(Sender: TObject);
3028  var
3029    tryOK, ok: boolean;
3030  
3031  begin
3032    if reboil.Visible then
3033    begin
3034      ok := (not reBoil.ReadOnly);
3035      mnuInsertObject.Enabled := ok;
3036      mnuInsertField.Enabled := ok;
3037      mnuPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
3038      if (ok) then
3039        ok := (reBoil.Lines.Count > 0);
3040      tryOK := (reBoil.Lines.Count > 0) or ((pnlGroupBP.Visible) and (reGroupBP.Lines.Count > 0));
3041      mnuErrorCheck.Enabled := tryOK;
3042      mnuTry.Enabled := tryOK;
3043      mnuSpellCheck.Enabled := ok and SpellCheckAvailable;
3044      mnuCheckGrammar.Enabled := ok and SpellCheckAvailable;
3045  
3046      mnuCopy.Enabled := (reBoil.SelLength > 0);
3047      mnuCut.Enabled := (ok and (reBoil.SelLength > 0));
3048      mnuSelectAll.Enabled := (reBoil.Lines.Count > 0);
3049      mnuUndo.Enabled := (reBoil.Perform(EM_CANUNDO, 0, 0) <> 0);
3050      mnuGroupBoilerplate.Enabled := pnlGroupBP.Visible;
3051    end
3052    else
3053    begin
3054      mnuInsertObject.Enabled := FALSE;
3055      mnuInsertField.Enabled := FALSE;
3056      mnuPaste.Enabled := FALSE;
3057      mnuErrorCheck.Enabled := FALSE;
3058      mnuTry.Enabled := FALSE;
3059      mnuSpellCheck.Enabled := FALSE;
3060      mnuCheckGrammar.Enabled := FALSE;
3061      mnuCopy.Enabled := FALSE;
3062      mnuCut.Enabled := FALSE;
3063      mnuSelectAll.Enabled := FALSE;
3064      mnuUndo.Enabled := FALSE;
3065      mnuGroupBoilerplate.Enabled := FALSE;
3066    end;
3067  end;
3068  
3069  procedure TfrmTemplateEditor.mnuGroupBoilerplateClick(Sender: TObject);
3070  begin
3071    mnuGroupCopy.Enabled := (pnlGroupBP.Visible and (reGroupBP.SelLength > 0));
3072    mnuGroupSelectAll.Enabled := (pnlGroupBP.Visible and (reGroupBP.Lines.Count > 0));
3073  end;
3074  
3075  procedure TfrmTemplateEditor.cbShFindOptionClick(Sender: TObject);
3076  begin
3077    SetFindNext(tvShared, FALSE);
3078    if (pnlShSearch.Visible) then edtShSearch.SetFocus;
3079  end;
3080  
3081  procedure TfrmTemplateEditor.cbPerFindOptionClick(Sender: TObject);
3082  begin
3083    SetFindNext(tvPersonal, FALSE);
3084    if (pnlPerSearch.Visible) then edtPerSearch.SetFocus;
3085  end;
3086  
3087  procedure TfrmTemplateEditor.mnuTemplateClick(Sender: TObject);
3088  var
3089    Tree: TTreeView;
3090  
3091  begin
3092    FFromMainMenu := TRUE;
3093    Tree := FCurTree;
3094    if (assigned(Tree) and assigned(Tree.Selected)) then
3095    begin
3096      if (Tree = tvShared) then
3097        mnuTDelete.Enabled := ((sbShDelete.Visible) and (sbShDelete.Enabled))
3098      else
3099        mnuTDelete.Enabled := ((sbPerDelete.Visible) and (sbPerDelete.Enabled));
3100      if (assigned(Tree) and assigned(Tree.Selected) and assigned(Tree.Selected.Data)) then
3101      begin
3102        mnuTCopy.Enabled := (TTemplate(Tree.Selected.Data).RealType in [ttDoc, ttGroup, ttClass]);
3103        mnuSort.Enabled := (TTemplate(Tree.Selected.Data).RealType in AllTemplateFolderTypes) and
3104          (Tree.Selected.HasChildren) and
3105          (Tree.Selected.GetFirstChild.GetNextSibling <> nil);
3106      end
3107      else
3108      begin
3109        mnuTCopy.Enabled := FALSE;
3110        mnuSort.Enabled := FALSE;
3111      end;
3112      FPasteNode := Tree.Selected;
3113      mnuTPaste.Enabled := PasteOK;
3114    end
3115    else
3116    begin
3117      mnuTCopy.Enabled := FALSE;
3118      mnuTPaste.Enabled := FALSE;
3119      mnuTDelete.Enabled := FALSE;
3120      mnuSort.Enabled := FALSE;
3121    end;
3122    mnuNewTemplate.Enabled := btnNew.Enabled;
3123    mnuAutoGen.Enabled := btnNew.Enabled;
3124    mnuFindShared.Checked := FFindShOn;
3125    mnuFindPersonal.Checked := FFindPerOn;
3126    mnuShCollapse.Enabled := dmodShared.NeedsCollapsing(tvShared);
3127    mnuPerCollapse.Enabled := dmodShared.NeedsCollapsing(tvPersonal);
3128  end;
3129  
3130  procedure TfrmTemplateEditor.mnuFindSharedClick(Sender: TObject);
3131  begin
3132    FMainMenuTree := tvShared;
3133    mnuFindTemplatesClick(tvShared);
3134  end;
3135  
3136  procedure TfrmTemplateEditor.mnuFindPersonalClick(Sender: TObject);
3137  begin
3138    FMainMenuTree := tvPersonal;
3139    mnuFindTemplatesClick(tvPersonal);
3140  end;
3141  
3142  procedure TfrmTemplateEditor.mnuShCollapseClick(Sender: TObject);
3143  begin
3144    FMainMenuTree := tvShared;
3145    mnuCollapseTreeClick(tvShared);
3146  end;
3147  
3148  procedure TfrmTemplateEditor.mnuPerCollapseClick(Sender: TObject);
3149  begin
3150    FMainMenuTree := tvPersonal;
3151    mnuCollapseTreeClick(tvPersonal);
3152  end;
3153  
3154  procedure TfrmTemplateEditor.pnlShSearchResize(Sender: TObject);
3155  begin
3156    if ((cbShMatchCase.Width + cbShWholeWords.Width) > pnlShSearch.Width) then
3157      cbShWholeWords.Left := cbShMatchCase.Width
3158    else
3159      cbShWholeWords.Left := pnlShSearch.Width - cbShWholeWords.Width;
3160  end;
3161  
3162  procedure TfrmTemplateEditor.pnlPerSearchResize(Sender: TObject);
3163  begin
3164    if ((cbPerMatchCase.Width + cbPerWholeWords.Width) > pnlPerSearch.Width) then
3165      cbPerWholeWords.Left := cbPerMatchCase.Width
3166    else
3167      cbPerWholeWords.Left := pnlPerSearch.Width - cbPerWholeWords.Width;
3168  end;
3169  
3170  procedure TfrmTemplateEditor.pnlPropertiesResize(Sender: TObject);
3171  begin
3172    btnNew.Width := pnlProperties.Width;
3173  end;
3174  
3175  procedure TfrmTemplateEditor.mbMainResize(Sender: TObject);
3176  begin
3177    pnlMenu.Width := mbMain.Width + 4;
3178    mbMain.Width := pnlMenu.Width - 3;
3179  end;
3180  
3181  procedure TfrmTemplateEditor.mnuBPCheckGrammarClick(Sender: TObject);
3182  begin
3183    GrammarCheckForControl(reBoil);
3184  end;
3185  
3186  procedure TfrmTemplateEditor.mnuSortClick(Sender: TObject);
3187  var
3188    Tree: TTreeView;
3189  
3190  begin
3191    Tree := FCurTree;
3192    if (assigned(Tree) and assigned(Tree.Selected) and Tree.Selected.HasChildren) then
3193    begin
3194      TTemplate(Tree.Selected.Data).SortChildren;
3195      Resync([TTemplate(Tree.Selected.Data)]);
3196      btnApply.Enabled := TRUE;
3197    end;
3198  end;
3199  
3200  procedure TfrmTemplateEditor.pnlBoilerplateCanResize(Sender: TObject;
3201    var NewWidth, NewHeight: Integer; var Resize: Boolean);
3202  begin
3203    if (NewHeight < 40) then Resize := FALSE;
3204  end;
3205  
3206  function TfrmTemplateEditor.AutoDel(Template: TTemplate): boolean;
3207  begin
3208    if (assigned(Template)) then
3209      Result := (((Template.ID = '0') or (Template.ID = '')) and
3210        (Template.PrintName = NewTemplateName) and
3211        (Template.Boilerplate = ''))
3212    else
3213      Result := FALSE;
3214  end;
3215  
3216  procedure TfrmTemplateEditor.mnuBPTryClick(Sender: TObject);
3217  var
3218    R: TRect;
3219    Move: boolean;
3220    tmpl: TTemplate;
3221    txt: string;
3222  
3223  begin
3224    mnuBPErrorCheckClick(nil);
3225    if (FBPOK) or (reBoil.Lines.Count = 0) then
3226    begin
3227      Move := assigned(frmTemplateView);
3228      if (Move) then
3229      begin
3230        R := frmTemplateView.BoundsRect;
3231        frmTemplateView.Free;
3232        frmTemplateView := nil;
3233      end;
3234      tmpl := TTemplate(FCurTree.Selected.Data);
3235      tmpl.TemplatePreviewMode := TRUE; // Prevents "Are you sure?" dialog when canceling
3236      txt := tmpl.Text;
3237      if (not tmpl.DialogAborted) then
3238        ShowTemplateData(Self, tmpl.PrintName, txt);
3239      if (Move) then
3240        frmTemplateView.BoundsRect := R;
3241      tmpl.TemplatePreviewMode := FALSE;
3242    end;
3243  end;
3244  
3245  procedure TfrmTemplateEditor.mnuAutoGenClick(Sender: TObject);
3246  var
3247    AName, AText: string;
3248  
3249  begin
3250    dmodShared.LoadTIUObjects;
3251    UpdatePersonalObjects;
3252    GetAutoGenText(AName, AText, uPersonalObjects); // -------- CQ #8665 - RV ------------
3253    if (AName <> '') and (AText <> '') then
3254    begin
3255      btnNewClick(Self);
3256      TTemplate(FBtnNewNode.Data).PrintName := AName;
3257      TTemplate(FBtnNewNode.Data).Boilerplate := AText;
3258      ShowInfo(FBtnNewNode);
3259      edtNameOldChange(Self);
3260    end;
3261  end;
3262  
3263  procedure TfrmTemplateEditor.reNotesChange(Sender: TObject);
3264  var
3265    Template: TTemplate;
3266    DoRefresh: boolean;
3267  
3268  begin
3269    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
3270    begin
3271      if CanClone(FCurTree.Selected) then
3272      begin
3273        DoRefresh := Clone(FCurTree.Selected);
3274        Template := TTemplate(FCurTree.Selected.Data);
3275        if assigned(Template) and Template.CanModify then
3276        begin
3277          Template.Description := reNotes.Lines.Text;
3278          UpdateApply(Template);
3279          if (DoRefresh) then
3280          begin
3281            tvShared.Invalidate;
3282            tvPersonal.Invalidate;
3283          end;
3284        end;
3285      end;
3286      btnApply.Enabled := TRUE;
3287  //      reNotes.Lines.Text := Template.Description;
3288    end;
3289  end;
3290  
3291  procedure TfrmTemplateEditor.mnuNotesUndoClick(Sender: TObject);
3292  begin
3293    reNotes.Perform(EM_UNDO, 0, 0);
3294  end;
3295  
3296  procedure TfrmTemplateEditor.mnuNotesCutClick(Sender: TObject);
3297  begin
3298    reNotes.CutToClipboard;
3299  end;
3300  
3301  procedure TfrmTemplateEditor.mnuNotesCopyClick(Sender: TObject);
3302  begin
3303    reNotes.CopyToClipboard;
3304  end;
3305  
3306  procedure TfrmTemplateEditor.mnuNotesPasteClick(Sender: TObject);
3307  begin
3308    reNotes.SelText := Clipboard.AsText;
3309  end;
3310  
3311  procedure TfrmTemplateEditor.mnuNotesSelectAllClick(Sender: TObject);
3312  begin
3313    reNotes.SelectAll;
3314  end;
3315  
3316  procedure TfrmTemplateEditor.mnuNotesGrammarClick(Sender: TObject);
3317  begin
3318    GrammarCheckForControl(reNotes);
3319  end;
3320  
3321  procedure TfrmTemplateEditor.mnuNotesSpellingClick(Sender: TObject);
3322  begin
3323    SpellCheckForControl(reNotes);
3324  end;
3325  
3326  procedure TfrmTemplateEditor.popNotesPopup(Sender: TObject);
3327  var
3328    ok: boolean;
3329  
3330  begin
3331    ok := not reNotes.ReadOnly;
3332    mnuNotesPaste.Enabled := (ok and Clipboard.HasFormat(CF_TEXT));
3333    if (ok) then
3334      ok := (reNotes.Lines.Count > 0);
3335    mnuNotesSpelling.Enabled := ok and SpellCheckAvailable;
3336    mnuNotesGrammar.Enabled := ok and SpellCheckAvailable;
3337    mnuNotesCopy.Enabled := (reNotes.SelLength > 0);
3338    mnuNotesCut.Enabled := (ok and (reNotes.SelLength > 0));
3339    mnuNotesSelectAll.Enabled := (reNotes.Lines.Count > 0);
3340    mnuNotesUndo.Enabled := (reNotes.Perform(EM_CANUNDO, 0, 0) <> 0);
3341  end;
3342  
3343  procedure TfrmTemplateEditor.cbNotesClick(Sender: TObject);
3344  var
3345    ScreenPoint: TPoint;
3346    NewHeight: Integer;
3347  begin
3348    pnlNotes.Visible := cbNotes.Checked;
3349    splNotes.Visible := cbNotes.Checked;
3350    if cbNotes.Checked then
3351    begin
3352      If (pnlGroupBP.Visible) and (tmplEditorSplitterNotes >= pnlGroupBP.Height - pnlGroupBP.Constraints.MinHeight) then
3353       tmplEditorSplitterNotes := pnlNotes.Constraints.MinHeight;
3354  
3355      pnlNotes.Height := tmplEditorSplitterNotes;
3356      pnlNotes.Top := pnlBottom.Top - pnlNotes.Height;
3357      splNotes.Top := pnlNotes.Top - 3;
3358      if pnlGroupBP.Height <= pnlGroupBP.Constraints.MinHeight then begin
3359        //Clear the points
3360        ScreenPoint.Y := 0;
3361        ScreenPoint.X := 0;
3362        if pnlNotes.ClientToScreen(ScreenPoint).Y + 30 > pnlBottom.Top then begin
3363          if pnlNotes.ClientToScreen(ScreenPoint).Y > pnlBottom.Top then
3364            NewHeight := pnlNotes.Height + (pnlNotes.ClientToScreen(ScreenPoint).Y - pnlBottom.Top)
3365          else
3366            NewHeight := pnlNotes.Height - (pnlNotes.ClientToScreen(ScreenPoint).Y - pnlBottom.Top);
3367          Reboil.Height := Reboil.Height - (NewHeight + 10);
3368          PnlNotes.Constraints.MinHeight := 30;
3369          pnlNotes.Height := PnlNotes.Constraints.MinHeight;
3370          if reboil.Height = reboil.Constraints.MinHeight then
3371            PnlTop.height := PnlTop.height - 100;
3372        end;
3373      end;
3374    end else begin
3375      pnlNotes.Constraints.MinHeight := 1;
3376      pnlNotes.Height := 1;
3377    end;
3378    pnlBoilerplateResize(Self);
3379  end;
3380  
3381  procedure TfrmTemplateEditor.cbDisplayOnlyClick(Sender: TObject);
3382  begin
3383    cbClick(TCheckBox(Sender), BPDisplayOnlyFld);
3384  end;
3385  
3386  procedure TfrmTemplateEditor.cbFirstLineClick(Sender: TObject);
3387  begin
3388    cbClick(TCheckBox(Sender), BPFirstLineFld);
3389  end;
3390  
3391  procedure TfrmTemplateEditor.cbOneItemOnlyClick(Sender: TObject);
3392  begin
3393    cbClick(TCheckBox(Sender), BPOneItemOnlyFld);
3394  end;
3395  
3396  procedure TfrmTemplateEditor.cbHideDlgItemsClick(Sender: TObject);
3397  begin
3398    cbClick(TCheckBox(Sender), BPHideDlgItemsFld);
3399  end;
3400  
3401  procedure TfrmTemplateEditor.cbHideItemsClick(Sender: TObject);
3402  begin
3403    cbClick(TCheckBox(Sender), BPHideItemsFld);
3404  end;
3405  
3406  procedure TfrmTemplateEditor.cbClick(Sender: TCheckBox; Index: integer);
3407  var
3408    Template: TTemplate;
3409  
3410  begin
3411    if ((not FUpdating) and (assigned(FCurTree)) and (assigned(FCurTree.Selected))) then
3412    begin
3413      if CanClone(FCurTree.Selected) then
3414      begin
3415        Clone(FCurTree.Selected);
3416        Template := TTemplate(FCurTree.Selected.Data);
3417        if assigned(Template) and Template.CanModify then
3418        begin
3419          case Index of
3420            BPDisplayOnlyFld: Template.DisplayOnly := Sender.Checked;
3421            BPFirstLineFld: Template.FirstLine := Sender.Checked;
3422            BPOneItemOnlyFld: Template.OneItemOnly := Sender.Checked;
3423            BPHideDlgItemsFld: Template.HideDlgItems := Sender.Checked;
3424            BPHideItemsFld: Template.HideItems := Sender.Checked;
3425            BPIndentFld: Template.IndentItems := Sender.Checked;
3426            BPLockFld: Template.Lock := Sender.Checked;
3427          end;
3428          UpdateApply(Template);
3429        end;
3430      end;
3431    end;
3432  end;
3433  
3434  procedure TfrmTemplateEditor.cbIndentClick(Sender: TObject);
3435  begin
3436    cbClick(TCheckBox(Sender), BPIndentFld);
3437  end;
3438  
3439  procedure TfrmTemplateEditor.mnuToolsClick(Sender: TObject);
3440  begin
3441    mnuEditTemplateFields.Enabled := CanEditTemplateFields;
3442    mnuImportTemplate.Enabled := btnNew.Enabled;
3443    mnuExportTemplate.Enabled := (assigned(FCurTree) and assigned(FCurTree.Selected) and
3444      assigned(FCurTree.Selected.Data));
3445  end;
3446  
3447  procedure TfrmTemplateEditor.mnuEditTemplateFieldsClick(Sender: TObject);
3448  begin
3449    if assigned(frmTemplateObjects) then
3450      frmTemplateObjects.Hide;
3451    if assigned(frmTemplateFields) then
3452      frmTemplateFields.Hide;
3453    if EditDialogFields and assigned(frmTemplateFields) then
3454      FreeAndNil(frmTemplateFields);
3455  end;
3456  
3457  procedure TfrmTemplateEditor.mnuBPInsertFieldClick(Sender: TObject);
3458  begin
3459    if (not assigned(frmTemplateFields)) then
3460    begin
3461      frmTemplateFields := TfrmTemplateFields.Create(Self);
3462      frmTemplateFields.Font := Font;
3463      frmTemplateFields.re := reBoil;
3464      frmTemplateFields.AutoLongLines := AutoLongLines;
3465    end;
3466    frmTemplateFields.Show;
3467  end;
3468  
3469  procedure TfrmTemplateEditor.UpdateInsertsDialogs;
3470  begin
3471    if assigned(frmTemplateObjects) then
3472      frmTemplateObjects.UpdateStatus;
3473    if assigned(frmTemplateFields) then
3474      frmTemplateFields.UpdateStatus;
3475  end;
3476  
3477  procedure TfrmTemplateEditor.mnuExportTemplateClick(Sender: TObject);
3478  var
3479    Tmpl, Flds: TStringList;
3480    i: integer;
3481    XMLDoc: IXMLDOMDocument;
3482    err: boolean;
3483  
3484  begin
3485    err := FALSE;
3486    if (assigned(FCurTree) and assigned(FCurTree.Selected) and assigned(FCurTree.Selected.Data)) then
3487    begin
3488      dlgExport.FileName := ValidFileName(TTemplate(FCurTree.Selected.Data).PrintName);
3489      if dlgExport.Execute then
3490      begin
3491        Tmpl := TStringList.Create;
3492        try
3493          Flds := TStringList.Create;
3494          try
3495            Tmpl.Add('<' + XMLHeader + '>');
3496            if TTemplate(FCurTree.Selected.Data).CanExportXML(Tmpl, Flds, 2) then
3497            begin
3498              if (Flds.Count > 0) then begin
3499                ExpandEmbeddedFields(Flds);
3500                FastAssign(ExportTemplateFields(Flds), Flds);
3501                for i := 0 to Flds.Count - 1 do
3502                  Flds[i] := '  ' + Flds[i];
3503                FastAddStrings(Flds, Tmpl);
3504              end; {if}
3505              Tmpl.Add('</' + XMLHeader + '>');
3506              try
3507                XMLDoc := CoDOMDocument.Create;
3508                try
3509                  XMLDoc.preserveWhiteSpace := TRUE;
3510                  XMLDoc.LoadXML(Tmpl.Text);
3511                  XMLDoc.Save(dlgExport.FileName);
3512                finally
3513                  XMLDoc := nil;
3514                end;
3515              except
3516                InfoBox(Format(NoIE5, ['Export']), NoIE5Header, MB_OK);
3517                err := TRUE;
3518              end;
3519              if not err then
3520                InfoBox('Template ' + TTemplate(FCurTree.Selected.Data).PrintName +
3521                  ' Exported.', 'Template Exported', MB_OK);
3522            end;
3523          finally
3524            Flds.Free;
3525          end;
3526        finally
3527          Tmpl.Free;
3528        end;
3529      end;
3530    end;
3531  end;
3532  
3533  procedure TfrmTemplateEditor.mnuImportTemplateClick(Sender: TObject);
3534  const
3535    Filter1 = 'Template Files|*.txml';
3536    WordFilter = '|Word Documents|*.doc;*.dot';
3537    Filter2 = '|XML Files|*.xml|All Files|*.*';
3538  
3539  var
3540    XMLDoc: IXMLDOMDocument;
3541    RootElement: IXMLDOMElement;
3542    ImportedTemplate: TTemplate;
3543    AppData, Flds, ResultSet: TStringList;
3544    tmp, j, p3: string;
3545    err, ok, changes, xmlerr: boolean;
3546    i: integer;
3547    choice: word;
3548  
3549    procedure ClearFields;
3550    begin
3551      Flds.Text := '';
3552      ResultSet.Text := '';
3553    end;
3554  
3555  begin
3556    tmp := Filter1;
3557    err := FALSE;
3558    if WordImportActive then
3559      tmp := tmp + WordFilter;
3560    tmp := tmp + Filter2;
3561    dlgImport.Filter := tmp;
3562    if btnNew.Enabled and dlgImport.Execute then
3563    begin
3564      tmp := ExtractFileExt(dlgImport.FileName);
3565      if (WordImportActive and ((CompareText(tmp, '.doc') = 0) or
3566        (CompareText(tmp, '.dot') = 0))) then
3567        AppData := TStringList.Create
3568      else
3569        AppData := nil;
3570      try
3571        try
3572          XMLDoc := CoDOMDocument.Create;
3573        except
3574          InfoBox(Format(NoIE5, ['Import']), NoIE5Header, MB_OK);
3575          exit;
3576        end;
3577        try
3578          if assigned(AppData) then
3579          begin
3580            try
3581              ok := GetXMLFromWord(dlgImport.FileName, AppData);
3582            except
3583              ok := FALSE;
3584              err := TRUE;
3585            end;
3586          end
3587          else
3588            ok := TRUE;
3589          if ok and assigned(XMLDoc) then
3590          begin
3591            XMLDoc.preserveWhiteSpace := TRUE;
3592            if assigned(AppData) then
3593              XMLDoc.LoadXML(AppData.Text)
3594            else
3595              XMLDoc.Load(dlgImport.FileName);
3596            RootElement := XMLDoc.DocumentElement;
3597            if not assigned(RootElement) then
3598              XMLImportError(0);
3599            try
3600              if (RootElement.tagName <> XMLHeader) then
3601                XMLImportError(0)
3602              else
3603              begin
3604                ImportedTemplate := nil;
3605                FXMLTemplateElement := FindXMLElement(RootElement, XMLTemplateTag);
3606                if assigned(FXMLTemplateElement) then
3607                begin
3608                  FXMLFieldElement := FindXMLElement(RootElement, XMLTemplateFieldsTag);
3609                  if