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

/*
 * Copyright (c) 2016, 2024 focus-timer contributors
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "ft_ui.h"
#include <glib.h>
#include <gtk/gtk.h>
#include "ft_core.h"
#include <glib-object.h>
#include <float.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <glib/gi18n-lib.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  {
	FT_ACTION_LIST_BOX_ROW_0_PROPERTY,
	FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY,
	FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY,
	FT_ACTION_LIST_BOX_ROW_NUM_PROPERTIES
};
static GParamSpec* ft_action_list_box_row_properties[FT_ACTION_LIST_BOX_ROW_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
enum  {
	FT_ACTION_LIST_BOX_ROW_MOVE_ROW_SIGNAL,
	FT_ACTION_LIST_BOX_ROW_NUM_SIGNALS
};
static guint ft_action_list_box_row_signals[FT_ACTION_LIST_BOX_ROW_NUM_SIGNALS] = {0};

struct _FtActionListBoxRowPrivate {
	guint _sort_order;
	GtkLabel* display_name_label;
	GtkSwitch* enabled_switch;
	FtAction* _action;
	GBinding* enabled_switch_binding;
	gdouble drag_x;
	gdouble drag_y;
	GtkListBox* drag_widget;
};

static gint FtActionListBoxRow_private_offset;
static gpointer ft_action_list_box_row_parent_class = NULL;

static void ft_action_list_box_row_update_display_name (FtActionListBoxRow* self);
static GdkContentProvider* ft_action_list_box_row_on_drag_prepare (FtActionListBoxRow* self,
                                                            gdouble x,
                                                            gdouble y);
static GdkContentProvider* _ft_action_list_box_row_on_drag_prepare_gtk_drag_source_prepare (GtkDragSource* _sender,
                                                                                     gdouble x,
                                                                                     gdouble y,
                                                                                     gpointer self);
static void ft_action_list_box_row_on_drag_begin (FtActionListBoxRow* self,
                                           GdkDrag* drag);
static void _ft_action_list_box_row_on_drag_begin_gtk_drag_source_drag_begin (GtkDragSource* _sender,
                                                                       GdkDrag* drag,
                                                                       gpointer self);
static gboolean ft_action_list_box_row_on_drop (FtActionListBoxRow* self,
                                         GValue* value,
                                         gdouble x,
                                         gdouble y);
static gboolean _ft_action_list_box_row_on_drop_gtk_drop_target_drop (GtkDropTarget* _sender,
                                                               GValue* value,
                                                               gdouble x,
                                                               gdouble y,
                                                               gpointer self);
static void ft_action_list_box_row_real_dispose (GObject* base);
static void ft_action_list_box_row_finalize (GObject * obj);
static GType ft_action_list_box_row_get_type_once (void);
static void _vala_ft_action_list_box_row_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec);
static void _vala_ft_action_list_box_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
ft_action_list_box_row_get_instance_private (FtActionListBoxRow* self)
{
	return G_STRUCT_MEMBER_P (self, FtActionListBoxRow_private_offset);
}

FtActionListBoxRow*
ft_action_list_box_row_construct (GType object_type,
                                  FtAction* action)
{
	FtActionListBoxRow * self = NULL;
	g_return_val_if_fail (action != NULL, NULL);
	self = (FtActionListBoxRow*) g_object_new (object_type, "action", action, NULL);
	return self;
}

FtActionListBoxRow*
ft_action_list_box_row_new (FtAction* action)
{
	return ft_action_list_box_row_construct (FT_TYPE_ACTION_LIST_BOX_ROW, action);
}

/**
         * Assume that action-list will set a new `action` here after it gets saved.
         */
static void
ft_action_list_box_row_update_display_name (FtActionListBoxRow* self)
{
	FtAction* _tmp0_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_action;
	if (_tmp0_ != NULL) {
		const gchar* _tmp1_ = NULL;
		FtAction* _tmp2_;
		const gchar* _tmp3_;
		const gchar* _tmp4_;
		GtkLabel* _tmp8_;
		_tmp2_ = self->priv->_action;
		_tmp3_ = ft_action_get_display_name (_tmp2_);
		_tmp4_ = _tmp3_;
		if (g_strcmp0 (_tmp4_, "") != 0) {
			FtAction* _tmp5_;
			const gchar* _tmp6_;
			const gchar* _tmp7_;
			_tmp5_ = self->priv->_action;
			_tmp6_ = ft_action_get_display_name (_tmp5_);
			_tmp7_ = _tmp6_;
			_tmp1_ = _tmp7_;
		} else {
			_tmp1_ = _ ("Untitled action");
		}
		_tmp8_ = self->priv->display_name_label;
		gtk_label_set_label (_tmp8_, _tmp1_);
	}
}

