ListView/ListIcon and correct using of Drag'n'Drop (move)?
Posted: Thu Mar 29, 2012 10:47 pm
Hello everyone,
I'm loosing my hair to find out, why the following use of EnableGadgetDrop() doesn't fire the needed #PB_EventType_DragStart event, when using it in my larger project source.
So anyone knows about restrictions / needed circumstances to have the Drag'n'Drop (move mode) to be supported in Listview / ListIcon gadgets?
The following code example works as expected, but in my large project it doesn't...
See below!
Here are some code snippets taken from my source...
Gadget definition (the gadget items are added/removed on the fly during the program flow):
Part of the Main event loop, where the related EventType is already checked directly after WindowEvent() for testing. But the requested EventType #PB_EventType_DragStart neither happens.... (not here, and not a bit later in the "regular" Select : Case : EndSelect block for processing all window+gadget events:
Did anyone of noticed similar effects?
(I'm currently coding on MacOS 10.5.8 - but it should be the same on Windows too...)
Thanks a lot for advice!
I'm loosing my hair to find out, why the following use of EnableGadgetDrop() doesn't fire the needed #PB_EventType_DragStart event, when using it in my larger project source.
So anyone knows about restrictions / needed circumstances to have the Drag'n'Drop (move mode) to be supported in Listview / ListIcon gadgets?
The following code example works as expected, but in my large project it doesn't...

Code: Select all
; English forum: http://www.purebasic.fr/english/viewtopic.php?f=13&t=49510
; Author: RASHAD (extended and reworked by Andre)
; Date: 21. March 2012
; OS: Windows, MacOS
; Demo: Yes
; Drag and Drop to the same ListIcon() as per the ques.
; Code by infratec (Berned)
Procedure ListIconGadgetColumns(GadgetNo)
i = 0
While GetGadgetItemAttribute(GadgetNo, 0, #PB_ListIcon_ColumnWidth, i) > 0
i + 1
Wend
ProcedureReturn i
EndProcedure
Procedure ListIconGadgetMove(GadgetNo.i, Source.i, Dest.i)
Columns = ListIconGadgetColumns(GadgetNo) - 1
For j = 0 To Columns
Source$ = GetGadgetItemText(GadgetNo, Source, j)
If Source < Dest
For i = Source To Dest - 1
Help$ = GetGadgetItemText(GadgetNo, i + 1, j)
SetGadgetItemText(GadgetNo, i, Help$, j)
Next i
Else
For i = Source To Dest + 1 Step - 1
Help$ = GetGadgetItemText(GadgetNo, i - 1, j)
SetGadgetItemText(GadgetNo, i, Help$, j)
Next i
EndIf
SetGadgetItemText(GadgetNo, i, Source$, j)
Next j
EndProcedure
Procedure ListViewGadgetMove(gad.i, Source.i, Dest.i)
Protected i, Help$, Source$
Source$ = GetGadgetItemText(gad, Source)
If Source < Dest
For i = Source To Dest - 1
Help$ = GetGadgetItemText(gad, i + 1)
SetGadgetItemText(gad, i, Help$)
Next
Else
For i = Source To Dest + 1 Step - 1
Help$ = GetGadgetItemText(gad, i - 1)
SetGadgetItemText(gad, i, Help$)
Next
EndIf
SetGadgetItemText(gad, i, Source$)
EndProcedure
OpenWindow(0, 0, 0, 600, 600, "Drag'n drop test", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)
; ContainerGadget is more for testing only (if the Drag'n'Drop also works, if the related gadgets are placed inside)
gad = ContainerGadget(#PB_Any, 5, 5, 590, 590, #PB_Container_Double)
listicon = ListIconGadget(#PB_Any, 10, 10, 570, 380, "Test 1", 150 , #PB_ListIcon_FullRowSelect|#PB_ListIcon_HeaderDragDrop)
AddGadgetColumn(listicon, 1, "Test 2", 150)
AddGadgetColumn(listicon, 2, "Test 3", 120)
AddGadgetColumn(listicon, 3, "Test 4", 120)
For i = 1 To 10
AddGadgetItem(listicon, -1, "Line " + Str(i) + " Col 1" + Chr(10) + "Line " + Str(i) + " Col 2"+ Chr(10) + "Line " + Str(i) + " Col 3" + Chr(10) + "Line " + Str(i) + " Col 4")
Next i
EnableGadgetDrop(Listicon, #PB_Drop_Private, #PB_Drag_Move, 1)
listview = ListViewGadget(#PB_Any, 10, 400, 570, 170)
For i = 1 To 10
AddGadgetItem(listview, -1, "Line " + Str(i))
Next i
EnableGadgetDrop(listview, #PB_Drop_Private, #PB_Drag_Move, 1)
Exit = #False
DragItem = -1
Repeat
Event = WaitWindowEvent()
EventType = EventType()
EventGadget = EventGadget()
Select Event
Case #PB_Event_Gadget
Select EventType
Case #PB_EventType_DragStart
DragItem = GetGadgetState(EventGadget)
DragPrivate(1, #PB_Drag_Move)
EndSelect
Case #PB_Event_GadgetDrop
If EventDropPrivate() = 1
TargetItem = GetGadgetState(EventGadget)
; Debug "Target: " + Str(TargetItem)
If EventGadget = listicon
ListIconGadgetMove(EventGadget, DragItem, TargetItem)
ElseIf EventGadget = listview
ListViewGadgetMove(EventGadget, DragItem, TargetItem)
EndIf
EndIf
Case #PB_Event_CloseWindow
Exit = #True
EndSelect
Until Exit
Here are some code snippets taken from my source...
Gadget definition (the gadget items are added/removed on the fly during the program flow):
Code: Select all
gad = ListViewGadget(#PB_Any, x, y, 200, fontheight*8, #PB_ListView_ClickSelect)
EnableGadgetDrop(gad, #PB_Drop_Private, #PB_Drag_Move, 1)
Code: Select all
Repeat
Event = WindowEvent() ; WaitWindowEvent()
EventGadget = EventGadget()
EventMenu = EventMenu()
EventWindow = EventWindow()
EventType = EventType()
If Event = #PB_Event_Gadget And Eventtype = #PB_EventType_DragStart
Debug "Drag'n'Drop started..."
DragItem = GetGadgetState(EventGadget)
DragPrivate(1, #PB_Drag_Move)
EndIf
....
(I'm currently coding on MacOS 10.5.8 - but it should be the same on Windows too...)
Thanks a lot for advice!