PageRenderTime 11ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/CTabControl.ahk

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