event-editor/date-chooser: Port to GTK4

This commit is contained in:
Georges Basile Stavracas Neto
2022-02-11 09:18:38 -03:00
parent e138a35a6b
commit 9b56010b12
3 changed files with 44 additions and 56 deletions
+28 -38
View File
@@ -30,7 +30,7 @@
struct _GcalDateChooser
{
GtkBin parent;
AdwBin parent;
GtkWidget *month_choice;
GtkWidget *year_choice;
@@ -57,7 +57,7 @@ struct _GcalDateChooser
GDestroyNotify day_options_destroy;
};
G_DEFINE_TYPE (GcalDateChooser, gcal_date_chooser, GTK_TYPE_BIN)
G_DEFINE_TYPE (GcalDateChooser, gcal_date_chooser, ADW_TYPE_BIN)
enum
{
@@ -410,49 +410,38 @@ multi_choice_changed (GcalDateChooser *self)
gcal_date_chooser_set_date (self, date);
}
static void
calendar_drag_data_received (GtkWidget *widget,
GdkDragContext *context,
gint x,
gint y,
GtkSelectionData *selection_data,
guint info,
guint time)
static gboolean
on_drop_target_drop_cb (GtkDropTarget *target,
const GValue *value,
double x,
double y,
GcalDateChooser *self)
{
GcalDateChooser *self = GCAL_DATE_CHOOSER (widget);
gchar *text;
GDate *gdate;
g_autoptr (GDateTime) date = NULL;
g_autoptr (GDate) gdate = NULL;
const gchar *text;
gint year, month, day;
GDateTime *date;
gdate = g_date_new ();
text = (gchar *)gtk_selection_data_get_text (selection_data);
text = g_value_get_string (value);
if (text)
{
g_date_set_parse (gdate, text);
g_free (text);
}
g_date_set_parse (gdate, text);
if (!g_date_valid (gdate))
{
g_date_free (gdate);
gtk_drag_finish (context, FALSE, FALSE, time);
return;
}
return FALSE;
year = g_date_get_year (gdate);
month = g_date_get_month (gdate);
day = g_date_get_day (gdate);
g_date_free (gdate);
gtk_drag_finish (context, TRUE, FALSE, time);
if (!self->show_heading || self->no_month_change)
g_date_time_get_ymd (self->date, &year, &month, NULL);
date = g_date_time_new_local (year, month, day, 1, 1, 1);
gcal_date_chooser_set_date (self, date);
g_date_time_unref (date);
return TRUE;
}
static void
@@ -483,8 +472,6 @@ gcal_date_chooser_class_init (GcalDateChooserClass *class)
object_class->set_property = calendar_set_property;
object_class->get_property = calendar_get_property;
widget_class->drag_data_received = calendar_drag_data_received;
gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/calendar/ui/event-editor/gcal-date-chooser.ui");
properties[PROP_DATE] = g_param_spec_boxed ("date",
@@ -548,6 +535,7 @@ gcal_date_chooser_class_init (GcalDateChooserClass *class)
static void
gcal_date_chooser_init (GcalDateChooser *self)
{
GtkDropTarget *drop_target;
GtkWidget *label;
gint row, col;
gint year, month;
@@ -599,14 +587,14 @@ gcal_date_chooser_init (GcalDateChooser *self)
*/
self->corner = gtk_stack_new ();
gtk_grid_attach (GTK_GRID (self->grid), self->corner, -1, -1, 1, 1);
label = gtk_label_new ("");
gtk_widget_show (label);
gtk_style_context_add_class (gtk_widget_get_style_context (label), "weekday");
gtk_container_add (GTK_CONTAINER (self->corner), label);
gtk_widget_add_css_class (label, "weekday");
gtk_stack_add_named (GTK_STACK (self->corner), label, "weekday");
label = gtk_label_new ("99");
gtk_style_context_add_class (gtk_widget_get_style_context (label), "weeknum");
gtk_container_add (GTK_CONTAINER (self->corner), label);
gtk_widget_add_css_class (label, "weeknum");
gtk_stack_add_named (GTK_STACK (self->corner), label, "weeknum");
self->day_grid = g_object_new (GTK_TYPE_GRID,
"valign", GTK_ALIGN_FILL,
@@ -643,8 +631,10 @@ gcal_date_chooser_init (GcalDateChooser *self)
gcal_multi_choice_set_value (GCAL_MULTI_CHOICE (self->month_choice), month - 1);
calendar_update_selected_day_display (self);
gtk_drag_dest_set (GTK_WIDGET (self), GTK_DEST_DEFAULT_ALL, NULL, 0, GDK_ACTION_COPY);
gtk_drag_dest_add_text_targets (GTK_WIDGET (self));
drop_target = gtk_drop_target_new (G_TYPE_STRING, GDK_ACTION_COPY);
gtk_drop_target_set_preload (drop_target, TRUE);
g_signal_connect (drop_target, "drop", G_CALLBACK (on_drop_target_drop_cb), NULL);
gtk_widget_add_controller (GTK_WIDGET (self), GTK_EVENT_CONTROLLER (drop_target));
}
GtkWidget*
+2 -2
View File
@@ -18,7 +18,7 @@
#ifndef __GCAL_DATE_CHOOSER_H__
#define __GCAL_DATE_CHOOSER_H__
#include <gtk/gtk.h>
#include <adwaita.h>
G_BEGIN_DECLS
@@ -32,7 +32,7 @@ typedef enum
#define GCAL_TYPE_DATE_CHOOSER (gcal_date_chooser_get_type ())
G_DECLARE_FINAL_TYPE (GcalDateChooser, gcal_date_chooser, GCAL, DATE_CHOOSER, GtkBin)
G_DECLARE_FINAL_TYPE (GcalDateChooser, gcal_date_chooser, GCAL, DATE_CHOOSER, AdwBin)
typedef GcalDateChooserDayOptions (*GcalDateChooserDayOptionsCallback) (GcalDateChooser *self,
GDateTime *date,
+14 -16
View File
@@ -1,10 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<requires lib="gtk+" version="3.16"/>
<template class="GcalDateChooser" parent="GtkBin">
<template class="GcalDateChooser" parent="AdwBin">
<child>
<object class="GtkGrid">
<property name="visible">True</property>
<property name="orientation">vertical</property>
<child>
<object class="GcalMultiChoice" id="month_choice">
@@ -15,11 +14,11 @@
<property name="animate">False</property>
<property name="value">0</property>
<signal name="notify::value" handler="multi_choice_changed" swapped="yes"/>
<layout>
<property name="column">0</property>
<property name="row">0</property>
</layout>
</object>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GcalMultiChoice" id="year_choice">
@@ -30,23 +29,22 @@
<property name="animate">False</property>
<property name="value">0</property>
<signal name="notify::value" handler="multi_choice_changed" swapped="yes"/>
<layout>
<property name="column">1</property>
<property name="row">0</property>
</layout>
</object>
<packing>
<property name="left-attach">1</property>
<property name="top-attach">0</property>
</packing>
</child>
<child>
<object class="GtkGrid" id="grid">
<property name="visible">True</property>
<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>
<packing>
<property name="left-attach">0</property>
<property name="top-attach">1</property>
<property name="width">2</property>
</packing>
</child>
</object>
</child>