unit frmScheduleEdit;

interface

uses
  ScheduleItemEditor, ActiveX, mshtml,
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, PageMngr, ComCtrls, ImgList, Planner, Menus,
  xmldom, XMLIntf, oxmldom, XMLDoc, OleCtrls, SHDocVw, msxmldom, FileCtrl;

type
  TScheduleEditForm = class(TForm)
    PageManager1: TPageManager;
    nbSchedule: TNotebook;
    Panel1: TPanel;
    PageProxy6: TPageProxy;
    PageProxy1: TPageProxy;
    PageProxy2: TPageProxy;
    PageProxy3: TPageProxy;
    PageProxy4: TPageProxy;
    PageProxy5: TPageProxy;
    FontDlg: TFontDialog;
    imglstTemplet: TImageList;
    PlannerPlay: TPlanner;
    Panel2: TPanel;
    rdogroupUnit: TRadioGroup;
    btnAddplay: TButton;
    btnEditplay: TButton;
    btnDeleteplay: TButton;
    popPlayItem: TPopupMenu;
    NEditItem: TMenuItem;
    N2: TMenuItem;
    NDeleteItem: TMenuItem;
    XMLDocScheduler: TXMLDocument;
    XML: TXMLDocument;
    Panel4: TPanel;
    Panel3: TPanel;
    Panel5: TPanel;
    lvTemplet: TListView;
    Label12: TLabel;
    Panel6: TPanel;
    btnPrior: TButton;
    btnNext: TButton;
    btnClose: TButton;
    Panel7: TPanel;
    Label14: TLabel;
    pnlPreView: TPanel;
    wbPreview: TWebBrowser;
    Panel8: TPanel;
    Label13: TLabel;
    memoTemplet: TMemo;
    Bevel1: TBevel;
    Bevel2: TBevel;
    savedlg: TSaveDialog;
    pnlSave: TPanel;
    rdobtnOrg: TRadioButton;
    rdobtnNew: TRadioButton;
    edtFilename: TEdit;
    btnBrows: TButton;
    Label1: TLabel;
    pnlSubTitle: TPanel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    lbFontSample: TLabel;
    rdobtnNoSub: TRadioButton;
    rdobtnHaveSub: TRadioButton;
    coboxSubPlace: TComboBox;
    coboxSubDir: TComboBox;
    TrackBarSubSpeed: TTrackBar;
    btnFont: TButton;
    procedure PlannerPlayPlannerRightClick(Sender: TObject; Position, FromSel,
      FromSelPrecise, ToSel, ToSelPrecise: Integer);
    procedure rdogroupUnitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure PlannerPlayItemDelete(Sender: TObject; Item: TPlannerItem);
    procedure PlannerPlayItemEndEdit(Sender: TObject; Item: TPlannerItem);
    procedure PageManager1PageChanged(Sender: TObject);
    procedure lvTempletSelectItem(Sender: TObject; Item: TListItem;
      Selected: Boolean);
    procedure PlannerPlayPlannerHeaderDraw(Sender: TObject;
      Canvas: TCanvas; Rect: TRect; Index: Integer; var DoDraw: Boolean);
    procedure PageManager1GetNextPage(CurrentPage: Integer;
      var NewPage: Integer);
    procedure btnAddplayClick(Sender: TObject);
    procedure btnDeleteplayClick(Sender: TObject);
    procedure btnEditplayClick(Sender: TObject);
    procedure NEditItemClick(Sender: TObject);
    procedure NDeleteItemClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
    procedure btnNextClick(Sender: TObject);
    procedure rdobtnOrgClick(Sender: TObject);
    procedure rdobtnNewClick(Sender: TObject);
    procedure btnFontClick(Sender: TObject);
    procedure rdobtnNoSubClick(Sender: TObject);
    procedure rdobtnHaveSubClick(Sender: TObject);
    procedure coboxSubPlaceChange(Sender: TObject);
    procedure btnBrowsClick(Sender: TObject);
    procedure edtFilenameChange(Sender: TObject);
    procedure lvTempletResize(Sender: TObject);
    procedure FormResize(Sender: TObject);
    procedure nbSchedulePageChanged(Sender: TObject);
  private
    { Private declarations }
    ScheItemItemEditor: TGCScheItemItemEditor;
    templetName: string;
    templethtml: string;
    DisplayAreaCount: Integer;
    procedure preview;
  public
    { Public declarations }
    IsNew : Boolean;
    orgSchedulerFileName : string;
    SchedulerFileName : string;
    procedure inittemplet;
    procedure initpreview;
    procedure initNew;
    procedure Load(fn: string);
    function CheckSchedule: Boolean;

    procedure CreateSubtitleNode;
    procedure CreateTempletNode;
    procedure CreatePlanNode;
  end;

