]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/score-element.cc
release: 1.3.70
[lilypond.git] / lily / score-element.cc
index 4f1870886a9857aa998987fff9ce24b2fc3dc032..47ca5c9eea8f0659b090a9a8bbb520ad539630af 100644 (file)
@@ -8,7 +8,9 @@
 
 
 #include <string.h>
+#include <math.h>
 
+#include "libc-extension.hh"
 #include "group-interface.hh"
 #include "misc.hh"
 #include "paper-score.hh"
 #include "side-position-interface.hh"
 #include "item.hh"
 
-Score_element::Score_element()
+/*
+TODO:
+
+remove dynamic_cast<Spanner,Item> and put this code into respective
+  subclass.
+*/
+
+
+#define INFINITY_MSG "Infinity or NaN encountered"
+
+Score_element::Score_element(SCM basicprops)
 {
-  dim_cache_[X_AXIS] = new Dimension_cache;
-  dim_cache_[Y_AXIS] = new Dimension_cache;
-  dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
-  
-  used_b_ = false;
+  set_extent_callback (molecule_extent, X_AXIS);
+  set_extent_callback (molecule_extent, Y_AXIS);    
 
-  dim_cache_[X_AXIS]->set_callback (molecule_extent);
-  dim_cache_[Y_AXIS]->set_callback (molecule_extent); 
-  used_b_ = false;
   pscore_l_=0;
   lookup_l_ =0;
   status_i_ = 0;
   self_scm_ = SCM_EOL;
   original_l_ = 0;
-  element_property_alist_ = SCM_EOL;
+  immutable_property_alist_ =  basicprops;
+  mutable_property_alist_ = SCM_EOL;
 
   smobify_self ();
-
-
   set_elt_property ("dependencies", SCM_EOL);
-  set_elt_property ("interfaces", SCM_EOL);
+
+  if (get_elt_property ("interfaces") == SCM_UNDEFINED)
+    set_elt_property ("interfaces", SCM_EOL);
 }
 
 
 Score_element::Score_element (Score_element const&s)
