lilypond-1.3.80
[lilypond.git] / lily / local-key-item.cc
blob6ef1dfba8a7f1b83f601742b61b7ba2f213bdb95
1 /*
2 local-key-item.cc -- implement Local_key_item, Musical_pitch
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2000 Han-Wen Nienhuys <hanwen@cs.uu.nl>
7 */
8 #include "local-key-item.hh"
9 #include "molecule.hh"
10 #include "staff-symbol-referencer.hh"
11 #include "lookup.hh"
12 #include "paper-def.hh"
13 #include "musical-request.hh"
14 #include "rhythmic-head.hh"
15 #include "misc.hh"
17 SCM
18 pitch_less (SCM p1, SCM p2)
20 for (int i = 3; i--; p1 = gh_cdr (p1), p2 = gh_cdr (p2))
22 if (scm_less_p (gh_car (p1), gh_car (p2)))
23 return SCM_BOOL_T;
24 if (gh_car (p1) != gh_car (p2))
25 return SCM_BOOL_F;
27 return SCM_BOOL_T;
30 SCM pitch_less_proc;
33 void
34 init_pitch_funcs ()
36 pitch_less_proc = gh_new_procedure2_0 ("pitch-less", &pitch_less);
39 ADD_SCM_INIT_FUNC(pitch,init_pitch_funcs);
42 void
43 Local_key_item::add_pitch (Score_element*me, Musical_pitch p, bool cautionary, bool natural)
45 SCM acs = me->get_elt_property ("accidentals");
46 SCM pitch = p.to_scm ();
47 SCM opts = SCM_EOL;
48 if (cautionary)
49 opts = gh_cons (ly_symbol2scm ("cautionary"), opts);
50 if (natural)
51 opts = gh_cons (ly_symbol2scm ("natural"), opts);
53 pitch = gh_append2 (pitch, opts);
54 acs = scm_merge_x (acs, gh_cons (pitch, SCM_EOL), pitch_less_proc);
56 me->set_elt_property ("accidentals", acs);
59 Molecule
60 Local_key_item::parenthesize (Score_element*me, Molecule m)
62 Molecule open = me->lookup_l ()->afm_find (String ("accidentals-("));
63 Molecule close = me->lookup_l ()->afm_find (String ("accidentals-)"));
64 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
65 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
67 return m;
71 UGH. clean me, revise placement routine (See Ross & Wanske;
72 accidental placement is more complicated than this.
75 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule);
76 SCM
77 Local_key_item::brew_molecule (SCM smob)
79 Score_element* me = unsmob_element (smob);
81 Molecule mol;
83 Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
84 Molecule octave_mol;
85 bool oct_b = false;
86 int lastoct = -100;
88 SCM accs = me->get_elt_property ("accidentals");
89 for (SCM s = accs;
90 gh_pair_p (s); s = gh_cdr (s))
92 Musical_pitch p (gh_car (s));
94 // do one octave
95 if (p.octave_i_ != lastoct)
97 if (oct_b)
99 Real dy =lastoct*7* note_distance;
100 octave_mol.translate_axis (dy, Y_AXIS);
101 mol.add_molecule (octave_mol);
102 octave_mol = Molecule ();
104 oct_b = true;
107 lastoct = p.octave_i_;
109 SCM c0 = me->get_elt_property ("c0-position");
110 Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
111 * note_distance;
113 Molecule acc (me->lookup_l ()->afm_find (String ("accidentals-")
114 + to_str (p.accidental_i_)));
116 if (scm_memq (ly_symbol2scm ("natural"), gh_car (s)) != SCM_BOOL_F)
118 Molecule prefix = me->lookup_l ()->afm_find (String ("accidentals-0"));
119 acc.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
122 if (scm_memq (ly_symbol2scm ("cautionary"), gh_car (s)) != SCM_BOOL_F)
123 acc = parenthesize (me, acc);
125 acc.translate_axis (dy, Y_AXIS);
126 octave_mol.add_at_edge (X_AXIS, RIGHT, acc, 0);
129 if (oct_b)
131 Real dy =lastoct*7*note_distance;
132 octave_mol.translate_axis (dy, Y_AXIS);
133 mol.add_molecule (octave_mol);
134 octave_mol = Molecule ();
137 if (gh_pair_p (accs))
139 Drul_array<SCM> pads;
142 Use a cons?
144 pads[RIGHT] = me->get_elt_property ("right-padding");
145 pads[LEFT] = me->get_elt_property ("left-padding");
148 // unused ?
149 Direction d = LEFT;
150 do {
151 if (!gh_number_p (pads[d]))
152 continue;
154 Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
155 Interval (0,0));
156 Molecule m (me->lookup_l ()->blank (b));
157 mol.add_at_edge (X_AXIS, d, m, 0);
158 } while ( flip (&d)!= LEFT);
161 return mol.create_scheme();
164 bool
165 Local_key_item::has_interface (Score_element*m)
167 return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));
169 void
170 Local_key_item::set_interface (Score_element*m)
172 m->set_interface (ly_symbol2scm ("accidentals-interface"));