// $Author: asskoala $
// $Date: 2005/12/11 21:07:21 $
// $Revision: 1.1 $

using System;
using System.Drawing;
using System.Windows.Forms;

namespace Icarus.LUCIE
{
	/// <summary>
	/// The root component is the invisible container that will have all of the components in a single interface in it. It is told who our parent .NET control is and uses it for things like getting mouse input, if necessary. Note that this component will always resize itself to match the dimensions of the parent control.
	/// </summary>
	public class RootComponent: AbstractComponent
	{
		/// <summary>
		/// The parent control of the UI tree.
		/// </summary>
		private Control mParentControl;

		private bool mListeningForEvents = false;

		/// <summary>
		/// Initializes the root component.
		/// </summary>
		/// <param name="parent">Which control do we use for user events and such?</param>
		public RootComponent(Control parent)
		{
			this.mParentControl = parent;
			UpdateBounds();
		}

		/// <summary>
		/// Sets whether or not this control should listen for events.
		/// </summary>
		public bool IsListeningForEvents
		{
			get { return mListeningForEvents; }
			set
			{
				if (value)
				{
					if (!IsListeningForEvents)
					{
						//Update variable:
						mListeningForEvents = true;

						//Attach handlers:
						this.mParentControl.SizeChanged += new EventHandler(mParentControl_SizeChanged);
						this.mParentControl.MouseUp += new MouseEventHandler(mParentControl_MouseUp);
					}
				}
				else
				{
					if (IsListeningForEvents)
					{
						//Update variable:
						mListeningForEvents = false;

						//Detach the events:
						this.mParentControl.SizeChanged -= new EventHandler(mParentControl_SizeChanged);
						this.mParentControl.MouseUp -= new MouseEventHandler(mParentControl_MouseUp);
					}
				}
			}
		}

		/// <summary>
		/// The parent .NET control of the UI tree.
		/// </summary>
		public Control ParentControl
		{
			get { return mParentControl; }
		}

		/// <summary>
		/// This function will set the bounds of the root component to match the bounds of the parent control.
		/// </summary>
		private void UpdateBounds()
		{
			this.Location = new Point(0, 0);
			this.Size = this.mParentControl.Size;
		}

		/// <summary>
		/// Gets called whenever the size of the parent control changes.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mParentControl_SizeChanged(object sender, EventArgs e)
		{
			this.UpdateBounds();
		}

		/// <summary>
		/// Gets called whenever the mouse button goes up over the control, which is our cue for a click.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void mParentControl_MouseUp(object sender, MouseEventArgs e)
		{
			AbstractComponent c = this.FindChildAt(new Point(e.X, e.Y));

			if (c != null)
			{
				//We've been clickzorred!
				c.OnClick();
			}
		}

		protected override void RenderSelf(Rectangle coordinates, Icarus.Graphics.Animation.IRenderParameters parameters)
		{
			//I'm invisible.
		}

	}
}
