Index: cprs/trunk/CPRS-Chart/Templates/fTemplateEditor.pas
===================================================================
--- cprs/trunk/CPRS-Chart/Templates/fTemplateEditor.pas	(revision 456)
+++ cprs/trunk/CPRS-Chart/Templates/fTemplateEditor.pas	(revision 829)
@@ -18,5 +18,6 @@
   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
   ExtCtrls, StdCtrls, ComCtrls, ORCtrls, Buttons, Mask, ORFn, ORNet,
-  uTemplates, Menus, ImgList, Clipbrd, ToolWin, MenuBar, TypInfo, MSXML_TLB;
+  uTemplates, Menus, ImgList, Clipbrd, ToolWin, MenuBar, TypInfo, MSXML_TLB, fBase508Form,
+  VA508AccessibilityManager, VA508ImageListLabeler;
 
 type
@@ -24,7 +25,7 @@
   TTemplateTreeType = (ttShared, ttPersonal);
 
-  TfrmTemplateEditor = class(TForm)
+  TfrmTemplateEditor = class(TfrmBase508Form)
     splMain: TSplitter;
-    pnlBottom: TORAutoPanel;
+    pnlBottom: TPanel;
     btnApply: TButton;
     btnCancel: TButton;
@@ -209,4 +210,5 @@
     cbxLink: TORComboBox;
     lblLink: TLabel;
+    imgLblTemplates: TVA508ImageListLabeler;
     procedure btnNewClick(Sender: TObject);
     procedure btnApplyClick(Sender: TObject);
@@ -339,6 +341,4 @@
       Direction, InsertAt: Integer);
     procedure cbxLinkChange(Sender: TObject);
-    procedure cbxLinkSynonymCheck(Sender: TObject; const Text: String;
-      var IsSynonym: Boolean);
     procedure reBoilKeyUp(Sender: TObject; var Key: Word;
       Shift: TShiftState);
@@ -346,4 +346,5 @@
     procedure reBoilKeyDown(Sender: TObject; var Key: Word;
       Shift: TShiftState);
+    procedure FormClose(Sender: TObject; var Action: TCloseAction);
   private
     FLastRect: TRect;
@@ -386,5 +387,5 @@
     FCanDoReminders: boolean;
     FCanDoCOMObjects: boolean;
-    FPersonalObjects: TStringList;
+    //FPersonalObjects: TStringList;
     FShowingTemplate: TTemplate;
     FConsultServices: TStringList;
@@ -416,12 +417,25 @@
     procedure UpdateInsertsDialogs;
     procedure AutoLongLines(Sender: TObject);
-    procedure UpdatePersonalObjects;
+    //procedure UpdatePersonalObjects;
     procedure UpdateApply(Template: TTemplate);
     procedure TemplateLocked(Sender: TObject);
     procedure InitTrees;
+    procedure AdjustControls4FontChange;
+    procedure ShowGroupBoilerplate(Visible: boolean);
     function GetLinkType(const ANode: TTreeNode): TTemplateLinkType;
   end;
 
 procedure EditTemplates(Form: TForm; NewTemplate: boolean = FALSE; CopiedText: string = ''; Shared: boolean = FALSE);
+
+const
+  TemplateEditorSplitters = 'frmTempEditSplitters';
+  TemplateEditorSplitters2 = 'frmTempEditSplitters2';
+
+var
+  tmplEditorSplitterMiddle: integer = 0;
+  tmplEditorSplitterProperties: integer = 0;
+  tmplEditorSplitterMain: integer = 0;
+  tmplEditorSplitterBoil: integer = 0;
+  tmplEditorSplitterNotes: integer = 0;
 
 implementation
@@ -431,5 +445,6 @@
 uses dShared, uCore, rTemplates, fTemplateObjects, uSpell, fTemplateView,
   fTemplateAutoGen, fDrawers, fTemplateFieldEditor, fTemplateFields, XMLUtils,
-  fIconLegend, uReminders, uConst, rCore, rEventHooks, rConsults;
+  fIconLegend, uReminders, uConst, rCore, rEventHooks, rConsults, VAUtils,
+  rMisc, fFindingTemplates;
 
 const
@@ -499,6 +514,6 @@
       Drawers := TFrmDrawers(Form)
     else
