]> git.donarmstrong.com Git - lilypond.git/blob - lily/engraver.cc
(internal_make_grob): use variables.
[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   (void) file;
114   (void) fun;
115   (void) line;
116   
117   SCM props = updated_grob_properties (context (), symbol);
118
119   Object_key const *key = context ()->get_grob_key (name);
120   Grob *grob = 0;
121
122   SCM handle = scm_sloppy_assq (ly_symbol2scm ("meta"), props);
123   SCM klass = scm_cdr (scm_sloppy_assq (ly_symbol2scm ("class"), scm_cdr (handle)));
124
125   if (klass == ly_symbol2scm ("Item"))
126     grob = new Item (props, key);
127   else if (klass == ly_symbol2scm ("Spanner"))
128     grob = new Spanner (props, key);
129   else if (klass == ly_symbol2scm ("Paper_column"))
130     grob = new Paper_column (props, key);
131
132   assert (grob);
133   announce_grob (grob, cause);
134
135 #ifndef NDEBUG
136   if (ly_is_procedure (creation_callback))
137     scm_apply_0 (creation_callback,
138                  scm_list_n (grob->self_scm (), scm_makfrom0str (file),
139                              scm_from_int (line), scm_makfrom0str (fun), SCM_UNDEFINED));
140 #endif
141
142   return grob;
143 }
144
145 Item *
146 Engraver::internal_make_item (SCM x, SCM cause,
147                               char const *name,
148                               char const *file, int line, char const *fun)
149 {
150   Item *it = dynamic_cast<Item *> (internal_make_grob (x, cause, name, file, line, fun));
151   assert (it);
152   return it;
153 }
154
155 Paper_column *
156 Engraver::internal_make_column (SCM x, char const *name,
157                                 char const *file, int line, char const *fun)
158 {
159   return dynamic_cast<Paper_column *> (internal_make_grob (x, SCM_EOL, name, file, line, fun));
160 }
161
162 Spanner *
163 Engraver::internal_make_spanner (SCM x, SCM cause, char const *name, char const *file, int line, char const *fun)
164 {
165   Spanner *sp = dynamic_cast<Spanner *> (internal_make_grob (x, cause, name, file, line, fun));
166   assert (sp);
167   return sp;
168 }
169
170 #include "translator.icc"
171
172 ADD_TRANSLATOR (Engraver,
173                 "Base class for engravers. Does nothing, so it is not used.",
174                 "",
175                 "",
176                 "");
177