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