Merge pull request #1275 from ranma42/fix-lib64
[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 using System.Drawing;
30 using System.Windows.Forms.VisualStyles;
31
32 namespace System.Windows.Forms
33 {
34         public sealed class GroupBoxRenderer
35         {
36                 private static bool always_use_visual_styles = false;
37                 
38                 #region Private Constructor
39                 private GroupBoxRenderer () { }
40                 #endregion
41
42                 #region Public Static Methods
43                 public static void DrawGroupBox (Graphics g, Rectangle bounds, GroupBoxState state)
44                 {
45                         DrawGroupBox (g, bounds, String.Empty, null, Color.Empty, TextFormatFlags.Default, state);
46                 }
47
48                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, GroupBoxState state)
49                 {
50                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, TextFormatFlags.Default, state);
51                 }
52
53                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, GroupBoxState state)
54                 {
55                         DrawGroupBox (g, bounds, groupBoxText, font, textColor, TextFormatFlags.Default, state);
56                 }
57
58                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, TextFormatFlags flags, GroupBoxState state)
59                 {
60                         DrawGroupBox (g, bounds, groupBoxText, font, Color.Empty, flags, state);
61                 }
62
63                 public static void DrawGroupBox (Graphics g, Rectangle bounds, string groupBoxText, Font font, Color textColor, TextFormatFlags flags, GroupBoxState state)
64                 {
65                         Size font_size = TextRenderer.MeasureText (groupBoxText, font);
66
67                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
68                                 VisualStyleRenderer vsr;
69                                 Rectangle new_bounds;
70
71                                 switch (state) {
72                                         case GroupBoxState.Normal:
73                                         default:
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);
76                                                 break;
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);
80                                                 break;
81                                 }
82
83                                 if (groupBoxText == String.Empty)
84                                         vsr.DrawBackground (g, bounds);
85                                 else
86                                         vsr.DrawBackgroundExcludingArea (g, new_bounds, new Rectangle (bounds.Left + 9, bounds.Top, font_size.Width - 3, font_size.Height));
87
88                                 if (textColor == Color.Empty)
89                                         textColor = vsr.GetColor (ColorProperty.TextColor);
90
91                                 if (groupBoxText != String.Empty)
92                                         TextRenderer.DrawText (g, groupBoxText, font, new Point (bounds.Left + 8, bounds.Top), textColor, flags);
93                         }
94                         else {
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));
100                                 
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);
104                                 
105                                 ControlPaint.DrawBorder3D (g, new_bounds, Border3DStyle.Etched);
106                                 
107                                 g.Clip = old_clip;
108
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);
114                                 }
115                         }
116                 }
117
118                 public static bool IsBackgroundPartiallyTransparent (GroupBoxState state)
119                 {
120                         if (!VisualStyleRenderer.IsSupported)
121                                 return false;
122
123                         VisualStyleRenderer vsr;
124
125                         switch (state) {
126                                 case GroupBoxState.Normal:
127                                 default:
128                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
129                                         break;
130                                 case GroupBoxState.Disabled:
131                                         vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Disabled);
132                                         break;
133                         }
134
135                         return vsr.IsBackgroundPartiallyTransparent ();
136                 }
137
138                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
139                 {
140                         if (!VisualStyleRenderer.IsSupported)
141                                 return;
142                         
143                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.GroupBox.Normal);
144
145                         vsr.DrawParentBackground (g, bounds, childControl);
146                 }
147                 #endregion
148
149                 #region Public Static Properties
150                 public static bool RenderMatchingApplicationState {
151                         get { return !always_use_visual_styles; }
152                         set { always_use_visual_styles = !value; }
153                 }
154                 #endregion
155         }
156 }