/* ZoomTool.c generated by valac 0.56.17, the Vala compiler
 * generated from ZoomTool.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 <gee.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <float.h>
#include <math.h>
#include <cairo-gobject.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_ZOOM_TOOL_0_PROPERTY,
	BIRD_FONT_ZOOM_TOOL_NUM_PROPERTIES
};
static GParamSpec* bird_font_zoom_tool_properties[BIRD_FONT_ZOOM_TOOL_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _BirdFontZoomToolPrivate {
	gint zoom_area_begin_x;
	gint zoom_area_begin_y;
	gint view_index;
	GeeArrayList* views;
};

static gint BirdFontZoomTool_private_offset;
static gpointer bird_font_zoom_tool_parent_class = NULL;

static void __lambda82_ (BirdFontZoomTool* self,
                  BirdFontTool* _self_);
static void ___lambda82__bird_font_tool_select_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gpointer self);
static void __lambda83_ (BirdFontZoomTool* self,
                  BirdFontTool* _self_);
static void ___lambda83__bird_font_tool_select_action (BirdFontTool* _sender,
                                                BirdFontTool* selected,
                                                gpointer self);
static void __lambda84_ (BirdFontZoomTool* self,
                  BirdFontTool* _self_,
                  gint b,
                  gint x,
                  gint y);
static void ___lambda84__bird_font_tool_press_action (BirdFontTool* _sender,
                                               BirdFontTool* selected,
                                               gint button,
                                               gint x,
                                               gint y,
                                               gpointer self);
static void __lambda85_ (BirdFontZoomTool* self,
                  BirdFontTool* _self_,
                  gint x,
                  gint y);
static void ___lambda85__bird_font_tool_move_action (BirdFontTool* _sender,
                                              BirdFontTool* selected,
                                              gint x,
                                              gint y,
                                              gpointer self);
static void __lambda86_ (BirdFontZoomTool* self,
                  BirdFontTool* _self_,
                  gint b,
                  gint x,
                  gint y);
static void ___lambda86__bird_font_tool_release_action (BirdFontTool* _sender,
                                                 BirdFontTool* selected,
                                                 gint button,
                                                 gint x,
                                                 gint y,
                                                 gpointer self);
static void __lambda87_ (BirdFontZoomTool* self,
                  BirdFontTool* tool,
                  cairo_t* cairo_context,
                  BirdFontGlyph* glyph);
static void ___lambda87__bird_font_tool_draw_action (BirdFontTool* _sender,
                                              BirdFontTool* selected,
                                              cairo_t* cr,
                                              BirdFontGlyph* glyph,
                                              gpointer self);
static void bird_font_zoom_tool_finalize (GObject * obj);
static GType bird_font_zoom_tool_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
bird_font_zoom_tool_get_instance_private (BirdFontZoomTool* self)
{
	return G_STRUCT_MEMBER_P (self, BirdFontZoomTool_private_offset);
}

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

static void
___lambda82__bird_font_tool_select_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gpointer self)
{
	__lambda82_ ((BirdFontZoomTool*) self, selected);
}

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

static void
___lambda83__bird_font_tool_select_action (BirdFontTool* _sender,
                                           BirdFontTool* selected,
                                           gpointer self)
{
	__lambda83_ ((BirdFontZoomTool*) self, selected);
}

static void
__lambda84_ (BirdFontZoomTool* self,
             BirdFontTool* _self_,
             gint b,
             gint x,
             gint y)
{
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	g_return_if_fail (_self_ != NULL);
	if (b == 1) {
		_tmp1_ = !bird_font_key_bindings_has_ctrl ();
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		_tmp0_ = !bird_font_key_bindings_has_shift ();
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		BirdFontGlyph* g = NULL;
		BirdFontGlyph* _tmp2_;
		BirdFontGlyph* _tmp3_;
		self->priv->zoom_area_begin_x = x;
		self->priv->zoom_area_begin_y = y;
		_tmp2_ = bird_font_main_window_get_current_glyph ();
		g = _tmp2_;
		_tmp3_ = g;
		_tmp3_->zoom_area_is_visible = TRUE;
		_g_object_unref0 (g);
	}
}

static void
___lambda84__bird_font_tool_press_action (BirdFontTool* _sender,
                                          BirdFontTool* selected,
                                          gint button,
                                          gint x,
                                          gint y,
                                          gpointer self)
{
	__lambda84_ ((BirdFontZoomTool*) self, selected, button, x, y);
}

static void
__lambda85_ (BirdFontZoomTool* self,
             BirdFontTool* _self_,
             gint x,
             gint y)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontGlyph* _tmp1_;
	g_return_if_fail (_self_ != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	if (_tmp1_->zoom_area_is_visible) {
		BirdFontGlyph* _tmp2_;
		_tmp2_ = g;
		bird_font_glyph_show_zoom_area (_tmp2_, self->priv->zoom_area_begin_x, self->priv->zoom_area_begin_y, x, y);
	}
	_g_object_unref0 (g);
}

static void
___lambda85__bird_font_tool_move_action (BirdFontTool* _sender,
                                         BirdFontTool* selected,
                                         gint x,
                                         gint y,
                                         gpointer self)
{
	__lambda85_ ((BirdFontZoomTool*) self, selected, x, y);
}

static void
__lambda86_ (BirdFontZoomTool* self,
             BirdFontTool* _self_,
             gint b,
             gint x,
             gint y)
{
	BirdFontGlyph* g = NULL;
	gboolean _tmp0_ = FALSE;
	gboolean _tmp1_ = FALSE;
	g_return_if_fail (_self_ != NULL);
	if (b == 1) {
		_tmp1_ = !bird_font_key_bindings_has_ctrl ();
	} else {
		_tmp1_ = FALSE;
	}
	if (_tmp1_) {
		_tmp0_ = !bird_font_key_bindings_has_shift ();
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		BirdFontGlyph* _tmp2_;
		gboolean _tmp3_ = FALSE;
		BirdFontGlyph* _tmp6_;
		bird_font_zoom_tool_store_current_view (self);
		_tmp2_ = bird_font_main_window_get_current_glyph ();
		_g_object_unref0 (g);
		g = _tmp2_;
		if (self->priv->zoom_area_begin_x == x) {
			_tmp3_ = self->priv->zoom_area_begin_y == y;
		} else {
			_tmp3_ = FALSE;
		}
		if (_tmp3_) {
			BirdFontGlyph* _tmp4_;
			_tmp4_ = g;
			bird_font_glyph_set_center (_tmp4_, (gdouble) x, (gdouble) y);
		} else {
			BirdFontGlyph* _tmp5_;
			_tmp5_ = g;
			bird_font_glyph_set_zoom_from_area (_tmp5_);
		}
		_tmp6_ = g;
		_tmp6_->zoom_area_is_visible = FALSE;
		self->priv->zoom_area_begin_x = -1;
		self->priv->zoom_area_begin_y = -1;
	}
	_g_object_unref0 (g);
}

static void
___lambda86__bird_font_tool_release_action (BirdFontTool* _sender,
                                            BirdFontTool* selected,
                                            gint button,
                                            gint x,
                                            gint y,
                                            gpointer self)
{
	__lambda86_ ((BirdFontZoomTool*) self, selected, button, x, y);
}

static void
__lambda87_ (BirdFontZoomTool* self,
             BirdFontTool* tool,
             cairo_t* cairo_context,
             BirdFontGlyph* glyph)
{
	g_return_if_fail (tool != NULL);
	g_return_if_fail (cairo_context != NULL);
	g_return_if_fail (glyph != NULL);
	bird_font_zoom_tool_draw_zoom_area (self, cairo_context);
}

static void
___lambda87__bird_font_tool_draw_action (BirdFontTool* _sender,
                                         BirdFontTool* selected,
                                         cairo_t* cr,
                                         BirdFontGlyph* glyph,
                                         gpointer self)
{
	__lambda87_ ((BirdFontZoomTool*) self, selected, cr, glyph);
}

BirdFontZoomTool*
bird_font_zoom_tool_construct (GType object_type,
                               const gchar* n)
{
	BirdFontZoomTool * self = NULL;
	GeeArrayList* _tmp0_;
	g_return_val_if_fail (n != NULL, NULL);
	self = (BirdFontZoomTool*) bird_font_tool_construct (object_type, n, "Zoom");
	_tmp0_ = gee_array_list_new (BIRD_FONT_TYPE_TAB, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, NULL, NULL, NULL);
	_g_object_unref0 (self->priv->views);
	self->priv->views = _tmp0_;
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda82__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "select-action", (GCallback) ___lambda83__bird_font_tool_select_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "press-action", (GCallback) ___lambda84__bird_font_tool_press_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "move-action", (GCallback) ___lambda85__bird_font_tool_move_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "release-action", (GCallback) ___lambda86__bird_font_tool_release_action, self, 0);
	g_signal_connect_object ((BirdFontTool*) self, "draw-action", (GCallback) ___lambda87__bird_font_tool_draw_action, self, 0);
	return self;
}

BirdFontZoomTool*
bird_font_zoom_tool_new (const gchar* n)
{
	return bird_font_zoom_tool_construct (BIRD_FONT_TYPE_ZOOM_TOOL, n);
}

void
bird_font_zoom_tool_draw_zoom_area (BirdFontZoomTool* self,
                                    cairo_t* cr)
{
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	BirdFontGlyph* _tmp1_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (cr != NULL);
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	if (_tmp1_->zoom_area_is_visible) {
		BirdFontGlyph* _tmp2_;
		BirdFontGlyph* _tmp3_;
		BirdFontGlyph* _tmp4_;
		BirdFontGlyph* _tmp5_;
		BirdFontGlyph* _tmp6_;
		BirdFontGlyph* _tmp7_;
		BirdFontGlyph* _tmp8_;
		BirdFontGlyph* _tmp9_;
		cairo_save (cr);
		cairo_set_line_width (cr, 2.0);
		bird_font_theme_color (cr, "Selection Border");
		_tmp2_ = g;
		_tmp3_ = g;
		_tmp4_ = g;
		_tmp5_ = g;
		_tmp6_ = g;
		_tmp7_ = g;
		_tmp8_ = g;
		_tmp9_ = g;
		cairo_rectangle (cr, fmin (_tmp2_->zoom_x1, _tmp3_->zoom_x2), fmin (_tmp4_->zoom_y1, _tmp5_->zoom_y2), fabs (_tmp6_->zoom_x1 - _tmp7_->zoom_x2), fabs (_tmp8_->zoom_y1 - _tmp9_->zoom_y2));
		cairo_stroke (cr);
		cairo_restore (cr);
	}
	_g_object_unref0 (g);
}

void
bird_font_zoom_tool_zoom_full_background_image (void)
{
	BirdFontBackgroundImage* bg = NULL;
	BirdFontGlyph* g = NULL;
	BirdFontGlyph* _tmp0_;
	gint x = 0;
	gint y = 0;
	BirdFontGlyph* _tmp1_;
	BirdFontGlyph* _tmp2_;
	BirdFontBackgroundImage* _tmp3_;
	BirdFontBackgroundImage* _tmp4_;
	gboolean _tmp5_;
	BirdFontGlyph* _tmp6_;
	BirdFontBackgroundImage* _tmp7_;
	BirdFontBackgroundImage* _tmp8_;
	gdouble _tmp9_;
	gdouble _tmp10_;
	BirdFontBackgroundImage* _tmp11_;
	gdouble _tmp12_;
	gdouble _tmp13_;
	BirdFontGlyph* _tmp14_;
	BirdFontBackgroundImage* _tmp15_;
	gint _tmp16_;
	gint _tmp17_;
	BirdFontBackgroundImage* _tmp18_;
	gdouble _tmp19_;
	gdouble _tmp20_;
	BirdFontBackgroundImage* _tmp21_;
	gint _tmp22_;
	gint _tmp23_;
	BirdFontBackgroundImage* _tmp24_;
	gdouble _tmp25_;
	gdouble _tmp26_;
	BirdFontGlyph* _tmp27_;
	_tmp0_ = bird_font_main_window_get_current_glyph ();
	g = _tmp0_;
	_tmp1_ = g;
	bird_font_font_display_reset_zoom ((BirdFontFontDisplay*) _tmp1_);
	_tmp2_ = g;
	_tmp3_ = bird_font_glyph_get_background_image (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = _tmp4_ == NULL;
	_g_object_unref0 (_tmp4_);
	if (_tmp5_) {
		_g_object_unref0 (g);
		_g_object_unref0 (bg);
		return;
	}
	_tmp6_ = g;
	_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);
	_tmp8_ = bg;
	_tmp9_ = bird_font_background_image_get_img_offset_x (_tmp8_);
	_tmp10_ = _tmp9_;
	x = (gint) _tmp10_;
	_tmp11_ = bg;
	_tmp12_ = bird_font_background_image_get_img_offset_y (_tmp11_);
	_tmp13_ = _tmp12_;
	y = (gint) _tmp13_;
	_tmp14_ = g;
	_tmp15_ = bg;
	_tmp16_ = bird_font_background_image_get_size_margin (_tmp15_);
	_tmp17_ = _tmp16_;
	_tmp18_ = bg;
	_tmp19_ = bird_font_background_image_get_img_scale_x (_tmp18_);
	_tmp20_ = _tmp19_;
	_tmp21_ = bg;
	_tmp22_ = bird_font_background_image_get_size_margin (_tmp21_);
	_tmp23_ = _tmp22_;
	_tmp24_ = bg;
	_tmp25_ = bird_font_background_image_get_img_scale_y (_tmp24_);
	_tmp26_ = _tmp25_;
	bird_font_glyph_set_zoom_area (_tmp14_, x, y, (gint) (x + (_tmp17_ * _tmp20_)), (gint) (y + (_tmp23_ * _tmp26_)));
	_tmp27_ = g;
	bird_font_glyph_set_zoom_from_area (_tmp27_);
	_g_object_unref0 (g);
	_g_object_unref0 (bg);
}

void
bird_font_zoom_tool_zoom_full_glyph (BirdFontZoomTool* self)
{
	BirdFontFontDisplay* _tmp0_;
	BirdFontFontDisplay* _tmp1_;
	g_return_if_fail (self != NULL);
	bird_font_zoom_tool_store_current_view (self);
	_tmp0_ = bird_font_main_window_get_current_display ();
	_tmp1_ = _tmp0_;
	bird_font_font_display_zoom_min (_tmp1_);
	_g_object_unref0 (_tmp1_);
}

/** Add an item to zoom view list. */
void
bird_font_zoom_tool_store_current_view (BirdFontZoomTool* self)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	GeeArrayList* _tmp9_;
	BirdFontTab* _tmp10_;
	BirdFontTab* _tmp11_;
	GeeArrayList* _tmp12_;
	gint _tmp13_;
	gint _tmp14_;
	BirdFontFontDisplay* _tmp15_;
	BirdFontFontDisplay* _tmp16_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->views;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	if ((_tmp2_ - 1) > self->priv->view_index) {
		gint i = 0;
		i = self->priv->view_index + 1;
		while (TRUE) {
			GeeArrayList* _tmp3_;
			gint _tmp4_;
			gint _tmp5_;
			GeeArrayList* _tmp6_;
			gpointer _tmp7_;
			BirdFontTab* _tmp8_;
			_tmp3_ = self->priv->views;
			_tmp4_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp3_);
			_tmp5_ = _tmp4_;
			if (!(i != (_tmp5_ - 1))) {
				break;
			}
			_tmp6_ = self->priv->views;
			_tmp7_ = gee_abstract_list_remove_at ((GeeAbstractList*) _tmp6_, i);
			_tmp8_ = (BirdFontTab*) _tmp7_;
			_g_object_unref0 (_tmp8_);
		}
	}
	_tmp9_ = self->priv->views;
	_tmp10_ = bird_font_main_window_get_current_tab ();
	_tmp11_ = _tmp10_;
	gee_abstract_collection_add ((GeeAbstractCollection*) _tmp9_, _tmp11_);
	_g_object_unref0 (_tmp11_);
	_tmp12_ = self->priv->views;
	_tmp13_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp12_);
	_tmp14_ = _tmp13_;
	self->priv->view_index = ((gint) _tmp14_) - 1;
	_tmp15_ = bird_font_main_window_get_current_display ();
	_tmp16_ = _tmp15_;
	bird_font_font_display_store_current_view (_tmp16_);
	_g_object_unref0 (_tmp16_);
}

