/* chess-view.c generated by valac 0.56.3, the Vala compiler
 * generated from chess-view.vala, do not modify */

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 *
 * Copyright (C) 2010-2014 Robert Ancell
 * Copyright (C) 2015-2016 Sahil Sareen
 *
 * This program 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
 * license.
 */

#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib.h>
#include <cairo-gobject.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <librsvg/rsvg.h>
#include <gio/gio.h>
#include "libchess.h"
#include <glib/gi18n-lib.h>

#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_CHESS_VIEW (chess_view_get_type ())
#define CHESS_VIEW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_VIEW, ChessView))
#define CHESS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_VIEW, ChessViewClass))
#define IS_CHESS_VIEW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_VIEW))
#define IS_CHESS_VIEW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_VIEW))
#define CHESS_VIEW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_VIEW, ChessViewClass))

typedef struct _ChessView ChessView;
typedef struct _ChessViewClass ChessViewClass;
typedef struct _ChessViewPrivate ChessViewPrivate;

#define TYPE_CHESS_SCENE (chess_scene_get_type ())
#define CHESS_SCENE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_SCENE, ChessScene))
#define CHESS_SCENE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_SCENE, ChessSceneClass))
#define IS_CHESS_SCENE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_SCENE))
#define IS_CHESS_SCENE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_SCENE))
#define CHESS_SCENE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_SCENE, ChessSceneClass))

typedef struct _ChessScene ChessScene;
typedef struct _ChessSceneClass ChessSceneClass;
enum  {
	CHESS_VIEW_0_PROPERTY,
	CHESS_VIEW_SCENE_PROPERTY,
	CHESS_VIEW_NUM_PROPERTIES
};
static GParamSpec* chess_view_properties[CHESS_VIEW_NUM_PROPERTIES];
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _cairo_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_destroy (var), NULL)))
typedef struct _ChessScenePrivate ChessScenePrivate;

#define TYPE_CHESS_MODEL (chess_model_get_type ())
#define CHESS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHESS_MODEL, ChessModel))
#define CHESS_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHESS_MODEL, ChessModelClass))
#define IS_CHESS_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHESS_MODEL))
#define IS_CHESS_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHESS_MODEL))
#define CHESS_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHESS_MODEL, ChessModelClass))

typedef struct _ChessModel ChessModel;
typedef struct _ChessModelClass ChessModelClass;
typedef struct _ChessModelPrivate ChessModelPrivate;

struct _ChessView {
	GtkDrawingArea parent_instance;
	ChessViewPrivate * priv;
};

struct _ChessViewClass {
	GtkDrawingAreaClass parent_class;
};

struct _ChessViewPrivate {
	gint border;
	gint square_size;
	gint selected_square_size;
	cairo_surface_t* model_surface;
	cairo_surface_t* selected_model_surface;
	gchar* loaded_theme_name;
	ChessScene* _scene;
};

struct _ChessScene {
	GObject parent_instance;
	ChessScenePrivate * priv;
	GList* pieces;
	gboolean animating;
	gint selected_rank;
	gint selected_file;
};

struct _ChessSceneClass {
	GObjectClass parent_class;
};

struct _ChessModel {
	GObject parent_instance;
	ChessModelPrivate * priv;
	ChessPiece* piece;
	gdouble x;
	gdouble y;
	gdouble target_x;
	gdouble target_y;
	gboolean under_threat;
	gboolean is_selected;
};

struct _ChessModelClass {
	GObjectClass parent_class;
};

static gint ChessView_private_offset;
static gpointer chess_view_parent_class = NULL;

VALA_EXTERN GType chess_view_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ChessView, g_object_unref)
VALA_EXTERN GType chess_scene_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ChessScene, g_object_unref)
VALA_EXTERN ChessView* chess_view_new (ChessScene* scene);
VALA_EXTERN ChessView* chess_view_construct (GType object_type,
                                 ChessScene* scene);
static void chess_view_on_click (ChessView* self,
                          GtkGestureClick* _click_controller,
                          gint n_press,
                          gdouble event_x,
                          gdouble event_y);
static void _chess_view_on_click_gtk_gesture_click_pressed (GtkGestureClick* _sender,
                                                     gint n_press,
                                                     gdouble x,
                                                     gdouble y,
                                                     gpointer self);
VALA_EXTERN void chess_view_draw (ChessView* self,
                      GtkDrawingArea* _self_,
                      cairo_t* c,
                      gint width,
                      gint height);
static void _chess_view_draw_gtk_drawing_area_draw_func (GtkDrawingArea* drawing_area,
                                                  cairo_t* cr,
                                                  gint width,
                                                  gint height,
                                                  gpointer self);
static void chess_view_real_resize (GtkDrawingArea* base,
                             gint width,
                             gint height);
static void chess_view_render_piece (ChessView* self,
                              cairo_t* c1,
                              cairo_t* c2,
                              const gchar* name,
                              gint offset);
VALA_EXTERN ChessScene* chess_view_get_scene (ChessView* self);
VALA_EXTERN const gchar* chess_scene_get_theme_name (ChessScene* self);
static void chess_view_load_theme (ChessView* self,
                            cairo_t* c);
VALA_EXTERN gdouble chess_scene_get_board_angle (ChessScene* self);
static gdouble chess_view_get_border_size (ChessView* self);
VALA_EXTERN gboolean chess_scene_get_show_numbering (ChessScene* self);
VALA_EXTERN ChessGame* chess_scene_get_game (ChessScene* self);
VALA_EXTERN void chess_view_draw_paused_overlay (ChessView* self,
                                     cairo_t* c);
VALA_EXTERN GType chess_model_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ChessModel, g_object_unref)
VALA_EXTERN gboolean chess_scene_get_show_move_hints (ChessScene* self);
static void chess_view_draw_piece (ChessView* self,
                            cairo_t* c,
                            cairo_surface_t* surface,
                            gint size,
                            ChessPiece* piece,
                            gdouble alpha);
VALA_EXTERN gboolean chess_scene_can_move (ChessScene* self,
                               gint rank,
                               gint file);
VALA_EXTERN ChessPiece* chess_scene_get_selected_piece (ChessScene* self);
VALA_EXTERN void chess_scene_select_square (ChessScene* self,
                                gint file,
                                gint rank);
static void chess_view_scene_changed_cb (ChessView* self,
                                  ChessScene* scene);
VALA_EXTERN void chess_view_set_scene (ChessView* self,
                           ChessScene* value);
static void _chess_view_scene_changed_cb_chess_scene_changed (ChessScene* _sender,
                                                       gpointer self);
