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