[gnome-calendar/gbsneto/gtk4: 4/37] date-chooser-day: Port to GTK4




commit 0c273cfe3ecc1dac85ce4150213babadf605f945
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Mon Jan 10 15:55:25 2022 -0300

    date-chooser-day: Port to GTK4
    
    Subclass AdwBin. Remove unnecessary GtkWidget overrides.

 src/gui/event-editor/gcal-date-chooser-day.c | 358 +++------------------------
 src/gui/event-editor/gcal-date-chooser-day.h |  11 +-
 2 files changed, 34 insertions(+), 335 deletions(-)
---
diff --git a/src/gui/event-editor/gcal-date-chooser-day.c b/src/gui/event-editor/gcal-date-chooser-day.c
index 6f819c06..58c6dd1b 100644
--- a/src/gui/event-editor/gcal-date-chooser-day.c
+++ b/src/gui/event-editor/gcal-date-chooser-day.c
@@ -16,10 +16,10 @@
  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include "config.h"
-
 #define G_LOG_DOMAIN "GcalDateChooserDay"
 
+#include "config.h"
+
 #include "gcal-date-chooser-day.h"
 
 #include <stdlib.h>
@@ -34,54 +34,23 @@ static guint signals[LAST_DAY_SIGNAL] = { 0, };
 
 struct _GcalDateChooserDay
 {
-  GtkBin              parent;
+  AdwBin              parent;
 
   GtkWidget          *label;
   GDateTime          *date;
-  GdkWindow          *event_window;
-  GtkGesture         *multipress_gesture;
 };
 
-G_DEFINE_TYPE (GcalDateChooserDay, gcal_date_chooser_day, GTK_TYPE_BIN)
+G_DEFINE_TYPE (GcalDateChooserDay, gcal_date_chooser_day, ADW_TYPE_BIN)
 
 static void
-day_pressed (GtkGestureMultiPress *gesture,
-             gint                  n_press,
-             gdouble               x,
-             gdouble               y,
-             GcalDateChooserDay   *self)
-{
-  gint button;
-
-  button = gtk_gesture_single_get_current_button (GTK_GESTURE_SINGLE (gesture));
-
-  if (button == GDK_BUTTON_PRIMARY)
-    {
-      if (n_press == 1)
-        g_signal_emit (self, signals[SELECTED], 0);
-    }
-}
-
-static gboolean
-gcal_date_chooser_day_key_press (GtkWidget   *widget,
-                                 GdkEventKey *event)
+day_pressed (GtkGestureClick    *click_gesture,
+             gint                n_press,
+             gdouble             x,
+             gdouble             y,
+             GcalDateChooserDay *self)
 {
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-
-  if (event->keyval == GDK_KEY_space ||
-      event->keyval == GDK_KEY_Return ||
-      event->keyval == GDK_KEY_ISO_Enter||
-      event->keyval == GDK_KEY_KP_Enter ||
-      event->keyval == GDK_KEY_KP_Space)
-    {
-      g_signal_emit (self, signals[SELECTED], 0);
-      return TRUE;
-    }
-
-  if (GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->key_press_event (widget, event))
-    return TRUE;
-
- return FALSE;
+  if (n_press == 1)
+    g_signal_emit (self, signals[SELECTED], 0);
 }
 
 static void
@@ -89,253 +58,11 @@ gcal_date_chooser_day_dispose (GObject *object)
 {
   GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (object);
 
-  g_clear_object (&self->multipress_gesture);
   g_clear_pointer (&self->date, g_date_time_unref);
 
   G_OBJECT_CLASS (gcal_date_chooser_day_parent_class)->dispose (object);
 }
 
