Summary :
- create a dropTarget and a dropSource : from Graphics.UI.WXCore.DragAndDrop
- use the Event "
on drag
" on the source widget, from where you would invokedragAndDrop
from Graphics.UI.WXCore.Events
My mistakes and wrong assumptions:
- I was looking for a "on drop" event, on the target. does not exists, and not needed, because:
- When "on drag", the other events are suspended. no more
mouse up
ormouse down
. If not released on (DnD) target, the drag will abort and normal events resume. [0] - Note that a textEntry need a focus to get the "paste", however the drop still occurs. look at "on drag Activated" on console.
- the text exchanged are the one from the DataObjects ( and not from the source, if this would be a textEntry. ). see the "text dropped".
The following code dumps the events on the console, to experiment:
module Main where
import Graphics.UI.WX hiding (empty)
import Data.Maybe
import Control.Monad
import Graphics.UI.WX.Events
import Graphics.UI.WXCore.WxcClassesMZ
--import Graphics.UI.WXCore.WxcClassesAL
import Graphics.UI.WXCore.DragAndDrop
import Graphics.UI.WXCore.Events
main
= start dndtest
dndtest
= do
f <- frame [text := "Drag And Drop test"]
p <- panel f []
ok <- button p [text := "Ok"]
xinput <- textEntry p [text := "here :"]
yinput <- staticText p [text := "drag me"]
set f [defaultButton := ok
,layout := container p $
margin 10 $
column 5 [boxed "coordinates" (grid 5 5 [[label "source:", hfill $ widget yinput]
,[label "target(focus first):", hfill $ widget xinput]
])
,floatBottomRight $ row 5 [widget ok]]
]
set xinput [ on enter := onEnter]
set yinput [ ]
---------------------------------------------------------
--- meaningful stuff starts here
---------------------------------------------------------
-- prepare the drop source : create a DataObject and associate it with the source
textdata' <- textDataObjectCreate "text dropped"
src <- dropSource textdata' yinput
-- prepare the drop target: create a DataObject (placeholder here) and associate it with the target
textdata <- textDataObjectCreate ".."
drop <- dropTarget xinput textdata
-- activate on drag the do dragdrop. this will replace the target dataObject with the source one.
-- Try with and without giving focus to the textEntry field
-- Try and source from your favorite editor also (focus first!)
set yinput [ on drag := onDrag src ]
---------------------------------------------------------
--- meaningful stuff stops here
---------------------------------------------------------
set ok [ on command := close f ]
return ()
--onDrag:: Graphics.UI.WXCore.WxcClassTypes.DropSource a -> Point -> IO ()
onDrag s p = do
dragAndDrop s Default (\_ -> return ())
putStrLn "on Drag activated:"
showMeE :: EventMouse -> IO ()
showMeE (MouseMotion point mod) = putStr "" --- discard meaningless Motion event
showMeE e = putStrLn $ show e
onEnter p = putStrLn $ "on Enter:" ++ show p
I have seen other goodies I may explore, like changing the cursor during the drag, or reacting on different variants of drop.