static void chess_view_finalize (GObject * obj);
static GType chess_view_get_type_once (void);
static void _vala_chess_view_get_property (GObject * object,
                                    guint property_id,
                                    GValue * value,
                                    GParamSpec * pspec);
static void _vala_chess_view_set_property (GObject * object,
                                    guint property_id,
                                    const GValue * value,
                                    GParamSpec * pspec);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);

static inline gpointer
chess_view_get_instance_private (ChessView* self)
{
	return G_STRUCT_MEMBER_P (self, ChessView_private_offset);
}

static void
_chess_view_on_click_gtk_gesture_click_pressed (GtkGestureClick* _sender,
                                                gint n_press,
                                                gdouble x,
                                                gdouble y,
                                                gpointer self)
{
	chess_view_on_click ((ChessView*) self, _sender, n_press, x, y);
}

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

static void
_chess_view_draw_gtk_drawing_area_draw_func (GtkDrawingArea* drawing_area,
                                             cairo_t* cr,
                                             gint width,
                                             gint height,
                                             gpointer self)
{
	chess_view_draw ((ChessView*) self, drawing_area, cr, width, height);
}

ChessView*
chess_view_construct (GType object_type,
                      ChessScene* scene)
{
	ChessView * self = NULL;
	GtkGestureClick* click_controller = NULL;
	GtkGestureClick* _tmp0_;
	GtkEventController* _tmp1_;
	g_return_val_if_fail (scene != NULL, NULL);
	self = (ChessView*) g_object_new (object_type, "scene", scene, NULL);
	_tmp0_ = (GtkGestureClick*) gtk_gesture_click_new ();
	click_controller = _tmp0_;
	g_signal_connect_object (click_controller, "pressed", (GCallback) _chess_view_on_click_gtk_gesture_click_pressed, self, 0);
	_tmp1_ = _g_object_ref0 ((GtkEventController*) click_controller);
	gtk_widget_add_controller ((GtkWidget*) self, _tmp1_);
	gtk_drawing_area_set_draw_func ((GtkDrawingArea*) self, _chess_view_draw_gtk_drawing_area_draw_func, g_object_ref (self), g_object_unref);
	gtk_widget_set_hexpand ((GtkWidget*) self, TRUE);
	gtk_widget_set_vexpand ((GtkWidget*) self, TRUE);
	gtk_widget_set_size_request ((GtkWidget*) self, 100, 100);
	_g_object_unref0 (click_controller);
	return self;
}

ChessView*
chess_view_new (ChessScene* scene)
{
	return chess_view_construct (TYPE_CHESS_VIEW, scene);
}

static void
chess_view_real_resize (GtkDrawingArea* base,
                        gint width,
                        gint height)
{
	ChessView * self;
	gint short_edge = 0;
	gdouble extra = 0.0;
	self = (ChessView*) base;
	short_edge = MIN (width, height);
	self->priv->square_size = (gint) floor ((short_edge - (2 * self->priv->border)) / 9.0);
	extra = self->priv->square_size * 0.1;
	if (extra < ((gdouble) 3)) {
		extra = (gdouble) 3;
	}
	self->priv->selected_square_size = self->priv->square_size + (2 * ((gint) (extra + 0.5)));
}

static void
chess_view_render_piece (ChessView* self,
                         cairo_t* c1,
                         cairo_t* c2,
                         const gchar* name,
                         gint offset)
{
	RsvgHandle* handle = NULL;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (c1 != NULL);
	g_return_if_fail (c2 != NULL);
	g_return_if_fail (name != NULL);
	{
		GInputStream* stream = NULL;
		ChessScene* _tmp0_;
		ChessScene* _tmp1_;
		const gchar* _tmp2_;
		const gchar* _tmp3_;
		gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		gchar* _tmp7_;
		GInputStream* _tmp8_;
		GInputStream* _tmp9_;
		RsvgHandle* _tmp10_ = NULL;
		GInputStream* _tmp11_;
		RsvgHandle* _tmp12_;
		RsvgHandle* _tmp13_;
		_tmp0_ = chess_view_get_scene (self);
		_tmp1_ = _tmp0_;
		_tmp2_ = chess_scene_get_theme_name (_tmp1_);
		_tmp3_ = _tmp2_;
		_tmp4_ = g_strconcat (name, ".svg", NULL);
		_tmp5_ = _tmp4_;
		_tmp6_ = g_build_path ("/", "/org/gnome/Chess/pieces", _tmp3_, _tmp5_, NULL);
		_tmp7_ = _tmp6_;
		_tmp8_ = g_resources_open_stream (_tmp7_, G_RESOURCE_LOOKUP_FLAGS_NONE, &_inner_error0_);
		_tmp9_ = _tmp8_;
		_g_free0 (_tmp7_);
		_g_free0 (_tmp5_);
		stream = _tmp9_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp11_ = stream;
		_tmp12_ = rsvg_handle_new_from_stream_sync (_tmp11_, NULL, RSVG_HANDLE_FLAGS_NONE, NULL, &_inner_error0_);
		_tmp10_ = _tmp12_;
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_object_unref0 (stream);
			goto __catch0_g_error;
		}
		_tmp13_ = _tmp10_;
		_tmp10_ = NULL;
		_g_object_unref0 (handle);
		handle = _tmp13_;
		_g_object_unref0 (_tmp10_);
		_g_object_unref0 (stream);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* e = NULL;
		GError* _tmp14_;
		const gchar* _tmp15_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp14_ = e;
		_tmp15_ = _tmp14_->message;
		g_warning ("chess-view.vala:75: Failed to load piece SVG: %s", _tmp15_);
		_g_error_free0 (e);
		_g_object_unref0 (handle);
		return;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (handle);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	{
		RsvgHandle* _tmp16_;
		RsvgRectangle _tmp17_ = {0};
		RsvgHandle* _tmp18_;
		RsvgRectangle _tmp19_ = {0};
		_tmp16_ = handle;
		memset (&_tmp17_, 0, sizeof (RsvgRectangle));
		_tmp17_.height = (gdouble) self->priv->square_size;
		_tmp17_.width = (gdouble) self->priv->square_size;
		_tmp17_.x = (gdouble) (self->priv->square_size * offset);
		_tmp17_.y = (gdouble) 0;
		rsvg_handle_render_document (_tmp16_, c1, &_tmp17_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch1_g_error;
		}
		_tmp18_ = handle;
		memset (&_tmp19_, 0, sizeof (RsvgRectangle));
		_tmp19_.height = (gdouble) self->priv->selected_square_size;
		_tmp19_.width = (gdouble) self->priv->selected_square_size;
		_tmp19_.x = (gdouble) (self->priv->selected_square_size * offset);
		_tmp19_.y = (gdouble) 0;
		rsvg_handle_render_document (_tmp18_, c2, &_tmp19_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch1_g_error;
		}
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError* e = NULL;
		GError* _tmp20_;
		const gchar* _tmp21_;
		e = _inner_error0_;
		_inner_error0_ = NULL;
		_tmp20_ = e;
		_tmp21_ = _tmp20_->message;
		g_warning ("chess-view.vala:86: Failed to render piece SVG: %s", _tmp21_);
		_g_error_free0 (e);
	}
	__finally1:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (handle);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (handle);
}

