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

/*
* Copyright (c) 2018-2026 (https://github.com/phase1geo/Minder)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authored by: Trevor Williams <phase1geo@gmail.com>
*/

#include <glib-object.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <gio/gio.h>
#include <cairo-gobject.h>
#include <float.h>
#include <math.h>
#include <string.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
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define TYPE_EXPORT_PRINT (export_print_get_type ())
#define EXPORT_PRINT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_EXPORT_PRINT, ExportPrint))
#define EXPORT_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_EXPORT_PRINT, ExportPrintClass))
#define IS_EXPORT_PRINT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_EXPORT_PRINT))
#define IS_EXPORT_PRINT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_EXPORT_PRINT))
#define EXPORT_PRINT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_EXPORT_PRINT, ExportPrintClass))

typedef struct _ExportPrint ExportPrint;
typedef struct _ExportPrintClass ExportPrintClass;
typedef struct _ExportPrintPrivate ExportPrintPrivate;

#define TYPE_MIND_MAP (mind_map_get_type ())
#define MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MIND_MAP, MindMap))
#define MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MIND_MAP, MindMapClass))
#define IS_MIND_MAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MIND_MAP))
#define IS_MIND_MAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MIND_MAP))
#define MIND_MAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MIND_MAP, MindMapClass))

typedef struct _MindMap MindMap;
typedef struct _MindMapClass MindMapClass;
enum  {
	EXPORT_PRINT_0_PROPERTY,
	EXPORT_PRINT_NUM_PROPERTIES
};
static GParamSpec* export_print_properties[EXPORT_PRINT_NUM_PROPERTIES];
#define _mind_map_unref0(var) ((var == NULL) ? NULL : (var = (mind_map_unref (var), NULL)))

#define TYPE_MAIN_WINDOW (main_window_get_type ())
#define MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAIN_WINDOW, MainWindow))
#define MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAIN_WINDOW, MainWindowClass))
#define IS_MAIN_WINDOW(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAIN_WINDOW))
#define IS_MAIN_WINDOW_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAIN_WINDOW))
#define MAIN_WINDOW_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAIN_WINDOW, MainWindowClass))

typedef struct _MainWindow MainWindow;
typedef struct _MainWindowClass MainWindowClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_variant_unref0(var) ((var == NULL) ? NULL : (var = (g_variant_unref (var), NULL)))

#define TYPE_MAP_MODEL (map_model_get_type ())
#define MAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_MAP_MODEL, MapModel))
#define MAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_MAP_MODEL, MapModelClass))
#define IS_MAP_MODEL(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_MAP_MODEL))
#define IS_MAP_MODEL_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_MAP_MODEL))
#define MAP_MODEL_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_MAP_MODEL, MapModelClass))

typedef struct _MapModel MapModel;
typedef struct _MapModelClass MapModelClass;
#define _cairo_destroy0(var) ((var == NULL) ? NULL : (var = (cairo_destroy (var), NULL)))

struct _ExportPrint {
	GObject parent_instance;
	ExportPrintPrivate * priv;
};

struct _ExportPrintClass {
	GObjectClass parent_class;
};

struct _ExportPrintPrivate {
	MindMap* _map;
};

static gint ExportPrint_private_offset;
static gpointer export_print_parent_class = NULL;
VALA_EXTERN GSettings* minder_settings;

VALA_EXTERN GType export_print_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (ExportPrint, g_object_unref)
VALA_EXTERN gpointer mind_map_ref (gpointer instance);
VALA_EXTERN void mind_map_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_mind_map (const gchar* name,
                                 const gchar* nick,
                                 const gchar* blurb,
                                 GType object_type,
                                 GParamFlags flags);
VALA_EXTERN void value_set_mind_map (GValue* value,
                         gpointer v_object);
VALA_EXTERN void value_take_mind_map (GValue* value,
                          gpointer v_object);
