2013-10-11 23:07:35 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2012 - Juan Ferrer Toribio
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vn-calendar.h"
|
|
|
|
#include <db/db.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* SECTION:vn-calendar
|
|
|
|
* @Short_description: a calendar widget
|
|
|
|
* @Title: VnCalendar
|
|
|
|
* @See_also: #VnField
|
|
|
|
* @Image: vn-calendar.png
|
|
|
|
*
|
|
|
|
* A calendar widget to see or set the date.
|
|
|
|
*/
|
|
|
|
G_DEFINE_TYPE (VnCalendar, vn_calendar, VN_TYPE_FIELD);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* vn_calendar_new:
|
|
|
|
*
|
|
|
|
* Creates a new #VnCalendar
|
|
|
|
*
|
|
|
|
* Return value: a #VnCalendar
|
|
|
|
**/
|
|
|
|
VnField * vn_calendar_new ()
|
|
|
|
{
|
|
|
|
return g_object_new (VN_TYPE_CALENDAR, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Private
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
static void vn_calendar_on_day_selected (GtkCalendar * calendar, VnCalendar * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
guint year;
|
|
|
|
guint month;
|
|
|
|
guint day;
|
|
|
|
GDate new_date;
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
gtk_calendar_clear_marks (calendar);
|
|
|
|
gtk_calendar_get_date (calendar, &year, &month, &day);
|
|
|
|
month++;
|
|
|
|
|
|
|
|
g_date_clear (&new_date, 1);
|
|
|
|
g_date_set_dmy (&new_date, day, month, year);
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
if (!g_date_valid (&self->date) || g_date_compare (&new_date, &self->date))
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
g_date_set_julian (&self->date, g_date_get_julian (&new_date));
|
2013-10-11 23:07:35 +00:00
|
|
|
gtk_calendar_mark_day (calendar, day);
|
|
|
|
g_value_init (&value, G_TYPE_DATE);
|
2014-07-16 14:12:57 +00:00
|
|
|
g_value_set_boxed (&value, &self->date);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
g_date_clear (&self->date, 1);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_init (&value, GVN_TYPE_NULL);
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
VN_FIELD_GET_CLASS (self)->value_changed (VN_FIELD (self), &value);
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_unset (&value);
|
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
void vn_calendar_on_month_changed (GtkCalendar * calendar, VnCalendar * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
guint year;
|
|
|
|
guint month;
|
2014-07-16 14:12:57 +00:00
|
|
|
GDate * date = &self->date;
|
2013-10-11 23:07:35 +00:00
|
|
|
|
|
|
|
gtk_calendar_clear_marks (calendar);
|
|
|
|
gtk_calendar_get_date (calendar, &year, &month, NULL);
|
|
|
|
|
|
|
|
if (g_date_valid (date)
|
|
|
|
&& g_date_get_month (date) == month + 1
|
|
|
|
&& g_date_get_year (date) == year)
|
|
|
|
{
|
|
|
|
guint day = g_date_get_day (date);
|
|
|
|
gtk_calendar_mark_day (calendar, day);
|
2014-07-16 14:12:57 +00:00
|
|
|
gtk_calendar_select_day (self->calendar, day);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
else
|
2014-07-16 14:12:57 +00:00
|
|
|
gtk_calendar_select_day (self->calendar, 0);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
static void vn_calendar_set_value (VnCalendar * self, const GValue * value)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
|
|
|
GDate * date = NULL;
|
|
|
|
|
|
|
|
if (G_VALUE_TYPE (value) != G_TYPE_DATE)
|
|
|
|
{
|
2014-02-11 12:16:39 +00:00
|
|
|
GValue new_value = G_VALUE_INIT;
|
2013-10-11 23:07:35 +00:00
|
|
|
g_value_init (&new_value, G_TYPE_DATE);
|
|
|
|
g_value_transform (value, &new_value);
|
|
|
|
date = g_value_get_boxed (&new_value);
|
|
|
|
}
|
|
|
|
else if (!gvn_value_is_null (value))
|
|
|
|
date = g_value_get_boxed (value);
|
|
|
|
|
|
|
|
if (date)
|
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
self->date = *date;
|
|
|
|
gtk_calendar_select_month (self->calendar
|
2013-10-11 23:07:35 +00:00
|
|
|
,g_date_get_month (date) - 1
|
|
|
|
,g_date_get_year (date)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
g_date_clear (&self->date, 1);
|
|
|
|
gtk_calendar_select_day (self->calendar, 0);
|
|
|
|
gtk_calendar_clear_marks (self->calendar);
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
static void vn_calendar_init (VnCalendar * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
g_date_clear (&self->date, 1);
|
2013-10-11 23:07:35 +00:00
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
self->calendar = GTK_CALENDAR (gtk_calendar_new ());
|
|
|
|
gtk_calendar_set_display_options (self->calendar,
|
2013-10-11 23:07:35 +00:00
|
|
|
GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
|
2014-07-16 14:12:57 +00:00
|
|
|
g_object_connect (self->calendar
|
|
|
|
,"signal::day-selected-double-click", vn_calendar_on_day_selected, self
|
|
|
|
,"signal::month-changed", vn_calendar_on_month_changed, self
|
2013-10-11 23:07:35 +00:00
|
|
|
,NULL
|
|
|
|
);
|
2014-07-16 14:12:57 +00:00
|
|
|
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->calendar));
|
|
|
|
|
|
|
|
VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
|
|
|
|
GTK_WIDGET (self->calendar));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
2014-07-16 14:12:57 +00:00
|
|
|
static void vn_calendar_finalize (VnCalendar * self)
|
2013-10-11 23:07:35 +00:00
|
|
|
{
|
2014-07-16 14:12:57 +00:00
|
|
|
G_OBJECT_CLASS (vn_calendar_parent_class)->finalize (G_OBJECT (self));
|
2013-10-11 23:07:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void vn_calendar_class_init (VnCalendarClass * klass)
|
|
|
|
{
|
|
|
|
G_OBJECT_CLASS (klass)->finalize = (GObjectFinalizeFunc) vn_calendar_finalize;
|
|
|
|
VN_FIELD_CLASS (klass)->set_value = (VnFieldSetValueFunc) vn_calendar_set_value;
|
|
|
|
}
|