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 "FuncStatus.h"
25 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "TextClass.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
37 #include "support/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
46 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
48 return autoOpen_ ? Open : status_;
52 InsetCollapsable::Geometry InsetCollapsable::geometry() const
54 switch (decoration()) {
55 case InsetLayout::Classic:
56 if (status() == Open) {
64 case InsetLayout::Minimalistic:
65 return status() == Open ? NoButton : ButtonOnly ;
67 case InsetLayout::Conglomerate:
68 return status() == Open ? SubLabel : Corners ;
70 case InsetLayout::Default:
71 break; // this shouldn't happen
74 // dummy return value to shut down a warning,
80 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
81 CollapseStatus status, TextClassPtr tc)
82 : InsetText(bp), status_(status),
83 openinlined_(false), autoOpen_(false), mouse_hover_(false)
86 setAutoBreakRows(true);
88 setFrameColor(Color_collapsableframe);
89 paragraphs().back().layout(bp.getTextClass().emptyLayout());
93 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
95 textClass_(rhs.textClass_),
97 labelstring_(rhs.labelstring_),
98 button_dim(rhs.button_dim),
100 openinlined_(rhs.openinlined_),
101 autoOpen_(rhs.autoOpen_),
102 // the sole purpose of this copy constructor
108 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
110 Dimension dim = dimensionCollapsed();
111 if (geometry() == NoButton)
112 return layout_->labelstring();
113 else if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
118 return _("Left-click to collapse the inset");
120 return _("Left-click to open the inset");
126 void InsetCollapsable::setLayout(BufferParams const & bp)
128 setLayout(bp.getTextClassPtr());
132 void InsetCollapsable::setLayout(TextClassPtr tc)
135 if ( tc.get() != 0 ) {
136 layout_ = &tc->insetlayout(name());
137 labelstring_ = layout_->labelstring();
139 layout_ = &TextClass::emptyInsetLayout();
140 labelstring_ = _("UNDEFINED");
147 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
159 text_.write(buf, os);
163 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
165 bool token_found = false;
168 string const token = lex.getString();
169 if (token == "status") {
171 string const tmp_token = lex.getString();
173 if (tmp_token == "collapsed") {
176 } else if (tmp_token == "open") {
180 lyxerr << "InsetCollapsable::read: Missing status!"
182 // Take countermeasures
183 lex.pushToken(token);
186 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
187 // take countermeasures
188 lex.pushToken(token);
191 //this must be set before we enter InsetText::read()
192 setLayout(buf.params());
194 InsetText::read(buf, lex);
197 status_ = isOpen() ? Open : Collapsed;
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() const
212 BOOST_ASSERT(layout_);
214 theFontMetrics(layout_->labelfont()).buttonText(
215 labelstring_, dim.wid, dim.asc, dim.des);
220 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
222 BOOST_ASSERT(layout_);
224 autoOpen_ = 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 switch (geometry()) {
232 InsetText::metrics(mi, dim);
235 InsetText::metrics(mi, dim);
240 InsetText::metrics(mi, dim);
241 // consider width of the inset label
242 FontInfo font(layout_->labelfont());
243 font.realize(sane_font);
249 theFontMetrics(font).rectText(labelstring_, w, a, d);
256 dim = dimensionCollapsed();
257 if (geometry() == TopButton
258 || geometry() == LeftButton) {
260 InsetText::metrics(mi, textdim);
261 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
263 // Correct for button width.
264 dim.wid += textdim.wid;
265 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
266 dim.asc = textdim.asc;
268 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
269 dim.wid = max(dim.wid, textdim.wid);
275 mi.base.font = tmpfont;
279 bool InsetCollapsable::setMouseHover(bool mouse_hover)
281 mouse_hover_ = mouse_hover;
286 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
288 BOOST_ASSERT(layout_);
290 autoOpen_ = pi.base.bv->cursor().isInside(this);
291 ColorCode const old_color = pi.background_color;
292 pi.background_color = backgroundColor();
294 FontInfo tmpfont = pi.base.font;
295 pi.base.font = layout_->font();
296 pi.base.font.realize(tmpfont);
298 // Draw button first -- top, left or only
299 Dimension dimc = dimensionCollapsed();
301 if (geometry() == TopButton ||
302 geometry() == LeftButton ||
303 geometry() == ButtonOnly) {
304 button_dim.x1 = x + 0;
305 button_dim.x2 = x + dimc.width();
306 button_dim.y1 = y - dimc.asc;
307 button_dim.y2 = y + dimc.des;
309 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
318 Dimension const textdim = InsetText::dimension(*pi.base.bv);
319 int const baseline = y;
321 switch (geometry()) {
323 textx = x + dimc.width();
325 InsetText::draw(pi, textx, texty);
329 texty = baseline + dimc.des + textdim.asc;
330 InsetText::draw(pi, textx, texty);
337 InsetText::draw(pi, textx, texty);
343 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
344 InsetText::draw(pi, textx, texty);
345 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
347 int desc = textdim.descent();
348 if (geometry() == Corners)
351 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
352 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
353 pi.pain.line(xx1, y + desc - 4,
355 layout_->labelfont().color());
357 pi.pain.line(xx1, y + desc,
359 layout_->labelfont().color());
361 // Make status_ value visible:
362 pi.pain.line(xx1, y + desc,
364 layout_->labelfont().color());
365 pi.pain.line(xx2 - 4, y + desc,
367 layout_->labelfont().color());
369 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
370 layout_->labelfont().color());
372 // the label below the text. Can be toggled.
373 if (geometry() == SubLabel) {
374 FontInfo font(layout_->labelfont());
375 font.realize(sane_font);
381 theFontMetrics(font).rectText(labelstring_, w, a, d);
382 int const ww = max(textdim.wid, w);
383 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
384 labelstring_, font, Color_none, Color_none);
388 // a visual cue when the cursor is inside the inset
389 Cursor & cur = pi.base.bv->cursor();
390 if (cur.isInside(this)) {
393 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
394 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
395 pi.pain.line(xx2, y + 4, xx2, y,
396 layout_->labelfont().color());
397 pi.pain.line(xx2 - 4, y, xx2, y,
398 layout_->labelfont().color());
402 pi.background_color = old_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() == ButtonOnly)
413 BOOST_ASSERT(geometry() != ButtonOnly);
415 InsetText::cursorPos(bv, sl, boundary, x, y);
416 Dimension const textdim = InsetText::dimension(bv);
418 switch (geometry()) {
420 x += dimensionCollapsed().wid;
423 y += dimensionCollapsed().des + textdim.asc;
438 Inset::EDITABLE InsetCollapsable::editable() const
440 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
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() == ButtonOnly
489 || (button_dim.contains(x, y)
490 && geometry() != 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 (cmd.button() == mouse_button::button1
506 && geometry() != NoButton) {
507 // reset selection if necessary (see bug 3060)
509 cur.bv().cursor().clearSelection();
515 if (geometry() == NoButton)
516 InsetText::doDispatch(cur, cmd);
517 else if (geometry() != ButtonOnly
519 InsetText::doDispatch(cur, cmd);
524 case LFUN_MOUSE_MOTION:
525 case LFUN_MOUSE_DOUBLE:
526 case LFUN_MOUSE_TRIPLE:
527 if (geometry() == NoButton)
528 InsetText::doDispatch(cur, cmd);
529 else if (geometry() != ButtonOnly
531 InsetText::doDispatch(cur, cmd);
536 case LFUN_MOUSE_RELEASE:
537 if (cmd.button() == mouse_button::button3) {
538 // There is no button to right click:
539 if (decoration() == InsetLayout::Minimalistic ||
540 geometry() == Corners ||
541 geometry() == SubLabel ||
542 geometry() == NoButton
545 setStatus(cur, Collapsed);
547 setStatus(cur, Open);
551 // configuration dialog
552 showInsetDialog(&cur.bv());
557 if (geometry() == NoButton) {
558 // The mouse click has to be within the inset!
559 InsetText::doDispatch(cur, cmd);
563 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
564 // if we are selecting, we do not want to
568 // Left button is clicked, the user asks to
569 // toggle the inset visual state.
571 cur.updateFlags(Update::Force | Update::FitCursor);
572 if (geometry() == ButtonOnly) {
573 setStatus(cur, Open);
577 setStatus(cur, Collapsed);
579 cur.bv().cursor() = cur;
583 // The mouse click is within the opened inset.
584 if (geometry() == TopButton
585 || geometry() == LeftButton)
586 InsetText::doDispatch(cur, cmd);
589 case LFUN_INSET_TOGGLE:
590 if (cmd.argument() == "open")
591 setStatus(cur, Open);
592 else if (cmd.argument() == "close")
593 setStatus(cur, Collapsed);
594 else if (cmd.argument() == "toggle" || cmd.argument().empty())
595 if (status_ == Open) {
596 setStatus(cur, Collapsed);
597 if (geometry() == ButtonOnly)
598 cur.top().forwardPos();
600 setStatus(cur, Open);
601 else // if assign or anything else
607 case LFUN_CLIPBOARD_PASTE:
608 case LFUN_PRIMARY_SELECTION_PASTE: {
609 InsetText::doDispatch(cur, cmd);
610 // Since we can only store plain text, we must reset all
612 // FIXME: Change only the pasted paragraphs
614 resetParagraphsFont();
619 if (layout_ && layout_->isForceLtr()) {
620 // Force any new text to latex_language
621 // FIXME: This should only be necessary in constructor, but
622 // new paragraphs that are created by pressing enter at the
623 // start of an existing paragraph get the buffer language
624 // and not latex_language, so we take this brute force
626 cur.current_font.setLanguage(latex_language);
627 cur.real_current_font.setLanguage(latex_language);
629 InsetText::doDispatch(cur, cmd);
635 bool InsetCollapsable::allowMultiPar() const
637 return layout_->isMultiPar();
641 void InsetCollapsable::resetParagraphsFont()
644 font.fontInfo() = inherit_font;
645 if (layout_->isForceLtr())
646 font.setLanguage(latex_language);
647 if (layout_->isPassThru()) {
648 ParagraphList::iterator par = paragraphs().begin();
649 ParagraphList::iterator const end = paragraphs().end();
651 par->resetFonts(font);
652 par->params().clear();
659 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
660 FuncStatus & flag) const
662 switch (cmd.action) {
664 case LFUN_ACCENT_ACUTE:
665 case LFUN_ACCENT_BREVE:
666 case LFUN_ACCENT_CARON:
667 case LFUN_ACCENT_CEDILLA:
668 case LFUN_ACCENT_CIRCLE:
669 case LFUN_ACCENT_CIRCUMFLEX:
670 case LFUN_ACCENT_DOT:
671 case LFUN_ACCENT_GRAVE:
672 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
673 case LFUN_ACCENT_MACRON:
674 case LFUN_ACCENT_OGONEK:
675 case LFUN_ACCENT_SPECIAL_CARON:
676 case LFUN_ACCENT_TIE:
677 case LFUN_ACCENT_TILDE:
678 case LFUN_ACCENT_UMLAUT:
679 case LFUN_ACCENT_UNDERBAR:
680 case LFUN_ACCENT_UNDERDOT:
682 case LFUN_BIBITEM_INSERT:
683 case LFUN_BOX_INSERT:
684 case LFUN_BRANCH_INSERT:
686 case LFUN_CAPTION_INSERT:
687 case LFUN_CLEARPAGE_INSERT:
688 case LFUN_CLEARDOUBLEPAGE_INSERT:
689 case LFUN_DEPTH_DECREMENT:
690 case LFUN_DEPTH_INCREMENT:
691 case LFUN_ENVIRONMENT_INSERT:
692 case LFUN_ERT_INSERT:
693 case LFUN_FILE_INSERT:
694 case LFUN_FLEX_INSERT:
695 case LFUN_FLOAT_INSERT:
696 case LFUN_FLOAT_LIST:
697 case LFUN_FLOAT_WIDE_INSERT:
699 case LFUN_FONT_TYPEWRITER:
700 case LFUN_FONT_DEFAULT:
702 case LFUN_FONT_FREE_APPLY:
703 case LFUN_FONT_FREE_UPDATE:
705 case LFUN_FONT_ROMAN:
710 case LFUN_FONT_STATE:
711 case LFUN_FONT_UNDERLINE:
712 case LFUN_FOOTNOTE_INSERT:
713 case LFUN_HFILL_INSERT:
714 case LFUN_HYPERLINK_INSERT:
715 case LFUN_INDEX_INSERT:
716 case LFUN_INDEX_PRINT:
717 case LFUN_INSET_INSERT:
718 case LFUN_LABEL_GOTO:
719 case LFUN_LABEL_INSERT:
720 case LFUN_LINE_INSERT:
721 case LFUN_NEWPAGE_INSERT:
722 case LFUN_PAGEBREAK_INSERT:
724 case LFUN_LAYOUT_PARAGRAPH:
725 case LFUN_LAYOUT_TABULAR:
726 case LFUN_MARGINALNOTE_INSERT:
727 case LFUN_MATH_DISPLAY:
728 case LFUN_MATH_INSERT:
729 case LFUN_MATH_MATRIX:
733 case LFUN_NOMENCL_INSERT:
734 case LFUN_NOMENCL_PRINT:
735 case LFUN_NOTE_INSERT:
737 case LFUN_OPTIONAL_INSERT:
738 case LFUN_PARAGRAPH_PARAMS:
739 case LFUN_PARAGRAPH_PARAMS_APPLY:
740 case LFUN_PARAGRAPH_SPACING:
741 case LFUN_PARAGRAPH_UPDATE:
742 case LFUN_REFERENCE_NEXT:
743 case LFUN_SERVER_GOTO_FILE_ROW:
744 case LFUN_SERVER_NOTIFY:
745 case LFUN_SERVER_SET_XY:
746 case LFUN_SPACE_INSERT:
747 case LFUN_SPECIALCHAR_INSERT:
748 case LFUN_TABULAR_INSERT:
749 case LFUN_TOC_INSERT:
750 case LFUN_WRAP_INSERT:
751 if (layout_->isPassThru()) {
755 return InsetText::getStatus(cur, cmd, flag);
757 case LFUN_INSET_TOGGLE:
758 if (cmd.argument() == "open" || cmd.argument() == "close" ||
759 cmd.argument() == "toggle")
766 flag.enabled(!layout_->isForceLtr());
767 return InsetText::getStatus(cur, cmd, flag);
769 case LFUN_BREAK_PARAGRAPH:
770 case LFUN_BREAK_PARAGRAPH_SKIP:
771 flag.enabled(layout_->isMultiPar());
775 return InsetText::getStatus(cur, cmd, flag);
780 void InsetCollapsable::setLabel(docstring const & l)
786 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
790 if (status_ == Collapsed)
791 cur.leaveInset(*this);
795 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
797 FloatList const & floats = bp.getTextClass().floats();
798 FloatList::const_iterator it = floats[type];
800 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
804 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
807 return InsetLayout::Classic;
808 InsetLayout::InsetDecoration const dec = layout_->decoration();
810 case InsetLayout::Classic:
811 case InsetLayout::Minimalistic:
812 case InsetLayout::Conglomerate:
814 case InsetLayout::Default:
817 if (lyxCode() == FLEX_CODE)
818 // FIXME: Is this really necessary?
819 return InsetLayout::Conglomerate;
820 return InsetLayout::Classic;
824 bool InsetCollapsable::isMacroScope(Buffer const &) const
826 // layout_ == 0 leads to no latex output, so ignore
827 // the macros outside
831 // see InsetCollapsable::latex(...) below. In these case
832 // an environment is opened there
833 if (!layout_->latexname().empty())
840 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
841 OutputParams const & runparams) const
843 // FIXME: What should we do layout_ is 0?
845 // 2) through an error
849 // This implements the standard way of handling the LaTeX output of
850 // a collapsable inset, either a command or an environment. Standard
851 // collapsable insets should not redefine this, non-standard ones may
853 if (!layout_->latexname().empty()) {
854 if (layout_->latextype() == "command") {
856 if (runparams.moving_arg)
858 os << '\\' << from_utf8(layout_->latexname());
859 if (!layout_->latexparam().empty())
860 os << from_utf8(layout_->latexparam());
862 } else if (layout_->latextype() == "environment") {
863 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
864 if (!layout_->latexparam().empty())
865 os << from_utf8(layout_->latexparam());
868 OutputParams rp = runparams;
869 if (layout_->isPassThru())
871 if (layout_->isNeedProtect())
872 rp.moving_arg = true;
873 int i = InsetText::latex(buf, os, rp);
874 if (!layout_->latexname().empty()) {
875 if (layout_->latextype() == "command") {
877 } else if (layout_->latextype() == "environment") {
878 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
886 void InsetCollapsable::validate(LaTeXFeatures & features) const
891 // Force inclusion of preamble snippet in layout file
892 features.require(to_utf8(layout_->name()));
893 InsetText::validate(features);
897 bool InsetCollapsable::undefined() const
899 docstring const & n = getLayout().name();
900 return n.empty() || n == TextClass::emptyInsetLayout().name();