/* gitg-diff-image-composite.c generated by valac 0.56.19, the Vala compiler
 * generated from gitg-diff-image-composite.vala, do not modify */

/*
 * This file is part of gitg
 *
 * Copyright (C) 2016 - Jesse van den Kieboom
 *
 * gitg 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 2 of the License, or
 * (at your option) any later version.
 *
 * gitg 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 gitg. If not, see <http://www.gnu.org/licenses/>.
 */

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

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

#define GITG_TYPE_DIFF_IMAGE_COMPOSITE (gitg_diff_image_composite_get_type ())
#define GITG_DIFF_IMAGE_COMPOSITE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite))
#define GITG_DIFF_IMAGE_COMPOSITE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageCompositeClass))
#define GITG_IS_DIFF_IMAGE_COMPOSITE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE))
#define GITG_IS_DIFF_IMAGE_COMPOSITE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GITG_TYPE_DIFF_IMAGE_COMPOSITE))
#define GITG_DIFF_IMAGE_COMPOSITE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageCompositeClass))

typedef struct _GitgDiffImageComposite GitgDiffImageComposite;
typedef struct _GitgDiffImageCompositeClass GitgDiffImageCompositeClass;
typedef struct _GitgDiffImageCompositePrivate GitgDiffImageCompositePrivate;

#define GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE (gitg_diff_image_surface_cache_get_type ())
#define GITG_DIFF_IMAGE_SURFACE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE, GitgDiffImageSurfaceCache))
#define GITG_IS_DIFF_IMAGE_SURFACE_CACHE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE))
#define GITG_DIFF_IMAGE_SURFACE_CACHE_GET_INTERFACE(obj) (G_TYPE_INSTANCE_GET_INTERFACE ((obj), GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE, GitgDiffImageSurfaceCacheIface))

typedef struct _GitgDiffImageSurfaceCache GitgDiffImageSurfaceCache;
typedef struct _GitgDiffImageSurfaceCacheIface GitgDiffImageSurfaceCacheIface;
enum  {
	GITG_DIFF_IMAGE_COMPOSITE_0_PROPERTY,
	GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY,
	GITG_DIFF_IMAGE_COMPOSITE_NUM_PROPERTIES
};
static GParamSpec* gitg_diff_image_composite_properties[GITG_DIFF_IMAGE_COMPOSITE_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))

struct _GitgDiffImageComposite {
	GtkDrawingArea parent_instance;
	GitgDiffImageCompositePrivate * priv;
};

struct _GitgDiffImageCompositeClass {
	GtkDrawingAreaClass parent_class;
};

struct _GitgDiffImageSurfaceCacheIface {
	GTypeInterface parent_iface;
	cairo_surface_t* (*get_old_surface) (GitgDiffImageSurfaceCache* self, GdkWindow* window);
	cairo_surface_t* (*get_new_surface) (GitgDiffImageSurfaceCache* self, GdkWindow* window);
	GdkPixbuf* (*get_old_pixbuf) (GitgDiffImageSurfaceCache* self);
	void (*set_old_pixbuf) (GitgDiffImageSurfaceCache* self, GdkPixbuf* value);
	GdkPixbuf* (*get_new_pixbuf) (GitgDiffImageSurfaceCache* self);
	void (*set_new_pixbuf) (GitgDiffImageSurfaceCache* self, GdkPixbuf* value);
	GdkWindow* (*get_window) (GitgDiffImageSurfaceCache* self);
	void (*set_window) (GitgDiffImageSurfaceCache* self, GdkWindow* value);
};

struct _GitgDiffImageCompositePrivate {
	GitgDiffImageSurfaceCache* _cache;
};

static gint GitgDiffImageComposite_private_offset;
static gpointer gitg_diff_image_composite_parent_class = NULL;

VALA_EXTERN GType gitg_diff_image_composite_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType gitg_diff_image_surface_cache_get_type (void) G_GNUC_CONST ;
static void gitg_diff_image_composite_get_natural_size (GitgDiffImageComposite* self,
                                                 gint* image_width,
                                                 gint* image_height);
VALA_EXTERN GitgDiffImageSurfaceCache* gitg_diff_image_composite_get_cache (GitgDiffImageComposite* self);
VALA_EXTERN GdkPixbuf* gitg_diff_image_surface_cache_get_old_pixbuf (GitgDiffImageSurfaceCache* self);
VALA_EXTERN cairo_surface_t* gitg_diff_image_surface_cache_get_old_surface (GitgDiffImageSurfaceCache* self,
                                                                GdkWindow* window);
VALA_EXTERN void gitg_diff_image_composite_get_sizing (GitgDiffImageComposite* self,
                                           gint width,
                                           gint* image_width,
                                           gint* image_height);
