[gnome-builder/wip/gtk4-port: 537/736] libide/editor: add helper to create menu for encodings




commit 90a821c87f73cd550a9cc573f5e6c8085d04e302
Author: Christian Hergert <chergert redhat com>
Date:   Fri Apr 8 21:40:21 2022 -0700

    libide/editor: add helper to create menu for encodings

 src/libide/editor/ide-editor-utils.c | 38 ++++++++++++++++++++++++++++++++++++
 src/libide/editor/ide-editor-utils.h |  1 +
 2 files changed, 39 insertions(+)
---
diff --git a/src/libide/editor/ide-editor-utils.c b/src/libide/editor/ide-editor-utils.c
index 8be2119d2..61333a6e1 100644
--- a/src/libide/editor/ide-editor-utils.c
+++ b/src/libide/editor/ide-editor-utils.c
@@ -46,6 +46,44 @@ sort_by_name (gconstpointer a,
                     gtk_source_encoding_get_name (b));
 }
 
+/**
+ * ide_editor_encoding_menu_new:
+ * @action_name: the action to activate when selecting menu items
+ *
+ * Creates a new #GMenuModel with items which will activate using
+ * their encoding charset for the action @action_name target.
+ *
+ * Returns: (transfer full): a #GMenuModel
+ */
+GMenuModel *
+ide_editor_encoding_menu_new (const char *action_name)
+{
+  GMenu *menu;
+  GSList *all;
+
+  g_return_val_if_fail (action_name, NULL);
+
+  menu = g_menu_new ();
+  all = g_slist_sort (gtk_source_encoding_get_all (), sort_by_name);
+
+  for (const GSList *l = all; l; l = l->next)
+    {
+      GtkSourceEncoding *encoding = l->data;
+      char *title = g_strdup_printf ("%s (%s)",
+                                     gtk_source_encoding_get_name (encoding),
+                                     gtk_source_encoding_get_charset (encoding));
+      GMenuItem *item = g_menu_item_new (title, NULL);
+
+      g_menu_item_set_action_and_target (item, action_name, "s", gtk_source_encoding_get_charset (encoding));
+      g_menu_append_item (menu, item);
+      g_object_unref (item);
+    }
+
+  g_slist_free (all);
+
+  return G_MENU_MODEL (menu);
+}
+
 void
 ide_editor_file_chooser_add_encodings (GtkFileChooser *chooser)
 {
diff --git a/src/libide/editor/ide-editor-utils.h b/src/libide/editor/ide-editor-utils.h
index baa349998..0404cd4a0 100644
--- a/src/libide/editor/ide-editor-utils.h
+++ b/src/libide/editor/ide-editor-utils.h
@@ -35,5 +35,6 @@ void                     ide_editor_file_chooser_add_line_endings  (GtkFileChoos
                                                                     GtkSourceNewlineType  selected);
 const GtkSourceEncoding *ide_editor_file_chooser_get_encoding      (GtkFileChooser       *chooser);
 GtkSourceNewlineType     ide_editor_file_chooser_get_line_ending   (GtkFileChooser       *chooser);
+GMenuModel              *ide_editor_encoding_menu_new              (const char           *action_name);
 
 G_END_DECLS


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