[gnome-builder/wip/gtk4-port] libide/gtk: add IdeEntryPopover



commit 577ada8f5652754f9d4f91fe7d127838baf85732
Author: Christian Hergert <chergert redhat com>
Date:   Mon Mar 28 15:54:51 2022 -0700

    libide/gtk: add IdeEntryPopover
    
    This is a port of DzlSimplePopover which is a popover with some labels,
    an entry, and a suggested-action button.

 src/libide/gtk/ide-entry-popover.c      | 417 ++++++++++++++++++++++++++++++++
 src/libide/gtk/ide-entry-popover.h      | 101 ++++++++
 src/libide/gtk/ide-entry-popover.ui     |  58 +++++
 src/libide/gtk/libide-gtk.gresource.xml |   1 +
 src/libide/gtk/meson.build              |   2 +
 5 files changed, 579 insertions(+)
---
diff --git a/src/libide/gtk/ide-entry-popover.c b/src/libide/gtk/ide-entry-popover.c
new file mode 100644
index 000000000..c0b61299d
--- /dev/null
+++ b/src/libide/gtk/ide-entry-popover.c
@@ -0,0 +1,417 @@
+/* ide-entry-popover.c
+ *
+ * Copyright (C) 2015 Christian Hergert <christian hergert me>
+ *
+ * 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 "config.h"
+
+#include "ide-entry-popover.h"
+
+typedef struct
+{
+  GtkPopover parent_instance;
+
+  GtkLabel  *title;
+  GtkLabel  *message;
+  GtkEntry  *entry;
+  GtkButton *button;
+} IdeEntryPopoverPrivate;
+
+G_DEFINE_TYPE_WITH_PRIVATE (IdeEntryPopover, ide_entry_popover, GTK_TYPE_POPOVER)
+
+enum {
+  PROP_0,
+  PROP_BUTTON_TEXT,
+  PROP_MESSAGE,
+  PROP_READY,
+  PROP_TEXT,
+  PROP_TITLE,
+  LAST_PROP
+};
+
+enum {
+  ACTIVATE,
+  CHANGED,
+  INSERT_TEXT,
+  LAST_SIGNAL
+};
+
+static GParamSpec *properties [LAST_PROP];
+static guint signals [LAST_SIGNAL];
+
+const gchar *
+ide_entry_popover_get_button_text (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_ENTRY_POPOVER (self), NULL);
+
+  return gtk_button_get_label (priv->button);
+}
+
+void
+ide_entry_popover_set_button_text (IdeEntryPopover *self,
+                                   const gchar     *button_text)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_ENTRY_POPOVER (self));
+
+  gtk_button_set_label (priv->button, button_text);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_BUTTON_TEXT]);
+}
+
+const gchar *
+ide_entry_popover_get_message (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_ENTRY_POPOVER (self), NULL);
+
+  return gtk_label_get_text (priv->message);
+}
+
+void
+ide_entry_popover_set_message (IdeEntryPopover *self,
+                               const gchar     *message)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_ENTRY_POPOVER (self));
+
+  gtk_label_set_label (priv->message, message);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_MESSAGE]);
+}
+
+gboolean
+ide_entry_popover_get_ready (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_ENTRY_POPOVER (self), FALSE);
+
+  return gtk_widget_get_sensitive (GTK_WIDGET (priv->button));
+}
+
+void
+ide_entry_popover_set_ready (IdeEntryPopover *self,
+                             gboolean         ready)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_ENTRY_POPOVER (self));
+
+  gtk_widget_set_sensitive (GTK_WIDGET (priv->button), ready);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_READY]);
+}
+
+const gchar *
+ide_entry_popover_get_text (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_ENTRY_POPOVER (self), NULL);
+
+  return gtk_editable_get_text (GTK_EDITABLE (priv->entry));
+}
+
+void
+ide_entry_popover_set_text (IdeEntryPopover *self,
+                            const gchar     *text)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_ENTRY_POPOVER (self));
+
+  gtk_editable_set_text (GTK_EDITABLE (priv->entry), text);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TEXT]);
+}
+
+const gchar *
+ide_entry_popover_get_title (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_val_if_fail (IDE_IS_ENTRY_POPOVER (self), NULL);
+
+  return gtk_label_get_label (priv->title);
+}
+
+void
+ide_entry_popover_set_title (IdeEntryPopover *self,
+                             const gchar     *title)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_return_if_fail (IDE_IS_ENTRY_POPOVER (self));
+
+  gtk_label_set_label (priv->title, title);
+  g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_TITLE]);
+}
+
+static void
+ide_entry_popover_button_clicked (IdeEntryPopover *self,
+                                  GtkButton       *button)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+  const gchar *text;
+
+  g_assert (IDE_IS_ENTRY_POPOVER (self));
+  g_assert (GTK_IS_BUTTON (button));
+
+  text = gtk_editable_get_text (GTK_EDITABLE (priv->entry));
+  g_signal_emit (self, signals [ACTIVATE], 0, text);
+  gtk_popover_popdown (GTK_POPOVER (self));
+}
+
+static void
+ide_entry_popover_entry_activate (IdeEntryPopover *self,
+                                  GtkEntry        *entry)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  g_assert (IDE_IS_ENTRY_POPOVER (self));
+  g_assert (GTK_IS_ENTRY (entry));
+
+  if (ide_entry_popover_get_ready (self))
+    gtk_widget_activate (GTK_WIDGET (priv->button));
+}
+
+static void
+ide_entry_popover_entry_changed (IdeEntryPopover *self,
+                                 GtkEntry        *entry)
+{
+  g_assert (IDE_IS_ENTRY_POPOVER (self));
+  g_assert (GTK_IS_ENTRY (entry));
+
+  g_signal_emit (self, signals [CHANGED], 0);
+}
+
+static void
+ide_entry_popover_entry_insert_text (IdeEntryPopover *self,
+                                     gchar           *new_text,
+                                     gint             new_text_length,
+                                     gint            *position,
+                                     GtkEntry        *entry)
+{
+  gboolean ret = GDK_EVENT_PROPAGATE;
+  guint pos;
+  guint n_chars;
+
+  g_assert (IDE_IS_ENTRY_POPOVER (self));
+  g_assert (new_text != NULL);
+  g_assert (position != NULL);
+
+  pos = *position;
+  n_chars = (new_text_length >= 0) ? new_text_length : g_utf8_strlen (new_text, -1);
+
+  g_signal_emit (self, signals [INSERT_TEXT], 0, pos, new_text, n_chars, &ret);
+
+  if (ret == GDK_EVENT_STOP)
+    g_signal_stop_emission_by_name (entry, "insert-text");
+}
+
+static void
+ide_entry_popover_get_property (GObject    *object,
+                                guint       prop_id,
+                                GValue     *value,
+                                GParamSpec *pspec)
+{
+  IdeEntryPopover *self = IDE_ENTRY_POPOVER (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUTTON_TEXT:
+      g_value_set_string (value, ide_entry_popover_get_button_text (self));
+      break;
+
+    case PROP_MESSAGE:
+      g_value_set_string (value, ide_entry_popover_get_message (self));
+      break;
+
+    case PROP_READY:
+      g_value_set_boolean (value, ide_entry_popover_get_ready (self));
+      break;
+
+    case PROP_TEXT:
+      g_value_set_string (value, ide_entry_popover_get_text (self));
+      break;
+
+    case PROP_TITLE:
+      g_value_set_string (value, ide_entry_popover_get_title (self));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_entry_popover_set_property (GObject      *object,
+                                guint         prop_id,
+                                const GValue *value,
+                                GParamSpec   *pspec)
+{
+  IdeEntryPopover *self = IDE_ENTRY_POPOVER (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUTTON_TEXT:
+      ide_entry_popover_set_button_text (self, g_value_get_string (value));
+      break;
+
+    case PROP_MESSAGE:
+      ide_entry_popover_set_message (self, g_value_get_string (value));
+      break;
+
+    case PROP_READY:
+      ide_entry_popover_set_ready (self, g_value_get_boolean (value));
+      break;
+
+    case PROP_TEXT:
+      ide_entry_popover_set_text (self, g_value_get_string (value));
+      break;
+
+    case PROP_TITLE:
+      ide_entry_popover_set_title (self, g_value_get_string (value));
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+    }
+}
+
+static void
+ide_entry_popover_class_init (IdeEntryPopoverClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->get_property = ide_entry_popover_get_property;
+  object_class->set_property = ide_entry_popover_set_property;
+
+  properties [PROP_BUTTON_TEXT] =
+    g_param_spec_string ("button-text",
+                         "Button Text",
+                         "Button Text",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_MESSAGE] =
+    g_param_spec_string ("message",
+                         "Message",
+                         "Message",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_READY] =
+    g_param_spec_boolean ("ready",
+                          "Ready",
+                          "Ready",
+                          FALSE,
+                          (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_TEXT] =
+    g_param_spec_string ("text",
+                         "Text",
+                         "Text",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  properties [PROP_TITLE] =
+    g_param_spec_string ("title",
+                         "Title",
+                         "Title",
+                         NULL,
+                         (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
+
+  g_object_class_install_properties (object_class, LAST_PROP, properties);
+
+  signals [ACTIVATE] =
+    g_signal_new ("activate",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (IdeEntryPopoverClass, activate),
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  1,
+                  G_TYPE_STRING);
+
+  signals [CHANGED] =
+    g_signal_new ("changed",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (IdeEntryPopoverClass, insert_text),
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  0);
+
+  signals [INSERT_TEXT] =
+    g_signal_new ("insert-text",
+                  G_TYPE_FROM_CLASS (klass),
+                  G_SIGNAL_RUN_LAST,
+                  G_STRUCT_OFFSET (IdeEntryPopoverClass, insert_text),
+                  NULL, NULL, NULL,
+                  G_TYPE_BOOLEAN,
+                  3,
+                  G_TYPE_UINT,
+                  G_TYPE_STRING,
+                  G_TYPE_UINT);
+
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/libide-gtk/ide-entry-popover.ui");
+  gtk_widget_class_bind_template_child_private (widget_class, IdeEntryPopover, title);
+  gtk_widget_class_bind_template_child_private (widget_class, IdeEntryPopover, message);
+  gtk_widget_class_bind_template_child_private (widget_class, IdeEntryPopover, entry);
+  gtk_widget_class_bind_template_child_private (widget_class, IdeEntryPopover, button);
+}
+
+static void
+ide_entry_popover_init (IdeEntryPopover *self)
+{
+  IdeEntryPopoverPrivate *priv = ide_entry_popover_get_instance_private (self);
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  g_signal_connect_object (priv->button,
+                           "clicked",
+                           G_CALLBACK (ide_entry_popover_button_clicked),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->entry,
+                           "changed",
+                           G_CALLBACK (ide_entry_popover_entry_changed),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->entry,
+                           "activate",
+                           G_CALLBACK (ide_entry_popover_entry_activate),
+                           self,
+                           G_CONNECT_SWAPPED);
+
+  g_signal_connect_object (priv->entry,
+                           "insert-text",
+                           G_CALLBACK (ide_entry_popover_entry_insert_text),
+                           self,
+                           G_CONNECT_SWAPPED);
+}
+
+GtkWidget *
+ide_entry_popover_new (void)
+{
+  return g_object_new (IDE_TYPE_ENTRY_POPOVER, NULL);
+}
diff --git a/src/libide/gtk/ide-entry-popover.h b/src/libide/gtk/ide-entry-popover.h
new file mode 100644
index 000000000..2036c0f8c
--- /dev/null
+++ b/src/libide/gtk/ide-entry-popover.h
@@ -0,0 +1,101 @@
+/* ide-entry-popover.h
+ *
+ * Copyright (C) 2015-2022 Christian Hergert <christian hergert me>
+ *
+ * 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>
+
+#include <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_ENTRY_POPOVER (ide_entry_popover_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_DERIVABLE_TYPE (IdeEntryPopover, ide_entry_popover, IDE, ENTRY_POPOVER, GtkPopover)
+
+struct _IdeEntryPopoverClass
+{
+  GtkPopoverClass parent;
+
+  /**
+   * IdeEntryPopover::activate:
+   * @self: The #IdeEntryPopover instance.
+   * @text: The text at the time of activation.
+   *
+   * This signal is emitted when the popover's forward button is activated.
+   * Connect to this signal to perform your forward progress.
+   */
+  void (*activate) (IdeEntryPopover *self,
+                    const gchar      *text);
+
+  /**
+   * IdeEntryPopover::insert-text:
+   * @self: A #IdeEntryPopover.
+   * @position: the position in UTF-8 characters.
+   * @chars: the NULL terminated UTF-8 text to insert.
+   * @n_chars: the number of UTF-8 characters in chars.
+   *
+   * Use this signal to determine if text should be allowed to be inserted
+   * into the text buffer. Return GDK_EVENT_STOP to prevent the text from
+   * being inserted.
+   */
+  gboolean (*insert_text) (IdeEntryPopover *self,
+                           guint             position,
+                           const gchar      *chars,
+                           guint             n_chars);
+
+
+  /**
+   * IdeEntryPopover::changed:
+   * @self: A #IdeEntryPopover.
+   *
+   * This signal is emitted when the entry text changes.
+   */
+  void (*changed) (IdeEntryPopover *self);
+};
+
+IDE_AVAILABLE_IN_ALL
+GtkWidget   *ide_entry_popover_new             (void);
+IDE_AVAILABLE_IN_ALL
+const gchar *ide_entry_popover_get_text        (IdeEntryPopover *self);
+IDE_AVAILABLE_IN_ALL
+void         ide_entry_popover_set_text        (IdeEntryPopover *self,
+                                                 const gchar     *text);
+IDE_AVAILABLE_IN_ALL
+const gchar *ide_entry_popover_get_message     (IdeEntryPopover *self);
+IDE_AVAILABLE_IN_ALL
+void         ide_entry_popover_set_message     (IdeEntryPopover *self,
+                                                 const gchar     *message);
+IDE_AVAILABLE_IN_ALL
+const gchar *ide_entry_popover_get_title       (IdeEntryPopover *self);
+IDE_AVAILABLE_IN_ALL
+void         ide_entry_popover_set_title       (IdeEntryPopover *self,
+                                                 const gchar     *title);
+IDE_AVAILABLE_IN_ALL
+const gchar *ide_entry_popover_get_button_text (IdeEntryPopover *self);
+IDE_AVAILABLE_IN_ALL
+void         ide_entry_popover_set_button_text (IdeEntryPopover *self,
+                                                 const gchar     *button_text);
+IDE_AVAILABLE_IN_ALL
+gboolean     ide_entry_popover_get_ready       (IdeEntryPopover *self);
+IDE_AVAILABLE_IN_ALL
+void         ide_entry_popover_set_ready       (IdeEntryPopover *self,
+                                                 gboolean         ready);
+
+G_END_DECLS
diff --git a/src/libide/gtk/ide-entry-popover.ui b/src/libide/gtk/ide-entry-popover.ui
new file mode 100644
index 000000000..9a1d0c788
--- /dev/null
+++ b/src/libide/gtk/ide-entry-popover.ui
@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk" version="4.0"/>
+  <template class="IdeEntryPopover" parent="GtkPopover">
+    <child>
+      <object class="GtkBox">
+        <property name="margin-top">12</property>
+        <property name="margin-bottom">12</property>
+        <property name="margin-start">12</property>
+        <property name="margin-end">12</property>
+        <property name="orientation">vertical</property>
+        <property name="spacing">6</property>
+        <property name="visible">true</property>
+        <child>
+          <object class="GtkLabel" id="title">
+            <property name="xalign">0.0</property>
+            <property name="visible">true</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+        </child>
+        <child>
+          <object class="GtkBox">
+            <property name="orientation">horizontal</property>
+            <property name="spacing">9</property>
+            <property name="visible">true</property>
+            <child>
+              <object class="GtkEntry" id="entry">
+                <property name="width-chars">20</property>
+                <property name="visible">true</property>
+              </object>
+            </child>
+            <child>
+              <object class="GtkButton" id="button">
+                <property name="sensitive">false</property>
+                <property name="use-underline">true</property>
+                <property name="visible">true</property>
+                <style>
+                  <class name="suggested-action"/>
+                </style>
+              </object>
+            </child>
+          </object>
+        </child>
+        <child>
+          <object class="GtkLabel" id="message">
+            <property name="xalign">0.0</property>
+            <property name="visible">true</property>
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+        </child>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/libide/gtk/libide-gtk.gresource.xml b/src/libide/gtk/libide-gtk.gresource.xml
index 3bc5bdbc9..8fd8a7c16 100644
--- a/src/libide/gtk/libide-gtk.gresource.xml
+++ b/src/libide/gtk/libide-gtk.gresource.xml
@@ -1,6 +1,7 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <gresources>
   <gresource prefix="/org/gnome/libide-gtk">
+    <file preprocess="xml-stripblanks">ide-entry-popover.ui</file>
     <file preprocess="xml-stripblanks">ide-search-entry.ui</file>
   </gresource>
 </gresources>
diff --git a/src/libide/gtk/meson.build b/src/libide/gtk/meson.build
index 9218f0918..0e5cc3e51 100644
--- a/src/libide/gtk/meson.build
+++ b/src/libide/gtk/meson.build
@@ -9,6 +9,7 @@ libide_include_directories += include_directories('.')
 libide_gtk_public_headers = [
   'ide-animation.h',
   'ide-cell-renderer-fancy.h',
+  'ide-entry-popover.h',
   'ide-fancy-tree-view.h',
   'ide-gtk.h',
   'ide-menu-manager.h',
@@ -29,6 +30,7 @@ install_headers(libide_gtk_public_headers, subdir: libide_gtk_header_subdir)
 libide_gtk_public_sources = [
   'ide-animation.c',
   'ide-cell-renderer-fancy.c',
+  'ide-entry-popover.c',
   'ide-fancy-tree-view.c',
   'ide-gtk.c',
   'ide-menu-manager.c',


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