[gnome-builder/wip/gtk4-port] libide/editor: add utils module
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/wip/gtk4-port] libide/editor: add utils module
- Date: Sat, 9 Apr 2022 03:44:47 +0000 (UTC)
commit 2ce186c12f89b078b729ea10b32b9676ae07583c
Author: Christian Hergert <chergert redhat com>
Date: Fri Apr 8 20:44:38 2022 -0700
libide/editor: add utils module
This includes a bunch of things to make working with file-choosers
easier wrt encoding and line endings.
src/libide/editor/ide-editor-utils.c | 167 +++++++++++++++++++++++++++++++++++
src/libide/editor/ide-editor-utils.h | 39 ++++++++
src/libide/editor/libide-editor.h | 1 +
src/libide/editor/meson.build | 2 +
4 files changed, 209 insertions(+)
---
diff --git a/src/libide/editor/ide-editor-utils.c b/src/libide/editor/ide-editor-utils.c
new file mode 100644
index 000000000..8be2119d2
--- /dev/null
+++ b/src/libide/editor/ide-editor-utils.c
@@ -0,0 +1,167 @@
+/* ide-editor-utils.c
+ *
+ * Copyright 2020 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-editor-utils"
+
+#include "config.h"
+
+#include <glib/gi18n.h>
+#include <string.h>
+#include <math.h>
+
+#include "ide-editor-utils.h"
+
+static const struct {
+ GtkSourceNewlineType type;
+ const char *id;
+ const char *label;
+} line_endings[] = {
+ { GTK_SOURCE_NEWLINE_TYPE_LF, "unix", N_("Unix/Linux (LF)") },
+ { GTK_SOURCE_NEWLINE_TYPE_CR, "mac", N_("Mac OS Classic (CR)") },
+ { GTK_SOURCE_NEWLINE_TYPE_CR_LF, "windows", N_("Windows (CR+LF)") },
+};
+
+static int
+sort_by_name (gconstpointer a,
+ gconstpointer b)
+{
+ return g_strcmp0 (gtk_source_encoding_get_name (a),
+ gtk_source_encoding_get_name (b));
+}
+
+void
+ide_editor_file_chooser_add_encodings (GtkFileChooser *chooser)
+{
+ GPtrArray *choices;
+ GPtrArray *labels;
+ GSList *all;
+
+ g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
+
+ all = g_slist_sort (gtk_source_encoding_get_all (), sort_by_name);
+ choices = g_ptr_array_new ();
+ labels = g_ptr_array_new_with_free_func (g_free);
+
+#define ADD_ENCODING(id, name) \
+ G_STMT_START { \
+ g_ptr_array_add(choices, (char *)id); \
+ g_ptr_array_add(labels, (char *)name); \
+ } G_STMT_END
+
+ ADD_ENCODING ("auto", g_strdup (N_("Automatically Detected")));
+
+ 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));
+ ADD_ENCODING (gtk_source_encoding_get_charset (encoding), title);
+ }
+
+ ADD_ENCODING (NULL, NULL);
+#undef ADD_ENCODING
+
+ gtk_file_chooser_add_choice (chooser,
+ "encoding",
+ _("Character Encoding:"),
+ (const char **)(gpointer)choices->pdata,
+ (const char **)(gpointer)labels->pdata);
+ gtk_file_chooser_set_choice (chooser, "encoding", "auto");
+
+ g_slist_free (all);
+ g_clear_pointer (&choices, g_ptr_array_unref);
+ g_clear_pointer (&labels, g_ptr_array_unref);
+}
+
+void
+ide_editor_file_chooser_add_line_endings (GtkFileChooser *chooser,
+ GtkSourceNewlineType selected)
+{
+ static GArray *choices;
+ static GArray *labels;
+
+ g_return_if_fail (GTK_IS_FILE_CHOOSER (chooser));
+
+ if (choices == NULL)
+ {
+ choices = g_array_new (TRUE, FALSE, sizeof (char *));
+ labels = g_array_new (TRUE, FALSE, sizeof (char *));
+
+ for (guint i = 0; i < G_N_ELEMENTS (line_endings); i++)
+ {
+ const char *msg = g_dgettext (GETTEXT_PACKAGE, line_endings[i].label);
+
+ g_array_append_val (choices, line_endings[i].id);
+ g_array_append_val (labels, msg);
+ }
+ }
+
+ gtk_file_chooser_add_choice (chooser,
+ "line-ending",
+ _("Line Ending:"),
+ (const char **)(gpointer)choices->data,
+ (const char **)(gpointer)labels->data);
+ gtk_file_chooser_set_choice (chooser, "line-endings", "unix");
+
+ for (guint i = 0; i < G_N_ELEMENTS (line_endings); i++)
+ {
+ if (line_endings[i].type == selected)
+ {
+ gtk_file_chooser_set_choice (chooser, "line-endings", line_endings[i].id);
+ break;
+ }
+ }
+}
+
+const GtkSourceEncoding *
+ide_editor_file_chooser_get_encoding (GtkFileChooser *chooser)
+{
+ const char *encoding;
+
+ g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), NULL);
+
+ if ((encoding = gtk_file_chooser_get_choice (chooser, "encoding")))
+ {
+ if (strcmp (encoding, "auto") != 0)
+ return gtk_source_encoding_get_from_charset (encoding);
+ }
+
+ return NULL;
+}
+
+GtkSourceNewlineType
+ide_editor_file_chooser_get_line_ending (GtkFileChooser *chooser)
+{
+ const char *ending;
+
+ g_return_val_if_fail (GTK_IS_FILE_CHOOSER (chooser), 0);
+
+ if ((ending = gtk_file_chooser_get_choice (chooser, "line-ending")))
+ {
+ for (guint i = 0; i < G_N_ELEMENTS (line_endings); i++)
+ {
+ if (g_strcmp0 (ending, line_endings[i].id) == 0)
+ return line_endings[i].type;
+ }
+ }
+
+ return GTK_SOURCE_NEWLINE_TYPE_LF;
+}
diff --git a/src/libide/editor/ide-editor-utils.h b/src/libide/editor/ide-editor-utils.h
new file mode 100644
index 000000000..baa349998
--- /dev/null
+++ b/src/libide/editor/ide-editor-utils.h
@@ -0,0 +1,39 @@
+/* ide-editor-utils.h
+ *
+ * Copyright 2020 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_EDITOR_INSIDE) && !defined (IDE_EDITOR_COMPILATION)
+# error "Only <libide-editor.h> can be included directly."
+#endif
+
+#include <gtksourceview/gtksource.h>
+
+#include <libide-core.h>
+
+G_BEGIN_DECLS
+
+void ide_editor_file_chooser_add_encodings (GtkFileChooser *chooser);
+void ide_editor_file_chooser_add_line_endings (GtkFileChooser *chooser,
+ GtkSourceNewlineType selected);
+const GtkSourceEncoding *ide_editor_file_chooser_get_encoding (GtkFileChooser *chooser);
+GtkSourceNewlineType ide_editor_file_chooser_get_line_ending (GtkFileChooser *chooser);
+
+G_END_DECLS
diff --git a/src/libide/editor/libide-editor.h b/src/libide/editor/libide-editor.h
index 9eecd565b..c1d6fe856 100644
--- a/src/libide/editor/libide-editor.h
+++ b/src/libide/editor/libide-editor.h
@@ -24,5 +24,6 @@
# include "ide-editor.h"
# include "ide-editor-page.h"
# include "ide-editor-page-addin.h"
+# include "ide-editor-utils.h"
# include "ide-editor-workspace.h"
#undef IDE_EDITOR_INSIDE
diff --git a/src/libide/editor/meson.build b/src/libide/editor/meson.build
index ff3abcdbc..c6eb702b8 100644
--- a/src/libide/editor/meson.build
+++ b/src/libide/editor/meson.build
@@ -14,6 +14,7 @@ libide_editor_public_headers = [
'ide-editor.h',
'ide-editor-page.h',
'ide-editor-page-addin.h',
+ 'ide-editor-utils.h',
'ide-editor-workspace.h',
'libide-editor.h',
]
@@ -32,6 +33,7 @@ libide_editor_public_sources = [
'ide-editor.c',
'ide-editor-page.c',
'ide-editor-page-addin.c',
+ 'ide-editor-utils.c',
'ide-editor-workspace.c',
]
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]