/* SwitchModelButton.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from SwitchModelButton.vala, do not modify */

/*
 * Copyright 2021 elementary, Inc. (https://elementary.io)
 * SPDX-License-Identifier: GPL-2.0-or-later
 */

#include "granite-7.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gtk/gtk.h>
#include <glib-object.h>
#include <float.h>
#include <math.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  {
	GRANITE_SWITCH_MODEL_BUTTON_0_PROPERTY,
	GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY,
	GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY,
	GRANITE_SWITCH_MODEL_BUTTON_NUM_PROPERTIES
};
static GParamSpec* granite_switch_model_button_properties[GRANITE_SWITCH_MODEL_BUTTON_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
typedef struct _Block6Data Block6Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _GraniteSwitchModelButtonPrivate {
	gchar* _text;
	gchar* _description;
};

struct _Block6Data {
	int _ref_count_;
	GraniteSwitchModelButton* self;
	GtkRevealer* description_revealer;
	GtkBox* box;
	GtkSwitch* button_switch;
};

static gint GraniteSwitchModelButton_private_offset;
static gpointer granite_switch_model_button_parent_class = NULL;

static GObject * granite_switch_model_button_constructor (GType type,
                                                   guint n_construct_properties,
                                                   GObjectConstructParam * construct_properties);
static Block6Data* block6_data_ref (Block6Data* _data6_);
static void block6_data_unref (void * _userdata_);
static void _granite_switch_model_button___lambda28_ (Block6Data* _data6_);
static void __granite_switch_model_button___lambda28__gtk_gesture_click_released (GtkGestureClick* _sender,
                                                                           gint n_press,
                                                                           gdouble x,
                                                                           gdouble y,
                                                                           gpointer self);
static void _granite_switch_model_button___lambda29_ (GraniteSwitchModelButton* self);
static void __granite_switch_model_button___lambda29__g_object_notify (GObject* _sender,
                                                                GParamSpec* pspec,
                                                                gpointer self);
static void _granite_switch_model_button___lambda30_ (Block6Data* _data6_);
static void __granite_switch_model_button___lambda30__g_object_notify (GObject* _sender,
                                                                GParamSpec* pspec,
                                                                gpointer self);
static void granite_switch_model_button_finalize (GObject * obj);
static GType granite_switch_model_button_get_type_once (void);
static void _vala_granite_switch_model_button_get_property (GObject * object,
                                                     guint property_id,
                                                     GValue * value,
                                                     GParamSpec * pspec);
static void _vala_granite_switch_model_button_set_property (GObject * object,
                                                     guint property_id,
                                                     const GValue * value,
                                                     GParamSpec * pspec);

static inline gpointer
granite_switch_model_button_get_instance_private (GraniteSwitchModelButton* self)
{
	return G_STRUCT_MEMBER_P (self, GraniteSwitchModelButton_private_offset);
}

const gchar*
granite_switch_model_button_get_text (GraniteSwitchModelButton* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_text;
	result = _tmp0_;
	return result;
}

void
granite_switch_model_button_set_text (GraniteSwitchModelButton* self,
                                      const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = granite_switch_model_button_get_text (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_text);
		self->priv->_text = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, granite_switch_model_button_properties[GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY]);
	}
}

const gchar*
granite_switch_model_button_get_description (GraniteSwitchModelButton* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_description;
	result = _tmp0_;
	return result;
}

void
granite_switch_model_button_set_description (GraniteSwitchModelButton* self,
                                             const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = granite_switch_model_button_get_description (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_description);
		self->priv->_description = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, granite_switch_model_button_properties[GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY]);
	}
}

GraniteSwitchModelButton*
granite_switch_model_button_construct (GType object_type,
                                       const gchar* text)
{
	GraniteSwitchModelButton * self = NULL;
	g_return_val_if_fail (text != NULL, NULL);
	self = (GraniteSwitchModelButton*) g_object_new (object_type, "text", text, NULL);
	return self;
}

