unit TeiHeader;

{
[TeiHeader] [1.1]
Delphi 2005
December 2005

LICENSE

The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
"http://www.mozilla.org/MPL/"

Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
the specific language governing rights and limitations under the License.

The Original Code is "[teiHeader.pas]".

The Initial Developer of the Original Code is Martin Holmes (Victoria,
BC, Canada, "http://www.mholmes.com/"). Copyright (C) 2005 Martin Holmes 
and the University of Victoria Computing and Media Centre. The code was 
co-developed for university and personal projects, and rights are shared
by Martin Holmes and the University of Victoria. All Rights Reserved.
}

{
Written by Martin Holmes, September 2005.

This form provides a simple interface to the teiHeader component of the
XML file, allowing the user to edit it, and also provides basic well-formedness
checking.

Dependencies:

MSXML (through my XMLUtilities unit)
TntUnicodeControls (Troy Wolbrink)
FormState (for saving size, position etc.)

}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  TntForms, Dialogs, FormState, StdCtrls, Buttons, TntButtons, TntStdCtrls,
  xmldom, XMLIntf, msxmldom, XMLDoc, TntDialogs, XMLUtilities, 
  ComCtrls, TntComCtrls, ToolWin, IconsIncluder, SynEditHighlighter,
  SynHighlighterXML, SynEdit, SynMemo;

type
  TufrmTeiHeader = class(TTntForm)
    ubbnTeiHeaderOK: TTntBitBtn;
    ubbnTeiHeaderCancel: TTntBitBtn;
    ustEditTeiHeader: TTntStaticText;
    XMLDoc: TXMLDocument;
    utbrTeiHeader: TTntToolBar;
    utbEditUndo: TTntToolButton;
    utbEditCut: TTntToolButton;
    utbEditCopy: TTntToolButton;
    utbEditPaste: TTntToolButton;
    utbEditDelete: TTntToolButton;
    utbEditSelectAll: TTntToolButton;
    umsgNotWellFormed: TTntLabel;
    usmTeiHeader: TSynMemo;
    xmlhlTeiHeader: TSynXMLSyn;
    utbSep1: TTntToolButton;
    utbFind: TTntToolButton;
    utbInsertTag: TTntToolButton;
    procedure usmTeiHeaderKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure TntFormActivate(Sender: TObject);
    procedure ubbnTeiHeaderCancelClick(Sender: TObject);
    procedure ubbnTeiHeaderOKClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
  private
    { Private declarations }
    FFormStateSaver: TFormStateSaver;
    FWellFormed: Boolean;
  public
    { Public declarations }
    function CheckXMLOK: Boolean;
  published
    property WellFormed: Boolean read FWellFormed write FWellFormed default True;
  end;

var
  ufrmTeiHeader: TufrmTeiHeader;

implementation

uses Main;

{$R *.dfm}

procedure TufrmTeiHeader.FormShow(Sender: TObject);
begin
  FFormStateSaver := TFormStateSaver.Create(Self, True);
end;

procedure TufrmTeiHeader.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(FFormStateSaver);
end;

procedure TufrmTeiHeader.ubbnTeiHeaderOKClick(Sender: TObject);
begin
  if CheckXMLOK then
    ModalResult := mrOK
  else
    ModalResult := mrNone; //prevent the window from closing
end;

procedure TufrmTeiHeader.ubbnTeiHeaderCancelClick(Sender: TObject);
begin
  ModalResult := mrCancel;
  Hide;
end;

function TufrmTeiHeader.CheckXMLOK: Boolean;
var
ErrorLineNum, ErrorLinePos: integer;
Temp: WideString;
Messidge: WideString;

begin
  WellFormed := ufrmXMLUtilities.IsWellFormedXMLTree(usmTeiHeader.Text);
  if WellFormed then
    Result := True
  else
    with usmTeiHeader do
      begin
        SelStart := Perform(EM_LINEINDEX, ErrorLineNum-1, 0) ;
        SelLength := Length(Lines[ErrorLineNum-1]);
        SetFocus;
      case WideMessageDlg(umsgNotWellFormed.Caption, mtWarning, mbYesNoCancel, 0) of
        mrYes:
          begin
            Text := ufrmXMLUtilities.FixImproperlyNestedTags(Text);
          end;
        else
          begin
            Result := False;
          end;
      end; //end case
    end;
end;

procedure TufrmTeiHeader.TntFormActivate(Sender: TObject);
begin
  ufrmMain.LastFocusWin := lfwTeiHeader;
end;

procedure TufrmTeiHeader.usmTeiHeaderKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = 69 then
    if Shift = [ssCtrl] then
      ufrmMain.aInsertTagExecute(ufrmMain.aInsertTag);
end;

end.
