Xamarin-4959: Fix copy of clipboard data after app exits
[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 using System.Drawing;
30 using System.Windows.Forms.VisualStyles;
31
32 namespace System.Windows.Forms
33 {
34         public sealed class ButtonRenderer
35         {
36                 private static bool always_use_visual_styles = false;
37
38                 #region Private Constructor
39                 private ButtonRenderer () { }
40                 #endregion
41
42                 #region Public Static Methods
43                 public static void DrawButton (Graphics g, Rectangle bounds, PushButtonState state)
44                 {
45                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, false, state);
46                 }
47
48                 public static void DrawButton (Graphics g, Rectangle bounds, bool focused, PushButtonState state)
49                 {
50                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, focused, state);
51                 }
52
53                 public static void DrawButton (Graphics g, Rectangle bounds, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
54                 {
55                         DrawButton (g, bounds, String.Empty, null, TextFormatFlags.Default, image, imageBounds, focused, state);
56                 }
57
58                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, bool focused, PushButtonState state)
59                 {
60                         DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
61                 }
62
63                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, bool focused, PushButtonState state)
64                 {
65                         DrawButton (g, bounds, buttonText, font, flags, null, Rectangle.Empty, focused, state);
66                 }
67
68                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
69                 {
70                         DrawButton (g, bounds, buttonText, font, TextFormatFlags.HorizontalCenter, image, imageBounds, focused, state);
71                 }
72
73                 public static void DrawButton (Graphics g, Rectangle bounds, string buttonText, Font font, TextFormatFlags flags, Image image, Rectangle imageBounds, bool focused, PushButtonState state)
74                 {
75                         if (Application.RenderWithVisualStyles || always_use_visual_styles == true) {
76                                 VisualStyleRenderer vsr = GetPushButtonRenderer (state);
77
78                                 vsr.DrawBackground (g, bounds);
79
80                                 if (image != null)
81                                         vsr.DrawImage (g, imageBounds, image);
82                         } else {
83                                 if (state == PushButtonState.Pressed)
84                                         ControlPaint.DrawButton (g, bounds, ButtonState.Pushed);
85                                 else
86                                         ControlPaint.DrawButton (g, bounds, ButtonState.Normal);
87
88                                 if (image != null)
89                                         g.DrawImage (image, imageBounds);
90                         }
91
92                         Rectangle focus_rect = bounds;
93                         focus_rect.Inflate (-3, -3);
94
95                         if (focused)
96                                 ControlPaint.DrawFocusRectangle (g, focus_rect);
97
98                         if (buttonText != String.Empty)
99                                 if (state == PushButtonState.Disabled)
100                                         TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.GrayText, flags);
101                                 else
102                                         TextRenderer.DrawText (g, buttonText, font, focus_rect, SystemColors.ControlText, flags);
103                 }
104
105                 public static bool IsBackgroundPartiallyTransparent (PushButtonState state)
106                 {
107                         if (!VisualStyleRenderer.IsSupported)
108                                 return false;
109
110                         VisualStyleRenderer vsr = GetPushButtonRenderer (state);
111
112                         return vsr.IsBackgroundPartiallyTransparent ();
113                 }
114
115                 public static void DrawParentBackground (Graphics g, Rectangle bounds, Control childControl)
116                 {
117                         if (!VisualStyleRenderer.IsSupported)
118                                 return;
119                         
120                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
121
122                         vsr.DrawParentBackground (g, bounds, childControl);
123                 }
124                 #endregion
125
126                 #region Private Static Methods
127                 internal static VisualStyleRenderer GetPushButtonRenderer (PushButtonState state)
128                 {
129                         switch (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:
139                                 default:
140                                         return new VisualStyleRenderer (VisualStyleElement.Button.PushButton.Default);
141                         }
142                 }
143                 #endregion
144
145                 #region Public Static Properties
146                 public static bool RenderMatchingApplicationState {
147                         get { return !always_use_visual_styles; }
148                         set { always_use_visual_styles = !value; }
149                 }
150                 #endregion
151         }
152 }