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

/*
Copyright (C) 2014 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-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <float.h>
#include <math.h>
#include <cairo-gobject.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_BACKGROUND_SELECTION_TOOL_0_PROPERTY,
	BIRD_FONT_BACKGROUND_SELECTION_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_background_selection_tool_properties[BIRD_FONT_BACKGROUND_SELECTION_TOOL_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

static gpointer bird_font_background_selection_tool_parent_class = NULL;

static void _bird_font_background_selection_tool_add_new_image_bird_font_cut_background_tool_new_image (BirdFontCutBackgroundTool* _sender,
                                                                                                 BirdFontBackgroundImage* file,
                                                                                                 gdouble coordinate_x,
                                                                                                 gdouble coordinate_y,
                                                                                                 gdouble width,
                                                                                                 gdouble height,
                                                                                                 gpointer self);
static void __lambda254_ (BirdFontBackgroundSelectionTool* self,
                   BirdFontTool* t);
static void ___lambda254__bird_font_tool_select_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gpointer self);
static void __lambda255_ (BirdFontBackgroundSelectionTool* self,
                   BirdFontTool* _self_,
                   cairo_t* cr,
                   BirdFontGlyph* glyph);
static void ___lambda255__bird_font_tool_draw_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               cairo_t* cr,
                                               BirdFontGlyph* glyph,
                                               gpointer self);
static GType bird_font_background_selection_tool_get_type_once (void);

static void
_bird_font_background_selection_tool_add_new_image_bird_font_cut_background_tool_new_image (BirdFontCutBackgroundTool* _sender,
                                                                                            BirdFontBackgroundImage* file,
                                                                                            gdouble coordinate_x,
                                                                                            gdouble coordinate_y,
                                                                                            gdouble width,
                                                                                            gdouble height,
                                                                                            gpointer self)
{
	bird_font_background_selection_tool_add_new_image ((BirdFontBackgroundSelectionTool*) self, file, coordinate_x, coordinate_y, width, height);
}

static void
__lambda254_ (BirdFontBackgroundSelectionTool* self,
              BirdFontTool* t)
{
	BirdFontTool* _tmp0_;
	g_return_if_fail (t != NULL);
	_tmp0_ = bird_font_drawing_tools_move_canvas;
	bird_font_tool_set_selected (_tmp0_, FALSE);
	bird_font_glyph_canvas_redraw ();
}

static void
___lambda254__bird_font_tool_select_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gpointer self)
{
	__lambda254_ ((BirdFontBackgroundSelectionTool*) self, selected);
}

static void
__lambda255_ (BirdFontBackgroundSelectionTool* self,
              BirdFontTool* _self_,
              cairo_t* cr,
              BirdFontGlyph* glyph)
{
	gdouble x = 0.0;
	gdouble y = 0.0;
	gdouble w = 0.0;
	gdouble h = 0.0;
	BirdFontBackgroundImage* bg = NULL;
	BirdFontText* label = NULL;
	BirdFontText* _tmp0_;
	gdouble tx = 0.0;
	gdouble ty = 0.0;
	gdouble font_height = 0.0;
	BirdFontBackgroundImage* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	gboolean _tmp3_;
	BirdFontBackgroundImage* _tmp4_;
	g_return_if_fail (_self_ != NULL);
	g_return_if_fail (cr != NULL);
	g_return_if_fail (glyph != NULL);
	_tmp0_ = bird_font_text_new ("", (gdouble) 17, (gdouble) 0);
	label = _tmp0_;
	_tmp1_ = bird_font_glyph_get_background_image (glyph);
	_tmp2_ = _tmp1_;
	_tmp3_ = _tmp2_ == NULL;
	_g_object_unref0 (_tmp2_);
	if (_tmp3_) {
		_g_object_unref0 (label);
		_g_object_unref0 (bg);
		return;
	}
	_tmp4_ = bird_font_glyph_get_background_image (glyph);
	_g_object_unref0 (bg);
	bg = G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage);
	{
		GeeArrayList* _bs_list = NULL;
		BirdFontBackgroundImage* _tmp5_;
		GeeArrayList* _tmp6_;
		gint _bs_size = 0;
		GeeArrayList* _tmp7_;
		gint _tmp8_;
		gint _tmp9_;
		gint _bs_index = 0;
		_tmp5_ = bg;
		_tmp6_ = _tmp5_->selections;
		_bs_list = _tmp6_;
		_tmp7_ = _bs_list;
		_tmp8_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp7_);
		_tmp9_ = _tmp8_;
		_bs_size = _tmp9_;
		_bs_index = -1;
		while (TRUE) {
			gint _tmp10_;
			gint _tmp11_;
			BirdFontBackgroundSelection* bs = NULL;
			GeeArrayList* _tmp12_;
			gpointer _tmp13_;
			BirdFontBackgroundSelection* _tmp14_;
			gdouble _tmp15_;
			gdouble _tmp16_;
			BirdFontBackgroundSelection* _tmp17_;
			gdouble _tmp18_;
			gdouble _tmp19_;
			BirdFontBackgroundSelection* _tmp20_;
			gdouble _tmp21_;
			gdouble _tmp22_;
			BirdFontBackgroundSelection* _tmp23_;
			gdouble _tmp24_;
			gdouble _tmp25_;
			BirdFontBackgroundSelection* _tmp26_;
			gdouble _tmp27_;
			gdouble _tmp28_;
			BirdFontBackgroundSelection* _tmp29_;
			gdouble _tmp30_;
			gdouble _tmp31_;
			BirdFontBackgroundSelection* _tmp32_;
			const gchar* _tmp33_;
			BirdFontBackgroundSelection* _tmp34_;
			const gchar* _tmp35_;
			_bs_index = _bs_index + 1;
			_tmp10_ = _bs_index;
			_tmp11_ = _bs_size;
			if (!(_tmp10_ < _tmp11_)) {
				break;
			}
			_tmp12_ = _bs_list;
			_tmp13_ = gee_abstract_list_get ((GeeAbstractList*) _tmp12_, _bs_index);
			bs = (BirdFontBackgroundSelection*) _tmp13_;
			_tmp14_ = bs;
			_tmp15_ = bird_font_background_selection_get_x (_tmp14_);
			_tmp16_ = _tmp15_;
			x = (gdouble) bird_font_glyph_reverse_path_coordinate_x (_tmp16_);
			_tmp17_ = bs;
			_tmp18_ = bird_font_background_selection_get_y (_tmp17_);
			_tmp19_ = _tmp18_;
			y = (gdouble) bird_font_glyph_reverse_path_coordinate_y (_tmp19_);
			_tmp20_ = bs;
			_tmp21_ = bird_font_background_selection_get_x (_tmp20_);
			_tmp22_ = _tmp21_;
			_tmp23_ = bs;
			_tmp24_ = bird_font_background_selection_get_w (_tmp23_);
			_tmp25_ = _tmp24_;
			w = bird_font_glyph_reverse_path_coordinate_x (_tmp22_ + _tmp25_) - x;
			_tmp26_ = bs;
			_tmp27_ = bird_font_background_selection_get_y (_tmp26_);
			_tmp28_ = _tmp27_;
			_tmp29_ = bs;
			_tmp30_ = bird_font_background_selection_get_h (_tmp29_);
			_tmp31_ = _tmp30_;
			h = bird_font_glyph_reverse_path_coordinate_y (_tmp28_ + _tmp31_) - y;
			cairo_save (cr);
			cairo_set_line_width (cr, 2.0);
			_tmp32_ = bs;
			_tmp33_ = _tmp32_->assigned_glyph;
			if (_tmp33_ != NULL) {
				cairo_set_source_rgba (cr, 237 / 255.0, 67 / 255.0, (gdouble) 0, (gdouble) 1);
			} else {
				cairo_set_source_rgba (cr, 132 / 255.0, 132 / 255.0, 132 / 255.0, (gdouble) 1);
			}
			cairo_rectangle (cr, x, y, w, h);
			cairo_stroke (cr);
			cairo_arc (cr, x + w, y + h, 9.0, (gdouble) 0, 2 * G_PI);
			cairo_fill (cr);
			_tmp34_ = bs;
			_tmp35_ = _tmp34_->assigned_glyph;
			if (_tmp35_ != NULL) {
				BirdFontText* _tmp36_;
				BirdFontBackgroundSelection* _tmp37_;
				const gchar* _tmp38_;
				BirdFontText* _tmp39_;
				BirdFontText* _tmp40_;
				BirdFontText* _tmp41_;
				BirdFontText* _tmp42_;
				BirdFontText* _tmp43_;
				BirdFontText* _tmp44_;
				font_height = (gdouble) 18;
				_tmp36_ = label;
				_tmp37_ = bs;
				_tmp38_ = _tmp37_->assigned_glyph;
				bird_font_text_set_text (_tmp36_, (const gchar*) _tmp38_);
				_tmp39_ = label;
				bird_font_text_set_font_size (_tmp39_, font_height);
				_tmp40_ = label;
				tx = (x + w) - (bird_font_widget_get_width ((BirdFontWidget*) _tmp40_) / 2.0);
				_tmp41_ = label;
				_tmp42_ = label;
				ty = ((y + h) + (bird_font_text_get_acender (_tmp41_) / 2.0)) - bird_font_text_get_decender (_tmp42_);
				_tmp43_ = label;
				bird_font_text_set_font_size (_tmp43_, font_height);
				_tmp44_ = label;
				bird_font_text_draw_at_baseline (_tmp44_, cr, tx, ty, "");
			}
			cairo_restore (cr);
			_g_object_unref0 (bs);
		}
	}
	_g_object_unref0 (label);
	_g_object_unref0 (bg);
}

static void
___lambda255__bird_font_tool_draw_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          cairo_t* cr,
                                          BirdFontGlyph* glyph,
                                          gpointer self)
{
	__lambda255_ ((BirdFontBackgroundSelectionTool*) self, selected, cr, glyph);
}

BirdFontBackgroundSelectionTool*
bird_font_background_selection_tool_construct (GType object_type)
{
	BirdFontBackgroundSelectionTool * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	_tmp0_ = bird_font_t_ ("Select background");
	_tmp1_ = _tmp0_;
	self = (BirdFontBackgroundSelectionTool*) bird_font_cut_background_tool_construct (object_type, "select_background", _tmp1_);
	_g_free0 (_tmp1_);
	((BirdFontTool*) self)->editor_events = TRUE;
	((BirdFontTool*) self)->persistent = TRUE;
	((BirdFontCutBackgroundTool*) self)->self_destination = FALSE;
	g_signal_connect_object ((BirdFontCutBackgroundTool*) self, "new-image", (GCallback) _bird_font_background_selection_tool_add_new_image_bird_font_cut_background_tool_new_image, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda254__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "draw-action", (GCallback) ___lambda255__bird_font_tool_draw_action, self, 0);
	return self;
}

BirdFontBackgroundSelectionTool*
bird_font_background_selection_tool_new (void)
{
	return bird_font_background_selection_tool_construct (BIRD_FONT_TYPE_BACKGROUND_SELECTION_TOOL);
}

void
bird_font_background_selection_tool_add_new_image (BirdFontBackgroundSelectionTool* self,
                                                   BirdFontBackgroundImage* image,
                                                   gdouble x,
                                                   gdouble y,
                                                   gdouble w,
                                                   gdouble h)
{
	BirdFontBackgroundImage* bg = NULL;
	BirdFontBackgroundSelection* selection = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	BirdFontBackgroundImage* _tmp3_;
	gboolean _tmp4_;
	BirdFontGlyph* _tmp5_;
	BirdFontGlyph* _tmp6_;
	BirdFontBackgroundImage* _tmp7_;
	BirdFontBackgroundImage* _tmp8_;
	BirdFontBackgroundSelection* _tmp9_;
	BirdFontBackgroundImage* _tmp10_;
	BirdFontBackgroundSelection* _tmp11_;
	BirdFontBackgroundTools* _tmp12_;
	BirdFontBackgroundSelection* _tmp13_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (image != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	_tmp1_ = _tmp0_;
	_tmp2_ = bird_font_glyph_get_background_image (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = _tmp3_ == NULL;
	_g_object_unref0 (_tmp3_);
	_g_object_unref0 (_tmp1_);
	if (_tmp4_) {
		g_warning ("BackgroundSelectionTool.vala:91: No image");
		_g_object_unref0 (selection);
		_g_object_unref0 (bg);
		return;
	}
	_tmp5_ = bird_font_main_window_get_current_glyph ();
	_tmp6_ = _tmp5_;
	_tmp7_ = bird_font_glyph_get_background_image (_tmp6_);
	_g_object_unref0 (bg);
	bg = G_TYPE_CHECK_INSTANCE_CAST (_tmp7_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage);
	_g_object_unref0 (_tmp6_);
	_tmp8_ = bg;
	_tmp9_ = bird_font_background_selection_new (image, _tmp8_, x, y, w, h);
	_g_object_unref0 (selection);
	selection = _tmp9_;
	_tmp10_ = bg;
	_tmp11_ = selection;
	bird_font_background_image_add_selection (_tmp10_, _tmp11_);
	_tmp12_ = bird_font_toolbox_background_tools;
	_tmp13_ = selection;
	bird_font_background_tools_add_part (_tmp12_, _tmp13_);
	_g_object_unref0 (selection);
	_g_object_unref0 (bg);
}

static void
bird_font_background_selection_tool_class_init (BirdFontBackgroundSelectionToolClass * klass,
                                                gpointer klass_data)
{
	bird_font_background_selection_tool_parent_class = g_type_class_peek_parent (klass);
}

static void
bird_font_background_selection_tool_instance_init (BirdFontBackgroundSelectionTool * self,
                                                   gpointer klass)
{
}

static GType
bird_font_background_selection_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontBackgroundSelectionToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_background_selection_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontBackgroundSelectionTool), 0, (GInstanceInitFunc) bird_font_background_selection_tool_instance_init, NULL };
	GType bird_font_background_selection_tool_type_id;
	bird_font_background_selection_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_CUT_BACKGROUND_TOOL, "BirdFontBackgroundSelectionTool", &g_define_type_info, 0);
	return bird_font_background_selection_tool_type_id;
}

GType
bird_font_background_selection_tool_get_type (void)
{
	static volatile gsize bird_font_background_selection_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_background_selection_tool_type_id__once)) {
		GType bird_font_background_selection_tool_type_id;
		bird_font_background_selection_tool_type_id = bird_font_background_selection_tool_get_type_once ();
		g_once_init_leave (&bird_font_background_selection_tool_type_id__once, bird_font_background_selection_tool_type_id);
	}
	return bird_font_background_selection_tool_type_id__once;
}

