Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.WinFormsUI/2.3.1/WinFormsUI-2.3.1/Docking/VS2005DockPaneCaption.cs @ 4068

Last change on this file since 4068 was 4068, checked in by swagner, 14 years ago

Sorted usings and removed unused usings in entire solution (#1094)

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