var
  ScheduleEditForm: TScheduleEditForm;

function NewScheduler: string;
function EditScheduler(ScheduleName: string): string;

implementation

uses PKSTutils;

{$R *.dfm}

function NewScheduler: string;
begin
  Result := '';
  with TScheduleEditForm.Create(Application) do
  try
    initNew;
    if ShowModal=mrok then
      Result := SchedulerFileName;
  finally
    Free;
  end;
end;

function EditScheduler(ScheduleName: string): string;
begin
  Result := '';
  with TScheduleEditForm.Create(Application) do
  try
    IsNew := False;
    Load(ScheduleName);
    if ShowModal=mrok then
      Result := SchedulerFileName;
  finally
    Free;
  end;
end;

procedure TScheduleEditForm.PlannerPlayPlannerRightClick(Sender: TObject;
  Position, FromSel, FromSelPrecise, ToSel, ToSelPrecise: Integer);
begin
  PlannerPlay.Items.Selected:=nil;

  if PlannerPlay.Items.HasItem(fromSel,toSel,Position) then
  begin
    ShowMessage('　　这个时间区段已经存在一个播放计划，'+#13#10+
                '请选择新的区段安排播放计划，或者调整时'+#13#10+
                '间单位以显示更准确的时间。');
    Exit;
  end;

  PlannerPlay.Items.BeginUpdate;
  ScheItemItemEditor.EditState := esNew;
  with (PlannerPlay.Items.Add) do
  begin
    AllowOverlap := False;
//    Name := 'Test';
    ItemPos := Position;
    ItemEnd := ToSel;
    ItemBegin := FromSel;
    PopupEdit;
  end;
  ScheItemItemEditor.EditState := esEdit;
  PlannerPlay.Items.EndUpdate;
end;

procedure TScheduleEditForm.rdogroupUnitClick(Sender: TObject);
begin
  case rdogroupUnit.ItemIndex of
    0:  PlannerPlay.Display.DisplayUnit := 1;
    1:  PlannerPlay.Display.DisplayUnit := 5;
    2:  PlannerPlay.Display.DisplayUnit := 10;
    3:  PlannerPlay.Display.DisplayUnit := 15;
    4:  PlannerPlay.Display.DisplayUnit := 30;
    5:  PlannerPlay.Display.DisplayUnit := 60;
  end;
  PlannerPlay.Items.BeginUpdate;
  PlannerPlay.Items.EndUpdate;
end;

procedure TScheduleEditForm.FormCreate(Sender: TObject);
begin
  OleInitialize(nil);
  ScheItemItemEditor := TGCScheItemItemEditor.Create(Application);
  ScheItemItemEditor.EditorUse := euDblClick;
  ScheItemItemEditor.EditState := esEdit;
  PlannerPlay.DefaultItem.Editor := ScheItemItemEditor;
  wbPreview.Navigate('about:blank');
  initpreview;
  DisplayAreaCount := 0;
  nbSchedule.PageIndex := 0;
  IsNew := False;
end;

procedure TScheduleEditForm.FormDestroy(Sender: TObject);
begin
  PlannerPlay.DefaultItem.Editor := nil;
  ScheItemItemEditor.Free;
  OleUninitialize;
end;

procedure TScheduleEditForm.PlannerPlayItemDelete(Sender: TObject;
  Item: TPlannerItem);
begin
  Item.Free;
end;

procedure TScheduleEditForm.PlannerPlayItemEndEdit(Sender: TObject;
  Item: TPlannerItem);
begin
  if (ScheItemItemEditor.EditState=esNew) and (ScheItemItemEditor.ModalResult<>mrOk) then
    Item.Free;
end;

procedure TScheduleEditForm.PageManager1PageChanged(Sender: TObject);
var
  OldUnit: Integer;
