PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/XNAFinalEngineEditor/UserInterface/Controls/TabControl.cs

#
C# | 330 lines | 239 code | 68 blank | 23 comment | 41 complexity | 457858af99c341f78b686e6b9d166712 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, LGPL-2.1
  1. #region License
  2. /*
  3. Based in the project Neoforce Controls (http://neoforce.codeplex.com/)
  4. GNU Library General Public License (LGPL)
  5. -----------------------------------------------------------------------------------------------------------------------------------------------
  6. Modified by: Schneider, José Ignacio (jis@cs.uns.edu.ar)
  7. -----------------------------------------------------------------------------------------------------------------------------------------------
  8. */
  9. #endregion
  10. #region Using directives
  11. using System;
  12. using System.Collections.Generic;
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Graphics;
  15. #endregion
  16. namespace XNAFinalEngine.UserInterface
  17. {
  18. public class TabPage : Container
  19. {
  20. #region Variables
  21. private Rectangle headerRectangle = Rectangle.Empty;
  22. #endregion
  23. #region Properties
  24. protected internal Rectangle HeaderRectangle { get { return headerRectangle; } }
  25. #endregion
  26. #region Constructor
  27. public TabPage()
  28. {
  29. Color = Color.Transparent;
  30. Passive = true;
  31. CanFocus = false;
  32. } // TabPage
  33. #endregion
  34. #region Calculate Rectangle
  35. protected internal void CalculateRectangle(Rectangle prev, SpriteFont font, Margins margins, Point offset, bool first)
  36. {
  37. int size = (int)Math.Ceiling(font.MeasureString(Text).X) + margins.Horizontal;
  38. if (first) offset.X = 0;
  39. headerRectangle = new Rectangle(prev.Right + offset.X, prev.Top, size, prev.Height);
  40. } // CalculateRectangle
  41. #endregion
  42. #region Adjust Margins
  43. /// <summary>
  44. /// Adjust the controls margin.
  45. /// </summary>
  46. /// <remarks>
  47. /// This implementation requires that the know margins are set before the base method is called because the base method use this modified information.
  48. /// </remarks>
  49. protected override void AdjustMargins()
  50. {
  51. ClientMargins = new Margins(0, 0, 0, 0);
  52. base.AdjustMargins();
  53. } // AdjustMargins
  54. #endregion
  55. } // TabPage
  56. public class TabControl : Container
  57. {
  58. #region Variables
  59. private readonly List<TabPage> tabPages = new List<TabPage>();
  60. private int selectedIndex;
  61. private int hoveredIndex = -1;
  62. #endregion
  63. #region Properties
  64. public TabPage[] TabPages { get { return tabPages.ToArray(); } }
  65. public virtual int SelectedIndex
  66. {
  67. get { return selectedIndex; }
  68. set
  69. {
  70. if (selectedIndex >= 0 && selectedIndex < tabPages.Count && value >= 0 && value < tabPages.Count)
  71. {
  72. TabPages[selectedIndex].Visible = false;
  73. }
  74. if (value >= 0 && value < tabPages.Count)
  75. {
  76. TabPages[value].Visible = true;
  77. ControlsList c = TabPages[value].ChildrenControls as ControlsList;
  78. if (c.Count > 0) c[0].Focused = true;
  79. selectedIndex = value;
  80. if (!Suspended) OnPageChanged(new EventArgs());
  81. }
  82. }
  83. } // SelectedIndex
  84. public virtual TabPage SelectedPage
  85. {
  86. get { return tabPages[SelectedIndex]; }
  87. set
  88. {
  89. for (int i = 0; i < tabPages.Count; i++)
  90. {
  91. if (tabPages[i] == value)
  92. {
  93. SelectedIndex = i;
  94. break;
  95. }
  96. }
  97. }
  98. } // SelectedPage
  99. #endregion
  100. #region Events
  101. public event EventHandler PageChanged;
  102. #endregion
  103. #region Constructor
  104. public TabControl()
  105. {
  106. CanFocus = false;
  107. } // TabControl
  108. #endregion
  109. #region Dispose
  110. /// <summary>
  111. /// Dispose managed resources.
  112. /// </summary>
  113. protected override void DisposeManagedResources()
  114. {
  115. // A disposed object could be still generating events, because it is alive for a time, in a disposed state, but alive nevertheless.
  116. PageChanged = null;
  117. base.DisposeManagedResources();
  118. } // DisposeManagedResources
  119. #endregion
  120. #region Draw
  121. /// <summary>
  122. /// Prerender the control into the control's render target.
  123. /// </summary>
  124. protected override void DrawControl(Rectangle rect)
  125. {
  126. SkinLayer l1 = SkinInformation.Layers["Control"];
  127. SkinLayer l2 = SkinInformation.Layers["Header"];
  128. Color col = Color != UndefinedColor ? Color : Color.White;
  129. Rectangle r1 = new Rectangle(rect.Left, rect.Top + l1.OffsetY, rect.Width, rect.Height - l1.OffsetY);
  130. if (tabPages.Count <= 0)
  131. {
  132. r1 = rect;
  133. }
  134. base.DrawControl(r1);
  135. if (tabPages.Count > 0)
  136. {
  137. Rectangle prev = new Rectangle(rect.Left, rect.Top + l2.OffsetY, 0, l2.Height);
  138. for (int i = 0; i < tabPages.Count; i++)
  139. {
  140. SpriteFont font = l2.Text.Font.Font.Resource;
  141. Margins margins = l2.ContentMargins;
  142. Point offset = new Point(l2.OffsetX, l2.OffsetY);
  143. if (i > 0) prev = tabPages[i - 1].HeaderRectangle;
  144. tabPages[i].CalculateRectangle(prev, font, margins, offset, i == 0);
  145. }
  146. for (int i = tabPages.Count - 1; i >= 0; i--)
  147. {
  148. int li = tabPages[i].Enabled ? l2.States.Enabled.Index : l2.States.Disabled.Index;
  149. Color lc = tabPages[i].Enabled ? l2.Text.Colors.Enabled : l2.Text.Colors.Disabled;
  150. if (i == hoveredIndex)
  151. {
  152. li = l2.States.Hovered.Index;
  153. lc = l2.Text.Colors.Hovered;
  154. }
  155. Margins m = l2.ContentMargins;
  156. Rectangle rx = tabPages[i].HeaderRectangle;
  157. Rectangle sx = new Rectangle(rx.Left + m.Left, rx.Top + m.Top, rx.Width - m.Horizontal, rx.Height - m.Vertical);
  158. if (i != selectedIndex)
  159. {
  160. Renderer.DrawLayer(l2, rx, col, li);
  161. Renderer.DrawString(l2.Text.Font.Font.Resource, tabPages[i].Text, sx, lc, l2.Text.Alignment);
  162. }
  163. }
  164. Margins mi = l2.ContentMargins;
  165. Rectangle ri = tabPages[selectedIndex].HeaderRectangle;
  166. Rectangle si = new Rectangle(ri.Left + mi.Left, ri.Top + mi.Top, ri.Width - mi.Horizontal, ri.Height - mi.Vertical);
  167. Renderer.DrawLayer(l2, ri, col, l2.States.Focused.Index);
  168. Renderer.DrawString(l2.Text.Font.Font.Resource, tabPages[selectedIndex].Text, si, l2.Text.Colors.Focused, l2.Text.Alignment, l2.Text.OffsetX, l2.Text.OffsetY, false);
  169. }
  170. } // DrawControl
  171. #endregion
  172. #region Add or Remove Page
  173. public virtual TabPage AddPage(string text)
  174. {
  175. TabPage p = AddPage();
  176. p.Text = text;
  177. return p;
  178. } // AddPage
  179. public virtual TabPage AddPage()
  180. {
  181. TabPage page = new TabPage
  182. {
  183. Left = 0,
  184. Top = 0,
  185. Width = ClientWidth,
  186. Height = ClientHeight,
  187. Anchor = Anchors.All,
  188. Text = "Tab " + (tabPages.Count + 1),
  189. Visible = false,
  190. AutoScroll = true,
  191. };
  192. Add(page, true);
  193. tabPages.Add(page);
  194. tabPages[0].Visible = true;
  195. return page;
  196. } // AddPage
  197. public virtual void RemovePage(TabPage page, bool dispose)
  198. {
  199. tabPages.Remove(page);
  200. if (dispose)
  201. {
  202. page.Dispose();
  203. }
  204. SelectedIndex = 0;
  205. } // RemovePage
  206. public virtual void RemovePage(TabPage page)
  207. {
  208. RemovePage(page, true);
  209. } // RemovePage
  210. #endregion
  211. #region OnMouseDown, OnMouseMove, OnPageChanged
  212. protected override void OnMouseDown(MouseEventArgs e)
  213. {
  214. base.OnMouseDown(e);
  215. if (tabPages.Count > 1)
  216. {
  217. Point p = new Point(e.State.X - Root.ControlLeftAbsoluteCoordinate, e.State.Y - Root.ControlTopAbsoluteCoordinate);
  218. for (int i = 0; i < tabPages.Count; i++)
  219. {
  220. Rectangle r = tabPages[i].HeaderRectangle;
  221. if (p.X >= r.Left && p.X <= r.Right && p.Y >= r.Top && p.Y <= r.Bottom)
  222. {
  223. SelectedIndex = i;
  224. break;
  225. }
  226. }
  227. }
  228. } // OnMouseDown
  229. protected override void OnMouseMove(MouseEventArgs e)
  230. {
  231. base.OnMouseMove(e);
  232. if (tabPages.Count > 1)
  233. {
  234. int index = hoveredIndex;
  235. Point p = new Point(e.State.X - Root.ControlLeftAbsoluteCoordinate, e.State.Y - Root.ControlTopAbsoluteCoordinate);
  236. for (int i = 0; i < tabPages.Count; i++)
  237. {
  238. Rectangle r = tabPages[i].HeaderRectangle;
  239. if (p.X >= r.Left && p.X <= r.Right && p.Y >= r.Top && p.Y <= r.Bottom && tabPages[i].Enabled)
  240. {
  241. index = i;
  242. break;
  243. }
  244. index = -1;
  245. }
  246. if (index != hoveredIndex)
  247. {
  248. hoveredIndex = index;
  249. Invalidate();
  250. }
  251. }
  252. } // OnMouseMove
  253. protected virtual void OnPageChanged(EventArgs e)
  254. {
  255. if (PageChanged != null) PageChanged.Invoke(this, e);
  256. } // OnPageChanged
  257. #endregion
  258. } // TabControl
  259. } // XNAFinalEngine.UserInterface