unit U_CanDelete;

interface

uses
  Windows, U_LNG, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, cxLookAndFeelPainters, StdCtrls, cxButtons, ComCtrls, U_BaseCommon, U_BaseConstants,
  RzBHints, ExtCtrls, siComp, siLngLnk, cxGraphics, cxLookAndFeels, Menus;

type TItemType = Integer;

type TCanDelItemData = Record
       ID_Compon: Integer;
       Complects: TStrings;
     end;
     PCanDelItemData = ^TCanDelItemData;
type TCanDelFormKind = (dfkCompon, dfkComponComplect);

type
  TF_CanDelete = class(TForm)
    Panel_ComponList: TPanel;
    Label_Messg: TLabel;
    Button_Close: TcxButton;
    ListView_NotDel: TListView;
    Button_Choice: TcxButton;
    Button_Cancel: TcxButton;
    lng_Forms: TsiLangLinked;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormShow(Sender: TObject);
    procedure Button_CloseClick(Sender: TObject);
    procedure ListView_NotDelMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure ListView_NotDelMouseMove(Sender: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure ListView_NotDelChange(Sender: TObject; Item: TListItem;
      Change: TItemChange);
  private
    { Private declarations }
    GForm: TForm;
    procedure WMMove(var Mes: TWMMove);
      message WM_Move;
  public
    { Public declarations }
    GMode: TCanDelFormKind;
    //GItemType: TItemType;
    GPrevListItem: TListItem;
    GAnswQuatDifferent: TRect;

    GLastSelID: Integer;

    procedure GotoOnTree;
    procedure ShowComplects;

    Constructor Create(AOwner: TComponent; AForm: TForm);
    Destructor  Destroy; override;
  end;
   {
var
  F_CanDelete: TF_CanDelete; }

implementation
Uses U_Main, Unit_DM_SCS, U_AnswerToQuast;
{$R *.dfm}

// ##### Показать комплектующие компоненты #####
procedure TF_CanDelete.ShowComplects;
var  ListItem: TListItem;
     Point : TPoint;
begin
    GetCursorPos(Point);
    Point := ListView_NotDel.ScreenToClient(Point);
    ListItem := ListView_NotDel.GetItemAt(Point.X, Point.Y);

    if GMode = dfkComponComplect then
      begin
         if (ListItem <> nil) {and (GPrevListItem <> ListItem)} {and (Button = mbRight)} then
           begin
               ListView_NotDel.Hint := cCanDelete_Msg1_1+' ' + #13 + '   "'+ ListItem.Caption +'":' + #13 +
                                       PCanDelItemData(ListItem.Data).Complects.Text;
               ListView_NotDel.ShowHint := true;
               Application.ShowHint := true;
           end
         else
           begin
               ListView_NotDel.ShowHint := false;
               Application.ShowHint := false;
           end;

         GPrevListItem := ListItem;
      end;
end;


// ##### Преход по Дереву #####
procedure TF_CanDelete.GotoOnTree;
var //Item: TListItem;
    Node: TTreeNode;
    ID_Comp: Integer;
begin
    if ListView_NotDel.Selected <> nil then
    with (GForm as TF_Main) do
      begin
          //GItemType := TF_Main(GForm).DM.GItemType;
          ID_Comp := 0;
          case GMode of
            dfkCompon        : ID_Comp := Integer(ListView_NotDel.Selected.Data^);
            dfkComponComplect: ID_Comp := PCanDelItemData(ListView_NotDel.Selected.Data).ID_Compon;
          end;
          {if (TF_Main(GForm).GDBMode = bkNormBase) or (GItemType = itComponent) then
            Node := FindComponInTree(ID_Comp, true) // Выйти на компонент
          else if GItemType <> itComponent then
            Node := FindComponInTree(Integer(ID_Comp), false); // Выйти на папку }
          Node := FindComponOrDirInTree(ID_Comp, true); // Выйти на компонент
          if Node <> nil then
            Tree_catalog.Selected := Node;
          GLastSelID := ID_Comp;
      end;
end;



// ############################################################################
//
// ##### Показать форму #####
procedure TF_CanDelete.FormShow(Sender: TObject);
begin
    ListView_NotDel.SetFocus;
    GLastSelID := 0;
end;

// ##### Изменение позиции в Листе #####
procedure TF_CanDelete.ListView_NotDelChange(Sender: TObject;
  Item: TListItem; Change: TItemChange);
begin
    GotoOnTree;
end;



// ##### Закрыть форму #####
procedure TF_CanDelete.FormClose(Sender: TObject;
  var Action: TCloseAction);
var i: Integer;
    ICount: Integer;
begin
    ICount :=  ListView_NotDel.Items.Count;
    if GMode = dfkCompon then
      for i := 0 to ICount - 1 do
        FreeMem(ListView_NotDel.Items[i].Data)
    else
      for i := 0 to ICount - 1 do
        begin
            FreeAndNil(PCanDelItemData(ListView_NotDel.Items[i].Data).Complects);
            FreeMem(ListView_NotDel.Items[i].Data);
        end;
    ListView_NotDel.Items.Clear;

    GMode := dfkCompon;
    ShowComplects;
end;


// ##### Нажатие на кнопку "Закрыть" #####
procedure TF_CanDelete.Button_CloseClick(Sender: TObject);
begin
    Close;
end;


// ##### Нажатие по компоненте в Листе #####
procedure TF_CanDelete.ListView_NotDelMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
    ShowComplects;
end;


procedure TF_CanDelete.ListView_NotDelMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
    ShowComplects;
end;


// ##### Перемещение формы #####
procedure TF_CanDelete.WMMove(var Mes: TWMMove);
begin
    if TF_Main(GForm).F_AnswerToQuast.Visible then
      begin
          TF_Main(GForm).F_AnswerToQuast.Top  := Top  + GAnswQuatDifferent.Top;
          TF_Main(GForm).F_AnswerToQuast.Left := Left + GAnswQuatDifferent.Left;
      end;
end;

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

destructor TF_CanDelete.Destroy;
begin
    inherited;
end;

end.
