[gnome-builder] commands: refactor editor commands into gb-editor-commands.c



commit 7a913994b58d0c7972c87d1273b9396d3db85453
Author: Christian Hergert <christian hergert me>
Date:   Thu Sep 11 23:29:17 2014 -0700

    commands: refactor editor commands into gb-editor-commands.c
    
    This should make it easier so we don't need to concern ourselves so much
    with whether we are in the tab or workspace file.

 src/editor/gb-editor-commands.c          |  299 +++++++++++++++++++++++++++++-
 src/editor/gb-editor-commands.h          |    4 +-
 src/editor/gb-editor-tab.c               |   75 --------
 src/editor/gb-editor-tab.h               |    1 -
 src/editor/gb-editor-workspace-private.h |   39 ++++
 src/editor/gb-editor-workspace.c         |  303 ++----------------------------
 6 files changed, 354 insertions(+), 367 deletions(-)
---
diff --git a/src/editor/gb-editor-commands.c b/src/editor/gb-editor-commands.c
index 6ad0f5b..6204c82 100644
--- a/src/editor/gb-editor-commands.c
+++ b/src/editor/gb-editor-commands.c
@@ -16,12 +16,28 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define G_LOG_DOMAIN "editor-commands"
+
+#include <glib/gi18n.h>
+
 #include "gb-editor-commands.h"
 #include "gb-editor-tab.h"
 #include "gb-editor-tab-private.h"
+#include "gb-editor-workspace.h"
+#include "gb-editor-workspace-private.h"
 #include "gb-log.h"
 #include "gb-source-formatter.h"
 
+typedef void (*GbEditorCommand) (GbEditorWorkspace *workspace,
+                                 GbEditorTab       *tab);
+
+typedef struct
+{
+  const gchar     *name;
+  GbEditorCommand  command;
+  gboolean         requires_tab;
+} GbEditorCommandsEntry;
+
 /**
  * gb_editor_commands_reformat:
  * @tab: A #GbEditorTab.
@@ -36,7 +52,8 @@
  *      transforms, useful for FixIt's too?
  */
 void
-gb_editor_commands_reformat (GbEditorTab *tab)
+gb_editor_commands_reformat (GbEditorWorkspace *workspace,
+                             GbEditorTab       *tab)
 {
   GbEditorTabPrivate *priv;
   GbSourceFormatter *formatter;
@@ -132,3 +149,283 @@ cleanup:
 
   EXIT;
 }
