planner-task-popup.c 6.01 KB
Newer Older
1
/*
Richard Hult's avatar
Richard Hult committed
2
 * Copyright (C) 2005 Imendio AB
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2004 Chris Ladd <caladd@particlestorm.net>
 *
 * 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 2 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <config.h>
22
#include <gtk/gtk.h>
23
#include <glib/gi18n.h>
24
#include "libplanner/mrp-paths.h"
25 26 27 28
#include "planner-task-tree.h"
#include "planner-task-popup.h"


29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static void task_popup_insert_task_cb         (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
static void task_popup_insert_subtask_cb      (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
static void task_popup_remove_task_cb         (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
static void task_popup_edit_task_cb           (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
static void task_popup_edit_task_resources_cb (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
static void task_popup_unlink_task_cb         (gpointer   callback_data,
					       guint      action,
					       GtkWidget *widget);
47 48 49 50 51 52


#define GIF_CB(x) ((GtkItemFactoryCallback)(x))

static GtkItemFactoryEntry popup_menu_items[] = {
	{ N_("/_Insert task"), NULL, GIF_CB (task_popup_insert_task_cb),
53
	  PLANNER_TASK_POPUP_INSERT, "<Item>", NULL
54
	},
55 56
	{ N_("/Insert _subtask"), NULL, GIF_CB (task_popup_insert_subtask_cb),
	  PLANNER_TASK_POPUP_SUBTASK, "<Item>", NULL
57 58
	},
	{ N_("/_Remove task"), NULL, GIF_CB (task_popup_remove_task_cb),
59 60 61
	  PLANNER_TASK_POPUP_REMOVE, "<StockItem>", GTK_STOCK_DELETE
	},
	{ "/sep1", NULL, 0, PLANNER_TASK_POPUP_NONE, "<Separator>"
62 63
	},
	{ N_("/_Unlink task"), NULL, GIF_CB (task_popup_unlink_task_cb),
64 65 66
	  PLANNER_TASK_POPUP_UNLINK, "<Item>", NULL
	},
	{ "/sep2", NULL, 0, PLANNER_TASK_POPUP_NONE, "<Separator>"
67
	},
68 69
	{ N_("/Assign _resources..."), NULL, GIF_CB (task_popup_edit_task_resources_cb),
	  PLANNER_TASK_POPUP_EDIT_RESOURCES,  "<Item>",   NULL
70 71
	},
	{ N_("/_Edit task..."), NULL, GIF_CB (task_popup_edit_task_cb),
72
	  PLANNER_TASK_POPUP_EDIT_TASK, "<Item>", NULL
73 74 75 76 77 78 79 80 81 82
	}
};

static char *
task_tree_item_factory_trans (const char *path, gpointer data)
{
	return _((gchar *)path);
}

static void
83 84 85
task_popup_insert_task_cb (gpointer   callback_data,
			   guint      action,
			   GtkWidget *widget)
86 87 88 89 90
{
	planner_task_tree_insert_task (callback_data);
}

static void
91 92 93
task_popup_insert_subtask_cb (gpointer   callback_data,
			      guint      action,
			      GtkWidget *widget)
94 95 96 97 98
{
	planner_task_tree_insert_subtask (callback_data);
}

static void
99 100 101
task_popup_remove_task_cb (gpointer   callback_data,
			   guint      action,
			   GtkWidget *widget)
102 103 104 105 106
{
	planner_task_tree_remove_task (callback_data);
}

static void
107 108 109 110 111 112 113 114 115 116 117 118
task_popup_edit_task_cb (gpointer   callback_data,
			 guint      action,
			 GtkWidget *widget)
{
	planner_task_tree_edit_task (callback_data,
				     PLANNER_TASK_DIALOG_PAGE_GENERAL);
}

static void
task_popup_edit_task_resources_cb (gpointer   callback_data,
				   guint      action,
				   GtkWidget *widget)
119
{
120 121
	planner_task_tree_edit_task (callback_data,
				     PLANNER_TASK_DIALOG_PAGE_RESOURCES);
122 123 124 125
}

static void
task_popup_unlink_task_cb (gpointer callback_data, guint action,
126
			   GtkWidget *widget)
127 128 129 130 131
{
	planner_task_tree_unlink_task (callback_data);
}

GtkItemFactory *
132
planner_task_popup_new (PlannerTaskTree *tree)
133
{
134
	GtkItemFactory *item_factory;
135

136
	item_factory = gtk_item_factory_new (GTK_TYPE_MENU, "<main>", NULL);
137
	gtk_item_factory_set_translate_func (item_factory,
138 139
					     task_tree_item_factory_trans,
					     NULL, NULL);
140 141

	gtk_item_factory_create_items (item_factory,
142 143
				       G_N_ELEMENTS (popup_menu_items),
				       popup_menu_items, tree);
144

145
	return item_factory;
146
}
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164

static void
task_popup_set_sensitive (GtkItemFactory *factory, gint id, gboolean sensitive)
{
	GtkWidget *widget;

	widget = gtk_item_factory_get_widget_by_action (factory, id);
	gtk_widget_set_sensitive (widget, sensitive);
}

void
planner_task_popup_update_sensitivity (GtkItemFactory *factory,
				       GList          *tasks)
{
	gint          length;
	MrpTask      *task;
	MrpTaskType   type;
	gboolean      milestone;
165

166 167 168 169
	length = g_list_length (tasks);

	/* Can always insert task. */
	task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_INSERT, TRUE);
170

171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
	/* Nothing else when nothing is selected. */
	if (length == 0) {
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_SUBTASK, FALSE);
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_REMOVE, FALSE);
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_UNLINK, FALSE);
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_EDIT_TASK, FALSE);
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_EDIT_RESOURCES, FALSE);
		return;
	}

	/* Can only insert subtask when one !milestone is selected. */
	if (length == 1) {
		task = tasks->data;

		type = mrp_task_get_task_type (task);
		milestone = (type == MRP_TASK_TYPE_MILESTONE);
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_SUBTASK, !milestone);
	} else {
		task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_SUBTASK, FALSE);
	}

	/* The rest are always sensitive when one more more tasks are selected. */
	task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_REMOVE, TRUE);
	task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_UNLINK, TRUE);
	task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_EDIT_TASK, TRUE);
	task_popup_set_sensitive (factory, PLANNER_TASK_POPUP_EDIT_RESOURCES, TRUE);
}