PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Other/Netron/WinFormsUI/NetronExtender/FromBase/DockPaneCaptionFromBase.cs

https://github.com/AnthonyNystrom/GenXSource
C# | 392 lines | 323 code | 57 blank | 12 comment | 21 complexity | 583aacd5884748c345d1d8ebaa5d18d5 MD5 | raw file
  1. using System;
  2. using System.Drawing;
  3. using System.Windows.Forms;
  4. using System.Drawing.Drawing2D;
  5. using WeifenLuo.WinFormsUI;
  6. using System.ComponentModel;
  7. namespace WeifenLuo.WinFormsUI.Netron
  8. {
  9. [ToolboxItem(false)]
  10. public class DockPaneCaptionFromBase : DockPaneCaptionBase
  11. {
  12. #region consts
  13. private const int _TextGapTop = 2;
  14. private const int _TextGapBottom = 0;
  15. private const int _TextGapLeft = 3;
  16. private const int _TextGapRight = 3;
  17. private const int _ButtonGapTop = 2;
  18. private const int _ButtonGapBottom = 1;
  19. private const int _ButtonGapBetween = 1;
  20. private const int _ButtonGapLeft = 1;
  21. private const int _ButtonGapRight = 2;
  22. private const string _ResourceImageCloseEnabled = "DockPaneCaption.CloseEnabled.bmp";
  23. private const string _ResourceImageCloseDisabled = "DockPaneCaption.CloseDisabled.bmp";
  24. private const string _ResourceImageAutoHideYes = "DockPaneCaption.AutoHideYes.bmp";
  25. private const string _ResourceImageAutoHideNo = "DockPaneCaption.AutoHideNo.bmp";
  26. private const string _ResourceToolTipClose = "DockPaneCaption.ToolTipClose";
  27. private const string _ResourceToolTipAutoHide = "DockPaneCaption.ToolTipAutoHide";
  28. #endregion
  29. private InertButton m_buttonClose;
  30. private InertButton m_buttonAutoHide;
  31. private InertButton m_buttonWhatIsIt;
  32. private LinearGradientBrush captionBrush;
  33. private Color darkColor = ColorMixer.DarkColor;//Color.SlateGray;
  34. private Color lightColor = Color.Silver;
  35. public Color DarkColor
  36. {
  37. get{return darkColor;}
  38. set
  39. {
  40. darkColor = value;
  41. SetBrush();
  42. }
  43. }
  44. public Color LightColor
  45. {
  46. get{return lightColor;}
  47. set
  48. {
  49. lightColor = value;
  50. SetBrush();
  51. }
  52. }
  53. public void SetBrush()
  54. {
  55. if(ClientRectangle==Rectangle.Empty) return;
  56. captionBrush = new LinearGradientBrush(ClientRectangle, darkColor, lightColor, LinearGradientMode.Horizontal);
  57. // if(bellShaped)
  58. // captionBrush.SetSigmaBellShape(bellCenter,bellFalloff);
  59. // darkBrush = new SolidBrush(darkColor);
  60. // roundingPen = new Pen(darkColor,1);
  61. // foreBrush = new SolidBrush(this.ForeColor);
  62. // unselectedForeBrush = new SolidBrush(unselectedForeColor);
  63. // unselectedEdgePen = new Pen(unselectedEdgeColor,1);
  64. // unselectedTabBrush = new SolidBrush(UnselectedTabColor);
  65. Invalidate();
  66. }
  67. protected internal DockPaneCaptionFromBase(DockPane pane) : base(pane)
  68. {
  69. SuspendLayout();
  70. Font = SystemInformation.MenuFont;
  71. m_buttonClose = new InertButton(ImageCloseEnabled, ImageCloseDisabled);
  72. m_buttonAutoHide = new InertButton();
  73. m_buttonClose.ToolTipText = ToolTipClose;
  74. m_buttonClose.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  75. m_buttonClose.Click += new EventHandler(this.Close_Click);
  76. m_buttonAutoHide.ToolTipText = ToolTipAutoHide;
  77. m_buttonAutoHide.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  78. m_buttonAutoHide.Click += new EventHandler(AutoHide_Click);
  79. m_buttonWhatIsIt = new InertButton();
  80. m_buttonWhatIsIt.BackColor = ColorMixer.LightColor;
  81. m_buttonWhatIsIt.ForeColor = ColorMixer.DarkColor;
  82. m_buttonWhatIsIt.ToolTipText = "What is this?";
  83. m_buttonWhatIsIt.Text = "?";
  84. m_buttonWhatIsIt.Anchor = AnchorStyles.Top | AnchorStyles.Right;
  85. m_buttonWhatIsIt.Click+=new EventHandler(m_buttonWhatIsIt_Click);
  86. Controls.AddRange(new Control[] { m_buttonWhatIsIt, m_buttonClose, m_buttonAutoHide });
  87. ResumeLayout();
  88. }
  89. #region Customizable Properties
  90. protected virtual int TextGapTop
  91. {
  92. get { return _TextGapTop; }
  93. }
  94. protected virtual int TextGapBottom
  95. {
  96. get { return _TextGapBottom; }
  97. }
  98. protected virtual int TextGapLeft
  99. {
  100. get { return _TextGapLeft; }
  101. }
  102. protected virtual int TextGapRight
  103. {
  104. get { return _TextGapRight; }
  105. }
  106. protected virtual int ButtonGapTop
  107. {
  108. get { return _ButtonGapTop; }
  109. }
  110. protected virtual int ButtonGapBottom
  111. {
  112. get { return _ButtonGapBottom; }
  113. }
  114. protected virtual int ButtonGapLeft
  115. {
  116. get { return _ButtonGapLeft; }
  117. }
  118. protected virtual int ButtonGapRight
  119. {
  120. get { return _ButtonGapRight; }
  121. }
  122. protected virtual int ButtonGapBetween
  123. {
  124. get { return _ButtonGapBetween; }
  125. }
  126. private static Image _imageCloseEnabled = null;
  127. protected virtual Image ImageCloseEnabled
  128. {
  129. get
  130. {
  131. if (_imageCloseEnabled == null)
  132. _imageCloseEnabled = ResourceHelper.LoadBitmap(_ResourceImageCloseEnabled);
  133. return _imageCloseEnabled;
  134. }
  135. }
  136. private static Image _imageCloseDisabled = null;
  137. protected virtual Image ImageCloseDisabled
  138. {
  139. get
  140. {
  141. if (_imageCloseDisabled == null)
  142. _imageCloseDisabled = ResourceHelper.LoadBitmap(_ResourceImageCloseDisabled);
  143. return _imageCloseDisabled;
  144. }
  145. }
  146. private static Image _imageAutoHideYes = null;
  147. protected virtual Image ImageAutoHideYes
  148. {
  149. get
  150. {
  151. if (_imageAutoHideYes == null)
  152. _imageAutoHideYes = ResourceHelper.LoadBitmap(_ResourceImageAutoHideYes);
  153. return _imageAutoHideYes;
  154. }
  155. }
  156. private static Image _imageAutoHideNo = null;
  157. protected virtual Image ImageAutoHideNo
  158. {
  159. get
  160. {
  161. if (_imageAutoHideNo == null)
  162. _imageAutoHideNo = ResourceHelper.LoadBitmap(_ResourceImageAutoHideNo);
  163. return _imageAutoHideNo;
  164. }
  165. }
  166. private static string _toolTipClose = null;
  167. protected virtual string ToolTipClose
  168. {
  169. get
  170. {
  171. if (_toolTipClose == null)
  172. _toolTipClose = ResourceHelper.GetString(_ResourceToolTipClose);
  173. return _toolTipClose;
  174. }
  175. }
  176. private static string _toolTipAutoHide = null;
  177. protected virtual string ToolTipAutoHide
  178. {
  179. get
  180. {
  181. if (_toolTipAutoHide == null)
  182. _toolTipAutoHide = ResourceHelper.GetString(_ResourceToolTipAutoHide);
  183. return _toolTipAutoHide;
  184. }
  185. }
  186. protected virtual Color ActiveBackColor
  187. {
  188. get { return SystemColors.ActiveCaption; }
  189. }
  190. protected virtual Color InactiveBackColor
  191. {
  192. get { return SystemColors.Control; }
  193. }
  194. protected virtual Color ActiveTextColor
  195. {
  196. get { return SystemColors.ActiveCaptionText; }
  197. }
  198. protected virtual Color InactiveTextColor
  199. {
  200. get { return SystemColors.ControlText; }
  201. }
  202. protected virtual Color InactiveBorderColor
  203. {
  204. get { return SystemColors.GrayText; }
  205. }
  206. protected virtual Color ActiveButtonBorderColor
  207. {
  208. get { return ActiveTextColor; }
  209. }
  210. protected virtual Color InactiveButtonBorderColor
  211. {
  212. get { return Color.Empty; }
  213. }
  214. private static StringFormat _textStringFormat = null;
  215. protected virtual StringFormat TextStringFormat
  216. {
  217. get
  218. {
  219. if (_textStringFormat == null)
  220. {
  221. _textStringFormat = new StringFormat();
  222. _textStringFormat.Trimming = StringTrimming.EllipsisCharacter;
  223. _textStringFormat.LineAlignment = StringAlignment.Center;
  224. _textStringFormat.FormatFlags = StringFormatFlags.NoWrap;
  225. }
  226. return _textStringFormat;
  227. }
  228. }
  229. #endregion
  230. protected internal override int MeasureHeight()
  231. {
  232. int height = Font.Height + TextGapTop + TextGapBottom;
  233. if (height < ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom)
  234. height = ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom;
  235. return height;
  236. }
  237. protected override void OnPaint(PaintEventArgs e)
  238. {
  239. base.OnPaint (e);
  240. if (DockPane.IsActivated)
  241. {
  242. using (captionBrush = new LinearGradientBrush(ClientRectangle, ColorMixer.DarkColor, ColorMixer.LightColor, LinearGradientMode.Horizontal))
  243. {
  244. e.Graphics.FillRectangle(captionBrush, ClientRectangle);
  245. }
  246. }
  247. DrawCaption(e.Graphics);
  248. }
  249. private void DrawCaption(Graphics g)
  250. {
  251. BackColor = DockPane.IsActivated ? ActiveBackColor : InactiveBackColor;
  252. Rectangle rectCaption = ClientRectangle;
  253. if (!DockPane.IsActivated)
  254. {
  255. using (Pen pen = new Pen(InactiveBorderColor))
  256. {
  257. g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y, rectCaption.X + rectCaption.Width - 2, rectCaption.Y);
  258. g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y + rectCaption.Height - 1, rectCaption.X + rectCaption.Width - 2, rectCaption.Y + rectCaption.Height - 1);
  259. g.DrawLine(pen, rectCaption.X, rectCaption.Y + 1, rectCaption.X, rectCaption.Y + rectCaption.Height - 2);
  260. g.DrawLine(pen, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + 1, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + rectCaption.Height - 2);
  261. }
  262. }
  263. m_buttonClose.ForeColor = m_buttonAutoHide.ForeColor = (DockPane.IsActivated ? ColorMixer.DarkColor : InactiveTextColor);
  264. m_buttonClose.BorderColor = m_buttonAutoHide.BorderColor = (DockPane.IsActivated ? ColorMixer.DarkColor : InactiveButtonBorderColor);
  265. m_buttonClose.BackColor = m_buttonAutoHide.BackColor = (DockPane.IsActivated ? ColorMixer.LightColor : InactiveButtonBorderColor);
  266. Rectangle rectCaptionText = rectCaption;
  267. rectCaptionText.X += TextGapLeft;
  268. rectCaptionText.Width = rectCaption.Width - ButtonGapRight
  269. - ButtonGapLeft
  270. - ButtonGapBetween - 2 * m_buttonClose.Width
  271. - TextGapLeft - TextGapRight;
  272. rectCaptionText.Y += TextGapTop;
  273. rectCaptionText.Height -= TextGapTop + TextGapBottom;
  274. using (Brush brush = new SolidBrush(DockPane.IsActivated ? ColorMixer.TabSelectedTextColor : InactiveTextColor))
  275. {
  276. g.DrawString(DockPane.CaptionText, Font, brush, rectCaptionText, TextStringFormat);
  277. }
  278. }
  279. protected override void OnLayout(LayoutEventArgs levent)
  280. {
  281. //SetBrush();
  282. // set the size and location for close and auto-hide buttons
  283. Rectangle rectCaption = ClientRectangle;
  284. int buttonWidth = ImageCloseEnabled.Width;
  285. int buttonHeight = ImageCloseEnabled.Height;
  286. int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom;
  287. if (buttonHeight < height)
  288. {
  289. buttonWidth = buttonWidth * (height / buttonHeight);
  290. buttonHeight = height;
  291. }
  292. m_buttonClose.SuspendLayout();
  293. m_buttonAutoHide.SuspendLayout();
  294. m_buttonWhatIsIt.SuspendLayout();
  295. Size buttonSize = new Size(buttonWidth, buttonHeight);
  296. m_buttonClose.Size = m_buttonAutoHide.Size = m_buttonWhatIsIt.Size = buttonSize;
  297. int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width;
  298. int y = rectCaption.Y + ButtonGapTop;
  299. Point point = m_buttonClose.Location = new Point(x, y);
  300. point.Offset(-(m_buttonAutoHide.Width + ButtonGapBetween), 0);
  301. m_buttonAutoHide.Location = point;
  302. point.Offset(-(m_buttonWhatIsIt.Width + ButtonGapBetween), 0);
  303. m_buttonWhatIsIt.Location = point;
  304. m_buttonClose.ResumeLayout();
  305. m_buttonAutoHide.ResumeLayout();
  306. base.OnLayout (levent);
  307. }
  308. protected override void OnRefreshChanges()
  309. {
  310. SetButtons();
  311. Invalidate();
  312. }
  313. private void SetButtons()
  314. {
  315. m_buttonClose.Enabled = (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false;
  316. m_buttonAutoHide.Visible = !DockPane.IsFloat;
  317. m_buttonAutoHide.ImageEnabled = DockPane.IsAutoHide ? ImageAutoHideYes : ImageAutoHideNo;
  318. }
  319. private void Close_Click(object sender, EventArgs e)
  320. {
  321. DockPane.CloseActiveContent();
  322. }
  323. private void AutoHide_Click(object sender, EventArgs e)
  324. {
  325. DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState);
  326. if (!DockPane.IsAutoHide)
  327. DockPane.Activate();
  328. }
  329. private void m_buttonWhatIsIt_Click(object sender, EventArgs e)
  330. {
  331. //if( DockPane.ActiveContent != null && DockPane.ActiveContent.DockHandler.AccessibleDescription !=null)
  332. // MessageBox.Show(DockPane.ActiveContent.AccessibleDescription,"What is this?", MessageBoxButtons.OK, MessageBoxIcon.Information);
  333. }
  334. }
  335. }