-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSQLiteViewer_Snippets.ahk
178 lines (148 loc) · 7.19 KB
/
SQLiteViewer_Snippets.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
class SQLiteViewer_Snippets {
__New(gui, width, height, snippets) {
static toolbarCommands := {
1: () => this.snippetGui(),
2: () => this.snippetGui(true),
3: () => this.removeSnippet()
}
this.gui := gui
this.snippets := snippets || []
this.toolbarImgList := IL_Create(1, 1, 0)
IL_Add(this.toolbarImgList, "add.ico")
IL_Add(this.toolbarImgList, "modify.ico")
IL_Add(this.toolbarImgList, "delete.ico")
this.tb := new Toolbar(this.gui, "x+0 y+0 h24 w" width " List ToolTips Transparent vSnippetsTB")
this.tb.ExStyle := 0x80 ; double buffer
this.tb.OnEvent("Click", (tb, id) => toolbarCommands[id]())
this.tb.SetImageList(this.toolbarImgList)
this.tb.SetMaxTextRows()
this.tb.Add(, "Add", 0, , , , 1) ; Add Button
this.tb.Add(, "Modify", 1, , , , 2) ; Modify Button
this.tb.Add(, "Delete", 2, , , , 3) ; Delete Button
this.tb.SetButtonSize(24, 24)
this.tb.AutoSize()
prevMarginy := this.gui.MarginY, this.gui.MarginY := 0
this.divider := this.gui.addText("h1 w" width " 0x10")
this.snippetsLV := this.gui.addListView("w" width " h" height - this.tb.ctrl.pos.h " -Hdr -E0x200 LV0x400 LV0x4000 0x100 Multi vSnippetsLV", "Snippets")
this.loadSnippets()
; if DoubleClick is on an item, set that item's "content" as the query edit's value
this.snippetsLV.OnEvent("DoubleClick", (ctrl, i) => i > 0 && ctrl.gui.control["query"].visible && queryTabs.queryEdit.SetText("", this.snippets[i].content, 1))
this.snippetsLVTooltipHwnd := SendMessage(LVM_GETTOOLTIPS := 0x104E, 0, 0, this.snippetsLV.hwnd)
this.snippetsLV.OnNotify(-158, (ctrl, l) => this.handleInfoTip(ctrl, l))
this.snippetsLV.OnNotify(LVN_GETEMPTYMARKUP := -187, (ctrl, l) => this.setEmptyText(ctrl, l))
SetExplorerTheme(this.snippetsLV.hwnd)
HideFocusBorder(this.snippetsLV.hwnd)
; Make the tiles only have a single line (they always display one line for the first column's text) and set the width
; LV_EX_SetTileViewLines(this.snippetsLV.hwnd, 0, width - SysGet(2)) ; Subtract the width of the vertical scrollbar
; By applying LVS_AUTOARRANGE when creating the ListView and then setting the TileViewInfo after, we have to send LVM_UPDATE once to properly space the Tiles
; SendMessage(LVM_UPDATE := 0x102A, 0, 0, , "ahk_id " this.snippetsLV.hwnd)
; Assing the previous MarginY back to the GUI
this.gui.MarginY := prevMarginY
}
setEmptyText(ctrl, l) {
text := "Add a snippet!"
; see LVN_GETEMPTYMARKUP
; put our string in the buffer
StrPut(text, l + A_PtrSize * 3 + 4, StrLen(text))
return true
}
handleInfoTip(ctrl, l) {
Static NMHDRSize := A_PtrSize * 3
Static offText := NMHDRSize + A_PtrSize
Static offItem := NMHDRSize + (A_PtrSize * 2) + 4
; Get the address of the string buffer holding text from first column
textAddr := NumGet(L + offText, "Ptr")
; Get the row we are over and then extract the text from the other columns
row := NumGet(L + offItem, "Int") + 1
snippet := this.snippets[row]
SendMessage(TTM_SETTITLEW := 0x421, 0, &snippet.title, this.snippetsLVTooltipHwnd)
StrPut(StrReplace(snippet.content, "`t", " "), textAddr, "UTF-16")
}
snippetGui(modify := false) {
if (modify) {
if (!this.toModify := this.snippetsLV.GetNext()) {
MsgBox("Please select a snippet to modify first", "No snippet selected", "Icon!")
return
}
}
else {
this.toModify := false
}
if (!this.addSnippetGui) {
this.addSnippetGui := GuiCreate("+ToolWindow", "Add new snippet")
this.addSnippetGui.SetFont("s9", "Tahoma")
this.addSnippetGui.addText("", "Title")
this.addSnippetGui.addEdit("w400 vTitle")
this.addSnippetGui.addText("", "Content")
; addSnippetGui.addEdit("w200 h200 WantTab t8 -Wrap vContent", modify ? this.snippets[toModify].content : "")
this.edit := new Scintilla(this.addSnippetGui, "w400 h400 Border vContent", , 0, 0)
setupSciControl(this.edit)
addButtonW := 100
addButtonX := (this.edit.ctrl.pos.w + (this.addSnippetGui.MarginX * 2) - addButtonW) // 2
addButton := this.addSnippetGui.addButton("x" addButtonX " w" addButtonW " Default vSubmit", "")
addButton.OnEvent("Click", (ctrl) => this.handleClick(ctrl, modify)) ;modify ? this.modifySnippet(toModify, ctrl) : this.addSnippet(ctrl))
}
this.addSnippetGui.control["title"].value := modify ? this.snippets[this.toModify].title : ""
this.edit.SetText("", modify ? this.snippets[this.toModify].content : "", 1)
this.addSnippetGui.control["submit"].text := !modify ? "Add" : "Save"
this.addSnippetGui.Show()
}
loadSnippets() {
for i, snippet in this.snippets {
this.snippetsLV.Add("", snippet["title"])
}
}
handleClick(ctrl, modify) {
if (!data := this.submitAddSnippetGui(ctrl.gui)) {
return
}
if (!modify) {
this.addSnippet(ctrl, data)
}
else {
if (!this.toModify) {
MsgBox("Please select a snippet to modify first", "No snippet selected", "Icon!")
return
}
this.modifySnippet(ctrl, this.toModify, data)
}
}
addSnippet(ctrl, data) {
this.snippets.push({id: this.snippets.length() + 1, title: data["title"], content: data["content"]})
this.snippetsLV.Add("", data["title"])
}
modifySnippet(ctrl, index, data) {
this.snippetsLV.Modify(index, "", data["title"])
this.snippets[index].title := data["title"]
this.snippets[index].content := data["content"]
}
removeSnippet() {
lastSelectedIndex := 0
while (id := this.snippetsLV.GetNext()) {
this.snippets.RemoveAt(id)
this.snippetsLV.Delete(id)
lastSelectedIndex := id
}
if (lastSelectedIndex) {
this.snippetsLV.Modify(lastSelectedIndex, "Select")
}
}
getSnippetContent() {
len := this.edit.GetLength() + 1
VarSetCapacity(content, len)
this.edit.GetText(len, &content)
return StrGet(&content, "UTF-8")
}
submitAddSnippetGui(gui) {
data := gui.submit(false)
data["content"] := this.getSnippetContent()
if (data["title"] = "" || data["content"] = "") {
MsgBox("Title and Content cannot be empty.", "Missing data!")
return false
}
else {
gui.Hide()
return data
}
}
}