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