2008-09-16 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Theming / VisualStyles / ToolStripPainter.cs
1 // Permission is hereby granted, free of charge, to any person obtaining
2 // a copy of this software and associated documentation files (the
3 // "Software"), to deal in the Software without restriction, including
4 // without limitation the rights to use, copy, modify, merge, publish,
5 // distribute, sublicense, and/or sell copies of the Software, and to
6 // permit persons to whom the Software is furnished to do so, subject to
7 // the following conditions:
8 //
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
11 //
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19 //
20 // Copyright (c) 2008 George Giolfan
21 //
22 // Authors:
23 //      George Giolfan (georgegiolfan@yahoo.com)
24
25 #if NET_2_0
26 using System.Drawing;
27 using System.Windows.Forms.VisualStyles;
28 namespace System.Windows.Forms.Theming.VisualStyles
29 {
30         class ToolStripPainter : Default.ToolStripPainter
31         {
32                 static bool IsDisabled (ToolStripItem toolStripItem)
33                 {
34                         return !toolStripItem.Enabled;
35                 }
36                 static bool IsPressed (ToolStripItem toolStripItem)
37                 {
38                         return toolStripItem.Pressed;
39                 }
40                 static bool IsChecked (ToolStripItem toolStripItem)
41                 {
42                         ToolStripButton tool_strip_button = toolStripItem as ToolStripButton;
43                         if (tool_strip_button == null)
44                                 return false;
45                         return tool_strip_button.Checked;
46                 }
47                 static bool IsHot (ToolStripItem toolStripItem)
48                 {
49                         return toolStripItem.Selected;
50                 }
51                 public override void OnRenderButtonBackground (ToolStripItemRenderEventArgs e)
52                 {
53                         if (!ThemeVisualStyles.RenderClientAreas) {
54                                 base.OnRenderButtonBackground (e);
55                                 return;
56                         }
57                         VisualStyleElement element;
58                         if (IsDisabled (e.Item))
59                                 element = VisualStyleElement.ToolBar.Button.Disabled;
60                         else if (IsPressed (e.Item))
61                                 element = VisualStyleElement.ToolBar.Button.Pressed;
62                         else if (IsChecked (e.Item))
63                                 if (IsHot (e.Item))
64                                         element = VisualStyleElement.ToolBar.Button.HotChecked;
65                                 else
66                                         element = VisualStyleElement.ToolBar.Button.Checked;
67                         else if (IsHot (e.Item))
68                                 element = VisualStyleElement.ToolBar.Button.Hot;
69                         else
70                                 element = VisualStyleElement.ToolBar.Button.Normal;
71                         if (!VisualStyleRenderer.IsElementDefined (element)) {
72                                 base.OnRenderButtonBackground (e);
73                                 return;
74                         }
75                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.Item.Bounds);
76                 }
77                 public override void OnRenderDropDownButtonBackground (ToolStripItemRenderEventArgs e)
78                 {
79                         if (!ThemeVisualStyles.RenderClientAreas) {
80                                 base.OnRenderDropDownButtonBackground (e);
81                                 return;
82                         }
83                         VisualStyleElement element;
84                         if (IsDisabled (e.Item))
85                                 element = VisualStyleElement.ToolBar.DropDownButton.Disabled;
86                         else if (IsPressed (e.Item))
87                                 element = VisualStyleElement.ToolBar.DropDownButton.Pressed;
88                         else if (IsChecked (e.Item))
89                                 if (IsHot (e.Item))
90                                         element = VisualStyleElement.ToolBar.DropDownButton.HotChecked;
91                                 else
92                                         element = VisualStyleElement.ToolBar.DropDownButton.Checked;
93                         else if (IsHot (e.Item))
94                                 element = VisualStyleElement.ToolBar.DropDownButton.Hot;
95                         else
96                                 element = VisualStyleElement.ToolBar.DropDownButton.Normal;
97                         if (!VisualStyleRenderer.IsElementDefined (element)) {
98                                 base.OnRenderDropDownButtonBackground (e);
99                                 return;
100                         }
101                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.Item.Bounds);
102                 }
103                 public override void OnRenderGrip (ToolStripGripRenderEventArgs e)
104                 {
105                         if (!ThemeVisualStyles.RenderClientAreas) {
106                                 base.OnRenderGrip (e);
107                                 return;
108                         }
109                         if (e.GripStyle == ToolStripGripStyle.Hidden)
110                                 return;
111                         VisualStyleElement element = e.GripDisplayStyle == ToolStripGripDisplayStyle.Vertical ?
112                                 VisualStyleElement.Rebar.Gripper.Normal :
113                                 VisualStyleElement.Rebar.GripperVertical.Normal;
114                         if (!VisualStyleRenderer.IsElementDefined (element)) {
115                                 base.OnRenderGrip (e);
116                                 return;
117                         }
118                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.GripDisplayStyle == ToolStripGripDisplayStyle.Vertical ?
119                                 // GetPartSize seems to return useless values.
120                                 new Rectangle (2, 0, 5, 20) :
121                                 new Rectangle (0, 2, 20, 5));
122                 }
123                 public override void OnRenderOverflowButtonBackground (ToolStripItemRenderEventArgs e)
124                 {
125                         if (!ThemeVisualStyles.RenderClientAreas) {
126                                 base.OnRenderOverflowButtonBackground (e);
127                                 return;
128                         }
129                         VisualStyleElement element = e.ToolStrip.Orientation == Orientation.Horizontal ?
130                                 VisualStyleElement.Rebar.Chevron.Normal :
131                                 VisualStyleElement.Rebar.ChevronVertical.Normal;
132                         if (!VisualStyleRenderer.IsElementDefined (element)) {
133                                 base.OnRenderOverflowButtonBackground (e);
134                                 return;
135                         }
136                         OnRenderButtonBackground (e);
137                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.Item.Bounds);
138                 }
139                 public override void OnRenderSeparator (ToolStripSeparatorRenderEventArgs e)
140                 {
141                         if (!ThemeVisualStyles.RenderClientAreas) {
142                                 base.OnRenderSeparator (e);
143                                 return;
144                         }
145                         VisualStyleElement element = e.ToolStrip.Orientation == Orientation.Horizontal ?
146                                 VisualStyleElement.ToolBar.SeparatorHorizontal.Normal :
147                                 VisualStyleElement.ToolBar.SeparatorVertical.Normal;
148                         if (!VisualStyleRenderer.IsElementDefined (element)) {
149                                 base.OnRenderSeparator (e);
150                                 return;
151                         }
152                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.Item.Bounds);
153                 }
154                 public override void OnRenderSplitButtonBackground (ToolStripItemRenderEventArgs e)
155                 {
156                         if (!ThemeVisualStyles.RenderClientAreas) {
157                                 base.OnRenderSplitButtonBackground (e);
158                                 return;
159                         }
160                         VisualStyleElement element, drop_down_element;
161                         if (IsDisabled (e.Item)) {
162                                 element = VisualStyleElement.ToolBar.SplitButton.Disabled;
163                                 drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.Disabled;;
164                         } else if (IsPressed (e.Item)) {
165                                 element = VisualStyleElement.ToolBar.SplitButton.Pressed;
166                                 drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.Pressed;
167                         } else if (IsChecked (e.Item))
168                                 if (IsHot (e.Item)) {
169                                         element = VisualStyleElement.ToolBar.SplitButton.HotChecked;
170                                         drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.HotChecked;
171                                 } else {
172                                         element = VisualStyleElement.ToolBar.Button.Checked;
173                                         drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.Checked;
174                                 }
175                         else if (IsHot (e.Item)) {
176                                 element = VisualStyleElement.ToolBar.SplitButton.Hot;
177                                 drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.Hot;
178                         } else {
179                                 element = VisualStyleElement.ToolBar.SplitButton.Normal;
180                                 drop_down_element = VisualStyleElement.ToolBar.SplitButtonDropDown.Normal;
181                         }
182                         if (!VisualStyleRenderer.IsElementDefined (element) ||
183                                 !VisualStyleRenderer.IsElementDefined (drop_down_element)) {
184                                 base.OnRenderSplitButtonBackground (e);
185                                 return;
186                         }
187                         ToolStripSplitButton tool_strip_split_button = (ToolStripSplitButton)e.Item;
188                         VisualStyleRenderer renderer = new VisualStyleRenderer (element);
189                         renderer.DrawBackground (e.Graphics, tool_strip_split_button.ButtonBounds);
190                         renderer.SetParameters (drop_down_element);
191                         renderer.DrawBackground (e.Graphics, tool_strip_split_button.DropDownButtonBounds);
192                 }
193                 public override void OnRenderToolStripBackground (ToolStripRenderEventArgs e)
194                 {
195                         if (!ThemeVisualStyles.RenderClientAreas) {
196                                 base.OnRenderToolStripBackground (e);
197                                 return;
198                         }
199                         VisualStyleElement element;
200                         if (e.ToolStrip is StatusStrip)
201                                 element = VisualStyleElement.Status.Bar.Normal;
202                         else
203                                 element = VisualStyleElement.Rebar.Band.Normal;
204                         if (!VisualStyleRenderer.IsElementDefined (element)) {
205                                 base.OnRenderToolStripBackground (e);
206                                 return;
207                         }
208                         new VisualStyleRenderer (element).DrawBackground (e.Graphics, e.ToolStrip.Bounds, e.AffectedBounds);
209                 }
210         }
211 }
212 #endif