2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / System.Drawing / System.Drawing / StringFormat.cs
index 8f30edfd46ce20aa13a39b0e39e3bd97185e7f00..1344cd2e25cce5c3073645c11289d5153847a756 100644 (file)
-//\r
-// System.Drawing.StringFormat.cs\r
-//\r
-// Authors:\r
-//   Dennis Hayes (dennish@Raytek.com)\r
-//   Miguel de Icaza (miguel@ximian.com)\r
-//\r
-// (C) 2002 Ximian, Inc\r
-// (C) 2003 Novell, Inc.\r
-//\r
-using System;\r
-using System.Drawing.Text;\r
-\r
-namespace System.Drawing\r
-{\r
-       /// <summary>\r
-       /// Summary description for StringFormat.\r
-       /// </summary>\r
-       public sealed class StringFormat : IDisposable\r
-       {\r
-               private static StringFormat genericDefault;\r
-               StringAlignment alignment;\r
-               StringAlignment line_alignment;\r
-               StringFormatFlags format_flags;\r
-               HotkeyPrefix hotkey_prefix;\r
-               StringTrimming trimming;\r
-               \r
-               public StringFormat()\r
-               {\r
-                       //\r
-                       // TODO: Add constructor logic here\r
-                       //\r
-                       alignment = StringAlignment.Center;\r
-                       line_alignment = StringAlignment.Center;\r
-                       format_flags = 0;\r
-               }\r
-\r
-               public void Dispose()\r
-               {\r
-               }\r
-\r
-               public StringFormat (StringFormat source)\r
-               {\r
-                       alignment = source.alignment;\r
-                       line_alignment = source.line_alignment;\r
-                       format_flags = source.format_flags;\r
-                       hotkey_prefix = source.hotkey_prefix;\r
-               }\r
-\r
-               public StringFormat (StringFormatFlags flags)\r
-               {\r
-                       alignment = StringAlignment.Center;\r
-                       line_alignment = StringAlignment.Center;\r
-                       format_flags = flags;\r
-               }\r
-               \r
-               public StringAlignment Alignment {\r
-                       get {\r
-                               return alignment;\r
-                       }\r
-\r
-                       set {\r
-                               alignment = value;\r
-                       }\r
-               }\r
-\r
-               public StringAlignment LineAlignment {\r
-                       get {\r
-                               return line_alignment;\r
-                       }\r
-\r
-                       set {\r
-                               line_alignment = value;\r
-                       }\r
-               }\r
-\r
-               public StringFormatFlags FormatFlags {\r
-                       get {\r
-                               return format_flags;\r
-                       }\r
-\r
-                       set {\r
-                               format_flags = value;\r
-                       }\r
-               }\r
-\r
-               public HotkeyPrefix HotkeyPrefix {\r
-                       get {\r
-                               return hotkey_prefix;\r
-                       }\r
-\r
-                       set {\r
-                               hotkey_prefix = value;\r
-                       }\r
-               }\r
-\r
-               public void SetMeasurableCharacterRanges (CharacterRange [] range)\r
-               {\r
-               }\r
-\r
-               public StringTrimming Trimming {\r
-                       get {\r
-                               return trimming;\r
-                       }\r
-\r
-                       set {\r
-                               trimming = value;\r
-                       }\r
-               }\r
-\r
-               public static StringFormat GenericDefault {\r
-                       get {\r
-                               lock (typeof (StringFormat)) {\r
-                                       if (genericDefault == null)  {\r
-                                               genericDefault = new StringFormat ();\r
-                                               genericDefault.Alignment = StringAlignment.Near;\r
-                                               //genericDefault.DigitSubstitutionMethod = StringDigitSubstitute.User;\r
-                                               genericDefault.LineAlignment = StringAlignment.Near;\r
-                                               genericDefault.Trimming = StringTrimming.Character;\r
-                                       }\r
-                               }\r
-                               return(genericDefault);\r
-                       }\r
-               }\r
-       }\r
-}\r
+//
+// System.Drawing.StringFormat.cs
+//
+// Authors:
+//   Dennis Hayes (dennish@Raytek.com)
+//   Miguel de Icaza (miguel@ximian.com)
+//   Jordi Mas i Hernandez (jordi@ximian.com)
+//
+// Copyright (C) 2002 Ximian, Inc (http://www.ximian.com)
+//
+// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+//
+// 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;
+using System.Drawing.Text;
+
+namespace System.Drawing
+{
+       /// <summary>
+       /// Summary description for StringFormat.
+       /// </summary>
+       public sealed class StringFormat : MarshalByRefObject, IDisposable, ICloneable
+       {
+               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)
+               {
+                       Status status = GDIPlus.GdipCreateStringFormat (options, lang, out nativeStrFmt);                               
+                       GDIPlus.CheckStatus (status);
+       
+                       LineAlignment =  StringAlignment.Near;
+                       Alignment =  StringAlignment.Near;                      
+                       language = lang;                        
+               }
+               
+               internal StringFormat(IntPtr native)
+               {
+                       nativeStrFmt = native;
+               }
+               
+               ~StringFormat ()
+               {       
+                       Dispose (false);
+               }
+               
+               public void Dispose ()
+               {       
+                       Dispose (true);
+                       System.GC.SuppressFinalize (this);
+               }
+
+               void Dispose (bool disposing)
+               {
+                       if (nativeStrFmt != IntPtr.Zero) {
+                               Status status = GDIPlus.GdipDeleteStringFormat (nativeStrFmt);
+                               GDIPlus.CheckStatus (status);
+                               
+                               nativeStrFmt = IntPtr.Zero;
+                       }
+               }
+
+               public StringFormat (StringFormat source)
+               {               
+                       Status status = GDIPlus.GdipCloneStringFormat (source.NativeObject, out nativeStrFmt);
+                       GDIPlus.CheckStatus (status);                   
+               }
+
+               public StringFormat (StringFormatFlags flags)
+               {
+                       Status status = GDIPlus.GdipCreateStringFormat (flags, GDIPlus.LANG_NEUTRAL, out nativeStrFmt);
+                       GDIPlus.CheckStatus (status);                   
+               }
+               
+               public StringAlignment Alignment {
+                       get {
+                                StringAlignment align;
+                               Status status = GDIPlus.GdipGetStringFormatAlign (nativeStrFmt, out align);
+                               GDIPlus.CheckStatus (status);
+
+                               return align;
+                       }
+
+                       set {                                   
+                               Status status = GDIPlus.GdipSetStringFormatAlign (nativeStrFmt, value);
+                               GDIPlus.CheckStatus (status);
+                       }
+               }
+
+               public StringAlignment LineAlignment {
+                       get {
+                               StringAlignment align;
+                               Status status = GDIPlus.GdipGetStringFormatLineAlign (nativeStrFmt, out align);
+                               GDIPlus.CheckStatus (status);
+
+                                return align;
+                       }
+
+                       set {                           
+                               Status status = GDIPlus.GdipSetStringFormatLineAlign (nativeStrFmt, value);
+                               GDIPlus.CheckStatus (status);
+                       }
+               }
+
+               public StringFormatFlags FormatFlags {
+                       get {                           
+                               StringFormatFlags flags;
+                               Status status = GDIPlus.GdipGetStringFormatFlags (nativeStrFmt, out flags);
+                               GDIPlus.CheckStatus (status);
+
+                               return flags;                   
+                       }
+
+                       set {
+                               Status status = GDIPlus.GdipSetStringFormatFlags (nativeStrFmt, value);
+                               GDIPlus.CheckStatus (status);
+                       }
+               }
+
+               public HotkeyPrefix HotkeyPrefix {
+                       get {                           
+                               HotkeyPrefix hotkeyPrefix;
+                               Status status = GDIPlus.GdipGetStringFormatHotkeyPrefix (nativeStrFmt, out hotkeyPrefix);
+                               GDIPlus.CheckStatus (status);
+
+                                       return hotkeyPrefix;
+                       }
+
+                       set {
+                               Status status = GDIPlus.GdipSetStringFormatHotkeyPrefix (nativeStrFmt, value);
+                               GDIPlus.CheckStatus (status);
+                       }
+               }
+
+
+               public StringTrimming Trimming {
+                       get {
+                               StringTrimming trimming;
+                               Status status = GDIPlus.GdipGetStringFormatTrimming (nativeStrFmt, out trimming);
+                               GDIPlus.CheckStatus (status);
+                               return trimming;
+                       }
+
+                       set {
+                               Status status = GDIPlus.GdipSetStringFormatTrimming (nativeStrFmt, value);
+                               GDIPlus.CheckStatus (status);
+                       }
+               }
+
+               public static StringFormat GenericDefault {
+                       get {
+                               IntPtr ptr;
+                               
+                               Status status = GDIPlus.GdipStringFormatGetGenericDefault (out ptr);
+                               GDIPlus.CheckStatus (status);
+       
+                               return new StringFormat (ptr);
+                               
+                       }
+               }
+               
+               
+               public int DigitSubstitutionLanguage {
+                       get{
+                               return language;
+                       }
+               }
+
+               
+               public static StringFormat GenericTypographic {
+                       get {
+                       
+                               IntPtr ptr;
+                                               
+                               Status status = GDIPlus.GdipStringFormatGetGenericTypographic (out ptr);
+                               GDIPlus.CheckStatus (status);
+       
+                               return new StringFormat (ptr);
+                       }
+               }
+
+                public StringDigitSubstitute  DigitSubstitutionMethod  {
+                       get {
+                                StringDigitSubstitute substitute;
+                                
+                                Status status = GDIPlus.GdipGetStringFormatDigitSubstitution(nativeStrFmt, language, out substitute);
+                               GDIPlus.CheckStatus (status);
+
+                                return substitute;     
+                       }
+               }
+
+
+               public void SetMeasurableCharacterRanges (CharacterRange [] range)
+               {                                       
+                       Status status = GDIPlus.GdipSetStringFormatMeasurableCharacterRanges (nativeStrFmt, 
+                               range.Length,   range);
+                               
+                       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);                       
+               }
+
+               public override string ToString()
+               {
+                       return "[StringFormat, FormatFlags=" + this.FormatFlags.ToString() + "]";
+               }
+               
+               internal IntPtr NativeObject
+                {            
+                       get{
+                               return nativeStrFmt;
+                       }
+                       set     {
+                               nativeStrFmt = value;
+                       }
+               }
+
+                public void SetTabStops(float firstTabOffset, float[] tabStops)
+                {
+                       Status status = GDIPlus.GdipSetStringFormatTabStops(nativeStrFmt, firstTabOffset, tabStops.Length, tabStops);
+                       GDIPlus.CheckStatus (status);
+                }
+
+                public void SetDigitSubstitution(int language,  StringDigitSubstitute substitute)
+                {
+                       Status status = GDIPlus.GdipSetStringFormatDigitSubstitution(nativeStrFmt, this.language, substitute);
+                       GDIPlus.CheckStatus (status);
+                }
+
+                public float[] GetTabStops(out float firstTabOffset)
+                {
+                        int count = 0;
+                        firstTabOffset = 0;
+                        
+                        Status status = GDIPlus.GdipGetStringFormatTabStopCount(nativeStrFmt, out count);
+                       GDIPlus.CheckStatus (status);
+
+                        float[] tabStops = new float[count];                        
+                        
+                        if (count != 0) {                        
+                               status = GDIPlus.GdipGetStringFormatTabStops(nativeStrFmt, count, out firstTabOffset, tabStops);
+                               GDIPlus.CheckStatus (status);
+                       }
+                               
+                        return tabStops;                        
+                }
+
+       }
+}