Fix problems with overlong directory names: phase #1
[mono.git] / mcs / class / Managed.Windows.Forms / System.Windows.Forms / TextRenderer.cs
1 //
2 // TextRenderer.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 #if NET_2_0
30 using System.Drawing;
31 using System.Runtime.InteropServices;
32 using System.Text;
33 using System.Drawing.Text;
34
35 namespace System.Windows.Forms
36 {
37         public sealed class TextRenderer
38         {
39                 private TextRenderer ()
40                 {
41                 }
42                 
43                 #region Public Methods
44                 [MonoTODO("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
45                 public static void DrawText (IDeviceContext dc, string text, Font font, Rectangle bounds, Color foreColor, TextFormatFlags flags)
46                 {
47                         if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
48                                 Rectangle new_bounds = bounds;
49                                 new_bounds.Offset ((int)(dc as Graphics).Transform.OffsetX + 0, (int)(dc as Graphics).Transform.OffsetY + 0);
50                                 IntPtr hdc = dc.GetHdc ();
51
52                                 SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
53                                 SetBkMode (hdc, 1);     //1-Transparent, 2-Opaque
54
55                                 VisualStyles.UXTheme.RECT r = VisualStyles.UXTheme.RECT.FromRectangle (new_bounds);
56
57                                 if (font != null)
58                                         SelectObject (hdc, font.ToHfont ());
59
60                                 DrawText (hdc, text, text.Length, ref r, (int)flags);
61                                 dc.ReleaseHdc ();
62                         }
63                         else {
64                                 Graphics g;
65
66                                 if (dc is Graphics)
67                                         g = (Graphics)dc;
68                                 else
69                                         g = Graphics.FromHdc (dc.GetHdc ());
70
71                                 StringFormat sf = FlagsToStringFormat (flags);
72
73                                 using (Brush b = new SolidBrush (foreColor))
74                                         g.DrawString (text, font, b, bounds, sf);
75
76                                 if (!(dc is Graphics)) {
77                                         g.Dispose ();
78                                         dc.ReleaseHdc ();
79                                 }
80                         }
81                 }
82
83                 [MonoTODO ("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
84                 public static void DrawText (IDeviceContext dc, string text, Font font, Point pt, Color foreColor, TextFormatFlags flags)
85                 {
86                         if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
87                                 IntPtr hdc = dc.GetHdc ();
88
89                                 SetTextColor (hdc, ColorTranslator.ToWin32 (foreColor));
90                                 SetBkMode (hdc, 1);     //1-Transparent, 2-Opaque
91
92                                 Size sz = MeasureText(text, font);
93                                 
94                                 VisualStyles.UXTheme.RECT r = new System.Windows.Forms.VisualStyles.UXTheme.RECT(pt.X, pt.Y, pt.X + sz.Width, pt.Y + sz.Height);
95
96                                 if (font != null)
97                                         SelectObject (hdc, font.ToHfont ());
98
99                                 DrawText (hdc, text, text.Length, ref r, (int)flags);
100                                 dc.ReleaseHdc ();
101                         }
102                         else {
103                                 Graphics g;
104
105                                 if (dc is Graphics)
106                                         g = (Graphics)dc;
107                                 else
108                                         g = Graphics.FromHdc (dc.GetHdc ());
109
110                                 StringFormat sf = FlagsToStringFormat (flags);
111
112                                 using (Brush b = new SolidBrush (foreColor))
113                                         g.DrawString (text, font, b, pt, sf);
114
115                                 if (!(dc is Graphics)) {
116                                         g.Dispose ();
117                                         dc.ReleaseHdc ();
118                                 }
119                         }
120                 }
121
122                 [MonoTODO ("This should be correct for Windows, other platforms need a more accurate fallback method than the one provided")]
123                 public static Size MeasureText (string text, Font font)
124                 {
125                         if (Environment.OSVersion.Platform == PlatformID.Win32NT || Environment.OSVersion.Platform == PlatformID.Win32Windows) {
126                                 Bitmap b = new Bitmap (5, 5);
127                                 Graphics g = Graphics.FromImage (b);
128                                 
129                                 IntPtr hdc = g.GetHdc ();
130
131                                 if (font != null)
132                                         SelectObject (hdc, font.ToHfont ());
133                                         
134                                 VisualStyles.UXTheme.SIZE text_size = new System.Windows.Forms.VisualStyles.UXTheme.SIZE();
135
136                                 GetTextExtentPoint32 (hdc, text, text.Length, out text_size);
137                                 
138                                 g.ReleaseHdc();
139                                 
140                                 Size retval = text_size.ToSize();
141                                 //retval.Height += 4;
142                                 if (retval.Width > 0) retval.Width += 6;
143                                 return retval;
144                         }
145                         else {
146                                 Bitmap b = new Bitmap (5, 5);
147                                 Graphics g = Graphics.FromImage (b);
148
149                                 Size retval = g.MeasureString(text,font).ToSize();
150                                 if (retval.Width > 0) retval.Width += 6;
151                                 return retval;  
152                         }
153                 }
154                 #endregion
155
156                 #region Private Methods
157                 private static StringFormat FlagsToStringFormat (TextFormatFlags flags)
158                 {
159                         StringFormat sf = new StringFormat ();
160
161                         // Translation table: http://msdn.microsoft.com/msdnmag/issues/06/03/TextRendering/default.aspx?fig=true#fig4
162
163                         // Horizontal Alignment
164                         if ((flags & TextFormatFlags.HorizontalCenter) == TextFormatFlags.HorizontalCenter)
165                                 sf.Alignment = StringAlignment.Center;
166                         else if ((flags & TextFormatFlags.Right) == TextFormatFlags.Right)
167                                 sf.Alignment = StringAlignment.Far;
168                         else
169                                 sf.Alignment = StringAlignment.Near;
170
171                         // Vertical Alignment
172                         if ((flags & TextFormatFlags.Bottom) == TextFormatFlags.Bottom)
173                                 sf.LineAlignment = StringAlignment.Far;
174                         else if ((flags & TextFormatFlags.VerticalCenter) == TextFormatFlags.VerticalCenter)
175                                 sf.LineAlignment = StringAlignment.Center;
176                         else
177                                 sf.LineAlignment = StringAlignment.Near;
178
179                         // Ellipsis
180                         if ((flags & TextFormatFlags.EndEllipsis) == TextFormatFlags.EndEllipsis)
181                                 sf.Trimming = StringTrimming.EllipsisCharacter;
182                         else if ((flags & TextFormatFlags.PathEllipsis) == TextFormatFlags.PathEllipsis)
183                                 sf.Trimming = StringTrimming.EllipsisPath;
184                         else if ((flags & TextFormatFlags.WordEllipsis) == TextFormatFlags.WordEllipsis)
185                                 sf.Trimming = StringTrimming.EllipsisWord;
186                         else
187                                 sf.Trimming = StringTrimming.Character;
188
189                         // Hotkey Prefix
190                         if ((flags & TextFormatFlags.NoPrefix) == TextFormatFlags.NoPrefix)
191                                 sf.HotkeyPrefix = HotkeyPrefix.None;
192                         else if ((flags & TextFormatFlags.HidePrefix) == TextFormatFlags.HidePrefix)
193                                 sf.HotkeyPrefix = HotkeyPrefix.Hide;
194                         else
195                                 sf.HotkeyPrefix = HotkeyPrefix.Show;
196
197                         // Text Padding
198                         if ((flags & TextFormatFlags.NoPadding) == TextFormatFlags.NoPadding)
199                                 sf.FormatFlags |= StringFormatFlags.FitBlackBox;
200
201                         // Text Wrapping
202                         if ((flags & TextFormatFlags.SingleLine) == TextFormatFlags.SingleLine)
203                                 sf.FormatFlags |= StringFormatFlags.NoWrap;
204                         else if ((flags & TextFormatFlags.TextBoxControl) == TextFormatFlags.TextBoxControl)
205                                 sf.FormatFlags |= StringFormatFlags.LineLimit;
206
207                         // Other Flags
208                         if ((flags & TextFormatFlags.RightToLeft) == TextFormatFlags.RightToLeft)
209                                 sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
210                         if ((flags & TextFormatFlags.NoClipping) == TextFormatFlags.NoClipping)
211                                 sf.FormatFlags |= StringFormatFlags.NoClip;
212                         sf.FormatFlags |= StringFormatFlags.NoClip;
213
214                         return sf;
215                 }
216                 #endregion
217
218                 #region DllImports (Windows)
219                 [DllImport ("user32", CharSet = CharSet.Unicode)]
220                 static extern int DrawText (IntPtr hdc, string lpStr, int nCount, ref VisualStyles.UXTheme.RECT lpRect, int wFormat);
221
222                 [DllImport ("gdi32")]
223                 static extern int SetTextColor (IntPtr hdc, int crColor);
224
225                 [DllImport ("gdi32")]
226                 private extern static IntPtr SelectObject (IntPtr hDC, IntPtr hObject);
227
228                 [DllImport ("gdi32")]
229                 static extern int SetBkColor (IntPtr hdc, int crColor);
230
231                 [DllImport ("gdi32")]
232                 static extern int SetBkMode (IntPtr hdc, int iBkMode);
233
234                 [DllImport ("gdi32")]
235                 static extern bool GetTextExtentExPoint (IntPtr hdc, string lpszStr, int cchString, int nMaxExtent, IntPtr lpnFit, IntPtr alpDx, out VisualStyles.UXTheme.SIZE lpSize);
236
237                 [DllImport ("gdi32")]
238                 static extern bool GetTextExtentPoint32 (IntPtr hdc, string lpString, int cbString, out VisualStyles.UXTheme.SIZE lpSize);
239                 #endregion
240         }
241
242 }
243 #endif