cropper

changeset 17:4453456abad1

gathered all code in one file
author meillo@marmaro.de
date Thu, 04 Mar 2010 19:34:12 +0100
parents 88b8856fccf5
children 9d6e8105b145
files Makefile callbacks.c interface.c main.c main.h
diffstat 5 files changed, 330 insertions(+), 560 deletions(-) [+]
line diff
     1.1 --- a/Makefile	Thu Mar 04 19:20:41 2010 +0100
     1.2 +++ b/Makefile	Thu Mar 04 19:34:12 2010 +0100
     1.3 @@ -1,33 +1,16 @@
     1.4  # makefile for cropper
     1.5  
     1.6 -NAME    = cropper
     1.7 -
     1.8 -SRC = interface.c callbacks.c main.c
     1.9 -DEP = main.h
    1.10 -OBJ = ${SRC:.c=.o}
    1.11 -
    1.12  CC = gcc
    1.13  CFLAGS = `pkg-config --cflags gtk+-2.0`
    1.14  LDFLAGS = `pkg-config --libs --cflags gtk+-2.0`
    1.15  
    1.16  
    1.17 -
    1.18 -all: ${NAME}
    1.19 -
    1.20 -${NAME}: ${OBJ} ${DEP}
    1.21 -	${CC} -o ${NAME} ${OBJ} ${LDFLAGS}
    1.22 -
    1.23 -.c.o:
    1.24 -	${CC} ${CFLAGS} -c $<
    1.25 -
    1.26 -
    1.27 -interface.o: main.h
    1.28 -callbacks.o: main.h
    1.29 -main.o: main.h
    1.30 +cropper: main.o
    1.31 +	${CC} -o cropper main.o ${LDFLAGS}
    1.32  
    1.33  
    1.34  clean:
    1.35  	rm -f *.o
    1.36  
    1.37  realclean: clean
    1.38 -	rm -f ${NAME}
    1.39 +	rm -f cropper
     2.1 --- a/callbacks.c	Thu Mar 04 19:20:41 2010 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,347 +0,0 @@
     2.4 -#include <gtk/gtk.h>
     2.5 -#include <gdk-pixbuf/gdk-pixbuf.h>
     2.6 -#include <gdk/gdkkeysyms.h>
     2.7 -#include "main.h"
     2.8 -
     2.9 -int image_width;
    2.10 -int image_height;
    2.11 -float inc = 0.3;
    2.12 -float zoom = 1.0;
    2.13 -
    2.14 -
    2.15 -/*
    2.16 -void
    2.17 -update_spin_button_ratio(GtkWidget* spinbutton)
    2.18 -{
    2.19 -	g_print("ratio change: %f\n", ratio);
    2.20 -	on_ratio_w_spinbutton_value_changed(spinbutton, NULL);
    2.21 -}
    2.22 -
    2.23 -
    2.24 -void
    2.25 -on_ratio_none_activate(GtkMenuItem* menuitem, gpointer user_data)
    2.26 -{
    2.27 -	ratio = 1;
    2.28 -	update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
    2.29 -}
    2.30 -
    2.31 -
    2.32 -void
    2.33 -on_ratio_square_activate(GtkMenuItem* menuitem, gpointer user_data)
    2.34 -{
    2.35 -	ratio = 1;
    2.36 -	update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
    2.37 -}
    2.38 -
    2.39 -
    2.40 -void
    2.41 -on_ratio_4x3_activate(GtkMenuItem* menuitem, gpointer user_data)
    2.42 -{
    2.43 -	ratio = 4.0/3.0;
    2.44 -	update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
    2.45 -}
    2.46 -
    2.47 -
    2.48 -void
    2.49 -on_ratio_3x2_activate(GtkMenuItem* menuitem, gpointer user_data)
    2.50 -{
    2.51 -	ratio = 1.5;
    2.52 -	update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
    2.53 -}
    2.54 -
    2.55 -
    2.56 -void
    2.57 -on_ratio_custom_activate(GtkMenuItem* menuitem, gpointer user_data)
    2.58 -{
    2.59 -	ratio = gtk_spin_button_get_value(lookup_widget(cropper_window, "ratio_w_spinbutton"))
    2.60 -	        / gtk_spin_button_get_value(lookup_widget(cropper_window, "ratio_h_spinbutton"));
    2.61 -	update_spin_button_ratio(lookup_widget(cropper_window, "ratio_w_spinbutton"));
    2.62 -}
    2.63 -*/
    2.64 -
    2.65 -
    2.66 -
    2.67 -
    2.68 -
    2.69 -
    2.70 -
    2.71 -
    2.72 -
    2.73 -/*
    2.74 -static void
    2.75 -ratio_optionmenu_changed_cb(GtkOptionMenu* optionmenu, gpointer unser_data)
    2.76 -{
    2.77 -	int      idx = gtk_option_menu_get_history(optionmenu);
    2.78 -	int      w = 1, h = 1;
    2.79 -	gboolean use_ratio = TRUE;
    2.80 -
    2.81 -	switch (idx) {
    2.82 -		case GTH_CROP_RATIO_NONE:
    2.83 -			use_ratio = FALSE;
    2.84 -			break;
    2.85 -		case GTH_CROP_RATIO_SQUARE:
    2.86 -			w = h = 1;
    2.87 -			break;
    2.88 -		case GTH_CROP_RATIO_IMAGE:
    2.89 -			w = lookup_widget(cropper_window, "image_width");
    2.90 -			h = lookup_widget(cropper_window, "image_height");
    2.91 -			break;
    2.92 -		case GTH_CROP_RATIO_DISPLAY:
    2.93 -			w = lookup_widget(cropper_window, "display_width");
    2.94 -			h = lookup_widget(cropper_window, "display_height");
    2.95 -			break;
    2.96 -		case GTH_CROP_RATIO_4_3:
    2.97 -			w = 4;
    2.98 -			h = 3;
    2.99 -			break;
   2.100 -		case GTH_CROP_RATIO_4_6:
   2.101 -			w = 4;
   2.102 -			h = 6;
   2.103 -			break;
   2.104 -		case GTH_CROP_RATIO_CUSTOM:
   2.105 -		default:
   2.106 -			w = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
   2.107 -			        lookup_widget(cropper_window, "ratio_w_spinbutton")));
   2.108 -			h = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(
   2.109 -			        lookup_widget(cropper_window, "ratio_h_spinbutton")));
   2.110 -			break;
   2.111 -	}
   2.112 -
   2.113 -	gtk_widget_set_sensitive(lookup_widget(cropper_window, "custom_ratio_box"),
   2.114 -	                         idx == GTH_CROP_RATIO_CUSTOM);
   2.115 -	set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_w_spinbutton"), w);
   2.116 -	set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_h_spinbutton"), h);
   2.117 -
   2.118 -	gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(lookup_widget(cropper_window, "crop_image")),
   2.119 -	                             use_ratio, (double) w / h);
   2.120 -}
   2.121 -
   2.122 -
   2.123 -static void
   2.124 -ratio_value_changed_cb(GtkSpinButton* spin, gpointer unser_data)
   2.125 -{
   2.126 -	int w, h;
   2.127 -
   2.128 -	w = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_w_spinbutton"));
   2.129 -	h = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_h_spinbutton"));
   2.130 -			
   2.131 -	gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(
   2.132 -	         lookup_widget(cropper_window, "crop_image")), TRUE, (double) w / h);
   2.133 -}
   2.134 -
   2.135 -
   2.136 -static void
   2.137 -ratio_swap_button_cb(GtkButton* button, gpointer unser_data)
   2.138 -{
   2.139 -	int w, h;
   2.140 -
   2.141 -	w = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_w_spinbutton"));
   2.142 -	h = gtk_spin_button_get_value_as_int(lookup_widget(cropper_window, "ratio_h_spinbutton"));
   2.143 -
   2.144 -	set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_w_spinbutton"), h);
   2.145 -	set_spin_value(cropper_window, lookup_widget(cropper_window, "ratio_h_spinbutton"), w);
   2.146 -
   2.147 -	gth_image_selector_set_ratio(GTH_IMAGE_SELECTOR(lookup_widget(cropper_window,
   2.148 -	                             "crop_image")), TRUE, (double) h / w);
   2.149 -}
   2.150 -
   2.151 -
   2.152 -
   2.153 -
   2.154 -
   2.155 -
   2.156 -
   2.157 -
   2.158 -void
   2.159 -on_ratio_w_spinbutton_value_changed(GtkSpinButton* spinbutton, gpointer user_data)
   2.160 -{
   2.161 -	g_print("spinbutton W - ratio: %f\n", ratio);
   2.162 -	gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_h_spinbutton"),
   2.163 -	                          gtk_spin_button_get_value(spinbutton) / ratio);
   2.164 -}
   2.165 -
   2.166 -
   2.167 -void
   2.168 -on_ratio_w_spinbutton_change_value(GtkSpinButton* spinbutton, GtkScrollType scroll, gpointer user_data)
   2.169 -{
   2.170 -}
   2.171 -
   2.172 -
   2.173 -void
   2.174 -on_ratio_h_spinbutton_change_value(GtkSpinButton* spinbutton, GtkScrollType scroll, gpointer user_data)
   2.175 -{
   2.176 -}
   2.177 -
   2.178 -
   2.179 -void
   2.180 -on_ratio_h_spinbutton_value_changed(GtkSpinButton* spinbutton, gpointer user_data)
   2.181 -{
   2.182 -	g_print("spinbutton H - ratio: %f\n", ratio);
   2.183 -	gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_w_spinbutton"),
   2.184 -	                          gtk_spin_button_get_value(spinbutton) * ratio);
   2.185 -
   2.186 -}
   2.187 -
   2.188 -
   2.189 -void
   2.190 -on_ratio_swap_button_clicked(GtkButton* button, gpointer user_data)
   2.191 -{
   2.192 -	
   2.193 -	ratio = 1.0 / ratio;
   2.194 -	gtk_spin_button_set_value(lookup_widget(cropper_window, "ratio_w_spinbutton"),
   2.195 -	                          gtk_spin_button_get_value(lookup_widget(cropper_window,
   2.196 -	                          "ratio_h_spinbutton")));
   2.197 -	gtk_spin_button_update(lookup_widget(cropper_window, "ratio_w_spinbutton"));
   2.198 -
   2.199 -}
   2.200 -*/
   2.201 -
   2.202 -
   2.203 -void
   2.204 -update_title(char* zoom)
   2.205 -{
   2.206 -	char title[128];
   2.207 -	snprintf(title, 128, "cropper (%s) %dx%d+%d+%d", zoom, w, h, x, y);
   2.208 -	gtk_window_set_title(GTK_WINDOW(cropper_window), title);
   2.209 -}
   2.210 -
   2.211 -
   2.212 -/* zoom */
   2.213 -void
   2.214 -set_zoom()
   2.215 -{
   2.216 -	static GdkPixbuf* pixbuf_new;
   2.217 -	g_object_unref(pixbuf_new);
   2.218 -
   2.219 -	pixbuf_new = gdk_pixbuf_scale_simple(image_buffer, image_width*zoom, image_height*zoom,
   2.220 -	                                     GDK_INTERP_BILINEAR);
   2.221 -	gtk_image_set_from_pixbuf((GtkImage*) lookup_widget(cropper_window, "image_area"), pixbuf_new);
   2.222 -}
   2.223 -
   2.224 -void
   2.225 -on_zoom_in_button_clicked(GtkObject* object, gpointer user_data)
   2.226 -{
   2.227 -	zoom *= 1 + inc;
   2.228 -	set_zoom();
   2.229 -}
   2.230 -
   2.231 -void
   2.232 -on_zoom_out_button_clicked(GtkObject* object, gpointer user_data)
   2.233 -{
   2.234 -	zoom *= 1 - inc;
   2.235 -	set_zoom();
   2.236 -}
   2.237 -
   2.238 -void
   2.239 -on_zoom_100_button_clicked(GtkObject* object, gpointer user_data)
   2.240 -{
   2.241 -	zoom = 1.0;
   2.242 -	set_zoom();
   2.243 -}
   2.244 -
   2.245 -void
   2.246 -on_zoom_fit_button_clicked(GtkObject* object, gpointer user_data)
   2.247 -{
   2.248 -	int w, h;
   2.249 -	float zw, zh;
   2.250 -	GtkWidget* image_a;
   2.251 -
   2.252 -	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   2.253 -	gdk_drawable_get_size(image_a->window, &w, &h);
   2.254 -
   2.255 -	zw = w*1.0 / image_width;
   2.256 -	zh = h*1.0 / image_height;
   2.257 -
   2.258 -	zoom = (zw < zh) ? zw : zh;
   2.259 -	set_zoom();
   2.260 -}
   2.261 -
   2.262 -
   2.263 -
   2.264 -
   2.265 -
   2.266 -
   2.267 -
   2.268 -void
   2.269 -crop(void)
   2.270 -{
   2.271 -	char crop_call[256];
   2.272 -
   2.273 -	sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"",
   2.274 -		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   2.275 -		                                 "crop_width_spinbutton")),
   2.276 -		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   2.277 -		                                 "crop_height_spinbutton")),
   2.278 -		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   2.279 -		                                 "crop_x_spinbutton")),
   2.280 -		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   2.281 -		                                 "crop_y_spinbutton")),
   2.282 -		image_filename,
   2.283 -		image_filename
   2.284 -	);
   2.285 -	system(crop_call);
   2.286 -	gtk_main_quit();
   2.287 -}
   2.288 -
   2.289 -
   2.290 -void
   2.291 -on_cropper_window_create(GtkObject* object, gpointer user_data)
   2.292 -{
   2.293 -	image_width = gdk_pixbuf_get_width(image_buffer);
   2.294 -	image_height = gdk_pixbuf_get_height(image_buffer);
   2.295 -
   2.296 -	/*
   2.297 -	image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
   2.298 -	image_area = gtk_image_new_from_pixbuf(image_buffer);
   2.299 -
   2.300 -	int w, h;
   2.301 -	GtkWidget* image_a;
   2.302 -
   2.303 -	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   2.304 -	gdk_drawable_get_size(image_a->window, &w, &h);
   2.305 -
   2.306 -	image_width = w - 200;
   2.307 -	image_height = h - 150;
   2.308 -	set_zoom();
   2.309 -	*/
   2.310 -}
   2.311 -
   2.312 -void
   2.313 -on_cropper_window_destroy(GtkObject* object, gpointer user_data)
   2.314 -{
   2.315 -	gtk_main_quit();
   2.316 -}
   2.317 -
   2.318 -
   2.319 -
   2.320 -
   2.321 -gboolean
   2.322 -on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata)
   2.323 -{
   2.324 -	if (pKey->type != GDK_KEY_PRESS) {
   2.325 -		return FALSE;
   2.326 -	}
   2.327 -	switch (pKey->keyval) {
   2.328 -	case GDK_q:
   2.329 -		gtk_main_quit();
   2.330 -		break;
   2.331 -	case GDK_Return:
   2.332 -		crop();
   2.333 -		break;
   2.334 -	case GDK_plus:
   2.335 -		on_zoom_in_button_clicked(NULL, userdata);
   2.336 -		break;
   2.337 -	case GDK_minus:
   2.338 -		on_zoom_out_button_clicked(NULL, userdata);
   2.339 -		break;
   2.340 -	case GDK_0:
   2.341 -		on_zoom_100_button_clicked(NULL, userdata);
   2.342 -		break;
   2.343 -	case GDK_f:
   2.344 -		on_zoom_fit_button_clicked(NULL, userdata);
   2.345 -		break;
   2.346 -	}
   2.347 -	return TRUE;
   2.348 -}
   2.349 -
   2.350 -
     3.1 --- a/interface.c	Thu Mar 04 19:20:41 2010 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,173 +0,0 @@
     3.4 -#include <sys/types.h>
     3.5 -#include <sys/stat.h>
     3.6 -#include <unistd.h>
     3.7 -#include <string.h>
     3.8 -#include <stdio.h>
     3.9 -
    3.10 -#include <gdk/gdkkeysyms.h>
    3.11 -#include <gtk/gtk.h>
    3.12 -#include <gdk-pixbuf/gdk-pixbuf.h>
    3.13 -
    3.14 -#include "main.h"
    3.15 -
    3.16 -#define GLADE_HOOKUP_OBJECT(component,widget,name) \
    3.17 -  g_object_set_data_full(G_OBJECT(component), name, \
    3.18 -    gtk_widget_ref(widget),(GDestroyNotify) gtk_widget_unref)
    3.19 -
    3.20 -#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
    3.21 -  g_object_set_data(G_OBJECT(component), name, widget)
    3.22 -
    3.23 -
    3.24 -/*
    3.25 - * This function returns a widget in a component created by Glade.
    3.26 - * Call it with the toplevel widget in the component (i.e. a window/dialog),
    3.27 - * or alternatively any widget in the component, and the name of the widget
    3.28 - * you want returned.
    3.29 - */
    3.30 -GtkWidget*
    3.31 -lookup_widget(GtkWidget* widget, const gchar* widget_name)
    3.32 -{
    3.33 -	GtkWidget* parent;
    3.34 -	GtkWidget* found_widget;
    3.35 -
    3.36 -	for (;;) {
    3.37 -		if (GTK_IS_MENU(widget)) {
    3.38 -			parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
    3.39 -		} else {
    3.40 -			parent = widget->parent;
    3.41 -		}
    3.42 -		if (!parent) {
    3.43 -			parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
    3.44 -		}
    3.45 -		if (parent == NULL) {
    3.46 -			break;
    3.47 -		}
    3.48 -		widget = parent;
    3.49 -	}
    3.50 -
    3.51 -	found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
    3.52 -	if (!found_widget) {
    3.53 -		g_warning("Widget not found: %s", widget_name);
    3.54 -	}
    3.55 -	return found_widget;
    3.56 -}
    3.57 -
    3.58 -
    3.59 -
    3.60 -GtkWidget*
    3.61 -create_cropper_window(void)
    3.62 -{
    3.63 -  GtkWidget *cropper_window;
    3.64 -  GtkWidget *dialog_vbox1;
    3.65 -  GtkWidget *status;
    3.66 -  GtkWidget *label1;
    3.67 -  GtkWidget *label2;
    3.68 -
    3.69 -
    3.70 -  cropper_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    3.71 -  gtk_widget_set_name(cropper_window, "cropper_window");
    3.72 -  gtk_container_set_border_width(GTK_CONTAINER(cropper_window), 6);
    3.73 -  gtk_window_set_title(GTK_WINDOW(cropper_window), "cropper-0.2");
    3.74 -
    3.75 -  dialog_vbox1 = gtk_vbox_new(FALSE, 6);
    3.76 -  gtk_widget_set_name(dialog_vbox1, "dialog_vbox1");
    3.77 -  gtk_widget_show(dialog_vbox1);
    3.78 -  gtk_container_add(GTK_CONTAINER(cropper_window), dialog_vbox1);
    3.79 -
    3.80 -
    3.81 -  status = gtk_hbox_new(FALSE, 0);
    3.82 -  gtk_widget_set_name(status, "status");
    3.83 -  gtk_widget_show(status);
    3.84 -  gtk_box_pack_end(GTK_BOX(dialog_vbox1), status, FALSE, FALSE, 0);
    3.85 -
    3.86 -  label1 = gtk_label_new("zoom:");
    3.87 -  gtk_widget_set_name(label1, "label1");
    3.88 -  gtk_widget_show(label1);
    3.89 -  gtk_box_pack_start(GTK_BOX(status), label1, FALSE, FALSE, 0);
    3.90 -
    3.91 -  label2 = gtk_label_new("1.0");
    3.92 -  gtk_widget_set_name(label2, "label2");
    3.93 -  gtk_widget_show(label2);
    3.94 -  gtk_box_pack_start(GTK_BOX(status), label2, FALSE, FALSE, 0);
    3.95 -
    3.96 -/* image_area */
    3.97 -  image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
    3.98 -  image_area = gtk_image_new_from_pixbuf(image_buffer);
    3.99 -
   3.100 -  gtk_widget_set_name(image_area, "image_area");
   3.101 -  gtk_widget_show(image_area);
   3.102 -  gtk_box_pack_start(GTK_BOX(dialog_vbox1), image_area, TRUE, TRUE, 0);
   3.103 -
   3.104 -
   3.105 -
   3.106 -
   3.107 -
   3.108 -g_signal_connect(G_OBJECT(cropper_window), "show",
   3.109 -                 G_CALLBACK(on_cropper_window_create), NULL);
   3.110 -
   3.111 -g_signal_connect(G_OBJECT(cropper_window), "destroy",
   3.112 -                 G_CALLBACK(on_cropper_window_destroy), NULL);
   3.113 -
   3.114 -g_signal_connect(cropper_window, "key-press-event",
   3.115 -                 G_CALLBACK(on_key_press), NULL);
   3.116 -
   3.117 -
   3.118 -  /* Store pointers to all widgets, for use by lookup_widget(). */
   3.119 -
   3.120 -  GLADE_HOOKUP_OBJECT_NO_REF(cropper_window, cropper_window, "cropper_window");
   3.121 -  GLADE_HOOKUP_OBJECT(cropper_window, dialog_vbox1, "dialog_vbox1");
   3.122 -
   3.123 -  GLADE_HOOKUP_OBJECT(cropper_window, label1, "label1");
   3.124 -  GLADE_HOOKUP_OBJECT(cropper_window, label2, "label2");
   3.125 -  GLADE_HOOKUP_OBJECT(cropper_window, status, "status");
   3.126 -  GLADE_HOOKUP_OBJECT(cropper_window, image_area, "image_area");
   3.127 -  GLADE_HOOKUP_OBJECT(cropper_window,(GtkWidget*) image_buffer, "image_buffer");
   3.128 -
   3.129 -
   3.130 -  return cropper_window;
   3.131 -}
   3.132 -
   3.133 -
   3.134 -
   3.135 -
   3.136 -
   3.137 -
   3.138 -
   3.139 -
   3.140 -
   3.141 -/*
   3.142 -  gtk_container_set_border_width(GTK_CONTAINER(hbox1), 5);
   3.143 -
   3.144 -  label1 = gtk_label_new("<b>Selection</b>");
   3.145 -  gtk_label_set_use_markup(GTK_LABEL(label1), TRUE);
   3.146 -  gtk_misc_set_alignment(GTK_MISC(label1), 0, 0.5);
   3.147 -
   3.148 -  gtk_table_set_row_spacings(GTK_TABLE(table2), 6);
   3.149 -  gtk_table_set_col_spacings(GTK_TABLE(table2), 6);
   3.150 -
   3.151 -  gtk_table_attach(GTK_TABLE(table2), label3, 0, 1, 0, 1,(GtkAttachOptions)(GTK_FILL),
   3.152 -                  (GtkAttachOptions)(0), 0, 0);
   3.153 -  gtk_misc_set_alignment(GTK_MISC(label3), 0, 0.5);
   3.154 -
   3.155 -  frame1 = gtk_frame_new(NULL);
   3.156 -  gtk_widget_set_name(frame1, "frame1");
   3.157 -  gtk_widget_show(frame1);
   3.158 -  gtk_box_pack_start(GTK_BOX(vbox5), frame1, TRUE, TRUE, 0);
   3.159 -  gtk_frame_set_shadow_type(GTK_FRAME(frame1), GTK_SHADOW_NONE);
   3.160 -
   3.161 -  vbox7 = gtk_vbox_new(FALSE, 6);
   3.162 -  gtk_widget_set_name(vbox7, "vbox7");
   3.163 -  gtk_widget_show(vbox7);
   3.164 -  gtk_container_add(GTK_CONTAINER(frame1), vbox7);
   3.165 -
   3.166 -g_signal_connect(G_OBJECT(lookup_widget(cropper_window, "crop_x_spinbutton")), "value_changed",
   3.167 -                 G_CALLBACK(selection_x_value_changed_cb), data);
   3.168 -g_signal_connect(G_OBJECT(lookup_widget(cropper_window, "crop_image")), "selection_changed",
   3.169 -                 G_CALLBACK(selection_changed_cb), data);
   3.170 -g_signal_connect(G_OBJECT(lookup_widget(cropper_window, "ratio_swap_button")), "clicked",
   3.171 -                 G_CALLBACK(ratio_swap_button_cb), NULL);
   3.172 -g_signal_connect(G_OBJECT(lookup_widget(cropper_window, "ratio_optionmenu")), "changed",
   3.173 -                 G_CALLBACK(ratio_optionmenu_changed_cb), NULL);
   3.174 -  g_signal_connect(G_OBJECT(ratio_none), "activate",
   3.175 -                   G_CALLBACK(on_ratio_none_activate), NULL);
   3.176 -*/
     4.1 --- a/main.c	Thu Mar 04 19:20:41 2010 +0100
     4.2 +++ b/main.c	Thu Mar 04 19:34:12 2010 +0100
     4.3 @@ -1,6 +1,332 @@
     4.4 +#include <sys/types.h>
     4.5 +#include <sys/stat.h>
     4.6 +#include <unistd.h>
     4.7 +#include <string.h>
     4.8 +#include <stdio.h>
     4.9 +
    4.10  #include <gtk/gtk.h>
    4.11 +#include <gdk/gdkkeysyms.h>
    4.12 +#include <gdk-pixbuf/gdk-pixbuf.h>
    4.13  
    4.14 -#include "main.h"
    4.15 +
    4.16 +
    4.17 +
    4.18 +GtkWidget* cropper_window;
    4.19 +char* image_filename;
    4.20 +
    4.21 +/* from interface.h */
    4.22 +GtkWidget* create_cropper_window(void);
    4.23 +GtkWidget* image_area;
    4.24 +GdkPixbuf* image_buffer;
    4.25 +
    4.26 +int w, h, x, y;
    4.27 +
    4.28 +int image_width;
    4.29 +int image_height;
    4.30 +float inc = 0.3;
    4.31 +float zoom = 1.0;
    4.32 +
    4.33 +/* from callbacks.h */
    4.34 +double ratio;
    4.35 +
    4.36 +void crop(void);
    4.37 +gboolean on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata);
    4.38 +void on_cropper_window_create(GtkObject* object, gpointer user_data);
    4.39 +void on_cropper_window_destroy(GtkObject* object, gpointer user_data);
    4.40 +
    4.41 +
    4.42 +
    4.43 +
    4.44 +
    4.45 +#define GLADE_HOOKUP_OBJECT(component,widget,name) \
    4.46 +  g_object_set_data_full(G_OBJECT(component), name, \
    4.47 +    gtk_widget_ref(widget),(GDestroyNotify) gtk_widget_unref)
    4.48 +
    4.49 +#define GLADE_HOOKUP_OBJECT_NO_REF(component,widget,name) \
    4.50 +  g_object_set_data(G_OBJECT(component), name, widget)
    4.51 +
    4.52 +
    4.53 +/*
    4.54 + * This function returns a widget in a component created by Glade.
    4.55 + * Call it with the toplevel widget in the component (i.e. a window/dialog),
    4.56 + * or alternatively any widget in the component, and the name of the widget
    4.57 + * you want returned.
    4.58 + */
    4.59 +GtkWidget*
    4.60 +lookup_widget(GtkWidget* widget, const gchar* widget_name)
    4.61 +{
    4.62 +	GtkWidget* parent;
    4.63 +	GtkWidget* found_widget;
    4.64 +
    4.65 +	for (;;) {
    4.66 +		if (GTK_IS_MENU(widget)) {
    4.67 +			parent = gtk_menu_get_attach_widget(GTK_MENU(widget));
    4.68 +		} else {
    4.69 +			parent = widget->parent;
    4.70 +		}
    4.71 +		if (!parent) {
    4.72 +			parent = (GtkWidget*) g_object_get_data(G_OBJECT(widget), "GladeParentKey");
    4.73 +		}
    4.74 +		if (parent == NULL) {
    4.75 +			break;
    4.76 +		}
    4.77 +		widget = parent;
    4.78 +	}
    4.79 +
    4.80 +	found_widget = (GtkWidget*) g_object_get_data(G_OBJECT(widget), widget_name);
    4.81 +	if (!found_widget) {
    4.82 +		g_warning("Widget not found: %s", widget_name);
    4.83 +	}
    4.84 +	return found_widget;
    4.85 +}
    4.86 +
    4.87 +
    4.88 +
    4.89 +GtkWidget*
    4.90 +create_cropper_window(void)
    4.91 +{
    4.92 +  GtkWidget *cropper_window;
    4.93 +  GtkWidget *dialog_vbox1;
    4.94 +  GtkWidget *status;
    4.95 +  GtkWidget *label1;
    4.96 +  GtkWidget *label2;
    4.97 +
    4.98 +
    4.99 +  cropper_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
   4.100 +  gtk_widget_set_name(cropper_window, "cropper_window");
   4.101 +  gtk_container_set_border_width(GTK_CONTAINER(cropper_window), 6);
   4.102 +  gtk_window_set_title(GTK_WINDOW(cropper_window), "cropper-0.2");
   4.103 +
   4.104 +  dialog_vbox1 = gtk_vbox_new(FALSE, 6);
   4.105 +  gtk_widget_set_name(dialog_vbox1, "dialog_vbox1");
   4.106 +  gtk_widget_show(dialog_vbox1);
   4.107 +  gtk_container_add(GTK_CONTAINER(cropper_window), dialog_vbox1);
   4.108 +
   4.109 +
   4.110 +  status = gtk_hbox_new(FALSE, 0);
   4.111 +  gtk_widget_set_name(status, "status");
   4.112 +  gtk_widget_show(status);
   4.113 +  gtk_box_pack_end(GTK_BOX(dialog_vbox1), status, FALSE, FALSE, 0);
   4.114 +
   4.115 +  label1 = gtk_label_new("zoom:");
   4.116 +  gtk_widget_set_name(label1, "label1");
   4.117 +  gtk_widget_show(label1);
   4.118 +  gtk_box_pack_start(GTK_BOX(status), label1, FALSE, FALSE, 0);
   4.119 +
   4.120 +  label2 = gtk_label_new("1.0");
   4.121 +  gtk_widget_set_name(label2, "label2");
   4.122 +  gtk_widget_show(label2);
   4.123 +  gtk_box_pack_start(GTK_BOX(status), label2, FALSE, FALSE, 0);
   4.124 +
   4.125 +/* image_area */
   4.126 +  image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
   4.127 +  image_area = gtk_image_new_from_pixbuf(image_buffer);
   4.128 +
   4.129 +  gtk_widget_set_name(image_area, "image_area");
   4.130 +  gtk_widget_show(image_area);
   4.131 +  gtk_box_pack_start(GTK_BOX(dialog_vbox1), image_area, TRUE, TRUE, 0);
   4.132 +
   4.133 +
   4.134 +
   4.135 +
   4.136 +
   4.137 +g_signal_connect(G_OBJECT(cropper_window), "show",
   4.138 +                 G_CALLBACK(on_cropper_window_create), NULL);
   4.139 +
   4.140 +g_signal_connect(G_OBJECT(cropper_window), "destroy",
   4.141 +                 G_CALLBACK(on_cropper_window_destroy), NULL);
   4.142 +
   4.143 +g_signal_connect(cropper_window, "key-press-event",
   4.144 +                 G_CALLBACK(on_key_press), NULL);
   4.145 +
   4.146 +
   4.147 +  /* Store pointers to all widgets, for use by lookup_widget(). */
   4.148 +
   4.149 +  GLADE_HOOKUP_OBJECT_NO_REF(cropper_window, cropper_window, "cropper_window");
   4.150 +  GLADE_HOOKUP_OBJECT(cropper_window, dialog_vbox1, "dialog_vbox1");
   4.151 +
   4.152 +  GLADE_HOOKUP_OBJECT(cropper_window, label1, "label1");
   4.153 +  GLADE_HOOKUP_OBJECT(cropper_window, label2, "label2");
   4.154 +  GLADE_HOOKUP_OBJECT(cropper_window, status, "status");
   4.155 +  GLADE_HOOKUP_OBJECT(cropper_window, image_area, "image_area");
   4.156 +  GLADE_HOOKUP_OBJECT(cropper_window,(GtkWidget*) image_buffer, "image_buffer");
   4.157 +
   4.158 +
   4.159 +  return cropper_window;
   4.160 +}
   4.161 +
   4.162 +
   4.163 +
   4.164 +
   4.165 +
   4.166 +
   4.167 +
   4.168 +
   4.169 +
   4.170 +
   4.171 +
   4.172 +
   4.173 +
   4.174 +
   4.175 +
   4.176 +
   4.177 +
   4.178 +
   4.179 +
   4.180 +
   4.181 +
   4.182 +void
   4.183 +update_title(char* zoom)
   4.184 +{
   4.185 +	char title[128];
   4.186 +	snprintf(title, 128, "cropper (%s) %dx%d+%d+%d", zoom, w, h, x, y);
   4.187 +	gtk_window_set_title(GTK_WINDOW(cropper_window), title);
   4.188 +}
   4.189 +
   4.190 +
   4.191 +/* zoom */
   4.192 +void
   4.193 +set_zoom()
   4.194 +{
   4.195 +	static GdkPixbuf* pixbuf_new;
   4.196 +	g_object_unref(pixbuf_new);
   4.197 +
   4.198 +	pixbuf_new = gdk_pixbuf_scale_simple(image_buffer, image_width*zoom, image_height*zoom,
   4.199 +	                                     GDK_INTERP_BILINEAR);
   4.200 +	gtk_image_set_from_pixbuf((GtkImage*) lookup_widget(cropper_window, "image_area"), pixbuf_new);
   4.201 +}
   4.202 +
   4.203 +void
   4.204 +on_zoom_in_button_clicked(GtkObject* object, gpointer user_data)
   4.205 +{
   4.206 +	zoom *= 1 + inc;
   4.207 +	set_zoom();
   4.208 +}
   4.209 +
   4.210 +void
   4.211 +on_zoom_out_button_clicked(GtkObject* object, gpointer user_data)
   4.212 +{
   4.213 +	zoom *= 1 - inc;
   4.214 +	set_zoom();
   4.215 +}
   4.216 +
   4.217 +void
   4.218 +on_zoom_100_button_clicked(GtkObject* object, gpointer user_data)
   4.219 +{
   4.220 +	zoom = 1.0;
   4.221 +	set_zoom();
   4.222 +}
   4.223 +
   4.224 +void
   4.225 +on_zoom_fit_button_clicked(GtkObject* object, gpointer user_data)
   4.226 +{
   4.227 +	int w, h;
   4.228 +	float zw, zh;
   4.229 +	GtkWidget* image_a;
   4.230 +
   4.231 +	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   4.232 +	gdk_drawable_get_size(image_a->window, &w, &h);
   4.233 +
   4.234 +	zw = w*1.0 / image_width;
   4.235 +	zh = h*1.0 / image_height;
   4.236 +
   4.237 +	zoom = (zw < zh) ? zw : zh;
   4.238 +	set_zoom();
   4.239 +}
   4.240 +
   4.241 +
   4.242 +
   4.243 +
   4.244 +
   4.245 +
   4.246 +
   4.247 +void
   4.248 +crop(void)
   4.249 +{
   4.250 +	char crop_call[256];
   4.251 +
   4.252 +	sprintf(crop_call, "echo \"convert -crop %ix%i+%i+%i %s cropped_%s\"",
   4.253 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   4.254 +		                                 "crop_width_spinbutton")),
   4.255 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   4.256 +		                                 "crop_height_spinbutton")),
   4.257 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   4.258 +		                                 "crop_x_spinbutton")),
   4.259 +		gtk_spin_button_get_value_as_int((GtkSpinButton*) lookup_widget(cropper_window,
   4.260 +		                                 "crop_y_spinbutton")),
   4.261 +		image_filename,
   4.262 +		image_filename
   4.263 +	);
   4.264 +	system(crop_call);
   4.265 +	gtk_main_quit();
   4.266 +}
   4.267 +
   4.268 +
   4.269 +void
   4.270 +on_cropper_window_create(GtkObject* object, gpointer user_data)
   4.271 +{
   4.272 +	image_width = gdk_pixbuf_get_width(image_buffer);
   4.273 +	image_height = gdk_pixbuf_get_height(image_buffer);
   4.274 +
   4.275 +	/*
   4.276 +	image_buffer = gdk_pixbuf_new_from_file(image_filename, NULL);
   4.277 +	image_area = gtk_image_new_from_pixbuf(image_buffer);
   4.278 +
   4.279 +	int w, h;
   4.280 +	GtkWidget* image_a;
   4.281 +
   4.282 +	image_a = (GtkWidget*) lookup_widget(cropper_window, "image_area");
   4.283 +	gdk_drawable_get_size(image_a->window, &w, &h);
   4.284 +
   4.285 +	image_width = w - 200;
   4.286 +	image_height = h - 150;
   4.287 +	set_zoom();
   4.288 +	*/
   4.289 +}
   4.290 +
   4.291 +void
   4.292 +on_cropper_window_destroy(GtkObject* object, gpointer user_data)
   4.293 +{
   4.294 +	gtk_main_quit();
   4.295 +}
   4.296 +
   4.297 +
   4.298 +
   4.299 +
   4.300 +gboolean
   4.301 +on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata)
   4.302 +{
   4.303 +	if (pKey->type != GDK_KEY_PRESS) {
   4.304 +		return FALSE;
   4.305 +	}
   4.306 +	switch (pKey->keyval) {
   4.307 +	case GDK_q:
   4.308 +		gtk_main_quit();
   4.309 +		break;
   4.310 +	case GDK_Return:
   4.311 +		crop();
   4.312 +		break;
   4.313 +	case GDK_plus:
   4.314 +		on_zoom_in_button_clicked(NULL, userdata);
   4.315 +		break;
   4.316 +	case GDK_minus:
   4.317 +		on_zoom_out_button_clicked(NULL, userdata);
   4.318 +		break;
   4.319 +	case GDK_0:
   4.320 +		on_zoom_100_button_clicked(NULL, userdata);
   4.321 +		break;
   4.322 +	case GDK_f:
   4.323 +		on_zoom_fit_button_clicked(NULL, userdata);
   4.324 +		break;
   4.325 +	}
   4.326 +	return TRUE;
   4.327 +}
   4.328 +
   4.329 +
   4.330 +
   4.331 +
   4.332 +
   4.333 +
   4.334  
   4.335  
   4.336  int
     5.1 --- a/main.h	Thu Mar 04 19:20:41 2010 +0100
     5.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.3 @@ -1,19 +0,0 @@
     5.4 -#include <gtk/gtk.h>
     5.5 -
     5.6 -GtkWidget* cropper_window;
     5.7 -char* image_filename;
     5.8 -
     5.9 -/* from interface.h */
    5.10 -GtkWidget* create_cropper_window(void);
    5.11 -GtkWidget* image_area;
    5.12 -GdkPixbuf* image_buffer;
    5.13 -
    5.14 -int w, h, x, y;
    5.15 -
    5.16 -/* from callbacks.h */
    5.17 -double ratio;
    5.18 -
    5.19 -void crop(void);
    5.20 -gboolean on_key_press(GtkWidget* window, GdkEventKey* pKey, gpointer userdata);
    5.21 -void on_cropper_window_create(GtkObject* object, gpointer user_data);
    5.22 -void on_cropper_window_destroy(GtkObject* object, gpointer user_data);