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

/*
 * Copyright 2019-2022 elementary, Inc. (https://elementary.io)
 * Copyright 2011–2013 Maxwell Barvian <maxwell@elementaryos.org>
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include "granite-7.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <gtk/gtk.h>
#include <gio/gio.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_DATE_PICKER_0_PROPERTY,
	GRANITE_DATE_PICKER_FORMAT_PROPERTY,
	GRANITE_DATE_PICKER_DATE_PROPERTY,
	GRANITE_DATE_PICKER_NUM_PROPERTIES
};
static GParamSpec* granite_date_picker_properties[GRANITE_DATE_PICKER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_date_time_unref0(var) ((var == NULL) ? NULL : (var = (g_date_time_unref (var), NULL)))
typedef struct _Block2Data Block2Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _GraniteDatePickerPrivate {
	gchar* _format;
	GDateTime* _date;
};

struct _Block2Data {
	int _ref_count_;
	GraniteDatePicker* self;
	GtkCalendar* calendar;
	GtkPopover* popover;
};

static gint GraniteDatePicker_private_offset;
static gpointer granite_date_picker_parent_class = NULL;
static GtkBuildableIface * granite_date_picker_gtk_buildable_parent_iface = NULL;

static GObject * granite_date_picker_constructor (GType type,
                                           guint n_construct_properties,
                                           GObjectConstructParam * construct_properties);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static void _granite_date_picker___lambda10_ (Block2Data* _data2_);
static void __granite_date_picker___lambda10__gtk_entry_icon_release (GtkEntry* _sender,
                                                               GtkEntryIconPosition icon_pos,
                                                               gpointer self);
static void _granite_date_picker___lambda11_ (Block2Data* _data2_);
static void __granite_date_picker___lambda11__gtk_calendar_day_selected (GtkCalendar* _sender,
                                                                  gpointer self);
static void _granite_date_picker___lambda12_ (Block2Data* _data2_);
static void __granite_date_picker___lambda12__g_object_notify (GObject* _sender,
                                                        GParamSpec* pspec,
                                                        gpointer self);
static void granite_date_picker_finalize (GObject * obj);
static GType granite_date_picker_get_type_once (void);
static void _vala_granite_date_picker_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec);
static void _vala_granite_date_picker_set_property (GObject * object,
                                             guint property_id,
                                             const GValue * value,
                                             GParamSpec * pspec);

static inline gpointer
granite_date_picker_get_instance_private (GraniteDatePicker* self)
{
	return G_STRUCT_MEMBER_P (self, GraniteDatePicker_private_offset);
}

const gchar*
granite_date_picker_get_format (GraniteDatePicker* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_format;
	result = _tmp0_;
	return result;
}

static void
granite_date_picker_set_format (GraniteDatePicker* self,
                                const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = granite_date_picker_get_format (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_format);
		self->priv->_format = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, granite_date_picker_properties[GRANITE_DATE_PICKER_FORMAT_PROPERTY]);
	}
}

GDateTime*
granite_date_picker_get_date (GraniteDatePicker* self)
{
	GDateTime* result;
	GDateTime* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_date;
	result = _tmp0_;
	return result;
}

static gpointer
_g_date_time_ref0 (gpointer self)
{
	return self ? g_date_time_ref (self) : NULL;
}

void
granite_date_picker_set_date (GraniteDatePicker* self,
                              GDateTime* value)
{
	GDateTime* old_value;
	g_return_if_fail (self != NULL);
	old_value = granite_date_picker_get_date (self);
	if (old_value != value) {
		GDateTime* _tmp0_;
		_tmp0_ = _g_date_time_ref0 (value);
		_g_date_time_unref0 (self->priv->_date);
		self->priv->_date = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, granite_date_picker_properties[GRANITE_DATE_PICKER_DATE_PROPERTY]);
	}
}

/**
     * Makes a new DatePicker
     *
     * @param format desired format of new DatePicker
     */
GraniteDatePicker*
granite_date_picker_construct_with_format (GType object_type,
                                           const gchar* format)
{
	GraniteDatePicker * self = NULL;
	g_return_val_if_fail (format != NULL, NULL);
	self = (GraniteDatePicker*) g_object_new (object_type, "format", format, NULL);
	return self;
}

