summaryrefslogtreecommitdiff
path: root/components/image-viewer/nautilus-image-view.c
blob: 29dde29bd81aca7060e91824631ac4c7579f9dbe (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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
/*
 * Generic image loading embeddable using gdk-pixbuf.
 *
 * Author:
 *   Michael Meeks (mmeeks@gnu.org)
 *   Gene Z. Ragan (gzr@eazel.com)
 *   Martin Baulig (baulig@suse.de)
 *
 * TODO:
 *    Progressive loading.
 *    Do not display more than required
 *    Queue request-resize on image size change/load
 *    Save image
 *
 * Copyright 2000, Helixcode Inc.
 * Copyright 2000, SuSE GmbH.
 * Copyright 2000, Eazel, Inc.
 */
 
#include <config.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <gnome.h>
#include <liboaf/liboaf.h>

#include <bonobo.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gdk-pixbuf/gdk-pixbuf-loader.h>
#include <libart_lgpl/art_misc.h>
#include <libart_lgpl/art_affine.h>
#include <libart_lgpl/art_pixbuf.h>
#include <libart_lgpl/art_rgb_pixbuf_affine.h>
#include <libart_lgpl/art_alphagamma.h>

#include "io-png.h"

/*
 * Number of running objects
 */ 
static int running_objects = 0;
static BonoboGenericFactory *image_factory = NULL;

/*
 * BonoboObject data
 */
typedef struct {
	BonoboControl    *control;
	GdkPixbuf        *pixbuf;

	GtkWidget        *root;
	GtkWidget        *drawing_area;
        GtkWidget        *scrolled_window;
	GdkPixbuf        *scaled;
        gboolean          size_allocated;
	gboolean	  initial_flag;
	gboolean	  resize_flag;
	
	GdkPixbuf        *zoomed;
	float             zoom_level;
	BonoboZoomable   *zoomable;
} bonobo_object_data_t;

static void control_update (bonobo_object_data_t *bod);

/*
 * Releases an image
 */
static void
release_pixbuf (bonobo_object_data_t *bod)
{
	g_return_if_fail (bod != NULL);

	if (bod->pixbuf != NULL) {
		gdk_pixbuf_unref (bod->pixbuf);
	}
	bod->pixbuf = NULL;

	if (bod->zoomed != NULL) {
		gdk_pixbuf_unref (bod->zoomed);
	}
	bod->zoomed = NULL;

	if (bod->scaled != NULL) {
		gdk_pixbuf_unref (bod->scaled);
	}
	bod->scaled = NULL;
}

static void
control_destroy_callback (BonoboControl *control, bonobo_object_data_t *bod)
{
        if (bod == NULL) {
		return;
	}

	release_pixbuf (bod);

	if (bod->scrolled_window != NULL) {
		gtk_widget_destroy (bod->scrolled_window);
	}
	bod->scrolled_window = NULL;

	g_free (bod);

	running_objects--;
	if (running_objects > 0) {
		return;
	}
	/*
	 * When last object has gone unref the factory & quit.
	 */
	bonobo_object_unref (BONOBO_OBJECT (image_factory));
	gtk_main_quit ();
}

static GdkPixbuf *
get_pixbuf (bonobo_object_data_t *bod)
{
	g_return_val_if_fail (bod != NULL, NULL);

	if (bod->zoomed != NULL) {
		return bod->zoomed;
	} else if (bod->scaled != NULL) {
		return bod->scaled;
	} else {
		return bod->pixbuf;
	}
}

static void
render_pixbuf (GdkPixbuf *buf, GtkWidget *dest_widget, GdkRectangle *rect)
{
	g_return_if_fail (buf != NULL);

	if (!GTK_IS_DRAWING_AREA (dest_widget)) {
		g_warning ("Non drawing area widget");
		return;
	}

	/* No drawing area yet ! */
	if (dest_widget == NULL || dest_widget->window == NULL) {
		return;
	}

	/*
	 * Do not draw outside the region that we know how to display
	 */
	if (rect->x > gdk_pixbuf_get_width (buf) ||
	    rect->y > gdk_pixbuf_get_height (buf)) {
/*		g_warning ("Render outside range %d %d %d %d (%d, %d)", rect->x, rect->y,
			   gdk_pixbuf_get_width (buf), gdk_pixbuf_get_height (buf),
			   rect->width, rect->height);*/
		return;
	}

	/*
	 * Clip the draw region
	 */
	if (rect->x + rect->width > gdk_pixbuf_get_width (buf)) {
		rect->width = gdk_pixbuf_get_width (buf) - rect->x;
	}

	if (rect->y + rect->height > gdk_pixbuf_get_height (buf)) {
		rect->height = gdk_pixbuf_get_height (buf) - rect->y;
	}

	/* Draw into the exposed region. */
	if (gdk_pixbuf_get_has_alpha (buf)) {
		gdk_draw_rgb_32_image (dest_widget->window,
				       dest_widget->style->white_gc,
				       rect->x, rect->y,
				       rect->width,
				       rect->height,
				       GDK_RGB_DITHER_NORMAL,
				       gdk_pixbuf_get_pixels (buf)
				       + (gdk_pixbuf_get_rowstride (buf) * rect->y + rect->x * 4),
				       gdk_pixbuf_get_rowstride (buf));
	} else {
		gdk_draw_rgb_image (dest_widget->window,
				    dest_widget->style->white_gc,
				    rect->x, rect->y,
				    rect->width,
				    rect->height,
				    GDK_RGB_DITHER_NORMAL,
				    gdk_pixbuf_get_pixels (buf)
				    + (gdk_pixbuf_get_rowstride (buf) * rect->y + rect->x * 3),
				    gdk_pixbuf_get_rowstride (buf));
	}
}

static void
redraw_control (bonobo_object_data_t *bod, GdkRectangle *rect)
{
	GdkPixbuf *buf = get_pixbuf (bod);

	if (buf == NULL) {
		return;
	}

	/*
	 * Don't actually render unless our size has been allocated,
	 * so we don't screw up the size allocation process by drawing
	 * an unscaled image too early.
	 */
	if (bod->size_allocated) {
	        render_pixbuf (buf, bod->drawing_area, rect);
	}
}

static void
configure_size (bonobo_object_data_t *bod, GdkRectangle *rect)
{
	GdkPixbuf *buf = get_pixbuf (bod);

	if (buf == NULL) {
		return;
	}

	/*
	 * Don't configure the size if it hasn't gotten allocated, to
	 * avoid messing with size_allocate process.
	 */
	if (!bod->size_allocated) {
		gtk_widget_set_usize (bod->drawing_area,
				      gdk_pixbuf_get_width (buf),
				      gdk_pixbuf_get_height (buf));
	  
		rect->x = 0;
		rect->y = 0;
		rect->width  = gdk_pixbuf_get_width (buf);
		rect->height = gdk_pixbuf_get_height (buf);

		bod->size_allocated = TRUE;
	} else {
		GtkAllocation *a = &bod->drawing_area->allocation;
		rect->x = a->x;
		rect->y = a->y;
		rect->width  = a->width;
		rect->height = a->height;
	}
}

static float preferred_zoom_levels[] = {
	1.0 / 10.0, 1.0 / 9.0, 1.0 / 8.0, 1.0 / 7.0, 1.0 / 6.0,
	1.0 / 5.0, 1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 1.0, 2.0,
	3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0
};
static const gchar *preferred_zoom_level_names[] = {
	"1:10", "1:9", "1:8", "1:7", "1:6", "1:5", "1:4", "1:3",
	"1:2", "1:1", "2:1", "3:1", "4:1", "5:1", "6:1", "7:1",
	"8:1", "9:1", "10:1"
};

static const gint max_preferred_zoom_levels = (sizeof (preferred_zoom_levels) /
					       sizeof (float)) - 1;

static int
zoom_index_from_float (float zoom_level)
{
	int i;

	for (i = 0; i < max_preferred_zoom_levels; i++) {
		float this, epsilon;

		/* if we're close to a zoom level */
		this = preferred_zoom_levels [i];
		epsilon = this * 0.01;

		if (zoom_level < this+epsilon)
			return i;
	}

	return max_preferred_zoom_levels;
}

static float
zoom_level_from_index (int index)
{
	if (index > max_preferred_zoom_levels)
		index = max_preferred_zoom_levels;

	return preferred_zoom_levels [index];
}

static void
zoomable_zoom_in_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
{
	float this_zoom_level, new_zoom_level;
	int index;

	g_return_if_fail (bod != NULL);

	index = zoom_index_from_float (bod->zoom_level);
	if (index == max_preferred_zoom_levels)
		return;

	/* if we were zoomed to fit, we're not on one of the pre-defined level.
	 * We want to zoom into the next real level instead of skipping it
	 */
	this_zoom_level = zoom_level_from_index (index);
	
	if (this_zoom_level > bod->zoom_level) {
		new_zoom_level = this_zoom_level;
	} else {  
		index++;
		new_zoom_level = zoom_level_from_index (index);
	}
	
	gtk_signal_emit_by_name (GTK_OBJECT (zoomable), "set_zoom_level",
				 new_zoom_level);
}

static void
zoomable_zoom_out_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
{
	float new_zoom_level;
	int index;

	g_return_if_fail (bod != NULL);

	index = zoom_index_from_float (bod->zoom_level);
	if (index == 0)
		return;

	index--;
	new_zoom_level = zoom_level_from_index (index);

	gtk_signal_emit_by_name (GTK_OBJECT (zoomable), "set_zoom_level",
				 new_zoom_level);
}

static void
zoomable_zoom_to_fit_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
{
	GtkAdjustment *hadj, *vadj;
	float width, height;
	float x_level, y_level;
	float new_zoom_level;

	width = gdk_pixbuf_get_width (bod->pixbuf);
	height = gdk_pixbuf_get_height (bod->pixbuf);

	hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (bod->scrolled_window));
	vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (bod->scrolled_window));

	x_level = hadj->page_size / width;
	y_level = vadj->page_size / height;

	new_zoom_level = (x_level < y_level) ? x_level : y_level;

