nautilus-image-view.c 20.8 KB
Newer Older
1
2
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

3
4
5
6
7
8
/*
 * Generic image loading embeddable using gdk-pixbuf.
 *
 * Author:
 *   Michael Meeks (mmeeks@gnu.org)
 *   Gene Z. Ragan (gzr@eazel.com)
9
 *   Martin Baulig (baulig@suse.de)
10
11
12
13
14
15
16
17
 *
 * TODO:
 *    Progressive loading.
 *    Do not display more than required
 *    Queue request-resize on image size change/load
 *    Save image
 *
 * Copyright 2000, Helixcode Inc.
18
 * Copyright 2000, SuSE GmbH.
19
20
21
22
23
24
25
26
27
28
29
 * 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>
30
#include <bonobo-activation/bonobo-activation.h>
31
32
33

#include <bonobo.h>

34
#include <eel/eel-debug.h>
35
36
37
38
39
40
41
42
43
44
#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"

45
46
#define LOAD_BUFFER_SIZE 65536

47
48
49
50
/*
 * Number of running objects
 */ 
static int running_objects = 0;
51
static BonoboGenericFactory *image_factory = NULL;
52
53
54
55
56

/*
 * BonoboObject data
 */
typedef struct {
57
	BonoboControl    *control;
58
	GdkPixbuf        *pixbuf;
59
60
61
62
63
64

	GtkWidget        *root;
	GtkWidget        *drawing_area;
        GtkWidget        *scrolled_window;
	GdkPixbuf        *scaled;
        gboolean          size_allocated;
65
66
	gboolean	  got_initial_size;
	gboolean	  got_penultimate_allocation;
67
	
68
69
70
	GdkPixbuf        *zoomed;
	float             zoom_level;
	BonoboZoomable   *zoomable;
71
72
} bonobo_object_data_t;

73
static void control_update (bonobo_object_data_t *bod);
74
75
76
77
78
79
80
81
82
83

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

	if (bod->pixbuf != NULL) {
84
		g_object_unref (G_OBJECT (bod->pixbuf));
85
86
	}
	bod->pixbuf = NULL;
87
88

	if (bod->zoomed != NULL) {
89
		g_object_unref (G_OBJECT (bod->zoomed));
90
91
92
93
	}
	bod->zoomed = NULL;

	if (bod->scaled != NULL) {
94
		g_object_unref (G_OBJECT (bod->scaled));
95
96
	}
	bod->scaled = NULL;
97
98
99
}

static void
100
control_destroy_callback (BonoboControl *control, bonobo_object_data_t *bod)
101
102
103
104
105
106
107
{
        if (bod == NULL) {
		return;
	}

	release_pixbuf (bod);

Mike Engber's avatar
Mike Engber committed
108
109
110
	if (bod->drawing_area != NULL) {
		gtk_widget_unref (bod->drawing_area);
		bod->drawing_area = NULL;
111
	}
Mike Engber's avatar
Mike Engber committed
112
113

	bod->root = NULL;
114
115
	bod->scrolled_window = NULL;

116
117
118
119
120
121
122
123
124
125
126
127
128
129
	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 *
130
get_pixbuf (bonobo_object_data_t *bod)
131
{
132
	g_return_val_if_fail (bod != NULL, NULL);
133

134
135
136
137
	if (bod->zoomed != NULL) {
		return bod->zoomed;
	} else if (bod->scaled != NULL) {
		return bod->scaled;
138
139
140
141
142
143
	} else {
		return bod->pixbuf;
	}
}

static void
144
render_pixbuf (GdkPixbuf *buf, GtkWidget *dest_widget, GdkRectangle *rect)
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
{
	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
205
redraw_control (bonobo_object_data_t *bod, GdkRectangle *rect)
206
{
207
	GdkPixbuf *buf = get_pixbuf (bod);
208

209
210
211
	if (buf == NULL) {
		return;
	}
212
213
214
215
216
217

	/*
	 * 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.
	 */
218
	if (bod->got_initial_size) {
219
		render_pixbuf (buf, bod->drawing_area, rect);
220
221
222
223
	}
}

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

228
229
230
	if (buf == NULL) {
		return;
	}
231
232
233
234
235

	/*
	 * Don't configure the size if it hasn't gotten allocated, to
	 * avoid messing with size_allocate process.
	 */
236
237
	if (!bod->size_allocated) {
		gtk_widget_set_usize (bod->drawing_area,
238
239
240
241
242
243
244
245
				      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);

246
		bod->size_allocated = TRUE;
247
	} else {
248
		GtkAllocation *a = &bod->drawing_area->allocation;
249
250
251
252
253
254
255
		rect->x = a->x;
		rect->y = a->y;
		rect->width  = a->width;
		rect->height = a->height;
	}
}

256
static float preferred_zoom_levels[] = {
257
258
259
	1.0 / 10.0,  1.0 / 8.0, 1.0 / 6.0, 1.0 / 5.0, 
	1.0 / 4.0, 1.0 / 3.0, 1.0 / 2.0, 2.0 / 3.0, 1.0, 1.5, 2.0,
	3.0, 4.0, 5.0, 6.0, 8.0, 10.0
260
261
};
static const gchar *preferred_zoom_level_names[] = {
262
263
264
	"1:10", "1:8", "1:6", "1:5", "1:4", "1:3",
	"1:2", "2:3",  "1:1", "3:2", "2:1", "3:1", "4:1", "5:1", "6:1",
	"8:1", "10:1"
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
};

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];
}

