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

/* -*- Mode: vala; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright © 2023 Jamie Murphy <jmurphy@gnome.org>
 *
 * This file is part of GNOME Sudoku.
 *
 * GNOME Sudoku 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 3 of the License, or
 * (at your option) any later version.
 *
 * GNOME Sudoku 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 GNOME Sudoku. If not, see <http://www.gnu.org/licenses/>.
 */

#include <gtk/gtk.h>
#include <glib-object.h>
#include <glib.h>
#include <float.h>
#include <math.h>

#define SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_DEFAULT_SIZE 25
#define SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_SMALL_SIZE 10
#define SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_SIZE_DIFF (SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_DEFAULT_SIZE - SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_SMALL_SIZE)
#define SUDOKU_WINDOW_MEDIUM_WINDOW_WIDTH 600
#define SUDOKU_WINDOW_SMALL_WINDOW_WIDTH 360
#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_SUDOKU_GRID_LAYOUT_MANAGER (sudoku_grid_layout_manager_get_type ())
#define SUDOKU_GRID_LAYOUT_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SUDOKU_GRID_LAYOUT_MANAGER, SudokuGridLayoutManager))
#define SUDOKU_GRID_LAYOUT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SUDOKU_GRID_LAYOUT_MANAGER, SudokuGridLayoutManagerClass))
#define IS_SUDOKU_GRID_LAYOUT_MANAGER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SUDOKU_GRID_LAYOUT_MANAGER))
#define IS_SUDOKU_GRID_LAYOUT_MANAGER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SUDOKU_GRID_LAYOUT_MANAGER))
#define SUDOKU_GRID_LAYOUT_MANAGER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SUDOKU_GRID_LAYOUT_MANAGER, SudokuGridLayoutManagerClass))