static void gitg_diff_image_composite_real_get_preferred_width (GtkWidget* base,
                                                         gint* minimum_width,
                                                         gint* natural_width);
static void gitg_diff_image_composite_real_get_preferred_height_for_width (GtkWidget* base,
                                                                    gint width,
                                                                    gint* minimum_height,
                                                                    gint* natural_height);
static GtkSizeRequestMode gitg_diff_image_composite_real_get_request_mode (GtkWidget* base);
static gboolean gitg_diff_image_composite_real_draw (GtkWidget* base,
                                              cairo_t* cr);
static void gitg_diff_image_composite_real_realize (GtkWidget* base);
VALA_EXTERN GitgDiffImageComposite* gitg_diff_image_composite_new (void);
VALA_EXTERN GitgDiffImageComposite* gitg_diff_image_composite_construct (GType object_type);
VALA_EXTERN void gitg_diff_image_composite_set_cache (GitgDiffImageComposite* self,
                                          GitgDiffImageSurfaceCache* value);
static void gitg_diff_image_composite_finalize (GObject * obj);
static GType gitg_diff_image_composite_get_type_once (void);
static void _vala_gitg_diff_image_composite_get_property (GObject * object,
                                                   guint property_id,
                                                   GValue * value,
                                                   GParamSpec * pspec);
static void _vala_gitg_diff_image_composite_set_property (GObject * object,
                                                   guint property_id,
                                                   const GValue * value,
                                                   GParamSpec * pspec);

static inline gpointer
gitg_diff_image_composite_get_instance_private (GitgDiffImageComposite* self)
{
	return G_STRUCT_MEMBER_P (self, GitgDiffImageComposite_private_offset);
}

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

