[anjuta] git: Add a context menu to the Branches pane



commit f77de6feec7414b18365697248f204f9b2fd8d0a
Author: James Liggett <jrliggett cox net>
Date:   Sat Jun 1 18:33:22 2013 -0700

    git: Add a context menu to the Branches pane

 plugins/git/anjuta-git.xml             |    6 +++
 plugins/git/git-branches-pane.c        |   60 ++++++++++++++++++++++++++++++++
 plugins/git/git-branches-pane.h        |    1 +
 plugins/git/git-delete-branches-pane.c |   35 ++++++++++++++++++
 plugins/git/git-delete-branches-pane.h |    5 ++-
 plugins/git/git-merge-pane.c           |   40 +++++++++++++++++++--
 plugins/git/git-merge-pane.h           |    4 ++
 plugins/git/plugin.c                   |   35 ++++++++++++++++++
 plugins/git/plugin.h                   |    1 +
 9 files changed, 182 insertions(+), 5 deletions(-)
---
diff --git a/plugins/git/anjuta-git.xml b/plugins/git/anjuta-git.xml
index a960c7e..1b20467 100644
--- a/plugins/git/anjuta-git.xml
+++ b/plugins/git/anjuta-git.xml
@@ -15,4 +15,10 @@
                <menuitem name="Revert" action="GitLogRevert" />
                <menuitem name="Reset..." action="GitLogReset" />
        </popup>
+
+       <popup name="GitBranchPopup">
+               <menuitem name="Switch" action="GitBranchSwitch" />
+               <menuitem name="Delete..." action="GitBranchDelete" />
+               <menuitem name="Merge..." action="GitBranchMerge" />
+       </popup>
 </ui>
\ No newline at end of file
diff --git a/plugins/git/git-branches-pane.c b/plugins/git/git-branches-pane.c
index 1f3bd8e..7b512c0 100644
--- a/plugins/git/git-branches-pane.c
+++ b/plugins/git/git-branches-pane.c
@@ -295,6 +295,35 @@ on_branches_view_row_activated (GtkTreeView *branches_view, GtkTreePath *path,
        anjuta_command_start (ANJUTA_COMMAND (checkout_command));
 }
 
+static gboolean
+on_branches_view_button_press_event (GtkWidget *branches_view, 
+                                     GdkEventButton *event,
+                                     GitBranchesPane *self)
+{
+       GtkTreeSelection *selection;
+       AnjutaPlugin *plugin;
+       AnjutaUI *ui;
+       GtkWidget *menu;
+
+       if (event->type == GDK_BUTTON_PRESS && event->button == 3)
+       {
+               selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (branches_view));
+
+               if (gtk_tree_selection_count_selected_rows (selection) > 0)
+               {
+                       plugin = anjuta_dock_pane_get_plugin (ANJUTA_DOCK_PANE (self));
+                       ui = anjuta_shell_get_ui (plugin->shell, NULL);
+                       menu = GTK_MENU (gtk_ui_manager_get_widget (GTK_UI_MANAGER (ui),
+                                                                   "/GitBranchPopup"));
+
+                       gtk_menu_popup (menu, NULL, NULL, NULL, NULL, event->button, 
+                                       event->time);
+               }
+       }
+
+       return FALSE;
+}
+
 static void
 git_branches_pane_init (GitBranchesPane *self)
 {
@@ -370,6 +399,11 @@ git_branches_pane_init (GitBranchesPane *self)
        g_signal_connect (G_OBJECT (branches_view), "row-activated",
                          G_CALLBACK (on_branches_view_row_activated),
                          self);
+
+       /* Pop-up menu */
+       g_signal_connect (G_OBJECT (branches_view), "button-press-event",
+                         G_CALLBACK (on_branches_view_button_press_event),
+                         self);
 }
 
 static void
@@ -507,6 +541,32 @@ git_branches_pane_get_selected_branch (GitBranchesPane *self)
        return selected_branch;
 }
 
+gboolean
+git_branches_pane_is_selected_branch_remote (GitBranchesPane *self)
+{
+       gboolean is_remote;
+       GtkTreeView *branches_view;
+       GtkTreeSelection *selection;
+       GtkTreeModel *branches_list_model;
+       GtkTreeIter iter;
+
+       is_remote = FALSE;
+       branches_view = GTK_TREE_VIEW (gtk_builder_get_object (self->priv->builder,
+                                                              "branches_view"));
+       selection = gtk_tree_view_get_selection (branches_view);
+
+       if (gtk_tree_selection_count_selected_rows (selection) > 0)
+       {
+               gtk_tree_selection_get_selected (selection, &branches_list_model,
+                                                &iter);
+
+               gtk_tree_model_get (branches_list_model, &iter, COL_REMOTE, 
+                                   &is_remote, -1);
+       }
+
+       return is_remote;
+}
+
 static gboolean
 clear_branch_selections (GtkTreeModel *model, GtkTreePath *path, 
                          GtkTreeIter *iter, gpointer data)
