159 lines
4.2 KiB
C
159 lines
4.2 KiB
C
/*
|
|
* 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
|
|
|
|
static void vn_calendar_on_day_selected (GtkCalendar * calendar, VnCalendar * self)
|
|
{
|
|
guint year;
|
|
guint month;
|
|
guint day;
|
|
GDate new_date;
|
|
GValue value = G_VALUE_INIT;
|
|
|
|
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);
|
|
|
|
if (!g_date_valid (&self->date) || g_date_compare (&new_date, &self->date))
|
|
{
|
|
g_date_set_julian (&self->date, g_date_get_julian (&new_date));
|
|
gtk_calendar_mark_day (calendar, day);
|
|
g_value_init (&value, G_TYPE_DATE);
|
|
g_value_set_boxed (&value, &self->date);
|
|
}
|
|
else
|
|
{
|
|
g_date_clear (&self->date, 1);
|
|
g_value_init (&value, GVN_TYPE_NULL);
|
|
}
|
|
|
|
VN_FIELD_GET_CLASS (self)->value_changed (VN_FIELD (self), &value);
|
|
g_value_unset (&value);
|
|
}
|
|
|
|
void vn_calendar_on_month_changed (GtkCalendar * calendar, VnCalendar * self)
|
|
{
|
|
guint year;
|
|
guint month;
|
|
GDate * date = &self->date;
|
|
|
|
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);
|
|
gtk_calendar_select_day (self->calendar, day);
|
|
}
|
|
else
|
|
gtk_calendar_select_day (self->calendar, 0);
|
|
}
|
|
|
|
static void vn_calendar_set_value (VnCalendar * self, const GValue * value)
|
|
{
|
|
GDate * date = NULL;
|
|
|
|
if (G_VALUE_TYPE (value) != G_TYPE_DATE)
|
|
{
|
|
GValue new_value = G_VALUE_INIT;
|
|
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)
|
|
{
|
|
self->date = *date;
|
|
gtk_calendar_select_month (self->calendar
|
|
,g_date_get_month (date) - 1
|
|
,g_date_get_year (date)
|
|
);
|
|
}
|
|
else
|
|
{
|
|
g_date_clear (&self->date, 1);
|
|
gtk_calendar_select_day (self->calendar, 0);
|
|
gtk_calendar_clear_marks (self->calendar);
|
|
}
|
|
}
|
|
|
|
//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class
|
|
|
|
static void vn_calendar_init (VnCalendar * self)
|
|
{
|
|
g_date_clear (&self->date, 1);
|
|
|
|
self->calendar = GTK_CALENDAR (gtk_calendar_new ());
|
|
gtk_calendar_set_display_options (self->calendar,
|
|
GTK_CALENDAR_SHOW_HEADING | GTK_CALENDAR_SHOW_DAY_NAMES);
|
|
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
|
|
,NULL
|
|
);
|
|
gtk_container_add (GTK_CONTAINER (self), GTK_WIDGET (self->calendar));
|
|
|
|
VN_FIELD_GET_CLASS (self)->set_widget (VN_FIELD (self),
|
|
GTK_WIDGET (self->calendar));
|
|
}
|
|
|
|
static void vn_calendar_finalize (VnCalendar * self)
|
|
{
|
|
G_OBJECT_CLASS (vn_calendar_parent_class)->finalize (G_OBJECT (self));
|
|
}
|
|
|
|
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;
|
|
}
|