From 7bb187ce71f2f67d1875602b6f1fcb9c91739bc3 Mon Sep 17 00:00:00 2001 From: RKrom Date: Wed, 26 Sep 2012 12:51:01 +0000 Subject: [PATCH] Removed the PictureBox from the Surface, meaning that the Surface is now just a Control, this should simplify the event stack and other logic and give us more control. git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2099 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Drawing/Surface.cs | 68 +++++++++++++++++++----------- Greenshot/Forms/ImageEditorForm.cs | 14 +++--- 2 files changed, 50 insertions(+), 32 deletions(-) diff --git a/Greenshot/Drawing/Surface.cs b/Greenshot/Drawing/Surface.cs index 16806fb2c..137c03ed5 100644 --- a/Greenshot/Drawing/Surface.cs +++ b/Greenshot/Drawing/Surface.cs @@ -47,7 +47,7 @@ namespace Greenshot.Drawing { /// /// Description of Surface. /// - public class Surface : PictureBox, ISurface { + public class Surface : Control, ISurface { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(Surface)); private static CoreConfiguration conf = IniConfig.GetIniSection(); @@ -188,7 +188,8 @@ namespace Greenshot.Drawing { private TextureBrush transparencyBackgroundBrush; /// - /// The buffer is only for drawing on it, saving a lot of "create new bitmap" commands + /// The buffer is only for drawing on it when using filters (to supply access) + /// This saves a lot of "create new bitmap" commands /// Should not be serialized, as it's generated. /// The actual bitmap is in the paintbox... /// TODO: Check if this buffer is still needed! @@ -224,6 +225,20 @@ namespace Greenshot.Drawing { /// private bool modified = true; + /// + /// The image is the actual captured image, needed with serialization + /// + private Image image = null; + + public Image Image { + get { + return image; + } + set { + image = value; + } + } + public FieldAggregator FieldAggregator { get { return fieldAggregator; @@ -310,7 +325,6 @@ namespace Greenshot.Drawing { public Surface() : base(){ LOG.Debug("Creating a surface!"); - this.SizeMode = PictureBoxSizeMode.AutoSize; this.MouseDown += new MouseEventHandler(SurfaceMouseDown); this.MouseUp += new MouseEventHandler(SurfaceMouseUp); this.MouseMove += new MouseEventHandler(SurfaceMouseMove); @@ -319,10 +333,14 @@ namespace Greenshot.Drawing { this.AllowDrop = true; this.DragDrop += new DragEventHandler(OnDragDrop); this.DragEnter += new DragEventHandler(OnDragEnter); - // bind selected & elements to this, otherwise they can't inform of modifications - selectedElements.Parent = this; - elements.Parent = this; + this.selectedElements.Parent = this; + this.elements.Parent = this; + // Make sure we are visible + this.Visible = true; + // Enable double buffering + this.DoubleBuffered = true; + this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.ContainerControl | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true); } /// @@ -330,20 +348,22 @@ namespace Greenshot.Drawing { /// /// The new image /// true if the old image needs to be disposed, when using undo this should not be true!! - private void SetImage(Image image, bool dispose) { + private void SetImage(Image newImage, bool dispose) { // Dispose - if (Image != null && dispose) { - Image.Dispose(); + if (image != null && dispose) { + image.Dispose(); } // Set new values - Image = image; + Image = newImage; + Size = newImage.Size; + modified = true; } - public Surface(Image image) : this() { - LOG.Debug("Got image with dimensions " + image.Width + "," + image.Height + " bpp: " + image.PixelFormat); - SetImage(image, true); + public Surface(Image newImage) : this() { + LOG.Debug("Got image with dimensions " + newImage.Width + "," + newImage.Height + " bpp: " + newImage.PixelFormat); + SetImage(newImage, true); } public Surface(ICapture capture) : this(capture.Image) { @@ -651,8 +671,7 @@ namespace Greenshot.Drawing { e.Effect=DragDropEffects.None; } else { List filenames = GetFilenames(e); - //|| e.Data.GetDataPresent(DataFormats.EnhancedMetafile, true) - if ( (filenames != null && filenames.Count > 0) || e.Data.GetDataPresent(DataFormats.Bitmap, true) || e.Data.GetDataPresent(DataFormats.EnhancedMetafile, true)) { + if ((filenames != null && filenames.Count > 0) || e.Data.GetDataPresent(DataFormats.Bitmap, true) || e.Data.GetDataPresent(DataFormats.EnhancedMetafile, true)) { e.Effect=DragDropEffects.Copy; } else { e.Effect=DragDropEffects.None; @@ -664,7 +683,7 @@ namespace Greenshot.Drawing { List filenames = GetFilenames(e); Point mouse = this.PointToClient(new Point(e.X, e.Y)); if ((filenames != null && filenames.Count > 0)) { - foreach(string filename in filenames) { + foreach (string filename in filenames) { if (filename != null && filename.Trim().Length > 0) { LOG.Debug("Drop - filename: " + filename); if (filename.ToLower().EndsWith("wmf")) { @@ -1031,12 +1050,12 @@ namespace Greenshot.Drawing { Cursor = Cursors.Default; } - if(mouseDown) { - if(mouseDownElement != null) { // an element is currently dragged + if (mouseDown) { + if (mouseDownElement != null) { // an element is currently dragged mouseDownElement.Invalidate(); selectedElements.HideGrippers(); // Move the element - if(mouseDownElement.Selected) { + if (mouseDownElement.Selected) { if (!isSurfaceMoveMadeUndoable) { // Only allow one undoable per mouse-down/move/up "cycle" isSurfaceMoveMadeUndoable = true; @@ -1070,7 +1089,7 @@ namespace Greenshot.Drawing { private Image GetImage(RenderMode renderMode) { // Generate a copy of the original image with a dpi equal to the default... - Bitmap clone = ImageHelper.Clone(Image); + Bitmap clone = ImageHelper.Clone(image); // otherwise we would have a problem drawing the image to the surface... :( using (Graphics graphics = Graphics.FromImage(clone)) { // Do not set the following, the containers need to decide themselves @@ -1124,8 +1143,7 @@ namespace Greenshot.Drawing { } targetGraphics.DrawImage(buffer, clipRectangle, clipRectangle, GraphicsUnit.Pixel); } else { - // Only "simple" elements need to be redrawn, as the image is already drawn before getting the event we don't need the next line: - // targetGraphics.DrawImage(Image, clipRectangle, clipRectangle, GraphicsUnit.Pixel); + targetGraphics.DrawImage(Image, clipRectangle, clipRectangle, GraphicsUnit.Pixel); elements.Draw(targetGraphics, null, RenderMode.EDIT, clipRectangle); } } @@ -1267,10 +1285,10 @@ namespace Greenshot.Drawing { SelectElements(dcs); } } else if (ClipboardHelper.ContainsImage()) { - using (Image image = ClipboardHelper.GetImage()) { - if (image != null) { + using (Image clipboardImage = ClipboardHelper.GetImage()) { + if (clipboardImage != null) { DeselectAllElements(); - IBitmapContainer bitmapContainer = AddBitmapContainer(image as Bitmap, 0, 0); + IBitmapContainer bitmapContainer = AddBitmapContainer(clipboardImage as Bitmap, 0, 0); SelectElement(bitmapContainer); } } diff --git a/Greenshot/Forms/ImageEditorForm.cs b/Greenshot/Forms/ImageEditorForm.cs index e228756f9..d7749f07d 100644 --- a/Greenshot/Forms/ImageEditorForm.cs +++ b/Greenshot/Forms/ImageEditorForm.cs @@ -94,8 +94,8 @@ namespace Greenshot { // // The InitializeComponent() call is required for Windows Forms designer support. // - this.ManualLanguageApply = true; - InitializeComponent(); + this.ManualLanguageApply = true; + InitializeComponent(); updateUI(); this.Load += delegate { @@ -162,11 +162,11 @@ namespace Greenshot { ApplyLanguage(); - // Fix title - if (surface != null && surface.CaptureDetails != null && surface.CaptureDetails.Title != null) { - this.Text = surface.CaptureDetails.Title + " - " + Language.GetString(LangKey.editor_title); - } - } + // Fix title + if (surface != null && surface.CaptureDetails != null && surface.CaptureDetails.Title != null) { + this.Text = surface.CaptureDetails.Title + " - " + Language.GetString(LangKey.editor_title); + } + } /// /// Get all the destinations and display them in the file menu and the buttons