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