[gnome-builder/wip/gtk4-port] plugins/editorui: add position label



commit e71081d2127f58a0afb75fe4d4011e1fb8c17fb2
Author: Christian Hergert <chergert redhat com>
Date:   Sun Apr 3 18:13:27 2022 -0700

    plugins/editorui: add position label
    
    This uses tnum for the various fields directly so that we get somewhat
    nice and non-jumping values.

 src/plugins/editorui/editorui.gresource.xml        |  1 +
 src/plugins/editorui/gbp-editorui-position-label.c | 82 ++++++++++++++++++++++
 src/plugins/editorui/gbp-editorui-position-label.h | 35 +++++++++
 .../editorui/gbp-editorui-position-label.ui        | 36 ++++++++++
 .../editorui/gbp-editorui-workspace-addin.c        | 51 +++++---------
 src/plugins/editorui/meson.build                   |  1 +
 6 files changed, 173 insertions(+), 33 deletions(-)
---
diff --git a/src/plugins/editorui/editorui.gresource.xml b/src/plugins/editorui/editorui.gresource.xml
index 7651fc6fb..0a3b863de 100644
--- a/src/plugins/editorui/editorui.gresource.xml
+++ b/src/plugins/editorui/editorui.gresource.xml
@@ -3,5 +3,6 @@
   <gresource prefix="/plugins/editorui">
     <file>editorui.plugin</file>
     <file preprocess="xml-stripblanks">gtk/menus.ui</file>
+    <file preprocess="xml-stripblanks">gbp-editorui-position-label.ui</file>
   </gresource>
 </gresources>
