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