cropper
view cropper.c @ 19:762de8cee1e4
fixed makefile; some cleanups
author | meillo@marmaro.de |
---|---|
date | Thu, 04 Mar 2010 19:44:28 +0100 |
parents | 9d6e8105b145 |
children |
line source
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <unistd.h>
4 #include <string.h>
5 #include <stdio.h>
7 #include <gtk/gtk.h>
8 #include <gdk/gdkkeysyms.h>
9 #include <gdk-pixbuf/gdk-pixbuf.h>
14 GtkWidget* cropper_window;
15 char* image_filename;
17 /* from interface.h */
18 GtkWidget* create_cropper_window(void);
19 GtkWidget* image_area;
20 GdkPixbuf* image_buffer;
22 int w, h, x, y;
24 int image_width;
25 int image_height;
26 float inc = 0.3;
27 float zoom = 1.0;
29 /* from callbacks.h */
30 double ratio;
35 #define GLADE_HOOKUP_OBJECT(component,widget,name) \
36 g_object_set_data_full(G_OBJECT(component), name, \
37 gtk_widget_ref(widget),(GDestroyNotify) gtk_widget_unref)
39 #define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
40 g_object_set_data(G_OBJECT(component), name, widget)
43 /*
44 * This function returns a widget in a component created by Glade.
45 * Call it with the toplevel widget in the component (i.e. a window/dialog),
46 * or alternatively any widget in the component, and the name of the widget
47 * you want returned.
48 */
49 GtkWidget*
50 lookup_widget(GtkWidget* widget, const gchar* widget_name)
51 {
52 GtkWidget* parent;
53 GtkWidget* found_widget;
55 for (;;) {
56 if (GTK_IS_MENU(widget)) {
57 parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
58 } else {
59 parent = widget->parent;
60 }
61 if (!parent) {
62 parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
63 }
64 if (parent == NULL) {
65 break;
66 }
67 widget = parent;
68 }
70 found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
71 if (!found_widget) {
72 g_warning("Widget not found: %s", widget_name);
73 }
74 return found_widget;
75 }
99 /* zoom */
100 void
101 set_zoom()
102 {
103 static GdkPixbuf* pixbuf_new;
104 g_object_unref(pixbuf_new);
106 pixbuf_new = gdk_pixbuf_scale_simple(image_buffer, image_width*zoom, image_height*zoom,
107 GDK_INTERP_BILINEAR);
108 gtk_image_set_from_pixbuf((GtkImage*) lookup_widget(cropper_window, "image_area"), pixbuf_new);
109 }
111 void
112 on_zoom_in_button_clicked(GtkObject* object, gpointer user_data)
113 {
114 zoom *= 1 + inc;
115 set_zoom();
116 }
118 void
119 on_zoom_out_button_clicked(GtkObject* object, gpointer user_data)
120 {
121 zoom *= 1 - inc;
122 set_zoom();
123 }
125 void
126 on_zoom_100_button_clicked(GtkObject* object, gpointer user_data)
127 {
128 zoom = 1.0;
129 set_zoom();
130 }
132 void
133 on_zoom_fit_button_clicked(GtkObject* object, gpointer user_data)
134 {
135 int w, h;
136 float zw, zh;
137 GtkWidget* image_a;
139 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
140 gdk_drawable_get_size(image_a->window, &w, &h);
142 zw = w*1.0 / image_width;
143 zh = h*1.0 / image_height;
145 zoom = (zw < zh) ? zw : zh;
146 set_zoom();
147 }
155 void
156 crop(void)
157 {
158 char crop_call[256];
160 sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"",
161 w, h, x, y, image_filename, image_filename);
162 system(crop_call);
163 gtk_main_quit();
164 }
167 void
168 on_cropper_window_create(GtkObject* object, gpointer user_data)
169 {
170 image_width = gdk_pixbuf_get_width(image_buffer);
171 image_height = gdk_pixbuf_get_height(image_buffer);
173 /*
174 image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
175 image_area = gtk_image_new_from_pixbuf(image_buffer);
177 int w, h;
178 GtkWidget* image_a;
180 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
181 gdk_drawable_get_size(image_a->window, &w, &h);
183 image_width = w - 200;
184 image_height = h - 150;
185 set_zoom();
186 */
187 }
189 void
190 on_cropper_window_destroy(GtkObject* object, gpointer user_data)
191 {
192 gtk_main_quit();
193 }
198 gboolean
199 on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata)
200 {
201 if (pKey->type != GDK_KEY_PRESS) {
202 return FALSE;
203 }
204 switch (pKey->keyval) {
205 case GDK_q:
206 gtk_main_quit();
207 break;
208 case GDK_Return:
209 crop();
210 break;
211 case GDK_plus:
212 on_zoom_in_button_clicked(NULL, userdata);
213 break;
214 case GDK_minus:
215 on_zoom_out_button_clicked(NULL, userdata);
216 break;
217 case GDK_0:
218 on_zoom_100_button_clicked(NULL, userdata);
219 break;
220 case GDK_f:
221 on_zoom_fit_button_clicked(NULL, userdata);
222 break;
223 }
224 return TRUE;
225 }
231 GtkWidget*
232 create_cropper_window(void)
233 {
234 GtkWidget *cropper_window;
235 GtkWidget *dialog_vbox1;
236 GtkWidget *status;
237 GtkWidget *label1;
238 GtkWidget *label2;
241 cropper_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
242 gtk_widget_set_name(cropper_window, "cropper_window");
243 gtk_container_set_border_width(GTK_CONTAINER(cropper_window), 6);
244 gtk_window_set_title(GTK_WINDOW(cropper_window), "cropper-0.2");
246 dialog_vbox1 = gtk_vbox_new(FALSE, 6);
247 gtk_widget_set_name(dialog_vbox1, "dialog_vbox1");
248 gtk_widget_show(dialog_vbox1);
249 gtk_container_add(GTK_CONTAINER(cropper_window), dialog_vbox1);
252 status = gtk_hbox_new(FALSE, 0);
253 gtk_widget_set_name(status, "status");
254 gtk_widget_show(status);
255 gtk_box_pack_end(GTK_BOX(dialog_vbox1), status, FALSE, FALSE, 0);
257 label1 = gtk_label_new("zoom:");
258 gtk_widget_set_name(label1, "label1");
259 gtk_widget_show(label1);
260 gtk_box_pack_start(GTK_BOX(status), label1, FALSE, FALSE, 0);
262 label2 = gtk_label_new("1.0");
263 gtk_widget_set_name(label2, "label2");
264 gtk_widget_show(label2);
265 gtk_box_pack_start(GTK_BOX(status), label2, FALSE, FALSE, 0);
267 /* image_area */
268 image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
269 image_area = gtk_image_new_from_pixbuf(image_buffer);
271 gtk_widget_set_name(image_area, "image_area");
272 gtk_widget_show(image_area);
273 gtk_box_pack_start(GTK_BOX(dialog_vbox1), image_area, TRUE, TRUE, 0);
279 g_signal_connect(G_OBJECT(cropper_window), "show",
280 G_CALLBACK(on_cropper_window_create), NULL);
282 g_signal_connect(G_OBJECT(cropper_window), "destroy",
283 G_CALLBACK(on_cropper_window_destroy), NULL);
285 g_signal_connect(cropper_window, "key-press-event",
286 G_CALLBACK(on_key_press), NULL);
289 /* Store pointers to all widgets, for use by lookup_widget(). */
291 GLADE_HOOKUP_OBJECT_NO_REF(cropper_window, cropper_window, "cropper_window");
292 GLADE_HOOKUP_OBJECT(cropper_window, dialog_vbox1, "dialog_vbox1");
294 GLADE_HOOKUP_OBJECT(cropper_window, label1, "label1");
295 GLADE_HOOKUP_OBJECT(cropper_window, label2, "label2");
296 GLADE_HOOKUP_OBJECT(cropper_window, status, "status");
297 GLADE_HOOKUP_OBJECT(cropper_window, image_area, "image_area");
298 GLADE_HOOKUP_OBJECT(cropper_window,(GtkWidget*) image_buffer, "image_buffer");
301 return cropper_window;
302 }
308 int
309 main(int argc, char* argv[])
310 {
311 /* commandline option handling */
312 if (argc != 2) {
313 g_print("cropper -- a crop frontend to convert\n");
314 g_print("usage: cropper IMAGE\n");
315 return 1;
316 }
318 image_filename = argv[1];
319 gtk_init(&argc, &argv);
321 cropper_window = create_cropper_window();
322 gtk_widget_show(cropper_window);
323 gtk_main();
325 return 0;
326 }