]> git.donarmstrong.com Git - lilypond.git/blob - lily/chord-name-engraver.cc
patch::: 1.5.11.jcn2
[lilypond.git] / lily / chord-name-engraver.cc
1 /*
2   chord-name-engraver.cc -- implement Chord_name_engraver
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "engraver.hh"
10 #include "chord-name.hh"
11 #include "chord.hh"
12 #include "musical-request.hh"
13 #include "paper-def.hh"
14 #include "font-interface.hh"
15 #include "paper-def.hh"
16 #include "main.hh"
17 #include "dimensions.hh"
18 #include "item.hh"
19 #include "pitch.hh"
20 #include "protected-scm.hh"
21
22 class Chord_name_engraver : public Engraver 
23 {
24 public:
25   Chord_name_engraver ();
26   VIRTUAL_COPY_CONS (Translator);
27
28 protected:
29   virtual void stop_translation_timestep ();
30   virtual void acknowledge_grob (Grob_info i);
31   virtual void create_grobs ();
32   virtual bool try_music (Music *);
33
34 private:
35   void add_note (Note_req *);
36   
37   Item* chord_name_p_;
38
39   Protected_scm chord_;
40   Protected_scm last_chord_;
41 };
42
43 ADD_THIS_TRANSLATOR (Chord_name_engraver);
44
45 Chord_name_engraver::Chord_name_engraver ()
46 {
47   chord_name_p_ = 0;
48   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
49   last_chord_ = chord_;
50 }
51
52 void
53 Chord_name_engraver::add_note (Note_req* n)
54 {
55   SCM pitches = ly_car (chord_);
56   SCM modifiers = ly_cdr (chord_);
57   SCM inversion = modifiers == SCM_EOL ? SCM_EOL : ly_car (modifiers);
58   SCM bass = modifiers == SCM_EOL ? SCM_EOL : ly_cdr (modifiers);
59   
60   if (n->get_mus_property ("inversion") == SCM_BOOL_T)
61     inversion = n->get_mus_property ("pitch");
62   else if (n->get_mus_property ("bass") == SCM_BOOL_T)
63     bass = n->get_mus_property ("pitch");
64   else
65     pitches = scm_sort_list (gh_cons (n->get_mus_property ("pitch"), pitches),
66                              Pitch::less_p_proc);
67   chord_ = gh_cons (pitches, gh_cons (inversion, bass));
68 }
69
70 bool
71 Chord_name_engraver::try_music (Music* m)
72 {
73   if (Note_req* n = dynamic_cast<Note_req*> (m))
74     {
75       add_note (n);
76       return true;
77     }
78   return false;
79 }
80
81 void
82 Chord_name_engraver::acknowledge_grob (Grob_info i)
83 {
84   if (Note_req* n = dynamic_cast<Note_req*> (i.req_l_))
85     add_note (n);
86 }
87
88 void
89 Chord_name_engraver::create_grobs ()
90 {
91   if (!chord_name_p_ && ly_car (chord_) != SCM_EOL)
92     {
93       chord_name_p_ = new Item (get_property ("ChordName"));
94       chord_name_p_->set_grob_property ("chord", chord_);
95       announce_grob (chord_name_p_, 0);
96       SCM s = get_property ("chordChanges");
97       if (to_boolean (s) && ly_car (last_chord_) != SCM_EOL
98                   && gh_equal_p (chord_, last_chord_))
99         chord_name_p_->set_grob_property ("begin-of-line-visible", SCM_BOOL_T);
100     }
101 }
102
103 void
104 Chord_name_engraver::stop_translation_timestep ()
105 {
106   if (chord_name_p_)
107     {
108       typeset_grob (chord_name_p_);
109     }
110   chord_name_p_ = 0;
111
112   if (ly_car (chord_) != SCM_EOL)
113     last_chord_ = chord_;
114   chord_ = gh_cons (SCM_EOL, gh_cons (SCM_EOL, SCM_EOL));
115 }
116