{$I-,F+,O+,X+}
unit FMDrag;

interface

uses Objects, FileMngr;

procedure GetDestPrim(FManager: PPanel; var S: FNameStr; Where: TPoint);
procedure DragDropPrim(FManager: PPanel; Where: TPoint);

implementation

uses Drivers, Views, App, PanelRec, FMPrim, DirPrim, DiskBase, DiskPrim,
  DragDrop, Archives, TPString, Consts, Commands, Defaults;

procedure GetDestPrim(FManager: PPanel; var S: FNameStr; Where: TPoint);
var
  Mini, Maxi: Boolean;
  C: Char;
  I, N: Integer;
begin
  with FManager^ do
  begin
    Mini := State and sfMiniStatus <> 0;
    Maxi := Settings and seMaxi <> 0;
    MakeLocal(Where, Where);
    if not Maxi and (Where.Y = 0) then SelectDriveDialog(
      'Select ~destination~ drive:', Which, ListDir^[1], S)
    else if Status = stTree then
    begin
      if Mini and (Where.Y >= Size.Y - 2) then I := Focused
      else I := TopItem + Where.Y - Byte(not Maxi);
      S := TheTree^.GetDirStr(I);
    end
    else if Options and ofValidate <> 0 then
    begin
      if Mini then N := Size.Y - 3 else N := Size.Y;
      N := Size.Y - Byte(Mini) * 2 - Byte(not Maxi);
      I := Where.Y - Byte(not Maxi) +
	(N * (Where.X div ColWidth)) + TopItem;
      GetItemStr(I, S);
    end
    else if InArchive then with Archiver^ do
    begin
      S := GetTopName;
      if ArchiveRec^.Extension <> nil then
	S := ArchiveRec^.Extension^ + ':' + S;
      Exit;
    end
    else GetListDir(S);
    if S = '' then S := #0 else S := AddedSlash(S);
  end;
end;

procedure DragDropPrim(FManager: PPanel; Where: TPoint);
var
  Action: Byte;
  X: Integer;
  W: PDragWindow;
  SR: PPanelRec;
  P: PString;
  E: TEvent;
  L: Longint;
  Title: String[4];
  R: TRect;
  Text: String;
begin
  if (GetShiftState and (kbCtrlShift + kbAltShift) <> 0) and
    not FManager^.InArchive then Action := 1 else Action := 0;
  if Action = 1 then Title := 'Move' else Title := 'Copy';

  with FManager^ do
  begin
    SR := List^.At(Focused);
    if (SR^.Name[1] = ':') or
       ((SR^.Attr and $08 <> 0) and (SR^.Name[1] <> '.')) then Exit;

    if SR^.Selected then
      if SelCount = 1 then Text := '1 file'
      else
      begin
	L := SelCount;
	FormatStr(Text, '%d files', L);
      end
    else AddNameExt('', SR, Text);

    if Length(Text) <= 12 then X := 16
    else
    begin
      X := Length(Text) + 4;
      if X > ScreenWidth then X := ScreenWidth;
    end;
    R.Assign(Where.X, Where.Y, Where.X + X, Where.Y + 3);
    W := New(PDragWindow, Init(R, Title, Owner, hcDragAndDrop, Text));
    Application^.Insert(W);

    Application^.GetExtent(R);
    E.What := evMouseDown;
    E.Where := Where;
    W^.DragView(E, dmDragMove, R, W^.Size, W^.Size);
    if W^.Cancel then
    begin
      W^.Close;
      Exit;
    end;

    P := Message(Desktop, evBroadcast, cmGetDest, W);
    if P = nil then Text := '' else Text := P^;
    W^.Close;
    if Text = #0 then Exit;
    CopyLeft := (Which = pnRight) and (FMOptions and foCopyLeft <> 0);
    if Text = '' then FMDeletePrim(FManager, not SR^.Selected)
    else if Action = 0 then FMCopyPrim(FManager, Text, not SR^.Selected)
    else FMRenMovPrim(FManager, Text, not SR^.Selected);
  end;
end;

end.