nemo r77 - trunk/gtk



Author: arj
Date: Wed Feb  6 18:33:29 2008
New Revision: 77
URL: http://svn.gnome.org/viewvc/nemo?rev=77&view=rev

Log:
Teach Cairo about memory management



Modified:
   trunk/gtk/Cairo.cs

Modified: trunk/gtk/Cairo.cs
==============================================================================
--- trunk/gtk/Cairo.cs	(original)
+++ trunk/gtk/Cairo.cs	Wed Feb  6 18:33:29 2008
@@ -23,117 +23,124 @@
 				
 			img_fs.Close();
 			
-			Cairo.ImageSurface img_surface = new ImageSurface("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
+			using (Cairo.ImageSurface img_surface = new ImageSurface("/tmp/nemo_cairo.png")) {
+				System.IO.File.Delete("/tmp/nemo_cairo.png");
 
-			Cairo.Context context = new Context(img_surface);
-			
-			Stream s = asm.GetManifestResourceStream("starred_right.png");
+				using (Cairo.Context context = new Context(img_surface)) {
+					Stream s = asm.GetManifestResourceStream("starred_right.png");
 
-			FileStream fs = new System.IO.FileStream("/tmp/nemo_cairo.png", FileMode.Create);
-			
-			for (int i = 0; i < s.Length; ++i)
-				fs.WriteByte((byte)s.ReadByte());
-				
-			fs.Close();
-			
-			Cairo.ImageSurface star_surface = new ImageSurface("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			
-			context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height);
-			context.Paint();
-
-			img_surface.WriteToPng("/tmp/nemo_cairo.png");
-			Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			return img;
+					FileStream fs = new System.IO.FileStream("/tmp/nemo_cairo.png", FileMode.Create);
+					
+					for (int i = 0; i < s.Length; ++i)
+						fs.WriteByte((byte)s.ReadByte());
+						
+					fs.Close();
+					
+					using (Cairo.ImageSurface star_surface = new ImageSurface("/tmp/nemo_cairo.png")) {
+						System.IO.File.Delete("/tmp/nemo_cairo.png");
+						
+						context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height);
+						context.Paint();
+
+						img_surface.WriteToPng("/tmp/nemo_cairo.png");
+						Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
+						System.IO.File.Delete("/tmp/nemo_cairo.png");
+
+						return img;
+					}
+				}
+			}
 		}
 
 		public static Gtk.Image create_big_starred_image(string pic_path)
 		{
-			Cairo.ImageSurface img_surface = new ImageSurface(pic_path);
-
-			Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2);
-		
-			Cairo.Context context = new Context(surface);
-
-			Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path);
-			if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available...
-				context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); 
-				context.Fill();
-				context.Stroke();
+			using (Cairo.ImageSurface img_surface = new ImageSurface(pic_path)) {
+				using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2)) {
+					using (Cairo.Context context = new Context(surface)) {
+						Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path);
+						if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available...
+							context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); 
+							context.Fill();
+							context.Stroke();
+						}
+
+						context.SetSource(img_surface, 1, 1);
+						context.Paint();
+
+						// evil hack because the interface to cairo is pretty bad
+
+						Assembly asm = Assembly.GetCallingAssembly();
+
+						Stream s = asm.GetManifestResourceStream("big_star.png");
+
+						FileStream fs = new System.IO.FileStream("/tmp/nemo_cairo.png", FileMode.Create);
+						
+						for (int i = 0; i < s.Length; ++i)
+							fs.WriteByte((byte)s.ReadByte());
+							
+						fs.Close();
+						
+						using (Cairo.ImageSurface star_surface = new ImageSurface("/tmp/nemo_cairo.png")) {
+							System.IO.File.Delete("/tmp/nemo_cairo.png");
+							
+							context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height);
+							context.Paint();
+
+							surface.WriteToPng("/tmp/nemo_cairo.png");
+							Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
+							System.IO.File.Delete("/tmp/nemo_cairo.png");
+							return img;
+						}
+					}
+				}
 			}