static void
chess_view_load_theme (ChessView* self,
                       cairo_t* c)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	ChessScene* _tmp2_;
	ChessScene* _tmp3_;
	const gchar* _tmp4_;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	cairo_surface_t* _tmp9_;
	cairo_surface_t* _tmp10_;
	cairo_surface_t* _tmp11_;
	cairo_t* c1 = NULL;
	cairo_surface_t* _tmp12_;
	cairo_t* _tmp13_;
	cairo_t* c2 = NULL;
	cairo_surface_t* _tmp14_;
	cairo_t* _tmp15_;
	cairo_t* _tmp16_;
	cairo_t* _tmp17_;
	cairo_t* _tmp18_;
	cairo_t* _tmp19_;
	cairo_t* _tmp20_;
	cairo_t* _tmp21_;
	cairo_t* _tmp22_;
	cairo_t* _tmp23_;
	cairo_t* _tmp24_;
	cairo_t* _tmp25_;
	cairo_t* _tmp26_;
	cairo_t* _tmp27_;
	cairo_t* _tmp28_;
	cairo_t* _tmp29_;
	cairo_t* _tmp30_;
	cairo_t* _tmp31_;
	cairo_t* _tmp32_;
	cairo_t* _tmp33_;
	cairo_t* _tmp34_;
	cairo_t* _tmp35_;
	cairo_t* _tmp36_;
	cairo_t* _tmp37_;
	cairo_t* _tmp38_;
	cairo_t* _tmp39_;
	ChessScene* _tmp40_;
	ChessScene* _tmp41_;
	const gchar* _tmp42_;
	const gchar* _tmp43_;
	gchar* _tmp44_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (c != NULL);
	_tmp2_ = chess_view_get_scene (self);
	_tmp3_ = _tmp2_;
	_tmp4_ = chess_scene_get_theme_name (_tmp3_);
	_tmp5_ = _tmp4_;
	_tmp6_ = self->priv->loaded_theme_name;
	if (g_strcmp0 (_tmp5_, _tmp6_) == 0) {
		cairo_surface_t* _tmp7_;
		_tmp7_ = self->priv->model_surface;
		_tmp1_ = _tmp7_ != NULL;
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		cairo_surface_t* _tmp8_;
		_tmp8_ = self->priv->model_surface;
		_tmp0_ = self->priv->square_size == cairo_image_surface_get_height (_tmp8_);
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		return;
	}
	_tmp9_ = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 12 * self->priv->square_size, self->priv->square_size);
	_cairo_surface_destroy0 (self->priv->model_surface);
	self->priv->model_surface = _tmp9_;
	_tmp10_ = cairo_get_target (c);
	_tmp11_ = cairo_surface_create_similar (_tmp10_, CAIRO_CONTENT_COLOR_ALPHA, 12 * self->priv->selected_square_size, self->priv->selected_square_size);
	_cairo_surface_destroy0 (self->priv->selected_model_surface);
	self->priv->selected_model_surface = _tmp11_;
	_tmp12_ = self->priv->model_surface;
	_tmp13_ = cairo_create (_tmp12_);
	c1 = _tmp13_;
	_tmp14_ = self->priv->selected_model_surface;
	_tmp15_ = cairo_create (_tmp14_);
	c2 = _tmp15_;
	_tmp16_ = c1;
	_tmp17_ = c2;
	chess_view_render_piece (self, _tmp16_, _tmp17_, "whitePawn", 0);
	_tmp18_ = c1;
	_tmp19_ = c2;
	chess_view_render_piece (self, _tmp18_, _tmp19_, "whiteRook", 1);
	_tmp20_ = c1;
	_tmp21_ = c2;
	chess_view_render_piece (self, _tmp20_, _tmp21_, "whiteKnight", 2);
	_tmp22_ = c1;
	_tmp23_ = c2;
	chess_view_render_piece (self, _tmp22_, _tmp23_, "whiteBishop", 3);
	_tmp24_ = c1;
	_tmp25_ = c2;
	chess_view_render_piece (self, _tmp24_, _tmp25_, "whiteQueen", 4);
	_tmp26_ = c1;
	_tmp27_ = c2;
	chess_view_render_piece (self, _tmp26_, _tmp27_, "whiteKing", 5);
	_tmp28_ = c1;
	_tmp29_ = c2;
	chess_view_render_piece (self, _tmp28_, _tmp29_, "blackPawn", 6);
	_tmp30_ = c1;
	_tmp31_ = c2;
	chess_view_render_piece (self, _tmp30_, _tmp31_, "blackRook", 7);
	_tmp32_ = c1;
	_tmp33_ = c2;
	chess_view_render_piece (self, _tmp32_, _tmp33_, "blackKnight", 8);
	_tmp34_ = c1;
	_tmp35_ = c2;
	chess_view_render_piece (self, _tmp34_, _tmp35_, "blackBishop", 9);
	_tmp36_ = c1;
	_tmp37_ = c2;
	chess_view_render_piece (self, _tmp36_, _tmp37_, "blackQueen", 10);
	_tmp38_ = c1;
	_tmp39_ = c2;
	chess_view_render_piece (self, _tmp38_, _tmp39_, "blackKing", 11);
	_tmp40_ = chess_view_get_scene (self);
	_tmp41_ = _tmp40_;
	_tmp42_ = chess_scene_get_theme_name (_tmp41_);
	_tmp43_ = _tmp42_;
	_tmp44_ = g_strdup (_tmp43_);
	_g_free0 (self->priv->loaded_theme_name);
	self->priv->loaded_theme_name = _tmp44_;
	_cairo_destroy0 (c2);
	_cairo_destroy0 (c1);
}

