/res/ahk/TextBlock.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 140 lines · 118 code · 19 blank · 3 comment · 13 complexity · cd43f834fd5ac14639a2845085742523 MD5 · raw file

  1. Class Textblock Extends CGUI
  2. {
  3. __New(mainGui, owner = "")
  4. {
  5. this.AddControl("Text", "a", "x6 y11 w417 h13", "Name:")
  6. this.edtName := this.AddControl("Edit", "edtName", "x6 y31 w417 h23" ,"")
  7. this.AddControl("Text", "d", "x6 y67 w417 h16 ", "Enter your text:")
  8. this.edtText := this.AddControl("Edit", "edtText", "x6 y86 w417 h242" ,"")
  9. this.AddControl("GroupBox", "z", "x6 y343 w417 h65","")
  10. this.chkDelay := this.AddControl("Checkbox", "chkDelay", "x18 y343 w181 h13 ", "Use delays between characters")
  11. this.edtDelay := this.AddControl("Edit", "edtDelay", "x40 y372 w65 h23 ", "0")
  12. this.txtDelay := this.AddControl("Text", "txtDelay", "x112 y377 w77 h13 ", "milliseconds")
  13. this.AddControl("Button", "btnClear", "x6 y465 w75 h23 ", "Clear")
  14. this.btnOK := this.AddControl("Button", "btnOK", "x87 y465 w75 h23 ", "OK")
  15. this.AddControl("Button", "btnCancel", "x168 y465 w75 h23 ", "Cancel")
  16. this.AddControl("Button", "l", "x348 y465 w75 h23 ", "Help")
  17. this.edtDelay.Disable()
  18. this.txtDelay.Disable()
  19. this.btnOK.Disable()
  20. this.gui := mainGui
  21. if (owner)
  22. this.Owner := owner, this.OwnerAutoClose := 1, this.MinimizeBox := 0
  23. this.Toolwindow := 1
  24. this.Title := "Text Block Manager"
  25. }
  26. edtName_TextChanged()
  27. {
  28. ; Checking if the edit has text in it.
  29. if (this.edtName.text)
  30. this.btnOK.Enable()
  31. else
  32. this.btnOK.Disable()
  33. }
  34. chkDelay_CheckedChanged()
  35. {
  36. if (this.chkDelay.Checked)
  37. {
  38. this.edtDelay.Enable()
  39. this.txtDelay.Enable()
  40. }
  41. else
  42. {
  43. this.edtDelay.Disable()
  44. this.txtDelay.Disable()
  45. }
  46. }
  47. btnClear_Click()
  48. {
  49. this.edtText.text := ""
  50. }
  51. btnCancel_Click()
  52. {
  53. debug ? debug("Hiding textblock gui")
  54. this.Hide()
  55. }
  56. btnOK_Click()
  57. {
  58. name := this.edtName.text
  59. text := this.edtText.text
  60. ; Checking to see if the delay is checked and has a value.
  61. if (this.edtDelay.Text && this.chkDelay.Checked)
  62. delay := this.edtDelay.Text
  63. else
  64. delay := -1
  65. firstChar := SubStr(name, 1, 1)
  66. if (InStr(name, A_Space))
  67. {
  68. MsgBox, 48, , No spaces allowed in name.
  69. return
  70. }
  71. else if firstChar is not Alpha
  72. {
  73. MsgBox, 48, , Macro names must start with a letter.
  74. return
  75. }
  76. else if (!name)
  77. return
  78. StringReplace, text, text, `n, ``n, All ; Change newlines to `n for storage in xml
  79. xml.AddText(name, text, delay)
  80. xml.Save(A_ScriptDir . "\res\Profiles\" . xml.Get("name") . ".xml") ; Save xml file.
  81. this.name := name
  82. this.edtName.text := "", this.edtText.text := "", this.editDelay := -1
  83. this.Hide()
  84. selectedRow := this.gui.keys.FocusedIndex
  85. key := this.gui.keys.Items[selectedRow][1]
  86. options := xml.GetAttribute(key)
  87. type := this.gui.keys.Items[selectedRow][2]
  88. repeat := this.gui.keys.Items[selectedRow][5]
  89. debug ? debug("Created textblock: " . name . " for key: " . key)
  90. xml.AddKey(key, "Textblock", this.name, options, repeat)
  91. StringReplace, options, options, % key
  92. this.gui.keys.Items.Modify(selectedRow, "", key, "Textblock", this.name, options, repeat)
  93. xml.Save(A_ScriptDir . "\res\Profiles\" . xml.Get("name") . ".xml") ; Save xml file.
  94. Hotkeys()
  95. }
  96. Load(textblockName)
  97. {
  98. this.Done := 0
  99. if (!xml.Exist("textblock", textblockName))
  100. {
  101. debug ? debug("Showing textblock gui")
  102. this.Show()
  103. return
  104. }
  105. text := xml.Get("textblock", textblockName, "value")
  106. Transform, text, Deref, % text ; turn ``n into actual new lines
  107. this.edtText.text := text
  108. this.edtName.Text := textblockName
  109. delay := xml.Get("textblock", textblockName, "delay")
  110. if (delay > 0)
  111. {
  112. ; Enable Delay edit box
  113. this.edtDelay.Enable()
  114. this.txtDelay.Enable()
  115. this.chkDelay.Checked := true
  116. this.edtDelay.Text := delay
  117. }
  118. debug ? debug("Loaded textblock: " . textblockName)
  119. this.Show()
  120. }
  121. }