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

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

#include "graphs.h"
#include <glib.h>
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
#include <glib-object.h>
#include <adwaita.h>
#include <glib/gi18n-lib.h>
#include <gio/gio.h>
#include <gdk/gdk.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_ITEM_COLOR_ROW_0_PROPERTY,
	GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY,
	GRAPHS_STYLE_ITEM_COLOR_ROW_NUM_PROPERTIES
};
static GParamSpec* graphs_style_item_color_row_properties[GRAPHS_STYLE_ITEM_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 _GraphsStyleItemColorRowOnColorChooseData GraphsStyleItemColorRowOnColorChooseData;
enum  {
	GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_CHANGED_SIGNAL,
	GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_REMOVED_SIGNAL,
	GRAPHS_STYLE_ITEM_COLOR_ROW_NUM_SIGNALS
};
static guint graphs_style_item_color_row_signals[GRAPHS_STYLE_ITEM_COLOR_ROW_NUM_SIGNALS] = {0};

struct _GraphsStyleItemColorRowPrivate {
	gint _index;
	GtkButton* _color_button;
	GtkCssProvider* provider;
	gchar* color;
};

struct _GraphsStyleItemColorRowOnColorChooseData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GTask* _async_result;
	GraphsStyleItemColorRow* self;
	GtkColorDialog* dialog;
	GtkColorDialog* _tmp0_;
	GdkRGBA rgba;
	GtkRoot* _tmp1_;
	const gchar* _tmp2_;
	GdkRGBA _tmp3_;
	GdkRGBA* _tmp4_;
	GdkRGBA* _tmp5_;
	GdkRGBA _tmp6_;
	GdkRGBA _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	GError* _inner_error0_;
};

static gint GraphsStyleItemColorRow_private_offset;
static gpointer graphs_style_item_color_row_parent_class = NULL;

static void graphs_style_item_color_row_load_color (GraphsStyleItemColorRow* self);
static void graphs_style_item_color_row_on_color_choose_data_free (gpointer _data);
static void graphs_style_item_color_row_on_color_choose (GraphsStyleItemColorRow* self,
                                                  GAsyncReadyCallback _callback_,
                                                  gpointer _user_data_);
static void graphs_style_item_color_row_on_color_choose_finish (GraphsStyleItemColorRow* self,
                                                         GAsyncResult* _res_);
static gboolean graphs_style_item_color_row_on_color_choose_co (GraphsStyleItemColorRowOnColorChooseData* _data_);
static void graphs_style_item_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_item_color_row_on_color_choose_gtk_button_clicked (GtkButton* _sender,
                                                                      gpointer self);
static void graphs_style_item_color_row_on_delete (GraphsStyleItemColorRow* self);
static void _graphs_style_item_color_row_on_delete_gtk_button_clicked (GtkButton* _sender,
                                                                gpointer self);
static GtkButton* graphs_style_item_color_row_get_color_button (GraphsStyleItemColorRow* self);
static GObject * graphs_style_item_color_row_constructor (GType type,
                                                   guint n_construct_properties,
                                                   GObjectConstructParam * construct_properties);
static void graphs_style_item_color_row_finalize (GObject * obj);
static GType graphs_style_item_color_row_get_type_once (void);
static void _vala_graphs_style_item_color_row_get_property (GObject * object,
                                                     guint property_id,
                                                     GValue * value,
                                                     GParamSpec * pspec);
static void _vala_graphs_style_item_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_item_color_row_get_instance_private (GraphsStyleItemColorRow* self)
{
	return G_STRUCT_MEMBER_P (self, GraphsStyleItemColorRow_private_offset);
}

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

GraphsStyleItemColorRow*
graphs_style_item_color_row_construct (GType object_type,
                                       GraphsStyleColorManager* color_manager,
                                       gint index,
                                       const gchar* color)
{
	GraphsStyleItemColorRow * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	GraphsStyleColorManager* _tmp2_;
	gchar* _tmp3_;
	g_return_val_if_fail (color_manager != NULL, NULL);
	g_return_val_if_fail (color != NULL, NULL);
	self = (GraphsStyleItemColorRow*) g_object_new (object_type, "index", index, NULL);
	_tmp0_ = g_strdup_printf (_ ("Color %d"), index + 1);
	_tmp1_ = _tmp0_;
	adw_preferences_row_set_title ((AdwPreferencesRow*) self, _tmp1_);
	_g_free0 (_tmp1_);
	_tmp2_ = _g_object_ref0 (color_manager);
	_g_object_unref0 (self->color_manager);
	self->color_manager = _tmp2_;
	_tmp3_ = g_strdup (color);
	_g_free0 (self->priv->color);
	self->priv->color = _tmp3_;
	graphs_style_item_color_row_load_color (self);
	return self;
}

