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

/* SPDX-License-Identifier: GPL-3.0-or-later*/

#include "graphs.h"
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <adwaita.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  {
	GRAPHS_STYLE_COLOR_ROW_0_PROPERTY,
	GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY,
	GRAPHS_STYLE_COLOR_ROW_NUM_PROPERTIES
};
static GParamSpec* graphs_style_color_row_properties[GRAPHS_STYLE_COLOR_ROW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define __vala_GdkRGBA_free0(var) ((var == NULL) ? NULL : (var = (_vala_GdkRGBA_free (var), NULL)))
typedef struct _GraphsStyleColorRowOnColorChooseData GraphsStyleColorRowOnColorChooseData;

struct _GraphsStyleColorRowPrivate {
	GtkImage* _image;
	GdkRGBA _color;
	GtkCssProvider* provider;
};

struct _GraphsStyleColorRowOnColorChooseData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GraphsStyleColorRow* self;
	GtkColorDialog* dialog;
	GtkColorDialog* _tmp0_;
	GdkRGBA* _tmp1_;
	GtkRoot* _tmp2_;
	GdkRGBA _tmp3_;
	GdkRGBA* _tmp4_;
	GdkRGBA _tmp5_;
	GError* _inner_error0_;
};

static gint GraphsStyleColorRow_private_offset;
static gpointer graphs_style_color_row_parent_class = NULL;

static void graphs_style_color_row_on_color (GraphsStyleColorRow* self);
static void graphs_style_color_row_on_color_choose_data_free (gpointer _data);
static void graphs_style_color_row_on_color_choose (GraphsStyleColorRow* self,
                                             GAsyncReadyCallback _callback_,
                                             gpointer _user_data_);
static void graphs_style_color_row_on_color_choose_finish (GraphsStyleColorRow* self,
                                                    GAsyncResult* _res_);
static gboolean graphs_style_color_row_on_color_choose_co (GraphsStyleColorRowOnColorChooseData* _data_);
static void graphs_style_color_row_on_color_choose_ready (GObject* source_object,
                                                   GAsyncResult* _res_,
                                                   gpointer _user_data_);
static void _vala_GdkRGBA_free (GdkRGBA* self);
static void _graphs_style_color_row_on_color_choose_adw_action_row_activated (AdwActionRow* _sender,
                                                                       gpointer self);
static GtkImage* graphs_style_color_row_get_image (GraphsStyleColorRow* self);
static gboolean _gdk_rgba_equal (const GdkRGBA * s1,
                          const GdkRGBA * s2);
static GObject * graphs_style_color_row_constructor (GType type,
                                              guint n_construct_properties,
                                              GObjectConstructParam * construct_properties);
static void _graphs_style_color_row_on_color_g_object_notify (GObject* _sender,
                                                       GParamSpec* pspec,
                                                       gpointer self);
static void graphs_style_color_row_finalize (GObject * obj);
static GType graphs_style_color_row_get_type_once (void);
static void _vala_graphs_style_color_row_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec);
static void _vala_graphs_style_color_row_set_property (GObject * object,
                                                guint property_id,
                                                const GValue * value,
                                                GParamSpec * pspec);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
graphs_style_color_row_get_instance_private (GraphsStyleColorRow* self)
{
	return G_STRUCT_MEMBER_P (self, GraphsStyleColorRow_private_offset);
}

static const gchar*
string_to_string (const gchar* self)
{
	const gchar* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self;
	return result;
}

static void
graphs_style_color_row_on_color (GraphsStyleColorRow* self)
{
	gchar* hex = NULL;
	GdkRGBA _tmp0_;
	gchar* _tmp1_;
	GtkCssProvider* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_color;
	_tmp1_ = graphs_tools_rgba_to_hex (&_tmp0_);
	hex = _tmp1_;
	_tmp2_ = self->priv->provider;
	_tmp3_ = string_to_string (hex);
	_tmp4_ = g_strconcat ("image { color: ", _tmp3_, "; }", NULL);
	_tmp5_ = _tmp4_;
	gtk_css_provider_load_from_string (_tmp2_, _tmp5_);
	_g_free0 (_tmp5_);
	_g_free0 (hex);
}

static void
graphs_style_color_row_on_color_choose_data_free (gpointer _data)
{
	GraphsStyleColorRowOnColorChooseData* _data_;
	_data_ = _data;
	_g_object_unref0 (_data_->self);
	g_slice_free (GraphsStyleColorRowOnColorChooseData, _data_);
}

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