-
-			context.SetSource(img_surface, 1, 1);
-			context.Paint();
-
-			// evil hack because the interface to cairo is pretty bad
-
-			Assembly asm = Assembly.GetCallingAssembly();
-
-			Stream s = asm.GetManifestResourceStream("big_star.png");
-
-			FileStream fs = new System.IO.FileStream("/tmp/nemo_cairo.png", FileMode.Create);
-			
-			for (int i = 0; i < s.Length; ++i)
-				fs.WriteByte((byte)s.ReadByte());
-				
-			fs.Close();
-			
-			Cairo.ImageSurface star_surface = new ImageSurface("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			
-			context.SetSource(star_surface, img_surface.Width-star_surface.Width, img_surface.Height-star_surface.Height);
-			context.Paint();
-
-			surface.WriteToPng("/tmp/nemo_cairo.png");
-			Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			return img;
 		}
 
 		public static Gtk.Image create_image_with_border(string pic_path)
 		{
-			Cairo.ImageSurface img_surface = new ImageSurface(pic_path);
-
-			Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2);
-		
-			Cairo.Context context = new Context(surface);
-			
-			Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path);
-			if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available...
-				context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); 
-				context.Fill();
-				context.Stroke();
+			using (Cairo.ImageSurface img_surface = new ImageSurface(pic_path)) {
+				using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, img_surface.Width + 2, img_surface.Height + 2)) {
+					using (Cairo.Context context = new Context(surface)) {
+						Gdk.Pixbuf tmp_pixbuf = new Gdk.Pixbuf(pic_path);
+						if (!tmp_pixbuf.HasAlpha) { // img_surface.Format not available...
+							context.Rectangle(0, 0, img_surface.Width+2, img_surface.Height+2); 
+							context.Fill();
+							context.Stroke();
+						}
+
+						context.SetSource(img_surface, 1, 1);
+						context.Paint();
+
+						surface.WriteToPng("/tmp/nemo_cairo.png");
+						Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
+						System.IO.File.Delete("/tmp/nemo_cairo.png");
+						return img;
+					}
+				}
 			}
-
-			context.SetSource(img_surface, 1, 1);
-			context.Paint();
-
-			surface.WriteToPng("/tmp/nemo_cairo.png");
-			Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			return img;
 		}
 
 		public static Gtk.Image create_dot_image(int r, int g, int b, int a, int width, int height)
 		{
-			Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height);
-			Cairo.Context context = new Context(surface);
-			draw(context, width, height, new Cairo.Color(((double)r)/255, ((double)g)/255, ((double)b)/255, ((double)a)/255));
-			surface.WriteToPng("/tmp/nemo_cairo.png");
-			Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			return img;
+			using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height)) {
+				using (Cairo.Context context = new Context(surface)) {
+					draw(context, width, height, new Cairo.Color(((double)r)/255, ((double)g)/255, ((double)b)/255, ((double)a)/255));
+					surface.WriteToPng("/tmp/nemo_cairo.png");
+					Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
+					System.IO.File.Delete("/tmp/nemo_cairo.png");
+					return img;
+				}
+			}
 		}
 
 		public static Gtk.Image create_empty_image(int width, int height)
 		{
-			Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height);
-			surface.WriteToPng("/tmp/nemo_cairo.png");
-			Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
-			System.IO.File.Delete("/tmp/nemo_cairo.png");
-			return img;
+			using (Cairo.ImageSurface surface = new Cairo.ImageSurface(Cairo.Format.ARGB32, width, height)) {
+				surface.WriteToPng("/tmp/nemo_cairo.png");
+				Gtk.Image img = new Gtk.Image("/tmp/nemo_cairo.png");
+				System.IO.File.Delete("/tmp/nemo_cairo.png");
+				return img;
+			}
 		}
 
         static readonly double M_PI = 3.14159265358979323846;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]