unit LayerList;

{
[LayerList] [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 "[layerlist.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.
}

{
 This form shows a list of the annotation titles in a Unicode check list box.

 Dependencies:
  TTntUnicode libraries (Troy Wolbrink).
  FormState library (for reading and writing position, size etc.)
}

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, TntForms, StdCtrls, CheckLst, TntCheckLst, FormState, ImgMarkup,
  TntStdCtrls, Buttons, TntButtons, AppEvnts;

type
  TufrmAreaList = class(TTntForm)
    uclbAreaList: TTntCheckListBox;
    ucbKeepOnTop: TTntCheckBox;
    usbUp: TTntSpeedButton;
    usbDown: TTntSpeedButton;
    aeAreaList: TApplicationEvents;
    procedure uclbAreaListDblClick(Sender: TObject);
    procedure aeAreaListIdle(Sender: TObject; var Done: Boolean);
    procedure usbDownClick(Sender: TObject);
    procedure usbUpClick(Sender: TObject);
    procedure TntFormDestroy(Sender: TObject);
    procedure ucbKeepOnTopClick(Sender: TObject);
    procedure uclbAreaListKeyUp(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure uclbAreaListClickCheck(Sender: TObject);
    procedure uclbAreaListClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
  private
    FFormStateSaver: TFormStateSaver;
    { Private declarations }
  public
    procedure FindAndDeleteItem(L: TPositionedLayerWithXML);
    procedure FindAndSetTitle(L: TPositionedLayerWithXML; NewTitle: WideString);
    procedure FindAndSelectItem(L: TPositionedLayerWithXML);
    procedure ResequenceLayers; //This puts the layers on the image into the same order
                                //as the list; they may have been reordered with SendToBack
                                //when unchecked.
    procedure SendUncheckedToBack; //This sends unchecked layers back after resequencing
    { Public declarations }
  end;

var
  ufrmAreaList: TufrmAreaList;

implementation

{$R *.dfm}

procedure TufrmAreaList.FormShow(Sender: TObject);
begin
  FFormStateSaver := TFormStateSaver.Create(Self, True);
end;

procedure TufrmAreaList.uclbAreaListClick(Sender: TObject);
begin
  ufrmImgMarkup.Selection :=
  TPositionedLayerWithXML(uclbAreaList.Items.Objects[uclbAreaList.ItemIndex]);
end;

procedure TufrmAreaList.FindAndSetTitle(L: TPositionedLayerWithXML; NewTitle: WideString);
var
i: integer;

begin
  with uclbAreaList do
    begin
      if Items.Count < 1 then
        Exit;
      for i := 0 to Items.Count-1 do
        begin
          if Items.Objects[i] = L then
            begin
              Items[i] := NewTitle;
              Exit;
            end;
        end;
    end;
end;

procedure TufrmAreaList.FindAndDeleteItem(L: TPositionedLayerWithXML);
var
i: integer;

begin
  with uclbAreaList do
    begin
      if Items.Count < 1 then
        Exit;
      for i := Items.Count-1 downto 0 do
        begin
          if Items.Objects[i] = L then
            begin
              Items.Objects[i] := nil;
              Items.Delete(i);
              Exit;
            end;
        end;
    end;
end;

procedure TufrmAreaList.FindAndSelectItem(L: TPositionedLayerWithXML);
var
i: integer;

begin
  with uclbAreaList do
    begin
      if Items.Count < 1 then
        Exit;
      for i := 0 to Items.Count-1 do
        begin
          if Items.Objects[i] = L then
            begin
              ItemIndex := i;
              Exit;
            end;
        end;
    end;
end;

procedure TufrmAreaList.uclbAreaListClickCheck(Sender: TObject);
var
CurrItem: integer;

begin
  with uclbAreaList do
    if ItemIndex > -1 then
      begin
        TPositionedLayerWithXML(Items.Objects[ItemIndex]).DrawShape := Checked[ItemIndex];
        if not Checked[ItemIndex] then
          TPositionedLayerWithXML(Items.Objects[ItemIndex]).SendToBack;
        ufrmImgMarkup.ImgView.Invalidate;
      end;

end;

procedure TufrmAreaList.uclbAreaListKeyUp(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  if Key = VK_DELETE then
    ufrmImgMarkup.aDeleteAreaExecute(ufrmImgMarkup.aDeleteArea);
end;

procedure TufrmAreaList.ucbKeepOnTopClick(Sender: TObject);
begin
  if ucbKeepOnTop.Checked then
    FormStyle := fsStayOnTop
  else
    FormStyle := fsNormal;
end;

procedure TufrmAreaList.TntFormDestroy(Sender: TObject);
begin
  FreeAndNil(FFormStateSaver);
end;

procedure TufrmAreaList.usbUpClick(Sender: TObject);
var
OldIndex, NewIndex: integer;
begin
  if uclbAreaList.ItemIndex > 0 then
    begin
//Move the layer in the list box
      OldIndex := uclbAreaList.ItemIndex;
      NewIndex := OldIndex - 1;
      uclbAreaList.Items.Move(OldIndex, NewIndex);
//Now reset the index of the layer in the TImgView32
      TPositionedLayerWithXML(uclbAreaList.Items.Objects[NewIndex]).Index :=
            TPositionedLayerWithXML(uclbAreaList.Items.Objects[NewIndex]).Index - 1;

//Let things get redrawn
      Application.ProcessMessages;
//Re-select the moved layer
      uclbAreaList.ItemIndex := NewIndex;
    end;
end;

procedure TufrmAreaList.usbDownClick(Sender: TObject);
var
OldIndex, NewIndex: integer;
begin
  if (uclbAreaList.ItemIndex < uclbAreaList.Count-1) and (uclbAreaList.ItemIndex > -1) then
    begin
//Move the layer in the list box
      OldIndex := uclbAreaList.ItemIndex;
      NewIndex := OldIndex + 1;
      uclbAreaList.Items.Move(OldIndex, NewIndex);
//Now reset the index of the layer in the TImgView32
      TPositionedLayerWithXML(uclbAreaList.Items.Objects[NewIndex]).Index :=
            TPositionedLayerWithXML(uclbAreaList.Items.Objects[NewIndex]).Index + 1;
//Let things get redrawn
      Application.ProcessMessages;
//Re-select the moved layer
      uclbAreaList.ItemIndex := NewIndex;
    end;
end;

procedure TufrmAreaList.aeAreaListIdle(Sender: TObject; var Done: Boolean);
begin
  usbUp.Enabled := (uclbAreaList.ItemIndex > 0);
  usbDown.Enabled := (uclbAreaList.ItemIndex < uclbAreaList.Count-1) and (uclbAreaList.ItemIndex > -1);
end;

procedure TufrmAreaList.uclbAreaListDblClick(Sender: TObject);
begin
  ufrmImgMarkup.Selection :=
  TPositionedLayerWithXML(uclbAreaList.Items.Objects[uclbAreaList.ItemIndex]);
  with TPositionedLayerWithXML(uclbAreaList.Items.Objects[uclbAreaList.ItemIndex]) do
    ufrmImgMarkup.ImgView.ScrollToCenter(CenterPoint.X, CenterPoint.Y);
end;

procedure TufrmAreaList.ResequenceLayers;
var
i: integer;

begin
  with uclbAreaList do
    if Items.Count > 0 then
      for i := Items.Count-1 downto 0 do
        TPositionedLayerWithXML(Items.Objects[i]).SendToBack;
end;

procedure TufrmAreaList.SendUncheckedToBack;
var
i: integer;

begin
  with uclbAreaList do
    if Items.Count > 0 then
      for i := Items.Count-1 downto 0 do
        if not Checked[i] then
          TPositionedLayerWithXML(Items.Objects[i]).SendToBack;
end;

end.