+
+/**
+ * gb_editor_commands_go_to_start:
+ * @tab: A #GbEditorTab.
+ *
+ * Move the insertion cursor to the beginning of the document.
+ * Scroll the view appropriately so that the cursor is visible.
+ */
+void
+gb_editor_commands_go_to_start (GbEditorWorkspace *workspace,
+                                GbEditorTab       *tab)
+{
+  GbEditorTabPrivate *priv;
+  GtkTextIter begin;
+  GtkTextIter end;
+
+  ENTRY;
+
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  priv = tab->priv;
+
+  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (priv->document), &begin, &end);
+  gtk_text_buffer_select_range (GTK_TEXT_BUFFER (priv->document),
+                                &begin, &begin);
+  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &begin,
+                                0.25, TRUE, 0.5, 0.5);
+
+  EXIT;
+}
+
+/**
+ * gb_editor_commands_go_to_end:
+ * @tab: A #GbEditorTab.
+ *
+ * Move the insertion cursor to the end of the document.
+ * Scroll the view appropriately so that the cursor is visible.
+ */
+void
+gb_editor_commands_go_to_end (GbEditorWorkspace *workspace,
+                              GbEditorTab       *tab)
+{
+  GbEditorTabPrivate *priv;
+  GtkTextIter begin;
+  GtkTextIter end;
+
+  ENTRY;
+
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  priv = tab->priv;
+
+  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (priv->document), &begin, &end);
+  gtk_text_buffer_select_range (GTK_TEXT_BUFFER (priv->document), &end, &end);
+  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &end,
+                                0.25, TRUE, 0.5, 0.5);
+
+  EXIT;
+}
+
+void
+gb_editor_commands_find (GbEditorWorkspace *workspace,
+                         GbEditorTab       *tab)
+{
+  GbEditorTabPrivate *priv;
+
+  ENTRY;
+
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  priv = tab->priv;
+
+  gtk_revealer_set_reveal_child (priv->revealer, TRUE);
+  gtk_source_search_context_set_highlight (priv->search_context, TRUE);
+  gtk_widget_grab_focus (GTK_WIDGET (priv->search_entry));
+
+  EXIT;
+}
+
+void
+gb_editor_commands_close_tab (GbEditorWorkspace *workspace,
+                              GbEditorTab       *tab)
+{
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  gb_tab_close (GB_TAB (tab));
+}
+
+void
+gb_editor_commands_save (GbEditorWorkspace *workspace,
+                         GbEditorTab       *tab)
+{
+}
+
+void
+gb_editor_commands_save_as (GbEditorWorkspace *workspace,
+                            GbEditorTab       *tab)
+{
+}
+
+static void
+gb_editor_commands_open (GbEditorWorkspace *workspace,
+                         GbEditorTab       *tab)
+{
+  GbEditorWorkspacePrivate *priv;
+  GtkFileChooserDialog *dialog;
+  GtkWidget *toplevel;
+  GtkWidget *suggested;
+  GtkResponseType response;
+  GbNotebook *notebook;
+  GbTab *active_tab;
+
+  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
+  g_return_if_fail (!tab || GB_IS_EDITOR_TAB (tab));
+
+  priv = workspace->priv;
+
+  active_tab = gb_multi_notebook_get_active_tab (priv->multi_notebook);
+  notebook = gb_multi_notebook_get_active_notebook (priv->multi_notebook);
+
+  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (workspace));
+
+  dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
+                         "action", GTK_FILE_CHOOSER_ACTION_OPEN,
+                         "local-only", FALSE,
+                         "select-multiple", TRUE,
+                         "show-hidden", FALSE,
+                         "transient-for", toplevel,
+                         "title", _("Open"),
+                         NULL);
+
+  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                          _("Cancel"), GTK_RESPONSE_CANCEL,
+                          _("Open"), GTK_RESPONSE_OK,
+                          NULL);
+
+  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
+
+  suggested = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog),
+                                                  GTK_RESPONSE_OK);
+  gtk_style_context_add_class (gtk_widget_get_style_context (suggested),
+                               GTK_STYLE_CLASS_SUGGESTED_ACTION);
+
+  response = gtk_dialog_run (GTK_DIALOG (dialog));
+
+  if (response == GTK_RESPONSE_OK)
+    {
+      GSList *files;
+      GSList *iter;
+
+      files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (dialog));
+
+      for (iter = files; iter; iter = iter->next)
+        {
+          GFile *file = iter->data;
+
+          if (!gb_editor_tab_get_is_default (GB_EDITOR_TAB (active_tab)))
+            {
+              tab = GB_EDITOR_TAB (gb_editor_tab_new ());
+              gb_notebook_add_tab (notebook, GB_TAB (tab));
+              gtk_widget_show (GTK_WIDGET (tab));
+            }
+
+          gb_editor_tab_open_file (tab, file);
+          gb_notebook_raise_tab (notebook, GB_TAB (tab));
+
+          g_clear_object (&file);
+        }
+
+      g_slist_free (files);
+    }
+
+  gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+static void
+gb_editor_commands_new_tab (GbEditorWorkspace *workspace,
+                            GbEditorTab       *tab)
+{
+  GbEditorWorkspacePrivate *priv;
+  GbNotebook *notebook;
+  gint page;
+
+  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
+
+  priv = workspace->priv;
+
+  notebook = gb_multi_notebook_get_active_notebook (priv->multi_notebook);
+
+  tab = g_object_new (GB_TYPE_EDITOR_TAB,
+                      "visible", TRUE,
+                      NULL);
+  gb_notebook_add_tab (notebook, GB_TAB (tab));
+
+  gtk_container_child_get (GTK_CONTAINER (notebook), GTK_WIDGET (tab),
+                           "position", &page,
+                           NULL);
+  gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page);
+}
+
+static void
+gb_editor_commands_preview (GbEditorWorkspace *workspace,
+                            GbEditorTab       *tab)
+{
+  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
+  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
+
+  gb_editor_tab_toggle_preview (tab);
+}
+
+
+static void
+gb_editor_commands_activate (GSimpleAction *action,
+                             GVariant      *variant,
+                             gpointer       user_data)
+{
+  GbEditorCommandsEntry *command;
+  GbEditorWorkspace *workspace = user_data;
+  const gchar *name;
+  GHashTable *hash;
+  GbTab *tab;
+
+  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
+  g_return_if_fail (G_IS_SIMPLE_ACTION (action));
+
+  name = g_action_get_name (G_ACTION (action));
+  if (!name)
+    return;
+
+  hash = workspace->priv->command_map;
+  if (!hash)
+    return;
+
+  command = g_hash_table_lookup (hash, name);
+  if (!command)
+    return;
+
+  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
+  if (!tab && command->requires_tab)
+    return;
+
+  if (tab)
+    command->command (workspace, GB_EDITOR_TAB (tab));
+  else
+    command->command (workspace, NULL);
+}
+
+void
+gb_editor_commands_init (GbEditorWorkspace *workspace)
+{
+  static const GbEditorCommandsEntry commands[] = {
+    { "close-tab",   gb_editor_commands_close_tab,   TRUE },
+    { "find",        gb_editor_commands_find,        TRUE },
+    { "go-to-start", gb_editor_commands_go_to_start, TRUE },
+    { "go-to-end",   gb_editor_commands_go_to_end,   TRUE },
+    { "new-tab",     gb_editor_commands_new_tab,     FALSE },
+    { "open",        gb_editor_commands_open,        FALSE },
+    { "preview",     gb_editor_commands_preview,     TRUE },
+    { "reformat",    gb_editor_commands_reformat,    TRUE },
+    { "save",        gb_editor_commands_save,        TRUE },
+    { "save-as",     gb_editor_commands_save_as,     TRUE },
+    { NULL }
+  };
+  guint i;
+
+  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
+  g_return_if_fail (workspace->priv->command_map);
+  g_return_if_fail (G_IS_SIMPLE_ACTION_GROUP (workspace->priv->actions));
+
+  for (i = 0; commands [i].name; i++)
+    {
+      GActionEntry entry = { commands [i].name,
+                             gb_editor_commands_activate };
+      g_hash_table_insert (workspace->priv->command_map,
+                           (gchar *)commands [i].name,
+                           (gpointer)&commands [i]);
+      g_action_map_add_action_entries (G_ACTION_MAP (workspace->priv->actions),
+                                       &entry, 1, workspace);
+    }
+}
diff --git a/src/editor/gb-editor-commands.h b/src/editor/gb-editor-commands.h
index 25d923e..3a8c3ff 100644
--- a/src/editor/gb-editor-commands.h
+++ b/src/editor/gb-editor-commands.h
@@ -19,11 +19,11 @@
 #ifndef GB_EDITOR_COMMANDS_H
 #define GB_EDITOR_COMMANDS_H
 
