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

/* -*- Mode: Vala; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
/*
    Multiplication Puzzle
    Copyright (C) 2008,2011 Michael Terry <mike@mterry.name>

    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.

    This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/

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


#define TYPE_CHAR_BOX (char_box_get_type ())
#define CHAR_BOX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CHAR_BOX, CharBox))
#define CHAR_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CHAR_BOX, CharBoxClass))
#define IS_CHAR_BOX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CHAR_BOX))
#define IS_CHAR_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CHAR_BOX))
#define CHAR_BOX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CHAR_BOX, CharBoxClass))

typedef struct _CharBox CharBox;
typedef struct _CharBoxClass CharBoxClass;
typedef struct _CharBoxPrivate CharBoxPrivate;

#define TYPE_DIGIT_BOX (digit_box_get_type ())
#define DIGIT_BOX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DIGIT_BOX, DigitBox))
#define DIGIT_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DIGIT_BOX, DigitBoxClass))
#define IS_DIGIT_BOX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DIGIT_BOX))
#define IS_DIGIT_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DIGIT_BOX))
#define DIGIT_BOX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DIGIT_BOX, DigitBoxClass))

typedef struct _DigitBox DigitBox;
typedef struct _DigitBoxClass DigitBoxClass;
typedef struct _DigitBoxPrivate DigitBoxPrivate;

#define TYPE_CANVAS (canvas_get_type ())
#define CANVAS(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_CANVAS, Canvas))
#define CANVAS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_CANVAS, CanvasClass))
#define IS_CANVAS(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_CANVAS))
#define IS_CANVAS_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_CANVAS))
#define CANVAS_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_CANVAS, CanvasClass))

typedef struct _Canvas Canvas;
typedef struct _CanvasClass CanvasClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define TYPE_CANVAS_MODE (canvas_mode_get_type ())
#define _cairo_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_destroy (var), NULL)))
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))

struct _CharBox {
	GtkEventBox parent_instance;
	CharBoxPrivate * priv;
};

struct _CharBoxClass {
	GtkEventBoxClass parent_class;
};

struct _DigitBox {
	CharBox parent_instance;
	DigitBoxPrivate * priv;
};

struct _DigitBoxClass {
	CharBoxClass parent_class;
};

struct _DigitBoxPrivate {
	Canvas* _canvas;
};

typedef enum  {
	CANVAS_MODE_NONE,
	CANVAS_MODE_DIGIT,
	CANVAS_MODE_CHAR
} CanvasMode;


static gpointer digit_box_parent_class = NULL;

GType char_box_get_type (void) G_GNUC_CONST;
GType digit_box_get_type (void) G_GNUC_CONST;
GType canvas_get_type (void) G_GNUC_CONST;
#define DIGIT_BOX_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_DIGIT_BOX, DigitBoxPrivate))
enum  {
	DIGIT_BOX_DUMMY_PROPERTY,
	DIGIT_BOX_CANVAS
};
DigitBox* digit_box_new (Canvas* canvas, const gchar* letter);
DigitBox* digit_box_construct (GType object_type, Canvas* canvas, const gchar* letter);
static void digit_box_handle_drag_begin (DigitBox* self, GdkDragContext* context);
Canvas* digit_box_get_canvas (DigitBox* self);
GType canvas_mode_get_type (void) G_GNUC_CONST;
void canvas_set_mode (Canvas* self, CanvasMode m, CharBox* box);
static void digit_box_handle_drag_end (DigitBox* self, GdkDragContext* context);
static void digit_box_handle_drag_get (DigitBox* self, GdkDragContext* context, GtkSelectionData* data, guint info, guint time_);
const gchar* char_box_get_letter (CharBox* self);
static void digit_box_set_canvas (DigitBox* self, Canvas* value);
static GObject * digit_box_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
void char_box_set_border (CharBox* self, GtkShadowType value);
static void _digit_box_handle_drag_get_gtk_widget_drag_data_get (GtkWidget* _sender, GdkDragContext* context, GtkSelectionData* selection_data, guint info, guint time_, gpointer self);
static void _digit_box_handle_drag_begin_gtk_widget_drag_begin (GtkWidget* _sender, GdkDragContext* context, gpointer self);
static void _digit_box_handle_drag_end_gtk_widget_drag_end (GtkWidget* _sender, GdkDragContext* context, gpointer self);
static void digit_box_finalize (GObject* obj);
static void _vala_digit_box_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
static void _vala_digit_box_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec);


DigitBox* digit_box_construct (GType object_type, Canvas* canvas, const gchar* letter) {
	DigitBox * self = NULL;
	g_return_val_if_fail (canvas != NULL, NULL);
	g_return_val_if_fail (letter != NULL, NULL);
	self = (DigitBox*) g_object_new (object_type, "canvas", canvas, "letter", letter, NULL);
	return self;
}


DigitBox* digit_box_new (Canvas* canvas, const gchar* letter) {
	return digit_box_construct (TYPE_DIGIT_BOX, canvas, letter);
}


static gpointer _cairo_surface_reference0 (gpointer self) {
	return self ? cairo_surface_reference (self) : NULL;
}


static void digit_box_handle_drag_begin (DigitBox* self, GdkDragContext* context) {
	GdkWindow* _tmp0_ = NULL;
	GdkWindow* _tmp1_ = NULL;
	cairo_region_t* _tmp2_ = NULL;
	GdkWindow* _tmp3_ = NULL;
	gint width = 0;
	gint height = 0;
	GdkWindow* _tmp4_ = NULL;
	gint _tmp5_;
	GdkWindow* _tmp6_ = NULL;
	gint _tmp7_;
	GdkWindow* _tmp8_ = NULL;
	cairo_surface_t* _tmp9_ = NULL;
	cairo_surface_t* _tmp10_;
	cairo_surface_t* surface;
	cairo_t* _tmp11_ = NULL;
	cairo_t* ctx;
	GdkWindow* _tmp12_ = NULL;
	gint x = 0;
	gint y = 0;
	gint _tmp13_;
	gint _tmp14_;
	gint _tmp15_;
	gint _tmp16_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	canvas_set_mode (self->priv->_canvas, CANVAS_MODE_DIGIT, (CharBox*) self);
	_tmp0_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp1_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp2_ = gdk_window_get_visible_region (_tmp1_);
	gdk_window_invalidate_region (_tmp0_, _tmp2_, TRUE);
	_tmp3_ = gtk_widget_get_window ((GtkWidget*) self);
	gdk_window_process_updates (_tmp3_, TRUE);
	_tmp4_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp5_ = gdk_window_get_width (_tmp4_);
	width = _tmp5_;
	_tmp6_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp7_ = gdk_window_get_height (_tmp6_);
	height = _tmp7_;
	_tmp8_ = gtk_widget_get_window ((GtkWidget*) self);
	_tmp9_ = gdk_window_create_similar_surface (_tmp8_, CAIRO_CONTENT_COLOR, width, height);
	_tmp10_ = _cairo_surface_reference0 (_tmp9_);
	surface = _tmp10_;
	_tmp11_ = cairo_create (surface);
	ctx = _tmp11_;
	_tmp12_ = gtk_widget_get_window ((GtkWidget*) self);
	gdk_cairo_set_source_window (ctx, _tmp12_, (gdouble) 0, (gdouble) 0);
	cairo_paint (ctx);
	gtk_widget_get_pointer ((GtkWidget*) self, &_tmp13_, &_tmp14_);
	x = _tmp13_;
	y = _tmp14_;
	_tmp15_ = CLAMP (x, 0, width);
	x = _tmp15_;
	_tmp16_ = CLAMP (y, 0, height);
	y = _tmp16_;
	cairo_surface_set_device_offset (surface, (gdouble) (-x), (gdouble) (-y));
	gtk_drag_set_icon_surface (context, surface);
	gtk_widget_hide ((GtkWidget*) self);
	_cairo_destroy0 (ctx);
	_cairo_surface_destroy0 (surface);
}


static void digit_box_handle_drag_end (DigitBox* self, GdkDragContext* context) {
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	gtk_widget_show ((GtkWidget*) self);
}


static void digit_box_handle_drag_get (DigitBox* self, GdkDragContext* context, GtkSelectionData* data, guint info, guint time_) {
	const gchar* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	gint _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (context != NULL);
	g_return_if_fail (data != NULL);
	_tmp0_ = char_box_get_letter ((CharBox*) self);
	_tmp1_ = char_box_get_letter ((CharBox*) self);
	_tmp2_ = strlen (_tmp1_);
	gtk_selection_data_set_text (data, _tmp0_, (gint) _tmp2_);
}


Canvas* digit_box_get_canvas (DigitBox* self) {
	Canvas* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self->priv->_canvas;
	return result;
}


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


static void digit_box_set_canvas (DigitBox* self, Canvas* value) {
	Canvas* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = _g_object_ref0 (value);
	_g_object_unref0 (self->priv->_canvas);
	self->priv->_canvas = _tmp0_;
	g_object_notify ((GObject *) self, "canvas");
}


static void _digit_box_handle_drag_get_gtk_widget_drag_data_get (GtkWidget* _sender, GdkDragContext* context, GtkSelectionData* selection_data, guint info, guint time_, gpointer self) {
	digit_box_handle_drag_get (self, context, selection_data, info, time_);
}


static void _digit_box_handle_drag_begin_gtk_widget_drag_begin (GtkWidget* _sender, GdkDragContext* context, gpointer self) {
	digit_box_handle_drag_begin (self, context);
}


static void _digit_box_handle_drag_end_gtk_widget_drag_end (GtkWidget* _sender, GdkDragContext* context, gpointer self) {
	digit_box_handle_drag_end (self, context);
}


static GObject * digit_box_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	DigitBox * self;
	GtkTargetEntry* _tmp0_ = NULL;
	GtkTargetEntry* entries;
	gint entries_length1;
	gint _entries_size_;
	parent_class = G_OBJECT_CLASS (digit_box_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = DIGIT_BOX (obj);
	char_box_set_border ((CharBox*) self, GTK_SHADOW_OUT);
	_tmp0_ = g_new0 (GtkTargetEntry, 1);
	entries = _tmp0_;
	entries_length1 = 1;
	_entries_size_ = 1;
	entries[0].target = "STRING";
	entries[0].flags = (guint) GTK_TARGET_SAME_APP;
	entries[0].info = (guint) 0;
	gtk_drag_source_set ((GtkWidget*) self, GDK_BUTTON1_MASK, entries, entries_length1, GDK_ACTION_MOVE);
	g_signal_connect_object ((GtkWidget*) self, "drag-data-get", (GCallback) _digit_box_handle_drag_get_gtk_widget_drag_data_get, self, 0);
	g_signal_connect_object ((GtkWidget*) self, "drag-begin", (GCallback) _digit_box_handle_drag_begin_gtk_widget_drag_begin, self, 0);
	g_signal_connect_object ((GtkWidget*) self, "drag-end", (GCallback) _digit_box_handle_drag_end_gtk_widget_drag_end, self, 0);
	entries = (g_free (entries), NULL);
	return obj;
}


static void digit_box_class_init (DigitBoxClass * klass) {
	digit_box_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (DigitBoxPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_digit_box_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_digit_box_set_property;
	G_OBJECT_CLASS (klass)->constructor = digit_box_constructor;
	G_OBJECT_CLASS (klass)->finalize = digit_box_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), DIGIT_BOX_CANVAS, g_param_spec_object ("canvas", "canvas", "canvas", TYPE_CANVAS, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
}


static void digit_box_instance_init (DigitBox * self) {
	self->priv = DIGIT_BOX_GET_PRIVATE (self);
}


static void digit_box_finalize (GObject* obj) {
	DigitBox * self;
	self = DIGIT_BOX (obj);
	_g_object_unref0 (self->priv->_canvas);
	G_OBJECT_CLASS (digit_box_parent_class)->finalize (obj);
}


GType digit_box_get_type (void) {
	static volatile gsize digit_box_type_id__volatile = 0;
	if (g_once_init_enter (&digit_box_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DigitBoxClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) digit_box_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DigitBox), 0, (GInstanceInitFunc) digit_box_instance_init, NULL };
		GType digit_box_type_id;
		digit_box_type_id = g_type_register_static (TYPE_CHAR_BOX, "DigitBox", &g_define_type_info, 0);
		g_once_init_leave (&digit_box_type_id__volatile, digit_box_type_id);
	}
	return digit_box_type_id__volatile;
}


static void _vala_digit_box_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	DigitBox * self;
	self = DIGIT_BOX (object);
	switch (property_id) {
		case DIGIT_BOX_CANVAS:
		g_value_set_object (value, digit_box_get_canvas (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


static void _vala_digit_box_set_property (GObject * object, guint property_id, const GValue * value, GParamSpec * pspec) {
	DigitBox * self;
	self = DIGIT_BOX (object);
	switch (property_id) {
		case DIGIT_BOX_CANVAS:
		digit_box_set_canvas (self, g_value_get_object (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



