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"
49 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
51 if (decoration() == InsetLayout::CONGLOMERATE)
53 return auto_open_[&bv] ? Open : status_;
57 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
59 switch (decoration()) {
60 case InsetLayout::CLASSIC:
61 if (status(bv) == Open)
62 return openinlined_ ? LeftButton : TopButton;
65 case InsetLayout::MINIMALISTIC:
66 return status(bv) == Open ? NoButton : ButtonOnly ;
68 case InsetLayout::CONGLOMERATE:
69 return status(bv) == Open ? SubLabel : Corners ;
71 case InsetLayout::DEFAULT:
72 break; // this shouldn't happen
75 // dummy return value to shut down a warning,
81 InsetCollapsable::Geometry InsetCollapsable::geometry() const
83 switch (decoration()) {
84 case InsetLayout::CLASSIC:
86 return openinlined_ ? LeftButton : TopButton;
89 case InsetLayout::MINIMALISTIC:
90 return status_ == Open ? NoButton : ButtonOnly ;
92 case InsetLayout::CONGLOMERATE:
93 return status_ == Open ? SubLabel : Corners ;
95 case InsetLayout::DEFAULT:
96 break; // this shouldn't happen
99 // dummy return value to shut down a warning,
100 // this is dead code.
105 InsetCollapsable::InsetCollapsable(Buffer const & buf)
106 : InsetText(buf), status_(Inset::Open),
107 openinlined_(false), mouse_hover_(false)
109 DocumentClass const & dc = buf.params().documentClass();
111 setAutoBreakRows(true);
113 setFrameColor(Color_collapsableframe);
114 paragraphs().back().setPlainLayout(dc);
118 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
120 status_(rhs.status_),
121 layout_(rhs.layout_),
122 labelstring_(rhs.labelstring_),
123 button_dim(rhs.button_dim),
124 openinlined_(rhs.openinlined_),
125 auto_open_(rhs.auto_open_),
126 // the sole purpose of this copy constructor
132 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
134 Dimension dim = dimensionCollapsed(bv);
135 if (geometry(bv) == NoButton)
136 return translateIfPossible(layout_->labelstring());
137 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
140 OutputParams rp(&buffer().params().encoding());
141 odocstringstream ods;
142 InsetText::plaintext(ods, rp);
143 docstring content_tip = ods.str();
144 // shorten it if necessary
145 if (content_tip.size() > 200)
146 content_tip = content_tip.substr(0, 200) + "...";
151 void InsetCollapsable::setLayout(BufferParams const & bp)
153 setLayout(bp.documentClassPtr());
157 void InsetCollapsable::setLayout(DocumentClass const * const dc)
160 layout_ = &(dc->insetLayout(name()));
161 labelstring_ = translateIfPossible(layout_->labelstring());
163 layout_ = &DocumentClass::plainInsetLayout();
164 labelstring_ = _("UNDEFINED");
171 void InsetCollapsable::write(ostream & os) const
183 text().write(buffer(), os);
187 void InsetCollapsable::read(Lexer & lex)
189 lex.setContext("InsetCollapsable::read");
192 lex >> "status" >> tmp_token;
193 if (tmp_token == "open")
196 // this must be set before we enter InsetText::read()
197 setLayout(buffer().params());
198 InsetText::read(lex);
199 // set button label again as the inset contents was not read yet at
203 // Force default font, if so requested
204 // This avoids paragraphs in buffer language that would have a
205 // foreign language after a document language change, and it ensures
206 // that all new text in ERT and similar gets the "latex" language,
207 // since new text inherits the language from the last position of the
208 // existing text. As a side effect this makes us also robust against
209 // bugs in LyX that might lead to font changes in ERT in .lyx files.
210 resetParagraphsFont();
214 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
216 LASSERT(layout_, /**/);
218 theFontMetrics(layout_->labelfont()).buttonText(
219 buttonLabel(bv), dim.wid, dim.asc, dim.des);
224 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
226 LASSERT(layout_, /**/);
228 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
230 FontInfo tmpfont = mi.base.font;
231 mi.base.font = layout_->font();
232 mi.base.font.realize(tmpfont);
234 BufferView const & bv = *mi.base.bv;
236 switch (geometry(bv)) {
238 InsetText::metrics(mi, dim);
241 InsetText::metrics(mi, dim);
246 InsetText::metrics(mi, dim);
247 // consider width of the inset label
248 FontInfo font(layout_->labelfont());
249 font.realize(sane_font);
255 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
262 dim = dimensionCollapsed(bv);
263 if (geometry(bv) == TopButton
264 || geometry(bv) == LeftButton) {
266 InsetText::metrics(mi, textdim);
267 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
269 // Correct for button width.
270 dim.wid += textdim.wid;
271 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
272 dim.asc = textdim.asc;
274 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
275 dim.wid = max(dim.wid, textdim.wid);
281 mi.base.font = tmpfont;
285 bool InsetCollapsable::setMouseHover(bool mouse_hover)
287 mouse_hover_ = mouse_hover;
292 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
294 LASSERT(layout_, /**/);
295 BufferView const & bv = *pi.base.bv;
297 auto_open_[&bv] = bv.cursor().isInside(this);
299 FontInfo tmpfont = pi.base.font;
300 pi.base.font = layout_->font();
301 pi.base.font.realize(tmpfont);
303 // Draw button first -- top, left or only
304 Dimension dimc = dimensionCollapsed(bv);
306 if (geometry(*pi.base.bv) == TopButton ||
307 geometry(*pi.base.bv) == LeftButton ||
308 geometry(*pi.base.bv) == ButtonOnly) {
309 button_dim.x1 = x + 0;
310 button_dim.x2 = x + dimc.width();
311 button_dim.y1 = y - dimc.asc;
312 button_dim.y2 = y + dimc.des;
314 pi.pain.buttonText(x, y, buttonLabel(bv), layout_->labelfont(),
323 Dimension const textdim = InsetText::dimension(bv);
324 int const baseline = y;
326 switch (geometry(bv)) {
328 textx = x + dimc.width();
330 InsetText::draw(pi, textx, texty);
334 texty = baseline + dimc.des + textdim.asc;
335 InsetText::draw(pi, textx, texty);
342 InsetText::draw(pi, textx, texty);
348 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
349 InsetText::draw(pi, textx, texty);
350 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
352 int desc = textdim.descent();
353 if (geometry(bv) == Corners)
356 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
357 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
358 pi.pain.line(xx1, y + desc - 4,
360 layout_->labelfont().color());
362 pi.pain.line(xx1, y + desc,
364 layout_->labelfont().color());
366 // Make status_ value visible:
367 pi.pain.line(xx1, y + desc,
369 layout_->labelfont().color());
370 pi.pain.line(xx2 - 4, y + desc,
372 layout_->labelfont().color());
374 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
375 y + desc - 4, layout_->labelfont().color());
377 // the label below the text. Can be toggled.
378 if (geometry(bv) == SubLabel) {
379 FontInfo font(layout_->labelfont());
380 font.realize(sane_font);
386 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
387 int const ww = max(textdim.wid, w);
388 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
389 buttonLabel(bv), font, Color_none, Color_none);
393 // a visual cue when the cursor is inside the inset
394 Cursor const & cur = bv.cursor();
395 if (cur.isInside(this)) {
398 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
399 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
400 pi.pain.line(xx2, y + 4, xx2, y,
401 layout_->labelfont().color());
402 pi.pain.line(xx2 - 4, y, xx2, y,
403 layout_->labelfont().color());
408 pi.base.font = tmpfont;
412 void InsetCollapsable::cursorPos(BufferView const & bv,
413 CursorSlice const & sl, bool boundary, int & x, int & y) const
415 if (geometry(bv) == ButtonOnly)
417 LASSERT(geometry(bv) != ButtonOnly, /**/);
419 InsetText::cursorPos(bv, sl, boundary, x, y);
420 Dimension const textdim = InsetText::dimension(bv);
422 switch (geometry(bv)) {
424 x += dimensionCollapsed(bv).wid;
427 y += dimensionCollapsed(bv).des + textdim.asc;
442 Inset::EDITABLE InsetCollapsable::editable() const
444 return geometry() != ButtonOnly ? HIGHLY_EDITABLE : IS_EDITABLE;
448 bool InsetCollapsable::descendable() const
450 return geometry() != ButtonOnly;
454 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
456 return button_dim.contains(cmd.x, cmd.y);
460 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
463 pos_type const max_length = 15;
464 pos_type const p_siz = paragraphs().begin()->size();
465 pos_type const n = min(max_length, p_siz);
468 for (; i < n && j < p_siz; ++j) {
469 if (paragraphs().begin()->isInset(j))
471 label += paragraphs().begin()->getChar(j);
474 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
477 return label.empty() ? l : label;
481 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
483 //lyxerr << "InsetCollapsable: edit left/right" << endl;
485 InsetText::edit(cur, front, entry_from);
489 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
491 //lyxerr << "InsetCollapsable: edit xy" << endl;
492 if (geometry(cur.bv()) == ButtonOnly
493 || (button_dim.contains(x, y)
494 && geometry(cur.bv()) != NoButton))
497 return InsetText::editXY(cur, x, y);
501 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
503 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
504 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
506 switch (cmd.action) {
507 case LFUN_MOUSE_PRESS:
508 if (hitButton(cmd)) {
509 switch (cmd.button()) {
510 case mouse_button::button1:
511 case mouse_button::button3:
512 // Pass the command to the enclosing InsetText,
513 // so that the cursor gets set.
516 case mouse_button::none:
517 case mouse_button::button2:
518 case mouse_button::button4:
519 case mouse_button::button5:
524 } else if (geometry(cur.bv()) != ButtonOnly)
525 InsetText::doDispatch(cur, cmd);
530 case LFUN_MOUSE_MOTION:
531 case LFUN_MOUSE_DOUBLE:
532 case LFUN_MOUSE_TRIPLE:
535 else if (geometry(cur.bv()) != ButtonOnly)
536 InsetText::doDispatch(cur, cmd);
541 case LFUN_MOUSE_RELEASE:
542 if (!hitButton(cmd)) {
543 // The mouse click has to be within the inset!
544 if (geometry(cur.bv()) != ButtonOnly)
545 InsetText::doDispatch(cur, cmd);
550 if (cmd.button() != mouse_button::button1) {
555 // if we are selecting, we do not want to
559 // Left button is clicked, the user asks to
560 // toggle the inset visual state.
562 cur.updateFlags(Update::Force | Update::FitCursor);
563 if (geometry(cur.bv()) == ButtonOnly) {
564 setStatus(cur, Open);
568 setStatus(cur, Collapsed);
569 cur.bv().cursor() = cur;
572 case LFUN_INSET_TOGGLE:
573 if (cmd.argument() == "open")
574 setStatus(cur, Open);
575 else if (cmd.argument() == "close")
576 setStatus(cur, Collapsed);
577 else if (cmd.argument() == "toggle" || cmd.argument().empty())
578 if (status_ == Open) {
579 setStatus(cur, Collapsed);
580 if (geometry(cur.bv()) == ButtonOnly)
581 cur.top().forwardPos();
583 setStatus(cur, Open);
584 else // if assign or anything else
590 case LFUN_CLIPBOARD_PASTE:
591 case LFUN_PRIMARY_SELECTION_PASTE: {
592 InsetText::doDispatch(cur, cmd);
593 // Since we can only store plain text, we must reset all
595 // FIXME: Change only the pasted paragraphs
597 resetParagraphsFont();
602 if (layout_ && layout_->isForceLtr()) {
603 // Force any new text to latex_language
604 // FIXME: This should only be necessary in constructor, but
605 // new paragraphs that are created by pressing enter at the
606 // start of an existing paragraph get the buffer language
607 // and not latex_language, so we take this brute force
609 cur.current_font.setLanguage(latex_language);
610 cur.real_current_font.setLanguage(latex_language);
612 InsetText::doDispatch(cur, cmd);
618 bool InsetCollapsable::allowMultiPar() const
620 return layout_->isMultiPar();
624 void InsetCollapsable::resetParagraphsFont()
626 Font font(inherit_font, buffer().params().language);
627 if (layout_->isForceLtr())
628 font.setLanguage(latex_language);
629 if (layout_->isPassThru()) {
630 ParagraphList::iterator par = paragraphs().begin();
631 ParagraphList::iterator const end = paragraphs().end();
633 par->resetFonts(font);
634 par->params().clear();
641 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
642 FuncStatus & flag) const
644 switch (cmd.action) {
645 // FIXME At present, these are being enabled and disabled according to
646 // whether PASSTHRU has been set in the InsetLayout. This makes some
647 // sense, but there are other checks that should really be done. E.g.,
648 // one should not be able to inset IndexPrint inside an optional argument!!
649 case LFUN_ACCENT_ACUTE:
650 case LFUN_ACCENT_BREVE:
651 case LFUN_ACCENT_CARON:
652 case LFUN_ACCENT_CEDILLA:
653 case LFUN_ACCENT_CIRCLE:
654 case LFUN_ACCENT_CIRCUMFLEX:
655 case LFUN_ACCENT_DOT:
656 case LFUN_ACCENT_GRAVE:
657 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
658 case LFUN_ACCENT_MACRON:
659 case LFUN_ACCENT_OGONEK:
660 case LFUN_ACCENT_TIE:
661 case LFUN_ACCENT_TILDE:
662 case LFUN_ACCENT_UMLAUT:
663 case LFUN_ACCENT_UNDERBAR:
664 case LFUN_ACCENT_UNDERDOT:
666 case LFUN_BOX_INSERT:
667 case LFUN_BRANCH_INSERT:
668 case LFUN_NEWLINE_INSERT:
669 case LFUN_CAPTION_INSERT:
670 case LFUN_DEPTH_DECREMENT:
671 case LFUN_DEPTH_INCREMENT:
672 case LFUN_ERT_INSERT:
673 case LFUN_FILE_INSERT:
674 case LFUN_FLEX_INSERT:
675 case LFUN_FLOAT_INSERT:
676 case LFUN_FLOAT_LIST_INSERT:
677 case LFUN_FLOAT_WIDE_INSERT:
679 case LFUN_FONT_BOLDSYMBOL:
680 case LFUN_FONT_TYPEWRITER:
681 case LFUN_FONT_DEFAULT:
683 case LFUN_TEXTSTYLE_APPLY:
684 case LFUN_TEXTSTYLE_UPDATE:
686 case LFUN_FONT_ROMAN:
691 case LFUN_FONT_STATE:
692 case LFUN_FONT_UNDERLINE:
693 case LFUN_FOOTNOTE_INSERT:
694 case LFUN_HYPERLINK_INSERT:
695 case LFUN_INDEX_INSERT:
696 case LFUN_INDEX_PRINT:
697 case LFUN_INSET_INSERT:
698 case LFUN_LABEL_GOTO:
699 case LFUN_LABEL_INSERT:
700 case LFUN_LINE_INSERT:
701 case LFUN_NEWPAGE_INSERT:
702 case LFUN_LAYOUT_TABULAR:
703 case LFUN_MARGINALNOTE_INSERT:
704 case LFUN_MATH_DISPLAY:
705 case LFUN_MATH_INSERT:
706 case LFUN_MATH_MATRIX:
710 case LFUN_NOMENCL_INSERT:
711 case LFUN_NOMENCL_PRINT:
712 case LFUN_NOTE_INSERT:
714 case LFUN_OPTIONAL_INSERT:
715 case LFUN_PHANTOM_INSERT:
716 case LFUN_REFERENCE_NEXT:
717 case LFUN_SERVER_GOTO_FILE_ROW:
718 case LFUN_SERVER_NOTIFY:
719 case LFUN_SERVER_SET_XY:
720 case LFUN_SPACE_INSERT:
721 case LFUN_SPECIALCHAR_INSERT:
722 case LFUN_TABULAR_INSERT:
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);