AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[module] TVX - Tree View eXtension
Goto page Previous  1, 2
 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
rexx



Joined: 28 Feb 2006
Posts: 72

PostPosted: Sat Apr 18, 2009 2:09 pm    Post subject: Reply with quote

I use icons in my treeview, so I modified it to keep the right icon when moving up and down.

Here's it.

Code:
;Title: TreeViewX
;      TreeViewX  extends standard TreeView control to support moving, deleting & inserting.

;----------------------------------------------------------------------------------------
; Function: TVX
;         Initialisation function. Mandatory to call before you show the TreeView.
;
; Parameters:
;         pTree      - AHK name of the TreeView control
;         pSub      - Subroutine for TreeViewX, the same rules as in g.
;         pOptions   - String containing space delimited options for setting up TreeViewX
;         pUserData   - Base name of the array holding user data.
;                    This array is indexed using tree view item handles.
;
; Options:
;         HasRoot      - TreeViewX has root item - the one containing all other items.
;                    Root item can't be moved, edited or delited, and items can not
;                    be moved or created outside of it. This option need to be set
;                    after root is already added to the menu, as TreeViewX need to
;                    know the root menu handle.
;
;      CollapseOnMove   - When moving item out of of its container, this option makes container collapse
;      EditOnInsert   - Automaticaly enters edit mode upon insertion of new item
;
; Example:
;>
;>       TVX("MyTree", "Handler", "HasRoot CollapseOnMove")
;>
TVX( pTree, pSub, pOptions="", pUserData="", pIconData="" ) {
   global

   if InStr(pOptions, "HasRoot")   {
      TVX_HasRoot  := 1
      TVX_root := TV_GetNext()
   }

   if InStr(pOptions, "CollapseOnMove")
        TVX_CollapseOnMove := 1

   if InStr(pOptions, "EditOnInsert")
      TVX_EditOnInsert   :=    1

   TVX_userData := pUserData
   TVX_iconData := pIconData
   TVX_sub := pSub
   GuiControl, +AltSubmit +ReadOnly +gTVX_OnEvent, %pTree%
}

;----------------------------------------------------------------------------------------
; Function: Walk
;         Walk the menu and rise events
;
; Parameters:
;         root      - menu to iterate, can be simple item also
;         label      - event handler
;         event_type   - event argument 1 - Event type
;         event_param   - event argument 2 - Item upon which event is rised
;
;
;               Type                    Param
;
;         +  - Iteration start,         root handle
;         M  - Menu item,               menu handle
;         I  - Item,                  item handle
;         E  - End of menu            menu handle         (pseudo item)
;         -  - Iteration end            root handle         (pseudo item)
;
TVX_Walk(root, label, ByRef event_type, ByRef event_param){
   local n, t, p, c,   pref, bSetEnd, lastParent, rootsParent, tmp

   ; start event for menus
   event_type := "+"
   event_param := root
   GoSub %label%

   if !TV_GetChild(root)
      return

   ; this will be exit condition. If we come to roots parent, stop walking.
   rootsParent := TV_GetParent(root)

   lastParent := root
   c := root
   loop {
      c := TV_GetNext(c, "Full")
      TV_GetText(tmp, c)

      ; Check if this item is submenu. If so, set the lastParent
      if ( TV_GetChild(c) ){
         lastParent := c
         event_type := "M"
      }
      else event_type := "I"         ; not a submenu, it is normal item

      event_param := c
      GoSub %label%


      ; Check if c is the last item in the current submenu
      ; Do so by taking the next item and checking its parent.
      ; If the parent is different then "lastParent" current item is
      ;  at the end of the its submenu.
      n := TV_GetNext(c, "FULL")
      if (n)
      {
         p := TV_GetParent(n)
         if ( p != lastParent){
            t := lastParent
            lastParent := p
         }
         else continue

         ; It is the last child
         Loop {   ; rise "E" (end of menu) event
            event_type := "E"
            event_param := t
            GoSub %label%

            t := TV_GetParent(t)
            if (t = rootsParent) {   ; rise "-" (end of walk) event
               event_type := "-"
               event_param := t
               GoSub %label%
               return
            }
            if (p = t)
               break
         }

      } else
         Loop {
             ;this is the end of the complite menu, so close all open submenus, if any
             if (lastParent = root)    {
               event_type := "-"
               event_param := root
               GoSub %label%
               return
             }

             event_type := "E"
             event_param := lastParent
             GoSub %label%
             lastParent := TV_GetParent(lastParent)
         }
   }
}

;----------------------------------------------------------------------------------------------
; Function:      Move
;            Moves tree view item up or down
;
; Parameters:
;            item      -   Handle of the item to move
;            direction   -   "u" or "d" (Up & Down)
;
; Returns:
;            Handle of the item
;
; Remarks:
;            Item to be moved is copied to the new place then source item is deleted. This
;            creates new handle for the moved item. New handle will be returned by the function.
;
TVX_Move(item, direction){
   local newc, newp, t, p, n, c

   p := TV_GetPrev(item)
   n := TV_GetNext(item)

   if (TVX_HasRoot)
   {
       if   TV_GetNext()=item
         return

       if (direction="u")
       {
          if (p = 0 && TV_GetParent(item)=TV_GetNext())         ;don't let item go above root
          {
            TV_Modify(item)
            TVX_sel:=item
            return
          }
       }
    }


   ; Do so by coping an item bellow calculated item and deleting the old one.
   ; Return handle of new item

   ; newc - calculated child after which "item" should be created.
   ; newp -  ... and its parent

   ; if moving down
   if (direction = "d")
   {
      ; handle end of submenu
      if !n
      {
         newc := TV_GetParent(item)

         ; check the end of the entire list
         if (TVX_HasRoot && newc = TVX_root)
            return

         if TVX_CollapseOnMove
            TV_Modify(newc, "-Expand")

         newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
         ; if submenu, go into it
         t := TV_Get(n, "E")
         if (t = n)
         {
            newp := n
            newc := "First"
         }
         ; not a submenu
         else
         {
            newc := n
            newp := TV_GetParent(n)
         }
      }
   }

   ; if moving up
   if (direction = "u")
   {
      ;going up - handle start of the submenu
      if !p
      {
         t := TV_GetParent(item)
         if TVX_CollapseOnMove
            TV_Modify(t, "-Expand")

         newc := TV_GetPrev(t)

         ; handle start of the menu again
         if !newc
         {
            newp := TV_GetParent(t)
            newc := "First"
         }
         else
            newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
         ; if submenu is expanded, go into it
         t := TV_Get(p, "E")
         if (t = p)
         {
            newc := TV_GetChild(p)
            Loop
            {
               c := TV_GetNext(newc)
               if c = 0
                  break
               newc := c
            }
;            newc := "First"
            newp := t
         }
         else
         {
            t := TV_GetPrev(p)
            ;check the top of the list
            if !t
            {
               newc := "First"
               newp := TV_GetParent(p)
            }
            else
            {
               newc := t
               newp := TV_GetParent(newc)
            }
         }
      }
   }

   newc := TVX_CopyItem(newc, newp, item)
   TV_Delete(item)
   return newc
}


