2007-04-25 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / GroupBoxRenderer.cs
1 //
2 // GroupBoxRenderer.cs
3 //
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:
11 // 
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
14 // 
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.
22 //
23 // Copyright (c) 2006 Novell, Inc.
24 //
25 // Authors:
26 //      Jonathan Pobst (monkey@jpobst.com)
27 //
28
29 #if NET_2_0
30 using System.Drawing;
31 using System.Windows.Forms.VisualStyles;
32
33 namespace System.Windows.Forms
34 {
35         public sealed class GroupBoxRenderer
36         {
37                 private static bool always_use_visual_styles = false;
38                 
39                 #region Private Constructor
40                 private GroupBoxRenderer () { }
41                 #endregion
42
43                 #region Public Static Methods
44                 public static void DrawGroupBox (Graphics g, Rectangle bounds, GroupBoxState state)
45                 {
46                         DrawGroupBox (g, bounds, String.Empty, null, Color.Empty, TextFormatFlags.Default, state);
47                 }
48
49                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, GroupBoxState state)
50                 {
51                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, TextFormatFlags.Default, state);
52                 }
53
54                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, GroupBoxState state)
55                 {
56                         DrawGroupBox (g, bounds, groupBoxText, font, textColor, TextFormatFlags.Default, state);
57                 }
58
59                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, TextFormatFlags flags, GroupBoxState state)
60                 {
61                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, flags, state);
62                 }
63
64                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)
65                 {
66                         Size font_size = TextRenderer.MeasureText (groupBoxText, font);
67
68                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
69                                 VisualStyleRenderer vsr;
70                                 Rectangle new_bounds;
71
72                                 switch (state) {
73                                         case GroupBoxState.Normal:
74                                         default:
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);
77                                                 break;
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);
81                                                 break;
82                                 }
83
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);
87
88                                 if (groupBoxText == String.Empty)
89                                         vsr.DrawBackground (g, bounds);
90                                 else
91                                         vsr.DrawBackground (g, new_bounds);
92
93                                 g.Clip = old_clip;
94
95                                 if (textColor == Color.Empty)
96                                         textColor = vsr.GetColor (ColorProperty.TextColor);
97
98                                 if (groupBoxText != String.Empty)
99                                         TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
100                         }
101                         else {
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);
107                                 
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);
111                                 
112                                 ControlPaint.DrawBorder3D (g, new_bounds, Border3DStyle.Etched);
113                                 
114                                 g.Clip = old_clip;
115
116                                 if (groupBoxText != String.Empty)
117                                         TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
118                         }
119                 }
120
121                 public static bool IsBackgroundPartiallyTransparent (GroupBoxState state)
122                 {
123                         if (!VisualStyleRenderer.IsSupported)
124                                 return false;
125
126                         VisualStyleRenderer vsr;
127
128                         switch (state) {
129                                 case GroupBoxState.Normal:
130                                 default:
131                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
132                                         break;
133                                 case GroupBoxState.Disabled:
134                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
135                                         break;
136                         }
137
138                         return vsr.IsBackgroundPartiallyTransparent ();
139                 }
140
141                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
142                 {
143                         if (!VisualStyleRenderer.IsSupported)
144                                 return;
145                         
146                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
147
148                         vsr.DrawParentBackground (g, bounds, childControl);
149                 }
150                 #endregion
151
152                 #region Public Static Properties
153                 public static bool RenderMatchingApplicationState {
154                         get { return !always_use_visual_styles; }
155                         set { always_use_visual_styles = !value; }
156                 }
157                 #endregion
158         }
159 }
160 #endif