TabControlPainter.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 ([email protected])
  24. using System.Drawing;
  25. using System.Windows.Forms.VisualStyles;
  26. namespace System.Windows.Forms.Theming.VisualStyles
  27. {
  28. class TabControlPainter : Default.TabControlPainter
  29. {
  30. static bool ShouldPaint (TabControl tabControl) {
  31. return ThemeVisualStyles.RenderClientAreas &&
  32. tabControl.Alignment == TabAlignment.Top &&
  33. tabControl.DrawMode == TabDrawMode.Normal;
  34. }
  35. protected override void DrawBackground (Graphics dc, Rectangle area, TabControl tab)
  36. {
  37. if (!ShouldPaint (tab)) {
  38. base.DrawBackground (dc, area, tab);
  39. return;
  40. }
  41. VisualStyleElement element = VisualStyleElement.Tab.Pane.Normal;
  42. if (!VisualStyleRenderer.IsElementDefined (element)) {
  43. base.DrawBackground (dc, area, tab);
  44. return;
  45. }
  46. Rectangle panel_rectangle = GetTabPanelRect (tab);
  47. if (panel_rectangle.IntersectsWith (area))
  48. new VisualStyleRenderer (element).DrawBackground (dc, panel_rectangle, area);
  49. }
  50. protected override int DrawTab (Graphics dc, TabPage page, TabControl tab, Rectangle bounds, bool is_selected)
  51. {
  52. if (!ShouldPaint (tab))
  53. return base.DrawTab (dc, page, tab, bounds, is_selected);
  54. VisualStyleElement element = GetVisualStyleElement (tab, page, is_selected);
  55. if (!VisualStyleRenderer.IsElementDefined (element))
  56. return base.DrawTab (dc, page, tab, bounds, is_selected);
  57. new VisualStyleRenderer (element).DrawBackground (dc, bounds);
  58. bounds.Inflate (
  59. -(tab.Padding.X),
  60. -(tab.Padding.Y));
  61. Rectangle text_area = bounds;
  62. if (tab.ImageList != null && page.ImageIndex >= 0 && page.ImageIndex < tab.ImageList.Images.Count) {
  63. int image_y = bounds.Y + (bounds.Height - tab.ImageList.ImageSize.Height) / 2;
  64. tab.ImageList.Draw (dc, new Point (bounds.X, image_y), page.ImageIndex);
  65. int image_occupied_space = tab.ImageList.ImageSize.Width + 2;
  66. text_area.X += image_occupied_space;
  67. text_area.Width -= image_occupied_space;
  68. }
  69. if (page.Text != null)
  70. dc.DrawString (page.Text, tab.Font, SystemBrushes.ControlText, text_area, DefaultFormatting);
  71. if (tab.Focused && is_selected && tab.ShowFocusCues)
  72. ControlPaint.DrawFocusRectangle (dc, bounds);
  73. return 0;
  74. }
  75. static VisualStyleElement GetVisualStyleElement (TabControl tabControl, TabPage tabPage, bool selected)
  76. {
  77. bool top_edge = tabPage.Row == tabControl.RowCount;
  78. int tab_page_index = tabControl.TabPages.IndexOf (tabPage);
  79. bool left_edge = true;
  80. int index;
  81. for (index = tabControl.SliderPos; index < tab_page_index; index++)
  82. if (tabControl.TabPages [index].Row == tabPage.Row) {
  83. left_edge = false;
  84. break;
  85. }
  86. bool right_edge = true;
  87. for (index = tab_page_index; index < tabControl.TabCount; index++)
  88. if (tabControl.TabPages [index].Row == tabPage.Row) {
  89. right_edge = false;
  90. break;
  91. }
  92. if (!tabPage.Enabled)
  93. #region Disabled
  94. if (top_edge)
  95. if (left_edge)
  96. if (right_edge)
  97. return VisualStyleElement.Tab.TopTabItem.Disabled;
  98. else
  99. return VisualStyleElement.Tab.TopTabItemLeftEdge.Disabled;
  100. else
  101. if (right_edge)
  102. return VisualStyleElement.Tab.TopTabItemRightEdge.Disabled;
  103. else
  104. return VisualStyleElement.Tab.TopTabItem.Disabled;
  105. else
  106. if (left_edge)
  107. if (right_edge)
  108. return VisualStyleElement.Tab.TabItem.Disabled;
  109. else
  110. return VisualStyleElement.Tab.TabItemLeftEdge.Disabled;
  111. else
  112. if (right_edge)
  113. return VisualStyleElement.Tab.TabItemRightEdge.Disabled;
  114. else
  115. return VisualStyleElement.Tab.TabItem.Disabled;
  116. #endregion
  117. else if (selected)
  118. #region Pressed
  119. if (top_edge)
  120. if (left_edge)
  121. if (right_edge)
  122. return VisualStyleElement.Tab.TopTabItem.Pressed;
  123. else
  124. return VisualStyleElement.Tab.TopTabItemLeftEdge.Pressed;
  125. else
  126. if (right_edge)
  127. return VisualStyleElement.Tab.TopTabItemRightEdge.Pressed;
  128. else
  129. return VisualStyleElement.Tab.TopTabItem.Pressed;
  130. else
  131. if (left_edge)
  132. if (right_edge)
  133. return VisualStyleElement.Tab.TabItem.Pressed;
  134. else
  135. return VisualStyleElement.Tab.TabItemLeftEdge.Pressed;
  136. else
  137. if (right_edge)
  138. return VisualStyleElement.Tab.TabItemRightEdge.Pressed;
  139. else
  140. return VisualStyleElement.Tab.TabItem.Pressed;
  141. #endregion
  142. else if (tabControl.EnteredTabPage == tabPage)
  143. #region Hot
  144. if (top_edge)
  145. if (left_edge)
  146. if (right_edge)
  147. return VisualStyleElement.Tab.TopTabItem.Hot;
  148. else
  149. return VisualStyleElement.Tab.TopTabItemLeftEdge.Hot;
  150. else
  151. if (right_edge)
  152. return VisualStyleElement.Tab.TopTabItemRightEdge.Hot;
  153. else
  154. return VisualStyleElement.Tab.TopTabItem.Hot;
  155. else
  156. if (left_edge)
  157. if (right_edge)
  158. return VisualStyleElement.Tab.TabItem.Hot;
  159. else
  160. return VisualStyleElement.Tab.TabItemLeftEdge.Hot;
  161. else
  162. if (right_edge)
  163. return VisualStyleElement.Tab.TabItemRightEdge.Hot;
  164. else
  165. return VisualStyleElement.Tab.TabItem.Hot;
  166. #endregion
  167. else
  168. #region Normal
  169. if (top_edge)
  170. if (left_edge)
  171. if (right_edge)
  172. return VisualStyleElement.Tab.TopTabItemBothEdges.Normal;
  173. else
  174. return VisualStyleElement.Tab.TopTabItemLeftEdge.Normal;
  175. else
  176. if (right_edge)
  177. return VisualStyleElement.Tab.TopTabItemRightEdge.Normal;
  178. else
  179. return VisualStyleElement.Tab.TopTabItem.Normal;
  180. else
  181. if (left_edge)
  182. if (right_edge)
  183. return VisualStyleElement.Tab.TabItemBothEdges.Normal;
  184. else
  185. return VisualStyleElement.Tab.TabItemLeftEdge.Normal;
  186. else
  187. if (right_edge)
  188. return VisualStyleElement.Tab.TabItemRightEdge.Normal;
  189. else
  190. return VisualStyleElement.Tab.TabItem.Normal;
  191. #endregion
  192. }
  193. public override bool HasHotElementStyles (TabControl tabControl)
  194. {
  195. if (!ShouldPaint (tabControl))
  196. return base.HasHotElementStyles (tabControl);
  197. return true;
  198. }
  199. protected override void DrawScrollButton (Graphics dc, Rectangle bounds, Rectangle clippingArea, ScrollButton button, PushButtonState state)
  200. {
  201. if (!ThemeVisualStyles.RenderClientAreas) {
  202. base.DrawScrollButton (dc, bounds, clippingArea, button, state);
  203. return;
  204. }
  205. VisualStyleElement element;
  206. if (button == ScrollButton.Left)
  207. switch (state) {
  208. case PushButtonState.Hot:
  209. element = VisualStyleElement.Spin.DownHorizontal.Hot;
  210. break;
  211. case PushButtonState.Pressed:
  212. element = VisualStyleElement.Spin.DownHorizontal.Pressed;
  213. break;
  214. default:
  215. element = VisualStyleElement.Spin.DownHorizontal.Normal;
  216. break;
  217. }
  218. else
  219. switch (state) {
  220. case PushButtonState.Hot:
  221. element = VisualStyleElement.Spin.UpHorizontal.Hot;
  222. break;
  223. case PushButtonState.Pressed:
  224. element = VisualStyleElement.Spin.UpHorizontal.Pressed;
  225. break;
  226. default:
  227. element = VisualStyleElement.Spin.UpHorizontal.Normal;
  228. break;
  229. }
  230. if (!VisualStyleRenderer.IsElementDefined (element)) {
  231. if (button == ScrollButton.Left)
  232. switch (state) {
  233. case PushButtonState.Hot:
  234. element = VisualStyleElement.ScrollBar.ArrowButton.LeftHot;
  235. break;
  236. case PushButtonState.Pressed:
  237. element = VisualStyleElement.ScrollBar.ArrowButton.LeftPressed;
  238. break;
  239. default:
  240. element = VisualStyleElement.ScrollBar.ArrowButton.LeftNormal;
  241. break;
  242. }
  243. else
  244. switch (state) {
  245. case PushButtonState.Hot:
  246. element = VisualStyleElement.ScrollBar.ArrowButton.RightHot;
  247. break;
  248. case PushButtonState.Pressed:
  249. element = VisualStyleElement.ScrollBar.ArrowButton.RightPressed;
  250. break;
  251. default:
  252. element = VisualStyleElement.ScrollBar.ArrowButton.RightNormal;
  253. break;
  254. }
  255. if (!VisualStyleRenderer.IsElementDefined (element)) {
  256. base.DrawScrollButton (dc, bounds, clippingArea, button, state);
  257. return;
  258. }
  259. }
  260. new VisualStyleRenderer (element).DrawBackground (dc, bounds, clippingArea);
  261. }
  262. }
  263. }