/** Redo last zoom.*/
void
bird_font_zoom_tool_next_view (BirdFontZoomTool* self)
{
	GeeArrayList* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	gint _tmp3_;
	BirdFontFontDisplay* _tmp4_;
	BirdFontFontDisplay* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->views;
	_tmp1_ = gee_abstract_collection_get_size ((GeeAbstractCollection*) _tmp0_);
	_tmp2_ = _tmp1_;
	if ((self->priv->view_index + 1) >= ((gint) _tmp2_)) {
		return;
	}
	_tmp3_ = self->priv->view_index;
	self->priv->view_index = _tmp3_ + 1;
	_tmp4_ = bird_font_main_window_get_current_display ();
	_tmp5_ = _tmp4_;
	bird_font_font_display_next_view (_tmp5_);
	_g_object_unref0 (_tmp5_);
	bird_font_glyph_canvas_redraw ();
}

/** Undo last zoom. */
void
bird_font_zoom_tool_previous_view (BirdFontZoomTool* self)
{
	gint _tmp0_;
	BirdFontFontDisplay* _tmp1_;
	BirdFontFontDisplay* _tmp2_;
	g_return_if_fail (self != NULL);
	if (self->priv->view_index == 0) {
		return;
	}
	_tmp0_ = self->priv->view_index;
	self->priv->view_index = _tmp0_ - 1;
	_tmp1_ = bird_font_main_window_get_current_display ();
	_tmp2_ = _tmp1_;
	bird_font_font_display_restore_last_view (_tmp2_);
	_g_object_unref0 (_tmp2_);
	bird_font_glyph_canvas_redraw ();
}

