Another drag-drop example with drag image (PB 4.1)

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...

Post by srod »

ts-soft wrote:@srod
have you an example with treegadget? Sort the items per drag & drop,
copying image, gadgetdata , all :wink:

This is for a real project and i have only problems with this.
Afraid not at this moment.

Wouldn't have thought it would be too difficult though - except for the sort. Of course, the sublevels will need a little care.
I may look like a mule, but I'm not a complete ass.
CSAUER
Enthusiast
Enthusiast
Posts: 188
Joined: Mon Oct 18, 2004 7:23 am
Location: Germany

Post by CSAUER »

A cross-platform version could look like following:

There is only an issue when moving above selection, then it turns to the same color. But this maybe can be fixed.

Code: Select all

;An example of the new Drag and Drop library for Purebasic 4.1.
;==============================================================
;Uses a DragCallback to display an image whilst an item of text is being dragged between
;two listicons.

;   Example by netmaestro with the image drag added by srod.
;   Made cross-platform compatible by CSAUER
;   Created with Purebasic 4.1 (beta 1) for Windows.
;   Date:  May 2007.
;   Platforms:  Windows.


Declare.l DragCallBack(Action)

Structure _drag
  image.l
  hdc.l
  ihdc.l
  oldimage.l
  x.l
  y.l
  width.l
  height.l
EndStructure


Global gDrag._drag


