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

/*
Copyright (C) 2013 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 <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <cairo-gobject.h>
#include <float.h>
#include <math.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_POINT_TOOL_0_PROPERTY,
	BIRD_FONT_POINT_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_point_tool_properties[BIRD_FONT_POINT_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))

static gpointer bird_font_point_tool_parent_class = NULL;

static void __lambda126_ (BirdFontPointTool* self,
                   BirdFontTool* _self_);
static void ___lambda126__bird_font_tool_select_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gpointer self);
static void __lambda127_ (BirdFontPointTool* self,
                   BirdFontTool* _self_);
static void ___lambda127__bird_font_tool_deselect_action (BirdFontTool* _sender,
                                                   BirdFontTool* selected,
                                                   gpointer self);
static void __lambda128_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda128__bird_font_tool_press_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gint button,
                                                gint x,
                                                gint y,
                                                gpointer self);
static void __lambda129_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda129__bird_font_tool_double_click_action (BirdFontTool* _sender,
                                                       BirdFontTool* selected,
                                                       gint button,
                                                       gint x,
                                                       gint y,
                                                       gpointer self);
static void __lambda130_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda130__bird_font_tool_release_action (BirdFontTool* _sender,
                                                  BirdFontTool* selected,
                                                  gint button,
                                                  gint x,
                                                  gint y,
                                                  gpointer self);
static void __lambda131_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   gint x,
                   gint y);
static void ___lambda131__bird_font_tool_move_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               gint x,
                                               gint y,
                                               gpointer self);
static void __lambda132_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   guint32 keyval);
static void ___lambda132__bird_font_tool_key_press_action (BirdFontTool* _sender,
                                                    BirdFontTool* selected,
                                                    guint32 keyval,
                                                    gpointer self);
static void __lambda133_ (BirdFontPointTool* self,
                   BirdFontTool* _self_,
                   guint32 keyval);
static void ___lambda133__bird_font_tool_key_release_action (BirdFontTool* _sender,
                                                      BirdFontTool* selected,
                                                      guint32 keyval,
                                                      gpointer self);
static void __lambda134_ (BirdFontPointTool* self,
                   BirdFontTool* tool,
                   cairo_t* cairo_context,
                   BirdFontGlyph* glyph);
static void ___lambda134__bird_font_tool_draw_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               cairo_t* cr,
                                               BirdFontGlyph* glyph,
                                               gpointer self);
static gchar* bird_font_point_tool_real_get_tip (BirdFontTool* base);
static GType bird_font_point_tool_get_type_once (void);

static void
__lambda126_ (BirdFontPointTool* self,
              BirdFontTool* _self_)
{
	g_return_if_fail (_self_ != NULL);
}

static void
___lambda126__bird_font_tool_select_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gpointer self)
{
	__lambda126_ ((BirdFontPointTool*) self, selected);
}

static void
__lambda127_ (BirdFontPointTool* self,
              BirdFontTool* _self_)
{
	g_return_if_fail (_self_ != NULL);
}

static void
___lambda127__bird_font_tool_deselect_action (BirdFontTool* _sender,
                                              BirdFontTool* selected,
                                              gpointer self)
{
	__lambda127_ ((BirdFontPointTool*) self, selected);
}

static void
__lambda128_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	if (b == 1) {
		BirdFontTool* _tmp1_;
		BirdFontTool* _tmp2_;
		_tmp1_ = p;
		_tmp2_ = p;
		g_signal_emit_by_name (_tmp1_, "press-action", _tmp2_, 3, x, y);
	} else {
		if (b == 2) {
			BirdFontTool* _tmp3_;
			BirdFontTool* _tmp4_;
			_tmp3_ = p;
			_tmp4_ = p;
			g_signal_emit_by_name (_tmp3_, "press-action", _tmp4_, 2, x, y);
		} else {
			if (b == 3) {
				BirdFontTool* _tmp5_;
				BirdFontTool* _tmp6_;
				_tmp5_ = p;
				_tmp6_ = p;
				g_signal_emit_by_name (_tmp5_, "press-action", _tmp6_, 1, x, y);
			}
		}
	}
	_g_object_unref0 (p);
}

static void
___lambda128__bird_font_tool_press_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gint button,
                                           gint x,
                                           gint y,
                                           gpointer self)
{
	__lambda128_ ((BirdFontPointTool*) self, selected, button, x, y);
}

static void
__lambda129_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	if (!bird_font_bird_font_android) {
		BirdFontTool* _tmp1_;
		BirdFontTool* _tmp2_;
		_tmp1_ = p;
		_tmp2_ = p;
		g_signal_emit_by_name (_tmp1_, "double-click-action", _tmp2_, b, x, y);
	}
	_g_object_unref0 (p);
}

static void
___lambda129__bird_font_tool_double_click_action (BirdFontTool* _sender,
                                                  BirdFontTool* selected,
                                                  gint button,
                                                  gint x,
                                                  gint y,
                                                  gpointer self)
{
	__lambda129_ ((BirdFontPointTool*) self, selected, button, x, y);
}

static void
__lambda130_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	if (b == 1) {
		BirdFontTool* _tmp1_;
		BirdFontTool* _tmp2_;
		_tmp1_ = p;
		_tmp2_ = p;
		g_signal_emit_by_name (_tmp1_, "release-action", _tmp2_, 3, x, y);
	} else {
		if (b == 2) {
			BirdFontTool* _tmp3_;
			BirdFontTool* _tmp4_;
			_tmp3_ = p;
			_tmp4_ = p;
			g_signal_emit_by_name (_tmp3_, "release-action", _tmp4_, 2, x, y);
		} else {
			if (b == 3) {
				BirdFontTool* _tmp5_;
				BirdFontTool* _tmp6_;
				_tmp5_ = p;
				_tmp6_ = p;
				g_signal_emit_by_name (_tmp5_, "release-action", _tmp6_, 1, x, y);
			}
		}
	}
	_g_object_unref0 (p);
}

static void
___lambda130__bird_font_tool_release_action (BirdFontTool* _sender,
                                             BirdFontTool* selected,
                                             gint button,
                                             gint x,
                                             gint y,
                                             gpointer self)
{
	__lambda130_ ((BirdFontPointTool*) self, selected, button, x, y);
}

static void
__lambda131_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              gint x,
              gint y)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	g_signal_emit_by_name (p, "move-action", p, x, y);
	_g_object_unref0 (p);
}

static void
___lambda131__bird_font_tool_move_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          gint x,
                                          gint y,
                                          gpointer self)
{
	__lambda131_ ((BirdFontPointTool*) self, selected, x, y);
}

static void
__lambda132_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              guint32 keyval)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	g_signal_emit_by_name (p, "key-press-action", p, keyval);
	_g_object_unref0 (p);
}

static void
___lambda132__bird_font_tool_key_press_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               guint32 keyval,
                                               gpointer self)
{
	__lambda132_ ((BirdFontPointTool*) self, selected, keyval);
}

static void
__lambda133_ (BirdFontPointTool* self,
              BirdFontTool* _self_,
              guint32 keyval)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	g_signal_emit_by_name (p, "key-release-action", p, keyval);
	_g_object_unref0 (p);
}

static void
___lambda133__bird_font_tool_key_release_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 guint32 keyval,
                                                 gpointer self)
{
	__lambda133_ ((BirdFontPointTool*) self, selected, keyval);
}

static void
__lambda134_ (BirdFontPointTool* self,
              BirdFontTool* tool,
              cairo_t* cairo_context,
              BirdFontGlyph* glyph)
{
	BirdFontTool* p = NULL;
	BirdFontTool* _tmp0_;
	g_return_if_fail (tool != NULL);
	g_return_if_fail (cairo_context != NULL);
	g_return_if_fail (glyph != NULL);
	_tmp0_ = bird_font_point_tool_pen ();
	p = _tmp0_;
	g_signal_emit_by_name (p, "draw-action", p, cairo_context, glyph);
	_g_object_unref0 (p);
}

static void
___lambda134__bird_font_tool_draw_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          cairo_t* cr,
                                          BirdFontGlyph* glyph,
                                          gpointer self)
{
	__lambda134_ ((BirdFontPointTool*) self, selected, cr, glyph);
}

BirdFontPointTool*
bird_font_point_tool_construct (GType object_type,
                                const gchar* name)
{
	BirdFontPointTool * self = NULL;
	g_return_val_if_fail (name != NULL, NULL);
	self = (BirdFontPointTool*) bird_font_tool_construct (object_type, name, "");
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda126__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "deselect-action", (GCallback) ___lambda127__bird_font_tool_deselect_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "press-action", (GCallback) ___lambda128__bird_font_tool_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "double-click-action", (GCallback) ___lambda129__bird_font_tool_double_click_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "release-action", (GCallback) ___lambda130__bird_font_tool_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "move-action", (GCallback) ___lambda131__bird_font_tool_move_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "key-press-action", (GCallback) ___lambda132__bird_font_tool_key_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "key-release-action", (GCallback) ___lambda133__bird_font_tool_key_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "draw-action", (GCallback) ___lambda134__bird_font_tool_draw_action, self, 0);
	return self;
}

BirdFontPointTool*
bird_font_point_tool_new (const gchar* name)
{
	return bird_font_point_tool_construct (BIRD_FONT_TYPE_POINT_TOOL, name);
}

static gchar*
bird_font_point_tool_real_get_tip (BirdFontTool* base)
{
	BirdFontPointTool * self;
	gchar* tip = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	const gchar* _tmp4_;
	BirdFontTool* _tmp5_;
	gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	gchar* _tmp10_;
	const gchar* _tmp11_;
	gchar* _tmp12_;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	const gchar* _tmp17_;
	gchar* _tmp18_;
	gchar* _tmp19_;
	gchar* _tmp20_;
	gchar* _tmp21_;
	gchar* _tmp22_;
	const gchar* _tmp23_;
	gchar* _tmp24_;
	gchar* _tmp25_;
	gchar* _tmp26_;
	gchar* _tmp27_;
	gchar* _tmp28_;
	const gchar* _tmp29_;
	gchar* _tmp30_;
	gchar* _tmp31_;
	gchar* _tmp32_;
	gchar* _tmp33_;
	gchar* _tmp34_;
	const gchar* _tmp35_;
	gchar* _tmp36_;
	gchar* _tmp37_;
	gchar* _tmp38_;
	gchar* _tmp39_;
	gchar* _tmp40_;
	gchar* result;
	self = (BirdFontPointTool*) base;
	_tmp0_ = bird_font_t_ ("Move control points");
	_tmp1_ = _tmp0_;
	_tmp2_ = g_strconcat (_tmp1_, "\n", NULL);
	_tmp3_ = _tmp2_;
	_g_free0 (_tmp1_);
	tip = _tmp3_;
	_tmp4_ = tip;
	_tmp5_ = bird_font_hidden_tools_move_along_axis;
	_tmp6_ = bird_font_tool_get_key_binding (_tmp5_);
	_tmp7_ = _tmp6_;
	_tmp8_ = g_strconcat (_tmp4_, _tmp7_, NULL);
	_g_free0 (tip);
	tip = _tmp8_;
	_g_free0 (_tmp7_);
	_tmp9_ = tip;
	_tmp10_ = g_strconcat (_tmp9_, " - ", NULL);
	_g_free0 (tip);
	tip = _tmp10_;
	_tmp11_ = tip;
	_tmp12_ = bird_font_t_ ("on axis");
	_tmp13_ = _tmp12_;
	_tmp14_ = g_strconcat (_tmp13_, "\n", NULL);
	_tmp15_ = _tmp14_;
	_tmp16_ = g_strconcat (_tmp11_, _tmp15_, NULL);
	_g_free0 (tip);
	tip = _tmp16_;
	_g_free0 (_tmp15_);
	_g_free0 (_tmp13_);
	_tmp17_ = tip;
	_tmp18_ = bird_font_t_ ("backspace");
	_tmp19_ = _tmp18_;
	_tmp20_ = g_strconcat (_tmp19_, " - ", NULL);
	_tmp21_ = _tmp20_;
	_tmp22_ = g_strconcat (_tmp17_, _tmp21_, NULL);
	_g_free0 (tip);
	tip = _tmp22_;
	_g_free0 (_tmp21_);
	_g_free0 (_tmp19_);
	_tmp23_ = tip;
	_tmp24_ = bird_font_t_ ("delete points");
	_tmp25_ = _tmp24_;
	_tmp26_ = g_strconcat (_tmp25_, "\n", NULL);
	_tmp27_ = _tmp26_;
	_tmp28_ = g_strconcat (_tmp23_, _tmp27_, NULL);
	_g_free0 (tip);
	tip = _tmp28_;
	_g_free0 (_tmp27_);
	_g_free0 (_tmp25_);
	_tmp29_ = tip;
	_tmp30_ = bird_font_t_ ("shift + backspace");
	_tmp31_ = _tmp30_;
	_tmp32_ = g_strconcat (_tmp31_, " - ", NULL);
	_tmp33_ = _tmp32_;
	_tmp34_ = g_strconcat (_tmp29_, _tmp33_, NULL);
	_g_free0 (tip);
	tip = _tmp34_;
	_g_free0 (_tmp33_);
	_g_free0 (_tmp31_);
	_tmp35_ = tip;
	_tmp36_ = bird_font_t_ ("delete points and break paths");
	_tmp37_ = _tmp36_;
	_tmp38_ = g_strconcat (_tmp37_, "\n", NULL);
	_tmp39_ = _tmp38_;
	_tmp40_ = g_strconcat (_tmp35_, _tmp39_, NULL);
	_g_free0 (tip);
	tip = _tmp40_;
	_g_free0 (_tmp39_);
	_g_free0 (_tmp37_);
	result = tip;
	return result;
}

BirdFontTool*
bird_font_point_tool_pen (void)
{
	BirdFontToolbox* _tmp0_;
	BirdFontToolbox* _tmp1_;
	BirdFontTool* _tmp2_;
	BirdFontTool* _tmp3_;
	BirdFontTool* result;
	_tmp0_ = bird_font_main_window_get_toolbox ();
	_tmp1_ = _tmp0_;
	_tmp2_ = bird_font_toolbox_get_tool (_tmp1_, "pen_tool");
	_tmp3_ = _tmp2_;
	_g_object_unref0 (_tmp1_);
	result = _tmp3_;
	return result;
}

void
bird_font_point_tool_tie_angle (gdouble center_x,
                                gdouble center_y,
                                gdouble coordinate_x,
                                gdouble coordinate_y,
                                gdouble* tied_x,
                                gdouble* tied_y)
{
	gdouble _vala_tied_x = 0.0;
	gdouble _vala_tied_y = 0.0;
	gdouble length = 0.0;
	gdouble min = 0.0;
	gdouble circle_edge = 0.0;
	gdouble circle_x = 0.0;
	gdouble circle_y = 0.0;
	length = fabs (bird_font_path_distance (center_x, coordinate_x, center_y, coordinate_y));
	_vala_tied_x = (gdouble) 0;
	_vala_tied_y = (gdouble) 0;
	min = DBL_MAX;
	{
		gdouble circle_angle = 0.0;
		circle_angle = (gdouble) 0;
		{
			gboolean _tmp0_ = FALSE;
			_tmp0_ = TRUE;
			while (TRUE) {
				if (!_tmp0_) {
					circle_angle += G_PI / 4;
				}
				_tmp0_ = FALSE;
				if (!(circle_angle < (2 * G_PI))) {
					break;
				}
				circle_x = center_x + (cos (circle_angle) * length);
				circle_y = center_y + (sin (circle_angle) * length);
				circle_edge = fabs (bird_font_path_distance (coordinate_x, circle_x, coordinate_y, circle_y));
				if (circle_edge < min) {
					_vala_tied_x = circle_x;
					_vala_tied_y = circle_y;
					min = circle_edge;
				}
			}
		}
	}
	if (tied_x) {
		*tied_x = _vala_tied_x;
	}
	if (tied_y) {
		*tied_y = _vala_tied_y;
	}
}

static void
bird_font_point_tool_class_init (BirdFontPointToolClass * klass,
                                 gpointer klass_data)
{
	bird_font_point_tool_parent_class = g_type_class_peek_parent (klass);
	((BirdFontToolClass *) klass)->get_tip = (gchar* (*) (BirdFontTool*)) bird_font_point_tool_real_get_tip;
}

static void
bird_font_point_tool_instance_init (BirdFontPointTool * self,
                                    gpointer klass)
{
}

/** Move control points. */
static GType
bird_font_point_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontPointToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_point_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontPointTool), 0, (GInstanceInitFunc) bird_font_point_tool_instance_init, NULL };
	GType bird_font_point_tool_type_id;
	bird_font_point_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontPointTool", &g_define_type_info, 0);
	return bird_font_point_tool_type_id;
}

GType
bird_font_point_tool_get_type (void)
{
	static volatile gsize bird_font_point_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_point_tool_type_id__once)) {
		GType bird_font_point_tool_type_id;
		bird_font_point_tool_type_id = bird_font_point_tool_get_type_once ();
		g_once_init_leave (&bird_font_point_tool_type_id__once, bird_font_point_tool_type_id);
	}
	return bird_font_point_tool_type_id__once;
}