-    if IsPublishedProp(Form, 'Drawers') then
-      Drawers := TFrmDrawers(GetOrdProp(Form, 'Drawers'));
+    if IsPublishedProp(Form, DrawersProperty) then
+      Drawers := TFrmDrawers(GetOrdProp(Form, DrawersProperty));
   end;
 
@@ -724,4 +739,5 @@
 
   BtnApply.Enabled := BackupDiffers;
+  SetFormPosition(Self);
 end;
 
@@ -873,8 +889,5 @@
   end;
   lblNotes.Enabled := (not reNotes.ReadOnly);
-  if(reNotes.ReadOnly) then
-    reNotes.Color := ReadOnlyColor
-  else
-    reNotes.Color := clWindow;
+  UpdateReadOnlyColorScheme(reNotes, reNotes.ReadOnly);
   cbxType.Enabled := ok;
   lblType.Enabled := ok;
@@ -894,8 +907,5 @@
   lblLines.Enabled := ok;
   reBoil.ReadOnly := not ok;
-  if(ok) then
-    reBoil.Color := clWindow
-  else
-    reBoil.Color := ReadOnlyColor;
+  UpdateReadOnlyColorScheme(reBoil, not ok);
   lblLink.Enabled := ok;
   cbxLink.Enabled := ok;
@@ -937,4 +947,22 @@
 end;
 
+procedure TfrmTemplateEditor.ShowGroupBoilerplate(Visible: boolean);
+begin
+  pnlGroupBP.Visible := Visible;
+  splBoil.Visible := Visible;
+  if Visible then
+  begin
+    reBoil.Align := alTop;
+    pnlGroupBP.Align := alClient;
+    reBoil.Height := tmplEditorSplitterBoil;
+    splBoil.Top := pnlGroupBP.Top - splBoil.Height;
+  end
+  else
+  begin
+    pnlGroupBP.Align := alBottom;
+    reBoil.Align := alClient;
+  end;
+end;
+
 procedure TfrmTemplateEditor.ShowInfo(Node: TTreeNode);
 var
@@ -942,8 +970,6 @@
   Idx: TTypeIndex;
   CanDoCOM: boolean;
-  LinkTemplate: TTemplate;
   lt: TTemplateLinkType;
   lts: string;
-  i: integer;
 
 begin
@@ -980,13 +1006,8 @@
             begin
               FConsultServices := TStringList.Create;
-              FConsultServices.Assign(LoadServiceListWithSynonyms(1));
+              FastAssign(LoadServiceListWithSynonyms(1), FConsultServices);
               SortByPiece(FConsultServices, U, 2);
             end;
-            for i := 0 to FConsultServices.Count-1 do
-            begin
-              LinkTemplate := GetLinkedTemplate(piece(FConsultServices[i],U,1), ltConsult);
-              if (not assigned(LinkTemplate)) or (LinkTemplate = FShowingTemplate) then
-                cbxLink.Items.Add(FConsultServices[i]);
-            end;
+            FastAssign(FConsultServices, cbxLink.Items);
           end
           else
@@ -1171,6 +1192,5 @@
       edtGap.Text := '0';
       reBoil.Clear;
-      pnlGroupBP.Visible := FALSE;
-      splBoil.Visible := FALSE;
+      ShowGroupBoilerplate(False);
       pnlBoilerplateResize(Self);
       pnlCOM.Visible := FALSE;
@@ -1196,5 +1216,7 @@
 begin
   if(pnlGroupBP.Visible) and (pnlGroupBP.Height > (pnlBoilerplate.Height-29)) then
+  begin
     pnlGroupBP.Height := pnlBoilerplate.Height-29;
+  end;
   if cbLongLines.checked then
     Max := 240
@@ -1449,13 +1471,10 @@
       begin
         reBoil.ReadOnly := TRUE;
-        reBoil.Color := ReadOnlyColor;
+        UpdateReadOnlyColorScheme(reBoil, TRUE);
         UpdateInsertsDialogs;
       end;
-      pnlGroupBP.Visible := ItemOK;
-      splBoil.Visible := ItemOK;
+      ShowGroupBoilerplate(ItemOK);
       if(not ItemOK) and (IsReminderDialog or IsCOMObject) then
         BPOK := FALSE;
-      if(ItemOK) then
-        splBoil.Top := pnlGroupBP.Top - splBoil.Height;
       pnlBoilerplateResize(Self);
       pnlBoilerplate.Visible := BPOK;
