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

/*
Copyright (C) 2015 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_ORIENTATION_TOOL_0_PROPERTY,
	BIRD_FONT_ORIENTATION_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_orientation_tool_properties[BIRD_FONT_ORIENTATION_TOOL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_source_unref0(var) ((var == NULL) ? NULL : (var = (g_source_unref (var), NULL)))

struct _BirdFontOrientationToolPrivate {
	gdouble time;
	gboolean count_down;
};

static gint BirdFontOrientationTool_private_offset;
static gpointer bird_font_orientation_tool_parent_class = NULL;

static void __lambda413_ (BirdFontOrientationTool* self,
                   BirdFontTool* _self_);
static void ___lambda413__bird_font_tool_select_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gpointer self);
static void __lambda415_ (BirdFontOrientationTool* self);
static void ___lambda415__bird_font_move_tool_selection_changed (BirdFontMoveTool* _sender,
                                                          gpointer self);
static gboolean __lambda414_ (BirdFontOrientationTool* self);
static gboolean ___lambda414__gsource_func (gpointer self);
static void bird_font_orientation_tool_finalize (GObject * obj);
static GType bird_font_orientation_tool_get_type_once (void);

static inline gpointer
bird_font_orientation_tool_get_instance_private (BirdFontOrientationTool* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontOrientationTool_private_offset);
}

static void
__lambda413_ (BirdFontOrientationTool* self,
              BirdFontTool* _self_)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	{
		GeeArrayList* _p_list = NULL;
		BirdFontGlyph* _tmp1_;
		GeeArrayList* _tmp2_;
		gint _p_size = 0;
		GeeArrayList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _p_index = 0;
		_tmp1_ = g;
		_tmp2_ = _tmp1_->active_paths;
		_p_list = _tmp2_;
		_tmp3_ = _p_list;
		_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_p_size = _tmp5_;
		_p_index = -1;
		while (TRUE) {
			gint _tmp6_;
			gint _tmp7_;
			BirdFontPath* p = NULL;
			GeeArrayList* _tmp8_;
			gpointer _tmp9_;
			BirdFontPath* _tmp10_;
			_p_index = _p_index + 1;
			_tmp6_ = _p_index;
			_tmp7_ = _p_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _p_list;
			_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _p_index);
			p = (BirdFontPath*) _tmp9_;
			_tmp10_ = p;
			bird_font_path_reverse (_tmp10_);
			_g_object_unref0 (p);
		}
	}
	self->priv->count_down = TRUE;
	bird_font_glyph_show_orientation_arrow = TRUE;
	bird_font_glyph_orientation_arrow_opacity = (gdouble) 1;
	self->priv->time = (gdouble) 10;
	bird_font_orientation_tool_fade_out (self);
	bird_font_orientation_tool_update_icon (self);
	bird_font_glyph_canvas_redraw ();
	_g_object_unref0 (g);
}

static void
___lambda413__bird_font_tool_select_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gpointer self)
{
	__lambda413_ ((BirdFontOrientationTool*) self, selected);
}

static void
__lambda415_ (BirdFontOrientationTool* self)
{
	bird_font_orientation_tool_update_icon (self);
}

static void
___lambda415__bird_font_move_tool_selection_changed (BirdFontMoveTool* _sender,
                                                     gpointer self)
{
	__lambda415_ ((BirdFontOrientationTool*) self);
}

BirdFontOrientationTool*
bird_font_orientation_tool_construct (GType object_type,
                                      const gchar* name,
                                      const gchar* tip)
{
	BirdFontOrientationTool * self = NULL;
	BirdFontMoveTool* _tmp0_;
	BirdFontMoveTool* _tmp1_;
	g_return_val_if_fail (name != NULL, NULL);
	g_return_val_if_fail (tip != NULL, NULL);
	self = (BirdFontOrientationTool*) bird_font_tool_construct (object_type, name, tip);
	bird_font_tool_set_icon ((BirdFontTool*) self, "orientation_both");
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda413__bird_font_tool_select_action, self, 0);
	_tmp0_ = bird_font_drawing_tools_get_move_tool ();
	_tmp1_ = _tmp0_;
	g_signal_connect_object (_tmp1_, "selection-changed", (GCallback) ___lambda415__bird_font_move_tool_selection_changed, self, 0);
	return self;
}

BirdFontOrientationTool*
bird_font_orientation_tool_new (const gchar* name,
                                const gchar* tip)
{
	return bird_font_orientation_tool_construct (BIRD_FONT_TYPE_ORIENTATION_TOOL, name, tip);
}

void
bird_font_orientation_tool_update_icon (BirdFontOrientationTool* self)
{
	BirdFontGlyph* glyph = NULL;
	BirdFontGlyph* _tmp0_;
	gboolean has_clockwise_paths = FALSE;
	gboolean has_counter_clockwise_paths = FALSE;
	gboolean _tmp12_ = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	glyph = _tmp0_;
	has_clockwise_paths = FALSE;
	has_counter_clockwise_paths = FALSE;
	{
		GeeArrayList* _p_list = NULL;
		BirdFontGlyph* _tmp1_;
		GeeArrayList* _tmp2_;
		gint _p_size = 0;
		GeeArrayList* _tmp3_;
		gint _tmp4_;
		gint _tmp5_;
		gint _p_index = 0;
		_tmp1_ = glyph;
		_tmp2_ = _tmp1_->active_paths;
		_p_list = _tmp2_;
		_tmp3_ = _p_list;
		_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
		_tmp5_ = _tmp4_;
		_p_size = _tmp5_;
		_p_index = -1;
		while (TRUE) {
			gint _tmp6_;
			gint _tmp7_;
			BirdFontPath* p = NULL;
			GeeArrayList* _tmp8_;
			gpointer _tmp9_;
			BirdFontPath* _tmp10_;
			BirdFontPath* _tmp11_;
			_p_index = _p_index + 1;
			_tmp6_ = _p_index;
			_tmp7_ = _p_size;
			if (!(_tmp6_ < _tmp7_)) {
				break;
			}
			_tmp8_ = _p_list;
			_tmp9_ = gee_abstract_list_get ((GeeAbstractList*) _tmp8_, _p_index);
			p = (BirdFontPath*) _tmp9_;
			_tmp10_ = p;
			if (bird_font_path_is_clockwise (_tmp10_)) {
				has_clockwise_paths = TRUE;
			}
			_tmp11_ = p;
			if (!bird_font_path_is_clockwise (_tmp11_)) {
				has_counter_clockwise_paths = TRUE;
			}
			_g_object_unref0 (p);
		}
	}
	if (has_clockwise_paths) {
		_tmp12_ = has_counter_clockwise_paths;
	} else {
		_tmp12_ = FALSE;
	}
	if (_tmp12_) {
		bird_font_tool_set_icon ((BirdFontTool*) self, "orientation_both");
	} else {
		if (has_clockwise_paths) {
			bird_font_tool_set_icon ((BirdFontTool*) self, "orientation_clockwise");
		} else {
			if (has_counter_clockwise_paths) {
				bird_font_tool_set_icon ((BirdFontTool*) self, "orientation_counter_clockwise");
			} else {
				bird_font_tool_set_icon ((BirdFontTool*) self, "orientation_both");
			}
		}
	}
	bird_font_toolbox_redraw_tool_box ();
	_g_object_unref0 (glyph);
}

static gboolean
__lambda414_ (BirdFontOrientationTool* self)
{
	gboolean result;
	if (self->priv->count_down) {
		if (self->priv->time <= ((gdouble) 0)) {
			bird_font_glyph_show_orientation_arrow = FALSE;
			self->priv->count_down = FALSE;
		}
		if (self->priv->time < ((gdouble) 1)) {
			bird_font_glyph_orientation_arrow_opacity = self->priv->time;
			bird_font_glyph_canvas_redraw ();
		}
		self->priv->time = self->priv->time - 0.1;
	} else {
		bird_font_glyph_show_orientation_arrow = FALSE;
	}
	result = self->priv->count_down;
	return result;
}

static gboolean
___lambda414__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda414_ ((BirdFontOrientationTool*) self);
	return result;
}

void
bird_font_orientation_tool_fade_out (BirdFontOrientationTool* self)
{
	GSource* timer = NULL;
	GSource* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_timeout_source_new ((guint) 100);
	timer = _tmp0_;
	g_source_set_callback (timer, ___lambda414__gsource_func, g_object_ref (self), g_object_unref);
	g_source_attach (timer, NULL);
	_g_source_unref0 (timer);
}

static void
bird_font_orientation_tool_class_init (BirdFontOrientationToolClass * klass,
                                       gpointer klass_data)
{
	bird_font_orientation_tool_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontOrientationTool_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_orientation_tool_finalize;
}

static void
bird_font_orientation_tool_instance_init (BirdFontOrientationTool * self,
                                          gpointer klass)
{
	self->priv = bird_font_orientation_tool_get_instance_private (self);
	self->priv->time = (gdouble) 0;
	self->priv->count_down = FALSE;
}

static void
bird_font_orientation_tool_finalize (GObject * obj)
{
	BirdFontOrientationTool * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_ORIENTATION_TOOL, BirdFontOrientationTool);
	G_OBJECT_CLASS (bird_font_orientation_tool_parent_class)->finalize (obj);
}

static GType
bird_font_orientation_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontOrientationToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_orientation_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontOrientationTool), 0, (GInstanceInitFunc) bird_font_orientation_tool_instance_init, NULL };
	GType bird_font_orientation_tool_type_id;
	bird_font_orientation_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontOrientationTool", &g_define_type_info, 0);
	BirdFontOrientationTool_private_offset = g_type_add_instance_private (bird_font_orientation_tool_type_id, sizeof (BirdFontOrientationToolPrivate));
	return bird_font_orientation_tool_type_id;
}

GType
bird_font_orientation_tool_get_type (void)
{
	static volatile gsize bird_font_orientation_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_orientation_tool_type_id__once)) {
		GType bird_font_orientation_tool_type_id;
		bird_font_orientation_tool_type_id = bird_font_orientation_tool_get_type_once ();
		g_once_init_leave (&bird_font_orientation_tool_type_id__once, bird_font_orientation_tool_type_id);
	}
	return bird_font_orientation_tool_type_id__once;
}