void
chess_view_draw (ChessView* self,
                 GtkDrawingArea* _self_,
                 cairo_t* c,
                 gint width,
                 gint height)
{
	ChessScene* _tmp0_;
	ChessScene* _tmp1_;
	gdouble _tmp2_;
	gdouble _tmp3_;
	gint board_size = 0;
	gdouble _tmp4_;
	gdouble _tmp5_;
	ChessScene* _tmp10_;
	ChessScene* _tmp11_;
	gboolean _tmp12_;
	gboolean _tmp13_;
	ChessScene* _tmp92_;
	ChessScene* _tmp93_;
	ChessGame* _tmp94_;
	ChessGame* _tmp95_;
	gboolean _tmp96_;
	gboolean _tmp97_;
	ChessScene* _tmp102_;
	ChessScene* _tmp103_;
	GList* _tmp104_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (_self_ != NULL);
	g_return_if_fail (c != NULL);
	chess_view_load_theme (self, c);
	cairo_translate (c, (gdouble) (gtk_widget_get_allocated_width ((GtkWidget*) self) / 2), (gdouble) (gtk_widget_get_allocated_height ((GtkWidget*) self) / 2));
	_tmp0_ = chess_view_get_scene (self);
	_tmp1_ = _tmp0_;
	_tmp2_ = chess_scene_get_board_angle (_tmp1_);
	_tmp3_ = _tmp2_;
	cairo_rotate (c, (G_PI * _tmp3_) / 180.0);
	_tmp4_ = chess_view_get_border_size (self);
	_tmp5_ = _tmp4_;
	board_size = (gint) ceil ((self->priv->square_size * 4) + _tmp5_);
	cairo_set_source_rgb (c, 0x2e / 255.0, 0x34 / 255.0, 0x36 / 255.0);
	cairo_rectangle (c, (gdouble) (-board_size), (gdouble) (-board_size), (gdouble) (board_size * 2), (gdouble) (board_size * 2));
	cairo_fill (c);
	{
		gint file = 0;
		file = 0;
		{
			gboolean _tmp6_ = FALSE;
			_tmp6_ = TRUE;
			while (TRUE) {
				if (!_tmp6_) {
					gint _tmp7_;
					_tmp7_ = file;
					file = _tmp7_ + 1;
				}
				_tmp6_ = FALSE;
				if (!(file < 8)) {
					break;
				}
				{
					gint rank = 0;
					rank = 0;
					{
						gboolean _tmp8_ = FALSE;
						_tmp8_ = TRUE;
						while (TRUE) {
							gint x = 0;
							gint y = 0;
							if (!_tmp8_) {
								gint _tmp9_;
								_tmp9_ = rank;
								rank = _tmp9_ + 1;
							}
							_tmp8_ = FALSE;
							if (!(rank < 8)) {
								break;
							}
							x = (gint) ((file - 4) * self->priv->square_size);
							y = (gint) ((3 - rank) * self->priv->square_size);
							cairo_rectangle (c, (gdouble) x, (gdouble) y, (gdouble) self->priv->square_size, (gdouble) self->priv->square_size);
							if (((file + rank) % 2) == 0) {
								cairo_set_source_rgb (c, 0xba / 255.0, 0xbd / 255.0, 0xb6 / 255.0);
							} else {
								cairo_set_source_rgb (c, 0xee / 255.0, 0xee / 255.0, 0xec / 255.0);
							}
							cairo_fill (c);
						}
					}
				}
			}
		}
	}
	_tmp10_ = chess_view_get_scene (self);
	_tmp11_ = _tmp10_;
	_tmp12_ = chess_scene_get_show_numbering (_tmp11_);
	_tmp13_ = _tmp12_;
	if (_tmp13_) {
		gdouble _tmp14_;
		gdouble _tmp15_;
		cairo_text_extents_t extents = {0};
		cairo_text_extents_t _tmp16_ = {0};
		gdouble y_offset = 0.0;
		cairo_text_extents_t _tmp17_;
		cairo_text_extents_t _tmp18_;
		cairo_text_extents_t _tmp19_;
		gdouble top = 0.0;
		gdouble bottom = 0.0;
		gdouble _tmp20_;
		gdouble _tmp21_;
		gdouble file_offset = 0.0;
		gdouble rank_offset = 0.0;
		gchar** files = NULL;
		gint files_length1 = 0;
		gint _files_size_ = 0;
		gchar** ranks = NULL;
		gint ranks_length1 = 0;
		gint _ranks_size_ = 0;
		cairo_matrix_t matrix = {0};
		cairo_matrix_t _tmp22_ = {0};
		ChessScene* _tmp23_;
		ChessScene* _tmp24_;
		gdouble _tmp25_;
		gdouble _tmp26_;
		cairo_matrix_t _tmp63_;
		cairo_set_source_rgb (c, 0x88 / 255.0, 0x8a / 255.0, 0x85 / 255.0);
		_tmp14_ = chess_view_get_border_size (self);
		_tmp15_ = _tmp14_;
		cairo_set_font_size (c, _tmp15_ * 0.6);
		cairo_select_font_face (c, "sans-serif", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
		cairo_text_extents (c, "abcdefgh", &_tmp16_);
		extents = _tmp16_;
		_tmp17_ = extents;
		_tmp18_ = extents;
		_tmp19_ = extents;
		y_offset = ((((self->priv->square_size / 2) - _tmp17_.height) / 2) + _tmp18_.height) + _tmp19_.y_bearing;
		top = -((self->priv->square_size * 4) + y_offset);
		_tmp20_ = chess_view_get_border_size (self);
		_tmp21_ = _tmp20_;
		bottom = ((self->priv->square_size * 4) + _tmp21_) - y_offset;
		file_offset = -(self->priv->square_size * 3.5);
		rank_offset = -(self->priv->square_size * 3.5);
		cairo_get_matrix (c, &_tmp22_);
		matrix = _tmp22_;
		_tmp23_ = chess_view_get_scene (self);
		_tmp24_ = _tmp23_;
		_tmp25_ = chess_scene_get_board_angle (_tmp24_);
		_tmp26_ = _tmp25_;
		if (_tmp26_ == 180.0) {
			gchar* _tmp27_;
			gchar* _tmp28_;
			gchar* _tmp29_;
			gchar* _tmp30_;
			gchar* _tmp31_;
			gchar* _tmp32_;
			gchar* _tmp33_;
			gchar* _tmp34_;
			gchar** _tmp35_;
			gchar* _tmp36_;
			gchar* _tmp37_;
			gchar* _tmp38_;
			gchar* _tmp39_;
			gchar* _tmp40_;
			gchar* _tmp41_;
			gchar* _tmp42_;
			gchar* _tmp43_;
			gchar** _tmp44_;
			_tmp27_ = g_strdup ("h");
			_tmp28_ = g_strdup ("g");
			_tmp29_ = g_strdup ("f");
			_tmp30_ = g_strdup ("e");
			_tmp31_ = g_strdup ("d");
			_tmp32_ = g_strdup ("c");
			_tmp33_ = g_strdup ("b");
			_tmp34_ = g_strdup ("a");
			_tmp35_ = g_new0 (gchar*, 8 + 1);
			_tmp35_[0] = _tmp27_;
			_tmp35_[1] = _tmp28_;
			_tmp35_[2] = _tmp29_;
			_tmp35_[3] = _tmp30_;
			_tmp35_[4] = _tmp31_;
			_tmp35_[5] = _tmp32_;
			_tmp35_[6] = _tmp33_;
			_tmp35_[7] = _tmp34_;
			files = (_vala_array_free (files, files_length1, (GDestroyNotify) g_free), NULL);
			files = _tmp35_;
			files_length1 = 8;
			_files_size_ = files_length1;
			_tmp36_ = g_strdup ("1");
			_tmp37_ = g_strdup ("2");
			_tmp38_ = g_strdup ("3");
			_tmp39_ = g_strdup ("4");
			_tmp40_ = g_strdup ("5");
			_tmp41_ = g_strdup ("6");
			_tmp42_ = g_strdup ("7");
			_tmp43_ = g_strdup ("8");
			_tmp44_ = g_new0 (gchar*, 8 + 1);
			_tmp44_[0] = _tmp36_;
			_tmp44_[1] = _tmp37_;
			_tmp44_[2] = _tmp38_;
			_tmp44_[3] = _tmp39_;
			_tmp44_[4] = _tmp40_;
			_tmp44_[5] = _tmp41_;
			_tmp44_[6] = _tmp42_;
			_tmp44_[7] = _tmp43_;
			ranks = (_vala_array_free (ranks, ranks_length1, (GDestroyNotify) g_free), NULL);
			ranks = _tmp44_;
			ranks_length1 = 8;
			_ranks_size_ = ranks_length1;
			cairo_matrix_scale (&matrix, (gdouble) -1, (gdouble) -1);
		} else {
			gchar* _tmp45_;
			gchar* _tmp46_;
			gchar* _tmp47_;
			gchar* _tmp48_;
			gchar* _tmp49_;
			gchar* _tmp50_;
			gchar* _tmp51_;
			gchar* _tmp52_;
			gchar** _tmp53_;
			gchar* _tmp54_;
			gchar* _tmp55_;
			gchar* _tmp56_;
			gchar* _tmp57_;
			gchar* _tmp58_;
			gchar* _tmp59_;
			gchar* _tmp60_;
			gchar* _tmp61_;
			gchar** _tmp62_;
			_tmp45_ = g_strdup ("a");
			_tmp46_ = g_strdup ("b");
			_tmp47_ = g_strdup ("c");
			_tmp48_ = g_strdup ("d");
			_tmp49_ = g_strdup ("e");
			_tmp50_ = g_strdup ("f");
			_tmp51_ = g_strdup ("g");
			_tmp52_ = g_strdup ("h");
			_tmp53_ = g_new0 (gchar*, 8 + 1);
			_tmp53_[0] = _tmp45_;
			_tmp53_[1] = _tmp46_;
			_tmp53_[2] = _tmp47_;
			_tmp53_[3] = _tmp48_;
			_tmp53_[4] = _tmp49_;
			_tmp53_[5] = _tmp50_;
			_tmp53_[6] = _tmp51_;
			_tmp53_[7] = _tmp52_;
			files = (_vala_array_free (files, files_length1, (GDestroyNotify) g_free), NULL);
			files = _tmp53_;
			files_length1 = 8;
			_files_size_ = files_length1;
			_tmp54_ = g_strdup ("8");
			_tmp55_ = g_strdup ("7");
			_tmp56_ = g_strdup ("6");
			_tmp57_ = g_strdup ("5");
			_tmp58_ = g_strdup ("4");
			_tmp59_ = g_strdup ("3");
			_tmp60_ = g_strdup ("2");
			_tmp61_ = g_strdup ("1");
			_tmp62_ = g_new0 (gchar*, 8 + 1);
			_tmp62_[0] = _tmp54_;
			_tmp62_[1] = _tmp55_;
			_tmp62_[2] = _tmp56_;
			_tmp62_[3] = _tmp57_;
			_tmp62_[4] = _tmp58_;
			_tmp62_[5] = _tmp59_;
			_tmp62_[6] = _tmp60_;
			_tmp62_[7] = _tmp61_;
			ranks = (_vala_array_free (ranks, ranks_length1, (GDestroyNotify) g_free), NULL);
			ranks = _tmp62_;
			ranks_length1 = 8;
			_ranks_size_ = ranks_length1;
		}
		cairo_save (c);
		_tmp63_ = matrix;
		cairo_set_matrix (c, &_tmp63_);
		{
			gint i = 0;
			i = 0;
			{
				gboolean _tmp64_ = FALSE;
				_tmp64_ = TRUE;
				while (TRUE) {
					gchar** _tmp66_;
					gint _tmp66__length1;
					const gchar* _tmp67_;
					cairo_text_extents_t _tmp68_ = {0};
					cairo_text_extents_t _tmp69_;
					gchar** _tmp70_;
					gint _tmp70__length1;
					const gchar* _tmp71_;
					cairo_text_extents_t _tmp72_;
					gchar** _tmp73_;
					gint _tmp73__length1;
					const gchar* _tmp74_;
					gchar** _tmp75_;
					gint _tmp75__length1;
					const gchar* _tmp76_;
					cairo_text_extents_t _tmp77_ = {0};
					cairo_text_extents_t _tmp78_;
					cairo_text_extents_t _tmp79_;
					gdouble _tmp80_;
					gdouble _tmp81_;
					gdouble _tmp82_;
					gdouble _tmp83_;
					cairo_text_extents_t _tmp84_;
					gchar** _tmp85_;
					gint _tmp85__length1;
					const gchar* _tmp86_;
					gdouble _tmp87_;
					gdouble _tmp88_;
					cairo_text_extents_t _tmp89_;
					gchar** _tmp90_;
					gint _tmp90__length1;
					const gchar* _tmp91_;
					if (!_tmp64_) {
						gint _tmp65_;
						_tmp65_ = i;
						i = _tmp65_ + 1;
					}
					_tmp64_ = FALSE;
					if (!(i < 8)) {
						break;
					}
					_tmp66_ = ranks;
					_tmp66__length1 = ranks_length1;
					_tmp67_ = _tmp66_[i];
					cairo_text_extents (c, _tmp67_, &_tmp68_);
					extents = _tmp68_;
					cairo_save (c);
					_tmp69_ = extents;
					cairo_move_to (c, file_offset - (_tmp69_.width / 2), top);
					_tmp70_ = files;
					_tmp70__length1 = files_length1;
					_tmp71_ = _tmp70_[i];
					cairo_show_text (c, _tmp71_);
					cairo_restore (c);
					cairo_save (c);
					_tmp72_ = extents;
					cairo_move_to (c, file_offset - (_tmp72_.width / 2), bottom);
					_tmp73_ = files;
					_tmp73__length1 = files_length1;
					_tmp74_ = _tmp73_[i];
					cairo_show_text (c, _tmp74_);
					cairo_restore (c);
					_tmp75_ = ranks;
					_tmp75__length1 = ranks_length1;
					_tmp76_ = _tmp75_[i];
					cairo_text_extents (c, _tmp76_, &_tmp77_);
					extents = _tmp77_;
					_tmp78_ = extents;
					_tmp79_ = extents;
					y_offset = -(_tmp78_.y_bearing + (_tmp79_.height / 2));
					cairo_save (c);
					_tmp80_ = chess_view_get_border_size (self);
					_tmp81_ = _tmp80_;
					_tmp82_ = chess_view_get_border_size (self);
					_tmp83_ = _tmp82_;
					_tmp84_ = extents;
					cairo_move_to (c, -(((((gdouble) self->priv->square_size) * 4) + _tmp81_) - ((_tmp83_ - _tmp84_.width) / 2)), rank_offset + y_offset);
					_tmp85_ = ranks;
					_tmp85__length1 = ranks_length1;
					_tmp86_ = _tmp85_[i];
					cairo_show_text (c, _tmp86_);
					cairo_restore (c);
					cairo_save (c);
					_tmp87_ = chess_view_get_border_size (self);
					_tmp88_ = _tmp87_;
					_tmp89_ = extents;
					cairo_move_to (c, (((gdouble) self->priv->square_size) * 4) + ((_tmp88_ - _tmp89_.width) / 2), rank_offset + y_offset);
					_tmp90_ = ranks;
					_tmp90__length1 = ranks_length1;
					_tmp91_ = _tmp90_[i];
					cairo_show_text (c, _tmp91_);
					cairo_restore (c);
					file_offset += (gdouble) self->priv->square_size;
					rank_offset += (gdouble) self->priv->square_size;
				}
			}
		}
		cairo_restore (c);
		ranks = (_vala_array_free (ranks, ranks_length1, (GDestroyNotify) g_free), NULL);
		files = (_vala_array_free (files, files_length1, (GDestroyNotify) g_free), NULL);
	}
	_tmp92_ = chess_view_get_scene (self);
	_tmp93_ = _tmp92_;
	_tmp94_ = chess_scene_get_game (_tmp93_);
	_tmp95_ = _tmp94_;
	_tmp96_ = chess_game_get_should_show_paused_overlay (_tmp95_);
	_tmp97_ = _tmp96_;
	if (_tmp97_) {
		ChessScene* _tmp98_;
		ChessScene* _tmp99_;
		gdouble _tmp100_;
		gdouble _tmp101_;
		_tmp98_ = chess_view_get_scene (self);
		_tmp99_ = _tmp98_;
		_tmp100_ = chess_scene_get_board_angle (_tmp99_);
		_tmp101_ = _tmp100_;
		cairo_rotate (c, (G_PI * _tmp101_) / 180.0);
		chess_view_draw_paused_overlay (self, c);
		return;
	}
	_tmp102_ = chess_view_get_scene (self);
	_tmp103_ = _tmp102_;
	_tmp104_ = _tmp103_->pieces;
	{
		GList* model_collection = NULL;
		GList* model_it = NULL;
		model_collection = _tmp104_;
		for (model_it = model_collection; model_it != NULL; model_it = model_it->next) {
			ChessModel* _tmp105_;
			ChessModel* model = NULL;
			_tmp105_ = _g_object_ref0 ((ChessModel*) model_it->data);
			model = _tmp105_;
			{
				ChessModel* _tmp106_;
				ChessModel* _tmp107_;
				ChessScene* _tmp108_;
				ChessScene* _tmp109_;
				gdouble _tmp110_;
				gdouble _tmp111_;
				cairo_surface_t* _tmp112_ = NULL;
				ChessModel* _tmp113_;
				gint _tmp116_ = 0;
				ChessModel* _tmp117_;
				gdouble _tmp118_ = 0.0;
				gboolean _tmp119_ = FALSE;
				ChessModel* _tmp120_;
				ChessModel* _tmp125_;
				ChessPiece* _tmp126_;
				cairo_save (c);
				_tmp106_ = model;
				_tmp107_ = model;
				cairo_translate (c, (_tmp106_->x - 4) * self->priv->square_size, (3 - _tmp107_->y) * self->priv->square_size);
				cairo_translate (c, (gdouble) (self->priv->square_size / 2), (gdouble) (self->priv->square_size / 2));
				_tmp108_ = chess_view_get_scene (self);
				_tmp109_ = _tmp108_;
				_tmp110_ = chess_scene_get_board_angle (_tmp109_);
				_tmp111_ = _tmp110_;
				cairo_rotate (c, ((-G_PI) * _tmp111_) / 180.0);
				_tmp113_ = model;
				if (_tmp113_->is_selected) {
					cairo_surface_t* _tmp114_;
					_tmp114_ = self->priv->selected_model_surface;
					_tmp112_ = _tmp114_;
				} else {
					cairo_surface_t* _tmp115_;
					_tmp115_ = self->priv->model_surface;
					_tmp112_ = _tmp115_;
				}
				_tmp117_ = model;
				if (_tmp117_->is_selected) {
					_tmp116_ = self->priv->selected_square_size;
				} else {
					_tmp116_ = self->priv->square_size;
				}
				_tmp120_ = model;
				if (_tmp120_->under_threat) {
					ChessScene* _tmp121_;
					ChessScene* _tmp122_;
					gboolean _tmp123_;
					gboolean _tmp124_;
					_tmp121_ = chess_view_get_scene (self);
					_tmp122_ = _tmp121_;
					_tmp123_ = chess_scene_get_show_move_hints (_tmp122_);
					_tmp124_ = _tmp123_;
					_tmp119_ = _tmp124_;
				} else {
					_tmp119_ = FALSE;
				}
				if (_tmp119_) {
					_tmp118_ = 0.8;
				} else {
					_tmp118_ = 1.0;
				}
				_tmp125_ = model;
				_tmp126_ = _tmp125_->piece;
				chess_view_draw_piece (self, c, _tmp112_, _tmp116_, _tmp126_, _tmp118_);
				cairo_restore (c);
				_g_object_unref0 (model);
			}
		}
	}
	{
		gint rank = 0;
		rank = 0;
		{
			gboolean _tmp127_ = FALSE;
			_tmp127_ = TRUE;
			while (TRUE) {
				if (!_tmp127_) {
					gint _tmp128_;
					_tmp128_ = rank;
					rank = _tmp128_ + 1;
				}
				_tmp127_ = FALSE;
				if (!(rank < 8)) {
					break;
				}
				{
					gint file = 0;
					file = 0;
					{
						gboolean _tmp129_ = FALSE;
						_tmp129_ = TRUE;
						while (TRUE) {
							gboolean _tmp131_ = FALSE;
							ChessScene* _tmp132_;
							ChessScene* _tmp133_;
							gboolean _tmp134_;
							gboolean _tmp135_;
							if (!_tmp129_) {
								gint _tmp130_;
								_tmp130_ = file;
								file = _tmp130_ + 1;
							}
							_tmp129_ = FALSE;
							if (!(file < 8)) {
								break;
							}
							_tmp132_ = chess_view_get_scene (self);
							_tmp133_ = _tmp132_;
							_tmp134_ = chess_scene_get_show_move_hints (_tmp133_);
							_tmp135_ = _tmp134_;
							if (_tmp135_) {
								ChessScene* _tmp136_;
								ChessScene* _tmp137_;
								_tmp136_ = chess_view_get_scene (self);
								_tmp137_ = _tmp136_;
								_tmp131_ = chess_scene_can_move (_tmp137_, rank, file);
							} else {
								_tmp131_ = FALSE;
							}
							if (_tmp131_) {
								ChessScene* _tmp138_;
								ChessScene* _tmp139_;
								gdouble _tmp140_;
								gdouble _tmp141_;
								cairo_surface_t* _tmp142_;
								ChessScene* _tmp143_;
								ChessScene* _tmp144_;
								ChessPiece* _tmp145_;
								ChessPiece* _tmp146_;
								cairo_save (c);
								cairo_translate (c, (gdouble) ((file - 4) * self->priv->square_size), (gdouble) ((3 - rank) * self->priv->square_size));
								cairo_translate (c, (gdouble) (self->priv->square_size / 2), (gdouble) (self->priv->square_size / 2));
								_tmp138_ = chess_view_get_scene (self);
								_tmp139_ = _tmp138_;
								_tmp140_ = chess_scene_get_board_angle (_tmp139_);
								_tmp141_ = _tmp140_;
								cairo_rotate (c, ((-G_PI) * _tmp141_) / 180.0);
								_tmp142_ = self->priv->model_surface;
								_tmp143_ = chess_view_get_scene (self);
								_tmp144_ = _tmp143_;
								_tmp145_ = chess_scene_get_selected_piece (_tmp144_);
								_tmp146_ = _tmp145_;
								chess_view_draw_piece (self, c, _tmp142_, self->priv->square_size, _tmp146_, 0.1);
								_g_object_unref0 (_tmp146_);
								cairo_restore (c);
							}
						}
					}
				}
			}
		}
	}
}

static void
chess_view_draw_piece (ChessView* self,
                       cairo_t* c,
                       cairo_surface_t* surface,
                       gint size,
                       ChessPiece* piece,
                       gdouble alpha)
{
	gint offset = 0;
	Color _tmp0_;
	Color _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (c != NULL);
	g_return_if_fail (surface != NULL);
	g_return_if_fail (piece != NULL);
	cairo_translate (c, (gdouble) ((-size) / 2), (gdouble) ((-size) / 2));
	offset = (gint) piece->type;
	_tmp0_ = chess_piece_get_color (piece);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == COLOR_BLACK) {
		offset += 6;
	}
	cairo_set_source_surface (c, surface, (gdouble) ((-offset) * size), (gdouble) 0);
	cairo_rectangle (c, (gdouble) 0, (gdouble) 0, (gdouble) size, (gdouble) size);
	cairo_clip (c);
	cairo_paint_with_alpha (c, alpha);
}

