-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_CApplication.ahk
589 lines (521 loc) · 14 KB
/
class_CApplication.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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
; Include IO library
#include <IO>
; Include CIniFile
#include <class_CIniFile>
; Include CInCLocalizeriFile
#include <class_CLocalizer>
; This is just for convenience because the function is included when it is called.
#include <IsEmpty>
;
; Function: CApplication
; Description:
; Creates an instance of the CApplication class without initializing it.
; Syntax: app := new CApplication()
;
class CApplication
{
conf := {}
CompanyName := ""
ProductName := ""
ProductVersion := ""
StartupPath := ""
ExecutablePath := ""
AppDataPath := ""
AppPropertiesPath := ""
AppLanguagesPath := ""
Forms := {}
Menus := {}
OpenForms := {}
DisallowedForms := ""
Hotkeys := {}
DefaultStrings := ""
ini := ""
loc := ""
StartupObject := ""
__New()
{
this.StartupPath := A_ScriptDir
this.ExecutablePath := A_ScriptFullPath
this.AppDataPath := A_ScriptDir "\AppData"
this.AppPropertiesPath := this.AppDataPath "\config.ini"
this.AppLanguagesPath := this.AppDataPath "\Languages"
}
__Delete()
{
for name, form in this.Forms
{
form.Destroy()
}
; TODO: free resources
this.conf := ""
this.ini := ""
this.forms := ""
this.openforms := ""
this.menus := ""
}
;
; Function: Get
; Description:
; Gets an application setting.
; Syntax: value := CApplication.Get(name)
; Parameters:
; name - The setting name.
; read - (Optional) The flag indicating that the setting should be updated from an INI file (false by default).
; Return Value:
; Returns the setting value.
; Remarks:
; If the setting is not set or does not exist, the function tries to read it from the INI file even if read = false.
; If the setting is absent from the INI file, an ErrorLevel is set to 1, and false is returned.
;
; SIDE-EFFECT Overwrites CApplication.conf[name] if the value was read from the INI file, i.e. either if the key was absent prior to the method call, or if the read flag is present and set to true. If you set the setting manually, e.g. CApplication.Set("number", 1) which is equal to CApplication.Set("number", 1, false), and then call Get("number", true), the "number" key value 5 will be overwritten with the value read from an INI file.
Get(name, read = false)
{
if(read || !this.conf.haskey(name))
{
val := this.ini.read("General", name)
if(val = this.ini.defaultValue)
{
ErrorLevel = 1
return val
}
else
{
this.conf[name] := val
}
}
return this.conf[name]
}
;
; Function: Set
; Description:
; Sets an application setting to a specified value and optionally saves it into the INI file.
; Syntax: CApplication.Set(name, value [, write])
; Parameters:
; name - The name of the setting.
; value - The new value for the setting.
; write - (Optional) The flag indicating if the new value should be written to the INI file (false by default).
;
Set(name, value, write = false)
{
if(write)
{
this.StoreSetting(name, value)
}
else
{
this.conf[name] := value
}
}
;
; Function: StoreSetting
; Description:
; Sets an application setting to a specified value and saves it into the INI file.
; Syntax: CApplication.StoreSetting(name, value)
; Parameters:
; name - The name of the setting.
; value - The new value for the setting.
;
StoreSetting(name, value)
{
; OutputDebug In StoreSetting(%name%, %value%)
this.conf[name] := value
this.ini.write("General", name, value)
}
;
; Function: SaveSettings
; Description:
; Saves all settings in the INI file.
; Syntax: CApplication.SaveSettings()
;
SaveSettings()
{
for name, value in this.conf
{
this.StoreSetting(name, value)
}
}
;
; Function: Initialize
; Description:
; Initializes an application object using the settings file or using default settings if the settings file is not available.
; Syntax: CApplication.Initialize([ConfigData])
; Parameters:
; ConfigData - (Optional) An associative array to get settings from (e.g. {ProductName:"MyProductName", ProductVersion:"1.0.0.0"}).
; Remarks:
; All settings that are not properties of this class will be written into the INI file if it does not exist. This may be used to create the default settings file.
; On success ErrorLevel is 0.
; On error, ErrorLevel is set to one of the following values:
; 1 - Could not create AppDataPath upon the first start. A_LastError is set to the result of the operating system's GetLastError() function. (See AutoHotkey documentation for FileCreateDir.)
; 2 - Could not create AppPropertiesPath upon the first start.
; 3 - Could not create AppLanguagesPath upon the first start.
; 4 - Could not store a setting in the INI file.
;
Initialize(ConfigData = "")
{
if(!IsEmpty(ConfigData))
{
if(ConfigData.CompanyName)
this.CompanyName := ConfigData.CompanyName
if(ConfigData.ProductName)
this.ProductName := ConfigData.ProductName
if(ConfigData.ProductVersion)
this.ProductVersion := ConfigData.ProductVersion
for name, value in ConfigData
{
if name not in StartupPath,ExecutablePath,AppDataPath,AppPropertiesPath,AppLanguagesPath,CompanyName,ProductName,ProductVersion,DefaultStrings
{
this.Set(name, value)
}
}
}
if(!CDirectory.Exists(this.AppDataPath))
{
CDirectory.Create(this.AppDataPath)
if ErrorLevel
{
ErrorLevel := 1
return
}
}
this.ini := new CIniFile(this.AppPropertiesPath)
if(!this.ini.Exists())
{
if(!this.ini.CreateIfNotExists())
{
ErrorLevel := 2
return
}
}
if(!CDirectory.Exists(this.AppLanguagesPath))
{
CDirectory.Create(this.AppLanguagesPath)
if ErrorLevel
{
ErrorLevel := 3
return
}
}
; Read all settings or save if they are absent from the INI file.
for name, value in this.conf
{
if(this.Get(name, true) = this.ini.defaultValue)
{
this.StoreSetting(name, value)
if ErrorLevel
{
ErrorLevel := 4
return
}
}
}
this.ReadSettings()
this.DefaultStrings := ConfigData.DefaultStrings
this.loc := new CLocalizer(this.AppLanguagesPath)
il := this.Get("InterfaceLanguage")
; OutputDebug app.Init before load ErrorLevel = %ErrorLevel%
this.loc.LoadStrings(il, ConfigData.DefaultStrings)
; OutputDebug app.Init after load ErrorLevel = %ErrorLevel%
if ErrorLevel = 1
ErrorLevel := 5
else if ErrorLevel = 2
ErrorLevel := 6
}
;
; Function: ReadSettings
; Description:
; Reads settings from an INI file.
; Syntax: CApplication.ReadSettings()
;
ReadSettings()
{
; TODO: this may be a vulnerability without checking values
this.conf := this.ini.ReadSection("General")
}
;
; Function: Run
; Description:
; Starts the application using the supplied object if any.
; Syntax: CApplication.Run([StartupObject])
; Parameters:
; StartupObject - (Optional) an object that has a Start() method which is called by the Run method.
;
Run(StartupObject = "")
{
for i, m in this.menus
{
m.create()
}
for i, hk in this.hotkeys
{
hk.Create()
if(hk.enabled)
hk.enable()
}
for i, g in this.Forms
{
g.create()
}
if(StartupObject)
{
this.StartupObject := StartupObject
this.StartupObject.Start()
}
else if(this.StartupObject)
this.StartupObject.Start()
}
;
; Function: Exit
; Description:
; Closes and destroys all forms by calling __Delete method and exits the application with the supplied code if any.
; Syntax: CApplication.Exit([ExitCode])
; Parameters:
; ExitCode - (Optional) An integer between -2147483648 and 2147483647 (can be an expression in v1.0.48.01+) that is returned to its caller when the script exits. This code is accessible to any program that spawned the script, such as another script (via RunWait) or a batch (.bat) file. If omitted, ExitCode defaults to zero. Zero is traditionally used to indicate success. Note: Windows 95 may be limited in how large ExitCode can be.
;
Exit(ExitCode = 0)
{
this.__Delete()
ExitApp, %ExitCode%
}
GetString(strId)
{
if(this.loc.strings.haskey(strId))
return this.loc.strings[strId]
else
return strId ; Pass recent file names, etc. without localization
}
GetLanguageList()
{
return this.loc.GetLanguageList()
}
Localize(langId)
{
this.StoreSetting("InterfaceLanguage", langId)
this.loc.LoadStrings(langId, this.DefaultStrings)
for i, m in this.menus
{
m.localize()
}
for i, g in this.Forms
{
g.localize()
}
}
; ================================= Hotkeys ====================================
AddHotkeyCmd(hkCmd, mi = "")
{
; OutputDebug In AddHotkeyCmd(%hkCmd%, %mi%)
if(app.Get(hkCmd, true))
{
newHk := new CHotKey(app.Get(hkCmd), hkCmd)
if ErrorLevel
{
; OutputDebug `tCreated hotkey ErrorLevel = %ErrorLevel%
app.Set(hkCmd, "", true)
ErrorLevel := el
}
else
{
newHk.enabled := true
if(mi)
{
mi.hk := newHk
hk.boundControls.insert(mi)
}
}
app.hotkeys[newHk.hk] := newHk
app.hotkeys[newHk.hk].boundControls[mi.name] := mi
}
if ErrorLevel
return false
else
return true
}
AddHotkey(newHk, boundCtrl = "")
{
; OutputDebug % "In AddHotkey(" newHk.hk "[" newHk.label "], " boundCtrl.name ")"
if(!this.hotkeys.hasKey(newHk.hk))
this.hotkeys[newHk.hk] := newHk
if(boundCtrl)
{
; OutputDebug `tSetting hk for boundCtrl
boundCtrl.ReplaceHotkey(newHk)
hk.boundControls[boundCtrl.name] := boundCtrl
}
}
ReplaceHotkey(oldHkString, newHkString)
{
; OutputDebug In ReplaceHotkey(%oldHkString%, %newHkString%)
if(oldHkString = newHkString)
return
oldHk := this.Hotkeys[oldHkString]
newHk := new CHotKey(newHkString, oldHk.label)
; OutputDebug `tCreated new hotkey %newHkString%
if not ErrorLevel
{
this.AddHotkey(newHk)
for name, ctrl in oldHk.boundControls
{
this.AddHotkey(newHk, ctrl)
}
this.Set(newHk.label, newHk.hk, true)
this.RemoveHotkey(oldHkString)
return true
}
else
{
; OutputDebug `tErrorLevel = %ErrorLevel%
return false
}
}
DisableHotkeys()
{
success := true
for strHk, hk in this.hotkeys
{
if(!hk.Disable())
{
success := false
; OutputDebug Could not disable hotkey %strHk%
}
}
return success
}
EnableHotkeys()
{
success := true
for strHk, hk in this.hotkeys
{
if(!hk.Enable())
{
success := false
; OutputDebug Could not enable hotkey %strHk%
}
}
return success
}
RemoveHotkey(strHk)
{
oldHk := this.hotkeys.remove(strHk)
oldHk.Disable()
oldHk := ""
}
; ================================== Forms =====================================
ShowForm(formName, disallowedForms = "")
{
if(this.IsDisallowed(formName))
return
if(!IsEmpty(this.DisallowedForms))
{
dforms := this.DisallowedForms
if formName in %dforms%
return
}
if(disallowedForms)
{
if(disallowedForms = "A" || disallowedForms = "All")
{
result := ""
for fName, form in this.forms
{
if(fname <> formName)
{
this.DisallowedForms .= fname ","
}
}
if(result && SubStr(result, 0) = ",")
StringTrimRight, result, result, 1
}
this.DisallowForms(disallowedForms)
}
this.Forms[formName].DisallowedForms := disallowedForms
this.OpenForms[formName] := this.Forms[formName]
this.Forms[formName].Show()
}
HideForm(formName)
{
this.Forms[formName].Hide()
this.OpenForms.remove(formName)
this.AdjustDisallowedForms()
}
DisallowForms(forms)
{
if(this.DisallowedForms)
this.DisallowedForms .= "," forms
else
this.DisallowedForms := forms
}
IsDisallowed(formName)
{
dforms := this.DisallowedForms
if formName in %dforms%
return true
else
return false
}
AllowForms(forms)
{
result := ""
dforms := this.DisallowedForms
loop, parse, dforms, `,
{
if A_LoopField not in %forms%
{
result .= A_LoopField ","
}
}
if(result && SubStr(result, 0) = ",")
StringTrimRight, result, result, 1
this.DisallowedForms := result
}
AdjustDisallowedForms()
{
this.DisallowedForms := ""
for fName, form in this.OpenForms
{
if(form.DisallowedForms)
{
this.AddDisallowedForms(form.DisallowedForms)
}
}
}
; ================================== Events ====================================
handlers := {}
RaiseEvent(e)
{
; OutputDebug In CApplication.RaiseEvent(%e%)
if(!this.handlers.haskey(e))
return
; OutputDebug `tRaising event %e%
for i, handler in this.handlers[e]
{
; OutputDebug `tCalling handler %handler%
handler.()
}
}
Subscribe(handler, e)
{
if(!this.handlers.haskey(e))
this.handlers[e] := []
handlerName := handler
if(handler.name <> "")
handlerName := handler.name
; OutputDebug In Events.Subscribe(%handlerName%, %e%)
if(!Contains(this.handlers[e], handlerName))
this.handlers[e].insert(handlerName)
}
Unsubscribe(handler, e)
{
if(!this.handlers.haskey(e))
return
handlerName := handler
if(handler.name <> "")
handlerName := handler.name
for i, registeredHandler in this.handlers[e]
{
if(registeredHandler = handlerName)
this.handlers[e].remove(i)
if(count(this.handlers[e]) = 0)
this.handlers.remove(e)
}
}
}