release commit
[lilypond.git] / lily / grob-scheme.cc
blobaa3acc99667012052695fe5465557759f8285408
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"
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}")
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");
17 if (!type_check_assignment (sym, val, ly_symbol2scm ("backend-type?")))
18 error ("typecheck failed");
20 sc->internal_set_grob_property (sym, val);
21 return SCM_UNSPECIFIED;
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\n"
27 "will return @code{'()} (end-of-list) if @var{g} doesn't have @var{sym} set.\n"
28 "\n"
29 "Grob properties are stored as GUILE association lists, with symbols as\n"
30 "keys. All lookup functions identify undefined properties with\n"
31 "end-of-list (i.e. @code{'()} in Scheme or @code{SCM_EOL} in C)\n"
32 "\n"
33 "Properties are stored in two ways:\n"
34 "@itemize @bullet\n"
35 "@item mutable properties.\n"
36 "Grob properties that change from object to object. The storage of\n"
37 "these are private to a grob. For example pointers to other grobs are\n"
38 "always stored in the mutable properties.\n"
39 "\n"
40 "@item immutable properties.\n"
41 "Grob properties that are shared across different grobs of the same\n"
42 "type. The storage is shared, and hence it is read-only. Typically, this\n"
43 "is used to store function callbacks, and default settings. They are\n"
44 "initially read from @file{scm/grob-description.scm}.\n"
45 "@end itemize\n"
46 "\n")
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");
52 return sc->internal_get_grob_property (sym);
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} is @code{-1} "
58 "for left, and @code{1} for right.")
60 Spanner * sl = dynamic_cast<Spanner*> (unsmob_grob (slur));
61 SCM_ASSERT_TYPE (sl, slur, SCM_ARG1, __FUNCTION__, "spanner grob");
62 SCM_ASSERT_TYPE (ly_dir_p (dir), slur, SCM_ARG2, __FUNCTION__, "dir");
63 return sl->get_bound (to_dir (dir))->self_scm ();
66 /* TODO: make difference between scaled and unscalead variable in
67 calling (i.e different funcs.) */
68 LY_DEFINE (ly_get_paper_var,"ly:get-paper-variable", 2, 0, 0,
69 (SCM grob, SCM sym),
70 "Get a variable from the \\paper block.")
72 Grob * sc = unsmob_grob (grob);
73 SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
74 SCM_ASSERT_TYPE (gh_symbol_p (sym), sym, SCM_ARG2, __FUNCTION__, "symbol");
76 return sc->get_paper () ->lookup_variable (sym);
81 LY_DEFINE (ly_get_extent, "ly:get-extent", 3, 0, 0,
82 (SCM grob, SCM refp, SCM axis),
83 "Get the extent in @var{axis} direction of @var{grob} relative to "
84 "the grob @var{refp}")
86 Grob * sc = unsmob_grob (grob);
87 Grob * ref = unsmob_grob (refp);
88 SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
89 SCM_ASSERT_TYPE (ref, refp, SCM_ARG2, __FUNCTION__, "grob");
91 SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG3, __FUNCTION__, "axis");
93 return ly_interval2scm ( sc->extent (ref, Axis (gh_scm2int (axis))));
96 LY_DEFINE (ly_get_parent, "ly:get-parent", 2, 0, 0, (SCM grob, SCM axis),
97 "Get the parent of @var{grob}. @var{axis} is 0 for the X-axis, "
98 "1 for the Y-axis.")
100 Grob * sc = unsmob_grob (grob);
101 SCM_ASSERT_TYPE (sc, grob, SCM_ARG1, __FUNCTION__, "grob");
102 SCM_ASSERT_TYPE (ly_axis_p (axis), axis, SCM_ARG2, __FUNCTION__, "axis");
104 Grob * par = sc->get_parent (Axis (gh_scm2int (axis)));
105 return par ? par->self_scm () : SCM_EOL;
108 /* ly prefix? */
109 LY_DEFINE (get_system,
110 "ly:get-system",
111 1, 0, 0, (SCM grob),
112 "Return the System Grob of @var{grob}.")
114 Grob *me = unsmob_grob (grob);
115 SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob");
117 if (System *g = me->get_system ())
118 return g->self_scm ();
120 return SCM_EOL;
123 /* ly prefix? */
124 LY_DEFINE (get_original,
125 "ly:get-original",
126 1, 0, 0, (SCM grob),
127 "Return the original Grob of @var{grob}")
129 Grob *me = unsmob_grob (grob);
130 SCM_ASSERT_TYPE (me, grob, SCM_ARG1, __FUNCTION__, "grob");
131 return me->original_ ? me->original_->self_scm () : me->self_scm ();
135 /* ly prefix? spanner in name? */
136 /* TODO: maybe we should return a vector -- random access is more
137 logical for this list? */
139 LY_DEFINE (get_broken_into,
140 "ly:get-broken-into", 1, 0, 0, (SCM spanner),
141 "Return broken-into list for @var{spanner}.")
143 /// Spanner *me = unsmob_spanner (spanner);
144 Spanner *me = dynamic_cast<Spanner*> (unsmob_grob (spanner));
145 SCM_ASSERT_TYPE (me, spanner, SCM_ARG1, __FUNCTION__, "spanner");
147 SCM s = SCM_EOL;
148 for (int i = me->broken_intos_.size (); i; i--)
149 s = gh_cons (me->broken_intos_[i-1]->self_scm (), s);
150 return s;