static void
chess_view_on_click (ChessView* self,
                     GtkGestureClick* _click_controller,
                     gint n_press,
                     gdouble event_x,
                     gdouble event_y)
{
	gboolean _tmp0_ = FALSE;
	ChessScene* _tmp1_;
	ChessScene* _tmp2_;
	ChessGame* _tmp3_;
	ChessGame* _tmp4_;
	ChessScene* _tmp11_;
	ChessScene* _tmp12_;
	ChessGame* _tmp13_;
	ChessGame* _tmp14_;
	gint file = 0;
	gint rank = 0;
	ChessScene* _tmp15_;
	ChessScene* _tmp16_;
	gdouble _tmp17_;
	gdouble _tmp18_;
	gboolean _tmp19_ = FALSE;
	gboolean _tmp20_ = FALSE;
	gboolean _tmp21_ = FALSE;
	ChessScene* _tmp22_;
	ChessScene* _tmp23_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (_click_controller != NULL);
	_tmp1_ = chess_view_get_scene (self);
	_tmp2_ = _tmp1_;
	_tmp3_ = chess_scene_get_game (_tmp2_);
	_tmp4_ = _tmp3_;
	if (_tmp4_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		ChessScene* _tmp5_;
		ChessScene* _tmp6_;
		ChessGame* _tmp7_;
		ChessGame* _tmp8_;
		gboolean _tmp9_;
		gboolean _tmp10_;
		_tmp5_ = chess_view_get_scene (self);
		_tmp6_ = _tmp5_;
		_tmp7_ = chess_scene_get_game (_tmp6_);
		_tmp8_ = _tmp7_;
		_tmp9_ = chess_game_get_should_show_paused_overlay (_tmp8_);
		_tmp10_ = _tmp9_;
		_tmp0_ = _tmp10_;
	}
	if (_tmp0_) {
		return;
	}
	_tmp11_ = chess_view_get_scene (self);
	_tmp12_ = _tmp11_;
	_tmp13_ = chess_scene_get_game (_tmp12_);
	_tmp14_ = _tmp13_;
	if (_tmp14_->result != CHESS_RESULT_IN_PROGRESS) {
		return;
	}
	file = (gint) floor (((event_x - (0.5 * gtk_widget_get_allocated_width ((GtkWidget*) self))) + (self->priv->square_size * 4)) / self->priv->square_size);
	rank = 7 - ((gint) floor (((event_y - (0.5 * gtk_widget_get_allocated_height ((GtkWidget*) self))) + (self->priv->square_size * 4)) / self->priv->square_size));
	_tmp15_ = chess_view_get_scene (self);
	_tmp16_ = _tmp15_;
	_tmp17_ = chess_scene_get_board_angle (_tmp16_);
	_tmp18_ = _tmp17_;
	if (_tmp18_ == 180.0) {
		rank = 7 - rank;
		file = 7 - file;
	}
	if (file < 0) {
		_tmp21_ = TRUE;
	} else {
		_tmp21_ = file >= 8;
	}
	if (_tmp21_) {
		_tmp20_ = TRUE;
	} else {
		_tmp20_ = rank < 0;
	}
	if (_tmp20_) {
		_tmp19_ = TRUE;
	} else {
		_tmp19_ = rank >= 8;
	}
	if (_tmp19_) {
		return;
	}
	_tmp22_ = chess_view_get_scene (self);
	_tmp23_ = _tmp22_;
	chess_scene_select_square (_tmp23_, file, rank);
}

