2007-03-29 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / ButtonRenderer.cs
1 //
2 // ButtonRenderer.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 ButtonRenderer
36         {
37                 private static bool always_use_visual_styles = false;
38
39                 #region Private Constructor
40                 private ButtonRenderer () { }
41                 #endregion
42
43                 #region Public Static Methods
44                 public static void DrawButton (Graphics g, Rectangle bounds, PushButtonState state)
45                 {
46                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, false, state);
47                 }
48
49                 public static void DrawButton (Graphics g, Rectangle bounds, bool focused, PushButtonState state)
50                 {
51                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, focused, state);
52                 }
53
54                 public static void DrawButton (Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
55                 {
56                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, image, imageBounds, focused, state);
57                 }
58
59                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state)
60                 {
61                         DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
62                 }
63
64                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state)
65                 {
66                         DrawButton (g, bounds, buttonText, font, flags, null, Rectangle.Empty, focused, state);
67                 }
68
69                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
70                 {
71                         DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
72                 }
73
74                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
75                 {
76                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
77                                 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
78
79                                 vsr.DrawBackground (g, bounds);
80
81                                 if (image != null)
82                                         vsr.DrawImage (g, imageBounds, image);
83                         } else {
84                                 if (state == PushButtonState.Pressed)
85                                         ControlPaint.DrawButton (g, bounds, ButtonState.Pushed);
86                                 else
87                                         ControlPaint.DrawButton (g, bounds, ButtonState.Normal);
88
89                                 if (image != null)
90                                         g.DrawImage (image, imageBounds);
91                         }
92
93                         Rectangle focus_rect = bounds;
94                         focus_rect.Inflate (-3, -3);
95
96                         if (focused)
97                                 ControlPaint.DrawFocusRectangle (g, focus_rect);
98
99                         if (buttonText != String.Empty)
100                                 if (state == PushButtonState.Disabled)
101                                         TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.GrayText, flags);
102                                 else
103                                         TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.ControlText, flags);
104                 }
105
106                 public static bool IsBackgroundPartiallyTransparent (PushButtonState state)
107                 {
108                         if (!VisualStyleRenderer.IsSupported)
109                                 return false;
110
111                         VisualStyleRenderer vsr = GetPushButtonRenderer (state);
112
113                         return vsr.IsBackgroundPartiallyTransparent ();
114                 }
115
116                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
117                 {
118                         if (!VisualStyleRenderer.IsSupported)
119                                 return;
120                         
121                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
122
123                         vsr.DrawParentBackground (g, bounds, childControl);
124                 }
125                 #endregion
126
127                 #region Private Static Methods
128                 private static VisualStyleRenderer GetPushButtonRenderer (PushButtonState state)
129                 {
130                         switch (state) {
131                                 case PushButtonState.Normal:
132                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Normal);
133                                 case PushButtonState.Hot:
134                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Hot);
135                                 case PushButtonState.Pressed:
136                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Pressed);
137                                 case PushButtonState.Disabled:
138                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Disabled);
139                                 case PushButtonState.Default:
140                                 default:
141                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
142                         }
143                 }
144                 #endregion
145
146                 #region Public Static Properties
147                 public static bool RenderMatchingApplicationState {
148                         get { return !always_use_visual_styles; }
149                         set { always_use_visual_styles = !value; }
150                 }
151                 #endregion
152         }
153 }
154 #endif