/* gitg-diff-image-slider.c generated by valac 0.56.19, the Vala compiler
 * generated from gitg-diff-image-slider.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 <glib.h>
#include <float.h>
#include <math.h>
#include <cairo-gobject.h>
#include <gdk/gdk.h>
#include <gdk-pixbuf/gdk-pixbuf.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_SLIDER (gitg_diff_image_slider_get_type ())
#define GITG_DIFF_IMAGE_SLIDER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSlider))
#define GITG_DIFF_IMAGE_SLIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSliderClass))
#define GITG_IS_DIFF_IMAGE_SLIDER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GITG_TYPE_DIFF_IMAGE_SLIDER))
#define GITG_IS_DIFF_IMAGE_SLIDER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GITG_TYPE_DIFF_IMAGE_SLIDER))
#define GITG_DIFF_IMAGE_SLIDER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSliderClass))

typedef struct _GitgDiffImageSlider GitgDiffImageSlider;
typedef struct _GitgDiffImageSliderClass GitgDiffImageSliderClass;
typedef struct _GitgDiffImageSliderPrivate GitgDiffImageSliderPrivate;
enum  {
	GITG_DIFF_IMAGE_SLIDER_0_PROPERTY,
	GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY,
	GITG_DIFF_IMAGE_SLIDER_NUM_PROPERTIES
};
static GParamSpec* gitg_diff_image_slider_properties[GITG_DIFF_IMAGE_SLIDER_NUM_PROPERTIES];

#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;
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

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

struct _GitgDiffImageCompositeClass {
	GtkDrawingAreaClass parent_class;
};

struct _GitgDiffImageSlider {
	GitgDiffImageComposite parent_instance;
	GitgDiffImageSliderPrivate * priv;
};

struct _GitgDiffImageSliderClass {
	GitgDiffImageCompositeClass parent_class;
};

struct _GitgDiffImageSliderPrivate {
	gdouble d_position;
};

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);
};

static gint GitgDiffImageSlider_private_offset;
static gpointer gitg_diff_image_slider_parent_class = NULL;

VALA_EXTERN GType gitg_diff_image_composite_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GType gitg_diff_image_slider_get_type (void) G_GNUC_CONST ;
static gboolean gitg_diff_image_slider_real_draw (GtkWidget* base,
                                           cairo_t* cr);
VALA_EXTERN void gitg_diff_image_composite_get_sizing (GitgDiffImageComposite* self,
                                           gint width,
                                           gint* image_width,
                                           gint* image_height);
VALA_EXTERN GType gitg_diff_image_surface_cache_get_type (void) G_GNUC_CONST ;
VALA_EXTERN GitgDiffImageSurfaceCache* gitg_diff_image_composite_get_cache (GitgDiffImageComposite* self);
VALA_EXTERN cairo_surface_t* gitg_diff_image_surface_cache_get_old_surface (GitgDiffImageSurfaceCache* self,
                                                                GdkWindow* window);
VALA_EXTERN cairo_surface_t* gitg_diff_image_surface_cache_get_new_surface (GitgDiffImageSurfaceCache* self,
                                                                GdkWindow* window);
VALA_EXTERN gdouble gitg_diff_image_slider_get_position (GitgDiffImageSlider* self);
static void gitg_diff_image_slider_real_realize (GtkWidget* base);
VALA_EXTERN GitgDiffImageSlider* gitg_diff_image_slider_new (void);
VALA_EXTERN GitgDiffImageSlider* gitg_diff_image_slider_construct (GType object_type);
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_slider_set_position (GitgDiffImageSlider* self,
                                          gdouble value);
static GObject * gitg_diff_image_slider_constructor (GType type,
                                              guint n_construct_properties,
                                              GObjectConstructParam * construct_properties);
static void gitg_diff_image_slider_finalize (GObject * obj);
static GType gitg_diff_image_slider_get_type_once (void);
static void _vala_gitg_diff_image_slider_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec);
static void _vala_gitg_diff_image_slider_set_property (GObject * object,
                                                guint property_id,
                                                const GValue * value,
                                                GParamSpec * pspec);

static inline gpointer
gitg_diff_image_slider_get_instance_private (GitgDiffImageSlider* self)
{
	return G_STRUCT_MEMBER_P (self, GitgDiffImageSlider_private_offset);
}

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

static gboolean
gitg_diff_image_slider_real_draw (GtkWidget* base,
                                  cairo_t* cr)
{
	GitgDiffImageSlider * self;
	GdkWindow* window = NULL;
	GdkWindow* _tmp0_;
	GdkWindow* _tmp1_;
	GtkAllocation alloc = {0};
	GtkAllocation _tmp2_ = {0};
	gint image_width = 0;
	gint image_height = 0;
	GtkAllocation _tmp3_;
	gint _tmp4_ = 0;
	gint _tmp5_ = 0;
	cairo_surface_t* old_surface = NULL;
	GitgDiffImageSurfaceCache* _tmp6_;
	GitgDiffImageSurfaceCache* _tmp7_;
	GdkWindow* _tmp8_;
	cairo_surface_t* _tmp9_;
	cairo_surface_t* new_surface = NULL;
	GitgDiffImageSurfaceCache* _tmp10_;
	GitgDiffImageSurfaceCache* _tmp11_;
	GdkWindow* _tmp12_;
	cairo_surface_t* _tmp13_;
	gint x = 0;
	GtkAllocation _tmp14_;
	gint y = 0;
	gint pos = 0;
	gdouble _tmp15_;
	gdouble _tmp16_;
	cairo_surface_t* _tmp17_;
	cairo_surface_t* _tmp19_;
	gboolean result;
	self = (GitgDiffImageSlider*) base;
	g_return_val_if_fail (cr != NULL, FALSE);
	GTK_WIDGET_CLASS (gitg_diff_image_slider_parent_class)->draw ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite), cr);
	_tmp0_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	window = _tmp1_;
	gtk_widget_get_allocation ((GtkWidget*) self, &_tmp2_);
	alloc = _tmp2_;
	_tmp3_ = alloc;
	gitg_diff_image_composite_get_sizing ((GitgDiffImageComposite*) self, _tmp3_.width, &_tmp4_, &_tmp5_);
	image_width = _tmp4_;
	image_height = _tmp5_;
	_tmp6_ = gitg_diff_image_composite_get_cache ((GitgDiffImageComposite*) self);
	_tmp7_ = _tmp6_;
	_tmp8_ = window;
	_tmp9_ = gitg_diff_image_surface_cache_get_old_surface (_tmp7_, _tmp8_);
	old_surface = _tmp9_;
	_tmp10_ = gitg_diff_image_composite_get_cache ((GitgDiffImageComposite*) self);
	_tmp11_ = _tmp10_;
	_tmp12_ = window;
	_tmp13_ = gitg_diff_image_surface_cache_get_new_surface (_tmp11_, _tmp12_);
	new_surface = _tmp13_;
	_tmp14_ = alloc;
	x = (_tmp14_.width - image_width) / 2;
	y = 0;
	_tmp15_ = gitg_diff_image_slider_get_position (self);
	_tmp16_ = _tmp15_;
	pos = (gint) (image_width * _tmp16_);
	_tmp17_ = old_surface;
	if (_tmp17_ != NULL) {
		cairo_save (cr);
		{
			cairo_surface_t* _tmp18_;
			cairo_rectangle (cr, (gdouble) x, (gdouble) y, (gdouble) pos, (gdouble) image_height);
			cairo_clip (cr);
			_tmp18_ = old_surface;
			cairo_set_source_surface (cr, _tmp18_, (gdouble) x, (gdouble) y);
			cairo_paint (cr);
		}
		cairo_restore (cr);
	}
	_tmp19_ = new_surface;
	if (_tmp19_ != NULL) {
		cairo_save (cr);
		{
			cairo_surface_t* _tmp20_;
			cairo_rectangle (cr, (gdouble) (x + pos), (gdouble) y, (gdouble) (image_width - pos), (gdouble) image_height);
			cairo_clip (cr);
			_tmp20_ = new_surface;
			cairo_set_source_surface (cr, _tmp20_, (gdouble) x, (gdouble) y);
			cairo_paint (cr);
		}
		cairo_restore (cr);
	}
	result = TRUE;
	_cairo_surface_destroy0 (new_surface);
	_cairo_surface_destroy0 (old_surface);
	_g_object_unref0 (window);
	return result;
}

static void
gitg_diff_image_slider_real_realize (GtkWidget* base)
{
	GitgDiffImageSlider * self;
	self = (GitgDiffImageSlider*) base;
	GTK_WIDGET_CLASS (gitg_diff_image_slider_parent_class)->realize ((GtkWidget*) G_TYPE_CHECK_INSTANCE_CAST (self, GITG_TYPE_DIFF_IMAGE_COMPOSITE, GitgDiffImageComposite));
	gtk_widget_queue_resize ((GtkWidget*) self);
}

GitgDiffImageSlider*
gitg_diff_image_slider_construct (GType object_type)
{
	GitgDiffImageSlider * self = NULL;
	self = (GitgDiffImageSlider*) gitg_diff_image_composite_construct (object_type);
	return self;
}

GitgDiffImageSlider*
gitg_diff_image_slider_new (void)
{
	return gitg_diff_image_slider_construct (GITG_TYPE_DIFF_IMAGE_SLIDER);
}

gdouble
gitg_diff_image_slider_get_position (GitgDiffImageSlider* self)
{
	gdouble result;
	g_return_val_if_fail (self != NULL, -1.0);
	result = self->priv->d_position;
	return result;
}

void
gitg_diff_image_slider_set_position (GitgDiffImageSlider* self,
                                     gdouble value)
{
	gdouble newpos = 0.0;
	g_return_if_fail (self != NULL);
	newpos = MAX ((gdouble) 0, MIN (value, (gdouble) 1));
	if (newpos != self->priv->d_position) {
		self->priv->d_position = newpos;
		gtk_widget_queue_draw ((GtkWidget*) self);
	}
	g_object_notify_by_pspec ((GObject *) self, gitg_diff_image_slider_properties[GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY]);
}

static GObject *
gitg_diff_image_slider_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GitgDiffImageSlider * self;
	parent_class = G_OBJECT_CLASS (gitg_diff_image_slider_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSlider);
	gitg_diff_image_slider_set_position (self, 0.5);
	return obj;
}

static void
gitg_diff_image_slider_class_init (GitgDiffImageSliderClass * klass,
                                   gpointer klass_data)
{
	gitg_diff_image_slider_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GitgDiffImageSlider_private_offset);
	((GtkWidgetClass *) klass)->draw = (gboolean (*) (GtkWidget*, cairo_t*)) gitg_diff_image_slider_real_draw;
	((GtkWidgetClass *) klass)->realize = (void (*) (GtkWidget*)) gitg_diff_image_slider_real_realize;
	G_OBJECT_CLASS (klass)->get_property = _vala_gitg_diff_image_slider_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_gitg_diff_image_slider_set_property;
	G_OBJECT_CLASS (klass)->constructor = gitg_diff_image_slider_constructor;
	G_OBJECT_CLASS (klass)->finalize = gitg_diff_image_slider_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY, gitg_diff_image_slider_properties[GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY] = g_param_spec_double ("position", "position", "position", -G_MAXDOUBLE, G_MAXDOUBLE, 0.0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
gitg_diff_image_slider_instance_init (GitgDiffImageSlider * self,
                                      gpointer klass)
{
	self->priv = gitg_diff_image_slider_get_instance_private (self);
}

static void
gitg_diff_image_slider_finalize (GObject * obj)
{
	GitgDiffImageSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSlider);
	G_OBJECT_CLASS (gitg_diff_image_slider_parent_class)->finalize (obj);
}

 G_GNUC_NO_INLINE static GType
gitg_diff_image_slider_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GitgDiffImageSliderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gitg_diff_image_slider_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GitgDiffImageSlider), 0, (GInstanceInitFunc) gitg_diff_image_slider_instance_init, NULL };
	GType gitg_diff_image_slider_type_id;
	gitg_diff_image_slider_type_id = g_type_register_static (GITG_TYPE_DIFF_IMAGE_COMPOSITE, "GitgDiffImageSlider", &g_define_type_info, 0);
	GitgDiffImageSlider_private_offset = g_type_add_instance_private (gitg_diff_image_slider_type_id, sizeof (GitgDiffImageSliderPrivate));
	return gitg_diff_image_slider_type_id;
}

GType
gitg_diff_image_slider_get_type (void)
{
	static gsize gitg_diff_image_slider_type_id__once = 0;
	if (g_once_init_enter (&gitg_diff_image_slider_type_id__once)) {
		GType gitg_diff_image_slider_type_id;
		gitg_diff_image_slider_type_id = gitg_diff_image_slider_get_type_once ();
		g_once_init_leave (&gitg_diff_image_slider_type_id__once, gitg_diff_image_slider_type_id);
	}
	return gitg_diff_image_slider_type_id__once;
}

static void
_vala_gitg_diff_image_slider_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec)
{
	GitgDiffImageSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSlider);
	switch (property_id) {
		case GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY:
		g_value_set_double (value, gitg_diff_image_slider_get_position (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_gitg_diff_image_slider_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec)
{
	GitgDiffImageSlider * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GITG_TYPE_DIFF_IMAGE_SLIDER, GitgDiffImageSlider);
	switch (property_id) {
		case GITG_DIFF_IMAGE_SLIDER_POSITION_PROPERTY:
		gitg_diff_image_slider_set_position (self, g_value_get_double (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

