forked from Ixiko/AHK-libs-and-classes-collection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniSettingsEditor.ahk
426 lines (383 loc) · 15.7 KB
/
IniSettingsEditor.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
IniSettingsEditor(ProgName,IniFile,OwnedBy = 0,DisableGui = 0, HelpText = 0) {
static pos
;Find a GUI ID that does not exist yet
Loop, 99 {
Gui %A_Index%:+LastFoundExist
If not WinExist() {
SettingsGuiID = %A_Index%
break
}Else If (A_Index = 99){
MsgBox, 4112, Error in IniSettingsEditor function, Can't open settings dialog,`nsince no GUI ID was available.
Return 0
}
}
Gui, %SettingsGuiID%:Default
;apply options to settings GUI
If OwnedBy {
Gui, +ToolWindow +Owner%OwnedBy%
If DisableGui
Gui, %OwnedBy%:+Disabled
}Else
DisableGui := False
Gui, +Resize +LabelGuiIniSettingsEditor
;create GUI (order of the two edit controls is crucial, since ClassNN is order dependent)
Gui, Add, Statusbar
Gui, Add, TreeView, x16 y75 w180 h242 0x400
Gui, Add, Edit, x215 y114 w340 h20, ;ahk_class Edit1
Gui, Add, Edit, x215 y174 w340 h100 ReadOnly, ;ahk_class Edit2
Gui, Add, Button, x250 y335 w70 h30 gExitSettings , E&xit ;ahk_class Button1
Gui, Add, Button, x505 y88 gBtnBrowseKeyValue Hidden, B&rowse ;ahk_class Button2
Gui, Add, Button, x215 y274 gBtnDefaultValue, &Restore ;ahk_class Button3
Gui, Add, DateTime, x215 y114 w340 h20 Hidden, ;ahk_class SysDateTimePick321
Gui, Add, Hotkey, x215 y114 w340 h20 Hidden, ;ahk_class msctls_hotkey321
Gui, Add, DropDownList, x215 y114 w340 h120 Hidden, ;ahk_class ComboBox1
Gui, Add, CheckBox, x215 y114 w340 h20 Hidden, ;ahk_class Button4
Gui, Add, GroupBox, x4 y63 w560 h263 , ;ahk_class Button5
Gui, Font, Bold
Gui, Add, Text, x215 y93, Value ;ahk_class Static1
Gui, Add, Text, x215 y154, Description ;ahk_class Static2
HelpTip = ( All changes are Auto-Saved )
IfNotEqual, HelpText, 0
{
HelpTip = ( All changes are Auto-Saved - Press F1 for Help )
Hotkey, IfWinActive, %ProgName% Settings
Hotkey, F1, ShowHelp
}
Gui, Add, Text, x45 y48 w480 h20 +Center, %HelpTip%
Gui, Font, S16 CDefault Bold, Verdana
Gui, Add, Text, x45 y13 w480 h35 +Center, Settings for %ProgName%
;read data from ini file, build tree and store values and description in arrays
Loop, Read, %IniFile%
{
CurrLine = %A_LoopReadLine%
CurrLineLength := StrLen(CurrLine)
;blank line
If CurrLine is space
Continue
;description (comment) line
If ( InStr(CurrLine,";") = 1 ){
StringLeft, chk2, CurrLine, % CurrLength + 2
StringTrimLeft, Des, CurrLine, % CurrLength + 2
;description of key
If ( %CurrID%Sec = False AND ";" CurrKey A_Space = chk2){
;handle key types
If ( InStr(Des,"Type: ") = 1 ){
StringTrimLeft, Typ, Des, 6
Typ = %Typ%
Des = `n%Des% ;add an extra line to the type definition in the description control
;handle format or list
If (InStr(Typ,"DropDown ") = 1) {
StringTrimLeft, Format, Typ, 9
%CurrID%For = %Format%
Typ = DropDown
Des =
}Else If (InStr(Typ,"DateTime") = 1) {
StringTrimLeft, Format, Typ, 9
If Format is space
Format = dddd MMMM d, yyyy HH:mm:ss tt
%CurrID%For = %Format%
Typ = DateTime
Des =
}
;set type
%CurrID%Typ := Typ
;remember default value
}Else If ( InStr(Des,"Default: ") = 1 ){
StringTrimLeft, Def, Des, 9
%CurrID%Def = %Def%
;remember custom options
}Else If ( InStr(Des,"Options: ") = 1 ){
StringTrimLeft, Opt, Des, 9
%CurrID%Opt = %Opt%
Des =
;remove hidden keys from tree
}Else If ( InStr(Des,"Hidden:") = 1 ){
TV_Delete(CurrID)
Des =
CurrID =
;handle checkbox name
}Else If ( InStr(Des,"CheckboxName: ") = 1 ){
StringTrimLeft, ChkN, Des, 14
%CurrID%ChkN = %ChkN%
Des =
}
%CurrID%Des := %CurrID%Des "`n" Des
;description of section
} Else If ( %CurrID%Sec = True AND ";" CurrSec A_Space = chk2 ){
;remove hidden section from tree
If ( InStr(Des,"Hidden:") = 1 ){
TV_Delete(CurrID)
Des =
CurrSecID =
}
;set description
%CurrID%Des := %CurrID%Des "`n" Des
}
;remove leading and trailing whitespaces and new lines
If ( InStr(%CurrID%Des, "`n") = 1 )
StringTrimLeft, %CurrID%Des, %CurrID%Des, 1
Continue
}
;section line
If ( InStr(CurrLine, "[") = 1 And InStr(CurrLine, "]", "", 0) = CurrLineLength) {
;extract section name
StringTrimLeft, CurrSec, CurrLine, 1
StringTrimRight, CurrSec, CurrSec, 1
CurrSec = %CurrSec%
CurrLength := StrLen(CurrSec) ;to easily trim name off of following comment lines
;add to tree
CurrSecID := TV_Add(CurrSec)
CurrID = %CurrSecID%
%CurrID%Sec := True
CurrKey =
Continue
}
;key line
Pos := InStr(CurrLine,"=")
If ( Pos AND CurrSecID ){
;extract key name and its value
StringLeft, CurrKey, CurrLine, % Pos - 1
StringTrimLeft, CurrVal, CurrLine, %Pos%
CurrKey = %CurrKey% ;remove whitespaces
CurrVal = %CurrVal%
CurrLength := StrLen(CurrKey)
;add to tree and store value
CurrID := TV_Add(CurrKey,CurrSecID)
%CurrID%Val := CurrVal
%CurrID%Sec := False
;store initial value as default for restore function
;will be overwritten if default is specified later on comment line
%CurrID%Def := CurrVal
}
}
;select first key of first section and expand section
TV_Modify(TV_GetChild(TV_GetNext()), "Select")
;show Gui and get UniqueID
Gui, Show, w570 h400, %ProgName% Settings
Gui, +LastFound
GuiID := WinExist()
;check for changes in GUI
Loop {
;get current tree selection
CurrID := TV_GetSelection()
If SetDefault {
%CurrID%Val := %CurrID%Def
LastID = 0
SetDefault := False
ValChanged := True
}
MouseGetPos,,, AWinID, ACtrl
If ( AWinID = GuiID){
If ( ACtrl = "Button3")
SB_SetText("Restores Value to default (if specified), else restores it to initial value before change")
} Else
SB_SetText("")
;change GUI content if tree selection changed
If (CurrID <> LastID) {
;remove custom options from last control
Loop, Parse, InvertedOptions, %A_Space%
GuiControl, %A_Loopfield%, %ControlUsed%
;hide/show browse button depending on key type
Typ := %CurrID%Typ
If Typ in File,Folder
GuiControl, Show , Button2,
Else
GuiControl, Hide , Button2,
;set the needed value control depending on key type
If (Typ = "DateTime")
ControlUsed = SysDateTimePick321
Else If ( Typ = "Hotkey" )
ControlUsed = msctls_hotkey321
Else If ( Typ = "DropDown")
ControlUsed = ComboBox1
Else If ( Typ = "CheckBox")
ControlUsed = Button4
Else ;e.g. Text,File,Folder,Float,Integer or No Tyo (e.g. Section)
ControlUsed = Edit1
;hide/show the value controls
Controls = SysDateTimePick321,msctls_hotkey321,ComboBox1,Button4,Edit1
Loop, Parse, Controls, `,
If ( ControlUsed = A_LoopField )
GuiControl, Show , %A_LoopField%,
Else
GuiControl, Hide , %A_LoopField%,
If ( ControlUsed = "Button4" )
GuiControl, , Button4, % %CurrID%ChkN
;get current options
CurrOpt := %CurrID%Opt
;apply current custom options to current control and memorize them inverted
InvertedOptions =
Loop, Parse, CurrOpt, %A_Space%
{
;get actual option name
StringLeft, chk, A_LoopField, 1
StringTrimLeft, chk2, A_LoopField, 1
If chk In +,-
{
GuiControl, %A_LoopField%, %ControlUsed%
If (chk = "+")
InvertedOptions = %InvertedOptions% -%chk2%
Else
InvertedOptions = %InvertedOptions% +%chk2%
}Else {
GuiControl, +%A_LoopField%, %ControlUsed%
InvertedOptions = %InvertedOptions% -%A_LoopField%
}
}
If %CurrID%Sec { ;section got selected
CurrVal =
GuiControl, , Edit1,
GuiControl, Disable , Edit1,
GuiControl, Disable , Button3,
}Else { ;new key got selected
CurrVal := %CurrID%Val ;get current value
GuiControl, , Edit1, %CurrVal% ;put current value in all value controls
GuiControl, Text, SysDateTimePick321, % %CurrID%For
GuiControl, , SysDateTimePick321, %CurrVal%
GuiControl, , msctls_hotkey321, %CurrVal%
GuiControl, , ComboBox1, % "|" %CurrID%For
GuiControl, ChooseString, ComboBox1, %CurrVal%
GuiControl, , Button4 , %CurrVal%
GuiControl, Enable , Edit1,
GuiControl, Enable , Button3,
}
GuiControl, , Edit2, % %CurrID%Des
}
LastID = %CurrID% ;remember last selection
;sleep to reduce CPU load
Sleep, 100
;exit endless loop, when settings GUI closes
If not WinExist("ahk_id" GuiID)
Break
;if key is selected, get value
If (%CurrID%Sec = False){
GuiControlGet, NewVal, , %ControlUsed%
;save key value when it has been changed
If ( NewVal <> CurrVal OR ValChanged ) {
ValChanged := False
;consistency check if type is integer or float
If (Typ = "Integer")
If NewVal is not space
If NewVal is not Integer
{
GuiControl, , Edit1, %CurrVal%
Continue
}
If (Typ = "Float")
If NewVal is not space
If NewVal is not Integer
If (NewVal <> ".")
If NewVal is not Float
{
GuiControl, , Edit1, %CurrVal%
Continue
}
;set new value and save it to INI
%CurrID%Val := NewVal
CurrVal = %NewVal%
PrntID := TV_GetParent(CurrID)
TV_GetText(SelSec, PrntID)
TV_GetText(SelKey, CurrID)
If (SelSec AND SelKey)
IniWrite, %NewVal%, %IniFile%, %SelSec%, %SelKey%
}
}
}
;Exit button got pressed
ExitSettings:
;re-enable calling GUI
If DisableGui {
Gui, %OwnedBy%:-Disabled
Gui, %OwnedBy%:,Show
}
Gui, Destroy
IfNotEqual, HelpText, 0
Hotkey, F1, Off
;exit function
Return 1
;browse button got pressed
BtnBrowseKeyValue:
;get current value
GuiControlGet, StartVal, , Edit1
Gui, +OwnDialogs
;Select file or folder depending on key type
If (Typ = "File"){
;get StartFolder
IfExist %A_ScriptDir%\%StartVal%
StartFolder = %A_ScriptDir%
Else IfExist %StartVal%
SplitPath, StartVal, , StartFolder
Else
StartFolder =
;select file
FileSelectFile, Selected,, %StartFolder%, Select file for %SelSec% - %SelKey%, Any file (*.*)
}Else If (Typ = "Folder"){
;get StartFolder
IfExist %A_ScriptDir%\%StartVal%
StartFolder = %A_ScriptDir%\%StartVal%
Else IfExist %StartVal%
StartFolder = %StartVal%
Else
StartFolder =
;select folder
FileSelectFolder, Selected, *%StartFolder% , 3, Select folder for %SelSec% - %SelKey%
;remove last backslash "\" if any
StringRight, LastChar, Selected, 1
If LastChar = \
StringTrimRight, Selected, Selected, 1
}
;If file or folder got selected, remove A_ScriptDir (since it's redundant) and set it into GUI
If Selected {
StringReplace, Selected, Selected, %A_ScriptDir%\
GuiControl, , Edit1, %Selected%
%CurrID%Val := Selected
}
Return ;end of browse button subroutine
;default button got pressed
BtnDefaultValue:
SetDefault := True
Return ;end of default button subroutine
;gui got resized, adjust control sizes
GuiIniSettingsEditorSize:
GuiIniSettingsEditorAnchor("SysTreeView321" , "wh")
GuiIniSettingsEditorAnchor("Edit1" , "x")
GuiIniSettingsEditorAnchor("Edit2" , "xh")
GuiIniSettingsEditorAnchor("Button1" , "xy",true)
GuiIniSettingsEditorAnchor("Button2" , "x",true)
GuiIniSettingsEditorAnchor("Button3" , "xy",true)
GuiIniSettingsEditorAnchor("Button4" , "x",true)
GuiIniSettingsEditorAnchor("Button5" , "wh",true)
GuiIniSettingsEditorAnchor("SysDateTimePick321" , "x")
GuiIniSettingsEditorAnchor("msctls_Hotkey321" , "x")
GuiIniSettingsEditorAnchor("ComboBox1" , "x")
GuiIniSettingsEditorAnchor("Static1" , "x")
GuiIniSettingsEditorAnchor("Static2" , "x")
GuiIniSettingsEditorAnchor("Static3" , "x")
GuiIniSettingsEditorAnchor("Static4" , "x")
Return
ShowHelp:
MsgBox, 64, %ProgName% Settings Help, %HelpText%
Return
} ;end of function
GuiIniSettingsEditorAnchor(ctrl, a, draw = false) { ; v3.2 by Titan (shortened)
static pos
sig = `n%ctrl%=
If !InStr(pos, sig) {
GuiControlGet, p, Pos, %ctrl%
pos := pos . sig . px - A_GuiWidth . "/" . pw - A_GuiWidth . "/"
. py - A_GuiHeight . "/" . ph - A_GuiHeight . "/"
}
StringTrimLeft, p, pos, InStr(pos, sig) - 1 + StrLen(sig)
StringSplit, p, p, /
c = xwyh
Loop, Parse, c
If InStr(a, A_LoopField) {
If A_Index < 3
e := p%A_Index% + A_GuiWidth
Else e := p%A_Index% + A_GuiHeight
m = %m%%A_LoopField%%e%
}
If draw
d = Draw
GuiControl, Move%d%, %ctrl%, %m%
}