lilypond-1.3.72
[lilypond.git] / lily / local-key-item.cc
blob04445201ede731a355c504e6374e295869f033d1
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 void
18 Local_key_item::add_pitch (Musical_pitch p, bool cautionary, bool natural)
20 for (int i=0; i< accidental_arr_.size(); i++)
21 if (!Musical_pitch::compare (p, accidental_arr_[i].pitch_))
22 return;
23 /* maybe natural (and cautionary) should be modif. nonetheless? */
25 Local_key_cautionary_tuple t;
26 t.pitch_ = p;
27 t.cautionary_b_ = cautionary;
28 t.natural_b_ = natural;
29 accidental_arr_.push (t);
32 MAKE_SCHEME_CALLBACK(Local_key_item,before_line_breaking);
34 SCM
35 Local_key_item::before_line_breaking (SCM smob)
37 Local_key_item* me = dynamic_cast<Local_key_item*>(unsmob_element (smob));
40 me->accidental_arr_.sort (Local_key_cautionary_tuple::compare);
41 return SCM_UNDEFINED;
44 Molecule
45 Local_key_item::accidental (Score_element*me, int j, bool cautionary, bool natural)
47 Molecule m (me->lookup_l ()->afm_find (String ("accidentals-") + to_str (j)));
48 if (natural)
50 Molecule prefix = me->lookup_l ()->afm_find (String ("accidentals-0"));
51 m.add_at_edge(X_AXIS, LEFT, Molecule(prefix), 0);
53 if (cautionary)
55 Molecule open = me->lookup_l ()->afm_find (String ("accidentals-("));
56 Molecule close = me->lookup_l ()->afm_find (String ("accidentals-)"));
57 m.add_at_edge(X_AXIS, LEFT, Molecule(open), 0);
58 m.add_at_edge(X_AXIS, RIGHT, Molecule(close), 0);
61 return m;
65 UGH. clean me up
68 MAKE_SCHEME_CALLBACK(Local_key_item,brew_molecule);
69 SCM
70 Local_key_item::brew_molecule (SCM smob)
72 Local_key_item* lki = dynamic_cast<Local_key_item*>(unsmob_element (smob));
73 Score_element* me = lki;
75 Molecule mol;
77 Real note_distance = Staff_symbol_referencer::staff_space (me)/2;
78 Molecule octave_mol;
79 bool oct_b = false;
80 int lastoct = -100;
82 for (int i = 0; i < lki->accidental_arr_.size(); i++)
84 Musical_pitch p (lki->accidental_arr_[i].pitch_);
85 // do one octave
86 if (p.octave_i_ != lastoct)
88 if (oct_b)
90 Real dy =lastoct*7* note_distance;
91 octave_mol.translate_axis (dy, Y_AXIS);
92 mol.add_molecule (octave_mol);
93 octave_mol = Molecule ();
95 oct_b = true;
98 lastoct = p.octave_i_;
100 SCM c0 = me->get_elt_property ("c0-position");
101 Real dy = (gh_number_p (c0) ? gh_scm2int (c0) : 0 + p.notename_i_)
102 * note_distance;
104 Molecule m (accidental (me,p.accidental_i_,
105 lki->accidental_arr_[i].cautionary_b_,
106 lki->accidental_arr_[i].natural_b_));
108 m.translate_axis (dy, Y_AXIS);
109 octave_mol.add_at_edge (X_AXIS, RIGHT, m, 0);
112 if (oct_b)
114 Real dy =lastoct*7*note_distance;
115 octave_mol.translate_axis (dy, Y_AXIS);
116 mol.add_molecule (octave_mol);
117 octave_mol = Molecule ();
120 if (lki->accidental_arr_.size())
122 Drul_array<SCM> pads;
125 Use a cons?
127 pads[RIGHT] = me->get_elt_property ("right-padding");
128 pads[LEFT] = me->get_elt_property ("left-padding");
131 // unused ?
132 Direction d = LEFT;
133 do {
134 if (!gh_number_p (pads[d]))
135 continue;
137 Box b(Interval (0, gh_scm2double (pads[d]) * note_distance),
138 Interval (0,0));
139 Molecule m (me->lookup_l ()->blank (b));
140 mol.add_at_edge (X_AXIS, d, m, 0);
141 } while ( flip (&d)!= LEFT);
144 return mol.create_scheme();
147 Local_key_item::Local_key_item (SCM s)
148 : Item (s)
152 bool
153 Local_key_item::has_interface (Score_element*m)
155 return m && m->has_interface (ly_symbol2scm ("accidentals-interface"));