New test.
[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 Private Constructor\r
40                 private GroupBoxRenderer () { }\r
41                 #endregion\r
42 \r
43                 #region Public Static Methods\r
44                 public static void DrawGroupBox (Graphics g, Rectangle bounds, GroupBoxState state)\r
45                 {\r
46                         DrawGroupBox (g, bounds, String.Empty, null, Color.Empty, TextFormatFlags.Default, state);\r
47                 }\r
48 \r
49                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, GroupBoxState state)\r
50                 {\r
51                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, TextFormatFlags.Default, state);\r
52                 }\r
53 \r
54                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, GroupBoxState state)\r
55                 {\r
56                         DrawGroupBox (g, bounds, groupBoxText, font, textColor, TextFormatFlags.Default, state);\r
57                 }\r
58 \r
59                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, TextFormatFlags flags, GroupBoxState state)\r
60                 {\r
61                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, flags, state);\r
62                 }\r
63 \r
64                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)\r
65                 {\r
66                         Size font_size = TextRenderer.MeasureText (groupBoxText, font);\r
67 \r
68                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {\r
69                                 VisualStyleRenderer vsr;\r
70                                 Rectangle new_bounds;\r
71 \r
72                                 switch (state) {\r
73                                         case GroupBoxState.Normal:\r
74                                         default:\r
75                                                 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);\r
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);\r
77                                                 break;\r
78                                         case GroupBoxState.Disabled:\r
79                                                 vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);\r
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);\r
81                                                 break;\r
82                                 }\r
83 \r
84                                 // Don't paint over the background where we are going to put the text\r
85                                 Region old_clip = g.Clip;\r
86                                 g.SetClip (new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);\r
87 \r
88                                 if (groupBoxText == String.Empty)\r
89                                         vsr.DrawBackground (g, bounds);\r
90                                 else\r
91                                         vsr.DrawBackground (g, new_bounds);\r
92 \r
93                                 g.Clip = old_clip;\r
94 \r
95                                 if (textColor == Color.Empty)\r
96                                         textColor = vsr.GetColor (ColorProperty.TextColor);\r
97 \r
98                                 if (groupBoxText != String.Empty)\r
99                                         TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);\r
100                         }\r
101                         else {\r
102                                 // MS has a pretty big bug when rendering the non-visual styles group box.  Instead of using the height\r
103                                 // part of the bounds as height, they use it as the bottom, so the boxes are drawn in completely different\r
104                                 // places.  Rather than emulate this bug, we do it correctly.  After googling for a while, I don't think\r
105                                 // anyone has ever actually used this class for anything, so it should be fine.  :)\r
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);\r
107                                 \r
108                                 // Don't paint over the background where we are going to put the text\r
109                                 Region old_clip = g.Clip;\r
110                                 g.SetClip (new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height), System.Drawing.Drawing2D.CombineMode.Exclude);\r
111                                 \r
112                                 ControlPaint.DrawBorder3D (g, new_bounds, Border3DStyle.Etched);\r
113                                 \r
114                                 g.Clip = old_clip;\r
115 \r
116                                 if (groupBoxText != String.Empty)\r
117                                         TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);\r
118                         }\r
119                 }\r
120 \r
121                 public static bool IsBackgroundPartiallyTransparent (GroupBoxState state)\r
122                 {\r
123                         if (!VisualStyleRenderer.IsSupported)\r
124                                 return false;\r
125 \r
126                         VisualStyleRenderer vsr;\r
127 \r
128                         switch (state) {\r
129                                 case GroupBoxState.Normal:\r
130                                 default:\r
131                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);\r
132                                         break;\r
133                                 case GroupBoxState.Disabled:\r
134                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);\r
135                                         break;\r
136                         }\r
137 \r
138                         return vsr.IsBackgroundPartiallyTransparent ();\r
139                 }\r
140 \r
141                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)\r
142                 {\r
143                         if (!VisualStyleRenderer.IsSupported)\r
144                                 return;\r
145                         \r
146                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);\r
147 \r
148                         vsr.DrawParentBackground (g, bounds, childControl);\r
149                 }\r
150                 #endregion\r
151 \r
152                 #region Public Static Properties\r
153                 public static bool RenderMatchingApplicationState {\r
154                         get { return !always_use_visual_styles; }\r
155                         set { always_use_visual_styles = !value; }\r
156                 }\r
157                 #endregion\r
158         }\r
159 }\r
160 #endif