Esempio n. 1
0
        public void Draw(ImageSurface dst)
        {
            if (disposed)
            {
                throw new ObjectDisposedException("PlacedSurface");
            }

            using (Cairo.Context g = new Cairo.Context(dst)) {
                g.Save();

                Rectangle r = what.GetBounds().ToCairoRectangle();

                // We need to use the source operator to fully replace the old
                // data.  Or else we may paint transparent on top of it and
                // it will still be visible.  [Bug #670411]
                using (Path p = g.CreateRectanglePath(new Rectangle(where.X, where.Y, r.Width, r.Height))) {
                    g.AppendPath(p);
                    g.Clip();
                    g.Operator = Operator.Source;
                    g.DrawPixbuf(what.ToPixbuf(), new Cairo.Point(where.X, where.Y));
                }

                g.Restore();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Reset (clear) the Selection.
        /// </summary>
        /// <param name="selectionSurface"></param>
        /// <param name="imageSize"></param>
        public void ResetSelection(Surface selectionSurface, Gdk.Size imageSize)
        {
            using (Cairo.Context g = new Cairo.Context(selectionSurface))
            {
                SelectionPath = g.CreateRectanglePath(new Rectangle(0, 0, imageSize.Width, imageSize.Height));
            }

            SelectionPolygons.Clear();
        }
Esempio n. 3
0
        protected override Rectangle DrawShape(Rectangle r, Layer l)
        {
            Path path = PintaCore.Layers.SelectionPath;

            using (Context g = new Context (l.Surface))
                PintaCore.Layers.SelectionPath = g.CreateRectanglePath (r);

            (path as IDisposable).Dispose ();

            // Add some padding for invalidation
            return new Rectangle (r.X, r.Y, r.Width + 2, r.Height + 2);
        }
Esempio n. 4
0
        /// <summary>
        /// Erase the handle that was drawn in a previous call to Render ().
        /// </summary>
        public void Clear (Context g)
        {
            g.Save ();

            var rect = GetHandleRect ().Inflate (2, 2);
            using (var path = g.CreateRectanglePath (rect))
                g.AppendPath (path);
            g.Operator = Operator.Clear;
            g.Fill ();

            g.Restore ();
        }
Esempio n. 5
0
        public void ResetSelectionPath()
        {
            Path old = SelectionPath;

            using (Cairo.Context g = new Cairo.Context(selection_layer.Surface))
                SelectionPath = g.CreateRectanglePath(new Cairo.Rectangle(0, 0, ImageSize.Width, ImageSize.Height));

            if (old != null)
            {
                (old as IDisposable).Dispose();
            }

            ShowSelection = false;
        }
Esempio n. 6
0
        public override void Redo()
        {
            Document doc = PintaCore.Workspace.ActiveDocument;

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer();
            doc.ShowSelectionLayer = true;

            Path p;

            using (Cairo.Context g = new Cairo.Context(doc.SelectionLayer.Surface)) {
                g.DrawPixbuf(paste_image, new Cairo.Point(0, 0));
                p = g.CreateRectanglePath(new Rectangle(0, 0, paste_image.Width, paste_image.Height));
            }

            Swap();

            PintaCore.Workspace.Invalidate();
            PintaCore.Tools.SetCurrentTool(Catalog.GetString("Move Selected Pixels"));
        }
Esempio n. 7
0
        private void HandlerPintaCoreActionsEditPasteActivated(object sender, EventArgs e)
        {
            Document doc = PintaCore.Workspace.ActiveDocument;

            PintaCore.Tools.Commit ();

            Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf image = cb.WaitForImage ();

            if (image == null)
                return;

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (image.Width > canvas_size.Width || image.Height > canvas_size.Height)
            {
                string message = Catalog.GetString ("The image being pasted is larger than the canvas size. What would you like to do?");

                var enlarge_dialog = new MessageDialog (PintaCore.Chrome.MainWindow, DialogFlags.Modal, MessageType.Question, ButtonsType.None, message);
                enlarge_dialog.AddButton (Catalog.GetString ("Expand canvas"), ResponseType.Accept);
                enlarge_dialog.AddButton (Catalog.GetString ("Don't change canvas size"), ResponseType.Reject);
                enlarge_dialog.AddButton (Stock.Cancel, ResponseType.Cancel);
                enlarge_dialog.DefaultResponse = ResponseType.Accept;

                ResponseType response = (ResponseType)enlarge_dialog.Run ();
                enlarge_dialog.Destroy ();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas (image.Width, image.Height, Pinta.Core.Anchor.Center);
                    PintaCore.Actions.View.UpdateCanvasScale ();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer ();
            doc.ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context (doc.SelectionLayer.Surface)) {
                g.DrawPixbuf (image, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Rectangle (0, 0, image.Width, image.Height));
            }

            PintaCore.Tools.SetCurrentTool (Catalog.GetString ("Move Selected Pixels"));

            Path old_path = doc.SelectionPath;
            bool old_show_selection = doc.ShowSelection;

            doc.SelectionPath = p;
            doc.ShowSelection = true;

            doc.Workspace.Invalidate ();

            doc.History.PushNewItem (new PasteHistoryItem (image, old_path, old_show_selection));
        }
Esempio n. 8
0
        public void ResetSelectionPath()
        {
            Path old = SelectionPath;

            using (Cairo.Context g = new Cairo.Context (selection_layer.Surface))
                SelectionPath = g.CreateRectanglePath (new Cairo.Rectangle (0, 0, ImageSize.Width, ImageSize.Height));

            if (old != null)
                (old as IDisposable).Dispose ();

            ShowSelection = false;
        }
Esempio n. 9
0
        private void HandlerPintaCoreActionsEditPasteActivated(object sender, EventArgs e)
        {
            PintaCore.Layers.FinishSelection ();

            Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone ();

            Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
            Gdk.Pixbuf image = cb.WaitForImage ();

            if (image == null)
                return;

            Path p;

            using (Cairo.Context g = new Cairo.Context (PintaCore.Layers.CurrentLayer.Surface)) {
                g.DrawPixbuf (image, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Rectangle (0, 0, image.Width, image.Height));
            }

            PintaCore.Layers.SelectionPath = p;
            PintaCore.Layers.ShowSelection = true;

            PintaCore.Workspace.Invalidate ();

            // TODO: Need paste icon
            PintaCore.History.PushNewItem (new SimpleHistoryItem ("Menu.Edit.EraseSelection.png", Mono.Unix.Catalog.GetString ("Paste"), old, PintaCore.Layers.CurrentLayerIndex));
        }
Esempio n. 10
0
        private void Activated(object sender, EventArgs e)
        {
            Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));
            if (PintaCore.Tools.CurrentTool.TryHandlePaste(cb))
            {
                return;
            }

            Document doc = PintaCore.Workspace.ActiveDocument;

            PintaCore.Tools.Commit();

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf image = cb.WaitForImage();

            if (image == null)
            {
                Dialogs.ClipboardEmptyDialog.Show();
                return;
            }

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // Merge the (optional) canvas resize and the pasted image into a single history item.
            var paste_action = new CompoundHistoryItem(Stock.Paste, Catalog.GetString("Paste"));

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (image.Width > canvas_size.Width || image.Height > canvas_size.Height)
            {
                ResponseType response = ShowExpandCanvasDialog();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas(image.Width, image.Height,
                                                     Pinta.Core.Anchor.Center, paste_action);
                    PintaCore.Actions.View.UpdateCanvasScale();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer();
            doc.ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context(doc.SelectionLayer.Surface))
            {
                g.DrawPixbuf(image, new Cairo.Point(0, 0));
                p = g.CreateRectanglePath(new Rectangle(0, 0, image.Width, image.Height));
            }

            PintaCore.Tools.SetCurrentTool(Catalog.GetString("Move Selected Pixels"));

            DocumentSelection old_selection = doc.Selection.Clone();
            bool old_show_selection         = doc.ShowSelection;

            doc.Selection.SelectionPath = p;
            doc.Selection.SelectionPolygons.Clear();
            doc.ShowSelection = true;

            doc.Workspace.Invalidate();

            paste_action.Push(new PasteHistoryItem(image, old_selection, old_show_selection));
            doc.History.PushNewItem(paste_action);
        }
