unit U_ItemsSelector;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, RzButton, RzPanel, ExtCtrls, Buttons, RzLstBox,
  RzSplit,

  U_BaseCommon, U_ResourceReport, ImgList, RzRadChk, siComp, siLngLnk;

type
  TF_ItemsSelector = class(TForm)
    RzPanel1: TRzPanel;
    gbOkCancel: TRzGroupBox;
    btOk: TRzBitBtn;
    btCancel: TRzBitBtn;
    RzSplitter1: TRzSplitter;
    RzPanel2: TRzPanel;
    RzPanel3: TRzPanel;
    RzPanel4: TRzPanel;
    RzPanel5: TRzPanel;
    RzPanel6: TRzPanel;
    lbAccessibleFields: TRzListBox;
    Label1: TLabel;
    Label2: TLabel;
    btAddItem: TSpeedButton;
    btRemoveItem: TSpeedButton;
    lbSelectedFields: TRzListBox;
    RzPanel7: TRzPanel;
    btMoveUp: TSpeedButton;
    btMoveDown: TSpeedButton;
    ImageList1: TImageList;
    RzPanel8: TRzPanel;
    cbDescending: TRzCheckBox;
    cbCaseSensitive: TRzCheckBox;
    lng_Forms: TsiLangLinked;
    procedure gbOkCancelResize(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure btAddItemClick(Sender: TObject);
    procedure btRemoveItemClick(Sender: TObject);
    procedure lbAccessibleFieldsDblClick(Sender: TObject);
    procedure lbSelectedFieldsDblClick(Sender: TObject);
    procedure btMoveUpClick(Sender: TObject);
    procedure btMoveDownClick(Sender: TObject);

  private
    GForm: TForm;

    procedure ChangeItemPosition(AListBox: TRzListBox; APosOffset: Integer);
    procedure MoveItemToOtherListBox(ASrcLB, ATrgLB: TRzListBox);
    procedure SetControls;
  public
    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor Destroy; override;

    function SelectReportSortFields(AReportSortInfo: TReportSortInfo): Boolean;
  end;

//var
//  Form1: TForm1;

implementation
uses Unit_DM_SCS;
{$R *.dfm}

constructor TF_ItemsSelector.Create(AOwner: TComponent; AForm: TForm);
begin
  GForm := AForm;
  inherited Create(AOwner);
end;

destructor TF_ItemsSelector.Destroy;
begin
  inherited;
end;

procedure TF_ItemsSelector.gbOkCancelResize(Sender: TObject);
begin
  SetMiddleControlChilds(TControl(Sender), TControl(Self));
end;

procedure TF_ItemsSelector.FormShow(Sender: TObject);
begin
  gbOkCancelResize(gbOkCancel);
end;

procedure TF_ItemsSelector.ChangeItemPosition(AListBox: TRzListBox; APosOffset: Integer);
var
  ItemIndexNewPos: Integer;
begin
  ItemIndexNewPos := AListBox.ItemIndex + APosOffset;
  if (ItemIndexNewPos >= 0) and (ItemIndexNewPos <= (AListBox.Items.Count - 1)) then
  begin
    AListBox.Items.Move(AListBox.ItemIndex, ItemIndexNewPos);
    AListBox.ItemIndex := ItemIndexNewPos;

    SetControls;
  end;
end;

procedure TF_ItemsSelector.MoveItemToOtherListBox(ASrcLB, ATrgLB: TRzListBox);
var
  StrObject: TObject;
begin
  if ASrcLB.ItemIndex <> -1 then
  begin
    StrObject := ASrcLB.Items.Objects[ASrcLB.ItemIndex];
    ATrgLB.Items.AddObject(ASrcLB.Items.Strings[ASrcLB.ItemIndex], StrObject);
    ASrcLB.Items.Delete(ASrcLB.ItemIndex);

    ATrgLB.ItemIndex := ATrgLB.Items.IndexOfObject(StrObject);

    SetControls;
  end;
end;

procedure TF_ItemsSelector.SetControls;
begin
  //btAddItem.Enabled := (lbAccessibleFields.Items.Count > 0) and (lbAccessibleFields.ItemIndex <> -1);
  //btRemoveItem.Enabled := (lbSelectedFields.Items.Count > 0) and (lbSelectedFields.ItemIndex <> -1);
  //btMoveUp.Enabled := (lbSelectedFields.ItemIndex > 0);
  //btMoveDown.Enabled := (lbSelectedFields.ItemIndex < (lbSelectedFields.Items.Count - 1));
end;

function TF_ItemsSelector.SelectReportSortFields(
  AReportSortInfo: TReportSortInfo): Boolean;
var
  i: Integer;
  FieldCaption: String;
begin
  Result := false;
  try
    try
      cbCaseSensitive.Checked := (AReportSortInfo.CaseSensitive = biTrue);
      cbDescending.Checked := (AReportSortInfo.Descending = biTrue);

      // Вносим все доступные поля
      for i := 0 to AReportSortInfo.AllFieldNames.Count - 1 do
        if AReportSortInfo.UsedFieldNames.IndexOf(AReportSortInfo.AllFieldNames[i]) = -1 then
        begin
          FieldCaption := AReportSortInfo.GetFieldCaption(AReportSortInfo.AllFieldNames[i]);
          AddGUIDIDToStrings(FieldCaption, AReportSortInfo.AllFieldNames[i], 0, lbAccessibleFields.Items);
        end;
      // Вносим выбранные поля
      for i := 0 to AReportSortInfo.UsedFieldNames.Count - 1 do
        if AReportSortInfo.AllFieldNames.IndexOf(AReportSortInfo.UsedFieldNames[i]) <> -1 then
        begin
          FieldCaption := AReportSortInfo.GetFieldCaption(AReportSortInfo.UsedFieldNames[i]);
          AddGUIDIDToStrings(FieldCaption, AReportSortInfo.UsedFieldNames[i], 0, lbSelectedFields.Items);
        end;
      lbAccessibleFields.Sorted := true;
      lbSelectedFields.Sorted := false;

      if ShowModal = mrOk then
      begin
        AReportSortInfo.CaseSensitive := BoolToInt(cbCaseSensitive.Checked);
        AReportSortInfo.Descending := BoolToInt(cbDescending.Checked);

        AReportSortInfo.UsedFieldNames.Clear;
        for i := 0 to lbSelectedFields.Items.Count - 1 do
          AReportSortInfo.UsedFieldNames.Add(GetGUIDFromStrings(lbSelectedFields.Items, i));

        Result := true;
      end;
    finally
      RemoveGUIDIDFromStrings(lbAccessibleFields.Items);
      lbAccessibleFields.Items.Clear;
      RemoveGUIDIDFromStrings(lbSelectedFields.Items);
      lbSelectedFields.Items.Clear;
    end;
  except
    on E: Exception do AddExceptionToLogEx('TF_ItemsSelector.SelectReportSortFields', E.Message);
  end;
end;

procedure TF_ItemsSelector.btAddItemClick(Sender: TObject);
begin
  MoveItemToOtherListBox(lbAccessibleFields, lbSelectedFields);
end;

procedure TF_ItemsSelector.btRemoveItemClick(Sender: TObject);
begin
  MoveItemToOtherListBox(lbSelectedFields, lbAccessibleFields);
end;

procedure TF_ItemsSelector.lbAccessibleFieldsDblClick(Sender: TObject);
begin
  btAddItem.Click;
end;

procedure TF_ItemsSelector.lbSelectedFieldsDblClick(Sender: TObject);
begin
  btRemoveItem.Click;
end;

procedure TF_ItemsSelector.btMoveUpClick(Sender: TObject);
begin
  ChangeItemPosition(lbSelectedFields, -1);
end;

procedure TF_ItemsSelector.btMoveDownClick(Sender: TObject);
begin
  ChangeItemPosition(lbSelectedFields, 1);
end;

end.