GraphsStyleItemColorRow*
graphs_style_item_color_row_new (GraphsStyleColorManager* color_manager,
                                 gint index,
                                 const gchar* color)
{
	return graphs_style_item_color_row_construct (GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, color_manager, index, color);
}

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_item_color_row_load_color (GraphsStyleItemColorRow* self)
{
	GtkCssProvider* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->provider;
	_tmp1_ = self->priv->color;
	_tmp2_ = string_to_string (_tmp1_);
	_tmp3_ = g_strconcat ("button { color: ", _tmp2_, "; }", NULL);
	_tmp4_ = _tmp3_;
	gtk_css_provider_load_from_string (_tmp0_, _tmp4_);
	_g_free0 (_tmp4_);
}

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

static void
graphs_style_item_color_row_on_color_choose (GraphsStyleItemColorRow* self,
                                             GAsyncReadyCallback _callback_,
                                             gpointer _user_data_)
{
	GraphsStyleItemColorRowOnColorChooseData* _data_;
	GraphsStyleItemColorRow* _tmp0_;
	g_return_if_fail (self != NULL);
	_data_ = g_slice_new0 (GraphsStyleItemColorRowOnColorChooseData);
	_data_->_async_result = g_task_new (G_OBJECT (self), NULL, _callback_, _user_data_);
	g_task_set_task_data (_data_->_async_result, _data_, graphs_style_item_color_row_on_color_choose_data_free);
	_tmp0_ = _g_object_ref0 (self);
	_data_->self = _tmp0_;
	graphs_style_item_color_row_on_color_choose_co (_data_);
}

static void
graphs_style_item_color_row_on_color_choose_finish (GraphsStyleItemColorRow* self,
                                                    GAsyncResult* _res_)
{
	GraphsStyleItemColorRowOnColorChooseData* _data_;
	_data_ = g_task_propagate_pointer (G_TASK (_res_), NULL);
}

