planner-calendar-selector.c 6.69 KB
Newer Older
Richard Hult's avatar
Richard Hult committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Copyright (C) 2002 CodeFactory AB
 * Copyright (C) 2002 Richard Hult <richard@imendio.com>
 * Copyright (C) 2002 Mikael Hallendal <micke@imendio.com>
 *
 * 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
Josué Abarca's avatar
Josué Abarca committed
19 20
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
Richard Hult's avatar
Richard Hult committed
21 22 23
 */

#include <config.h>
24
#include <glib/gi18n.h>
Richard Hult's avatar
Richard Hult committed
25 26 27 28 29
#include <glade/glade.h>
#include <gtk/gtk.h>
#include <libplanner/mrp-day.h>
#include <libplanner/mrp-object.h>
#include <libplanner/mrp-project.h>
30
#include "libplanner/mrp-paths.h"
Richard Hult's avatar
Richard Hult committed
31 32 33 34 35 36 37 38 39 40
#include "planner-calendar.h"
#include "planner-calendar-selector.h"

enum {
	COL_CALENDAR,
	COL_NAME,
	NUM_COLS
};

typedef struct {
Richard Hult's avatar
Richard Hult committed
41
	PlannerWindow  *main_window;
Richard Hult's avatar
Richard Hult committed
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
	MrpProject    *project;

	GtkWidget     *selector;
	GtkWidget     *tree_view;
	GtkWidget     *ok_button;
	GtkWidget     *cancel_button;

	GtkWidget     *this_label;
} DialogData;

typedef struct {
	DialogData    *data;
	MrpCalendar   *calendar;
	gboolean       found;
	GtkTreeIter    found_iter;
} FindCalendarData;


#define SELECTOR_GET_DATA(d) g_object_get_data ((GObject*)d, "data")

static GtkTreeModel *cal_selector_create_model          (MrpProject       *project,
							 GtkTreeView      *tree_view);
static void          cal_selector_setup_tree_view       (GtkTreeView      *tree_view,
							 MrpProject       *project);
static MrpCalendar * cal_selector_get_selected_calendar (GtkTreeView      *tree_view);
static void          cal_selector_selection_changed_cb  (GtkTreeSelection *selection,
							 DialogData       *data);

GtkWidget *
Richard Hult's avatar
Richard Hult committed
71
planner_calendar_selector_new (PlannerWindow *window,
72
			       const gchar  *caption)
Richard Hult's avatar
Richard Hult committed
73 74 75 76 77 78
{
	DialogData       *data;
	GladeXML         *glade;
	GtkWidget        *selector;
	GtkWidget        *w;
	GtkTreeSelection *selection;
79
	gchar		 *filename;
80

81
	g_return_val_if_fail (PLANNER_IS_WINDOW (window), NULL);
82

83 84
	filename = mrp_paths_get_glade_dir ("calendar-dialog.glade");
	glade = glade_xml_new (filename,
Richard Hult's avatar
Richard Hult committed
85 86
			       "calendar_selector",
			       GETTEXT_PACKAGE);
87
	g_free (filename);
Richard Hult's avatar
Richard Hult committed
88 89 90 91 92 93
	if (!glade) {
		g_warning ("Could not create calendar selector.");
		return NULL;
	}

	selector = glade_xml_get_widget (glade, "calendar_selector");
94

Richard Hult's avatar
Richard Hult committed
95 96
	data = g_new0 (DialogData, 1);

Richard Hult's avatar
Richard Hult committed
97
	data->project = planner_window_get_project (window);
Richard Hult's avatar
Richard Hult committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
	data->main_window = window;
	data->selector = selector;

	data->tree_view = glade_xml_get_widget (glade, "treeview");

	w = glade_xml_get_widget (glade, "caption_label");
	gtk_label_set_text (GTK_LABEL (w), caption);

	data->ok_button = glade_xml_get_widget (glade, "ok_button");
	data->cancel_button = glade_xml_get_widget (glade, "cancel_button");

	g_object_set_data_full (G_OBJECT (selector),
				"data", data,
				g_free);

	cal_selector_setup_tree_view (GTK_TREE_VIEW (data->tree_view),
				      data->project);

	selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->tree_view));
	gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);

	g_signal_connect (selection,
			  "changed",
			  G_CALLBACK (cal_selector_selection_changed_cb),
			  data);
123

Richard Hult's avatar
Richard Hult committed
124 125 126 127 128 129 130 131 132 133 134 135 136 137
	return selector;
}