Esempio n. 11
0
        public override void Redo()
        {
            Document doc = PintaCore.Workspace.ActiveDocument;

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer ();
            doc.ShowSelectionLayer = true;

            Path p;

            using (Cairo.Context g = new Cairo.Context (doc.SelectionLayer.Surface)) {
                g.DrawPixbuf (paste_image, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Rectangle (0, 0, paste_image.Width, paste_image.Height));
            }

            Swap ();

            PintaCore.Workspace.Invalidate ();
            PintaCore.Tools.SetCurrentTool (Catalog.GetString ("Move Selected Pixels"));
        }
Esempio n. 12
0
        private void HandlerPintaCoreActionsEditPasteIntoNewImageActivated(object sender, EventArgs e)
        {
            Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

            if (cb.WaitIsImageAvailable())
            {
                bool canceled = false;

                Gdk.Pixbuf image = cb.WaitForImage();

                Gdk.Size size = new Gdk.Size(image.Width, image.Height);

                if (PintaCore.Workspace.IsDirty)
                {
                    var primary = Catalog.GetString("Save the changes to image \"{0}\" before creating a new image?");
                    var secondary = Catalog.GetString("If you don't save, all changes will be permanently lost.");
                    var markup = "<span weight=\"bold\" size=\"larger\">{0}</span>\n\n{1}\n";
                    markup = string.Format(markup, primary, secondary);

                    var md = new MessageDialog(PintaCore.Chrome.MainWindow, DialogFlags.Modal,
                                                MessageType.Question, ButtonsType.None, true,
                                                markup,
                                                System.IO.Path.GetFileName(PintaCore.Workspace.Filename));

                    md.AddButton(Catalog.GetString("Close without saving"), ResponseType.No);
                    md.AddButton(Stock.Cancel, ResponseType.Cancel);
                    md.AddButton(Stock.Save, ResponseType.Yes);

                    // so that user won't accidentally overwrite
                    md.DefaultResponse = ResponseType.Cancel;

                    ResponseType response = (ResponseType)md.Run();
                    md.Destroy();

                    if (response == ResponseType.Yes)
                    {
                        PintaCore.Actions.File.Save.Activate();
                    }
                    else
                    {
                        canceled = response == ResponseType.Cancel;
                    }

                    PintaCore.Actions.File.NewFile(size);
                }
                else
                {
                    PintaCore.Workspace.ResizeImage(image.Width, image.Height);
                }

                PintaCore.Layers.FinishSelection();

                Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone();

                Path p;

                using (Cairo.Context g = new Cairo.Context(PintaCore.Layers.CurrentLayer.Surface))
                {
                    g.DrawPixbuf(image, new Cairo.Point(0, 0));
                    p = g.CreateRectanglePath(new Rectangle(0, 0, image.Width, image.Height));
                }

                PintaCore.Workspace.Invalidate();

                PintaCore.History.PushNewItem(new SimpleHistoryItem(Stock.Paste, Catalog.GetString("Paste into new image"), old, PintaCore.Layers.CurrentLayerIndex));
            }
            else
            {
                ClipboardEmptyError();
            }
        }
