' How to dynamically move your buttons for Rapid-Q (Linux/Unix) by William Yu
' One complication is that if you try moving the button with the
' callback attached to it, all the events are cleared, strange...
' Because of this, you'll have to create a temporary button.

$APPTYPE GUI
$TYPECHECK ON

CONST False = 0
CONST True = NOT False

CONST LeftButton = 0
CONST CenterButton = 1
CONST RightButton = 2

DECLARE SUB ButtonMouseDown(Btn AS INTEGER, X AS INTEGER, Y AS INTEGER)
DECLARE SUB ButtonMouseUp(Btn AS INTEGER, X AS INTEGER, Y AS INTEGER)
DECLARE SUB ButtonMouseMove(X AS INTEGER, Y AS INTEGER)

DIM DownX AS INTEGER, DownY AS INTEGER
DIM MoveButton AS INTEGER

CREATE Form AS QForm
  Caption = "Click & Drag Demo"
  Center
  CREATE Button2 AS QButton
    Caption = "Move Me"
    Left = 100
    height = 30
  END CREATE
  CREATE Button AS QButton      '' Overlap button
    Caption = "Move Me"
    Left = 100
    height = 30
    'Hint = "Click & Drag"
    OnMouseDown = ButtonMouseDown
    OnMouseUp = ButtonMouseUp
    OnMouseMove = ButtonMouseMove
  END CREATE
  ShowModal
END CREATE


SUB ButtonMouseDown (Btn AS INTEGER, X AS INTEGER, Y AS DOUBLE)
  IF Btn <> LeftButton THEN
    EXIT SUB
  END IF
  MoveButton = True
  DownX = X
  DownY = Y
  Button.Visible = False
END SUB

SUB ButtonMouseUp (Btn AS INTEGER, X AS INTEGER, Y AS DOUBLE)
  IF MoveButton = False THEN
    EXIT SUB
  END IF
  Button.Left = Button.Left + (X-DownX)
  Button.Top = Button.Top + (Y-DownY)
  Button2.Left = Button.Left      '' Guards against the "fast mouse" problem
  Button2.Top = Button.Top
  Button.Visible = True
  MoveButton = False
END SUB

SUB ButtonMouseMove(X AS INTEGER, Y AS INTEGER)
  IF MoveButton = False THEN
    EXIT SUB
  END IF
  Button2.Left = Button.Left + (X-DownX)
  Button2.Top = Button.Top + (Y-DownY)
END SUB
