Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Drawing / System.Drawing / StringFormat.cs
index f79d82c468c0913d3054d52b9ffd1afbb7f6065b..7e9a218f990f33eb2f2679bbd78d771566b8bf0d 100644 (file)
@@ -6,36 +6,48 @@
 //   Miguel de Icaza (miguel@ximian.com)
 //   Jordi Mas i Hernandez (jordi@ximian.com)
 //
-// (C) 2002 Ximian, Inc
-// (C) 2003 Novell, Inc.
+// Copyright (C) 2002 Ximian, Inc (http://www.ximian.com)
+// Copyright (C) 2004,2006 Novell, Inc (http://www.novell.com)
 //
-using System;
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System.ComponentModel;
 using System.Drawing.Text;
 
-namespace System.Drawing
-{
-       /// <summary>
-       /// Summary description for StringFormat.
-       /// </summary>
+namespace System.Drawing {
+
        public sealed class StringFormat : MarshalByRefObject, IDisposable, ICloneable
        {
-               private static StringFormat genericDefault;
+//             private static StringFormat genericDefault;
                private IntPtr nativeStrFmt = IntPtr.Zero;
                 private int language = GDIPlus.LANG_NEUTRAL;
-               
+                               
                public StringFormat() : this (0, GDIPlus.LANG_NEUTRAL)
                {                                          
-                       
                }               
                
-               public StringFormat(StringFormatFlags options, int lang)
+               public StringFormat(StringFormatFlags options, int language)
                {
-                       Status status = GDIPlus.GdipCreateStringFormat (options, lang, out nativeStrFmt);                               
+                       Status status = GDIPlus.GdipCreateStringFormat (options, language, out nativeStrFmt);                           
                        GDIPlus.CheckStatus (status);
-
-                       LineAlignment =  StringAlignment.Near;
-                       Alignment =  StringAlignment.Near;                      
-                       language = lang;
                }
                
                internal StringFormat(IntPtr native)
@@ -43,12 +55,12 @@ namespace System.Drawing
                        nativeStrFmt = native;
                }
                
-               ~StringFormat()
+               ~StringFormat ()
                {       
-                       Dispose ();
+                       Dispose (false);
                }
                
-               public void Dispose()
+               public void Dispose ()
                {       
                        Dispose (true);
                        System.GC.SuppressFinalize (this);
@@ -56,22 +68,26 @@ namespace System.Drawing
 
                void Dispose (bool disposing)
                {
-                       if (disposing) {
+                       if (nativeStrFmt != IntPtr.Zero) {
                                Status status = GDIPlus.GdipDeleteStringFormat (nativeStrFmt);
+                               nativeStrFmt = IntPtr.Zero;
                                GDIPlus.CheckStatus (status);
                        }
                }
 
-               public StringFormat (StringFormat source)
-               {                       
-                       Status status = GDIPlus.GdipCloneStringFormat (source.NativeObject, out nativeStrFmt);
+               public StringFormat (StringFormat format)
+               {
+                       if (format == null)
+                               throw new ArgumentNullException ("format");
+
+                       Status status = GDIPlus.GdipCloneStringFormat (format.NativeObject, out nativeStrFmt);
                        GDIPlus.CheckStatus (status);
                }
 
-               public StringFormat (StringFormatFlags flags)
+               public StringFormat (StringFormatFlags options)
                {
-                       Status status = GDIPlus.GdipCreateStringFormat (flags, GDIPlus.LANG_NEUTRAL, out nativeStrFmt);
-                       GDIPlus.CheckStatus (status);
+                       Status status = GDIPlus.GdipCreateStringFormat (options, GDIPlus.LANG_NEUTRAL, out nativeStrFmt);
+                       GDIPlus.CheckStatus (status);                   
                }
                
                public StringAlignment Alignment {
@@ -83,7 +99,10 @@ namespace System.Drawing
                                return align;
                        }
 
-                       set {                                   
+                       set {
+                               if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
+                                       throw new InvalidEnumArgumentException ("Alignment");
+
                                Status status = GDIPlus.GdipSetStringFormatAlign (nativeStrFmt, value);
                                GDIPlus.CheckStatus (status);
                        }
@@ -98,7 +117,10 @@ namespace System.Drawing
                                 return align;
                        }
 
-                       set {                           
+                       set {
+                               if ((value < StringAlignment.Near) || (value > StringAlignment.Far))
+                                       throw new InvalidEnumArgumentException ("Alignment");
+
                                Status status = GDIPlus.GdipSetStringFormatLineAlign (nativeStrFmt, value);
                                GDIPlus.CheckStatus (status);
                        }
@@ -129,6 +151,9 @@ namespace System.Drawing
                        }
 
                        set {
+                               if ((value < HotkeyPrefix.None) || (value > HotkeyPrefix.Hide))
+                                       throw new InvalidEnumArgumentException ("HotkeyPrefix");
+
                                Status status = GDIPlus.GdipSetStringFormatHotkeyPrefix (nativeStrFmt, value);
                                GDIPlus.CheckStatus (status);
                        }
@@ -144,6 +169,9 @@ namespace System.Drawing
                        }
 
                        set {
+                               if ((value < StringTrimming.None) || (value > StringTrimming.EllipsisPath))
+                                       throw new InvalidEnumArgumentException ("Trimming");
+
                                Status status = GDIPlus.GdipSetStringFormatTrimming (nativeStrFmt, value);
                                GDIPlus.CheckStatus (status);
                        }
@@ -155,7 +183,7 @@ namespace System.Drawing
                                
                                Status status = GDIPlus.GdipStringFormatGetGenericDefault (out ptr);
                                GDIPlus.CheckStatus (status);
-
+       
                                return new StringFormat (ptr);
                        }
                }
@@ -171,11 +199,11 @@ namespace System.Drawing
                public static StringFormat GenericTypographic {
                        get {
                                IntPtr ptr;
-                                       
+                                               
                                Status status = GDIPlus.GdipStringFormatGetGenericTypographic (out ptr);
                                GDIPlus.CheckStatus (status);
-
-                               return new StringFormat (ptr);                          
+       
+                               return new StringFormat (ptr);
                        }
                }
 
@@ -191,20 +219,31 @@ namespace System.Drawing
                }
 
 
-               [MonoTODO]
-               public void SetMeasurableCharacterRanges (CharacterRange [] range)
-               {
-
+               public void SetMeasurableCharacterRanges (CharacterRange [] ranges)
+               {                                       
+                       Status status = GDIPlus.GdipSetStringFormatMeasurableCharacterRanges (nativeStrFmt, 
+                               ranges.Length,  ranges);
+                               
+                       GDIPlus.CheckStatus (status);
                }
-       
+               
+               internal int GetMeasurableCharacterRangeCount () 
+               {
+                       int cnt;                
+                       Status status = GDIPlus.GdipGetStringFormatMeasurableCharacterRangeCount (nativeStrFmt, out cnt);
+                               
+                       GDIPlus.CheckStatus (status);                   
+                       return cnt;                     
+               }                       
+                       
                public object Clone()
                {
                        IntPtr native;
-                       
+                               
                        Status status = GDIPlus.GdipCloneStringFormat (nativeStrFmt, out native);
                        GDIPlus.CheckStatus (status);
-
-                       return new StringFormat (native);
+       
+                       return new StringFormat (native);                       
                }
 
                public override string ToString()