]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
6e36de07d4aa9c901f430a892cc60c5a641d172a
[lilypond.git] / lily / grob-scheme.cc
1 #include "grob.hh"
2 #include "warn.hh"
3 #include "spanner.hh"
4 #include "item.hh"
5 #include "paper-def.hh"
6 #include "system.hh"
7
8
9 LY_DEFINE (ly_set_grob_property,"ly:set-grob-property!", 3, 0, 0,
10   (SCM grob, SCM sym, SCM val),
11   "Set @var{sym} in grob @var{grob} to value @var{val}")
12 {
13   Grob * sc = unsmob_grob (grob);
14   SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
15   SCM_ASSERT_TYPE (gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");  
16
17   if (!type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
18     error ("typecheck failed");
19       
20   sc->internal_set_grob_property (sym, val);
21   return SCM_UNSPECIFIED;
22 }
23
24 LY_DEFINE (ly_get_grob_property,
25           "ly:get-grob-property", 2, 0, 0, (SCM grob, SCM sym),
26           "Get the value of a value in grob @var{g} of property @var{sym}. It
27 will return @code{'()} (end-of-list) if @var{g} doesn't have @var{sym} set.
28
29 Grob properties are stored as GUILE association lists, with symbols as
30 keys. All lookup functions identify undefined properties with
31 end-of-list (i.e. @code{'()} in Scheme or @code{SCM_EOL} in C)
32
33 Properties are stored in two ways:
34 @itemize @bullet
35 @item mutable properties.
36 Grob properties that change from object to object. The storage of
37 these are private to a grob. For example pointers to other grobs are
38 always stored in the mutable properties.
39
40 @item immutable properties.
41 Grob properties that are shared across different grobs of the same
42 type. The storage is shared, and hence it is read-only. Typically, this
43 is used to store function callbacks, and default settings. They are
44 initially read from @file{scm/grob-description.scm}.
45 @end itemize
46
47 ")
48 {
49   Grob * sc = unsmob_grob (grob);
50   SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
51   SCM_ASSERT_TYPE (gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");  
52
53   return sc->internal_get_grob_property (sym);
54 }
55
56 LY_DEFINE (spanner_get_bound, "ly:get-spanner-bound", 2 , 0, 0,
57            (SCM slur, SCM dir),
58            "Get one of the bounds of @var{spanner}. @var{dir} is @code{-1} "
59            "for left, and @code{1} for right.")
60 {
61   Spanner * sl = dynamic_cast<Spanner*> (unsmob_grob (slur));
62   SCM_ASSERT_TYPE (sl, slur, SCM_ARG1, __FUNCTION__, "spanner grob");
63   SCM_ASSERT_TYPE (ly_dir_p (dir), slur, SCM_ARG2, __FUNCTION__, "dir");
64   return sl->get_bound (to_dir (dir))->self_scm ();
65 }
66
67 /* TODO: make difference between scaled and unscalead variable in
68    calling (i.e different funcs.) */
69 LY_DEFINE (ly_get_paper_var,"ly:get-paper-variable", 2, 0, 0,
70   (SCM grob, SCM sym),
71   "Get a variable from the \\paper block.")
72 {
73   Grob * sc = unsmob_grob (grob);
74   SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
75   SCM_ASSERT_TYPE (gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");  
76
77   return sc->get_paper () ->lookup_variable (sym);
78 }
79
80
81
82 LY_DEFINE (ly_get_extent, "ly:get-extent", 3, 0, 0,
83            (SCM grob, SCM refp, SCM axis),
84            "Get the extent in @var{axis} direction of @var{grob} relative to "
85            "the grob @var{refp}")
86 {
87   Grob * sc = unsmob_grob (grob);
88   Grob * ref = unsmob_grob (refp);
89   SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
90   SCM_ASSERT_TYPE (ref, refp, SCM_ARG2, __FUNCTION__, "grob");
91   
92   SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
93
94   return ly_interval2scm ( sc->extent (ref, Axis (gh_scm2int (axis))));
95 }
96
97 LY_DEFINE (ly_get_parent, "ly:get-parent", 2, 0, 0, (SCM grob, SCM axis),
98            "Get the parent of @var{grob}.  @var{axis} is 0 for the X-axis, "
99            "1 for the Y-axis.")
100 {
101   Grob * sc = unsmob_grob (grob);
102   SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
103   SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
104
105   Grob * par = sc->get_parent (Axis (gh_scm2int (axis)));
106   return par ? par->self_scm () : SCM_EOL;
107 }
108
109 /* ly prefix? */
110 LY_DEFINE (get_system,
111            "ly:get-system",
112            1, 0, 0, (SCM grob),
113            "Return the System Grob of @var{grob}.")
114 {
115   Grob *me = unsmob_grob (grob);
116   SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob");
117   
118   if (System *g = me->get_system ())
119     return g->self_scm ();
120     
121   return SCM_EOL;
122 }
123
124 /* ly prefix? */
125 LY_DEFINE (get_original,
126            "ly:get-original",
127            1, 0, 0, (SCM grob),
128            "Return the original Grob of @var{grob}")
129 {
130   Grob *me = unsmob_grob (grob);
131   SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob");
132   return me->original_ ? me->original_->self_scm () : me->self_scm ();
133 }
134
135
136 /* ly prefix? spanner in name? */
137 /* TODO: maybe we should return a vector -- random access is more
138   logical for this list? */
139
140 LY_DEFINE (get_broken_into,
141           "ly:get-broken-into", 1, 0, 0, (SCM spanner),
142            "Return broken-into list for @var{spanner}.")
143 {
144   ///  Spanner *me = unsmob_spanner (spanner);
145   Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (spanner));
146   SCM_ASSERT_TYPE (me, spanner, SCM_ARG1, __FUNCTION__, "spanner");
147
148   SCM s = SCM_EOL;
149   for (int i = me->broken_intos_.size (); i; i--)
150     s = gh_cons (me->broken_intos_[i-1]->self_scm (), s);
151   return s;
152 }
153