-static gboolean
-gcal_date_chooser_day_draw (GtkWidget *widget,
-                            cairo_t   *cr)
-{
-  GtkStyleContext *context;
-  GtkStateFlags state;
-  gint x, y, width, height;
-
-  context = gtk_widget_get_style_context (widget);
-  state = gtk_style_context_get_state (context);
-
-  x = 0;
-  y = 0;
-  width = gtk_widget_get_allocated_width (widget);
-  height = gtk_widget_get_allocated_height (widget);
-
-  gtk_render_background (context, cr, x, y, width, height);
-  gtk_render_frame (context, cr, x, y, width, height);
-
-  GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->draw (widget, cr);
-
-  if (gtk_widget_has_visible_focus (widget))
-    {
-      GtkBorder border;
-
-      gtk_style_context_get_border (context, state, &border);
-      gtk_render_focus (context, cr, border.left, border.top,
-                        gtk_widget_get_allocated_width (widget) - border.left - border.right,
-                        gtk_widget_get_allocated_height (widget) - border.top - border.bottom);
-    }
-
-  return FALSE;
-}
-
-static void
-gcal_date_chooser_day_map (GtkWidget *widget)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-
-  GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->map (widget);
-
-  gdk_window_show (self->event_window);
-}
-
-static void
-gcal_date_chooser_day_unmap (GtkWidget *widget)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-
-  gdk_window_hide (self->event_window);
-
-  GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->unmap (widget);
-}
-
-static void
-gcal_date_chooser_day_realize (GtkWidget *widget)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-  GtkAllocation allocation;
-  GdkWindow *window;
-  GdkWindowAttr attributes;
-  gint attributes_mask;
-
-  gtk_widget_get_allocation (widget, &allocation);
-  gtk_widget_set_realized (widget, TRUE);
-
-  attributes.window_type = GDK_WINDOW_CHILD;
-  attributes.x = allocation.x;
-  attributes.y = allocation.y;
-  attributes.width = allocation.width;
-  attributes.height = allocation.height;
-  attributes.wclass = GDK_INPUT_ONLY;
-  attributes.event_mask = gtk_widget_get_events (widget);
-  attributes.event_mask |= GDK_BUTTON_PRESS_MASK
-                           | GDK_BUTTON_RELEASE_MASK
-                           | GDK_TOUCH_MASK
-                           | GDK_ENTER_NOTIFY_MASK
-                           | GDK_LEAVE_NOTIFY_MASK;
-
-  attributes_mask = GDK_WA_X | GDK_WA_Y;
-
-  window = gtk_widget_get_parent_window (widget);
-  gtk_widget_set_window (widget, window);
-  g_object_ref (window);
-
-  self->event_window = gdk_window_new (window, &attributes, attributes_mask);
-  gtk_widget_register_window (widget, self->event_window);
-}
-
-static void
-gcal_date_chooser_day_unrealize (GtkWidget *widget)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-
-  if (self->event_window)
-    {
-      gtk_widget_unregister_window (widget, self->event_window);
-      gdk_window_destroy (self->event_window);
-      self->event_window = NULL;
-    }
-
-  GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->unrealize (widget);
-}
-
-static void
-gcal_date_chooser_day_size_allocate (GtkWidget     *widget,
-                                     GtkAllocation *allocation)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-
-  GTK_WIDGET_CLASS (gcal_date_chooser_day_parent_class)->size_allocate (widget, allocation);
-
-  if (gtk_widget_get_realized (widget))
-    {
-      gdk_window_move_resize (self->event_window,
-                              allocation->x,
-                              allocation->y,
-                              allocation->width,
-                              allocation->height);
-    }
-}
-
-static void
-gcal_date_chooser_day_drag_data_get (GtkWidget        *widget,
-                                     GdkDragContext   *context,
-                                     GtkSelectionData *selection_data,
-                                     guint             info,
-                                     guint             time)
-{
-  GcalDateChooserDay *self = GCAL_DATE_CHOOSER_DAY (widget);
-  gchar *text;
-
-  text = g_date_time_format (self->date, "%x");
-  gtk_selection_data_set_text (selection_data, text, -1);
-  g_free (text);
-}
-
-static void
-gcal_date_chooser_day_get_preferred_width (GtkWidget *widget,
-                                           gint      *minimum,
-                                           gint      *natural)
-{
-  GcalDateChooserDay *self;
-  GtkStyleContext *context;
-  GtkStateFlags flags;
-  GtkBorder border, margin, padding;
-  gint label_min, label_nat;
-  gint min_width, min, nat;
-
-  self = GCAL_DATE_CHOOSER_DAY (widget);
-  context = gtk_widget_get_style_context (widget);
-  flags = gtk_style_context_get_state (context);
-
-  gtk_style_context_get_border (context, flags, &border);
-  gtk_style_context_get_margin (context, flags, &margin);
-  gtk_style_context_get_padding (context, flags, &padding);
-
-  gtk_style_context_get (context,
-                         flags,
-                         "min-width", &min_width,
-                         NULL);
-
-  gtk_widget_get_preferred_width (self->label, &label_min, &label_nat);
-
-  min = label_min + margin.left + border.left + padding.left + margin.right + border.right + padding.right;
-  nat = label_nat + margin.left + border.left + padding.left + margin.right + border.right + padding.right;
-
-  if (minimum)
-    *minimum = MAX (min, min_width);
-
-  if (natural)
-    *natural = MAX (nat, min_width);
-}
-
-static void
-gcal_date_chooser_day_get_preferred_height (GtkWidget *widget,
-                                            gint      *minimum,
-                                            gint      *natural)
-{
-  GcalDateChooserDay *self;
-  GtkStyleContext *context;
-  GtkStateFlags flags;
-  GtkBorder border, margin, padding;
-  gint label_min, label_nat;
-  gint min_height, min, nat;
-
-  self = GCAL_DATE_CHOOSER_DAY (widget);
-  context = gtk_widget_get_style_context (widget);
-  flags = gtk_style_context_get_state (context);
-
-  gtk_style_context_get_border (context, flags, &border);
-  gtk_style_context_get_margin (context, flags, &margin);
-  gtk_style_context_get_padding (context, flags, &padding);
-
-  gtk_style_context_get (context,
-                         flags,
-                         "min-height", &min_height,
-                         NULL);
-
-  gtk_widget_get_preferred_height (self->label, &label_min, &label_nat);
-
-  min = label_min + margin.top + border.top + padding.top + margin.bottom + border.bottom + padding.bottom;
-  nat = label_nat + margin.top + border.top + padding.top + margin.bottom + border.bottom + padding.bottom;
-
-  if (minimum)
-    *minimum = MAX (min, min_height);
-
-  if (natural)
-    *natural = MAX (nat, min_height);
-}
-
-static gboolean
-gcal_date_chooser_day_enter_notify_event (GtkWidget        *widget,
-                                          GdkEventCrossing *event)
-{
-  GtkStyleContext *context;
-  GtkStateFlags state;
-
-  context = gtk_widget_get_style_context (widget);
-  state = gtk_style_context_get_state (context);
-
-  gtk_style_context_set_state (context, state | GTK_STATE_FLAG_PRELIGHT);
-
-  return GDK_EVENT_PROPAGATE;
-}
-
-static gboolean
-gcal_date_chooser_day_leave_notify_event (GtkWidget        *widget,
-                                          GdkEventCrossing *event)
-{
-  GtkStyleContext *context;
-  GtkStateFlags state;
-
-  context = gtk_widget_get_style_context (widget);
-  state = gtk_style_context_get_state (context);
-
-  gtk_style_context_set_state (context, state & ~GTK_STATE_FLAG_PRELIGHT);
-
-  return GDK_EVENT_PROPAGATE;
-}
-
 static void
 gcal_date_chooser_day_class_init (GcalDateChooserDayClass *class)
 {
@@ -344,19 +71,6 @@ gcal_date_chooser_day_class_init (GcalDateChooserDayClass *class)
 
   object_class->dispose = gcal_date_chooser_day_dispose;
 
-  widget_class->draw = gcal_date_chooser_day_draw;
-  widget_class->realize = gcal_date_chooser_day_realize;
-  widget_class->unrealize = gcal_date_chooser_day_unrealize;
-  widget_class->map = gcal_date_chooser_day_map;
-  widget_class->unmap = gcal_date_chooser_day_unmap;
-  widget_class->key_press_event = gcal_date_chooser_day_key_press;
-  widget_class->size_allocate = gcal_date_chooser_day_size_allocate;
-  widget_class->drag_data_get = gcal_date_chooser_day_drag_data_get;
-  widget_class->get_preferred_width = gcal_date_chooser_day_get_preferred_width;
-  widget_class->get_preferred_height = gcal_date_chooser_day_get_preferred_height;
-  widget_class->enter_notify_event = gcal_date_chooser_day_enter_notify_event;
-  widget_class->leave_notify_event = gcal_date_chooser_day_leave_notify_event;
-
   signals[SELECTED] = g_signal_new ("selected",
                                     GCAL_TYPE_DATE_CHOOSER_DAY,
                                     G_SIGNAL_RUN_FIRST,
@@ -367,11 +81,18 @@ gcal_date_chooser_day_class_init (GcalDateChooserDayClass *class)
                                     G_TYPE_NONE, 0);
 
   gtk_widget_class_set_css_name (widget_class, "day");
+
+  gtk_widget_class_add_binding_signal (widget_class, GDK_KEY_space, 0, "selected", NULL);
+  gtk_widget_class_add_binding_signal (widget_class, GDK_KEY_Return, 0, "selected", NULL);
+  gtk_widget_class_add_binding_signal (widget_class, GDK_KEY_ISO_Enter, 0, "selected", NULL);
+  gtk_widget_class_add_binding_signal (widget_class, GDK_KEY_KP_Enter, 0, "selected", NULL);
+  gtk_widget_class_add_binding_signal (widget_class, GDK_KEY_KP_Space, 0, "selected", NULL);
 }
 
 static void
 gcal_date_chooser_day_init (GcalDateChooserDay *self)
 {
+  GtkGesture *click_gesture;
   GtkWidget *widget = GTK_WIDGET (self);
 
   gtk_widget_set_halign (widget, GTK_ALIGN_CENTER);
@@ -387,12 +108,13 @@ gcal_date_chooser_day_init (GcalDateChooserDay *self)
   gtk_widget_set_hexpand (self->label, TRUE);
   gtk_widget_set_vexpand (self->label, TRUE);
 
-  gtk_container_add (GTK_CONTAINER (self), self->label);
+  adw_bin_set_child (ADW_BIN (self), self->label);
 
-  self->multipress_gesture = gtk_gesture_multi_press_new (widget);
-  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (self->multipress_gesture), 0);
+  click_gesture = gtk_gesture_click_new ();
+  gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (click_gesture), 0);
+  gtk_widget_add_controller (widget, GTK_EVENT_CONTROLLER (click_gesture));
 
