summaryrefslogtreecommitdiff
path: root/libnautilus/nautilus-zoomable.c
blob: 9b997961fba7a9879d83cdd868a7f5b65b9771b0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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
123
124
125
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 *  libnautilus: A library for nautilus view implementations.
 *
 *  Copyright (C) 2000 Eazel, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Author: Maciej Stachowiak <mjs@eazel.com>
 *
 */

/* nautilus-zoomable.c */

#include <config.h>
#include "nautilus-zoomable.h"

#include <libnautilus-extensions/nautilus-gtk-macros.h>
#include <gtk/gtksignal.h>
#include <bonobo/bonobo-main.h>
#include <bonobo/bonobo-control.h>

NautilusZoomable *foo;

struct NautilusZoomableDetails {
	BonoboControl *control;
	
	gdouble zoom_level;
	gdouble min_zoom_level;
	gdouble max_zoom_level;
	gboolean is_continuous;
	
	Nautilus_ZoomableFrame zoomable_frame;
};

enum {
	SET_ZOOM_LEVEL,
	ZOOM_IN,
	ZOOM_OUT,
	ZOOM_TO_FIT,
	LAST_SIGNAL
};

enum {
	ARG_0,
	ARG_BONOBO_CONTROL,
	ARG_MIN_ZOOM_LEVEL,
	ARG_MAX_ZOOM_LEVEL,
	ARG_IS_CONTINUOUS,
};

static guint signals[LAST_SIGNAL];

typedef struct {
	POA_Nautilus_View servant;
	gpointer bonobo_object;
	
	NautilusZoomable *gtk_object;
} impl_POA_Nautilus_Zoomable;


void                 nautilus_zoomable_real_set_bonobo_control  (NautilusZoomable           *view,
								 BonoboControl              *bonobo_control);
