; ; AutoHotkey Version: 1.x ; Language: English ; Platform: Win9x/NT ; Author: Jack Dunning ; ; Script Function: ; This version of RecipeTree reads the CSV file RecipeTree.csv and constructs the GUI ; window and saves again. ; Includes routines for moving branches up and down #NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases. ; #Warn ; Enable warnings to assist with detecting common errors. SendMode Input ; Recommended for new scripts due to its superior speed and reliability. SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory. OnExit, GuiClose RecipeChange := 0 Menu, RecipeMenu, Add,Add New Recipe, AddRecipe Menu, RecipeMenu, Add,Add Ingredient, AddIngredient Menu, RecipeMenu, Add,Delete Recipe, DeleteBranch Menu, IngredientMenu, Add,Move Up, ^Up Menu, IngredientMenu, Add,Move Down, ^Down Menu, IngredientMenu, Add,Add Ingredient, AddIngredient Menu, IngredientMenu, Add,Delete Ingredient, DeleteBranch Hotkey, ^!r, ShowRecipe ; Auto-execute section Menu, Tray, Add, Show Recipe Tree, ShowRecipe ; Auto-execute section Gui, default Gui +AlwaysOnTop +Resize Gui, Add, TreeView, vMyTreeView gMyTreeView h400 AltSubmit -ReadOnly Gui, Add, Edit, ys vMyEdit gMyEdit w300 h400 Gui, Add, Button, xm vSave gSave, Save Loop, Read, RecipeTree.csv { Loop, Parse, A_LoopReadLine , CSV { RowData%A_Index% := A_LoopField } StringReplace, RowData4, RowData4,|,`n, All If RowData2 = 0 { RecipeID := TV_Add(RowData3,0,"Sort") %RowData1% := RecipeID %RecipeID% := RowData1 RowText := RecipeID . "text" %RowText% := RowData4 } Else { Ingredient := RowData1 . RowData2 IngredientID := TV_Add(RowData3, %Rowdata1%) %IngredientID% := Ingredient RowText := IngredientID . "text" %RowText% := RowData4 } } ; GuiControl, +readonly, MyEdit Gui, Show,, RecipeTree ; Show the window and its TreeView. return MyEdit: Gui, Submit, NoHide Text := TV_GetSelection() . "text" %Text% := MyEdit GuiControl, +backgroundFFFFCC, MyTreeView RecipeChange := 1 Return MyTreeView: TV_GetText(TreeText, TV_GetSelection()) Unit := TV_GetSelection() Text := TV_GetSelection() . "text" GuiControl, , MyEdit, % %Text% If A_GuiEvent = E { GuiControl, +backgroundFFFFCC, MyTreeView RecipeChange := 1 } Return Save: UpdateFile() GuiControl, +backgroundWhite, MyTreeView RecipeChange := 0 TV_Modify(0, "Sort") Return GuiContextMenu: ; Launched in response to a right-click or press of the Apps key. If A_GuiControl <> MyTreeView ; Display the menu only for clicks inside the ListView. return ; Show the menu at the provided coordinates, A_GuiX and A_GuiY. These should be used ; because they provide correct coordinates even if the user pressed the Apps key ItemId := A_EventInfo TV_Modify(ItemID, "Select") If TV_GetParent(A_EventInfo) = 0 Menu, RecipeMenu, Show , %A_GuiX%, %A_GuiY% Else Menu, IngredientMenu, Show , %A_GuiX%, %A_GuiY% return GuiSize: ; Expand or shrink the TreeView in response to the user's resizing of the window. if A_EventInfo = 1 ; The window has been minimized. No action needed. return ; Otherwise, the window has been resized or maximized. Resize the ListView to match. GuiControl, Move, MyTreeView, % " H" . (A_GuiHeight - 40) ;"W" . (A_GuiWidth - 20) . GuiControl, Move, MyEdit, % "W" . (A_GuiWidth - 275) . " H" . (A_GuiHeight - 40) GuiControl, Move, Button1, % "y" . (A_GuiHeight - 30) Return GuiClose: ; Exit the script when the user closes the TreeView's GUI window. If RecipeChange = 0 ExitApp RecipeChange := 0 MsgBox, 4100, Data Changed! Save?, Data Changed! Save? Click Yes or No? IfMsgBox No ;Don't delete ExitApp UpdateFile() ExitApp Return ShowRecipe: Gui, Show,, Recipe Tree Return AddRecipe: RecipeID := TV_Add("New Recipe",0,"Sort") %RecipeID%text := "Enter Recipe Description here." TV_Modify(RecipeID, "Select") Return AddIngredient: If TV_GetParent(ItemID) = 0 NewAdd :=TV_Add("New Ingredient", ItemID) Else NewAdd :=TV_Add("New Ingredient", TV_GetParent(ItemID), ItemID) %NewAdd%text := "Enter instructions here." TV_Modify(NewAdd, "Select") Return DeleteBranch: MsgBox, 4100, Delete Item?, Delete Item? Click Yes or No? IfMsgBox No ;Don't delete Return ;If TV_GetChild(ItemID) != 0 ; Return TV_Delete(ItemID) Return UpdateFile() { FileMove, RecipeTree.csv, RecipeTree.bak, 1 R = 1 ItemID = 0 ; Causes the loop's first iteration to start the search at the top of the tree. Loop { ItemID := TV_GetNext(ItemID, "Full") if not ItemID ; No more items in tree. break TV_GetText(ItemText, ItemID) If TV_GetParent(ItemID) = 0 { ParentCode := "R" . R ChildCode := 0 R += 1 C = 1 } Else { ChildCode := "C" . C C += 1 } TextValue := %ItemID%text StringReplace, TextValue, TextValue,", "", All StringReplace, TextValue, TextValue,`n,|, All FileAppend, "%ParentCode%"`,"%ChildCode%"`,"%ItemText%"`,"%TextValue%" `n, RecipeTree.csv } ; Reload } #IfWinActive, RecipeTree ^Up:: ThisOne := TV_GetSelection() If TV_GetPrev(ThisOne) != 0 { TV_GetText(MoveName,ThisOne) EditText := % %Thisone%text If TV_GetPrev(TV_GetPrev(ThisOne)) != 0 { NewAdd :=TV_Add(MoveName, TV_GetParent(ThisOne), TV_GetPrev(TV_GetPrev(ThisOne))) } Else { NewAdd :=TV_Add(MoveName, TV_GetParent(ThisOne), "First") } %NewAdd%text := EditText TV_Delete(ThisOne) TV_Modify(NewAdd, "Select") } Return ^Down:: ThisOne := TV_GetSelection() If TV_GetNext(ThisOne) != 0 { TV_GetText(MoveName,ThisOne) EditText := % %Thisone%text NewAdd := TV_Add(MoveName, TV_GetParent(ThisOne), TV_GetNext(ThisOne)) %NewAdd%text := EditText TV_Delete(ThisOne) TV_Modify(NewAdd, "Select") } Return