{$X+,F+,O+}
unit DragDrop;

interface

uses Objects, Drivers, Views;

type
  PDragWindow = ^TDragWindow;
  TDragWindow = object(TWindow)
    Caller: PView;
    Cancel: Boolean;
    constructor Init(var Bounds: TRect; ATitle: TTitleStr;
      ACaller: PView; AHelpCtx: Word; const AText: TTitleStr);
    procedure GetEvent(var Event: TEvent); virtual;
    function GetHelpCtx: Word; virtual;
    function GetPalette: PPalette; virtual;
    procedure SetState(AState: Word; Enable: Boolean); virtual;
  end;

implementation

uses Dialogs, Defaults;

constructor TDragWindow.Init(var Bounds: TRect; ATitle: TTitleStr;
  ACaller: PView; AHelpCtx: Word; const AText: TTitleStr);
begin
  TWindow.Init(Bounds, ATitle, wnNoNumber);
  HelpCtx := AHelpCtx;
  Flags := wfMove;
  Caller := ACaller;
  Bounds.Assign(1, 1, Size.X - 1, 2);
  if Length(AText) < Size.X - 2 then
    Inc(Bounds.A.X, (Size.X - Length(AText) - 2) div 2);
  Insert(New(PStaticText, Init(Bounds, AText)));
end;

procedure TDragWindow.GetEvent(var Event: TEvent);
begin
  if (ddMode = ddButtonUp) and (Flags and wfClose = 0) then
  begin
    Flags := Flags or wfClose;
    repeat until not MouseEvent(Event, evMouseUp);
  end;
  TWindow.GetEvent(Event);
  if (Event.What = evKeyDown) and (Event.KeyCode = kbEsc) then
  begin
    Event.What := evMouseUp;
    Cancel := True;
  end
  else if (Event.What = evMouseDown) and (Flags and wfClose <> 0) then
    Event.What := evMouseUp;
end;

function TDragWindow.GetHelpCtx: Word;
begin
  GetHelpCtx := HelpCtx;
end;

function TDragWindow.GetPalette: PPalette;
begin
  GetPalette := Caller^.GetPalette;
end;

procedure TDragWindow.SetState(AState: Word; Enable: Boolean);
begin
  TGroup.SetState(AState, Enable);
end;

end.