+   : dim_cache_ (s.dim_cache_)
 {
-  dim_cache_[X_AXIS] = new Dimension_cache (*s.dim_cache_[X_AXIS]);
-  dim_cache_[Y_AXIS] = new Dimension_cache (*s.dim_cache_[Y_AXIS]);
-  dim_cache_[X_AXIS]->elt_l_ = dim_cache_[Y_AXIS]->elt_l_ = this;
-  
   self_scm_ = SCM_EOL;
-  used_b_ = true;
   original_l_ =(Score_element*) &s;
-  element_property_alist_ = SCM_EOL; // onstack;
-
+  immutable_property_alist_ = s.immutable_property_alist_;
+  mutable_property_alist_ = SCM_EOL;
+  
   status_i_ = s.status_i_;
   lookup_l_ = s.lookup_l_;
   pscore_l_ = s.pscore_l_;
@@ -74,84 +78,107 @@ Score_element::Score_element (Score_element const&s)
 
 Score_element::~Score_element()
 {
-  assert (status_i_ >=0);
-  status_i_  = -1;
+  // this goes awry when score-elements are copied...
 
-  delete dim_cache_[X_AXIS];
-  delete dim_cache_[Y_AXIS];  
+  /*
+     Kijk goed naar hoe smobify en unsmobify werken.  unsmobify_self
+    is te gebruiken wanneer C++ geheugen beheer weer overneemt van
+    GUILE. --hwn */
 }
 
-// should also have one that takes SCM arg. 
+
 SCM
-Score_element::get_elt_property (String nm) const
+Score_element::get_elt_property (const char *nm) const
 {
-  SCM sym =  ly_symbol2scm (nm.ch_C());
-  SCM s = scm_assq(sym, element_property_alist_);
+  SCM sym = ly_symbol2scm (nm);
+  return get_elt_property (sym);
+}
 
+SCM
+Score_element::get_elt_property (SCM sym) const
+{
+  SCM s = scm_assq(sym, mutable_property_alist_);
   if (s != SCM_BOOL_F)
-    return gh_cdr (s); 
-  
-  if (pscore_l_)
-    {
-      SCM sym2 = ly_symbol2scm ((name () + ("::" + nm)).ch_C());
-      SCM val;
-      
-      // should probably check for Type::sym as well.
-      Paper_def * p= pscore_l_->paper_l_;
-      if (p->default_properties_.try_retrieve (sym2, &val))
-       return val;
-      else if (p->default_properties_.try_retrieve (sym, &val))
-       return val;
-    }
-  
-  return SCM_UNDEFINED;
+    return gh_cdr (s);
+
+  s = scm_assq (sym, immutable_property_alist_);
+  return (s == SCM_BOOL_F) ? SCM_UNDEFINED : gh_cdr (s); 
 }
 
+/*
+  Remove the value associated with KEY, and return it. The result is
+  that a next call will yield SCM_UNDEFINED (and not the underlying
+  `basic' property.
+*/
 SCM
-Score_element::remove_elt_property (String key)
+Score_element::remove_elt_property (const char* key)
 {
-  SCM s = get_elt_property (key); 
-  SCM sym = ly_symbol2scm (key.ch_C());
-  element_property_alist_ =  scm_assq_remove_x (element_property_alist_, sym);
-  return s;
+  SCM val = get_elt_property (key);
+  if (val != SCM_UNDEFINED)
+    set_elt_property (key, SCM_UNDEFINED);
+  return val;
+}
+
+void
+Score_element::set_elt_property (const char* k, SCM v)
+{
+  SCM s = ly_symbol2scm (k);
+  set_elt_property (s, v);
 }
 
 /*
-  UGH. assoc vs. assq
+  Puts the k, v in the immutable_property_alist_, which is convenient for
+  storing variables that are needed during the breaking process. (eg.
+  Line_of_score::rank : int )
  */
 void
-Score_element::set_elt_property (String k, SCM v)
+Score_element::set_immutable_elt_property (const char*k, SCM v)
 {
-  SCM s = ly_symbol2scm (k.ch_C( ));
-  element_property_alist_ = scm_assoc_set_x (element_property_alist_, s, v);
+  SCM s = ly_symbol2scm (k);
+  set_immutable_elt_property (s, v);
 }
 
-Interval
-Score_element::molecule_extent(Dimension_cache const *c)
+void
+Score_element::set_immutable_elt_property (SCM s, SCM v)
 {
-  Score_element *s = dynamic_cast<Score_element*>(c->element_l());
-  Molecule m = s->do_brew_molecule();
-  return   m.extent()[c->axis ()];
+  immutable_property_alist_ = gh_cons (gh_cons (s,v), mutable_property_alist_);
+  mutable_property_alist_ = scm_assq_remove_x (mutable_property_alist_, s);
+}
+void
+Score_element::set_elt_property (SCM s, SCM v)
+{
+  mutable_property_alist_ = scm_assq_set_x (mutable_property_alist_, s, v);
 }
 
 
-void
-Score_element::print() const
+Interval
+Score_element::molecule_extent (Score_element *s, Axis a)
 {
-#ifndef NPRINT
-  DEBUG_OUT << classname(this) << "{\n";
-    
-  if (flower_dstream && !flower_dstream->silent_b ("Score_element"))
-    ly_display_scm (element_property_alist_);
+  Molecule m = s->get_molecule ();
+  return m.extent(a);
+}
 
-  if (original_l_)
-    DEBUG_OUT << "Copy ";
-  do_print();
+Interval
+Score_element::preset_extent (Score_element  *s , Axis a)
+{
+  SCM ext = s->get_elt_property ((a == X_AXIS)
+                                ? "extent-X"
+                                : "extent-Y");
   
-  DEBUG_OUT <<  "}\n";
-#endif
+  if (gh_pair_p (ext))
+    {
+      Real l = gh_scm2double (gh_car (ext));
+      Real r = gh_scm2double (gh_cdr (ext));
+      l *= s->paper_l ()->get_var ("staffspace");
+      r *= s->paper_l ()->get_var ("staffspace");
+      return Interval (l, r);
+    }
+  
+  return Interval ();
 }
 
+
+
 Paper_def*
 Score_element::paper_l ()  const
 {
@@ -182,70 +209,52 @@ Score_element::add_processing()
     return;
   status_i_ ++;
 
-#if 0
-    /*
-    UGH. UGH. UGH.
-   */
-  if (get_elt_property ("self-alignment-X") != SCM_UNDEFINED
-      && !dim_cache_[X_AXIS]->off_callback_l_)
-    {
-      add_offset_callback (Side_position_interface::aligned_on_self,X_AXIS);
-    }
-  
-  if (get_elt_property ("self-alignment-Y") != SCM_UNDEFINED
-      && !dim_cache_[X_AXIS]->off_callback_l_)
-      
-    {
-      add_offset_callback (Side_position_interface::aligned_on_self, Y_AXIS);
-    }
-#endif
-  
   do_add_processing();
 }
 
 void
-Score_element::calculate_dependencies (int final, int busy,
-                                      Score_element_method_pointer funcptr)
+Score_element::calculate_dependencies (int final, int busy, SCM funcname)
 {
   assert (status_i_ >=0);
 
   if (status_i_ >= final)
     return;
 
-  assert (status_i_!= busy);
+  if (status_i_== busy)
+    {
+      programming_error ("Element is busy, come back later");
+      return;
+    }
+  
   status_i_= busy;
 
-  for (SCM d=  get_elt_property ("dependencies"); d != SCM_EOL; d = gh_cdr (d))
+  for (SCM d=  get_elt_property ("dependencies"); gh_pair_p (d); d = gh_cdr (d))
     {
       unsmob_element (gh_car (d))
-       ->calculate_dependencies (final, busy, funcptr);
+       ->calculate_dependencies (final, busy, funcname);
     }
 
-  (this->*funcptr)();
+  // ughugh.
+  String s = ly_symbol2string (funcname);
+  SCM proc = get_elt_property (s.ch_C());
+  if (gh_procedure_p (proc))
+    gh_call1 (proc, this->self_scm_);
+  
   status_i_= final;
+
 }
 
-void
-Score_element::output_processing () 
+Molecule
+Score_element::get_molecule ()  const
 {
-  if (to_boolean  (get_elt_property ("transparent")))
-    return;
+  SCM proc = get_elt_property ("molecule-callback");
+  if (gh_procedure_p (proc)) 
+    return create_molecule (gh_apply (proc, gh_list (this->self_scm_, SCM_UNDEFINED)));
 
-  
-  Molecule m (do_brew_molecule ());
-  Offset o (relative_coordinate (0, X_AXIS), relative_coordinate (0, Y_AXIS));
-
-  SCM s = get_elt_property ("extra-offset");
-  if (gh_pair_p (s))
-    {
-      Real il = paper_l ()->get_var ("interline");
-      o[X_AXIS] += il * gh_scm2double (gh_car (s));
-      o[Y_AXIS] += il * gh_scm2double (gh_cdr (s));      
-    }
-  
-  pscore_l_->outputter_l_->output_molecule (m.expr_, o, classname(this));
+  return Molecule ();
 }
 
+
 /*
   
   VIRTUAL STUBS
@@ -256,26 +265,6 @@ Score_element::do_break_processing()
 {
 }
 
-void
-Score_element::after_line_breaking ()
-{
-}
-
-void
-Score_element::do_breakable_col_processing()
-{
-  handle_prebroken_dependencies();
-}
-
-void
-Score_element::before_line_breaking ()
-{
-}
-
-void
-Score_element::do_space_processing ()
-{
-}
 
 void
 Score_element::do_add_processing()
@@ -283,21 +272,23 @@ Score_element::do_add_processing()
 }
 
 
+MAKE_SCHEME_CALLBACK(Score_element,brew_molecule)
 
-Molecule 
-Score_element::do_brew_molecule() const
+/*
+  ugh.
+ */  
+SCM
+Score_element::brew_molecule (SCM smob) 
 {
-  SCM glyph = get_elt_property ("glyph");
+  Score_element * sc = unsmob_element (smob);
+  SCM glyph = sc->get_elt_property ("glyph");
   if (gh_string_p (glyph))
     {
-      return lookup_l ()->afm_find (String (ly_scm2string (glyph)));
-      
+      return sc->lookup_l ()->afm_find (String (ly_scm2string (glyph))).create_scheme ();
     }
   else
     {
-      Molecule m ;
-      m.set_empty (true);
-      return m;
+      return SCM_EOL;
     }
 }
 
@@ -313,7 +304,7 @@ Score_element::add_dependency (Score_element*e)
 {
   if (e)
     {
-      Group_interface gi (this, "dependencies");
+      Pointer_group_interface gi (this, "dependencies");
       gi.add_element (e);
     }
   else
@@ -332,60 +323,68 @@ SCM
 Score_element::handle_broken_smobs (SCM src, SCM criterion)
 {
  again:
-
-  
   Score_element *sc = unsmob_element (src);
   if (sc)
     {
-      if (criterion == SCM_UNDEFINED)
-       return SCM_UNDEFINED;
-      else if (gh_number_p (criterion))
+      if (gh_number_p (criterion))
        {
          Item * i = dynamic_cast<Item*> (sc);
          Direction d = to_dir (criterion);
          if (i && i->break_status_dir () != d)
            {
-             Item *br = i->find_broken_piece (d);
+             Item *br = i->find_prebroken_piece (d);
              return  (br) ? br->self_scm_ : SCM_UNDEFINED;
            }
        }
       else
        {
-         Score_element * ln = unsmob_element ( criterion);
-         Line_of_score * line = dynamic_cast<Line_of_score*> (ln);
-         Score_element * br =0;
-         Line_of_score * dep_line = sc->line_l ();
-         if (dep_line != line)
+         Line_of_score * line
+           = dynamic_cast<Line_of_score*> (unsmob_element (criterion));
+         if (sc->line_l () != line)
            {
-             br = sc->find_broken_piece (line);
-             return  (br) ?  br->self_scm_ : SCM_UNDEFINED;
+             sc = sc->find_broken_piece (line);
+
            }
-         if (!dep_line)
+
+         /* now: !sc || (sc && sc->line_l () == line) */
+         if (!sc)
            return SCM_UNDEFINED;
+
+         /* now: sc && sc->line_l () == line */
+         if (!line
+             || (sc->common_refpoint (line, X_AXIS)
+                 && sc->common_refpoint (line, Y_AXIS)))
+           {
+             return sc->self_scm_;
+           }
+         return SCM_UNDEFINED;
        }
     }
   else if (gh_pair_p (src))
     {
+      SCM oldcar =gh_car (src);
       /*
        UGH! breaks on circular lists.
       */
-      SCM car = handle_broken_smobs (gh_car (src), criterion);
-      SCM cdr = gh_cdr (src);
+      SCM newcar = handle_broken_smobs (oldcar, criterion);
+      SCM oldcdr = gh_cdr (src);
       
-      if (car == SCM_UNDEFINED
-         && (gh_pair_p (cdr) || cdr == SCM_EOL))
+      if (newcar == SCM_UNDEFINED
+         && (gh_pair_p (oldcdr) || oldcdr == SCM_EOL))
        {
          /*
            This is tail-recursion, ie. 
            
            return handle_broken_smobs (cdr, criterion);
 
-           We don't want to rely on the compiler to do this.  */
-         src =  cdr;   
+           We don't want to rely on the compiler to do this.  Without
+           tail-recursion, this easily crashes with a stack overflow.  */
+         src =  oldcdr;
          goto again;
        }
 
-      return gh_cons (car, handle_broken_smobs (cdr, criterion));
+      SCM newcdr = handle_broken_smobs (oldcdr, criterion);
+      return gh_cons (newcar, newcdr);
     }
   else
     return src;
@@ -406,44 +405,61 @@ Score_element::handle_broken_dependencies()
        {
          Score_element * sc = s->broken_into_l_arr_[i];
          Line_of_score * l = sc->line_l ();
-         s->broken_into_l_arr_[i]->element_property_alist_ =
-           handle_broken_smobs (element_property_alist_,
+         sc->mutable_property_alist_ =
+           handle_broken_smobs (mutable_property_alist_,
                                 l ? l->self_scm_ : SCM_UNDEFINED);
        }
     }
 
+
   Line_of_score *line = line_l();
-  element_property_alist_
-    = handle_broken_smobs (element_property_alist_,
-                          line ? line->self_scm_ : SCM_UNDEFINED);
-}
 
+  if (line && common_refpoint (line, X_AXIS) && common_refpoint (line, Y_AXIS))
+    {
+      mutable_property_alist_
+       = handle_broken_smobs (mutable_property_alist_,
+                              line ? line->self_scm_ : SCM_UNDEFINED);
+    }
+  else if (dynamic_cast <Line_of_score*> (this))
+    {
+      mutable_property_alist_ = handle_broken_smobs (mutable_property_alist_,
+                                           SCM_UNDEFINED);
+    }
+  else
+    {
+      /*
+       This element is `invalid'; it has been removed from all
+       dependencies, so let's junk the element itself.
+
+       do not do this for Line_of_score, since that would remove
+       references to the originals of score-elts, which get then GC'd
+       (a bad thing.)
+      */
+      suicide();
+    }
+}
 
 /*
-  TODO: cleanify.
- */
+ Note that we still want references to this element to be
+ rearranged, and not silently thrown away, so we keep pointers
+ like {broken_into_{drul,array}, original}
+*/
 void
-Score_element::handle_prebroken_dependencies()
+Score_element::suicide ()
 {
-  if (Item*i =dynamic_cast<Item*> (this))
+  mutable_property_alist_ = SCM_EOL;
+  immutable_property_alist_ = SCM_EOL;
+  set_extent_callback (0, Y_AXIS);
+  set_extent_callback (0, X_AXIS);
+
+  for (int a= X_AXIS; a <= Y_AXIS; a++)
     {
-      if (original_l_)
-       {
-         element_property_alist_
-           = handle_broken_smobs (original_l_->element_property_alist_,
-                              gh_int2scm (i->break_status_dir ()));
-       }
+      dim_cache_[a].off_callbacks_.clear ();
     }
 }
 
-bool
-Score_element::linked_b() const
-{
-  return used_b_;
-}
-
 void
-Score_element::do_print () const
+Score_element::handle_prebroken_dependencies()
 {
 }
 
@@ -456,60 +472,143 @@ Score_element::find_broken_piece (Line_of_score*) const
 void
 Score_element::translate_axis (Real y, Axis a)
 {
-  dim_cache_[a]->translate (y);
+  if (isinf (y) || isnan (y))
+    programming_error (_(INFINITY_MSG));
+  else
+    {
+      dim_cache_[a].offset_ += y;
+    }
 }  
 
 Real
-Score_element::relative_coordinate (Score_element const*e, Axis a) const
+Score_element::relative_coordinate (Score_element const*refp, Axis a) const
 {
-  return dim_cache_[a]->relative_coordinate (e ? e->dim_cache_[a] : 0);
+  if (refp == this)
+    return 0.0;
+
+  /*
+    We catch PARENT_L_ == nil case with this, but we crash if we did
+    not ask for the absolute coordinate (ie. REFP == nil.)
+    
+   */
+  if (refp == dim_cache_[a].parent_l_)
+    return get_offset (a);
+  else
+    return get_offset (a) + dim_cache_[a].parent_l_->relative_coordinate (refp, a);
 }
 
-Score_element * 
-Score_element::common_refpoint (Score_element const* s, Axis a) const
+Real
+Score_element::get_offset (Axis a) const
 {
-  Dimension_cache *dim = dim_cache_[a]->common_refpoint (s->dim_cache_[a]);
-  return  dim ? dim->element_l () : 0;
+  Score_element *me = (Score_element*) this;
+  while (dim_cache_[a].off_callbacks_.size ())
+    {
+      Offset_callback c = dim_cache_[a].off_callbacks_[0];
+      me->dim_cache_[a].off_callbacks_.del (0);
+      Real r =  (*c) (me,a );
+      if (isinf (r) || isnan (r))
+       {
+         r = 0.0;
+         programming_error (INFINITY_MSG);
+       }
+      me->dim_cache_[a].offset_ +=r;
+    }
+  return dim_cache_[a].offset_;
 }
 
-void
-Score_element::set_empty (Axis a)
+
+Interval
+Score_element::point_dimension_callback (Score_element* , Axis)
 {
-  dim_cache_[a]->extent_callback_l_ =0;
+  return Interval (0,0);
 }
 
 bool
 Score_element::empty_b (Axis a)const
 {
-  return !dim_cache_[a]->extent_callback_l_;
+  return !dim_cache_[a].extent_callback_l_;
 }
 
 Interval
 Score_element::extent (Axis a) const
 {
-  Dimension_cache const * d = dim_cache_[a];
+  Dimension_cache * d = (Dimension_cache *)&dim_cache_[a];
+  if (!d->extent_callback_l_)
+    {
+      d->dim_.set_empty ();
+    }
+  else if (!d->valid_b_)
+    {
+      d->dim_= (*d->extent_callback_l_ ) ((Score_element*)this, a);
+      d->valid_b_ = true;
+    }
+
+  Interval ext = d->dim_;
+  
+  if (empty_b (a)) 
+    return ext;
 
-  return d->get_dim ();
+  SCM extra = get_elt_property (a == X_AXIS
+                               ? "extra-extent-X"
+                               : "extra-extent-Y");
+
+  /*
+    signs ?
+   */
+  Real s = paper_l ()->get_var ("staffspace");
+  if (gh_pair_p (extra))
+    {
+      ext[BIGGER] +=  s * gh_scm2double (gh_cdr (extra));
+      ext[SMALLER] +=  s * gh_scm2double (gh_car (extra));
+    }
+  
+  extra = get_elt_property (a == X_AXIS
+                               ? "minimum-extent-X"
+                               : "minimum-extent-Y");
+  if (gh_pair_p (extra))
+    {
+      ext.unite (Interval (s * gh_scm2double (gh_car (extra)),
+                          s * gh_scm2double (gh_cdr (extra))));
+    }
+  
+  return ext;
 }
 
 
 Score_element*
 Score_element::parent_l (Axis a) const
 {
-  Dimension_cache*d= dim_cache_[a]->parent_l_;
-  return d ? d->elt_l_ : 0;
+  return  dim_cache_[a].parent_l_;
+}
+
+Score_element * 
+Score_element::common_refpoint (Score_element const* s, Axis a) const
+{
+  /*
+    I don't like the quadratic aspect of this code, but I see no other
+    way. The largest chain of parents might be 10 high or so, so
+    it shouldn't be a real issue. */
+  for (Score_element const *c = this; c; c = c->dim_cache_[a].parent_l_)
+    for (Score_element const * d = s; d; d = d->dim_cache_[a].parent_l_)
+      if (d == c)
+       return (Score_element*)d;
+
+  return 0;
 }
 
+
 Score_element *
-Score_element::common_refpoint (Link_array<Score_element> gs, Axis a) const
+Score_element::common_refpoint (SCM elist, Axis a) const
 {
-  Dimension_cache * common = dim_cache_[a];
-  for (int i=0; i < gs.size (); i++)
+  Score_element * common = (Score_element*) this;
+  for (; gh_pair_p (elist); elist = gh_cdr (elist))
     {
-      common = common->common_refpoint (gs[i]->dim_cache_[a]);
+      Score_element * s = unsmob_element (gh_car (elist));
+      if (s)
+       common = common->common_refpoint (s, a);
     }
 
-  return common->element_l ();
+  return common;
 }
 
 char const *
@@ -519,50 +618,61 @@ Score_element::name () const
 }
 
 void
