/* SwitchModelButton.c generated by valac 0.56.17, 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.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <float.h>
#include <math.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  {
	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 _Block13Data Block13Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

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

struct _Block13Data {
	int _ref_count_;
	GraniteSwitchModelButton* self;
	GtkRevealer* description_revealer;
	GtkSwitch* button_switch;
	GtkGrid* grid;
};

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 Block13Data* block13_data_ref (Block13Data* _data13_);
static void block13_data_unref (void * _userdata_);
static gboolean _granite_switch_model_button___lambda85_ (Block13Data* _data13_);
static gboolean __granite_switch_model_button___lambda85__gtk_widget_button_release_event (GtkWidget* _sender,
                                                                                    GdkEventButton* event,
                                                                                    gpointer self);
static void _granite_switch_model_button___lambda86_ (Block13Data* _data13_);
static void __granite_switch_model_button___lambda86__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 _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

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 Block13Data*
block13_data_ref (Block13Data* _data13_)
{
	g_atomic_int_inc (&_data13_->_ref_count_);
	return _data13_;
}

static void
block13_data_unref (void * _userdata_)
{
	Block13Data* _data13_;
	_data13_ = (Block13Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data13_->_ref_count_)) {
		GraniteSwitchModelButton* self;
		self = _data13_->self;
		_g_object_unref0 (_data13_->grid);
		_g_object_unref0 (_data13_->button_switch);
		_g_object_unref0 (_data13_->description_revealer);
		_g_object_unref0 (self);
		g_slice_free (Block13Data, _data13_);
	}
}

static gboolean
_granite_switch_model_button___lambda85_ (Block13Data* _data13_)
{
	GraniteSwitchModelButton* self;
	GtkSwitch* _tmp0_;
	gboolean result;
	self = _data13_->self;
	_tmp0_ = _data13_->button_switch;
	g_signal_emit_by_name (_tmp0_, "activate");
	result = GDK_EVENT_STOP;
	return result;
}

static gboolean
__granite_switch_model_button___lambda85__gtk_widget_button_release_event (GtkWidget* _sender,
                                                                           GdkEventButton* event,
                                                                           gpointer self)
{
	gboolean result;
	result = _granite_switch_model_button___lambda85_ (self);
	return result;
}

static void
_granite_switch_model_button___lambda86_ (Block13Data* _data13_)
{
	GraniteSwitchModelButton* self;
	gboolean _tmp0_ = FALSE;
	const gchar* _tmp1_;
	self = _data13_->self;
	_tmp1_ = self->priv->_description;
	if (_tmp1_ == NULL) {
		_tmp0_ = TRUE;
	} else {
		const gchar* _tmp2_;
		_tmp2_ = self->priv->_description;
		_tmp0_ = g_strcmp0 (_tmp2_, "") == 0;
	}
	if (_tmp0_) {
		GtkGrid* _tmp3_;
		GtkRevealer* _tmp4_;
		_tmp3_ = _data13_->grid;
		_tmp4_ = _data13_->description_revealer;
		gtk_container_remove ((GtkContainer*) _tmp3_, (GtkWidget*) _tmp4_);
	} else {
		GtkGrid* _tmp5_;
		GtkRevealer* _tmp6_;
		GtkSwitch* _tmp7_;
		GtkRevealer* _tmp8_;
		_tmp5_ = _data13_->grid;
		_tmp6_ = _data13_->description_revealer;
		gtk_grid_attach (_tmp5_, (GtkWidget*) _tmp6_, 0, 1, 1, 1);
		_tmp7_ = _data13_->button_switch;
		_tmp8_ = _data13_->description_revealer;
		g_object_bind_property_with_closures ((GObject*) _tmp7_, "active", (GObject*) _tmp8_, "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___lambda86__g_object_notify (GObject* _sender,
                                                           GParamSpec* pspec,
                                                           gpointer self)
{
	_granite_switch_model_button___lambda86_ (self);
}

static GObject *
granite_switch_model_button_constructor (GType type,
                                         guint n_construct_properties,
                                         GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GraniteSwitchModelButton * self;
	Block13Data* _data13_;
	GtkLabel* label = NULL;
	const gchar* _tmp0_;
	GtkLabel* _tmp1_ = NULL;
	GtkLabel* description_label = NULL;
	GtkLabel* _tmp2_ = NULL;
	GtkStyleContext* description_style_context = NULL;
	GtkLabel* _tmp3_;
	GtkStyleContext* _tmp4_;
	GtkStyleContext* _tmp5_;
	GtkStyleContext* _tmp6_;
	GtkRevealer* _tmp7_;
	GtkRevealer* _tmp8_;
	GtkLabel* _tmp9_;
	GtkSwitch* _tmp10_ = NULL;
	GtkGrid* _tmp11_ = NULL;
	GtkGrid* _tmp12_;
	GtkLabel* _tmp13_;
	GtkGrid* _tmp14_;
	GtkSwitch* _tmp15_;
	GtkGrid* _tmp16_;
	GtkLabel* _tmp17_;
	GtkLabel* _tmp18_;
	GtkSwitch* _tmp19_;
	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);
	_data13_ = g_slice_new0 (Block13Data);
	_data13_->_ref_count_ = 1;
	_data13_->self = g_object_ref (self);
	_tmp0_ = self->priv->_text;
	_tmp1_ = (GtkLabel*) gtk_label_new (_tmp0_);
	gtk_widget_set_halign ((GtkWidget*) _tmp1_, GTK_ALIGN_START);
	gtk_widget_set_hexpand ((GtkWidget*) _tmp1_, TRUE);
	gtk_widget_set_vexpand ((GtkWidget*) _tmp1_, TRUE);
	g_object_ref_sink (_tmp1_);
	label = _tmp1_;
	_tmp2_ = (GtkLabel*) gtk_label_new (NULL);
	gtk_label_set_max_width_chars (_tmp2_, 25);
	g_object_set (_tmp2_, "wrap", TRUE, NULL);
	gtk_label_set_xalign (_tmp2_, (gfloat) 0);
	g_object_ref_sink (_tmp2_);
	description_label = _tmp2_;
	_tmp3_ = description_label;
	_tmp4_ = gtk_widget_get_style_context ((GtkWidget*) _tmp3_);
	description_style_context = _tmp4_;
	_tmp5_ = description_style_context;
	gtk_style_context_add_class (_tmp5_, GRANITE_STYLE_CLASS_SMALL_LABEL);
	_tmp6_ = description_style_context;
	gtk_style_context_add_class (_tmp6_, GTK_STYLE_CLASS_DIM_LABEL);
	_tmp7_ = (GtkRevealer*) gtk_revealer_new ();
	g_object_ref_sink (_tmp7_);
	_data13_->description_revealer = _tmp7_;
	_tmp8_ = _data13_->description_revealer;
	_tmp9_ = description_label;
	gtk_container_add ((GtkContainer*) _tmp8_, (GtkWidget*) _tmp9_);
	_tmp10_ = (GtkSwitch*) gtk_switch_new ();
	gtk_widget_set_valign ((GtkWidget*) _tmp10_, GTK_ALIGN_START);
	g_object_ref_sink (_tmp10_);
	_data13_->button_switch = _tmp10_;
	_tmp11_ = (GtkGrid*) gtk_grid_new ();
	gtk_grid_set_column_spacing (_tmp11_, 12);
	g_object_ref_sink (_tmp11_);
	_data13_->grid = _tmp11_;
	_tmp12_ = _data13_->grid;
	_tmp13_ = label;
	gtk_grid_attach (_tmp12_, (GtkWidget*) _tmp13_, 0, 0, 1, 1);
	_tmp14_ = _data13_->grid;
	_tmp15_ = _data13_->button_switch;
	gtk_grid_attach (_tmp14_, (GtkWidget*) _tmp15_, 1, 0, 1, 2);
	_tmp16_ = _data13_->grid;
	gtk_container_add ((GtkContainer*) self, (GtkWidget*) _tmp16_);
	_tmp17_ = label;
	g_object_bind_property_with_closures ((GObject*) self, "text", (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_ = description_label;
	g_object_bind_property_with_closures ((GObject*) self, "description", (GObject*) _tmp18_, "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)));
	_tmp19_ = _data13_->button_switch;
	g_object_bind_property_with_closures ((GObject*) self, "active", (GObject*) _tmp19_, "active", 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)));
	g_signal_connect_data ((GtkWidget*) self, "button-release-event", (GCallback) __granite_switch_model_button___lambda85__gtk_widget_button_release_event, block13_data_ref (_data13_), (GClosureNotify) block13_data_unref, 0);
	g_signal_connect_data ((GObject*) self, "notify::description", (GCallback) __granite_switch_model_button___lambda86__g_object_notify, block13_data_ref (_data13_), (GClosureNotify) block13_data_unref, 0);
	_g_object_unref0 (description_label);
	_g_object_unref0 (label);
	block13_data_unref (_data13_);
	_data13_ = NULL;
	return obj;
}

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

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

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

