Canvasgadget + drag n drop

Just starting out? Need help? Post your questions and find answers here.
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Canvasgadget + drag n drop

Post by captain_skank »

Is it possible to drag and drop a canvas gadget ??

I don't think it is but am more than happy to be proved wrong :D

Cheers
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Canvasgadget + drag n drop

Post by davido »

I use drag/drop on canvas gadgets a lot:

A little demo to show dragging text to a canvas gadget.

I hope I have read your request correctly. :)

Code: Select all

If OpenWindow(0, 100, 200, 195, 260, "PureBasic Window", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
  
  CanvasGadget(0,10,10,100,100)
  EnableGadgetDrop(0,#PB_Drop_Text,#PB_Drag_Copy)
  
  Repeat
    Event = WaitWindowEvent()
   
    If Event =   #PB_Event_GadgetDrop
      Select EventGadget()
      Case 0
        Debug EventDropText()
      EndSelect
    EndIf
    If Event = #PB_Event_CloseWindow  ; If the user has pressed on the close button
      Quit = 1
    EndIf
    
  Until Quit = 1
  
EndIf

End  
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Canvasgadget + drag n drop

Post by TI-994A »

captain_skank wrote:Is it possible to drag and drop a canvas gadget ??
Hello captain_skank. I'm not absolutely sure what you mean by drag & drop a canvas gadget, but this example simulates a drag&drop functionality for a CanvasGadget():

Code: Select all

EnableExplicit
InitNetwork()

Enumeration
  #MainWindow
  #Canvas
  #Image
EndEnumeration

Define wFlags, appQuit, dragOn, canvasMouseXOffset, canvasMouseYOffset

If ReceiveHTTPFile("https://dl.dropboxusercontent.com/u/38177172/CS.bmp",
                    GetTemporaryDirectory() + "CS.bmp")
  LoadImage(#Image, GetTemporaryDirectory() + "CS.bmp")

  wFlags = #PB_Window_SystemMenu |#PB_Window_ScreenCentered
  OpenWindow(#MainWindow, #PB_Any, #PB_Any, 640, 480, "Drag&Drop CanvasGadget", wFlags)
  CanvasGadget(#Canvas, 10, 10, 200, 205, #PB_Canvas_Border)
  SetGadgetAttribute(#Canvas, #PB_Canvas_Image, ImageID(#Image))

  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow
        appQuit = 1
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #Canvas
            Select EventType()
              Case #PB_EventType_LeftButtonDown
                dragOn = 1
                canvasMouseXOffset = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseX)
                canvasMouseYOffset = GetGadgetAttribute(#Canvas, #PB_Canvas_MouseY)
              Case #PB_EventType_LeftButtonUp
                dragOn = 0
              Case #PB_EventType_MouseMove
                If dragOn
                  ResizeGadget(#Canvas, WindowMouseX(#MainWindow) - canvasMouseXOffset, 
                               WindowMouseY(#MainWindow) - canvasMouseYOffset, #PB_Ignore, #PB_Ignore)
                EndIf
            EndSelect
        EndSelect
    EndSelect
  Until appQuit = 1
EndIf
Hope you like the image. :wink:
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Canvasgadget + drag n drop

Post by davido »

Hi TI-994A,

I thought my reply was a little naïve.

I think that your's is better.
A very nice demo by the way.

Also see: http://www.purebasic.fr/english/viewtop ... 13&t=54098
DE AA EB
User avatar
TI-994A
Addict
Addict
Posts: 2705
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: Canvasgadget + drag n drop

Post by TI-994A »

Hi davido, and thank you. A working example to someone who's not familiar with the functionality is never naive. I've always known it's there, but I've never had a chance to use PureBasic's Drag&Drop functions; very interesting. Thanks for the example, and the link too.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
coder14
Enthusiast
Enthusiast
Posts: 327
Joined: Tue Jun 21, 2011 10:39 am

Re: Canvasgadget + drag n drop

Post by coder14 »

Nice TI. Clever choice of image. :lol:
User avatar
captain_skank
Enthusiast
Enthusiast
Posts: 639
Joined: Fri Oct 06, 2006 3:57 pm
Location: England

Re: Canvasgadget + drag n drop

Post by captain_skank »

Thanks guys - some nice code there.

It's not quite what I was after but has given me an idea how to acheive my end result - so thanks again.

cheers
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Canvasgadget + drag n drop

Post by netmaestro »

Based on the assumption that what you want to do is drag the image from the canvas to somewhere else, could you see if this might serve:

Code: Select all

; drag the red box from the canvas to the imagegadget
CreateImage(0, 64,64,24, #Black)
CreateImage(1, 64,64,24, #Red)

OpenWindow(1, 0, 0, 300, 100, "Drag & Drop", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
CanvasGadget(0, 20,10,64,64)
SetGadgetAttribute(0, #PB_Canvas_Image, ImageID(1))
ImageGadget(1, 200, 10, 64, 64, ImageID(0))

EnableGadgetDrop(1, #PB_Drop_Image, #PB_Drag_Copy|#PB_Drag_Move)

Repeat
  Event = WaitWindowEvent()      
  If Event = #PB_Event_Gadget And EventGadget() = 0 And EventType() =  #PB_EventType_LeftButtonDown  
    DragImage(ImageID(1))
  EndIf      
  If Event = #PB_Event_GadgetDrop And EventGadget() = 1
    SetGadgetState(EventGadget(), EventDropImage(2))
  EndIf
Until Event = #PB_Event_CloseWindow
BERESHEIT
Post Reply