static void
graphs_style_color_row_on_color_choose (GraphsStyleColorRow* self,
                                        GAsyncReadyCallback _callback_,
                                        gpointer _user_data_)
{
	GraphsStyleColorRowOnColorChooseData* _data_;
	GraphsStyleColorRow* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (GraphsStyleColorRowOnColorChooseData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, graphs_style_color_row_on_color_choose_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	graphs_style_color_row_on_color_choose_co (_data_);
}

static void
graphs_style_color_row_on_color_choose_finish (GraphsStyleColorRow* self,
                                               GAsyncResult* _res_)
{
	GraphsStyleColorRowOnColorChooseData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
graphs_style_color_row_on_color_choose_ready (GObject* source_object,
                                              GAsyncResult* _res_,
                                              gpointer _user_data_)
{
	GraphsStyleColorRowOnColorChooseData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	graphs_style_color_row_on_color_choose_co (_data_);
}

static void
_vala_GdkRGBA_free (GdkRGBA* self)
{
	g_boxed_free (gdk_rgba_get_type (), self);
}

static gboolean
graphs_style_color_row_on_color_choose_co (GraphsStyleColorRowOnColorChooseData* _data_)
{
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	_data_->_tmp0_ = NULL;
	_data_->_tmp0_ = gtk_color_dialog_new ();
	gtk_color_dialog_set_with_alpha (_data_->_tmp0_, FALSE);
	_data_->dialog = _data_->_tmp0_;
	{
		_data_->_tmp2_ = gtk_widget_get_root ((GtkWidget*) _data_->self);
		_data_->_tmp3_ = _data_->self->priv->_color;
		_data_->_state_ = 1;
		gtk_color_dialog_choose_rgba (_data_->dialog, G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp2_, gtk_window_get_type (), GtkWindow), &_data_->_tmp3_, NULL, graphs_style_color_row_on_color_choose_ready, _data_);
		return FALSE;
		_state_1:
		_data_->_tmp4_ = gtk_color_dialog_choose_rgba_finish (_data_->dialog, _data_->_res_, &_data_->_inner_error0_);
		_data_->_tmp1_ = _data_->_tmp4_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp5_ = *_data_->_tmp1_;
		graphs_style_color_row_set_color (_data_->self, &_data_->_tmp5_);
		__vala_GdkRGBA_free0 (_data_->_tmp1_);
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_data_->_inner_error0_);
	}
	__finally0:
	if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
		_g_object_unref0 (_data_->dialog);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error0_->message, g_quark_to_string (_data_->_inner_error0_->domain), _data_->_inner_error0_->code);
		g_clear_error (&_data_->_inner_error0_);
		g_object_unref (_data_->_async_result);
		return FALSE;
	}
	_g_object_unref0 (_data_->dialog);
	g_task_return_pointer (_data_->_async_result, _data_, NULL);
	if (_data_->_state_ != 0) {
		while (!g_task_get_completed (_data_->_async_result)) {
			g_main_context_iteration (g_task_get_context (_data_->_async_result), TRUE);
		}
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}

static void
_graphs_style_color_row_on_color_choose_adw_action_row_activated (AdwActionRow* _sender,
                                                                  gpointer self)
{
	graphs_style_color_row_on_color_choose ((GraphsStyleColorRow*) self, NULL, NULL);
}

GraphsStyleColorRow*
graphs_style_color_row_construct (GType object_type)
{
	GraphsStyleColorRow * self = NULL;
	self = (GraphsStyleColorRow*) g_object_new (object_type, NULL);
	return self;
}

GraphsStyleColorRow*
graphs_style_color_row_new (void)
{
	return graphs_style_color_row_construct (GRAPHS_TYPE_STYLE_COLOR_ROW);
}

static GtkImage*
graphs_style_color_row_get_image (GraphsStyleColorRow* self)
{
	GtkImage* result;
	GtkImage* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_image;
	result = _tmp0_;
	return result;
}

void
graphs_style_color_row_get_color (GraphsStyleColorRow* self,
                                  GdkRGBA * result)
{
	GdkRGBA _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_color;
	*result = _tmp0_;
	return;
}

static gboolean
_gdk_rgba_equal (const GdkRGBA * s1,
                 const GdkRGBA * s2)
{
	if (s1 == s2) {
		return TRUE;
	}
	if (s1 == NULL) {
		return FALSE;
	}
	if (s2 == NULL) {
		return FALSE;
	}
	if (s1->red != s2->red) {
		return FALSE;
	}
	if (s1->green != s2->green) {
		return FALSE;
	}
	if (s1->blue != s2->blue) {
		return FALSE;
	}
	if (s1->alpha != s2->alpha) {
		return FALSE;
	}
	return TRUE;
}

void
graphs_style_color_row_set_color (GraphsStyleColorRow* self,
                                  GdkRGBA * value)
{
	GdkRGBA old_value;
	g_return_if_fail (self != NULL);
	graphs_style_color_row_get_color (self, &old_value);
	if (_gdk_rgba_equal (value, &old_value) != TRUE) {
		GdkRGBA _tmp0_;
		_tmp0_ = *value;
		self->priv->_color = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, graphs_style_color_row_properties[GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY]);
	}
}