@@ -1482,5 +1501,13 @@
     frmTemplateFields := nil;
   end;
-  KillObj(@FPersonalObjects);
+  //---------- CQ #8665 - RV --------
+  //KillObj(@FPersonalObjects);
+  if (assigned(uPersonalObjects)) then
+  begin
+    KillObj(@uPersonalObjects);
+    uPersonalObjects.Free;
+    uPersonalObjects := nil;
+  end;
+  // ----  end CQ #8665 -------------
   dmodShared.OnTemplateLock := nil;
   dmodShared.InEditor := FALSE;
@@ -2040,4 +2067,32 @@
 end;
 
+procedure TfrmTemplateEditor.AdjustControls4FontChange;
+var
+  x: integer;
+
+  procedure Adjust(Control: TWinControl);
+  begin
+    x := x - Control.Width - 2;
+    Control.Left := x;
+  end;
+
+begin
+  if FCanEditShared then
+  begin
+    x := pnlSharedBottom.Width;
+    Adjust(sbSHDelete);
+    Adjust(sbSHDown);
+    Adjust(sbSHUp);
+    cbSHHide.Width := x;
+  end;
+  x := pnlBottom.Width;
+  Adjust(btnApply);
+  Adjust(btnCancel);
+  Adjust(btnOK);
+  cbEditShared.Width := TextWidthByFont(cbEditShared.Font.Handle, cbEditShared.Caption) + 25;
+  cbNotes.Left := cbEditShared.Left + cbEditShared.Width + 60;
+  cbNotes.Width := TextWidthByFont(cbNotes.Font.Handle, cbNotes.Caption) + 25;
+end;
+
 function TfrmTemplateEditor.AllowMove(ADropNode, ADragNode: TTreeNode): boolean;
 var
@@ -2205,4 +2260,6 @@
   MoveCopyButtons;
   tvTreeChange(FCurTree, FCurTree.Selected);
+  if FCanEditShared then
+    AdjustControls4FontChange;
 end;
 
@@ -2327,6 +2384,7 @@
 procedure TfrmTemplateEditor.btnFindClick(Sender: TObject);
 var
-  Found: boolean;
+  Found: TTreeNode;
   edtSearch: TEdit;
+  IsNext: boolean;
   FindNext: boolean;
   FindWholeWords: boolean;
@@ -2334,5 +2392,5 @@
   Tree: TTreeView;
   LastFoundNode, TmpNode: TTreeNode;
-  S1,S2: string;
+//  S1,S2: string;
 
 begin
@@ -2345,13 +2403,4 @@
     FindCase := cbShMatchCase.Checked;
     LastFoundNode := FLastFoundShNode;
-    if(FSharedEmptyNodeCount > 0) then
-    begin
-      FInternalHiddenExpand := TRUE;
-      try
-        tvShared.Items.GetFirstNode.Expand(TRUE);
-      finally
-        FInternalHiddenExpand := FALSE;
-      end;
-    end;
   end
   else
@@ -2363,53 +2412,28 @@
     FindCase := cbPerMatchCase.Checked;
     LastFoundNode := FLastFoundPerNode;
-    if(FPersonalEmptyNodeCount > 0) then
-    begin
-      FInternalHiddenExpand := TRUE;
-      try
-        tvPersonal.Items.GetFirstNode.Expand(TRUE);
-      finally
-        FInternalHiddenExpand := FALSE;
-      end;
-    end;
   end;
   if(edtSearch.text <> '') then
   begin
-    if((FindNext) and assigned (LastFoundNode)) then
-      TmpNode := LastFoundNode.GetNext
+    IsNext := ((FindNext) and assigned (LastFoundNode));
+    if IsNext then
+    
+      TmpNode := LastFoundNode
     else
       TmpNode := Tree.Items.GetFirstNode;
-    Found := FALSE;
-    if(assigned(TmpNode)) then
-    begin
-      S1 := edtSearch.Text;
-      if(not FindCase) then
-        S1 := UpperCase(S1);
-      while (assigned(TmpNode) and (not Found)) do
-      begin
-        S2 := TmpNode.Text;
-        if(not FindCase) then
-          S2 := UpperCase(S2);
-        Found := SearchMatch(S1, S2, FindWholeWords);
-        if(not Found) then
-          TmpNode := TmpNode.GetNext;
-      end;
-    end;
-    if(Found) then
-    begin
+    FInternalHiddenExpand := TRUE;
+    try
+      Found := FindTemplate(edtSearch.Text, Tree, Self, TmpNode,
+                            IsNext, not FindCase, FindWholeWords);
+    finally
+      FInternalHiddenExpand := FALSE;
+    end;
+    if Assigned(Found) then
+    begin
+      Tree.Selected := Found;
       if(Tree = tvShared) then