GraniteDatePicker*
granite_date_picker_new_with_format (const gchar* format)
{
	return granite_date_picker_construct_with_format (GRANITE_TYPE_DATE_PICKER, format);
}

GraniteDatePicker*
granite_date_picker_construct (GType object_type)
{
	GraniteDatePicker * self = NULL;
	self = (GraniteDatePicker*) g_object_new (object_type, NULL);
	return self;
}

GraniteDatePicker*
granite_date_picker_new (void)
{
	return granite_date_picker_construct (GRANITE_TYPE_DATE_PICKER);
}

static Block2Data*
block2_data_ref (Block2Data* _data2_)
{
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}

static void
block2_data_unref (void * _userdata_)
{
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		GraniteDatePicker* self;
		self = _data2_->self;
		_g_object_unref0 (_data2_->popover);
		_g_object_unref0 (_data2_->calendar);
		_g_object_unref0 (self);
		g_slice_free (Block2Data, _data2_);
	}
}

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

static void
_granite_date_picker___lambda10_ (Block2Data* _data2_)
{
	GraniteDatePicker* self;
	GtkPopover* _tmp0_;
	self = _data2_->self;
	_tmp0_ = _data2_->popover;
	gtk_popover_popup (_tmp0_);
}

static void
__granite_date_picker___lambda10__gtk_entry_icon_release (GtkEntry* _sender,
                                                          GtkEntryIconPosition icon_pos,
                                                          gpointer self)
{
	_granite_date_picker___lambda10_ (self);
}

static void
_granite_date_picker___lambda11_ (Block2Data* _data2_)
{
	GraniteDatePicker* self;
	GtkCalendar* _tmp0_;
	gint _tmp1_;
	gint _tmp2_;
	GtkCalendar* _tmp3_;
	gint _tmp4_;
	gint _tmp5_;
	GtkCalendar* _tmp6_;
	gint _tmp7_;
	gint _tmp8_;
	GDateTime* _tmp9_;
	GDateTime* _tmp10_;
	self = _data2_->self;
	_tmp0_ = _data2_->calendar;
	g_object_get (_tmp0_, "year", &_tmp1_, NULL);
	_tmp2_ = _tmp1_;
	_tmp3_ = _data2_->calendar;
	g_object_get (_tmp3_, "month", &_tmp4_, NULL);
	_tmp5_ = _tmp4_;
	_tmp6_ = _data2_->calendar;
	g_object_get (_tmp6_, "day", &_tmp7_, NULL);
	_tmp8_ = _tmp7_;
	_tmp9_ = g_date_time_new_local (_tmp2_, _tmp5_ + 1, _tmp8_, 0, 0, (gdouble) 0);
	_tmp10_ = _tmp9_;
	granite_date_picker_set_date (self, _tmp10_);
	_g_date_time_unref0 (_tmp10_);
}

static void
__granite_date_picker___lambda11__gtk_calendar_day_selected (GtkCalendar* _sender,
                                                             gpointer self)
{
	_granite_date_picker___lambda11_ (self);
}

static void
_granite_date_picker___lambda12_ (Block2Data* _data2_)
{
	GraniteDatePicker* self;
	GDateTime* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* _tmp3_;
	GtkCalendar* _tmp4_;
	GDateTime* _tmp5_;
	self = _data2_->self;
	_tmp0_ = self->priv->_date;
	_tmp1_ = self->priv->_format;
	_tmp2_ = g_date_time_format (_tmp0_, _tmp1_);
	_tmp3_ = _tmp2_;
	gtk_editable_set_text ((GtkEditable*) self, _tmp3_);
	_g_free0 (_tmp3_);
	_tmp4_ = _data2_->calendar;
	_tmp5_ = self->priv->_date;
	gtk_calendar_select_day (_tmp4_, _tmp5_);
}

static void
__granite_date_picker___lambda12__g_object_notify (GObject* _sender,
                                                   GParamSpec* pspec,
                                                   gpointer self)
{
	_granite_date_picker___lambda12_ (self);
}