GraniteSwitchModelButton*
granite_switch_model_button_new (const gchar* text)
{
	return granite_switch_model_button_construct (GRANITE_TYPE_SWITCH_MODEL_BUTTON, text);
}

static Block6Data*
block6_data_ref (Block6Data* _data6_)
{
	g_atomic_int_inc (&_data6_->_ref_count_);
	return _data6_;
}

static void
block6_data_unref (void * _userdata_)
{
	Block6Data* _data6_;
	_data6_ = (Block6Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data6_->_ref_count_)) {
		GraniteSwitchModelButton* self;
		self = _data6_->self;
		_g_object_unref0 (_data6_->button_switch);
		_g_object_unref0 (_data6_->box);
		_g_object_unref0 (_data6_->description_revealer);
		_g_object_unref0 (self);
		g_slice_free (Block6Data, _data6_);
	}
}

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

static void
_granite_switch_model_button___lambda28_ (Block6Data* _data6_)
{
	GraniteSwitchModelButton* self;
	GtkSwitch* _tmp0_;
	self = _data6_->self;
	_tmp0_ = _data6_->button_switch;
	g_signal_emit_by_name (_tmp0_, "activate");
}

static void
__granite_switch_model_button___lambda28__gtk_gesture_click_released (GtkGestureClick* _sender,
                                                                      gint n_press,
                                                                      gdouble x,
                                                                      gdouble y,
                                                                      gpointer self)
{
	_granite_switch_model_button___lambda28_ (self);
}

static void
_granite_switch_model_button___lambda29_ (GraniteSwitchModelButton* self)
{
	gboolean _tmp0_;
	gboolean _tmp1_;
	_tmp0_ = gtk_toggle_button_get_active ((GtkToggleButton*) self);
	_tmp1_ = _tmp0_;
	gtk_accessible_update_state ((GtkAccessible*) self, GTK_ACCESSIBLE_STATE_CHECKED, _tmp1_, -1, -1);
}

static void
__granite_switch_model_button___lambda29__g_object_notify (GObject* _sender,
                                                           GParamSpec* pspec,
                                                           gpointer self)
{
	_granite_switch_model_button___lambda29_ ((GraniteSwitchModelButton*) self);
}