VALA_EXTERN gpointer value_get_mind_map (const GValue* value);
VALA_EXTERN GType mind_map_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MindMap, mind_map_unref)
VALA_EXTERN ExportPrint* export_print_new (void);
VALA_EXTERN ExportPrint* export_print_construct (GType object_type);
VALA_EXTERN GType main_window_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MainWindow, g_object_unref)
VALA_EXTERN void export_print_print (ExportPrint* self,
                         MindMap* map,
                         MainWindow* main);
VALA_EXTERN void export_print_draw_page (ExportPrint* self,
                             GtkPrintOperation* op,
                             GtkPrintContext* context,
                             gint page_nr);
static void _export_print_draw_page_gtk_print_operation_draw_page (GtkPrintOperation* _sender,
                                                            GtkPrintContext* context,
                                                            gint page_nr,
                                                            gpointer self);
VALA_EXTERN gpointer map_model_ref (gpointer instance);
VALA_EXTERN void map_model_unref (gpointer instance);
VALA_EXTERN GParamSpec* param_spec_map_model (const gchar* name,
                                  const gchar* nick,
                                  const gchar* blurb,
                                  GType object_type,
                                  GParamFlags flags);
VALA_EXTERN void value_set_map_model (GValue* value,
                          gpointer v_object);
VALA_EXTERN void value_take_map_model (GValue* value,
                           gpointer v_object);
VALA_EXTERN gpointer value_get_map_model (const GValue* value);
VALA_EXTERN GType map_model_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (MapModel, map_model_unref)
VALA_EXTERN MapModel* mind_map_get_model (MindMap* self);
VALA_EXTERN void map_model_document_rectangle (MapModel* self,
                                   gdouble* x,
                                   gdouble* y,
                                   gdouble* width,
                                   gdouble* height);
VALA_EXTERN void map_model_draw_all (MapModel* self,
                         cairo_t* ctx,
                         gboolean exporting,
                         gboolean moving_node);
static void export_print_finalize (GObject * obj);
static GType export_print_get_type_once (void);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
export_print_get_instance_private (ExportPrint* self)
{
	return G_STRUCT_MEMBER_P (self, ExportPrint_private_offset);
}

ExportPrint*
export_print_construct (GType object_type)
{
	ExportPrint * self = NULL;
	self = (ExportPrint*) g_object_new (object_type, NULL);
	return self;
}

ExportPrint*
export_print_new (void)
{
	return export_print_construct (TYPE_EXPORT_PRINT);
}

static gpointer
_mind_map_ref0 (gpointer self)
{
	return self ? mind_map_ref (self) : NULL;
}

static void
_export_print_draw_page_gtk_print_operation_draw_page (GtkPrintOperation* _sender,
                                                       GtkPrintContext* context,
                                                       gint page_nr,
                                                       gpointer self)
{
	export_print_draw_page ((ExportPrint*) self, _sender, context, page_nr);
}

