[gnome-builder] editor: add context menu support for setting language highlight mode.



commit 4afb50338576e3cdae09abf6d1d16567cf3b7bc2
Author: Christian Hergert <christian hergert me>
Date:   Thu Sep 25 02:48:19 2014 -0700

    editor: add context menu support for setting language highlight mode.

 src/editor/gb-editor-commands.c       |   41 ++++++++++++++++
 src/editor/gb-editor-tab.c            |   33 +++++++++++++-
 src/editor/gb-source-highlight-menu.c |   83 +++++++++++++++++++++++++++++++++
 src/editor/gb-source-highlight-menu.h |   30 ++++++++++++
 src/gnome-builder.mk                  |    2 +
 5 files changed, 188 insertions(+), 1 deletions(-)
---
diff --git a/src/editor/gb-editor-commands.c b/src/editor/gb-editor-commands.c
index de336fa..f6a8784 100644
--- a/src/editor/gb-editor-commands.c
+++ b/src/editor/gb-editor-commands.c
@@ -917,6 +917,37 @@ gb_editor_commands_scroll_up (GbEditorWorkspace *workspace,
 }
 
 static void
+gb_editor_commands_highlight_mode (GSimpleAction     *action,
+                                   GVariant          *parameter,
+                                   GbEditorWorkspace *workspace)
+{
+  GbTab *tab;
+
+  g_assert (GB_IS_EDITOR_WORKSPACE (workspace));
+
+  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
+
+  if (GB_IS_TAB (tab))
+    {
+      const gchar *name;
+      gsize len = 0;
+
+      if ((name = g_variant_get_string (parameter, &len)))
+        {
+          GtkSourceLanguageManager *lm;
+          GtkSourceLanguage *l;
+
+          lm = gtk_source_language_manager_get_default ();
+          l = gtk_source_language_manager_get_language (lm, name);
+
+          if (l)
+            gtk_source_buffer_set_language (
+                GTK_SOURCE_BUFFER (GB_EDITOR_TAB (tab)->priv->document), l);
+        }
+    }
+}
+
+static void
 gb_editor_commands_activate (GSimpleAction *action,
                              GVariant      *variant,
                              gpointer       user_data)
@@ -972,6 +1003,7 @@ gb_editor_commands_init (GbEditorWorkspace *workspace)
     { "scroll-up",           gb_editor_commands_scroll_up,           TRUE },
     { NULL }
   };
+  GSimpleAction *action;
   guint i;
 
   g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
@@ -988,4 +1020,13 @@ gb_editor_commands_init (GbEditorWorkspace *workspace)
       g_action_map_add_action_entries (G_ACTION_MAP (workspace->priv->actions),
                                        &entry, 1, workspace);
     }
+
+  action = g_simple_action_new ("highlight-mode", G_VARIANT_TYPE_STRING);
+  g_signal_connect (action,
+                    "activate",
+                    G_CALLBACK (gb_editor_commands_highlight_mode),
+                    workspace);
+  g_action_map_add_action (G_ACTION_MAP (workspace->priv->actions),
+                           G_ACTION (action));
+  g_object_unref (action);
 }
diff --git a/src/editor/gb-editor-tab.c b/src/editor/gb-editor-tab.c
index adc01aa..631fa9c 100644
--- a/src/editor/gb-editor-tab.c
+++ b/src/editor/gb-editor-tab.c
@@ -25,11 +25,13 @@
 #include "gb-log.h"
 #include "gb-rgba.h"
 #include "gb-source-change-gutter-renderer.h"
+#include "gb-source-highlight-menu.h"
 #include "gb-source-snippet.h"
 #include "gb-source-snippets-manager.h"
 #include "gb-source-snippets.h"
 #include "gb-string.h"
 #include "gb-widget.h"
+#include "gb-workbench.h"
 
 #define GB_EDITOR_TAB_UI_RESOURCE "/org/gnome/builder/ui/gb-editor-tab.ui"
 