static GObject *
granite_date_picker_constructor (GType type,
                                 guint n_construct_properties,
                                 GObjectConstructParam * construct_properties)
{
	GObject * obj;
	GObjectClass * parent_class;
	GraniteDatePicker * self;
	Block2Data* _data2_;
	const gchar* _tmp0_;
	GtkCalendar* _tmp3_;
	GtkCalendar* _tmp4_;
	GtkWidget* _tmp5_;
	GtkPopover* _tmp6_ = NULL;
	GtkPopover* _tmp7_;
	GDateTime* _tmp8_;
	GDateTime* _tmp9_;
	GThemedIcon* _tmp10_;
	GThemedIcon* _tmp11_;
	GThemedIcon* _tmp12_;
	GThemedIcon* _tmp13_;
	GtkCalendar* _tmp14_;
	parent_class = G_OBJECT_CLASS (granite_date_picker_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRANITE_TYPE_DATE_PICKER, GraniteDatePicker);
	_data2_ = g_slice_new0 (Block2Data);
	_data2_->_ref_count_ = 1;
	_data2_->self = g_object_ref (self);
	_tmp0_ = self->priv->_format;
	if (_tmp0_ == NULL) {
		gchar* _tmp1_;
		gchar* _tmp2_;
		_tmp1_ = granite_date_time_get_default_date_format (FALSE, TRUE, TRUE);
		_tmp2_ = _tmp1_;
		granite_date_picker_set_format (self, _tmp2_);
		_g_free0 (_tmp2_);
	}
	_tmp3_ = (GtkCalendar*) gtk_calendar_new ();
	g_object_ref_sink (_tmp3_);
	_data2_->calendar = _tmp3_;
	_tmp4_ = _data2_->calendar;
	_tmp5_ = _g_object_ref0 ((GtkWidget*) _tmp4_);
	_tmp6_ = (GtkPopover*) gtk_popover_new ();
	gtk_widget_set_halign ((GtkWidget*) _tmp6_, GTK_ALIGN_END);
	gtk_popover_set_autohide (_tmp6_, TRUE);
	gtk_popover_set_child (_tmp6_, _tmp5_);
	_g_object_unref0 (_tmp5_);
	gtk_popover_set_has_arrow (_tmp6_, FALSE);
	gtk_popover_set_position (_tmp6_, GTK_POS_BOTTOM);
	g_object_ref_sink (_tmp6_);
	_data2_->popover = _tmp6_;
	_tmp7_ = _data2_->popover;
	gtk_widget_set_parent ((GtkWidget*) _tmp7_, (GtkWidget*) self);
	_tmp8_ = g_date_time_new_now_local ();
	_tmp9_ = _tmp8_;
	granite_date_picker_set_date (self, _tmp9_);
	_g_date_time_unref0 (_tmp9_);
	gtk_editable_set_editable ((GtkEditable*) self, FALSE);
	_tmp10_ = (GThemedIcon*) g_themed_icon_new_with_default_fallbacks ("office-calendar-symbolic");
	_tmp11_ = _tmp10_;
	g_object_set ((GtkEntry*) self, "primary-icon-gicon", (GIcon*) _tmp11_, NULL);
	_g_object_unref0 (_tmp11_);
	_tmp12_ = (GThemedIcon*) g_themed_icon_new_with_default_fallbacks ("pan-down-symbolic");
	_tmp13_ = _tmp12_;
	g_object_set ((GtkEntry*) self, "secondary-icon-gicon", (GIcon*) _tmp13_, NULL);
	_g_object_unref0 (_tmp13_);
	gtk_widget_add_css_class ((GtkWidget*) self, "date-picker");
	g_signal_connect_data ((GtkEntry*) self, "icon-release", (GCallback) __granite_date_picker___lambda10__gtk_entry_icon_release, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	_tmp14_ = _data2_->calendar;
	g_signal_connect_data (_tmp14_, "day-selected", (GCallback) __granite_date_picker___lambda11__gtk_calendar_day_selected, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	g_signal_connect_data ((GObject*) self, "notify::date", (GCallback) __granite_date_picker___lambda12__g_object_notify, block2_data_ref (_data2_), (GClosureNotify) block2_data_unref, 0);
	block2_data_unref (_data2_);
	_data2_ = NULL;
	return obj;
}

static void
granite_date_picker_class_init (GraniteDatePickerClass * klass,
                                gpointer klass_data)
{
	granite_date_picker_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &GraniteDatePicker_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_granite_date_picker_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_granite_date_picker_set_property;
	G_OBJECT_CLASS (klass)->constructor = granite_date_picker_constructor;
	G_OBJECT_CLASS (klass)->finalize = granite_date_picker_finalize;
	/**
	     * Desired format of DatePicker
	     */
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRANITE_DATE_PICKER_FORMAT_PROPERTY, granite_date_picker_properties[GRANITE_DATE_PICKER_FORMAT_PROPERTY] = g_param_spec_string ("format", "format", "format", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY));
	/**
	     * Current Date
	     */
	g_object_class_install_property (G_OBJECT_CLASS (klass), GRANITE_DATE_PICKER_DATE_PROPERTY, granite_date_picker_properties[GRANITE_DATE_PICKER_DATE_PROPERTY] = g_param_spec_boxed ("date", "date", "date", G_TYPE_DATE_TIME, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE | G_PARAM_WRITABLE));
}

static void
granite_date_picker_gtk_buildable_interface_init (GtkBuildableIface * iface,
                                                  gpointer iface_data)
{
	granite_date_picker_gtk_buildable_parent_iface = g_type_interface_peek_parent (iface);
}

static void
granite_date_picker_instance_init (GraniteDatePicker * self,
                                   gpointer klass)
{
	self->priv = granite_date_picker_get_instance_private (self);
}

static void
granite_date_picker_finalize (GObject * obj)
{
	GraniteDatePicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, GRANITE_TYPE_DATE_PICKER, GraniteDatePicker);
	_g_free0 (self->priv->_format);
	_g_date_time_unref0 (self->priv->_date);
	G_OBJECT_CLASS (granite_date_picker_parent_class)->finalize (obj);
}