static void
chess_view_scene_changed_cb (ChessView* self,
                             ChessScene* scene)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (scene != NULL);
	gtk_widget_queue_draw ((GtkWidget*) self);
}

void
chess_view_draw_paused_overlay (ChessView* self,
                                cairo_t* c)
{
	gchar* text = NULL;
	gchar* _tmp0_;
	cairo_text_extents_t extents = {0};
	cairo_text_extents_t _tmp1_ = {0};
	cairo_text_extents_t _tmp2_;
	cairo_text_extents_t _tmp3_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (c != NULL);
	cairo_save (c);
	cairo_set_source_rgba (c, (gdouble) 0, (gdouble) 0, (gdouble) 0, 0.75);
	cairo_paint (c);
	cairo_select_font_face (c, "Sans", CAIRO_FONT_SLANT_NORMAL, CAIRO_FONT_WEIGHT_BOLD);
	cairo_set_font_size (c, gtk_widget_get_allocated_width ((GtkWidget*) self) * 0.125);
	_tmp0_ = g_strdup (_ ("Paused"));
	text = _tmp0_;
	cairo_text_extents (c, text, &_tmp1_);
	extents = _tmp1_;
	_tmp2_ = extents;
	_tmp3_ = extents;
	cairo_move_to (c, (-_tmp2_.width) / 2.0, _tmp3_.height / 2.0);
	cairo_set_source_rgb (c, (gdouble) 1, (gdouble) 1, (gdouble) 1);
	cairo_show_text (c, text);
	cairo_restore (c);
	_g_free0 (text);
}

