1 //
2 // ResizeImageDialog.cs
3 //
4 // Author:
5 //       Jonathan Pobst <monkey@jpobst.com>
6 //
7 // Copyright (c) 2010 Jonathan Pobst
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 Gtk;
29 using Mono.Unix;
30 using Pinta.Core;
31 
32 namespace Pinta
33 {
34 	public class ResizeImageDialog : Dialog
35 	{
36 		private RadioButton percentageRadio;
37 		private RadioButton absoluteRadio;
38 		private SpinButton percentageSpinner;
39 		private SpinButton widthSpinner;
40 		private SpinButton heightSpinner;
41 		private CheckButton aspectCheckbox;
42 
43 		private bool value_changing;
44 
ResizeImageDialog()45 		public ResizeImageDialog () : base (Catalog.GetString ("Resize Image"), PintaCore.Chrome.MainWindow,
46 		                                    DialogFlags.Modal,
47 											Gtk.Stock.Cancel, Gtk.ResponseType.Cancel,
48 											Gtk.Stock.Ok, Gtk.ResponseType.Ok)
49 		{
50 			Build ();
51 
52 			aspectCheckbox.Active = true;
53 
54 			widthSpinner.Value = PintaCore.Workspace.ImageSize.Width;
55 			heightSpinner.Value = PintaCore.Workspace.ImageSize.Height;
56 
57 			percentageRadio.Toggled += new EventHandler (percentageRadio_Toggled);
58 			absoluteRadio.Toggled += new EventHandler (absoluteRadio_Toggled);
59 			percentageRadio.Toggle ();
60 
61 			percentageSpinner.Value = 100;
62 			percentageSpinner.ValueChanged += new EventHandler (percentageSpinner_ValueChanged);
63 
64 			widthSpinner.ValueChanged += new EventHandler (widthSpinner_ValueChanged);
65 			heightSpinner.ValueChanged += new EventHandler (heightSpinner_ValueChanged);
66 
67 			AlternativeButtonOrder = new int[] { (int) Gtk.ResponseType.Ok, (int) Gtk.ResponseType.Cancel };
68 			DefaultResponse = Gtk.ResponseType.Ok;
69 
70 			widthSpinner.ActivatesDefault = true;
71 			heightSpinner.ActivatesDefault = true;
72 			percentageSpinner.ActivatesDefault = true;
73 			percentageSpinner.GrabFocus();
74 		}
75 
76 		#region Public Methods
SaveChanges()77 		public void SaveChanges ()
78 		{
79 			PintaCore.Workspace.ResizeImage (widthSpinner.ValueAsInt, heightSpinner.ValueAsInt);
80 		}
81 		#endregion
82 
83 		#region Private Methods
heightSpinner_ValueChanged(object sender, EventArgs e)84 		private void heightSpinner_ValueChanged (object sender, EventArgs e)
85 		{
86 			if (value_changing)
87 				return;
88 
89 			if (aspectCheckbox.Active) {
90 				value_changing = true;
91 				widthSpinner.Value = (int)((heightSpinner.Value * PintaCore.Workspace.ImageSize.Width) / PintaCore.Workspace.ImageSize.Height);
92 				value_changing = false;
93 			}
94 		}
95 
widthSpinner_ValueChanged(object sender, EventArgs e)96 		private void widthSpinner_ValueChanged (object sender, EventArgs e)
97 		{
98 			if (value_changing)
99 				return;
100 
101 			if (aspectCheckbox.Active) {
102 				value_changing = true;
103 				heightSpinner.Value = (int)((widthSpinner.Value * PintaCore.Workspace.ImageSize.Height) / PintaCore.Workspace.ImageSize.Width);
104 				value_changing = false;
105 			}
106 		}
107 
percentageSpinner_ValueChanged(object sender, EventArgs e)108 		private void percentageSpinner_ValueChanged (object sender, EventArgs e)
109 		{
110 			widthSpinner.Value = (int)(PintaCore.Workspace.ImageSize.Width * (percentageSpinner.ValueAsInt / 100f));
111 			heightSpinner.Value = (int)(PintaCore.Workspace.ImageSize.Height * (percentageSpinner.ValueAsInt / 100f));
112 		}
113 
absoluteRadio_Toggled(object sender, EventArgs e)114 		private void absoluteRadio_Toggled (object sender, EventArgs e)
115 		{
116 			RadioToggle ();
117 		}
118 
percentageRadio_Toggled(object sender, EventArgs e)119 		private void percentageRadio_Toggled (object sender, EventArgs e)
120 		{
121 			RadioToggle ();
122 		}
123 
RadioToggle()124 		private void RadioToggle ()
125 		{
126 			if (percentageRadio.Active) {
127 				percentageSpinner.Sensitive = true;
128 
129 				widthSpinner.Sensitive = false;
130 				heightSpinner.Sensitive = false;
131 				aspectCheckbox.Sensitive = false;
132 			} else {
133 				percentageSpinner.Sensitive = false;
134 
135 				widthSpinner.Sensitive = true;
136 				heightSpinner.Sensitive = true;
137 				aspectCheckbox.Sensitive = true;
138 			}
139 		}
140 
Build()141 		private void Build()
142 		{
143 			Icon = PintaCore.Resources.GetIcon ("Menu.Image.Resize.png");
144 			WindowPosition = WindowPosition.CenterOnParent;
145 
146 			DefaultWidth = 300;
147 			DefaultHeight = 200;
148 
149 			percentageRadio = new RadioButton (Catalog.GetString ("By percentage:"));
150 			absoluteRadio = new RadioButton (percentageRadio, Catalog.GetString ("By absolute size:"));
151 
152 			percentageSpinner = new SpinButton (1, 1000, 1);
153 			widthSpinner = new SpinButton (1, 10000, 1);
154 			heightSpinner = new SpinButton (1, 10000, 1);
155 
156 			aspectCheckbox = new CheckButton (Catalog.GetString ("Maintain aspect ratio"));
157 
158 			const int spacing = 6;
159 			var main_vbox = new VBox () { Spacing = spacing, BorderWidth = 12 };
160 
161 			var hbox_percent = new HBox () { Spacing = spacing };
162 			hbox_percent.PackStart (percentageRadio, true, true, 0);
163 			hbox_percent.PackStart (percentageSpinner, false, false, 0);
164 			hbox_percent.PackEnd (new Label ("%"), false, false, 0);
165 			main_vbox.PackStart (hbox_percent, false, false, 0);
166 
167 			main_vbox.PackStart (absoluteRadio, false, false, 0);
168 
169 			var hbox_width = new HBox () { Spacing = spacing };
170 			hbox_width.PackStart (new Label (Catalog.GetString ("Width:")), false, false, 0);
171 			hbox_width.PackStart (widthSpinner, false, false, 0);
172 			hbox_width.PackStart (new Label (Catalog.GetString ("pixels")), false, false, 0);
173 			main_vbox.PackStart (hbox_width, false, false, 0);
174 
175 			var hbox_height = new HBox () { Spacing = spacing };
176 			hbox_height.PackStart (new Label (Catalog.GetString ("Height:")), false, false, 0);
177 			hbox_height.PackStart (heightSpinner, false, false, 0);
178 			hbox_height.PackStart (new Label (Catalog.GetString ("pixels")), false, false, 0);
179 			main_vbox.PackStart (hbox_height, false, false, 0);
180 
181 			main_vbox.PackStart (aspectCheckbox, false, false, 0);
182 
183 			VBox.BorderWidth = 2;
184 			VBox.Add (main_vbox);
185 
186 			ShowAll ();
187 		}
188 		#endregion
189 	}
190 }
191 
192