PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/stable-1.0.3/Client/PHPPageItemControl.cs

#
C# | 270 lines | 224 code | 36 blank | 10 comment | 17 complexity | d75d84f1c3fa46f85e150d0e22712d78 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //-----------------------------------------------------------------------
  2. // <copyright>
  3. // Copyright (C) Ruslan Yakushev for the PHP Manager for IIS project.
  4. //
  5. // This file is subject to the terms and conditions of the Microsoft Public License (MS-PL).
  6. // See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL for more details.
  7. // </copyright>
  8. //-----------------------------------------------------------------------
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Drawing;
  12. using System.Security.Permissions;
  13. using System.Text;
  14. using System.Windows.Forms;
  15. namespace Web.Management.PHP
  16. {
  17. internal partial class PHPPageItemControl : UserControl
  18. {
  19. private const int WS_EX_NOINHERITLAYOUT = 0x100000;
  20. private const int WS_EX_LAYOUTRTL = 0x400000;
  21. private const string WarningLabelName = "warningLabel";
  22. private bool _rightToLeftLayout;
  23. private int _tlpRowCount;
  24. private Action<int> _handler;
  25. public PHPPageItemControl()
  26. {
  27. InitializeComponent();
  28. }
  29. protected override CreateParams CreateParams
  30. {
  31. [
  32. SecurityPermission(SecurityAction.InheritanceDemand, Flags = SecurityPermissionFlag.UnmanagedCode),
  33. SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)
  34. ]
  35. get
  36. {
  37. CreateParams CP;
  38. CP = base.CreateParams;
  39. if (_rightToLeftLayout)
  40. {
  41. CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL | WS_EX_NOINHERITLAYOUT;
  42. }
  43. return CP;
  44. }
  45. }
  46. public Image Image
  47. {
  48. get
  49. {
  50. return _pictureBox.Image;
  51. }
  52. set
  53. {
  54. _pictureBox.Image = value;
  55. }
  56. }
  57. public bool RightToLeftLayout
  58. {
  59. get
  60. {
  61. return _rightToLeftLayout;
  62. }
  63. set
  64. {
  65. if (_rightToLeftLayout != value)
  66. {
  67. _rightToLeftLayout = value;
  68. if (IsHandleCreated)
  69. {
  70. base.OnRightToLeftChanged(EventArgs.Empty);
  71. }
  72. }
  73. }
  74. }
  75. public string Title
  76. {
  77. get
  78. {
  79. return _titleLabel.Text;
  80. }
  81. set
  82. {
  83. _titleLabel.Text = value;
  84. }
  85. }
  86. public Font TitleFont
  87. {
  88. get
  89. {
  90. return _titleLabel.Font;
  91. }
  92. set
  93. {
  94. _titleLabel.Font = value;
  95. }
  96. }
  97. public event LinkLabelLinkClickedEventHandler TitleClick
  98. {
  99. add
  100. {
  101. _titleLabel.LinkClicked += value;
  102. }
  103. remove
  104. {
  105. _titleLabel.LinkClicked -= value;
  106. }
  107. }
  108. public void AddInfoRow(Label labelName, Label labelValue)
  109. {
  110. labelName.Dock = labelValue.Dock = DockStyle.Fill;
  111. labelName.TextAlign = labelValue.TextAlign = ContentAlignment.MiddleLeft;
  112. _infoTlp.Controls.Add(labelName, 0, _tlpRowCount);
  113. _infoTlp.Controls.Add(labelValue, 1, _tlpRowCount);
  114. _tlpRowCount++;
  115. }
  116. public void AddSpanRow(Label labelSpan)
  117. {
  118. labelSpan.Dock = DockStyle.Fill;
  119. labelSpan.TextAlign = ContentAlignment.MiddleLeft;
  120. _infoTlp.Controls.Add(labelSpan, 0, _tlpRowCount);
  121. _infoTlp.SetColumnSpan(labelSpan, 2);
  122. _tlpRowCount++;
  123. }
  124. public void AddTask(Action<int> handler, params string[] actionTitles)
  125. {
  126. if (handler == null)
  127. {
  128. throw new ArgumentNullException("handler");
  129. }
  130. if (_handler != null)
  131. {
  132. throw new InvalidOperationException();
  133. }
  134. _handler = handler;
  135. StringBuilder sb = new StringBuilder();
  136. bool first = true;
  137. List<LinkLabel.Link> links = new List<LinkLabel.Link>();
  138. foreach (string s in actionTitles)
  139. {
  140. if (!first)
  141. {
  142. sb.Append(Resources.PHPPageItemTaskSeparator);
  143. }
  144. first = false;
  145. links.Add(new LinkLabel.Link(sb.Length, s.Length, links.Count));
  146. sb.Append(s);
  147. }
  148. _tasksLabel.Text = sb.ToString();
  149. foreach (LinkLabel.Link l in links)
  150. {
  151. _tasksLabel.Links.Add(l);
  152. }
  153. }
  154. public void ClearWarning()
  155. {
  156. _warningPanel.Controls.RemoveByKey(WarningLabelName);
  157. _warningPanel.Visible = false;
  158. }
  159. private Size DoLayout(Size proposedSize, bool performLayout)
  160. {
  161. if (performLayout)
  162. {
  163. _titleLabel.Width = ClientRectangle.Width;
  164. }
  165. int nextTop = _titleLabel.Top + _titleLabel.Height + 8;
  166. if (_warningPanel.Visible)
  167. {
  168. Size warningSize = _warningPanel.GetPreferredSize(new Size(proposedSize.Width - _warningPanel.Left, Int32.MaxValue));
  169. if (performLayout)
  170. {
  171. _warningPanel.Top = nextTop;
  172. _warningPanel.Size = warningSize;
  173. }
  174. nextTop += _warningPanel.Height + 8;
  175. }
  176. Size infoSize = _infoTlp.GetPreferredSize(new Size(proposedSize.Width - _infoTlp.Left, Int32.MaxValue));
  177. if (performLayout)
  178. {
  179. _infoTlp.Top = nextTop;
  180. _infoTlp.Size = infoSize;
  181. }
  182. nextTop += _infoTlp.Height + 10;
  183. Size tasksSize = _tasksLabel.GetPreferredSize(new Size(proposedSize.Width - _tasksLabel.Left, Int32.MaxValue));
  184. if (performLayout)
  185. {
  186. _tasksLabel.Top = nextTop;
  187. _tasksLabel.Size = tasksSize;
  188. }
  189. int height = tasksSize.Height + nextTop + 12;
  190. return new Size(proposedSize.Width, height);
  191. }
  192. public override Size GetPreferredSize(Size proposedSize)
  193. {
  194. Size size = DoLayout(proposedSize, false);
  195. return size;
  196. }
  197. protected override void OnLayout(LayoutEventArgs e)
  198. {
  199. DoLayout(this.Size, true);
  200. }
  201. protected override void OnRightToLeftChanged(EventArgs e)
  202. {
  203. base.OnRightToLeftChanged(e);
  204. RightToLeftLayout = RightToLeft == RightToLeft.Yes;
  205. }
  206. private void OnTasksLabelLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
  207. {
  208. _handler((int)e.Link.LinkData);
  209. }
  210. public void SetTaskState(int index, bool enabled)
  211. {
  212. LinkLabel.Link l = _tasksLabel.Links[index];
  213. if (l != null)
  214. {
  215. l.Enabled = enabled;
  216. }
  217. }
  218. public void SetTitleState(bool enabled)
  219. {
  220. _titleLabel.Enabled = enabled;
  221. }
  222. public void SetWarning(Label warningLabel)
  223. {
  224. warningLabel.AutoSize = true;
  225. warningLabel.Location = new Point(25, 6);
  226. warningLabel.Name = WarningLabelName;
  227. // Remove existing label from the warning panel in case it exists
  228. _warningPanel.Controls.RemoveByKey(WarningLabelName);
  229. // Add new label to the warning panel
  230. _warningPanel.Controls.Add(warningLabel);
  231. _warningPanel.Visible = true;
  232. }
  233. }
  234. }