sheet-object-cell-comment.c 8.61 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5

/*
 * sheet-object-cell-comment.c: A SheetObject to support cell comments.
 *
6
 * Copyright (C) 2000 Jody Goldberg (jody@gnome.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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
 */
23 24
#include <gnumeric-config.h>
#include "gnumeric.h"
25
#include "sheet-object-cell-comment.h"
26

27 28
#include "sheet-object-impl.h"
#include "sheet.h"
29
#include "sheet-merge.h"
30
#include "sheet-control-gui-priv.h"
31 32

#include <gal/util/e-util.h>
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
#include <gal/widgets/e-cursors.h>

struct _CellComment {
	SheetObject	s_object;

	char *author, *text;
};
typedef struct {
	SheetObjectClass s_object_class;
} CellCommentClass;
static SheetObjectClass *cell_comment_parent_class;

static void
cell_comment_destroy (GtkObject *object)
{
	CellComment *cc = CELL_COMMENT (object);

	g_return_if_fail (cc != NULL);

	if (cc->author != NULL) {
		g_free (cc->author);
		cc->author = NULL;
	}
	if (cc->text != NULL) {
		g_free (cc->text);
		cc->text = NULL;
	}

	/* If this comment is being displayed we shut down nicely */
	SHEET_FOREACH_CONTROL (cc->s_object.sheet, control,
63
		scg_comment_unselect ((SheetControlGUI *) control, cc););
64 65 66 67 68 69

	GTK_OBJECT_CLASS (cell_comment_parent_class)->destroy (object);
}

#define TRIANGLE_WIDTH 6
static GnomeCanvasPoints *
70
comment_get_points (SheetControlGUI *scg, SheetObject *so)
71 72
{
	GnomeCanvasPoints *points;
73 74 75
	int x, y, i, far_col;
	Range const *r;

76
	r = sheet_merge_is_corner (so->sheet, &so->anchor.cell_bound.start);
77 78 79 80 81
	if (r != NULL) {
		so->anchor.cell_bound.end.col = r->end.col;
		far_col = 1 + r->end.col;
	} else
		far_col = 1 + so->anchor.cell_bound.start.col;
82 83

	/* TODO : This could be optimized using the offsets associated with the visible region */
84
	x = scg_colrow_distance_get (scg, TRUE, 0, far_col) - 1;
85
	y = scg_colrow_distance_get (scg, FALSE, 0, so->anchor.cell_bound.start.row) + 1;
86 87 88 89 90 91 92 93 94 95

	points = gnome_canvas_points_new (3);
	points->coords [0] = x - TRIANGLE_WIDTH;
	points->coords [1] = y;
	points->coords [2] = x;
	points->coords [3] = y;
	points->coords [4] = x;
	points->coords [5] = y + TRIANGLE_WIDTH;

	for (i = 0; i < 3; i++)
96
		/* FIXME : panes ? */
97
		gnome_canvas_c2w (GNOME_CANVAS (scg_pane (scg, 0)),
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
				  points->coords [i*2],
				  points->coords [i*2+1],
				  &(points->coords [i*2]),
				  &(points->coords [i*2+1]));

	return points;
}

static void
cell_comment_update_bounds (SheetObject *so, GtkObject *view,
			    SheetControlGUI *scg)
{
	GnomeCanvasItem *item = GNOME_CANVAS_ITEM (view);
	GnomeCanvasPoints *points = comment_get_points (scg, so);
	gnome_canvas_item_set (item, "points", points, NULL);
	gnome_canvas_points_free (points);
114 115 116 117 118

	if (so->is_visible)
		gnome_canvas_item_show (item);
	else
		gnome_canvas_item_hide (item);
119 120 121 122 123 124 125
}

static int
cell_comment_event (GnomeCanvasItem *view, GdkEvent *event, SheetControlGUI *scg)
{
	CellComment *cc;
	SheetObject *so;
126
	
127 128 129 130 131 132 133 134 135 136 137 138 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
	switch (event->type) {
	default:
		return FALSE;

	case GDK_BUTTON_RELEASE:
		if (event->button.button != 1)
			return FALSE;
	case GDK_ENTER_NOTIFY:
	case GDK_LEAVE_NOTIFY:
		break;
	}

	so = sheet_object_view_obj (GTK_OBJECT (view));
	cc = CELL_COMMENT (so);

	g_return_val_if_fail (cc != NULL, FALSE);

	switch (event->type) {
	case GDK_BUTTON_RELEASE:
		scg_comment_display (scg, cc);
		break;

	case GDK_ENTER_NOTIFY:
		e_cursor_set_widget (view->canvas, E_CURSOR_ARROW);
		scg_comment_select (scg, cc);
		break;

	case GDK_LEAVE_NOTIFY:
		scg_comment_unselect (scg, cc);
		break;

	default:
		return FALSE;
	}
	return TRUE;
}

static GtkObject *
cell_comment_new_view (SheetObject *so, SheetControlGUI *scg)
{
	GnomeCanvasPoints *points;
	GnomeCanvasGroup *group;
	GnomeCanvasItem *item = NULL;
	CellComment *cc = CELL_COMMENT (so);

	g_return_val_if_fail (cc != NULL, NULL);
	g_return_val_if_fail (IS_SHEET_CONTROL_GUI (scg), NULL);

175
	/* FIXME : panes ? */
176
	group = GNOME_CANVAS_GROUP (GNOME_CANVAS (scg_pane (scg, 0))->root);
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	points = comment_get_points (scg, so);
	item = gnome_canvas_item_new (
		group, gnome_canvas_polygon_get_type (),
		"points",     points,
		"fill_color", "red",
		NULL);
	gnome_canvas_points_free (points);

	/* Do not use the standard handler, comments are not movable */
	gtk_signal_connect (GTK_OBJECT (item), "event",
			    GTK_SIGNAL_FUNC (cell_comment_event), scg);

	return GTK_OBJECT (item);
}

