]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver.cc
2453188fff76d573dc0584b024b039def46dacd8
[lilypond.git] / lily / engraver.cc
1 /*
2   engraver.cc -- implement Engraver
3
4   Sourcefile of GNU LilyPond music type setter
5
6   (c) 1997--2006 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "engraver.hh"
10
11 #include "context.hh"
12 #include "international.hh"
13 #include "item.hh"
14 #include "lilypond-key.hh"
15 #include "music.hh"
16 #include "paper-column.hh"
17 #include "score-engraver.hh"
18 #include "spanner.hh"
19 #include "stream-event.hh"
20 #include "warn.hh"
21
22 Engraver_group *
23 Engraver::get_daddy_engraver () const
24 {
25   return dynamic_cast<Engraver_group *> (get_daddy_translator ());
26 }
27
28 void
29 Engraver::announce_grob (Grob_info inf)
30 {
31   get_daddy_engraver ()->announce_grob (inf);
32 }
33
34 void
35 Engraver::announce_end_grob (Grob_info inf)
36 {
37   get_daddy_engraver ()->announce_grob (inf);
38 }
39
40 /*
41   CAUSE is the object (typically a Stream_event object)  that
42   was the reason for making E.
43 */
44 void
45 Engraver::announce_grob (Grob *e, SCM cause)
46 {
47   /* TODO: Remove Music code when it's no longer needed */
48   if (Music *m = unsmob_music (cause))
49     {
50       cause = m->to_event ()->unprotect ();
51     }
52   if (unsmob_stream_event (cause) || unsmob_grob (cause))
53     e->set_property ("cause", cause);
54
55   Grob_info i (this, e);
56
57   Engraver_group *g = get_daddy_engraver ();
58   if (g)
59     g->announce_grob (i);
60 }
61
62
63 /*
64   CAUSE is the object (typically a Music object)  that
65   was the reason for making E.
66 */
67 void
68 Engraver::announce_end_grob (Grob *e, SCM cause)
69 {
70   /* TODO: Remove Music code when it's no longer needed */
71   if (Music *m = unsmob_music (cause))
72     {
73       cause = m->to_event ()->unprotect ();
74     }
75   if (unsmob_stream_event (cause) || unsmob_grob (cause))
76     e->set_property ("cause", cause);
77
78   Grob_info i (this, e);
79
80   i.start_end_ = STOP;
81   Engraver_group *g = get_daddy_engraver ();
82   if (g)
83     g->announce_grob (i);
84 }
85
86
87 Engraver::Engraver ()
88 {
89 }
90
91 #ifndef NDEBUG
92 static SCM creation_callback = SCM_EOL;
93 LY_DEFINE (ly_set_grob_creation_callback, "ly:set-grob-creation-callback",
94            1, 0, 0, (SCM cb),
95            "Specify a procedure that will be called every time a new grob "
96            "is created. The callback will receive as arguments the grob "
97            "that was created, the name of the C++ source file that caused "
98            "the grob to be created and the corresponding line number in the "
99            "C++ source file.")
100 {
101   if (!ly_is_procedure (cb))
102     warning (_ ("not setting creation callback: not a procedure"));
103   else
104     creation_callback = cb;
105
106   return SCM_EOL;
107 }
108 #endif
109
110 Grob *
111 Engraver::internal_make_grob (SCM symbol, SCM cause, char const *name, char const *file, int line, char const *fun)
112 {
113   SCM props = updated_grob_properties (context (), symbol);
114
115   Object_key const *key = context ()->get_grob_key (name);
116   Grob *grob = 0;
117
118   SCM handle = scm_sloppy_assq (ly_symbol2scm ("meta"), props);
119   SCM klass = scm_cdr (scm_sloppy_assq (ly_symbol2scm ("class"), scm_cdr (handle)));
120
121   if (klass == ly_symbol2scm ("Item"))
122     grob = new Item (props, key);
123   else if (klass == ly_symbol2scm ("Spanner"))
124     grob = new Spanner (props, key);
125   else if (klass == ly_symbol2scm ("Paper_column"))
126     grob = new Paper_column (props, key);
127
128   assert (grob);
129   announce_grob (grob, cause);
130
131 #ifndef NDEBUG
132   if (ly_is_procedure (creation_callback))
133     scm_apply_0 (creation_callback,
134                  scm_list_n (grob->self_scm (), scm_makfrom0str (file),
135                              scm_from_int (line), scm_makfrom0str (fun), SCM_UNDEFINED));
136 #endif
137
138   return grob;
139 }
140
141 Item *
142 Engraver::internal_make_item (SCM x, SCM cause, char const *name, char const *file, int line, char const *fun)
143 {
144   Item *it = dynamic_cast<Item *> (internal_make_grob (x, cause, name, file, line, fun));
145   assert (it);
146   return it;
147 }
148
149 Paper_column *
150 Engraver::internal_make_column (SCM x, char const *name, char const *file, int line, char const *fun)
151 {
152   return dynamic_cast<Paper_column *> (internal_make_grob (x, SCM_EOL, name, file, line, fun));
153 }
154
155 Spanner *
156 Engraver::internal_make_spanner (SCM x, SCM cause, char const *name, char const *file, int line, char const *fun)
157 {
158   Spanner *sp = dynamic_cast<Spanner *> (internal_make_grob (x, cause, name, file, line, fun));
159   assert (sp);
160   return sp;
161 }
162
163 #include "translator.icc"
164
165 ADD_TRANSLATOR (Engraver,
166                 "Base class for engravers. Does nothing, so it is not used.",
167                 "",
168                 "",
169                 "",
170                 "");
171