2 // GroupBoxRenderer.cs
\r
4 // Permission is hereby granted, free of charge, to any person obtaining
\r
5 // a copy of this software and associated documentation files (the
\r
6 // "Software"), to deal in the Software without restriction, including
\r
7 // without limitation the rights to use, copy, modify, merge, publish,
\r
8 // distribute, sublicense, and/or sell copies of the Software, and to
\r
9 // permit persons to whom the Software is furnished to do so, subject to
\r
10 // the following conditions:
\r
12 // The above copyright notice and this permission notice shall be
\r
13 // included in all copies or substantial portions of the Software.
\r
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
\r
16 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
\r
17 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
\r
18 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
\r
19 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
\r
20 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
\r
21 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
23 // Copyright (c) 2006 Novell, Inc.
\r
26 // Jonathan Pobst (monkey@jpobst.com)
\r
30 using System.Drawing;
\r
31 using System.Windows.Forms.VisualStyles;
\r
33 namespace System.Windows.Forms
\r
35 public sealed class GroupBoxRenderer
\r
37 private static bool always_use_visual_styles = false;
\r
39 #region Public Static Methods
\r
40 public static void DrawGroupBox (Graphics g, Rectangle bounds, GroupBoxState state)
\r
42 DrawGroupBox (g, bounds, String.Empty, null, Color.Empty, TextFormatFlags.Default, state);
\r
45 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, GroupBoxState state)
\r
47 DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, TextFormatFlags.Default, state);
\r
50 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, GroupBoxState state)
\r
52 DrawGroupBox (g, bounds, groupBoxText, font, textColor, TextFormatFlags.Default, state);
\r
55 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, TextFormatFlags flags, GroupBoxState state)
\r
57 DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, flags, state);
\r
60 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)
\r
62 Size font_size = TextRenderer.MeasureText (groupBoxText, font);
\r
64 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
\r
65 VisualStyleRenderer vsr;
\r
66 Rectangle new_bounds;
\r
69 case GroupBoxState.Normal:
\r
71 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
\r
72 new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1);
\r
74 case GroupBoxState.Disabled:
\r
75 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
\r
76 new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2);
\r
80 // Don't paint over the background where we are going to put the text
\r
81 Region old_clip = g.Clip;
\r
82 g.SetClip (new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);
\r
84 if (groupBoxText == String.Empty)
\r
85 vsr.DrawBackground (g, bounds);
\r
87 vsr.DrawBackground (g, new_bounds);
\r
91 if (textColor == Color.Empty)
\r
92 textColor = vsr.GetColor (ColorProperty.TextColor);
\r
94 if (groupBoxText != String.Empty)
\r
95 TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
\r
98 // MS has a pretty big bug when rendering the non-visual styles group box. Instead of using the height
\r
99 // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different
\r
100 // places. Rather than emulate this bug, we do it correctly. After googling for a while, I don't think
\r
101 // anyone has ever actually used this class for anything, so it should be fine. :)
\r
102 Rectangle new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1);
\r
104 // Don't paint over the background where we are going to put the text
\r
105 Region old_clip = g.Clip;
\r
106 g.SetClip (new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);
\r
108 ControlPaint.DrawBorder3D (g, new_bounds, Border3DStyle.Etched);
\r
112 if (groupBoxText != String.Empty)
\r
113 TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
\r
117 public static bool IsBackgroundPartiallyTransparent (GroupBoxState state)
\r
119 if (!VisualStyleRenderer.IsSupported)
\r
122 VisualStyleRenderer vsr;
\r
125 case GroupBoxState.Normal:
\r
127 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
\r
129 case GroupBoxState.Disabled:
\r
130 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
\r
134 return vsr.IsBackgroundPartiallyTransparent ();
\r
137 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
\r
139 if (!VisualStyleRenderer.IsSupported)
\r
142 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
\r
144 vsr.DrawParentBackground (g, bounds, childControl);
\r
148 #region Public Static Properties
\r
149 public static bool RenderMatchingApplicationState {
\r
150 get { return !always_use_visual_styles; }
\r
151 set { always_use_visual_styles = !value; }
\r