#if 0
	g_message ("zoom_to_fit: (%g,%g) - (%g,%g) - (%g,%g) - %g",
		   width, height, hadj->page_size, vadj->page_size,
		   x_level, y_level, new_zoom_level);
#endif

	if (new_zoom_level > 0) {
		gtk_signal_emit_by_name (GTK_OBJECT (zoomable), "set_zoom_level",
				 new_zoom_level);
	}
}

static void
zoomable_zoom_to_default_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
{
	gtk_signal_emit_by_name (GTK_OBJECT (zoomable), "set_zoom_level",
				 1.0);
}

static void
resize_control (bonobo_object_data_t *bod)
{
	GdkRectangle rect;

	g_return_if_fail (bod != NULL);

	/* Clear out old bitmap data in drawing area */
	if ((bod->drawing_area != NULL) &&
	    (bod->drawing_area->window != NULL)) {
		gdk_window_clear (bod->drawing_area->window);
	}
		
	/* Update scrollbar size and postion */
	bod->size_allocated = FALSE;
	configure_size (bod, &rect);
	
	gtk_widget_queue_resize (bod->root);
}

static void
rezoom_control (bonobo_object_data_t *bod, float new_zoom_level)
{
	const GdkPixbuf *pixbuf;

	float old_width, old_height;
	float new_width, new_height;

	pixbuf = bod->pixbuf;
	old_width = gdk_pixbuf_get_width (pixbuf);
	old_height = gdk_pixbuf_get_height (pixbuf);

	new_width = old_width * new_zoom_level;
	new_height = old_height * new_zoom_level;

	if (bod->zoomed)
		gdk_pixbuf_unref (bod->zoomed);

	if (new_width >= 1 && new_height >= 1) {
		bod->zoomed = gdk_pixbuf_scale_simple (pixbuf, new_width, 
						       new_height, ART_FILTER_NEAREST);
	}

	resize_control (bod);
}

