]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-selector.cc
186eae8ba906fec1f479f6b7935a8189ef6d2771
[lilypond.git] / lily / grob-selector.cc
1 /*
2   grob-selector.cc -- implement Grob selection.
3
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2004 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "context-selector.hh"
10 #include "context.hh"
11 #include "grob-selector.hh"
12 #include "grob.hh"
13 #include "paper-column.hh"
14 #include "scm-hash.hh"
15 #include "warn.hh"
16
17 Scheme_hash_table *Grob_selector::grobs_ = 0;
18
19 void
20 Grob_selector::register_grob (Context *context, Grob *grob)
21 {
22   if (!grobs_)
23     grobs_ = new Scheme_hash_table ();
24   int count = 0;
25   if (Grob *first = retrieve_grob (identify_grob (context, grob, 0)))
26     {
27       count = robust_scm2int (first->get_property ("max"), 0);
28       count++;
29       SCM s = scm_int2num (count);
30       first->set_property ("max", s);
31       grob->set_property ("count", s);
32     }
33   grob->set_property ("context", context->self_scm ());
34   grobs_->set (identify_grob (context, grob, count), grob->self_scm ());
35 }
36
37 SCM
38 Grob_selector::identify_grob (Context *context, Grob *grob, int count)
39 {
40   return ly_symbol2scm ((ly_symbol2string (Context_selector::identify_context (context))
41                          + ","
42                          + grob->name ()
43 #if 0   // "when" not defined yet?
44                          + ","
45                          + Paper_column::when_mom (((Item*)grob)->get_column ()).to_string (),
46 #endif                   
47                          + ","
48                          + to_string (count)).to_str0 ());
49 }
50
51 SCM
52 Grob_selector::identify_grob (Grob *grob)
53 {
54   return identify_grob (unsmob_context (grob->get_property ("context")),
55                         grob,
56                         robust_scm2int (grob->get_property ("count"), 0));
57 }
58
59 Grob *
60 Grob_selector::retrieve_grob (SCM key)
61 {
62   return unsmob_grob (grobs_->get (key));
63 }
64
65 LY_DEFINE (ly_grob_id, "ly:grob-id",
66            1, 0, 0, (SCM grob_scm),
67            "Return grob id.")
68 {
69   Grob *grob = unsmob_grob (grob_scm);
70   SCM_ASSERT_TYPE (grob, grob_scm, SCM_ARG1, __FUNCTION__, "grob");
71   return Grob_selector::identify_grob (grob);
72 }