cropper
view callbacks.c @ 14:da18f2d4f92f
removed all buttons and stuff from the window; cleaned up
author | meillo@marmaro.de |
---|---|
date | Thu, 04 Mar 2010 18:31:28 +0100 |
parents | 13bc21684b8a |
children | 7dbf3879939a |
line source
1 #include <gtk/gtk.h>
2 #include <gdk-pixbuf/gdk-pixbuf.h>
3 #include <gdk/gdkkeysyms.h>
4 #include "main.h"
5 #include "callbacks.h"
6 #include "support.h"
8 int image_width;
9 int image_height;
10 float inc = 0.3;
11 float zoom = 1.0;
14 /*
15 void
16 update_spin_button_ratio(GtkWidget* spinbutton)
17 {
18 g_print("ratio change: %f\n", ratio);
19 on_ratio_w_spinbutton_value_changed(spinbutton, NULL);
20 }
23 void
24 on_ratio_none_activate(GtkMenuItem* menuitem, gpointer user_data)
25 {
26 ratio = 1;
27 update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
28 }
31 void
32 on_ratio_square_activate(GtkMenuItem* menuitem, gpointer user_data)
33 {
34 ratio = 1;
35 update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
36 }
39 void
40 on_ratio_4x3_activate(GtkMenuItem* menuitem, gpointer user_data)
41 {
42 ratio = 4.0/3.0;
43 update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
44 }
47 void
48 on_ratio_3x2_activate(GtkMenuItem* menuitem, gpointer user_data)
49 {
50 ratio = 1.5;
51 update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
52 }
55 void
56 on_ratio_custom_activate(GtkMenuItem* menuitem, gpointer user_data)
57 {
58 ratio = gtk_spin_button_get_value(lookup_widget(cropper_window, "ratio_w_spinbutton"))
59 / gtk_spin_button_get_value(lookup_widget(cropper_window, "ratio_h_spinbutton"));
60 update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
61 }
62 */
72 /*
73 static void
74 ratio_optionmenu_changed_cb(GtkOptionMenu* optionmenu, gpointer unser_data)
75 {
76 int idx = gtk_option_menu_get_history(optionmenu);
77 int w = 1, h = 1;
78 gboolean use_ratio = TRUE;
80 switch (idx) {
81 case GTH_CROP_RATIO_NONE:
82 use_ratio = FALSE;
83 break;
84 case GTH_CROP_RATIO_SQUARE:
85 w = h = 1;
86 break;
87 case GTH_CROP_RATIO_IMAGE:
88 w = lookup_widget(cropper_window, "image_width");
89 h = lookup_widget(cropper_window, "image_height");
90 break;
91 case GTH_CROP_RATIO_DISPLAY:
92 w = lookup_widget(cropper_window, "display_width");
93 h = lookup_widget(cropper_window, "display_height");
94 break;
95 case GTH_CROP_RATIO_4_3:
96 w = 4;
97 h = 3;
98 break;
99 case GTH_CROP_RATIO_4_6:
100 w = 4;
101 h = 6;
102 break;
103 case GTH_CROP_RATIO_CUSTOM:
104 default:
105 w = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
106 lookup_widget(cropper_window, "ratio_w_spinbutton")));
107 h = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
108 lookup_widget(cropper_window, "ratio_h_spinbutton")));
109 break;
110 }
112 gtk_widget_set_sensitive(lookup_widget(cropper_window, "custom_ratio_box"),
113 idx == GTH_CROP_RATIO_CUSTOM);
114 set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_w_spinbutton"), w);
115 set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_h_spinbutton"), h);
117 gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(lookup_widget(cropper_window, "crop_image")),
118 use_ratio, (double) w / h);
119 }
122 static void
123 ratio_value_changed_cb(GtkSpinButton* spin, gpointer unser_data)
124 {
125 int w, h;
127 w = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_w_spinbutton"));
128 h = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_h_spinbutton"));
130 gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(
131 lookup_widget(cropper_window, "crop_image")), TRUE, (double) w / h);
132 }
135 static void
136 ratio_swap_button_cb(GtkButton* button, gpointer unser_data)
137 {
138 int w, h;
140 w = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_w_spinbutton"));
141 h = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_h_spinbutton"));
143 set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_w_spinbutton"), h);
144 set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_h_spinbutton"), w);
146 gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(lookup_widget(cropper_window,
147 "crop_image")), TRUE, (double) h / w);
148 }
157 void
158 on_ratio_w_spinbutton_value_changed(GtkSpinButton* spinbutton, gpointer user_data)
159 {
160 g_print("spinbutton W - ratio: %f\n", ratio);
161 gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_h_spinbutton"),
162 gtk_spin_button_get_value(spinbutton) / ratio);
163 }
166 void
167 on_ratio_w_spinbutton_change_value(GtkSpinButton* spinbutton, GtkScrollType scroll, gpointer user_data)
168 {
169 }
172 void
173 on_ratio_h_spinbutton_change_value(GtkSpinButton* spinbutton, GtkScrollType scroll, gpointer user_data)
174 {
175 }
178 void
179 on_ratio_h_spinbutton_value_changed(GtkSpinButton* spinbutton, gpointer user_data)
180 {
181 g_print("spinbutton H - ratio: %f\n", ratio);
182 gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_w_spinbutton"),
183 gtk_spin_button_get_value(spinbutton) * ratio);
185 }
188 void
189 on_ratio_swap_button_clicked(GtkButton* button, gpointer user_data)
190 {
192 ratio = 1.0 / ratio;
193 gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_w_spinbutton"),
194 gtk_spin_button_get_value(lookup_widget(cropper_window,
195 "ratio_h_spinbutton")));
196 gtk_spin_button_update(lookup_widget(cropper_window, "ratio_w_spinbutton"));
198 }
199 */
202 void
203 update_title(char* zoom)
204 {
205 char title[128];
206 snprintf(title, 128, "cropper (%s) %dx%d+%d+%d", zoom, w, h, x, y);
207 gtk_window_set_title(GTK_WINDOW(cropper_window), title);
208 }
211 /* zoom */
212 void
213 set_zoom()
214 {
215 static GdkPixbuf* pixbuf_new;
216 g_object_unref(pixbuf_new);
218 pixbuf_new = gdk_pixbuf_scale_simple(image_buffer, image_width*zoom, image_height*zoom,
219 GDK_INTERP_BILINEAR);
220 gtk_image_set_from_pixbuf((GtkImage*) lookup_widget(cropper_window, "image_area"), pixbuf_new);
221 }
223 void
224 on_zoom_in_button_clicked(GtkObject* object, gpointer user_data)
225 {
226 zoom *= 1 + inc;
227 set_zoom();
228 }
230 void
231 on_zoom_out_button_clicked(GtkObject* object, gpointer user_data)
232 {
233 zoom *= 1 - inc;
234 set_zoom();
235 }
237 void
238 on_zoom_100_button_clicked(GtkObject* object, gpointer user_data)
239 {
240 zoom = 1.0;
241 set_zoom();
242 }
244 void
245 on_zoom_fit_button_clicked(GtkObject* object, gpointer user_data)
246 {
247 int w, h;
248 float zw, zh;
249 GtkWidget* image_a;
251 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
252 gdk_drawable_get_size(image_a->window, &w, &h);
254 zw = w*1.0 / image_width;
255 zh = h*1.0 / image_height;
257 zoom = (zw < zh) ? zw : zh;
258 set_zoom();
259 }
267 void
268 crop(void)
269 {
270 char crop_call[256];
272 sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"",
273 gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
274 "crop_width_spinbutton")),
275 gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
276 "crop_height_spinbutton")),
277 gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
278 "crop_x_spinbutton")),
279 gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
280 "crop_y_spinbutton")),
281 image_filename,
282 image_filename
283 );
284 system(crop_call);
285 gtk_main_quit();
286 }
289 void
290 on_cropper_window_create(GtkObject* object, gpointer user_data)
291 {
292 image_width = gdk_pixbuf_get_width(image_buffer);
293 image_height = gdk_pixbuf_get_height(image_buffer);
295 /*
296 image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
297 image_area = gtk_image_new_from_pixbuf(image_buffer);
299 int w, h;
300 GtkWidget* image_a;
302 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
303 gdk_drawable_get_size(image_a->window, &w, &h);
305 image_width = w - 200;
306 image_height = h - 150;
307 set_zoom();
308 */
309 }
311 void
312 on_cropper_window_destroy(GtkObject* object, gpointer user_data)
313 {
314 gtk_main_quit();
315 }
320 gboolean
321 on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata)
322 {
323 if (pKey->type != GDK_KEY_PRESS) {
324 return FALSE;
325 }
326 switch (pKey->keyval) {
327 case GDK_q:
328 gtk_main_quit();
329 break;
330 case GDK_Return:
331 crop();
332 break;
333 case GDK_plus:
334 on_zoom_in_button_clicked(NULL, userdata);
335 break;
336 case GDK_minus:
337 on_zoom_out_button_clicked(NULL, userdata);
338 break;
339 case GDK_0:
340 on_zoom_100_button_clicked(NULL, userdata);
341 break;
342 case GDK_f:
343 on_zoom_fit_button_clicked(NULL, userdata);
344 break;
345 }
346 return TRUE;
347 }