maxscript snippets .net listview sort multidimensional


rollout floaterSnippets "Snippets" width:210 height:190 --360
(
    button btnAddValue "A" pos: [4,5] width:21 height:21 enabled:true toolTip: "Add values into list"
    button btnRemoveValue "R" pos: [25,5] width:21 height:21 enabled:true toolTip: "Remove values from list"
   
    spinner spnVal "" pos:[50,8] width:50 height:16 range:[-1000,1000,00] type:#float
    edittext edtVal "" pos: [102,8] fieldWidth:80 labelOnTop:false text: "zero" readOnly: false
   
    dotNetControl lv "system.windows.forms.listView" pos:[5,35] width:200 height:150
   
    local normalfont, boldfont
    local lvColumnsArr=#("#", "Float", "String")
    local arr, olditems=#()
   
    fn initLv theLv = (
        theLv.view=(dotNetClass "system.windows.forms.view").details
        theLv.FullRowSelect=true
        theLv.GridLines=true
        theLv.MultiSelect=true --multiple selections
        theLv.CheckBoxes=false
        theLv.hideSelection=false
        theLv.IsAccessible=true
        theLv.LabelEdit=false --true
        )
       
    fn addColumns theLv columnsAr = (
        theLv.columns.add columnsAr[1] 40
        theLv.columns.add columnsAr[2] 60
        theLv.columns.add columnsAr[3] 80
        )
       
    fn populateList theLv arr = (
            rows=#()
            for t=1 to arr.count do (
            li=dotNetObject "System.Windows.Forms.ListViewItem" (t as string)
            li.UseItemStyleForSubItems=true
               
            colAdd=240+(if (mod t 2)==0 then 10 else -10)
            li.BackColor=li.backcolor.fromARGB colAdd colAdd colAdd
           
            li.subitems.add (arr[t][1] as string)
            li.subitems.add arr[t][2]
            li.subitems.item[0].name="0"
            li.subitems.item[1].name="1"
            li.subitems.item[2].name="2"
           
            append rows li
            )
           
       
        theLv.items.addRange rows
        theLv.Update()
        )
       
       
    on floaterSnippets open do (
        normalfont = dotNetObject "System.Drawing.Font" "Microsoft Sans Serif" 11 \
        (dotNetClass "System.Drawing.FontStyle").Regular (dotNetClass "System.Drawing.GraphicsUnit").Pixel
        boldfont = dotNetObject "System.Drawing.Font" "Microsoft Sans Serif" 11 \
        (dotNetClass "System.Drawing.FontStyle").Bold (dotNetClass "System.Drawing.GraphicsUnit").Pixel
       
        s=getFilenamePath (getSourceFileName()) --path to this script
        --print (maxFilePath+maxFileName) -- "" if no scene
           
        f=s+"Icons\\Add.gif"
        if doesFileExist f then btnAddValue.images=#(f, f, 1, 1, 1, 1, 1)
       
        f=s+"Icons\\Remove.gif"
        if doesFileExist f then btnRemoveValue.images=#(f, f, 1, 1, 1, 1, 1)
       
        arr=#()
        arr[1]=#(1.0, "one")
        arr[2]=#(2, "two")
        arr[3]=#(3, "three")
        arr[4]=#(4, "four")
        arr[5]=#(5, "five")
        arr[6]=#(6, "six")
        arr[7]=#(7.0, "seven")
       
        initLv lv
        addColumns lv lvColumnsArr
        populateList lv arr
        )
       
    on floaterSnippets resized arg do lv.height=arg.y-40
   
   
    fn makeshuffle arr = (
        n=arr.count
        for i=1 to n do (k=random i n; swap arr[i] arr[k])
        arr
        )
       
    fn compareFN v1 v2 col: = (
    case of (
        (v1[col]>v2[col]) : 1
        (v1[col]<v2[col]) : -1
        default: 0
        )
    )

    on lv columnClick arg do (
        lv.Clear()
        addColumns lv lvColumnsArr
        case arg.Column of (
            0: makeshuffle arr --shuffle array
            1: qsort arr compareFN col:1
            2: qsort arr compareFN col:2
            )
        populateList lv arr
        --for i in olditems do (lv.items.item[i].font=boldfont; lv.items.item[i].Selected=true)
        )
       
    on btnAddValue pressed do (
        append arr #(spnVal.value, edtVal.text)
        lv.Clear()
        addColumns lv lvColumnsArr
        populateList lv arr
        for i in olditems do (lv.items.item[i].font=boldfont; lv.items.item[i].Selected=true)
        )
   
    on btnRemoveValue pressed do if olditems.count>0 then (
        pushPrompt ("Removed indices: "+olditems as string)
        for i=olditems.count to 1 by -1 do deleteItem arr (olditems[i]+1)
        lv.Clear()
        addColumns lv lvColumnsArr
        populateList lv arr
        olditems=#()
        )
   
    on spnVal changed arg do if olditems.count>0 then (
        for i in olditems do (
            arr[i+1][1]=arg
            lv.items.item[i].subitems.item[1].text=arg as string
            )
        )
       
    on edtVal entered arg do if olditems.count>0 then (
        for i in olditems do (
            arr[i+1][2]=arg
            lv.items.item[i].subitems.item[2].text=arg
            )
        )
       
    on lv ItemSelectionChanged arg do (
            for i in olditems do lv.items.item[i].font=normalfont
            a=lv.SelectedItems
            olditems=#()
            for i=0 to (a.count-1) do (j=a.item[i].index; append olditems j; lv.items.item[j].font=boldfont)
        )
       
    on lv MouseClick arg do (
       
        theItem = lv.GetItemAt arg.x arg.y
        theSubItem = (theItem.GetSubItemAt arg.x arg.y)
        row = theItem.Index
        col = theSubItem.name as integer
       
        if arg.button==arg.Button.Left then (
            floaterSnippets.title="r: "+(row as string)+" c: "+(col as string)+" v: "+lv.items.item[row].subitems.item[col].text
            )
        if arg.button==arg.Button.Right then pushPrompt ("ArrItem: "+arr[row+1] as string)
        )
       
    on lv MouseDoubleClick arg do (
        hit=(lv.HitTest (dotNetObject "System.Drawing.Point" arg.x arg.y))
        row=hit.item.Index
        colAdd=(if (mod row 2)==0 then 20 else -20)
           
        lv.items.item[row].BackColor=lv.items.item[row].BackColor.fromARGB (220+colAdd) (100+colAdd) (100+colAdd)
        )
       
)
createDialog floaterSnippets style: #(#style_titlebar, #style_border, #style_sysmenu, #style_resizing, #style_minimizebox) lockHeight: false \
    lockWidth: true




















--TO BE CONTINUED