diff --git a/plugins/git/git-branches-pane.h b/plugins/git/git-branches-pane.h
index 08aa15f..86ac92c 100644
--- a/plugins/git/git-branches-pane.h
+++ b/plugins/git/git-branches-pane.h
@@ -54,6 +54,7 @@ GList *git_branches_pane_get_selected_local_branches (GitBranchesPane *self);
 GList *git_branches_pane_get_selected_remote_branches (GitBranchesPane *self);
 gsize git_branches_pane_count_selected_items (GitBranchesPane *self);
 gchar *git_branches_pane_get_selected_branch (GitBranchesPane *self);
+gboolean git_branches_pane_is_selected_branch_remote (GitBranchesPane *self);
 void git_branches_pane_set_select_column_visible (GitBranchesPane *self, 
                                                   gboolean visible);
 
diff --git a/plugins/git/git-delete-branches-pane.c b/plugins/git/git-delete-branches-pane.c
index b1d7f30..8421c4b 100644
--- a/plugins/git/git-delete-branches-pane.c
+++ b/plugins/git/git-delete-branches-pane.c
@@ -224,3 +224,38 @@ on_delete_branches_button_clicked (GtkAction *action, Git *plugin)
                                          "Delete Branches", NULL, delete_branches_pane,
                                          GDL_DOCK_BOTTOM, NULL, 0, NULL);
 }
+
+void
+on_git_branch_delete_activated (GtkAction *action, Git *plugin)
+{
+       gchar *selected_branch;
+       GList *branches;
+       GitBranchDeleteCommand *delete_command;
+
+       selected_branch = git_branches_pane_get_selected_branch (GIT_BRANCHES_PANE (plugin->branches_pane));
+
+       if (anjuta_util_dialog_boolean_question (NULL, FALSE, 
+                                                _("Are you sure you want to delete branch %s?"),
+                                                selected_branch))
+       {
+               branches = g_list_append (NULL, selected_branch);
+               delete_command = git_branch_delete_command_new (plugin->project_root_directory,
+                                                               branches, 
+                                                               git_branches_pane_is_selected_branch_remote 
(GIT_BRANCHES_PANE (plugin->branches_pane)),
+                                                               FALSE);
+
+               g_list_free (branches);
+
+               g_signal_connect (G_OBJECT (delete_command), "command-finished",
+                                 G_CALLBACK (git_pane_report_errors),
+                                 plugin);
+
+               g_signal_connect (G_OBJECT (delete_command), "command-finished",
+                                 G_CALLBACK (g_object_unref),
+                                 NULL);
+
+               anjuta_command_start (ANJUTA_COMMAND (delete_command));
+       }
+
+       g_free (selected_branch);
+}
diff --git a/plugins/git/git-delete-branches-pane.h b/plugins/git/git-delete-branches-pane.h
index d6c606a..0347dc2 100644
--- a/plugins/git/git-delete-branches-pane.h
+++ b/plugins/git/git-delete-branches-pane.h
@@ -53,8 +53,9 @@ struct _GitDeleteBranchesPane
 
 GType git_delete_branches_pane_get_type (void) G_GNUC_CONST;
 AnjutaDockPane * git_delete_branches_pane_new (Git *plugin);
-void on_delete_branches_button_clicked (GtkAction *action, 
-                                        Git *plugin);
+
+void on_delete_branches_button_clicked (GtkAction *action, Git *plugin);
+void on_git_branch_delete_activated (GtkAction *action, Git *plugin);
 
 G_END_DECLS
 
diff --git a/plugins/git/git-merge-pane.c b/plugins/git/git-merge-pane.c
index 0026be8..bce31fe 100644
--- a/plugins/git/git-merge-pane.c
+++ b/plugins/git/git-merge-pane.c
@@ -203,6 +203,29 @@ git_merge_pane_new (Git *plugin)
        return g_object_new (GIT_TYPE_MERGE_PANE, "plugin", plugin, NULL);
 }
 