static void
gitg_diff_image_composite_get_natural_size (GitgDiffImageComposite* self,
                                            gint* image_width,
                                            gint* image_height)
{
	gint _vala_image_width = 0;
	gint _vala_image_height = 0;
	GdkPixbuf* pixbuf = NULL;
	GitgDiffImageSurfaceCache* _tmp0_;
	GdkPixbuf* _tmp1_;
	GdkPixbuf* _tmp2_;
	GdkPixbuf* _tmp3_;
	GdkPixbuf* _tmp4_;
	GdkWindow* window = NULL;
	GdkWindow* _tmp5_;
	GdkWindow* _tmp6_;
	gdouble xscale = 0.0;
	gdouble yscale = 0.0;
	GdkWindow* _tmp7_;
	GdkPixbuf* _tmp14_;
	GdkPixbuf* _tmp15_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_cache;
	_tmp1_ = gitg_diff_image_surface_cache_get_old_pixbuf (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = _g_object_ref0 (_tmp2_);
	pixbuf = _tmp3_;
	_tmp4_ = pixbuf;
	if (_tmp4_ == NULL) {
		_vala_image_width = 0;
		_vala_image_height = 0;
		_g_object_unref0 (pixbuf);
		if (image_width) {
			*image_width = _vala_image_width;
		}
		if (image_height) {
			*image_height = _vala_image_height;
		}
		return;
	}
	_tmp5_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp6_ = _g_object_ref0 (_tmp5_);
	window = _tmp6_;
	xscale = (gdouble) 1;
	yscale = (gdouble) 1;
	_tmp7_ = window;
	if (_tmp7_ != NULL) {
		GitgDiffImageSurfaceCache* _tmp8_;
		GdkWindow* _tmp9_;
		cairo_surface_t* _tmp10_;
		cairo_surface_t* _tmp11_;
		gdouble _tmp12_ = 0.0;
		gdouble _tmp13_ = 0.0;
		_tmp8_ = self->priv->_cache;
		_tmp9_ = gtk_widget_get_window ((GtkWidget*) self);
		_tmp10_ = gitg_diff_image_surface_cache_get_old_surface (_tmp8_, _tmp9_);
		_tmp11_ = _tmp10_;
		cairo_surface_get_device_scale (_tmp11_, &_tmp12_, &_tmp13_);
		xscale = _tmp12_;
		yscale = _tmp13_;
		_cairo_surface_destroy0 (_tmp11_);
	}
	_tmp14_ = pixbuf;
	_vala_image_width = (gint) (gdk_pixbuf_get_width (_tmp14_) / xscale);
	_tmp15_ = pixbuf;
	_vala_image_height = (gint) (gdk_pixbuf_get_height (_tmp15_) / yscale);
	_g_object_unref0 (window);
	_g_object_unref0 (pixbuf);
	if (image_width) {
		*image_width = _vala_image_width;
	}
	if (image_height) {
		*image_height = _vala_image_height;
	}
}

void
gitg_diff_image_composite_get_sizing (GitgDiffImageComposite* self,
                                      gint width,
                                      gint* image_width,
                                      gint* image_height)
{
	gint _vala_image_width = 0;
	gint _vala_image_height = 0;
	gint _tmp0_ = 0;
	gint _tmp1_ = 0;
	g_return_if_fail (self != NULL);
	gitg_diff_image_composite_get_natural_size (self, &_tmp0_, &_tmp1_);
	_vala_image_width = _tmp0_;
	_vala_image_height = _tmp1_;
	if (_vala_image_width > width) {
		_vala_image_height = _vala_image_height * (width / _vala_image_width);
		_vala_image_width = width;
	}
	if (image_width) {
		*image_width = _vala_image_width;
	}
	if (image_height) {
		*image_height = _vala_image_height;
	}
}

static void
gitg_diff_image_composite_real_get_preferred_width (GtkWidget* base,
                                                    gint* minimum_width,
                                                    gint* natural_width)
{
	GitgDiffImageComposite * self;
	gint _vala_minimum_width = 0;
	gint _vala_natural_width = 0;
	gint natural_height = 0;
	gint _tmp0_ = 0;
	gint _tmp1_ = 0;
	self = (GitgDiffImageComposite*) base;
	gitg_diff_image_composite_get_natural_size (self, &_tmp0_, &_tmp1_);
	_vala_natural_width = _tmp0_;
	natural_height = _tmp1_;
	_vala_minimum_width = 0;
	if (minimum_width) {
		*minimum_width = _vala_minimum_width;
	}
	if (natural_width) {
		*natural_width = _vala_natural_width;
	}
}

static void
gitg_diff_image_composite_real_get_preferred_height_for_width (GtkWidget* base,
                                                               gint width,
                                                               gint* minimum_height,
                                                               gint* natural_height)
{
	GitgDiffImageComposite * self;
	gint _vala_minimum_height = 0;
	gint _vala_natural_height = 0;
	gint image_width = 0;
	gint image_height = 0;
	gint _tmp0_ = 0;
	gint _tmp1_ = 0;
	self = (GitgDiffImageComposite*) base;
	gitg_diff_image_composite_get_sizing (self, width, &_tmp0_, &_tmp1_);
	image_width = _tmp0_;
	image_height = _tmp1_;
	_vala_minimum_height = image_height;
	_vala_natural_height = image_height;
	if (minimum_height) {
		*minimum_height = _vala_minimum_height;
	}
	if (natural_height) {
		*natural_height = _vala_natural_height;
	}
}

static GtkSizeRequestMode
gitg_diff_image_composite_real_get_request_mode (GtkWidget* base)
{
	GitgDiffImageComposite * self;
	GtkSizeRequestMode result;
	self = (GitgDiffImageComposite*) base;
	result = GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
	return result;
}

static gboolean
gitg_diff_image_composite_real_draw (GtkWidget* base,
                                     cairo_t* cr)
{
	GitgDiffImageComposite * self;
	GtkAllocation alloc = {0};
	GtkAllocation _tmp0_ = {0};
	GtkStyleContext* ctx = NULL;
	GtkStyleContext* _tmp1_;
	GtkStyleContext* _tmp2_;
	GtkAllocation _tmp3_;
	GtkAllocation _tmp4_;
	GtkAllocation _tmp5_;
	GtkAllocation _tmp6_;
	gboolean result;
	self = (GitgDiffImageComposite*) base;
	g_return_val_if_fail (cr != NULL, FALSE);
	gtk_widget_get_allocation ((GtkWidget*) self, &_tmp0_);
	alloc = _tmp0_;
	_tmp1_ = gtk_widget_get_style_context ((GtkWidget*) self);
	_tmp2_ = _g_object_ref0 (_tmp1_);
	ctx = _tmp2_;
	_tmp3_ = alloc;
	_tmp4_ = alloc;
	_tmp5_ = alloc;
	_tmp6_ = alloc;
	gtk_render_background (ctx, cr, (gdouble) _tmp3_.x, (gdouble) _tmp4_.y, (gdouble) _tmp5_.width, (gdouble) _tmp6_.height);
	result = TRUE;
	_g_object_unref0 (ctx);
	return result;
}

static void
gitg_diff_image_composite_real_realize (GtkWidget* base)
{
	GitgDiffImageComposite * self;
	self = (GitgDiffImageComposite*) base;
	GTK_WIDGET_CLASS (gitg_diff_image_composite_parent_class)->realize ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_drawing_area_get_type (), GtkDrawingArea));
	gtk_widget_queue_resize ((GtkWidget*) self);
}

GitgDiffImageComposite*
gitg_diff_image_composite_construct (GType object_type)
{
	GitgDiffImageComposite * self = NULL;
	self = (GitgDiffImageComposite*) g_object_new (object_type, NULL);
	return self;
}

GitgDiffImageComposite*
gitg_diff_image_composite_new (void)
{
	return gitg_diff_image_composite_construct (GITG_TYPE_DIFF_IMAGE_COMPOSITE);
}