static void
graphs_style_item_color_row_on_color_choose_ready (GObject* source_object,
                                                   GAsyncResult* _res_,
                                                   gpointer _user_data_)
{
	GraphsStyleItemColorRowOnColorChooseData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	graphs_style_item_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_item_color_row_on_color_choose_co (GraphsStyleItemColorRowOnColorChooseData* _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_->_tmp1_ = gtk_widget_get_root ((GtkWidget*) _data_->self);
		_data_->_tmp2_ = _data_->self->priv->color;
		memset (&_data_->_tmp3_, 0, sizeof (GdkRGBA));
		graphs_tools_hex_to_rgba (_data_->_tmp2_, &_data_->_tmp3_);
		_data_->_state_ = 1;
		gtk_color_dialog_choose_rgba (_data_->dialog, G_TYPE_CHECK_INSTANCE_CAST (_data_->_tmp1_, gtk_window_get_type (), GtkWindow), &_data_->_tmp3_, NULL, graphs_style_item_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_->_tmp5_ = _data_->_tmp4_;
		_data_->_tmp6_ = *_data_->_tmp5_;
		__vala_GdkRGBA_free0 (_data_->_tmp5_);
		_data_->rgba = _data_->_tmp6_;
		if (G_UNLIKELY (_data_->_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
		_data_->_tmp7_ = _data_->rgba;
		_data_->_tmp8_ = graphs_tools_rgba_to_hex (&_data_->_tmp7_);
		_g_free0 (_data_->self->priv->color);
		_data_->self->priv->color = _data_->_tmp8_;
		graphs_style_item_color_row_load_color (_data_->self);
		_data_->_tmp9_ = _data_->self->priv->color;
		g_signal_emit (_data_->self, graphs_style_item_color_row_signals[GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_CHANGED_SIGNAL], 0, _data_->_tmp9_);
	}
	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_item_color_row_on_color_choose_gtk_button_clicked (GtkButton* _sender,
                                                                 gpointer self)
{
	graphs_style_item_color_row_on_color_choose ((GraphsStyleItemColorRow*) self, NULL, NULL);
}

static void
graphs_style_item_color_row_on_delete (GraphsStyleItemColorRow* self)
{
	g_return_if_fail (self != NULL);
	g_signal_emit (self, graphs_style_item_color_row_signals[GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_REMOVED_SIGNAL], 0);
}

static void
_graphs_style_item_color_row_on_delete_gtk_button_clicked (GtkButton* _sender,
                                                           gpointer self)
{
	graphs_style_item_color_row_on_delete ((GraphsStyleItemColorRow*) self);
}

gint
graphs_style_item_color_row_get_index (GraphsStyleItemColorRow* self)
{
	gint result;
	g_return_val_if_fail (self != NULL, -1);
	result = self->priv->_index;
	return result;
}

void
graphs_style_item_color_row_set_index (GraphsStyleItemColorRow* self,
                                       gint value)
{
	gint old_value;
	g_return_if_fail (self != NULL);
	old_value = graphs_style_item_color_row_get_index (self);
	if (old_value != value) {
		self->priv->_index = value;
		g_object_notify_by_pspec ((GObject *) self, graphs_style_item_color_row_properties[GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY]);
	}
}

static GtkButton*
graphs_style_item_color_row_get_color_button (GraphsStyleItemColorRow* self)
{
	GtkButton* result;
	GtkButton* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_color_button;
	result = _tmp0_;
	return result;
}

static GObject *
graphs_style_item_color_row_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GraphsStyleItemColorRow * self;
	GtkButton* _tmp0_;
	GtkStyleContext* _tmp1_;
	GtkCssProvider* _tmp2_;
	parent_class = G_OBJECT_CLASS (graphs_style_item_color_row_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, GraphsStyleItemColorRow);
	_tmp0_ = self->priv->_color_button;
	_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);
	return obj;
}

static void
graphs_style_item_color_row_class_init (GraphsStyleItemColorRowClass * klass,
                                        gpointer klass_data)
{
	graphs_style_item_color_row_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraphsStyleItemColorRow_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_graphs_style_item_color_row_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_graphs_style_item_color_row_set_property;
	G_OBJECT_CLASS (klass)->constructor = graphs_style_item_color_row_constructor;
	G_OBJECT_CLASS (klass)->finalize = graphs_style_item_color_row_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY, graphs_style_item_color_row_properties[GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY] = g_param_spec_int ("index", "index", "index", G_MININT, G_MAXINT, 0, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/se/sjoerd/Graphs/ui/style-editor/item-color-row.ui");
	graphs_style_item_color_row_signals[GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_CHANGED_SIGNAL] = g_signal_new ("color-changed", GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__STRING, G_TYPE_NONE, 1, G_TYPE_STRING);
	graphs_style_item_color_row_signals[GRAPHS_STYLE_ITEM_COLOR_ROW_COLOR_REMOVED_SIGNAL] = g_signal_new ("color-removed", GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__VOID, G_TYPE_NONE, 0);
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "color_button", FALSE, GraphsStyleItemColorRow_private_offset + G_STRUCT_OFFSET (GraphsStyleItemColorRowPrivate, _color_button));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_color_choose", G_CALLBACK(_graphs_style_item_color_row_on_color_choose_gtk_button_clicked));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_delete", G_CALLBACK(_graphs_style_item_color_row_on_delete_gtk_button_clicked));
}

static void
graphs_style_item_color_row_instance_init (GraphsStyleItemColorRow * self,
                                           gpointer klass)
{
	GtkCssProvider* _tmp0_;
	self->priv = graphs_style_item_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_item_color_row_finalize (GObject * obj)
{
	GraphsStyleItemColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, GraphsStyleItemColorRow);
	_g_object_unref0 (self->priv->provider);
	_g_free0 (self->priv->color);
	_g_object_unref0 (self->color_manager);
	G_OBJECT_CLASS (graphs_style_item_color_row_parent_class)->finalize (obj);
}

/**
     * Style Color Box
     */
static GType
graphs_style_item_color_row_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraphsStyleItemColorRowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) graphs_style_item_color_row_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraphsStyleItemColorRow), 0, (GInstanceInitFunc) graphs_style_item_color_row_instance_init, NULL };
	GType graphs_style_item_color_row_type_id;
	graphs_style_item_color_row_type_id = g_type_register_static (adw_action_row_get_type (), "GraphsStyleItemColorRow", &g_define_type_info, 0);
	GraphsStyleItemColorRow_private_offset = g_type_add_instance_private (graphs_style_item_color_row_type_id, sizeof (GraphsStyleItemColorRowPrivate));
	return graphs_style_item_color_row_type_id;
}

GType
graphs_style_item_color_row_get_type (void)
{
	static volatile gsize graphs_style_item_color_row_type_id__once = 0;
	if (g_once_init_enter (&graphs_style_item_color_row_type_id__once)) {
		GType graphs_style_item_color_row_type_id;
		graphs_style_item_color_row_type_id = graphs_style_item_color_row_get_type_once ();
		g_once_init_leave (&graphs_style_item_color_row_type_id__once, graphs_style_item_color_row_type_id);
	}
	return graphs_style_item_color_row_type_id__once;
}

static void
_vala_graphs_style_item_color_row_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec)
{
	GraphsStyleItemColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, GraphsStyleItemColorRow);
	switch (property_id) {
		case GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY:
		g_value_set_int (value, graphs_style_item_color_row_get_index (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_graphs_style_item_color_row_set_property (GObject * object,
                                                guint property_id,
                                                const GValue * value,
                                                GParamSpec * pspec)
{
	GraphsStyleItemColorRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRAPHS_TYPE_STYLE_ITEM_COLOR_ROW, GraphsStyleItemColorRow);
	switch (property_id) {
		case GRAPHS_STYLE_ITEM_COLOR_ROW_INDEX_PROPERTY:
		graphs_style_item_color_row_set_index (self, g_value_get_int (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;
}