ChessScene*
chess_view_get_scene (ChessView* self)
{
	ChessScene* result;
	ChessScene* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_scene;
	result = _tmp0_;
	return result;
}

static void
_chess_view_scene_changed_cb_chess_scene_changed (ChessScene* _sender,
                                                  gpointer self)
{
	chess_view_scene_changed_cb ((ChessView*) self, _sender);
}

void
chess_view_set_scene (ChessView* self,
                      ChessScene* value)
{
	ChessScene* _tmp0_;
	ChessScene* _tmp1_;
	g_return_if_fail (self != NULL);
	_tmp0_ = _g_object_ref0 (value);
	_g_object_unref0 (self->priv->_scene);
	self->priv->_scene = _tmp0_;
	_tmp1_ = self->priv->_scene;
	g_signal_connect_object (_tmp1_, "changed", (GCallback) _chess_view_scene_changed_cb_chess_scene_changed, self, 0);
	gtk_widget_queue_draw ((GtkWidget*) self);
	g_object_notify_by_pspec ((GObject *) self, chess_view_properties[CHESS_VIEW_SCENE_PROPERTY]);
}

static gdouble
chess_view_get_border_size (ChessView* self)
{
	gdouble result;
	g_return_val_if_fail (self != NULL, 0.0);
	result = (gdouble) (self->priv->square_size / 2);
	return result;
}

