New tests, updates
[mono.git] / mcs / class / System.Drawing / System.Drawing / StringFormat.cs
1 //
2 // System.Drawing.StringFormat.cs
3 //
4 // Authors:
5 //   Dennis Hayes (dennish@Raytek.com)
6 //   Miguel de Icaza (miguel@ximian.com)
7 //   Jordi Mas i Hernandez (jordi@ximian.com)
8 //
9 // Copyright (C) 2002 Ximian, Inc (http://www.ximian.com)
10 // Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System.ComponentModel;
33 using System.Drawing.Text;
34
35 namespace System.Drawing {
36
37         public sealed class StringFormat : MarshalByRefObject, IDisposable, ICloneable
38         {
39                 private static StringFormat genericDefault;
40                 private IntPtr nativeStrFmt = IntPtr.Zero;
41                 private int language = GDIPlus.LANG_NEUTRAL;
42                                 
43                 public StringFormat() : this (0, GDIPlus.LANG_NEUTRAL)
44                 {                                          
45                 }               
46                 
47                 public StringFormat(StringFormatFlags options, int language)
48                 {
49                         Status status = GDIPlus.GdipCreateStringFormat (options, language, out nativeStrFmt);                           
50                         GDIPlus.CheckStatus (status);
51                 }
52                 
53                 internal StringFormat(IntPtr native)
54                 {
55                         nativeStrFmt = native;
56                 }
57                 
58                 ~StringFormat ()
59                 {       
60                         Dispose (false);
61                 }
62                 
63                 public void Dispose ()
64                 {       
65                         Dispose (true);
66                         System.GC.SuppressFinalize (this);
67                 }
68
69                 void Dispose (bool disposing)
70                 {
71                         if (nativeStrFmt != IntPtr.Zero) {
72                                 Status status = GDIPlus.GdipDeleteStringFormat (nativeStrFmt);
73                                 nativeStrFmt = IntPtr.Zero;
74                                 GDIPlus.CheckStatus (status);
75                         }
76                 }
77
78                 public StringFormat (StringFormat format)
79                 {
80                         if (format == null)
81                                 throw new ArgumentNullException ("format");
82
83                         Status status = GDIPlus.GdipCloneStringFormat (format.NativeObject, out nativeStrFmt);
84                         GDIPlus.CheckStatus (status);
85                 }
86
87                 public StringFormat (StringFormatFlags options)
88                 {
89                         Status status = GDIPlus.GdipCreateStringFormat (options, GDIPlus.LANG_NEUTRAL, out nativeStrFmt);
90                         GDIPlus.CheckStatus (status);                   
91                 }
92                 
93                 public StringAlignment Alignment {
94                         get {
95                                 StringAlignment align;
96                                 Status status = GDIPlus.GdipGetStringFormatAlign (nativeStrFmt, out align);
97                                 GDIPlus.CheckStatus (status);
98
99                                 return align;
100                         }
101
102                         set {
103                                 if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
104                                         throw new InvalidEnumArgumentException ("Alignment");
105
106                                 Status status = GDIPlus.GdipSetStringFormatAlign (nativeStrFmt, value);
107                                 GDIPlus.CheckStatus (status);
108                         }
109                 }
110
111                 public StringAlignment LineAlignment {
112                         get {
113                                 StringAlignment align;
114                                 Status status = GDIPlus.GdipGetStringFormatLineAlign (nativeStrFmt, out align);
115                                 GDIPlus.CheckStatus (status);
116
117                                 return align;
118                         }
119
120                         set {
121                                 if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
122                                         throw new InvalidEnumArgumentException ("Alignment");
123
124                                 Status status = GDIPlus.GdipSetStringFormatLineAlign (nativeStrFmt, value);
125                                 GDIPlus.CheckStatus (status);
126                         }
127                 }
128
129                 public StringFormatFlags FormatFlags {
130                         get {                           
131                                 StringFormatFlags flags;
132                                 Status status = GDIPlus.GdipGetStringFormatFlags (nativeStrFmt, out flags);
133                                 GDIPlus.CheckStatus (status);
134
135                                 return flags;                   
136                         }
137
138                         set {
139                                 Status status = GDIPlus.GdipSetStringFormatFlags (nativeStrFmt, value);
140                                 GDIPlus.CheckStatus (status);
141                         }
142                 }
143
144                 public HotkeyPrefix HotkeyPrefix {
145                         get {                           
146                                 HotkeyPrefix hotkeyPrefix;
147                                 Status status = GDIPlus.GdipGetStringFormatHotkeyPrefix (nativeStrFmt, out hotkeyPrefix);
148                                 GDIPlus.CheckStatus (status);
149
150                                 return hotkeyPrefix;
151                         }
152
153                         set {
154                                 if ((value < HotkeyPrefix.None) || (value > HotkeyPrefix.Hide))
155                                         throw new InvalidEnumArgumentException ("HotkeyPrefix");
156
157                                 Status status = GDIPlus.GdipSetStringFormatHotkeyPrefix (nativeStrFmt, value);
158                                 GDIPlus.CheckStatus (status);
159                         }
160                 }
161
162
163                 public StringTrimming Trimming {
164                         get {
165                                 StringTrimming trimming;
166                                 Status status = GDIPlus.GdipGetStringFormatTrimming (nativeStrFmt, out trimming);
167                                 GDIPlus.CheckStatus (status);
168                                 return trimming;
169                         }
170
171                         set {
172                                 if ((value < StringTrimming.None) || (value > StringTrimming.EllipsisPath))
173                                         throw new InvalidEnumArgumentException ("Trimming");
174
175                                 Status status = GDIPlus.GdipSetStringFormatTrimming (nativeStrFmt, value);
176                                 GDIPlus.CheckStatus (status);
177                         }
178                 }
179
180                 public static StringFormat GenericDefault {
181                         get {
182                                 IntPtr ptr;
183                                 
184                                 Status status = GDIPlus.GdipStringFormatGetGenericDefault (out ptr);
185                                 GDIPlus.CheckStatus (status);
186         
187                                 return new StringFormat (ptr);
188                         }
189                 }
190                 
191                 
192                 public int DigitSubstitutionLanguage {
193                         get{
194                                 return language;
195                         }
196                 }
197
198                 
199                 public static StringFormat GenericTypographic {
200                         get {
201                                 IntPtr ptr;
202                                                 
203                                 Status status = GDIPlus.GdipStringFormatGetGenericTypographic (out ptr);
204                                 GDIPlus.CheckStatus (status);
205         
206                                 return new StringFormat (ptr);
207                         }
208                 }
209
210                 public StringDigitSubstitute  DigitSubstitutionMethod  {
211                         get {
212                                 StringDigitSubstitute substitute;
213                                 
214                                 Status status = GDIPlus.GdipGetStringFormatDigitSubstitution(nativeStrFmt, language, out substitute);
215                                 GDIPlus.CheckStatus (status);
216
217                                 return substitute;     
218                         }
219                 }
220
221
222                 public void SetMeasurableCharacterRanges (CharacterRange [] ranges)
223                 {                                       
224                         Status status = GDIPlus.GdipSetStringFormatMeasurableCharacterRanges (nativeStrFmt, 
225                                 ranges.Length,  ranges);
226                                 
227                         GDIPlus.CheckStatus (status);
228                 }
229                 
230                 internal int GetMeasurableCharacterRangeCount () 
231                 {
232                         int cnt;                
233                         Status status = GDIPlus.GdipGetStringFormatMeasurableCharacterRangeCount (nativeStrFmt, out cnt);
234                                 
235                         GDIPlus.CheckStatus (status);                   
236                         return cnt;                     
237                 }                       
238                         
239                 public object Clone()
240                 {
241                         IntPtr native;
242                                 
243                         Status status = GDIPlus.GdipCloneStringFormat (nativeStrFmt, out native);
244                         GDIPlus.CheckStatus (status);
245         
246                         return new StringFormat (native);                       
247                 }
248
249                 public override string ToString()
250                 {
251                         return "[StringFormat, FormatFlags=" + this.FormatFlags.ToString() + "]";
252                 }
253                 
254                 internal IntPtr NativeObject
255                 {            
256                         get{
257                                 return nativeStrFmt;
258                         }
259                         set     {
260                                 nativeStrFmt = value;
261                         }
262                 }
263
264                 public void SetTabStops(float firstTabOffset, float[] tabStops)
265                 {
266                         Status status = GDIPlus.GdipSetStringFormatTabStops(nativeStrFmt, firstTabOffset, tabStops.Length, tabStops);
267                         GDIPlus.CheckStatus (status);
268                 }
269
270                 public void SetDigitSubstitution(int language,  StringDigitSubstitute substitute)
271                 {
272                         Status status = GDIPlus.GdipSetStringFormatDigitSubstitution(nativeStrFmt, this.language, substitute);
273                         GDIPlus.CheckStatus (status);
274                 }
275
276                 public float[] GetTabStops(out float firstTabOffset)
277                 {
278                         int count = 0;
279                         firstTabOffset = 0;
280                         
281                         Status status = GDIPlus.GdipGetStringFormatTabStopCount(nativeStrFmt, out count);
282                         GDIPlus.CheckStatus (status);
283
284                         float[] tabStops = new float[count];                        
285                         
286                         if (count != 0) {                        
287                                 status = GDIPlus.GdipGetStringFormatTabStops(nativeStrFmt, count, out firstTabOffset, tabStops);
288                                 GDIPlus.CheckStatus (status);
289                         }
290                                 
291                         return tabStops;                        
292                 }
293
294         }
295 }