sheet-object-container.c 5.69 KB
Newer Older
Jody Goldberg's avatar
Jody Goldberg committed
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
Arturo Espinosa's avatar
Arturo Espinosa committed
2 3
/*
 * sheet-object-container.c:
4
 *   SheetObject for containers (Bonobo, Graphs)
Arturo Espinosa's avatar
Arturo Espinosa committed
5 6 7
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
8
 *   Michael Meeks (mmeeks@gnu.org)
Arturo Espinosa's avatar
Arturo Espinosa committed
9
 */
10
#include <gnumeric-config.h>
Zbigniew Chyla's avatar
Zbigniew Chyla committed
11
#include <gnumeric-i18n.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
12
#include "gnumeric.h"
13 14
#include "sheet-object-container.h"

15
#include "workbook-control-gui-priv.h"
16
#include "sheet.h"
17
#include "gui-util.h"
18
#include "sheet-control-gui.h"
19
#include "gnumeric-canvas.h"
20
#include "sheet-object-widget.h"
21

22 23
#include <gdk/gdkkeysyms.h>
#include <math.h>
Jody Goldberg's avatar
Jody Goldberg committed
24
#include <gsf/gsf-impl-utils.h>
4's avatar
4 committed
25
#include <bonobo/bonobo-item-container.h>
26
#include <bonobo/bonobo-exception.h>
Arturo Espinosa's avatar
Arturo Espinosa committed
27

28
#ifdef GNOME2_CONVERSION_COMPLETE
29
static void
30
cb_user_activation_request (BonoboViewFrame *view_frame, GtkObject *so_view)
31
{
32 33 34 35
	SheetControlGUI *scg = sheet_object_view_control (so_view);
	SheetObject *so = sheet_object_view_obj (so_view);

	scg_mode_edit_object (scg, so);
36 37
}

38
static GtkObject *
39
sheet_object_container_new_view (SheetObject *so, SheetControlGUI *scg)
40
{
41
	/* FIXME : this is bogus */
42
	GnmCanvas *gcanvas = scg_pane (scg, 0);
43
	WorkbookControlGUI *wbcg = scg_get_wbcg	(scg);
44
	SheetObjectContainer *soc;
45
	BonoboViewFrame *view_frame;
46
	GtkWidget	*view_widget;
Jody Goldberg's avatar
Jody Goldberg committed
47
	FooCanvasItem *view_item;
48

49 50
	g_return_val_if_fail (IS_SHEET_OBJECT_CONTAINER (so), NULL);

51
	soc = SHEET_OBJECT_CONTAINER (so);
52

53
	view_frame = bonobo_client_site_new_view_full (
Jody Goldberg's avatar
Jody Goldberg committed
54
		SHEET_OBJECT_BONOBO (so)->control_frame,
55
		bonobo_ui_component_get_container (wbcg->uic), FALSE, FALSE);
Michael Meeks's avatar
Michael Meeks committed
56 57 58 59 60
	if (!view_frame) {
		g_warning ("Component died");
		return NULL;
	}

61
	view_widget = bonobo_view_frame_get_wrapper (view_frame);
Jody Goldberg's avatar
Jody Goldberg committed
62 63
	foo_canvas_item_raise_to_top (FOO_CANVAS_ITEM (gcanvas->sheet_object_group));
	view_item = foo_canvas_item_new (
64
		gcanvas->sheet_object_group,
Jody Goldberg's avatar
Jody Goldberg committed
65
		foo_canvas_widget_get_type (),
66 67 68
		"widget", view_widget,
		"size_pixels", FALSE,
		NULL);
69
	gtk_object_set_data (GTK_OBJECT (view_item), "view_frame", view_frame);
70
	g_signal_connect (G_OBJECT (view_frame),
Jody Goldberg's avatar
Jody Goldberg committed
71 72
		"user_activate",
		G_CALLBACK (cb_user_activation_request), view_item);
73

74 75 76 77
	scg_object_widget_register (so, view_widget, view_item);
	gtk_widget_show (view_widget);

	return GTK_OBJECT (view_item);
Arturo Espinosa's avatar
Arturo Espinosa committed
78 79
}

80 81 82 83
/*
 * This implemenation moves the widget rather than
 * destroying/updating/creating the views
 */
Arturo Espinosa's avatar
Arturo Espinosa committed
84
static void
85
sheet_object_container_update_bounds (SheetObject *so, GtkObject *view,
86
		                     SheetControlGUI *scg)
Arturo Espinosa's avatar
Arturo Espinosa committed
87
{
88 89 90 91
	double coords [4];

	/* NOTE : far point is EXCLUDED so we add 1 */
	scg_object_view_position (scg, so, coords);
Jody Goldberg's avatar
Jody Goldberg committed
92
	foo_canvas_item_set (FOO_CANVAS_ITEM (view),
93 94 95 96
		"x", coords [0], "y", coords [1],
		"width",  coords [2] - coords [0] + 1.,
		"height", coords [3] - coords [1] + 1.,
		NULL);
97 98

	if (so->is_visible)
Jody Goldberg's avatar
Jody Goldberg committed
99
		foo_canvas_item_show (FOO_CANVAS_ITEM (view));
100
	else
Jody Goldberg's avatar
Jody Goldberg committed
101
		foo_canvas_item_hide (FOO_CANVAS_ITEM (view));
102 103
}