Esempio n. 13
0
        private void HandlerPintaCoreActionsEditPasteActivated(object sender, EventArgs e)
        {
            Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

            if (cb.WaitIsImageAvailable())
            {
                PintaCore.Layers.FinishSelection();

                Cairo.ImageSurface old = PintaCore.Layers.CurrentLayer.Surface.Clone();

                Gdk.Pixbuf image = cb.WaitForImage();

                Path p;

                using (Cairo.Context g = new Cairo.Context(PintaCore.Layers.CurrentLayer.Surface))
                {
                    g.DrawPixbuf(image, new Cairo.Point(0, 0));
                    p = g.CreateRectanglePath(new Rectangle(0, 0, image.Width, image.Height));
                }

                PintaCore.Layers.SelectionPath = p;
                PintaCore.Layers.ShowSelection = true;

                PintaCore.Workspace.Invalidate();

                PintaCore.History.PushNewItem(new SimpleHistoryItem(Stock.Paste, Catalog.GetString("Paste"), old, PintaCore.Layers.CurrentLayerIndex));
            }
            else
            {
                ClipboardEmptyError();
            }
        }
Esempio n. 14
0
        private void HandlerPintaCoreActionsEditPasteActivated(object sender, EventArgs e)
        {
            Document doc = PintaCore.Workspace.ActiveDocument;

            PintaCore.Tools.Commit();

            Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf image = cb.WaitForImage();

            if (image == null)
            {
                return;
            }

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (image.Width > canvas_size.Width || image.Height > canvas_size.Height)
            {
                string message = Catalog.GetString("The image being pasted is larger than the canvas size. What would you like to do?");

                var enlarge_dialog = new MessageDialog(PintaCore.Chrome.MainWindow, DialogFlags.Modal, MessageType.Question, ButtonsType.None, message);
                enlarge_dialog.AddButton(Catalog.GetString("Expand canvas"), ResponseType.Accept);
                enlarge_dialog.AddButton(Catalog.GetString("Don't change canvas size"), ResponseType.Reject);
                enlarge_dialog.AddButton(Stock.Cancel, ResponseType.Cancel);
                enlarge_dialog.DefaultResponse = ResponseType.Accept;

                ResponseType response = (ResponseType)enlarge_dialog.Run();
                enlarge_dialog.Destroy();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas(image.Width, image.Height, Pinta.Core.Anchor.Center);
                    PintaCore.Actions.View.UpdateCanvasScale();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer();
            doc.ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context(doc.SelectionLayer.Surface)) {
                g.DrawPixbuf(image, new Cairo.Point(0, 0));
                p = g.CreateRectanglePath(new Rectangle(0, 0, image.Width, image.Height));
            }

            PintaCore.Tools.SetCurrentTool(Catalog.GetString("Move Selected Pixels"));

            Path old_path           = doc.SelectionPath;
            bool old_show_selection = doc.ShowSelection;

            doc.SelectionPath = p;
            doc.ShowSelection = true;

            doc.Workspace.Invalidate();

            doc.History.PushNewItem(new PasteHistoryItem(image, old_path, old_show_selection));
        }
