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