Drag and drop with multi

Share your advanced PureBasic knowledge/code with the community.
Jagermeister
Enthusiast
Enthusiast
Posts: 137
Joined: Thu Nov 15, 2012 11:38 pm
Location: Los Angeles

Drag and drop with multi

Post by Jagermeister »

My apologies for lack of comments. Enjoy!

The order of generating the full catalog followed by the section extract looks funny but it makes sense when an actual catalog is provided through an SQL query or external file.

Code: Select all

; Comment: Drag and drop with multi
; Author : Jagermeister
; Version: J
; Created: 21.12.2015
; Updated: 21.12.2015
; Link   : http://www.purebasic.fr/german/viewtopic.php?f=8&t=29343
; Notes  : Items only exist in one list_listicon at any time (intentional)

Structure sec_fields
  name.s
  start.l
  stop.l
EndStructure

Global NewList list_section.sec_fields()

;================================================

Structure cat_fields
  name.s
  no.s
  index.l
EndStructure

Global NewList list_catalog.cat_fields()

;================================================

Structure drop_fields
  drop_address.l
  drop_no.l
EndStructure

Global NewList list_drop.drop_fields()

;================================================

Global NewList list_listicons.l()

;================================================

Global win, section, catalog

;================================================

Declare filter()
Declare drag()
Declare drop()
Declare sort()

Procedure clear()
  
  ClearList(list_drop())
  
  ForEach list_listicons()
    ClearGadgetItems(list_listicons())
  Next
  
EndProcedure

Procedure filter()
  
  ClearGadgetItems(catalog)
  
  Select GetGadgetState(section)
    Case -1 ; If clicked in blank zone, show entire catalog...
      ForEach list_catalog()
        c = CountGadgetItems(catalog)
        string$ = list_catalog()\name + Chr(32)
        string$ + list_catalog()\no
        AddGadgetItem(catalog, c, string$)
        SetGadgetItemData(catalog, c, list_catalog()\index)
      Next
    Default ; ...otherwise filter to section
      selected = GetGadgetItemData(section, GetGadgetState(section))
      SelectElement(list_section(), selected)
      For i = list_section()\start To list_section()\stop
        c = CountGadgetItems(catalog)
        SelectElement(list_catalog(), i)
        string$ = list_catalog()\name + Chr(32)
        string$ + list_catalog()\no
        AddGadgetItem(catalog, c, string$)
        SetGadgetItemData(catalog, c, list_catalog()\index)
      Next
  EndSelect
  
EndProcedure

Procedure drag()
  
  Shared dragGadget
  
  Select EventType()
    Case #PB_EventType_DragStart
      dragGadget = EventGadget()
      ForEach list_listicons() ; Check if drag *from* gadget is catalog or a draggable listicon
        Select list_listicons()
          Case dragGadget
            list_listicon = dragGadget
            Break
        EndSelect
      Next           
      Select dragGadget
        Case catalog, list_listicon 
          Debug "drag: " + Str(dragGadget) + " : " + Str(GetGadgetItemData(dragGadget, GetGadgetState(dragGadget)))
          DragText("0") ; dummy data
      EndSelect
  EndSelect
  
EndProcedure

Procedure drop()
  
  Shared dragGadget
  
  dropGadget = EventGadget()
  
  For i = 0 To CountGadgetItems(dragGadget) - 1
    Select GetGadgetItemState(dragGadget, i)
      Case #PB_ListIcon_Selected
        selected = GetGadgetItemData(dragGadget, i)
        ForEach list_drop()
          Select list_drop()\drop_no
            Case selected
              DeleteElement(list_drop())
              Break
          EndSelect
        Next
        AddElement(list_drop())
        list_drop()\drop_address = dropGadget
        list_drop()\drop_no = selected
        Debug "drag: " + Str(dragGadget) + " : " + Str(selected) + " drop: " + Str(list_drop()\drop_address) + " : " + Str(list_drop()\drop_no)
    EndSelect
  Next
  
  sort()
  