-Score_element::add_offset_callback (Offset_cache_callback cb, Axis a)
+Score_element::add_offset_callback (Offset_callback cb, Axis a)
 {
-  dim_cache_[a]->off_callbacks_.push (cb);
+  dim_cache_[a].off_callbacks_.push (cb);
 }
 
 bool
-Score_element::has_offset_callback_b (Offset_cache_callback cb, Axis a)const
+Score_element::has_extent_callback_b (Extent_callback cb, Axis a)const
 {
-  for (int i= dim_cache_[a]->off_callbacks_.size (); i--;)
+  return cb == dim_cache_[a].extent_callback_l_;
+}
+
+bool
+Score_element::has_offset_callback_b (Offset_callback cb, Axis a)const
+{
+  for (int i= dim_cache_[a].off_callbacks_.size (); i--;)
     {
-      if (dim_cache_[a]->off_callbacks_[i] == cb)
+      if (dim_cache_[a].off_callbacks_[i] == cb)
        return true;
     }
   return false;
 }
 
 void
-Score_element::set_parent (Score_element *g, Axis a)
+Score_element::set_extent_callback (Dim_cache_callback dc, Axis a)
 {
-  dim_cache_[a]->parent_l_ = g ? g->dim_cache_[a]: 0;
+  dim_cache_[a].extent_callback_l_ = dc ;
 }
 
