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:
12 // The above copyright notice and this permission notice shall be
13 // included in all copies or substantial portions of the Software.
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.
23 // Copyright (c) 2006 Novell, Inc.
26 // Jonathan Pobst (monkey@jpobst.com)
30 using System.Windows.Forms.VisualStyles;
32 namespace System.Windows.Forms
34 public sealed class ButtonRenderer
36 private static bool always_use_visual_styles = false;
38 #region Private Constructor
39 private ButtonRenderer () { }
42 #region Public Static Methods
43 public static void DrawButton (Graphics g, Rectangle bounds, PushButtonState state)
45 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, false, state);
48 public static void DrawButton (Graphics g, Rectangle bounds, bool focused, PushButtonState state)
50 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, focused, state);
53 public static void DrawButton (Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
55 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, image, imageBounds, focused, state);
58 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state)
60 DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
63 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state)
65 DrawButton (g, bounds, buttonText, font, flags, null, Rectangle.Empty, focused, state);
68 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
70 DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
73 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
75 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
76 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
78 vsr.DrawBackground (g, bounds);
81 vsr.DrawImage (g, imageBounds, image);
83 if (state == PushButtonState.Pressed)
84 ControlPaint.DrawButton (g, bounds, ButtonState.Pushed);
86 ControlPaint.DrawButton (g, bounds, ButtonState.Normal);
89 g.DrawImage (image, imageBounds);
92 Rectangle focus_rect = bounds;
93 focus_rect.Inflate (-3, -3);
96 ControlPaint.DrawFocusRectangle (g, focus_rect);
98 if (buttonText != String.Empty)
99 if (state == PushButtonState.Disabled)
100 TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.GrayText, flags);
102 TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.ControlText, flags);
105 public static bool IsBackgroundPartiallyTransparent (PushButtonState state)
107 if (!VisualStyleRenderer.IsSupported)
110 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
112 return vsr.IsBackgroundPartiallyTransparent ();
115 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
117 if (!VisualStyleRenderer.IsSupported)
120 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
122 vsr.DrawParentBackground (g, bounds, childControl);
126 #region Private Static Methods
127 internal static VisualStyleRenderer GetPushButtonRenderer (PushButtonState state)
130 case PushButtonState.Normal:
131 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Normal);
132 case PushButtonState.Hot:
133 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Hot);
134 case PushButtonState.Pressed:
135 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Pressed);
136 case PushButtonState.Disabled:
137 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Disabled);
138 case PushButtonState.Default:
140 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
145 #region Public Static Properties
146 public static bool RenderMatchingApplicationState {
147 get { return !always_use_visual_styles; }
148 set { always_use_visual_styles = !value; }