static void
_granite_switch_model_button___lambda30_ (Block6Data* _data6_)
{
	GraniteSwitchModelButton* self;
	const gchar* _tmp0_;
	gboolean _tmp1_ = FALSE;
	const gchar* _tmp2_;
	self = _data6_->self;
	_tmp0_ = self->priv->_description;
	gtk_accessible_update_property ((GtkAccessible*) self, GTK_ACCESSIBLE_PROPERTY_DESCRIPTION, _tmp0_, -1, -1);
	_tmp2_ = self->priv->_description;
	if (_tmp2_ == NULL) {
		_tmp1_ = TRUE;
	} else {
		const gchar* _tmp3_;
		_tmp3_ = self->priv->_description;
		_tmp1_ = g_strcmp0 (_tmp3_, "") == 0;
	}
	if (_tmp1_) {
		GtkBox* _tmp4_;
		GtkRevealer* _tmp5_;
		_tmp4_ = _data6_->box;
		_tmp5_ = _data6_->description_revealer;
		gtk_box_remove (_tmp4_, (GtkWidget*) _tmp5_);
	} else {
		GtkBox* _tmp6_;
		GtkRevealer* _tmp7_;
		GtkSwitch* _tmp8_;
		GtkRevealer* _tmp9_;
		_tmp6_ = _data6_->box;
		_tmp7_ = _data6_->description_revealer;
		gtk_box_append (_tmp6_, (GtkWidget*) _tmp7_);
		_tmp8_ = _data6_->button_switch;
		_tmp9_ = _data6_->description_revealer;
		g_object_bind_property_with_closures ((GObject*) _tmp8_, "active", (GObject*) _tmp9_, "reveal-child", G_BINDING_SYNC_CREATE, (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	}
}

static void
__granite_switch_model_button___lambda30__g_object_notify (GObject* _sender,
                                                           GParamSpec* pspec,
                                                           gpointer self)
{
	_granite_switch_model_button___lambda30_ (self);
}

static GObject *
granite_switch_model_button_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GraniteSwitchModelButton * self;
	Block6Data* _data6_;
	GtkLabel* label = NULL;
	const gchar* _tmp0_;
	GtkWidget* _tmp1_;
	GtkLabel* _tmp2_ = NULL;
	GtkLabel* description_label = NULL;
	GtkLabel* _tmp3_ = NULL;
	GtkLabel* _tmp4_;
	GtkLabel* _tmp5_;
	GtkLabel* _tmp6_;
	GtkWidget* _tmp7_;
	GtkRevealer* _tmp8_ = NULL;
	GtkBoxLayout* _tmp9_;
	GtkBox* _tmp10_;
	GtkBox* _tmp11_;
	GtkLabel* _tmp12_;
	GtkBox* _tmp13_;
	GtkSwitch* _tmp14_ = NULL;
	GtkSwitch* _tmp15_;
	GtkLabel* _tmp16_;
	GtkLabel* _tmp17_;
	GtkSwitch* _tmp18_;
	GtkGestureClick* controller = NULL;
	GtkGestureClick* _tmp19_;
	GtkGestureClick* _tmp20_;
	GtkEventController* _tmp21_;
	GtkGestureClick* _tmp22_;
	parent_class = G_OBJECT_CLASS (granite_switch_model_button_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRANITE_TYPE_SWITCH_MODEL_BUTTON, GraniteSwitchModelButton);
	_data6_ = g_slice_new0 (Block6Data);
	_data6_->_ref_count_ = 1;
	_data6_->self = g_object_ref (self);
	_tmp0_ = self->priv->_text;
	_tmp1_ = _g_object_ref0 ((GtkWidget*) self);
	_tmp2_ = (GtkLabel*) gtk_label_new (_tmp0_);
	gtk_widget_set_halign ((GtkWidget*) _tmp2_, GTK_ALIGN_START);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp2_, TRUE);
	gtk_label_set_max_width_chars (_tmp2_, 25);
	gtk_widget_set_vexpand ((GtkWidget*) _tmp2_, TRUE);
	gtk_label_set_wrap (_tmp2_, TRUE);
	gtk_label_set_xalign (_tmp2_, (gfloat) 0);
	gtk_label_set_mnemonic_widget (_tmp2_, _tmp1_);
	_g_object_unref0 (_tmp1_);
	g_object_ref_sink (_tmp2_);
	label = _tmp2_;
	_tmp3_ = (GtkLabel*) gtk_label_new (NULL);
	gtk_label_set_max_width_chars (_tmp3_, 25);
	gtk_label_set_wrap (_tmp3_, TRUE);
	gtk_label_set_xalign (_tmp3_, (gfloat) 0);
	g_object_ref_sink (_tmp3_);
	description_label = _tmp3_;
	_tmp4_ = description_label;
	gtk_widget_add_css_class ((GtkWidget*) _tmp4_, GRANITE_STYLE_CLASS_SMALL_LABEL);
	_tmp5_ = description_label;
	gtk_widget_add_css_class ((GtkWidget*) _tmp5_, GRANITE_STYLE_CLASS_DIM_LABEL);
	_tmp6_ = description_label;
	_tmp7_ = _g_object_ref0 ((GtkWidget*) _tmp6_);
	_tmp8_ = (GtkRevealer*) gtk_revealer_new ();
	gtk_revealer_set_child (_tmp8_, _tmp7_);
	_g_object_unref0 (_tmp7_);
	g_object_ref_sink (_tmp8_);
	_data6_->description_revealer = _tmp8_;
	_tmp9_ = (GtkBoxLayout*) gtk_box_layout_new (GTK_ORIENTATION_HORIZONTAL);
	gtk_widget_set_layout_manager ((GtkWidget*) self, (GtkLayoutManager*) _tmp9_);
	_tmp10_ = (GtkBox*) gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
	g_object_ref_sink (_tmp10_);
	_data6_->box = _tmp10_;
	_tmp11_ = _data6_->box;
	_tmp12_ = label;
	gtk_box_append (_tmp11_, (GtkWidget*) _tmp12_);
	_tmp13_ = _data6_->box;
	gtk_widget_set_parent ((GtkWidget*) _tmp13_, (GtkWidget*) self);
	_tmp14_ = (GtkSwitch*) gtk_switch_new ();
	gtk_widget_set_focusable ((GtkWidget*) _tmp14_, FALSE);
	gtk_widget_set_valign ((GtkWidget*) _tmp14_, GTK_ALIGN_START);
	g_object_ref_sink (_tmp14_);
	_data6_->button_switch = _tmp14_;
	_tmp15_ = _data6_->button_switch;
	gtk_widget_set_parent ((GtkWidget*) _tmp15_, (GtkWidget*) self);
	g_object_set ((GtkAccessible*) self, "accessible-role", GTK_ACCESSIBLE_ROLE_SWITCH, NULL);
	_tmp16_ = label;
	g_object_bind_property_with_closures ((GObject*) self, "text", (GObject*) _tmp16_, "label", G_BINDING_DEFAULT, (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	_tmp17_ = description_label;
	g_object_bind_property_with_closures ((GObject*) self, "description", (GObject*) _tmp17_, "label", G_BINDING_DEFAULT, (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	_tmp18_ = _data6_->button_switch;
	g_object_bind_property_with_closures ((GObject*) self, "active", (GObject*) _tmp18_, "active", G_BINDING_BIDIRECTIONAL | G_BINDING_SYNC_CREATE, (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)), (GClosure*) ((NULL == NULL) ? NULL : g_cclosure_new ((GCallback) NULL, NULL, (GClosureNotify) NULL)));
	_tmp19_ = (GtkGestureClick*) gtk_gesture_click_new ();
	controller = _tmp19_;
	_tmp20_ = controller;
	_tmp21_ = _g_object_ref0 ((GtkEventController*) _tmp20_);
	gtk_widget_add_controller ((GtkWidget*) self, _tmp21_);
	_tmp22_ = controller;
	g_signal_connect_data (_tmp22_, "released", (GCallback) __granite_switch_model_button___lambda28__gtk_gesture_click_released, block6_data_ref (_data6_), (GClosureNotify) block6_data_unref, 0);
	g_signal_connect_object ((GObject*) self, "notify::active", (GCallback) __granite_switch_model_button___lambda29__g_object_notify, self, 0);
	g_signal_connect_data ((GObject*) self, "notify::description", (GCallback) __granite_switch_model_button___lambda30__g_object_notify, block6_data_ref (_data6_), (GClosureNotify) block6_data_unref, 0);
	_g_object_unref0 (controller);
	_g_object_unref0 (description_label);
	_g_object_unref0 (label);
	block6_data_unref (_data6_);
	_data6_ = NULL;
	return obj;
}

static void
granite_switch_model_button_base_init (GraniteSwitchModelButtonClass * klass)
{
	gtk_widget_class_set_css_name (GTK_WIDGET_CLASS (klass), "modelbutton");
}

static void
granite_switch_model_button_class_init (GraniteSwitchModelButtonClass * klass,
                                        gpointer klass_data)
{
	granite_switch_model_button_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraniteSwitchModelButton_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_granite_switch_model_button_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_granite_switch_model_button_set_property;
	G_OBJECT_CLASS (klass)->constructor = granite_switch_model_button_constructor;
	G_OBJECT_CLASS (klass)->finalize = granite_switch_model_button_finalize;
	/**
	     * The label for the button.
	     */
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY, granite_switch_model_button_properties[GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY] = g_param_spec_string ("text", "text", "text", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT));
	/**
	     * Small, dim description text shown when active.
	     */
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY, granite_switch_model_button_properties[GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY] = g_param_spec_string ("description", "description", "description", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
granite_switch_model_button_instance_init (GraniteSwitchModelButton * self,
                                           gpointer klass)
{
	self->priv = granite_switch_model_button_get_instance_private (self);
}

static void
granite_switch_model_button_finalize (GObject * obj)
{
	GraniteSwitchModelButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRANITE_TYPE_SWITCH_MODEL_BUTTON, GraniteSwitchModelButton);
	while (TRUE) {
		GtkWidget* _tmp0_;
		GtkWidget* _tmp1_;
		_tmp0_ = gtk_widget_get_first_child ((GtkWidget*) self);
		if (!(_tmp0_ != NULL)) {
			break;
		}
		_tmp1_ = gtk_widget_get_first_child ((GtkWidget*) self);
		gtk_widget_unparent (_tmp1_);
	}
	_g_free0 (self->priv->_text);
	_g_free0 (self->priv->_description);
	G_OBJECT_CLASS (granite_switch_model_button_parent_class)->finalize (obj);
}

/**
 * SwitchModelButton is a {@link Gtk.ToggleButton} containing a {@link Gtk.Label}
 * and a {@link Gtk.Switch} and using the menuitem css name. It can optionally
 * show description text when activated.
 *
 * ''Example''<<BR>>
 * {{{
 *   var switchmodelbutton = new Granite.SwitchModelButton ("With Description") {
 *       active = true,
 *       description = "A description of additional affects related to the activation state of this switch"
 *   };
 * }}}
 */
static GType
granite_switch_model_button_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraniteSwitchModelButtonClass), (GBaseInitFunc) granite_switch_model_button_base_init, (GBaseFinalizeFunc) NULL, (GClassInitFunc) granite_switch_model_button_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraniteSwitchModelButton), 0, (GInstanceInitFunc) granite_switch_model_button_instance_init, NULL };
	GType granite_switch_model_button_type_id;
	granite_switch_model_button_type_id = g_type_register_static (gtk_toggle_button_get_type (), "GraniteSwitchModelButton", &g_define_type_info, 0);
	GraniteSwitchModelButton_private_offset = g_type_add_instance_private (granite_switch_model_button_type_id, sizeof (GraniteSwitchModelButtonPrivate));
	return granite_switch_model_button_type_id;
}