Esempio n. 15
0
        /// <summary>
        /// Create a rectangular Selection from a Rectangle.
        /// </summary>
        /// <param name="selectionSurface">The selection surface to use for calculating the rectangular Path.</param>
        /// <param name="r">The Rectangle.</param>
        public void CreateRectangleSelection(Surface selectionSurface, Rectangle r)
        {
            using (Context g = new Context(selectionSurface))
            {
                SelectionPath = g.CreateRectanglePath(r);
            }

            //Clear the Selection Polygons collection to start from a clean slate.
            SelectionPolygons.Clear();

            //The 4 corners of the Rectangle.
            int corner1X = (int)Math.Round(r.X);
            int corner1Y = (int)Math.Round(r.Y);
            int corner2X = (int)Math.Round(r.X + r.Width);
            int corner2Y = (int)Math.Round(r.Y + r.Height);

            //Create a new Polygon to store the upcoming rectangle.
            List<IntPoint> newPolygon = new List<IntPoint>();

            //Store each of the 4 corners of the Rectangle in the Polygon, and then store
            //the first corner again. It is important to note that the order of the
            //corners being added (clockwise) and the first/last Point being the same
            //should be kept this way; otherwise, problems could result.
            newPolygon.Add(new IntPoint(corner1X, corner1Y));
            newPolygon.Add(new IntPoint(corner2X, corner1Y));
            newPolygon.Add(new IntPoint(corner2X, corner2Y));
            newPolygon.Add(new IntPoint(corner1X, corner2Y));
            newPolygon.Add(new IntPoint(corner1X, corner1Y));

            //Add the newly calculated rectangular Polygon.
            SelectionPolygons.Add(newPolygon);
        }
