/* AspectImage.c generated by valac 0.26.2, the Vala compiler
 * generated from AspectImage.vala, do not modify */

/*  This file is part of corebird, a Gtk+ linux Twitter client.
 *  Copyright (C) 2013 Timm Bäder
 *
 *  corebird is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  corebird is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with corebird.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <float.h>
#include <math.h>
#include <cairo.h>
#include <gdk/gdk.h>


#define TYPE_ASPECT_IMAGE (aspect_image_get_type ())
#define ASPECT_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_ASPECT_IMAGE, AspectImage))
#define ASPECT_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_ASPECT_IMAGE, AspectImageClass))
#define IS_ASPECT_IMAGE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_ASPECT_IMAGE))
#define IS_ASPECT_IMAGE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_ASPECT_IMAGE))
#define ASPECT_IMAGE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_ASPECT_IMAGE, AspectImageClass))

typedef struct _AspectImage AspectImage;
typedef struct _AspectImageClass AspectImageClass;
typedef struct _AspectImagePrivate AspectImagePrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _AspectImage {
	GtkWidget parent_instance;
	AspectImagePrivate * priv;
};

struct _AspectImageClass {
	GtkWidgetClass parent_class;
};

struct _AspectImagePrivate {
	GdkPixbuf* _pixbuf;
	gdouble _scale;
};


static gpointer aspect_image_parent_class = NULL;

GType aspect_image_get_type (void) G_GNUC_CONST;
#define ASPECT_IMAGE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_ASPECT_IMAGE, AspectImagePrivate))
enum  {
	ASPECT_IMAGE_DUMMY_PROPERTY,
	ASPECT_IMAGE_PIXBUF,
	ASPECT_IMAGE_SCALE
};
AspectImage* aspect_image_new (void);
AspectImage* aspect_image_construct (GType object_type);
static void aspect_image_real_get_preferred_height_for_width (GtkWidget* base, gint width, gint* min_height, gint* nat_height);
GdkPixbuf* aspect_image_get_pixbuf (AspectImage* self);
gdouble aspect_image_get_scale (AspectImage* self);
static GtkSizeRequestMode aspect_image_real_get_request_mode (GtkWidget* base);
static gboolean aspect_image_real_draw (GtkWidget* base, cairo_t* ct);
void aspect_image_set_pixbuf (AspectImage* self, GdkPixbuf* value);
void aspect_image_set_scale (AspectImage* self, gdouble value);
static GObject * aspect_image_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
static void aspect_image_finalize (GObject* obj);
static void _vala_aspect_image_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_aspect_image_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


AspectImage* aspect_image_construct (GType object_type) {
	AspectImage * self = NULL;
	self = (AspectImage*) gtk_widget_new (object_type, NULL);
	return self;
}


AspectImage* aspect_image_new (void) {
	return aspect_image_construct (TYPE_ASPECT_IMAGE);
}


static void aspect_image_real_get_preferred_height_for_width (GtkWidget* base, gint width, gint* min_height, gint* nat_height) {
	AspectImage * self;
	gint _vala_min_height = 0;
	gint _vala_nat_height = 0;
	GdkPixbuf* _tmp0_ = NULL;
	GdkPixbuf* _tmp1_ = NULL;
	GdkPixbuf* _tmp2_ = NULL;
	GdkPixbuf* _tmp3_ = NULL;
	gint _tmp4_ = 0;
	gdouble _tmp5_ = 0.0;
	GdkPixbuf* _tmp6_ = NULL;
	GdkPixbuf* _tmp7_ = NULL;
	gint _tmp8_ = 0;
	gdouble _tmp9_ = 0.0;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, TYPE_ASPECT_IMAGE, AspectImage);
	_tmp0_ = aspect_image_get_pixbuf (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		_vala_min_height = 0;
		_vala_nat_height = 1;
		if (min_height) {
			*min_height = _vala_min_height;
		}
		if (nat_height) {
			*nat_height = _vala_nat_height;
		}
		return;
	}
	_tmp2_ = aspect_image_get_pixbuf (self);
	_tmp3_ = _tmp2_;
	_tmp4_ = gdk_pixbuf_get_height (_tmp3_);
	_tmp5_ = self->priv->_scale;
	_vala_min_height = (gint) (_tmp4_ * _tmp5_);
	_tmp6_ = aspect_image_get_pixbuf (self);
	_tmp7_ = _tmp6_;
	_tmp8_ = gdk_pixbuf_get_height (_tmp7_);
	_tmp9_ = self->priv->_scale;
	_vala_nat_height = (gint) (_tmp8_ * _tmp9_);
	if (min_height) {
		*min_height = _vala_min_height;
	}
	if (nat_height) {
		*nat_height = _vala_nat_height;
	}
}


static GtkSizeRequestMode aspect_image_real_get_request_mode (GtkWidget* base) {
	AspectImage * self;
	GtkSizeRequestMode result = 0;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, TYPE_ASPECT_IMAGE, AspectImage);
	result = GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
	return result;
}


static gboolean aspect_image_real_draw (GtkWidget* base, cairo_t* ct) {
	AspectImage * self;
	gboolean result = FALSE;
	GdkPixbuf* _tmp0_ = NULL;
	GdkPixbuf* _tmp1_ = NULL;
	gint width = 0;
	gint _tmp2_ = 0;
	gint height = 0;
	gint _tmp3_ = 0;
	gdouble scale_x = 0.0;
	gint _tmp4_ = 0;
	GdkPixbuf* _tmp5_ = NULL;
	GdkPixbuf* _tmp6_ = NULL;
	gint _tmp7_ = 0;
	gdouble scale_y = 0.0;
	gint _tmp8_ = 0;
	GdkPixbuf* _tmp9_ = NULL;
	GdkPixbuf* _tmp10_ = NULL;
	gint _tmp11_ = 0;
	gint x = 0;
	gint y = 0;
	gdouble _tmp12_ = 0.0;
	gdouble _tmp17_ = 0.0;
	cairo_t* _tmp22_ = NULL;
	gdouble _tmp23_ = 0.0;
	gdouble _tmp24_ = 0.0;
	cairo_t* _tmp25_ = NULL;
	GdkPixbuf* _tmp26_ = NULL;
	GdkPixbuf* _tmp27_ = NULL;
	gint _tmp28_ = 0;
	gint _tmp29_ = 0;
	cairo_t* _tmp30_ = NULL;
	gint _tmp31_ = 0;
	GdkPixbuf* _tmp32_ = NULL;
	GdkPixbuf* _tmp33_ = NULL;
	gint _tmp34_ = 0;
	cairo_t* _tmp35_ = NULL;
	self = G_TYPE_CHECK_INSTANCE_CAST (base, TYPE_ASPECT_IMAGE, AspectImage);
	g_return_val_if_fail (ct != NULL, FALSE);
	_tmp0_ = aspect_image_get_pixbuf (self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		result = TRUE;
		return result;
	}
	_tmp2_ = gtk_widget_get_allocated_width (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget));
	width = _tmp2_;
	_tmp3_ = gtk_widget_get_allocated_height (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget));
	height = _tmp3_;
	_tmp4_ = width;
	_tmp5_ = aspect_image_get_pixbuf (self);
	_tmp6_ = _tmp5_;
	_tmp7_ = gdk_pixbuf_get_width (_tmp6_);
	scale_x = _tmp4_ / ((gdouble) _tmp7_);
	_tmp8_ = height;
	_tmp9_ = aspect_image_get_pixbuf (self);
	_tmp10_ = _tmp9_;
	_tmp11_ = gdk_pixbuf_get_height (_tmp10_);
	scale_y = _tmp8_ / ((gdouble) _tmp11_);
	x = 0;
	y = 0;
	_tmp12_ = scale_x;
	if (_tmp12_ < ((gdouble) 1)) {
		gint _tmp13_ = 0;
		GdkPixbuf* _tmp14_ = NULL;
		GdkPixbuf* _tmp15_ = NULL;
		gint _tmp16_ = 0;
		scale_x = (gdouble) 1;
		_tmp13_ = width;
		_tmp14_ = aspect_image_get_pixbuf (self);
		_tmp15_ = _tmp14_;
		_tmp16_ = gdk_pixbuf_get_width (_tmp15_);
		x = (_tmp13_ - _tmp16_) / 2;
	}
	_tmp17_ = scale_y;
	if (_tmp17_ < ((gdouble) 1)) {
		gint _tmp18_ = 0;
		GdkPixbuf* _tmp19_ = NULL;
		GdkPixbuf* _tmp20_ = NULL;
		gint _tmp21_ = 0;
		scale_y = (gdouble) 1;
		_tmp18_ = height;
		_tmp19_ = aspect_image_get_pixbuf (self);
		_tmp20_ = _tmp19_;
		_tmp21_ = gdk_pixbuf_get_height (_tmp20_);
		y = _tmp18_ - _tmp21_;
	}
	_tmp22_ = ct;
	_tmp23_ = scale_x;
	_tmp24_ = scale_y;
	cairo_scale (_tmp22_, _tmp23_, _tmp24_);
	_tmp25_ = ct;
	_tmp26_ = aspect_image_get_pixbuf (self);
	_tmp27_ = _tmp26_;
	_tmp28_ = x;
	_tmp29_ = y;
	gdk_cairo_set_source_pixbuf (_tmp25_, _tmp27_, (gdouble) _tmp28_, (gdouble) _tmp29_);
	_tmp30_ = ct;
	_tmp31_ = width;
	_tmp32_ = aspect_image_get_pixbuf (self);
	_tmp33_ = _tmp32_;
	_tmp34_ = gdk_pixbuf_get_height (_tmp33_);
	cairo_rectangle (_tmp30_, (gdouble) 0, (gdouble) 0, (gdouble) _tmp31_, (gdouble) _tmp34_);
	_tmp35_ = ct;
	cairo_fill (_tmp35_);
	result = FALSE;
	return result;
}


GdkPixbuf* aspect_image_get_pixbuf (AspectImage* self) {
	GdkPixbuf* result;
	GdkPixbuf* _tmp0_ = NULL;
	g_return_val_if_fail (IS_ASPECT_IMAGE (self), NULL);
	_tmp0_ = self->priv->_pixbuf;
	result = _tmp0_;
	return result;
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


void aspect_image_set_pixbuf (AspectImage* self, GdkPixbuf* value) {
	GdkPixbuf* _tmp0_ = NULL;
	GdkPixbuf* _tmp1_ = NULL;
	g_return_if_fail (IS_ASPECT_IMAGE (self));
	_tmp0_ = value;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	_g_object_unref0 (self->priv->_pixbuf);
	self->priv->_pixbuf = _tmp1_;
	gtk_widget_queue_draw (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget));
	gtk_widget_queue_resize (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget));
	g_object_notify ((GObject *) self, "pixbuf");
}


gdouble aspect_image_get_scale (AspectImage* self) {
	gdouble result;
	gdouble _tmp0_ = 0.0;
	g_return_val_if_fail (IS_ASPECT_IMAGE (self), 0.0);
	_tmp0_ = self->priv->_scale;
	result = _tmp0_;
	return result;
}


void aspect_image_set_scale (AspectImage* self, gdouble value) {
	gdouble _tmp0_ = 0.0;
	g_return_if_fail (IS_ASPECT_IMAGE (self));
	_tmp0_ = value;
	self->priv->_scale = _tmp0_;
	g_object_notify ((GObject *) self, "scale");
}


static GObject * aspect_image_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	AspectImage * self;
	parent_class = G_OBJECT_CLASS (aspect_image_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_ASPECT_IMAGE, AspectImage);
	gtk_widget_set_has_window (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget), FALSE);
	return obj;
}


static void aspect_image_class_init (AspectImageClass * klass) {
	aspect_image_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (AspectImagePrivate));
	((GtkWidgetClass *) klass)->get_preferred_height_for_width = aspect_image_real_get_preferred_height_for_width;
	((GtkWidgetClass *) klass)->get_request_mode = aspect_image_real_get_request_mode;
	((GtkWidgetClass *) klass)->draw = aspect_image_real_draw;
	G_OBJECT_CLASS (klass)->get_property = _vala_aspect_image_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_aspect_image_set_property;
	G_OBJECT_CLASS (klass)->constructor = aspect_image_constructor;
	G_OBJECT_CLASS (klass)->finalize = aspect_image_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), ASPECT_IMAGE_PIXBUF, g_param_spec_object ("pixbuf", "pixbuf", "pixbuf", GDK_TYPE_PIXBUF, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), ASPECT_IMAGE_SCALE, g_param_spec_double ("scale", "scale", "scale", -G_MAXDOUBLE, G_MAXDOUBLE, 1.0, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE));
}


static void aspect_image_instance_init (AspectImage * self) {
	self->priv = ASPECT_IMAGE_GET_PRIVATE (self);
	self->priv->_scale = 1.0;
}


static void aspect_image_finalize (GObject* obj) {
	AspectImage * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_ASPECT_IMAGE, AspectImage);
	_g_object_unref0 (self->priv->_pixbuf);
	G_OBJECT_CLASS (aspect_image_parent_class)->finalize (obj);
}


GType aspect_image_get_type (void) {
	static volatile gsize aspect_image_type_id__volatile = 0;
	if (g_once_init_enter (&aspect_image_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (AspectImageClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) aspect_image_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (AspectImage), 0, (GInstanceInitFunc) aspect_image_instance_init, NULL };
		GType aspect_image_type_id;
		aspect_image_type_id = g_type_register_static (gtk_widget_get_type (), "AspectImage", &g_define_type_info, 0);
		g_once_init_leave (&aspect_image_type_id__volatile, aspect_image_type_id);
	}
	return aspect_image_type_id__volatile;
}


static void _vala_aspect_image_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	AspectImage * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_ASPECT_IMAGE, AspectImage);
	switch (property_id) {
		case ASPECT_IMAGE_PIXBUF:
		g_value_set_object (value, aspect_image_get_pixbuf (self));
		break;
		case ASPECT_IMAGE_SCALE:
		g_value_set_double (value, aspect_image_get_scale (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_aspect_image_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	AspectImage * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_ASPECT_IMAGE, AspectImage);
	switch (property_id) {
		case ASPECT_IMAGE_PIXBUF:
		aspect_image_set_pixbuf (self, g_value_get_object (value));
		break;
		case ASPECT_IMAGE_SCALE:
		aspect_image_set_scale (self, g_value_get_double (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