begin
{  if nbSchedule.ActivePage = '样式选择' then
  begin
    PlannerPlay.Positions := DisplayAreaCount;
    PageManager1.NextBtn := btnNext;
    btnNext.Caption := '下一步';
    exit;
  end; {}
  if nbSchedule.ActivePage = '播放计划' then
  begin
    OldUnit := PlannerPlay.Display.DisplayUnit;
    PlannerPlay.Display.DisplayUnit := 1;
    PlannerPlay.Positions := DisplayAreaCount;
    PlannerPlay.Display.DisplayUnit := OldUnit;
    PageManager1.NextBtn := nil;
    btnNext.Caption := '下一步';
    exit;
  end;
  if nbSchedule.ActivePage = '保存计划' then
  begin
    PageManager1.NextBtn := nil;
    btnNext.Caption := '完成';
    exit;
  end;
  PageManager1.NextBtn := btnNext;
  if PageManager1.NextBtn<>nil then
    btnNext.Caption := '下一步';
  if nbSchedule.ActivePage = '样式选择' then
  begin
    inittemplet;
    exit;
  end;
end;

procedure TScheduleEditForm.inittemplet;
var
  AttrIndex: TFileAttr;
  FileExt: string;
  MaskPtr: PChar;
  Ptr: PChar;
  AttrWord: Integer;
  FileInfo: TSearchRec;
const
   Attributes: array[TFileAttr] of Word = (faReadOnly, faHidden, faSysFile,
     faVolumeID, faDirectory, faArchive, 0);
begin
  templetName := '';
  AttrWord := faAnyFile;
  begin
    ChDir(SlashSep(ExtractFilePath(Application.ExeName),'templet')); { go to the directory we want }

    lvTemplet.Clear;
    lvTemplet.Items.BeginUpdate;
    try
      MaskPtr := '*.GCT';
      if FindFirst(MaskPtr, AttrWord, FileInfo) = 0 then
      begin
        repeat            { exclude normal files if ftNormal not set }
          if (FileInfo.Attr and AttrWord <> 0) then
            if FileInfo.Attr and faDirectory <> 0 then
            begin
            end
            else
            begin
              FileExt := AnsiLowerCase(ExtractFileExt(FileInfo.Name));
              if (UpperCase(FileExt) = '.GCT') then
                with lvTemplet.Items.Add do
                begin
                  ImageIndex := 0;
                  Caption := ChangeFileExt(ExtractFileName(FileInfo.Name), '');
                end;
            end;
        until FindNext(FileInfo) <> 0;
        FindClose(FileInfo);
      end;
    finally
     lvTemplet.Items.EndUpdate;
    end;
  end;
end;

procedure TScheduleEditForm.lvTempletSelectItem(Sender: TObject;
  Item: TListItem; Selected: Boolean);
var
  fn: string;
begin
  if templetName = Item.Caption then exit;
  fn := SlashSep(ExtractFilePath(Application.ExeName), 'templet\'+Item.Caption+'.GCT');
  XML.FileName := fn;
  try
    XML.Active := True;
    templethtml := XML.XML.Text;
    Preview;
  except
    XML.Active := False;
    XML.XML.Clear;
    XML.FileName := '';
    templetName := '';
    templethtml := '';
    DisplayAreaCount := 0;
    memoTemplet.Lines.Clear;
    memoTemplet.Lines.Add('不是一个合法的模板，或者模板已经损坏');
    initpreview;
    PageManager1.CheckBtnEnabled;
    Exit;
  end;
  templetName := Item.Caption;
  PageManager1.CheckBtnEnabled;
end;

procedure TScheduleEditForm.initpreview;
var
  vv: Variant;
  HTMLDocument: IHTMLDocument2;
begin
  OleInitialize(nil);
  vv := VarArrayCreate([0,0],varVariant);
  vv[0] := '<html><body scroll=''no''><body><html>';
  HTMLDocument := wbPreview.Document as IHTMLDocument2;
  HTMLDocument.write(PSafeArray(TVarData(vv).VArray));
  HTMLDocument.close;
  vv[0] := Unassigned;
end;

procedure TScheduleEditForm.preview;
var
  vv: Variant;
  AStream: TMemoryStream;
  HTMLDocument: IHTMLDocument2;

  procedure ProcNodes(ANode: IXMLNode);
  var
    i: Integer;
  begin
    for i:= 0 to ANode.ChildNodes.Count-1 do
    begin
      if ANode.ChildNodes.Nodes[i].NodeName='div' then
        if pos('GCPLAYAREA', ANode.ChildNodes.Nodes[i].Attributes['id']) = 1 then
        begin
          Inc(DisplayAreaCount);
          with ANode.ChildNodes.Nodes[i].AddChild('div') do
          begin
            Attributes['align'] := 'center';
            with AddChild('table') do
            begin
              Attributes['width']:='100%';
              Attributes['height']:='100%';
              Attributes['border']:='1';
              Attributes['cellpadding']:='0';
              Attributes['cellspacing']:='0';
              with AddChild('tr') do
                with AddChild('td') do
                  with AddChild('div') do
                  begin
                    Attributes['align'] := 'center';
                    Text := StringReplace(ANode.ChildNodes.Nodes[i].Attributes['id'],'GCPLAYAREA','',[]);
                  end;
            end;
          end;
          continue;
        end;
      ProcNodes(ANode.ChildNodes.Nodes[i]);
    end;
  end;
begin
  XML.Active := False;
  XML.FileName := '';
  XML.XML.Text := templethtml;
  XML.Active := True;

  DisplayAreaCount := 0;
  memoTemplet.Clear;
  if XML.Node.ChildNodes.Count>1 then
  begin
    if XML.Node.ChildNodes.Nodes[0].NodeType = ntComment then
      memoTemplet.Lines.Add(XML.Node.ChildNodes.Nodes[0].Text);
  end;
  ProcNodes(XML.Node);

  vv := VarArrayCreate([0,0],varVariant);
  vv[0] := XML.XML.Text;
  HTMLDocument := wbPreview.Document as IHTMLDocument2;
  HTMLDocument.write(PSafeArray(TVarData(vv).VArray));
  HTMLDocument.close;
  vv := Unassigned;
  XML.Active := false;
end;

procedure TScheduleEditForm.initNew;
begin
  IsNew := true;
  XMLDocScheduler.Active := True;
  with XMLDocScheduler.AddChild('scheduler') do
  begin
    AddChild('subtitle');
    AddChild('templet').IsTextElement;
    AddChild('plan');
  end;
end;

procedure TScheduleEditForm.Load(fn: string);
var
  i,j: Integer;
  NCount: integer;
  Node: IXMLNode;
  FontNode: IXMLNode;
  PlanNode: IXMLNode;
  ItemNode: IXMLNode;
  Style: TFontStyles;
begin
  SchedulerFileName := fn;
  XMLDocScheduler.Active := False;
  XMLDocScheduler.FileName := fn;
  XMLDocScheduler.Active := True;

  if XMLDocScheduler.ChildNodes.FindNode('scheduler')=nil then
  begin
    Showmessage('对不起，这不是一个合法的播放计划文件');
    abort;
  end;

  with XMLDocScheduler.ChildNodes.FindNode('scheduler') do
  begin
    Node := ChildNodes.FindNode('subtitle');
    if Node<>nil then
    begin
      with Node do
      begin
        if StrToBool(Attributes['nosubtitle']) then
          rdobtnNoSub.Checked := true
        else
        begin
          rdobtnNoSub.Checked := False;
          rdobtnHaveSub.Checked := True;
          coboxSubPlace.ItemIndex := coboxSubPlace.Items.IndexOf(ChildValues['place']);
          coboxSubPlaceChange(coboxSubPlace);
          coboxSubDir.ItemIndex := coboxSubDir.Items.IndexOf(ChildValues['direction']);
          TrackBarSubSpeed.Position := StrToInt(ChildValues['speed']);
          FontNode := ChildNodes.FindNode('font');
          if FontNode<>nil then
          begin
            FontDlg.Font.Name := FontNode.Attributes['name'];
            FontDlg.Font.Color := TColor(strtoint('$'+FontNode.Attributes['color']));
            FontDlg.Font.Size := StrToInt(FontNode.Attributes['size']);
            Style := [];
            if StrToBool(FontNode.Attributes['bold']) then
              Include(Style, fsbold);
            if StrToBool(FontNode.Attributes['italic']) then
              Include(Style, fsitalic);
            if StrToBool(FontNode.Attributes['underline']) then
              Include(Style, fsunderline);
            if StrToBool(FontNode.Attributes['strikeout']) then
              Include(Style, fsstrikeout);
            FontDlg.Font.Style := Style;
            lbFontSample.Font := FontDlg.Font;
          end;
        end;
      end;
    end;

    Node := ChildNodes.FindNode('templet');
    if Node<>nil then
    begin
      templethtml := Node.ChildNodes.Nodes[0].Text;
      templethtml := StringReplace(templethtml, #$A#$A, #$D#$A, [rfReplaceAll]);
      preview;
    end;

    Node := ChildNodes.FindNode('plan');
    if Node<>nil then
    begin
      PlannerPlay.Positions := Node.ChildNodes.Count;
      NCount := 0;
      for i:= 0 to Node.ChildNodes.Count-1 do
      begin
        PlanNode := Node.ChildNodes.FindNode('displayarea'+inttostr(i+1));
        if PlanNode=nil then Continue;
        inc(NCount);
        ItemNode := PlanNode.ChildNodes.FindNode('planitem');
        while ItemNode<>nil do
        begin
          with PlannerPlay.Items.Add do
          begin
            ItemPos := i;
            Attachement := ItemNode.Attributes['filename'];
            ItemStartTime := StrToTime(ItemNode.Attributes['start']);
            if ItemNode.Attributes['end']='0:00:00' then
              ItemEnd := PlannerPlay.Display.ActiveEnd
            else
              ItemEndTime := StrToTime(ItemNode.Attributes['end']);
            Text.Text := Attachement;
          end;

          ItemNode := ItemNode.NextSibling;
        end;
      end;
      PlannerPlay.Positions := NCount;
    end;
  end;
//  XMLDocScheduler.Active := false;
//  XMLDocScheduler.FileName := '';
  preview;
end;

procedure TScheduleEditForm.PlannerPlayPlannerHeaderDraw(Sender: TObject;
  Canvas: TCanvas; Rect: TRect; Index: Integer; var DoDraw: Boolean);
var
  s: string;
begin
  if Index>=1 then
  begin
    Canvas.Rectangle(Rect.Left,Rect.Top,Rect.Right,Rect.Bottom);
    InflateRect(Rect,-2,-2);
    s := '播放区域'+inttostr(Index);
    DrawText(Canvas.Handle, pchar(s),length(s),Rect, DT_CENTER or DT_VCENTER);
  end;
end;

procedure TScheduleEditForm.PageManager1GetNextPage(CurrentPage: Integer;
  var NewPage: Integer);
begin
  case CurrentPage of
    1: if DisplayAreaCount=0 then
        NewPage := -1;
    2: begin

      end;
  end;
end;

function TScheduleEditForm.CheckSchedule: Boolean;
var
  i: integer;
  spare: Integer;
  OldUnit: Integer;
begin
  OldUnit := PlannerPlay.Display.DisplayUnit;
  PlannerPlay.Display.DisplayUnit := 1;
  spare := (PlannerPlay.Display.ActiveEnd-PlannerPlay.Display.ActiveStart)*DisplayAreaCount;
  for i := 0 to PlannerPlay.Items.Count-1 do
  begin
    with PlannerPlay.Items.Items[i] do
    begin
      if ItemPos>= DisplayAreaCount then
        Continue;
      spare := spare - (ItemEnd-ItemBegin);
    end;
  end;
  PlannerPlay.Display.DisplayUnit := OldUnit;
  Result := spare=0;
end;

procedure TScheduleEditForm.btnAddplayClick(Sender: TObject);
begin
  PlannerPlay.Items.Selected:=nil;

  PlannerPlay.Items.BeginUpdate;
  ScheItemItemEditor.EditState := esNew;
  with (PlannerPlay.Items.Add) do
  begin
    AllowOverlap := False;
    ItemPos := 0;
    ItemEnd := 0;
    ItemBegin := 0;
    PopupEdit;
  end;
  ScheItemItemEditor.EditState := esEdit;
  PlannerPlay.Items.EndUpdate;
end;

procedure TScheduleEditForm.btnDeleteplayClick(Sender: TObject);
begin
  if PlannerPlay.Items.Selected<> nil then
    PlannerPlay.Items.Selected.Free;
end;

procedure TScheduleEditForm.btnEditplayClick(Sender: TObject);
begin
  if PlannerPlay.Items.Selected<> nil then
    PlannerPlay.Items.Selected.PopupEdit;
end;

procedure TScheduleEditForm.NEditItemClick(Sender: TObject);
begin
  if TObject(popPlayItem.PopupComponent) is TPlannerItem then
    TPlannerItem(popPlayItem.PopupComponent).PopupEdit;
end;

procedure TScheduleEditForm.NDeleteItemClick(Sender: TObject);
begin
  if TObject(popPlayItem.PopupComponent) is TPlannerItem then
    TPlannerItem(popPlayItem.PopupComponent).Free;
end;

Procedure TScheduleEditForm.CreatePlanNode;
var
  i: integer;
  iPos: integer;
  Node: IXMLNode;
begin
  Node := XMLDocScheduler.ChildNodes.Nodes[0].ChildNodes.FindNode('plan');
  if Node = nil then
    Node := XMLDocScheduler.ChildNodes.Nodes[0].AddChild('plan');
  Node.ChildNodes.Clear;
  for i:= 1 to DisplayAreaCount do
    Node.AddChild('displayarea'+inttostr(i));
  for i := 0 to PlannerPlay.Items.Count-1 do
  begin
    iPos := PlannerPlay.Items.Items[i].ItemPos+1;
    if iPos > DisplayAreaCount then Continue;
    with Node.ChildNodes.FindNode('displayarea'+inttostr(iPos)) do
      with AddChild('planitem') do
      begin
        Attributes['filename'] := PlannerPlay.Items.Items[i].Attachement;
        Attributes['start'] := TimeToStr(PlannerPlay.Items.Items[i].ItemStartTime);
        Attributes['end'] := TimeToStr(PlannerPlay.Items.Items[i].ItemEndTime);
      end;
  end;
end;

procedure TScheduleEditForm.btnCloseClick(Sender: TObject);
begin
  if nbSchedule.ActivePage<>'结束' then
  begin
    ModalResult := mrCancel;
    Close;
  end;
end;

procedure TScheduleEditForm.btnNextClick(Sender: TObject);
begin
  if nbSchedule.PageIndex<2 then
    exit;
  if btnNext.Caption = '完成' then
  begin
    CreateSubtitleNode;
    CreateTempletNode;
    CreatePlanNode;
    XMLDocScheduler.SaveToFile(SchedulerFileName);
    ModalResult := mrOk;
  end;
  if btnNext.Caption = '下一步' then
  begin
    if not CheckSchedule then
    begin
      ShowMessage('计划中存在未安排时间段，请先将所有时间段安排完毕！');
      exit;
    end;
    PageManager1.NextPage;
    btnNext.Caption := '完成';
    if IsNew then
    begin
      rdobtnNew.Checked := True;
      rdobtnOrg.Checked := false;
      rdobtnOrg.Enabled := false;
    end
    else
    begin
      rdobtnOrg.Checked := True;
      rdobtnNew.Checked := false;
    end;
  end;
end;

procedure TScheduleEditForm.rdobtnOrgClick(Sender: TObject);
begin
  if rdobtnOrg.Checked then
  begin
    edtFilename.Enabled := False;
    btnBrows.Enabled := False;
    SchedulerFileName := orgSchedulerFileName;
  end;
end;

procedure TScheduleEditForm.rdobtnNewClick(Sender: TObject);
begin
  if rdobtnNew.Checked then
  begin
    edtFilename.Enabled := True;
    btnBrows.Enabled := True;
    SchedulerFileName := edtFilename.Text;
  end;
end;

procedure TScheduleEditForm.CreateSubtitleNode;
var
  Node: IXMLNode;
begin
  Node := XMLDocScheduler.ChildNodes.Nodes[0].ChildNodes.FindNode('subtitle');
  if Node=nil then
    Node := XMLDocScheduler.ChildNodes.Nodes[0].AddChild('subtitle');
  Node.ChildNodes.Clear;
  Node.Attributes['nosubtitle'] := BoolToStr(rdobtnNoSub.Checked,true);
  if rdobtnHaveSub.Checked then
  begin
    with Node do
    begin
      AddChild('place').Text := coboxSubPlace.Text;
      AddChild('direction').Text := coboxSubDir.Text;
      AddChild('speed').Text := IntToStr(TrackBarSubSpeed.Position);
      with AddChild('font') do
      begin
        Attributes['name'] := lbFontSample.Font.Name;
        Attributes['color'] := IntToHex(Integer(lbFontSample.Font.Color), 8);
        Attributes['size'] := IntToStr(lbFontSample.Font.Size);
        Attributes['bold'] := BoolToStr((fsbold in lbFontSample.Font.Style),true);
        Attributes['italic'] := BoolToStr((fsItalic in lbFontSample.Font.Style),true);
        Attributes['underline'] := BoolToStr((fsUnderline in lbFontSample.Font.Style),true);
        Attributes['strikeout'] := BoolToStr((fsStrikeOut in lbFontSample.Font.Style),true);
      end;
    end;
  end;
end;

procedure TScheduleEditForm.CreateTempletNode;
var
  Node: IXMLNode;
begin
  Node := XMLDocScheduler.ChildNodes.Nodes[0].ChildNodes.FindNode('templet');
  if Node=nil then
    Node := XMLDocScheduler.ChildNodes.Nodes[0].AddChild('templet');
  Node.ChildNodes.Clear;
  Node.ChildNodes.Add(XMLDocScheduler.CreateNode(widestring(templethtml),ntCData));
end;

procedure TScheduleEditForm.btnFontClick(Sender: TObject);
begin
  if FontDlg.Execute then
    lbFontSample.Font := FontDlg.Font;
end;

procedure TScheduleEditForm.rdobtnNoSubClick(Sender: TObject);
begin
  if rdobtnNoSub.Checked then
  begin
    coboxSubPlace.Enabled := False;
    coboxSubDir.Enabled := False;
    TrackBarSubSpeed.Enabled := False;
    btnFont.Enabled := False;
  end;
end;

procedure TScheduleEditForm.rdobtnHaveSubClick(Sender: TObject);
begin
  if rdobtnHaveSub.Checked then
  begin
    coboxSubPlace.Enabled := True;
    if coboxSubPlace.ItemIndex<0 then
      coboxSubPlace.ItemIndex := 0;
    coboxSubDir.Enabled := True;
    TrackBarSubSpeed.Enabled := True;
    btnFont.Enabled := True;
  end;
end;

procedure TScheduleEditForm.coboxSubPlaceChange(Sender: TObject);
begin
  if coboxSubPlace.ItemIndex <=1 then
  begin
    if coboxSubDir.Items.Count>0 then
      if coboxSubDir.Items.Strings[0] = '从左到右' then
      begin
        if coboxSubDir.ItemIndex<0 then
          coboxSubDir.ItemIndex := 0;
        exit;
      end;
    coboxSubDir.Items.Clear;
    coboxSubDir.Items.Add('从左到右');
    coboxSubDir.Items.Add('从右到左');
    coboxSubDir.ItemIndex := 0;
  end
  else
  begin
    if coboxSubDir.Items.Count>0 then
      if coboxSubDir.Items.Strings[0] = '从上到下' then
      begin
        if coboxSubDir.ItemIndex<0 then
          coboxSubDir.ItemIndex := 0;
        exit;
      end;
    coboxSubDir.Items.Clear;
    coboxSubDir.Items.Add('从上到下');
    coboxSubDir.Items.Add('从下到上');
    coboxSubDir.ItemIndex := 0;
  end
end;

procedure TScheduleEditForm.btnBrowsClick(Sender: TObject);
begin
  savedlg.InitialDir := SlashSep(ExtractFilePath(Application.ExeName),'Schedule');
  if savedlg.Execute then
    edtFilename.Text := savedlg.FileName;
  SchedulerFileName := edtFilename.Text;
end;

procedure TScheduleEditForm.edtFilenameChange(Sender: TObject);
begin
  SchedulerFileName := edtFilename.Text;
end;

procedure TScheduleEditForm.lvTempletResize(Sender: TObject);
begin
  lvTemplet.Arrange(arDefault);
end;

procedure TScheduleEditForm.FormResize(Sender: TObject);
begin
  pnlSave.Left := (nbSchedule.Width-pnlSave.Width) div 2;
  pnlSave.top := (nbSchedule.Height-pnlSave.Height) div 2;
  pnlSubTitle.Left := (nbSchedule.Width-pnlSubTitle.Width) div 2;
  pnlSubTitle.top := (nbSchedule.Height-pnlSubTitle.Height) div 2;
end;

procedure TScheduleEditForm.nbSchedulePageChanged(Sender: TObject);
begin
  Caption := '计划向导 - '+nbSchedule.ActivePage;
end;

end.