static void
chess_view_class_init (ChessViewClass * klass,
                       gpointer klass_data)
{
	chess_view_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ChessView_private_offset);
	((GtkDrawingAreaClass *) klass)->resize = (void (*) (GtkDrawingArea*, gint, gint)) chess_view_real_resize;
	G_OBJECT_CLASS (klass)->get_property = _vala_chess_view_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_chess_view_set_property;
	G_OBJECT_CLASS (klass)->finalize = chess_view_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), CHESS_VIEW_SCENE_PROPERTY, chess_view_properties[CHESS_VIEW_SCENE_PROPERTY] = g_param_spec_object ("scene", "scene", "scene", TYPE_CHESS_SCENE, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
}

static void
chess_view_instance_init (ChessView * self,
                          gpointer klass)
{
	gchar* _tmp0_;
	self->priv = chess_view_get_instance_private (self);
	self->priv->border = 6;
	_tmp0_ = g_strdup ("");
	self->priv->loaded_theme_name = _tmp0_;
}

static void
chess_view_finalize (GObject * obj)
{
	ChessView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_CHESS_VIEW, ChessView);
	_cairo_surface_destroy0 (self->priv->model_surface);
	_cairo_surface_destroy0 (self->priv->selected_model_surface);
	_g_free0 (self->priv->loaded_theme_name);
	_g_object_unref0 (self->priv->_scene);
	G_OBJECT_CLASS (chess_view_parent_class)->finalize (obj);
}

static GType
chess_view_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ChessViewClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) chess_view_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ChessView), 0, (GInstanceInitFunc) chess_view_instance_init, NULL };
	GType chess_view_type_id;
	chess_view_type_id = g_type_register_static (gtk_drawing_area_get_type (), "ChessView", &g_define_type_info, 0);
	ChessView_private_offset = g_type_add_instance_private (chess_view_type_id, sizeof (ChessViewPrivate));
	return chess_view_type_id;
}

GType
chess_view_get_type (void)
{
	static volatile gsize chess_view_type_id__once = 0;
	if (g_once_init_enter (&chess_view_type_id__once)) {
		GType chess_view_type_id;
		chess_view_type_id = chess_view_get_type_once ();
		g_once_init_leave (&chess_view_type_id__once, chess_view_type_id);
	}
	return chess_view_type_id__once;
}

static void
_vala_chess_view_get_property (GObject * object,
                               guint property_id,
                               GValue * value,
                               GParamSpec * pspec)
{
	ChessView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CHESS_VIEW, ChessView);
	switch (property_id) {
		case CHESS_VIEW_SCENE_PROPERTY:
		g_value_set_object (value, chess_view_get_scene (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_chess_view_set_property (GObject * object,
                               guint property_id,
                               const GValue * value,
                               GParamSpec * pspec)
{
	ChessView * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, TYPE_CHESS_VIEW, ChessView);
	switch (property_id) {
		case CHESS_VIEW_SCENE_PROPERTY:
		chess_view_set_scene (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