+AnjutaDockPane *
+git_merge_pane_new_with_revision (Git *plugin, const gchar *revision)
+{
+       GitMergePane *self;
+       AnjutaEntry *merge_revision_entry;
+
+       self = g_object_new (GIT_TYPE_MERGE_PANE, "plugin", plugin, NULL);;
+       merge_revision_entry = ANJUTA_ENTRY (gtk_builder_get_object (self->priv->builder,
+                                                                                        
"merge_revision_entry"));
+
+       anjuta_entry_set_text (merge_revision_entry, revision);
+
+       return ANJUTA_DOCK_PANE (self);
+}
+
+static void
+add_pane (AnjutaDockPane *pane, Git *plugin)
+{
+       anjuta_dock_replace_command_pane (ANJUTA_DOCK (plugin->dock), "Merge", 
+                                         _("Merge"), NULL, pane, GDL_DOCK_BOTTOM, NULL, 0,
+                                         NULL);
+}
+
 void
 on_merge_button_clicked (GtkAction *action, Git *plugin)
 {
@@ -210,7 +233,18 @@ on_merge_button_clicked (GtkAction *action, Git *plugin)
 
        pane = git_merge_pane_new (plugin);
 
-       anjuta_dock_replace_command_pane (ANJUTA_DOCK (plugin->dock), "Merge", 
-                                         _("Merge"), NULL, pane, GDL_DOCK_BOTTOM, NULL, 0,
-                                         NULL);
+       add_pane (pane, plugin);
+}
+
+void
+on_git_branch_merge_activated (GtkAction *action, Git *plugin)
+{
+       gchar *selected_branch;
+       AnjutaDockPane *pane;
+
+       selected_branch = git_branches_pane_get_selected_branch (GIT_BRANCHES_PANE (plugin->branches_pane));
+       pane = git_merge_pane_new_with_revision (plugin, selected_branch);
+
+       g_free (selected_branch);
+       add_pane (pane, plugin);
 }
diff --git a/plugins/git/git-merge-pane.h b/plugins/git/git-merge-pane.h
index 0faeff7..f9b7b9b 100644
--- a/plugins/git/git-merge-pane.h
+++ b/plugins/git/git-merge-pane.h
@@ -24,6 +24,7 @@
 #include <libanjuta/anjuta-column-text-view.h>
 #include "git-pane.h"
 #include "git-merge-command.h"
+#include "git-branches-pane.h"
 
 G_BEGIN_DECLS
 
@@ -52,8 +53,11 @@ struct _GitMergePane
 
 GType git_merge_pane_get_type (void) G_GNUC_CONST;
 AnjutaDockPane *git_merge_pane_new (Git *plugin);
+AnjutaDockPane *git_merge_pane_new_with_revision (Git *plugin, 
+                                                  const gchar *revision);
 
 void on_merge_button_clicked (GtkAction *action, Git *plugin);
+void on_git_branch_merge_activated (GtkAction *action, Git *plugin);
 
 G_END_DECLS
 
diff --git a/plugins/git/plugin.c b/plugins/git/plugin.c
index 0b1801a..1bf4bf7 100644
--- a/plugins/git/plugin.c
+++ b/plugins/git/plugin.c
@@ -541,6 +541,34 @@ static GtkActionEntry log_menu_entries[] =
        }
 };
 
+static GtkActionEntry branch_menu_entries[] =
+{
+       {
+               "GitBranchSwitch",
+               NULL,
+               N_("Switch"),
+               NULL,
+               NULL,
+               G_CALLBACK (on_switch_branch_button_clicked)
+       },
+       {
+               "GitBranchDelete",
+               NULL,
+               N_("Delete..."),
+               NULL,
+               NULL,
+               G_CALLBACK (on_git_branch_delete_activated)
+       },
+       {
+               "GitBranchMerge",
+               NULL,
+               N_("Merge..."),
+               NULL,
+               NULL,
+               G_CALLBACK (on_git_branch_merge_activated)
+       }
+};
+
 static gpointer parent_class;
 
 static void
@@ -792,6 +820,12 @@ git_activate_plugin (AnjutaPlugin *plugin)
                                                                         G_N_ELEMENTS (log_menu_entries),
                                                                         GETTEXT_PACKAGE,
                                                                         FALSE, plugin);
+       git_plugin->branch_menu_group = anjuta_ui_add_action_group_entries (ui, "GitBrancPopup",
+                                                                           _("Branch popup menu"),
+                                                                           branch_menu_entries,
+                                                                           G_N_ELEMENTS 
(branch_menu_entries),
+                                                                           GETTEXT_PACKAGE,
+                                                                           FALSE, plugin);
 
        
        /* Create the branch list commands. There are two commands because some 
@@ -932,6 +966,7 @@ git_deactivate_plugin (AnjutaPlugin *plugin)
        ui = anjuta_shell_get_ui (plugin->shell, NULL);
        anjuta_ui_remove_action_group (ui, git_plugin->status_menu_group);
        anjuta_ui_remove_action_group (ui, git_plugin->log_menu_group);
+       anjuta_ui_remove_action_group (ui, git_plugin->branch_menu_group);
        anjuta_ui_unmerge (ui, git_plugin->uiid);
 
        g_object_unref (git_plugin->local_branch_list_command);
diff --git a/plugins/git/plugin.h b/plugins/git/plugin.h
index 008f9b4..37bf3c9 100644
--- a/plugins/git/plugin.h
+++ b/plugins/git/plugin.h
@@ -81,6 +81,7 @@ struct _Git
        gint uiid;
        GtkActionGroup *status_menu_group;
        GtkActionGroup *log_menu_group;
+       GtkActionGroup *branch_menu_group;
 
        /* List commands for various panes. 
         * Keep them in the plugin so that the commands have the most direct


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