typedef struct _SudokuGridLayoutManager SudokuGridLayoutManager;
typedef struct _SudokuGridLayoutManagerClass SudokuGridLayoutManagerClass;
typedef struct _SudokuGridLayoutManagerPrivate SudokuGridLayoutManagerPrivate;
enum  {
	SUDOKU_GRID_LAYOUT_MANAGER_0_PROPERTY,
	SUDOKU_GRID_LAYOUT_MANAGER_NUM_PROPERTIES
};
static GParamSpec* sudoku_grid_layout_manager_properties[SUDOKU_GRID_LAYOUT_MANAGER_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _SudokuGridLayoutManager {
	GtkLayoutManager parent_instance;
	SudokuGridLayoutManagerPrivate * priv;
};

struct _SudokuGridLayoutManagerClass {
	GtkLayoutManagerClass parent_class;
};

static gpointer sudoku_grid_layout_manager_parent_class = NULL;

VALA_EXTERN GType sudoku_grid_layout_manager_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (SudokuGridLayoutManager, g_object_unref)
static gint sudoku_grid_layout_manager_get_align (SudokuGridLayoutManager* self,
                                           gint size);
static gdouble sudoku_grid_layout_manager_normalize (SudokuGridLayoutManager* self,
                                              gint val,
                                              gint min,
                                              gint max);
static void sudoku_grid_layout_manager_real_measure (GtkLayoutManager* base,
                                              GtkWidget* widget,
                                              GtkOrientation orientation,
                                              gint for_size,
                                              gint* minimum,
                                              gint* natural,
                                              gint* minimum_baseline,
                                              gint* natural_baseline);
static void sudoku_grid_layout_manager_real_allocate (GtkLayoutManager* base,
                                               GtkWidget* widget,
                                               gint width,
                                               gint height,
                                               gint baseline);
VALA_EXTERN SudokuGridLayoutManager* sudoku_grid_layout_manager_new (void);
VALA_EXTERN SudokuGridLayoutManager* sudoku_grid_layout_manager_construct (GType object_type);
static GType sudoku_grid_layout_manager_get_type_once (void);

static gint
sudoku_grid_layout_manager_get_align (SudokuGridLayoutManager* self,
                                      gint size)
{
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	if (size > SUDOKU_WINDOW_MEDIUM_WINDOW_WIDTH) {
		result = SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_DEFAULT_SIZE;
		return result;
	} else {
		gdouble factor = 0.0;
		factor = sudoku_grid_layout_manager_normalize (self, size, SUDOKU_WINDOW_SMALL_WINDOW_WIDTH, SUDOKU_WINDOW_MEDIUM_WINDOW_WIDTH);
		result = SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_SMALL_SIZE + ((gint) (SUDOKU_GRID_LAYOUT_MANAGER_MARGIN_SIZE_DIFF * factor));
		return result;
	}
}

static gdouble
sudoku_grid_layout_manager_normalize (SudokuGridLayoutManager* self,
                                      gint val,
                                      gint min,
                                      gint max)
{
	gdouble result;
	g_return_val_if_fail (self != NULL, -1.0);
	CLAMP (val, min, max);
	result = (val - min) / ((gdouble) (max - min));
	return result;
}

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

static void
sudoku_grid_layout_manager_real_measure (GtkLayoutManager* base,
                                         GtkWidget* widget,
                                         GtkOrientation orientation,
                                         gint for_size,
                                         gint* minimum,
                                         gint* natural,
                                         gint* minimum_baseline,
                                         gint* natural_baseline)
{
	SudokuGridLayoutManager * self;
	gint _vala_minimum = 0;
	gint _vala_natural = 0;
	gint _vala_minimum_baseline = 0;
	gint _vala_natural_baseline = 0;
	GtkWidget* child = NULL;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	gboolean _tmp2_;
	gboolean _tmp3_;
	self = (SudokuGridLayoutManager*) base;
	g_return_if_fail (widget != NULL);
	_tmp0_ = gtk_widget_get_first_child (widget);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	child = _tmp1_;
	_tmp2_ = gtk_widget_get_visible (widget);
	_tmp3_ = _tmp2_;
	if (_tmp3_) {
		gint child_height_min = 0;
		gint child_height_nat = 0;
		gint child_width_min = 0;
		gint child_width_nat = 0;
		GtkWidget* _tmp4_;
		gint _tmp5_ = 0;
		gint _tmp6_ = 0;
		GtkWidget* _tmp7_;
		gint _tmp8_ = 0;
		gint _tmp9_ = 0;
		_tmp4_ = child;
		gtk_widget_measure (_tmp4_, GTK_ORIENTATION_HORIZONTAL, -1, &_tmp5_, &_tmp6_, NULL, NULL);
		child_width_min = _tmp5_;
		child_width_nat = _tmp6_;
		_tmp7_ = child;
		gtk_widget_measure (_tmp7_, GTK_ORIENTATION_VERTICAL, -1, &_tmp8_, &_tmp9_, NULL, NULL);
		child_height_min = _tmp8_;
		child_height_nat = _tmp9_;
		_vala_minimum = MAX (child_height_min, child_width_min);
		_vala_natural = MAX (child_height_nat, child_width_nat);
	} else {
		_vala_natural = 0;
		_vala_minimum = _vala_natural;
	}
	_vala_natural_baseline = -1;
	_vala_minimum_baseline = _vala_natural_baseline;
	_g_object_unref0 (child);
	if (minimum) {
		*minimum = _vala_minimum;
	}
	if (natural) {
		*natural = _vala_natural;
	}
	if (minimum_baseline) {
		*minimum_baseline = _vala_minimum_baseline;
	}
	if (natural_baseline) {
		*natural_baseline = _vala_natural_baseline;
	}
}

static void
sudoku_grid_layout_manager_real_allocate (GtkLayoutManager* base,
                                          GtkWidget* widget,
                                          gint width,
                                          gint height,
                                          gint baseline)
{
	SudokuGridLayoutManager * self;
	GtkWidget* child = NULL;
	GtkWidget* _tmp0_;
	GtkWidget* _tmp1_;
	gint child_width = 0;
	gint child_height = 0;
	gint halign = 0;
	gint valign = 0;
	gint start = 0;
	gint top = 0;
	gint maximum_top_offset = 0;
	GtkAllocation child_allocation = {0};
	GtkAllocation _tmp2_ = {0};
	GtkAllocation _tmp3_;
	self = (SudokuGridLayoutManager*) base;
	g_return_if_fail (widget != NULL);
	_tmp0_ = gtk_widget_get_first_child (widget);
	_tmp1_ = _g_object_ref0 (_tmp0_);
	child = _tmp1_;
	halign = sudoku_grid_layout_manager_get_align (self, width);
	valign = sudoku_grid_layout_manager_get_align (self, height);
	child_height = MIN (width, height) - (MIN (halign, valign) * 2);
	child_width = child_height;
	start = MAX (halign, (width - child_width) / 2);
	top = MAX (valign, (height - child_height) / 2);
	maximum_top_offset = 40;
	top = MIN (top, maximum_top_offset);
	_tmp2_.x = start;
	_tmp2_.y = top;
	_tmp2_.width = child_width;
	_tmp2_.height = child_height;
	child_allocation = _tmp2_;
	_tmp3_ = child_allocation;
	gtk_widget_size_allocate (child, &_tmp3_, baseline);
	_g_object_unref0 (child);
}

SudokuGridLayoutManager*
sudoku_grid_layout_manager_construct (GType object_type)
{
	SudokuGridLayoutManager * self = NULL;
	self = (SudokuGridLayoutManager*) g_object_new (object_type, NULL);
	return self;
}

SudokuGridLayoutManager*
sudoku_grid_layout_manager_new (void)
{
	return sudoku_grid_layout_manager_construct (TYPE_SUDOKU_GRID_LAYOUT_MANAGER);
}

static void
sudoku_grid_layout_manager_class_init (SudokuGridLayoutManagerClass * klass,
                                       gpointer klass_data)
{
	sudoku_grid_layout_manager_parent_class = g_type_class_peek_parent (klass);
	((GtkLayoutManagerClass *) klass)->measure = (void (*) (GtkLayoutManager*, GtkWidget*, GtkOrientation, gint, gint*, gint*, gint*, gint*)) sudoku_grid_layout_manager_real_measure;
	((GtkLayoutManagerClass *) klass)->allocate = (void (*) (GtkLayoutManager*, GtkWidget*, gint, gint, gint)) sudoku_grid_layout_manager_real_allocate;
}

static void
sudoku_grid_layout_manager_instance_init (SudokuGridLayoutManager * self,
                                          gpointer klass)
{
}

static GType
sudoku_grid_layout_manager_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (SudokuGridLayoutManagerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) sudoku_grid_layout_manager_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SudokuGridLayoutManager), 0, (GInstanceInitFunc) sudoku_grid_layout_manager_instance_init, NULL };
	GType sudoku_grid_layout_manager_type_id;
	sudoku_grid_layout_manager_type_id = g_type_register_static (gtk_layout_manager_get_type (), "SudokuGridLayoutManager", &g_define_type_info, 0);
	return sudoku_grid_layout_manager_type_id;
}

GType
sudoku_grid_layout_manager_get_type (void)
{
	static volatile gsize sudoku_grid_layout_manager_type_id__once = 0;
	if (g_once_init_enter (&sudoku_grid_layout_manager_type_id__once)) {
		GType sudoku_grid_layout_manager_type_id;
		sudoku_grid_layout_manager_type_id = sudoku_grid_layout_manager_get_type_once ();
		g_once_init_leave (&sudoku_grid_layout_manager_type_id__once, sudoku_grid_layout_manager_type_id);
	}
	return sudoku_grid_layout_manager_type_id__once;
}