static GdkContentProvider*
ft_action_list_box_row_on_drag_prepare (FtActionListBoxRow* self,
                                        gdouble x,
                                        gdouble y)
{
	GValue drag_value = {0};
	GValue _tmp0_ = {0};
	GValue _tmp1_;
	GdkContentProvider* _tmp2_;
	GdkContentProvider* result;
	g_return_val_if_fail (self != NULL, NULL);
	self->priv->drag_x = x;
	self->priv->drag_y = y;
	g_value_init (&_tmp0_, FT_TYPE_ACTION_LIST_BOX_ROW);
	drag_value = _tmp0_;
	g_value_set_object (&drag_value, (GObject*) self);
	_tmp1_ = drag_value;
	_tmp2_ = gdk_content_provider_new_for_value (&_tmp1_);
	result = _tmp2_;
	G_IS_VALUE (&drag_value) ? (g_value_unset (&drag_value), NULL) : NULL;
	return result;
}

static GdkContentProvider*
_ft_action_list_box_row_on_drag_prepare_gtk_drag_source_prepare (GtkDragSource* _sender,
                                                                 gdouble x,
                                                                 gdouble y,
                                                                 gpointer self)
{
	GdkContentProvider* result;
	result = ft_action_list_box_row_on_drag_prepare ((FtActionListBoxRow*) self, x, y);
	return result;
}

static void
ft_action_list_box_row_on_drag_begin (FtActionListBoxRow* self,
                                      GdkDrag* drag)
{
	FtActionListBoxRow* row = NULL;
	FtAction* _tmp0_;
	FtActionListBoxRow* _tmp1_;
	GtkListBox* _tmp2_;
	GtkListBox* _tmp3_;
	GtkListBox* _tmp4_;
	GtkListBox* _tmp5_;
	GtkDragIcon* drag_icon = NULL;
	GtkDragIcon* _tmp6_;
	GtkListBox* _tmp7_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (drag != NULL);
	_tmp0_ = self->priv->_action;
	_tmp1_ = ft_action_list_box_row_new (_tmp0_);
	g_object_ref_sink (_tmp1_);
	row = _tmp1_;
	_tmp2_ = (GtkListBox*) gtk_list_box_new ();
	g_object_ref_sink (_tmp2_);
	_g_object_unref0 (self->priv->drag_widget);
	self->priv->drag_widget = _tmp2_;
	_tmp3_ = self->priv->drag_widget;
	gtk_widget_set_size_request ((GtkWidget*) _tmp3_, gtk_widget_get_width ((GtkWidget*) self), gtk_widget_get_height ((GtkWidget*) self));
	_tmp4_ = self->priv->drag_widget;
	gtk_list_box_append (_tmp4_, (GtkWidget*) row);
	_tmp5_ = self->priv->drag_widget;
	gtk_list_box_drag_highlight_row (_tmp5_, (GtkListBoxRow*) row);
	_tmp6_ = (GtkDragIcon*) gtk_drag_icon_get_for_drag (drag);
	g_object_ref_sink (_tmp6_);
	drag_icon = _tmp6_;
	_tmp7_ = self->priv->drag_widget;
	gtk_drag_icon_set_child (drag_icon, (GtkWidget*) _tmp7_);
	gdk_drag_set_hotspot (drag, (gint) self->priv->drag_x, (gint) self->priv->drag_y);
	_g_object_unref0 (drag_icon);
	_g_object_unref0 (row);
}

static void
_ft_action_list_box_row_on_drag_begin_gtk_drag_source_drag_begin (GtkDragSource* _sender,
                                                                  GdkDrag* drag,
                                                                  gpointer self)
{
	ft_action_list_box_row_on_drag_begin ((FtActionListBoxRow*) self, drag);
}

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

static gboolean
ft_action_list_box_row_on_drop (FtActionListBoxRow* self,
                                GValue* value,
                                gdouble x,
                                gdouble y)
{
	FtActionListBoxRow* source = NULL;
	GObject* _tmp0_;
	FtActionListBoxRow* _tmp1_;
	FtActionListBoxRow* _tmp2_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	g_return_val_if_fail (value != NULL, FALSE);
	if (!G_VALUE_HOLDS (value, FT_TYPE_ACTION_LIST_BOX_ROW)) {
		result = FALSE;
		return result;
	}
	_tmp0_ = g_value_get_object (value);
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, FT_TYPE_ACTION_LIST_BOX_ROW, FtActionListBoxRow));
	source = _tmp1_;
	_tmp2_ = source;
	g_signal_emit (_tmp2_, ft_action_list_box_row_signals[FT_ACTION_LIST_BOX_ROW_MOVE_ROW_SIGNAL], 0, self);
	result = TRUE;
	_g_object_unref0 (source);
	return result;
}