static CORBA_double  impl_Nautilus_Zoomable__get_zoom_level     (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static void          impl_Nautilus_Zoomable__set_zoom_level     (impl_POA_Nautilus_Zoomable *servant,
								 const CORBA_double          zoom_level,
								 CORBA_Environment          *ev);
static CORBA_double  impl_Nautilus_Zoomable__get_min_zoom_level (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static CORBA_double  impl_Nautilus_Zoomable__get_max_zoom_level (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static CORBA_boolean impl_Nautilus_Zoomable__get_is_continuous (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static void          impl_Nautilus_Zoomable_zoom_in             (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static void          impl_Nautilus_Zoomable_zoom_out            (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);
static void          impl_Nautilus_Zoomable_zoom_to_fit         (impl_POA_Nautilus_Zoomable *servant,
								 CORBA_Environment          *ev);

POA_Nautilus_Zoomable__epv libnautilus_Nautilus_Zoomable_epv =
{
	NULL,			/* _private */
	(gpointer) &impl_Nautilus_Zoomable__get_zoom_level,
	(gpointer) &impl_Nautilus_Zoomable__set_zoom_level,
	(gpointer) &impl_Nautilus_Zoomable__get_min_zoom_level,
	(gpointer) &impl_Nautilus_Zoomable__get_max_zoom_level,
	(gpointer) &impl_Nautilus_Zoomable__get_is_continuous,
	(gpointer) &impl_Nautilus_Zoomable_zoom_in,
	(gpointer) &impl_Nautilus_Zoomable_zoom_out,
	(gpointer) &impl_Nautilus_Zoomable_zoom_to_fit
};

static PortableServer_ServantBase__epv base_epv;
static POA_Nautilus_Zoomable__vepv impl_Nautilus_Zoomable_vepv =
{
	&base_epv,
	NULL,
	&libnautilus_Nautilus_Zoomable_epv
};

static CORBA_double
impl_Nautilus_Zoomable__get_zoom_level (impl_POA_Nautilus_Zoomable *servant,
					CORBA_Environment          *ev)
{
	return servant->gtk_object->details->zoom_level;
}

static void 
impl_Nautilus_Zoomable__set_zoom_level (impl_POA_Nautilus_Zoomable *servant,
					const CORBA_double          zoom_level,
					CORBA_Environment          *ev)
{
	gtk_signal_emit (GTK_OBJECT (servant->gtk_object), signals[SET_ZOOM_LEVEL], zoom_level);
	
}

static CORBA_double
impl_Nautilus_Zoomable__get_min_zoom_level (impl_POA_Nautilus_Zoomable *servant,
					    CORBA_Environment      *ev)
{
	return servant->gtk_object->details->min_zoom_level;
}
static CORBA_double
impl_Nautilus_Zoomable__get_max_zoom_level (impl_POA_Nautilus_Zoomable *servant,
					    CORBA_Environment      *ev)
{
	return servant->gtk_object->details->max_zoom_level;
}

static CORBA_boolean
impl_Nautilus_Zoomable__get_is_continuous (impl_POA_Nautilus_Zoomable *servant,
					    CORBA_Environment      *ev)
{
	return servant->gtk_object->details->is_continuous;
}

static void
impl_Nautilus_Zoomable_zoom_in (impl_POA_Nautilus_Zoomable *servant,
				CORBA_Environment          *ev)
{	
	gtk_signal_emit (GTK_OBJECT (servant->gtk_object), signals[ZOOM_IN]);
}

static void
impl_Nautilus_Zoomable_zoom_out (impl_POA_Nautilus_Zoomable *servant,
				 CORBA_Environment          *ev)
{
	gtk_signal_emit (GTK_OBJECT (servant->gtk_object), signals[ZOOM_OUT]);
}

static void
impl_Nautilus_Zoomable_zoom_to_fit (impl_POA_Nautilus_Zoomable *servant,
				    CORBA_Environment      *ev)
{
	gtk_signal_emit (GTK_OBJECT (servant->gtk_object), signals[ZOOM_TO_FIT]);
}

static void
impl_Nautilus_Zoomable__destroy(BonoboObject *obj, impl_POA_Nautilus_Zoomable *servant)
{
	PortableServer_ObjectId *objid;
	CORBA_Environment ev;
	void (*servant_destroy_func) (PortableServer_Servant servant, CORBA_Environment *ev);
	
	CORBA_exception_init(&ev);
	
	servant_destroy_func = NAUTILUS_ZOOMABLE_CLASS (GTK_OBJECT (servant->gtk_object)->klass)->servant_destroy_func;
	objid = PortableServer_POA_servant_to_id (bonobo_poa (), servant, &ev);
	PortableServer_POA_deactivate_object (bonobo_poa (), objid, &ev);
	CORBA_free (objid);
	obj->servant = NULL;
	
	servant_destroy_func ((PortableServer_Servant) servant, &ev);
	g_free (servant);
	CORBA_exception_free(&ev);
}

static Nautilus_Zoomable
impl_Nautilus_Zoomable__create(NautilusZoomable *zoomable, CORBA_Environment * ev)
{
	Nautilus_Zoomable retval;
	impl_POA_Nautilus_Zoomable *servant;
	void (*servant_init_func) (PortableServer_Servant servant, CORBA_Environment *ev);
	
	NautilusZoomableClass *zoomable_class = NAUTILUS_ZOOMABLE_CLASS (GTK_OBJECT(zoomable)->klass);
	
	servant_init_func = zoomable_class->servant_init_func;
	servant = g_new0 (impl_POA_Nautilus_Zoomable, 1);
	servant->servant.vepv = zoomable_class->vepv;
	if (!servant->servant.vepv->Bonobo_Unknown_epv)
		servant->servant.vepv->Bonobo_Unknown_epv = bonobo_object_get_epv ();
	servant_init_func ((PortableServer_Servant) servant, ev);
	
	servant->gtk_object = zoomable;
	
	retval = bonobo_object_activate_servant (BONOBO_OBJECT (zoomable), servant);
	
	gtk_signal_connect (GTK_OBJECT (zoomable), "destroy", GTK_SIGNAL_FUNC (impl_Nautilus_Zoomable__destroy), servant);
	
	return retval;
}

static void nautilus_zoomable_initialize_class (NautilusZoomableClass *klass);
static void nautilus_zoomable_initialize       (NautilusZoomable      *zoomable);
static void nautilus_zoomable_destroy          (NautilusZoomable      *view);
static void nautilus_zoomable_set_arg          (GtkObject             *object,
						GtkArg                *arg,
						guint                  arg_id);
static void nautilus_zoomable_get_arg          (GtkObject             *object,
						GtkArg                *arg,
						guint                  arg_id);

NAUTILUS_DEFINE_CLASS_BOILERPLATE(NautilusZoomable, nautilus_zoomable, BONOBO_OBJECT_TYPE)

/* This would go into nautilus-gtk-extensions.c, but we don't want the dependency. */
static void
marshal_NONE__DOUBLE (GtkObject *object,
		      GtkSignalFunc func,
		      gpointer func_data,
		      GtkArg *args)
{
	(* (void (*)(GtkObject *, double, gpointer)) func)
		(object,
		 GTK_VALUE_DOUBLE (args[0]),
		 func_data);
}

static void
nautilus_zoomable_initialize_class (NautilusZoomableClass *klass)
{
	GtkObjectClass *object_class;
	
	object_class = (GtkObjectClass*) klass;
	object_class->destroy = (void (*)(GtkObject*))nautilus_zoomable_destroy;
	object_class->set_arg = nautilus_zoomable_set_arg;
	object_class->get_arg = nautilus_zoomable_get_arg;
	
	parent_class = gtk_type_class (gtk_type_parent (object_class->type));
	
	klass->servant_init_func = POA_Nautilus_Zoomable__init;
	klass->servant_destroy_func = POA_Nautilus_Zoomable__fini;
	klass->vepv = &impl_Nautilus_Zoomable_vepv;
	
	signals[SET_ZOOM_LEVEL] =
		gtk_signal_new ("set_zoom_level",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusZoomableClass, set_zoom_level),
				marshal_NONE__DOUBLE,
				GTK_TYPE_NONE, 1, GTK_TYPE_DOUBLE);
	signals[ZOOM_IN] = 
		gtk_signal_new ("zoom_in",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusZoomableClass, zoom_in),
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 0);
	signals[ZOOM_OUT] = 
		gtk_signal_new ("zoom_out",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusZoomableClass, zoom_out),
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 0);
	signals[ZOOM_TO_FIT] = 
		gtk_signal_new ("zoom_to_fit",
				GTK_RUN_LAST,
				object_class->type,
				GTK_SIGNAL_OFFSET (NautilusZoomableClass, zoom_to_fit),
				gtk_marshal_NONE__NONE,
				GTK_TYPE_NONE, 0);

	gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
	
	gtk_object_add_arg_type ("NautilusZoomable::bonobo_control",
				 GTK_TYPE_OBJECT,
				 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT | GTK_ARG_CONSTRUCT_ONLY,
				 ARG_BONOBO_CONTROL);
	gtk_object_add_arg_type ("NautilusZoomable::min_zoom_level",
				 GTK_TYPE_DOUBLE,
				 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT | GTK_ARG_CONSTRUCT_ONLY,
				 ARG_MIN_ZOOM_LEVEL);
	gtk_object_add_arg_type ("NautilusZoomable::max_zoom_level",
				 GTK_TYPE_DOUBLE,
				 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT | GTK_ARG_CONSTRUCT_ONLY,
				 ARG_MAX_ZOOM_LEVEL);
	gtk_object_add_arg_type ("NautilusZoomable::is_continuous",
				 GTK_TYPE_BOOL,
				 GTK_ARG_READWRITE | GTK_ARG_CONSTRUCT | GTK_ARG_CONSTRUCT_ONLY,
				 ARG_IS_CONTINUOUS);
}

static void
nautilus_zoomable_set_arg (GtkObject      *object,
			   GtkArg         *arg,
			   guint	   arg_id)
{
	NautilusZoomable *zoomable;
	
	zoomable = NAUTILUS_ZOOMABLE (object);
	
	switch(arg_id) {
	case ARG_BONOBO_CONTROL:
		nautilus_zoomable_real_set_bonobo_control
			(zoomable,
			 BONOBO_CONTROL (GTK_VALUE_OBJECT (*arg)));
		break;
	case ARG_MIN_ZOOM_LEVEL:
		zoomable->details->min_zoom_level = GTK_VALUE_DOUBLE (*arg);
		break;
	case ARG_MAX_ZOOM_LEVEL:
		zoomable->details->max_zoom_level = GTK_VALUE_DOUBLE (*arg);
		break;
	case ARG_IS_CONTINUOUS:
		zoomable->details->is_continuous = GTK_VALUE_BOOL (*arg);
		break;
	}
}

static void
nautilus_zoomable_get_arg (GtkObject      *object,
			   GtkArg         *arg,
			   guint	     arg_id)
{
	NautilusZoomable *view;
	
	view = NAUTILUS_ZOOMABLE (object);
	
	switch(arg_id) {
	case ARG_BONOBO_CONTROL:
		GTK_VALUE_OBJECT (*arg) = GTK_OBJECT (nautilus_zoomable_get_bonobo_control (NAUTILUS_ZOOMABLE (object)));
		break;
	case ARG_MIN_ZOOM_LEVEL:
		GTK_VALUE_DOUBLE (*arg) = NAUTILUS_ZOOMABLE (object)->details->min_zoom_level;
		break;
	case ARG_MAX_ZOOM_LEVEL:
		GTK_VALUE_DOUBLE (*arg) = NAUTILUS_ZOOMABLE (object)->details->max_zoom_level;
		break;
	case ARG_IS_CONTINUOUS:
		GTK_VALUE_BOOL (*arg) = NAUTILUS_ZOOMABLE (object)->details->is_continuous;
		break;
	}
}

static void
nautilus_zoomable_initialize (NautilusZoomable *zoomable)
{
	CORBA_Environment ev;
	CORBA_exception_init(&ev);
	
	zoomable->details = g_new0 (NautilusZoomableDetails, 1);
	
	bonobo_object_construct (BONOBO_OBJECT (zoomable), impl_Nautilus_Zoomable__create (zoomable, &ev));
	
	CORBA_exception_free(&ev);
}

NautilusZoomable *
nautilus_zoomable_new (GtkWidget *widget, 
		       gdouble min_zoom_level, 
		       gdouble max_zoom_level, 
		       gboolean is_continuous)
{
	return nautilus_zoomable_new_from_bonobo_control
		(bonobo_control_new (widget),
		 min_zoom_level, 
		 max_zoom_level, 
		 is_continuous);
}

NautilusZoomable *
nautilus_zoomable_new_from_bonobo_control (BonoboControl *bonobo_control,
					   gdouble min_zoom_level, 
					   gdouble max_zoom_level, 
					   gboolean is_continuous)
{
	NautilusZoomable *zoomable;
	
	zoomable = NAUTILUS_ZOOMABLE (gtk_object_new (NAUTILUS_TYPE_ZOOMABLE,
						      "bonobo_control", bonobo_control,
						      "min_zoom_level", min_zoom_level,
						      "max_zoom_level", max_zoom_level,
						      "is_continuous",  is_continuous,
						      NULL));
	
	return zoomable;
}

static void
nautilus_zoomable_destroy (NautilusZoomable *view)
{
	g_free (view->details);
	
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, GTK_OBJECT (view));
}

static gboolean
nautilus_zoomable_ensure_zoomable_frame (NautilusZoomable *view)
{
	CORBA_Environment ev;
	
	g_assert (NAUTILUS_IS_ZOOMABLE (view));
	
	CORBA_exception_init (&ev);
	
	if (CORBA_Object_is_nil (view->details->zoomable_frame, &ev)) {
		view->details->zoomable_frame = Bonobo_Unknown_query_interface 
			(bonobo_control_get_control_frame 
			 (BONOBO_CONTROL (nautilus_zoomable_get_bonobo_control (view))),
			 "IDL:Nautilus/ZoomableFrame:1.0", &ev);
	}
	
	if (ev._major != CORBA_NO_EXCEPTION) {
		view->details->zoomable_frame = CORBA_OBJECT_NIL;
	}
	
	if (CORBA_Object_is_nil (view->details->zoomable_frame, &ev)) {
		CORBA_exception_free (&ev);
		return FALSE;
	} else {
		CORBA_exception_free (&ev);
		return TRUE;
	}
}

void
nautilus_zoomable_set_zoom_level (NautilusZoomable *view,
				  double zoom_level)
{
	CORBA_Environment ev;
	
	g_return_if_fail (NAUTILUS_IS_ZOOMABLE (view));
	
	CORBA_exception_init (&ev);
	
	view->details->zoom_level = zoom_level;
	
	if (nautilus_zoomable_ensure_zoomable_frame (view)) {
		Nautilus_ZoomableFrame_report_zoom_level_changed (view->details->zoomable_frame, zoom_level, &ev);
		if (ev._major != CORBA_NO_EXCEPTION) {
			CORBA_Object_release (view->details->zoomable_frame, &ev);
			view->details->zoomable_frame = CORBA_OBJECT_NIL;
		}
	}
	
	CORBA_exception_free (&ev);
}

BonoboControl *
nautilus_zoomable_get_bonobo_control (NautilusZoomable *view)
{
	return view->details->control;
}

void
nautilus_zoomable_real_set_bonobo_control (NautilusZoomable *view,
					   BonoboControl *bonobo_control)
{
	view->details->control = bonobo_control;
	bonobo_object_add_interface (BONOBO_OBJECT (view), BONOBO_OBJECT (view->details->control));
}