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)
30 using System.Windows.Forms.VisualStyles;
32 namespace System.Windows.Forms
34 public sealed class GroupBoxRenderer
36 private static bool always_use_visual_styles = false;
38 #region Private Constructor
39 private GroupBoxRenderer () { }
42 #region Public Static Methods
43 public static void DrawGroupBox (Graphics g, Rectangle bounds, GroupBoxState state)
45 DrawGroupBox (g, bounds, String.Empty, null, Color.Empty, TextFormatFlags.Default, state);
48 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, GroupBoxState state)
50 DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, TextFormatFlags.Default, state);
53 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, GroupBoxState state)
55 DrawGroupBox (g, bounds, groupBoxText, font, textColor, TextFormatFlags.Default, state);
58 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, TextFormatFlags flags, GroupBoxState state)
60 DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, flags, state);
63 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)
65 Size font_size = TextRenderer.MeasureText (groupBoxText, font);
67 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
68 VisualStyleRenderer vsr;
72 case GroupBoxState.Normal:
74 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
75 new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 1, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 1);
77 case GroupBoxState.Disabled:
78 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
79 new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2) - 2, bounds.Width, bounds.Height - (int)(font_size.Height / 2) + 2);
83 if (groupBoxText == String.Empty)
84 vsr.DrawBackground (g, bounds);
86 vsr.DrawBackgroundExcludingArea (g, new_bounds, new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height));
88 if (textColor == Color.Empty)
89 textColor = vsr.GetColor (ColorProperty.TextColor);
91 if (groupBoxText != String.Empty)
92 TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
95 // MS has a pretty big bug when rendering the non-visual styles group box. Instead of using the height
96 // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different
97 // places. Rather than emulate this bug, we do it correctly. After googling for a while, I don't think
98 // anyone has ever actually used this class for anything, so it should be fine. :)
99 Rectangle new_bounds = new Rectangle (bounds.Left, bounds.Top + (int)(font_size.Height / 2), bounds.Width, bounds.Height - (int)(font_size.Height / 2));
101 // Don't paint over the background where we are going to put the text
102 Region old_clip = g.Clip;
103 g.SetClip (new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);
105 ControlPaint.DrawBorder3D (g, new_bounds, Border3DStyle.Etched);
109 if (groupBoxText != String.Empty) {
110 if (textColor == Color.Empty)
111 textColor = state == GroupBoxState.Normal ? SystemColors.ControlText :
112 SystemColors.GrayText;
113 TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
118 public static bool IsBackgroundPartiallyTransparent (GroupBoxState state)
120 if (!VisualStyleRenderer.IsSupported)
123 VisualStyleRenderer vsr;
126 case GroupBoxState.Normal:
128 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
130 case GroupBoxState.Disabled:
131 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
135 return vsr.IsBackgroundPartiallyTransparent ();
138 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
140 if (!VisualStyleRenderer.IsSupported)
143 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
145 vsr.DrawParentBackground (g, bounds, childControl);
149 #region Public Static Properties
150 public static bool RenderMatchingApplicationState {
151 get { return !always_use_visual_styles; }
152 set { always_use_visual_styles = !value; }