[gnome-builder/wip/gtk4-port: 85/343] libide/gtk: add IdeSearchEntry




commit 4f5d8712412236172c55d1f409cda15e855a70d9
Author: Christian Hergert <chergert redhat com>
Date:   Mon Mar 28 13:52:46 2022 -0700

    libide/gtk: add IdeSearchEntry
    
    This is a port form EditorSearchEntry which we use in gnome-text-editor
    and does the same thing we used IdeTaggedEntry for.

 src/libide/gtk/ide-search-entry.c       |  230 ++++++
 src/libide/gtk/ide-search-entry.h       |   47 ++
 src/libide/gtk/ide-search-entry.ui      |   30 +
 src/libide/gtk/libide-gtk.gresource.xml |    6 +
 src/libide/gtk/libide-gtk.h             |    1 +
 src/libide/gtk/meson.build              |   14 +
 src/libide/gui/ide-tagged-entry.c       | 1240 -------------------------------
 src/libide/gui/ide-tagged-entry.h       |  123 ---
 src/libide/gui/meson.build              |    2 -
 9 files changed, 328 insertions(+), 1365 deletions(-)
---
diff --git a/src/libide/gtk/ide-search-entry.c b/src/libide/gtk/ide-search-entry.c
new file mode 100644
index 000000000..9e485cd86
--- /dev/null
+++ b/src/libide/gtk/ide-search-entry.c
@@ -0,0 +1,230 @@
+/* ide-search-entry.c
+ *
+ * Copyright 2021-2022 Christian Hergert <chergert redhat 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#define G_LOG_DOMAIN "ide-search-entry"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+
+#include "ide-search-entry.h"
+
+struct _IdeSearchEntry
+{
+  GtkWidget  parent_instance;
+
+  GtkText   *text;
+  GtkLabel  *info;
+
+  guint      occurrence_count;
+  int        occurrence_position;
+};
+
+static void editable_iface_init (GtkEditableInterface *iface);
+
+G_DEFINE_TYPE_WITH_CODE (IdeSearchEntry, ide_search_entry, GTK_TYPE_WIDGET,
+                         G_IMPLEMENT_INTERFACE (GTK_TYPE_EDITABLE, editable_iface_init))
+
+GtkWidget *
+ide_search_entry_new (void)
+{
+  return g_object_new (IDE_TYPE_SEARCH_ENTRY, NULL);
+}
+
+static gboolean
+ide_search_entry_grab_focus (GtkWidget *widget)
+{
+  return gtk_widget_grab_focus (GTK_WIDGET (IDE_SEARCH_ENTRY (widget)->text));
+}
+
+static void
+on_text_activate_cb (IdeSearchEntry *self,
+                     GtkText           *text)
+{
+  g_assert (IDE_IS_SEARCH_ENTRY (self));
+  g_assert (GTK_IS_TEXT (text));
+
+  gtk_widget_activate_action (GTK_WIDGET (self), "search.move-next", "b", FALSE);
+}
+
+static void
+on_text_notify_cb (IdeSearchEntry *self,
+                   GParamSpec        *pspec,
+                   GtkText           *text)
+{
+  GObjectClass *klass;
+
+  g_assert (IDE_IS_SEARCH_ENTRY (self));
+  g_assert (GTK_IS_TEXT (text));
+
+  klass = G_OBJECT_GET_CLASS (self);
+  pspec = g_object_class_find_property (klass, pspec->name);
+
+  if (pspec != NULL)
+    g_object_notify_by_pspec (G_OBJECT (self), pspec);
+}
+
+static void
+ide_search_entry_dispose (GObject *object)
+{
+  IdeSearchEntry *self = (IdeSearchEntry *)object;
+  GtkWidget *child;
+
+  self->text = NULL;
+  self->info = NULL;
+
+  while ((child = gtk_widget_get_first_child (GTK_WIDGET (self))))
+    gtk_widget_unparent (child);
+
+  G_OBJECT_CLASS (ide_search_entry_parent_class)->dispose (object);
+}
+
+static void
+ide_search_entry_get_property (GObject    *object,
+                               guint       prop_id,
+                               GValue     *value,
+                               GParamSpec *pspec)
+{
+  if (gtk_editable_delegate_get_property (object, prop_id, value, pspec))
+    return;
+
+  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+}
+
+static void
+ide_search_entry_set_property (GObject      *object,
+                               guint         prop_id,
+                               const GValue *value,
+                               GParamSpec   *pspec)
+{
+  if (gtk_editable_delegate_set_property (object, prop_id, value, pspec))
+    return;
+
+  G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+}
+
+static void
+ide_search_entry_class_init (IdeSearchEntryClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->dispose = ide_search_entry_dispose;
+  object_class->get_property = ide_search_entry_get_property;
+  object_class->set_property = ide_search_entry_set_property;
+
+  widget_class->grab_focus = ide_search_entry_grab_focus;
+
+  gtk_editable_install_properties (object_class, 1);
+
+  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
+  gtk_widget_class_set_css_name (widget_class, "entry");
+  gtk_widget_class_set_accessible_role (widget_class, GTK_ACCESSIBLE_ROLE_TEXT_BOX);
+  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/libide/gtk/ide-search-entry.ui");
+  gtk_widget_class_bind_template_child (widget_class, IdeSearchEntry, info);
+  gtk_widget_class_bind_template_child (widget_class, IdeSearchEntry, text);
+  gtk_widget_class_bind_template_callback (widget_class, on_text_activate_cb);
+  gtk_widget_class_bind_template_callback (widget_class, on_text_notify_cb);
+
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_g, GDK_CONTROL_MASK|GDK_SHIFT_MASK, 
"search.move-previous", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_g, GDK_CONTROL_MASK, "search.move-next", "b", 
FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Down, 0, "search.move-next", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Up, 0, "search.move-previous", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Return, 0, "search.move-next", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Enter, 0, "search.move-next", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Return, GDK_SHIFT_MASK, "search.move-previous", 
"b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Enter, GDK_SHIFT_MASK, 
"search.move-previous", "b", FALSE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Return, GDK_CONTROL_MASK, "search.move-next", 
"b", TRUE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Enter, GDK_CONTROL_MASK, "search.move-next", 
"b", TRUE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_Return, GDK_CONTROL_MASK|GDK_SHIFT_MASK, 
"search.move-previous", "b", TRUE);
+  gtk_widget_class_add_binding_action (widget_class, GDK_KEY_KP_Enter, GDK_CONTROL_MASK|GDK_SHIFT_MASK, 
"search.move-previous", "b", TRUE);
+}
+
+static void
+ide_search_entry_init (IdeSearchEntry *self)
+{
+  cairo_font_options_t *options;
+
+  self->occurrence_position = -1;
+
+  gtk_widget_init_template (GTK_WIDGET (self));
+
+  options = cairo_font_options_create ();
+  cairo_font_options_set_variations (options, "tnum");
+  gtk_widget_set_font_options (GTK_WIDGET (self->info), options);
+  cairo_font_options_destroy (options);
+}
+
+static GtkEditable *
+ide_search_entry_get_delegate (GtkEditable *editable)
+{
+  return GTK_EDITABLE (IDE_SEARCH_ENTRY (editable)->text);
+}
+
+static void
+editable_iface_init (GtkEditableInterface *iface)
+{
+  iface->get_delegate = ide_search_entry_get_delegate;
+}
+
+static void
+ide_search_entry_update_position (IdeSearchEntry *self)
+{
+  g_assert (IDE_IS_SEARCH_ENTRY (self));
+
+  if (self->occurrence_count == 0)
+    {
+      gtk_label_set_label (self->info, NULL);
+    }
+  else
+    {
+      /* translators: the first %u is replaced with the current position, the second with the number of 
search results */
+      g_autofree char *str = g_strdup_printf (_("%u of %u"), MAX (0, self->occurrence_position), 
self->occurrence_count);
+      gtk_label_set_label (self->info, str);
+    }
+}
+
+void
+ide_search_entry_set_occurrence_count (IdeSearchEntry *self,
+                                       guint           occurrence_count)
+{
+  g_assert (IDE_IS_SEARCH_ENTRY (self));
+
+  if (self->occurrence_count != occurrence_count)
+    {
+      self->occurrence_count = occurrence_count;
+      ide_search_entry_update_position (self);
+    }
+}
+
+void
+ide_search_entry_set_occurrence_position (IdeSearchEntry *self,
+                                          int             occurrence_position)
+{
+  g_assert (IDE_IS_SEARCH_ENTRY (self));
+
+  occurrence_position = MAX (-1, occurrence_position);
+
+  if (self->occurrence_position != occurrence_position)
+    {
+      self->occurrence_position = occurrence_position;
+      ide_search_entry_update_position (self);
+    }
+}
diff --git a/src/libide/gtk/ide-search-entry.h b/src/libide/gtk/ide-search-entry.h
new file mode 100644
index 000000000..d4101e4be
--- /dev/null
+++ b/src/libide/gtk/ide-search-entry.h
@@ -0,0 +1,47 @@
+/* ide-search-entry.h
+ *
+ * Copyright 2021-2022 Christian Hergert <chergert redhat 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/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+#pragma once
+
+#if !defined (IDE_GTK_INSIDE) && !defined (IDE_GTK_COMPILATION)
+# error "Only <libide-gtk.h> can be included directly."
+#endif
+
+#include <gtk/gtk.h>
+
+#include <libide-core.h>
+
+G_BEGIN_DECLS
+
+#define IDE_TYPE_SEARCH_ENTRY (ide_search_entry_get_type())
+
+IDE_AVAILABLE_IN_ALL
+G_DECLARE_FINAL_TYPE (IdeSearchEntry, ide_search_entry, IDE, SEARCH_ENTRY, GtkWidget)
+
+IDE_AVAILABLE_IN_ALL
+GtkWidget *ide_search_entry_new                     (void);
+IDE_AVAILABLE_IN_ALL
+void       ide_search_entry_set_occurrence_count    (IdeSearchEntry *self,
+                                                     guint           occurrence_count);
+IDE_AVAILABLE_IN_ALL
+void       ide_search_entry_set_occurrence_position (IdeSearchEntry *self,
+                                                     int             occurrence_position);
+
+G_END_DECLS
diff --git a/src/libide/gtk/ide-search-entry.ui b/src/libide/gtk/ide-search-entry.ui
new file mode 100644
index 000000000..7bcb1c30c
--- /dev/null
+++ b/src/libide/gtk/ide-search-entry.ui
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <requires lib="gtk" version="4.0"/>
+  <template class="IdeSearchEntry" parent="GtkWidget">
+    <property name="width-request">225</property>
+    <child>
+      <object class="GtkImage">
+        <property name="icon-name">edit-find-symbolic</property>
+      </object>
+    </child>
+    <child>
+      <object class="GtkText" id="text">
+        <property name="hexpand">true</property>
+        <property name="vexpand">true</property>
+        <property name="width-chars">12</property>
+        <property name="max-width-chars">12</property>
+        <signal name="notify" handler="on_text_notify_cb" swapped="true"/>
+        <signal name="activate" handler="on_text_activate_cb" swapped="true"/>
+      </object>
+    </child>
+    <child>
+      <object class="GtkLabel" id="info">
+        <property name="xalign">1</property>
+        <attributes>
+          <attribute name="foreground-alpha" value="33000"/>
+        </attributes>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/libide/gtk/libide-gtk.gresource.xml b/src/libide/gtk/libide-gtk.gresource.xml
new file mode 100644
index 000000000..3bc5bdbc9
--- /dev/null
+++ b/src/libide/gtk/libide-gtk.gresource.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<gresources>
+  <gresource prefix="/org/gnome/libide-gtk">
+    <file preprocess="xml-stripblanks">ide-search-entry.ui</file>
+  </gresource>
+</gresources>
diff --git a/src/libide/gtk/libide-gtk.h b/src/libide/gtk/libide-gtk.h
index a2a8e9df3..44921651a 100644
--- a/src/libide/gtk/libide-gtk.h
+++ b/src/libide/gtk/libide-gtk.h
@@ -26,4 +26,5 @@
 # include "ide-fancy-tree-view.h"
 # include "ide-gtk-enums.h"
 # include "ide-menu-manager.h"
+# include "ide-search-entry.h"
 #undef IDE_GTK_INSIDE
diff --git a/src/libide/gtk/meson.build b/src/libide/gtk/meson.build
index d2fb8824c..f46d30060 100644
--- a/src/libide/gtk/meson.build
+++ b/src/libide/gtk/meson.build
@@ -11,6 +11,7 @@ libide_gtk_public_headers = [
   'ide-cell-renderer-fancy.h',
   'ide-fancy-tree-view.h',
   'ide-menu-manager.h',
+  'ide-search-entry.h',
   'libide-gtk.h',
 ]
 
@@ -29,6 +30,7 @@ libide_gtk_public_sources = [
   'ide-cell-renderer-fancy.c',
   'ide-fancy-tree-view.c',
   'ide-menu-manager.c',
+  'ide-search-entry.c',
 ]
 
 libide_gtk_private_sources = [
@@ -50,6 +52,18 @@ libide_gtk_enums = gnome.mkenums_simple('ide-gtk-enums',
 libide_gtk_generated_sources = [libide_gtk_enums[0]]
 libide_gtk_generated_headers = [libide_gtk_enums[1]]
 
+#
+# Generated Resource Files
+#
+
+libide_gtk_resources = gnome.compile_resources(
+  'ide-gtk-resources',
+  'libide-gtk.gresource.xml',
+  c_name: 'ide_gtk',
+)
+libide_gtk_generated_sources += [libide_gtk_resources[0]]
+libide_gtk_generated_headers += [libide_gtk_resources[1]]
+
 #
 # Dependencies
 #
diff --git a/src/libide/gui/meson.build b/src/libide/gui/meson.build
index b34dfb8c8..e11d9de13 100644
--- a/src/libide/gui/meson.build
+++ b/src/libide/gui/meson.build
@@ -32,7 +32,6 @@ libide_gui_public_headers = [
   'ide-session-addin.h',
   'ide-surface.h',
   'ide-surfaces-button.h',
-  'ide-tagged-entry.h',
   'ide-transfer-button.h',
   'ide-transient-sidebar.h',
   'ide-workbench.h',
@@ -112,7 +111,6 @@ libide_gui_public_sources = [
   'ide-shortcut-label.c',
   'ide-surface.c',
   'ide-surfaces-button.c',
-  'ide-tagged-entry.c',
   'ide-transient-sidebar.c',
   'ide-transfer-button.c',
   'ide-workbench.c',


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