298
static void
299
zoomable_zoom_in_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
300
{
301
	float this_zoom_level, new_zoom_level;
302
303
304
305
306
307
308
309
	int index;

	g_return_if_fail (bod != NULL);

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

310
311
312
313
314
315
316
317
318
319
320
321
	/* 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);
	}
	
322
	g_signal_emit_by_name (zoomable, "set_zoom_level",
323
324
				 new_zoom_level);
}
325

326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
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);

341
	g_signal_emit_by_name (zoomable, "set_zoom_level",
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
				 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;
363
	if (new_zoom_level > 0) {
364
		g_signal_emit_by_name (zoomable, "set_zoom_level",
365
				 new_zoom_level);
366
	}
367
368
369
370
371
}

static void
zoomable_zoom_to_default_callback (BonoboZoomable *zoomable, bonobo_object_data_t *bod)
{
372
	g_signal_emit_by_name (zoomable, "set_zoom_level",
373
374
				 1.0);
}
375

376
377
378
379
380
381
static void
resize_control (bonobo_object_data_t *bod)
{
	GdkRectangle rect;

	g_return_if_fail (bod != NULL);
382
383

	/* Clear out old bitmap data in drawing area */
384
385
386
	if ((bod->drawing_area != NULL) &&
	    (bod->drawing_area->window != NULL)) {
		gdk_window_clear (bod->drawing_area->window);
387
	}
388
389
390
391
		
	/* Update scrollbar size and postion */
	bod->size_allocated = FALSE;
	configure_size (bod, &rect);
392
	
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
	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)
412
		g_object_unref (G_OBJECT (bod->zoomed));
413

414
415
	if (new_width >= 1 && new_height >= 1) {
		bod->zoomed = gdk_pixbuf_scale_simple (pixbuf, new_width, 
416
						       new_height, GDK_INTERP_BILINEAR);
417
	}
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432

	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);

Michael Meeks's avatar
Michael Meeks committed
433
434
	bonobo_zoomable_report_zoom_level_changed (
		bod->zoomable, new_zoom_level, NULL);
435
436
437
}

static void
438
control_update (bonobo_object_data_t *bod)
439
440
441
{
	GdkRectangle rect;

442
	g_return_if_fail (bod != NULL);
443

444
	configure_size (bod, &rect);
445
		
446
	redraw_control (bod, &rect);
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
}

/*
 * 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 {
484
		Bonobo_Stream_read (stream, LOAD_BUFFER_SIZE, &buffer, ev);
485
486
		if (ev->_major != CORBA_NO_EXCEPTION) {
			gdk_pixbuf_loader_close (loader);
487
			g_object_unref (G_OBJECT (loader));
488
489
			return;
		}
490
491
492
		
		if (buffer->_buffer != NULL && 
		    !gdk_pixbuf_loader_write (loader, buffer->_buffer, buffer->_length)) {
493
			CORBA_free (buffer);
494
495
496
497
				if (ev->_major != CORBA_NO_EXCEPTION) {
					CORBA_exception_set (ev, CORBA_USER_EXCEPTION,
										 ex_Bonobo_Persist_WrongDataType, NULL);
				}				
498
				gdk_pixbuf_loader_close (loader);
499
				g_object_unref (G_OBJECT (loader));
500
501
				return;
		}
502
		
503
504
505
506
		len = buffer->_length;
		CORBA_free (buffer);
	} while (len > 0);

507
	gdk_pixbuf_loader_close (loader);
508
509
510
511
	bod->pixbuf = gdk_pixbuf_loader_get_pixbuf (loader);

	if (bod->pixbuf == NULL) {
		CORBA_exception_set (ev, CORBA_USER_EXCEPTION, ex_Bonobo_Persist_WrongDataType, NULL);
512
		g_object_unref (G_OBJECT (loader));
513
	} else {
514
		g_object_ref (G_OBJECT (bod->pixbuf));
515
516
517
518
519
520
		
		/* Restore current zoomed pixbuf cache. */
		if (bod->zoom_level != 1.0) {
			rezoom_control (bod, bod->zoom_level);
		}
		
