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:
9 // The above copyright notice and this permission notice shall be
10 // included in all copies or substantial portions of the Software.
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.
20 // Copyright (c) 2008 George Giolfan
23 // George Giolfan (georgegiolfan@yahoo.com)
26 using System.Windows.Forms.VisualStyles;
27 namespace System.Windows.Forms.Theming.VisualStyles
29 class TabControlPainter : Default.TabControlPainter
31 static bool ShouldPaint (TabControl tabControl) {
32 return ThemeVisualStyles.RenderClientAreas &&
33 tabControl.Alignment == TabAlignment.Top &&
34 tabControl.DrawMode == TabDrawMode.Normal;
36 protected override void DrawBackground (Graphics dc, Rectangle area, TabControl tab)
38 if (!ShouldPaint (tab)) {
39 base.DrawBackground (dc, area, tab);
43 VisualStyleElement element = VisualStyleElement.Tab.Pane.Normal;
44 if (!VisualStyleRenderer.IsElementDefined (element)) {
45 base.DrawBackground (dc, area, tab);
48 Rectangle panel_rectangle = GetTabPanelRect (tab);
49 if (panel_rectangle.IntersectsWith (area))
50 new VisualStyleRenderer (element).DrawBackground (dc, panel_rectangle, area);
52 protected override int DrawTab (Graphics dc, TabPage page, TabControl tab, Rectangle bounds, bool is_selected)
54 if (!ShouldPaint (tab))
55 return base.DrawTab (dc, page, tab, bounds, is_selected);
56 VisualStyleElement element = GetVisualStyleElement (tab, page, is_selected);
57 if (!VisualStyleRenderer.IsElementDefined (element))
58 return base.DrawTab (dc, page, tab, bounds, is_selected);
59 new VisualStyleRenderer (element).DrawBackground (dc, bounds);
63 Rectangle text_area = bounds;
64 if (tab.ImageList != null && page.ImageIndex >= 0 && page.ImageIndex < tab.ImageList.Images.Count) {
65 int image_y = bounds.Y + (bounds.Height - tab.ImageList.ImageSize.Height) / 2;
66 tab.ImageList.Draw (dc, new Point (bounds.X, image_y), page.ImageIndex);
67 int image_occupied_space = tab.ImageList.ImageSize.Width + 2;
68 text_area.X += image_occupied_space;
69 text_area.Width -= image_occupied_space;
71 if (page.Text != null)
72 dc.DrawString (page.Text, tab.Font, SystemBrushes.ControlText, text_area, DefaultFormatting);
73 if (tab.Focused && is_selected && tab.ShowFocusCues)
74 ControlPaint.DrawFocusRectangle (dc, bounds);
77 static VisualStyleElement GetVisualStyleElement (TabControl tabControl, TabPage tabPage, bool selected)
79 bool top_edge = tabPage.Row == tabControl.RowCount;
80 int tab_page_index = tabControl.TabPages.IndexOf (tabPage);
81 bool left_edge = true;
83 for (index = tabControl.SliderPos; index < tab_page_index; index++)
84 if (tabControl.TabPages [index].Row == tabPage.Row) {
88 bool right_edge = true;
89 for (index = tab_page_index; index < tabControl.TabCount; index++)
90 if (tabControl.TabPages [index].Row == tabPage.Row) {
99 return VisualStyleElement.Tab.TopTabItem.Disabled;
101 return VisualStyleElement.Tab.TopTabItemLeftEdge.Disabled;
104 return VisualStyleElement.Tab.TopTabItemRightEdge.Disabled;
106 return VisualStyleElement.Tab.TopTabItem.Disabled;
110 return VisualStyleElement.Tab.TabItem.Disabled;
112 return VisualStyleElement.Tab.TabItemLeftEdge.Disabled;
115 return VisualStyleElement.Tab.TabItemRightEdge.Disabled;
117 return VisualStyleElement.Tab.TabItem.Disabled;
124 return VisualStyleElement.Tab.TopTabItem.Pressed;
126 return VisualStyleElement.Tab.TopTabItemLeftEdge.Pressed;
129 return VisualStyleElement.Tab.TopTabItemRightEdge.Pressed;
131 return VisualStyleElement.Tab.TopTabItem.Pressed;
135 return VisualStyleElement.Tab.TabItem.Pressed;
137 return VisualStyleElement.Tab.TabItemLeftEdge.Pressed;
140 return VisualStyleElement.Tab.TabItemRightEdge.Pressed;
142 return VisualStyleElement.Tab.TabItem.Pressed;
144 else if (tabControl.EnteredTabPage == tabPage)
149 return VisualStyleElement.Tab.TopTabItem.Hot;
151 return VisualStyleElement.Tab.TopTabItemLeftEdge.Hot;
154 return VisualStyleElement.Tab.TopTabItemRightEdge.Hot;
156 return VisualStyleElement.Tab.TopTabItem.Hot;
160 return VisualStyleElement.Tab.TabItem.Hot;
162 return VisualStyleElement.Tab.TabItemLeftEdge.Hot;
165 return VisualStyleElement.Tab.TabItemRightEdge.Hot;
167 return VisualStyleElement.Tab.TabItem.Hot;
174 return VisualStyleElement.Tab.TopTabItemBothEdges.Normal;
176 return VisualStyleElement.Tab.TopTabItemLeftEdge.Normal;
179 return VisualStyleElement.Tab.TopTabItemRightEdge.Normal;
181 return VisualStyleElement.Tab.TopTabItem.Normal;
185 return VisualStyleElement.Tab.TabItemBothEdges.Normal;
187 return VisualStyleElement.Tab.TabItemLeftEdge.Normal;
190 return VisualStyleElement.Tab.TabItemRightEdge.Normal;
192 return VisualStyleElement.Tab.TabItem.Normal;
195 public override bool HasHotElementStyles (TabControl tabControl)
197 if (!ShouldPaint (tabControl))
198 return base.HasHotElementStyles (tabControl);
201 protected override void DrawScrollButton (Graphics dc, Rectangle bounds, Rectangle clippingArea, ScrollButton button, PushButtonState state)
203 if (!ThemeVisualStyles.RenderClientAreas) {
204 base.DrawScrollButton (dc, bounds, clippingArea, button, state);
207 VisualStyleElement element;
208 if (button == ScrollButton.Left)
210 case PushButtonState.Hot:
211 element = VisualStyleElement.Spin.DownHorizontal.Hot;
213 case PushButtonState.Pressed:
214 element = VisualStyleElement.Spin.DownHorizontal.Pressed;
217 element = VisualStyleElement.Spin.DownHorizontal.Normal;
222 case PushButtonState.Hot:
223 element = VisualStyleElement.Spin.UpHorizontal.Hot;
225 case PushButtonState.Pressed:
226 element = VisualStyleElement.Spin.UpHorizontal.Pressed;
229 element = VisualStyleElement.Spin.UpHorizontal.Normal;
232 if (!VisualStyleRenderer.IsElementDefined (element)) {
233 if (button == ScrollButton.Left)
235 case PushButtonState.Hot:
236 element = VisualStyleElement.ScrollBar.ArrowButton.LeftHot;
238 case PushButtonState.Pressed:
239 element = VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
242 element = VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
247 case PushButtonState.Hot:
248 element = VisualStyleElement.ScrollBar.ArrowButton.RightHot;
250 case PushButtonState.Pressed:
251 element = VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
254 element = VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
257 if (!VisualStyleRenderer.IsElementDefined (element)) {
258 base.DrawScrollButton (dc, bounds, clippingArea, button, state);
262 new VisualStyleRenderer (element).DrawBackground (dc, bounds, clippingArea);