void
export_print_print (ExportPrint* self,
                    MindMap* map,
                    MainWindow* main)
{
	MindMap* _tmp0_;
	GtkPrintOperation* op = NULL;
	GtkPrintOperation* _tmp1_;
	GtkPrintOperation* _tmp2_;
	GSettings* _tmp3_;
	GVariant* _tmp4_;
	GVariant* _tmp5_;
	GtkPrintSettings* _tmp6_;
	GtkPrintSettings* _tmp7_;
	GtkPrintOperation* _tmp8_;
	GSettings* _tmp9_;
	GVariant* _tmp10_;
	GVariant* _tmp11_;
	GtkPageSetup* _tmp12_;
	GtkPageSetup* _tmp13_;
	GtkPrintOperation* _tmp14_;
	GtkPrintOperation* _tmp15_;
	GtkPrintOperation* _tmp16_;
	GtkPrintOperation* _tmp17_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (map != NULL);
	g_return_if_fail (main != NULL);
	_tmp0_ = _mind_map_ref0 (map);
	_mind_map_unref0 (self->priv->_map);
	self->priv->_map = _tmp0_;
	_tmp1_ = gtk_print_operation_new ();
	op = _tmp1_;
	_tmp2_ = op;
	_tmp3_ = minder_settings;
	_tmp4_ = g_settings_get_value (_tmp3_, "print-settings");
	_tmp5_ = _tmp4_;
	_tmp6_ = gtk_print_settings_new_from_gvariant (_tmp5_);
	_tmp7_ = _tmp6_;
	gtk_print_operation_set_print_settings (_tmp2_, _tmp7_);
	_g_object_unref0 (_tmp7_);
	_g_variant_unref0 (_tmp5_);
	_tmp8_ = op;
	_tmp9_ = minder_settings;
	_tmp10_ = g_settings_get_value (_tmp9_, "page-setup");
	_tmp11_ = _tmp10_;
	_tmp12_ = gtk_page_setup_new_from_gvariant (_tmp11_);
	_tmp13_ = _tmp12_;
	gtk_print_operation_set_default_page_setup (_tmp8_, _tmp13_);
	_g_object_unref0 (_tmp13_);
	_g_variant_unref0 (_tmp11_);
	_tmp14_ = op;
	g_object_set (_tmp14_, "n-pages", 1, NULL);
	_tmp15_ = op;
	g_object_set (_tmp15_, "unit", GTK_UNIT_MM, NULL);
	_tmp16_ = op;
	gtk_print_operation_set_embed_page_setup (_tmp16_, TRUE);
	_tmp17_ = op;
	g_signal_connect_object (_tmp17_, "draw-page", (GCallback) _export_print_draw_page_gtk_print_operation_draw_page, self, 0);
	{
		GtkPrintOperationResult res = 0;
		GtkPrintOperation* _tmp18_;
		_tmp18_ = op;
		res = gtk_print_operation_run (_tmp18_, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, (GtkWindow*) main, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		switch (res) {
			case GTK_PRINT_OPERATION_RESULT_APPLY:
			{
				GSettings* _tmp19_;
				GtkPrintOperation* _tmp20_;
				GtkPrintSettings* _tmp21_;
				GtkPrintSettings* _tmp22_;
				GVariant* _tmp23_;
				GVariant* _tmp24_;
				GSettings* _tmp25_;
				GtkPrintOperation* _tmp26_;
				GtkPageSetup* _tmp27_;
				GtkPageSetup* _tmp28_;
				GVariant* _tmp29_;
				GVariant* _tmp30_;
				_tmp19_ = minder_settings;
				_tmp20_ = op;
				_tmp21_ = gtk_print_operation_get_print_settings (_tmp20_);
				_tmp22_ = _tmp21_;
				_tmp23_ = gtk_print_settings_to_gvariant (_tmp22_);
				g_variant_ref_sink (_tmp23_);
				_tmp24_ = _tmp23_;
				g_settings_set_value (_tmp19_, "print-settings", _tmp24_);
				_g_variant_unref0 (_tmp24_);
				_tmp25_ = minder_settings;
				_tmp26_ = op;
				_tmp27_ = gtk_print_operation_get_default_page_setup (_tmp26_);
				_tmp28_ = _tmp27_;
				_tmp29_ = gtk_page_setup_to_gvariant (_tmp28_);
				g_variant_ref_sink (_tmp29_);
				_tmp30_ = _tmp29_;
				g_settings_set_value (_tmp25_, "page-setup", _tmp30_);
				_g_variant_unref0 (_tmp30_);
				break;
			}
			case GTK_PRINT_OPERATION_RESULT_ERROR:
			{
				break;
			}
			case GTK_PRINT_OPERATION_RESULT_IN_PROGRESS:
			{
				break;
			}
			default:
			break;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (op);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return;
	}
	_g_object_unref0 (op);
}

static gpointer
_cairo_reference0 (gpointer self)
{
	return self ? cairo_reference (self) : NULL;
}

void
export_print_draw_page (ExportPrint* self,
                        GtkPrintOperation* op,
                        GtkPrintContext* context,
                        gint page_nr)
{
	cairo_t* ctx = NULL;
	cairo_t* _tmp0_;
	cairo_t* _tmp1_;
	gdouble page_width = 0.0;
	gdouble page_height = 0.0;
	gdouble x = 0.0;
	gdouble y = 0.0;
	gdouble w = 0.0;
	gdouble h = 0.0;
	MindMap* _tmp2_;
	MapModel* _tmp3_;
	MapModel* _tmp4_;
	gdouble _tmp5_ = 0.0;
	gdouble _tmp6_ = 0.0;
	gdouble _tmp7_ = 0.0;
	gdouble _tmp8_ = 0.0;
	gdouble width = 0.0;
	gdouble height = 0.0;
	gdouble _tmp9_ = 0.0;
	gdouble sf = 0.0;
	cairo_t* _tmp10_;
	cairo_t* _tmp11_;
	MindMap* _tmp12_;
	MapModel* _tmp13_;
	MapModel* _tmp14_;
	cairo_t* _tmp15_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (op != NULL);
	g_return_if_fail (context != NULL);
	_tmp0_ = gtk_print_context_get_cairo_context (context);
	_tmp1_ = _cairo_reference0 (_tmp0_);
	ctx = _tmp1_;
	page_width = gtk_print_context_get_width (context);
	page_height = gtk_print_context_get_height (context);
	_tmp2_ = self->priv->_map;
	_tmp3_ = mind_map_get_model (_tmp2_);
	_tmp4_ = _tmp3_;
	map_model_document_rectangle (_tmp4_, &_tmp5_, &_tmp6_, &_tmp7_, &_tmp8_);
	x = _tmp5_;
	y = _tmp6_;
	w = _tmp7_;
	h = _tmp8_;
	width = page_width / w;
	height = page_height / h;
	if (width < height) {
		_tmp9_ = width;
	} else {
		_tmp9_ = height;
	}
	sf = _tmp9_;
	_tmp10_ = ctx;
	cairo_scale (_tmp10_, sf, sf);
	_tmp11_ = ctx;
	cairo_translate (_tmp11_, 0 - x, 0 - y);
	_tmp12_ = self->priv->_map;
	_tmp13_ = mind_map_get_model (_tmp12_);
	_tmp14_ = _tmp13_;
	_tmp15_ = ctx;
	map_model_draw_all (_tmp14_, _tmp15_, TRUE, FALSE);
	_cairo_destroy0 (ctx);
}

static void
export_print_class_init (ExportPrintClass * klass,
                         gpointer klass_data)
{
	export_print_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &ExportPrint_private_offset);
	G_OBJECT_CLASS (klass)->finalize = export_print_finalize;
}

static void
export_print_instance_init (ExportPrint * self,
                            gpointer klass)
{
	self->priv = export_print_get_instance_private (self);
}

static void
export_print_finalize (GObject * obj)
{
	ExportPrint * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, TYPE_EXPORT_PRINT, ExportPrint);
	_mind_map_unref0 (self->priv->_map);
	G_OBJECT_CLASS (export_print_parent_class)->finalize (obj);
}

static GType
export_print_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (ExportPrintClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) export_print_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ExportPrint), 0, (GInstanceInitFunc) export_print_instance_init, NULL };
	GType export_print_type_id;
	export_print_type_id = g_type_register_static (G_TYPE_OBJECT, "ExportPrint", &g_define_type_info, 0);
	ExportPrint_private_offset = g_type_add_instance_private (export_print_type_id, sizeof (ExportPrintPrivate));
	return export_print_type_id;
}

GType
export_print_get_type (void)
{
	static volatile gsize export_print_type_id__once = 0;
	if (g_once_init_enter (&export_print_type_id__once)) {
		GType export_print_type_id;
		export_print_type_id = export_print_get_type_once ();
		g_once_init_leave (&export_print_type_id__once, export_print_type_id);
	}
	return export_print_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;
}