static void
zoomable_set_zoom_level_callback (BonoboZoomable *zoomable, float new_zoom_level,
				  bonobo_object_data_t *bod)
{
	g_return_if_fail (bod != NULL);

	rezoom_control (bod, new_zoom_level);
	bod->zoom_level = new_zoom_level;

	control_update (bod);

	bonobo_zoomable_report_zoom_level_changed (bod->zoomable,
						   new_zoom_level);
}

static void
control_update (bonobo_object_data_t *bod)
{
	GdkRectangle rect;

	g_return_if_fail (bod != NULL);

	configure_size (bod, &rect);
		
	redraw_control (bod, &rect);
}

/*
 * Loads a png to a Bonobo_Stream
 */
static void
save_image_to_stream (BonoboPersistStream *ps, Bonobo_Stream stream,
		      Bonobo_Persist_ContentType type, void *data,
		      CORBA_Environment *ev)
{
	bonobo_object_data_t *bod = data;

	if (bod->pixbuf == NULL) {
		return;
	}

	image_save (stream, bod->pixbuf, ev);
}

/*
 * Loads an Image from a Bonobo_Stream
 */
static void
load_image_from_stream (BonoboPersistStream *ps, Bonobo_Stream stream,
			Bonobo_Persist_ContentType type, void *data,
			CORBA_Environment *ev)
{
	bonobo_object_data_t *bod = data;
	GdkPixbufLoader      *loader = gdk_pixbuf_loader_new ();
	Bonobo_Stream_iobuf  *buffer;
	CORBA_long            len;

	/* Free old data */
	release_pixbuf (bod);

	/* Load new data from stream */
	do {
		Bonobo_Stream_read (stream, 4096, &buffer, ev);
		if (ev->_major != CORBA_NO_EXCEPTION) {
			gdk_pixbuf_loader_close (loader);
			gtk_object_unref (GTK_OBJECT (loader));
			return;
		}
		
		if (buffer->_buffer != NULL && 
		    !gdk_pixbuf_loader_write (loader, buffer->_buffer, buffer->_length)) {
			CORBA_free (buffer);
				if (ev->_major != CORBA_NO_EXCEPTION) {
					CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
										 ex_Bonobo_Persist_WrongDataType, NULL);
				}				
				gdk_pixbuf_loader_close (loader);
				gtk_object_unref (GTK_OBJECT (loader));
				return;
		}
		
		len = buffer->_length;
		CORBA_free (buffer);
	} while (len > 0);

	gdk_pixbuf_loader_close (loader);
	bod->pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);

	if (bod->pixbuf == NULL) {
		CORBA_exception_set (ev, CORBA_USER_EXCEPTION, ex_Bonobo_Persist_WrongDataType, NULL);
		gtk_object_unref (GTK_OBJECT (loader));
	} else {
		gdk_pixbuf_ref (bod->pixbuf);
		resize_control (bod);
	}
}