Esempio n. 16
0
        /// <summary>
        /// Create a rectangular Selection from a Rectangle.
        /// </summary>
        /// <param name="selectionSurface">The selection surface to use for calculating the rectangular Path.</param>
        /// <param name="r">The Rectangle.</param>
        public void CreateRectangleSelection(Surface selectionSurface, Rectangle r)
        {
            using (Context g = new Context(selectionSurface))
            {
                SelectionPath = g.CreateRectanglePath(r);
            }

            //Clear the Selection Polygons collection to start from a clean slate.
            SelectionPolygons.Clear();

            SelectionPolygons.Add (CreateRectanglePolygon (r));
        }
Esempio n. 17
0
        /// <summary>
        /// Reset (clear) the Selection.
        /// </summary>
        /// <param name="selectionSurface"></param>
        /// <param name="imageSize"></param>
        public void ResetSelection(Surface selectionSurface, Gdk.Size imageSize)
        {
            using (Cairo.Context g = new Cairo.Context(selectionSurface))
            {
                SelectionPath = g.CreateRectanglePath(new Rectangle(0, 0, imageSize.Width, imageSize.Height));
            }

            SelectionPolygons.Clear();
        }
Esempio n. 18
0
        private void Activated(object sender, EventArgs e)
        {
            Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));
            if (PintaCore.Tools.CurrentTool.TryHandlePaste (cb))
                return;

            Document doc = PintaCore.Workspace.ActiveDocument;

            PintaCore.Tools.Commit ();

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf image = cb.WaitForImage ();

            if (image == null)
            {
                Dialogs.ClipboardEmptyDialog.Show ();
                return;
            }

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (image.Width > canvas_size.Width || image.Height > canvas_size.Height)
            {
                ResponseType response = ShowExpandCanvasDialog ();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas (image.Width, image.Height, Pinta.Core.Anchor.Center);
                    PintaCore.Actions.View.UpdateCanvasScale ();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer ();
            doc.ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context (doc.SelectionLayer.Surface))
            {
                g.DrawPixbuf (image, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Rectangle (0, 0, image.Width, image.Height));
            }

            PintaCore.Tools.SetCurrentTool (Catalog.GetString ("Move Selected Pixels"));

            Path old_path = doc.SelectionPath;
            bool old_show_selection = doc.ShowSelection;

            doc.SelectionPath = p;
            doc.ShowSelection = true;

            doc.Workspace.Invalidate ();

            doc.History.PushNewItem (new PasteHistoryItem (image, old_path, old_show_selection));
        }
Esempio n. 19
0
        private void HandlerPintaCoreActionsEditPasteActivated(object sender, EventArgs e)
        {
            Document doc = PintaCore.Workspace.ActiveDocument;

            PintaCore.Tools.Commit ();

            Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf image = cb.WaitForImage ();

            if (image == null)
                return;

            // Copy the paste to the temp layer
            doc.CreateSelectionLayer ();
            doc.ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context (doc.SelectionLayer.Surface)) {
                g.DrawPixbuf (image, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Rectangle (0, 0, image.Width, image.Height));
            }

            PintaCore.Tools.SetCurrentTool (Catalog.GetString ("Move Selected Pixels"));

            Path old_path = doc.SelectionPath;
            bool old_show_selection = doc.ShowSelection;

            doc.SelectionPath = p;
            doc.ShowSelection = true;

            doc.Workspace.Invalidate ();

            doc.History.PushNewItem (new PasteHistoryItem (image, old_path, old_show_selection));
        }