GType
granite_switch_model_button_get_type (void)
{
	static volatile gsize granite_switch_model_button_type_id__once = 0;
	if (g_once_init_enter (&granite_switch_model_button_type_id__once)) {
		GType granite_switch_model_button_type_id;
		granite_switch_model_button_type_id = granite_switch_model_button_get_type_once ();
		g_once_init_leave (&granite_switch_model_button_type_id__once, granite_switch_model_button_type_id);
	}
	return granite_switch_model_button_type_id__once;
}

static void
_vala_granite_switch_model_button_get_property (GObject * object,
                                                guint property_id,
                                                GValue * value,
                                                GParamSpec * pspec)
{
	GraniteSwitchModelButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRANITE_TYPE_SWITCH_MODEL_BUTTON, GraniteSwitchModelButton);
	switch (property_id) {
		case GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY:
		g_value_set_string (value, granite_switch_model_button_get_text (self));
		break;
		case GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY:
		g_value_set_string (value, granite_switch_model_button_get_description (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_granite_switch_model_button_set_property (GObject * object,
                                                guint property_id,
                                                const GValue * value,
                                                GParamSpec * pspec)
{
	GraniteSwitchModelButton * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRANITE_TYPE_SWITCH_MODEL_BUTTON, GraniteSwitchModelButton);
	switch (property_id) {
		case GRANITE_SWITCH_MODEL_BUTTON_TEXT_PROPERTY:
		granite_switch_model_button_set_text (self, g_value_get_string (value));
		break;
		case GRANITE_SWITCH_MODEL_BUTTON_DESCRIPTION_PROPERTY:
		granite_switch_model_button_set_description (self, g_value_get_string (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

