(c) 1999 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
+#include <math.h>
+
#include "staff-symbol-referencer.hh"
#include "staff-symbol.hh"
#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)
+ elt_l_->set_elt_property ("staff-position", gh_double2scm (0.0));
+ elt_l_->dim_cache_[Y_AXIS]->off_callbacks_.push (callback);
+}
+
+
+bool
+Staff_symbol_referencer_interface::has_interface_b ()
+{
+ return unsmob_element (elt_l_->get_elt_property ("staff-symbol"))
+ || gh_number_p (elt_l_->get_elt_property ("staff-position"));
+}
+
+
+int
+Staff_symbol_referencer_interface::line_count () const
+{
+ Staff_symbol *st = staff_symbol_l ();
+ return st ? st->line_count () : 0;
+}
+
+Staff_symbol*
+Staff_symbol_referencer_interface::staff_symbol_l () const
+{
+ SCM st = elt_l_->get_elt_property ("staff-symbol");
+ return dynamic_cast<Staff_symbol* > (unsmob_element(st));
+}
+
+Real
+Staff_symbol_referencer_interface::staff_space () const
+{
+ 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;
+}
+
+
+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)
{
- staff_sym_l_ = dynamic_cast<Staff_symbol*> (n);
+ 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;
}
-int
-Staff_symbol_referencer::lines_i () const
+
+
+/*
+ should use offset callback!
+ */
+Real
+Staff_symbol_referencer_interface::callback (Dimension_cache const * c)
{
- return (staff_sym_l_) ? staff_sym_l_->no_lines_i_ : 5;
+ 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;
}
+/*
+ Huh? -- jcn
+ */
void
-Staff_symbol_referencer::set_staff_symbol (Staff_symbol*s)
+Staff_symbol_referencer_interface::set_position (Real p)
{
- staff_sym_l_ =s;
- add_dependency (s);
+ Staff_symbol * st = staff_symbol_l ();
+ if (st && elt_l_->common_refpoint(st, Y_AXIS))
+ {
+ Real oldpos = position_f ();
+ // Aargh!
+ elt_l_->set_elt_property ("staff-position", gh_double2scm (p - oldpos));
+ }
+ else
+ {
+ elt_l_->set_elt_property ("staff-position",
+ gh_double2scm (p));
+
+ }
+
+ Array<Offset_cache_callback> &callbacks (elt_l_->dim_cache_[Y_AXIS]->off_callbacks_);
+ for (int i=0; i < callbacks.size ();i++)
+ if (callbacks[i] == callback)
+ return ;
+
+ callbacks.push (callback);
}
-Staff_symbol*
-Staff_symbol_referencer::staff_symbol_l () const
+Staff_symbol_referencer_interface
+staff_symbol_referencer (Score_element const*e)
{
- return staff_sym_l_;
+ return e; // gee, I'm so smart!
}
-Real
-Staff_symbol_referencer::staff_line_leading_f () const
+int
+compare_position (Score_element *const &a, Score_element * const &b)
{
- return (staff_sym_l_) ? staff_sym_l_->staff_line_leading_f_ : paper_l ()->get_realvar (interline_scm_sym);
+ Staff_symbol_referencer_interface s1(a);
+ Staff_symbol_referencer_interface s2(b);
+
+ return sign(s1.position_f () - s2.position_f ());
}