Page 2 of 2

Posted: Fri Jun 01, 2007 1:56 pm
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.

Posted: Mon Jun 04, 2007 11:19 am
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 

Posted: Mon Jun 04, 2007 6:48 pm
by srod
I can't presently get this to display correctly without using a little api!

I'll keep trying though.

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

Posted: Mon Mar 13, 2017 4:23 pm
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.

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

Posted: Mon Mar 13, 2017 6:36 pm
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...

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

Posted: Mon Mar 13, 2017 6:50 pm
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).

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

Posted: Mon Mar 13, 2017 7:06 pm
by Mindphazer
Yeah I know...
Seems Fred still doesn't know how to fix the issue...

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

Posted: Mon Mar 13, 2017 8:19 pm
by davido
Perhaps the PB model could be changed? :)

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

Posted: Mon Mar 13, 2017 9:06 pm
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!

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

Posted: Mon Mar 13, 2017 10:23 pm
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:

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

Posted: Mon Mar 13, 2017 10:54 pm
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..." :(