Esempio n. 20
0
        /// <summary>
        /// Pastes an image from the clipboard.
        /// </summary>
        /// <param name="toNewLayer">Set to TRUE to paste into a
        /// new layer.  Otherwise, will paste to the current layer.</param>
        /// <param name="x">Optional. Location within image to paste to.
        /// Position will be adjusted if pasted image would hang
        /// over right or bottom edges of canvas.</param>
        /// <param name="y">Optional. Location within image to paste to.
        /// Position will be adjusted if pasted image would hang
        /// over right or bottom edges of canvas.</param>
        public void Paste(bool toNewLayer, int x = 0, int y = 0)
        {
            // Create a compound history item for recording several
            // operations so that they can all be undone/redone together.
            CompoundHistoryItem paste_action;
            if (toNewLayer)
            {
                paste_action = new CompoundHistoryItem (Stock.Paste, Catalog.GetString ("Paste Into New Layer"));
            }
            else
            {
                paste_action = new CompoundHistoryItem (Stock.Paste, Catalog.GetString ("Paste"));
            }

            Gtk.Clipboard cb = Gtk.Clipboard.Get (Gdk.Atom.Intern ("CLIPBOARD", false));

            // See if the current tool wants to handle the paste
            // operation (e.g., the text tool could paste text)
            if (!toNewLayer)
            {
                if (PintaCore.Tools.CurrentTool.TryHandlePaste (cb))
                    return;
            }

            PintaCore.Tools.Commit ();

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf cbImage = cb.WaitForImage ();

            if (cbImage == null)
            {
                ShowClipboardEmptyDialog();
                return;
            }

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (cbImage.Width > canvas_size.Width || cbImage.Height > canvas_size.Height)
            {
                ResponseType response = ShowExpandCanvasDialog ();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas (cbImage.Width, cbImage.Height,
                    Pinta.Core.Anchor.Center, paste_action);
                    PintaCore.Actions.View.UpdateCanvasScale ();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // If the pasted image would fall off bottom- or right-
            // side of image, adjust paste position
            x = Math.Max (0, Math.Min (x, canvas_size.Width - cbImage.Width));
            y = Math.Max (0, Math.Min (y, canvas_size.Height - cbImage.Height));

            // If requested, create a new layer, make it the current
            // layer and record it's creation in the history
            if (toNewLayer)
            {
                UserLayer l = AddNewLayer (string.Empty);
                SetCurrentUserLayer (l);
                paste_action.Push (new AddLayerHistoryItem ("Menu.Layers.AddNewLayer.png", Catalog.GetString ("Add New Layer"), UserLayers.IndexOf (l)));
            }

            // Copy the paste to the temp layer, which should be at least the size of this document.
            CreateSelectionLayer (Math.Max(ImageSize.Width, cbImage.Width),
                                  Math.Max(ImageSize.Height, cbImage.Height));
            ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context (SelectionLayer.Surface))
            {

                g.DrawPixbuf (cbImage, new Cairo.Point (0, 0));
                p = g.CreateRectanglePath (new Cairo.Rectangle (x, y, cbImage.Width, cbImage.Height));
            }

            SelectionLayer.Transform.InitIdentity();
            SelectionLayer.Transform.Translate (x, y);

            PintaCore.Tools.SetCurrentTool (Catalog.GetString ("Move Selected Pixels"));

            DocumentSelection old_selection = Selection.Clone();
            bool old_show_selection = ShowSelection;

            Selection.SelectionPath = p;
            Selection.SelectionPolygons.Clear();
            ShowSelection = true;

            Workspace.Invalidate ();

            paste_action.Push (new PasteHistoryItem (cbImage, old_selection, old_show_selection));
            History.PushNewItem (paste_action);
        }
