New test.
[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 lang)
48                 {
49                         Status status = GDIPlus.GdipCreateStringFormat (options, lang, 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                                 GDIPlus.CheckStatus (status);
74                                 
75                                 nativeStrFmt = IntPtr.Zero;
76                         }
77                 }
78
79                 public StringFormat (StringFormat source)
80                 {
81                         if (source == null)
82                                 throw new ArgumentNullException ("source");
83
84                         Status status = GDIPlus.GdipCloneStringFormat (source.NativeObject, out nativeStrFmt);
85                         GDIPlus.CheckStatus (status);
86                 }
87
88                 public StringFormat (StringFormatFlags flags)
89                 {
90                         Status status = GDIPlus.GdipCreateStringFormat (flags, GDIPlus.LANG_NEUTRAL, out nativeStrFmt);
91                         GDIPlus.CheckStatus (status);                   
92                 }
93                 
94                 public StringAlignment Alignment {
95                         get {
96                                 StringAlignment align;
97                                 Status status = GDIPlus.GdipGetStringFormatAlign (nativeStrFmt, out align);
98                                 GDIPlus.CheckStatus (status);
99
100                                 return align;
101                         }
102
103                         set {
104                                 if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
105                                         throw new InvalidEnumArgumentException ("Alignment");
106
107                                 Status status = GDIPlus.GdipSetStringFormatAlign (nativeStrFmt, value);
108                                 GDIPlus.CheckStatus (status);
109                         }
110                 }
111
112                 public StringAlignment LineAlignment {
113                         get {
114                                 StringAlignment align;
115                                 Status status = GDIPlus.GdipGetStringFormatLineAlign (nativeStrFmt, out align);
116                                 GDIPlus.CheckStatus (status);
117
118                                 return align;
119                         }
120
121                         set {
122                                 if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
123                                         throw new InvalidEnumArgumentException ("Alignment");
124
125                                 Status status = GDIPlus.GdipSetStringFormatLineAlign (nativeStrFmt, value);
126                                 GDIPlus.CheckStatus (status);
127                         }
128                 }
129
130                 public StringFormatFlags FormatFlags {
131                         get {                           
132                                 StringFormatFlags flags;
133                                 Status status = GDIPlus.GdipGetStringFormatFlags (nativeStrFmt, out flags);
134                                 GDIPlus.CheckStatus (status);
135
136                                 return flags;                   
137                         }
138
139                         set {
140                                 Status status = GDIPlus.GdipSetStringFormatFlags (nativeStrFmt, value);
141                                 GDIPlus.CheckStatus (status);
142                         }
143                 }
144
145                 public HotkeyPrefix HotkeyPrefix {
146                         get {                           
147                                 HotkeyPrefix hotkeyPrefix;
148                                 Status status = GDIPlus.GdipGetStringFormatHotkeyPrefix (nativeStrFmt, out hotkeyPrefix);
149                                 GDIPlus.CheckStatus (status);
150
151                                 return hotkeyPrefix;
152                         }
153
154                         set {
155                                 if ((value < HotkeyPrefix.None) || (value > HotkeyPrefix.Hide))
156                                         throw new InvalidEnumArgumentException ("HotkeyPrefix");
157
158                                 Status status = GDIPlus.GdipSetStringFormatHotkeyPrefix (nativeStrFmt, value);
159                                 GDIPlus.CheckStatus (status);
160                         }
161                 }
162
163
164                 public StringTrimming Trimming {
165                         get {
166                                 StringTrimming trimming;
167                                 Status status = GDIPlus.GdipGetStringFormatTrimming (nativeStrFmt, out trimming);
168                                 GDIPlus.CheckStatus (status);
169                                 return trimming;
170                         }
171
172                         set {
173                                 if ((value < StringTrimming.None) || (value > StringTrimming.EllipsisPath))
174                                         throw new InvalidEnumArgumentException ("Trimming");
175
176                                 Status status = GDIPlus.GdipSetStringFormatTrimming (nativeStrFmt, value);
177                                 GDIPlus.CheckStatus (status);
178                         }
179                 }
180
181                 public static StringFormat GenericDefault {
182                         get {
183                                 IntPtr ptr;
184                                 
185                                 Status status = GDIPlus.GdipStringFormatGetGenericDefault (out ptr);
186                                 GDIPlus.CheckStatus (status);
187         
188                                 return new StringFormat (ptr);
189                         }
190                 }
191                 
192                 
193                 public int DigitSubstitutionLanguage {
194                         get{
195                                 return language;
196                         }
197                 }
198
199                 
200                 public static StringFormat GenericTypographic {
201                         get {
202                                 IntPtr ptr;
203                                                 
204                                 Status status = GDIPlus.GdipStringFormatGetGenericTypographic (out ptr);
205                                 GDIPlus.CheckStatus (status);
206         
207                                 return new StringFormat (ptr);
208                         }
209                 }
210
211                 public StringDigitSubstitute  DigitSubstitutionMethod  {
212                         get {
213                                 StringDigitSubstitute substitute;
214                                 
215                                 Status status = GDIPlus.GdipGetStringFormatDigitSubstitution(nativeStrFmt, language, out substitute);
216                                 GDIPlus.CheckStatus (status);
217
218                                 return substitute;     
219                         }
220                 }
221
222
223                 public void SetMeasurableCharacterRanges (CharacterRange [] range)
224                 {                                       
225                         Status status = GDIPlus.GdipSetStringFormatMeasurableCharacterRanges (nativeStrFmt, 
226                                 range.Length,   range);
227                                 
228                         GDIPlus.CheckStatus (status);
229                 }
230                 
231                 internal int GetMeasurableCharacterRangeCount () 
232                 {
233                         int cnt;                
234                         Status status = GDIPlus.GdipGetStringFormatMeasurableCharacterRangeCount (nativeStrFmt, out cnt);
235                                 
236                         GDIPlus.CheckStatus (status);                   
237                         return cnt;                     
238                 }                       
239                         
240                 public object Clone()
241                 {
242                         IntPtr native;
243                                 
244                         Status status = GDIPlus.GdipCloneStringFormat (nativeStrFmt, out native);
245                         GDIPlus.CheckStatus (status);
246         
247                         return new StringFormat (native);                       
248                 }
249
250                 public override string ToString()
251                 {
252                         return "[StringFormat, FormatFlags=" + this.FormatFlags.ToString() + "]";
253                 }
254                 
255                 internal IntPtr NativeObject
256                 {            
257                         get{
258                                 return nativeStrFmt;
259                         }
260                         set     {
261                                 nativeStrFmt = value;
262                         }
263                 }
264
265                 public void SetTabStops(float firstTabOffset, float[] tabStops)
266                 {
267                         Status status = GDIPlus.GdipSetStringFormatTabStops(nativeStrFmt, firstTabOffset, tabStops.Length, tabStops);
268                         GDIPlus.CheckStatus (status);
269                 }
270
271                 public void SetDigitSubstitution(int language,  StringDigitSubstitute substitute)
272                 {
273                         Status status = GDIPlus.GdipSetStringFormatDigitSubstitution(nativeStrFmt, this.language, substitute);
274                         GDIPlus.CheckStatus (status);
275                 }
276
277                 public float[] GetTabStops(out float firstTabOffset)
278                 {
279                         int count = 0;
280                         firstTabOffset = 0;
281                         
282                         Status status = GDIPlus.GdipGetStringFormatTabStopCount(nativeStrFmt, out count);
283                         GDIPlus.CheckStatus (status);
284
285                         float[] tabStops = new float[count];                        
286                         
287                         if (count != 0) {                        
288                                 status = GDIPlus.GdipGetStringFormatTabStops(nativeStrFmt, count, out firstTabOffset, tabStops);
289                                 GDIPlus.CheckStatus (status);
290                         }
291                                 
292                         return tabStops;                        
293                 }
294
295         }
296 }