;---------------------------------------------------------------------------------------------
; TVX_Walk event handler wrapped in the function
;
; Function that copies menu item to the destination item.
; Handle of destination item is specified in the global variable TVX_copyDest.
;
TVX_CopyProc(iType, item) {
   local c, txt
   static lastParent

   TV_GetText(txt, item)
   if iType in +
   {
      lastParent := TVX_copyDest
      TV_Modify(TVX_copyDest, "Icon" . %TVX_iconData%%item%, txt)

      if TVX_userData
      {
         %TVX_userData%%TVX_copyDest% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
      if TVX_iconData
      {
         %TVX_iconData%%TVX_copyDest% := %TVX_iconData%%item%
         %TVX_iconData%%item% := ""
      }
   }

   if iType in I,M
   {
      c := TV_Add(txt, lastParent, "Icon" . %TVX_iconData%%item%)
      if iType = M
         lastParent := c

      if TVX_userData
      {
         %TVX_userData%%c% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
      if TVX_iconData
      {
         %TVX_iconData%%c% := %TVX_iconData%%item%
         %TVX_iconData%%item% := ""
      }
   }

   if iType = E
      lastParent := TV_GetParent(lastParent)
}

;----------------------------------------------

_TVX_CopyProc:
   TVX_CopyProc(TVX_itemType, TVX_param)
return

;-----------------------------------------------------------------------------------------------
; Create new item after the child "destc" with parent "destp" and copy the "source" item into it
;
TVX_CopyItem(destc, destp, source){
   global

   ;create the holder and call the copy function
   TVX_copyDest := TV_Add("", destp , destc)
   TVX_Walk(source, "_TVX_CopyProc", TVX_itemType, TVX_param)

   return TVX_copyDest
}

;----------------------------------------------------------------------------------------------
; Used to control moving
;
TVX_OnItemSelect(pItemId){
   global

   if (TVX_bSelfSelect)   {
      TVX_bSelfSelect := false
      return true
   }

   TVX_prevSel := TVX_sel
   TVX_sel := pItemId

   if GetKeyState("Shift") && (TVX_lastKey=38 || TVX_lastKey=40)
    if (pItemId != TVX_root)
    {
        TVX_sel := TVX_Move( TVX_prevSel, TVX_lastKey=40 ? "d" : "u")
      TVX_prevSel := pItemId


      TVX_bSelfSelect := true
      TV_Modify(TVX_sel, "Select")
;      TV_Modify(TVX_sel, "Select Bold")
      return true
    }

    return false
}

;----------------------------------------------------------------------------------------------

TVX_OnKeyPress(pKey){
   local tp, sel, tttt

   TVX_lastKey := pKey

   if (TVX_bSelfPress)   {
      TVX_bSelfPress := false
      return true
   }

   ;delete
   if pKey = 46
   {
      ; use GetSelection instead Editor_sel since if key is pressed and hold
      ; TVX_OnSelect handler may not be called before delete to set the TVX_sel

      sel := TV_GetSelection()
      if (TVX_HasRoot && sel = TVX_root)
         return false

      ; is shift delete is pressed return - some problems with this combination
      if (GetKeyState("Shift"))
         return false

      TV_Delete(sel)
      return true
   }

   ;insert
   if pKey = 45
   {
      tp := TV_GetParent(TVX_sel)
      if (TVX_sel = TVX_root)
         tp := TVX_root

      if TV_Get(TVX_sel, "E") ; if expand, add into it
         tp := TV_Add("New Item", TVX_sel, "Select Bold First")
      else
         tp := TV_Add("New Item", tp, "Select Bold " . TVX_sel)
      %TVX_userData%%tp% = ; clear data
      %TVX_iconData%%tp% = ; clear data
      if GetKeyState("Shift")
      {
         tttt := TV_Add("New Item", tp, "Bold First ")
         %TVX_userData%%tttt% = ; clear data
         %TVX_iconData%%tttt% = ; clear data
         TV_Modify(tp,"Expand", "New Menu")
      }

      if (TVX_EditOnInsert)
      {
         TVX_bSelfPress := TVX_bSelfSelect := true
         TV_Modify(tp, "Select")
         Send, {F2}
      }

      return true
   }

   return false
}

;----------------------------------------------------------------------------------------------
; g soubroutine for Tree View
;
TVX_OnEvent:
   if (A_GuiEvent="S")
      if TVX_OnItemSelect(A_EventInfo)
         return

   if (A_GuiEvent="K")
      if TVX_OnKeyPress(A_EventInfo)
         return

   ;if not the Xtended property send event to the caller
   gosub %TVX_sub%
return
Back to top
View user's profile Send private message
Jimay
Guest





PostPosted: Sun Aug 02, 2009 9:41 pm    Post subject: Reply with quote

Okay, nice code!

I know how to save the treeview. But it is also possible to load the treeview?
I use the following code: (I formatted the code so that it is easily understood)


Code:

Gui, Add, TreeView, MyTreeView

; read each line into a var
FileReadLine, contentLine1, myfile.txt, 1
FileReadLine, contentLine2, myfile.txt, 2
FileReadLine, contentLine3, myfile.txt, 3
etc.

; extracts the ID's P2231416 P2231417 P2231418 from each line
StringLeft, OutputVar1, contentLine1, 8
StringLeft, OutputVar2, contentLine2, 8
StringLeft, OutputVar3, contentLine3, 8
etc.

; extracts the right-part of the ID's --> example TV_Add("First parent")
StringTrimLeft, RightpartLine1, contentLine1, 10
StringTrimLeft, RightpartLine2, contentLine1, 10
StringTrimLeft, RightpartLine3, contentLine1, 10
etc.

; join them together --> results in example P1 := TV_Add("First parent")
%OutputVar1% := %RightpartLine1%
%OutputVar2% := %RightpartLine2%
%OutputVar3% := %RightpartLine3%
etc.



Unfortunately this always gives the following error: The following variable name contains an illegal character: TV_Add("First parent") However, I know the variable is correct. Is this a bug? Or am I doing something wrong? Is there a/another way to accomplish the loading of the treeview from a file?
Back to top
majkinetor



Joined: 24 May 2006
Posts: 4116
Location: Belgrade

PostPosted: Wed Aug 05, 2009 1:57 pm    Post subject: Reply with quote

Module doesn't load or save. There are just test samples I provided too feel the API. Save / Load is entierly your responsibility.
_________________
Back to top
View user's profile Send private message
haichen



Joined: 05 Feb 2007
Posts: 178
Location: Osnabrück, Germany

PostPosted: Thu Aug 06, 2009 9:25 am    Post subject: Reply with quote

@rexx
when moving an item the iconplace is empty.
Is there something wrong or missing in this example?

Code:
ImageListID := IL_Create(10) 
Loop 10 
    IL_Add(ImageListID, "shell32.dll", A_Index)  ;
 
   Gui, Add, TreeView, h400 w300 vMyTree ImageList%ImageListID%
   root  := TV_Add("Root", "" , "Expand")
   loop, 10
      P     := TV_Add(A_Index, root, "Icon" . A_Index) 

   TVX("MyTree", "Handler", "HasRoot CollapseOnMove","",ImageList%ImageListID%)
   
   Gui, Show,  autosize
return

Handler:
return

#include TVX_rexx.ahk   
Back to top
View user's profile Send private message
Guest






PostPosted: Sun Aug 09, 2009 1:20 am    Post subject: Reply with quote

A load module would be nice.
Back to top
Tyrsius



Joined: 09 Jul 2009
Posts: 75

PostPosted: Thu Mar 11, 2010 5:43 am    Post subject: Reply with quote

I found this while looking for ways to create and save checklists. majkinetor you rock!

I spent all day working on a save/load system for this. I also modified the TreeView to use checkboxes (my original intent was a checklist, after all), which the load function uses. I needed to modify majkinetor's save function to get the checkboxes and loading to work they way I wanted them to.

I also modified the library file to change the behavior of Inserted Items and Groups to reflect what I thought was a more intuitive behavior. These changes are unrelated to the save/load system, and are personal taste changes only.

I plan on cleaning this up (though would not turn down help in this area), and creating a file select option. Currently the save/load location are not changable by the user, and use a default location. I want to make a file select option to allow different checklists to be saved and loaded.

I have provided a working version of what I have so far. Save both of the scripts below (make sure they are in the same DIR), and run the example. The checklist starts out blank, but when you hit save everything will be stored in a file (checklist.txt, in the same DIR). When the example is reloaded, it will load the checklist.txt file into the listview, checkboxes and all.

Example
Code:

#NoEnv
#SingleInstance force
   
   UserToDoPath := "Checklist.txt"
   gosub CreateGui
   gosub FillTV


   TVX("MyTree", "Handler", "HasRoot CollapseOnMove ", "aTooltip")      ;!!!!!
   Gui, Show, h410 w430, To-Do Checklist
return


Handler:
   if A_GuiEvent = S
      Tooltip % aTooltip%A_EventInfo%, 0, 0
return

;-------------------------------------------------------------------------------

Save:
   TVX_Walk(root, "SaveHandler", Event, Item)
return

SaveHandler:
   TV_GetText(txt, Item)
   if Event = +
   {
      ChecklistToSave :=
      line := "|-"
   }

   if Event = E
      StringTrimRight, line, line, 1
   
   if Event in I,M
   {
      if TV_Get(Item, "Checked")
         line .= "µ"
      ChecklistToSave .=   line txt "`n"
      StringReplace, line, line, µ,,all
   }

   if Event = M
      line .= "-"
   
   if Event = -
      {
      StringTrimRight, ChecklistToSave, ChecklistToSave, 1
      FileDelete, %UserToDoPath%
      FileAppend, %ChecklistToSave%, %UserToDoPath%
      }
return

;---------------------------------------------------------------------------------

Modify:

   if A_GuiControl=Delete
       ControlSend, SysTreeView321, {DELETE}

   if A_GuiControl=Insert
       ControlSend, SysTreeView321, {INSERT}

   if A_GuiControl=Insert Submenu
   {
       ControlSend, SysTreeView321, {SHIFT down}
       ControlSend, SysTreeView321, {INSERT}
      Sleep 50
       ControlSend, SysTreeView321, {SHIFT up}
   }
return

;---------------------------------------------------------------------------------

FillTV:
   root := TV_Add("To-Do Checklist", "" , "Expand")
   FileRead, LVToCreate, %UserToDoPath%
   StringReplace, LVToCreate, LVToCreate, `r`n, `n, all
   LV_PrevNodeIteration=0
   LV_Parent=root
   RegExParentNode := "(.+?)_(\d+)$"
   RegExNodeDiff := "_\d+$"
   loop, parse, LVToCreate, `n
      {
      
      LineChecked := InStr(A_Loopfield, "µ")
      StringReplace, I_Loopfield, A_Loopfield, µ,,all
      StringReplace, I_Loopfield, I_Loopfield, `|,,all
      StringReplace, I_Loopfield, I_Loopfield, `-,, UseErrorLevel
      LV_NodeLevel := ErrorLevel
      if (LV_NodeLevel=1)
         {
         LV_PrevNodeIteration++
         LV_Nodename := "root_" LV_PrevNodeIteration
         %LV_Nodename% := TV_Add(I_Loopfield, root, "Expand Check" . LineChecked)
         LV_PrevNodeLevel=1
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel>LV_PrevNodeLevel)
         {
         LV_Nodename := LV_PrevNodeName "_1"
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_PrevNodeName%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel=LV_PrevNodeLevel)
         {
         RegExMatch(LV_PrevNodeName, RegExParentNode, RegNodeOut)
         LV_Parent := RegNodeOut1
         RegNodeOut2++
         LV_Nodename := LV_Parent "_" RegNodeOut2
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_Parent%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel<LV_PrevNodeLevel)
         {
         LV_NodeLevelDiff := LV_PrevNodeLevel-LV_NodeLevel
         ;msgbox, NodeDiff: %LV_NodeLevelDiff%
         LV_PrevNodeName := RegExReplace(LV_PrevNodeName, RegExNodeDiff, "", RegExTrash, %LV_NodeLevelDiff%)
         RegExMatch(LV_PrevNodeName, RegExParentNode, RegNodeOut)
         LV_Parent := RegExParentNode1
         RegExParentNode2++
         LV_Nodename := LV_Parent "_" RegExParentNode2
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_PrevNodeName%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename      
         }
      
      }
return

;---------------------------------------------------------------------------------

CreateGui:
   Gui, 15:Destroy
   Gui, 15:Default

   Gui, Add, TreeView, h400 w300 vMyTree Checked
   ;Gui, Add, Button, w100 x+10 , Up
   ;Gui, Add, Button, wp ,Down

   Gui, Add, Button, w100 x+10 gModify, Insert
   Gui, Add, Button, wp gModify, Insert Submenu

   Gui, Add, Button, y+20 wp gModify, Delete
   Gui, Add, Button, y+20 wp gSave, Save to file
return

;---------------------------------------------------------------------------------

Edit:
    Gui, Submit, Nohide
    c := TV_GetSelection()
    aTooltip%c% := MyEdit
return

15GuiEscape:
15GuiClose:
Gui, 15:Destroy
Return

GuiClose:
GuiEscape:
    ExitApp
return

F9::Reload

#include TVX.ahk



Modified Library
Code:

;Title: TreeViewX
;      TreeViewX  extends standard TreeView control to support moving, deleting & inserting.

;----------------------------------------------------------------------------------------
; Function: TVX
;         Initialisation function. Mandatory to call before you show the TreeView.
;
; Parameters:
;         pTree      - AHK name of the TreeView control
;         pSub      - Subroutine for TreeViewX, the same rules as in g.
;         pOptions   - String containing space delimited options for setting up TreeViewX
;         pUserData   - Base name of the array holding user data.
;                    This array is indexed using tree view item handles.
;         
; Options:
;         HasRoot      - TreeViewX has root item - the one containing all other items.
;                    Root item can't be moved, edited or delited, and items can not
;                    be moved or created outside of it. This option need to be set
;                    after root is already added to the menu, as TreeViewX need to
;                    know the root menu handle.
;                   
;      CollapseOnMove   - When moving item out of of its container, this option makes container collapse
;      EditOnInsert   - Automaticaly enters edit mode upon insertion of new item
;
; Example:
;>
;>       TVX("MyTree", "Handler", "HasRoot CollapseOnMove")
;>
TVX( pTree, pSub, pOptions="", pUserData="" ) {
   global

   if InStr(pOptions, "HasRoot")   {
      TVX_HasRoot  := 1
      TVX_root := TV_GetNext()
   }
   
   if InStr(pOptions, "CollapseOnMove")
        TVX_CollapseOnMove := 1

   if InStr(pOptions, "EditOnInsert")
      TVX_EditOnInsert   :=    1

   TVX_userData := pUserData
   TVX_sub := pSub
   GuiControl, +AltSubmit -ReadOnly +gTVX_OnEvent, %pTree%
}

;----------------------------------------------------------------------------------------
; Function: Walk
;         Walk the menu and rise events
;
; Parameters:
;         root      - menu to iterate, can be simple item also
;         label      - event handler
;         event_type   - event argument 1 - Event type
;         event_param   - event argument 2 - Item upon which event is rised
;         
;
;               Type                    Param
;
;         +  - Iteration start,         root handle
;         M  - Menu item,               menu handle
;         I  - Item,                  item handle
;         E  - End of menu            menu handle         (pseudo item)
;         -  - Iteration end            root handle         (pseudo item)
;
TVX_Walk(root, label, ByRef event_type, ByRef event_param){
   local n, t, p, c,   pref, bSetEnd, lastParent, rootsParent, tmp

   ; start event for menus
   event_type := "+"
   event_param := root
   GoSub %label%

   if !TV_GetChild(root)
      return
   
   ; this will be exit condition. If we come to roots parent, stop walking.
   rootsParent := TV_GetParent(root)
   
   lastParent := root
   c := root
   loop {
      c := TV_GetNext(c, "Full")
      TV_GetText(tmp, c)

      ; Check if this item is submenu. If so, set the lastParent
      if ( TV_GetChild(c) ){   
         lastParent := c
         event_type := "M"
      }
      else event_type := "I"         ; not a submenu, it is normal item

      event_param := c
      GoSub %label%      
   

      ; Check if c is the last item in the current submenu
      ; Do so by taking the next item and checking its parent.
      ; If the parent is different then "lastParent" current item is
      ;  at the end of the its submenu.
      n := TV_GetNext(c, "FULL")
      if (n)
      {
         p := TV_GetParent(n)
         if ( p != lastParent){   
            t := lastParent
            lastParent := p
         }
         else continue

         ; It is the last child
         Loop {   ; rise "E" (end of menu) event
            event_type := "E"
            event_param := t
            GoSub %label%

            t := TV_GetParent(t)
            if (t = rootsParent) {   ; rise "-" (end of walk) event
               event_type := "-"
               event_param := t
               GoSub %label%
               return
            }
            if (p = t)
               break
         }

      } else
         Loop {
             ;this is the end of the complite menu, so close all open submenus, if any
             if (lastParent = root)    {
               event_type := "-"
               event_param := root
               GoSub %label%
               return
             }
            
             event_type := "E"
             event_param := lastParent
             GoSub %label%
             lastParent := TV_GetParent(lastParent)
         }   
   }
}

;----------------------------------------------------------------------------------------------
; Function:      Move
;            Moves tree view item up or down
;
; Parameters:
;            item      -   Handle of the item to move
;            direction   -   "u" or "d" (Up & Down)
;
; Returns:
;            Handle of the item
;
; Remarks:
;            Item to be moved is copied to the new place then source item is deleted. This
;            creates new handle for the moved item. New handle will be returned by the function.
;
TVX_Move(item, direction){
   local newc, newp, t, p, n

   p := TV_GetPrev(item)
   n := TV_GetNext(item)

   if (TVX_HasRoot)
   {
       if   TV_GetNext()=item
         return

       if (direction="u")
       {
          if (p = 0 && TV_GetParent(item)=TV_GetNext())         ;don't let item go above root
          {
            TV_Modify(item)
            TVX_sel:=item
            return
          }
       }
    }


   ; Do so by coping an item bellow calculated item and deleting the old one.
   ; Return handle of new item
    
   ; newc - calculated child after which "item" should be created.
   ; newp -  ... and its parent

   ; if moving down
   if (direction = "d")
   {
      ; handle end of submenu
      if !n
      {
         newc := TV_GetParent(item)

         ; check the end of the entire list
         if (TVX_HasRoot && newc = TVX_root)
            return

         if TVX_CollapseOnMove
            TV_Modify(newc, "-Expand")

         newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
            ; if submenu, go into it
         t := TV_Get(n, "E")
         if (t = n)
         {
            newp := n
            newc := "First"
         }
         ; not a submenu
         else
         {
            newc := n
            newp := TV_GetParent(n)
         }   
      }
   }

   ; if moving up
   if (direction = "u")
   {
      ;going up - handle start of the submenu
      if !p
      {
         t := TV_GetParent(item)
         if TVX_CollapseOnMove
            TV_Modify(t, "-Expand")
                                                                
         newc := TV_GetPrev(t)
                                                                                  
         ; handle start of the menu again
         if !newc
         {
            newp := TV_GetParent(t)
            newc := "First"
         }
         else
            newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
            ; if submenu is expanded, go into it
         t := TV_Get(p, "E")
         if (t = p)
         {
            newc := "First"
            newp := t
         }
         else
         {
            t := TV_GetPrev(p)
            ;check the top of the list
            if !t
            {
               newc := "First"
               newp := TV_GetParent(p)            
            }
            else
            {
               newc := t
               newp := TV_GetParent(newc)
            }
         }
      }
   }

   newc := TVX_CopyItem(newc, newp, item)
   TV_Delete(item)
   return newc
}


;---------------------------------------------------------------------------------------------
; TVX_Walk event handler wrapped in the function
;   
; Function that copies menu item to the destination item.
; Handle of destination item is specified in the global variable TVX_copyDest.
;
TVX_CopyProc(iType, item) {
   local c, txt
   static lastParent
   
   TV_GetText(txt, item)
   if iType in +
   {
      lastParent := TVX_copyDest
      TV_Modify(TVX_copyDest, "", txt)
      
      if TVX_userData
      {
         %TVX_userData%%TVX_copyDest% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
   }

   if iType in I,M
   {
      c := TV_Add(txt, lastParent)
      if iType = M
         lastParent := c

      if TVX_userData
      {
         %TVX_userData%%c% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
   }

   if iType = E
      lastParent := TV_GetParent(lastParent)   
}

;----------------------------------------------

_TVX_CopyProc:
   TVX_CopyProc(TVX_itemType, TVX_param)
return

;-----------------------------------------------------------------------------------------------
; Create new item after the child "destc" with parent "destp" and copy the "source" item into it
;
TVX_CopyItem(destc, destp, source){
   global

   ;create the holder and call the copy function
   TVX_copyDest := TV_Add("", destp , destc )
   TVX_Walk(source, "_TVX_CopyProc", TVX_itemType, TVX_param)

   return TVX_copyDest
}

;----------------------------------------------------------------------------------------------
; Used to control moving
;
TVX_OnItemSelect(pItemId){
   global

   if (TVX_bSelfSelect)   {   
      TVX_bSelfSelect := false
      return true
   }   

   TVX_prevSel := TVX_sel
   TVX_sel := pItemId

   if GetKeyState("Shift") && (TVX_lastKey=38 || TVX_lastKey=40)
    if (pItemId != TVX_root)
    {
        TVX_sel := TVX_Move( TVX_prevSel, TVX_lastKey=40 ? "d" : "u")
      TVX_prevSel := pItemId

      
      TVX_bSelfSelect := true
      TV_Modify(TVX_sel, "Select Bold")
      return true
    }

    return false
}

;----------------------------------------------------------------------------------------------

TVX_OnKeyPress(pKey){
   local tp, sel

   TVX_lastKey := pKey

   if (TVX_bSelfPress)   {   
      TVX_bSelfPress := false
      return true
   }   

   ;delete
   if pKey = 46
   {
      ; use GetSelection instead Editor_sel since if key is pressed and hold
      ; TVX_OnSelect handler may not be called before delete to set the TVX_sel

      sel := TV_GetSelection()
      if (TVX_HasRoot && sel = TVX_root)
         return false

      ; is shift delete is pressed return - some problems with this combination
      if (GetKeyState("Shift"))
         return false

      TV_Delete(sel)
      return true
   }

   ;insert
   if pKey = 45
   {
      tp := TV_GetParent(TVX_sel)
      if (TVX_sel = TVX_root)
         tp := TVX_root

      sp := TV_GetSelection()
      if GetKeyState("Shift")
      {
         TV_Add("New", sp, "Expand Bold First ")
      }
      else
      {
         tp := TV_Add("New", tp, "Expand Bold " . TVX_sel)
      }
      if (TVX_EditOnInsert)
      {
         TVX_bSelfPress := TVX_bSelfSelect := true
         TV_Modify(tp, "Select")
         Send, {F2}
      }

      return true
   }

   return false
}

;----------------------------------------------------------------------------------------------
; g soubroutine for Tree View
;
TVX_OnEvent:
   if (A_GuiEvent="S")
      if TVX_OnItemSelect(A_EventInfo)
         return

   if (A_GuiEvent="K")
      if TVX_OnKeyPress(A_EventInfo)
         return
   
   ;if not the Xtended property send event to the caller
   gosub %TVX_sub%
return
Back to top
View user's profile Send private message
Tyrsius



Joined: 09 Jul 2009
Posts: 75

PostPosted: Fri Mar 12, 2010 1:33 am    Post subject: Reply with quote

Ok, I found a few bugs in my save/load system, and I have corrected them. I also modified the GUI in <Example>. It looks a little better (personal taste) and uses an Edit field to allow naming entries before they are entered. Here are the updated files.

Example:
Code:

#NoEnv
#SingleInstance force
   
   UserToDoPath := "Checklist.txt"
   gosub CreateGui
   gosub FillTV


   TVX("MyTree", "Handler", "HasRoot CollapseOnMove ", "aTooltip")      ;!!!!!
   Gui, Show, Autosize center, To-Do Checklist
return


Handler:
   if A_GuiEvent = S
      Tooltip % aTooltip%A_EventInfo%, 0, 0
return

;-------------------------------------------------------------------------------

Save:
   TVX_Walk(root, "SaveHandler", Event, Item)
return

SaveHandler:
   TV_GetText(txt, Item)
   if Event = +
   {
      ChecklistToSave :=
      line := "|-"
   }

   if Event = E
      StringTrimRight, line, line, 1
   
   if Event in I,M
   {
      if TV_Get(Item, "Checked")
         line .= "µ"
      ChecklistToSave .=   line txt "`n"
      StringReplace, line, line, µ,,all
   }

   if Event = M
      line .= "-"
   
   if Event = -
      {
      StringTrimRight, ChecklistToSave, ChecklistToSave, 1
      FileDelete, %UserToDoPath%
      FileAppend, %ChecklistToSave%, %UserToDoPath%
      }
return

;---------------------------------------------------------------------------------

Modify:
   Gui, Submit, NoHide
   nl := NewInsertLabel
   if A_GuiControl=Delete
       ControlSend, SysTreeView321, {DELETE}

   if A_GuiControl=Insert
       ControlSend, SysTreeView321, {INSERT}

   if A_GuiControl=Insert Submenu
   {
       ControlSend, SysTreeView321, {SHIFT down}
       ControlSend, SysTreeView321, {INSERT}
      Sleep 50
       ControlSend, SysTreeView321, {SHIFT up}
   }
return

;---------------------------------------------------------------------------------

FillTV:
   root := TV_Add("To-Do Checklist", "" , "Expand")
   FileRead, LVToCreate, %UserToDoPath%
   StringReplace, LVToCreate, LVToCreate, `r`n, `n,all
   ; P1 := TV_Add( "Project 1", root, "")
   LV_PrevNodeIteration=0
   LV_Parent=root
   RegExParentNode := "(.+?)_(\d+)$"
   RegExNodeDiff := "_\d+$"
   loop, parse, LVToCreate, `n
      {
      LineChecked := InStr(A_Loopfield, "µ")
      StringReplace, I_Loopfield, A_Loopfield, µ,,all
      StringReplace, I_Loopfield, I_Loopfield, `|,,all
      StringReplace, I_Loopfield, I_Loopfield, `-,, UseErrorLevel
      LV_NodeLevel := ErrorLevel
      if (LV_NodeLevel=1)
         {
         LV_PrevNodeIteration++
         LV_Nodename := "root_" LV_PrevNodeIteration
         %LV_Nodename% := TV_Add(I_Loopfield, root, "Expand Check" . LineChecked)
         LV_PrevNodeLevel=1
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel>LV_PrevNodeLevel)
         {
         LV_Nodename := LV_PrevNodeName "_1"
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_PrevNodeName%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel=LV_PrevNodeLevel)
         {
         RegExMatch(LV_PrevNodeName, RegExParentNode, RegNodeOut)
         LV_Parent := RegNodeOut1
         RegNodeOut2++
         LV_Nodename := LV_Parent "_" RegNodeOut2
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_Parent%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename
         }
      else if (LV_NodeLevel<LV_PrevNodeLevel)
         {
         LV_NodeLevelDiff := LV_PrevNodeLevel-LV_NodeLevel
         LV_PrevNodeName := RegExReplace(LV_PrevNodeName, RegExNodeDiff, "", RegExTrash, LV_NodeLevelDiff)
         RegExMatch(LV_PrevNodeName, RegExParentNode, RegNodeOut)
         LV_Parent := RegNodeOut1
         RegNodeOut2++
         LV_Nodename := LV_Parent "_" RegNodeOut2
         %LV_Nodename% := TV_Add(I_Loopfield, %LV_Parent%, "Expand Check" . LineChecked)
         LV_PrevNodeLevel := LV_NodeLevel
         LV_PrevNodeName := LV_Nodename      
         }
      
      }
return

;---------------------------------------------------------------------------------

CreateGui:
   Gui, 15:Destroy
   Gui, 15:Default
   
   Gui, Add, Edit, x5 y10 h20 w120 Section vNewInsertLabel,
   Gui, Add, Button, w80 xp+130 yp gModify, Insert
   Gui, Add, Button, xp+90 wp gModify, Insert SubItem
   
   Gui, Add, TreeView, h400 w300 xs ys+30 vMyTree Checked
   ;Gui, Add, Button, w100 x+10 , Up
   ;Gui, Add, Button, wp ,Down

   Gui, Add, Button, yp+410 xp w80 gModify, Delete
   Gui, Add, Button, yp xp+90 wp gSave, Save to file
return

;---------------------------------------------------------------------------------

Edit:
    Gui, Submit, Nohide
    c := TV_GetSelection()
    aTooltip%c% := MyEdit
return

15GuiEscape:
15GuiClose:
Gui, 15:Destroy
Return

GuiClose:
GuiEscape:
    ExitApp
return

F9::Reload

#include TVX.ahk


Library TVX:
Code:

;Title: TreeViewX
;      TreeViewX  extends standard TreeView control to support moving, deleting & inserting.

;----------------------------------------------------------------------------------------
; Function: TVX
;         Initialisation function. Mandatory to call before you show the TreeView.
;
; Parameters:
;         pTree      - AHK name of the TreeView control
;         pSub      - Subroutine for TreeViewX, the same rules as in g.
;         pOptions   - String containing space delimited options for setting up TreeViewX
;         pUserData   - Base name of the array holding user data.
;                    This array is indexed using tree view item handles.
;         
; Options:
;         HasRoot      - TreeViewX has root item - the one containing all other items.
;                    Root item can't be moved, edited or delited, and items can not
;                    be moved or created outside of it. This option need to be set
;                    after root is already added to the menu, as TreeViewX need to
;                    know the root menu handle.
;                   
;      CollapseOnMove   - When moving item out of of its container, this option makes container collapse
;      EditOnInsert   - Automaticaly enters edit mode upon insertion of new item
;
; Example:
;>
;>       TVX("MyTree", "Handler", "HasRoot CollapseOnMove")
;>
TVX( pTree, pSub, pOptions="", pUserData="" ) {
   global

   if InStr(pOptions, "HasRoot")   {
      TVX_HasRoot  := 1
      TVX_root := TV_GetNext()
   }
   
   if InStr(pOptions, "CollapseOnMove")
        TVX_CollapseOnMove := 1

   if InStr(pOptions, "EditOnInsert")
      TVX_EditOnInsert   :=    1

   TVX_userData := pUserData
   TVX_sub := pSub
   GuiControl, +AltSubmit -ReadOnly +gTVX_OnEvent, %pTree%
}

;----------------------------------------------------------------------------------------
; Function: Walk
;         Walk the menu and rise events
;
; Parameters:
;         root      - menu to iterate, can be simple item also
;         label      - event handler
;         event_type   - event argument 1 - Event type
;         event_param   - event argument 2 - Item upon which event is rised
;         
;
;               Type                    Param
;
;         +  - Iteration start,         root handle
;         M  - Menu item,               menu handle
;         I  - Item,                  item handle
;         E  - End of menu            menu handle         (pseudo item)
;         -  - Iteration end            root handle         (pseudo item)
;
TVX_Walk(root, label, ByRef event_type, ByRef event_param){
   local n, t, p, c,   pref, bSetEnd, lastParent, rootsParent, tmp

   ; start event for menus
   event_type := "+"
   event_param := root
   GoSub %label%

   if !TV_GetChild(root)
      return
   
   ; this will be exit condition. If we come to roots parent, stop walking.
   rootsParent := TV_GetParent(root)
   
   lastParent := root
   c := root
   loop {
      c := TV_GetNext(c, "Full")
      TV_GetText(tmp, c)

      ; Check if this item is submenu. If so, set the lastParent
      if ( TV_GetChild(c) ){   
         lastParent := c
         event_type := "M"
      }
      else event_type := "I"         ; not a submenu, it is normal item

      event_param := c
      GoSub %label%      
   

      ; Check if c is the last item in the current submenu
      ; Do so by taking the next item and checking its parent.
      ; If the parent is different then "lastParent" current item is
      ;  at the end of the its submenu.
      n := TV_GetNext(c, "FULL")
      if (n)
      {
         p := TV_GetParent(n)
         if ( p != lastParent){   
            t := lastParent
            lastParent := p
         }
         else continue

         ; It is the last child
         Loop {   ; rise "E" (end of menu) event
            event_type := "E"
            event_param := t
            GoSub %label%

            t := TV_GetParent(t)
            if (t = rootsParent) {   ; rise "-" (end of walk) event
               event_type := "-"
               event_param := t
               GoSub %label%
               return
            }
            if (p = t)
               break
         }

      } else
         Loop {
             ;this is the end of the complite menu, so close all open submenus, if any
             if (lastParent = root)    {
               event_type := "-"
               event_param := root
               GoSub %label%
               return
             }
            
             event_type := "E"
             event_param := lastParent
             GoSub %label%
             lastParent := TV_GetParent(lastParent)
         }   
   }
}

;----------------------------------------------------------------------------------------------
; Function:      Move
;            Moves tree view item up or down
;
; Parameters:
;            item      -   Handle of the item to move
;            direction   -   "u" or "d" (Up & Down)
;
; Returns:
;            Handle of the item
;
; Remarks:
;            Item to be moved is copied to the new place then source item is deleted. This
;            creates new handle for the moved item. New handle will be returned by the function.
;
TVX_Move(item, direction){
   local newc, newp, t, p, n

   p := TV_GetPrev(item)
   n := TV_GetNext(item)

   if (TVX_HasRoot)
   {
       if   TV_GetNext()=item
         return

       if (direction="u")
       {
          if (p = 0 && TV_GetParent(item)=TV_GetNext())         ;don't let item go above root
          {
            TV_Modify(item)
            TVX_sel:=item
            return
          }
       }
    }


   ; Do so by coping an item bellow calculated item and deleting the old one.
   ; Return handle of new item
    
   ; newc - calculated child after which "item" should be created.
   ; newp -  ... and its parent

   ; if moving down
   if (direction = "d")
   {
      ; handle end of submenu
      if !n
      {
         newc := TV_GetParent(item)

         ; check the end of the entire list
         if (TVX_HasRoot && newc = TVX_root)
            return

         if TVX_CollapseOnMove
            TV_Modify(newc, "-Expand")

         newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
            ; if submenu, go into it
         t := TV_Get(n, "E")
         if (t = n)
         {
            newp := n
            newc := "First"
         }
         ; not a submenu
         else
         {
            newc := n
            newp := TV_GetParent(n)
         }   
      }
   }

   ; if moving up
   if (direction = "u")
   {
      ;going up - handle start of the submenu
      if !p
      {
         t := TV_GetParent(item)
         if TVX_CollapseOnMove
            TV_Modify(t, "-Expand")
                                                                
         newc := TV_GetPrev(t)
                                                                                  
         ; handle start of the menu again
         if !newc
         {
            newp := TV_GetParent(t)
            newc := "First"
         }
         else
            newp := TV_GetParent(newc)
      }
      ; somewhere in the middle
      else
      {
            ; if submenu is expanded, go into it
         t := TV_Get(p, "E")
         if (t = p)
         {
            newc := "First"
            newp := t
         }
         else
         {
            t := TV_GetPrev(p)
            ;check the top of the list
            if !t
            {
               newc := "First"
               newp := TV_GetParent(p)            
            }
            else
            {
               newc := t
               newp := TV_GetParent(newc)
            }
         }
      }
   }

   newc := TVX_CopyItem(newc, newp, item)
   TV_Delete(item)
   return newc
}


;---------------------------------------------------------------------------------------------
; TVX_Walk event handler wrapped in the function
;   
; Function that copies menu item to the destination item.
; Handle of destination item is specified in the global variable TVX_copyDest.
;
TVX_CopyProc(iType, item) {
   local c, txt
   static lastParent
   
   TV_GetText(txt, item)
   if iType in +
   {
      lastParent := TVX_copyDest
      TV_Modify(TVX_copyDest, "", txt)
      
      if TVX_userData
      {
         %TVX_userData%%TVX_copyDest% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
   }

   if iType in I,M
   {
      c := TV_Add(txt, lastParent)
      if iType = M
         lastParent := c

      if TVX_userData
      {
         %TVX_userData%%c% := %TVX_userData%%item%
         %TVX_userData%%item% := ""
      }
   }

   if iType = E
      lastParent := TV_GetParent(lastParent)   
}

;----------------------------------------------

_TVX_CopyProc:
   TVX_CopyProc(TVX_itemType, TVX_param)
return

;-----------------------------------------------------------------------------------------------
; Create new item after the child "destc" with parent "destp" and copy the "source" item into it
;
TVX_CopyItem(destc, destp, source){
   global

   ;create the holder and call the copy function
   TVX_copyDest := TV_Add("", destp , destc )
   TVX_Walk(source, "_TVX_CopyProc", TVX_itemType, TVX_param)

   return TVX_copyDest
}

;----------------------------------------------------------------------------------------------
; Used to control moving
;
TVX_OnItemSelect(pItemId){
   global

   if (TVX_bSelfSelect)   {   
      TVX_bSelfSelect := false
      return true
   }   

   TVX_prevSel := TVX_sel
   TVX_sel := pItemId

   if GetKeyState("Shift") && (TVX_lastKey=38 || TVX_lastKey=40)
    if (pItemId != TVX_root)
    {
        TVX_sel := TVX_Move( TVX_prevSel, TVX_lastKey=40 ? "d" : "u")
      TVX_prevSel := pItemId

      
      TVX_bSelfSelect := true
      TV_Modify(TVX_sel, "Select Bold")
      return true
    }

    return false
}

;----------------------------------------------------------------------------------------------

TVX_OnKeyPress(pKey){
   local tp, sel

   TVX_lastKey := pKey

   if (TVX_bSelfPress)   {   
      TVX_bSelfPress := false
      return true
   }   

   ;delete
   if pKey = 46
   {
      ; use GetSelection instead Editor_sel since if key is pressed and hold
      ; TVX_OnSelect handler may not be called before delete to set the TVX_sel

      sel := TV_GetSelection()
      if (TVX_HasRoot && sel = TVX_root)
         return false

      ; is shift delete is pressed return - some problems with this combination
      if (GetKeyState("Shift"))
         return false

      TV_Delete(sel)
      return true
   }

   ;insert
   if pKey = 45
   {
      if (nl="")
         nl := "New"
      tp := TV_GetParent(TVX_sel)
      if (TVX_sel = TVX_root)
         tp := TVX_root

      sp := TV_GetSelection()
      if GetKeyState("Shift")
      {
         TV_Add(nl, sp, "Expand Bold First ")
      }
      else
      {
         tp := TV_Add(nl, tp, "Expand Bold " . TVX_sel)
      }
      if (TVX_EditOnInsert)
      {
         TVX_bSelfPress := TVX_bSelfSelect := true
         TV_Modify(tp, "Select")
         Send, {F2}
      }

      return true
   }

   return false
}

;----------------------------------------------------------------------------------------------
; g soubroutine for Tree View
;
TVX_OnEvent:
   if (A_GuiEvent="S")
      if TVX_OnItemSelect(A_EventInfo)
         return

   if (A_GuiEvent="K")
      if TVX_OnKeyPress(A_EventInfo)
         return
   
   ;if not the Xtended property send event to the caller
   gosub %TVX_sub%
return
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group