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