104
static void
105
sheet_object_container_set_active (SheetObject *so, GtkObject *view,
106 107 108 109 110 111 112 113 114
				   gboolean active)
{
	BonoboViewFrame *view_frame;

	g_return_if_fail (IS_SHEET_OBJECT_CONTAINER (so));

	/*
	 * We need to ref the view_frame on activation because the view
	 * can get destroyed. Sure, we will be called during destruction, but
115
	 * at this point, the view_frame will already be gone and we won't
116
	 * be able to correctly deactivate.
117 118 119 120 121 122 123 124 125 126 127 128 129 130
	 */
	view_frame = gtk_object_get_data (GTK_OBJECT (view), "view_frame");

	if (active) {
		bonobo_object_ref (BONOBO_OBJECT (view_frame));
		bonobo_view_frame_view_activate (view_frame);
		bonobo_view_frame_set_covered (view_frame, FALSE);
	} else {
		bonobo_view_frame_view_deactivate (view_frame);
		bonobo_view_frame_set_covered (view_frame, TRUE);
		bonobo_object_unref (BONOBO_OBJECT (view_frame));
	}
}

Arturo Espinosa's avatar
Arturo Espinosa committed
131 132 133
static void
sheet_object_container_class_init (GtkObjectClass *object_class)
{
134
	SheetObjectClass *so_class;
Arturo Espinosa's avatar
Arturo Espinosa committed
135

136 137 138
	/* SheetObject class method overrides */
	so_class = SHEET_OBJECT_CLASS (object_class);
	so_class->new_view 	= sheet_object_container_new_view;
Jody Goldberg's avatar
Jody Goldberg committed
139
	so_class->update_view_bounds = sheet_object_container_update_bounds;
140
	so_class->set_active    = sheet_object_container_set_active;
Arturo Espinosa's avatar
Arturo Espinosa committed
141 142 143
}

SheetObject *
144
sheet_object_container_new (Workbook *wb)
145
{
146
	return sheet_object_container_new_object (wb, NULL);
147 148 149
}

SheetObject *
150
sheet_object_container_new_object (Workbook *wb, char const *object_id)
Arturo Espinosa's avatar
Arturo Espinosa committed
151
{
Jody Goldberg's avatar
Jody Goldberg committed
152
	SheetObjectContainer *c = g_object_new (SHEET_OBJECT_CONTAINER_TYPE, NULL);
153
	if (!sheet_object_bonobo_construct (
154
		SHEET_OBJECT_BONOBO (c), wb->priv->bonobo_container, object_id)) {
155 156 157
		gtk_object_destroy (GTK_OBJECT (c));
		return NULL;
	}
158

Arturo Espinosa's avatar
Arturo Espinosa committed
159 160
	return SHEET_OBJECT (c);
}
161

162
SheetObject *
163
sheet_object_container_new_file (Workbook *wb, char const *fname)
164
{
165
	CORBA_Environment ev;
166
	SheetObject *so = NULL;
167 168
	char        *msg;
	char        *iid;
169
	static char const *required_ids [] = {
170 171 172
		"IDL:Bonobo/Embeddable:1.0",
		NULL
	};
173

174
	iid = bonobo_directory_find_for_file (fname, required_ids, &msg);
175
	if (iid != NULL) {
176
		so = sheet_object_container_new_object (wb, iid);
177
		if (so == NULL) {
178
			msg = g_strdup_printf (_("can't create object for '%s'"), iid);
179 180
			/* FIXME : we should really provide a parent*/
			gnumeric_notice (NULL, GTK_MESSAGE_ERROR, msg);
181 182 183 184 185 186 187 188
		} else {
			CORBA_exception_init (&ev);
			sheet_object_bonobo_load_file (SHEET_OBJECT_BONOBO (so),
						       fname, &ev);
			if (BONOBO_EX (&ev)) {
				msg = g_strdup_printf (
					_("Could not load file: %s"),
					bonobo_exception_get_text (&ev));
189 190
				/* FIXME : we should really provide a parent*/
				gnumeric_notice (NULL, GTK_MESSAGE_ERROR, msg);
191 192 193
			}
			CORBA_exception_free (&ev);
		}
194 195
		g_free (iid);
	} else
196
		gnumeric_notice (NULL, GTK_MESSAGE_ERROR, msg);
197 198

	g_free (msg);
199 200 201

	return so;
}
202

Jody Goldberg's avatar
Jody Goldberg committed
203 204 205
GSF_CLASS (SheetObjectContainer, sheet_object_container,
	   sheet_object_container_class_init, NULL,
	   SHEET_OBJECT_BONOBO_TYPE);
206
#endif