static gboolean
_ft_action_list_box_row_on_drop_gtk_drop_target_drop (GtkDropTarget* _sender,
                                                      GValue* value,
                                                      gdouble x,
                                                      gdouble y,
                                                      gpointer self)
{
	gboolean result;
	result = ft_action_list_box_row_on_drop ((FtActionListBoxRow*) self, value, x, y);
	return result;
}

static void
ft_action_list_box_row_real_dispose (GObject* base)
{
	FtActionListBoxRow * self;
	GBinding* _tmp0_;
	self = (FtActionListBoxRow*) base;
	_tmp0_ = self->priv->enabled_switch_binding;
	if (_tmp0_ != NULL) {
		GBinding* _tmp1_;
		GBinding* _tmp2_;
		_tmp1_ = self->priv->enabled_switch_binding;
		_tmp2_ = _g_object_ref0 (_tmp1_);
		g_binding_unbind (_tmp2_);
		self->priv->enabled_switch_binding = NULL;
	}
	_g_object_unref0 (self->priv->_action);
	self->priv->_action = NULL;
	_g_object_unref0 (self->priv->drag_widget);
	self->priv->drag_widget = NULL;
	G_OBJECT_CLASS (ft_action_list_box_row_parent_class)->dispose ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, gtk_list_box_row_get_type (), GtkListBoxRow));
}

FtAction*
ft_action_list_box_row_get_action (FtActionListBoxRow* self)
{
	FtAction* result;
	FtAction* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_action;
	result = _tmp0_;
	return result;
}

void
ft_action_list_box_row_set_action (FtActionListBoxRow* self,
                                   FtAction* value)
{
	FtAction* _tmp0_;
	GBinding* _tmp1_;
	FtAction* _tmp4_;
	FtAction* _tmp5_;
	GtkSwitch* _tmp6_;
	GBinding* _tmp7_;
	g_return_if_fail (self != NULL);
	_tmp0_ = self->priv->_action;
	if (_tmp0_ == value) {
		return;
	}
	_tmp1_ = self->priv->enabled_switch_binding;
	if (_tmp1_ != NULL) {
		GBinding* _tmp2_;
		GBinding* _tmp3_;
		_tmp2_ = self->priv->enabled_switch_binding;
		_tmp3_ = _g_object_ref0 (_tmp2_);
		g_binding_unbind (_tmp3_);
		self->priv->enabled_switch_binding = NULL;
	}
	_tmp4_ = _g_object_ref0 (value);
	_g_object_unref0 (self->priv->_action);
	self->priv->_action = _tmp4_;
	_tmp5_ = self->priv->_action;
	_tmp6_ = self->priv->enabled_switch;
	_tmp7_ = g_object_bind_property_with_closures ((GObject*) _tmp5_, "enabled", (GObject*) _tmp6_, "active", G_BINDING_SYNC_CREATE | G_BINDING_BIDIRECTIONAL, (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	self->priv->enabled_switch_binding = _tmp7_;
	ft_action_list_box_row_update_display_name (self);
	g_object_notify_by_pspec ((GObject *) self, ft_action_list_box_row_properties[FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY]);
}

guint
ft_action_list_box_row_get_sort_order (FtActionListBoxRow* self)
{
	guint result;
	g_return_val_if_fail (self != NULL, 0U);
	result = self->priv->_sort_order;
	return result;
}

void
ft_action_list_box_row_set_sort_order (FtActionListBoxRow* self,
                                       guint value)
{
	guint old_value;
	g_return_if_fail (self != NULL);
	old_value = ft_action_list_box_row_get_sort_order (self);
	if (old_value != value) {
		self->priv->_sort_order = value;
		g_object_notify_by_pspec ((GObject *) self, ft_action_list_box_row_properties[FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY]);
	}
}

void
ft_action_list_box_row_move_row (FtActionListBoxRow* self,
                                 FtActionListBoxRow* destination_row)
{
	g_return_if_fail (self != NULL);
	g_return_if_fail (destination_row != NULL);
	g_signal_emit (self, ft_action_list_box_row_signals[FT_ACTION_LIST_BOX_ROW_MOVE_ROW_SIGNAL], 0, destination_row);
}

static void
ft_action_list_box_row_class_init (FtActionListBoxRowClass * klass,
                                   gpointer klass_data)
{
	ft_action_list_box_row_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &FtActionListBoxRow_private_offset);
	((GObjectClass *) klass)->dispose = (void (*) (GObject*)) ft_action_list_box_row_real_dispose;
	G_OBJECT_CLASS (klass)->get_property = _vala_ft_action_list_box_row_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_ft_action_list_box_row_set_property;
	G_OBJECT_CLASS (klass)->finalize = ft_action_list_box_row_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY, ft_action_list_box_row_properties[FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY] = g_param_spec_object ("action", "action", "action", FT_TYPE_ACTION, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	g_object_class_install_property (G_OBJECT_CLASS (klass), FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY, ft_action_list_box_row_properties[FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY] = g_param_spec_uint ("sort-order", "sort-order", "sort-order", 0, G_MAXUINT, 0U, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
	gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (klass), "/io/github/focustimerhq/FocusTimer/ui/preferences/automation/action/action-listboxrow.ui");
	ft_action_list_box_row_signals[FT_ACTION_LIST_BOX_ROW_MOVE_ROW_SIGNAL] = g_signal_new ("move-row", FT_TYPE_ACTION_LIST_BOX_ROW, G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_VOID__OBJECT, G_TYPE_NONE, 1, FT_TYPE_ACTION_LIST_BOX_ROW);
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "display_name_label", FALSE, FtActionListBoxRow_private_offset + G_STRUCT_OFFSET (FtActionListBoxRowPrivate, display_name_label));
	gtk_widget_class_bind_template_child_full (GTK_WIDGET_CLASS (klass), "enabled_switch", FALSE, FtActionListBoxRow_private_offset + G_STRUCT_OFFSET (FtActionListBoxRowPrivate, enabled_switch));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_drag_prepare", G_CALLBACK(_ft_action_list_box_row_on_drag_prepare_gtk_drag_source_prepare));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_drag_begin", G_CALLBACK(_ft_action_list_box_row_on_drag_begin_gtk_drag_source_drag_begin));
	gtk_widget_class_bind_template_callback_full (GTK_WIDGET_CLASS (klass), "on_drop", G_CALLBACK(_ft_action_list_box_row_on_drop_gtk_drop_target_drop));
}

static void
ft_action_list_box_row_instance_init (FtActionListBoxRow * self,
                                      gpointer klass)
{
	self->priv = ft_action_list_box_row_get_instance_private (self);
	self->priv->_sort_order = 0U;
	self->priv->_action = NULL;
	self->priv->enabled_switch_binding = NULL;
	self->priv->drag_widget = NULL;
	gtk_widget_init_template (GTK_WIDGET (self));
}

static void
ft_action_list_box_row_finalize (GObject * obj)
{
	FtActionListBoxRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, FT_TYPE_ACTION_LIST_BOX_ROW, FtActionListBoxRow);
	_g_object_unref0 (self->priv->_action);
	_g_object_unref0 (self->priv->drag_widget);
	G_OBJECT_CLASS (ft_action_list_box_row_parent_class)->finalize (obj);
}

