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

/*
Copyright (C) 2012 Johan Mattsson

This library is free software; you can redistribute it and/or modify 
it under the terms of the GNU Lesser General Public License as 
published by the Free Software Foundation; either version 3 of the 
License, or (at your option) any later version.

This library 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 
Lesser General Public License for more details.
*/

#include "birdfont.h"
#include <glib.h>
#include <float.h>
#include <math.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gee.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

enum  {
	BIRD_FONT_RECTANGLE_TOOL_0_PROPERTY,
	BIRD_FONT_RECTANGLE_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_rectangle_tool_properties[BIRD_FONT_RECTANGLE_TOOL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _BirdFontRectangleToolPrivate {
	BirdFontPath* rectangle;
	gdouble press_x;
	gdouble press_y;
	gboolean resize;
};

static gint BirdFontRectangleTool_private_offset;
static gpointer bird_font_rectangle_tool_parent_class = NULL;

static void __lambda498_ (BirdFontRectangleTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void bird_font_rectangle_tool_press (BirdFontRectangleTool* self,
                                     gint button,
                                     gdouble x,
                                     gdouble y);
static void ___lambda498__bird_font_tool_press_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gint button,
                                                gint x,
                                                gint y,
                                                gpointer self);
static void __lambda499_ (BirdFontRectangleTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda499__bird_font_tool_release_action (BirdFontTool* _sender,
                                                  BirdFontTool* selected,
                                                  gint button,
                                                  gint x,
                                                  gint y,
                                                  gpointer self);
static void __lambda500_ (BirdFontRectangleTool* self,
                   BirdFontTool* _self_,
                   gint x,
                   gint y);
static void bird_font_rectangle_tool_move (BirdFontRectangleTool* self,
                                    gdouble x,
                                    gdouble y);
static void ___lambda500__bird_font_tool_move_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               gint x,
                                               gint y,
                                               gpointer self);
static void bird_font_rectangle_tool_finalize (GObject * obj);
static GType bird_font_rectangle_tool_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_rectangle_tool_get_instance_private (BirdFontRectangleTool* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontRectangleTool_private_offset);
}

static void
__lambda498_ (BirdFontRectangleTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
	bird_font_rectangle_tool_press (self, b, (gdouble) x, (gdouble) y);
}

static void
___lambda498__bird_font_tool_press_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gint button,
                                           gint x,
                                           gint y,
                                           gpointer self)
{
	__lambda498_ ((BirdFontRectangleTool*) self, selected, button, x, y);
}

static void
__lambda499_ (BirdFontRectangleTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontPath* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	self->priv->press_x = (gdouble) -1;
	self->priv->press_y = (gdouble) -1;
	_tmp0_ = bird_font_path_new ();
	_g_object_unref0 (self->priv->rectangle);
	self->priv->rectangle = _tmp0_;
	self->priv->resize = FALSE;
}

static void
___lambda499__bird_font_tool_release_action (BirdFontTool* _sender,
                                             BirdFontTool* selected,
                                             gint button,
                                             gint x,
                                             gint y,
                                             gpointer self)
{
	__lambda499_ ((BirdFontRectangleTool*) self, selected, button, x, y);
}

static void
__lambda500_ (BirdFontRectangleTool* self,
              BirdFontTool* _self_,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
	bird_font_rectangle_tool_move (self, (gdouble) x, (gdouble) y);
}

static void
___lambda500__bird_font_tool_move_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          gint x,
                                          gint y,
                                          gpointer self)
{
	__lambda500_ ((BirdFontRectangleTool*) self, selected, x, y);
}

BirdFontRectangleTool*
bird_font_rectangle_tool_construct (GType object_type,
                                    const gchar* n)
{
	BirdFontRectangleTool * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (n != NULL, NULL);
	_tmp0_ = bird_font_t_ ("Rectangle");
	_tmp1_ = _tmp0_;
	self = (BirdFontRectangleTool*) bird_font_tool_construct (object_type, n, _tmp1_);
	_g_free0 (_tmp1_);
	g_signal_connect_object ((BirdFontTool*) self, "press-action", (GCallback) ___lambda498__bird_font_tool_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "release-action", (GCallback) ___lambda499__bird_font_tool_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "move-action", (GCallback) ___lambda500__bird_font_tool_move_action, self, 0);
	return self;
}