static int
drawing_area_exposed (GtkWidget *widget, GdkEventExpose *event,
		      bonobo_object_data_t *bod)
{
	if (bod->pixbuf == NULL) {
		return TRUE;
	}
	
	redraw_control (bod, &event->area);

	return TRUE;
}

/*
 * This callback will be invoked when the container assigns us a size.
 */
static void
control_size_allocate_callback (GtkWidget *drawing_area, GtkAllocation *allocation,
				bonobo_object_data_t *bod)
{
	const GdkPixbuf *buf;
	GdkPixbuf       *control_buf;
	GdkInterpType    type;

	g_return_if_fail (bod != NULL);
	g_return_if_fail (allocation != NULL);

	bod->size_allocated = TRUE;

	if (bod->pixbuf == NULL) {
		return;
	}

	buf = bod->pixbuf;

	if (allocation->width  == gdk_pixbuf_get_width (buf) &&
	    allocation->height == gdk_pixbuf_get_height (buf)) {
		if (bod->scaled != NULL) {
			gdk_pixbuf_unref (bod->scaled);
			bod->scaled = NULL;
		}
		return;
	}

	control_buf = bod->scaled;
	if (control_buf != NULL) {
		if (allocation->width  == gdk_pixbuf_get_width (control_buf) &&
		    allocation->height == gdk_pixbuf_get_height (control_buf)) {
			return;
		} else {
			bod->scaled = NULL;
			gdk_pixbuf_unref (control_buf);
			control_buf = NULL;
		}
	}

	/* Too slow below this */
	if (allocation->width < gdk_pixbuf_get_width (buf) / 4 ||
	    allocation->width < gdk_pixbuf_get_width (buf) / 4)
		type = ART_FILTER_NEAREST;
	else
		type = ART_FILTER_TILES;

	if (allocation->width >= 1 && allocation->height >= 1) {
		bod->scaled = gdk_pixbuf_scale_simple (buf, allocation->width,
					       	       allocation->height, type);
	}
	
	control_update (bod);
}


/*
 * This callback will be invoked when the container assigns us a size.
 */
static void
scrolled_control_size_allocate_callback (GtkWidget *drawing_area,
					 GtkAllocation *allocation,
					 bonobo_object_data_t *bod)
{	
	control_update (bod);
}

