+ Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+ Lisp_Object rest;
+ Lisp_Object items = IMAGE_INSTANCE_LAYOUT_CHILDREN (ii);
+ int x, y, maxph = 0, maxpw = 0, nitems = 0,
+ horiz_spacing, vert_spacing, ph_adjust = 0;
+ int gheight, gwidth;
+
+ /* If we are not initialized then we won't have any children. */
+ if (!IMAGE_INSTANCE_INITIALIZED (ii))
+ return 0;
+
+ /* Pick up the border text if we have one. */
+ if (INTP (IMAGE_INSTANCE_LAYOUT_BORDER (ii)))
+ {
+ Lisp_Object border = XCAR (items);
+ items = XCDR (items);
+ glyph_query_geometry (border, &gwidth, &gheight,
+ IMAGE_DESIRED_GEOMETRY, image_instance);
+ ph_adjust = gheight / 2;
+ IMAGE_INSTANCE_LAYOUT_BORDER (ii) = make_int (ph_adjust);
+
+ /* #### Really, what should this be? */
+ glyph_do_layout (border, gwidth, gheight, 10, 0,
+ image_instance);
+ }
+
+ /* Flip through the items to work out how much stuff we have to display. */
+ LIST_LOOP (rest, items)
+ {
+ Lisp_Object glyph = XCAR (rest);
+
+ glyph_query_geometry (glyph, &gwidth, &gheight,
+ IMAGE_DESIRED_GEOMETRY, image_instance);
+ nitems ++;
+ if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_HORIZONTAL)
+ {
+ maxph = max (maxph, gheight);
+ maxpw += gwidth;
+ }
+ else
+ {
+ maxpw = max (maxpw, gwidth);
+ maxph += gheight;
+ }
+ }
+
+ /* work out spacing between items and bounds of the layout */
+ if (width < maxpw)
+ /* The user wants a smaller space than the largest item, so we
+ just provide default spacing and will let the output routines
+ clip.. */
+ horiz_spacing = WIDGET_BORDER_WIDTH * 2;
+ else if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_HORIZONTAL)
+ /* We have a larger area to display in so distribute the space
+ evenly. */
+ horiz_spacing = (width - (maxpw +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii) * 2))
+ / (nitems + 1);
+ else
+ horiz_spacing = (width - maxpw) / 2
+ - IMAGE_INSTANCE_MARGIN_WIDTH (ii);
+
+ if (height < maxph)
+ vert_spacing = WIDGET_BORDER_HEIGHT * 2;
+ else if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_VERTICAL)
+ vert_spacing = (height - (maxph + ph_adjust +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii) * 2))
+ / (nitems + 1);
+ else
+ vert_spacing = (height - (maxph + ph_adjust)) / 2
+ - IMAGE_INSTANCE_MARGIN_WIDTH (ii);
+
+ y = vert_spacing + ph_adjust + IMAGE_INSTANCE_MARGIN_WIDTH (ii);
+ x = horiz_spacing + IMAGE_INSTANCE_MARGIN_WIDTH (ii);
+
+ /* Now flip through putting items where we want them, paying
+ attention to justification. Make sure we don't mess with the
+ border glyph. */
+ LIST_LOOP (rest, items)
+ {
+ Lisp_Object glyph = XCAR (rest);
+
+ glyph_query_geometry (glyph, &gwidth, &gheight,
+ IMAGE_DESIRED_GEOMETRY, image_instance);
+
+ if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_HORIZONTAL)
+ {
+ if (IMAGE_INSTANCE_SUBWINDOW_JUSTIFY (ii)
+ == LAYOUT_JUSTIFY_RIGHT)
+ y = height - (gheight + vert_spacing);
+ if (IMAGE_INSTANCE_SUBWINDOW_JUSTIFY (ii)
+ == LAYOUT_JUSTIFY_CENTER)
+ y = (height - gheight) / 2;
+ }
+ else
+ {
+ if (IMAGE_INSTANCE_SUBWINDOW_JUSTIFY (ii)
+ == LAYOUT_JUSTIFY_RIGHT)
+ x = width - (gwidth + horiz_spacing);
+ if (IMAGE_INSTANCE_SUBWINDOW_JUSTIFY (ii)
+ == LAYOUT_JUSTIFY_CENTER)
+ x = (width - gwidth) / 2;
+ }
+
+ /* Now layout subwidgets if they require it. */
+ glyph_do_layout (glyph, gwidth, gheight, x, y, image_instance);
+
+ if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_HORIZONTAL)
+ {
+ x += (gwidth + horiz_spacing);
+ }
+ else
+ {
+ y += (gheight + vert_spacing);
+ }
+
+ }
+ return 1;
+}
+
+/* Get the glyphs that comprise a layout. These are created internally
+ and so are otherwise inaccessible to lisp. We need some way of getting
+ properties from the widgets that comprise a layout and this is the
+ simplest way of doing it.
+
+ #### Eventually we should allow some more intelligent access to
+ sub-widgets. */
+static Lisp_Object
+layout_property (Lisp_Object image_instance, Lisp_Object prop)
+{
+ /* This function can GC. */
+ Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+ if (EQ (prop, Q_items))
+ {
+ if (INTP (IMAGE_INSTANCE_LAYOUT_BORDER (ii)) &&
+ CONSP (IMAGE_INSTANCE_LAYOUT_CHILDREN (ii)))
+ return Fcopy_sequence (XCDR
+ (IMAGE_INSTANCE_LAYOUT_CHILDREN (ii)));
+ else
+ return Fcopy_sequence (IMAGE_INSTANCE_LAYOUT_CHILDREN (ii));
+ }
+ return Qunbound;
+}
+
+/* Layout subwindows if they are real subwindows. */
+static int
+native_layout_layout (Lisp_Object image_instance,
+ int width, int height, int xoffset, int yoffset,
+ Lisp_Object domain)
+{
+ Lisp_Image_Instance* ii = XIMAGE_INSTANCE (image_instance);
+ Lisp_Object rest;
+
+ /* The first time this gets called, the layout will be only
+ partially instantiated. The children get done in
+ post_instantiate. */
+ if (!IMAGE_INSTANCE_INITIALIZED (ii))
+ return 0;
+
+ /* Defining this overrides the default layout_layout so we first have to call that to get
+ suitable instances and values set up. */
+ layout_layout (image_instance, width, height, xoffset, yoffset, domain);
+
+ LIST_LOOP (rest, IMAGE_INSTANCE_LAYOUT_CHILDREN (ii))
+ {
+ struct display_glyph_area dga;
+ dga.xoffset = 0;
+ dga.yoffset = 0;
+ dga.width = IMAGE_INSTANCE_WIDTH (ii);
+ dga.height = IMAGE_INSTANCE_HEIGHT (ii);
+
+ map_subwindow (XCAR (rest),
+ IMAGE_INSTANCE_XOFFSET (ii),
+ IMAGE_INSTANCE_YOFFSET (ii), &dga);
+ }
+ return 1;