BirdFontRectangleTool*
bird_font_rectangle_tool_new (const gchar* n)
{
	return bird_font_rectangle_tool_construct (BIRD_FONT_TYPE_RECTANGLE_TOOL, n);
}

static void
bird_font_rectangle_tool_move (BirdFontRectangleTool* self,
                               gdouble x,
                               gdouble y)
{
	BirdFontGlyph* g = NULL;
	gdouble x1 = 0.0;
	gdouble y1 = 0.0;
	gdouble x2 = 0.0;
	gdouble y2 = 0.0;
	g_return_if_fail (self != NULL);
	if (self->priv->resize) {
		BirdFontGlyph* _tmp0_;
		BirdFontGlyph* _tmp1_;
		BirdFontPath* _tmp2_;
		BirdFontPath* _tmp3_;
		BirdFontPath* _tmp4_;
		BirdFontEditPoint* _tmp5_;
		BirdFontEditPoint* _tmp6_;
		BirdFontPath* _tmp7_;
		BirdFontEditPoint* _tmp8_;
		BirdFontEditPoint* _tmp9_;
		BirdFontPath* _tmp10_;
		BirdFontEditPoint* _tmp11_;
		BirdFontEditPoint* _tmp12_;
		BirdFontPath* _tmp13_;
		BirdFontEditPoint* _tmp14_;
		BirdFontEditPoint* _tmp15_;
		BirdFontPath* _tmp18_;
		BirdFontPath* _tmp19_;
		BirdFontGlyph* _tmp20_;
		BirdFontPath* _tmp21_;
		BirdFontPath* _tmp34_;
		BirdFontPath* _tmp35_;
		_tmp0_ = bird_font_main_window_get_current_glyph ();
		_g_object_unref0 (g);
		g = _tmp0_;
		_tmp1_ = g;
		_tmp2_ = self->priv->rectangle;
		bird_font_glyph_delete_path (_tmp1_, _tmp2_);
		_tmp3_ = bird_font_path_new ();
		_g_object_unref0 (self->priv->rectangle);
		self->priv->rectangle = _tmp3_;
		x1 = bird_font_glyph_path_coordinate_x (self->priv->press_x);
		y1 = bird_font_glyph_path_coordinate_y (self->priv->press_y);
		x2 = bird_font_glyph_path_coordinate_x (x);
		y2 = bird_font_glyph_path_coordinate_y (y);
		if (bird_font_grid_tool_is_visible ()) {
			bird_font_grid_tool_tie_coordinate (&x1, &y1);
			bird_font_grid_tool_tie_coordinate (&x2, &y2);
		}
		_tmp4_ = self->priv->rectangle;
		_tmp5_ = bird_font_path_add (_tmp4_, x1, y1);
		_tmp6_ = _tmp5_;
		_g_object_unref0 (_tmp6_);
		_tmp7_ = self->priv->rectangle;
		_tmp8_ = bird_font_path_add (_tmp7_, x2, y1);
		_tmp9_ = _tmp8_;
		_g_object_unref0 (_tmp9_);
		_tmp10_ = self->priv->rectangle;
		_tmp11_ = bird_font_path_add (_tmp10_, x2, y2);
		_tmp12_ = _tmp11_;
		_g_object_unref0 (_tmp12_);
		_tmp13_ = self->priv->rectangle;
		_tmp14_ = bird_font_path_add (_tmp13_, x1, y2);
		_tmp15_ = _tmp14_;
		_g_object_unref0 (_tmp15_);
		if (bird_font_stroke_tool_add_stroke) {
			BirdFontPath* _tmp16_;
			BirdFontPath* _tmp17_;
			_tmp16_ = self->priv->rectangle;
			bird_font_path_set_stroke (_tmp16_, bird_font_stroke_tool_stroke_width);
			_tmp17_ = self->priv->rectangle;
			_tmp17_->line_cap = bird_font_stroke_tool_line_cap;
		}
		_tmp18_ = self->priv->rectangle;
		bird_font_path_init_point_type (_tmp18_, BIRD_FONT_POINT_TYPE_NONE);
		_tmp19_ = self->priv->rectangle;
		bird_font_path_close (_tmp19_);
		_tmp20_ = g;
		_tmp21_ = self->priv->rectangle;
		bird_font_glyph_add_path (_tmp20_, _tmp21_);
		{
			GeeArrayList* _e_list = NULL;
			BirdFontPath* _tmp22_;
			GeeArrayList* _tmp23_;
			GeeArrayList* _tmp24_;
			gint _e_size = 0;
			GeeArrayList* _tmp25_;
			gint _tmp26_;
			gint _tmp27_;
			gint _e_index = 0;
			_tmp22_ = self->priv->rectangle;
			_tmp23_ = bird_font_path_get_points (_tmp22_);
			_tmp24_ = _tmp23_;
			_e_list = _tmp24_;
			_tmp25_ = _e_list;
			_tmp26_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp25_);
			_tmp27_ = _tmp26_;
			_e_size = _tmp27_;
			_e_index = -1;
			while (TRUE) {
				gint _tmp28_;
				gint _tmp29_;
				BirdFontEditPoint* e = NULL;
				GeeArrayList* _tmp30_;
				gpointer _tmp31_;
				BirdFontPath* _tmp32_;
				BirdFontEditPoint* _tmp33_;
				_e_index = _e_index + 1;
				_tmp28_ = _e_index;
				_tmp29_ = _e_size;
				if (!(_tmp28_ < _tmp29_)) {
					break;
				}
				_tmp30_ = _e_list;
				_tmp31_ = gee_abstract_list_get ((GeeAbstractList*) _tmp30_, _e_index);
				e = (BirdFontEditPoint*) _tmp31_;
				_tmp32_ = self->priv->rectangle;
				_tmp33_ = e;
				bird_font_path_recalculate_linear_handles_for_point (_tmp32_, _tmp33_);
				_g_object_unref0 (e);
			}
		}
		_tmp34_ = self->priv->rectangle;
		bird_font_path_reset_stroke (_tmp34_);
		_tmp35_ = self->priv->rectangle;
		bird_font_path_update_region_boundaries (_tmp35_);
		bird_font_glyph_canvas_redraw ();
	}
	_g_object_unref0 (g);
}