diff --git a/src/plugins/editorui/gbp-editorui-position-label.c 
b/src/plugins/editorui/gbp-editorui-position-label.c
new file mode 100644
index 000000000..fcab427b1
--- /dev/null
+++ b/src/plugins/editorui/gbp-editorui-position-label.c
@@ -0,0 +1,82 @@
+/* gbp-editorui-position-label.c
+ *
+ * Copyright 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 "gbp-editorui-position-label"
+
+#include "config.h"
+
+#include "gbp-editorui-position-label.h"
+
+struct _GbpEditoruiPositionLabel
+{
+  GtkWidget parent_instance;
+  GtkLabel *line;
+  GtkLabel *column;
+};
+
+G_DEFINE_TYPE (GbpEditoruiPositionLabel, gbp_editorui_position_label, GTK_TYPE_WIDGET)
+
+static void
+gbp_editorui_position_label_dispose (GObject *object)
+{
+  GbpEditoruiPositionLabel *self = (GbpEditoruiPositionLabel *)object;
+  GtkWidget *child;
+
+  while ((child = gtk_widget_get_first_child (GTK_WIDGET (self))))
+    gtk_widget_unparent (child);
+
+  G_OBJECT_CLASS (gbp_editorui_position_label_parent_class)->dispose (object);
+}
+
+static void
+gbp_editorui_position_label_class_init (GbpEditoruiPositionLabelClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+  object_class->dispose = gbp_editorui_position_label_dispose;
+
+  gtk_widget_class_set_layout_manager_type (widget_class, GTK_TYPE_BOX_LAYOUT);
+  gtk_widget_class_set_template_from_resource (widget_class, 
"/plugins/editorui/gbp-editorui-position-label.ui");
+  gtk_widget_class_bind_template_child (widget_class, GbpEditoruiPositionLabel, line);
+  gtk_widget_class_bind_template_child (widget_class, GbpEditoruiPositionLabel, column);
+}
+
+static void
+gbp_editorui_position_label_init (GbpEditoruiPositionLabel *self)
+{
+  gtk_widget_init_template (GTK_WIDGET (self));
+}
+
+void
+gbp_editorui_position_label_update (GbpEditoruiPositionLabel *self,
+                                    guint                     line,
+                                    guint                     column)
+{
+  char str[32];
+
+  g_return_if_fail (GBP_IS_EDITORUI_POSITION_LABEL (self));
+
+  g_snprintf (str, sizeof str, "%u", line + 1);
+  gtk_label_set_label (self->line, str);
+
+  g_snprintf (str, sizeof str, "%u", column + 1);
+  gtk_label_set_label (self->column, str);
+}
diff --git a/src/plugins/editorui/gbp-editorui-position-label.h 
b/src/plugins/editorui/gbp-editorui-position-label.h
new file mode 100644
index 000000000..6013ec41e
--- /dev/null
+++ b/src/plugins/editorui/gbp-editorui-position-label.h
@@ -0,0 +1,35 @@
+/* gbp-editorui-position-label.h
+ *
+ * Copyright 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
+
+#include <gtk/gtk.h>
+
+G_BEGIN_DECLS
+
+#define GBP_TYPE_EDITORUI_POSITION_LABEL (gbp_editorui_position_label_get_type())
+
+G_DECLARE_FINAL_TYPE (GbpEditoruiPositionLabel, gbp_editorui_position_label, GBP, EDITORUI_POSITION_LABEL, 
GtkWidget)
+
+void gbp_editorui_position_label_update (GbpEditoruiPositionLabel *self,
+                                         guint                     line,
+                                         guint                     column);
+
+G_END_DECLS
diff --git a/src/plugins/editorui/gbp-editorui-position-label.ui 
b/src/plugins/editorui/gbp-editorui-position-label.ui
new file mode 100644
index 000000000..60544769e
--- /dev/null
+++ b/src/plugins/editorui/gbp-editorui-position-label.ui
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<interface>
+  <template class="GbpEditoruiPositionLabel" parent="GtkWidget">
+    <child>
+      <object class="GtkLabel">
+        <property name="label" translatable="yes">Ln</property>
+      </object>
+    </child>
+    <child>
+      <object class="GtkLabel" id="line">
+        <property name="xalign">1</property>
+        <property name="width-chars">2</property>
+        <property name="margin-start">6</property>
+        <attributes>
+          <attribute name="font-features" value="tnum"/>
+        </attributes>
+      </object>
+    </child>
+    <child>
+      <object class="GtkLabel">
+        <property name="margin-start">6</property>
+        <property name="label" translatable="yes">Col</property>
+      </object>
+    </child>
+    <child>
+      <object class="GtkLabel" id="column">
+        <property name="xalign">1</property>
+        <property name="width-chars">2</property>
+        <property name="margin-start">6</property>
+        <attributes>
+          <attribute name="font-features" value="tnum"/>
+        </attributes>
+      </object>
+    </child>
+  </template>
+</interface>
diff --git a/src/plugins/editorui/gbp-editorui-workspace-addin.c 
b/src/plugins/editorui/gbp-editorui-workspace-addin.c
index a31861b31..5e2252131 100644
--- a/src/plugins/editorui/gbp-editorui-workspace-addin.c
+++ b/src/plugins/editorui/gbp-editorui-workspace-addin.c
@@ -26,25 +26,26 @@
 
 #include <libide-editor.h>
 
+#include "gbp-editorui-position-label.h"
 #include "gbp-editorui-workspace-addin.h"
 
 struct _GbpEditoruiWorkspaceAddin
 {
-  GObject          parent_instance;
+  GObject                   parent_instance;
 
-  IdeWorkspace    *workspace;
-  PanelStatusbar  *statusbar;
+  IdeWorkspace             *workspace;
+  PanelStatusbar           *statusbar;
 
-  IdeSignalGroup  *buffer_signals;
-  IdeSignalGroup  *view_signals;
+  IdeSignalGroup           *buffer_signals;
+  IdeSignalGroup           *view_signals;
 
-  GtkMenuButton   *indentation;
-  GtkLabel        *indentation_label;
+  GtkMenuButton            *indentation;
+  GtkLabel                 *indentation_label;
 
-  GtkMenuButton   *position;
-  GtkLabel        *position_label;
+  GtkMenuButton            *position;
+  GbpEditoruiPositionLabel *position_label;
 
-  guint            queued_cursor_moved;
+  guint                     queued_cursor_moved;
 };
 
 #define clear_from_statusbar(s,w) clear_from_statusbar(s, (GtkWidget **)w)
@@ -60,24 +61,6 @@ static void
     }
 }
 
-static GtkLabel *
-tnum_label_new (void)
-{
-  static cairo_font_options_t *options;
-  GtkWidget *label;
-
-  if (options == NULL)
-    {
-      options = cairo_font_options_create ();
-      cairo_font_options_set_variations (options, "tnum");
-    }
-
-  label = gtk_label_new (NULL);
-  gtk_widget_set_font_options (label, options);
-
-  return GTK_LABEL (label);
-}
-
 static void
 notify_indentation_cb (GbpEditoruiWorkspaceAddin *self)
 {
@@ -111,15 +94,17 @@ notify_indentation_cb (GbpEditoruiWorkspaceAddin *self)
 static void
 update_position (GbpEditoruiWorkspaceAddin *self)
 {
-  g_autofree char *label = NULL;
   IdeSourceView *view;
 
   g_assert (GBP_IS_EDITORUI_WORKSPACE_ADDIN (self));
 
   if ((view = ide_signal_group_get_target (self->view_signals)))
-    label = ide_source_view_dup_position_label (view);
+    {
+      guint line, column;
 
-  gtk_label_set_label (self->position_label, label);
+      ide_source_view_get_visual_position (view, &line, &column);
+      gbp_editorui_position_label_update (self->position_label, line, column);
+    }
 }
 
 static gboolean
@@ -186,7 +171,7 @@ gbp_editorui_workspace_addin_load (IdeWorkspaceAddin *addin,
 
   /* Indentation status, tabs/spaces/etc */
   menu = ide_application_get_menu_by_id (IDE_APPLICATION_DEFAULT, "editorui-indent-menu");
-  self->indentation_label = tnum_label_new ();
+  self->indentation_label = g_object_new (GTK_TYPE_LABEL, NULL);
   self->indentation = g_object_new (GTK_TYPE_MENU_BUTTON,
                                     "menu-model", menu,
                                     "direction", GTK_ARROW_UP,
@@ -196,7 +181,7 @@ gbp_editorui_workspace_addin_load (IdeWorkspaceAddin *addin,
   panel_statusbar_add_suffix (self->statusbar, GTK_WIDGET (self->indentation));
 
   /* Label for cursor position and jump to line/column */
-  self->position_label = tnum_label_new ();
+  self->position_label = g_object_new (GBP_TYPE_EDITORUI_POSITION_LABEL, NULL);
   self->position = g_object_new (GTK_TYPE_MENU_BUTTON,
                                  "direction", GTK_ARROW_UP,
                                  "visible", FALSE,
diff --git a/src/plugins/editorui/meson.build b/src/plugins/editorui/meson.build
index 62926aec3..acba126eb 100644
--- a/src/plugins/editorui/meson.build
+++ b/src/plugins/editorui/meson.build
@@ -1,5 +1,6 @@
 plugins_sources += files([
   'editorui-plugin.c',
+  'gbp-editorui-position-label.c',
   'gbp-editorui-workbench-addin.c',
   'gbp-editorui-workspace-addin.c',
 ])


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