/* MediaImageWidget.c generated by valac 0.34.6, the Vala compiler
 * generated from MediaImageWidget.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 <float.h>
#include <math.h>
#include "CbMedia.h"
#include <gdk/gdk.h>
#include <cairo.h>


#define TYPE_MEDIA_IMAGE_WIDGET (media_image_widget_get_type ())
#define MEDIA_IMAGE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MEDIA_IMAGE_WIDGET, MediaImageWidget))
#define MEDIA_IMAGE_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MEDIA_IMAGE_WIDGET, MediaImageWidgetClass))
#define IS_MEDIA_IMAGE_WIDGET(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MEDIA_IMAGE_WIDGET))
#define IS_MEDIA_IMAGE_WIDGET_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MEDIA_IMAGE_WIDGET))
#define MEDIA_IMAGE_WIDGET_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MEDIA_IMAGE_WIDGET, MediaImageWidgetClass))

typedef struct _MediaImageWidget MediaImageWidget;
typedef struct _MediaImageWidgetClass MediaImageWidgetClass;
typedef struct _MediaImageWidgetPrivate MediaImageWidgetPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _MediaImageWidget {
	GtkScrolledWindow parent_instance;
	MediaImageWidgetPrivate * priv;
};

struct _MediaImageWidgetClass {
	GtkScrolledWindowClass parent_class;
};

struct _MediaImageWidgetPrivate {
	GtkImage* image;
	gdouble dnd_x;
	gdouble dnd_y;
};


static gpointer media_image_widget_parent_class = NULL;

GType media_image_widget_get_type (void) G_GNUC_CONST;
#define MEDIA_IMAGE_WIDGET_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_MEDIA_IMAGE_WIDGET, MediaImageWidgetPrivate))
enum  {
	MEDIA_IMAGE_WIDGET_DUMMY_PROPERTY
};
MediaImageWidget* media_image_widget_new (CbMedia* media);
MediaImageWidget* media_image_widget_construct (GType object_type, CbMedia* media);
static gboolean media_image_widget_event_box_motion_notify_cb (MediaImageWidget* self, GdkEventMotion* evt);
static gboolean _media_image_widget_event_box_motion_notify_cb_gtk_widget_motion_notify_event (GtkWidget* _sender, GdkEventMotion* event, gpointer self);
static gboolean media_image_widget_event_box_button_press_cb (MediaImageWidget* self, GdkEventButton* evt);
static gboolean _media_image_widget_event_box_button_press_cb_gtk_widget_button_press_event (GtkWidget* _sender, GdkEventButton* event, gpointer self);
static void media_image_widget_finalize (GObject* obj);


static gboolean _media_image_widget_event_box_motion_notify_cb_gtk_widget_motion_notify_event (GtkWidget* _sender, GdkEventMotion* event, gpointer self) {
	gboolean result;
	result = media_image_widget_event_box_motion_notify_cb ((MediaImageWidget*) self, event);
	return result;
}


static gboolean _media_image_widget_event_box_button_press_cb_gtk_widget_button_press_event (GtkWidget* _sender, GdkEventButton* event, gpointer self) {
	gboolean result;
	result = media_image_widget_event_box_button_press_cb ((MediaImageWidget*) self, event);
	return result;
}


MediaImageWidget* media_image_widget_construct (GType object_type, CbMedia* media) {
	MediaImageWidget * self = NULL;
	GtkImage* _tmp0_ = NULL;
	GtkEventBox* event_box = NULL;
	GtkEventBox* _tmp1_ = NULL;
	GtkEventBox* _tmp2_ = NULL;
	GtkImage* _tmp3_ = NULL;
	GtkEventBox* _tmp4_ = NULL;
	GtkEventBox* _tmp5_ = NULL;
	GtkEventBox* _tmp6_ = NULL;
	CbMedia* _tmp7_ = NULL;
	CbMediaType _tmp8_ = 0;
	gint img_width = 0;
	CbMedia* _tmp17_ = NULL;
	cairo_surface_t* _tmp18_ = NULL;
	gint _tmp19_ = 0;
	gint img_height = 0;
	CbMedia* _tmp20_ = NULL;
	cairo_surface_t* _tmp21_ = NULL;
	gint _tmp22_ = 0;
	gint win_width = 0;
	gint win_height = 0;
	gint _tmp23_ = 0;
	gint _tmp24_ = 0;
	gint _tmp26_ = 0;
	gint _tmp27_ = 0;
	gint _tmp29_ = 0;
	gint _tmp30_ = 0;
	g_return_val_if_fail (CB_IS_MEDIA (media), NULL);
	self = (MediaImageWidget*) g_object_new (object_type, NULL);
	_tmp0_ = (GtkImage*) gtk_image_new ();
	g_object_ref_sink (_tmp0_);
	_g_object_unref0 (self->priv->image);
	self->priv->image = _tmp0_;
	_tmp1_ = (GtkEventBox*) gtk_event_box_new ();
	g_object_ref_sink (_tmp1_);
	event_box = _tmp1_;
	_tmp2_ = event_box;
	_tmp3_ = self->priv->image;
	gtk_container_add (G_TYPE_CHECK_INSTANCE_CAST (_tmp2_, gtk_container_get_type (), GtkContainer), G_TYPE_CHECK_INSTANCE_CAST (_tmp3_, gtk_widget_get_type (), GtkWidget));
	_tmp4_ = event_box;
	g_signal_connect_object (G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, gtk_widget_get_type (), GtkWidget), "motion-notify-event", (GCallback) _media_image_widget_event_box_motion_notify_cb_gtk_widget_motion_notify_event, self, 0);
	_tmp5_ = event_box;
	g_signal_connect_object (G_TYPE_CHECK_INSTANCE_CAST (_tmp5_, gtk_widget_get_type (), GtkWidget), "button-press-event", (GCallback) _media_image_widget_event_box_button_press_cb_gtk_widget_button_press_event, self, 0);
	_tmp6_ = event_box;
	gtk_container_add (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_container_get_type (), GtkContainer), G_TYPE_CHECK_INSTANCE_CAST (_tmp6_, gtk_widget_get_type (), GtkWidget));
	_tmp7_ = media;
	_tmp8_ = _tmp7_->type;
	if (_tmp8_ == CB_MEDIA_TYPE_GIF) {
		CbMedia* _tmp9_ = NULL;
		GdkPixbufAnimation* _tmp10_ = NULL;
		GtkImage* _tmp11_ = NULL;
		CbMedia* _tmp12_ = NULL;
		GdkPixbufAnimation* _tmp13_ = NULL;
		_tmp9_ = media;
		_tmp10_ = _tmp9_->animation;
		_vala_assert (_tmp10_ != NULL, "media.animation != null");
		_tmp11_ = self->priv->image;
		_tmp12_ = media;
		_tmp13_ = _tmp12_->animation;
		gtk_image_set_from_animation (_tmp11_, _tmp13_);
	} else {
		GtkImage* _tmp14_ = NULL;
		CbMedia* _tmp15_ = NULL;
		cairo_surface_t* _tmp16_ = NULL;
		_tmp14_ = self->priv->image;
		_tmp15_ = media;
		_tmp16_ = _tmp15_->surface;
		gtk_image_set_from_surface (_tmp14_, _tmp16_);
	}
	_tmp17_ = media;
	_tmp18_ = _tmp17_->surface;
	_tmp19_ = cairo_image_surface_get_width (_tmp18_);
	img_width = _tmp19_;
	_tmp20_ = media;
	_tmp21_ = _tmp20_->surface;
	_tmp22_ = cairo_image_surface_get_height (_tmp21_);
	img_height = _tmp22_;
	win_width = 800;
	win_height = 600;
	_tmp23_ = img_width;
	_tmp24_ = gdk_screen_width ();
	if (((gdouble) _tmp23_) <= (_tmp24_ * 0.7)) {
		gint _tmp25_ = 0;
		_tmp25_ = img_width;
		win_width = _tmp25_;
		g_object_set (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow), "hscrollbar-policy", GTK_POLICY_NEVER, NULL);
	}
	_tmp26_ = img_height;
	_tmp27_ = gdk_screen_height ();
	if (((gdouble) _tmp26_) <= (_tmp27_ * 0.7)) {
		gint _tmp28_ = 0;
		_tmp28_ = img_height;
		win_height = _tmp28_;
		g_object_set (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow), "vscrollbar-policy", GTK_POLICY_NEVER, NULL);
	}
	_tmp29_ = win_width;
	_tmp30_ = win_height;
	gtk_widget_set_size_request (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_widget_get_type (), GtkWidget), _tmp29_, _tmp30_);
	_g_object_unref0 (event_box);
	return self;
}


MediaImageWidget* media_image_widget_new (CbMedia* media) {
	return media_image_widget_construct (TYPE_MEDIA_IMAGE_WIDGET, media);
}


static gboolean media_image_widget_event_box_motion_notify_cb (MediaImageWidget* self, GdkEventMotion* evt) {
	gboolean result = FALSE;
	GdkEventMotion* _tmp0_ = NULL;
	GdkModifierType _tmp1_ = 0;
	g_return_val_if_fail (IS_MEDIA_IMAGE_WIDGET (self), FALSE);
	g_return_val_if_fail (evt != NULL, FALSE);
	_tmp0_ = evt;
	_tmp1_ = _tmp0_->state;
	if ((_tmp1_ & GDK_MODIFIER_MASK) >= GDK_BUTTON2_MASK) {
		gdouble diff_x = 0.0;
		gdouble _tmp2_ = 0.0;
		GdkEventMotion* _tmp3_ = NULL;
		gdouble _tmp4_ = 0.0;
		gdouble diff_y = 0.0;
		gdouble _tmp5_ = 0.0;
		GdkEventMotion* _tmp6_ = NULL;
		gdouble _tmp7_ = 0.0;
		GtkAdjustment* _tmp8_ = NULL;
		GtkAdjustment* _tmp9_ = NULL;
		GtkAdjustment* _tmp10_ = NULL;
		GtkAdjustment* _tmp11_ = NULL;
		gdouble _tmp12_ = 0.0;
		gdouble _tmp13_ = 0.0;
		gdouble _tmp14_ = 0.0;
		GtkAdjustment* _tmp15_ = NULL;
		GtkAdjustment* _tmp16_ = NULL;
		GtkAdjustment* _tmp17_ = NULL;
		GtkAdjustment* _tmp18_ = NULL;
		gdouble _tmp19_ = 0.0;
		gdouble _tmp20_ = 0.0;
		gdouble _tmp21_ = 0.0;
		_tmp2_ = self->priv->dnd_x;
		_tmp3_ = evt;
		_tmp4_ = _tmp3_->x;
		diff_x = _tmp2_ - _tmp4_;
		_tmp5_ = self->priv->dnd_y;
		_tmp6_ = evt;
		_tmp7_ = _tmp6_->y;
		diff_y = _tmp5_ - _tmp7_;
		_tmp8_ = gtk_scrolled_window_get_vadjustment (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow));
		_tmp9_ = _tmp8_;
		_tmp10_ = gtk_scrolled_window_get_vadjustment (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow));
		_tmp11_ = _tmp10_;
		_tmp12_ = gtk_adjustment_get_value (_tmp11_);
		_tmp13_ = _tmp12_;
		_tmp14_ = diff_y;
		gtk_adjustment_set_value (_tmp11_, _tmp13_ + _tmp14_);
		_tmp15_ = gtk_scrolled_window_get_hadjustment (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow));
		_tmp16_ = _tmp15_;
		_tmp17_ = gtk_scrolled_window_get_hadjustment (G_TYPE_CHECK_INSTANCE_CAST (self, gtk_scrolled_window_get_type (), GtkScrolledWindow));
		_tmp18_ = _tmp17_;
		_tmp19_ = gtk_adjustment_get_value (_tmp18_);
		_tmp20_ = _tmp19_;
		_tmp21_ = diff_x;
		gtk_adjustment_set_value (_tmp18_, _tmp20_ + _tmp21_);
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}


static gboolean media_image_widget_event_box_button_press_cb (MediaImageWidget* self, GdkEventButton* evt) {
	gboolean result = FALSE;
	GdkEventButton* _tmp0_ = NULL;
	guint _tmp1_ = 0U;
	g_return_val_if_fail (IS_MEDIA_IMAGE_WIDGET (self), FALSE);
	g_return_val_if_fail (evt != NULL, FALSE);
	_tmp0_ = evt;
	_tmp1_ = _tmp0_->button;
	if (_tmp1_ == ((guint) 2)) {
		GdkEventButton* _tmp2_ = NULL;
		gdouble _tmp3_ = 0.0;
		GdkEventButton* _tmp4_ = NULL;
		gdouble _tmp5_ = 0.0;
		_tmp2_ = evt;
		_tmp3_ = _tmp2_->x;
		self->priv->dnd_x = _tmp3_;
		_tmp4_ = evt;
		_tmp5_ = _tmp4_->y;
		self->priv->dnd_y = _tmp5_;
		result = TRUE;
		return result;
	}
	result = FALSE;
	return result;
}


static void media_image_widget_class_init (MediaImageWidgetClass * klass) {
	media_image_widget_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (MediaImageWidgetPrivate));
	G_OBJECT_CLASS (klass)->finalize = media_image_widget_finalize;
}


static void media_image_widget_instance_init (MediaImageWidget * self) {
	self->priv = MEDIA_IMAGE_WIDGET_GET_PRIVATE (self);
}


static void media_image_widget_finalize (GObject* obj) {
	MediaImageWidget * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_MEDIA_IMAGE_WIDGET, MediaImageWidget);
	_g_object_unref0 (self->priv->image);
	G_OBJECT_CLASS (media_image_widget_parent_class)->finalize (obj);
}


GType media_image_widget_get_type (void) {
	static volatile gsize media_image_widget_type_id__volatile = 0;
	if (g_once_init_enter (&media_image_widget_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (MediaImageWidgetClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) media_image_widget_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (MediaImageWidget), 0, (GInstanceInitFunc) media_image_widget_instance_init, NULL };
		GType media_image_widget_type_id;
		media_image_widget_type_id = g_type_register_static (gtk_scrolled_window_get_type (), "MediaImageWidget", &g_define_type_info, 0);
		g_once_init_leave (&media_image_widget_type_id__volatile, media_image_widget_type_id);
	}
	return media_image_widget_type_id__volatile;
}



