forked from ChrisS85/CGUI_Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNoteExample.ahk
144 lines (139 loc) · 4.53 KB
/
NoteExample.ahk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
gui := new NoteExample()
#include <CGUI>
;json modifies #Escapechar and #Commentflag unfortunately so they're restored here
#include <json>
#Escapechar `
#Commentflag ;
Class NoteExample Extends CGUI
{
;The TreeView control is added as a subclass here because it allows better grouping of control-related functions and properties
Class treeNoteList
{
static Type := "TreeView"
static Options := "x12 y12 w214 h400"
static Text := ""
__New()
{
j := FileExist(A_ScriptDir "\Notes.json") ? json_load(A_ScriptDir "\Notes.json") : {Categories : [], Notes : [], IsCategory : true} ;Load notes and categories
this.FillTreeView(j) ;Add loaded notes/categories to the TreeView
}
;Fill tree view and assign the treeview IDs to the object
FillTreeView(j, Parent=0)
{
if(Parent=0) ;Root
Parent := this.Items ;There is a root item of class CTreeViewControl.CItem. It's ID is 0.
else
Parent := Parent.Add(j.Name)
Parent.IsCategory := true
if(j.HasKey("Categories")) ;Add sub-categories
for index, Category in j.Categories
this.FillTreeView(Category, Parent)
if(j.HasKey("Notes")) ;Add notes
for index2, Note in j.Notes
{
Node := Parent.Add(Note.Name)
Node.NoteText := Note.Text
}
}
;Triggered when the selected item in the TreeView was changed by the user or through code
ItemSelected(GUI, Item)
{
t1 := this.PreviouslySelectedItem.Text
t2 := this.SelectedItem.Text
if(this.PreviouslySelectedItem.ID) ;if a note was selected before, store its text
if(!this.PreviouslySelectedItem.IsCategory) ;if selected item was a note, store its text
this.PreviouslySelectedItem.NoteText := GUI.txtNote.Text
if(Item.IsCategory) ;If new item is a category
{
GUI.txtNote.Enabled := false
GUI.txtNote.Text := ""
}
else
{
GUI.txtNote.Enabled := true
GUI.txtNote.Text := Item.NoteText
}
GUI.txtName.Text := Item.Text ;display the name of the category/note
}
}
btnAddCategory := this.AddControl("Button", "btnAddCategory", "x12 y418 w79 h23", "Add category")
btnDelete := this.AddControl("Button", "btnDelete", "x164 y418 w62 h23", "Delete")
txtName := this.AddControl("Edit", "txtName", "x232 y12 w508 h23", "")
txtNote := this.AddControl("Edit", "txtNote", "x232 y42 w508 h400", "")
btnAddNote := this.AddControl("Button", "btnAddNote", "x97 y418 w61 h23", "Add note")
__New()
{
this.txtNote.Multi := 1
this.Title := "NoteExample"
this.DestroyOnClose := true ;By Setting this the window will destroy itself when the user cloeses it
this.Show()
}
BuildSaveTree(j, Node)
{
if(Node.IsCategory)
{
for Index, Child in Node
{
if(Child.IsCategory)
{
if(!j.HasKey("Categories"))
j.Insert("Categories", [])
jNode := {Name : Child.Text}
j.Categories.Insert(jNode)
this.BuildSaveTree(jNode, Child)
}
else
{
if(!j.HasKey("Notes"))
j.Insert("Notes", [])
jNode := {Name : Child.Text, Text : Child.NoteText}
j.Notes.Insert(jNode)
}
}
}
return j
}
;Called when btnAddCategory was clicked
btnAddCategory_Click()
{
if(this.treeNoteList.SelectedItem.IsCategory)
TreeParent := this.treeNoteList.SelectedItem
else
TreeParent :=this.treeNoteList.SelectedItem.Parent
SelectedTreeItem := TreeParent.Add(Name := "New category") ;Add a new item as child node
SelectedTreeItem.IsCategory := true
SelectedTreeItem.Selected := true ;This will trigger treeNoteList.ItemSelected()
}
;Called when btnAddNote was clicked
btnAddNote_Click()
{
if(this.treeNoteList.SelectedItem.IsCategory)
TreeParent := this.treeNoteList.SelectedItem
else
TreeParent := this.treeNoteList.SelectedItem.Parent
SelectedTreeItem := TreeParent.Add(Name := "New Note") ;Add a new item to the tree and store it
SelectedTreeItem.Selected := true ;This will trigger treeNoteList_ItemSelected()
}
;Called when btnDelete was clicked
btnDelete_Click()
{
SelectedTreeItem := this.treeNoteList.SelectedItem
SelectedTreeItem.Parent.Remove(SelectedTreeItem)
}
;Called when the text of txtName was changed
txtName_TextChanged()
{
this.treeNoteList.SelectedItem.Text := this.txtName.Text
}
;Catch this event to make sure current note gets saved when window gets closed
txtNote_TextChanged()
{
this.treeNoteList.SelectedItem.NoteText := this.txtNote.Text
}
;Called when the window was destroyed (e.g. closed here)
PreClose()
{
json_save(this.BuildSaveTree({Categories : [], Notes : []},this.TreeNoteList.Items), A_ScriptDir "\notes.json") ;Save all notes/categories
ExitApp
}
}