]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver.cc
1b83b07a85ce638cab4ff0c448318779c01dbde5
[lilypond.git] / lily / engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver.hh"
21
22 #include "context.hh"
23 #include "international.hh"
24 #include "music.hh"
25 #include "paper-column.hh"
26 #include "score-engraver.hh"
27 #include "spanner.hh"
28 #include "stream-event.hh"
29 #include "warn.hh"
30
31 Engraver_group *
32 Engraver::get_daddy_engraver () const
33 {
34   return dynamic_cast<Engraver_group *> (get_daddy_translator ());
35 }
36
37 void
38 Engraver::announce_grob (Grob_info inf)
39 {
40   get_daddy_engraver ()->announce_grob (inf);
41 }
42
43 void
44 Engraver::announce_end_grob (Grob_info inf)
45 {
46   get_daddy_engraver ()->announce_grob (inf);
47 }
48
49 /*
50   CAUSE is the object (typically a Stream_event object)  that
51   was the reason for making E.
52 */
53 void
54 Engraver::announce_grob (Grob *e, SCM cause)
55 {
56   /* TODO: Remove Music code when it's no longer needed */
57   if (Music *m = unsmob_music (cause))
58     {
59       cause = m->to_event ()->unprotect ();
60     }
61   if (unsmob_stream_event (cause) || unsmob_grob (cause))
62     e->set_property ("cause", cause);
63
64   Grob_info i (this, e);
65
66   Engraver_group *g = get_daddy_engraver ();
67   if (g)
68     g->announce_grob (i);
69 }
70
71
72 /*
73   CAUSE is the object (typically a Music object)  that
74   was the reason for making E.
75 */
76 void
77 Engraver::announce_end_grob (Grob *e, SCM cause)
78 {
79   /* TODO: Remove Music code when it's no longer needed */
80   if (Music *m = unsmob_music (cause))
81     {
82       cause = m->to_event ()->unprotect ();
83     }
84   if (e->get_property ("cause") == SCM_EOL
85       && (unsmob_stream_event (cause) || unsmob_grob (cause)))
86     e->set_property ("cause", cause);
87
88   Grob_info i (this, e);
89
90   i.start_end_ = STOP;
91   Engraver_group *g = get_daddy_engraver ();
92   if (g)
93     g->announce_grob (i);
94 }
95
96
97 Engraver::Engraver ()
98 {
99 }
100
101 #ifndef NDEBUG
102 static SCM creation_callback = SCM_EOL;
103 LY_DEFINE (ly_set_grob_creation_callback, "ly:set-grob-creation-callback",
104            1, 0, 0, (SCM cb),
105            "Specify a procedure that will be called every time a new grob"
106            " is created.  The callback will receive as arguments the grob"
107            " that was created, the name of the C++ source file that caused"
108            " the grob to be created, and the corresponding line number in"
109            " the C++ source file.")
110 {
111   LY_ASSERT_TYPE (ly_is_procedure, cb, 1);
112
113   creation_callback = cb;
114
115   return SCM_UNSPECIFIED;
116 }
117 #endif
118
119 Grob *
120 Engraver::internal_make_grob (SCM symbol,
121                               SCM cause,
122                               char const * /* name */,
123                               char const *file,
124                               int line,
125                               char const *fun)
126 {
127 #ifdef NDEBUG
128   (void)file;
129   (void)line;
130   (void)fun;
131 #endif
132
133   SCM props = updated_grob_properties (context (), symbol);
134
135   Grob *grob = 0;
136
137   SCM handle = scm_sloppy_assq (ly_symbol2scm ("meta"), props);
138   SCM klass = scm_cdr (scm_sloppy_assq (ly_symbol2scm ("class"), scm_cdr (handle)));
139
140   if (klass == ly_symbol2scm ("Item"))
141     grob = new Item (props);
142   else if (klass == ly_symbol2scm ("Spanner"))
143     grob = new Spanner (props);
144   else if (klass == ly_symbol2scm ("Paper_column"))
145     grob = new Paper_column (props);
146
147   assert (grob);
148   announce_grob (grob, cause);
149
150 #ifndef NDEBUG
151   if (ly_is_procedure (creation_callback))
152     scm_apply_0 (creation_callback,
153                  scm_list_n (grob->self_scm (), scm_from_locale_string (file),
154                              scm_from_int (line), scm_from_locale_string (fun), SCM_UNDEFINED));
155 #endif
156
157   return grob;
158 }
159
160 Item *
161 Engraver::internal_make_item (SCM x, SCM cause,
162                               char const *name,
163                               char const *file, int line, char const *fun)
164 {
165   Item *it = dynamic_cast<Item *> (internal_make_grob (x, cause, name, file, line, fun));
166   assert (it);
167   return it;
168 }
169
170 Paper_column *
171 Engraver::internal_make_column (SCM x, char const *name,
172                                 char const *file, int line, char const *fun)
173 {
174   return dynamic_cast<Paper_column *> (internal_make_grob (x, SCM_EOL, name, file, line, fun));
175 }
176
177 Spanner *
178 Engraver::internal_make_spanner (SCM x, SCM cause, char const *name, char const *file, int line, char const *fun)
179 {
180   Spanner *sp = dynamic_cast<Spanner *> (internal_make_grob (x, cause, name, file, line, fun));
181   assert (sp);
182   return sp;
183 }
184
185 #include "translator.icc"
186
187 ADD_TRANSLATOR (Engraver,
188                 /* doc */
189                 "Base class for engravers.  Does nothing, so it is not used.",
190
191                 /* create */
192                 "",
193
194                 /* read */
195                 "",
196
197                 /* write */
198                 ""
199                 );
200