+ Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+ Lisp_Object orient = find_keyword_in_vector (instantiator, Q_orientation);
+
+#ifdef DEBUG_WIDGET_OUTPUT
+ stderr_out ("layout instantiated\n");
+#endif
+ /* Do widget type instantiation first. */
+ widget_instantiate (image_instance, instantiator, pointer_fg, pointer_bg,
+ dest_mask, domain);
+
+ if (NILP (orient))
+ {
+ IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii) = LAYOUT_VERTICAL;
+ }
+
+ /* Get child glyphs and finish instantiation. We can't do image
+ instance children yet as we might not have a containing
+ window. */
+ layout_update (image_instance, instantiator);
+}
+
+static void
+layout_post_instantiate (Lisp_Object image_instance, Lisp_Object instantiator,
+ Lisp_Object domain)
+{
+}
+
+/* Layout widget. Sizing commentary: we have a number of problems that
+ we would like to address. Some consider some of these more
+ important than others. It used to be that size information was
+ determined at instantiation time and was then fixed forever
+ after. Generally this is not what we want. Users want size to be
+ "big enough" to accommodate whatever they are trying to show and
+ this is dependent on text length, lines, font metrics etc. Of
+ course these attributes can change dynamically and so the size
+ should changed dynamically also. Only in a few limited cases should
+ the size be fixed and remain fixed. Of course this actually means
+ that we don't really want to specify the size *at all* for most
+ widgets - we want it to be discovered dynamically. Thus we can
+ envisage the following scenarios:
+
+ 1. A button is sized to accommodate its text, the text changes and the
+ button should change size also.
+
+ 2. A button is given an explicit size. Its size should never change.
+
+ 3. Layout is put inside an area. The size of the area changes, the
+ layout should change with it.
+
+ 4. A button grows to accommodate additional text. The whitespace
+ around it should be modified to cope with the new layout
+ requirements.
+
+ 5. A button grows. The area surrounding it should grow also if
+ possible.
+
+ What metrics are important?
+ 1. Actual width and height.
+
+ 2. Whether the width and height are what the widget actually wants, or
+ whether it can grow or shrink.
+
+ Text glyphs are particularly troublesome since their metrics depend
+ on the context in which they are being viewed. For instance they
+ can appear differently depending on the window face, frame face or
+ glyph face. In order to simplify this text glyphs can now only have
+ a glyph-face or image-instance face. All other glyphs are
+ essentially fixed in appearance. Perhaps the problem is that text
+ glyphs are cached on a device basis like most other glyphs. Instead
+ they should be cached per-window and then the instance would be
+ fixed and we wouldn't have to mess around with font metrics and the
+ rest. */
+
+/* Query the geometry of a layout widget. We assume that we can only
+ get here if the size is not already fixed. */
+static void
+layout_query_geometry (Lisp_Object image_instance, int* width,
+ int* height, enum image_instance_geometry disp,
+ Lisp_Object domain)
+{
+ Lisp_Image_Instance *ii = XIMAGE_INSTANCE (image_instance);
+ Lisp_Object items = IMAGE_INSTANCE_LAYOUT_CHILDREN (ii), rest;
+ int maxph = 0, maxpw = 0, nitems = 0, ph_adjust = 0;
+ int gheight, gwidth;
+
+ /* If we are not initialized then we won't have any children. */
+ if (!IMAGE_INSTANCE_INITIALIZED (ii))
+ return;
+
+ /* First just set up what we already have. */
+ if (width) *width = IMAGE_INSTANCE_WIDTH (ii);
+ if (height) *height = IMAGE_INSTANCE_HEIGHT (ii);
+
+ /* If we are not allowed to dynamically size then return. */
+ if (!IMAGE_INSTANCE_SUBWINDOW_V_RESIZEP (ii)
+ &&
+ !IMAGE_INSTANCE_SUBWINDOW_H_RESIZEP (ii))
+ return;
+
+ /* Pick up the border text if we have one. */
+ if (INTP (IMAGE_INSTANCE_LAYOUT_BORDER (ii)))
+ {
+ glyph_query_geometry (XCAR (items), &gwidth, &gheight, disp,
+ image_instance);
+ ph_adjust = gheight / 2;
+ items = XCDR (items);
+ }
+
+ /* 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, disp, 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 minimum space we need to fit all the items. This could
+ have been fixed by the user. */
+ if (!NILP (IMAGE_INSTANCE_WIDGET_WIDTH_SUBR (ii)))
+ {
+ Lisp_Object dynamic_width =
+ Feval (IMAGE_INSTANCE_WIDGET_WIDTH_SUBR (ii));
+ if (INTP (dynamic_width))
+ *width = XINT (dynamic_width);
+ }
+ else if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_HORIZONTAL)
+ *width = maxpw + ((nitems + 1) * WIDGET_BORDER_WIDTH +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii)) * 2;
+ else
+ *width = maxpw + 2 * (WIDGET_BORDER_WIDTH * 2 +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii));
+
+ /* Work out vertical spacings. */
+ if (!NILP (IMAGE_INSTANCE_WIDGET_HEIGHT_SUBR (ii)))
+ {
+ Lisp_Object dynamic_height =
+ Feval (IMAGE_INSTANCE_WIDGET_HEIGHT_SUBR (ii));
+ if (INTP (dynamic_height))
+ *height = XINT (dynamic_height);
+ }
+ else if (IMAGE_INSTANCE_SUBWINDOW_ORIENT (ii)
+ == LAYOUT_VERTICAL)
+ *height = maxph + ((nitems + 1) * WIDGET_BORDER_HEIGHT +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii)) * 2 + ph_adjust;
+ else
+ *height = maxph + (2 * WIDGET_BORDER_HEIGHT +
+ IMAGE_INSTANCE_MARGIN_WIDTH (ii)) * 2 + ph_adjust;
+}
+
+int
+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;
+ 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;