Use scalar instead of embedded_scm for context mod overrides.
[lilypond/mpolesky.git] / lily / chord-name-engraver.cc
blob288e29348076fea42f9f7a6526c274f903c168f5
1 /*
2 chord-name-engraver.cc -- implement Chord_name_engraver
4 source file of the GNU LilyPond music typesetter
6 (c) 1998--2009 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "chord-name.hh"
10 #include "context.hh"
11 #include "dimensions.hh"
12 #include "engraver.hh"
13 #include "font-interface.hh"
14 #include "item.hh"
15 #include "output-def.hh"
16 #include "pitch.hh"
17 #include "protected-scm.hh"
18 #include "stream-event.hh"
19 #include "text-interface.hh"
20 #include "warn.hh"
22 #include "translator.icc"
24 class Chord_name_engraver : public Engraver
26 TRANSLATOR_DECLARATIONS (Chord_name_engraver);
27 protected:
28 void stop_translation_timestep ();
29 void process_music ();
30 virtual void finalize ();
31 virtual void derived_mark () const;
32 DECLARE_TRANSLATOR_LISTENER (note);
33 DECLARE_TRANSLATOR_LISTENER (rest);
34 private:
35 Item *chord_name_;
36 vector<Stream_event*> notes_;
38 SCM last_chord_;
39 Stream_event *rest_event_;
42 void
43 Chord_name_engraver::finalize ()
47 void
48 Chord_name_engraver::derived_mark () const
50 scm_gc_mark (last_chord_);
53 Chord_name_engraver::Chord_name_engraver ()
55 chord_name_ = 0;
56 last_chord_ = SCM_EOL;
57 rest_event_ = 0;
60 void
61 Chord_name_engraver::process_music ()
63 SCM markup;
64 SCM bass = SCM_EOL;
65 SCM inversion = SCM_EOL;
66 SCM pitches = SCM_EOL;
68 if (rest_event_)
70 SCM no_chord_markup = get_property ("noChordSymbol");
71 if (!Text_interface::is_markup (no_chord_markup))
72 return;
73 markup = no_chord_markup;
75 else
77 if (!notes_.size ())
78 return;
80 Stream_event *inversion_event = 0;
81 for (vsize i = 0; i < notes_.size (); i++)
83 Stream_event *n = notes_[i];
84 SCM p = n->get_property ("pitch");
85 if (!unsmob_pitch (p))
86 continue;
88 if (n->get_property ("inversion") == SCM_BOOL_T)
90 inversion_event = n;
91 inversion = p;
93 else if (n->get_property ("bass") == SCM_BOOL_T)
94 bass = p;
95 else
96 pitches = scm_cons (p, pitches);
99 if (inversion_event)
101 SCM oct = inversion_event->get_property ("octavation");
102 if (scm_is_number (oct))
104 Pitch *p = unsmob_pitch (inversion_event->get_property ("pitch"));
105 int octavation = scm_to_int (oct);
106 Pitch orig = p->transposed (Pitch (-octavation, 0, 0));
108 pitches = scm_cons (orig.smobbed_copy (), pitches);
110 else
111 programming_error ("inversion does not have original pitch");
114 pitches = scm_sort_list (pitches, Pitch::less_p_proc);
116 SCM name_proc = get_property ("chordNameFunction");
117 markup = scm_call_4 (name_proc, pitches, bass, inversion,
118 context ()->self_scm ());
121 Ugh.
123 SCM chord_as_scm = scm_cons (pitches, scm_cons (bass, inversion));
125 chord_name_ = make_item ("ChordName",
126 rest_event_ ? rest_event_->self_scm () : notes_[0]->self_scm ());
127 chord_name_->set_property ("text", markup);
129 SCM chord_changes = get_property("chordChanges");
130 if (to_boolean (chord_changes) && scm_is_pair (last_chord_)
131 && ly_is_equal (chord_as_scm, last_chord_))
132 chord_name_->set_property ("begin-of-line-visible", SCM_BOOL_T);
134 last_chord_ = chord_as_scm;
137 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, note);
138 void
139 Chord_name_engraver::listen_note (Stream_event *ev)
141 notes_.push_back (ev);
144 IMPLEMENT_TRANSLATOR_LISTENER (Chord_name_engraver, rest);
145 void
146 Chord_name_engraver::listen_rest (Stream_event *ev)
148 ASSIGN_EVENT_ONCE(rest_event_, ev);
151 void
152 Chord_name_engraver::stop_translation_timestep ()
154 chord_name_ = 0;
155 notes_.clear ();
156 rest_event_ = 0;
160 The READs description is not strictly accurate:
161 which properties are read depend on the chord naming function active.
163 ADD_TRANSLATOR (Chord_name_engraver,
164 /* doc */
165 "Catch note and rest events and generate the appropriate chordname.",
167 /* create */
168 "ChordName ",
170 /* read */
171 "chordChanges "
172 "chordNameExceptions "
173 "chordNameFunction "
174 "chordNoteNamer "
175 "chordRootNamer "
176 "chordNameExceptions "
177 "majorSevenSymbol "
178 "noChordSymbol ",
180 /* write */