static GType
ft_action_list_box_row_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (FtActionListBoxRowClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ft_action_list_box_row_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (FtActionListBoxRow), 0, (GInstanceInitFunc) ft_action_list_box_row_instance_init, NULL };
	GType ft_action_list_box_row_type_id;
	ft_action_list_box_row_type_id = g_type_register_static (gtk_list_box_row_get_type (), "FtActionListBoxRow", &g_define_type_info, 0);
	FtActionListBoxRow_private_offset = g_type_add_instance_private (ft_action_list_box_row_type_id, sizeof (FtActionListBoxRowPrivate));
	return ft_action_list_box_row_type_id;
}

GType
ft_action_list_box_row_get_type (void)
{
	static volatile gsize ft_action_list_box_row_type_id__once = 0;
	if (g_once_init_enter (&ft_action_list_box_row_type_id__once)) {
		GType ft_action_list_box_row_type_id;
		ft_action_list_box_row_type_id = ft_action_list_box_row_get_type_once ();
		g_once_init_leave (&ft_action_list_box_row_type_id__once, ft_action_list_box_row_type_id);
	}
	return ft_action_list_box_row_type_id__once;
}

static void
_vala_ft_action_list_box_row_get_property (GObject * object,
                                           guint property_id,
                                           GValue * value,
                                           GParamSpec * pspec)
{
	FtActionListBoxRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_ACTION_LIST_BOX_ROW, FtActionListBoxRow);
	switch (property_id) {
		case FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY:
		g_value_set_object (value, ft_action_list_box_row_get_action (self));
		break;
		case FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY:
		g_value_set_uint (value, ft_action_list_box_row_get_sort_order (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_ft_action_list_box_row_set_property (GObject * object,
                                           guint property_id,
                                           const GValue * value,
                                           GParamSpec * pspec)
{
	FtActionListBoxRow * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, FT_TYPE_ACTION_LIST_BOX_ROW, FtActionListBoxRow);
	switch (property_id) {
		case FT_ACTION_LIST_BOX_ROW_ACTION_PROPERTY:
		ft_action_list_box_row_set_action (self, g_value_get_object (value));
		break;
		case FT_ACTION_LIST_BOX_ROW_SORT_ORDER_PROPERTY:
		ft_action_list_box_row_set_sort_order (self, g_value_get_uint (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;
}

