PageRenderTime 29ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/CStatusBarControl.ahk

http://github.com/Skiouros/Macro
AutoHotKey | 305 lines | 201 code | 4 blank | 100 comment | 36 complexity | 81570ffa5de046dec8a1824797cfab6b MD5 | raw file
  1. /*
  2. Class: CStatusBarControl
  3. The status bar. Can only be used once per window.
  4. This control extends <CControl>. All basic properties and functions are implemented and documented in this class.
  5. */
  6. Class CStatusBarControl Extends CControl
  7. {
  8. __New(Name, Options, Text, GUINum)
  9. {
  10. base.__New(Name, Options, Text, GUINum)
  11. this.Type := "StatusBar"
  12. this._.Insert("ControlStyles", {SizingGrip : 0x100})
  13. this._.Insert("Events", ["Click", "DoubleClick", "RightClick", "DoubleRightClick"])
  14. }
  15. PostCreate()
  16. {
  17. Base.PostCreate()
  18. this._.Parts := new this.CParts(this.GUINum, this.hwnd)
  19. if(this.Content)
  20. this._.Parts._.Insert(1, new this.CParts.CPart(Text, 1, "", "", "", "", this.GUINum, this.hwnd))
  21. }
  22. /*
  23. Variable: Parts
  24. An array of status bar parts/segments. See <CStatusBarControl.CParts>
  25. Variable: Text
  26. The text of the first part.
  27. */
  28. __Get(Name, Params*)
  29. {
  30. if(Name = "Parts")
  31. Value := this._.Parts
  32. else if(Name = "Text")
  33. Value := this._.Parts[1].Text
  34. Loop % Params.MaxIndex()
  35. if(IsObject(Value)) ;Fix unlucky multi parameter __GET
  36. Value := Value[Params[A_Index]]
  37. if(Value != "")
  38. return Value
  39. }
  40. __Set(Name, Params*)
  41. {
  42. Value := Params[Params.MaxIndex()]
  43. Params.Remove(Params.MaxIndex())
  44. if(Name = "Text") ;Assign text -> assign text of first part
  45. {
  46. this._.Parts[1].Text := Value
  47. return true
  48. }
  49. if(Name = "Parts") ;Assign all parts at once
  50. {
  51. if(Params[1] >= 1 && Params[1] <= this._.Parts.MaxIndex()) ;Set a single part
  52. {
  53. if(IsObject(Value)) ;Set an object
  54. {
  55. Part := new this.CParts.CPart(Value.HasKey("Text") ? Value.Text : "", Params[1], Value.HasKey("Width") ? Value.Width : 50, Value.HasKey("Style") ? Value.Style : "", Value.HasKey("Icon") ? Value.Icon : "", Value.HasKey("IconNumber") ? Value.IconNumber : "", this.GUINum, this.hwnd)
  56. this._.Parts._.Remove(Params[1])
  57. this._.Parts._.Insert(Params[1], Part)
  58. this.RebuildStatusBar()
  59. }
  60. else ;Just set text directly
  61. this._Parts[Params[1]].Text := Value
  62. ;~ PartNumber := Params[Params.MaxIndex()]
  63. ;~ Params.Remove(Params.MaxIndex())
  64. ;~ Part := this._.Parts[PartNumber]
  65. ;~ Part := Value ;ASLDIHSVO)UGBOQWFH)=RFZS
  66. return Value
  67. }
  68. else
  69. {
  70. Data := Value
  71. if(!IsObject(Data))
  72. {
  73. Data := Object()
  74. Loop, Parse, Value, |
  75. Data.Insert({Text : A_LoopField})
  76. }
  77. this._.Insert("Parts", new this.CParts(this.GUINum, this.hwnd))
  78. Loop % Data.MaxIndex()
  79. this._.Parts._.Insert(new this.CParts.CPart(Data[A_Index].HasKey("Text") ? Data[A_Index].Text : "", A_Index, Data[A_Index].HasKey("Width") ? Data[A_Index].Width : 50, Data[A_Index].HasKey("Style") ? Data[A_Index].Style : "", Data[A_Index].HasKey("Icon") ? Data[A_Index].Icon : "", Data[A_Index].HasKey("IconNumber") ? Data[A_Index].IconNumber : "", this.GUINum, this.hwnd))
  80. this.RebuildStatusBar()
  81. }
  82. return Value
  83. }
  84. }
  85. /*
  86. Reconstructs the statusbar from the information stored in this.Parts
  87. */
  88. RebuildStatusBar()
  89. {
  90. Widths := []
  91. for index, Part in this._.Parts
  92. if(index < this._.Parts._.MaxIndex()) ;Insert all but the last one
  93. Widths.Insert(Part.Width ? Part.Width : 50)
  94. Gui, % this.GUINum ":Default"
  95. SB_SetParts()
  96. SB_SetParts(Widths*)
  97. for index, Part in this._.Parts
  98. {
  99. SB_SetText(Part.Text, index, Part.Style)
  100. ;~ if(Part.Icon)
  101. SB_SetIcon(Part.Icon, Part.IconNumber, index)
  102. }
  103. }
  104. /*
  105. Class: CStatusBarControl.CParts
  106. An array of StatusBar parts/segments.
  107. */
  108. Class CParts
  109. {
  110. __New(GUINum, hwnd)
  111. {
  112. this.Insert("_", [])
  113. this.GUINum := GUINum
  114. this.hwnd := hwnd
  115. }
  116. /*
  117. Variable: 1,2,3,4,...
  118. Individual parts can be accessed by their index. Returns an object of type <CStatusBarControl.CParts.CPart>
  119. */
  120. __Get(Name, Params*)
  121. {
  122. if Name is Integer
  123. {
  124. if(Name <= this._.MaxIndex())
  125. {
  126. if(Params.MaxIndex() >= 1)
  127. return this._[Name][Params*]
  128. else
  129. return this._[Name]
  130. }
  131. }
  132. }
  133. /*
  134. Function: MaxIndex
  135. Returns the number of parts.
  136. */
  137. MaxIndex()
  138. {
  139. return this._.MaxIndex()
  140. }
  141. _NewEnum()
  142. {
  143. ;~ global CEnumerator
  144. return new CEnumerator(this._)
  145. }
  146. __Set(Name, Params*)
  147. {
  148. Value := Params[Params.MaxIndex()]
  149. Params.Remove(Params.MaxIndex())
  150. if Name is Integer
  151. {
  152. if(Name <= this._.MaxIndex())
  153. {
  154. if(Params.MaxIndex() >= 1) ;Set a property of CPart
  155. {
  156. Part := this._[Name]
  157. Part[Params*] := Value
  158. }
  159. return Value
  160. }
  161. }
  162. }
  163. /*
  164. Function: Add
  165. Adds a part.
  166. Parameters:
  167. Text - The text of the part.
  168. PartNumber - The position at which the new part will appear.
  169. Width - The width of the new part.
  170. Style - Style options for the new part. See AHK documentation.
  171. Icon - An icon filename for the part.
  172. IconNumber - The index of the icon in a multi-icon file.
  173. */
  174. Add(Text, PartNumber = "", Width = 50, Style = "", Icon = "", IconNumber = "")
  175. {
  176. ;~ global CGUI
  177. if(PartNumber)
  178. this._.Insert(PartNumber, new this.CPart(Text, PartNumber, Width, Style, Icon, IconNumber, this.GUINum, this.hwnd))
  179. else
  180. this._.Insert(new this.CPart(Text, this._.MaxIndex() + 1, Width, Style, Icon, IconNumber, this.GUINum, this.hwnd))
  181. Control := CGUI.GUIList[this.GUINum].Controls[this.hwnd]
  182. Control.RebuildStatusBar()
  183. }
  184. /*
  185. Function: Remove
  186. Removes a part.
  187. Parameters:
  188. PartNumber - The index of the part which should be removed.
  189. */
  190. Remove(PartNumber)
  191. {
  192. ;~ global CGUI
  193. if PartNumber is Integer
  194. {
  195. this._.Remove(PartNumber)
  196. Control := CGUI.GUIList[this.GUINum].Controls[this.hwnd]
  197. Control.RebuildStatusBar()
  198. }
  199. }
  200. /*
  201. Class: CStatusBarControl.CParts.CPart
  202. A single part object.
  203. */
  204. Class CPart
  205. {
  206. __New(Text, PartNumber, Width, Style, Icon, IconNumber, GUINum, hwnd)
  207. {
  208. this.Insert("_", {})
  209. this._.Text := Text
  210. this._.PartNumber := PartNumber
  211. this._.Width := Width
  212. this._.Style := Style
  213. this._.Icon := Icon
  214. this._.IconNumber := IconNumber
  215. this._.GUINum := GUINum
  216. this._.hwnd := hwnd
  217. }
  218. /*
  219. Variable: Text
  220. The text of this part.
  221. Variable: PartNumber
  222. The index of this part.
  223. Variable: Width
  224. The width of this part.
  225. Variable: Style
  226. The style of this part. See AHK docs.
  227. Variable: Icon
  228. The path to the icon file assigned to this part.
  229. Variable: IconNumber
  230. The index of the icon in a multi-icon file.
  231. */
  232. __Get(Name)
  233. {
  234. if(Name != "_" && this._.HasKey(Name))
  235. return this._[Name]
  236. }
  237. __Set(Name, Value)
  238. {
  239. ;~ global CGUI
  240. Control := CGUI.GUIList[this.GUINum].Controls[this.hwnd]
  241. if(Name = "Width")
  242. {
  243. this._[Name] := Value
  244. Control.RebuildStatusBar()
  245. return Value
  246. }
  247. else if(Name = "Text" || Name = "Style")
  248. {
  249. this._[Name] := Value
  250. Gui, % this.GUINum ":Default"
  251. SB_SetText(Name = "Text" ? Value : this._.Text, this._.PartNumber, Name = "Style" ? Value : this._.Style)
  252. return Value
  253. }
  254. else if(Name = "Icon" || Name = "IconNumber")
  255. {
  256. this._[Name] := Value
  257. if(this._.Icon)
  258. {
  259. Gui, % this.GUINum ":Default"
  260. SB_SetIcon(this._.Icon, this._.IconNumber, this._.PartNumber)
  261. }
  262. return Value
  263. }
  264. }
  265. }
  266. }
  267. /*
  268. Event: Introduction
  269. To handle control events you need to create a function with this naming scheme in your window class: ControlName_EventName(params)
  270. The parameters depend on the event and there may not be params at all in some cases.
  271. Additionally it is required to create a label with this naming scheme: GUIName_ControlName
  272. GUIName is the name of the window class that extends CGUI. The label simply needs to call CGUI.HandleEvent().
  273. For better readability labels may be chained since they all execute the same code.
  274. Instead of using ControlName_EventName() you may also call <CControl.RegisterEvent> on a control instance to register a different event function name.
  275. Event: Click(PartIndex)
  276. Invoked when the user clicked on the control.
  277. Event: DoubleClick(PartIndex)
  278. Invoked when the user double-clicked on the control.
  279. Event: RightClick(PartIndex)
  280. Invoked when the user right-clicked on the control.
  281. Event: DoubleRightClick(PartIndex)
  282. Invoked when the user double-right-clicked on the control.
  283. */
  284. HandleEvent(Event)
  285. {
  286. this.CallEvent({Normal : "Click", DoubleClick : "DoubleClick", Right : "RightClick", R : "DoubleRightClick"}[Event.GUIEvent], Event.EventInfo)
  287. }
  288. }