521
		resize_control (bod);
522
523
524
525
	}
}

static int
526
527
drawing_area_exposed (GtkWidget *widget, GdkEventExpose *event,
		      bonobo_object_data_t *bod)
528
{
529
	if (bod->pixbuf == NULL) {
530
531
532
		return TRUE;
	}
	
533
	redraw_control (bod, &event->area);
534
535
536
537
538
539
540
541

	return TRUE;
}

/*
 * This callback will be invoked when the container assigns us a size.
 */
static void
542
543
control_size_allocate_callback (GtkWidget *drawing_area, GtkAllocation *allocation,
				bonobo_object_data_t *bod)
544
545
{
	const GdkPixbuf *buf;
546
	GdkPixbuf       *control_buf;
547
	
548
	g_return_if_fail (bod != NULL);
549
550
	g_return_if_fail (allocation != NULL);

551
	bod->size_allocated = TRUE;
552

553
	if (bod->pixbuf == NULL) {
554
555
556
		return;
	}

557
	buf = bod->pixbuf;
558
559
560

	if (allocation->width  == gdk_pixbuf_get_width (buf) &&
	    allocation->height == gdk_pixbuf_get_height (buf)) {
561
		if (bod->scaled != NULL) {
562
			g_object_unref (G_OBJECT (bod->scaled));
563
			bod->scaled = NULL;
564
565
566
567
		}
		return;
	}

568
569
570
571
	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)) {
572
573
			return;
		} else {
574
			bod->scaled = NULL;
575
			g_object_unref (G_OBJECT (control_buf));
576
			control_buf = NULL;
577
578
		}
	}
579
	
580
581
	if (allocation->width >= 1 && allocation->height >= 1) {
		bod->scaled = gdk_pixbuf_scale_simple (buf, allocation->width,
582
					       	       allocation->height, GDK_INTERP_BILINEAR);
583
584
	}
	
585
	control_update (bod);
586
587
588
589
590
591
}

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

599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
/*
 * determine if the image is larger than the display area *
 */
static gboolean
image_fits_in_container (bonobo_object_data_t *bod)
{
	GtkAdjustment *hadj, *vadj;
	float width, height;

	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));

	return width <= hadj->page_size && height <= vadj->page_size;
}
 
617
618
619
620
621
/*
 * This callback will be invoked when the container assigns us a size.
 */
static void
scrolled_window_size_allocate_callback (GtkWidget *drawing_area,
622
623
					GtkAllocation *allocation,
					bonobo_object_data_t *bod)
624
{	
625
626
627
628
	/* 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 improved.
629
	 */
630

631
632
633
634
635
	if (bod->got_penultimate_allocation
	    && !bod->got_initial_size
	    && allocation->width > 1
	    && allocation->height > 1) {
		bod->got_initial_size = TRUE;
636
637
638
	 	if (!image_fits_in_container (bod)) {
			zoomable_zoom_to_fit_callback (bod->zoomable, bod);
	 	}
639
640
641
642
643
		gtk_widget_queue_draw (bod->drawing_area);
	} else if (!bod->got_penultimate_allocation
		   && allocation->width == 1
		   && allocation->height == 1) {
		bod->got_penultimate_allocation = TRUE;
644
645
646
	}
}

647
648
649
650
651
652
653
654
655
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);
}
656

657
658
static bonobo_object_data_t *
control_factory_common (GtkWidget *scrolled_window)
659
{
660
661
	BonoboPersistStream *stream;
	bonobo_object_data_t *bod;
662

663
664
665
666
	bod = g_new0 (bonobo_object_data_t, 1);
	bod->zoom_level = 1.0;
	bod->drawing_area = gtk_drawing_area_new ();
	bod->scrolled_window = scrolled_window;
667

668
	g_signal_connect (bod->drawing_area,
669
			    "expose_event",
670
			    G_CALLBACK (drawing_area_exposed), bod);
671
672

	if (scrolled_window) {
673
674
675
		bod->root = scrolled_window;
		gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (bod->root), 
						       bod->drawing_area);
676
	} else {
677
		bod->root = bod->drawing_area;
678
679
		bod->got_initial_size = TRUE;
	}
680

681
682
	gtk_widget_show_all (bod->root);
	bod->control = bonobo_control_new (bod->root);
683

Mike Engber's avatar
Mike Engber committed
684
685
686
687
688
689
690
691
	/* We ref the drawing area to keep its memory from being freed when it
	 * is destroyed. The control may live on after the widget is destroyed
	 * and if it has been freed, tests like:
	 *  (bod->drawing_area != NULL) && (bod->drawing_area->window != NULL)
	 * won't work.
	 */
	gtk_widget_ref (bod->drawing_area);