/* utility routine to determine if the image is bigger than the current viewer */
static gboolean
image_bigger_than_viewer (bonobo_object_data_t *bod)
{
	int width, height;
	GtkAdjustment *hadj, *vadj;
	
	if (bod->pixbuf == NULL) {
		return FALSE;
	}
	
	width = gdk_pixbuf_get_width (bod->pixbuf);
	height = gdk_pixbuf_get_height (bod->pixbuf);

	hadj = gtk_scrolled_window_get_hadjustment (GTK_SCROLLED_WINDOW (bod->scrolled_window));
	vadj = gtk_scrolled_window_get_vadjustment (GTK_SCROLLED_WINDOW (bod->scrolled_window));

	if (hadj == NULL || vadj == NULL) {
		return FALSE;
	}
	
	return width > hadj->page_size || height > vadj->page_size;
}

/*
 * This callback will be invoked when the container assigns us a size.
 */
static void
scrolled_window_size_allocate_callback (GtkWidget *drawing_area,
					 GtkAllocation *allocation,
					 bonobo_object_data_t *bod)
{	
	/* implement initial shrink-to-fit if necessary.  It's hard to tell when resizing
	 * is complete, inspiring this hackish solution determining when; it should
	 * be replaced with a cleaner approach when the framework is improve.
	 */

	if (bod->resize_flag && bod->initial_flag && allocation->width > 1 && allocation->height > 1) {
		if (image_bigger_than_viewer (bod)) {
			zoomable_zoom_to_fit_callback (bod->zoomable, bod);		
			control_update (bod);
		}
		bod->initial_flag = FALSE;
	} else if (!bod->resize_flag && allocation->width == 1 && allocation->height == 1) {
		bod->resize_flag = TRUE;
	}
}

static void
control_activate_callback (BonoboControl *control, gboolean activate, gpointer data)
{
	/*
	 * Notify the ControlFrame that we accept to be activated or
	 * deactivated (we are an acquiescent BonoboControl, yes we are).
	 */
	bonobo_control_activate_notify (control, activate);
}

static bonobo_object_data_t *
control_factory_common (GtkWidget *scrolled_window)
{
	BonoboPersistStream *stream;
	bonobo_object_data_t *bod;

	bod = g_new0 (bonobo_object_data_t, 1);
	bod->scaled = NULL;
	bod->zoomed = NULL;
	bod->zoom_level = 1.0;
	bod->drawing_area = gtk_drawing_area_new ();
	bod->size_allocated = FALSE;
	bod->scrolled_window = scrolled_window;

	/* set flags that control initial shrink-to-fit */
	bod->initial_flag = TRUE;
	bod->resize_flag = FALSE;
	
	gtk_signal_connect (GTK_OBJECT (bod->drawing_area),
			    "expose_event",
			    GTK_SIGNAL_FUNC (drawing_area_exposed), bod);

	if (scrolled_window) {
		bod->root = scrolled_window;
		gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (bod->root), 
						       bod->drawing_area);
	} else
		bod->root = bod->drawing_area;

	gtk_widget_show_all (bod->root);
	bod->control = bonobo_control_new (bod->root);

	gtk_signal_connect (GTK_OBJECT (bod->control), "destroy",
			    GTK_SIGNAL_FUNC (control_destroy_callback), bod);

	bod->zoomable = bonobo_zoomable_new ();

	gtk_signal_connect (GTK_OBJECT (bod->zoomable), "set_zoom_level",
			    GTK_SIGNAL_FUNC (zoomable_set_zoom_level_callback), bod);
	gtk_signal_connect (GTK_OBJECT (bod->zoomable), "zoom_in",
			    GTK_SIGNAL_FUNC (zoomable_zoom_in_callback), bod);
	gtk_signal_connect (GTK_OBJECT (bod->zoomable), "zoom_out",
			    GTK_SIGNAL_FUNC (zoomable_zoom_out_callback), bod);
	gtk_signal_connect (GTK_OBJECT (bod->zoomable), "zoom_to_fit",
			    GTK_SIGNAL_FUNC (zoomable_zoom_to_fit_callback), bod);
	gtk_signal_connect (GTK_OBJECT (bod->zoomable), "zoom_to_default",
			    GTK_SIGNAL_FUNC (zoomable_zoom_to_default_callback), bod);

	bod->zoom_level = 1.0;
	bonobo_zoomable_set_parameters_full (bod->zoomable,
					     bod->zoom_level,
					     preferred_zoom_levels [0],
					     preferred_zoom_levels [max_preferred_zoom_levels],
					     FALSE, FALSE, TRUE,
					     preferred_zoom_levels,
					     preferred_zoom_level_names,
					     max_preferred_zoom_levels + 1);


	bonobo_object_add_interface (BONOBO_OBJECT (bod->control),
				     BONOBO_OBJECT (bod->zoomable));

	gtk_signal_connect (GTK_OBJECT (bod->control), "activate",
			    GTK_SIGNAL_FUNC (control_activate_callback), bod);

	/*
	 * Interface Bonobo::PersistStream 
	 */
	stream = bonobo_persist_stream_new (load_image_from_stream, 
					    save_image_to_stream, 
					    NULL, NULL, bod);
	bonobo_object_add_interface (BONOBO_OBJECT (bod->control),
				     BONOBO_OBJECT (stream));

	running_objects++;

        return bod;
}