/**
 * This class allows users to pick dates from a calendar.
 */
static GType
granite_date_picker_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (GraniteDatePickerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) granite_date_picker_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GraniteDatePicker), 0, (GInstanceInitFunc) granite_date_picker_instance_init, NULL };
	static const GInterfaceInfo gtk_buildable_info = { (GInterfaceInitFunc) granite_date_picker_gtk_buildable_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType granite_date_picker_type_id;
	granite_date_picker_type_id = g_type_register_static (gtk_entry_get_type (), "GraniteDatePicker", &g_define_type_info, 0);
	g_type_add_interface_static (granite_date_picker_type_id, gtk_buildable_get_type (), &gtk_buildable_info);
	GraniteDatePicker_private_offset = g_type_add_instance_private (granite_date_picker_type_id, sizeof (GraniteDatePickerPrivate));
	return granite_date_picker_type_id;
}

GType
granite_date_picker_get_type (void)
{
	static volatile gsize granite_date_picker_type_id__once = 0;
	if (g_once_init_enter (&granite_date_picker_type_id__once)) {
		GType granite_date_picker_type_id;
		granite_date_picker_type_id = granite_date_picker_get_type_once ();
		g_once_init_leave (&granite_date_picker_type_id__once, granite_date_picker_type_id);
	}
	return granite_date_picker_type_id__once;
}

static void
_vala_granite_date_picker_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec)
{
	GraniteDatePicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRANITE_TYPE_DATE_PICKER, GraniteDatePicker);
	switch (property_id) {
		case GRANITE_DATE_PICKER_FORMAT_PROPERTY:
		g_value_set_string (value, granite_date_picker_get_format (self));
		break;
		case GRANITE_DATE_PICKER_DATE_PROPERTY:
		g_value_set_boxed (value, granite_date_picker_get_date (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_granite_date_picker_set_property (GObject * object,
                                        guint property_id,
                                        const GValue * value,
                                        GParamSpec * pspec)
{
	GraniteDatePicker * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, GRANITE_TYPE_DATE_PICKER, GraniteDatePicker);
	switch (property_id) {
		case GRANITE_DATE_PICKER_FORMAT_PROPERTY:
		granite_date_picker_set_format (self, g_value_get_string (value));
		break;
		case GRANITE_DATE_PICKER_DATE_PROPERTY:
		granite_date_picker_set_date (self, g_value_get_boxed (value));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

