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

/*
Copyright (C) 2012 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.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <cairo-gobject.h>
#include <glib-object.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_TOOL_0_PROPERTY,
	BIRD_FONT_BACKGROUND_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_background_tool_properties[BIRD_FONT_BACKGROUND_TOOL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _cairo_surface_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_surface_destroy (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_source_unref0(var) ((var == NULL) ? NULL : (var = (g_source_unref (var), NULL)))

struct _BirdFontBackgroundToolPrivate {
	gdouble begin_x;
	gdouble begin_y;
	gint last_x;
	gint last_y;
	gboolean move_bg;
	gdouble rotation_position_x;
	gdouble rotation_position_y;
};

static gint BirdFontBackgroundTool_private_offset;
static gpointer bird_font_background_tool_parent_class = NULL;
gdouble bird_font_background_tool_top_limit = 0.0;
gdouble bird_font_background_tool_bottom_limit = 0.0;
static BirdFontBackgroundImage* bird_font_background_tool_imported_background;
static BirdFontBackgroundImage* bird_font_background_tool_imported_background = NULL;
static cairo_surface_t* bird_font_background_tool_imported_surface;
static cairo_surface_t* bird_font_background_tool_imported_surface = NULL;
static gboolean bird_font_background_tool_on_axis;
static gboolean bird_font_background_tool_on_axis = FALSE;

static void __lambda150_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_);
static void ___lambda150__bird_font_tool_select_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gpointer self);
static void __lambda151_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_);
static void ___lambda151__bird_font_tool_deselect_action (BirdFontTool* _sender,
                                                   BirdFontTool* selected,
                                                   gpointer self);
static void __lambda152_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda152__bird_font_tool_press_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gint button,
                                                gint x,
                                                gint y,
                                                gpointer self);
static void __lambda153_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_,
                   gint b,
                   gint x,
                   gint y);
static void ___lambda153__bird_font_tool_release_action (BirdFontTool* _sender,
                                                  BirdFontTool* selected,
                                                  gint button,
                                                  gint x,
                                                  gint y,
                                                  gpointer self);
static void __lambda154_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_,
                   gint x,
                   gint y);
static void bird_font_background_tool_move (BirdFontBackgroundTool* self,
                                     gint x,
                                     gint y);
static void ___lambda154__bird_font_tool_move_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               gint x,
                                               gint y,
                                               gpointer self);
static void __lambda155_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_,
                   guint32 keyval);
static void ___lambda155__bird_font_tool_key_press_action (BirdFontTool* _sender,
                                                    BirdFontTool* selected,
                                                    guint32 keyval,
                                                    gpointer self);
static void __lambda156_ (BirdFontBackgroundTool* self,
                   BirdFontTool* _self_,
                   cairo_t* cairo_context,
                   BirdFontGlyph* glyph);
static void ___lambda156__bird_font_tool_draw_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               cairo_t* cr,
                                               BirdFontGlyph* glyph,
                                               gpointer self);
static gchar* bird_font_background_tool_real_get_tip (BirdFontTool* base);
static gboolean __lambda157_ (void);
static gboolean ___lambda157__gsource_func (gpointer self);
VALA_EXTERN void bird_font_background_tool_import_background_image (void);
static void __lambda158_ (const gchar* fn);
static void ___lambda158__bird_font_file_chooser_file_selected (BirdFontFileChooser* _sender,
                                                         const gchar* path,
                                                         gpointer self);
static void bird_font_background_tool_finalize (GObject * obj);
static GType bird_font_background_tool_get_type_once (void);

static inline gpointer
bird_font_background_tool_get_instance_private (BirdFontBackgroundTool* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontBackgroundTool_private_offset);
}

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

static void
___lambda150__bird_font_tool_select_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gpointer self)
{
	__lambda150_ ((BirdFontBackgroundTool*) self, selected);
}

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

static void
___lambda151__bird_font_tool_deselect_action (BirdFontTool* _sender,
                                              BirdFontTool* selected,
                                              gpointer self)
{
	__lambda151_ ((BirdFontBackgroundTool*) self, selected);
}

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

static void
__lambda152_ (BirdFontBackgroundTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontBackgroundImage* bg = NULL;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	BirdFontBackgroundImage* background = NULL;
	BirdFontBackgroundImage* _tmp3_;
	BirdFontBackgroundImage* _tmp4_;
	BirdFontBackgroundImage* _tmp5_;
	BirdFontGlyph* _tmp6_;
	BirdFontBackgroundImage* _tmp7_;
	BirdFontBackgroundImage* _tmp8_;
	gdouble _tmp9_;
	gdouble _tmp10_;
	BirdFontBackgroundImage* _tmp11_;
	gdouble _tmp12_;
	gdouble _tmp13_;
	BirdFontBackgroundImage* _tmp14_;
	gdouble _tmp15_;
	gdouble _tmp16_;
	BirdFontBackgroundImage* _tmp17_;
	cairo_surface_t* _tmp18_;
	cairo_surface_t* _tmp19_;
	BirdFontBackgroundImage* _tmp20_;
	gdouble _tmp21_;
	gdouble _tmp22_;
	BirdFontBackgroundImage* _tmp23_;
	cairo_surface_t* _tmp24_;
	cairo_surface_t* _tmp25_;
	BirdFontBackgroundImage* _tmp26_;
	gdouble _tmp27_;
	gdouble _tmp28_;
	BirdFontBackgroundImage* _tmp29_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	_tmp2_ = bird_font_glyph_get_background_image (_tmp1_);
	bg = _tmp2_;
	_tmp3_ = bg;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (background);
		_g_object_unref0 (bg);
		_g_object_unref0 (g);
		return;
	}
	_tmp4_ = bg;
	_tmp5_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage));
	_g_object_unref0 (background);
	background = _tmp5_;
	_tmp6_ = g;
	bird_font_glyph_store_undo_state (_tmp6_, FALSE);
	_tmp7_ = background;
	bird_font_background_image_handler_press (_tmp7_, (gdouble) x, (gdouble) y);
	self->priv->begin_x = (gdouble) x;
	self->priv->begin_y = (gdouble) y;
	_tmp8_ = background;
	_tmp9_ = bird_font_background_image_get_img_offset_x (_tmp8_);
	_tmp10_ = _tmp9_;
	self->img_offset_x = _tmp10_;
	_tmp11_ = background;
	_tmp12_ = bird_font_background_image_get_img_offset_y (_tmp11_);
	_tmp13_ = _tmp12_;
	self->img_offset_y = _tmp13_;
	_tmp14_ = background;
	_tmp15_ = bird_font_background_image_get_img_scale_x (_tmp14_);
	_tmp16_ = _tmp15_;
	self->img_scale_x = _tmp16_;
	_tmp17_ = background;
	_tmp18_ = bird_font_background_image_get_img (_tmp17_);
	_tmp19_ = _tmp18_;
	_tmp20_ = background;
	_tmp21_ = bird_font_background_image_get_img_scale_x (_tmp20_);
	_tmp22_ = _tmp21_;
	self->img_width = cairo_image_surface_get_width (_tmp19_) * _tmp22_;
	_cairo_surface_destroy0 (_tmp19_);
	_tmp23_ = background;
	_tmp24_ = bird_font_background_image_get_img (_tmp23_);
	_tmp25_ = _tmp24_;
	_tmp26_ = background;
	_tmp27_ = bird_font_background_image_get_img_scale_y (_tmp26_);
	_tmp28_ = _tmp27_;
	self->img_height = cairo_image_surface_get_height (_tmp25_) * _tmp28_;
	_cairo_surface_destroy0 (_tmp25_);
	_tmp29_ = background;
	bird_font_background_image_start_rotation_preview (_tmp29_);
	self->priv->move_bg = TRUE;
	self->priv->last_x = x;
	self->priv->last_y = y;
	_g_object_unref0 (background);
	_g_object_unref0 (bg);
	_g_object_unref0 (g);
}

static void
___lambda152__bird_font_tool_press_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gint button,
                                           gint x,
                                           gint y,
                                           gpointer self)
{
	__lambda152_ ((BirdFontBackgroundTool*) self, selected, button, x, y);
}

static void
__lambda153_ (BirdFontBackgroundTool* self,
              BirdFontTool* _self_,
              gint b,
              gint x,
              gint y)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontBackgroundImage* bg = NULL;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	gdouble coordinate_x = 0.0;
	gdouble coordinate_y = 0.0;
	BirdFontBackgroundImage* _tmp3_;
	BirdFontBackgroundImage* background = NULL;
	BirdFontBackgroundImage* _tmp4_;
	BirdFontBackgroundImage* _tmp5_;
	BirdFontBackgroundImage* _tmp6_;
	BirdFontBackgroundImage* _tmp8_;
	gdouble _tmp9_;
	gdouble _tmp10_;
	BirdFontBackgroundImage* _tmp11_;
	gdouble _tmp12_;
	gdouble _tmp13_;
	BirdFontBackgroundImage* _tmp14_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	_tmp2_ = bird_font_glyph_get_background_image (_tmp1_);
	bg = _tmp2_;
	_tmp3_ = bg;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (bg);
		_g_object_unref0 (g);
		return;
	}
	_tmp4_ = bg;
	_tmp5_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage));
	background = _tmp5_;
	coordinate_x = bird_font_glyph_path_coordinate_x ((gdouble) x);
	coordinate_y = bird_font_glyph_path_coordinate_y ((gdouble) y);
	_tmp6_ = background;
	if (_tmp6_->selected_handle == 2) {
		BirdFontBackgroundImage* _tmp7_;
		_tmp7_ = background;
		bird_font_background_image_set_img_rotation_from_coordinate (_tmp7_, self->priv->rotation_position_x, self->priv->rotation_position_y);
	}
	_tmp8_ = background;
	_tmp9_ = bird_font_background_image_get_img_offset_x (_tmp8_);
	_tmp10_ = _tmp9_;
	self->img_offset_x = _tmp10_;
	_tmp11_ = background;
	_tmp12_ = bird_font_background_image_get_img_offset_y (_tmp11_);
	_tmp13_ = _tmp12_;
	self->img_offset_y = _tmp13_;
	_tmp14_ = background;
	bird_font_background_image_handler_release (_tmp14_, (gdouble) x, (gdouble) y);
	bird_font_background_tool_on_axis = FALSE;
	self->priv->move_bg = FALSE;
	_g_object_unref0 (background);
	_g_object_unref0 (bg);
	_g_object_unref0 (g);
}

static void
___lambda153__bird_font_tool_release_action (BirdFontTool* _sender,
                                             BirdFontTool* selected,
                                             gint button,
                                             gint x,
                                             gint y,
                                             gpointer self)
{
	__lambda153_ ((BirdFontBackgroundTool*) self, selected, button, x, y);
}

static void
__lambda154_ (BirdFontBackgroundTool* self,
              BirdFontTool* _self_,
              gint x,
              gint y)
{
	g_return_if_fail (_self_ != NULL);
	bird_font_background_tool_move (self, x, y);
}

static void
___lambda154__bird_font_tool_move_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          gint x,
                                          gint y,
                                          gpointer self)
{
	__lambda154_ ((BirdFontBackgroundTool*) self, selected, x, y);
}

static void
__lambda155_ (BirdFontBackgroundTool* self,
              BirdFontTool* _self_,
              guint32 keyval)
{
	g_return_if_fail (_self_ != NULL);
	self->priv->move_bg = TRUE;
	self->priv->begin_x = (gdouble) 0;
	self->priv->begin_y = (gdouble) 0;
	switch (keyval) {
		case BIRD_FONT_KEY_UP:
		{
			bird_font_background_tool_move (self, 0, -1);
			break;
		}
		case BIRD_FONT_KEY_DOWN:
		{
			bird_font_background_tool_move (self, 0, 1);
			break;
		}
		case BIRD_FONT_KEY_LEFT:
		{
			bird_font_background_tool_move (self, -1, 0);
			break;
		}
		case BIRD_FONT_KEY_RIGHT:
		{
			bird_font_background_tool_move (self, 1, 0);
			break;
		}
		default:
		{
			break;
		}
	}
	self->priv->move_bg = FALSE;
}

static void
___lambda155__bird_font_tool_key_press_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               guint32 keyval,
                                               gpointer self)
{
	__lambda155_ ((BirdFontBackgroundTool*) self, selected, keyval);
}

static void
__lambda156_ (BirdFontBackgroundTool* self,
              BirdFontTool* _self_,
              cairo_t* cairo_context,
              BirdFontGlyph* glyph)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontBackgroundImage* background_image = NULL;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	BirdFontBackgroundImage* _tmp3_;
	BirdFontBackgroundImage* _tmp4_;
	g_return_if_fail (_self_ != NULL);
	g_return_if_fail (cairo_context != NULL);
	g_return_if_fail (glyph != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	_tmp2_ = bird_font_glyph_get_background_image (_tmp1_);
	background_image = _tmp2_;
	_tmp3_ = background_image;
	if (_tmp3_ == NULL) {
		_g_object_unref0 (background_image);
		_g_object_unref0 (g);
		return;
	}
	_tmp4_ = background_image;
	bird_font_background_image_draw_handle (G_TYPE_CHECK_INSTANCE_CAST (_tmp4_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage), cairo_context, glyph);
	_g_object_unref0 (background_image);
	_g_object_unref0 (g);
}

static void
___lambda156__bird_font_tool_draw_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          cairo_t* cr,
                                          BirdFontGlyph* glyph,
                                          gpointer self)
{
	__lambda156_ ((BirdFontBackgroundTool*) self, selected, cr, glyph);
}

BirdFontBackgroundTool*
bird_font_background_tool_construct (GType object_type,
                                     const gchar* name)
{
	BirdFontBackgroundTool * self = NULL;
	BirdFontBackgroundImage* _tmp0_;
	g_return_val_if_fail (name != NULL, NULL);
	self = (BirdFontBackgroundTool*) bird_font_tool_construct (object_type, name, "");
	bird_font_background_tool_top_limit = (gdouble) 0;
	bird_font_background_tool_bottom_limit = (gdouble) 0;
	_tmp0_ = bird_font_background_image_new ("");
	_g_object_unref0 (bird_font_background_tool_imported_background);
	bird_font_background_tool_imported_background = _tmp0_;
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda150__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "deselect-action", (GCallback) ___lambda151__bird_font_tool_deselect_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "press-action", (GCallback) ___lambda152__bird_font_tool_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "release-action", (GCallback) ___lambda153__bird_font_tool_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "move-action", (GCallback) ___lambda154__bird_font_tool_move_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "key-press-action", (GCallback) ___lambda155__bird_font_tool_key_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "draw-action", (GCallback) ___lambda156__bird_font_tool_draw_action, self, 0);
	return self;
}

BirdFontBackgroundTool*
bird_font_background_tool_new (const gchar* name)
{
	return bird_font_background_tool_construct (BIRD_FONT_TYPE_BACKGROUND_TOOL, name);
}

static gchar*
bird_font_background_tool_real_get_tip (BirdFontTool* base)
{
	BirdFontBackgroundTool * 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_;
	gchar* result;
	self = (BirdFontBackgroundTool*) base;
	_tmp0_ = bird_font_t_ ("Move, resize and rotate background image");
	_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_);
	result = tip;
	return result;
}

void
bird_font_background_tool_move_handle_on_axis (void)
{
	bird_font_background_tool_on_axis = TRUE;
}

static void
bird_font_background_tool_move (BirdFontBackgroundTool* self,
                                gint x,
                                gint y)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontBackgroundImage* background_image = NULL;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	BirdFontBackgroundImage* bg = NULL;
	BirdFontBackgroundImage* _tmp3_;
	BirdFontBackgroundImage* _tmp4_;
	gdouble xscale = 0.0;
	gdouble yscale = 0.0;
	gdouble dx = 0.0;
	gdouble dy = 0.0;
	gdouble xc = 0.0;
	gdouble yc = 0.0;
	gdouble coordinate_x = 0.0;
	gdouble coordinate_y = 0.0;
	gdouble view_zoom = 0.0;
	BirdFontBackgroundImage* _tmp5_;
	BirdFontBackgroundImage* _tmp6_;
	BirdFontGlyph* _tmp7_;
	BirdFontGlyph* _tmp8_;
	BirdFontBackgroundImage* _tmp9_;
	BirdFontBackgroundImage* _tmp26_;
	gboolean _tmp36_ = FALSE;
	g_return_if_fail (self != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	_tmp2_ = bird_font_glyph_get_background_image (_tmp1_);
	background_image = _tmp2_;
	_tmp3_ = background_image;
	_tmp4_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp3_, BIRD_FONT_TYPE_BACKGROUND_IMAGE, BirdFontBackgroundImage));
	bg = _tmp4_;
	_tmp5_ = background_image;
	if (_tmp5_ == NULL) {
		_g_object_unref0 (bg);
		_g_object_unref0 (background_image);
		_g_object_unref0 (g);
		return;
	}
	_tmp6_ = bg;
	bird_font_background_image_handler_move (_tmp6_, (gdouble) x, (gdouble) y);
	dx = x - self->priv->begin_x;
	dy = y - self->priv->begin_y;
	_tmp7_ = g;
	dx *= 1 / _tmp7_->view_zoom;
	_tmp8_ = g;
	dy *= 1 / _tmp8_->view_zoom;
	dx *= bird_font_pen_tool_precision;
	dy *= bird_font_pen_tool_precision;
	_tmp9_ = bg;
	if (_tmp9_->selected_handle == 2) {
		BirdFontGlyph* _tmp10_;
		BirdFontGlyph* _tmp11_;
		BirdFontBackgroundImage* _tmp25_;
		coordinate_x = bird_font_glyph_path_coordinate_x ((gdouble) x);
		coordinate_y = bird_font_glyph_path_coordinate_y ((gdouble) y);
		_tmp10_ = bird_font_main_window_get_current_glyph ();
		_tmp11_ = _tmp10_;
		view_zoom = _tmp11_->view_zoom;
		_g_object_unref0 (_tmp11_);
		self->priv->rotation_position_x = coordinate_x;
		self->priv->rotation_position_y = coordinate_y;
		if (bird_font_background_tool_on_axis) {
			gdouble length = 0.0;
			BirdFontBackgroundImage* _tmp12_;
			gdouble _tmp13_;
			gdouble _tmp14_;
			BirdFontBackgroundImage* _tmp15_;
			gdouble _tmp16_;
			gdouble _tmp17_;
			gdouble min = 0.0;
			gdouble circle_edge = 0.0;
			gdouble circle_x = 0.0;
			gdouble circle_y = 0.0;
			_tmp12_ = bg;
			_tmp13_ = bird_font_background_image_get_img_middle_x (_tmp12_);
			_tmp14_ = _tmp13_;
			_tmp15_ = bg;
			_tmp16_ = bird_font_background_image_get_img_middle_y (_tmp15_);
			_tmp17_ = _tmp16_;
			length = fabs (bird_font_path_distance (_tmp14_, coordinate_x, _tmp17_, coordinate_y));
			min = DBL_MAX;
			{
				gdouble circle_angle = 0.0;
				circle_angle = (gdouble) 0;
				{
					gboolean _tmp18_ = FALSE;
					_tmp18_ = TRUE;
					while (TRUE) {
						BirdFontBackgroundImage* _tmp19_;
						gdouble _tmp20_;
						gdouble _tmp21_;
						BirdFontBackgroundImage* _tmp22_;
						gdouble _tmp23_;
						gdouble _tmp24_;
						if (!_tmp18_) {
							circle_angle += G_PI / 4;
						}
						_tmp18_ = FALSE;
						if (!(circle_angle < (2 * G_PI))) {
							break;
						}
						_tmp19_ = bg;
						_tmp20_ = bird_font_background_image_get_img_middle_x (_tmp19_);
						_tmp21_ = _tmp20_;
						circle_x = _tmp21_ + (cos (circle_angle) * length);
						_tmp22_ = bg;
						_tmp23_ = bird_font_background_image_get_img_middle_y (_tmp22_);
						_tmp24_ = _tmp23_;
						circle_y = _tmp24_ + (sin (circle_angle) * length);
						circle_edge = fabs (bird_font_path_distance (coordinate_x, circle_x, coordinate_y, circle_y));
						if (circle_edge < min) {
							self->priv->rotation_position_x = circle_x;
							self->priv->rotation_position_y = circle_y;
							min = circle_edge;
						}
					}
				}
			}
		}
		_tmp25_ = bg;
		bird_font_background_image_preview_img_rotation_from_coordinate (_tmp25_, self->priv->rotation_position_x, self->priv->rotation_position_y, view_zoom);
	}
	_tmp26_ = bg;
	if (_tmp26_->selected_handle == 1) {
		BirdFontBackgroundImage* _tmp27_;
		gdouble _tmp28_;
		gdouble _tmp29_;
		BirdFontBackgroundImage* _tmp30_;
		gdouble _tmp31_;
		gdouble _tmp32_;
		BirdFontBackgroundImage* _tmp33_;
		BirdFontBackgroundImage* _tmp34_;
		BirdFontBackgroundImage* _tmp35_;
		xscale = self->img_scale_x * ((self->img_width - dx) / self->img_width);
		yscale = xscale;
		_tmp27_ = bg;
		_tmp28_ = bird_font_background_image_get_img_middle_x (_tmp27_);
		_tmp29_ = _tmp28_;
		xc = _tmp29_;
		_tmp30_ = bg;
		_tmp31_ = bird_font_background_image_get_img_middle_y (_tmp30_);
		_tmp32_ = _tmp31_;
		yc = _tmp32_;
		_tmp33_ = bg;
		bird_font_background_image_set_img_scale (_tmp33_, xscale, yscale);
		_tmp34_ = bg;
		bird_font_background_image_set_img_middle_x (_tmp34_, xc);
		_tmp35_ = bg;
		bird_font_background_image_set_img_middle_y (_tmp35_, yc);
	}
	if (self->priv->move_bg) {
		BirdFontBackgroundImage* _tmp37_;
		_tmp37_ = bg;
		_tmp36_ = _tmp37_->selected_handle <= 0;
	} else {
		_tmp36_ = FALSE;
	}
	if (_tmp36_) {
		BirdFontBackgroundImage* _tmp38_;
		_tmp38_ = bg;
		bird_font_background_image_set_img_offset (_tmp38_, self->img_offset_x + dx, self->img_offset_y + dy);
	}
	bird_font_glyph_canvas_redraw ();
	self->priv->last_x = x;
	self->priv->last_y = y;
	_g_object_unref0 (bg);
	_g_object_unref0 (background_image);
	_g_object_unref0 (g);
}

static gboolean
__lambda157_ (void)
{
	BirdFontTabBar* tb = NULL;
	BirdFontTabBar* _tmp0_;
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp1_;
	BirdFontBackgroundImage* _tmp2_;
	BirdFontBackgroundImage* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gboolean result;
	_tmp0_ = bird_font_main_window_get_tab_bar ();
	tb = _tmp0_;
	_tmp1_ = bird_font_main_window_get_current_glyph ();
	g = _tmp1_;
	_tmp2_ = bird_font_background_tool_imported_background;
	bird_font_glyph_set_background_image (g, _tmp2_);
	_tmp3_ = bird_font_background_tool_imported_background;
	bird_font_background_image_center_in_glyph (_tmp3_, NULL);
	bird_font_toolbox_select_tool_by_name ("zoom_background_image");
	_tmp4_ = bird_font_font_display_get_name ((BirdFontFontDisplay*) g);
	_tmp5_ = _tmp4_;
	bird_font_tab_bar_select_tab_name (tb, _tmp5_);
	_g_free0 (_tmp5_);
	bird_font_toolbox_select_tool_by_name ("cut_background");
	bird_font_glyph_canvas_redraw ();
	result = FALSE;
	_g_object_unref0 (g);
	_g_object_unref0 (tb);
	return result;
}

static gboolean
___lambda157__gsource_func (gpointer self)
{
	gboolean result;
	result = __lambda157_ ();
	return result;
}

void
bird_font_background_tool_load_background_image (void)
{
	BirdFontBackgroundImage* _tmp0_;
	cairo_surface_t* _tmp1_;
	GSource* idle = NULL;
	GSource* _tmp2_;
	_tmp0_ = bird_font_background_tool_imported_background;
	_tmp1_ = bird_font_background_image_get_img (_tmp0_);
	_cairo_surface_destroy0 (bird_font_background_tool_imported_surface);
	bird_font_background_tool_imported_surface = _tmp1_;
	_tmp2_ = g_idle_source_new ();
	idle = _tmp2_;
	g_source_set_callback (idle, ___lambda157__gsource_func, NULL, NULL);
	g_source_attach (idle, NULL);
	_g_source_unref0 (idle);
}

static void
__lambda158_ (const gchar* fn)
{
	BirdFontBackgroundImage* bg = NULL;
	gchar* path = NULL;
	if (fn != NULL) {
		gchar* _tmp0_;
		const gchar* _tmp1_;
		BirdFontBackgroundImage* _tmp2_;
		BirdFontBackgroundImage* _tmp3_;
		BirdFontBackgroundImage* _tmp4_;
		BirdFontNativeWindow* _tmp5_;
		_tmp0_ = g_strdup ((const gchar*) fn);
		_g_free0 (path);
		path = _tmp0_;
		_tmp1_ = path;
		_tmp2_ = bird_font_background_image_new (_tmp1_);
		_g_object_unref0 (bg);
		bg = _tmp2_;
		_tmp3_ = bg;
		_tmp4_ = _g_object_ref0 (_tmp3_);
		_g_object_unref0 (bird_font_background_tool_imported_background);
		bird_font_background_tool_imported_background = _tmp4_;
		_tmp5_ = bird_font_main_window_native_window;
		bird_font_native_window_load_background_image (_tmp5_);
	}
	_g_free0 (path);
	_g_object_unref0 (bg);
}

static void
___lambda158__bird_font_file_chooser_file_selected (BirdFontFileChooser* _sender,
                                                    const gchar* path,
                                                    gpointer self)
{
	__lambda158_ (path);
}

void
bird_font_background_tool_import_background_image (void)
{
	BirdFontFileChooser* fc = NULL;
	BirdFontFileChooser* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	_tmp0_ = bird_font_file_chooser_new ();
	fc = _tmp0_;
	g_signal_connect (fc, "file-selected", (GCallback) ___lambda158__bird_font_file_chooser_file_selected, NULL);
	_tmp1_ = bird_font_t_ ("Select background image");
	_tmp2_ = _tmp1_;
	bird_font_main_window_file_chooser (_tmp2_, fc, BIRD_FONT_FILE_CHOOSER_LOAD);
	_g_free0 (_tmp2_);
	_g_object_unref0 (fc);
}

static void
bird_font_background_tool_class_init (BirdFontBackgroundToolClass * klass,
                                      gpointer klass_data)
{
	bird_font_background_tool_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontBackgroundTool_private_offset);
	((BirdFontToolClass *) klass)->get_tip = (gchar* (*) (BirdFontTool*)) bird_font_background_tool_real_get_tip;
	G_OBJECT_CLASS (klass)->finalize = bird_font_background_tool_finalize;
}

static void
bird_font_background_tool_instance_init (BirdFontBackgroundTool * self,
                                         gpointer klass)
{
	self->priv = bird_font_background_tool_get_instance_private (self);
	self->priv->begin_x = (gdouble) 0;
	self->priv->begin_y = (gdouble) 0;
	self->img_offset_x = (gdouble) 0;
	self->img_offset_y = (gdouble) 0;
	self->img_width = (gdouble) 0;
	self->img_height = (gdouble) 0;
	self->img_scale_x = (gdouble) 0;
	self->priv->rotation_position_x = (gdouble) 0;
	self->priv->rotation_position_y = (gdouble) 0;
}

static void
bird_font_background_tool_finalize (GObject * obj)
{
	BirdFontBackgroundTool * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_BACKGROUND_TOOL, BirdFontBackgroundTool);
	G_OBJECT_CLASS (bird_font_background_tool_parent_class)->finalize (obj);
}

static GType
bird_font_background_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontBackgroundToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_background_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontBackgroundTool), 0, (GInstanceInitFunc) bird_font_background_tool_instance_init, NULL };
	GType bird_font_background_tool_type_id;
	bird_font_background_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontBackgroundTool", &g_define_type_info, 0);
	BirdFontBackgroundTool_private_offset = g_type_add_instance_private (bird_font_background_tool_type_id, sizeof (BirdFontBackgroundToolPrivate));
	return bird_font_background_tool_type_id;
}

GType
bird_font_background_tool_get_type (void)
{
	static volatile gsize bird_font_background_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_background_tool_type_id__once)) {
		GType bird_font_background_tool_type_id;
		bird_font_background_tool_type_id = bird_font_background_tool_get_type_once ();
		g_once_init_leave (&bird_font_background_tool_type_id__once, bird_font_background_tool_type_id);
	}
	return bird_font_background_tool_type_id__once;
}

