]> git.donarmstrong.com Git - lilypond.git/blob - lily/grob-scheme.cc
tags
[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 LY_DEFINE(ly_get_paper_var,"ly-get-paper-variable", 2, 0, 0,
68   (SCM grob, SCM sym),
69   "Get a variable from the \\paper block.")
70 {
71   Grob * sc = unsmob_grob (grob);
72   SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob");
73   SCM_ASSERT_TYPE(gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");  
74
75   return sc->get_paper () ->get_scmvar_scm (sym);
76 }
77
78
79
80 LY_DEFINE(ly_get_extent, "ly-get-extent", 3, 0, 0,
81           (SCM grob, SCM refp, SCM axis),
82           "Get the extent in @var{axis} direction of @var{grob} relative to the
83 grob @var{refp}")
84 {
85   Grob * sc = unsmob_grob (grob);
86   Grob * ref = unsmob_grob (refp);
87   SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob");
88   SCM_ASSERT_TYPE(ref, refp, SCM_ARG2, __FUNCTION__, "grob");
89   
90   SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
91
92   return ly_interval2scm ( sc->extent (ref, Axis (gh_scm2int (axis))));
93 }
94
95 LY_DEFINE (ly_get_parent,   "ly-get-parent", 2, 0, 0, (SCM grob, SCM axis),
96            "Get the parent of @var{grob}.  @var{axis} can be 0 for the X-axis, 1
97 for the Y-axis.")
98 {
99   Grob * sc = unsmob_grob (grob);
100   SCM_ASSERT_TYPE(sc, grob, SCM_ARG1, __FUNCTION__, "grob");
101   SCM_ASSERT_TYPE(ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
102
103   Grob * par = sc->get_parent (Axis (gh_scm2int (axis)));
104   return par ? par->self_scm() : SCM_EOL;
105 }
106
107 /* ly prefix? */
108 LY_DEFINE (get_system,
109            "get-system",
110            1, 0, 0, (SCM grob),
111            "
112 Return the System Grob of @var{grob}.
113 ")
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            "get-original",
127            1, 0, 0, (SCM grob),
128            "
129 Return the original Grob of @var{grob}
130 ")
131 {
132   Grob *me = unsmob_grob (grob);
133   SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob");
134   return me->original_ ? me->original_->self_scm () : me->self_scm ();
135 }
136
137
138 /* ly prefix? spanner in name? */
139 LY_DEFINE (get_broken_into,
140           "get-broken-into", 1, 0, 0, (SCM spanner),
141            "
142 Return broken-into list for @var{spanner}.
143 "
144 )
145 {
146   ///  Spanner *me = unsmob_spanner (spanner);
147   Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (spanner));
148   SCM_ASSERT_TYPE (me, spanner, SCM_ARG1, __FUNCTION__, "spanner");
149
150   SCM s = SCM_EOL;
151   for (int i = me->broken_intos_.size (); i; i--)
152     s = gh_cons (me->broken_intos_[i-1]->self_scm (), s);
153   return s;
154 }
155