Drag/drop the contents of just about any gadget.

Share your advanced PureBasic knowledge/code with the community.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Drag/drop the contents of just about any gadget.

Post by srod »

Code updated for 5.20+

Hi,

I 'hacked' :) up the following in response to a question in the coding section.

Dragging gadgets is easy enough. Dragging the contents of gadgets is easy. However, dragging the text content of a gadget in such a way that a copy of the content moves with the cursor, is perhaps not so easy - unless I've missed something!

The following example uses a dirty trick and shows one way of dragging and dropping the contents of just about any gadget in a visual manner; that is you can see what is being dragged as you move the cursor around. The example shows a text gadget, a string gadget and a ListView gadget.

Code: Select all

;Drag and drop the contents of gadgets.
;This can be changed to accommodate just about any kind of gadget.
;It uses a dirty trick with a hidden ListIcon gadget.

;By Stephen Rodriguez.
;Purebasic 4 beta 11. Tested on Win XP only.


#text1=1
#text2=2
#text3=3



If OpenWindow(0, 0, 0, 600, 300,"Drag and drop between gadgets.", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
  TextGadget(#text1,25,20,180,20, "Drag between any of these gadgets")
  StringGadget(#text2,25,50,180,20, "Drop on to any of these gadgets")
  ListViewGadget(#text3, 25, 90, 250, 120)
  For a = 0 To 3
    AddGadgetItem (#text3, -1, "Item " + Str(a)) ; define listview content
  Next
  Repeat
    ev=WaitWindowEvent()
    Select ev
      Case #WM_LBUTTONDOWN
        Debug 1
        dragfrom=GetDlgCtrlID_(ChildWindowFromPoint_(WindowID(0), WindowMouseY(0) << 32 + WindowMouseX(0)))
        If dragfrom>=#text1 And dragfrom<=#text3 
          ;User clicked a text gadget.
          ;Create a temporary ListIcon gadget so that windows will do all the hard work in displaying the
          ;drag image!
          templisticon=ListIconGadget(#PB_Any, 0, 0, 0, 190, "", 190)
          AddGadgetItem(templisticon, -1, "Gadget1 - Item"+Str(i))
          SetGadgetItemText(templisticon,0,GetGadgetText(dragfrom),0)
          DragImageList = SendMessage_(GadgetID(templisticon), #LVM_CREATEDRAGIMAGE, 0, @UpperLeft.POINT)
          If DragImageList 
            If ImageList_BeginDrag_(DragImageList , 0, 0, 0)
              ImageList_DragShowNolock_(#True)
              SetCapture_(WindowID(0))
              ShowCursor_(#False)
              IsDraging = #True
            Else
              ImageList_Destroy_(DragImageList)
              FreeGadget(templisticon)
            EndIf
          Else
            FreeGadget(templisticon)
          EndIf
        EndIf
      Case #WM_MOUSEMOVE
        GetCursorPos_(pt.point)
        ImageList_DragMove_(pt\x,pt\y)
        ;The following 2 lines will need uncommenting if some more detailed processing is to occur here.
        ;      ImageList_DragShowNolock_(#False)
        ;      ImageList_DragShowNolock_(#True)
      Case #WM_LBUTTONUP 
        If IsDraging
          ReleaseCapture_()
          ImageList_EndDrag_()
          ImageList_Destroy_(DragImageList)
          ShowCursor_(#True)
          FreeGadget(templisticon)
          IsDraging = #False
          ;Check to see if the text was dropped onto another text gadget.
          dragto=GetDlgCtrlID_(ChildWindowFromPoint_(WindowID(0), WindowMouseY(0) << 32 + WindowMouseX(0)))
          Select dragto
            Case #text1, #text2
              ;Drop the text onto the targetted gadget.
              SetGadgetText(dragto, GetGadgetText(dragfrom))
            Case #text3 ;ListView gadget.
              AddGadgetItem (dragto, -1, GetGadgetText(dragfrom)) 
          EndSelect
        EndIf
    EndSelect    
  Until ev = #PB_Event_CloseWindow
EndIf
End
Only tested on Win XP.
Last edited by srod on Mon May 01, 2006 1:33 am, edited 1 time in total.
I may look like a mule, but I'm not a complete ass.
SoulReaper
Enthusiast
Enthusiast
Posts: 372
Joined: Sun Apr 03, 2005 2:14 am
Location: England

Post by SoulReaper »

Great Work :)

I have just checked it and it works fine on windows 2000 pro :wink:

regards
kevin :) :wink:
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Thanks srod for your ingenious code. But ...

I just cannot get it to work correctly. Everything is OK except that a DragImage is never visible. As the cursor is also hidden (though I can alter that by commenting out the ShowCursor_(#False) line) I have no clue as to where I am moving the cursor and I do not know where the end drop will occur. If I do manage to drop in a suitable position, then the text is correctly copied.

I have tried modifying the program, but nothing makes the drag image appear. Have you any idea what the problem may be?

It may be relevant that I am running under Windows ME.
Anthony Jordan
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yea, I've seen this before with other drag and drop code which seems to work on some setups, but not others. I never could get to the bottom of it. :?
I may look like a mule, but I'm not a complete ass.
Post Reply