lock resource creation, cloning, and destroying
[mono.git] / mcs / class / System.Drawing / System.Drawing / Brush.cs
1 //
2 // System.Drawing.Brush.cs
3 //
4 // Authors:
5 //   Miguel de Icaza (miguel@ximian.com)
6 //   Ravindra (rkumar@novell.com)
7 //
8 // (C) Ximian, Inc.  http://www.ximian.com
9 // (C) Novell, Inc.  Http://www.novell.com
10 //
11
12 using System;
13 using System.Drawing;
14 using System.Drawing.Drawing2D;
15
16 namespace System.Drawing
17 {
18         public abstract class Brush : MarshalByRefObject, ICloneable, IDisposable
19         {
20                 internal IntPtr nativeObject;
21                 internal bool disposed = false;
22                 abstract public object Clone ();
23
24                 internal Brush ()
25                 { }
26
27                 internal Brush (IntPtr ptr)
28                 {
29                         nativeObject = ptr;
30                 }
31                 
32                 internal IntPtr NativeObject {
33                         get {
34                                 return nativeObject;
35                         }
36                         set {
37                                 nativeObject = value;
38                         }
39                 }
40
41                 internal Brush CreateBrush (IntPtr brush, System.Drawing.BrushType type)
42                 {
43                         switch (type) {
44
45                         case BrushType.BrushTypeSolidColor:
46                                 return new SolidBrush (brush);
47
48                         case BrushType.BrushTypeHatchFill:
49                                 return new HatchBrush (brush);
50
51                         case BrushType.BrushTypeTextureFill:
52                                 return new TextureBrush (brush);
53
54                         default:
55                                 throw new NotImplementedException ();
56                         }
57                 }
58
59                 public void Dispose ()
60                 {
61                         Dispose (true);
62                         System.GC.SuppressFinalize (this);
63                 }
64
65                 protected virtual void Dispose (bool disposing)
66                 {
67                         if (disposed == false) {
68                                 lock (this)
69                                 {
70                                         Status status = GDIPlus.GdipDeleteBrush (nativeObject);
71                                         GDIPlus.CheckStatus (status);
72                                         disposed = true;
73                                 }
74                         }
75                 }
76
77                 ~Brush ()
78                 {
79                         Dispose (false);
80                 }
81         }
82 }
83