Mercurial > cropper
annotate cropper.c @ 19:762de8cee1e4 default tip
fixed makefile; some cleanups
author | meillo@marmaro.de |
---|---|
date | Thu, 04 Mar 2010 19:44:28 +0100 |
parents | 9d6e8105b145 |
children |
rev | line source |
---|---|
17 | 1 #include <sys/types.h> |
2 #include <sys/stat.h> | |
3 #include <unistd.h> | |
4 #include <string.h> | |
5 #include <stdio.h> | |
6 | |
0 | 7 #include <gtk/gtk.h> |
17 | 8 #include <gdk/gdkkeysyms.h> |
9 #include <gdk-pixbuf/gdk-pixbuf.h> | |
10 | |
11 | |
12 | |
13 | |
14 GtkWidget* cropper_window; | |
15 char* image_filename; | |
16 | |
17 /* from interface.h */ | |
18 GtkWidget* create_cropper_window(void); | |
19 GtkWidget* image_area; | |
20 GdkPixbuf* image_buffer; | |
21 | |
22 int w, h, x, y; | |
23 | |
24 int image_width; | |
25 int image_height; | |
26 float inc = 0.3; | |
27 float zoom = 1.0; | |
28 | |
29 /* from callbacks.h */ | |
30 double ratio; | |
31 | |
32 | |
33 | |
34 | |
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) | |
38 | |
39 #define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \ | |
40 g_object_set_data(G_OBJECT(component), name, widget) | |
41 | |
42 | |
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; | |
54 | |
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 } | |
69 | |
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 } | |
76 | |
77 | |
78 | |
79 | |
80 | |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
0 | 87 |
17 | 88 |
89 | |
90 | |
91 | |
92 | |
93 | |
94 | |
95 | |
96 | |
97 | |
98 | |
99 /* zoom */ | |
100 void | |
101 set_zoom() | |
102 { | |
103 static GdkPixbuf* pixbuf_new; | |
104 g_object_unref(pixbuf_new); | |
105 | |
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 } | |
110 | |
111 void | |
112 on_zoom_in_button_clicked(GtkObject* object, gpointer user_data) | |
113 { | |
114 zoom *= 1 + inc; | |
115 set_zoom(); | |
116 } | |
117 | |
118 void | |
119 on_zoom_out_button_clicked(GtkObject* object, gpointer user_data) | |
120 { | |
121 zoom *= 1 - inc; | |
122 set_zoom(); | |
123 } | |
124 | |
125 void | |
126 on_zoom_100_button_clicked(GtkObject* object, gpointer user_data) | |
127 { | |
128 zoom = 1.0; | |
129 set_zoom(); | |
130 } | |
131 | |
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; | |
138 | |
139 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area"); | |
140 gdk_drawable_get_size(image_a->window, &w, &h); | |
141 | |
142 zw = w*1.0 / image_width; | |
143 zh = h*1.0 / image_height; | |
144 | |
145 zoom = (zw < zh) ? zw : zh; | |
146 set_zoom(); | |
147 } | |
148 | |
149 | |
150 | |
151 | |
152 | |
153 | |
154 | |
155 void | |
156 crop(void) | |
157 { | |
158 char crop_call[256]; | |
159 | |
160 sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"", | |
19 | 161 w, h, x, y, image_filename, image_filename); |
17 | 162 system(crop_call); |
163 gtk_main_quit(); | |
164 } | |
165 | |
166 | |
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); | |
172 | |
173 /* | |
174 image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL); | |
175 image_area = gtk_image_new_from_pixbuf(image_buffer); | |
176 | |
177 int w, h; | |
178 GtkWidget* image_a; | |
179 | |
180 image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area"); | |
181 gdk_drawable_get_size(image_a->window, &w, &h); | |
182 | |
183 image_width = w - 200; | |
184 image_height = h - 150; | |
185 set_zoom(); | |
186 */ | |
187 } | |
188 | |
189 void | |
190 on_cropper_window_destroy(GtkObject* object, gpointer user_data) | |
191 { | |
192 gtk_main_quit(); | |
193 } | |
194 | |
195 | |
196 | |
197 | |
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 } | |
226 | |
227 | |
228 | |
229 | |
230 | |
19 | 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; | |
239 | |
240 | |
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"); | |
245 | |
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); | |
250 | |
251 | |
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); | |
256 | |
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); | |
261 | |
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); | |
266 | |
267 /* image_area */ | |
268 image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL); | |
269 image_area = gtk_image_new_from_pixbuf(image_buffer); | |
270 | |
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); | |
274 | |
275 | |
276 | |
277 | |
278 | |
279 g_signal_connect(G_OBJECT(cropper_window), "show", | |
280 G_CALLBACK(on_cropper_window_create), NULL); | |
281 | |
282 g_signal_connect(G_OBJECT(cropper_window), "destroy", | |
283 G_CALLBACK(on_cropper_window_destroy), NULL); | |
284 | |
285 g_signal_connect(cropper_window, "key-press-event", | |
286 G_CALLBACK(on_key_press), NULL); | |
287 | |
288 | |
289 /* Store pointers to all widgets, for use by lookup_widget(). */ | |
290 | |
291 GLADE_HOOKUP_OBJECT_NO_REF(cropper_window, cropper_window, "cropper_window"); | |
292 GLADE_HOOKUP_OBJECT(cropper_window, dialog_vbox1, "dialog_vbox1"); | |
293 | |
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"); | |
299 | |
300 | |
301 return cropper_window; | |
302 } | |
303 | |
304 | |
17 | 305 |
0 | 306 |
2
e359bea4c8ac
added code for ratio swap; new names for ratio operations; added main.h; some more
meillo@marmaro.de
parents:
1
diff
changeset
|
307 |
11 | 308 int |
309 main(int argc, char* argv[]) | |
310 { | |
4
2f11ab3e6047
added option handling; added output for convert; commented all ratio code cause it is not really important now
meillo@marmaro.de
parents:
2
diff
changeset
|
311 /* commandline option handling */ |
11 | 312 if (argc != 2) { |
10 | 313 g_print("cropper -- a crop frontend to convert\n"); |
314 g_print("usage: cropper IMAGE\n"); | |
315 return 1; | |
4
2f11ab3e6047
added option handling; added output for convert; commented all ratio code cause it is not really important now
meillo@marmaro.de
parents:
2
diff
changeset
|
316 } |
2f11ab3e6047
added option handling; added output for convert; commented all ratio code cause it is not really important now
meillo@marmaro.de
parents:
2
diff
changeset
|
317 |
11 | 318 image_filename = argv[1]; |
10 | 319 gtk_init(&argc, &argv); |
4
2f11ab3e6047
added option handling; added output for convert; commented all ratio code cause it is not really important now
meillo@marmaro.de
parents:
2
diff
changeset
|
320 |
10 | 321 cropper_window = create_cropper_window(); |
322 gtk_widget_show(cropper_window); | |
11 | 323 gtk_main(); |
0 | 324 |
10 | 325 return 0; |
0 | 326 } |