event-editor/summary: Port to GTK4

This commit is contained in:
Georges Basile Stavracas Neto
2022-02-11 09:18:38 -03:00
parent 333b8ecc2a
commit b5dc363a3b
6 changed files with 41 additions and 281 deletions
+33 -12
View File
@@ -23,17 +23,16 @@
#include "gcal-context.h"
#include "gcal-debug.h"
#include "gcal-event-editor-section.h"
#include "gcal-expandable-entry.h"
#include "gcal-summary-section.h"
#include <glib/gi18n.h>
struct _GcalSummarySection
{
GtkBin parent;
AdwBin parent;
GtkEntry *summary_entry;
GtkEntry *location_entry;
GtkEditable *summary_entry;
GtkEditable *location_entry;
GcalContext *context;
GcalEvent *event;
@@ -42,7 +41,7 @@ struct _GcalSummarySection
static void gcal_event_editor_section_iface_init (GcalEventEditorSectionInterface *iface);
G_DEFINE_TYPE_WITH_CODE (GcalSummarySection, gcal_summary_section, GTK_TYPE_BIN,
G_DEFINE_TYPE_WITH_CODE (GcalSummarySection, gcal_summary_section, ADW_TYPE_BIN,
G_IMPLEMENT_INTERFACE (GCAL_TYPE_EVENT_EDITOR_SECTION, gcal_event_editor_section_iface_init))
enum
@@ -52,6 +51,27 @@ enum
N_PROPS
};
/*
* Auxiliary methods
*/
static void
make_entry_expandable (GtkEditable *entry)
{
GtkWidget *child;
for (child = gtk_widget_get_first_child (GTK_WIDGET (entry));
child;
child = gtk_widget_get_next_sibling (child))
{
if (!GTK_IS_TEXT (child))
continue;
gtk_text_set_propagate_text_width (GTK_TEXT (child), TRUE);
}
}
/*
* GcalEventEditorSection interface
*/
@@ -76,11 +96,11 @@ gcal_reminders_section_set_event (GcalEventEditorSection *section,
summary = gcal_event_get_summary (event);
if (g_strcmp0 (summary, "") == 0)
gtk_entry_set_text (GTK_ENTRY (self->summary_entry), _("Unnamed event"));
gtk_editable_set_text (self->summary_entry, _("Unnamed event"));
else
gtk_entry_set_text (GTK_ENTRY (self->summary_entry), summary);
gtk_editable_set_text (self->summary_entry, summary);
gtk_entry_set_text (self->location_entry, gcal_event_get_location (event));
gtk_editable_set_text (self->location_entry, gcal_event_get_location (event));
GCAL_EXIT;
}
@@ -94,8 +114,8 @@ gcal_reminders_section_apply (GcalEventEditorSection *section)
self = GCAL_SUMMARY_SECTION (section);
gcal_event_set_summary (self->event, gtk_entry_get_text (self->summary_entry));
gcal_event_set_location (self->event, gtk_entry_get_text (self->location_entry));
gcal_event_set_summary (self->event, gtk_editable_get_text (self->summary_entry));
gcal_event_set_location (self->event, gtk_editable_get_text (self->location_entry));
GCAL_EXIT;
}
@@ -168,8 +188,6 @@ gcal_summary_section_class_init (GcalSummarySectionClass *klass)
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
g_type_ensure (GCAL_TYPE_EXPANDABLE_ENTRY);
object_class->finalize = gcal_summary_section_finalize;
object_class->get_property = gcal_summary_section_get_property;
object_class->set_property = gcal_summary_section_set_property;
@@ -186,4 +204,7 @@ static void
gcal_summary_section_init (GcalSummarySection *self)
{
gtk_widget_init_template (GTK_WIDGET (self));
make_entry_expandable (self->location_entry);
make_entry_expandable (self->summary_entry);
}
+2 -2
View File
@@ -20,11 +20,11 @@
#pragma once
#include <gtk/gtk.h>
#include <adwaita.h>
G_BEGIN_DECLS
#define GCAL_TYPE_SUMMARY_SECTION (gcal_summary_section_get_type())
G_DECLARE_FINAL_TYPE (GcalSummarySection, gcal_summary_section, GCAL, SUMMARY_SECTION, GtkBin)
G_DECLARE_FINAL_TYPE (GcalSummarySection, gcal_summary_section, GCAL, SUMMARY_SECTION, AdwBin)
G_END_DECLS
+6 -16
View File
@@ -1,34 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="GcalSummarySection" parent="GtkBin">
<property name="can_focus">False</property>
<template class="GcalSummarySection" parent="AdwBin">
<child>
<object class="GtkListBox">
<property name="visible">True</property>
<property name="selection-mode">none</property>
<style>
<class name="content" />
<class name="boxed-list" />
</style>
<!-- Summary -->
<child>
<object class="HdyActionRow">
<property name="visible">True</property>
<object class="AdwActionRow">
<property name="title" translatable="yes">Title</property>
<property name="activatable-widget">summary_entry</property>
<child>
<object class="GcalExpandableEntry" id="summary_entry">
<property name="visible">True</property>
<object class="GtkEntry" id="summary_entry">
<property name="hexpand">True</property>
<property name="halign">end</property>
<property name="valign">center</property>
<property name="can_focus">True</property>
<property name="width-chars">20</property>
<property name="max-width-chars">30</property>
<property name="propagate-natural-width">True</property>
</object>
</child>
@@ -37,22 +31,18 @@
<!-- Location -->
<child>
<object class="HdyActionRow">
<property name="visible">True</property>
<object class="AdwActionRow">
<property name="title" translatable="yes">Location</property>
<property name="activatable-widget">location_entry</property>
<child>
<object class="GcalExpandableEntry" id="location_entry">
<property name="visible">True</property>
<object class="GtkEntry" id="location_entry">
<property name="hexpand">True</property>
<property name="halign">end</property>
<property name="valign">center</property>
<property name="can_focus">True</property>
<property name="width-chars">20</property>
<property name="max-width-chars">30</property>
<property name="secondary_icon_name">find-location-symbolic</property>
<property name="propagate-natural-width">True</property>
</object>
</child>
-215
View File
@@ -1,215 +0,0 @@
/* gcal-expandable-entry.c
*
* Copyright (C) 2017 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* 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 "gcal-expandable-entry.h"
struct _GcalExpandableEntry
{
GtkEntry parent;
gboolean propagate_natural_width;
};
G_DEFINE_TYPE (GcalExpandableEntry, gcal_expandable_entry, GTK_TYPE_ENTRY)
enum
{
PROP_0,
PROP_PROPAGATE_NATURAL_WIDTH,
N_PROPS
};
static GParamSpec *properties[N_PROPS] = { NULL, };
/*
* GtkWidget overrides
*/
static gboolean
gcal_expandable_entry_enter_notify_event (GtkWidget *widget,
GdkEventCrossing *event_crossing)
{
gtk_widget_set_state_flags (widget, GTK_STATE_FLAG_PRELIGHT, FALSE);
gtk_widget_queue_draw (widget);
return GDK_EVENT_PROPAGATE;
}
static void
gcal_expandable_entry_get_preferred_width (GtkWidget *widget,
gint *minimum_width,
gint *natural_width)
{
GcalExpandableEntry *self;
PangoLayout *layout;
gint local_minimum;
gint local_natural;
self = GCAL_EXPANDABLE_ENTRY (widget);
GTK_WIDGET_CLASS (gcal_expandable_entry_parent_class)->get_preferred_width (widget,
&local_minimum,
&local_natural);
if (self->propagate_natural_width)
{
GtkStyleContext *context;
GdkRectangle primary_icon_rect;
GdkRectangle secondary_icon_rect;
GtkBorder padding;
GtkBorder border;
GtkBorder margin;
gint text_width;
context = gtk_widget_get_style_context (widget);
gtk_style_context_get_border (context, gtk_style_context_get_state (context), &border);
gtk_style_context_get_margin (context, gtk_style_context_get_state (context), &margin);
gtk_style_context_get_padding (context, gtk_style_context_get_state (context), &padding);
gtk_entry_get_icon_area (GTK_ENTRY (self), GTK_ENTRY_ICON_PRIMARY, &primary_icon_rect);
gtk_entry_get_icon_area (GTK_ENTRY (self), GTK_ENTRY_ICON_SECONDARY, &secondary_icon_rect);
layout = gtk_entry_get_layout (GTK_ENTRY (widget));
pango_layout_get_pixel_size (layout, &text_width, NULL);
local_natural = MAX (local_minimum, text_width + primary_icon_rect.width + secondary_icon_rect.width);
local_natural += margin.left + margin.right;
local_natural += border.left + border.right;
local_natural += padding.left + padding.right;
}
if (minimum_width)
*minimum_width = local_minimum;
if (natural_width)
*natural_width = local_natural;
}
static gboolean
gcal_expandable_entry_leave_notify_event (GtkWidget *widget,
GdkEventCrossing *event_crossing)
{
gtk_widget_unset_state_flags (widget, GTK_STATE_FLAG_PRELIGHT);
gtk_widget_queue_draw (widget);
return GDK_EVENT_PROPAGATE;
}
/*
* GObject overrides
*/
static void
gcal_expandable_entry_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GcalExpandableEntry *self = GCAL_EXPANDABLE_ENTRY (object);
switch (prop_id)
{
case PROP_PROPAGATE_NATURAL_WIDTH:
g_value_set_boolean (value, self->propagate_natural_width);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_expandable_entry_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GcalExpandableEntry *self = GCAL_EXPANDABLE_ENTRY (object);
switch (prop_id)
{
case PROP_PROPAGATE_NATURAL_WIDTH:
gcal_expandable_entry_set_propagate_natural_width (self, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
gcal_expandable_entry_class_init (GcalExpandableEntryClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
object_class->get_property = gcal_expandable_entry_get_property;
object_class->set_property = gcal_expandable_entry_set_property;
widget_class->enter_notify_event = gcal_expandable_entry_enter_notify_event;
widget_class->get_preferred_width = gcal_expandable_entry_get_preferred_width;
widget_class->leave_notify_event = gcal_expandable_entry_leave_notify_event;
properties[PROP_PROPAGATE_NATURAL_WIDTH] = g_param_spec_boolean ("propagate-natural-width",
"Propagate natural width",
"Propagate natural width",
FALSE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (object_class, N_PROPS, properties);
}
static void
gcal_expandable_entry_init (GcalExpandableEntry *self)
{
self->propagate_natural_width = FALSE;
g_signal_connect_object (self,
"notify::text",
G_CALLBACK (gtk_widget_queue_resize),
self,
G_CONNECT_SWAPPED);
}
GtkWidget*
gcal_expandable_entry_new (void)
{
return g_object_new (GCAL_TYPE_EXPANDABLE_ENTRY, NULL);
}
gboolean
gcal_expandable_entry_get_propagate_natural_width (GcalExpandableEntry *self)
{
g_return_val_if_fail (GCAL_IS_EXPANDABLE_ENTRY (self), FALSE);
return self->propagate_natural_width;
}
void
gcal_expandable_entry_set_propagate_natural_width (GcalExpandableEntry *self,
gboolean propagate_natural_width)
{
g_return_if_fail (GCAL_IS_EXPANDABLE_ENTRY (self));
if (self->propagate_natural_width == propagate_natural_width)
return;
self->propagate_natural_width = propagate_natural_width;
gtk_widget_queue_resize (GTK_WIDGET (self));
}
-35
View File
@@ -1,35 +0,0 @@
/* gcal-expandable-entry.h
*
* Copyright (C) 2017 Georges Basile Stavracas Neto <georges.stavracas@gmail.com>
*
* 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/>.
*/
#pragma once
#include <gtk/gtk.h>
G_BEGIN_DECLS
#define GCAL_TYPE_EXPANDABLE_ENTRY (gcal_expandable_entry_get_type())
G_DECLARE_FINAL_TYPE (GcalExpandableEntry, gcal_expandable_entry, GCAL, EXPANDABLE_ENTRY, GtkEntry)
GtkWidget* gcal_expandable_entry_new (void);
gboolean gcal_expandable_entry_get_propagate_natural_width (GcalExpandableEntry *self);
void gcal_expandable_entry_set_propagate_natural_width (GcalExpandableEntry *self,
gboolean propagate_natural_width);
G_END_DECLS
-1
View File
@@ -18,7 +18,6 @@ sources += files(
'gcal-calendar-popover.c',
'gcal-event-popover.c',
'gcal-event-widget.c',
'gcal-expandable-entry.c',
'gcal-meeting-row.c',
'gcal-quick-add-popover.c',
'gcal-search-button.c',