2005-09-26 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / System.Drawing / Graphics.cs
old mode 100755 (executable)
new mode 100644 (file)
index 978f3d0..42b107c
@@ -8,7 +8,7 @@
 //
 // Copyright (C) 2003 Ximian, Inc. (http://www.ximian.com)
 //
-// Copyright (C) 2004 Novell, Inc. (http://www.novell.com)
+// Copyright (C) 2004-2005 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
 // 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.Drawing2D;
 using System.Drawing.Imaging;
 using System.Drawing.Text;
 using System.ComponentModel;
 using System.Runtime.InteropServices;
+using System.Security.Permissions;
 using System.Text;
 
 namespace System.Drawing
@@ -787,50 +788,43 @@ namespace System.Drawing
                }
 
                public void DrawString (string s, Font font, Brush brush, RectangleF layoutRectangle)
-               {                       
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, ref layoutRectangle, IntPtr.Zero, brush.nativeObject);
-                       GDIPlus.CheckStatus (status);
+               {
+                       DrawString (s, font, brush, layoutRectangle, null);
                }
-               
+
                public void DrawString (string s, Font font, Brush brush, PointF point)
                {
-                       RectangleF rc = new RectangleF (point.X, point.Y, 0, 0);
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, ref rc, IntPtr.Zero, brush.nativeObject);
-                       GDIPlus.CheckStatus (status);
+                       DrawString (s, font, brush, new RectangleF (point.X, point.Y, 0, 0), null);
                }
-               
+
                public void DrawString (string s, Font font, Brush brush, PointF point, StringFormat format)
                {
-                       RectangleF rc = new RectangleF (point.X, point.Y, 0, 0);
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, ref rc, format.NativeObject, brush.nativeObject);
-                       GDIPlus.CheckStatus (status);
-               }
-               
-               public void DrawString (string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
-               {
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, ref layoutRectangle, format.NativeObject, brush.nativeObject);
-                       GDIPlus.CheckStatus (status);
+                       DrawString(s, font, brush, new RectangleF(point.X, point.Y, 0, 0), format);
                }
 
                public void DrawString (string s, Font font, Brush brush, float x, float y)
                {
-                       RectangleF rc = new RectangleF (x, y, 0, 0);
-                       
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, 
-                               ref rc, IntPtr.Zero, brush.nativeObject);
-                       GDIPlus.CheckStatus (status);
+                       DrawString (s, font, brush, new RectangleF (x, y, 0, 0), null);
                }
 
                public void DrawString (string s, Font font, Brush brush, float x, float y, StringFormat format)
                {
-                       RectangleF rc = new RectangleF (x, y, 0, 0);
+                       DrawString (s, font, brush, new RectangleF(x, y, 0, 0), format);
+               }
+
+               public void DrawString (string s, Font font, Brush brush, RectangleF layoutRectangle, StringFormat format)
+               {
+                       if (font == null)
+                               throw new ArgumentNullException ("font");
+                       if (brush == null)
+                               throw new ArgumentNullException ("brush");
+                       if (s == null || s.Length == 0)
+                               return;
 
-                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject,
-                               ref rc, format.NativeObject, brush.nativeObject);
+                       Status status = GDIPlus.GdipDrawString (nativeObject, s, s.Length, font.NativeObject, ref layoutRectangle, format != null ? format.NativeObject : IntPtr.Zero, brush.nativeObject);
                        GDIPlus.CheckStatus (status);
                }
 