@@ -791,6 +793,9 @@ on_source_view_populate_popup (GtkTextView *text_view,
                                GtkWidget   *popup,
                                GbEditorTab *tab)
 {
+  GbWorkbench *workbench;
+  GbWorkspace *workspace;
+
   g_return_if_fail (GB_IS_SOURCE_VIEW (text_view));
   g_return_if_fail (GB_IS_EDITOR_TAB (tab));
 
@@ -798,6 +803,13 @@ on_source_view_populate_popup (GtkTextView *text_view,
     {
       PangoFontDescription *font = NULL;
       GtkStyleContext *context;
+      GMenuModel *model;
+      GtkWidget *menu_item;
+      GtkWidget *menu;
+      GtkWidget *separator;
+
+      workbench = GB_WORKBENCH (gtk_widget_get_toplevel (GTK_WIDGET (text_view)));
+      workspace = gb_workbench_get_active_workspace (workbench);
 
       /*
        * WORKAROUND:
@@ -821,8 +833,27 @@ on_source_view_populate_popup (GtkTextView *text_view,
 
       /*
        * TODO: Add menu for controlling font size.
-       *       Add menu for controlling highlight.
        */
+
+      /*
+       * Add separator.
+       */
+      separator = gtk_separator_menu_item_new ();
+      gtk_menu_shell_prepend (GTK_MENU_SHELL (popup), GTK_WIDGET (separator));
+      gtk_widget_show (separator);
+
+      /*
+       * Add menu for highlight mode.
+       */
+      model = gb_source_highlight_menu_new ();
+      menu = gtk_menu_new_from_model (model);
+      menu_item = gtk_menu_item_new_with_label (_("Highlight Mode"));
+      gtk_menu_item_set_submenu (GTK_MENU_ITEM (menu_item), menu);
+      gtk_menu_shell_prepend (GTK_MENU_SHELL (popup), GTK_WIDGET (menu_item));
+      gtk_widget_insert_action_group (GTK_WIDGET (menu_item), "editor",
+                                      gb_workspace_get_actions (workspace));
+      gtk_widget_show (GTK_WIDGET (menu_item));
+      g_object_unref (model);
     }
 }
 
diff --git a/src/editor/gb-source-highlight-menu.c b/src/editor/gb-source-highlight-menu.c
new file mode 100644
index 0000000..a865caa
--- /dev/null
+++ b/src/editor/gb-source-highlight-menu.c
@@ -0,0 +1,83 @@
+/* gb-source-highlight-menu.c
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#include <gtksourceview/gtksource.h>
+
+#include "gb-source-highlight-menu.h"
+
+GMenuModel *
+gb_source_highlight_menu_new (void)
+{
+  GtkSourceLanguageManager *manager;
+  const gchar * const *lang_ids;
+  GHashTable *groups;
+  GHashTableIter iter;
+  const gchar *key;
+  GMenu *value;
+  GMenu *top_menu;
+  guint i;
+
+  manager = gtk_source_language_manager_get_default ();
+  lang_ids = gtk_source_language_manager_get_language_ids (manager);
+
+  groups = g_hash_table_new (g_str_hash, g_str_equal);
+
+  for (i = 0; lang_ids [i]; i++)
+    {
+      GtkSourceLanguage *lang;
+      const gchar *section;
+      const gchar *name;
+      GMenuItem *item;
+      GMenu *menu;
+      gchar *detailed;
+
+      lang = gtk_source_language_manager_get_language (manager, lang_ids [i]);
+
+      section = gtk_source_language_get_section (lang);
+      name = gtk_source_language_get_name (lang);
+      menu = g_hash_table_lookup (groups, section);
+
+      if (!menu)
+        {
+          menu = g_menu_new ();
+          g_hash_table_insert (groups, (gchar *)section, menu);
+        }
+
+      detailed = g_strdup_printf ("editor.highlight-mode('%s')", lang_ids [i]);
+      item = g_menu_item_new (name, detailed);
+      g_menu_append_item (menu, item);
+      g_free (detailed);
+    }
+
+  g_hash_table_iter_init (&iter, groups);
+
+  top_menu = g_menu_new ();
+
+  while (g_hash_table_iter_next (&iter, (gpointer *)&key, (gpointer *)&value))
+    {
+      GMenuItem *item;
+
+      item = g_menu_item_new (key, NULL);
+      g_menu_item_set_submenu (item, G_MENU_MODEL (value));
+      g_menu_append_item (top_menu, item);
+    }
+
+  g_hash_table_unref (groups);
+
+  return G_MENU_MODEL (top_menu);
+}
diff --git a/src/editor/gb-source-highlight-menu.h b/src/editor/gb-source-highlight-menu.h
new file mode 100644
index 0000000..78e6d2c
--- /dev/null
+++ b/src/editor/gb-source-highlight-menu.h
@@ -0,0 +1,30 @@
+/* gb-source-highlight-menu.h
+ *
+ * Copyright (C) 2014 Christian Hergert <christian hergert me>
+ *
+ * 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/>.
+ */
+
+#ifndef GB_SOURCE_HIGHLIGHT_MENU_H
+#define GB_SOURCE_HIGHLIGHT_MENU_H
+
+#include <gio/gio.h>
+
+G_BEGIN_DECLS
+
+GMenuModel *gb_source_highlight_menu_new (void);
+
+G_END_DECLS
+
+#endif /* GB_SOURCE_HIGHLIGHT_MENU_H */
diff --git a/src/gnome-builder.mk b/src/gnome-builder.mk
index 36e8d7a..3fc3f49 100644
--- a/src/gnome-builder.mk
+++ b/src/gnome-builder.mk
@@ -41,6 +41,8 @@ libgnome_builder_la_SOURCES = \
        src/editor/gb-source-formatter.h \
        src/editor/gb-source-change-gutter-renderer.c \
        src/editor/gb-source-change-gutter-renderer.h \
+       src/editor/gb-source-highlight-menu.c \
+       src/editor/gb-source-highlight-menu.h \
        src/editor/gb-source-search-highlighter.h \
        src/editor/gb-source-search-highlighter.c \
        src/markdown/gs-markdown.c \


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