static bonobo_object_data_t *
scaled_control_factory (void)
{
        bonobo_object_data_t *bod;

	bod = control_factory_common (NULL);

	gtk_signal_connect (GTK_OBJECT (bod->drawing_area), "size_allocate",
			    GTK_SIGNAL_FUNC (control_size_allocate_callback), bod);

        return bod;
}

static bonobo_object_data_t *
scrollable_control_factory (void)
{
	bonobo_object_data_t *bod;
	GtkWidget *scroll;

	scroll = gtk_scrolled_window_new (NULL, NULL);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
					GTK_POLICY_ALWAYS,
					GTK_POLICY_ALWAYS);

	bod = control_factory_common (scroll);

	gtk_signal_connect (GTK_OBJECT (bod->drawing_area), "size_allocate",
			    GTK_SIGNAL_FUNC (scrolled_control_size_allocate_callback),
			    bod);
	
	gtk_signal_connect (GTK_OBJECT (bod->scrolled_window), "size_allocate",
			    GTK_SIGNAL_FUNC (scrolled_window_size_allocate_callback),
			    bod);

        return bod;
}

static BonoboObject *
bonobo_object_factory (BonoboGenericFactory *this, const char *oaf_iid,
		       void *data)
{
	bonobo_object_data_t *bod;

	g_return_val_if_fail (this != NULL, NULL);

	/*
	 * Creates the BonoboObject server
	 */

	if (strcmp (oaf_iid, "OAFIID:nautilus-image-generic:6ed7ef0d-9274-4132-9a27-9f048142782f") == 0) {
		bod = scaled_control_factory ();
	} else if (strcmp (oaf_iid, "OAFIID:nautilus-image-viewer:30686633-23d5-422b-83c6-4f1b06f8abcd") == 0) {
		bod = scrollable_control_factory ();
	} else {
		return NULL;
	}

	if (bod == NULL) {
		return NULL;
	} else {
		return BONOBO_OBJECT (bod->control);
	}
}

static void
init_bonobo_image_generic_factory (void)
{
        char *registration_id;

	registration_id = oaf_make_registration_id ("OAFIID:nautilus_image_view_factory:61ea9ab1-e4b4-4da8-8f54-61cf6f33c4f6", g_getenv ("DISPLAY"));

	image_factory = bonobo_generic_factory_new_multi 
		(registration_id,
		 bonobo_object_factory, NULL);

	g_free (registration_id);
}

static void
init_server_factory (int argc, char **argv)
{
	CORBA_Environment ev;
	CORBA_exception_init (&ev);

        gnome_init_with_popt_table("bonobo-image-generic", VERSION,
				   argc, argv,
				   oaf_popt_options, 0, NULL); 
	oaf_init (argc, argv);

	if (!bonobo_init (CORBA_OBJECT_NIL, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL))
		g_error (_("I could not initialize Bonobo"));

	CORBA_exception_free (&ev);
}

int
main (int argc, char *argv [])
{
	init_server_factory (argc, argv);

	init_bonobo_image_generic_factory ();

	gtk_widget_set_default_colormap (gdk_rgb_get_cmap ());
	gtk_widget_set_default_visual   (gdk_rgb_get_visual ());

	bonobo_main ();
	
	return 0;
}