1////////////////////////////////////////////////////////////////////////////////
2//
3//  ADOBE SYSTEMS INCORPORATED
4//  Copyright 2003-2006 Adobe Systems Incorporated
5//  All Rights Reserved.
6//
7//  NOTICE: Adobe permits you to use, modify, and distribute this file
8//  in accordance with the terms of the license agreement accompanying it.
9//
10////////////////////////////////////////////////////////////////////////////////
11
12package mx.containers
13{
14
15import flash.events.Event;
16import flash.text.TextFieldAutoSize;
17import mx.controls.Label;
18import mx.core.EdgeMetrics;
19import mx.core.UIComponent;
20
21include "../styles/metadata/LeadingStyle.as"
22include "../styles/metadata/PaddingStyles.as"
23include "../styles/metadata/TextStyles.as"
24
25/**
26 *  Number of pixels between the label area and the heading text.
27 *
28 *  @default 14
29 *
30 *  @langversion 3.0
31 *  @playerversion Flash 9
32 *  @playerversion AIR 1.1
33 *  @productversion Flex 3
34 */
35[Style(name="indicatorGap", type="Number", format="Length", inherit="yes")]
36
37/**
38 *  Width of the form labels.
39 *  The default value is the length of the longest label in the form.
40 *  For FormHeading, the <code>labelWidth</code>
41 *  is space to the left of the heading text.
42 *
43 *  @langversion 3.0
44 *  @playerversion Flash 9
45 *  @playerversion AIR 1.1
46 *  @productversion Flex 3
47 */
48[Style(name="labelWidth", type="Number", format="Length", inherit="yes")]
49
50/**
51 *  Number of pixels above the heading text.
52 *
53 *  @default 0
54 *
55 *  @langversion 3.0
56 *  @playerversion Flash 9
57 *  @playerversion AIR 1.1
58 *  @productversion Flex 3
59 */
60[Style(name="paddingTop", type="Number", format="Length", inherit="no")]
61
62[IconFile("FormHeading.png")]
63
64[Alternative(replacement="spark.components.FormHeading", since="4.5")]
65
66/**
67 *  The FormHeading container is used to display a heading
68 *  for a group of controls inside a Form container.
69 *  The left side of the heading is aligned
70 *  with the left side of the controls inside the form.
71 *  You can include multiple FormHeading containers within a single Form
72 *  container.
73 *
74 *  @mxml
75 *
76 *  <p>The <code>&lt;mx:FormHeading&gt;</code> tag inherits all of the tag
77 *  attributes of its superclass and adds the following tag attributes:</p>
78 *
79 *  <pre>
80 *  &lt;mx:FormHeading
81 *    <strong>Properties</strong>
82 *    label=""
83 *
84 *    <strong>Styles</strong>
85 *    color="0x0B333C"
86 *    disabledColor="0xAAB3B3"
87 *    fontAntiAliasType="advanced|normal"
88 *    fontFamily="Verdana"
89 *    fontGridFitType="pixel|subpixel|none"
90 *    fontSharpness="0"
91 *    fontSize="12"
92 *    fontStyle="normal|italic"
93 *    fontThickness="0"
94 *    fontWeight="normal|bold"
95 *    indicatorGap="14"
96 *    labelWidth="<i>Calculated</i>"
97 *    leading="2"
98 *    paddingLeft="0"
99 *    paddingRight="0"
100 *    paddingTop="0"
101 *    textAlign="<i>Calculated</i>"
102 *    textDecoration="none|underline"
103 *    textIndent="0"
104 *  /&gt;
105 *  </pre>
106 *
107 *  @see mx.containers.Form
108 *  @see mx.containers.FormItem
109 *
110 *  @includeExample examples/FormExample.mxml
111 *
112 *  @langversion 3.0
113 *  @playerversion Flash 9
114 *  @playerversion AIR 1.1
115 *  @productversion Flex 3
116 */
117public class FormHeading extends UIComponent
118{
119	include "../core/Version.as";
120
121	//--------------------------------------------------------------------------
122	//
123	//  Constructor
124	//
125	//--------------------------------------------------------------------------
126
127	/**
128	 *  Constructor.
129	 *
130	 *  @langversion 3.0
131	 *  @playerversion Flash 9
132	 *  @playerversion AIR 1.1
133	 *  @productversion Flex 3
134	 */
135	public function FormHeading()
136	{
137		super();
138	}
139
140	//--------------------------------------------------------------------------
141	//
142	//  Variables
143	//
144	//--------------------------------------------------------------------------
145
146	/**
147	 *  @private
148	 */
149	private var labelObj:Label;
150
151	//--------------------------------------------------------------------------
152	//
153	//  Properties
154	//
155	//--------------------------------------------------------------------------
156
157	//----------------------------------
158	//  label
159	//----------------------------------
160
161	/**
162	 *  @private
163	 *  Storage for the label property.
164	 */
165	private var _label:String = "";
166
167	[Bindable("labelChanged")]
168	[Inspectable(category="General", defaultValue="")]
169
170	/**
171	 *  Form heading text.
172	 *
173	 *  @langversion 3.0
174	 *  @playerversion Flash 9
175	 *  @playerversion AIR 1.1
176	 *  @productversion Flex 3
177	 */
178	public function get label():String
179	{
180		return _label;
181	}
182
183	/**
184	 *  @private
185	 */
186	public function set label(value:String):void
187	{
188		_label = value;
189
190		invalidateProperties();
191
192		dispatchEvent(new Event("labelChanged"));
193	}
194
195	//--------------------------------------------------------------------------
196	//
197	//  Overridden methods: UIComponent
198	//
199	//--------------------------------------------------------------------------
200
201	/**
202	 *  @private
203	 */
204	override protected function commitProperties():void
205	{
206		super.commitProperties();
207
208		createLabel();
209	}
210
211	/**
212	 *  @private
213	 */
214	override protected function measure():void
215	{
216		super.measure();
217
218		var preferredWidth:Number = 0;
219		var preferredHeight:Number = getStyle("paddingTop");
220
221		if (labelObj)
222		{
223			if (isNaN(labelObj.measuredWidth))
224				labelObj.validateSize();
225
226			preferredWidth = labelObj.measuredWidth;
227			preferredHeight += labelObj.measuredHeight;
228		}
229
230		preferredWidth += getLabelWidth() + getStyle("indicatorGap");
231
232		measuredMinWidth = preferredWidth;
233		measuredMinHeight = preferredHeight;
234		measuredWidth = preferredWidth;
235		measuredHeight = preferredHeight;
236	}
237
238	/**
239	 *  @private
240	 */
241	override protected function updateDisplayList(unscaledWidth:Number,
242												  unscaledHeight:Number):void
243	{
244		super.updateDisplayList(unscaledWidth, unscaledHeight);
245
246		if (labelObj)
247		{
248			var indicatorGap:Number = getStyle("indicatorGap");
249			var paddingTop:Number = getStyle("paddingTop");
250			var labelWidth:Number = width;
251
252			labelObj.move(getLabelWidth() + indicatorGap, paddingTop);
253
254			if (parent && parent is Form)
255			{
256				var vm:EdgeMetrics = Form(parent).viewMetricsAndPadding;
257
258				labelWidth = parent.width -
259					(getLabelWidth() + indicatorGap + vm.left + vm.right);
260			}
261
262			labelObj.setActualSize(labelWidth, height);
263		}
264	}
265
266	//--------------------------------------------------------------------------
267	//
268	//  Methods
269	//
270	//--------------------------------------------------------------------------
271
272	/**
273	 *  @private
274	 */
275	private function createLabel():void
276	{
277		// See if we need to create our labelObj.
278		if (_label && _label.length > 0)
279		{
280			if (!labelObj)
281			{
282				labelObj = new Label();
283				labelObj.styleName = this;
284				addChild(labelObj);
285			}
286
287			if (labelObj.text != _label)
288			{
289				labelObj.text = _label;
290
291				labelObj.validateSize();
292
293				invalidateSize();
294				invalidateDisplayList();
295			}
296		}
297
298		// See if we need to destroy our labelObj.
299		if ((_label==null || _label.length == 0) && labelObj)
300		{
301			removeChild(labelObj);
302			labelObj = null;
303
304			invalidateSize();
305			invalidateDisplayList();
306		}
307	}
308
309	/**
310	 *  @private
311	 */
312	private function getLabelWidth():Number
313	{
314		var labelWidth:Number = getStyle("labelWidth");
315
316		// labelWidth of 0 is the same as NaN
317		if (labelWidth == 0)
318		{
319			labelWidth = NaN;
320		}
321
322		if (isNaN(labelWidth) && parent is Form)
323			labelWidth = Form(parent).calculateLabelWidth();
324
325		if (isNaN(labelWidth))
326			labelWidth = 0;
327
328		return labelWidth;
329	}
330}
331
332}
333