2008-09-24 Jonathan Pobst <monkey@jpobst.com>
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms.Theming / Default / TabControlPainter.cs
1 // Permission is hereby granted, free of charge, to any person obtaining\r
2 // a copy of this software and associated documentation files (the\r
3 // "Software"), to deal in the Software without restriction, including\r
4 // without limitation the rights to use, copy, modify, merge, publish,\r
5 // distribute, sublicense, and/or sell copies of the Software, and to\r
6 // permit persons to whom the Software is furnished to do so, subject to\r
7 // the following conditions:\r
8 //\r
9 // The above copyright notice and this permission notice shall be\r
10 // included in all copies or substantial portions of the Software.\r
11 //\r
12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
13 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
14 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
15 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE\r
16 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION\r
17 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION\r
18 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
19 //\r
20 // Copyright (c) 2007 Novell, Inc.\r
21 //\r
22 // Authors:\r
23 //      Andreia Gaita (avidigal@novell.com)\r
24 \r
25 using System;\r
26 using System.Drawing;\r
27 using System.Drawing.Text;\r
28 using System.Windows.Forms;\r
29 using System.Windows.Forms.VisualStyles;\r
30 \r
31 namespace System.Windows.Forms.Theming.Default\r
32 {\r
33         /// <summary>\r
34         /// Summary description for TabControl.\r
35         /// </summary>\r
36         internal class TabControlPainter {\r
37                 protected SystemResPool ResPool { get { return ThemeEngine.Current.ResPool; } }\r
38 \r
39                 #region private\r
40 \r
41                 private Size    defaultItemSize;\r
42                 private Point   defaultPadding;\r
43                 private int             minimumTabWidth;\r
44                 private Rectangle selectedTabDelta;\r
45 \r
46                 private Point   tabPanelOffset;\r
47                 \r
48                 private int             selectedSpacing;\r
49 \r
50                 private Size    rowSpacingNormal;\r
51                 private Size    rowSpacingButtons;\r
52                 private Size    rowSpacingFlatButtons;\r
53                 private int             scrollerWidth;\r
54                 private Point   focusRectSpacing;\r
55                 private Rectangle tabPageSpacing;\r
56                 private int             colSpacing;\r
57                 private int             flatButtonSpacing;\r
58 \r
59                 private Point   imagePadding;\r
60 \r
61                 private StringFormat defaultFormatting;\r
62 \r
63                 private Rectangle borderThickness;\r
64 \r
65                 #endregion\r
66 \r
67                 #region Properties\r
68 \r
69                 public virtual Size DefaultItemSize {\r
70                         get { return defaultItemSize; }\r
71                         set { defaultItemSize = value; }\r
72                 }\r
73 \r
74                 public virtual Point DefaultPadding {\r
75                         get { return defaultPadding; }\r
76                         set { defaultPadding = value; }\r
77                 }\r
78 \r
79                 public virtual int MinimumTabWidth {\r
80                         get { return minimumTabWidth; }\r
81                         set { minimumTabWidth = value; }\r
82                 }\r
83 \r
84                 public virtual Rectangle SelectedTabDelta {\r
85                         get { return selectedTabDelta; }\r
86                         set { selectedTabDelta = value; }\r
87                 }\r
88 \r
89                 public virtual Point TabPanelOffset {\r
90                         get { return tabPanelOffset; }\r
91                         set { tabPanelOffset = value; }\r
92                 }\r
93 \r
94                 public virtual int SelectedSpacing {\r
95                         get { return selectedSpacing; }\r
96                         set { selectedSpacing = value; }\r
97                 }\r
98 \r
99                 public virtual Size RowSpacingNormal { \r
100                         get { return rowSpacingNormal; }\r
101                         set { rowSpacingNormal = value; }\r
102                 }\r
103 \r
104                 public virtual Size RowSpacingButtons { \r
105                         get { return rowSpacingButtons; }\r
106                         set { rowSpacingButtons = value; }\r
107                 }\r
108 \r
109                 public virtual Size RowSpacingFlatButtons { \r
110                         get { return rowSpacingFlatButtons; }\r
111                         set { rowSpacingFlatButtons = value; }\r
112                 }\r
113 \r
114                 public virtual Point FocusRectSpacing {\r
115                         get { return focusRectSpacing; }\r
116                         set { focusRectSpacing = value; }\r
117                 }\r
118 \r
119                 public virtual int ColSpacing { \r
120                         get { return colSpacing; }\r
121                         set { colSpacing = value; }\r
122                 }\r
123 \r
124                 public virtual int FlatButtonSpacing { \r
125                         get { return flatButtonSpacing; }\r
126                         set { flatButtonSpacing = value; }\r
127                 }\r
128 \r
129                 public virtual Rectangle TabPageSpacing {\r
130                         get { return tabPageSpacing; }\r
131                         set { tabPageSpacing = value; }\r
132                 }\r
133 \r
134                 public virtual Point ImagePadding {\r
135                         get { return imagePadding; }\r
136                         set { imagePadding = value; }\r
137                 }\r
138 \r
139                 public virtual StringFormat DefaultFormatting {\r
140                         get { return defaultFormatting; }\r
141                         set { defaultFormatting = value; }\r
142                 }\r
143 \r
144                 public virtual Rectangle BorderThickness {\r
145                         get { return borderThickness; }\r
146                         set { borderThickness = value; }\r
147                 }\r
148 \r
149                 public virtual int ScrollerWidth {\r
150                         get { return scrollerWidth; }\r
151                         set { scrollerWidth = value; }\r
152                 }\r
153 \r
154                 public virtual Size RowSpacing (System.Windows.Forms.TabControl tab) {\r
155                         switch (tab.Appearance) {\r
156                                 case TabAppearance.Normal:\r
157                                         return rowSpacingNormal;\r
158                                 case TabAppearance.Buttons:\r
159                                         return rowSpacingButtons;\r
160                                 case TabAppearance.FlatButtons:\r
161                                         return rowSpacingFlatButtons;\r
162                                 default:\r
163                                         throw new Exception ("Invalid Appearance value: " + tab.Appearance);\r
164                         }\r
165                 }\r
166                 #endregion\r
167 \r
168                 public TabControlPainter ()\r
169                 {\r
170                         defaultItemSize = new Size (42, 21);\r
171                         defaultPadding = new Point (6, 3);\r
172                         selectedTabDelta = new Rectangle (2, 2, 4, 3);\r
173                         selectedSpacing = 0;\r
174 \r
175                         rowSpacingNormal = new Size (0, 0);\r
176                         rowSpacingButtons = new Size (3, 3);\r
177                         rowSpacingFlatButtons = new Size (9, 3);\r
178                         \r
179                         colSpacing = 0;\r
180 \r
181                         minimumTabWidth = 42;\r
182                         scrollerWidth = 17;\r
183                         focusRectSpacing = new Point (2, 2);\r
184                         tabPanelOffset = new Point (4, 0);\r
185                         flatButtonSpacing = 8;\r
186                         tabPageSpacing = new Rectangle (4, 2, 3, 4);\r
187 \r
188                         imagePadding = new Point (2, 3);\r
189 \r
190                         defaultFormatting = new StringFormat();\r
191                         defaultFormatting.Alignment = StringAlignment.Near;\r
192                         defaultFormatting.LineAlignment = StringAlignment.Near;\r
193                         defaultFormatting.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.NoClip;\r
194                         defaultFormatting.HotkeyPrefix = HotkeyPrefix.Show;\r
195 \r
196                         borderThickness = new Rectangle (1, 1, 2, 2);\r
197                 }\r
198 \r
199                 public virtual Rectangle GetLeftScrollRect (System.Windows.Forms.TabControl tab)\r
200                 {\r
201                         switch (tab.Alignment) {\r
202                                 case TabAlignment.Top:\r
203                                         return new Rectangle (tab.ClientRectangle.Right - (scrollerWidth * 2), tab.ClientRectangle.Top + 1, scrollerWidth, scrollerWidth);\r
204                                 default:\r
205                                         Rectangle panel_rect = GetTabPanelRect (tab);\r
206                                         return new Rectangle (tab.ClientRectangle.Right - (scrollerWidth * 2), panel_rect.Bottom + 2, scrollerWidth, scrollerWidth);\r
207                         }\r
208                 }\r
209 \r
210                 public virtual Rectangle GetRightScrollRect (System.Windows.Forms.TabControl tab)\r
211                 {\r
212                         switch (tab.Alignment) {\r
213                                 case TabAlignment.Top:\r
214                                         return new Rectangle (tab.ClientRectangle.Right - (scrollerWidth), tab.ClientRectangle.Top + 1, scrollerWidth, scrollerWidth);\r
215                                 default:\r
216                                         Rectangle panel_rect = GetTabPanelRect (tab);\r
217                                         return new Rectangle (tab.ClientRectangle.Right - (scrollerWidth), panel_rect.Bottom + 2, scrollerWidth, scrollerWidth);\r
218                         }\r
219                 }\r
220 \r
221                 public Rectangle GetDisplayRectangle (System.Windows.Forms.TabControl tab)\r
222                 {\r
223                         Rectangle ext = GetTabPanelRect (tab);\r
224                         // Account for border size\r
225                         return new Rectangle (ext.Left + tabPageSpacing.X, ext.Top + tabPageSpacing.Y, \r
226                                 ext.Width - tabPageSpacing.X - tabPageSpacing.Width, ext.Height - tabPageSpacing.Y - tabPageSpacing.Height);\r
227                 }\r
228                 \r
229                 public Rectangle GetTabPanelRect (System.Windows.Forms.TabControl tab)\r
230                 {       \r
231                         // Offset the tab page (panel) from the top corner\r
232                         Rectangle res = new Rectangle (tab.ClientRectangle.X,\r
233                                 tab.ClientRectangle.Y,\r
234                                 tab.ClientRectangle.Width,\r
235                                 tab.ClientRectangle.Height);\r
236 \r
237                         if (tab.TabCount == 0)\r
238                                 return res;\r
239 \r
240                         int spacing = RowSpacing (tab).Height;\r
241                         int tabOffset = (tab.ItemSize.Height + spacing - selectedTabDelta.Height) * tab.RowCount + selectedTabDelta.Y;\r
242                         switch (tab.Alignment) {\r
243                                 case TabAlignment.Top:\r
244                                         res.Y += tabOffset;\r
245                                         res.Height -= tabOffset;\r
246                                         break;\r
247                                 case TabAlignment.Bottom:\r
248                                         res.Height -= tabOffset;\r
249                                         break;\r
250                                 case TabAlignment.Left:\r
251                                         res.X += tabOffset;\r
252                                         res.Width -= tabOffset;\r
253                                         break;\r
254                                 case TabAlignment.Right:\r
255                                         res.Width -= tabOffset;\r
256                                         break;\r
257                         }\r
258 \r
259                         return res;\r
260                 }\r
261 \r
262                 public virtual void Draw (Graphics dc, Rectangle area, TabControl tab)\r
263                 {\r
264                         DrawBackground (dc, area, tab);\r
265 \r
266                         int start = 0;\r
267                         int end = tab.TabPages.Count;\r
268                         int delta = 1;\r
269 \r
270                         if (tab.Alignment == TabAlignment.Top) {\r
271                                 start = end;\r
272                                 end = 0;\r
273                                 delta = -1;\r
274                         }\r
275                         int counter = start;\r
276                         for (; counter != end; counter += delta) {\r
277                                 for (int i = tab.SliderPos; i < tab.TabPages.Count; i++) {\r
278                                         if (i == tab.SelectedIndex)\r
279                                                 continue;\r
280                                         if (counter != tab.TabPages[i].Row)\r
281                                                 continue;\r
282                                         Rectangle rect = tab.GetTabRect (i);\r
283                                         if (!rect.IntersectsWith (area))\r
284                                                 continue;\r
285                                         DrawTab (dc, tab.TabPages[i], tab, rect, false);\r
286                                 }\r
287                         }\r
288 \r
289                         if (tab.SelectedIndex != -1 && tab.SelectedIndex >= tab.SliderPos) {\r
290                                 Rectangle rect = tab.GetTabRect (tab.SelectedIndex);\r
291                                 if (rect.IntersectsWith (area))\r
292                                         DrawTab (dc, tab.TabPages[tab.SelectedIndex], tab, rect, true);\r
293                         }\r
294 \r
295                         if (tab.ShowSlider) {\r
296                                 Rectangle right = GetRightScrollRect (tab);\r
297                                 Rectangle left = GetLeftScrollRect (tab);\r
298                                 DrawScrollButton (dc, right, area, ScrollButton.Right, tab.RightSliderState);\r
299                                 DrawScrollButton (dc, left, area, ScrollButton.Left, tab.LeftSliderState);\r
300                         }\r
301                 }\r
302 \r
303                 protected virtual void DrawScrollButton (Graphics dc, Rectangle bounds, Rectangle clippingArea, ScrollButton button, PushButtonState state)\r
304                 {\r
305                         ControlPaint.DrawScrollButton (dc, bounds, button, GetButtonState (state));\r
306                 }\r
307 \r
308                 static ButtonState GetButtonState (PushButtonState state)\r
309                 {\r
310                         switch (state) {\r
311                         case PushButtonState.Pressed:\r
312                                 return ButtonState.Pushed;\r
313                         default:\r
314                                 return ButtonState.Normal;\r
315                         }\r
316                 }\r
317 \r
318                 protected virtual void DrawBackground (Graphics dc, Rectangle area, TabControl tab)\r
319                 {\r
320                         Brush brush = SystemBrushes.Control;\r
321                         dc.FillRectangle (brush, area);\r
322                         Rectangle panel_rect = GetTabPanelRect (tab);\r
323 \r
324                         if (tab.Appearance == TabAppearance.Normal) {\r
325                                 ControlPaint.DrawBorder3D (dc, panel_rect, Border3DStyle.RaisedInner, Border3DSide.Left | Border3DSide.Top);\r
326                                 ControlPaint.DrawBorder3D (dc, panel_rect, Border3DStyle.Raised, Border3DSide.Right | Border3DSide.Bottom);\r
327                         }\r
328                 }\r
329 \r
330                 protected virtual int DrawTab (Graphics dc, System.Windows.Forms.TabPage page, System.Windows.Forms.TabControl tab, Rectangle bounds, bool is_selected)\r
331                 {\r
332                         Rectangle interior;\r
333                         int res = bounds.Width;\r
334 \r
335                         dc.FillRectangle (ResPool.GetSolidBrush (tab.BackColor), bounds);\r
336 \r
337                         if (tab.Appearance == TabAppearance.Buttons || tab.Appearance == TabAppearance.FlatButtons) {\r
338                                 // Separators\r
339                                 if (tab.Appearance == TabAppearance.FlatButtons) {\r
340                                         int width = bounds.Width;\r
341                                         bounds.Width += (flatButtonSpacing - 2);\r
342                                         res = bounds.Width;\r
343                                         if (tab.Alignment == TabAlignment.Top || tab.Alignment == TabAlignment.Bottom)\r
344                                                 ThemeEngine.Current.CPDrawBorder3D (dc, bounds, Border3DStyle.Etched, Border3DSide.Right);\r
345                                         else\r
346                                                 ThemeEngine.Current.CPDrawBorder3D (dc, bounds, Border3DStyle.Etched, Border3DSide.Top);\r
347                                         bounds.Width = width;\r
348                                 }\r
349 \r
350                                 if (is_selected) {\r
351                                         ThemeEngine.Current.CPDrawBorder3D (dc, bounds, Border3DStyle.Sunken, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);\r
352                                 } else if (tab.Appearance != TabAppearance.FlatButtons) {\r
353                                         ThemeEngine.Current.CPDrawBorder3D (dc, bounds, Border3DStyle.Raised, Border3DSide.Left | Border3DSide.Right | Border3DSide.Top | Border3DSide.Bottom);\r
354                                 }\r
355 \r
356 \r
357                         } else {\r
358                                 CPColor cpcolor = ResPool.GetCPColor (tab.BackColor);\r
359 \r
360                                 Pen light = ResPool.GetPen (cpcolor.LightLight);\r
361 \r
362                                 switch (tab.Alignment) {\r
363 \r
364                                         case TabAlignment.Top:\r
365 \r
366                                                 dc.DrawLine (light, bounds.Left, bounds.Bottom - 1, bounds.Left, bounds.Top + 3);\r
367                                                 dc.DrawLine (light, bounds.Left, bounds.Top + 3, bounds.Left + 2, bounds.Top);\r
368                                                 dc.DrawLine (light, bounds.Left + 2, bounds.Top, bounds.Right - 3, bounds.Top);\r
369 \r
370                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Right - 2, bounds.Top + 1, bounds.Right - 2, bounds.Bottom - 1);\r
371                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 2, bounds.Top + 1, bounds.Right - 1, bounds.Top + 2);\r
372                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 1, bounds.Top + 2, bounds.Right - 1, bounds.Bottom - 1);\r
373                                                 break;\r
374 \r
375                                         case TabAlignment.Bottom:\r
376 \r
377                                                 dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Left, bounds.Bottom - 2);\r
378                                                 dc.DrawLine (light, bounds.Left, bounds.Bottom - 2, bounds.Left + 3, bounds.Bottom);\r
379                                                 \r
380                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 3, bounds.Bottom, bounds.Right - 3, bounds.Bottom);\r
381                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Left + 3, bounds.Bottom - 1, bounds.Right - 3, bounds.Bottom - 1);\r
382 \r
383                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Right - 2, bounds.Bottom - 1, bounds.Right - 2, bounds.Top + 1);\r
384                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 2, bounds.Bottom - 1, bounds.Right - 1, bounds.Bottom - 2);\r
385                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right - 1, bounds.Bottom - 2, bounds.Right - 1, bounds.Top + 1);\r
386 \r
387                                                 break;\r
388 \r
389                                         case TabAlignment.Left:\r
390 \r
391                                                 dc.DrawLine (light, bounds.Left - 2, bounds.Top, bounds.Right, bounds.Top);\r
392                                                 dc.DrawLine (light, bounds.Left, bounds.Top + 2, bounds.Left - 2, bounds.Top);\r
393                                                 dc.DrawLine (light, bounds.Left, bounds.Top + 2, bounds.Left, bounds.Bottom - 2);\r
394 \r
395                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Left, bounds.Bottom - 2, bounds.Left + 2, bounds.Bottom - 1);\r
396 \r
397                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Left + 2, bounds.Bottom - 1, bounds.Right, bounds.Bottom - 1);\r
398                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left + 2, bounds.Bottom, bounds.Right, bounds.Bottom);\r
399 \r
400                                                 break;\r
401 \r
402                                         default: // TabAlignment.Right\r
403 \r
404                                                 dc.DrawLine (light, bounds.Left, bounds.Top, bounds.Right - 3, bounds.Top);\r
405                                                 dc.DrawLine (light, bounds.Right - 3, bounds.Top, bounds.Right, bounds.Top + 3);\r
406 \r
407                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Right - 1, bounds.Top + 1, bounds.Right - 1, bounds.Bottom - 1);\r
408                                                 dc.DrawLine (SystemPens.ControlDark, bounds.Left, bounds.Bottom - 1, bounds.Right - 2, bounds.Bottom - 1);\r
409 \r
410                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Right, bounds.Top + 3, bounds.Right, bounds.Bottom - 3);\r
411                                                 dc.DrawLine (SystemPens.ControlDarkDark, bounds.Left, bounds.Bottom, bounds.Right - 3, bounds.Bottom);\r
412 \r
413                                                 break;\r
414                                 }\r
415                         }\r
416 \r
417                         interior = new Rectangle (bounds.Left + focusRectSpacing.X + borderThickness.Left, \r
418                                 bounds.Top + focusRectSpacing.Y +  + borderThickness.Top,\r
419                                 bounds.Width - (focusRectSpacing.X * 2) - borderThickness.Width + 1, \r
420                                 bounds.Height - (focusRectSpacing.Y * 2) - borderThickness.Height);\r
421 \r
422                         if (tab.DrawMode == TabDrawMode.Normal && page.Text != null) {\r
423                                 if (tab.Alignment == TabAlignment.Left) {\r
424                                         dc.TranslateTransform (bounds.Left, bounds.Bottom);\r
425                                         dc.RotateTransform (-90);\r
426                                         dc.DrawString (page.Text, page.Font,\r
427                                                 SystemBrushes.ControlText, \r
428                                                 tab.Padding.X - 2, // drawstring adds some extra unwanted leading spaces, so trimming\r
429                                                 tab.Padding.Y,\r
430                                                 defaultFormatting);\r
431                                         dc.ResetTransform ();\r
432                                 } else if (tab.Alignment == TabAlignment.Right) {\r
433                                         dc.TranslateTransform (bounds.Right, bounds.Top);\r
434                                         dc.RotateTransform (90);\r
435                                         dc.DrawString (page.Text, page.Font,\r
436                                                 SystemBrushes.ControlText, \r
437                                                 tab.Padding.X - 2, // drawstring adds some extra unwanted leading spaces, so trimming\r
438                                                 tab.Padding.Y,\r
439                                                 defaultFormatting);\r
440                                         dc.ResetTransform ();\r
441                                 } else {\r
442                                         Rectangle str_rect = interior;\r
443 \r
444                                         if (tab.ImageList != null && page.ImageIndex >= 0 && page.ImageIndex < tab.ImageList.Images.Count) {\r
445                                                 int image_y = interior.Y + (interior.Height - tab.ImageList.ImageSize.Height) / 2;\r
446                                                 tab.ImageList.Draw (dc, new Point (interior.X, image_y), page.ImageIndex);\r
447                                                 str_rect.X += tab.ImageList.ImageSize.Width + 2;\r
448                                                 str_rect.Width -= tab.ImageList.ImageSize.Width + 2;\r
449                                         }\r
450                                         dc.DrawString (page.Text, page.Font,\r
451                                                 SystemBrushes.ControlText,\r
452                                                 str_rect, \r
453                                                 defaultFormatting);\r
454 \r
455                                 }\r
456                         } else if (page.Text != null) {\r
457                                 DrawItemState state = DrawItemState.None;\r
458                                 if (page == tab.SelectedTab)\r
459                                         state |= DrawItemState.Selected;\r
460                                 DrawItemEventArgs e = new DrawItemEventArgs (dc,\r
461                                         tab.Font, bounds, tab.IndexForTabPage (page),\r
462                                         state, page.ForeColor, page.BackColor);\r
463                                 tab.OnDrawItemInternal (e);\r
464                                 return res;\r
465                         }\r
466 \r
467                         if (page.Parent.Focused && is_selected && tab.ShowFocusCues) {\r
468                                 interior.Width -= 1;\r
469                                 ThemeEngine.Current.CPDrawFocusRectangle (dc, interior, tab.ForeColor, tab.BackColor);\r
470                         }\r
471 \r
472                         return res;\r
473                 }\r
474 \r
475                 public virtual bool HasHotElementStyles (TabControl tabControl) {\r
476                         return false;\r
477                 }\r
478         }\r
479 }\r