Page 2 of 2

Re: ListIconGadget() with private drag and drop

Posted: Wed Dec 30, 2009 7:23 pm
by WilliamL
Yes, very nice example of how to use the drag functions. It works fine on my Mac.

Thanks for the example.

Re: ListIconGadget() with private drag and drop

Posted: Thu Dec 31, 2009 11:39 am
by infratec
@Marco2007

I made a version with AddGadgetItem() and RemoveGadgetItem().
My first thought was: this has to be faster.
But it takes nearly the same time.
Maybe that's because the whole internal numeration has to be changed
by both functions.
Hm, the code itself is shorter. But it looks not so 'intelligent' :mrgreen:

Here is the code for your own tests:

Code: Select all

#VERSION = 2

Procedure ListIconGadgetColumns(GadgetNo)
 i = 0
; While GetGadgetItemAttribute(GadgetNo, 0, #PB_ListIcon_ColumnWidth, i) > 0
;  i + 1
; Wend
Repeat
 width = GetGadgetItemAttribute(GadgetNo, 0, #PB_ListIcon_ColumnWidth, i)
 i + 1
Until width <= 0 Or width > GadgetWidth(GadgetNo)
i - 1 
 
 ProcedureReturn i

EndProcedure


Procedure ListIconGadgetMove(GadgetNo.i, Source.i, Dest.i)

;Debug "Source: " + Str(Source) + " Dest: " + Str(Dest)
 
 If Dest >= 0
  Columns = ListIconGadgetColumns(GadgetNo) - 1
CompilerIf #VERSION = 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
CompilerElse
  Source$ = ""
  For i = 0 To Columns
   Source$ + GetGadgetItemText(GadgetNo, Source, i) + Chr(10)
  Next i
  Source$ = Left(Source$, Len(Source$) - 1)
  RemoveGadgetItem(GadgetNo, Source)
  AddGadgetItem(GadgetNo, Dest, Source$)
  SetGadgetState(GadgetNo, Dest)
CompilerEndIf
 EndIf
EndProcedure


OpenWindow(0, 0, 0, 330, 220, "Drag'n drop test", #PB_Window_MinimizeGadget|#PB_Window_ScreenCentered)

ListIconGadget(0, 10, 10, 310, 200, "Test 1", 150 , #PB_ListIcon_FullRowSelect|#PB_ListIcon_HeaderDragDrop)
AddGadgetColumn(0, 1, "Test 2", 150)
For i = 1 To 20
; AddGadgetItem(0, -1, "Line " + Str(i))
 AddGadgetItem(0, -1, "Line " + Str(i) + " Col 1" + Chr(10) + "Line " + Str(i) + " Col 2")
Next i

EnableGadgetDrop(0, #PB_Drop_Private, #PB_Drag_Move, 1)


StartTime = ElapsedMilliseconds()
For i = 0 To 1000
 ListIconGadgetMove(0, 18, 1)
Next i
Debug ElapsedMilliseconds() - StartTime



Exit = #False
DragItem = -1

Repeat
 Event = WaitWindowEvent()

 Select Event
  Case #PB_Event_Gadget
   Select EventType()
    Case #PB_EventType_DragStart
     DragItem = GetGadgetState(0)
     DragPrivate(1, #PB_Drag_Move)
   EndSelect
  Case #PB_Event_GadgetDrop
   If EventDropPrivate() = 1
    TargetItem = GetGadgetState(0)
    ListIconGadgetMove(0, DragItem, TargetItem)
   EndIf
  Case #PB_Event_CloseWindow
   Exit = #True
 EndSelect

Until Exit
Simply change the #VERSION constant to 1 or 2

But I still have to build the text across all columns.
I modified now the ListIconGadgetColumns() to make one more check.
But as it looks, it still depends on 'something' if it works or not.


@WilliamL
Thank's for the check on a MAC.
(That's the only OS what I can not test)

Best regards,
Bernd