cropper

diff main.c @ 17:4453456abad1

gathered all code in one file
author meillo@marmaro.de
date Thu, 04 Mar 2010 19:34:12 +0100
parents 7dbf3879939a
children
line diff
     1.1 --- a/main.c	Thu Mar 04 19:20:41 2010 +0100
     1.2 +++ b/main.c	Thu Mar 04 19:34:12 2010 +0100
     1.3 @@ -1,6 +1,332 @@
     1.4 +#include <sys/types.h>
     1.5 +#include <sys/stat.h>
     1.6 +#include <unistd.h>
     1.7 +#include <string.h>
     1.8 +#include <stdio.h>
     1.9 +
    1.10  #include <gtk/gtk.h>
    1.11 +#include <gdk/gdkkeysyms.h>
    1.12 +#include <gdk-pixbuf/gdk-pixbuf.h>
    1.13  
    1.14 -#include "main.h"
    1.15 +
    1.16 +
    1.17 +
    1.18 +GtkWidget* cropper_window;
    1.19 +char* image_filename;
    1.20 +
    1.21 +/* from interface.h */
    1.22 +GtkWidget* create_cropper_window(void);
    1.23 +GtkWidget* image_area;
    1.24 +GdkPixbuf* image_buffer;
    1.25 +
    1.26 +int w, h, x, y;
    1.27 +
    1.28 +int image_width;
    1.29 +int image_height;
    1.30 +float inc = 0.3;
    1.31 +float zoom = 1.0;
    1.32 +
    1.33 +/* from callbacks.h */
    1.34 +double ratio;
    1.35 +
    1.36 +void crop(void);
    1.37 +gboolean on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata);
    1.38 +void on_cropper_window_create(GtkObject* object, gpointer user_data);
    1.39 +void on_cropper_window_destroy(GtkObject* object, gpointer user_data);
    1.40 +
    1.41 +
    1.42 +
    1.43 +
    1.44 +
    1.45 +#define GLADE_HOOKUP_OBJECT(component,widget,name) \
    1.46 +  g_object_set_data_full(G_OBJECT(component), name, \
    1.47 +    gtk_widget_ref(widget),(GDestroyNotify) gtk_widget_unref)
    1.48 +
    1.49 +#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
    1.50 +  g_object_set_data(G_OBJECT(component), name, widget)
    1.51 +
    1.52 +
    1.53 +/*
    1.54 + * This function returns a widget in a component created by Glade.
    1.55 + * Call it with the toplevel widget in the component (i.e. a window/dialog),
    1.56 + * or alternatively any widget in the component, and the name of the widget
    1.57 + * you want returned.
    1.58 + */
    1.59 +GtkWidget*
    1.60 +lookup_widget(GtkWidget* widget, const gchar* widget_name)
    1.61 +{
    1.62 +	GtkWidget* parent;
    1.63 +	GtkWidget* found_widget;
    1.64 +
    1.65 +	for (;;) {
    1.66 +		if (GTK_IS_MENU(widget)) {
    1.67 +			parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
    1.68 +		} else {
    1.69 +			parent = widget->parent;
    1.70 +		}
    1.71 +		if (!parent) {
    1.72 +			parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
    1.73 +		}
    1.74 +		if (parent == NULL) {
    1.75 +			break;
    1.76 +		}
    1.77 +		widget = parent;
    1.78 +	}
    1.79 +
    1.80 +	found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
    1.81 +	if (!found_widget) {
    1.82 +		g_warning("Widget not found: %s", widget_name);
    1.83 +	}
    1.84 +	return found_widget;
    1.85 +}
    1.86 +
    1.87 +
    1.88 +
    1.89 +GtkWidget*
    1.90 +create_cropper_window(void)
    1.91 +{
    1.92 +  GtkWidget *cropper_window;
    1.93 +  GtkWidget *dialog_vbox1;
    1.94 +  GtkWidget *status;
    1.95 +  GtkWidget *label1;
    1.96 +  GtkWidget *label2;
    1.97 +
    1.98 +
    1.99 +  cropper_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   1.100 +  gtk_widget_set_name(cropper_window, "cropper_window");
   1.101 +  gtk_container_set_border_width(GTK_CONTAINER(cropper_window), 6);
   1.102 +  gtk_window_set_title(GTK_WINDOW(cropper_window), "cropper-0.2");
   1.103 +
   1.104 +  dialog_vbox1 = gtk_vbox_new(FALSE, 6);
   1.105 +  gtk_widget_set_name(dialog_vbox1, "dialog_vbox1");
   1.106 +  gtk_widget_show(dialog_vbox1);
   1.107 +  gtk_container_add(GTK_CONTAINER(cropper_window), dialog_vbox1);
   1.108 +
   1.109 +
   1.110 +  status = gtk_hbox_new(FALSE, 0);
   1.111 +  gtk_widget_set_name(status, "status");
   1.112 +  gtk_widget_show(status);
   1.113 +  gtk_box_pack_end(GTK_BOX(dialog_vbox1), status, FALSE, FALSE, 0);
   1.114 +
   1.115 +  label1 = gtk_label_new("zoom:");
   1.116 +  gtk_widget_set_name(label1, "label1");
   1.117 +  gtk_widget_show(label1);
   1.118 +  gtk_box_pack_start(GTK_BOX(status), label1, FALSE, FALSE, 0);
   1.119 +
   1.120 +  label2 = gtk_label_new("1.0");
   1.121 +  gtk_widget_set_name(label2, "label2");
   1.122 +  gtk_widget_show(label2);
   1.123 +  gtk_box_pack_start(GTK_BOX(status), label2, FALSE, FALSE, 0);
   1.124 +
   1.125 +/* image_area */
   1.126 +  image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
   1.127 +  image_area = gtk_image_new_from_pixbuf(image_buffer);
   1.128 +
   1.129 +  gtk_widget_set_name(image_area, "image_area");
   1.130 +  gtk_widget_show(image_area);
   1.131 +  gtk_box_pack_start(GTK_BOX(dialog_vbox1), image_area, TRUE, TRUE, 0);
   1.132 +
   1.133 +
   1.134 +
   1.135 +
   1.136 +
   1.137 +g_signal_connect(G_OBJECT(cropper_window), "show",
   1.138 +                 G_CALLBACK(on_cropper_window_create), NULL);
   1.139 +
   1.140 +g_signal_connect(G_OBJECT(cropper_window), "destroy",
   1.141 +                 G_CALLBACK(on_cropper_window_destroy), NULL);
   1.142 +
   1.143 +g_signal_connect(cropper_window, "key-press-event",
   1.144 +                 G_CALLBACK(on_key_press), NULL);
   1.145 +
   1.146 +
   1.147 +  /* Store pointers to all widgets, for use by lookup_widget(). */
   1.148 +
   1.149 +  GLADE_HOOKUP_OBJECT_NO_REF(cropper_window, cropper_window, "cropper_window");
   1.150 +  GLADE_HOOKUP_OBJECT(cropper_window, dialog_vbox1, "dialog_vbox1");
   1.151 +
   1.152 +  GLADE_HOOKUP_OBJECT(cropper_window, label1, "label1");
   1.153 +  GLADE_HOOKUP_OBJECT(cropper_window, label2, "label2");
   1.154 +  GLADE_HOOKUP_OBJECT(cropper_window, status, "status");
   1.155 +  GLADE_HOOKUP_OBJECT(cropper_window, image_area, "image_area");
   1.156 +  GLADE_HOOKUP_OBJECT(cropper_window,(GtkWidget*) image_buffer, "image_buffer");
   1.157 +
   1.158 +
   1.159 +  return cropper_window;
   1.160 +}
   1.161 +
   1.162 +
   1.163 +
   1.164 +
   1.165 +
   1.166 +
   1.167 +
   1.168 +
   1.169 +
   1.170 +
   1.171 +
   1.172 +
   1.173 +
   1.174 +
   1.175 +
   1.176 +
   1.177 +
   1.178 +
   1.179 +
   1.180 +
   1.181 +
   1.182 +void
   1.183 +update_title(char* zoom)
   1.184 +{
   1.185 +	char title[128];
   1.186 +	snprintf(title, 128, "cropper (%s) %dx%d+%d+%d", zoom, w, h, x, y);
   1.187 +	gtk_window_set_title(GTK_WINDOW(cropper_window), title);
   1.188 +}
   1.189 +
   1.190 +
   1.191 +/* zoom */
   1.192 +void
   1.193 +set_zoom()
   1.194 +{
   1.195 +	static GdkPixbuf* pixbuf_new;
   1.196 +	g_object_unref(pixbuf_new);
   1.197 +
   1.198 +	pixbuf_new = gdk_pixbuf_scale_simple(image_buffer, image_width*zoom, image_height*zoom,
   1.199 +	                                     GDK_INTERP_BILINEAR);
   1.200 +	gtk_image_set_from_pixbuf((GtkImage*) lookup_widget(cropper_window, "image_area"), pixbuf_new);
   1.201 +}
   1.202 +
   1.203 +void
   1.204 +on_zoom_in_button_clicked(GtkObject* object, gpointer user_data)
   1.205 +{
   1.206 +	zoom *= 1 + inc;
   1.207 +	set_zoom();
   1.208 +}
   1.209 +
   1.210 +void
   1.211 +on_zoom_out_button_clicked(GtkObject* object, gpointer user_data)
   1.212 +{
   1.213 +	zoom *= 1 - inc;
   1.214 +	set_zoom();
   1.215 +}
   1.216 +
   1.217 +void
   1.218 +on_zoom_100_button_clicked(GtkObject* object, gpointer user_data)
   1.219 +{
   1.220 +	zoom = 1.0;
   1.221 +	set_zoom();
   1.222 +}
   1.223 +
   1.224 +void
   1.225 +on_zoom_fit_button_clicked(GtkObject* object, gpointer user_data)
   1.226 +{
   1.227 +	int w, h;
   1.228 +	float zw, zh;
   1.229 +	GtkWidget* image_a;
   1.230 +
   1.231 +	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   1.232 +	gdk_drawable_get_size(image_a->window, &w, &h);
   1.233 +
   1.234 +	zw = w*1.0 / image_width;
   1.235 +	zh = h*1.0 / image_height;
   1.236 +
   1.237 +	zoom = (zw < zh) ? zw : zh;
   1.238 +	set_zoom();
   1.239 +}
   1.240 +
   1.241 +
   1.242 +
   1.243 +
   1.244 +
   1.245 +
   1.246 +
   1.247 +void
   1.248 +crop(void)
   1.249 +{
   1.250 +	char crop_call[256];
   1.251 +
   1.252 +	sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"",
   1.253 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   1.254 +		                                 "crop_width_spinbutton")),
   1.255 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   1.256 +		                                 "crop_height_spinbutton")),
   1.257 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   1.258 +		                                 "crop_x_spinbutton")),
   1.259 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   1.260 +		                                 "crop_y_spinbutton")),
   1.261 +		image_filename,
   1.262 +		image_filename
   1.263 +	);
   1.264 +	system(crop_call);
   1.265 +	gtk_main_quit();
   1.266 +}
   1.267 +
   1.268 +
   1.269 +void
   1.270 +on_cropper_window_create(GtkObject* object, gpointer user_data)
   1.271 +{
   1.272 +	image_width = gdk_pixbuf_get_width(image_buffer);
   1.273 +	image_height = gdk_pixbuf_get_height(image_buffer);
   1.274 +
   1.275 +	/*
   1.276 +	image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
   1.277 +	image_area = gtk_image_new_from_pixbuf(image_buffer);
   1.278 +
   1.279 +	int w, h;
   1.280 +	GtkWidget* image_a;
   1.281 +
   1.282 +	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   1.283 +	gdk_drawable_get_size(image_a->window, &w, &h);
   1.284 +
   1.285 +	image_width = w - 200;
   1.286 +	image_height = h - 150;
   1.287 +	set_zoom();
   1.288 +	*/
   1.289 +}
   1.290 +
   1.291 +void
   1.292 +on_cropper_window_destroy(GtkObject* object, gpointer user_data)
   1.293 +{
   1.294 +	gtk_main_quit();
   1.295 +}
   1.296 +
   1.297 +
   1.298 +
   1.299 +
   1.300 +gboolean
   1.301 +on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata)
   1.302 +{
   1.303 +	if (pKey->type != GDK_KEY_PRESS) {
   1.304 +		return FALSE;
   1.305 +	}
   1.306 +	switch (pKey->keyval) {
   1.307 +	case GDK_q:
   1.308 +		gtk_main_quit();
   1.309 +		break;
   1.310 +	case GDK_Return:
   1.311 +		crop();
   1.312 +		break;
   1.313 +	case GDK_plus:
   1.314 +		on_zoom_in_button_clicked(NULL, userdata);
   1.315 +		break;
   1.316 +	case GDK_minus:
   1.317 +		on_zoom_out_button_clicked(NULL, userdata);
   1.318 +		break;
   1.319 +	case GDK_0:
   1.320 +		on_zoom_100_button_clicked(NULL, userdata);
   1.321 +		break;
   1.322 +	case GDK_f:
   1.323 +		on_zoom_fit_button_clicked(NULL, userdata);
   1.324 +		break;
   1.325 +	}
   1.326 +	return TRUE;
   1.327 +}
   1.328 +
   1.329 +
   1.330 +
   1.331 +
   1.332 +
   1.333 +
   1.334  
   1.335  
   1.336  int