OpenWindow(0,0,0,480,400,"",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
CreateGadgetList(WindowID(0))
ListIconGadget(0,20,20,200,300,"Gadget 0", 195)
ListIconGadget(1,250,20,200,300,"Gadget 1", 195)

AddGadgetItem(0, -1, "Arthur")
AddGadgetItem(0, -1, "Beryl")
AddGadgetItem(0, -1, "Charles")
AddGadgetItem(0, -1, "Daniel")
AddGadgetItem(0, -1, "Ernest")
AddGadgetItem(0, -1, "Francis")
AddGadgetItem(0, -1, "Gordon")
AddGadgetItem(0, -1, "Harold")
AddGadgetItem(0, -1, "Ian")
AddGadgetItem(0, -1, "John")

EnableGadgetDrop(0,#PB_Drop_Text, #PB_Drag_Copy)
EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Copy)

Repeat
  ev = WaitWindowEvent()
  Select ev
    Case #PB_Event_GadgetDrop
      RemoveGadgetItem(draggadget, dragrow)
      AddGadgetItem(EventGadget(), GetGadgetState(EventGadget()), EventDropText())
      FreeGadget(gDrag\hdc)
    Case #PB_Event_Gadget
      If EventType() = #PB_EventType_DragStart
        dragrow = GetGadgetState(EventGadget())
        dragtxt.s = GetGadgetItemText(EventGadget(), dragrow)
        draggadget = EventGadget()
        StartDrawing(WindowOutput(0))
          DrawingFont(GetGadgetFont(draggadget))
          gDrag\width = TextWidth(dragtxt) : gDrag\height = TextHeight(dragtxt)
        StopDrawing()        
        gDrag\hdc = TextGadget(#PB_Any,0,0,gDrag\width,20,dragtxt)
        SetGadgetColor(gDrag\hdc,#PB_Gadget_BackColor,$FFFFFF)
        HideGadget(gDrag\hdc,1)
        gDragx = -1 : gDrag\y = -1
        SetDragCallback(@DragCallBack())
        DragText(dragtxt, #PB_Drag_Copy)
      EndIf
 EndSelect
Until ev = #PB_Event_CloseWindow


Procedure.l DragCallBack(Action)
  If action<>#PB_Drag_None
    If gDrag\x = -1 Or gDrag\y = -1
      HideGadget(gDrag\hdc,0)
    EndIf
    gDrag\x = WindowMouseX(0)
    gDrag\y = WindowMouseY(0)
    ResizeGadget(gDrag\hdc,gDrag\x-(gDrag\width/2),gDrag\y-10,#PB_Ignore,#PB_Ignore)
  Else
    gDrag\x = -1 : gDrag\y = -1
    HideGadget(gDrag\hdc,1)
  EndIf
  ProcedureReturn 1
EndProcedure 
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

I can't presently get this to display correctly without using a little api!

I'll keep trying though.
I may look like a mule, but I'm not a complete ass.
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: Another drag-drop example with drag image (PB 4.1)

Post by Mesa »

Added to the nemaestro's and others' code, the choice between move and copy and should be multiplatform.

Code: Select all

;An example of the new Drag and Drop library for Purebasic 5.60.
;==============================================================
;Uses a DragCallback to display an image whilst an item of text is being dragged between
;two listicons.

;   Example by netmaestro with the image drag added by srod.
;   Created with Purebasic 4.1 (beta 1) for Windows.
;   Date:  May 2007.
;   Platforms:  Windows.

;Update by Mesa, March 2017
; Drag to copy and Shift+drag to move
; PureBasic 5.60
; Platform: Should be multiplatform


EnableExplicit

Declare DragCallBack(Action)
Declare DropCallback(Handle, State, Format, Action, x, y)
Declare RefreshGadget(ID) ; Without api

Structure _drag
  n.l
  hdc.l
  x.l
  y.l
  width.l
  height.l
EndStructure


Global gDrag._drag
Global ev

Define draggadget, dragrow, dragtxt.s


OpenWindow(0,0,0,480,400,"Drag to Copy | SHIFT+Drag to Move",#PB_Window_ScreenCentered|#PB_Window_SystemMenu) 
CreateGadgetList(WindowID(0)) 
ListIconGadget(0,20,20,200,300,"Gadget 0", 195, #PB_ListIcon_FullRowSelect) 
ListIconGadget(1,250,20,200,300,"Gadget 1", 195, #PB_ListIcon_FullRowSelect) 

AddGadgetItem(0, -1, "Arthur") 
AddGadgetItem(0, -1, "Beryl") 
AddGadgetItem(0, -1, "Charles") 
AddGadgetItem(0, -1, "Daniel") 
AddGadgetItem(0, -1, "Ernest") 
AddGadgetItem(0, -1, "Francis") 
AddGadgetItem(0, -1, "Gordon") 
AddGadgetItem(0, -1, "Harold") 
AddGadgetItem(0, -1, "Ian") 
AddGadgetItem(0, -1, "John") 

EnableGadgetDrop(0,#PB_Drop_Text, #PB_Drag_Move|#PB_Drag_Copy) 
EnableGadgetDrop(1,#PB_Drop_Text, #PB_Drag_Move|#PB_Drag_Copy) 

SetDropCallback(@DropCallback())

Repeat 
  ev = WaitWindowEvent() 
  Select ev 
    Case #PB_Event_GadgetDrop 
      If EventDropAction() = #PB_Drag_Move
        RemoveGadgetItem(draggadget, dragrow)
      EndIf
      If draggadget <> EventGadget() Or dragrow <> GetGadgetState(EventGadget())
        AddGadgetItem(EventGadget(), GetGadgetState(EventGadget()), EventDropText()) 
      EndIf
      ;FreeGadget(gDrag\hdc):gDrag\n=0; If you don't want to use a dropcallback
      
    Case #PB_Event_Gadget 
      If EventType() = #PB_EventType_DragStart 
        
        dragrow = GetGadgetState(EventGadget()) 
        dragtxt = GetGadgetItemText(EventGadget(), dragrow) 
        draggadget = EventGadget() 
        ;Create drag gadgettext
        If gDrag\n=1; If ESC key has been pressed during a drag&drop, so destroy the old gadgettext  
          If IsGadget(gDrag\hdc) <> 0
            FreeGadget(gDrag\hdc)
            RefreshGadget(0); Not working very well ;(
          EndIf  
        EndIf
        gDrag\hdc = TextGadget(#PB_Any,0,0,0,0,dragtxt)
        gDrag\n=1
        gDrag\width =GadgetWidth(gDrag\hdc,#PB_Gadget_RequiredSize)
        gDrag\height =  GadgetHeight(gDrag\hdc,#PB_Gadget_RequiredSize)
        ResizeGadget(gDrag\hdc, #PB_Ignore, #PB_Ignore,gDrag\width,gDrag\height)
        SetGadgetColor(gDrag\hdc,#PB_Gadget_BackColor,$FFFFFF)
        HideGadget(gDrag\hdc,1)
        gDrag\x = -1 : gDrag\y = -1
        SetDragCallback(@DragCallBack())
        DragText(dragtxt, #PB_Drag_Move|#PB_Drag_Copy) 
       
      EndIf 
  EndSelect 
Until ev = #PB_Event_CloseWindow 


Procedure DragCallBack(Action)
  
  If gDrag\x>-1 And gDrag\y>-1
    HideGadget(gDrag\hdc,0)
  EndIf
  
  If action<>#PB_Drag_None
    
    gDrag\x = WindowMouseX(0)
    gDrag\y = WindowMouseY(0)
    ResizeGadget(gDrag\hdc,gDrag\x-(gDrag\width/2),gDrag\y-(gDrag\height/2),#PB_Ignore,#PB_Ignore)
    
  Else
    gDrag\x = -1 : gDrag\y = -1
    HideGadget(gDrag\hdc,1)
  EndIf
  ProcedureReturn 1
EndProcedure

Procedure DropCallback(targetID, State, Format, Action, x, y)
  If targetID=WindowID(0)
    If state = #PB_Drag_Finish 
      ;Debug "dd finish" 
      
      If IsGadget(gDrag\hdc) <> 0
        FreeGadget(gDrag\hdc)
        gDrag\n=0
      EndIf
      
    EndIf
  EndIf
  ProcedureReturn #True
  
EndProcedure

Procedure RefreshGadget(ID)
  AddGadgetItem(ID, -1, " ") 
  While WindowEvent() : Wend
  RemoveGadgetItem(ID, CountGadgetItems(ID)-1)

EndProcedure


M.
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 460
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Another drag-drop example with drag image (PB 4.1)

Post by Mindphazer »

Mesa wrote:Added to the nemaestro's and others' code, the choice between move and copy and should be multiplatform.
M.
Not on OS X unfortunately...
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Another drag-drop example with drag image (PB 4.1)

Post by Shardik »

Mindphazer wrote:Not on OS X unfortunately...
That's true. MacOS problems with drag 'n drop were collected in this thread by fsw. Fred wrote in that thread 4 years ago:
Fred wrote:Cocoa drag'n'drop model is not fully compatiable with PB model, I don't know how to work around it (for now).
User avatar
Mindphazer
Enthusiast
Enthusiast
Posts: 460
Joined: Mon Sep 10, 2012 10:41 am
Location: Savoie

Re: Another drag-drop example with drag image (PB 4.1)

Post by Mindphazer »

Yeah I know...
Seems Fred still doesn't know how to fix the issue...
MacBook Pro 16" M4 Pro - 24 Gb - MacOS 15.4.1 - Iphone 15 Pro Max - iPad at home
...and unfortunately... Windows at work...
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Another drag-drop example with drag image (PB 4.1)

Post by davido »

Perhaps the PB model could be changed? :)
DE AA EB
wombats
Enthusiast
Enthusiast
Posts: 718
Joined: Thu Dec 29, 2011 5:03 pm

Re: Another drag-drop example with drag image (PB 4.1)

Post by wombats »

davido wrote:Perhaps the PB model could be changed? :)
Yeah, I don't know why it can't be adapted for macOS...but then, I personally have no experience with drag and drop on macOS. I really need it to be fixed for my project, but I'll just have to keep hoping it happens!
User avatar
Shardik
Addict
Addict
Posts: 2060
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Another drag-drop example with drag image (PB 4.1)

Post by Shardik »

wombats wrote:I really need it to be fixed for my project, but I'll just have to keep hoping it happens!
If you don't need features of newer PB versions, you might try to compile your source code with PB 5.11 x86 and set the subsystem to "Carbon" (PB 5.11 x64 won't work because the Carbon framework was never ported to 64 bit by Apple!). The drag 'n drop implementation of the PB version using the Carbon framework was much better than the current PB version using the Cocoa framework. Even SetDragCallback() and SetDropCallback() are working in the Carbon framework version while they don't work in the Cocoa framework version. Furthermore the MacOS help for PB's drag 'n drop commands is still written for the Carbon framework... :wink:
wombats
Enthusiast
Enthusiast
Posts: 718
Joined: Thu Dec 29, 2011 5:03 pm

Re: Another drag-drop example with drag image (PB 4.1)

Post by wombats »

Shardik wrote:
wombats wrote:I really need it to be fixed for my project, but I'll just have to keep hoping it happens!
If you don't need features of newer PB versions, you might try to compile your source code with PB 5.11 x86 and set the subsystem to "Carbon" (PB 5.11 x64 won't work because the Carbon framework was never ported to 64 bit by Apple!). The drag 'n drop implementation of the PB version using the Carbon framework was much better than the current PB version using the Cocoa framework. Even SetDragCallback() and SetDropCallback() are working in the Carbon framework version while they don't work in the Cocoa framework version. Furthermore the MacOS help for PB's drag 'n drop commands is still written for the Carbon framework... :wink:
That's a nice idea, but I do need the features of the later versions, unfortunately. I use the Dialog library extensively, and I also use the OpenGLGadget and JSON libraries. Besides, 5.11 won't even work for me...I just downloaded it and it complains about, "The compiler isn't loaded yet..." :(
Post Reply