[gnome-builder/tweak] GtkEditorView: wire language tweak to source buffer
- From: Christian Hergert <chergert src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-builder/tweak] GtkEditorView: wire language tweak to source buffer
- Date: Wed, 17 Dec 2014 10:26:20 +0000 (UTC)
commit c0d8044739e7a867268d1db4976b5d10d0b03889
Author: Christian Hergert <christian hergert me>
Date: Wed Dec 17 02:26:14 2014 -0800
GtkEditorView: wire language tweak to source buffer
src/editor/gb-editor-tweak-widget.c | 51 +++++++++++++++++++++++++++++++++++
src/editor/gb-editor-view.c | 47 ++++++++++++++++++++++++++++++-
2 files changed, 96 insertions(+), 2 deletions(-)
---
diff --git a/src/editor/gb-editor-tweak-widget.c b/src/editor/gb-editor-tweak-widget.c
index 8371899..8747ab3 100644
--- a/src/editor/gb-editor-tweak-widget.c
+++ b/src/editor/gb-editor-tweak-widget.c
@@ -86,6 +86,51 @@ gb_editor_tweak_widget_entry_changed (GbEditorTweakWidget *widget,
g_free);
}
+static GActionGroup *
+find_action_group (GtkWidget *widget,
+ const gchar *name)
+{
+ GActionGroup *group = NULL;
+
+ g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
+ g_return_val_if_fail (name, NULL);
+
+ while (!group && widget)
+ {
+ group = gtk_widget_get_action_group (widget, name);
+ widget = gtk_widget_get_parent (widget);
+ }
+
+ return group;
+}
+
+static void
+gb_editor_tweak_widget_row_activated (GbEditorTweakWidget *widget,
+ GtkListBoxRow *row,
+ GtkListBox *list_box)
+{
+ GtkSourceLanguage *lang;
+ GActionGroup *group;
+ const gchar *lang_id;
+ GtkWidget *child;
+ GVariant *param;
+
+ g_return_if_fail (GB_IS_EDITOR_TWEAK_WIDGET (widget));
+ g_return_if_fail (GTK_IS_LIST_BOX_ROW (row));
+ g_return_if_fail (GTK_IS_LIST_BOX (list_box));
+
+ child = gtk_bin_get_child (GTK_BIN (row));
+ lang = g_object_get_qdata (G_OBJECT (child), gLangQuark);
+
+ if (lang)
+ {
+ group = find_action_group (GTK_WIDGET (widget), "editor-view");
+ lang_id = gtk_source_language_get_id (lang);
+ param = g_variant_new_string (lang_id);
+ g_action_group_activate_action (group, "language", param);
+ }
+}
+
static void
gb_editor_tweak_widget_constructed (GObject *object)
{
@@ -125,6 +170,12 @@ gb_editor_tweak_widget_constructed (GObject *object)
G_CALLBACK (gb_editor_tweak_widget_entry_changed),
widget,
G_CONNECT_SWAPPED);
+
+ g_signal_connect_object (widget->priv->list_box,
+ "row-activated",
+ G_CALLBACK (gb_editor_tweak_widget_row_activated),
+ widget,
+ G_CONNECT_SWAPPED);
}
static void
diff --git a/src/editor/gb-editor-view.c b/src/editor/gb-editor-view.c
index f4e9dd9..785f838 100644
--- a/src/editor/gb-editor-view.c
+++ b/src/editor/gb-editor-view.c
@@ -28,6 +28,7 @@
#include "gb-glib.h"
#include "gb-html-document.h"
#include "gb-log.h"
+#include "gb-string.h"
#include "gb-widget.h"
struct _GbEditorViewPrivate
@@ -51,6 +52,7 @@ struct _GbEditorViewPrivate
GtkButton *modified_cancel_button;
GtkRevealer *modified_revealer;
GtkMenuButton *tweak_button;
+ GtkMenuButton *tweak_widget;
guint auto_indent : 1;
guint highlight_current_line : 1;
@@ -100,6 +102,30 @@ gb_editor_view_action_set_state (GbEditorView *view,
g_simple_action_set_state (G_SIMPLE_ACTION (action), state);
}
+static void
+apply_state_language (GSimpleAction *action,
+ GVariant *param,
+ gpointer user_data)
+{
+ GbEditorView *view = user_data;
+ GtkSourceLanguage *l = NULL;
+ const gchar *lang_id;
+
+ g_return_if_fail (GB_IS_EDITOR_VIEW (view));
+
+ lang_id = g_variant_get_string (param, NULL);
+
+ if (!gb_str_empty0 (lang_id))
+ {
+ GtkSourceLanguageManager *m;
+
+ m = gtk_source_language_manager_get_default ();
+ l = gtk_source_language_manager_get_language (m, lang_id);
+ }
+
+ gtk_source_buffer_set_language (GTK_SOURCE_BUFFER (view->priv->document), l);
+}
+
gboolean
gb_editor_view_get_auto_indent (GbEditorView *view)
{
@@ -214,10 +240,20 @@ gb_editor_view_notify_language (GbEditorView *view,
GParamSpec *pspec,
GbEditorDocument *document)
{
+ GtkSourceLanguage *language;
+ const gchar *lang_id = "";
+
g_return_if_fail (GB_IS_EDITOR_VIEW (view));
g_return_if_fail (GB_IS_EDITOR_DOCUMENT (document));
g_object_notify (G_OBJECT (view), "can-preview");
+
+ language = gtk_source_buffer_get_language (GTK_SOURCE_BUFFER (document));
+ if (language)
+ lang_id = gtk_source_language_get_id (language);
+
+ gb_editor_view_action_set_state (view, "language",
+ g_variant_new_string (lang_id));
}
static void
@@ -897,6 +933,7 @@ gb_editor_view_class_init (GbEditorViewClass *klass)
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, progress_bar);
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, split_button);
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, tweak_button);
+ GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, tweak_widget);
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, modified_revealer);
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, modified_label);
GB_WIDGET_CLASS_BIND (widget_class, GbEditorView, modified_cancel_button);
@@ -916,6 +953,7 @@ gb_editor_view_init (GbEditorView *self)
{ "auto-indent", NULL, NULL, "false", apply_state_auto_indent },
{ "highlight-current-line", NULL, NULL, "false",
apply_state_highlight_current_line },
+ { "language", NULL, "s", "''", apply_state_language },
{ "show-line-numbers", NULL, NULL, "false", apply_state_show_line_numbers },
{ "show-right-margin", NULL, NULL, "false", apply_state_show_right_margin },
{ "switch-pane", gb_editor_view_switch_pane },
@@ -930,15 +968,20 @@ gb_editor_view_init (GbEditorView *self)
gtk_widget_init_template (GTK_WIDGET (self));
actions = g_simple_action_group_new ();
+
+ /*
+ * Unfortunately, we need to manually attach the action group in
+ * a few places due to the complexity of how they are handled.
+ */
g_action_map_add_action_entries (G_ACTION_MAP (actions), entries,
G_N_ELEMENTS (entries), self);
-
gtk_widget_insert_action_group (GTK_WIDGET (self), "editor-view",
G_ACTION_GROUP (actions));
-
controls = gb_document_view_get_controls (GB_DOCUMENT_VIEW (self));
gtk_widget_insert_action_group (GTK_WIDGET (controls), "editor-view",
G_ACTION_GROUP (actions));
+ gtk_widget_insert_action_group (GTK_WIDGET (self->priv->tweak_widget),
+ "editor-view", G_ACTION_GROUP (actions));
g_clear_object (&actions);
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]