1 | using System;
|
---|
2 | using System.Drawing;
|
---|
3 | using System.Drawing.Drawing2D;
|
---|
4 | using System.Windows.Forms;
|
---|
5 | using System.ComponentModel;
|
---|
6 | using System.Windows.Forms.VisualStyles;
|
---|
7 |
|
---|
8 | namespace WeifenLuo.WinFormsUI.Docking
|
---|
9 | {
|
---|
10 | internal class VS2005DockPaneCaption : DockPaneCaptionBase
|
---|
11 | {
|
---|
12 | private sealed class InertButton : InertButtonBase
|
---|
13 | {
|
---|
14 | private Bitmap m_image, m_imageAutoHide;
|
---|
15 |
|
---|
16 | public InertButton(VS2005DockPaneCaption dockPaneCaption, Bitmap image, Bitmap imageAutoHide)
|
---|
17 | : base()
|
---|
18 | {
|
---|
19 | m_dockPaneCaption = dockPaneCaption;
|
---|
20 | m_image = image;
|
---|
21 | m_imageAutoHide = imageAutoHide;
|
---|
22 | RefreshChanges();
|
---|
23 | }
|
---|
24 |
|
---|
25 | private VS2005DockPaneCaption m_dockPaneCaption;
|
---|
26 | private VS2005DockPaneCaption DockPaneCaption
|
---|
27 | {
|
---|
28 | get { return m_dockPaneCaption; }
|
---|
29 | }
|
---|
30 |
|
---|
31 | public bool IsAutoHide
|
---|
32 | {
|
---|
33 | get { return DockPaneCaption.DockPane.IsAutoHide; }
|
---|
34 | }
|
---|
35 |
|
---|
36 | public override Bitmap Image
|
---|
37 | {
|
---|
38 | get { return IsAutoHide ? m_imageAutoHide : m_image; }
|
---|
39 | }
|
---|
40 |
|
---|
41 | protected override void OnRefreshChanges()
|
---|
42 | {
|
---|
43 | if (DockPaneCaption.DockPane.DockPanel != null)
|
---|
44 | {
|
---|
45 | if (DockPaneCaption.TextColor != ForeColor)
|
---|
46 | {
|
---|
47 | ForeColor = DockPaneCaption.TextColor;
|
---|
48 | Invalidate();
|
---|
49 | }
|
---|
50 | }
|
---|
51 | }
|
---|
52 | }
|
---|
53 |
|
---|
54 | #region consts
|
---|
55 | private const int _TextGapTop = 2;
|
---|
56 | private const int _TextGapBottom = 0;
|
---|
57 | private const int _TextGapLeft = 3;
|
---|
58 | private const int _TextGapRight = 3;
|
---|
59 | private const int _ButtonGapTop = 2;
|
---|
60 | private const int _ButtonGapBottom = 1;
|
---|
61 | private const int _ButtonGapBetween = 1;
|
---|
62 | private const int _ButtonGapLeft = 1;
|
---|
63 | private const int _ButtonGapRight = 2;
|
---|
64 | #endregion
|
---|
65 |
|
---|
66 | private static Bitmap _imageButtonClose;
|
---|
67 | private static Bitmap ImageButtonClose
|
---|
68 | {
|
---|
69 | get
|
---|
70 | {
|
---|
71 | if (_imageButtonClose == null)
|
---|
72 | _imageButtonClose = Resources.DockPane_Close;
|
---|
73 |
|
---|
74 | return _imageButtonClose;
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | private InertButton m_buttonClose;
|
---|
79 | private InertButton ButtonClose
|
---|
80 | {
|
---|
81 | get
|
---|
82 | {
|
---|
83 | if (m_buttonClose == null)
|
---|
84 | {
|
---|
85 | m_buttonClose = new InertButton(this, ImageButtonClose, ImageButtonClose);
|
---|
86 | m_toolTip.SetToolTip(m_buttonClose, ToolTipClose);
|
---|
87 | m_buttonClose.Click += new EventHandler(Close_Click);
|
---|
88 | Controls.Add(m_buttonClose);
|
---|
89 | }
|
---|
90 |
|
---|
91 | return m_buttonClose;
|
---|
92 | }
|
---|
93 | }
|
---|
94 |
|
---|
95 | private static Bitmap _imageButtonAutoHide;
|
---|
96 | private static Bitmap ImageButtonAutoHide
|
---|
97 | {
|
---|
98 | get
|
---|
99 | {
|
---|
100 | if (_imageButtonAutoHide == null)
|
---|
101 | _imageButtonAutoHide = Resources.DockPane_AutoHide;
|
---|
102 |
|
---|
103 | return _imageButtonAutoHide;
|
---|
104 | }
|
---|
105 | }
|
---|
106 |
|
---|
107 | private static Bitmap _imageButtonDock;
|
---|
108 | private static Bitmap ImageButtonDock
|
---|
109 | {
|
---|
110 | get
|
---|
111 | {
|
---|
112 | if (_imageButtonDock == null)
|
---|
113 | _imageButtonDock = Resources.DockPane_Dock;
|
---|
114 |
|
---|
115 | return _imageButtonDock;
|
---|
116 | }
|
---|
117 | }
|
---|
118 |
|
---|
119 | private InertButton m_buttonAutoHide;
|
---|
120 | private InertButton ButtonAutoHide
|
---|
121 | {
|
---|
122 | get
|
---|
123 | {
|
---|
124 | if (m_buttonAutoHide == null)
|
---|
125 | {
|
---|
126 | m_buttonAutoHide = new InertButton(this, ImageButtonDock, ImageButtonAutoHide);
|
---|
127 | m_toolTip.SetToolTip(m_buttonAutoHide, ToolTipAutoHide);
|
---|
128 | m_buttonAutoHide.Click += new EventHandler(AutoHide_Click);
|
---|
129 | Controls.Add(m_buttonAutoHide);
|
---|
130 | }
|
---|
131 |
|
---|
132 | return m_buttonAutoHide;
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private static Bitmap _imageButtonOptions;
|
---|
137 | private static Bitmap ImageButtonOptions
|
---|
138 | {
|
---|
139 | get
|
---|
140 | {
|
---|
141 | if (_imageButtonOptions == null)
|
---|
142 | _imageButtonOptions = Resources.DockPane_Option;
|
---|
143 |
|
---|
144 | return _imageButtonOptions;
|
---|
145 | }
|
---|
146 | }
|
---|
147 |
|
---|
148 | private InertButton m_buttonOptions;
|
---|
149 | private InertButton ButtonOptions
|
---|
150 | {
|
---|
151 | get
|
---|
152 | {
|
---|
153 | if (m_buttonOptions == null)
|
---|
154 | {
|
---|
155 | m_buttonOptions = new InertButton(this, ImageButtonOptions, ImageButtonOptions);
|
---|
156 | m_toolTip.SetToolTip(m_buttonOptions, ToolTipOptions);
|
---|
157 | m_buttonOptions.Click += new EventHandler(Options_Click);
|
---|
158 | Controls.Add(m_buttonOptions);
|
---|
159 | }
|
---|
160 | return m_buttonOptions;
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 | private IContainer m_components;
|
---|
165 | private IContainer Components
|
---|
166 | {
|
---|
167 | get { return m_components; }
|
---|
168 | }
|
---|
169 |
|
---|
170 | private ToolTip m_toolTip;
|
---|
171 |
|
---|
172 | public VS2005DockPaneCaption(DockPane pane) : base(pane)
|
---|
173 | {
|
---|
174 | SuspendLayout();
|
---|
175 |
|
---|
176 | m_components = new Container();
|
---|
177 | m_toolTip = new ToolTip(Components);
|
---|
178 |
|
---|
179 | ResumeLayout();
|
---|
180 | }
|
---|
181 |
|
---|
182 | protected override void Dispose(bool disposing)
|
---|
183 | {
|
---|
184 | if (disposing)
|
---|
185 | Components.Dispose();
|
---|
186 | base.Dispose(disposing);
|
---|
187 | }
|
---|
188 |
|
---|
189 | private static int TextGapTop
|
---|
190 | {
|
---|
191 | get { return _TextGapTop; }
|
---|
192 | }
|
---|
193 |
|
---|
194 | private static Font TextFont
|
---|
195 | {
|
---|
196 | get { return SystemInformation.MenuFont; }
|
---|
197 | }
|
---|
198 |
|
---|
199 | private static int TextGapBottom
|
---|
200 | {
|
---|
201 | get { return _TextGapBottom; }
|
---|
202 | }
|
---|
203 |
|
---|
204 | private static int TextGapLeft
|
---|
205 | {
|
---|
206 | get { return _TextGapLeft; }
|
---|
207 | }
|
---|
208 |
|
---|
209 | private static int TextGapRight
|
---|
210 | {
|
---|
211 | get { return _TextGapRight; }
|
---|
212 | }
|
---|
213 |
|
---|
214 | private static int ButtonGapTop
|
---|
215 | {
|
---|
216 | get { return _ButtonGapTop; }
|
---|
217 | }
|
---|
218 |
|
---|
219 | private static int ButtonGapBottom
|
---|
220 | {
|
---|
221 | get { return _ButtonGapBottom; }
|
---|
222 | }
|
---|
223 |
|
---|
224 | private static int ButtonGapLeft
|
---|
225 | {
|
---|
226 | get { return _ButtonGapLeft; }
|
---|
227 | }
|
---|
228 |
|
---|
229 | private static int ButtonGapRight
|
---|
230 | {
|
---|
231 | get { return _ButtonGapRight; }
|
---|
232 | }
|
---|
233 |
|
---|
234 | private static int ButtonGapBetween
|
---|
235 | {
|
---|
236 | get { return _ButtonGapBetween; }
|
---|
237 | }
|
---|
238 |
|
---|
239 | private static string _toolTipClose;
|
---|
240 | private static string ToolTipClose
|
---|
241 | {
|
---|
242 | get
|
---|
243 | {
|
---|
244 | if (_toolTipClose == null)
|
---|
245 | _toolTipClose = Strings.DockPaneCaption_ToolTipClose;
|
---|
246 | return _toolTipClose;
|
---|
247 | }
|
---|
248 | }
|
---|
249 |
|
---|
250 | private static string _toolTipOptions;
|
---|
251 | private static string ToolTipOptions
|
---|
252 | {
|
---|
253 | get
|
---|
254 | {
|
---|
255 | if (_toolTipOptions == null)
|
---|
256 | _toolTipOptions = Strings.DockPaneCaption_ToolTipOptions;
|
---|
257 |
|
---|
258 | return _toolTipOptions;
|
---|
259 | }
|
---|
260 | }
|
---|
261 |
|
---|
262 | private static string _toolTipAutoHide;
|
---|
263 | private static string ToolTipAutoHide
|
---|
264 | {
|
---|
265 | get
|
---|
266 | {
|
---|
267 | if (_toolTipAutoHide == null)
|
---|
268 | _toolTipAutoHide = Strings.DockPaneCaption_ToolTipAutoHide;
|
---|
269 | return _toolTipAutoHide;
|
---|
270 | }
|
---|
271 | }
|
---|
272 |
|
---|
273 | private static Blend _activeBackColorGradientBlend;
|
---|
274 | private static Blend ActiveBackColorGradientBlend
|
---|
275 | {
|
---|
276 | get
|
---|
277 | {
|
---|
278 | if (_activeBackColorGradientBlend == null)
|
---|
279 | {
|
---|
280 | Blend blend = new Blend(2);
|
---|
281 |
|
---|
282 | blend.Factors = new float[]{0.5F, 1.0F};
|
---|
283 | blend.Positions = new float[]{0.0F, 1.0F};
|
---|
284 | _activeBackColorGradientBlend = blend;
|
---|
285 | }
|
---|
286 |
|
---|
287 | return _activeBackColorGradientBlend;
|
---|
288 | }
|
---|
289 | }
|
---|
290 |
|
---|
291 | private Color TextColor
|
---|
292 | {
|
---|
293 | get
|
---|
294 | {
|
---|
295 | if (DockPane.IsActivated)
|
---|
296 | return DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.TextColor;
|
---|
297 | else
|
---|
298 | return DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.TextColor;
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | private static TextFormatFlags _textFormat =
|
---|
303 | TextFormatFlags.SingleLine |
|
---|
304 | TextFormatFlags.EndEllipsis |
|
---|
305 | TextFormatFlags.VerticalCenter;
|
---|
306 | private TextFormatFlags TextFormat
|
---|
307 | {
|
---|
308 | get
|
---|
309 | {
|
---|
310 | if (RightToLeft == RightToLeft.No)
|
---|
311 | return _textFormat;
|
---|
312 | else
|
---|
313 | return _textFormat | TextFormatFlags.RightToLeft | TextFormatFlags.Right;
|
---|
314 | }
|
---|
315 | }
|
---|
316 |
|
---|
317 | protected internal override int MeasureHeight()
|
---|
318 | {
|
---|
319 | int height = TextFont.Height + TextGapTop + TextGapBottom;
|
---|
320 |
|
---|
321 | if (height < ButtonClose.Image.Height + ButtonGapTop + ButtonGapBottom)
|
---|
322 | height = ButtonClose.Image.Height + ButtonGapTop + ButtonGapBottom;
|
---|
323 |
|
---|
324 | return height;
|
---|
325 | }
|
---|
326 |
|
---|
327 | protected override void OnPaint(PaintEventArgs e)
|
---|
328 | {
|
---|
329 | base.OnPaint (e);
|
---|
330 | DrawCaption(e.Graphics);
|
---|
331 | }
|
---|
332 |
|
---|
333 | private void DrawCaption(Graphics g)
|
---|
334 | {
|
---|
335 | if (ClientRectangle.Width == 0 || ClientRectangle.Height == 0)
|
---|
336 | return;
|
---|
337 |
|
---|
338 | if (DockPane.IsActivated)
|
---|
339 | {
|
---|
340 | Color startColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.StartColor;
|
---|
341 | Color endColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.EndColor;
|
---|
342 | LinearGradientMode gradientMode = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.LinearGradientMode;
|
---|
343 | using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, startColor, endColor, gradientMode))
|
---|
344 | {
|
---|
345 | brush.Blend = ActiveBackColorGradientBlend;
|
---|
346 | g.FillRectangle(brush, ClientRectangle);
|
---|
347 | }
|
---|
348 | }
|
---|
349 | else
|
---|
350 | {
|
---|
351 | Color startColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.StartColor;
|
---|
352 | Color endColor = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.EndColor;
|
---|
353 | LinearGradientMode gradientMode = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.LinearGradientMode;
|
---|
354 | using (LinearGradientBrush brush = new LinearGradientBrush(ClientRectangle, startColor, endColor, gradientMode))
|
---|
355 | {
|
---|
356 | g.FillRectangle(brush, ClientRectangle);
|
---|
357 | }
|
---|
358 | }
|
---|
359 |
|
---|
360 | Rectangle rectCaption = ClientRectangle;
|
---|
361 |
|
---|
362 | Rectangle rectCaptionText = rectCaption;
|
---|
363 | rectCaptionText.X += TextGapLeft;
|
---|
364 | rectCaptionText.Width -= TextGapLeft + TextGapRight;
|
---|
365 | rectCaptionText.Width -= ButtonGapLeft + ButtonClose.Width + ButtonGapRight;
|
---|
366 | if (ShouldShowAutoHideButton)
|
---|
367 | rectCaptionText.Width -= ButtonAutoHide.Width + ButtonGapBetween;
|
---|
368 | if (HasTabPageContextMenu)
|
---|
369 | rectCaptionText.Width -= ButtonOptions.Width + ButtonGapBetween;
|
---|
370 | rectCaptionText.Y += TextGapTop;
|
---|
371 | rectCaptionText.Height -= TextGapTop + TextGapBottom;
|
---|
372 |
|
---|
373 | Color colorText;
|
---|
374 | if (DockPane.IsActivated)
|
---|
375 | colorText = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.ActiveCaptionGradient.TextColor;
|
---|
376 | else
|
---|
377 | colorText = DockPane.DockPanel.Skin.DockPaneStripSkin.ToolWindowGradient.InactiveCaptionGradient.TextColor;
|
---|
378 |
|
---|
379 | TextRenderer.DrawText(g, DockPane.CaptionText, TextFont, DrawHelper.RtlTransform(this, rectCaptionText), colorText, TextFormat);
|
---|
380 | }
|
---|
381 |
|
---|
382 | protected override void OnLayout(LayoutEventArgs levent)
|
---|
383 | {
|
---|
384 | SetButtonsPosition();
|
---|
385 | base.OnLayout (levent);
|
---|
386 | }
|
---|
387 |
|
---|
388 | protected override void OnRefreshChanges()
|
---|
389 | {
|
---|
390 | SetButtons();
|
---|
391 | Invalidate();
|
---|
392 | }
|
---|
393 |
|
---|
394 | private bool CloseButtonEnabled
|
---|
395 | {
|
---|
396 | get { return (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false; }
|
---|
397 | }
|
---|
398 |
|
---|
399 | /// <summary>
|
---|
400 | /// Determines whether the close button is visible on the content
|
---|
401 | /// </summary>
|
---|
402 | private bool CloseButtonVisible
|
---|
403 | {
|
---|
404 | get { return (DockPane.ActiveContent != null) ? DockPane.ActiveContent.DockHandler.CloseButtonVisible : false; }
|
---|
405 | }
|
---|
406 |
|
---|
407 | private bool ShouldShowAutoHideButton
|
---|
408 | {
|
---|
409 | get { return !DockPane.IsFloat; }
|
---|
410 | }
|
---|
411 |
|
---|
412 | private void SetButtons()
|
---|
413 | {
|
---|
414 | ButtonClose.Enabled = CloseButtonEnabled;
|
---|
415 | ButtonClose.Visible = CloseButtonVisible;
|
---|
416 | ButtonAutoHide.Visible = ShouldShowAutoHideButton;
|
---|
417 | ButtonOptions.Visible = HasTabPageContextMenu;
|
---|
418 | ButtonClose.RefreshChanges();
|
---|
419 | ButtonAutoHide.RefreshChanges();
|
---|
420 | ButtonOptions.RefreshChanges();
|
---|
421 |
|
---|
422 | SetButtonsPosition();
|
---|
423 | }
|
---|
424 |
|
---|
425 | private void SetButtonsPosition()
|
---|
426 | {
|
---|
427 | // set the size and location for close and auto-hide buttons
|
---|
428 | Rectangle rectCaption = ClientRectangle;
|
---|
429 | int buttonWidth = ButtonClose.Image.Width;
|
---|
430 | int buttonHeight = ButtonClose.Image.Height;
|
---|
431 | int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom;
|
---|
432 | if (buttonHeight < height)
|
---|
433 | {
|
---|
434 | buttonWidth = buttonWidth * (height / buttonHeight);
|
---|
435 | buttonHeight = height;
|
---|
436 | }
|
---|
437 | Size buttonSize = new Size(buttonWidth, buttonHeight);
|
---|
438 | int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width;
|
---|
439 | int y = rectCaption.Y + ButtonGapTop;
|
---|
440 | Point point = new Point(x, y);
|
---|
441 | ButtonClose.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
|
---|
442 |
|
---|
443 | // If the close button is not visible draw the auto hide button overtop.
|
---|
444 | // Otherwise it is drawn to the left of the close button.
|
---|
445 | if (CloseButtonVisible)
|
---|
446 | point.Offset(-(buttonWidth + ButtonGapBetween), 0);
|
---|
447 |
|
---|
448 | ButtonAutoHide.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
|
---|
449 | if (ShouldShowAutoHideButton)
|
---|
450 | point.Offset(-(buttonWidth + ButtonGapBetween), 0);
|
---|
451 | ButtonOptions.Bounds = DrawHelper.RtlTransform(this, new Rectangle(point, buttonSize));
|
---|
452 | }
|
---|
453 |
|
---|
454 | private void Close_Click(object sender, EventArgs e)
|
---|
455 | {
|
---|
456 | DockPane.CloseActiveContent();
|
---|
457 | }
|
---|
458 |
|
---|
459 | private void AutoHide_Click(object sender, EventArgs e)
|
---|
460 | {
|
---|
461 | DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState);
|
---|
462 | if (DockHelper.IsDockStateAutoHide(DockPane.DockState))
|
---|
463 | DockPane.DockPanel.ActiveAutoHideContent = null;
|
---|
464 |
|
---|
465 | }
|
---|
466 |
|
---|
467 | private void Options_Click(object sender, EventArgs e)
|
---|
468 | {
|
---|
469 | ShowTabPageContextMenu(PointToClient(Control.MousePosition));
|
---|
470 | }
|
---|
471 |
|
---|
472 | protected override void OnRightToLeftChanged(EventArgs e)
|
---|
473 | {
|
---|
474 | base.OnRightToLeftChanged(e);
|
---|
475 | PerformLayout();
|
---|
476 | }
|
---|
477 | }
|
---|
478 | }
|
---|