+                                   
 void
-Score_element::fixup_refpoint ()
+Score_element::set_parent (Score_element *g, Axis a)
 {
+  dim_cache_[a].parent_l_ = g;
+}
+
+MAKE_SCHEME_CALLBACK(Score_element,fixup_refpoint);
+SCM
+Score_element::fixup_refpoint (SCM smob)
+{
+  Score_element *me = unsmob_element (smob);
   for (int a = X_AXIS; a < NO_AXES; a ++)
     {
       Axis ax = (Axis)a;
-      Score_element * parent = parent_l (ax);
+      Score_element * parent = me->parent_l (ax);
 
       if (!parent)
        continue;
       
-      if (parent->line_l () != line_l ())
+      if (parent->line_l () != me->line_l () && me->line_l ())
        {
-         Score_element * newparent = parent->find_broken_piece (line_l ());
-         set_parent (newparent, ax);
-         if (!newparent)
-           {
-             programming_error ("Orphaned score-element.");
-           }
+         Score_element * newparent = parent->find_broken_piece (me->line_l ());
+         me->set_parent (newparent, ax);
        }
 
-      if (Item * i  = dynamic_cast<Item*> (this))
+      if (Item * i  = dynamic_cast<Item*> (me))
        {
          Item *parenti = dynamic_cast<Item*> (parent);
 
@@ -571,12 +681,13 @@ Score_element::fixup_refpoint ()
              Direction  my_dir = i->break_status_dir () ;
              if (my_dir!= parenti->break_status_dir())
                {
-                 Item *newparent =  parenti->find_broken_piece (my_dir);
-                 set_parent (newparent, ax);
+                 Item *newparent =  parenti->find_prebroken_piece (my_dir);
+                 me->set_parent (newparent, ax);
                }
            }
        }
     }