static void
_graphs_style_color_row_on_color_g_object_notify (GObject* _sender,
                                                  GParamSpec* pspec,
                                                  gpointer self)
{
	graphs_style_color_row_on_color ((GraphsStyleColorRow*) self);
}

static GObject *
graphs_style_color_row_constructor (GType type,
                                    guint n_construct_properties,
                                    GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GraphsStyleColorRow * self;
	GtkImage* _tmp0_;
	GtkStyleContext* _tmp1_;
	GtkCssProvider* _tmp2_;
	parent_class = G_OBJECT_CLASS (graphs_style_color_row_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_STYLE_COLOR_ROW, GraphsStyleColorRow);
	_tmp0_ = self->priv->_image;
	_tmp1_ = gtk_widget_get_style_context ((GtkWidget*) _tmp0_);
	_tmp2_ = self->priv->provider;
	gtk_style_context_add_provider (_tmp1_, (GtkStyleProvider*) _tmp2_, (guint) GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
	g_signal_connect_object ((GObject*) self, "notify::color", (GCallback) _graphs_style_color_row_on_color_g_object_notify, self, 0);
	return obj;
}

static void
graphs_style_color_row_class_init (GraphsStyleColorRowClass * klass,
                                   gpointer klass_data)
{
	graphs_style_color_row_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraphsStyleColorRow_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_graphs_style_color_row_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_graphs_style_color_row_set_property;
	G_OBJECT_CLASS (klass)->constructor = graphs_style_color_row_constructor;
	G_OBJECT_CLASS (klass)->finalize = graphs_style_color_row_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY, graphs_style_color_row_properties[GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY] = g_param_spec_boxed ("color", "color", "color", gdk_rgba_get_type (), G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/se/sjoerd/Graphs/ui/style-editor/color-row.ui");
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "image", FALSE, GraphsStyleColorRow_private_offset + G_STRUCT_OFFSET (GraphsStyleColorRowPrivate, _image));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_color_choose", G_CALLBACK(_graphs_style_color_row_on_color_choose_adw_action_row_activated));
}

static void
graphs_style_color_row_instance_init (GraphsStyleColorRow * self,
                                      gpointer klass)
{
	GtkCssProvider* _tmp0_;
	self->priv = graphs_style_color_row_get_instance_private (self);
	_tmp0_ = gtk_css_provider_new ();
	self->priv->provider = _tmp0_;
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
graphs_style_color_row_finalize (GObject * obj)
{
	GraphsStyleColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_STYLE_COLOR_ROW, GraphsStyleColorRow);
	_g_object_unref0 (self->priv->provider);
	G_OBJECT_CLASS (graphs_style_color_row_parent_class)->finalize (obj);
}

/**
     * Style Color Box
     */
static GType
graphs_style_color_row_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraphsStyleColorRowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) graphs_style_color_row_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraphsStyleColorRow), 0, (GInstanceInitFunc) graphs_style_color_row_instance_init, NULL };
	GType graphs_style_color_row_type_id;
	graphs_style_color_row_type_id = g_type_register_static (adw_action_row_get_type (), "GraphsStyleColorRow", &g_define_type_info, 0);
	GraphsStyleColorRow_private_offset = g_type_add_instance_private (graphs_style_color_row_type_id, sizeof (GraphsStyleColorRowPrivate));
	return graphs_style_color_row_type_id;
}

GType
graphs_style_color_row_get_type (void)
{
	static volatile gsize graphs_style_color_row_type_id__once = 0;
	if (g_once_init_enter (&graphs_style_color_row_type_id__once)) {
		GType graphs_style_color_row_type_id;
		graphs_style_color_row_type_id = graphs_style_color_row_get_type_once ();
		g_once_init_leave (&graphs_style_color_row_type_id__once, graphs_style_color_row_type_id);
	}
	return graphs_style_color_row_type_id__once;
}

static void
_vala_graphs_style_color_row_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec)
{
	GraphsStyleColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRAPHS_TYPE_STYLE_COLOR_ROW, GraphsStyleColorRow);
	switch (property_id) {
		case GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY:
		{
			GdkRGBA boxed;
			graphs_style_color_row_get_color (self, &boxed);
			g_value_set_boxed (value, &boxed);
		}
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_graphs_style_color_row_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec)
{
	GraphsStyleColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRAPHS_TYPE_STYLE_COLOR_ROW, GraphsStyleColorRow);
	switch (property_id) {
		case GRAPHS_STYLE_COLOR_ROW_COLOR_PROPERTY:
		graphs_style_color_row_set_color (self, g_value_get_boxed (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

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;
}