GitgDiffImageSurfaceCache*
gitg_diff_image_composite_get_cache (GitgDiffImageComposite* self)
{
	GitgDiffImageSurfaceCache* result;
	GitgDiffImageSurfaceCache* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_cache;
	result = _tmp0_;
	return result;
}

void
gitg_diff_image_composite_set_cache (GitgDiffImageComposite* self,
                                     GitgDiffImageSurfaceCache* value)
{
	GitgDiffImageSurfaceCache* old_value;
	g_return_if_fail (self != NULL);
	old_value = gitg_diff_image_composite_get_cache (self);
	if (old_value != value) {
		GitgDiffImageSurfaceCache* _tmp0_;
		_tmp0_ = _g_object_ref0 (value);
		_g_object_unref0 (self->priv->_cache);
		self->priv->_cache = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, gitg_diff_image_composite_properties[GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY]);
	}
}

static void
gitg_diff_image_composite_class_init (GitgDiffImageCompositeClass * klass,
                                      gpointer klass_data)
{
	gitg_diff_image_composite_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GitgDiffImageComposite_private_offset);
	((GtkWidgetClass *) klass)->get_preferred_width = (void (*) (GtkWidget*, gint*, gint*)) gitg_diff_image_composite_real_get_preferred_width;
	((GtkWidgetClass *) klass)->get_preferred_height_for_width = (void (*) (GtkWidget*, gint, gint*, gint*)) gitg_diff_image_composite_real_get_preferred_height_for_width;
	((GtkWidgetClass *) klass)->get_request_mode = (GtkSizeRequestMode (*) (GtkWidget*)) gitg_diff_image_composite_real_get_request_mode;
	((GtkWidgetClass *) klass)->draw = (gboolean (*) (GtkWidget*, cairo_t*)) gitg_diff_image_composite_real_draw;
	((GtkWidgetClass *) klass)->realize = (void (*) (GtkWidget*)) gitg_diff_image_composite_real_realize;
	G_OBJECT_CLASS (klass)->get_property = _vala_gitg_diff_image_composite_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_gitg_diff_image_composite_set_property;
	G_OBJECT_CLASS (klass)->finalize = gitg_diff_image_composite_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY, gitg_diff_image_composite_properties[GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY] = g_param_spec_object ("cache", "cache", "cache", GITG_TYPE_DIFF_IMAGE_SURFACE_CACHE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
gitg_diff_image_composite_instance_init (GitgDiffImageComposite * self,
                                         gpointer klass)
{
	self->priv = gitg_diff_image_composite_get_instance_private (self);
}

static void
gitg_diff_image_composite_finalize (GObject * obj)
{
	GitgDiffImageComposite * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite);
	_g_object_unref0 (self->priv->_cache);
	G_OBJECT_CLASS (gitg_diff_image_composite_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
gitg_diff_image_composite_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GitgDiffImageCompositeClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gitg_diff_image_composite_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GitgDiffImageComposite), 0, (GInstanceInitFunc) gitg_diff_image_composite_instance_init, NULL };
	GType gitg_diff_image_composite_type_id;
	gitg_diff_image_composite_type_id = g_type_register_static (gtk_drawing_area_get_type (), "GitgDiffImageComposite", &g_define_type_info, 0);
	GitgDiffImageComposite_private_offset = g_type_add_instance_private (gitg_diff_image_composite_type_id, sizeof (GitgDiffImageCompositePrivate));
	return gitg_diff_image_composite_type_id;
}

GType
gitg_diff_image_composite_get_type (void)
{
	static gsize gitg_diff_image_composite_type_id__once = 0;
	if (g_once_init_enter (&gitg_diff_image_composite_type_id__once)) {
		GType gitg_diff_image_composite_type_id;
		gitg_diff_image_composite_type_id = gitg_diff_image_composite_get_type_once ();
		g_once_init_leave (&gitg_diff_image_composite_type_id__once, gitg_diff_image_composite_type_id);
	}
	return gitg_diff_image_composite_type_id__once;
}

static void
_vala_gitg_diff_image_composite_get_property (GObject * object,
                                              guint property_id,
                                              GValue * value,
                                              GParamSpec * pspec)
{
	GitgDiffImageComposite * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite);
	switch (property_id) {
		case GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY:
		g_value_set_object (value, gitg_diff_image_composite_get_cache (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_gitg_diff_image_composite_set_property (GObject * object,
                                              guint property_id,
                                              const GValue * value,
                                              GParamSpec * pspec)
{
	GitgDiffImageComposite * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite);
	switch (property_id) {
		case GITG_DIFF_IMAGE_COMPOSITE_CACHE_PROPERTY:
		gitg_diff_image_composite_set_cache (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

