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