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
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
|
/* -*- 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., 51 Franklin St, Fifth Floor,
Boston, MA 02110-1301, 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-mate-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 <gdk/gdkx.h>
#include <gio/gio.h>
#include <math.h>
#include <stdio.h>
#define MATE_DESKTOP_USE_UNSTABLE_API
#include <libmateui/mate-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);
static void set_image_properties (EelBackground *background);
static void init_fade (EelBackground *background, GtkWidget *widget);
static void free_fade (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;
MateBG *bg;
GtkWidget *widget;
/* Realized data: */
GdkPixmap *background_pixmap;
gboolean background_pixmap_is_unset_root_pixmap;
MateBGCrossfade *fade;
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;
/* Desktop monitors configuration watcher */
gulong screen_monitors_handler;
/* Can we use common pixmap for root window and desktop window */
gboolean use_common_pixmap;
guint change_idle_id;
/* activity status */
gboolean is_active;
};
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 (MateBG *bg, EelBackground *background)
{
init_fade (background, background->details->widget);
g_signal_emit (G_OBJECT (background),
signals[APPEARANCE_CHANGED], 0);
}
static void
on_bg_transitioned (MateBG *bg, EelBackground *background)
{
free_fade (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 = mate_bg_new ();
background->details->is_active = TRUE;
g_signal_connect (background->details->bg, "changed",
G_CALLBACK (on_bg_changed), background);
g_signal_connect (background->details->bg, "transitioned",
G_CALLBACK (on_bg_transitioned), 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
free_fade (EelBackground *background)
{
if (background->details->fade != NULL)
{
g_object_unref (background->details->fade);
background->details->fade = NULL;
}
}
static void
free_background_pixmap (EelBackground *background)
{
GdkDisplay *display;
GdkPixmap *pixmap;
pixmap = background->details->background_pixmap;
if (pixmap != NULL)
{
/* If we created a root pixmap and didn't set it as background
it will live forever, so we need to kill it manually.
If set as root background it will be killed next time the
background is changed. */
if (background->details->background_pixmap_is_unset_root_pixmap)
{
display = gdk_drawable_get_display (GDK_DRAWABLE (pixmap));
XKillClient (GDK_DISPLAY_XDISPLAY (display),
GDK_PIXMAP_XID (pixmap));
}
g_object_unref (pixmap);
background->details->background_pixmap = 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);
free_background_pixmap (background);
free_fade (background);
g_free (background->details);
EEL_CALL_PARENT (G_OBJECT_CLASS, finalize, (object));
}
static EelBackgroundImagePlacement
placement_mate_to_eel (MateBGPlacement p)
{
switch (p)
{
case MATE_BG_PLACEMENT_CENTERED:
return EEL_BACKGROUND_CENTERED;
case MATE_BG_PLACEMENT_FILL_SCREEN:
return EEL_BACKGROUND_SCALED;
case MATE_BG_PLACEMENT_SCALED:
return EEL_BACKGROUND_SCALED_ASPECT;
case MATE_BG_PLACEMENT_ZOOMED:
return EEL_BACKGROUND_ZOOM;
case MATE_BG_PLACEMENT_TILED:
return EEL_BACKGROUND_TILED;
case MATE_BG_PLACEMENT_SPANNED:
return EEL_BACKGROUND_SPANNED;
}
return EEL_BACKGROUND_TILED;
}
static MateBGPlacement
placement_eel_to_mate (EelBackgroundImagePlacement p)
{
switch (p)
{
case EEL_BACKGROUND_CENTERED:
return MATE_BG_PLACEMENT_CENTERED;
case EEL_BACKGROUND_SCALED:
return MATE_BG_PLACEMENT_FILL_SCREEN;
case EEL_BACKGROUND_SCALED_ASPECT:
return MATE_BG_PLACEMENT_SCALED;
case EEL_BACKGROUND_ZOOM:
return MATE_BG_PLACEMENT_ZOOMED;
case EEL_BACKGROUND_TILED:
return MATE_BG_PLACEMENT_TILED;
case EEL_BACKGROUND_SPANNED:
return MATE_BG_PLACEMENT_SPANNED;
}
return MATE_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_mate_to_eel (mate_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));
mate_bg_set_placement (background->details->bg,
placement_eel_to_mate (new_placement));
}
EelBackground *
eel_background_new (void)
{
return EEL_BACKGROUND (g_object_new (EEL_TYPE_BACKGROUND, NULL));
}
static void
eel_background_unrealize (EelBackground *background)
{
free_background_pixmap (background);
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.
*/
#if GTK_CHECK_VERSION(3, 0, 0)
width = gdk_window_get_width(GDK_WINDOW(drawable));
height = gdk_window_get_height(GDK_WINDOW(drawable));
#else
gdk_drawable_get_size(drawable, width, height); // FIXME: this is right?
#endif
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 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;
}
free_background_pixmap (background);
changed = FALSE;
set_image_properties (background);
background->details->background_pixmap = mate_bg_create_pixmap (background->details->bg,
window,
entire_width, entire_height,
background->details->is_desktop);
background->details->background_pixmap_is_unset_root_pixmap = background->details->is_desktop;
/* We got the pixmap and everything, so we don't care about a change
that is pending (unless things actually change after this time) */
g_object_set_data (G_OBJECT (background->details->bg),
"ignore-pending-change", GINT_TO_POINTER (TRUE));
changed = TRUE;
background->details->background_entire_width = entire_width;
background->details->background_entire_height = entire_height;
return changed;
}
#define CLAMP_COLOR(v) (t = (v), CLAMP (t, 0, G_MAXUSHORT))
#define SATURATE(v) ((1.0 - saturation) * intensity + saturation * (v))
static void
make_color_inactive (EelBackground *background, GdkColor *color)
{
double intensity, saturation;
gushort t;
if (!background->details->is_active)
{
saturation = 0.7;
intensity = color->red * 0.30 + color->green * 0.59 + color->blue * 0.11;
color->red = SATURATE (color->red);
color->green = SATURATE (color->green);
color->blue = SATURATE (color->blue);
if (intensity > G_MAXUSHORT / 2)
{
color->red *= 0.9;
color->green *= 0.9;
color->blue *= 0.9;
}
else
{
color->red *= 1.25;
color->green *= 1.25;
color->blue *= 1.25;
}
color->red = CLAMP_COLOR (color->red);
color->green = CLAMP_COLOR (color->green);
color->blue = CLAMP_COLOR (color->blue);
}
}
static GdkPixmap *
eel_background_get_pixmap_and_color (EelBackground *background,
GdkWindow *window,
GdkColor *color)
{
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;
make_color_inactive (background, color);
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;
GdkPixmap *pixmap;
GdkGC *gc;
GdkGCValues gc_values;
GdkGCValuesMask value_mask;
GdkWindow *widget_window;
EelBackground *background;
widget_window = gtk_widget_get_window (widget);
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);
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)
{
GdkColor c;
if (!background->details->color)
{
c = background->details->default_color;
make_color_inactive (background, &c);
mate_bg_set_color (background->details->bg, MATE_BG_COLOR_SOLID,
&c, NULL);
}
else if (!eel_gradient_is_gradient (background->details->color))
{
eel_gdk_color_parse_with_white_default (background->details->color, &c);
make_color_inactive (background, &c);
mate_bg_set_color (background->details->bg, MATE_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);
make_color_inactive (background, &c1);
g_free (spec);
spec = eel_gradient_get_end_color_spec (background->details->color);
eel_gdk_color_parse_with_white_default (spec, &c2);
make_color_inactive (background, &c2);
g_free (spec);
if (eel_gradient_is_horizontal (background->details->color))
mate_bg_set_color (background->details->bg, MATE_BG_COLOR_H_GRADIENT, &c1, &c2);
else
mate_bg_set_color (background->details->bg, MATE_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 = mate_bg_get_filename (background->details->bg);
if (filename)
{
return g_filename_to_uri (filename, NULL, NULL);
}
return 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;
}
mate_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
|| mate_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
set_root_pixmap (EelBackground *background,
GdkWindow *window)
{
GdkPixmap *pixmap, *root_pixmap;
GdkScreen *screen;
GdkColor color;
pixmap = eel_background_get_pixmap_and_color (background,
window,
&color);
screen = gdk_drawable_get_screen (window);
if (background->details->use_common_pixmap)
{
background->details->background_pixmap_is_unset_root_pixmap = FALSE;
root_pixmap = g_object_ref (pixmap);
}
else
{
root_pixmap = mate_bg_create_pixmap (background->details->bg, window,
gdk_screen_get_width (screen), gdk_screen_get_height (screen), TRUE);
}
mate_bg_set_pixmap_as_root (screen, pixmap);
g_object_unref (pixmap);
g_object_unref (root_pixmap);
}
static gboolean
fade_to_pixmap (EelBackground *background,
GdkWindow *window,
GdkPixmap *pixmap)
{
if (background->details->fade == NULL)
{
return FALSE;
}
if (!mate_bg_crossfade_set_end_pixmap (background->details->fade,
pixmap))
{
return FALSE;
}
if (!mate_bg_crossfade_is_started (background->details->fade))
{
mate_bg_crossfade_start (background->details->fade, window);
if (background->details->is_desktop)
{
g_signal_connect_swapped (background->details->fade,
"finished",
G_CALLBACK (set_root_pixmap), background);
}
}
return mate_bg_crossfade_is_started (background->details->fade);
}
static void
eel_background_set_up_widget (EelBackground *background, GtkWidget *widget)
{
GtkStyle *style;
GdkPixmap *pixmap;
GdkColor color;
int window_width;
int window_height;
GdkWindow *window;
GdkWindow *widget_window;
gboolean in_fade;
if (!gtk_widget_get_realized (widget))
{
return;
}
widget_window = gtk_widget_get_window (widget);
drawable_get_adjusted_size (background, widget_window, &window_width, &window_height);
pixmap = eel_background_get_pixmap_and_color (background,
widget_window,
&color);
style = gtk_widget_get_style (widget);
gdk_rgb_find_color (style->colormap, &color);
if (EEL_IS_CANVAS (widget))
{
window = gtk_layout_get_bin_window (GTK_LAYOUT (widget));
}
else
{
window = widget_window;
}
if (background->details->fade != NULL)
{
in_fade = fade_to_pixmap (background, window, pixmap);
}
else
{
in_fade = FALSE;
}
if (!in_fade)
{
if (background->details->is_desktop)
{
gdk_window_set_back_pixmap (window, pixmap, FALSE);
}
else
{
gdk_window_set_background (window, &color);
gdk_window_set_back_pixmap (window, pixmap, FALSE);
}
}
if (background->details->is_desktop && !in_fade)
{
set_root_pixmap (background, window);
}
if (pixmap)
{
g_object_unref (pixmap);
}
}
static gboolean
on_background_changed (EelBackground *background)
{
if (background->details->change_idle_id == 0)
{
return FALSE;
}
background->details->change_idle_id = 0;
eel_background_unrealize (background);
eel_background_set_up_widget (background, background->details->widget);
gtk_widget_queue_draw (background->details->widget);
return FALSE;
}
static void
init_fade (EelBackground *background, GtkWidget *widget)
{
if (widget == NULL || !gtk_widget_get_realized (widget))
return;
if (!background->details->is_desktop)
{
return;
}
if (background->details->fade == NULL)
{
GdkWindow *window;
int old_width, old_height, width, height;
/* If this was the result of a screen size change,
* we don't want to crossfade
*/
window = gtk_widget_get_window (widget);
#if GTK_CHECK_VERSION(3, 0, 0)
old_width = gdk_window_get_width(GDK_WINDOW(window));
old_height = gdk_window_get_height(GDK_WINDOW(window));
#else
gdk_drawable_get_size(window, &old_width, &old_height);
#endif
drawable_get_adjusted_size (background, window,
&width, &height);
if (old_width == width && old_height == height)
{
background->details->fade = mate_bg_crossfade_new (width, height);
g_signal_connect_swapped (background->details->fade,
"finished",
G_CALLBACK (free_fade),
background);
}
}
if (background->details->fade != NULL && !mate_bg_crossfade_is_started (background->details->fade))
{
GdkPixmap *start_pixmap;
if (background->details->background_pixmap == NULL)
{
start_pixmap = mate_bg_get_pixmap_from_root (gtk_widget_get_screen (widget));
}
else
{
start_pixmap = g_object_ref (background->details->background_pixmap);
}
mate_bg_crossfade_set_start_pixmap (background->details->fade,
start_pixmap);
g_object_unref (start_pixmap);
}
}
static void
eel_widget_queue_background_change (GtkWidget *widget)
{
EelBackground *background;
background = eel_get_widget_background (widget);
if (background->details->change_idle_id > 0)
{
return;
}
background->details->change_idle_id = g_idle_add ((GSourceFunc) on_background_changed, background);
}
/* 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);
if (previous_style != NULL)
{
eel_widget_queue_background_change (widget);
}
}
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);
if (background->details->screen_monitors_handler > 0)
{
g_signal_handler_disconnect (screen,
background->details->screen_monitors_handler);
}
background->details->screen_monitors_handler =
g_signal_connect (screen, "monitors-changed",
G_CALLBACK (screen_size_changed), background);
root_window = gdk_screen_get_root_window(screen);
#if GTK_CHECK_VERSION(3, 0, 0)
if (gdk_window_get_visual(root_window) == gtk_widget_get_visual(widget))
#else
if (gdk_drawable_get_visual(root_window) == gtk_widget_get_visual(widget))
#endif
{
background->details->use_common_pixmap = TRUE;
}
else
{
background->details->use_common_pixmap = FALSE;
}
init_fade (background, widget);
}
}
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;
}
if (background->details->screen_monitors_handler > 0)
{
g_signal_handler_disconnect (gtk_widget_get_screen (GTK_WIDGET (widget)),
background->details->screen_monitors_handler);
background->details->screen_monitors_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_get_realized (widget) && background->details->is_desktop)
{
widget_realized_setup (widget, background);
}
}
gboolean
eel_background_is_desktop (EelBackground *background)
{
return background->details->is_desktop;
}
static void
on_widget_destroyed (GtkWidget *widget, EelBackground *background)
{
if (background->details->change_idle_id != 0)
{
g_source_remove (background->details->change_idle_id);
background->details->change_idle_id = 0;
}
background->details->widget = NULL;
}
/* 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_sink (background);
g_object_set_data_full (G_OBJECT (widget), "eel_background",
background, g_object_unref);
background->details->widget = widget;
g_signal_connect_object (widget, "destroy", G_CALLBACK (on_widget_destroyed), background, 0);
/* Arrange to get the signal whenever the background changes. */
g_signal_connect_object (background, "appearance_changed",
G_CALLBACK (eel_widget_queue_background_change), widget, G_CONNECT_SWAPPED);
eel_widget_queue_background_change (widget);
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)
{
GdkScreen *screen;
GdkRectangle rect;
/* only check for the background on the 0th monitor */
screen = gdk_screen_get_default ();
gdk_screen_get_monitor_geometry (screen, 0, &rect);
return mate_bg_is_dark (background->details->bg, rect.width, rect.height);
}
/* 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;
GtkAllocation allocation;
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 (gtk_selection_data_get_length ((GtkSelectionData *) selection_data) != 8 ||
gtk_selection_data_get_format ((GtkSelectionData *) selection_data) != 16)
{
g_warning ("received invalid color data");
return;
}
channels = (guint16 *) gtk_selection_data_get_data ((GtkSelectionData *) selection_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.
*/
gtk_widget_get_allocation (widget, &allocation);
left_border = 32;
right_border = allocation.width - 32;
top_border = 32;
bottom_border = 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_settings (EelBackground *background)
{
if (background->details->bg)
mate_bg_save_to_preferences (background->details->bg);
}
void
eel_background_set_active (EelBackground *background,
gboolean is_active)
{
if (background->details->is_active != is_active)
{
background->details->is_active = is_active;
set_image_properties (background);
}
}
/* 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");
g_object_ref_sink (background);
g_object_unref (background);
}
#endif
|