]> Pileus Git - ~andy/gtk/blob - demos/gtk-demo/transparent.c
gtk-demo: Add test of transparent GdkWindows
[~andy/gtk] / demos / gtk-demo / transparent.c
1 /* Transparent
2  *
3  * Use transparent background on GdkWindows to create a shadow effect on a GtkOverlay widget.
4  */
5
6 #include <gtk/gtk.h>
7
8 #define SHADOW_OFFSET_X 7
9 #define SHADOW_OFFSET_Y 7
10 #define SHADOW_RADIUS 5
11
12 static void
13 draw_shadow_box (cairo_t   *cr,
14                  GdkRectangle rect,
15                  double radius,
16                  double transparency)
17 {
18   cairo_pattern_t *pattern;
19   double x0, x1, x2, x3;
20   double y0, y1, y2, y3;
21
22   x0 = rect.x;
23   x1 = rect.x + radius;
24   x2 = rect.x + rect.width - radius;
25   x3 = rect.x + rect.width;
26   
27   y0 = rect.y;
28   y1 = rect.y + radius;
29   y2 = rect.y + rect.height - radius;
30   y3 = rect.y + rect.height;
31   
32    /* Fill non-border part */
33   cairo_set_source_rgba (cr, 0, 0, 0, transparency);
34   cairo_rectangle (cr,
35                    x1, y1, x2 - x1, y2 - y1);
36   cairo_fill (cr);
37
38   /* Upper border */
39
40   pattern = cairo_pattern_create_linear (0, y0, 0, y1);
41   
42   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, 0.0);
43   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, transparency);
44   
45   cairo_set_source (cr, pattern);
46   cairo_pattern_destroy (pattern);
47   
48   cairo_rectangle (cr,
49                    x1, y0,
50                    x2 - x1, y1 - y0);
51   cairo_fill (cr);
52
53   /* Bottom border */
54
55   pattern = cairo_pattern_create_linear (0, y2, 0, y3);
56   
57   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
58   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
59   
60   cairo_set_source (cr, pattern);
61   cairo_pattern_destroy (pattern);
62   
63   cairo_rectangle (cr,
64                    x1, y2,
65                    x2 - x1, y3 - y2);
66   cairo_fill (cr);
67   
68   /* Left border */
69
70   pattern = cairo_pattern_create_linear (x0, 0, x1, 0);
71   
72   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, 0.0);
73   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, transparency);
74   
75   cairo_set_source (cr, pattern);
76   cairo_pattern_destroy (pattern);
77   
78   cairo_rectangle (cr,
79                    x0, y1,
80                    x1 - x0, y2 - y1);
81   cairo_fill (cr);
82
83   /* Right border */
84
85   pattern = cairo_pattern_create_linear (x2, 0, x3, 0);
86   
87   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
88   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
89   
90   cairo_set_source (cr, pattern);
91   cairo_pattern_destroy (pattern);
92   
93   cairo_rectangle (cr,
94                    x2, y1,
95                    x3 - x2, y2 - y1);
96   cairo_fill (cr);
97
98   /* NW corner */
99   
100   pattern = cairo_pattern_create_radial (x1, y1, 0,
101                                          x1, y1, radius);
102
103   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
104   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
105   
106   cairo_set_source (cr, pattern);
107   cairo_pattern_destroy (pattern);
108   
109   cairo_rectangle (cr,
110                    x0, y0,
111                    x1 - x0, y1 - y0);
112   cairo_fill (cr);
113
114   /* NE corner */
115   
116   pattern = cairo_pattern_create_radial (x2, y1, 0,
117                                          x2, y1, radius);
118
119   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
120   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
121   
122   cairo_set_source (cr, pattern);
123   cairo_pattern_destroy (pattern);
124   
125   cairo_rectangle (cr,
126                    x2, y0,
127                    x3 - x2, y1 - y0);
128   cairo_fill (cr);
129
130   /* SW corner */
131   
132   pattern = cairo_pattern_create_radial (x1, y2, 0,
133                                          x1, y2, radius);
134
135   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
136   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
137   
138   cairo_set_source (cr, pattern);
139   cairo_pattern_destroy (pattern);
140   
141   cairo_rectangle (cr,
142                    x0, y2,
143                    x1 - x0, y3 - y2);
144   cairo_fill (cr);
145
146   /* SE corner */
147   
148   pattern = cairo_pattern_create_radial (x2, y2, 0,
149                                          x2, y2, radius);
150
151   cairo_pattern_add_color_stop_rgba (pattern, 0.0, 0.0, 0, 0, transparency);
152   cairo_pattern_add_color_stop_rgba (pattern, 1.0, 0.0, 0, 0, 0.0);
153   
154   cairo_set_source (cr, pattern);
155   cairo_pattern_destroy (pattern);
156   
157   cairo_rectangle (cr,
158                    x2, y2,
159                    x3 - x2, y3 - y2);
160   cairo_fill (cr);
161 }
162
163 static gboolean
164 draw_callback (GtkWidget *widget,
165                cairo_t   *cr,
166                gpointer   data)
167 {
168   GdkRectangle rect;
169
170   gtk_widget_get_allocation (widget, &rect);
171   rect.x += SHADOW_OFFSET_X;
172   rect.y += SHADOW_OFFSET_Y;
173   rect.width -= SHADOW_OFFSET_X;
174   rect.height -= SHADOW_OFFSET_Y;
175
176   draw_shadow_box (cr,
177                    rect, SHADOW_RADIUS, 0.4);
178   
179   return FALSE;
180 }
181
182 GtkWidget *
183 do_transparent (GtkWidget *do_widget)
184 {
185   static GtkWidget *window = NULL;
186
187   if (!window)
188     {
189       GtkWidget *view;
190       GtkWidget *sw;
191       GtkWidget *overlay;
192       GtkWidget *align;
193       GtkWidget *entry;
194       GdkRGBA transparent = {0.1, 0, 0, 0};
195
196       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
197       gtk_window_set_screen (GTK_WINDOW (window),
198                              gtk_widget_get_screen (do_widget));
199       gtk_window_set_default_size (GTK_WINDOW (window),
200                                    450, 450);
201
202       g_signal_connect (window, "destroy",
203                         G_CALLBACK (gtk_widget_destroyed), &window);
204
205       gtk_window_set_title (GTK_WINDOW (window), "Transparent");
206       gtk_container_set_border_width (GTK_CONTAINER (window), 0);
207
208       view = gtk_text_view_new ();
209
210       sw = gtk_scrolled_window_new (NULL, NULL);
211       gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw),
212                                       GTK_POLICY_AUTOMATIC,
213                                       GTK_POLICY_AUTOMATIC);
214       gtk_container_add (GTK_CONTAINER (sw), view);
215
216       overlay = gtk_overlay_new ();
217       gtk_container_add (GTK_CONTAINER (overlay), sw);
218       gtk_container_add (GTK_CONTAINER (window), overlay);
219
220       gtk_widget_override_background_color (overlay, 0, &transparent);
221
222       align = gtk_alignment_new (0.0, 0.0, 0.0, 0.0);
223       gtk_alignment_set_padding (GTK_ALIGNMENT (align),
224                                  0, SHADOW_OFFSET_Y, 0, SHADOW_OFFSET_X);
225       g_signal_connect (align, "draw", G_CALLBACK (draw_callback), NULL);
226       
227       entry = gtk_entry_new ();
228       gtk_container_add (GTK_CONTAINER (align), entry);
229       
230       gtk_overlay_add_overlay (GTK_OVERLAY (overlay), align);
231       gtk_widget_set_halign (align, GTK_ALIGN_CENTER);
232       gtk_widget_set_valign (align, GTK_ALIGN_START);
233
234       gtk_widget_show_all (overlay);
235     }
236
237   if (!gtk_widget_get_visible (window))
238     {
239       gtk_widget_show (window);
240     }
241   else
242     {
243       gtk_widget_destroy (window);
244       window = NULL;
245     }
246
247   return window;
248 }