PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/res/ahk/TextBlock.ahk

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