1 //
2 // MagicWandTool.cs
3 //
4 // Author:
5 //       Olivier Dufour <olivier.duff@gmail.com>
6 //
7 // Copyright (c) 2010 Olivier Dufour
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 // THE SOFTWARE.
26 
27 using System;
28 using Cairo;
29 using Pinta.Core;
30 using Mono.Unix;
31 using ClipperLibrary;
32 using System.Collections.Generic;
33 
34 namespace Pinta.Tools
35 {
36 	public class MagicWandTool : FloodTool
37 	{
38 		private Gtk.ToolItem selection_sep;
39         private CombineMode combine_mode;
40 
41 		public override Gdk.Key ShortcutKey { get { return Gdk.Key.S; } }
42 
MagicWandTool()43 		public MagicWandTool()
44 		{
45 			LimitToSelection = false;
46 		}
47 
OnBuildToolBar(Gtk.Toolbar tb)48 		protected override void OnBuildToolBar(Gtk.Toolbar tb)
49 		{
50 			base.OnBuildToolBar(tb);
51 
52 			if (selection_sep == null)
53 				selection_sep = new Gtk.SeparatorToolItem();
54 
55 			tb.AppendItem(selection_sep);
56 
57 			PintaCore.Workspace.SelectionHandler.BuildToolbar(tb);
58 		}
59 
60 		public override string Name
61 		{
62 			get { return Catalog.GetString("Magic Wand Select"); }
63 		}
64 
65 		public override string Icon
66 		{
67 			get { return "Tools.MagicWand.png"; }
68 		}
69 
70 		public override string StatusBarText
71 		{
72 			get { return Catalog.GetString("Click to select region of similar color."); }
73 		}
74 
75 		public override Gdk.Cursor DefaultCursor
76 		{
77             get { return new Gdk.Cursor (Gdk.Display.Default, PintaCore.Resources.GetIcon ("Cursor.MagicWand.png"), 21, 10); }
78 		}
79 		public override int Priority { get { return 17; } }
80 
OnMouseDown(Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)81 		protected override void OnMouseDown(Gtk.DrawingArea canvas, Gtk.ButtonPressEventArgs args, Cairo.PointD point)
82 		{
83 			Document doc = PintaCore.Workspace.ActiveDocument;
84             combine_mode = PintaCore.Workspace.SelectionHandler.DetermineCombineMode (args);
85 
86 			base.OnMouseDown(canvas, args, point);
87 			doc.Selection.Visible = true;
88 		}
89 
OnFillRegionComputed(Point[][] polygonSet)90 		protected override void OnFillRegionComputed(Point[][] polygonSet)
91 		{
92 			Document doc = PintaCore.Workspace.ActiveDocument;
93 
94 			SelectionHistoryItem undoAction = new SelectionHistoryItem(this.Icon, this.Name);
95 			undoAction.TakeSnapshot();
96 
97             doc.PreviousSelection.Dispose ();
98             doc.PreviousSelection = doc.Selection.Clone ();
99 
100             doc.Selection.SelectionPolygons.Clear ();
101             SelectionModeHandler.PerformSelectionMode (combine_mode, DocumentSelection.ConvertToPolygons (polygonSet));
102 
103 			doc.History.PushNewItem(undoAction);
104 			doc.Workspace.Invalidate();
105 		}
106 	}
107 }
108