-  g_signal_connect_object (self->multipress_gesture,
+  g_signal_connect_object (click_gesture,
                            "pressed",
                            G_CALLBACK (day_pressed),
                            self,
@@ -429,24 +151,10 @@ void
 gcal_date_chooser_day_set_other_month (GcalDateChooserDay *self,
                                        gboolean            other_month)
 {
-  GtkStyleContext *context;
-
-  context = gtk_widget_get_style_context (GTK_WIDGET (self));
-
   if (other_month)
-    {
-      gtk_style_context_add_class (context, "other-month");
-      gtk_drag_source_unset (GTK_WIDGET (self));
-    }
+    gtk_widget_add_css_class (GTK_WIDGET (self), "other-month");
   else
-    {
-      gtk_style_context_remove_class (context, "other-month");
-      gtk_drag_source_set (GTK_WIDGET (self),
-                           GDK_BUTTON1_MASK | GDK_BUTTON3_MASK,
-                           NULL, 0,
-                           GDK_ACTION_COPY);
-      gtk_drag_source_add_text_targets (GTK_WIDGET (self));
-    }
+    gtk_widget_remove_css_class (GTK_WIDGET (self), "other-month");
 }
 
 void
@@ -463,22 +171,18 @@ void
 gcal_date_chooser_day_set_options (GcalDateChooserDay        *self,
                                    GcalDateChooserDayOptions  options)
 {
-  GtkStyleContext *context;
-
-  context = gtk_widget_get_style_context (GTK_WIDGET (self));
-
   if (options & GCAL_DATE_CHOOSER_DAY_WEEKEND)
-    gtk_style_context_add_class (context, "weekend");
+    gtk_widget_add_css_class (GTK_WIDGET (self), "weekend");
   else
-    gtk_style_context_remove_class (context, "weekend");
+    gtk_widget_remove_css_class (GTK_WIDGET (self), "weekend");
 
   if (options & GCAL_DATE_CHOOSER_DAY_HOLIDAY)
-    gtk_style_context_add_class (context, "holiday");
+    gtk_widget_add_css_class (GTK_WIDGET (self), "holiday");
   else
-    gtk_style_context_remove_class (context, "holiday");
+    gtk_widget_remove_css_class (GTK_WIDGET (self), "holiday");
 
   if (options & GCAL_DATE_CHOOSER_DAY_MARKED)
-    gtk_style_context_add_class (context, "marked");
+    gtk_widget_add_css_class (GTK_WIDGET (self), "marked");
   else
-    gtk_style_context_remove_class (context, "marked");
+    gtk_widget_remove_css_class (GTK_WIDGET (self), "marked");
 }
diff --git a/src/gui/event-editor/gcal-date-chooser-day.h b/src/gui/event-editor/gcal-date-chooser-day.h
index afb18c27..d1b39cb8 100644
--- a/src/gui/event-editor/gcal-date-chooser-day.h
+++ b/src/gui/event-editor/gcal-date-chooser-day.h
@@ -15,19 +15,16 @@
  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  */
 
-#ifndef __GCAL_DATE_CHOOSER_DAY_PRIVATE_H__
-#define __GCAL_DATE_CHOOSER_DAY_PRIVATE_H__
+#pragma once
 
 #include "gcal-date-chooser.h"
 
-#include <glib-object.h>
-#include <gtk/gtk.h>
+#include <adwaita.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, GtkBin)
+G_DECLARE_FINAL_TYPE (GcalDateChooserDay, gcal_date_chooser_day, GCAL, DATE_CHOOSER_DAY, AdwBin)
 
 GtkWidget*           gcal_date_chooser_day_new                   (void);
 
@@ -46,5 +43,3 @@ void                 gcal_date_chooser_day_set_options           (GcalDateChoose
                                                                   GcalDateChooserDayOptions options);
 
 G_END_DECLS
-
-#endif /* __GCAL_DATE_CHOOSER_DAY_PRIVATE_H__ */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]