2 * \file InsetCollapsable.cpp
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 Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "InsetCollapsable.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
21 #include "Dimension.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
28 #include "LaTeXFeatures.h"
30 #include "MetricsInfo.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
34 #include "TextClass.h"
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lassert.h"
43 #include "support/lstrings.h"
50 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
52 if (decoration() == InsetLayout::CONGLOMERATE)
54 return auto_open_[&bv] ? Open : status_;
58 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
60 switch (decoration()) {
61 case InsetLayout::CLASSIC:
62 if (status(bv) == Open)
63 return openinlined_ ? LeftButton : TopButton;
66 case InsetLayout::MINIMALISTIC:
67 return status(bv) == Open ? NoButton : ButtonOnly ;
69 case InsetLayout::CONGLOMERATE:
70 return status(bv) == Open ? SubLabel : Corners ;
72 case InsetLayout::DEFAULT:
73 break; // this shouldn't happen
76 // dummy return value to shut down a warning,
82 InsetCollapsable::Geometry InsetCollapsable::geometry() const
84 switch (decoration()) {
85 case InsetLayout::CLASSIC:
87 return openinlined_ ? LeftButton : TopButton;
90 case InsetLayout::MINIMALISTIC:
91 return status_ == Open ? NoButton : ButtonOnly ;
93 case InsetLayout::CONGLOMERATE:
94 return status_ == Open ? SubLabel : Corners ;
96 case InsetLayout::DEFAULT:
97 break; // this shouldn't happen
100 // dummy return value to shut down a warning,
101 // this is dead code.
106 InsetCollapsable::InsetCollapsable(Buffer const & buf, InsetText::UsePlain ltype)
107 : InsetText(buf, ltype), status_(Inset::Open),
108 openinlined_(false), mouse_hover_(false)
110 setLayout(&buf.params().documentClass());
111 setAutoBreakRows(true);
113 setFrameColor(Color_collapsableframe);
117 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
119 status_(rhs.status_),
120 layout_(rhs.layout_),
121 labelstring_(rhs.labelstring_),
122 button_dim(rhs.button_dim),
123 openinlined_(rhs.openinlined_),
124 auto_open_(rhs.auto_open_),
125 // the sole purpose of this copy constructor
131 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
133 Dimension dim = dimensionCollapsed(bv);
134 if (geometry(bv) == NoButton)
135 return translateIfPossible(layout_->labelstring());
136 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
139 OutputParams rp(&buffer().params().encoding());
140 odocstringstream ods;
141 InsetText::plaintext(ods, rp);
142 docstring const content_tip = ods.str();
143 return support::wrapParas(content_tip, 4);
147 void InsetCollapsable::setLayout(BufferParams const & bp)
149 setLayout(bp.documentClassPtr());
153 void InsetCollapsable::setLayout(DocumentClass const * const dc)
156 layout_ = &(dc->insetLayout(name()));
157 labelstring_ = translateIfPossible(layout_->labelstring());
159 layout_ = &DocumentClass::plainInsetLayout();
160 labelstring_ = _("UNDEFINED");
167 void InsetCollapsable::write(ostream & os) const
179 text().write(buffer(), os);
183 void InsetCollapsable::read(Lexer & lex)
185 lex.setContext("InsetCollapsable::read");
188 lex >> "status" >> tmp_token;
189 if (tmp_token == "open")
192 // this must be set before we enter InsetText::read()
193 setLayout(buffer().params());
194 InsetText::read(lex);
195 // set button label again as the inset contents was not read yet at
199 // Force default font, if so requested
200 // This avoids paragraphs in buffer language that would have a
201 // foreign language after a document language change, and it ensures
202 // that all new text in ERT and similar gets the "latex" language,
203 // since new text inherits the language from the last position of the
204 // existing text. As a side effect this makes us also robust against
205 // bugs in LyX that might lead to font changes in ERT in .lyx files.
206 resetParagraphsFont();
210 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
212 LASSERT(layout_, /**/);
214 theFontMetrics(layout_->labelfont()).buttonText(
215 buttonLabel(bv), dim.wid, dim.asc, dim.des);
220 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
222 LASSERT(layout_, /**/);
224 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
226 FontInfo tmpfont = mi.base.font;
227 mi.base.font = layout_->font();
228 mi.base.font.realize(tmpfont);
230 BufferView const & bv = *mi.base.bv;
232 switch (geometry(bv)) {
234 InsetText::metrics(mi, dim);
237 InsetText::metrics(mi, dim);
242 InsetText::metrics(mi, dim);
243 // consider width of the inset label
244 FontInfo font(layout_->labelfont());
245 font.realize(sane_font);
251 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
258 dim = dimensionCollapsed(bv);
259 if (geometry(bv) == TopButton
260 || geometry(bv) == LeftButton) {
262 InsetText::metrics(mi, textdim);
263 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
265 // Correct for button width.
266 dim.wid += textdim.wid;
267 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
268 dim.asc = textdim.asc;
270 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
271 dim.wid = max(dim.wid, textdim.wid);
277 mi.base.font = tmpfont;
281 bool InsetCollapsable::setMouseHover(bool mouse_hover)
283 mouse_hover_ = mouse_hover;
288 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
290 LASSERT(layout_, /**/);
291 BufferView const & bv = *pi.base.bv;
293 auto_open_[&bv] = bv.cursor().isInside(this);
295 FontInfo tmpfont = pi.base.font;
296 pi.base.font = layout_->font();
297 pi.base.font.realize(tmpfont);
299 // Draw button first -- top, left or only
300 Dimension dimc = dimensionCollapsed(bv);
302 if (geometry(*pi.base.bv) == TopButton ||
303 geometry(*pi.base.bv) == LeftButton ||
304 geometry(*pi.base.bv) == ButtonOnly) {
305 button_dim.x1 = x + 0;
306 button_dim.x2 = x + dimc.width();
307 button_dim.y1 = y - dimc.asc;
308 button_dim.y2 = y + dimc.des;
310 pi.pain.buttonText(x, y, buttonLabel(bv), layout_->labelfont(),
319 Dimension const textdim = InsetText::dimension(bv);
320 int const baseline = y;
322 switch (geometry(bv)) {
324 textx = x + dimc.width();
326 InsetText::draw(pi, textx, texty);
330 texty = baseline + dimc.des + textdim.asc;
331 InsetText::draw(pi, textx, texty);
338 InsetText::draw(pi, textx, texty);
344 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
345 InsetText::draw(pi, textx, texty);
346 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
348 int desc = textdim.descent();
349 if (geometry(bv) == Corners)
352 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
353 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
354 pi.pain.line(xx1, y + desc - 4,
356 layout_->labelfont().color());
358 pi.pain.line(xx1, y + desc,
360 layout_->labelfont().color());
362 // Make status_ value visible:
363 pi.pain.line(xx1, y + desc,
365 layout_->labelfont().color());
366 pi.pain.line(xx2 - 4, y + desc,
368 layout_->labelfont().color());
370 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
371 y + desc - 4, layout_->labelfont().color());
373 // the label below the text. Can be toggled.
374 if (geometry(bv) == SubLabel) {
375 FontInfo font(layout_->labelfont());
376 font.realize(sane_font);
382 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
383 int const ww = max(textdim.wid, w);
384 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
385 buttonLabel(bv), font, Color_none, Color_none);
389 // a visual cue when the cursor is inside the inset
390 Cursor const & cur = bv.cursor();
391 if (cur.isInside(this)) {
394 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
395 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
396 pi.pain.line(xx2, y + 4, xx2, y,
397 layout_->labelfont().color());
398 pi.pain.line(xx2 - 4, y, xx2, y,
399 layout_->labelfont().color());
404 pi.base.font = tmpfont;
408 void InsetCollapsable::cursorPos(BufferView const & bv,
409 CursorSlice const & sl, bool boundary, int & x, int & y) const
411 if (geometry(bv) == ButtonOnly)
413 LASSERT(geometry(bv) != ButtonOnly, /**/);
415 InsetText::cursorPos(bv, sl, boundary, x, y);
416 Dimension const textdim = InsetText::dimension(bv);
418 switch (geometry(bv)) {
420 x += dimensionCollapsed(bv).wid;
423 y += dimensionCollapsed(bv).des + textdim.asc;
438 bool InsetCollapsable::editable() const
440 return geometry() != ButtonOnly;
444 bool InsetCollapsable::descendable() const
446 return geometry() != ButtonOnly;
450 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
452 return button_dim.contains(cmd.x, cmd.y);
456 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
459 pos_type const max_length = 15;
460 pos_type const p_siz = paragraphs().begin()->size();
461 pos_type const n = min(max_length, p_siz);
464 for (; i < n && j < p_siz; ++j) {
465 if (paragraphs().begin()->isInset(j))
467 label += paragraphs().begin()->getChar(j);
470 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
473 return label.empty() ? l : label;
477 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
479 //lyxerr << "InsetCollapsable: edit left/right" << endl;
481 InsetText::edit(cur, front, entry_from);
485 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
487 //lyxerr << "InsetCollapsable: edit xy" << endl;
488 if (geometry(cur.bv()) == ButtonOnly
489 || (button_dim.contains(x, y)
490 && geometry(cur.bv()) != NoButton))
493 return InsetText::editXY(cur, x, y);
497 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
499 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
500 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
502 switch (cmd.action) {
503 case LFUN_MOUSE_PRESS:
504 if (hitButton(cmd)) {
505 switch (cmd.button()) {
506 case mouse_button::button1:
507 case mouse_button::button3:
508 // Pass the command to the enclosing InsetText,
509 // so that the cursor gets set.
512 case mouse_button::none:
513 case mouse_button::button2:
514 case mouse_button::button4:
515 case mouse_button::button5:
520 } else if (geometry(cur.bv()) != ButtonOnly)
521 InsetText::doDispatch(cur, cmd);
526 case LFUN_MOUSE_MOTION:
527 case LFUN_MOUSE_DOUBLE:
528 case LFUN_MOUSE_TRIPLE:
531 else if (geometry(cur.bv()) != ButtonOnly)
532 InsetText::doDispatch(cur, cmd);
537 case LFUN_MOUSE_RELEASE:
538 if (!hitButton(cmd)) {
539 // The mouse click has to be within the inset!
540 if (geometry(cur.bv()) != ButtonOnly)
541 InsetText::doDispatch(cur, cmd);
546 if (cmd.button() != mouse_button::button1) {
551 // if we are selecting, we do not want to
555 // Left button is clicked, the user asks to
556 // toggle the inset visual state.
558 cur.updateFlags(Update::Force | Update::FitCursor);
559 if (geometry(cur.bv()) == ButtonOnly) {
560 setStatus(cur, Open);
564 setStatus(cur, Collapsed);
565 cur.bv().cursor() = cur;
568 case LFUN_INSET_TOGGLE:
569 if (cmd.argument() == "open")
570 setStatus(cur, Open);
571 else if (cmd.argument() == "close")
572 setStatus(cur, Collapsed);
573 else if (cmd.argument() == "toggle" || cmd.argument().empty())
574 if (status_ == Open) {
575 setStatus(cur, Collapsed);
576 if (geometry(cur.bv()) == ButtonOnly)
577 cur.top().forwardPos();
579 setStatus(cur, Open);
580 else // if assign or anything else
586 case LFUN_CLIPBOARD_PASTE:
587 case LFUN_SELECTION_PASTE:
588 case LFUN_PRIMARY_SELECTION_PASTE: {
589 InsetText::doDispatch(cur, cmd);
590 // Since we can only store plain text, we must reset all
592 // FIXME: Change only the pasted paragraphs
594 resetParagraphsFont();
599 if (layout_ && layout_->isForceLtr()) {
600 // Force any new text to latex_language
601 // FIXME: This should only be necessary in constructor, but
602 // new paragraphs that are created by pressing enter at the
603 // start of an existing paragraph get the buffer language
604 // and not latex_language, so we take this brute force
606 cur.current_font.setLanguage(latex_language);
607 cur.real_current_font.setLanguage(latex_language);
609 InsetText::doDispatch(cur, cmd);
615 bool InsetCollapsable::allowMultiPar() const
617 return layout_->isMultiPar();
621 void InsetCollapsable::resetParagraphsFont()
623 Font font(inherit_font, buffer().params().language);
624 if (layout_->isForceLtr())
625 font.setLanguage(latex_language);
626 if (layout_->isPassThru()) {
627 ParagraphList::iterator par = paragraphs().begin();
628 ParagraphList::iterator const end = paragraphs().end();
630 par->resetFonts(font);
631 par->params().clear();
638 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
639 FuncStatus & flag) const
641 switch (cmd.action) {
642 // FIXME At present, these are being enabled and disabled according to
643 // whether PASSTHRU has been set in the InsetLayout. This makes some
644 // sense, but there are other checks that should really be done. E.g.,
645 // one should not be able to inset IndexPrint inside an optional argument!!
646 case LFUN_ACCENT_ACUTE:
647 case LFUN_ACCENT_BREVE:
648 case LFUN_ACCENT_CARON:
649 case LFUN_ACCENT_CEDILLA:
650 case LFUN_ACCENT_CIRCLE:
651 case LFUN_ACCENT_CIRCUMFLEX:
652 case LFUN_ACCENT_DOT:
653 case LFUN_ACCENT_GRAVE:
654 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
655 case LFUN_ACCENT_MACRON:
656 case LFUN_ACCENT_OGONEK:
657 case LFUN_ACCENT_TIE:
658 case LFUN_ACCENT_TILDE:
659 case LFUN_ACCENT_UMLAUT:
660 case LFUN_ACCENT_UNDERBAR:
661 case LFUN_ACCENT_UNDERDOT:
663 case LFUN_BOX_INSERT:
664 case LFUN_BRANCH_INSERT:
665 case LFUN_CAPTION_INSERT:
666 case LFUN_DEPTH_DECREMENT:
667 case LFUN_DEPTH_INCREMENT:
668 case LFUN_ERT_INSERT:
669 case LFUN_FILE_INSERT:
670 case LFUN_FLEX_INSERT:
671 case LFUN_FLOAT_INSERT:
672 case LFUN_FLOAT_LIST_INSERT:
673 case LFUN_FLOAT_WIDE_INSERT:
675 case LFUN_FONT_BOLDSYMBOL:
676 case LFUN_FONT_TYPEWRITER:
677 case LFUN_FONT_DEFAULT:
680 case LFUN_FONT_ROMAN:
685 case LFUN_FONT_STATE:
686 case LFUN_FONT_UNDERLINE:
687 case LFUN_FONT_STRIKEOUT:
688 case LFUN_FONT_UULINE:
689 case LFUN_FONT_UWAVE:
690 case LFUN_FOOTNOTE_INSERT:
691 case LFUN_HYPERLINK_INSERT:
692 case LFUN_INDEX_INSERT:
693 case LFUN_INDEX_PRINT:
694 case LFUN_INSET_INSERT:
695 case LFUN_LABEL_GOTO:
696 case LFUN_LABEL_INSERT:
697 case LFUN_LAYOUT_TABULAR:
698 case LFUN_LINE_INSERT:
699 case LFUN_MARGINALNOTE_INSERT:
700 case LFUN_MATH_DISPLAY:
701 case LFUN_MATH_INSERT:
702 case LFUN_MATH_MATRIX:
705 case LFUN_NEWLINE_INSERT:
706 case LFUN_NEWPAGE_INSERT:
708 case LFUN_NOMENCL_INSERT:
709 case LFUN_NOMENCL_PRINT:
710 case LFUN_NOTE_INSERT:
712 case LFUN_OPTIONAL_INSERT:
713 case LFUN_PHANTOM_INSERT:
714 case LFUN_REFERENCE_NEXT:
715 case LFUN_SERVER_GOTO_FILE_ROW:
716 case LFUN_SERVER_NOTIFY:
717 case LFUN_SERVER_SET_XY:
718 case LFUN_SPACE_INSERT:
719 case LFUN_SPECIALCHAR_INSERT:
720 case LFUN_TABULAR_INSERT:
721 case LFUN_TEXTSTYLE_APPLY:
722 case LFUN_TEXTSTYLE_UPDATE:
723 case LFUN_TOC_INSERT:
724 case LFUN_WRAP_INSERT:
725 if (layout_->isPassThru()) {
726 flag.setEnabled(false);
729 return InsetText::getStatus(cur, cmd, flag);
731 case LFUN_INSET_TOGGLE:
732 if (cmd.argument() == "open")
733 flag.setEnabled(status_ != Open);
734 else if (cmd.argument() == "close")
735 flag.setEnabled(status_ == Open);
736 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
737 flag.setEnabled(true);
738 flag.setOnOff(status_ == Open);
740 flag.setEnabled(false);
744 flag.setEnabled(!layout_->isForceLtr());
745 return InsetText::getStatus(cur, cmd, flag);
747 case LFUN_BREAK_PARAGRAPH:
748 flag.setEnabled(layout_->isMultiPar());
752 return InsetText::getStatus(cur, cmd, flag);
757 void InsetCollapsable::setLabel(docstring const & l)
763 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
767 if (status_ == Collapsed) {
768 cur.leaveInset(*this);
769 mouse_hover_ = false;
774 docstring InsetCollapsable::floatName(
775 string const & type, BufferParams const & bp) const
777 FloatList const & floats = bp.documentClass().floats();
778 FloatList::const_iterator it = floats[type];
780 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
784 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
787 return InsetLayout::CLASSIC;
788 InsetLayout::InsetDecoration const dec = layout_->decoration();
790 case InsetLayout::CLASSIC:
791 case InsetLayout::MINIMALISTIC:
792 case InsetLayout::CONGLOMERATE:
794 case InsetLayout::DEFAULT:
797 if (lyxCode() == FLEX_CODE)
798 return InsetLayout::CONGLOMERATE;
799 return InsetLayout::CLASSIC;
803 int InsetCollapsable::latex(odocstream & os,
804 OutputParams const & runparams) const
806 // FIXME: What should we do layout_ is 0?
812 // This implements the standard way of handling the LaTeX output of
813 // a collapsable inset, either a command or an environment. Standard
814 // collapsable insets should not redefine this, non-standard ones may
816 if (!layout_->latexname().empty()) {
817 if (layout_->latextype() == InsetLayout::COMMAND) {
819 if (runparams.moving_arg)
821 os << '\\' << from_utf8(layout_->latexname());
822 if (!layout_->latexparam().empty())
823 os << from_utf8(layout_->latexparam());
825 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
826 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
827 if (!layout_->latexparam().empty())
828 os << from_utf8(layout_->latexparam());
831 OutputParams rp = runparams;
832 if (layout_->isPassThru())
834 if (layout_->isNeedProtect())
835 rp.moving_arg = true;
836 int i = InsetText::latex(os, rp);
837 if (!layout_->latexname().empty()) {
838 if (layout_->latextype() == InsetLayout::COMMAND) {
840 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
841 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
849 // FIXME It seems as if it ought to be possible to do this more simply,
850 // maybe by calling InsetText::docbook() in the middle there.
851 int InsetCollapsable::docbook(odocstream & os, OutputParams const & runparams) const
853 ParagraphList::const_iterator const beg = paragraphs().begin();
854 ParagraphList::const_iterator par = paragraphs().begin();
855 ParagraphList::const_iterator const end = paragraphs().end();
858 sgml::openTag(os, getLayout().latexname(),
859 par->getID(buffer(), runparams) + getLayout().latexparam());
861 for (; par != end; ++par) {
862 par->simpleDocBookOnePar(buffer(), os, runparams,
863 outerFont(distance(beg, par),
868 sgml::closeTag(os, getLayout().latexname());
874 void InsetCollapsable::validate(LaTeXFeatures & features) const
876 string const preamble = getLayout().preamble();
877 if (!preamble.empty())
878 features.addPreambleSnippet(preamble);
879 features.require(getLayout().requires());
880 InsetText::validate(features);
884 bool InsetCollapsable::undefined() const
886 docstring const & n = getLayout().name();
887 return n.empty() || n == DocumentClass::plainInsetLayout().name();
891 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
894 if (decoration() == InsetLayout::CONGLOMERATE)
895 return from_ascii("context-conglomerate");
897 if (geometry(bv) == NoButton)
898 return from_ascii("context-collapsable");
900 Dimension dim = dimensionCollapsed(bv);
901 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
902 return from_ascii("context-collapsable");
904 return InsetText::contextMenu(bv, x, y);
907 void InsetCollapsable::tocString(odocstream & os) const
909 if (!getLayout().isInToc())
911 os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);