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)
31 using System.Windows.Forms.VisualStyles;
33 namespace System.Windows.Forms
35 public sealed class ButtonRenderer
37 private static bool always_use_visual_styles = false;
39 #region Private Constructor
40 private ButtonRenderer () { }
43 #region Public Static Methods
44 public static void DrawButton (Graphics g, Rectangle bounds, PushButtonState state)
46 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, false, state);
49 public static void DrawButton (Graphics g, Rectangle bounds, bool focused, PushButtonState state)
51 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, focused, state);
54 public static void DrawButton (Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
56 DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, image, imageBounds, focused, state);
59 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state)
61 DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
64 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state)
66 DrawButton (g, bounds, buttonText, font, flags, null, Rectangle.Empty, focused, state);
69 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
71 DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
74 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
76 if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
77 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
79 vsr.DrawBackground (g, bounds);
82 vsr.DrawImage (g, imageBounds, image);
84 if (state == PushButtonState.Pressed)
85 ControlPaint.DrawButton (g, bounds, ButtonState.Pushed);
87 ControlPaint.DrawButton (g, bounds, ButtonState.Normal);
90 g.DrawImage (image, imageBounds);
93 Rectangle focus_rect = bounds;
94 focus_rect.Inflate (-3, -3);
97 ControlPaint.DrawFocusRectangle (g, focus_rect);
99 if (buttonText != String.Empty)
100 if (state == PushButtonState.Disabled)
101 TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.GrayText, flags);
103 TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.ControlText, flags);
106 public static bool IsBackgroundPartiallyTransparent (PushButtonState state)
108 if (!VisualStyleRenderer.IsSupported)
111 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
113 return vsr.IsBackgroundPartiallyTransparent ();
116 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
118 if (!VisualStyleRenderer.IsSupported)
121 VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
123 vsr.DrawParentBackground (g, bounds, childControl);
127 #region Private Static Methods
128 private static VisualStyleRenderer GetPushButtonRenderer (PushButtonState 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:
141 return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
146 #region Public Static Properties
147 public static bool RenderMatchingApplicationState {
148 get { return !always_use_visual_styles; }
149 set { always_use_visual_styles = !value; }