summaryrefslogtreecommitdiff
path: root/eel/eel-background.c
blob: c2348c9cad0fd301e0688020d3b81a428a443574 (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
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-

   eel-background.c: Object for the background of a widget.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program 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 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
   Library General Public License for more details.
  
   You should have received a copy of the GNU Library 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.
  
   Author: Darin Adler <darin@eazel.com>
*/

#include <config.h>
#include "eel-background.h"
#include "eel-gdk-extensions.h"
#include "eel-gdk-pixbuf-extensions.h"
#include "eel-glib-extensions.h"
#include "eel-gnome-extensions.h"
#include "eel-gtk-macros.h"
#include "eel-lib-self-check-functions.h"
#include "eel-string.h"
#include "eel-marshal.h"
#include "eel-types.h"
#include "eel-type-builtins.h"
#include <gtk/gtk.h>
#include <eel/eel-canvas.h>
#include <eel/eel-canvas-util.h>
#include <gio/gio.h>
#include <math.h>
#include <stdio.h>
#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnomeui/gnome-bg.h>

static void       eel_background_class_init                (gpointer       klass);
static void       eel_background_init                      (gpointer       object,
							    gpointer       klass);
static void       eel_background_finalize                  (GObject       *object);
static GdkPixmap *eel_background_get_pixmap_and_color      (EelBackground *background,
							    GdkWindow     *window,
							    GdkColor      *color,
							    gboolean      *changes_with_size);
static void set_image_properties (EelBackground *background);

EEL_CLASS_BOILERPLATE (EelBackground, eel_background, GTK_TYPE_OBJECT)

enum {
	APPEARANCE_CHANGED,
	SETTINGS_CHANGED,
	RESET,
	LAST_SIGNAL
};

/* This is the size of the GdkRGB dither matrix, in order to avoid
 * bad dithering when tiling the gradient
 */
#define GRADIENT_PIXMAP_TILE_SIZE 128

static guint signals[LAST_SIGNAL];

struct EelBackgroundDetails {
	char *color;
	
	GnomeBG *bg;

	/* Realized data: */
	gboolean background_changes_with_size;
	GdkPixmap *background_pixmap;
	int background_entire_width;
	int background_entire_height;
	GdkColor default_color;

	gboolean use_base;
	
	/* Is this background attached to desktop window */
	gboolean is_desktop;
	/* Desktop screen size watcher */
	gulong screen_size_handler;
	/* Can we use common pixmap for root window and desktop window */
	gboolean use_common_pixmap;
};

static void
eel_background_class_init (gpointer klass)
{
	GObjectClass *object_class;

	object_class = G_OBJECT_CLASS (klass);

	eel_type_init ();

	signals[APPEARANCE_CHANGED] =
		g_signal_new ("appearance_changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE,
			      G_STRUCT_OFFSET (EelBackgroundClass,
					       appearance_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE,
			      0);
	signals[SETTINGS_CHANGED] =
		g_signal_new ("settings_changed",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE,
			      G_STRUCT_OFFSET (EelBackgroundClass,
					       settings_changed),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__INT,
			      G_TYPE_NONE,
			      1, G_TYPE_INT);
	signals[RESET] =
		g_signal_new ("reset",
			      G_TYPE_FROM_CLASS (object_class),
			      G_SIGNAL_RUN_LAST | G_SIGNAL_NO_RECURSE,
			      G_STRUCT_OFFSET (EelBackgroundClass,
					       reset),
			      NULL, NULL,
			      g_cclosure_marshal_VOID__VOID,
			      G_TYPE_NONE,
			      0);

	object_class->finalize = eel_background_finalize;
}

static void
on_bg_changed (GnomeBG *bg, EelBackground *background)
{
	g_signal_emit (G_OBJECT (background),
		       signals[APPEARANCE_CHANGED], 0);
}

static void
eel_background_init (gpointer object, gpointer klass)
{
	EelBackground *background;

	background = EEL_BACKGROUND (object);

	background->details = g_new0 (EelBackgroundDetails, 1);
	background->details->default_color.red = 0xffff;
	background->details->default_color.green = 0xffff;
	background->details->default_color.blue = 0xffff;
	background->details->bg = gnome_bg_new ();

	g_signal_connect (background->details->bg, "changed",
			  G_CALLBACK (on_bg_changed), background);
	
}

/* The safe way to clear an image from a background is:
 * 		eel_background_set_image_uri (NULL);
 * This fn is a private utility - it does NOT clear
 * the details->bg_uri setting.
 */
static void
eel_background_remove_current_image (EelBackground *background)
{
	if (background->details->bg != NULL) {
		g_object_unref (G_OBJECT (background->details->bg));
		background->details->bg = NULL;
	}
}

static void
eel_background_finalize (GObject *object)
{
	EelBackground *background;

	background = EEL_BACKGROUND (object);

	g_free (background->details->color);
	eel_background_remove_current_image (background);

	if (background->details->background_pixmap != NULL) {
		g_object_unref (background->details->background_pixmap);
		background->details->background_pixmap = NULL;
	}

	g_free (background->details);

	EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}

static EelBackgroundImagePlacement
placement_gnome_to_eel (GnomeBGPlacement p)
{
	switch (p) {
	case GNOME_BG_PLACEMENT_CENTERED:
		return EEL_BACKGROUND_CENTERED;
	case GNOME_BG_PLACEMENT_FILL_SCREEN:
		return EEL_BACKGROUND_SCALED;
	case GNOME_BG_PLACEMENT_SCALED:
		return EEL_BACKGROUND_SCALED_ASPECT;
	case GNOME_BG_PLACEMENT_ZOOMED:
		return EEL_BACKGROUND_ZOOM;
	case GNOME_BG_PLACEMENT_TILED:
		return EEL_BACKGROUND_TILED;
	}

	return EEL_BACKGROUND_TILED;
}

static GnomeBGPlacement
placement_eel_to_gnome (EelBackgroundImagePlacement p)
{
	switch (p) {
	case EEL_BACKGROUND_CENTERED:
		return GNOME_BG_PLACEMENT_CENTERED;
	case EEL_BACKGROUND_SCALED:
		return GNOME_BG_PLACEMENT_FILL_SCREEN;
	case EEL_BACKGROUND_SCALED_ASPECT:
		return GNOME_BG_PLACEMENT_SCALED;
	case EEL_BACKGROUND_ZOOM:
		return GNOME_BG_PLACEMENT_ZOOMED;
	case EEL_BACKGROUND_TILED:
		return GNOME_BG_PLACEMENT_TILED;
	}

	return GNOME_BG_PLACEMENT_TILED;
}

EelBackgroundImagePlacement
eel_background_get_image_placement (EelBackground *background)
{
	g_return_val_if_fail (EEL_IS_BACKGROUND (background), EEL_BACKGROUND_TILED);

	return placement_gnome_to_eel (gnome_bg_get_placement (background->details->bg));
}

void
eel_background_set_image_placement (EelBackground              *background,
				    EelBackgroundImagePlacement new_placement)
{
	g_return_if_fail (EEL_IS_BACKGROUND (background));

	gnome_bg_set_placement (background->details->bg,
				placement_eel_to_gnome (new_placement));
}

EelBackground *
eel_background_new (void)
{
	return EEL_BACKGROUND (g_object_new (EEL_TYPE_BACKGROUND, NULL));
}

static void
eel_background_unrealize (EelBackground *background)
{
	if (background->details->background_pixmap != NULL) {
		g_object_unref (background->details->background_pixmap);
		background->details->background_pixmap = NULL;
	}
	background->details->background_entire_width = 0;
	background->details->background_entire_height = 0;
	background->details->default_color.red = 0xffff;
	background->details->default_color.green = 0xffff;
	background->details->default_color.blue = 0xffff;
}

static void
drawable_get_adjusted_size (EelBackground *background,
			    GdkDrawable   *drawable,
			    int		  *width,
			    int	          *height)
{
	GdkScreen *screen;
	
	/* 
	 * Screen resolution change makes root drawable have incorrect size.
	 */    
	gdk_drawable_get_size (drawable, width, height);

	if (background->details->is_desktop) {
		screen = gdk_drawable_get_screen (drawable);
		*width = gdk_screen_get_width (screen);
		*height = gdk_screen_get_height (screen);
	}
}

static gboolean
eel_background_ensure_realized (EelBackground *background, GdkWindow *window)
{
	gpointer data;
	GtkWidget *widget;
	GtkStyle *style;
	gboolean changed;
	int entire_width;
	int entire_height;
	
	drawable_get_adjusted_size (background, window, &entire_width, &entire_height);
	
	/* Set the default color */
	
	/* Get the widget to which the window belongs and its style as well */
	gdk_window_get_user_data (window, &data);
	widget = GTK_WIDGET (data);
	if (widget != NULL) {
		style = gtk_widget_get_style (widget);
		if (background->details->use_base) {
			background->details->default_color = style->base[GTK_STATE_NORMAL];
		} else {
			background->details->default_color = style->bg[GTK_STATE_NORMAL];
		}
		
		gdk_rgb_find_color (style->colormap, &(background->details->default_color));
	}

	/* If the pixmap doesn't change with the window size, never update
	 * it again.
	 */
	if (background->details->background_pixmap != NULL &&
	    !background->details->background_changes_with_size) {
		return FALSE;
	}

	/* If the window size is the same as last time, don't update */
	if (entire_width == background->details->background_entire_width &&
	    entire_height == background->details->background_entire_height) {
		return FALSE;
	}

	if (background->details->background_pixmap != NULL) {
		g_object_unref (background->details->background_pixmap);
		background->details->background_pixmap = NULL;
	}

	changed = FALSE;

	set_image_properties (background);

	background->details->background_changes_with_size = gnome_bg_changes_with_size (background->details->bg);
	background->details->background_pixmap = gnome_bg_create_pixmap (background->details->bg,
									 window,
									 entire_width, entire_height,
									 background->details->is_desktop);

	changed = TRUE;
	
	
	background->details->background_entire_width = entire_width;
	background->details->background_entire_height = entire_height;
	
	return changed;
}

static GdkPixmap *
eel_background_get_pixmap_and_color (EelBackground *background,
				     GdkWindow     *window,
				     GdkColor      *color,
				     gboolean      *changes_with_size)
{
	int entire_width;
	int entire_height;

	drawable_get_adjusted_size (background, window, &entire_width, &entire_height);

	eel_background_ensure_realized (background, window);
	
	*color = background->details->default_color;
	*changes_with_size = background->details->background_changes_with_size;
	
	if (background->details->background_pixmap != NULL) {
		return g_object_ref (background->details->background_pixmap);
	} 
	return NULL;
}

void
eel_background_expose (GtkWidget                   *widget,
		       GdkEventExpose              *event)
{
	GdkColor color;
	int window_width;
	int window_height;
	gboolean changes_with_size;
	GdkPixmap *pixmap;
	GdkGC *gc;
	GdkGCValues gc_values;
	GdkGCValuesMask value_mask;

	EelBackground *background;
	
	if (event->window != widget->window) {
		return;
	}

	background = eel_get_widget_background (widget);

	drawable_get_adjusted_size (background, widget->window, &window_width, &window_height);
	
	pixmap = eel_background_get_pixmap_and_color (background,
						      widget->window,
						      &color,
						      &changes_with_size);

        if (!changes_with_size) {
                /* The background was already drawn by X, since we set
                 * the GdkWindow background/back_pixmap.
                 * No need to draw it again. */
                if (pixmap) {
                        g_object_unref (pixmap);
                }
                return;
        }
 
	if (pixmap) {
		gc_values.tile = pixmap;
		gc_values.ts_x_origin = 0;
		gc_values.ts_y_origin = 0;
		gc_values.fill = GDK_TILED;
		value_mask = GDK_GC_FILL | GDK_GC_TILE | GDK_GC_TS_X_ORIGIN | GDK_GC_TS_Y_ORIGIN;
	} else {
		gdk_rgb_find_color (gtk_widget_get_colormap (widget), &color);
		gc_values.foreground = color;
		gc_values.fill = GDK_SOLID;
		value_mask = GDK_GC_FILL | GDK_GC_FOREGROUND;
	}
	
	gc = gdk_gc_new_with_values (widget->window, &gc_values, value_mask);
	
	gdk_gc_set_clip_rectangle (gc, &event->area);

	gdk_draw_rectangle (widget->window, gc, TRUE, 0, 0, window_width, window_height);
	
	g_object_unref (gc);
	
	if (pixmap) {
		g_object_unref (pixmap);
	}
}

static void
set_image_properties (EelBackground *background)
{
	if (!background->details->color) {
		gnome_bg_set_color (background->details->bg, GNOME_BG_COLOR_SOLID,
				 &background->details->default_color, NULL);
	} else if (!eel_gradient_is_gradient (background->details->color)) {
		GdkColor c;

		eel_gdk_color_parse_with_white_default (background->details->color, &c);
	
		gnome_bg_set_color (background->details->bg, GNOME_BG_COLOR_SOLID, &c, NULL);
	} else {
		GdkColor c1;
		GdkColor c2;
		char *spec;

		spec = eel_gradient_get_start_color_spec (background->details->color);
		eel_gdk_color_parse_with_white_default (spec, &c1);
		g_free (spec);

		spec = eel_gradient_get_end_color_spec (background->details->color);
		eel_gdk_color_parse_with_white_default (spec, &c2);
		g_free (spec);

		if (eel_gradient_is_horizontal (background->details->color))
			gnome_bg_set_color (background->details->bg, GNOME_BG_COLOR_H_GRADIENT, &c1, &c2);
		else
			gnome_bg_set_color (background->details->bg, GNOME_BG_COLOR_V_GRADIENT, &c1, &c2);

	}
}

char *
eel_background_get_color (EelBackground *background)
{
	g_return_val_if_fail (EEL_IS_BACKGROUND (background), NULL);

	return g_strdup (background->details->color);
}

char *
eel_background_get_image_uri (EelBackground *background)
{
	const char *filename;
	
	g_return_val_if_fail (EEL_IS_BACKGROUND (background), NULL);

	filename = gnome_bg_get_filename (background->details->bg);
	
	return g_filename_to_uri (filename, NULL, NULL);
}

/* Use style->base as the default color instead of bg */
void
eel_background_set_use_base (EelBackground *background,
			     gboolean use_base)
{
	background->details->use_base = use_base;
}

void
eel_background_set_color (EelBackground *background,
			  const char *color)
{
	if (eel_strcmp (background->details->color, color) != 0) {
		g_free (background->details->color);
		background->details->color = g_strdup (color);
		
		set_image_properties (background);
	}
}

static gboolean
eel_background_set_image_uri_helper (EelBackground *background,
				     const char *image_uri,
				     gboolean emit_signal)
{
	char *filename;

	if (image_uri != NULL) {
		filename = g_filename_from_uri (image_uri, NULL, NULL);
	}
	else {
		filename = NULL;
	}
	
	gnome_bg_set_filename (background->details->bg, filename);

	if (emit_signal) {
		g_signal_emit (GTK_OBJECT (background), signals[SETTINGS_CHANGED], 0, GDK_ACTION_COPY);
	}

	set_image_properties (background);
	
	g_free (filename);
	
	return TRUE;
}

void
eel_background_set_image_uri (EelBackground *background, const char *image_uri)
{
	
	
	eel_background_set_image_uri_helper (background, image_uri, TRUE);
}

/* Use this fn to set both the image and color and avoid flash. The color isn't
 * changed till after the image is done loading, that way if an update occurs
 * before then, it will use the old color and image.
 */
static void
eel_background_set_image_uri_and_color (EelBackground *background, GdkDragAction action,
					const char *image_uri, const char *color)
{
	eel_background_set_image_uri_helper (background, image_uri, FALSE);
	eel_background_set_color (background, color);

	/* We always emit, even if the color didn't change, because the image change
	 * relies on us doing it here.
	 */

	g_signal_emit (background, signals[SETTINGS_CHANGED], 0, action);
}

void
eel_background_receive_dropped_background_image (EelBackground *background,
						 GdkDragAction action,
						 const char *image_uri)
{
	/* Currently, we only support tiled images. So we set the placement.
	 * We rely on eel_background_set_image_uri_and_color to emit
	 * the SETTINGS_CHANGED & APPEARANCE_CHANGE signals.
	 */
	eel_background_set_image_placement (background, EEL_BACKGROUND_TILED);
	
	eel_background_set_image_uri_and_color (background, action, image_uri, NULL);
}

/**
 * eel_background_is_set:
 * 
 * Check whether the background's color or image has been set.
 */
gboolean
eel_background_is_set (EelBackground *background)
{
	g_assert (EEL_IS_BACKGROUND (background));

	return background->details->color != NULL
		|| gnome_bg_get_filename (background->details->bg) != NULL;
}

/**
 * eel_background_reset:
 *
 * Emit the reset signal to forget any color or image that has been
 * set previously.
 */
void
eel_background_reset (EelBackground *background)
{
	g_return_if_fail (EEL_IS_BACKGROUND (background));

	g_signal_emit (GTK_OBJECT (background), signals[RESET], 0);
}

static void
eel_background_set_up_widget (EelBackground *background, GtkWidget *widget)
{
	GtkStyle *style;
	GdkPixmap *pixmap;
	GdkPixmap *root_pixmap;
	GdkColor color;
	
	int window_width;
	int window_height;
	
	GdkWindow *window;
	gboolean changes_with_size;

	if (!GTK_WIDGET_REALIZED (widget)) {
		return;
	}

	drawable_get_adjusted_size (background, widget->window, &window_width, &window_height);
	
	pixmap = eel_background_get_pixmap_and_color (background,
						      widget->window,
						      &color, 
						      &changes_with_size);

	style = gtk_widget_get_style (widget);
	
	gdk_rgb_find_color (style->colormap, &color);

	if (EEL_IS_CANVAS (widget)) {
		window = GTK_LAYOUT (widget)->bin_window;
	} else {
		window = widget->window;
	}

	if (!changes_with_size || background->details->is_desktop) {
		gdk_window_set_back_pixmap (window, pixmap, FALSE);
	} else {
		gdk_window_set_back_pixmap (window, NULL, FALSE);
		gdk_window_set_background (window, &color);
	}
	

	background->details->background_changes_with_size =
		gnome_bg_changes_with_size (background->details->bg);
	
	if (background->details->is_desktop) {

		root_pixmap = NULL;

		if (background->details->use_common_pixmap) {
			root_pixmap = g_object_ref (pixmap);
		} else {
			root_pixmap = gnome_bg_create_pixmap (background->details->bg, window,
							      window_width, window_height, TRUE);
		}

		gnome_bg_set_pixmap_as_root (gdk_drawable_get_screen (window), root_pixmap);
		g_object_unref (root_pixmap);
	}
	
	if (pixmap) {
		g_object_unref (pixmap);
	}
}

static void
eel_widget_background_changed (GtkWidget *widget, EelBackground *background)
{
	eel_background_unrealize (background);
	eel_background_set_up_widget (background, widget);

	gtk_widget_queue_draw (widget);
}

/* Callback used when the style of a widget changes.  We have to regenerate its
 * EelBackgroundStyle so that it will match the chosen GTK+ theme.
 */
static void
widget_style_set_cb (GtkWidget *widget, GtkStyle *previous_style, gpointer data)
{
	EelBackground *background;
	
	background = EEL_BACKGROUND (data);
	
	eel_widget_background_changed (widget, background);
}

static void
screen_size_changed (GdkScreen *screen, EelBackground *background)
{
	g_signal_emit (background, signals[APPEARANCE_CHANGED], 0);
}


static void
widget_realized_setup (GtkWidget *widget, gpointer data)
{
	EelBackground *background;
	
	background = EEL_BACKGROUND (data);
	
        if (background->details->is_desktop) {
		GdkWindow *root_window;	
		GdkScreen *screen;
		
		screen = gtk_widget_get_screen (widget);

		if (background->details->screen_size_handler > 0) {
		        g_signal_handler_disconnect (screen,
				                     background->details->screen_size_handler);
		}
	
		background->details->screen_size_handler = 
			g_signal_connect (screen, "size_changed",
            				  G_CALLBACK (screen_size_changed), background);

		root_window = gdk_screen_get_root_window(screen);			
		
		if (gdk_drawable_get_visual (root_window) == gtk_widget_get_visual (widget)) {
			background->details->use_common_pixmap = TRUE;
		} else {
			background->details->use_common_pixmap = FALSE;
		}
	}
}

static void
widget_realize_cb (GtkWidget *widget, gpointer data)
{
	EelBackground *background;
	
	background = EEL_BACKGROUND (data);

	widget_realized_setup (widget, data);
		
	eel_background_set_up_widget (background, widget);
}

static void
widget_unrealize_cb (GtkWidget *widget, gpointer data)
{
	EelBackground *background;
	
	background = EEL_BACKGROUND (data);

	if (background->details->screen_size_handler > 0) {
		        g_signal_handler_disconnect (gtk_widget_get_screen (GTK_WIDGET (widget)),
				                     background->details->screen_size_handler);
			background->details->screen_size_handler = 0;
	}
	background->details->use_common_pixmap = FALSE;
}

void
eel_background_set_desktop (EelBackground *background, GtkWidget *widget, gboolean is_desktop)
{
	background->details->is_desktop = is_desktop;

	if (GTK_WIDGET_REALIZED(widget) && background->details->is_desktop) {
		widget_realized_setup (widget, background);
	}
	
}

gboolean
eel_background_is_desktop (EelBackground *background)
{
	return background->details->is_desktop;
}

/* Gets the background attached to a widget.

   If the widget doesn't already have a EelBackground object,
   this will create one. To change the widget's background, you can
   just call eel_background methods on the widget.

   If the widget is a canvas, nothing more needs to be done.  For
   normal widgets, you need to call eel_background_expose() from your
   expose handler to draw the background.

   Later, we might want a call to find out if we already have a background,
   or a way to share the same background among multiple widgets; both would
   be straightforward.
*/
EelBackground *
eel_get_widget_background (GtkWidget *widget)
{
	gpointer data;
	EelBackground *background;

	g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);

	/* Check for an existing background. */
	data = g_object_get_data (G_OBJECT (widget), "eel_background");
	if (data != NULL) {
		g_assert (EEL_IS_BACKGROUND (data));
		return data;
	}

	/* Store the background in the widget's data. */
	background = eel_background_new ();
	g_object_ref (background);
	gtk_object_sink (GTK_OBJECT (background));
	g_object_set_data_full (G_OBJECT (widget), "eel_background",
				background, g_object_unref);

	/* Arrange to get the signal whenever the background changes. */
	g_signal_connect_object (background, "appearance_changed",
				 G_CALLBACK (eel_widget_background_changed), widget, G_CONNECT_SWAPPED);
	eel_widget_background_changed (widget, background);

	g_signal_connect_object (widget, "style_set",
				 G_CALLBACK (widget_style_set_cb),
				 background,
				 0);
	g_signal_connect_object (widget, "realize",
				 G_CALLBACK (widget_realize_cb),
				 background,
				 0);
	g_signal_connect_object (widget, "unrealize",
				 G_CALLBACK (widget_unrealize_cb),
				 background,
				 0);

	return background;
}

/* determine if a background is darker or lighter than average, to help clients know what
   colors to draw on top with */
gboolean
eel_background_is_dark (EelBackground *background)
{
	return gnome_bg_is_dark (background->details->bg);
}
   
/* handle dropped colors */
void
eel_background_receive_dropped_color (EelBackground *background,
				      GtkWidget *widget,
				      GdkDragAction action,
				      int drop_location_x,
				      int drop_location_y,
				      const GtkSelectionData *selection_data)
{
	guint16 *channels;
	char *color_spec;
	char *new_gradient_spec;
	int left_border, right_border, top_border, bottom_border;

	g_return_if_fail (EEL_IS_BACKGROUND (background));
	g_return_if_fail (GTK_IS_WIDGET (widget));
	g_return_if_fail (selection_data != NULL);

	/* Convert the selection data into a color spec. */
	if (selection_data->length != 8 || selection_data->format != 16) {
		g_warning ("received invalid color data");
		return;
	}
	channels = (guint16 *) selection_data->data;
	color_spec = g_strdup_printf ("#%02X%02X%02X",
				      channels[0] >> 8,
				      channels[1] >> 8,
				      channels[2] >> 8);

	/* Figure out if the color was dropped close enough to an edge to create a gradient.
	   For the moment, this is hard-wired, but later the widget will have to have some
	   say in where the borders are.
	*/
	left_border = 32;
	right_border = widget->allocation.width - 32;
	top_border = 32;
	bottom_border = widget->allocation.height - 32;
	if (drop_location_x < left_border && drop_location_x <= right_border) {
		new_gradient_spec = eel_gradient_set_left_color_spec (background->details->color, color_spec);
	} else if (drop_location_x >= left_border && drop_location_x > right_border) {
		new_gradient_spec = eel_gradient_set_right_color_spec (background->details->color, color_spec);
	} else if (drop_location_y < top_border && drop_location_y <= bottom_border) {
		new_gradient_spec = eel_gradient_set_top_color_spec (background->details->color, color_spec);
	} else if (drop_location_y >= top_border && drop_location_y > bottom_border) {
		new_gradient_spec = eel_gradient_set_bottom_color_spec (background->details->color, color_spec);
	} else {
		new_gradient_spec = g_strdup (color_spec);
	}
	
	g_free (color_spec);

	eel_background_set_image_uri_and_color (background, action, NULL, new_gradient_spec);

	g_free (new_gradient_spec);
}

void
eel_background_save_to_gconf (EelBackground *background)
{
	GConfClient *client = gconf_client_get_default ();

	if (background->details->bg)
		gnome_bg_save_to_preferences (background->details->bg, client);
}

/* self check code */

#if !defined (EEL_OMIT_SELF_CHECK)

void
eel_self_check_background (void)
{
	EelBackground *background;

	background = eel_background_new ();

	eel_background_set_color (background, NULL);
	eel_background_set_color (background, "");
	eel_background_set_color (background, "red");
	eel_background_set_color (background, "red-blue");
	eel_background_set_color (background, "red-blue:h");

	gtk_object_sink (GTK_OBJECT (background));
}

#endif