-        FLastFoundShNode := TmpNode
+        FLastFoundShNode := Found
       else
-        FLastFoundPerNode := TmpNode;
+        FLastFoundPerNode := Found;
       SetFindNext(Tree, TRUE);
-      Tree.Selected := TmpNode;
-    end
-    else
-    begin
-      if(FindNext) then
-        S1 := ''
-      else
-        S1 := '  "' + edtSearch.Text + '" was not Found.';
-      SetFindNext(Tree, FALSE);
-      InfoBox('Search Complete.' + S1, 'Information', MB_OK or MB_ICONINFORMATION);
     end;
   end;
@@ -2499,4 +2523,6 @@
     end;
     pnlBoilerplateResize(Self);
+    AdjustControls4FontChange;
+    MoveCopyButtons;
   end;
 end;
@@ -2516,14 +2542,14 @@
     begin
       UpdatePersonalObjects;
-      if FPersonalObjects.Count > 0 then
+      if uPersonalObjects.Count > 0 then                                                  // -------- CQ #8665 - RV ------------
       begin
         DoIt := FALSE;
         for i := 0 to dmodShared.TIUObjects.Count-1 do
-          if FPersonalObjects.IndexOf(Piece(dmodShared.TIUObjects[i],U,2)) >= 0 then
+          if uPersonalObjects.IndexOf(Piece(dmodShared.TIUObjects[i],U,2)) >= 0 then      // -------- CQ #8665 - RV ------------
             frmTemplateObjects.cboObjects.Items.Add(dmodShared.TIUObjects[i]);
       end;
     end;
     if DoIt then
-      frmTemplateObjects.cboObjects.Items.Assign(dmodShared.TIUObjects);
+      FastAssign(dmodShared.TIUObjects, frmTemplateObjects.cboObjects.Items);
     frmTemplateObjects.Font := Font;
     frmTemplateObjects.re := reBoil;
@@ -2633,4 +2659,10 @@
 end;
 
+procedure TfrmTemplateEditor.FormClose(Sender: TObject;
+  var Action: TCloseAction);
+begin
+  SaveUserBounds(Self);
+end;
+
 procedure TfrmTemplateEditor.FormCloseQuery(Sender: TObject;
   var CanClose: Boolean);
@@ -2666,4 +2698,8 @@
 procedure TfrmTemplateEditor.splBoilMoved(Sender: TObject);
 begin
+  if pnlBoilerplate.Visible and pnlGroupBP.Visible then
+    tmplEditorSplitterBoil := reBoil.Height;
+  if pnlNotes.Visible then
+    tmplEditorSplitterNotes := pnlNotes.Height;
   pnlBoilerplateResize(Self);
 end;
@@ -3105,5 +3141,6 @@
 procedure TfrmTemplateEditor.mbMainResize(Sender: TObject);
 begin
-  pnlMenu.Width := mbMain.Width + 3;
+  pnlMenu.Width := mbMain.Width + 4;
+  mbMain.Width := pnlMenu.Width - 3;
 end;
 
@@ -3179,5 +3216,5 @@
   dmodShared.LoadTIUObjects;
   UpdatePersonalObjects;
-  GetAutoGenText(AName, AText, FPersonalObjects);
+  GetAutoGenText(AName, AText, uPersonalObjects);   // -------- CQ #8665 - RV ------------
   if(AName <> '') and (AText <> '') then
   begin
@@ -3274,5 +3311,10 @@
   pnlNotes.Visible := cbNotes.Checked;
   splNotes.Visible := cbNotes.Checked;
-  splNotes.Top := pnlNotes.Top-3;
+  if cbNotes.Checked then
+  begin
+    pnlNotes.Height := tmplEditorSplitterNotes;
+    pnlNotes.Top := pnlBottom.Top - pnlNotes.Height;
+    splNotes.Top := pnlNotes.Top-3;
+  end;
   pnlBoilerplateResize(Self);
 end;