192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
static gboolean
cell_comment_read_xml (SheetObject *so,
		       XmlParseContext const *ctxt, xmlNodePtr	tree)
{
	CellComment *cc = CELL_COMMENT (so);
	char *author = xmlGetProp (tree, "Author");
	char *text = xmlGetProp (tree, "Text");

	if (author != NULL) {
		cell_comment_author_set (cc, author);
		xmlFree (author);
	}
	if (text != NULL) {
		cell_comment_text_set (cc, text);
		xmlFree (text);
	}

	return FALSE;
}

static gboolean
cell_comment_write_xml (SheetObject const *so,
			XmlParseContext const *ctxt, xmlNodePtr tree)
{
	CellComment const *cc = CELL_COMMENT (so);
217 218
	xml_node_set_cstr (tree, "Author", cc->author);
	xml_node_set_cstr (tree, "Text", cc->text);
219 220 221
	return FALSE;
}

222
static void
223 224
cell_comment_print (SheetObject const *so, GnomePrintContext *ctx,
		    double base_x, double base_y)
225
{
226 227 228 229
	/*
	 * Nothing in here. This function is here to suppress a warning
	 * about an unprintable sheet object.
	 */
230 231
}

232 233 234 235 236 237 238 239 240 241 242 243 244 245
static SheetObject *
cell_comment_clone (SheetObject const *so, Sheet *sheet)
{
	CellComment *comment = CELL_COMMENT (so);
	CellComment *new_comment;

	new_comment = gtk_type_new (CELL_COMMENT_TYPE);

	new_comment->author = comment->author ? g_strdup (comment->author) : NULL;
	new_comment->text   = comment->text   ? g_strdup (comment->text) : NULL;
	
	return SHEET_OBJECT (new_comment);
}

246 247 248 249 250 251 252 253 254 255 256 257 258
static void
cell_comment_class_init (GtkObjectClass *object_class)
{
	SheetObjectClass *sheet_object_class = SHEET_OBJECT_CLASS (object_class);

	cell_comment_parent_class = gtk_type_class (sheet_object_get_type ());

	/* Object class method overrides */
	object_class->destroy = cell_comment_destroy;

	/* SheetObject class method overrides */
	sheet_object_class->update_bounds = &cell_comment_update_bounds;
	sheet_object_class->new_view	  = &cell_comment_new_view;
259 260
	sheet_object_class->read_xml      = &cell_comment_read_xml;
	sheet_object_class->write_xml     = &cell_comment_write_xml;
261
	sheet_object_class->print         = &cell_comment_print;
262
	sheet_object_class->clone         = &cell_comment_clone;
263 264
}

265 266
E_MAKE_TYPE (cell_comment, "CellComment", CellComment,
	     cell_comment_class_init, NULL, SHEET_OBJECT_TYPE);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

char const  *
cell_comment_author_get (CellComment const *cc)
{
	g_return_val_if_fail (IS_CELL_COMMENT (cc), NULL);
	return cc->author;
}

void
cell_comment_author_set (CellComment *cc, char const *author)
{
	char *tmp;
	g_return_if_fail (IS_CELL_COMMENT (cc));

	tmp = author ? g_strdup (author) : NULL;
	if (cc->author)
		g_free (cc->author);
	cc->author = tmp;
}

char const  *
cell_comment_text_get (CellComment const *cc)
{
	g_return_val_if_fail (IS_CELL_COMMENT (cc), NULL);
	return cc->text;
}

void
cell_comment_text_set (CellComment *cc, char const *text)
{
	char *tmp;
	g_return_if_fail (IS_CELL_COMMENT (cc));

	tmp = text ? g_strdup (text) : NULL;
	if (cc->text)
		g_free (cc->text);
	cc->text = tmp;
}

/* convenience routine */
CellComment *
cell_set_comment (Sheet *sheet, CellPos const *pos,
		  char const *author, char const *text)
{
	/* top right */
312 313 314 315 316
	static SheetObjectAnchorType const anchor_types [4] = {
		SO_ANCHOR_PERCENTAGE_FROM_COLROW_END,
		SO_ANCHOR_PERCENTAGE_FROM_COLROW_START,
		SO_ANCHOR_PERCENTAGE_FROM_COLROW_END,
		SO_ANCHOR_PERCENTAGE_FROM_COLROW_START
317
	};
318
	SheetObjectAnchor anchor;
319 320 321 322 323 324 325 326 327 328 329
	CellComment *cc;
	Range	     r;

	g_return_val_if_fail (IS_SHEET (sheet), NULL);
	g_return_val_if_fail (pos != NULL, NULL);

	cc = gtk_type_new (CELL_COMMENT_TYPE);
	cc->author = author ? g_strdup (author) : NULL;
	cc->text = text ? g_strdup (text) : NULL;

	r.start = r.end = *pos;
330 331 332
	sheet_object_anchor_init (&anchor, &r, NULL, anchor_types,
				  SO_DIR_DOWN_RIGHT);
	sheet_object_anchor_set (SHEET_OBJECT (cc), &anchor);
333
	sheet_object_set_sheet (SHEET_OBJECT (cc), sheet);
334 335 336 337 338 339
	return cc;
}

/*
 * Use this for hyperlinks gnome_url_show (url);
 */