Page 1 of 2

Listicon - Delete, copy, paste, move items. [Win,Mac,Linux]

Posted: Sat Aug 23, 2014 2:51 pm
by minimy
This is a small include file to help manage listicon gadgets. :idea:

What can do? :?:
Delete item or items from listicon.
Move up or down, item or items from listicon.
Copy and paste, item or items from listicon..
Work in Win,Mac and Linux.

If you can improve it do not forget to share. Thank you and enjoy it! :mrgreen:

Greetings to all!! :)

Code: Select all

;{
; -------------------------------------------------------
; -------------------------------------------------------
; --                                                   --
; --   List Manager                                    --
; --                       Toni M. (minimy) 2014       --
; --                                                   --
; -------------------------------------------------------
; -------------------------------------------------------
;}

;{
; This is a little listicon help/manager.
; Win, Mac, Linux compatible.
; Spectrum And Atari Not compatible ;-)   Enjoy it..!!
;}

;----LISTICON
Global TListPortaPapelesRAM.s=""
Procedure TListGetCol(Lista.l)
  ; Column may have header name o width > 0 to work
  ; Return numbers of columns
  Protected Txt.s, P.l
  Protected Items = 0
  Protected Lar=  0
  For p=0 To 1000
    Txt = GetGadgetItemText(Lista,-1,p)
    Lar = GetGadgetItemAttribute(Lista,-1,#PB_ListIcon_ColumnWidth,p)
    If Len(Txt)>0 Or (Lar>0 And Lar<1000)
      Items+1
    EndIf
  Next p
  ProcedureReturn Items
EndProcedure
Procedure TListDelete(Lista,Pregunta=0)
  ; Delete selected items
  Protected Pos.l=0, Resp.l
  If CountGadgetItems(Lista)>0
    If Pregunta=1
      Resp=MessageRequester("Remove","Do you really want remove this item?",#PB_MessageRequester_YesNo)
    Else
      Resp=#PB_MessageRequester_Yes
    EndIf
  
    If Resp=#PB_MessageRequester_Yes
      Repeat
        If GetGadgetItemState(Lista,Pos) & #PB_ListIcon_Selected
        RemoveGadgetItem(Lista,Pos)
        Pos=-1
        EndIf
        Pos+1
      Until Pos=CountGadgetItems(Lista)
    EndIf
  EndIf
ProcedureReturn Resp
EndProcedure
Procedure TListDown(Lista)
  ; Seletcted items down
  Protected BloqueoLista.l, Col.l, Lin.l, ChainA$, ChainB$, EstadoA.l, EstadoB.l
  If CountGadgetItems(Lista)>1
    For p=1 To CountGadgetItems(Lista)-1
      If GetGadgetItemState(Lista,p) & #PB_ListIcon_Selected
      BloqueoLista=p
      EndIf
    Next p
    
    If BloqueoLista<CountGadgetItems(Lista)-1
      Col=TListGetCol(Lista)
      Lin=CountGadgetItems(Lista)
      For p= Lin-2 To 0 Step -1
        EstadoA=GetGadgetItemState(Lista,p)
        If EstadoA=1 Or EstadoA=3
          ChainA$=""
          ChainB$=""
          EstadoB=GetGadgetItemState(Lista,p+1)
          For n=0 To Col-1
            ChainA$=GetGadgetItemText(Lista,p,n)
            ChainB$=GetGadgetItemText(Lista,p+1,n)
            SetGadgetItemText(Lista,p,ChainB$,n)
            SetGadgetItemText(Lista,p+1,ChainA$,n)
            SetGadgetItemState(Lista,p,EstadoB)
            SetGadgetItemState(Lista,p+1,EstadoA)
          Next n
        EndIf
      Next p
    EndIf
  EndIf
EndProcedure
Procedure TListUp(Lista)
  ; Seletcted items up
  Protected BloqueoLista.l, Col.l, Lin.l, ChainA$, ChainB$, EstadoA.l, EstadoB.l
  If CountGadgetItems(Lista)>1
      If GetGadgetItemState(Lista,0) & #PB_ListIcon_Selected
      BloqueoLista=1
      EndIf
  
    If BloqueoLista=0
      Col=TListGetCol(Lista)
      Lin=CountGadgetItems(Lista)
      For p= 0 To Lin-2
        EstadoA=GetGadgetItemState(Lista,p+1)
        If EstadoA=1 Or EstadoA=3
          ChainA$=""
          ChainB$=""
          EstadoB=GetGadgetItemState(Lista,p)
          For n=0 To Col-1
            ChainA$=GetGadgetItemText(Lista,p+1,n)
            ChainB$=GetGadgetItemText(Lista,p,n)
            SetGadgetItemText(Lista,p+1,ChainB$,n)
            SetGadgetItemText(Lista,p,ChainA$,n)
            SetGadgetItemState(Lista,p+1,EstadoB)
            SetGadgetItemState(Lista,p,EstadoA)
          Next n
        EndIf
      Next p
    EndIf
  EndIf
EndProcedure
Procedure TListCopy(Lista)
  ; copy items
If CountGadgetItems(Lista)>0
  Protected Col.l=-1 : Repeat : Col+1 : Until GetGadgetItemText(Lista,-1,Col)=""; : Debug Col ; Numero de columnas
  Protected LCopy$=""
  Protected Pos.l=0, Estado.l, n.l, a$
  Repeat
    If GetGadgetItemState(Lista,Pos) & #PB_ListIcon_Selected
      Estado=GetGadgetItemState(Lista,Pos)
      SetGadgetItemState(Lista,Pos,Estado-1)
      LCopy$+Str(Estado)
      For n=0 To Col-1
        LCopy$+GetGadgetItemText(Lista,Pos,n)+"¬"
      Next n
      LCopy$+"^"
      a$="Copiado: "+Str(Pos)
;       Debug a$
    EndIf
    Pos+1
  Until Pos=CountGadgetItems(Lista)
  If LCopy$<>"" : TListPortaPapelesRAM=LCopy$ : EndIf
EndIf
EndProcedure
Procedure TListPaste(Lista.l)
  ; Paste items
  Protected Sele.l, OldSele.l, Estado.l, EstadoO.l, P1.l, P2.l, Cadena$, Chain$, lugar.l, resto.l
  Sele=GetGadgetState(lista)
  OldSele=Sele
If TListPortaPapelesRAM<>""
  If Sele>-1
    Resto=CountGadgetItems(Lista)-Sele : EstadoO=GetGadgetItemState(Lista,sele)
    If OldSele>-1:SetGadgetItemState(Lista,Sele,EstadoO-1):EndIf
    Col=CountString(TListPortaPapelesRAM,"¬") :  P1=1
    Repeat
        P2=FindString(TListPortaPapelesRAM,"^",P1) : Cadena$=Mid(TListPortaPapelesRAM,P1,P2-P1)+Chr(10) : P1=P2+1
        Chain$="" : Estado=Val(Left(Cadena$,1))
        Pos1=2
      Repeat : pos2=FindString(Cadena$,"¬",pos1) : Chain$+Mid(Cadena$,pos1,pos2-pos1)+Chr(10) : pos1=pos2+1 : Until pos2=0
      If P2>0
        AddGadgetItem(Lista,-1,Chain$) : SetGadgetItemState(Lista,CountGadgetItems(Lista)-1,Estado)
      EndIf
    Until P2=0
    For p=1 To resto-1 : TListUp(Lista) :  Next p
  Else
    Resto=CountGadgetItems(Lista)-Sele : EstadoO=GetGadgetItemState(Lista,sele)
    If OldSele>-1:SetGadgetItemState(Lista,Sele,EstadoO-1):EndIf
    Col=CountString(TListPortaPapelesRAM,"¬") :  P1=1
    Repeat
        P2=FindString(TListPortaPapelesRAM,"^",P1) : Cadena$=Mid(TListPortaPapelesRAM,P1,P2-P1)+Chr(10) : P1=P2+1
        Chain$="" : Estado=Val(Left(Cadena$,1))
        Pos1=2
      Repeat : pos2=FindString(Cadena$,"¬",pos1) : Chain$+Mid(Cadena$,pos1,pos2-pos1)+Chr(10) : pos1=pos2+1 : Until pos2=0
      If P2>0
        AddGadgetItem(Lista,lugar,Chain$) : If lugar > -1 : SetGadgetItemState(Lista,lugar,Estado) : Else : SetGadgetItemState(Lista,CountGadgetItems(Lista)-1,Estado) : EndIf
      EndIf
    Until P2=0
  EndIf

EndIf
EndProcedure



;{ Example
CompilerIf Not #PB_Compiler_IsIncludeFile
  
  Window_0 = OpenWindow(#PB_Any, 438, 83, 643, 400, "List Manager", #PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_MinimizeGadget|#PB_Window_TitleBar)
  If Window_0
    ListIcon_0 = ListIconGadget(#PB_Any, 10, 20, 520, 350, "Gadget_0", 100, #PB_ListIcon_AlwaysShowSelection|#PB_ListIcon_GridLines|#PB_ListIcon_MultiSelect|#PB_ListIcon_FullRowSelect)
    AddGadgetColumn(ListIcon_0,1,"kaka",200)
    ;SendMessage_(GadgetID(ListIcon_0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE_USEHEADER) ; Only windows ;-)
    Button_1 = ButtonGadget(#PB_Any, 535, 20, 80, 25, "clear list")
    Button_2 = ButtonGadget(#PB_Any, 535, 50, 80, 25, "add item")
    Button_3 = ButtonGadget(#PB_Any, 535, 80, 80, 25, "move up")
    Button_4 = ButtonGadget(#PB_Any, 535, 110, 80, 25, "move down")
    Button_5 = ButtonGadget(#PB_Any, 535, 140, 80, 25, "copy")
    Button_6 = ButtonGadget(#PB_Any, 535, 170, 80, 25, "paste")
    Button_7 = ButtonGadget(#PB_Any, 535, 200, 80, 25, "Delete")
  EndIf
  
  Repeat
    Event = WaitWindowEvent(1)
    Select Event
        
      Case #PB_Event_Gadget
        EventGadget = EventGadget()
        EventType = EventType()
        If EventGadget = ListIcon_0
        ElseIf EventGadget = Button_1
          ClearGadgetItems(ListIcon_0)
          
        ElseIf EventGadget = Button_2
          AddGadgetItem(ListIcon_0,-1, Str(Random(99999999))+Chr(10)+"more... "+Str(Random(99999999)) )
          
        ElseIf EventGadget = Button_3
          TListUp(ListIcon_0)
          
        ElseIf EventGadget = Button_4
          TListDown(ListIcon_0)
          
        ElseIf EventGadget = Button_5
          TListCopy(ListIcon_0)
          
        ElseIf EventGadget = Button_6
          TListPaste(ListIcon_0)
          
         ElseIf EventGadget = Button_7
          TListDelete(ListIcon_0)
        EndIf
        
      Case #PB_Event_CloseWindow
        EventWindow = EventWindow()
        If EventWindow = Window_0
          CloseWindow(Window_0)
          Window_0 = 0
          Break
        EndIf
        
    EndSelect
  ForEver
  
CompilerEndIf
;}

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sat Aug 23, 2014 3:01 pm
by skywalk
Nicely done. Missing Delete...
ElseIf EventGadget = Button_7

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sat Aug 23, 2014 3:19 pm
by minimy
skywalk wrote:Nicely done. Missing Delete...
ElseIf EventGadget = Button_7
Solved, thanks! :oops:

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sat Aug 23, 2014 8:33 pm
by Andre
Nice, thank you. :)

Just a note: the SendMessage_() function is Windows only.... probably you can use the PB-command SetGadgetItemAttribute() instead?

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sun Aug 24, 2014 1:33 am
by electrochrisso
Neat minimy, thanks for sharing. :)

I did what Andre suggested and used

Code: Select all

SetGadgetItemAttribute(ListIcon_0, 0, #PB_ListIcon_ColumnWidth, #LVSCW_AUTOSIZE_USEHEADER, 0)
which seems to work ok. The only thing is the value constant to be used for the other OS , I am only on windows so I cant test this.

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sun Aug 24, 2014 10:58 am
by davido
@minimy

Very nice. Thank you for sharing. :D

By the way, it does not work on a Mac or LInux.
I tried the suggestion made by Andre, implemented by electrochrisso but unfortunately that does not work on Mac or Linux.

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sun Aug 24, 2014 2:11 pm
by RASHAD

Code: Select all

;SendMessage_(GadgetID(ListIcon_0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE_USEHEADER)
.
.
.
.
        ElseIf EventGadget = Button_2
          AddGadgetItem(ListIcon_0,-1, Str(Random(99999999))+Chr(10)+"more... "+Str(Random(99999999)) )
          StartDrawing(WindowOutput(Window_0))
          		Colwidth = TextWidth(GetGadgetItemText(ListIcon_0,CountGadgetItems(ListIcon_0)-1,0))
          StopDrawing()
          If Colwidth >= Oldcolwidth
             SetGadgetItemAttribute(ListIcon_0, 0, #PB_ListIcon_ColumnWidth, Colwidth, 0)
             Oldcolwidth = Colwidth
          EndIf


Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Mon Aug 25, 2014 12:44 am
by electrochrisso
davido wrote:@minimy

Very nice. Thank you for sharing. :D

By the way, it does not work on a Mac or LInux.
I tried the suggestion made by Andre, implemented by electrochrisso but unfortunately that does not work on Mac or Linux.
I think it can work if Mac and Linux have a constant similar to the windows #LVSCW_AUTOSIZE_USEHEADER, which is the size of the header text, which is what RASHAD code has done in another way by grabbing the width of the header text by using the drawing commands. :)
I am still wondering though if Mac and Linux have similar constants as windows has. :?:

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Mon Aug 25, 2014 10:08 am
by Shardik
electrochrisso wrote:I think it can work if Mac and Linux have a constant similar to the windows #LVSCW_AUTOSIZE_USEHEADER, which is the size of the header text, which is what RASHAD code has done in another way by grabbing the width of the header text by using the drawing commands. :)
I am still wondering though if Mac and Linux have similar constants as windows has. :?:
It's not necessary to use any API code or constants at all. RASHAD has already demonstrated how to obtain the width of a text cell in a cross-platform way and set the width of the text cell's column. Instead of using the Windows constant #LVSCW_AUTOSIZE_USEHEADER to resize a column to the width of its header cell, you may insert this procedure:

Code: Select all

Procedure SetColumnWidthToHeaderWidth(WindowID.I, ListIconID.I, Column.I)
  If StartDrawing(WindowOutput(WindowID))
    ColumnWidth = TextWidth(GetGadgetItemText(ListIconID, -1, Column))
    StopDrawing()
    SetGadgetItemAttribute(ListIconID, 0, #PB_ListIcon_ColumnWidth, ColumnWidth, Column)
  EndIf
EndProcedure
and then replace

Code: Select all

    SendMessage_(GadgetID(ListIcon_0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE_USEHEADER)
by a call to the new procedure:

Code: Select all

    SetColumnWidthToHeaderWidth(Window_0, ListIcon_0, 0)
and you have a true cross-platform solution.

By the way the most easiest change would be to simply delete the SendMessage_() line because it is in no way essential to demonstrate the use of minimy's functions. Running the example with my added procedure, the text in the first column will be cut on Linux. So it would be more useful to adjust the column width to the widest entry in a column as RASHAD has demonstrated in his posting...

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Mon Aug 25, 2014 1:59 pm
by davido
@RASHAD,

Neat solution! And no API :wink:
Works on my Mac, now.
Thank you very much. :D

Thanks, Shardik, I'll take a look at your solution. Looks interesting.

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Tue Aug 26, 2014 1:01 am
by electrochrisso
@Shardik, yes I know how RASHAD'S code work, I have used the drawing text width many times for such things, I was simply asking if Mac and Linux have equivalent constant to windows, if so, how many other equivalents. It would be interesting to make up a cross reference table of compatible constants between the different OS. :)

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Wed Aug 27, 2014 8:02 pm
by minimy
Sorry.. :oops:

I put this symbol ';' before sendmessage.. and solved, multiplataform again.. :mrgreen:

; SendMessage_(GadgetID(ListIcon_0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE_USEHEADER)


Greetings and thank to all for aportations!

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Thu Aug 28, 2014 12:45 am
by electrochrisso
minimy wrote:Sorry.. :oops:

I put this symbol ';' before sendmessage.. and solved, multiplataform again.. :mrgreen:

; SendMessage_(GadgetID(ListIcon_0), #LVM_SETCOLUMNWIDTH, 0, #LVSCW_AUTOSIZE_USEHEADER)


Greetings and thank to all for aportations!
You little brat :lol:

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Mon Nov 21, 2016 2:35 pm
by blueb
Cleaned up code and converted to a module...

Code: Select all

;==================================================================
; File name: ListManager.pbi
; Typical Usage:    ListManager Module - to help working with lists
;
; File version: 1.1
; Programmed by : Blueb
; Date : November 27, 2016
; Last Update : 
; PureBasic code : V5.50 
; Platforms tested: Windows(x86) , MacOS
; 
; This code was originally created by:  Toni M. (minimy) 2014
; Original code location: http://www.purebasic.fr/english/viewtopic.php?p=451679#p451679
; ------------------------------------------------------------------------------------------------------------------------------------------------
; I deserve credit only to convert the original code into a Module.
;
; This code is free to be use where ever you like but you use it at your own risk.
;
; The author can in no way be held responsible for data loss, damage or other annoying situations that
; may occur.
; ------------------------------------------------------------------------------------------------------------------------------------------------
; Changes:
; Nov. 27 - mk-soft - Changed ListDelete() procedure for Mac bug. 
;==================================================================
DeclareModule ListManager
;Declares...
     Declare ListGetCol(ListNum.i)                          ; Return numbers of columns
     Declare ListDelete(ListNum.i, AskUser.i = 0)     ; Delete selected items
     Declare ListDown(ListNum.i)                             ; Move selected items down
     Declare ListUp(ListNum.i)                                  ; Move seletcted items up
     Declare ListCopy(ListNum.i)                              ; Copy selected items
     Declare ListPaste(ListNum.i)                            ; Paste selected items
     
EndDeclareModule

Module ListManager
;Globals...
Global LineContents.s = "" ; assures empty value.
Global LastPosition.i
;Procedures...
Procedure ListGetCol(ListNum.i)  ; Return number of columns
 ;  Column may have header name o width > 0 to work
     Protected Text$, X.i
     Protected Items = 0
     Protected Attrib =  0
     For X = 0 To 1000
          Text$ = GetGadgetItemText(ListNum, -1, X)
          Attrib = GetGadgetItemAttribute(ListNum, -1, #PB_ListIcon_ColumnWidth, X)
          If Len(Text$) > 0 Or (Attrib > 0 And Attrib <1000)
               Items+1
          EndIf
     Next X
     ProcedureReturn Items
EndProcedure
; --------------------------------------------------
 Procedure ListDelete(ListNum.i, AskUser = 0)   ; Delete selected items
    Protected Pos.i = 0, Reply.i
    If CountGadgetItems(ListNum) > 0
      If AskUser = 1
        Reply = MessageRequester("Remove", "Do you really want remove this item?", #PB_MessageRequester_YesNo)
      Else
        Reply = #PB_MessageRequester_Yes
      EndIf
      
      If Reply = #PB_MessageRequester_Yes
        For Pos = CountGadgetItems(ListNum) - 1 To 0 Step -1
          If GetGadgetItemState(ListNum, Pos)  &  #PB_ListIcon_Selected
            RemoveGadgetItem(ListNum, Pos)
          EndIf
        Next
      EndIf
    EndIf
    
    ProcedureReturn Reply
  EndProcedure
; --------------------------------------------------
Procedure ListDown(ListNum.i)  ; Move selected items down
     Protected ListBlock.i, Col.i, Lin.i, ChainA$, ChainB$, StateA.i, StateB.i
     If CountGadgetItems(ListNum) > 1
          For X = 1 To CountGadgetItems(ListNum) - 1
               If GetGadgetItemState(ListNum, X)  &  #PB_ListIcon_Selected
                    ListBlock = X
               EndIf
          Next X
          
          If ListBlock < CountGadgetItems(ListNum) - 1
               Col=ListGetCol(ListNum)
               Lin=CountGadgetItems(ListNum)
              
               For X= Lin - 2 To  0 Step - 1
                    StateA = GetGadgetItemState(ListNum, X)
                    If StateA = 1 Or StateA = 3
                         ChainA$=""
                         ChainB$=""
                         StateB = GetGadgetItemState(ListNum, X+1)
                         For Y = 0 To Col - 1
                              ChainA$=GetGadgetItemText(ListNum, X, Y)
                              ChainB$=GetGadgetItemText(ListNum, X + 1, Y)
                              SetGadgetItemText(ListNum, X, ChainB$, Y)
                              SetGadgetItemText(ListNum, X + 1, ChainA$, Y)
                              SetGadgetItemState(ListNum, X, StateB)
                              SetGadgetItemState(ListNum, X + 1, StateA)
                         Next Y
                    EndIf
               Next X
          EndIf
     EndIf
EndProcedure
; --------------------------------------------------
Procedure ListUp(ListNum.i)   ; Move seletcted items up
     Protected ListBlock.i, Col.i, Lin.i, ChainA$, ChainB$, StateA.i, StateB.i
     If CountGadgetItems(ListNum) > 1
          If GetGadgetItemState(ListNum, 0)  &  #PB_ListIcon_Selected
               ListBlock = 1
          EndIf
          
          If ListBlock = 0
               Col = ListGetCol(ListNum)
               Lin = CountGadgetItems(ListNum)
               For X = 0  To  Lin - 2
                    StateA = GetGadgetItemState(ListNum, X + 1)
                    If StateA = 1 Or StateA = 3
                         ChainA$=""
                         ChainB$=""
                         StateB = GetGadgetItemState(ListNum, X)
                         For Y = 0 To Col - 1
                              ChainA$=GetGadgetItemText(ListNum, X + 1, Y)
                              ChainB$=GetGadgetItemText(ListNum, X, Y)
                              SetGadgetItemText(ListNum, X + 1, ChainB$, Y)
                              SetGadgetItemText(ListNum, X, ChainA$, Y)
                              SetGadgetItemState(ListNum, X + 1, StateB)
                              SetGadgetItemState(ListNum, X, StateA)
                         Next Y
                    EndIf
               Next X
          EndIf
     EndIf
EndProcedure
; --------------------------------------------------
Procedure ListCopy(ListNum.i)  ; copy selected items
     If CountGadgetItems(ListNum) > 0
          Protected Col.i = -1 
          Repeat 
               Col + 1 
          Until GetGadgetItemText(ListNum, -1, Col) = "" ;  Debug Col ; Number of columns
          Protected LCopy$=""
          Protected Pos.i = 0, State.i, X.i, a$
          Repeat
               If GetGadgetItemState(ListNum,Pos)  &  #PB_ListIcon_Selected
                    State = GetGadgetItemState(ListNum, Pos)
                    SetGadgetItemState(ListNum, Pos, State - 1)
                    LCopy$ + Str(State)
                     For X = 0 To Col - 1
                         LCopy$ + GetGadgetItemText(ListNum, Pos, X) + "¬"
                    Next X
                    LCopy$ + "^"
                    a$="Copy Line Num: "+Str(Pos) ;  Debug a$ ; Copy Line Num
               EndIf
               Pos+1
          Until Pos = CountGadgetItems(ListNum)
          If LCopy$ <> "" 
               LineContents = LCopy$
          EndIf
     EndIf
EndProcedure
; --------------------------------------------------
Procedure ListPaste(ListNum.i) ; Paste selected items
     Protected SelectedItem.i, OldSelectedItem.i, State.i, State2.i, P1.i, P2.i, Link$, Chain$, Place.i, ItemCount.l
 
     SelectedItem = GetGadgetState(ListNum)
     OldSelectedItem = SelectedItem
     
     If LineContents <> ""
          If Selection > -1
               ItemCount = CountGadgetItems(ListNum) - SelectedItem
               State2 = GetGadgetItemState(ListNum, SelectedItem)
               If OldSelectedItem > -1 
                    SetGadgetItemState(ListNum, SelectedItem, State2 - 1)
               EndIf
               Col = CountString(LineContents, "¬")
                P1 = 1
               Repeat
                    P2 = FindString(LineContents, "^", P1) 
                    Link$ = Mid(LineContents, P1, P2 - P1) + Chr(10)
                    P1 = P2 + 1
                    Chain$ = "" 
                    State = Val(Left(Link$, 1))
                    Pos1 = 2
                    Repeat 
                         pos2 = FindString(Link$, "¬", pos1)
                         Chain$ + Mid(Link$, pos1, pos2 - pos1) + Chr(10) 
                         pos1 = pos2 + 1
                    Until pos2 = 0
                    If P2 > 0
                         AddGadgetItem(ListNum,-1,Chain$) 
                         SetGadgetItemState(ListNum, CountGadgetItems(ListNum) -1, State)
                    EndIf
               Until P2 = 0
               For p = 1 To  ItemCount - 1 
                    ListUp(ListNum)
               Next p
          Else
               ItemCount = CountGadgetItems(ListNum) - SelectedItem 
               State2 = GetGadgetItemState(ListNum, SelectedItem)
               If OldSelectedItem > -1
                    SetGadgetItemState(ListNum, SelectedItem, State2 - 1)
               EndIf
               Col = CountString(LineContents, "¬") 
                P1 = 1
               Repeat
                    P2 = FindString(LineContents, "^", P1) 
                    Link$ = Mid(LineContents, P1, P2 - P1) + Chr(10)
                    P1 = P2 + 1
                    Chain$ = "" 
                    State = Val(Left(Link$, 1))
                    Pos1 = 2
                    Repeat 
                         pos2 = FindString(Link$, "¬", pos1) 
                         Chain$ + Mid(Link$, pos1, pos2 - pos1) + Chr(10) 
                         pos1 = pos2 + 1
                    Until pos2 = 0
                    If  P2 > 0
                         AddGadgetItem(ListNum, Place, Chain$) 
                         If Place > -1
                              SetGadgetItemState(ListNum,Place,State) 
                         Else 
                              SetGadgetItemState(ListNum, CountGadgetItems(ListNum) - 1, State)
                         EndIf
                    EndIf
               Until P2 = 0
          EndIf
          
     EndIf
EndProcedure
; --------------------------------------------------
EndModule

;==================================================================
;------- Test Area -------
;==================================================================

CompilerIf Not #PB_Compiler_IsIncludeFile
     
     UseModule ListManager
     
     TestWindow = OpenWindow(#PB_Any, 438, 83, 643, 400, "List Manager", #PB_Window_SystemMenu | #PB_Window_SizeGadget | #PB_Window_MinimizeGadget | #PB_Window_TitleBar)
     If TestWindow
               myList = ListIconGadget(#PB_Any, 10, 20, 520, 350, "Transaction #", 100, #PB_ListIcon_AlwaysShowSelection | #PB_ListIcon_GridLines | #PB_ListIcon_MultiSelect | #PB_ListIcon_FullRowSelect)
                              AddGadgetColumn(myList, 1, "Lotto Number", 300)
              ; Create some test buttons
               ClearBtn = ButtonGadget(#PB_Any, 535, 20, 80, 25, "Clear List")
               AddBtn = ButtonGadget(#PB_Any, 535, 50, 80, 25, "Add Item")
               MoveUpBtn = ButtonGadget(#PB_Any, 535, 80, 80, 25, "Move Up")
               MoveDnBtn = ButtonGadget(#PB_Any, 535, 110, 80, 25, "Move Down")
               CopyBtn = ButtonGadget(#PB_Any, 535, 140, 80, 25, "Copy")
               PasteBtn = ButtonGadget(#PB_Any, 535, 170, 80, 25, "Paste")
               DeleteBtn = ButtonGadget(#PB_Any, 535, 200, 80, 25, "Delete")
     EndIf
     
     Repeat
          
          Select WaitWindowEvent(1)              
              ; -----------------------------------------                    
                Case #PB_Event_Gadget
                    Select EventGadget()
                             Case ClearBtn
                                   ClearGadgetItems(myList)
                                   
                               Case AddBtn
                                    lotto.s = "Your winning Lotto number for today is: " + 
                                              Str(Random(49)) + " " + Str(Random(49)) + " " + 
                                              Str(Random(49)) + " " + Str(Random(49)) + " " + 
                                              Str(Random(49)) + " " + Str(Random(49))
                                    
                                   AddGadgetItem(myList, -1,  Str(Random(99999)) + Chr(10) + lotto.s)
                                   
                               Case MoveUpBtn
                                   ListUp(myList)
                                   
                               Case MoveDnBtn
                                   ListDown(myList)
                                   
                               Case CopyBtn
                                   ListCopy(myList)
                                   
                               Case PasteBtn
                                   ListPaste(myList)
                                   
                              Case DeleteBtn
                                   ListDelete(myList, 0) ; (1 confirms with the user)
                       
                       EndSelect
              ; -----------------------------------------     
               Case #PB_Event_CloseWindow
                    EventWindow = EventWindow()
                    If EventWindow = TestWindow
                         CloseWindow(TestWindow)
                         TestWindow = 0
                         Break
                    EndIf
                    
          EndSelect
     ForEver
     
CompilerEndIf

;------- End of Test Area

Re: Listicon - Delete, copy, paste, move items. [Win,Mac,Lin

Posted: Sun Nov 27, 2016 1:20 pm
by mk-soft
Very nice :wink:

But PB for mac have a bug with "If GetGadgetItemState(ListNum, Pos) & #PB_ListIcon_Selected" and deleted all items to end of list.

I have change code to...

Code: Select all

  Procedure ListDelete(ListNum.i, AskUser = 0)   ; Delete selected items
    Protected Pos.i = 0, Reply.i
    If CountGadgetItems(ListNum) > 0
      If AskUser = 1
        Reply = MessageRequester("Remove", "Do you really want remove this item?", #PB_MessageRequester_YesNo)
      Else
        Reply = #PB_MessageRequester_Yes
      EndIf
      
      If Reply = #PB_MessageRequester_Yes
        For Pos = CountGadgetItems(ListNum) - 1 To 0 Step -1
          If GetGadgetItemState(ListNum, Pos)  &  #PB_ListIcon_Selected
            RemoveGadgetItem(ListNum, Pos)
          EndIf
        Next
      EndIf
    EndIf
    
    ProcedureReturn Reply
  EndProcedure