]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-selector.cc
* lily/context-property.cc (make_item_from_properties):
[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 Protected_scm Grob_selector::tweaks_ = SCM_EOL;
19
20 void
21 Grob_selector::register_grob (Context *context, Grob *grob)
22 {
23   if (!grobs_)
24     grobs_ = new Scheme_hash_table ();
25   int count = 0;
26   Moment m = context->now_mom ();
27   if (Grob *first = retrieve_grob (identify_grob (context, m, grob, 0)))
28     {
29       count = robust_scm2int (first->get_property ("tweak-count"), 0);
30       count++;
31       SCM s = scm_int2num (count);
32       first->set_property ("tweak-count", s);
33       grob->set_property ("tweak-rank", s);
34     }
35   grob->set_property ("context", context->self_scm ());
36   SCM grob_id = identify_grob (context, m, grob, count);
37   store_grob (grob_id, grob);
38   SCM tweak = ly_assoc_get (grob_id, tweaks_, SCM_BOOL_F);
39   if (tweak != SCM_BOOL_F)
40     grob->set_property (ly_symbol2string (scm_car (tweak)).to_str0 (),
41                         scm_cadr (tweak));
42 }
43
44 SCM
45 Grob_selector::identify_grob (Context *context, Moment m, Grob *grob, int count)
46 {
47   return scm_list_4 (Context_selector::identify_context (context),
48                      scm_makfrom0str (m.to_string ().to_str0 ()),
49                      scm_makfrom0str (grob->name ().to_str0 ()),
50                      scm_int2num (count));
51 }
52
53 SCM
54 Grob_selector::identify_grob (Grob *grob)
55 {
56   Moment m;
57   return identify_grob (unsmob_context (grob->get_property ("context")),
58                         Paper_column::when_mom (((Item*) grob)->get_column ()),
59                         grob,
60                         robust_scm2int (grob->get_property ("tweak-rank"), 0));
61 }
62
63 void
64 Grob_selector::store_grob (SCM grob_id, Grob *grob)
65 {
66   grobs_->set (ly_to_symbol (grob_id), grob->self_scm ());
67 }
68
69 Grob *
70 Grob_selector::retrieve_grob (SCM grob_id)
71 {
72   return unsmob_grob (grobs_->get (ly_to_symbol (grob_id)));
73 }
74
75 void
76 Grob_selector::set_tweaks (SCM tweaks)
77 {
78   tweaks_ = tweaks;
79 }
80
81 LY_DEFINE (ly_grob_id, "ly:grob-id",
82            1, 0, 0, (SCM grob_scm),
83            "Return grob id.")
84 {
85   Grob *grob = unsmob_grob (grob_scm);
86   SCM_ASSERT_TYPE (grob, grob_scm, SCM_ARG1, __FUNCTION__, "grob");
87   return Grob_selector::identify_grob (grob);
88 }