static void
bird_font_zoom_tool_class_init (BirdFontZoomToolClass * klass,
                                gpointer klass_data)
{
	bird_font_zoom_tool_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &BirdFontZoomTool_private_offset);
	G_OBJECT_CLASS (klass)->finalize = bird_font_zoom_tool_finalize;
}

static void
bird_font_zoom_tool_instance_init (BirdFontZoomTool * self,
                                   gpointer klass)
{
	self->priv = bird_font_zoom_tool_get_instance_private (self);
	self->priv->zoom_area_begin_x = -1;
	self->priv->zoom_area_begin_y = -1;
	self->priv->view_index = 0;
}

static void
bird_font_zoom_tool_finalize (GObject * obj)
{
	BirdFontZoomTool * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, BIRD_FONT_TYPE_ZOOM_TOOL, BirdFontZoomTool);
	_g_object_unref0 (self->priv->views);
	G_OBJECT_CLASS (bird_font_zoom_tool_parent_class)->finalize (obj);
}

static GType
bird_font_zoom_tool_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (BirdFontZoomToolClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) bird_font_zoom_tool_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (BirdFontZoomTool), 0, (GInstanceInitFunc) bird_font_zoom_tool_instance_init, NULL };
	GType bird_font_zoom_tool_type_id;
	bird_font_zoom_tool_type_id = g_type_register_static (BIRD_FONT_TYPE_TOOL, "BirdFontZoomTool", &g_define_type_info, 0);
	BirdFontZoomTool_private_offset = g_type_add_instance_private (bird_font_zoom_tool_type_id, sizeof (BirdFontZoomToolPrivate));
	return bird_font_zoom_tool_type_id;
}

GType
bird_font_zoom_tool_get_type (void)
{
	static volatile gsize bird_font_zoom_tool_type_id__once = 0;
	if (g_once_init_enter (&bird_font_zoom_tool_type_id__once)) {
		GType bird_font_zoom_tool_type_id;
		bird_font_zoom_tool_type_id = bird_font_zoom_tool_get_type_once ();
		g_once_init_leave (&bird_font_zoom_tool_type_id__once, bird_font_zoom_tool_type_id);
	}
	return bird_font_zoom_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;
}

