Fix bug #395
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TabRenderer.cs
1 //
2 // TabRenderer.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 TabRenderer
35         {
36                 #region Private Constructor
37                 private TabRenderer () { }
38                 #endregion
39
40                 #region Public Static Methods
41                 public static void DrawTabItem (Graphics g, Rectangle bounds, TabItemState state)
42                 {
43                         DrawTabItem(g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, false, state);
44                 }
45
46                 public static void DrawTabItem (Graphics g, Rectangle bounds, bool focused, TabItemState state)
47                 {
48                         DrawTabItem (g, bounds, String.Empty, null, TextFormatFlags.Default, null, Rectangle.Empty, focused, state);
49                 }
50                 
51                 public static void DrawTabItem (Graphics g, Rectangle bounds, string tabItemText, Font font, TabItemState state)
52                 {
53                         DrawTabItem (g, bounds, tabItemText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, false, state);
54                 }
55
56                 public static void DrawTabItem (Graphics g, Rectangle bounds, Image image, Rectangle imageRectangle, bool focused, TabItemState state)
57                 {
58                         DrawTabItem (g, bounds, String.Empty, null, TextFormatFlags.HorizontalCenter, image, imageRectangle, focused, state);
59                 }
60
61                 public static void DrawTabItem (Graphics g, Rectangle bounds, string tabItemText, Font font, bool focused, TabItemState state)
62                 {
63                         DrawTabItem (g, bounds, tabItemText, font, TextFormatFlags.HorizontalCenter, null, Rectangle.Empty, focused, state);
64                 }
65
66                 public static void DrawTabItem (Graphics g, Rectangle bounds, string tabItemText, Font font, TextFormatFlags flags, bool focused, TabItemState state)
67                 {
68                         DrawTabItem (g, bounds, tabItemText, font, flags, null, Rectangle.Empty, focused, state);
69                 }
70
71                 public static void DrawTabItem (Graphics g, Rectangle bounds, string tabItemText, Font font, Image image, Rectangle imageRectangle, bool focused, TabItemState state)
72                 {
73                         DrawTabItem (g, bounds, tabItemText, font, TextFormatFlags.HorizontalCenter, image, imageRectangle, focused, state);
74                 }
75
76                 public static void DrawTabItem (Graphics g, Rectangle bounds, string tabItemText, Font font, TextFormatFlags flags, Image image, Rectangle imageRectangle, bool focused, TabItemState state)
77                 {
78                         if (!IsSupported)
79                                 throw new InvalidOperationException ();
80                                 
81                         VisualStyleRenderer vsr;
82                         
83                         switch (state) {
84                                 case TabItemState.Disabled:
85                                         vsr = new VisualStyleRenderer (VisualStyleElement.Tab.TabItem.Disabled);
86                                         break;
87                                 case TabItemState.Hot:
88                                         vsr = new VisualStyleRenderer (VisualStyleElement.Tab.TabItem.Hot);
89                                         break;
90                                 case TabItemState.Normal:
91                                 default:
92                                         vsr = new VisualStyleRenderer (VisualStyleElement.Tab.TabItem.Normal);
93                                         break;
94                                 case TabItemState.Selected:
95                                         vsr = new VisualStyleRenderer (VisualStyleElement.Tab.TabItem.Pressed);
96                                         break;
97                         }
98
99                         vsr.DrawBackground (g, bounds);
100                         
101                         if(image != null)
102                                 vsr.DrawImage(g, imageRectangle, image);
103                                 
104                         bounds.Offset(3,3);
105                         bounds.Height -= 6;
106                         bounds.Width -= 6;
107                         
108                         if(tabItemText != String.Empty)
109                                 TextRenderer.DrawText(g, tabItemText, font, bounds, SystemColors.ControlText,flags);
110                                 
111                         if(focused)
112                                 ControlPaint.DrawFocusRectangle(g, bounds);
113                 }
114
115                 public static void DrawTabPage (Graphics g, Rectangle bounds)
116                 {
117                         if (!IsSupported)
118                                 throw new InvalidOperationException ();
119
120                         VisualStyleRenderer vsr = new VisualStyleRenderer (VisualStyleElement.Tab.Pane.Normal); ;
121
122                         vsr.DrawBackground (g, bounds);
123                 }
124                 #endregion
125                 
126                 #region Public Static Properties
127                 public static bool IsSupported {
128                         get { return VisualStyleInformation.IsEnabledByUser && (Application.VisualStyleState == VisualStyleState.ClientAndNonClientAreasEnabled || Application.VisualStyleState == VisualStyleState.ClientAreaEnabled); }
129                 }
130                 #endregion
131         }
132 }