@@ -3397,8 +3439,8 @@
             if (Flds.Count > 0) then begin
               ExpandEmbeddedFields(Flds);
-              Flds.Assign(ExportTemplateFields(Flds));
+              FastAssign(ExportTemplateFields(Flds), Flds);
               for i := 0 to Flds.Count-1 do
                 Flds[i] := '  ' + Flds[i];
-              Tmpl.AddStrings(Flds);
+              FastAddStrings(Flds, Tmpl);
             end; {if}
             Tmpl.Add('</'+XMLHeader+'>');
@@ -3704,5 +3746,5 @@
             FUpdating := FALSE;
           end;
-          ShowMessage('Can not assign a Reminder Dialog to a Reason for Request');
+          ShowMsg('Can not assign a Reminder Dialog to a Reason for Request');
         end
         else
@@ -3790,5 +3832,5 @@
 end;
 
-procedure TfrmTemplateEditor.UpdatePersonalObjects;
+(*procedure TfrmTemplateEditor.UpdatePersonalObjects;
 var
   i: integer;
@@ -3803,5 +3845,5 @@
     FPersonalObjects.Sorted := TRUE;
   end;
-end;
+end;*)
 
 (*function TfrmTemplateEditor.ModifyAllowed(const Node: TTreeNode): boolean;
@@ -3875,5 +3917,5 @@
 begin
   Resync([TTemplate(Sender)]);
-  ShowMessage(Format(TemplateLockedText, [TTemplate(Sender).PrintName]));
+  ShowMsg(Format(TemplateLockedText, [TTemplate(Sender).PrintName]));
 end;
 
@@ -4074,9 +4116,9 @@
   try
     case TTemplateLinkType(pnlLink.Tag) of
-      ltTitle:     tmpSL.Assign(SubSetOfAllTitles(StartFrom, Direction));
+      ltTitle:     FastAssign(SubSetOfAllTitles(StartFrom, Direction), tmpSL);
 //      ltConsult:
       ltProcedure:
         begin
-          tmpSL.Assign(SubSetOfProcedures(StartFrom, Direction));
+          FastAssign(SubSetOfProcedures(StartFrom, Direction), tmpSL);
           for i := 0 to tmpSL.Count-1 do
           begin
@@ -4096,4 +4138,5 @@
 var
   Template,LinkTemplate: TTemplate;
+  update: boolean;
 
 begin
@@ -4104,4 +4147,5 @@
     if assigned(Template) and Template.CanModify then
     begin
+      update := true;
       if cbxLink.ItemIEN > 0 then
       begin
@@ -4109,37 +4153,21 @@
         if (assigned(LinkTemplate) and (LinkTemplate <> Template)) then
         begin
-          ShowMessage(GetLinkName(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag)) +
+          ShowMsg(GetLinkName(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag)) +
                       ' is already assigned to another template.');
-          cbxLink.ItemIndex := -1;
+          cbxLink.SelectByID(Template.LinkIEN);
+          update := False;
+        end
+        else
+        begin
+          Template.FileLink := ConvertFileLink(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag));
+          if Template.LinkName <> '' then
+            edtName.Text := copy(Template.LinkName,1,edtName.MaxLength);
         end;
-        Template.FileLink := ConvertFileLink(cbxLink.ItemID, TTemplateLinkType(pnlLink.tag));
-        if Template.LinkName <> '' then
-          edtName.Text := copy(Template.LinkName,1,edtName.MaxLength);
       end
       else
         Template.FileLink := '';
-      UpdateApply(Template);
-    end;
-  end;
-end;
-
-procedure TfrmTemplateEditor.cbxLinkSynonymCheck(Sender: TObject;
-  const Text: String; var IsSynonym: Boolean);
-var
-  LinkTemplate: TTemplate;
-  var IEN: string;
-
-begin
-  IsSynonym := FALSE;
-  if pnlLink.Visible and assigned(FShowingTemplate) then
-  begin
-    IEN := Piece(Text,#9,30);
-    if IEN <> '' then
-    begin
-      LinkTemplate := GetLinkedTemplate(IEN, TTemplateLinkType(pnlLink.Tag));
-      IsSynonym := (assigned(LinkTemplate) and (LinkTemplate <> FShowingTemplate));
-    end
-    else
-      IsSynonym := FALSE;
+      if update then
+        UpdateApply(Template);
+    end;
   end;
 end;
