/lib/CTabControl.ahk

http://github.com/Skiouros/Macro · AutoHotKey · 330 lines · 211 code · 6 blank · 113 comment · 49 complexity · dfdb43f8b5b088d860f37f11df07b788 MD5 · raw file

  1. /*
  2. Class: CTabControl
  3. A tab container control. The controls added to a tab panel can be accessed through this object.
  4. This control extends <CControl>. All basic properties and functions are implemented and documented in this class.
  5. */
  6. Class CTabControl Extends CControl
  7. {
  8. __New(Name, Options, Text, GUINum)
  9. {
  10. base.__New(Name, Options, Text, GUINum)
  11. this.Type := "Tab2" ;Use Tab2 initially but revert back to Tab in PostCreate to mask the real AHK type of the control.
  12. this._.Insert("ControlStyles", {Bottom : 0x2, HotTrack : 0x40, Buttons : 0x100, MultiLine : 0x200})
  13. this._.Insert("Events", ["Click", "DoubleClick", "RightClick", "DoubleRightClick"])
  14. this._.Insert("Messages", {0x004E : "Notify"})
  15. CGUI.GUIList[this.GUINum].TabCount := CGUI.GUIList[this.GUINum].TabCount ? CGUI.GUIList[this.GUINum].TabCount + 1 : 1 ;Increase count of tabs in this GUI, required for GUI, Tab command
  16. this._.Insert("TabIndex", CGUI.GUIList[this.GUINum].TabCount)
  17. }
  18. PostCreate()
  19. {
  20. Base.PostCreate()
  21. ;Make sure future controls don't get added to this tab
  22. Gui, % this.GUINum ":Tab"
  23. this.Type := "Tab" ;Fix tab type value
  24. this._.Insert("ImageListManager", new this.CImageListManager(this.GUINum, this.hwnd))
  25. this._.Tabs := new this.CTabs(this.GUINum, this.hwnd)
  26. ;Parse Initial tabs
  27. Content := this.Content
  28. Loop, Parse, Content, |
  29. this._.Tabs._.Insert(new this.CTabs.CTab(A_LoopField, A_Index, this.GUINum, this.hwnd))
  30. }
  31. /*
  32. Variable: Tabs
  33. A list of all tabs. Each tab contains a list of controls that belong to it. The returned object is of type <CTabControl.CTabs>
  34. Variable: Text
  35. The text of the first tab.
  36. Variable: SelectedItem
  37. The selected tab item.
  38. Variable: SelectedIndex
  39. The index of the selected tab item.
  40. */
  41. __Get(Name, Params*)
  42. {
  43. if(Name = "Tabs")
  44. Value := this._.Tabs
  45. else if(Name = "Text")
  46. Value := this._.Tabs[1].Text
  47. else if(Name = "SelectedItem")
  48. {
  49. ControlGet, Value, Tab, , , % "ahk_id " this.hwnd
  50. Value := this.Tabs[Value]
  51. }
  52. else if(Name = "Selectedndex")
  53. ControlGet, Value, Tab, , , % "ahk_id " this.hwnd
  54. if(Params.MaxIndex() >= 1 && IsObject(value)) ;Fix unlucky multi parameter __GET
  55. {
  56. Value := Value[Params[1]]
  57. if(Params.MaxIndex() >= 2 && IsObject(value))
  58. Value := Value[Params[2]]
  59. }
  60. if(Value != "")
  61. return Value
  62. }
  63. __Set(Name, Params*)
  64. {
  65. Value := Params[Params.MaxIndex()]
  66. Params.Remove(Params.MaxIndex())
  67. Handled := true
  68. if(Name = "Text") ;Assign text -> assign text of first Tab
  69. this._.Tabs[1].Text := Value
  70. else if(Name = "Tabs")
  71. {
  72. if(Params[1] >= 1 && Params[1] <= this._.Tabs.MaxIndex()) ;Set a single Tab
  73. {
  74. if(IsObject(Value)) ;Set an object
  75. {
  76. this._.Tabs[Params[1]].Text := Value.Text
  77. ;Maybe do this later when icons are available?
  78. ;~ Tab := new this.CTabs.CTab(Value.HasKey("Text") ? Value.Text : "", Params[1], this.GUINum, this.Name)
  79. ;~ this._.Tabs._.Remove(Params[1])
  80. ;~ this._.Tabs._.Insert(Params[1], Tab)
  81. }
  82. else ;Just set text directly
  83. this._Tabs[Params[1]].Text := Value
  84. }
  85. }
  86. else if(Name = "SelectedItem" && CGUI_TypeOf(Value) = "CTabControl.CTabs.CTab")
  87. GuiControl, % this._.GUINum ":Choose", % this.ClassNN, % Value._.TabNumber
  88. else if(Name = "SelectedIndex" && CGUI_TypeOf(Value) = "CTabControl.CTabs.CTab")
  89. GuiControl, % this._.GUINum ":Choose", % this.ClassNN, % Value
  90. else
  91. Handled := false
  92. if(Handled)
  93. return Value
  94. }
  95. /*
  96. Event: Introduction
  97. To handle control events you need to create a function with this naming scheme in your window class: ControlName_EventName(params)
  98. The parameters depend on the event and there may not be params at all in some cases.
  99. Additionally it is required to create a label with this naming scheme: GUIName_ControlName
  100. GUIName is the name of the window class that extends CGUI. The label simply needs to call CGUI.HandleEvent().
  101. For better readability labels may be chained since they all execute the same code.
  102. Instead of using ControlName_EventName() you may also call <CControl.RegisterEvent> on a control instance to register a different event function name.
  103. Event: Attention
  104. The tab control does not support the FocusEnter and FocusLeave events.
  105. Event: Click(TabItem)
  106. Invoked when the user clicked on the control.
  107. Event: DoubleClick(TabItem)
  108. Invoked when the user double-clicked on the control.
  109. Event: RightClick(TabItem)
  110. Invoked when the user right-clicked on the control.
  111. Event: DoubleRightClick(TabItem)
  112. Invoked when the user double-right-clicked on the control.
  113. */
  114. HandleEvent(Event)
  115. {
  116. this.CallEvent({Normal : "Click", DoubleClick : "DoubleClick", Right : "RightClick", R : "DoubleRightClick"}[Event.GUIEvent], this.SelectedItem)
  117. }
  118. /*
  119. Class: CTabControl.CTabs
  120. An array of tabs.
  121. */
  122. Class CTabs
  123. {
  124. __New(GUINum, hwnd)
  125. {
  126. this.Insert("_", [])
  127. this.GUINum := GUINum
  128. this.hwnd := hwnd
  129. }
  130. /*
  131. Variable: 1,2,3,4,...
  132. Individual tabs can be accessed by their index.
  133. */
  134. __Get(Name, Params*)
  135. {
  136. if Name is Integer
  137. {
  138. if(Name <= this._.MaxIndex())
  139. {
  140. if(Params.MaxIndex() >= 1)
  141. return this._[Name][Params*]
  142. else
  143. return this._[Name]
  144. }
  145. }
  146. }
  147. /*
  148. Function: MaxIndex
  149. Returns the number of tabs.
  150. */
  151. MaxIndex()
  152. {
  153. return this._.MaxIndex()
  154. }
  155. _NewEnum()
  156. {
  157. ;~ global CEnumerator
  158. return new CEnumerator(this._)
  159. }
  160. __Set(Name, Params*)
  161. {
  162. ;~ global CGUI
  163. Value := Params[Params.MaxIndex()]
  164. Params.Remove(Params.MaxIndex())
  165. if Name is Integer
  166. {
  167. if(Name <= this._.MaxIndex())
  168. {
  169. if(Params.MaxIndex() >= 1) ;Set a property of CTab
  170. {
  171. Tab := this._[Name]
  172. Tab[Params*] := Value
  173. }
  174. return Value
  175. }
  176. }
  177. }
  178. /*
  179. Function: Add
  180. Adds a tab.
  181. Parameters:
  182. Text - The text of the new tab.
  183. Returns:
  184. An object of type <CTabControl.CTabs.CTab>.
  185. */
  186. Add(Text)
  187. {
  188. ;~ global CGUI
  189. Tabs := []
  190. Loop, Parse, Text, |
  191. {
  192. Tab := new this.CTab(A_loopField, this._.MaxIndex() + 1, this.GUINum, this.hwnd)
  193. this._.Insert(Tab)
  194. Tabs.Insert(Tab)
  195. Control := CGUI.GUIList[this.GUINum][this.hwnd]
  196. GuiControl, % this.GUINum ":", % Control.ClassNN, %A_loopField%
  197. }
  198. return Tabs.MaxIndex() > 1 ? Tabs : Tabs[1]
  199. }
  200. ;Removing tabs is unsupported for now because the controls will not be removed
  201. ;~ Remove(TabNumber)
  202. ;~ {
  203. ;~ }
  204. /*
  205. Class: CTabControl.CTabs.CTab
  206. A single tab object.
  207. */
  208. Class CTab
  209. {
  210. __New(Text, TabNumber, GUINum, hwnd)
  211. {
  212. this.Insert("_", {})
  213. this._.Text := Text
  214. this._.TabNumber := TabNumber
  215. this._.GUINum := GUINum
  216. this._.hwnd := hwnd
  217. this._.Controls := {}
  218. }
  219. /*
  220. Function: AddControl
  221. Adds a control to this tab. The parameters correspond to the Add() function of CGUI.
  222. Parameters:
  223. Type - The type of the control.
  224. Name - The name of the control.
  225. Options - Options used for creating the control.
  226. Text - The text of the control.
  227. */
  228. AddControl(type, Name, Options, Text)
  229. {
  230. ;~ global CGUI
  231. if(type != "Tab")
  232. {
  233. GUI := CGUI.GUIList[this.GUINum]
  234. TabControl := GUI.Controls[this._.hwnd]
  235. Gui, % this.GUINum ":Tab", % this._.TabNumber, % TabControl._.TabIndex
  236. Control := GUI.AddControl(type, Name, Options, Text, this._.Controls)
  237. Control.hParentControl := this._.hwnd
  238. Control.TabNumber := this._.TabNumber
  239. Gui, % this.GUINum ":Tab"
  240. return Control
  241. }
  242. else
  243. Msgbox Tabs may not be added in a tab container.
  244. }
  245. /*
  246. Variable: Text
  247. The text of the tab.
  248. Variable: Icon
  249. The filename of the icon associated with this tab.
  250. Variable: IconNumber
  251. The index of the icon in a multi-icon file.
  252. */
  253. __Get(Name, Params*)
  254. {
  255. if(Name != "_" && this._.HasKey(Name))
  256. Value := this._[Name]
  257. Loop % Params.MaxIndex()
  258. if(IsObject(Value)) ;Fix unlucky multi parameter __GET
  259. Value := Value[Params[A_Index]]
  260. if(Value)
  261. return Value
  262. }
  263. __Set(Name, Value)
  264. {
  265. ;~ global CGUI
  266. if(Name = "Text")
  267. {
  268. Control := CGUI.GUIList[this.GUINum].Controls[this.hwnd]
  269. Tabs := ""
  270. for index, Tab in Control.Tabs
  271. if(index != this._.TabNumber)
  272. Tabs .= "|" Tab.Text
  273. else
  274. Tabs .= "|" Value
  275. this._[Name] := Value
  276. GuiControl, % this.GUINum ":", % Control.ClassNN, %Tabs%
  277. return Value
  278. }
  279. else if(Name = "Icon")
  280. {
  281. this.SetIcon(Value, this.IconNumber ? this.IconNumber : 1)
  282. return Value
  283. }
  284. else if(Name = "IconNumber")
  285. {
  286. this.IconNumber := Value
  287. if(this.Icon)
  288. this.SetIcon(this.Icon, Value)
  289. return Value
  290. }
  291. }
  292. /*
  293. Function: SetIcon
  294. Sets the icon of a tab.
  295. Parameters:
  296. Filename - The filename of the file containing the icon.
  297. IconNumber - The icon number in a multi-icon file.
  298. */
  299. SetIcon(Filename, IconNumber = 1)
  300. {
  301. ;~ global CGUI
  302. this._.Icon := Filename
  303. this._.IconNumber := IconNumber
  304. Control := CGUI.GUIList[this.GUINum].Controls[this.hwnd]
  305. Control._.ImageListManager.SetIcon(this._.TabNumber, Filename, IconNumber)
  306. }
  307. }
  308. }
  309. }