+  return smob;
 }
 
 
@@ -585,11 +696,11 @@ Score_element::fixup_refpoint ()
   SMOB funcs
  ****************************************************/
 
-
 #include "ly-smobs.icc"
 
 IMPLEMENT_UNSMOB(Score_element, element);
 IMPLEMENT_SMOBS(Score_element);
+
 SCM
 Score_element::mark_smob (SCM ses)
 {
@@ -599,7 +710,15 @@ Score_element::mark_smob (SCM ses)
       programming_error ("SMOB marking gone awry");
       return SCM_EOL;
     }
-  return s->element_property_alist_;
+  scm_gc_mark ( s->immutable_property_alist_);
+
+  s->do_derived_mark ();
+  if (s->parent_l (Y_AXIS))
+    scm_gc_mark (s->parent_l (Y_AXIS)->self_scm_);
+  if (s->parent_l (X_AXIS))
+    scm_gc_mark (s->parent_l (X_AXIS)->self_scm_);
+
+  return s->mutable_property_alist_;
 }
 
 int
@@ -617,6 +736,11 @@ Score_element::print_smob (SCM s, SCM port, scm_print_state *)
   return 1;
 }
 
+void
+Score_element::do_derived_mark ()
+{
+}
+
 void
 Score_element::do_smobify_self ()
 {
@@ -630,7 +754,7 @@ Score_element::equal_p (SCM a, SCM b)
 
 
 SCM
-Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
+ly_set_elt_property (SCM elt, SCM sym, SCM val)
 {
   Score_element * sc = unsmob_element (elt);
 
@@ -643,7 +767,7 @@ Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
 
   if (sc)
     {
-      sc->element_property_alist_ = scm_assoc_set_x (sc->element_property_alist_, sym, val);
+      sc->set_elt_property (sym, val);
     }
   else
     {
@@ -656,18 +780,13 @@ Score_element::ly_set_elt_property (SCM elt, SCM sym, SCM val)
 
 
 SCM
-Score_element::ly_get_elt_property (SCM elt, SCM sym)
+ly_get_elt_property (SCM elt, SCM sym)
 {
   Score_element * sc = unsmob_element (elt);
   
   if (sc)
     {
-      SCM s = scm_assq(sym, sc->element_property_alist_);
-
-      if (s != SCM_BOOL_F)
-       return gh_cdr (s); 
-      else
-       return SCM_UNDEFINED;
+      return sc->get_elt_property (sym);
     }
   else
     {
@@ -678,11 +797,56 @@ Score_element::ly_get_elt_property (SCM elt, SCM sym)
 }
 
 
+void
+Score_element::discretionary_processing()
+{
+}
+
+
+
+SCM
+spanner_get_bound (SCM slur, SCM dir)
+{
+  return dynamic_cast<Spanner*> (unsmob_element (slur))->get_bound (to_dir (dir))->self_scm_;
+}
+
+
+
+static SCM interfaces_sym;
+
 static void
 init_functions ()
 {
-  scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))Score_element::ly_get_elt_property);
-  scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))Score_element::ly_set_elt_property);
+  interfaces_sym = scm_permanent_object (ly_symbol2scm ("interfaces"));
+  
+  scm_make_gsubr ("ly-get-elt-property", 2, 0, 0, (SCM(*)(...))ly_get_elt_property);
+  scm_make_gsubr ("ly-set-elt-property", 3, 0, 0, (SCM(*)(...))ly_set_elt_property);
+  scm_make_gsubr ("ly-get-spanner-bound", 2 , 0, 0, (SCM(*)(...)) spanner_get_bound);
+}
+
+bool
+Score_element::has_interface (SCM k)
+{
+  if (mutable_property_alist_ == SCM_EOL)
+    return false;
+  
+  SCM ifs = get_elt_property (interfaces_sym);
+
+  return scm_memq (k, ifs) != SCM_BOOL_F;
 }
 
+void
+Score_element::set_interface (SCM k)
+{
+  if (has_interface (k))
+    return ;
+  else
+    {
+      set_elt_property (interfaces_sym,
+                       gh_cons  (k, get_elt_property (interfaces_sym)));
+    }
+}
+
+
 ADD_SCM_INIT_FUNC(scoreelt, init_functions);
+