static void
bird_font_rectangle_tool_press (BirdFontRectangleTool* self,
                                gint button,
                                gdouble x,
                                gdouble y)
{
	g_return_if_fail (self != NULL);
	self->priv->press_x = x;
	self->priv->press_y = y;
	self->priv->resize = TRUE;
	bird_font_glyph_canvas_redraw ();
}

static void
bird_font_rectangle_tool_class_init (BirdFontRectangleToolClass * klass,
                                     gpointer klass_data)
{
	bird_font_rectangle_tool_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontRectangleTool_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_rectangle_tool_finalize;
}

static void
bird_font_rectangle_tool_instance_init (BirdFontRectangleTool * self,
                                        gpointer klass)
{
	BirdFontPath* _tmp0_;
	self->priv = bird_font_rectangle_tool_get_instance_private (self);
	_tmp0_ = bird_font_path_new ();
	self->priv->rectangle = _tmp0_;
	self->priv->press_x = (gdouble) -1;
	self->priv->press_y = (gdouble) -1;
	self->priv->resize = FALSE;
}

static void
bird_font_rectangle_tool_finalize (GObject * obj)
{
	BirdFontRectangleTool * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_RECTANGLE_TOOL, BirdFontRectangleTool);
	_g_object_unref0 (self->priv->rectangle);
	G_OBJECT_CLASS (bird_font_rectangle_tool_parent_class)->finalize (obj);
}

static GType
bird_font_rectangle_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontRectangleToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_rectangle_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontRectangleTool), 0, (GInstanceInitFunc) bird_font_rectangle_tool_instance_init, NULL };
	GType bird_font_rectangle_tool_type_id;
	bird_font_rectangle_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontRectangleTool", &g_define_type_info, 0);
	BirdFontRectangleTool_private_offset = g_type_add_instance_private (bird_font_rectangle_tool_type_id, sizeof (BirdFontRectangleToolPrivate));
	return bird_font_rectangle_tool_type_id;
}

GType
bird_font_rectangle_tool_get_type (void)
{
	static volatile gsize bird_font_rectangle_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_rectangle_tool_type_id__once)) {
		GType bird_font_rectangle_tool_type_id;
		bird_font_rectangle_tool_type_id = bird_font_rectangle_tool_get_type_once ();
		g_once_init_leave (&bird_font_rectangle_tool_type_id__once, bird_font_rectangle_tool_type_id);
	}
	return bird_font_rectangle_tool_type_id__once;
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

