| 1 |
/* Overlay/Transparency |
| 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 *entry; |
| 193 |
GtkCssProvider *provider; |
| 194 |
gchar *css; |
| 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), 450, 450); |
| 200 |
|
| 201 |
g_signal_connect (window, "destroy", |
| 202 |
G_CALLBACK (gtk_widget_destroyed), &window); |
| 203 |
|
| 204 |
gtk_window_set_title (GTK_WINDOW (window), "Transparency"); |
| 205 |
gtk_container_set_border_width (GTK_CONTAINER (window), 0); |
| 206 |
|
| 207 |
view = gtk_text_view_new (); |
| 208 |
|
| 209 |
sw = gtk_scrolled_window_new (NULL, NULL); |
| 210 |
gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), |
| 211 |
GTK_POLICY_AUTOMATIC, |
| 212 |
GTK_POLICY_AUTOMATIC); |
| 213 |
gtk_container_add (GTK_CONTAINER (sw), view); |
| 214 |
|
| 215 |
overlay = gtk_overlay_new (); |
| 216 |
gtk_container_add (GTK_CONTAINER (overlay), sw); |
| 217 |
gtk_container_add (GTK_CONTAINER (window), overlay); |
| 218 |
|
| 219 |
entry = gtk_entry_new (); |
| 220 |
provider = gtk_css_provider_new (); |
| 221 |
css = g_strdup_printf ("* { border-width: 0px %dpx %dpx 0px; }", |
| 222 |
SHADOW_OFFSET_X, SHADOW_OFFSET_Y); |
| 223 |
gtk_css_provider_load_from_data (provider, css, -1, NULL); |
| 224 |
g_free (css); |
| 225 |
gtk_style_context_add_provider (gtk_widget_get_style_context (entry), |
| 226 |
GTK_STYLE_PROVIDER (provider), |
| 227 |
GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); |
| 228 |
g_signal_connect (entry, "draw", G_CALLBACK (draw_callback), NULL); |
| 229 |
gtk_overlay_add_overlay (GTK_OVERLAY (overlay), entry); |
| 230 |
gtk_widget_set_halign (entry, GTK_ALIGN_CENTER); |
| 231 |
gtk_widget_set_valign (entry, GTK_ALIGN_START); |
| 232 |
|
| 233 |
gtk_widget_show_all (overlay); |
| 234 |
} |
| 235 |
|
| 236 |
if (!gtk_widget_get_visible (window)) |
| 237 |
gtk_widget_show (window); |
| 238 |
else |
| 239 |
gtk_widget_destroy (window); |
| 240 |
|
| 241 |
return window; |
| 242 |
} |