gui: Move DateChooser to common

DateChooser is used both by the event editor and the main window.
Moving it to common for a better file organization.
This commit is contained in:
Titouan Real
2025-04-09 15:35:20 +00:00
committed by Georges Basile Stavracas Neto
parent 0aa0619e99
commit 303cba1622
10 changed files with 6 additions and 6 deletions
+2
View File
@@ -3,5 +3,7 @@
<gresource prefix="/org/gnome/calendar/ui/common">
<file compressed="true">gcal-calendar-combo-row-item.ui</file>
<file compressed="true">gcal-calendar-combo-row.ui</file>
<file compressed="true">gcal-date-chooser.ui</file>
<file compressed="true">gcal-date-chooser-day.ui</file>
</gresource>
</gresources>
+312
View File
@@ -0,0 +1,312 @@
/* GTK - The GIMP Toolkit
*
* Copyright (C) 2015 Red Hat, Inc.
* Copyright (C) 2022 Purism SPC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#define G_LOG_DOMAIN "GcalDateChooserDay"
#include "config.h"
#include "gcal-date-chooser-day.h"
#include "gcal-date-time-utils.h"
#include "gcal-utils.h"
#include <stdlib.h>
#include <langinfo.h>
struct _GcalDateChooserDay
{
GtkButton parent;
GtkWidget *dot_revealer;
GtkWidget *label;
GDateTime *date;
};
enum {
PROP_0,
PROP_HAS_DOT,
PROP_DOT_VISIBLE,
NUM_PROPS
};
static GParamSpec *properties[NUM_PROPS] = { NULL, };
G_DEFINE_TYPE (GcalDateChooserDay, gcal_date_chooser_day, GTK_TYPE_BUTTON)
static void
gcal_date_chooser_day_dispose (GObject *object)
{
GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (object);
g_clear_pointer (&self->date, g_date_time_unref);
G_OBJECT_CLASS (gcal_date_chooser_day_parent_class)->dispose (object);
}
static void
gcal_date_chooser_day_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (object);
switch (prop_id)
{
case PROP_HAS_DOT:
g_value_set_boolean (value, gcal_date_chooser_day_get_has_dot (self));
break;
case PROP_DOT_VISIBLE:
g_value_set_boolean (value, gcal_date_chooser_day_get_dot_visible (self));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_date_chooser_day_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (object);
switch (prop_id)
{
case PROP_HAS_DOT:
gcal_date_chooser_day_set_has_dot (self, g_value_get_boolean (value));
break;
case PROP_DOT_VISIBLE:
gcal_date_chooser_day_set_dot_visible (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_date_chooser_day_class_init (GcalDateChooserDayClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
object_class->dispose = gcal_date_chooser_day_dispose;
object_class->get_property = gcal_date_chooser_day_get_property;
object_class->set_property = gcal_date_chooser_day_set_property;
/**
* GcalDateChooserDay:has-dot:
*
* Whether the day has a dot or not.
*
* If the day has a dot, the label will be excentered to leave room for the
* dot, it will be centered otherwise.
*/
properties[PROP_HAS_DOT] =
g_param_spec_boolean ("has-dot",
"Has dot",
"Whether the day has a dot or not",
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
/**
* GcalDateChooserDay:dot-visible:
*
* Whether the dot is visible or not.
*
* There will be no visible dot if [property@GcalDateChooserDay:has-dot] is
* #FALSE.
*/
properties[PROP_DOT_VISIBLE] =
g_param_spec_boolean ("dot-visible",
"Dot visible",
"Whether the dot is visible or not",
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, NUM_PROPS, properties);
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/common/gcal-date-chooser-day.ui");
gtk_widget_class_bind_template_child (widget_class, GcalDateChooserDay, dot_revealer);
gtk_widget_class_bind_template_child (widget_class, GcalDateChooserDay, label);
}
static void
gcal_date_chooser_day_init (GcalDateChooserDay *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
}
GtkWidget*
gcal_date_chooser_day_new (void)
{
return g_object_new (GCAL_TYPE_DATE_CHOOSER_DAY, NULL);
}
void
gcal_date_chooser_day_set_date (GcalDateChooserDay *self,
GDateTime *date)
{
GtkWidget *widget = GTK_WIDGET (self);
g_autoptr (GDateTime) now = NULL;
g_autofree gchar *text = NULL;
gboolean today;
gint weekday;
g_assert (date != NULL);
g_clear_pointer (&self->date, g_date_time_unref);
self->date = g_date_time_ref (date);
now = g_date_time_new_now (g_date_time_get_timezone (date));
today = gcal_date_time_compare_date (date, now) == 0;
weekday = g_date_time_get_day_of_week (date) % 7;
if (G_UNLIKELY (today))
gtk_widget_add_css_class (widget, "today");
else
gtk_widget_remove_css_class (widget, "today");
if (G_LIKELY (is_workday (weekday)))
gtk_widget_remove_css_class (widget, "non-workday");
else
gtk_widget_add_css_class (widget, "non-workday");
text = g_strdup_printf ("%d", g_date_time_get_day_of_month (date));
gtk_label_set_label (GTK_LABEL (self->label), text);
}
GDateTime*
gcal_date_chooser_day_get_date (GcalDateChooserDay *self)
{
return self->date;
}
void
gcal_date_chooser_day_set_other_month (GcalDateChooserDay *self,
gboolean other_month)
{
if (other_month)
gtk_widget_add_css_class (GTK_WIDGET (self), "other-month");
else
gtk_widget_remove_css_class (GTK_WIDGET (self), "other-month");
}
/**
* gcal_date_chooser_day_set_has_dot:
* @self: a #GcalDateChooserDay
* @has_dot: whether the day has a dot or not.
*
* Sets whether the day has a dot or not.
*
* If the day has a dot, the label will be excentered to leave room for the
* dot, it will be centered otherwise.
*/
void
gcal_date_chooser_day_set_has_dot (GcalDateChooserDay *self,
gboolean has_dot)
{
g_return_if_fail (GCAL_IS_DATE_CHOOSER_DAY (self));
has_dot = !!has_dot;
if (gtk_widget_get_visible (self->dot_revealer) == has_dot)
return;
gtk_widget_set_visible (self->dot_revealer, has_dot);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_HAS_DOT]);
}
/**
* gcal_date_chooser_day_get_has_dot:
* @self: a #GcalDateChooserDay
*
* Returns: whether the day has a dot or not.
*/
gboolean
gcal_date_chooser_day_get_has_dot (GcalDateChooserDay *self)
{
g_return_val_if_fail (GCAL_IS_DATE_CHOOSER_DAY (self), FALSE);
return gtk_widget_get_visible (self->dot_revealer);
}
/**
* gcal_date_chooser_day_set_dot_visible:
* @self: a #GcalDateChooserDay
* @dot_visible: whether the dot is visible or not.
*
* Sets whether the dot is visible or not.
*
* There will be no visible dot if [property@GcalDateChooserDay:has-dot] is
* #FALSE.
*/
void
gcal_date_chooser_day_set_dot_visible (GcalDateChooserDay *self,
gboolean dot_visible)
{
g_return_if_fail (GCAL_IS_DATE_CHOOSER_DAY (self));
dot_visible = !!dot_visible;
if (gtk_revealer_get_reveal_child (GTK_REVEALER (self->dot_revealer)) == dot_visible)
return;
gtk_revealer_set_reveal_child (GTK_REVEALER (self->dot_revealer), dot_visible);
g_object_notify_by_pspec (G_OBJECT (self), properties[PROP_DOT_VISIBLE]);
}
/**
* gcal_date_chooser_day_get_dot_visible:
* @self: a #GcalDateChooserDay
*
* Returns: whether the dot is visible or not.
*/
gboolean
gcal_date_chooser_day_get_dot_visible (GcalDateChooserDay *self)
{
g_return_val_if_fail (GCAL_IS_DATE_CHOOSER_DAY (self), FALSE);
return gtk_revealer_get_reveal_child (GTK_REVEALER (self->dot_revealer));
}
void
gcal_date_chooser_day_set_selected (GcalDateChooserDay *self,
gboolean selected)
{
GtkWidget *widget = GTK_WIDGET (self);
if (G_UNLIKELY (selected))
{
gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_SELECTED, FALSE);
gtk_widget_remove_css_class (widget, "flat");
}
else
{
gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_SELECTED);
gtk_widget_add_css_class (widget, "flat");
}
}
+51
View File
@@ -0,0 +1,51 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2015 Red Hat, Inc.
* Copyright (C) 2022 Purism SPC
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define GCAL_TYPE_DATE_CHOOSER_DAY (gcal_date_chooser_day_get_type())
G_DECLARE_FINAL_TYPE (GcalDateChooserDay, gcal_date_chooser_day, GCAL, DATE_CHOOSER_DAY, GtkButton)
GtkWidget* gcal_date_chooser_day_new (void);
GDateTime* gcal_date_chooser_day_get_date (GcalDateChooserDay *day);
void gcal_date_chooser_day_set_date (GcalDateChooserDay *day,
GDateTime *date);
gboolean gcal_date_chooser_day_get_has_dot (GcalDateChooserDay *day);
void gcal_date_chooser_day_set_has_dot (GcalDateChooserDay *day,
gboolean has_dot);
gboolean gcal_date_chooser_day_get_dot_visible (GcalDateChooserDay *day);
void gcal_date_chooser_day_set_dot_visible (GcalDateChooserDay *day,
gboolean dot_visible);
void gcal_date_chooser_day_set_other_month (GcalDateChooserDay *day,
gboolean other_month);
void gcal_date_chooser_day_set_selected (GcalDateChooserDay *day,
gboolean selected);
G_END_DECLS
+48
View File
@@ -0,0 +1,48 @@
<?xml version="1.0"?>
<interface>
<template class="GcalDateChooserDay" parent="GtkButton">
<property name="halign">center</property>
<property name="valign">center</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<style>
<class name="circular"/>
<class name="day"/>
<class name="flat"/>
<class name="text-button"/>
</style>
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="valign">center</property>
<child>
<object class="GtkLabel" id="label">
<property name="halign">center</property>
<property name="label">0</property>
<property name="valign">center</property>
<property name="width-chars">2</property>
<style>
<class name="day"/>
<class name="numeric"/>
</style>
</object>
</child>
<child>
<object class="GtkRevealer" id="dot_revealer">
<property name="halign">center</property>
<property name="transition-type">crossfade</property>
<property name="valign">end</property>
<property name="visible">False</property>
<property name="child">
<object class="AdwBin" id="dot">
<property name="css-name">dot</property>
<property name="halign">center</property>
<property name="valign">center</property>
</object>
</property>
</object>
</child>
</object>
</child>
</template>
</interface>
File diff suppressed because it is too large Load Diff
+63
View File
@@ -0,0 +1,63 @@
/* GTK - The GIMP Toolkit
* Copyright (C) 2015 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __GCAL_DATE_CHOOSER_H__
#define __GCAL_DATE_CHOOSER_H__
#include <adwaita.h>
G_BEGIN_DECLS
#define GCAL_TYPE_DATE_CHOOSER (gcal_date_chooser_get_type ())
G_DECLARE_FINAL_TYPE (GcalDateChooser, gcal_date_chooser, GCAL, DATE_CHOOSER, AdwBin)
GtkWidget* gcal_date_chooser_new (void);
gboolean gcal_date_chooser_get_show_heading (GcalDateChooser *self);
void gcal_date_chooser_set_show_heading (GcalDateChooser *self,
gboolean setting);
gboolean gcal_date_chooser_get_show_day_names (GcalDateChooser *self);
void gcal_date_chooser_set_show_day_names (GcalDateChooser *self,
gboolean setting);
gboolean gcal_date_chooser_get_show_week_numbers (GcalDateChooser *self);
void gcal_date_chooser_set_show_week_numbers (GcalDateChooser *self,
gboolean setting);
gboolean gcal_date_chooser_get_show_selected_week (GcalDateChooser *self);
void gcal_date_chooser_set_show_selected_week (GcalDateChooser *self,
gboolean setting);
gboolean gcal_date_chooser_get_show_events (GcalDateChooser *self);
void gcal_date_chooser_set_show_events (GcalDateChooser *self,
gboolean setting);
gboolean gcal_date_chooser_get_split_month_year (GcalDateChooser *self);
void gcal_date_chooser_set_split_month_year (GcalDateChooser *self,
gboolean setting);
G_END_DECLS
#endif /* __GCAL_DATE_CHOOSER_H__ */
+114
View File
@@ -0,0 +1,114 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.16"/>
<template class="GcalDateChooser" parent="AdwBin">
<child>
<object class="GtkGrid">
<property name="orientation">vertical</property>
<child>
<object class="GcalMultiChoice" id="month_choice">
<property name="halign">fill</property>
<property name="min-value">0</property>
<property name="max-value">11</property>
<property name="animate">False</property>
<property name="value">0</property>
<binding name="visible">
<closure type="gboolean" function="get_split_choice_visible">
<lookup name="show-heading">GcalDateChooser</lookup>
<lookup name="split-month-year">GcalDateChooser</lookup>
</closure>
</binding>
<signal name="notify::value" handler="split_multi_choice_changed" swapped="yes"/>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GcalMultiChoice" id="year_choice">
<binding name="visible">
<closure type="gboolean" function="get_split_choice_visible">
<lookup name="show-heading">GcalDateChooser</lookup>
<lookup name="split-month-year">GcalDateChooser</lookup>
</closure>
</binding>
<property name="halign">fill</property>
<property name="min-value">1</property>
<property name="max-value">9999</property>
<property name="animate">False</property>
<property name="value">0</property>
<signal name="notify::value" handler="split_multi_choice_changed" swapped="yes"/>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GcalMultiChoice" id="combined_choice">
<binding name="visible">
<closure type="gboolean" function="get_combined_choice_visible">
<lookup name="show-heading">GcalDateChooser</lookup>
<lookup name="split-month-year">GcalDateChooser</lookup>
</closure>
</binding>
<property name="halign">fill</property>
<!-- From January 1 (month 12) to December 9999 (month 119999) -->
<property name="min-value">12</property>
<property name="max-value">119999</property>
<property name="popover">popover</property>
<property name="animate">False</property>
<property name="value">0</property>
<signal name="notify::value" handler="combined_multi_choice_changed" swapped="yes"/>
<layout>
<property name="column">0</property>
<property name="column-span">2</property>
<property name="row">0</property>
</layout>
</object>
</child>
<child>
<object class="GtkGrid" id="grid">
<property name="row-homogeneous">True</property>
<property name="column-homogeneous">True</property>
<layout>
<property name="column">0</property>
<property name="column-span">2</property>
<property name="row">1</property>
</layout>
</object>
</child>
</object>
</child>
</template>
<object class="GtkPopover" id="popover">
<child>
<object class="GtkBox">
<property name="orientation">vertical</property>
<property name="spacing">12</property>
<child>
<object class="GcalMultiChoice" id="popover_month_choice">
<property name="min-value" bind-source="month_choice" bind-property="min-value" bind-flags="sync-create|bidirectional" />
<property name="max-value" bind-source="month_choice" bind-property="max-value" bind-flags="sync-create|bidirectional" />
<property name="animate" bind-source="month_choice" bind-property="animate" bind-flags="sync-create|bidirectional" />
<property name="value" bind-source="month_choice" bind-property="value" bind-flags="sync-create|bidirectional" />
</object>
</child>
<child>
<object class="GcalMultiChoice">
<property name="min-value" bind-source="year_choice" bind-property="min-value" bind-flags="sync-create|bidirectional" />
<property name="max-value" bind-source="year_choice" bind-property="max-value" bind-flags="sync-create|bidirectional" />
<property name="animate" bind-source="year_choice" bind-property="animate" bind-flags="sync-create|bidirectional" />
<property name="value" bind-source="year_choice" bind-property="value" bind-flags="sync-create|bidirectional" />
</object>
</child>
<style>
<class name="activatable"/>
<class name="month"/>
</style>
</object>
</child>
</object>
</interface>
+2
View File
@@ -9,4 +9,6 @@ built_sources += gnome.compile_resources(
sources += files(
'gcal-calendar-combo-row-item.c',
'gcal-calendar-combo-row.c',
'gcal-date-chooser.c',
'gcal-date-chooser-day.c',
)