+/****************************************************************
+ VERTICAL ORDERING
+****************************************************************/
+
+Grob *
+get_maybe_root_vertical_alignment (Grob *g, Grob *maybe)
+{
+ if (!g)
+ return maybe;
+ if (Align_interface::has_interface (g))
+ return get_maybe_root_vertical_alignment (g->get_parent (Y_AXIS), g);
+ return get_maybe_root_vertical_alignment (g->get_parent (Y_AXIS), maybe);
+
+}
+
+Grob *
+Grob::get_root_vertical_alignment (Grob *g)
+{
+ return get_maybe_root_vertical_alignment (g, 0);
+}
+
+Grob *
+Grob::get_vertical_axis_group (Grob *g)
+{
+ if (!g)
+ return 0;
+ if (!g->get_parent (Y_AXIS))
+ return 0;
+ if (Axis_group_interface::has_interface (g)
+ && Align_interface::has_interface (g->get_parent (Y_AXIS)))
+ return g;
+ return get_vertical_axis_group (g->get_parent (Y_AXIS));
+
+}
+
+int
+Grob::get_vertical_axis_group_index (Grob *g)
+{
+ Grob *val = get_root_vertical_alignment (g);
+ if (!val)
+ return -1;
+ Grob *vax = get_vertical_axis_group (g);
+ extract_grob_set (val, "elements", elts);
+ for (vsize i = 0; i < elts.size (); i++)
+ if (elts[i] == vax)
+ return (int) i;
+ g->programming_error ("could not find this grob's vertical axis group in the vertical alignment");
+ return -1;
+}
+
+bool
+Grob::vertical_less (Grob *g1, Grob *g2)
+{
+ return internal_vertical_less (g1, g2, false);
+}
+
+bool
+Grob::pure_vertical_less (Grob *g1, Grob *g2)
+{
+ return internal_vertical_less (g1, g2, true);
+}
+
+bool
+Grob::internal_vertical_less (Grob *g1, Grob *g2, bool pure)
+{
+ Grob *vag = get_root_vertical_alignment (g1);
+ if (!vag)
+ {
+ g1->programming_error ("grob does not belong to a VerticalAlignment?");
+ return false;
+ }
+
+ Grob *ag1 = get_vertical_axis_group (g1);
+ Grob *ag2 = get_vertical_axis_group (g2);
+
+ extract_grob_set (vag, "elements", elts);
+
+ if (ag1 == ag2 && !pure)
+ {
+ Grob *common = g1->common_refpoint (g2, Y_AXIS);
+ return g1->relative_coordinate (common, Y_AXIS) > g2->relative_coordinate (common, Y_AXIS);
+ }
+
+ for (vsize i = 0; i < elts.size (); i++)
+ {
+ if (elts[i] == ag1)
+ return true;
+ if (elts[i] == ag2)
+ return false;
+ }
+
+ g1->programming_error ("could not place this grob in its axis group");
+ return false;
+}