-#include "gb-editor-tab.h"
+#include "gb-editor-workspace.h"
 
 G_BEGIN_DECLS
 
-void gb_editor_commands_reformat (GbEditorTab *tab);
+void gb_editor_commands_init (GbEditorWorkspace *workspace);
 
 G_END_DECLS
 
diff --git a/src/editor/gb-editor-tab.c b/src/editor/gb-editor-tab.c
index 4813864..f3d0e5b 100644
--- a/src/editor/gb-editor-tab.c
+++ b/src/editor/gb-editor-tab.c
@@ -119,81 +119,6 @@ gb_editor_tab_get_file (GbEditorTab *tab)
   return tab->priv->file;
 }
 
-/**
- * gb_editor_tab_go_to_start:
- * @tab: A #GbEditorTab.
- *
- * Move the insertion cursor to the beginning of the document.
- * Scroll the view appropriately so that the cursor is visible.
- */
-void
-gb_editor_tab_go_to_start (GbEditorTab *tab)
-{
-  GbEditorTabPrivate *priv;
-  GtkTextIter begin;
-  GtkTextIter end;
-
-  ENTRY;
-
-  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
-
-  priv = tab->priv;
-
-  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (priv->document), &begin, &end);
-  gtk_text_buffer_select_range (GTK_TEXT_BUFFER (priv->document),
-                                &begin, &begin);
-  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &begin,
-                                0.25, TRUE, 0.5, 0.5);
-
-  EXIT;
-}
-
-/**
- * gb_editor_tab_go_to_end:
- * @tab: A #GbEditorTab.
- *
- * Move the insertion cursor to the end of the document.
- * Scroll the view appropriately so that the cursor is visible.
- */
-void
-gb_editor_tab_go_to_end (GbEditorTab *tab)
-{
-  GbEditorTabPrivate *priv;
-  GtkTextIter begin;
-  GtkTextIter end;
-
-  ENTRY;
-
-  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
-
-  priv = tab->priv;
-
-  gtk_text_buffer_get_bounds (GTK_TEXT_BUFFER (priv->document), &begin, &end);
-  gtk_text_buffer_select_range (GTK_TEXT_BUFFER (priv->document), &end, &end);
-  gtk_text_view_scroll_to_iter (GTK_TEXT_VIEW (priv->source_view), &end,
-                                0.25, TRUE, 0.5, 0.5);
-
-  EXIT;
-}
-
-void
-gb_editor_tab_focus_search (GbEditorTab *tab)
-{
-  GbEditorTabPrivate *priv;
-
-  ENTRY;
-
-  g_return_if_fail (GB_IS_EDITOR_TAB (tab));
-
-  priv = tab->priv;
-
-  gtk_revealer_set_reveal_child (priv->revealer, TRUE);
-  gtk_source_search_context_set_highlight (priv->search_context, TRUE);
-  gtk_widget_grab_focus (GTK_WIDGET (priv->search_entry));
-
-  EXIT;
-}
-
 void
 gb_editor_tab_toggle_preview (GbEditorTab *tab)
 {
diff --git a/src/editor/gb-editor-tab.h b/src/editor/gb-editor-tab.h
index 1af6b23..9049f06 100644
--- a/src/editor/gb-editor-tab.h
+++ b/src/editor/gb-editor-tab.h
@@ -61,7 +61,6 @@ void              gb_editor_tab_set_font_desc  (GbEditorTab                *tab,
                                                 const PangoFontDescription *font_desc);
 void              gb_editor_tab_focus_search   (GbEditorTab                *tab);
 gboolean          gb_editor_tab_get_is_default (GbEditorTab                *tab);
-void              gb_editor_tab_reformat       (GbEditorTab                *tab);
 void              gb_editor_tab_go_to_end      (GbEditorTab                *tab);
 void              gb_editor_tab_go_to_start    (GbEditorTab                *tab);
 void              gb_editor_tab_save_as        (GbEditorTab                *tab);
diff --git a/src/editor/gb-editor-workspace-private.h b/src/editor/gb-editor-workspace-private.h
new file mode 100644
index 0000000..e159e5e
--- /dev/null
+++ b/src/editor/gb-editor-workspace-private.h
@@ -0,0 +1,39 @@
+/* gb-editor-workspace-private.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_EDITOR_WORKSPACE_PRIVATE_H
+#define GB_EDITOR_WORKSPACE_PRIVATE_H
+
+#include <gtk/gtk.h>
+
+#include "gb-editor-tab.h"
+#include "gb-multi-notebook.h"
+#include "gb-notebook.h"
+
+G_BEGIN_DECLS
+
+struct _GbEditorWorkspacePrivate
+{
+  GSimpleActionGroup *actions;
+  GHashTable         *command_map;
+  GbMultiNotebook    *multi_notebook;
+};
+
+G_END_DECLS
+
+#endif /* GB_EDITOR_WORKSPACE_PRIVATE_H */
diff --git a/src/editor/gb-editor-workspace.c b/src/editor/gb-editor-workspace.c
index 71b4ed0..6ac903f 100644
--- a/src/editor/gb-editor-workspace.c
+++ b/src/editor/gb-editor-workspace.c
@@ -19,16 +19,8 @@
 #include <glib/gi18n.h>
 
 #include "gb-editor-commands.h"
-#include "gb-editor-tab.h"
 #include "gb-editor-workspace.h"
-#include "gb-multi-notebook.h"
-#include "gb-notebook.h"
-
-struct _GbEditorWorkspacePrivate
-{
-  GSimpleActionGroup *actions;
-  GbMultiNotebook    *multi_notebook;
-};
+#include "gb-editor-workspace-private.h"
 
 enum {
   PROP_0,
@@ -46,252 +38,6 @@ gb_editor_workspace_get_actions (GbWorkspace * workspace)
 }
 
 static void
-on_new_tab_activate (GSimpleAction *action,
-                     GVariant      *parameter,
-                     gpointer       user_data)
-{
-  GbEditorWorkspacePrivate *priv;
-  GbEditorWorkspace *workspace = user_data;
-  GbNotebook *notebook;
-  GbTab *tab;
-  gint page;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  priv = workspace->priv;
-
-  notebook = gb_multi_notebook_get_active_notebook (priv->multi_notebook);
-  tab = g_object_new (GB_TYPE_EDITOR_TAB,
-                      "visible", TRUE,
-                      NULL);
-  gb_notebook_add_tab (notebook, tab);
-
-  gtk_container_child_get (GTK_CONTAINER (notebook), GTK_WIDGET (tab),
-                           "position", &page,
-                           NULL);
-  gtk_notebook_set_current_page (GTK_NOTEBOOK (notebook), page);
-}
-
-static void
-on_close_tab_activate (GSimpleAction *action,
-                       GVariant      *parameter,
-                       gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-  if (tab)
-    gb_tab_close (tab);
-}
-
-static void
-on_find_activate (GSimpleAction *action,
-                  GVariant      *parameter,
-                  gpointer       user_data)
-{
-  GbEditorWorkspacePrivate *priv;
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  priv = workspace->priv;
-
-  tab = gb_multi_notebook_get_active_tab (priv->multi_notebook);
-  if (tab)
-    gb_editor_tab_focus_search (GB_EDITOR_TAB (tab));
-}
-
-static void
-on_reformat_activate (GSimpleAction *action,
-                      GVariant      *parameter,
-                      gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    gb_editor_commands_reformat (GB_EDITOR_TAB (tab));
-}
-
-static void
-on_open_activate (GSimpleAction *action,
-                  GVariant      *parameter,
-                  gpointer       user_data)
-{
-  GbEditorWorkspacePrivate *priv;
-  GbEditorWorkspace *workspace = user_data;
-  GtkFileChooserDialog *dialog;
-  GtkWidget *toplevel;
-  GtkWidget *suggested;
-  GtkResponseType response;
-  GbNotebook *notebook;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  priv = workspace->priv;
-
-  tab = gb_multi_notebook_get_active_tab (priv->multi_notebook);
-  notebook = gb_multi_notebook_get_active_notebook (priv->multi_notebook);
-
-  g_assert (!tab || GB_IS_EDITOR_TAB (tab));
-
-  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (tab));
-
-  dialog = g_object_new (GTK_TYPE_FILE_CHOOSER_DIALOG,
-                         "action", GTK_FILE_CHOOSER_ACTION_OPEN,
-                         "local-only", FALSE,
-                         "select-multiple", TRUE,
-                         "show-hidden", FALSE,
-                         "transient-for", toplevel,
-                         "title", _("Open"),
-                         NULL);
-
-  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
-                          _("Cancel"), GTK_RESPONSE_CANCEL,
-                          _("Open"), GTK_RESPONSE_OK,
-                          NULL);
-
-  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
-
-  suggested = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog),
-                                                  GTK_RESPONSE_OK);
-  gtk_style_context_add_class (gtk_widget_get_style_context (suggested),
-                               GTK_STYLE_CLASS_SUGGESTED_ACTION);
-
-  response = gtk_dialog_run (GTK_DIALOG (dialog));
-
-  if (response == GTK_RESPONSE_OK)
-    {
-      GSList *files;
-      GSList *iter;
-
-      files = gtk_file_chooser_get_files (GTK_FILE_CHOOSER (dialog));
-
-      for (iter = files; iter; iter = iter->next)
-        {
-          GFile *file = iter->data;
-
-          if (!gb_editor_tab_get_is_default (GB_EDITOR_TAB (tab)))
-            {
-              tab = GB_TAB (gb_editor_tab_new ());
-              gb_notebook_add_tab (notebook, tab);
-              gtk_widget_show (GTK_WIDGET (tab));
-            }
-
-          gb_editor_tab_open_file (GB_EDITOR_TAB (tab), file);
-          gb_notebook_raise_tab (notebook, tab);
-
-          g_clear_object (&file);
-        }
-
-      g_slist_free (files);
-    }
-
-  gtk_widget_destroy (GTK_WIDGET (dialog));
-}
-
-static void
-on_save_activate (GSimpleAction *action,
-                  GVariant      *parameter,
-                  gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    {
-      g_assert (GB_IS_EDITOR_TAB (tab));
-      gb_editor_tab_save (GB_EDITOR_TAB (tab));
-    }
-}
-
-static void
-on_save_as_activate (GSimpleAction *action,
-                     GVariant      *parameter,
-                     gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    {
-      g_assert (GB_IS_EDITOR_TAB (tab));
-      gb_editor_tab_save_as (GB_EDITOR_TAB (tab));
-    }
-}
-
-static void
-on_go_to_start_activate (GSimpleAction *action,
-                         GVariant      *parameter,
-                         gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    {
-      g_assert (GB_IS_EDITOR_TAB (tab));
-      gb_editor_tab_go_to_start (GB_EDITOR_TAB (tab));
-    }
-}
-
-static void
-on_go_to_end_activate (GSimpleAction *action,
-                       GVariant      *parameter,
-                       gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    {
-      g_assert (GB_IS_EDITOR_TAB (tab));
-      gb_editor_tab_go_to_end (GB_EDITOR_TAB (tab));
-    }
-}
-
-static void
-on_preview_activate (GSimpleAction *action,
-                     GVariant      *variant,
-                     gpointer       user_data)
-{
-  GbEditorWorkspace *workspace = user_data;
-  GbTab *tab;
-
-  g_return_if_fail (GB_IS_EDITOR_WORKSPACE (workspace));
-
-  tab = gb_multi_notebook_get_active_tab (workspace->priv->multi_notebook);
-
-  if (tab)
-    gb_editor_tab_toggle_preview (GB_EDITOR_TAB (tab));
-}
-
-static void
 gb_editor_workspace_grab_focus (GtkWidget *widget)
 {
   GbEditorWorkspace *workspace = GB_EDITOR_WORKSPACE (widget);
@@ -311,6 +57,7 @@ gb_editor_workspace_finalize (GObject *object)
   GbEditorWorkspacePrivate *priv = GB_EDITOR_WORKSPACE (object)->priv;
 
   g_clear_object (&priv->actions);
+  g_clear_pointer (&priv->command_map, g_hash_table_unref);
 
   G_OBJECT_CLASS (gb_editor_workspace_parent_class)->finalize (object);
 }