692
	g_signal_connect (bod->control, "destroy",
693
			    G_CALLBACK (control_destroy_callback), bod);
694

695
	bod->zoomable = bonobo_zoomable_new ();
696

697
	g_signal_connect (bod->zoomable, "set_zoom_level",
698
			    G_CALLBACK (zoomable_set_zoom_level_callback), bod);
699
	g_signal_connect (bod->zoomable, "zoom_in",
700
			    G_CALLBACK (zoomable_zoom_in_callback), bod);
701
	g_signal_connect (bod->zoomable, "zoom_out",
702
			    G_CALLBACK (zoomable_zoom_out_callback), bod);
703
	g_signal_connect (bod->zoomable, "zoom_to_fit",
704
			    G_CALLBACK (zoomable_zoom_to_fit_callback), bod);
705
	g_signal_connect (bod->zoomable, "zoom_to_default",
706
			    G_CALLBACK (zoomable_zoom_to_default_callback), bod);
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721

	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));

722
	g_signal_connect (bod->control, "activate",
723
			    G_CALLBACK (control_activate_callback), bod);
724
725
726
727
728
729
730
731
732

	/*
	 * 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));
733
734
735

	running_objects++;

736
        return bod;
737
738
}

739
740
static bonobo_object_data_t *
scaled_control_factory (void)
741
{
742
        bonobo_object_data_t *bod;
743

744
	bod = control_factory_common (NULL);
745

746
	g_signal_connect (bod->drawing_area, "size_allocate",
747
			    G_CALLBACK (control_size_allocate_callback), bod);
748

749
        return bod;
750
751
}

752
753
static bonobo_object_data_t *
scrollable_control_factory (void)
754
{
755
756
	bonobo_object_data_t *bod;
	GtkWidget *scroll;
757
758
759
760

	scroll = gtk_scrolled_window_new (NULL, NULL);

	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scroll),
761
762
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
763

764
	bod = control_factory_common (scroll);
765

766
	g_signal_connect (bod->drawing_area, "size_allocate",
767
			    G_CALLBACK (scrolled_control_size_allocate_callback),
768
			    bod);
769
	
770
	g_signal_connect (bod->scrolled_window, "size_allocate",
771
			    G_CALLBACK (scrolled_window_size_allocate_callback),
772
			    bod);
773

774
        return bod;
775
776
777
}

static BonoboObject *
778
bonobo_object_factory (BonoboGenericFactory *this, const char *bonobo_activation_iid,
779
		       void *data)
780
781
782
783
784
785
786
787
788
{
	bonobo_object_data_t *bod;

	g_return_val_if_fail (this != NULL, NULL);

	/*
	 * Creates the BonoboObject server
	 */

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

797
	if (bod == NULL) {
798
		return NULL;
799
800
	} else {
		return BONOBO_OBJECT (bod->control);
801
802
803
804
805
806
	}
}

static void
init_bonobo_image_generic_factory (void)
{
807
808
        char *registration_id;

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

812
	image_factory = bonobo_generic_factory_new_multi 
813
		(registration_id,
814
		 bonobo_object_factory, NULL);
815
816

	g_free (registration_id);
817
818
819
820
821
822
823
}

static void
init_server_factory (int argc, char **argv)
{
	CORBA_Environment ev;
	CORBA_exception_init (&ev);
824
825
826
	
	/* Disable session manager connection */
	gnome_client_disable_master_connection ();
827

828
829
	gnomelib_register_popt_table (bonobo_activation_popt_options, bonobo_activation_get_popt_table_name ());
	bonobo_activation_init (argc, argv);
830

831
832
833
834
        gnome_init ("bonobo-image-generic", VERSION,
		    argc, argv); 
	gdk_rgb_init ();

835
	if (!bonobo_init (CORBA_OBJECT_NIL, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL))
836
837
838
839
840
841
842
843
		g_error (_("I could not initialize Bonobo"));

	CORBA_exception_free (&ev);
}

int
main (int argc, char *argv [])
{
844
845
846
847
848
849
	/* Initialize gettext support */
#ifdef ENABLE_NLS
	bindtextdomain (PACKAGE, GNOMELOCALEDIR);
	textdomain (PACKAGE);
#endif

850
851
852
853
854
855
856
	/* Make criticals and warnings stop in the debugger if NAUTILUS_DEBUG is set.
	 * Unfortunately, this has to be done explicitly for each domain.
	 */
	if (g_getenv ("NAUTILUS_DEBUG") != NULL) {
		eel_make_warnings_and_criticals_stop_in_debugger (G_LOG_DOMAIN, NULL);
	}

857
858
859
860
861
862
863
864
865
866
867
	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;
}