3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Stefan Schimanski
10 * Full author contact details are available in file CREDITS.
15 #include "MathMacro.h"
16 #include "MathSupport.h"
17 #include "MathExtern.h"
18 #include "MathStream.h"
21 #include "BufferView.h"
23 #include "support/debug.h"
24 #include "LaTeXFeatures.h"
25 #include "FuncStatus.h"
26 #include "FuncRequest.h"
29 #include "frontends/Painter.h"
39 /// A proxy for the macro values
40 class ArgumentProxy : public InsetMath {
43 ArgumentProxy(MathMacro & mathMacro, size_t idx)
44 : mathMacro_(mathMacro), idx_(idx) {}
46 ArgumentProxy(MathMacro & mathMacro, size_t idx, docstring const & def)
47 : mathMacro_(mathMacro), idx_(idx)
52 void metrics(MetricsInfo & mi, Dimension & dim) const {
53 mathMacro_.macro()->unlock();
54 mathMacro_.cell(idx_).metrics(mi, dim);
55 if (!mathMacro_.editMetrics(mi.base.bv)
56 && mathMacro_.cell(idx_).empty())
57 def_.metrics(mi, dim);
58 mathMacro_.macro()->lock();
61 void draw(PainterInfo & pi, int x, int y) const {
62 if (mathMacro_.editMetrics(pi.base.bv)) {
63 // The only way a ArgumentProxy can appear is in a cell of the
64 // MathMacro. Moreover the cells are only drawn in the DISPLAY_FOLDED
65 // mode and then, if the macro is edited the monochrome
66 // mode is entered by the MathMacro before calling the cells' draw
67 // method. Then eventually this code is reached and the proxy leaves
68 // monochrome mode temporarely. Hence, if it is not in monochrome
69 // here (and the assert triggers in pain.leaveMonochromeMode())
71 pi.pain.leaveMonochromeMode();
72 mathMacro_.cell(idx_).draw(pi, x, y);
73 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
74 } else if (mathMacro_.cell(idx_).empty()) {
75 mathMacro_.cell(idx_).setXY(*pi.base.bv, x, y);
78 mathMacro_.cell(idx_).draw(pi, x, y);
81 size_t idx() const { return idx_; }
83 int kerning(BufferView const * bv) const
85 if (mathMacro_.editMetrics(bv)
86 || !mathMacro_.cell(idx_).empty())
87 return mathMacro_.cell(idx_).kerning(bv);
89 return def_.kerning(bv);
96 return new ArgumentProxy(*this);
99 MathMacro & mathMacro_;
107 MathMacro::MathMacro(docstring const & name)
108 : InsetMathNest(0), name_(name), displayMode_(DISPLAY_INIT),
109 attachedArgsNum_(0), optionals_(0), nextFoldMode_(true),
110 macro_(0), needsUpdate_(false)
114 Inset * MathMacro::clone() const
116 MathMacro * copy = new MathMacro(*this);
117 copy->needsUpdate_ = true;
118 copy->expanded_.cell(0).clear();
123 docstring MathMacro::name() const
125 if (displayMode_ == DISPLAY_UNFOLDED)
126 return asString(cell(0));
132 void MathMacro::cursorPos(BufferView const & bv,
133 CursorSlice const & sl, bool boundary, int & x, int & y) const
135 // We may have 0 arguments, but InsetMathNest requires at least one.
137 InsetMathNest::cursorPos(bv, sl, boundary, x, y);
141 int MathMacro::cursorIdx(Cursor const & cur) const {
142 for (size_t i = 0; i != cur.depth(); ++i)
143 if (&cur[i].inset() == this)
149 bool MathMacro::editMode(BufferView const * bv) const {
150 // find this in cursor trace
151 Cursor const & cur = bv->cursor();
152 for (size_t i = 0; i != cur.depth(); ++i)
153 if (&cur[i].inset() == this) {
154 // look if there is no other macro in edit mode above
156 for (; i != cur.depth(); ++i) {
157 MathMacro const * macro = dynamic_cast<MathMacro const *>(&cur[i].inset());
158 if (macro && macro->displayMode() == DISPLAY_NORMAL)
162 // ok, none found, I am the highest one
170 bool MathMacro::editMetrics(BufferView const * bv) const
176 void MathMacro::metrics(MetricsInfo & mi, Dimension & dim) const
178 // set edit mode for which we will have calculated metrics. But only
179 editing_[mi.base.bv] = editMode(mi.base.bv);
181 // calculate new metrics according to display mode
182 if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
183 mathed_string_dim(mi.base.font, from_ascii("\\") + name(), dim);
184 } else if (displayMode_ == DISPLAY_UNFOLDED) {
185 cell(0).metrics(mi, dim);
187 mathed_string_dim(mi.base.font, from_ascii("\\"), bsdim);
188 dim.wid += bsdim.width() + 1;
189 dim.asc = max(bsdim.ascent(), dim.ascent());
190 dim.des = max(bsdim.descent(), dim.descent());
193 BOOST_ASSERT(macro_ != 0);
195 // calculate metrics finally
197 expanded_.cell(0).metrics(mi, dim);
200 // calculate dimension with label while editing
201 if (editing_[mi.base.bv]) {
202 FontInfo font = mi.base.font;
203 augmentFont(font, from_ascii("lyxtex"));
205 mathed_string_dim(font, name(), namedim);
207 dim.wid += 2 + namedim.wid + 2 + 2;
208 dim.asc = max(dim.asc, namedim.asc) + 2;
209 dim.des = max(dim.des, namedim.des) + 2;
211 dim.wid = max(1 + namedim.wid + 1, 2 + dim.wid + 2);
212 dim.asc += 1 + namedim.height() + 1;
219 int MathMacro::kerning(BufferView const * bv) const {
220 if (displayMode_ == DISPLAY_NORMAL && !editing_[bv])
221 return expanded_.kerning(bv);
227 void MathMacro::updateMacro(MacroContext const & mc)
230 macro_ = mc.get(name());
231 if (macro_ && macroBackup_ != *macro_) {
232 macroBackup_ = *macro_;
241 void MathMacro::updateRepresentation(Cursor const * bvCur)
243 // index of child where the cursor is (or -1 if none is edited)
246 curIdx = cursorIdx(*bvCur);
247 previousCurIdx_[&bvCur->bv()] = curIdx;
255 requires_ = macro_->requires();
257 // non-normal mode? We are done!
258 if (displayMode_ != DISPLAY_NORMAL)
263 needsUpdate_ = false;
265 // get default values of macro
266 vector<docstring> const & defaults = macro_->defaults();
268 // create MathMacroArgumentValue objects pointing to the cells of the macro
269 vector<MathData> values(nargs());
270 for (size_t i = 0; i < nargs(); ++i) {
271 ArgumentProxy * proxy;
273 || i >= defaults.size()
274 || defaults[i].empty()
276 proxy = new ArgumentProxy(*this, i);
278 proxy = new ArgumentProxy(*this, i, defaults[i]);
279 values[i].insert(0, MathAtom(proxy));
282 // expanding macro with the values
283 macro_->expand(values, expanded_.cell(0));
288 void MathMacro::draw(PainterInfo & pi, int x, int y) const
290 Dimension const dim = dimension(*pi.base.bv);
292 setPosCache(pi, x, y);
296 if (displayMode_ == DISPLAY_INIT || displayMode_ == DISPLAY_INTERACTIVE_INIT) {
297 PainterInfo pi2(pi.base.bv, pi.pain);
298 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
299 //pi2.base.style = LM_ST_TEXT;
300 pi2.pain.text(x, y, from_ascii("\\") + name(), pi2.base.font);
301 } else if (displayMode_ == DISPLAY_UNFOLDED) {
302 PainterInfo pi2(pi.base.bv, pi.pain);
303 pi2.base.font.setColor(macro_ ? Color_latex : Color_error);
304 //pi2.base.style = LM_ST_TEXT;
305 pi2.pain.text(x, y, from_ascii("\\"), pi2.base.font);
306 x += mathed_string_width(pi2.base.font, from_ascii("\\")) + 1;
307 cell(0).draw(pi2, x, y);
308 drawMarkers(pi2, expx, expy);
311 for (size_t i = 0; i < nargs(); ++i)
312 cell(i).setXY(*pi.base.bv, x, y);
314 if (editing_[pi.base.bv]) {
315 // draw header and rectangle around
316 FontInfo font = pi.base.font;
317 augmentFont(font, from_ascii("lyxtex"));
318 font.setSize(FONT_SIZE_TINY);
319 font.setColor(Color_mathmacrolabel);
321 mathed_string_dim(font, name(), namedim);
323 pi.pain.fillRectangle(x, y - dim.asc, 2 + namedim.width() + 2, dim.height(), Color_mathmacrobg);
324 pi.pain.text(x + 2, y, name(), font);
325 expx += 2 + namew + 2;
327 pi.pain.fillRectangle(x, y - dim.asc, dim.wid, 1 + namedim.height() + 1, Color_mathmacrobg);
328 pi.pain.text(x + 1, y - dim.asc + namedim.asc + 2, name(), font);
329 expx += (dim.wid - expanded_.cell(0).dimension(*pi.base.bv).width()) / 2;
331 pi.pain.enterMonochromeMode(Color_mathbg, Color_mathmacroblend);
332 expanded_.cell(0).draw(pi, expx, expy);
333 pi.pain.leaveMonochromeMode();
335 expanded_.cell(0).draw(pi, expx, expy);
337 // draw frame while editing
338 if (editing_[pi.base.bv])
339 pi.pain.rectangle(x, y - dim.asc, dim.wid, dim.height(), Color_mathmacroframe);
342 // another argument selected or edit mode changed?
343 idx_type curIdx = cursorIdx(pi.base.bv->cursor());
344 if (previousCurIdx_[pi.base.bv] != curIdx
345 || editing_[pi.base.bv] != editMode(pi.base.bv))
346 pi.base.bv->cursor().updateFlags(Update::Force);
350 void MathMacro::drawSelection(PainterInfo & pi, int x, int y) const
352 // We may have 0 arguments, but InsetMathNest requires at least one.
353 if (cells_.size() > 0)
354 InsetMathNest::drawSelection(pi, x, y);
358 void MathMacro::setDisplayMode(MathMacro::DisplayMode mode)
360 if (displayMode_ != mode) {
361 // transfer name if changing from or to DISPLAY_UNFOLDED
362 if (mode == DISPLAY_UNFOLDED) {
364 asArray(name_, cell(0));
365 } else if (displayMode_ == DISPLAY_UNFOLDED) {
366 name_ = asString(cell(0));
376 MathMacro::DisplayMode MathMacro::computeDisplayMode() const
378 if (nextFoldMode_ == true && macro_ && !macro_->locked())
379 return DISPLAY_NORMAL;
381 return DISPLAY_UNFOLDED;
385 bool MathMacro::validName() const
387 docstring n = name();
393 // converting back and force doesn't swallow anything?
396 if (asString(ma) != n)
400 for (size_t i = 0; i<n.size(); ++i) {
401 if (!(n[i] >= 'a' && n[i] <= 'z') &&
402 !(n[i] >= 'A' && n[i] <= 'Z'))
410 void MathMacro::validate(LaTeXFeatures & features) const
412 if (!requires_.empty())
413 features.require(requires_);
415 if (name() == "binom" || name() == "mathcircumflex")
416 features.require(to_utf8(name()));
420 void MathMacro::edit(Cursor & cur, bool left)
422 cur.updateFlags(Update::Force);
423 InsetMathNest::edit(cur, left);
427 Inset * MathMacro::editXY(Cursor & cur, int x, int y)
429 // We may have 0 arguments, but InsetMathNest requires at least one.
431 cur.updateFlags(Update::Force);
432 return InsetMathNest::editXY(cur, x, y);
438 void MathMacro::removeArgument(Inset::pos_type pos) {
439 if (displayMode_ == DISPLAY_NORMAL) {
440 BOOST_ASSERT(size_t(pos) < cells_.size());
441 cells_.erase(cells_.begin() + pos);
442 if (size_t(pos) < attachedArgsNum_)
444 if (size_t(pos) < optionals_) {
453 void MathMacro::insertArgument(Inset::pos_type pos) {
454 if (displayMode_ == DISPLAY_NORMAL) {
455 BOOST_ASSERT(size_t(pos) <= cells_.size());
456 cells_.insert(cells_.begin() + pos, MathData());
457 if (size_t(pos) < attachedArgsNum_)
459 if (size_t(pos) < optionals_)
467 void MathMacro::detachArguments(vector<MathData> & args, bool strip)
469 BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);
472 // strip off empty cells, but not more than arity-attachedArgsNum_
475 for (i = cells_.size(); i > attachedArgsNum_; --i)
476 if (!cell(i - 1).empty()) break;
480 attachedArgsNum_ = 0;
481 expanded_.cell(0) = MathData();
488 void MathMacro::attachArguments(vector<MathData> const & args, size_t arity, int optionals)
490 BOOST_ASSERT(displayMode_ == DISPLAY_NORMAL);
492 attachedArgsNum_ = args.size();
493 cells_.resize(arity);
494 expanded_.cell(0) = MathData();
495 optionals_ = optionals;
501 bool MathMacro::idxFirst(Cursor & cur) const
503 cur.updateFlags(Update::Force);
504 return InsetMathNest::idxFirst(cur);
508 bool MathMacro::idxLast(Cursor & cur) const
510 cur.updateFlags(Update::Force);
511 return InsetMathNest::idxLast(cur);
515 bool MathMacro::notifyCursorLeaves(Cursor & cur)
517 cur.updateFlags(Update::Force);
518 return InsetMathNest::notifyCursorLeaves(cur);
522 void MathMacro::fold(Cursor & cur)
524 if (!nextFoldMode_) {
525 nextFoldMode_ = true;
526 cur.updateFlags(Update::Force);
531 void MathMacro::unfold(Cursor & cur)
534 nextFoldMode_ = false;
535 cur.updateFlags(Update::Force);
540 bool MathMacro::folded() const
542 return nextFoldMode_;
546 void MathMacro::write(WriteStream & os) const
549 if (displayMode_ != DISPLAY_NORMAL) {
550 os << "\\" << name() << " ";
551 os.pendingSpace(true);
556 BOOST_ASSERT(macro_);
558 // optional arguments make macros fragile
559 if (optionals_ > 0 && os.fragile())
562 os << "\\" << name();
565 // Optional arguments:
566 // First find last non-empty optional argument
567 idx_type emptyOptFrom = 0;
569 for (; i < cells_.size() && i < optionals_; ++i) {
570 if (!cell(i).empty())
571 emptyOptFrom = i + 1;
574 // print out optionals
575 for (i=0; i < cells_.size() && i < emptyOptFrom; ++i) {
577 os << "[" << cell(i) << "]";
580 // skip the tailing empty optionals
583 // Print remaining macros
584 for (; i < cells_.size(); ++i) {
585 if (cell(i).size() == 1
586 && cell(i)[0].nucleus()->asCharInset()) {
591 os << "{" << cell(i) << "}";
595 // add space if there was no argument
597 os.pendingSpace(true);
601 void MathMacro::maple(MapleStream & os) const
603 lyx::maple(expanded_.cell(0), os);
607 void MathMacro::mathmlize(MathStream & os) const
609 lyx::mathmlize(expanded_.cell(0), os);
613 void MathMacro::octave(OctaveStream & os) const
615 lyx::octave(expanded_.cell(0), os);
619 void MathMacro::infoize(odocstream & os) const
621 os << "Macro: " << name();
625 void MathMacro::infoize2(odocstream & os) const
627 os << "Macro: " << name();