@@ -332,41 +79,21 @@ gb_editor_workspace_class_init (GbEditorWorkspaceClass *klass)
 static void
 gb_editor_workspace_init (GbEditorWorkspace *workspace)
 {
-  static const GActionEntry action_entries[] = {
-    { "close-tab", on_close_tab_activate },
-    { "find", on_find_activate },
-    { "go-to-end", on_go_to_end_activate },
-    { "go-to-start", on_go_to_start_activate },
-    { "new-tab", on_new_tab_activate },
-    { "open", on_open_activate },
-    { "reformat", on_reformat_activate },
-    { "save", on_save_activate },
-    { "save-as", on_save_as_activate },
-    { "preview", on_preview_activate },
-  };
-  GbEditorWorkspacePrivate *priv;
-  GbNotebook *notebook;
-  GbTab *tab;
-
-  priv = workspace->priv = gb_editor_workspace_get_instance_private (workspace);
+  workspace->priv = gb_editor_workspace_get_instance_private (workspace);
 
-  priv->actions = g_simple_action_group_new ();
-  g_action_map_add_action_entries (G_ACTION_MAP (priv->actions),
-                                   action_entries,
-                                   G_N_ELEMENTS (action_entries),
-                                   workspace);
+  workspace->priv->actions = g_simple_action_group_new ();
+  workspace->priv->command_map = g_hash_table_new (g_str_hash, g_str_equal);
 
-  priv->multi_notebook = g_object_new (GB_TYPE_MULTI_NOTEBOOK,
-                                       "visible", TRUE,
-                                       "group-name", "GB_EDITOR_WORKSPACE",
-                                       NULL);
+  /*
+   * TODO: make this be done with GtkBuilder.
+   */
+  workspace->priv->multi_notebook =
+      g_object_new (GB_TYPE_MULTI_NOTEBOOK,
+                    "visible", TRUE,
+                    "group-name", "GB_EDITOR_WORKSPACE",
+                    NULL);
   gtk_container_add (GTK_CONTAINER (workspace),
-                     GTK_WIDGET (priv->multi_notebook));
-
-  notebook = gb_multi_notebook_get_active_notebook (priv->multi_notebook);
+                     GTK_WIDGET (workspace->priv->multi_notebook));
 
-  tab = g_object_new (GB_TYPE_EDITOR_TAB,
-                      "visible", TRUE,
-                      NULL);
-  gb_notebook_add_tab (notebook, tab);
+  gb_editor_commands_init (workspace);
 }


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