EndProcedure

Procedure sort()
  
  SortStructuredList(list_drop(), #PB_Sort_Ascending, OffsetOf(drop_fields\drop_address), TypeOf(drop_fields\drop_address))
  SortStructuredList(list_drop(), #PB_Sort_Ascending, OffsetOf(drop_fields\drop_no), TypeOf(drop_fields\drop_no))
  
  ForEach list_listicons()
    ClearGadgetItems(list_listicons())
    ForEach list_drop()
      Select list_drop()\drop_address
        Case list_listicons()
          ForEach list_catalog()
            Select list_catalog()\index
              Case list_drop()\drop_no
                string$ = list_catalog()\name + Chr(32)
                string$ + list_catalog()\no
                AddGadgetItem(list_listicons(), -1, string$)
                SetGadgetItemData(list_listicons(), CountGadgetItems(list_listicons())-1, list_catalog()\index)
                Break
            EndSelect
          Next
      EndSelect
    Next
    SendMessage_(GadgetID(list_listicons()), #LVM_ENSUREVISIBLE, CountGadgetItems(list_listicons())-1, #True)
  Next
  
EndProcedure

win = OpenWindow(#PB_Any, 0, 0, 620, 345, "", #PB_Window_ScreenCentered | #PB_Window_SystemMenu | #PB_Window_Invisible)
button = ButtonGadget(#PB_Any, WindowWidth(win)-100, WindowHeight(win)-30, 95, 25, "Clear")
section = ListIconGadget(#PB_Any, 5, 5, 200, 305, "", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #LVS_NOCOLUMNHEADER)
catalog = ListIconGadget(#PB_Any, 210, 5, 200, 305, "", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #LVS_NOCOLUMNHEADER)

AddElement(list_listicons())
list_listicons() = ListIconGadget(#PB_Any, 415, 5, 200, 150, "", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #PB_ListIcon_MultiSelect)
EnableGadgetDrop(list_listicons(), #PB_Drop_Text, #PB_Drag_Copy)

AddElement(list_listicons())
list_listicons() = ListIconGadget(#PB_Any, 415, 160, 200, 150, "", 100, #PB_ListIcon_FullRowSelect | #PB_ListIcon_GridLines | #PB_ListIcon_MultiSelect)
EnableGadgetDrop(list_listicons(), #PB_Drop_Text, #PB_Drag_Copy)

;- Generate catalog list items

For i = 65 To 70
  While n < 4
    n + 1
    c = CountGadgetItems(catalog)
    AddElement(list_catalog())
    list_catalog()\name = Chr(i)
    list_catalog()\no = Str(n)
    list_catalog()\index = ListIndex(list_catalog())
    AddGadgetItem(catalog, ListIndex(list_catalog()), Chr(i) + Str(n))
    SetGadgetItemData(catalog, ListIndex(list_catalog()), ListIndex(list_catalog()))
  Wend
  n = 0
Next

ForEach list_catalog()
  new$ = list_catalog()\name
  If new$ <> old$
    c = CountGadgetItems(section)
    AddElement(list_section())
    list_section()\name = new$
    list_section()\start = ListIndex(list_catalog())
    AddGadgetItem(section, c, new$)
    SetGadgetItemData(section, c, ListIndex(list_section()))
  EndIf
  list_section()\stop = ListIndex(list_catalog())
  old$ = new$
Next

ForEach list_section()
  Debug list_section()\name + ", " + Str(list_section()\start) + ", " + Str(list_section()\stop)
Next

HideWindow(win, 0)

BindGadgetEvent(button, @clear())
BindEvent(#PB_Event_Gadget, @drag())
BindEvent(#PB_Event_GadgetDrop, @drop())
BindGadgetEvent(section, @filter(), #PB_EventType_LeftClick)

Repeat
  event = WaitWindowEvent()
Until event = #PB_Event_CloseWindow
End