1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) James Liggett 2010 <jrliggett@cox.net>
6 * git-shell-test is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * git-shell-test is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * See the GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "git-tags-pane.h"
28 static GtkTargetEntry drag_targets
[] =
37 struct _GitTagsPanePriv
40 GtkListStore
*tags_list_model
;
42 GtkAction
*delete_action
;
46 G_DEFINE_TYPE (GitTagsPane
, git_tags_pane
, GIT_TYPE_PANE
);
49 on_tag_list_command_started (AnjutaCommand
*command
, GitTagsPane
*self
)
51 GtkTreeView
*tags_view
;
52 GtkListStore
*tags_list_model
;
54 tags_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
56 tags_list_model
= GTK_LIST_STORE (gtk_builder_get_object (self
->priv
->builder
,
59 gtk_tree_view_set_model (tags_view
, NULL
);
60 gtk_list_store_clear (tags_list_model
);
64 on_tag_list_command_finished (AnjutaCommand
*command
, guint return_code
,
67 GtkTreeView
*tags_view
;
69 tags_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
72 gtk_tree_view_set_model (tags_view
, GTK_TREE_MODEL(self
->priv
->tags_list_model
));
76 on_tag_list_command_data_arrived (AnjutaCommand
*command
,
77 GtkListStore
*tags_list_model
)
83 output
= git_raw_output_command_get_output (GIT_RAW_OUTPUT_COMMAND (command
));
85 while (g_queue_peek_head (output
))
87 name
= g_queue_pop_head (output
);
88 gtk_list_store_append (tags_list_model
, &iter
);
90 gtk_list_store_set (tags_list_model
, &iter
,
101 on_selected_renderer_toggled (GtkCellRendererToggle
*renderer
,
102 gchar
*path
, GitTagsPane
* self
)
107 gtk_tree_model_get_iter_from_string (GTK_TREE_MODEL(self
->priv
->tags_list_model
),
109 gtk_tree_model_get (GTK_TREE_MODEL(self
->priv
->tags_list_model
), &iter
,
110 COL_SELECTED
, &selected
,
113 selected
= !selected
;
115 gtk_list_store_set (GTK_LIST_STORE (self
->priv
->tags_list_model
), &iter
, 0, selected
,
117 git_tags_pane_update_ui (self
);
121 on_tags_list_view_drag_data_get (GtkWidget
*tags_list_view
,
122 GdkDragContext
*drag_context
,
123 GtkSelectionData
*data
,
124 guint info
, guint time
,
127 GtkTreeSelection
*selection
;
129 GtkTreeModel
*tags_list_model
;
132 selection
= gtk_tree_view_get_selection (GTK_TREE_VIEW (tags_list_view
));
134 if (gtk_tree_selection_count_selected_rows (selection
) > 0)
136 gtk_tree_selection_get_selected (selection
, &tags_list_model
,
139 gtk_tree_model_get (tags_list_model
, &iter
, COL_NAME
, &name
, -1);
141 gtk_selection_data_set_text (data
, name
, -1);
148 get_selected_tags (GtkTreeModel
*tags_list_model
, gchar
*path
,
149 GtkTreeIter
*iter
, GList
**list
)
154 gtk_tree_model_get (tags_list_model
, iter
, COL_SELECTED
, &selected
, -1);
158 gtk_tree_model_get (tags_list_model
, iter
, COL_NAME
, &name
, -1);
160 *list
= g_list_append (*list
, name
);
167 git_tags_pane_init (GitTagsPane
*self
)
169 gchar
*objects
[] = {"tags_pane",
172 GError
*error
= NULL
;
173 GtkTreeView
*tags_view
;
174 GtkCellRenderer
*tags_selected_renderer
;
176 self
->priv
= g_new0 (GitTagsPanePriv
, 1);
177 self
->priv
->builder
= gtk_builder_new ();
180 if (!gtk_builder_add_objects_from_file (self
->priv
->builder
, BUILDER_FILE
,
184 g_warning ("Couldn't load builder file: %s", error
->message
);
185 g_error_free (error
);
188 tags_view
= GTK_TREE_VIEW (gtk_builder_get_object (self
->priv
->builder
,
190 self
->priv
->tags_list_model
=
191 GTK_LIST_STORE (gtk_builder_get_object (self
->priv
->builder
,
193 tags_selected_renderer
= GTK_CELL_RENDERER (gtk_builder_get_object (self
->priv
->builder
,
194 "tags_selected_renderer"));
197 gtk_tree_view_enable_model_drag_source (tags_view
,
200 G_N_ELEMENTS (drag_targets
),
203 g_signal_connect (G_OBJECT (tags_view
), "drag-data-get",
204 G_CALLBACK (on_tags_list_view_drag_data_get
),
207 g_signal_connect (G_OBJECT (tags_selected_renderer
), "toggled",
208 G_CALLBACK (on_selected_renderer_toggled
),
213 git_tags_pane_finalize (GObject
*object
)
217 self
= GIT_TAGS_PANE (object
);
219 g_object_unref (self
->priv
->builder
);
222 G_OBJECT_CLASS (git_tags_pane_parent_class
)->finalize (object
);
226 git_tags_pane_get_widget (AnjutaDockPane
*pane
)
230 self
= GIT_TAGS_PANE (pane
);
232 return GTK_WIDGET (gtk_builder_get_object (self
->priv
->builder
,
237 git_tags_pane_class_init (GitTagsPaneClass
*klass
)
239 GObjectClass
* object_class
= G_OBJECT_CLASS (klass
);
240 AnjutaDockPaneClass
*pane_class
= ANJUTA_DOCK_PANE_CLASS (klass
);
242 object_class
->finalize
= git_tags_pane_finalize
;
243 pane_class
->get_widget
= git_tags_pane_get_widget
;
244 pane_class
->refresh
= NULL
;
249 git_tags_pane_new (Git
*plugin
)
252 self
= g_object_new (GIT_TYPE_TAGS_PANE
, "plugin", plugin
, NULL
);
255 g_signal_connect (G_OBJECT (plugin
->tag_list_command
), "command-started",
256 G_CALLBACK (on_tag_list_command_started
),
259 g_signal_connect (G_OBJECT (plugin
->tag_list_command
), "command-finished",
260 G_CALLBACK (on_tag_list_command_finished
),
263 g_signal_connect (G_OBJECT (plugin
->tag_list_command
), "data-arrived",
264 G_CALLBACK (on_tag_list_command_data_arrived
),
265 self
->priv
->tags_list_model
);
267 return ANJUTA_DOCK_PANE (self
);
271 git_tags_pane_get_selected_tags (GitTagsPane
*self
)
276 gtk_tree_model_foreach (GTK_TREE_MODEL(self
->priv
->tags_list_model
),
277 (GtkTreeModelForeachFunc
) get_selected_tags
, &list
);
282 void git_tags_pane_update_ui (GitTagsPane
*self
)
284 GList
* selected_tags
;
286 /* Enable only actions that make sense with the selection */
287 if (!self
->priv
->delete_action
)
290 ANJUTA_PLUGIN_GIT (anjuta_dock_pane_get_plugin (ANJUTA_DOCK_PANE (self
)));
291 AnjutaCommandBar
* bar
= anjuta_dock_get_command_bar (ANJUTA_DOCK(plugin
->dock
));
292 self
->priv
->delete_action
= anjuta_command_bar_get_action (bar
,
296 selected_tags
= git_tags_pane_get_selected_tags (self
);
297 gtk_action_set_sensitive (self
->priv
->delete_action
, g_list_length (selected_tags
) > 0);
298 g_list_free (selected_tags
);