1 using System; 2 using System.Drawing; 3 using System.Windows.Forms; 4 5 namespace WeifenLuo.WinFormsUI.Docking 6 { 7 using WeifenLuo.WinFormsUI.ThemeVS2003; 8 9 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/ClassDef/*'/> 10 internal class VS2003DockPaneCaption : 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 #endregion 23 24 private InertButton m_buttonClose; 25 private InertButton m_buttonAutoHide; 26 27 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Construct[@name="(DockPane)"]/*'/> VS2003DockPaneCaption(DockPane pane)28 protected internal VS2003DockPaneCaption(DockPane pane) : base(pane) 29 { 30 SuspendLayout(); 31 32 Font = SystemInformation.MenuFont; 33 34 m_buttonClose = new InertButton(ImageCloseEnabled, ImageCloseDisabled); 35 m_buttonAutoHide = new InertButton(); 36 37 m_buttonClose.ToolTipText = ToolTipClose; 38 m_buttonClose.Anchor = AnchorStyles.Top | AnchorStyles.Right; 39 m_buttonClose.Click += new EventHandler(this.Close_Click); 40 41 m_buttonAutoHide.ToolTipText = ToolTipAutoHide; 42 m_buttonAutoHide.Anchor = AnchorStyles.Top | AnchorStyles.Right; 43 m_buttonAutoHide.Click += new EventHandler(AutoHide_Click); 44 45 Controls.AddRange(new Control[] { m_buttonClose, m_buttonAutoHide }); 46 47 ResumeLayout(); 48 } 49 50 #region Customizable Properties 51 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapTop"]/*'/> 52 protected virtual int TextGapTop 53 { 54 get { return _TextGapTop; } 55 } 56 57 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapBottom"]/*'/> 58 protected virtual int TextGapBottom 59 { 60 get { return _TextGapBottom; } 61 } 62 63 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapLeft"]/*'/> 64 protected virtual int TextGapLeft 65 { 66 get { return _TextGapLeft; } 67 } 68 69 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextGapRight"]/*'/> 70 protected virtual int TextGapRight 71 { 72 get { return _TextGapRight; } 73 } 74 75 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapTop"]/*'/> 76 protected virtual int ButtonGapTop 77 { 78 get { return _ButtonGapTop; } 79 } 80 81 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapBottom"]/*'/> 82 protected virtual int ButtonGapBottom 83 { 84 get { return _ButtonGapBottom; } 85 } 86 87 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapLeft"]/*'/> 88 protected virtual int ButtonGapLeft 89 { 90 get { return _ButtonGapLeft; } 91 } 92 93 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapRight"]/*'/> 94 protected virtual int ButtonGapRight 95 { 96 get { return _ButtonGapRight; } 97 } 98 99 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ButtonGapBetween"]/*'/> 100 protected virtual int ButtonGapBetween 101 { 102 get { return _ButtonGapBetween; } 103 } 104 105 private static Image _imageCloseEnabled = null; 106 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageCloseEnabled"]/*'/> 107 protected virtual Image ImageCloseEnabled 108 { 109 get 110 { 111 if (_imageCloseEnabled == null) 112 _imageCloseEnabled = Resources.DockPaneCaption_CloseEnabled; 113 return _imageCloseEnabled; 114 } 115 } 116 117 private static Image _imageCloseDisabled = null; 118 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageCloseDisabled"]/*'/> 119 protected virtual Image ImageCloseDisabled 120 { 121 get 122 { 123 if (_imageCloseDisabled == null) 124 _imageCloseDisabled = Resources.DockPaneCaption_CloseDisabled; 125 return _imageCloseDisabled; 126 } 127 } 128 129 private static Image _imageAutoHideYes = null; 130 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageAutoHideYes"]/*'/> 131 protected virtual Image ImageAutoHideYes 132 { 133 get 134 { 135 if (_imageAutoHideYes == null) 136 _imageAutoHideYes = Resources.DockPaneCaption_AutoHideYes; 137 return _imageAutoHideYes; 138 } 139 } 140 141 private static Image _imageAutoHideNo = null; 142 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ImageAutoHideNo"]/*'/> 143 protected virtual Image ImageAutoHideNo 144 { 145 get 146 { 147 if (_imageAutoHideNo == null) 148 _imageAutoHideNo = Resources.DockPaneCaption_AutoHideNo; 149 return _imageAutoHideNo; 150 } 151 } 152 153 private static string _toolTipClose = null; 154 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ToolTipClose"]/*'/> 155 protected virtual string ToolTipClose 156 { 157 get 158 { 159 if (_toolTipClose == null) 160 _toolTipClose = Strings.DockPaneCaption_ToolTipClose; 161 return _toolTipClose; 162 } 163 } 164 165 private static string _toolTipAutoHide = null; 166 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ToolTipAutoHide"]/*'/> 167 protected virtual string ToolTipAutoHide 168 { 169 get 170 { 171 if (_toolTipAutoHide == null) 172 _toolTipAutoHide = Strings.DockPaneCaption_ToolTipAutoHide; 173 return _toolTipAutoHide; 174 } 175 } 176 177 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveBackColor"]/*'/> 178 protected virtual Color ActiveBackColor 179 { 180 get { return SystemColors.ActiveCaption; } 181 } 182 183 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveBackColor"]/*'/> 184 protected virtual Color InactiveBackColor 185 { 186 get { return SystemColors.Control; } 187 } 188 189 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveTextColor"]/*'/> 190 protected virtual Color ActiveTextColor 191 { 192 get { return SystemColors.ActiveCaptionText; } 193 } 194 195 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveTextColor"]/*'/> 196 protected virtual Color InactiveTextColor 197 { 198 get { return SystemColors.ControlText; } 199 } 200 201 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveBorderColor"]/*'/> 202 protected virtual Color InactiveBorderColor 203 { 204 get { return SystemColors.GrayText; } 205 } 206 207 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="ActiveButtonBorderColor"]/*'/> 208 protected virtual Color ActiveButtonBorderColor 209 { 210 get { return ActiveTextColor; } 211 } 212 213 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="InactiveButtonBorderColor"]/*'/> 214 protected virtual Color InactiveButtonBorderColor 215 { 216 get { return Color.Empty; } 217 } 218 219 private static TextFormatFlags _textFormat = 220 TextFormatFlags.SingleLine | 221 TextFormatFlags.EndEllipsis | 222 TextFormatFlags.VerticalCenter; 223 /// <include file='CodeDoc/DockPaneCaptionVS2003.xml' path='//CodeDoc/Class[@name="DockPaneCaptionVS2003"]/Property[@name="TextStringFormat"]/*'/> 224 protected virtual TextFormatFlags TextFormat 225 { 226 get 227 { 228 return _textFormat; 229 } 230 } 231 232 #endregion 233 234 /// <exclude/> MeasureHeight()235 protected override int MeasureHeight() 236 { 237 int height = Font.Height + TextGapTop + TextGapBottom; 238 239 if (height < ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom) 240 height = ImageCloseEnabled.Height + ButtonGapTop + ButtonGapBottom; 241 242 return height; 243 } 244 245 /// <exclude/> OnPaint(PaintEventArgs e)246 protected override void OnPaint(PaintEventArgs e) 247 { 248 base.OnPaint (e); 249 DrawCaption(e.Graphics); 250 } 251 DrawCaption(Graphics g)252 private void DrawCaption(Graphics g) 253 { 254 BackColor = DockPane.IsActivated ? ActiveBackColor : InactiveBackColor; 255 256 Rectangle rectCaption = ClientRectangle; 257 258 if (!DockPane.IsActivated) 259 { 260 using (Pen pen = new Pen(InactiveBorderColor)) 261 { 262 g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y, rectCaption.X + rectCaption.Width - 2, rectCaption.Y); 263 g.DrawLine(pen, rectCaption.X + 1, rectCaption.Y + rectCaption.Height - 1, rectCaption.X + rectCaption.Width - 2, rectCaption.Y + rectCaption.Height - 1); 264 g.DrawLine(pen, rectCaption.X, rectCaption.Y + 1, rectCaption.X, rectCaption.Y + rectCaption.Height - 2); 265 g.DrawLine(pen, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + 1, rectCaption.X + rectCaption.Width - 1, rectCaption.Y + rectCaption.Height - 2); 266 } 267 } 268 269 m_buttonClose.ForeColor = m_buttonAutoHide.ForeColor = (DockPane.IsActivated ? ActiveTextColor : InactiveTextColor); 270 m_buttonClose.BorderColor = m_buttonAutoHide.BorderColor = (DockPane.IsActivated ? ActiveButtonBorderColor : InactiveButtonBorderColor); 271 272 Rectangle rectCaptionText = rectCaption; 273 rectCaptionText.X += TextGapLeft; 274 if (ShouldShowCloseButton && ShouldShowAutoHideButton) 275 rectCaptionText.Width = rectCaption.Width - ButtonGapRight 276 - ButtonGapLeft - TextGapLeft - TextGapRight - 277 (m_buttonAutoHide.Width + ButtonGapBetween + m_buttonClose.Width); 278 else if (ShouldShowCloseButton || ShouldShowAutoHideButton) 279 rectCaptionText.Width = rectCaption.Width - ButtonGapRight 280 - ButtonGapLeft - TextGapLeft - TextGapRight - m_buttonClose.Width; 281 else 282 rectCaptionText.Width = rectCaption.Width - TextGapLeft - TextGapRight; 283 rectCaptionText.Y += TextGapTop; 284 rectCaptionText.Height -= TextGapTop + TextGapBottom; 285 TextRenderer.DrawText(g, DockPane.CaptionText, Font, rectCaptionText, DockPane.IsActivated ? ActiveTextColor : InactiveTextColor, TextFormat); 286 } 287 288 /// <exclude/> OnLayout(LayoutEventArgs levent)289 protected override void OnLayout(LayoutEventArgs levent) 290 { 291 SetButtonsPosition(); 292 base.OnLayout (levent); 293 } 294 295 /// <exclude/> OnRefreshChanges()296 protected override void OnRefreshChanges() 297 { 298 SetButtons(); 299 Invalidate(); 300 } 301 302 private bool ShouldShowCloseButton 303 { 304 get { return (DockPane.ActiveContent != null)? DockPane.ActiveContent.DockHandler.CloseButton : false; } 305 } 306 307 private bool ShouldShowAutoHideButton 308 { 309 get { return !DockPane.IsFloat; } 310 } 311 SetButtons()312 private void SetButtons() 313 { 314 m_buttonClose.Visible = ShouldShowCloseButton; 315 m_buttonAutoHide.Visible = ShouldShowAutoHideButton; 316 m_buttonAutoHide.ImageEnabled = DockPane.IsAutoHide ? ImageAutoHideYes : ImageAutoHideNo; 317 318 SetButtonsPosition(); 319 } 320 SetButtonsPosition()321 private void SetButtonsPosition() 322 { 323 // set the size and location for close and auto-hide buttons 324 Rectangle rectCaption = ClientRectangle; 325 int buttonWidth = ImageCloseEnabled.Width; 326 int buttonHeight = ImageCloseEnabled.Height; 327 int height = rectCaption.Height - ButtonGapTop - ButtonGapBottom; 328 if (buttonHeight < height) 329 { 330 buttonWidth = buttonWidth * height / buttonHeight; 331 buttonHeight = height; 332 } 333 m_buttonClose.SuspendLayout(); 334 m_buttonAutoHide.SuspendLayout(); 335 Size buttonSize = new Size(buttonWidth, buttonHeight); 336 m_buttonClose.Size = m_buttonAutoHide.Size = buttonSize; 337 int x = rectCaption.X + rectCaption.Width - 1 - ButtonGapRight - m_buttonClose.Width; 338 int y = rectCaption.Y + ButtonGapTop; 339 Point point = m_buttonClose.Location = new Point(x, y); 340 if (ShouldShowCloseButton) 341 point.Offset(-(m_buttonAutoHide.Width + ButtonGapBetween), 0); 342 m_buttonAutoHide.Location = point; 343 m_buttonClose.ResumeLayout(); 344 m_buttonAutoHide.ResumeLayout(); 345 } 346 Close_Click(object sender, EventArgs e)347 private void Close_Click(object sender, EventArgs e) 348 { 349 DockPane.CloseActiveContent(); 350 } 351 AutoHide_Click(object sender, EventArgs e)352 private void AutoHide_Click(object sender, EventArgs e) 353 { 354 DockPane.DockState = DockHelper.ToggleAutoHideState(DockPane.DockState); 355 if (!DockPane.IsAutoHide) 356 DockPane.Activate(); 357 } 358 } 359 } 360