diff --git a/tests/Makefile.am b/tests/Makefile.am index 426dea30e9..66f9aad030 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -55,6 +55,7 @@ noinst_PROGRAMS = $(TEST_PROGS) \ testmultiscreen \ testnotebookdnd \ testnouiprint \ + testoffscreen \ testprint \ testrgb \ testrecentchooser \ @@ -133,6 +134,7 @@ testmultidisplay_DEPENDENCIES = $(TEST_DEPS) testmultiscreen_DEPENDENCIES = $(TEST_DEPS) testnotebookdnd_DEPENDENCIES = $(TEST_DEPS) testnouiprint_DEPENDENCIES = $(TEST_DEPS) +testoffscreen_DEPENDENCIES = $(TEST_DEPS) testprint_DEPENDENCIES = $(TEST_DEPS) testrecentchooser_DEPENDENCIES = $(TEST_DEPS) testrecentchoosermenu_DEPENDENCIES = $(TEST_DEPS) @@ -191,6 +193,7 @@ testmultidisplay_LDADD = $(LDADDS) testmultiscreen_LDADD = $(LDADDS) testnotebookdnd_LDADD = $(LDADDS) testnouiprint_LDADD = $(LDADDS) +testoffscreen_LDADD = $(LDADDS) testprint_LDADD = $(LDADDS) testrecentchooser_LDADD = $(LDADDS) testrecentchoosermenu_LDADD = $(LDADDS) @@ -317,6 +320,11 @@ testrecentchoosermenu_SOURCES = \ testvolumebutton_SOURCES = \ testvolumebutton.c +testoffscreen_SOURCES = \ + gtkoffscreenbox.c \ + gtkoffscreenbox.h \ + testoffscreen.c + EXTRA_DIST += \ prop-editor.h \ testgtk.1 \ diff --git a/tests/gtkoffscreenbox.c b/tests/gtkoffscreenbox.c new file mode 100644 index 0000000000..fe9b6fbcca --- /dev/null +++ b/tests/gtkoffscreenbox.c @@ -0,0 +1,560 @@ +/* + * gtkoffscreenbox.c + */ + +#include "config.h" + +#include +#include + +#include "gtkoffscreenbox.h" + +static void gtk_offscreen_box_realize (GtkWidget *widget); +static void gtk_offscreen_box_unrealize (GtkWidget *widget); +static void gtk_offscreen_box_size_request (GtkWidget *widget, + GtkRequisition *requisition); +static void gtk_offscreen_box_size_allocate (GtkWidget *widget, + GtkAllocation *allocation); +static gboolean gtk_offscreen_box_damage (GtkWidget *widget, + GdkEventExpose *event); +static gboolean gtk_offscreen_box_expose (GtkWidget *widget, + GdkEventExpose *offscreen); + +static void gtk_offscreen_box_add (GtkContainer *container, + GtkWidget *child); +static void gtk_offscreen_box_remove (GtkContainer *container, + GtkWidget *widget); +static void gtk_offscreen_box_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data); +static GType gtk_offscreen_box_child_type (GtkContainer *container); + +static void from_parent (GdkWindow *child, + gdouble parent_x, + gdouble parent_y, + gdouble *child_x, + gdouble *child_y); +static void to_parent (GdkWindow *child, + gdouble child_x, + gdouble child_y, + gdouble *parent_x, + gdouble *parent_y); + +static const GdkOffscreenChildHooks offscreen_hooks = { + from_parent, + to_parent, +}; + +#define CHILD1_SIZE_SCALE 1.0 +#define CHILD2_SIZE_SCALE 1.0 + +G_DEFINE_TYPE (GtkOffscreenBox, gtk_offscreen_box, GTK_TYPE_CONTAINER); + +static void +gtk_offscreen_box_class_init (GtkOffscreenBoxClass *klass) +{ + GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass); + GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass); + + widget_class->realize = gtk_offscreen_box_realize; + widget_class->unrealize = gtk_offscreen_box_unrealize; + widget_class->size_request = gtk_offscreen_box_size_request; + widget_class->size_allocate = gtk_offscreen_box_size_allocate; + widget_class->expose_event = gtk_offscreen_box_expose; + + g_signal_override_class_closure (g_signal_lookup ("damage-event", GTK_TYPE_WIDGET), + GTK_TYPE_OFFSCREEN_BOX, + g_cclosure_new (G_CALLBACK (gtk_offscreen_box_damage), + NULL, NULL)); + + container_class->add = gtk_offscreen_box_add; + container_class->remove = gtk_offscreen_box_remove; + container_class->forall = gtk_offscreen_box_forall; + container_class->child_type = gtk_offscreen_box_child_type; +} + +static void +gtk_offscreen_box_init (GtkOffscreenBox *offscreen_box) +{ + GTK_WIDGET_UNSET_FLAGS (offscreen_box, GTK_NO_WINDOW); +} + +GtkWidget * +gtk_offscreen_box_new (void) +{ + return g_object_new (GTK_TYPE_OFFSCREEN_BOX, NULL); +} + +static void +gtk_offscreen_box_realize (GtkWidget *widget) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget); + GdkWindowAttr attributes; + gint attributes_mask; + gint border_width; + GtkRequisition child_requisition; + int start_y = 0; + + GTK_WIDGET_SET_FLAGS (widget, GTK_REALIZED); + + border_width = GTK_CONTAINER (widget)->border_width; + + 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; + + widget->window = gdk_window_new (gtk_widget_get_parent_window (widget), + &attributes, attributes_mask); + gdk_window_set_user_data (widget->window, widget); + + attributes.window_type = GDK_WINDOW_OFFSCREEN; + + /* Child 1 */ + attributes.x = attributes.y = 0; + if (offscreen_box->child1 && GTK_WIDGET_VISIBLE (offscreen_box->child1)) + { + attributes.width = offscreen_box->child1->allocation.width; + attributes.height = offscreen_box->child1->allocation.height; + start_y += offscreen_box->child1->allocation.height; + } + offscreen_box->offscreen_window1 = gdk_window_new (widget->window, + &attributes, attributes_mask); + gdk_window_set_user_data (offscreen_box->offscreen_window1, widget); + if (offscreen_box->child1) + gtk_widget_set_parent_window (offscreen_box->child1, offscreen_box->offscreen_window1); + + /* Child 2 */ + attributes.y = start_y; + child_requisition.width = child_requisition.height = 0; + if (offscreen_box->child2 && GTK_WIDGET_VISIBLE (offscreen_box->child2)) + { + attributes.width = offscreen_box->child2->allocation.width; + attributes.height = offscreen_box->child2->allocation.height; + } + offscreen_box->offscreen_window2 = gdk_window_new (widget->window, + &attributes, attributes_mask); + gdk_window_set_offscreen_hooks (offscreen_box->offscreen_window2, &offscreen_hooks); + gdk_window_set_user_data (offscreen_box->offscreen_window2, widget); + if (offscreen_box->child2) + gtk_widget_set_parent_window (offscreen_box->child2, offscreen_box->offscreen_window2); + + widget->style = gtk_style_attach (widget->style, widget->window); + + gtk_style_set_background (widget->style, widget->window, GTK_STATE_NORMAL); + gtk_style_set_background (widget->style, offscreen_box->offscreen_window1, GTK_STATE_NORMAL); + gtk_style_set_background (widget->style, offscreen_box->offscreen_window2, GTK_STATE_NORMAL); + + gdk_window_show (offscreen_box->offscreen_window1); + gdk_window_show (offscreen_box->offscreen_window2); +} + +static void +gtk_offscreen_box_unrealize (GtkWidget *widget) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget); + + gdk_window_set_user_data (offscreen_box->offscreen_window1, NULL); + gdk_window_destroy (offscreen_box->offscreen_window1); + offscreen_box->offscreen_window1 = NULL; + + gdk_window_set_user_data (offscreen_box->offscreen_window2, NULL); + gdk_window_destroy (offscreen_box->offscreen_window2); + offscreen_box->offscreen_window2 = NULL; + + GTK_WIDGET_CLASS (gtk_offscreen_box_parent_class)->unrealize (widget); +} + +static GType +gtk_offscreen_box_child_type (GtkContainer *container) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container); + + if (offscreen_box->child1 && offscreen_box->child2) + return G_TYPE_NONE; + + return GTK_TYPE_WIDGET; +} + +static void +gtk_offscreen_box_add (GtkContainer *container, + GtkWidget *widget) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container); + + if (!offscreen_box->child1) + gtk_offscreen_box_add1 (offscreen_box, widget); + else if (!offscreen_box->child2) + gtk_offscreen_box_add2 (offscreen_box, widget); + else + g_warning ("GtkOffscreenBox cannot have more than 2 children\n"); +} + +void +gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen_box, + GtkWidget *child) +{ + g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box)); + g_return_if_fail (GTK_IS_WIDGET (child)); + + if (offscreen_box->child1 == NULL) + { + gtk_widget_set_parent_window (child, offscreen_box->offscreen_window1); + gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box)); + offscreen_box->child1 = child; + } +} + +void +gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen_box, + GtkWidget *child) +{ + g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box)); + g_return_if_fail (GTK_IS_WIDGET (child)); + + if (offscreen_box->child2 == NULL) + { + gtk_widget_set_parent_window (child, offscreen_box->offscreen_window2); + gtk_widget_set_parent (child, GTK_WIDGET (offscreen_box)); + offscreen_box->child2 = child; + } +} + +static void +gtk_offscreen_box_remove (GtkContainer *container, + GtkWidget *widget) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container); + gboolean was_visible; + + was_visible = GTK_WIDGET_VISIBLE (widget); + + if (offscreen_box->child1 == widget) + { + gtk_widget_unparent (widget); + + offscreen_box->child1 = NULL; + + if (was_visible && GTK_WIDGET_VISIBLE (container)) + gtk_widget_queue_resize (GTK_WIDGET (container)); + } + else if (offscreen_box->child2 == widget) + { + gtk_widget_unparent (widget); + + offscreen_box->child2 = NULL; + + if (was_visible && GTK_WIDGET_VISIBLE (container)) + gtk_widget_queue_resize (GTK_WIDGET (container)); + } +} + +static void +gtk_offscreen_box_forall (GtkContainer *container, + gboolean include_internals, + GtkCallback callback, + gpointer callback_data) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (container); + + g_return_if_fail (callback != NULL); + + if (offscreen_box->child1) + (*callback) (offscreen_box->child1, callback_data); + if (offscreen_box->child2) + (*callback) (offscreen_box->child2, callback_data); +} + +void +gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen_box, + gdouble angle) +{ + g_return_if_fail (GTK_IS_OFFSCREEN_BOX (offscreen_box)); + + offscreen_box->angle = angle; + gtk_widget_queue_draw (GTK_WIDGET (offscreen_box)); + + /* TODO: Really needs to resent pointer events if over the rotated window */ +} + + +static void +gtk_offscreen_box_size_request (GtkWidget *widget, + GtkRequisition *requisition) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget); + int w, h; + + w = 0; + h = 0; + + if (offscreen_box->child1 && GTK_WIDGET_VISIBLE (offscreen_box->child1)) + { + GtkRequisition child_requisition; + + gtk_widget_size_request (offscreen_box->child1, &child_requisition); + + w = MAX (w, CHILD1_SIZE_SCALE * child_requisition.width); + h += CHILD1_SIZE_SCALE * child_requisition.height; + } + + if (offscreen_box->child2 && GTK_WIDGET_VISIBLE (offscreen_box->child2)) + { + GtkRequisition child_requisition; + + gtk_widget_size_request (offscreen_box->child2, &child_requisition); + + w = MAX (w, CHILD2_SIZE_SCALE * child_requisition.width); + h += CHILD2_SIZE_SCALE * child_requisition.height; + } + + requisition->width = GTK_CONTAINER (widget)->border_width * 2 + w; + requisition->height = GTK_CONTAINER (widget)->border_width * 2 + h; +} + +static void +gtk_offscreen_box_size_allocate (GtkWidget *widget, + GtkAllocation *allocation) +{ + GtkOffscreenBox *offscreen_box; + gint border_width; + gint start_y; + + widget->allocation = *allocation; + offscreen_box = GTK_OFFSCREEN_BOX (widget); + + border_width = GTK_CONTAINER (widget)->border_width; + + if (GTK_WIDGET_REALIZED (widget)) + gdk_window_move_resize (widget->window, + allocation->x + border_width, + allocation->y + border_width, + allocation->width - border_width * 2, + allocation->height - border_width * 2); + + start_y = 0; + + if (offscreen_box->child1 && GTK_WIDGET_VISIBLE (offscreen_box->child1)) + { + GtkRequisition child_requisition; + GtkAllocation child_allocation; + + gtk_widget_get_child_requisition (offscreen_box->child1, &child_requisition); + child_allocation.x = child_requisition.width * (CHILD1_SIZE_SCALE - 1.0) / 2; + child_allocation.y = start_y + child_requisition.height * (CHILD1_SIZE_SCALE - 1.0) / 2; + child_allocation.width = MAX (1, (gint) widget->allocation.width - 2 * border_width); + child_allocation.height = child_requisition.height; + + start_y += CHILD1_SIZE_SCALE * child_requisition.height; + + if (GTK_WIDGET_REALIZED (widget)) + gdk_window_move_resize (offscreen_box->offscreen_window1, + child_allocation.x, + child_allocation.y, + child_allocation.width, + child_allocation.height); + + child_allocation.x = child_allocation.y = 0; + gtk_widget_size_allocate (offscreen_box->child1, &child_allocation); + } + + if (offscreen_box->child2 && GTK_WIDGET_VISIBLE (offscreen_box->child2)) + { + GtkRequisition child_requisition; + GtkAllocation child_allocation; + + gtk_widget_get_child_requisition (offscreen_box->child2, &child_requisition); + child_allocation.x = child_requisition.width * (CHILD2_SIZE_SCALE - 1.0) / 2; + child_allocation.y = start_y + child_requisition.height * (CHILD2_SIZE_SCALE - 1.0) / 2; + child_allocation.width = MAX (1, (gint) widget->allocation.width - 2 * border_width); + child_allocation.height = child_requisition.height; + + start_y += CHILD2_SIZE_SCALE * child_requisition.height; + + if (GTK_WIDGET_REALIZED (widget)) + gdk_window_move_resize (offscreen_box->offscreen_window2, + child_allocation.x, + child_allocation.y, + child_allocation.width, + child_allocation.height); + + child_allocation.x = child_allocation.y = 0; + gtk_widget_size_allocate (offscreen_box->child2, &child_allocation); + } +} + +static gboolean +gtk_offscreen_box_damage (GtkWidget *widget, + GdkEventExpose *event) +{ + gdk_window_invalidate_rect (widget->window, NULL, FALSE); + + return TRUE; +} + +static gboolean +gtk_offscreen_box_expose (GtkWidget *widget, + GdkEventExpose *event) +{ + GtkOffscreenBox *offscreen_box = GTK_OFFSCREEN_BOX (widget); + + if (GTK_WIDGET_DRAWABLE (widget)) + { + if (event->window == widget->window) + { + GdkPixmap *pixmap; + GtkAllocation child_area; + cairo_t *cr; + int start_y = 0; + + if (offscreen_box->child1 && GTK_WIDGET_VISIBLE (offscreen_box->child1)) + { + pixmap = gdk_window_get_offscreen_pixmap (offscreen_box->offscreen_window1); + child_area = offscreen_box->child1->allocation; + + cr = gdk_cairo_create (widget->window); + + gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0); + cairo_paint (cr); + + cairo_destroy (cr); + + start_y += child_area.height; + } + + if (offscreen_box->child2 && GTK_WIDGET_VISIBLE (offscreen_box->child2)) + { + gint w, h; + + pixmap = gdk_window_get_offscreen_pixmap (offscreen_box->offscreen_window2); + child_area = offscreen_box->child2->allocation; + + cr = gdk_cairo_create (widget->window); + + /* transform */ + cairo_translate (cr, 0, start_y); + cairo_translate (cr, child_area.width / 2, child_area.height / 2); + cairo_rotate (cr, offscreen_box->angle); + cairo_translate (cr, -child_area.width / 2, -child_area.height / 2); + + /* clip */ + gdk_drawable_get_size (pixmap, &w, &h); + cairo_rectangle (cr, 0, 0, w, h); + cairo_clip (cr); + + /* paint */ + gdk_cairo_set_source_pixmap (cr, pixmap, 0, 0); + cairo_paint (cr); + + cairo_destroy (cr); + } + } + else if (event->window == offscreen_box->offscreen_window1) + { + gtk_paint_flat_box (widget->style, event->window, + GTK_STATE_NORMAL, GTK_SHADOW_NONE, + &event->area, widget, "blah", + 0, 0, -1, -1); + + if (offscreen_box->child1) + gtk_container_propagate_expose (GTK_CONTAINER (widget), + offscreen_box->child1, + event); + } + else if (event->window == offscreen_box->offscreen_window2) + { + gtk_paint_flat_box (widget->style, event->window, + GTK_STATE_NORMAL, GTK_SHADOW_NONE, + &event->area, widget, "blah", + 0, 0, -1, -1); + + if (offscreen_box->child2) + gtk_container_propagate_expose (GTK_CONTAINER (widget), + offscreen_box->child2, + event); + } + } + + return FALSE; +} + +static void +from_parent (GdkWindow *child, + gdouble parent_x, + gdouble parent_y, + gdouble *child_x, + gdouble *child_y) +{ + GtkOffscreenBox *offscreen_box = NULL; + GtkAllocation child2_area; + gpointer window_data; + double pos_x, pos_y, rot_x, rot_y, start_y, angle; + GdkWindow *parent; + + parent = gdk_window_get_parent (child); + gdk_window_get_user_data (parent, &window_data); + offscreen_box = window_data; + + start_y = offscreen_box->child1 ? offscreen_box->child1->allocation.height : 0; + child2_area = offscreen_box->child2->allocation; + + pos_x = parent_x - child2_area.width / 2; + pos_y = parent_y - start_y - child2_area.height / 2; + + angle = -offscreen_box->angle; + rot_x = pos_x * cos (angle) - pos_y * sin (angle); + rot_y = pos_x * sin (angle) + pos_y * cos (angle); + + *child_x = rot_x + child2_area.width / 2; + *child_y = rot_y + child2_area.height / 2; +} + +static void +to_parent (GdkWindow *child, + gdouble child_x, + gdouble child_y, + gdouble *parent_x, + gdouble *parent_y) +{ + GtkOffscreenBox *offscreen_box = NULL; + GtkAllocation child2_area; + gpointer window_data; + gdouble pos_x, pos_y, rot_x, rot_y, start_y, angle; + GdkWindow *parent; + + parent = gdk_window_get_parent (child); + gdk_window_get_user_data (parent, &window_data); + offscreen_box = window_data; + + start_y = offscreen_box->child1 ? offscreen_box->child1->allocation.height : 0; + child2_area = offscreen_box->child2->allocation; + + pos_x = child_x - child2_area.width / 2; + pos_y = child_y - child2_area.height / 2; + + angle = offscreen_box->angle; + rot_x = pos_x * cos (angle) - pos_y * sin (angle); + rot_y = pos_x * sin (angle) + pos_y * cos (angle); + + rot_x += child2_area.width / 2; + rot_y += child2_area.height / 2; + + *parent_x = rot_x; + *parent_y = rot_y + start_y; +} diff --git a/tests/gtkoffscreenbox.h b/tests/gtkoffscreenbox.h new file mode 100644 index 0000000000..aa1d15d775 --- /dev/null +++ b/tests/gtkoffscreenbox.h @@ -0,0 +1,52 @@ +#ifndef __GTK_OFFSCREEN_BOX_H__ +#define __GTK_OFFSCREEN_BOX_H__ + + +#include +#include + + +G_BEGIN_DECLS + +#define GTK_TYPE_OFFSCREEN_BOX (gtk_offscreen_box_get_type ()) +#define GTK_OFFSCREEN_BOX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GTK_TYPE_OFFSCREEN_BOX, GtkOffscreenBox)) +#define GTK_OFFSCREEN_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_OFFSCREEN_BOX, GtkOffscreenBoxClass)) +#define GTK_IS_OFFSCREEN_BOX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GTK_TYPE_OFFSCREEN_BOX)) +#define GTK_IS_OFFSCREEN_BOX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_OFFSCREEN_BOX)) +#define GTK_OFFSCREEN_BOX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_OFFSCREEN_BOX, GtkOffscreenBoxClass)) + +typedef struct _GtkOffscreenBox GtkOffscreenBox; +typedef struct _GtkOffscreenBoxClass GtkOffscreenBoxClass; + +struct _GtkOffscreenBox +{ + GtkContainer container; + + GtkWidget *child1; + GtkWidget *child2; + + GdkWindow *offscreen_window1; + GdkWindow *offscreen_window2; + + gdouble angle; +}; + +struct _GtkOffscreenBoxClass +{ + GtkBinClass parent_class; +}; + +GType gtk_offscreen_box_get_type (void) G_GNUC_CONST; +GtkWidget* gtk_offscreen_box_new (void); +void gtk_offscreen_box_add1 (GtkOffscreenBox *offscreen, + GtkWidget *child); +void gtk_offscreen_box_add2 (GtkOffscreenBox *offscreen, + GtkWidget *child); +void gtk_offscreen_box_set_angle (GtkOffscreenBox *offscreen, + double angle); + + + +G_END_DECLS + +#endif /* __GTK_OFFSCREEN_BOX_H__ */ diff --git a/tests/testoffscreen.c b/tests/testoffscreen.c new file mode 100644 index 0000000000..f332d62c42 --- /dev/null +++ b/tests/testoffscreen.c @@ -0,0 +1,381 @@ +/* + * testoffscreen.c + */ + +#undef GTK_DISABLE_DEPRECATED + +#include +#include +#include "gtkoffscreenbox.h" + + +static void +combo_changed_cb (GtkWidget *combo, + gpointer data) +{ + GtkWidget *label = GTK_WIDGET (data); + gint active; + + active = gtk_combo_box_get_active (GTK_COMBO_BOX (combo)); + + gtk_label_set_ellipsize (GTK_LABEL (label), (PangoEllipsizeMode)active); +} + +static gboolean +layout_expose_handler (GtkWidget *widget, + GdkEventExpose *event) +{ + GtkLayout *layout = GTK_LAYOUT (widget); + + gint i,j; + gint imin, imax, jmin, jmax; + + if (event->window != layout->bin_window) + return FALSE; + + imin = (event->area.x) / 10; + imax = (event->area.x + event->area.width + 9) / 10; + + jmin = (event->area.y) / 10; + jmax = (event->area.y + event->area.height + 9) / 10; + + for (i = imin; i < imax; i++) + for (j = jmin; j < jmax; j++) + if ((i + j) % 2) + gdk_draw_rectangle (layout->bin_window, + widget->style->black_gc, + TRUE, + 10 * i, 10 * j, + 1 + i % 10, 1 + j % 10); + + return FALSE; +} + +static gboolean +scroll_layout (gpointer data) +{ + GtkWidget *layout = data; + GtkAdjustment *adj; + + adj = gtk_layout_get_hadjustment (GTK_LAYOUT (layout)); + gtk_adjustment_set_value (adj, + gtk_adjustment_get_value (adj) + 5.0); + return TRUE; +} + +static guint layout_timeout; + +static void +create_layout (GtkWidget *vbox) +{ + GtkWidget *layout; + GtkWidget *scrolledwindow; + GtkWidget *button; + gchar buf[16]; + gint i, j; + + scrolledwindow = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), + GTK_SHADOW_IN); + gtk_scrolled_window_set_placement (GTK_SCROLLED_WINDOW (scrolledwindow), + GTK_CORNER_TOP_RIGHT); + + gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0); + + layout = gtk_layout_new (NULL, NULL); + gtk_container_add (GTK_CONTAINER (scrolledwindow), layout); + + /* We set step sizes here since GtkLayout does not set + * them itself. + */ + GTK_LAYOUT (layout)->hadjustment->step_increment = 10.0; + GTK_LAYOUT (layout)->vadjustment->step_increment = 10.0; + + gtk_widget_set_events (layout, GDK_EXPOSURE_MASK); + g_signal_connect (layout, "expose_event", + G_CALLBACK (layout_expose_handler), + NULL); + + gtk_layout_set_size (GTK_LAYOUT (layout), 1600, 128000); + + for (i = 0 ; i < 16 ; i++) + for (j = 0 ; j < 16 ; j++) + { + g_snprintf (buf, sizeof (buf), "Button %d, %d", i, j); + + if ((i + j) % 2) + button = gtk_button_new_with_label (buf); + else + button = gtk_label_new (buf); + + gtk_layout_put (GTK_LAYOUT (layout), button, + j * 100, i * 100); + } + + for (i = 16; i < 1280; i++) + { + g_snprintf (buf, sizeof (buf), "Button %d, %d", i, 0); + + if (i % 2) + button = gtk_button_new_with_label (buf); + else + button = gtk_label_new (buf); + + gtk_layout_put (GTK_LAYOUT (layout), button, + 0, i * 100); + } + + layout_timeout = g_timeout_add (1000, scroll_layout, layout); +} + +static void +create_treeview (GtkWidget *vbox) +{ + GtkWidget *scrolledwindow; + GtkListStore *store; + GtkWidget *tree_view; + GSList *stock_ids; + GSList *list; + + scrolledwindow = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwindow), + GTK_SHADOW_IN); + + gtk_box_pack_start (GTK_BOX (vbox), scrolledwindow, TRUE, TRUE, 0); + + store = gtk_list_store_new (2, G_TYPE_STRING, G_TYPE_STRING); + tree_view = gtk_tree_view_new_with_model (GTK_TREE_MODEL (store)); + g_object_unref (store); + + gtk_container_add (GTK_CONTAINER (scrolledwindow), tree_view); + + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), -1, + "Icon", + gtk_cell_renderer_pixbuf_new (), + "stock-id", 0, + NULL); + gtk_tree_view_insert_column_with_attributes (GTK_TREE_VIEW (tree_view), -1, + "Label", + gtk_cell_renderer_text_new (), + "text", 1, + NULL); + + stock_ids = gtk_stock_list_ids (); + + for (list = stock_ids; list; list = g_slist_next (list)) + { + const gchar *stock_id = list->data; + GtkStockItem item; + + if (gtk_stock_lookup (stock_id, &item)) + { + gtk_list_store_insert_with_values (store, NULL, -1, + 0, item.stock_id, + 1, item.label, + -1); + } + } + + g_slist_foreach (stock_ids, (GFunc) g_free, NULL); + g_slist_free (stock_ids); +} + +static GtkWidget * +create_widgets (void) +{ + GtkWidget *main_hbox, *main_vbox; + GtkWidget *vbox, *hbox, *label, *combo, *entry, *button, *cb; + GtkWidget *sw, *text_view; + GList *cbitems = NULL; + + main_vbox = gtk_vbox_new (0, FALSE); + + main_hbox = gtk_hbox_new (0, FALSE); + gtk_box_pack_start (GTK_BOX (main_vbox), main_hbox, TRUE, TRUE, 0); + + vbox = gtk_vbox_new (0, FALSE); + gtk_box_pack_start (GTK_BOX (main_hbox), vbox, TRUE, TRUE, 0); + + hbox = gtk_hbox_new (0, FALSE); + gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); + + label = gtk_label_new ("This label may be ellipsized\nto make it fit."); + gtk_box_pack_start (GTK_BOX (hbox), label, TRUE, TRUE, 0); + + combo = gtk_combo_box_new_text (); + gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "NONE"); + gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "START"); + gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "MIDDLE"); + gtk_combo_box_append_text (GTK_COMBO_BOX (combo), "END"); + gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0); + gtk_box_pack_start (GTK_BOX (hbox), combo, TRUE, TRUE, 0); + + g_signal_connect (combo, "changed", + G_CALLBACK (combo_changed_cb), + label); + + entry = gtk_entry_new (); + gtk_entry_set_text (GTK_ENTRY (entry), "an entry - lots of text.... lots of text.... lots of text.... lots of text.... "); + gtk_box_pack_start (GTK_BOX (vbox), entry, FALSE, FALSE, 0); + + label = gtk_label_new ("Label after entry."); + gtk_label_set_selectable (GTK_LABEL (label), TRUE); + gtk_box_pack_start (GTK_BOX (vbox), label, TRUE, TRUE, 0); + + button = gtk_button_new_with_label ("Button"); + gtk_box_pack_start (GTK_BOX (vbox), button, TRUE, TRUE, 0); + + button = gtk_check_button_new_with_mnemonic ("_Check button"); + gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); + + cb = gtk_combo_new (); + cbitems = g_list_append (cbitems, "item0"); + cbitems = g_list_append (cbitems, "item1 item1"); + cbitems = g_list_append (cbitems, "item2 item2 item2"); + gtk_combo_set_popdown_strings (GTK_COMBO (cb), cbitems); + gtk_entry_set_text (GTK_ENTRY (GTK_COMBO (cb)->entry), "hello world ♥ foo"); + gtk_editable_select_region (GTK_EDITABLE (GTK_COMBO (cb)->entry), + 0, -1); + gtk_box_pack_start (GTK_BOX (vbox), cb, TRUE, TRUE, 0); + + sw = gtk_scrolled_window_new (NULL, NULL); + gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (sw), + GTK_POLICY_AUTOMATIC, + GTK_POLICY_AUTOMATIC); + text_view = gtk_text_view_new (); + gtk_box_pack_start (GTK_BOX (vbox), sw, TRUE, TRUE, 0); + gtk_container_add (GTK_CONTAINER (sw), text_view); + + create_layout (vbox); + + create_treeview (main_hbox); + + return main_vbox; +} + + +static void +scale_changed (GtkRange *range, + GtkOffscreenBox *offscreen_box) +{ + gtk_offscreen_box_set_angle (offscreen_box, gtk_range_get_value (range)); +} + +static GtkWidget *scale = NULL; + +static void +remove_clicked (GtkButton *button, + GtkWidget *widget) +{ + gtk_widget_destroy (widget); + g_source_remove (layout_timeout); + + gtk_widget_set_sensitive (GTK_WIDGET (button), FALSE); + gtk_widget_set_sensitive (scale, FALSE); +} + +int +main (int argc, + char *argv[]) +{ + GtkWidget *window, *widget, *vbox, *button; + GtkWidget *offscreen = NULL; + gboolean use_offscreen; + + gtk_init (&argc, &argv); + + use_offscreen = argc == 1; + + window = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (window), 300,300); + + g_signal_connect (window, "destroy", + G_CALLBACK (gtk_main_quit), + NULL); + + vbox = gtk_vbox_new (0, FALSE); + gtk_container_add (GTK_CONTAINER (window), vbox); + + scale = gtk_hscale_new_with_range (0, + M_PI * 2, + 0.01); + gtk_box_pack_start (GTK_BOX (vbox), scale, FALSE, FALSE, 0); + + button = gtk_button_new_with_label ("Remove child 2"); + gtk_box_pack_start (GTK_BOX (vbox), button, FALSE, FALSE, 0); + + if (use_offscreen) + { + offscreen = gtk_offscreen_box_new (); + + g_signal_connect (scale, "value_changed", + G_CALLBACK (scale_changed), + offscreen); + } + else + { + offscreen = gtk_vpaned_new (); + } + + gtk_box_pack_start (GTK_BOX (vbox), offscreen, TRUE, TRUE, 0); + + widget = create_widgets (); + if (use_offscreen) + gtk_offscreen_box_add1 (GTK_OFFSCREEN_BOX (offscreen), + widget); + else + gtk_paned_add1 (GTK_PANED (offscreen), widget); + + widget = create_widgets (); + if (1) + { + GtkWidget *widget2, *box2, *offscreen2; + + offscreen2 = gtk_offscreen_box_new (); + gtk_box_pack_start (GTK_BOX (widget), offscreen2, FALSE, FALSE, 0); + + g_signal_connect (scale, "value_changed", + G_CALLBACK (scale_changed), + offscreen2); + + box2 = gtk_vbox_new (FALSE, 0); + gtk_offscreen_box_add2 (GTK_OFFSCREEN_BOX (offscreen2), box2); + + widget2 = gtk_button_new_with_label ("Offscreen in offscreen"); + gtk_box_pack_start (GTK_BOX (box2), widget2, FALSE, FALSE, 0); + + widget2 = gtk_entry_new (); + gtk_entry_set_text (GTK_ENTRY (widget2), "Offscreen in offscreen"); + gtk_box_pack_start (GTK_BOX (box2), widget2, FALSE, FALSE, 0); + } + + if (use_offscreen) + gtk_offscreen_box_add2 (GTK_OFFSCREEN_BOX (offscreen), widget); + else + gtk_paned_add2 (GTK_PANED (offscreen), widget); + + gtk_widget_show_all (window); + + g_signal_connect (G_OBJECT (button), "clicked", + G_CALLBACK (remove_clicked), + widget); + + /* redirect */ + if (0) + { + GtkWidget *redirect_win; + + redirect_win = gtk_window_new (GTK_WINDOW_TOPLEVEL); + gtk_window_set_default_size (GTK_WINDOW (redirect_win), 400,400); + gtk_widget_show (redirect_win); + gtk_widget_realize (redirect_win); + gtk_widget_realize (window); + gdk_window_redirect_to_drawable (window->window, + GDK_DRAWABLE (redirect_win->window), + 0, 0, 0, 0, -1, -1); + } + + gtk_main (); + + return 0; +}