/usr/share/gtk-2.0/demo/offscreen_window.c is in gtk2.0-examples 2.24.23-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /* Offscreen windows/Rotated button
*
* Offscreen windows can be used to transform parts of a widget
* hierarchy. Note that the rotated button is fully functional.
*/
#include <math.h>
#include <gtk/gtk.h>
#define GTK_TYPE_ROTATED_BIN (gtk_rotated_bin_get_type ())
#define GTK_ROTATED_BIN(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_ROTATED_BIN, GtkRotatedBin))
#define GTK_ROTATED_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_ROTATED_BIN, GtkRotatedBinClass))
#define GTK_IS_ROTATED_BIN(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_ROTATED_BIN))
#define GTK_IS_ROTATED_BIN_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_ROTATED_BIN))
#define GTK_ROTATED_BIN_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_ROTATED_BIN, GtkRotatedBinClass))
typedef struct _GtkRotatedBin GtkRotatedBin;
typedef struct _GtkRotatedBinClass GtkRotatedBinClass;
struct _GtkRotatedBin
{
GtkContainer container;
GtkWidget *child;
GdkWindow *offscreen_window;
gdouble angle;
};
struct _GtkRotatedBinClass
{
GtkContainerClass parent_class;
};
GType gtk_rotated_bin_get_type (void) G_GNUC_CONST;
GtkWidget* gtk_rotated_bin_new (void);
void gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
gdouble angle);
/*** implementation ***/
static void gtk_rotated_bin_realize (GtkWidget *widget);
static void gtk_rotated_bin_unrealize (GtkWidget *widget);
static void gtk_rotated_bin_size_request (GtkWidget *widget,
GtkRequisition *requisition);
static void gtk_rotated_bin_size_allocate (GtkWidget *widget,
GtkAllocation *allocation);
static gboolean gtk_rotated_bin_damage (GtkWidget *widget,
GdkEventExpose *event);
static gboolean gtk_rotated_bin_expose (GtkWidget *widget,
GdkEventExpose *offscreen);
static void gtk_rotated_bin_add (GtkContainer *container,
GtkWidget *child);
static void gtk_rotated_bin_remove (GtkContainer *container,
GtkWidget *widget);
static void gtk_rotated_bin_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data);
static GType gtk_rotated_bin_child_type (GtkContainer *container);
G_DEFINE_TYPE (GtkRotatedBin, gtk_rotated_bin, GTK_TYPE_CONTAINER);
static void
to_child (GtkRotatedBin *bin,
double widget_x,
double widget_y,
double *x_out,
double *y_out)
{
GtkAllocation child_area;
double x, y, xr, yr;
double c, s;
double w, h;
s = sin (bin->angle);
c = cos (bin->angle);
gtk_widget_get_allocation (bin->child, &child_area);
w = c * child_area.width + s * child_area.height;
h = s * child_area.width + c * child_area.height;
x = widget_x;
y = widget_y;
x -= (w - child_area.width) / 2;
y -= (h - child_area.height) / 2;
x -= child_area.width / 2;
y -= child_area.height / 2;
xr = x * c + y * s;
yr = y * c - x * s;
x = xr;
y = yr;
x += child_area.width / 2;
y += child_area.height / 2;
*x_out = x;
*y_out = y;
}
static void
to_parent (GtkRotatedBin *bin,
double offscreen_x,
double offscreen_y,
double *x_out,
double *y_out)
{
GtkAllocation child_area;
double x, y, xr, yr;
double c, s;
double w, h;
s = sin (bin->angle);
c = cos (bin->angle);
gtk_widget_get_allocation (bin->child, &child_area);
w = c * child_area.width + s * child_area.height;
h = s * child_area.width + c * child_area.height;
x = offscreen_x;
y = offscreen_y;
x -= child_area.width / 2;
y -= child_area.height / 2;
xr = x * c - y * s;
yr = x * s + y * c;
x = xr;
y = yr;
x += child_area.width / 2;
y += child_area.height / 2;
x -= (w - child_area.width) / 2;
y -= (h - child_area.height) / 2;
*x_out = x;
*y_out = y;
}
static void
gtk_rotated_bin_class_init (GtkRotatedBinClass *klass)
{
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
widget_class->realize = gtk_rotated_bin_realize;
widget_class->unrealize = gtk_rotated_bin_unrealize;
widget_class->size_request = gtk_rotated_bin_size_request;
widget_class->size_allocate = gtk_rotated_bin_size_allocate;
widget_class->expose_event = gtk_rotated_bin_expose;
g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET),
GTK_TYPE_ROTATED_BIN,
g_cclosure_new (G_CALLBACK (gtk_rotated_bin_damage),
NULL, NULL));
container_class->add = gtk_rotated_bin_add;
container_class->remove = gtk_rotated_bin_remove;
container_class->forall = gtk_rotated_bin_forall;
container_class->child_type = gtk_rotated_bin_child_type;
}
static void
gtk_rotated_bin_init (GtkRotatedBin *bin)
{
gtk_widget_set_has_window (GTK_WIDGET (bin), TRUE);
}
GtkWidget *
gtk_rotated_bin_new (void)
{
return g_object_new (GTK_TYPE_ROTATED_BIN, NULL);
}
static GdkWindow *
pick_offscreen_child (GdkWindow *offscreen_window,
double widget_x,
double widget_y,
GtkRotatedBin *bin)
{
GtkAllocation child_area;
double x, y;
if (bin->child && gtk_widget_get_visible (bin->child))
{
to_child (bin, widget_x, widget_y, &x, &y);
gtk_widget_get_allocation (bin->child, &child_area);
if (x >= 0 && x < child_area.width &&
y >= 0 && y < child_area.height)
return bin->offscreen_window;
}
return NULL;
}
static void
offscreen_window_to_parent (GdkWindow *offscreen_window,
double offscreen_x,
double offscreen_y,
double *parent_x,
double *parent_y,
GtkRotatedBin *bin)
{
to_parent (bin, offscreen_x, offscreen_y, parent_x, parent_y);
}
static void
offscreen_window_from_parent (GdkWindow *window,
double parent_x,
double parent_y,
double *offscreen_x,
double *offscreen_y,
GtkRotatedBin *bin)
{
to_child (bin, parent_x, parent_y, offscreen_x, offscreen_y);
}
static void
gtk_rotated_bin_realize (GtkWidget *widget)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
GdkWindow *gdk_window;
GdkWindowAttr attributes;
gint attributes_mask;
gint border_width;
GtkRequisition child_requisition;
GtkAllocation widget_allocation, bin_child_allocation;
GtkStyle *style;
gtk_widget_set_realized (widget, TRUE);
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
gtk_widget_get_allocation (widget, &widget_allocation);
attributes.x = widget_allocation.x + border_width;
attributes.y = widget_allocation.y + border_width;
attributes.width = widget_allocation.width - 2 * border_width;
attributes.height = widget_allocation.height - 2 * border_width;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.event_mask = gtk_widget_get_events (widget)
| GDK_EXPOSURE_MASK
| GDK_POINTER_MOTION_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_SCROLL_MASK
| GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK;
attributes.visual = gtk_widget_get_visual (widget);
attributes.colormap = gtk_widget_get_colormap (widget);
attributes.wclass = GDK_INPUT_OUTPUT;
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
gdk_window = gdk_window_new (gtk_widget_get_parent_window (widget), &attributes, attributes_mask);
gtk_widget_set_window (widget, gdk_window);
gdk_window_set_user_data (gdk_window, widget);
g_signal_connect (gdk_window, "pick-embedded-child",
G_CALLBACK (pick_offscreen_child), bin);
attributes.window_type = GDK_WINDOW_OFFSCREEN;
child_requisition.width = child_requisition.height = 0;
if (bin->child && gtk_widget_get_visible (bin->child))
{
gtk_widget_get_allocation (bin->child, &bin_child_allocation);
attributes.width = bin_child_allocation.width;
attributes.height = bin_child_allocation.height;
}
bin->offscreen_window = gdk_window_new (gtk_widget_get_root_window (widget),
&attributes, attributes_mask);
gdk_window_set_user_data (bin->offscreen_window, widget);
if (bin->child)
gtk_widget_set_parent_window (bin->child, bin->offscreen_window);
gdk_offscreen_window_set_embedder (bin->offscreen_window, gtk_widget_get_window (widget));
g_signal_connect (bin->offscreen_window, "to-embedder",
G_CALLBACK (offscreen_window_to_parent), bin);
g_signal_connect (bin->offscreen_window, "from-embedder",
G_CALLBACK (offscreen_window_from_parent), bin);
gtk_widget_style_attach (widget);
style = gtk_widget_get_style (widget);
gtk_style_set_background (style, gdk_window, GTK_STATE_NORMAL);
gtk_style_set_background (style, bin->offscreen_window, GTK_STATE_NORMAL);
gdk_window_show (bin->offscreen_window);
}
static void
gtk_rotated_bin_unrealize (GtkWidget *widget)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
gdk_window_set_user_data (bin->offscreen_window, NULL);
gdk_window_destroy (bin->offscreen_window);
bin->offscreen_window = NULL;
GTK_WIDGET_CLASS (gtk_rotated_bin_parent_class)->unrealize (widget);
}
static GType
gtk_rotated_bin_child_type (GtkContainer *container)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
if (bin->child)
return G_TYPE_NONE;
return GTK_TYPE_WIDGET;
}
static void
gtk_rotated_bin_add (GtkContainer *container,
GtkWidget *widget)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
if (!bin->child)
{
gtk_widget_set_parent_window (widget, bin->offscreen_window);
gtk_widget_set_parent (widget, GTK_WIDGET (bin));
bin->child = widget;
}
else
g_warning ("GtkRotatedBin cannot have more than one child\n");
}
static void
gtk_rotated_bin_remove (GtkContainer *container,
GtkWidget *widget)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
gboolean was_visible;
was_visible = gtk_widget_get_visible (widget);
if (bin->child == widget)
{
gtk_widget_unparent (widget);
bin->child = NULL;
if (was_visible && gtk_widget_get_visible (GTK_WIDGET (container)))
gtk_widget_queue_resize (GTK_WIDGET (container));
}
}
static void
gtk_rotated_bin_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_data)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (container);
g_return_if_fail (callback != NULL);
if (bin->child)
(*callback) (bin->child, callback_data);
}
void
gtk_rotated_bin_set_angle (GtkRotatedBin *bin,
gdouble angle)
{
g_return_if_fail (GTK_IS_ROTATED_BIN (bin));
bin->angle = angle;
gtk_widget_queue_resize (GTK_WIDGET (bin));
gdk_window_geometry_changed (bin->offscreen_window);
}
static void
gtk_rotated_bin_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
GtkRequisition child_requisition;
gint border_width;
double s, c;
double w, h;
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
child_requisition.width = 0;
child_requisition.height = 0;
if (bin->child && gtk_widget_get_visible (bin->child))
gtk_widget_size_request (bin->child, &child_requisition);
s = sin (bin->angle);
c = cos (bin->angle);
w = c * child_requisition.width + s * child_requisition.height;
h = s * child_requisition.width + c * child_requisition.height;
requisition->width = border_width * 2 + w;
requisition->height = border_width * 2 + h;
}
static void
gtk_rotated_bin_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
gint border_width;
gint w, h;
gdouble s, c;
gtk_widget_set_allocation (widget, allocation);
border_width = gtk_container_get_border_width (GTK_CONTAINER (widget));
w = allocation->width - border_width * 2;
h = allocation->height - border_width * 2;
if (gtk_widget_get_realized (widget))
gdk_window_move_resize (gtk_widget_get_window (widget),
allocation->x + border_width,
allocation->y + border_width,
w, h);
if (bin->child && gtk_widget_get_visible (bin->child))
{
GtkRequisition child_requisition;
GtkAllocation child_allocation;
s = sin (bin->angle);
c = cos (bin->angle);
gtk_widget_get_child_requisition (bin->child, &child_requisition);
child_allocation.x = 0;
child_allocation.y = 0;
child_allocation.height = child_requisition.height;
if (c == 0.0)
child_allocation.width = h / s;
else if (s == 0.0)
child_allocation.width = w / c;
else
child_allocation.width = MIN ((w - s * child_allocation.height) / c,
(h - c * child_allocation.height) / s);
if (gtk_widget_get_realized (widget))
gdk_window_move_resize (bin->offscreen_window,
child_allocation.x,
child_allocation.y,
child_allocation.width,
child_allocation.height);
child_allocation.x = child_allocation.y = 0;
gtk_widget_size_allocate (bin->child, &child_allocation);
}
}
static gboolean
gtk_rotated_bin_damage (GtkWidget *widget,
GdkEventExpose *event)
{
gdk_window_invalidate_rect (gtk_widget_get_window (widget), NULL, FALSE);
return TRUE;
}
static gboolean
gtk_rotated_bin_expose (GtkWidget *widget,
GdkEventExpose *event)
{
GtkRotatedBin *bin = GTK_ROTATED_BIN (widget);
gint width, height;
gdouble s, c;
gdouble w, h;
if (gtk_widget_is_drawable (widget))
{
if (event->window == gtk_widget_get_window (widget))
{
GdkPixmap *pixmap;
GtkAllocation child_area;
cairo_t *cr;
if (bin->child && gtk_widget_get_visible (bin->child))
{
pixmap = gdk_offscreen_window_get_pixmap (bin->offscreen_window);
gtk_widget_get_allocation (bin->child, &child_area);
cr = gdk_cairo_create (gtk_widget_get_window (widget));
/* transform */
s = sin (bin->angle);
c = cos (bin->angle);
w = c * child_area.width + s * child_area.height;
h = s * child_area.width + c * child_area.height;
cairo_translate (cr, (w - child_area.width) / 2, (h - child_area.height) / 2);
cairo_translate (cr, child_area.width / 2, child_area.height / 2);
cairo_rotate (cr, bin->angle);
cairo_translate (cr, -child_area.width / 2, -child_area.height / 2);
/* clip */
gdk_pixmap_get_size (pixmap, &width, &height);
cairo_rectangle (cr, 0, 0, width, height);
cairo_clip (cr);
/* paint */
gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0);
cairo_paint (cr);
cairo_destroy (cr);
}
}
else if (event->window == bin->offscreen_window)
{
gtk_paint_flat_box (gtk_widget_get_style (widget), event->window,
GTK_STATE_NORMAL, GTK_SHADOW_NONE,
&event->area, widget, "blah",
0, 0, -1, -1);
if (bin->child)
gtk_container_propagate_expose (GTK_CONTAINER (widget),
bin->child,
event);
}
}
return FALSE;
}
/*** ***/
static void
scale_changed (GtkRange *range,
GtkRotatedBin *bin)
{
gtk_rotated_bin_set_angle (bin, gtk_range_get_value (range));
}
static GtkWidget *window = NULL;
GtkWidget *
do_offscreen_window (GtkWidget *do_widget)
{
if (!window)
{
GtkWidget *bin, *vbox, *scale, *button;
GdkColor black;
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_screen (GTK_WINDOW (window),
gtk_widget_get_screen (do_widget));
gtk_window_set_title (GTK_WINDOW (window), "Rotated widget");
g_signal_connect (window, "destroy",
G_CALLBACK (gtk_widget_destroyed), &window);
gdk_color_parse ("black", &black);
gtk_widget_modify_bg (window, GTK_STATE_NORMAL, &black);
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
vbox = gtk_vbox_new (0, FALSE);
scale = gtk_hscale_new_with_range (0, G_PI/2, 0.01);
gtk_scale_set_draw_value (GTK_SCALE (scale), FALSE);
button = gtk_button_new_with_label ("A Button");
bin = gtk_rotated_bin_new ();
g_signal_connect (scale, "value-changed", G_CALLBACK (scale_changed), bin);
gtk_container_add (GTK_CONTAINER (window), vbox);
gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), bin, TRUE, TRUE, 0);
gtk_container_add (GTK_CONTAINER (bin), button);
}
if (!gtk_widget_get_visible (window))
gtk_widget_show_all (window);
else
{
gtk_widget_destroy (window);
window = NULL;
}
return window;
}
|