-               
                public void EndContainer (GraphicsContainer container)
                {
                        Status status = GDIPlus.GdipEndContainer(nativeObject, container.NativeObject);
@@ -1224,8 +1218,8 @@ namespace System.Drawing
                {
                        Status status = GDIPlus.GdipFlush (nativeObject, intention);
                         GDIPlus.CheckStatus (status);                    
-                       if (GDIPlus.UseQuartzDrawable)
-                               Carbon.CGContextFlush (GDIPlus.Display);
+                       if (GDIPlus.UseQuartzDrawable || GDIPlus.UseCocoaDrawable)
+                               Carbon.CGContextSynchronize (GDIPlus.Display);
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]               
@@ -1245,6 +1239,7 @@ namespace System.Drawing
                }
 
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public static Graphics FromHdcInternal (IntPtr hdc)
                {
                        GDIPlus.Display = hdc;
@@ -1256,6 +1251,13 @@ namespace System.Drawing
                {
                        IntPtr graphics;
 
+                       if (GDIPlus.UseCocoaDrawable) {
+                               CarbonContext cgContext = Carbon.GetCGContextForNSView (hwnd);
+                               GDIPlus.GdipCreateFromQuartz_macosx (cgContext.ctx, cgContext.width, cgContext.height, out graphics);
+                               
+                               GDIPlus.Display = cgContext.ctx;
+                               return new Graphics (graphics);
+                       }
                        if (GDIPlus.UseQuartzDrawable) {
                                CarbonContext cgContext = Carbon.GetCGContextForView (hwnd);
                                GDIPlus.GdipCreateFromQuartz_macosx (cgContext.ctx, cgContext.width, cgContext.height, out graphics);
@@ -1277,25 +1279,29 @@ namespace System.Drawing
 
                        return new Graphics (graphics);
                }
-
-               [MonoTODO]
+               
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public static Graphics FromHwndInternal (IntPtr hwnd)
                {
-                       throw new NotImplementedException ();
+                       return FromHwnd (hwnd);
                }
 
                public static Graphics FromImage (Image image)
                {
                        IntPtr graphics;
 
-                       if (image == null) throw new ArgumentException ();
+                       if (image == null) 
+                               throw new ArgumentNullException ();
 
                        Status status = GDIPlus.GdipGetImageGraphicsContext (image.nativeObject, out graphics);
                        GDIPlus.CheckStatus (status);
                        Graphics result = new Graphics (graphics);
                                
-                       if (Environment.OSVersion.Platform == (PlatformID) 128)  {
+                       // check for Unix platforms - see FAQ for more details
+                       // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
+                       int platform = (int) Environment.OSVersion.Platform;
+                       if ((platform == 4) || (platform == 128)) {
                                Rectangle rect  = new Rectangle (0,0, image.Width, image.Height);
                                GDIPlus.GdipSetVisibleClip_linux (result.NativeObject, ref rect);
                        }
@@ -1318,11 +1324,12 @@ namespace System.Drawing
                        throw new NotImplementedException ();
                }
 
-               [MonoTODO]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
                public IntPtr GetHdc ()
                {
-                       return nativeObject;
+                       IntPtr hdc;
+                       GDIPlus.CheckStatus (GDIPlus.GdipGetDC (this.nativeObject, out hdc));
+                       return hdc;
                }
 
                
@@ -1587,6 +1594,7 @@ namespace System.Drawing
 
                [MonoTODO]
                [EditorBrowsable (EditorBrowsableState.Advanced)]
+               [SecurityPermission (SecurityAction.LinkDemand, UnmanagedCode = true)]
                public void ReleaseHdcInternal (IntPtr hdc)
                {
                        throw new NotImplementedException ();
@@ -1606,8 +1614,7 @@ namespace System.Drawing
                }
 
                public void Restore (GraphicsState gstate)
-               {
-                       Transform = gstate.matrix.Clone ();
+               {                       
                        Status status = GDIPlus.GdipRestoreGraphics (nativeObject, gstate.nativeState);
                        GDIPlus.CheckStatus (status);
                }
@@ -1626,13 +1633,12 @@ namespace System.Drawing
                }
 
                public GraphicsState Save ()
-               {
-                       GraphicsState state = new GraphicsState ();
-                       state.matrix = Transform.Clone ();
+               {                                               
                        uint saveState;
                        Status status = GDIPlus.GdipSaveGraphics (nativeObject, out saveState);
                        GDIPlus.CheckStatus (status);
 
+                       GraphicsState state = new GraphicsState ();
                        state.nativeState = saveState;
                        return state;
                }