Esempio n. 21
0
        /// <summary>
        /// Pastes an image from the clipboard.
        /// </summary>
        /// <param name="toNewLayer">Set to TRUE to paste into a
        /// new layer.  Otherwise, will paste to the current layer.</param>
        /// <param name="x">Optional. Location within image to paste to.
        /// Position will be adjusted if pasted image would hang
        /// over right or bottom edges of canvas.</param>
        /// <param name="y">Optional. Location within image to paste to.
        /// Position will be adjusted if pasted image would hang
        /// over right or bottom edges of canvas.</param>
        public void Paste(bool toNewLayer, int x = 0, int y = 0)
        {
            // Create a compound history item for recording several
            // operations so that they can all be undone/redone together.
            CompoundHistoryItem paste_action;

            if (toNewLayer)
            {
                paste_action = new CompoundHistoryItem(Stock.Paste, Catalog.GetString("Paste Into New Layer"));
            }
            else
            {
                paste_action = new CompoundHistoryItem(Stock.Paste, Catalog.GetString("Paste"));
            }

            Gtk.Clipboard cb = Gtk.Clipboard.Get(Gdk.Atom.Intern("CLIPBOARD", false));

            // See if the current tool wants to handle the paste
            // operation (e.g., the text tool could paste text)
            if (!toNewLayer)
            {
                if (PintaCore.Tools.CurrentTool.TryHandlePaste(cb))
                {
                    return;
                }
            }

            PintaCore.Tools.Commit();

            Path p;

            // Don't dispose this, as we're going to give it to the history
            Gdk.Pixbuf cbImage = cb.WaitForImage();

            if (cbImage == null)
            {
                ShowClipboardEmptyDialog();
                return;
            }

            Gdk.Size canvas_size = PintaCore.Workspace.ImageSize;

            // If the image being pasted is larger than the canvas size, allow the user to optionally resize the canvas
            if (cbImage.Width > canvas_size.Width || cbImage.Height > canvas_size.Height)
            {
                ResponseType response = ShowExpandCanvasDialog();

                if (response == ResponseType.Accept)
                {
                    PintaCore.Workspace.ResizeCanvas(cbImage.Width, cbImage.Height,
                                                     Pinta.Core.Anchor.Center, paste_action);
                    PintaCore.Actions.View.UpdateCanvasScale();
                }
                else if (response == ResponseType.Cancel || response == ResponseType.DeleteEvent)
                {
                    return;
                }
            }

            // If the pasted image would fall off bottom- or right-
            // side of image, adjust paste position
            x = Math.Max(0, Math.Min(x, canvas_size.Width - cbImage.Width));
            y = Math.Max(0, Math.Min(y, canvas_size.Height - cbImage.Height));

            // If requested, create a new layer, make it the current
            // layer and record it's creation in the history
            if (toNewLayer)
            {
                UserLayer l = AddNewLayer(string.Empty);
                SetCurrentUserLayer(l);
                paste_action.Push(new AddLayerHistoryItem("Menu.Layers.AddNewLayer.png", Catalog.GetString("Add New Layer"), UserLayers.IndexOf(l)));
            }

            // Copy the paste to the temp layer, which should be at least the size of this document.
            CreateSelectionLayer(Math.Max(ImageSize.Width, cbImage.Width),
                                 Math.Max(ImageSize.Height, cbImage.Height));
            ShowSelectionLayer = true;

            using (Cairo.Context g = new Cairo.Context(SelectionLayer.Surface))
            {
                g.DrawPixbuf(cbImage, new Cairo.Point(0, 0));
                p = g.CreateRectanglePath(new Cairo.Rectangle(x, y, cbImage.Width, cbImage.Height));
            }

            SelectionLayer.Transform.InitIdentity();
            SelectionLayer.Transform.Translate(x, y);

            PintaCore.Tools.SetCurrentTool(Catalog.GetString("Move Selected Pixels"));

            DocumentSelection old_selection = Selection.Clone();
            bool old_show_selection         = ShowSelection;

            Selection.SelectionPath = p;
            Selection.SelectionPolygons.Clear();
            ShowSelection = true;

            Workspace.Invalidate();

            paste_action.Push(new PasteHistoryItem(cbImage, old_selection, old_show_selection));
            History.PushNewItem(paste_action);
        }
Esempio n. 22
0
        public void Draw(ImageSurface dst)
        {
            if (disposed)
                throw new ObjectDisposedException ("PlacedSurface");

            using (Cairo.Context g = new Cairo.Context (dst)) {
                g.Save ();

                Rectangle r = what.GetBounds ().ToCairoRectangle ();

                // We need to use the source operator to fully replace the old
                // data.  Or else we may paint transparent on top of it and
                // it will still be visible.  [Bug #670411]
                using (Path p = g.CreateRectanglePath (new Rectangle (where.X, where.Y, r.Width, r.Height))) {
                    g.AppendPath (p);
                    g.Clip ();
                    g.Operator = Operator.Source;
                    g.DrawPixbuf (what.ToPixbuf (), new Cairo.Point (where.X, where.Y));
                }

                g.Restore ();
            }
        }