MrpCalendar *
planner_calendar_selector_get_calendar (GtkWidget *dialog)
{
	DialogData  *data;
	MrpCalendar *calendar;

	g_return_val_if_fail (GTK_IS_WIDGET (dialog), NULL);

	data = SELECTOR_GET_DATA (dialog);

	calendar = cal_selector_get_selected_calendar (GTK_TREE_VIEW (data->tree_view));
138

Richard Hult's avatar
Richard Hult committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
	return calendar;
}

static MrpCalendar *
cal_selector_get_selected_calendar (GtkTreeView *tree_view)
{
	GtkTreeSelection *selection;
	GtkTreeModel     *model;
	GtkTreeIter       iter;
	MrpCalendar      *calendar;

	selection = gtk_tree_view_get_selection (tree_view);
	if (gtk_tree_selection_get_selected (selection, &model, &iter)) {
		gtk_tree_model_get (model, &iter,
				    COL_CALENDAR, &calendar,
				    -1);

		return calendar;
	}

	return NULL;
}

static void
cal_selector_selection_changed_cb (GtkTreeSelection *selection,
				   DialogData       *data)
{
	MrpCalendar *calendar;

	calendar = cal_selector_get_selected_calendar (GTK_TREE_VIEW (data->tree_view));

	gtk_widget_set_sensitive (data->ok_button, calendar != NULL);
}

static void
cal_selector_setup_tree_view (GtkTreeView *tree_view,
			      MrpProject  *project)
{
	GtkTreeModel      *model;
	GtkCellRenderer   *cell;
	GtkTreeViewColumn *col;
180

Richard Hult's avatar
Richard Hult committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	model = cal_selector_create_model (project, tree_view);

	gtk_tree_view_set_model (tree_view, model);

	cell = gtk_cell_renderer_text_new ();
	col = gtk_tree_view_column_new_with_attributes (
		NULL,
		cell,
		"text", COL_NAME,
		NULL);
	gtk_tree_view_append_column (tree_view, col);

	gtk_tree_view_expand_all (tree_view);
}

static void
cal_selector_build_tree (GtkTreeStore *store,
			 GtkTreeIter  *parent,
			 MrpCalendar  *calendar)
{
	GtkTreeIter  iter;
	const gchar *name;
	GList       *children, *l;
204

Richard Hult's avatar
Richard Hult committed
205
	name = mrp_calendar_get_name (calendar);
206

Richard Hult's avatar
Richard Hult committed
207 208 209 210 211 212 213
	gtk_tree_store_append (store, &iter, parent);
	gtk_tree_store_set (store,
			    &iter,
			    COL_NAME, name,
			    COL_CALENDAR, calendar,
			    -1);

214
	children = mrp_calendar_get_children (calendar);
Richard Hult's avatar
Richard Hult committed
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
	for (l = children; l; l = l->next) {
		cal_selector_build_tree (store, &iter, l->data);
	}
}

static void
cal_selector_tree_changed (MrpProject  *project,
			   MrpCalendar *root,
			   GtkTreeView *tree_view)
{
	GtkTreeStore *store;
	GList        *children, *l;

	store = GTK_TREE_STORE (gtk_tree_view_get_model (tree_view));

	gtk_tree_store_clear (store);
231 232

	children = mrp_calendar_get_children (root);
Richard Hult's avatar
Richard Hult committed
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
	for (l = children; l; l = l->next) {
		cal_selector_build_tree (store, NULL, l->data);
	}

	gtk_tree_view_expand_all (tree_view);
}

static GtkTreeModel *
cal_selector_create_model (MrpProject  *project,
			   GtkTreeView *tree_view)
{
	GtkTreeStore *store;
	MrpCalendar  *root;
	GList        *children, *l;

	root = mrp_project_get_root_calendar (project);

	store = gtk_tree_store_new (NUM_COLS,
				    G_TYPE_OBJECT,
				    G_TYPE_STRING);

254
	children = mrp_calendar_get_children (root);
Richard Hult's avatar
Richard Hult committed
255 256 257 258 259 260 261 262 263
	for (l = children; l; l = l->next) {
		cal_selector_build_tree (store, NULL, l->data);
	}

	g_signal_connect_object (project,
				 "calendar_tree_changed",
				 G_CALLBACK (cal_selector_tree_changed),
				 tree_view,
				 0);
264

Richard Hult's avatar
Richard Hult committed
265 266
	return GTK_TREE_MODEL (store);
}