]> git.donarmstrong.com Git - lilypond.git/blobdiff - lily/staff-symbol-referencer.cc
release: 1.3.50
[lilypond.git] / lily / staff-symbol-referencer.cc
index aad498bce3b24fb009e90d10c773adf3e5814764..8126bbbcf172cb47855cb1f623cce71a513b35e9 100644 (file)
@@ -3,7 +3,7 @@
   
   source file of the GNU LilyPond music typesetter
   
-  (c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
+  (c) 1999--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
  */
 
 #include <math.h>
 #include "paper-def.hh"
 #include "dimension-cache.hh"
 
-Staff_symbol_referencer::Staff_symbol_referencer ()
+Staff_symbol_referencer_interface::Staff_symbol_referencer_interface (Score_element const *sc)
 {
-  staff_sym_l_ =0;
+  elt_l_ = (Score_element*)sc;
 }
 
 void
-Staff_symbol_referencer::do_substitute_element_pointer (Score_element *o, Score_element*n)
+Staff_symbol_referencer_interface::set_interface ()
 {
-  if (staff_sym_l_ == o)
-    {
-      staff_sym_l_ = dynamic_cast<Staff_symbol*> (n);
-    }
+  elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
+  elt_l_->add_offset_callback (callback, Y_AXIS);
+  
 }
 
-int
-Staff_symbol_referencer::lines_i () const
+
+bool
+Staff_symbol_referencer_interface::has_interface_b ()
 {
-  return (staff_sym_l_) ?  staff_sym_l_->no_lines_i_ : 5;
+  return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
+    || gh_number_p (elt_l_->get_elt_property ("staff-position"));
 }
 
-void
-Staff_symbol_referencer::set_staff_symbol (Staff_symbol*s)
+
+int
+Staff_symbol_referencer_interface::line_count () const
 {
-  staff_sym_l_ =s;
-  add_dependency (s);
+  Staff_symbol *st = staff_symbol_l ();
+  return st  ?  st->line_count () : 0;
 }
 
 Staff_symbol*
-Staff_symbol_referencer::staff_symbol_l () const
+Staff_symbol_referencer_interface::staff_symbol_l () const
 {
-  return staff_sym_l_;
+  SCM st = elt_l_->get_elt_property ("staff-symbol");
+  return dynamic_cast<Staff_symbol* > (unsmob_element(st));
 }
 
 Real
-Staff_symbol_referencer::staff_line_leading_f () const
+Staff_symbol_referencer_interface::staff_space () const
 {
-  return (staff_sym_l_) ? staff_sym_l_->staff_line_leading_f_ : paper_l ()->get_realvar (interline_scm_sym);
+  Staff_symbol * st = staff_symbol_l ();
+  if (st)
+    return st->staff_space ();
+  else if (elt_l_->pscore_l_ && elt_l_->paper_l ())
+    return elt_l_->paper_l ()->get_var ("interline");
+  return 0.0;
 }
 
 
-int
-Staff_symbol_referencer::position_i () const
+Real
+Staff_symbol_referencer_interface::position_f () const
+{
+  Real p =0.0;
+  Staff_symbol * st = staff_symbol_l ();
+  Score_element * c = st ? elt_l_->common_refpoint (st, Y_AXIS) : 0;
+  if (st && c)
+    {
+      Real y = elt_l_->relative_coordinate (c, Y_AXIS)
+       - st->relative_coordinate (c, Y_AXIS);
+
+      p += 2.0 * y / st->staff_space ();
+    }
+  else
+    {
+      SCM pos = elt_l_->get_elt_property ("staff-position");
+      if (gh_number_p (pos))
+       return gh_scm2double (pos);
+    }
+  
+  return  p;
+}
+
+
+
+/*
+  should use offset callback!
+ */
+Real
+Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
+{
+  Score_element * sc = dynamic_cast<Score_element*> (c->element_l ());
+
+  
+  SCM pos = sc->get_elt_property ("staff-position");
+  Real off =0.0;
+  if (gh_number_p (pos))
+    {
+      Real space = staff_symbol_referencer (sc).staff_space ();
+      off = gh_scm2double (pos) * space/2.0;
+    }
+  sc->set_elt_property ("staff-position", gh_double2scm (0.0));
+
+  return off;
+}
+
+/*
+  
+  This sets the position relative to the center of the staff symbol.
+
+  The function is hairy, because it can be callled in two situations:
+
+  1.  There is no staff yet; we must set staff-position
+
+  2.  There is a staff, and perhaps someone even applied a
+  translate_axis (). Then we must compensate for the translation
+  
+  In either case, we set a callback to be sure that our new position
+  will be extracted from staff-position
+  
+ */
+void
+Staff_symbol_referencer_interface::set_position (Real p)
 {
-  if (!staff_sym_l_ )
-    return 0;
+  Staff_symbol * st = staff_symbol_l ();
+  if (st && elt_l_->common_refpoint(st, Y_AXIS))
+    {
+      Real oldpos = position_f ();
+      elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
+    }
+  else
+    {
+      elt_l_->set_elt_property ("staff-position",
+                               gh_double2scm (p));
 
-  Graphical_element * c = common_refpoint (staff_sym_l_, Y_AXIS);
-  Real y = relative_coordinate (c, Y_AXIS) - staff_sym_l_->relative_coordinate (c, Y_AXIS);
+    }
+
+  if (elt_l_->has_offset_callback_b (callback, Y_AXIS))
+    return ; 
+
+  elt_l_->add_offset_callback (callback, Y_AXIS);
+}
+
+Staff_symbol_referencer_interface
+staff_symbol_referencer (Score_element const*e)
+{
+  return e;                    // gee, I'm so smart!
+}
+
+int
+compare_position (Score_element *const  &a, Score_element * const &b)
+{
+  Staff_symbol_referencer_interface s1(a);
+  Staff_symbol_referencer_interface s2(b);      
 
-  return int (2.0 * y / staff_line_leading_f ());
+  return sign(s1.position_f () - s2.position_f ());
 }