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/lassert.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
49 if (decoration() == InsetLayout::Conglomerate)
51 return autoOpen_ ? Open : status_;
55 InsetCollapsable::Geometry InsetCollapsable::geometry() const
57 switch (decoration()) {
58 case InsetLayout::Classic:
60 return openinlined_ ? LeftButton : TopButton;
63 case InsetLayout::Minimalistic:
64 return status() == Open ? NoButton : ButtonOnly ;
66 case InsetLayout::Conglomerate:
67 return status() == Open ? SubLabel : Corners ;
69 case InsetLayout::Default:
70 break; // this shouldn't happen
73 // dummy return value to shut down a warning,
79 InsetCollapsable::InsetCollapsable(Buffer const & buf,
80 CollapseStatus status)
81 : InsetText(buf), status_(status),
82 openinlined_(false), autoOpen_(false), mouse_hover_(false)
84 DocumentClass const & dc = buf.params().documentClass();
86 setAutoBreakRows(true);
88 setFrameColor(Color_collapsableframe);
89 paragraphs().back().setLayout(dc.emptyLayout());
93 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
96 labelstring_(rhs.labelstring_),
97 button_dim(rhs.button_dim),
99 openinlined_(rhs.openinlined_),
100 autoOpen_(rhs.autoOpen_),
101 // the sole purpose of this copy constructor
107 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
109 Dimension dim = dimensionCollapsed();
110 if (geometry() == NoButton)
111 return translateIfPossible(layout_->labelstring());
112 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
115 docstring default_tip;
118 default_tip = _("Left-click to collapse the inset");
121 default_tip = _("Left-click to open the inset");
125 OutputParams rp(&buffer().params().encoding());
126 odocstringstream ods;
127 InsetText::plaintext(ods, rp);
128 docstring content_tip = ods.str();
129 // shorten it if necessary
130 if (content_tip.size() > 200)
131 content_tip = content_tip.substr(0, 200) + "...";
132 if (!isOpen() && !content_tip.empty())
133 return content_tip + '\n' + default_tip;
138 void InsetCollapsable::setLayout(BufferParams const & bp)
140 setLayout(bp.documentClassPtr());
144 void InsetCollapsable::setLayout(DocumentClass const * const dc)
147 layout_ = &(dc->insetLayout(name()));
148 labelstring_ = translateIfPossible(layout_->labelstring());
150 layout_ = &DocumentClass::emptyInsetLayout();
151 labelstring_ = _("UNDEFINED");
158 void InsetCollapsable::write(ostream & os) const
170 text_.write(buffer(), os);
174 void InsetCollapsable::read(Lexer & lex)
176 lex.setContext("InsetCollapsable::read");
179 lex >> "status" >> tmp_token;
180 if (tmp_token == "open")
183 // this must be set before we enter InsetText::read()
184 setLayout(buffer().params());
186 InsetText::read(lex);
188 // Force default font, if so requested
189 // This avoids paragraphs in buffer language that would have a
190 // foreign language after a document language change, and it ensures
191 // that all new text in ERT and similar gets the "latex" language,
192 // since new text inherits the language from the last position of the
193 // existing text. As a side effect this makes us also robust against
194 // bugs in LyX that might lead to font changes in ERT in .lyx files.
195 resetParagraphsFont();
199 Dimension InsetCollapsable::dimensionCollapsed() const
201 LASSERT(layout_, /**/);
203 theFontMetrics(layout_->labelfont()).buttonText(
204 labelstring_, dim.wid, dim.asc, dim.des);
209 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
211 LASSERT(layout_, /**/);
213 autoOpen_ = mi.base.bv->cursor().isInside(this);
215 FontInfo tmpfont = mi.base.font;
216 mi.base.font = layout_->font();
217 mi.base.font.realize(tmpfont);
219 switch (geometry()) {
221 InsetText::metrics(mi, dim);
224 InsetText::metrics(mi, dim);
229 InsetText::metrics(mi, dim);
230 // consider width of the inset label
231 FontInfo font(layout_->labelfont());
232 font.realize(sane_font);
238 theFontMetrics(font).rectText(labelstring_, w, a, d);
245 dim = dimensionCollapsed();
246 if (geometry() == TopButton || geometry() == LeftButton) {
248 InsetText::metrics(mi, textdim);
249 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
251 // Correct for button width.
252 dim.wid += textdim.wid;
253 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
254 dim.asc = textdim.asc;
256 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
257 dim.wid = max(dim.wid, textdim.wid);
263 mi.base.font = tmpfont;
267 bool InsetCollapsable::setMouseHover(bool mouse_hover)
269 mouse_hover_ = mouse_hover;
274 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
276 LASSERT(layout_, /**/);
278 autoOpen_ = pi.base.bv->cursor().isInside(this);
279 ColorCode const old_color = pi.background_color;
280 pi.background_color = backgroundColor();
282 FontInfo tmpfont = pi.base.font;
283 pi.base.font = layout_->font();
284 pi.base.font.realize(tmpfont);
286 // Draw button first -- top, left or only
287 Dimension dimc = dimensionCollapsed();
289 if (geometry() == TopButton ||
290 geometry() == LeftButton ||
291 geometry() == ButtonOnly) {
292 button_dim.x1 = x + 0;
293 button_dim.x2 = x + dimc.width();
294 button_dim.y1 = y - dimc.asc;
295 button_dim.y2 = y + dimc.des;
297 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
306 Dimension const textdim = InsetText::dimension(*pi.base.bv);
307 int const baseline = y;
309 switch (geometry()) {
311 textx = x + dimc.width();
313 InsetText::draw(pi, textx, texty);
317 texty = baseline + dimc.des + textdim.asc;
318 InsetText::draw(pi, textx, texty);
325 InsetText::draw(pi, textx, texty);
331 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
332 InsetText::draw(pi, textx, texty);
333 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
335 int desc = textdim.descent();
336 if (geometry() == Corners)
339 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
340 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
341 pi.pain.line(xx1, y + desc - 4,
343 layout_->labelfont().color());
345 pi.pain.line(xx1, y + desc,
347 layout_->labelfont().color());
349 // Make status_ value visible:
350 pi.pain.line(xx1, y + desc,
352 layout_->labelfont().color());
353 pi.pain.line(xx2 - 4, y + desc,
355 layout_->labelfont().color());
357 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
358 y + desc - 4, layout_->labelfont().color());
360 // the label below the text. Can be toggled.
361 if (geometry() == SubLabel) {
362 FontInfo font(layout_->labelfont());
363 font.realize(sane_font);
369 theFontMetrics(font).rectText(labelstring_, w, a, d);
370 int const ww = max(textdim.wid, w);
371 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
372 labelstring_, font, Color_none, Color_none);
376 // a visual cue when the cursor is inside the inset
377 Cursor & cur = pi.base.bv->cursor();
378 if (cur.isInside(this)) {
381 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
382 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
383 pi.pain.line(xx2, y + 4, xx2, y,
384 layout_->labelfont().color());
385 pi.pain.line(xx2 - 4, y, xx2, y,
386 layout_->labelfont().color());
390 pi.background_color = old_color;
392 pi.base.font = tmpfont;
396 void InsetCollapsable::cursorPos(BufferView const & bv,
397 CursorSlice const & sl, bool boundary, int & x, int & y) const
399 if (geometry() == ButtonOnly)
401 LASSERT(geometry() != ButtonOnly, /**/);
403 InsetText::cursorPos(bv, sl, boundary, x, y);
404 Dimension const textdim = InsetText::dimension(bv);
406 switch (geometry()) {
408 x += dimensionCollapsed().wid;
411 y += dimensionCollapsed().des + textdim.asc;
426 Inset::EDITABLE InsetCollapsable::editable() const
428 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
432 bool InsetCollapsable::descendable() const
434 return geometry() != ButtonOnly;
438 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
440 return button_dim.contains(cmd.x, cmd.y);
444 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
447 pos_type const max_length = 15;
448 pos_type const p_siz = paragraphs().begin()->size();
449 pos_type const n = min(max_length, p_siz);
452 for (; i < n && j < p_siz; ++j) {
453 if (paragraphs().begin()->isInset(j))
455 label += paragraphs().begin()->getChar(j);
458 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
461 return label.empty() ? l : label;
465 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
467 //lyxerr << "InsetCollapsable: edit left/right" << endl;
469 InsetText::edit(cur, front, entry_from);
473 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
475 //lyxerr << "InsetCollapsable: edit xy" << endl;
476 if (geometry() == ButtonOnly
477 || (button_dim.contains(x, y)
478 && geometry() != NoButton))
481 return InsetText::editXY(cur, x, y);
485 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
487 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
488 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
490 switch (cmd.action) {
491 case LFUN_MOUSE_PRESS:
492 if (hitButton(cmd) && geometry() != NoButton) {
493 switch (cmd.button()) {
494 case mouse_button::button1:
495 // reset selection if necessary (see bug 3060)
497 cur.bv().cursor().clearSelection();
502 case mouse_button::none:
503 case mouse_button::button2:
504 case mouse_button::button3:
505 case mouse_button::button4:
506 case mouse_button::button5:
512 if (geometry() == NoButton
513 || (geometry() != ButtonOnly && !hitButton(cmd)))
514 InsetText::doDispatch(cur, cmd);
519 case LFUN_MOUSE_MOTION:
520 case LFUN_MOUSE_DOUBLE:
521 case LFUN_MOUSE_TRIPLE:
522 if (geometry() == NoButton)
523 InsetText::doDispatch(cur, cmd);
524 else if (geometry() != ButtonOnly
526 InsetText::doDispatch(cur, cmd);
531 case LFUN_MOUSE_RELEASE:
532 if (geometry() == NoButton || !hitButton(cmd)) {
533 // The mouse click has to be within the inset!
534 InsetText::doDispatch(cur, cmd);
537 if (cmd.button() != mouse_button::button1) {
541 // if we are selecting, we do not want to
545 // Left button is clicked, the user asks to
546 // toggle the inset visual state.
548 cur.updateFlags(Update::Force | Update::FitCursor);
549 if (geometry() == ButtonOnly) {
550 setStatus(cur, Open);
554 setStatus(cur, Collapsed);
555 cur.bv().cursor() = cur;
558 case LFUN_INSET_TOGGLE:
559 if (cmd.argument() == "open")
560 setStatus(cur, Open);
561 else if (cmd.argument() == "close")
562 setStatus(cur, Collapsed);
563 else if (cmd.argument() == "toggle" || cmd.argument().empty())
564 if (status_ == Open) {
565 setStatus(cur, Collapsed);
566 if (geometry() == ButtonOnly)
567 cur.top().forwardPos();
569 setStatus(cur, Open);
570 else // if assign or anything else
576 case LFUN_CLIPBOARD_PASTE:
577 case LFUN_PRIMARY_SELECTION_PASTE: {
578 InsetText::doDispatch(cur, cmd);
579 // Since we can only store plain text, we must reset all
581 // FIXME: Change only the pasted paragraphs
583 resetParagraphsFont();
588 if (layout_ && layout_->isForceLtr()) {
589 // Force any new text to latex_language
590 // FIXME: This should only be necessary in constructor, but
591 // new paragraphs that are created by pressing enter at the
592 // start of an existing paragraph get the buffer language
593 // and not latex_language, so we take this brute force
595 cur.current_font.setLanguage(latex_language);
596 cur.real_current_font.setLanguage(latex_language);
598 InsetText::doDispatch(cur, cmd);
604 bool InsetCollapsable::allowMultiPar() const
606 return layout_->isMultiPar();
610 void InsetCollapsable::resetParagraphsFont()
613 font.fontInfo() = inherit_font;
614 if (layout_->isForceLtr())
615 font.setLanguage(latex_language);
616 if (layout_->isPassThru()) {
617 ParagraphList::iterator par = paragraphs().begin();
618 ParagraphList::iterator const end = paragraphs().end();
620 par->resetFonts(font);
621 par->params().clear();
628 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
629 FuncStatus & flag) const
631 switch (cmd.action) {
633 case LFUN_ACCENT_ACUTE:
634 case LFUN_ACCENT_BREVE:
635 case LFUN_ACCENT_CARON:
636 case LFUN_ACCENT_CEDILLA:
637 case LFUN_ACCENT_CIRCLE:
638 case LFUN_ACCENT_CIRCUMFLEX:
639 case LFUN_ACCENT_DOT:
640 case LFUN_ACCENT_GRAVE:
641 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
642 case LFUN_ACCENT_MACRON:
643 case LFUN_ACCENT_OGONEK:
644 case LFUN_ACCENT_TIE:
645 case LFUN_ACCENT_TILDE:
646 case LFUN_ACCENT_UMLAUT:
647 case LFUN_ACCENT_UNDERBAR:
648 case LFUN_ACCENT_UNDERDOT:
650 case LFUN_BIBITEM_INSERT:
651 case LFUN_BOX_INSERT:
652 case LFUN_BRANCH_INSERT:
653 case LFUN_NEWLINE_INSERT:
654 case LFUN_CAPTION_INSERT:
655 case LFUN_DEPTH_DECREMENT:
656 case LFUN_DEPTH_INCREMENT:
657 case LFUN_ENVIRONMENT_INSERT:
658 case LFUN_ERT_INSERT:
659 case LFUN_FILE_INSERT:
660 case LFUN_FLEX_INSERT:
661 case LFUN_FLOAT_INSERT:
662 case LFUN_FLOAT_LIST:
663 case LFUN_FLOAT_WIDE_INSERT:
665 case LFUN_FONT_TYPEWRITER:
666 case LFUN_FONT_DEFAULT:
668 case LFUN_FONT_FREE_APPLY:
669 case LFUN_FONT_FREE_UPDATE:
671 case LFUN_FONT_ROMAN:
676 case LFUN_FONT_STATE:
677 case LFUN_FONT_UNDERLINE:
678 case LFUN_FOOTNOTE_INSERT:
679 case LFUN_HYPERLINK_INSERT:
680 case LFUN_INDEX_INSERT:
681 case LFUN_INDEX_PRINT:
682 case LFUN_INSET_INSERT:
683 case LFUN_LABEL_GOTO:
684 case LFUN_LABEL_INSERT:
685 case LFUN_LINE_INSERT:
686 case LFUN_NEWPAGE_INSERT:
688 case LFUN_LAYOUT_PARAGRAPH:
689 case LFUN_LAYOUT_TABULAR:
690 case LFUN_MARGINALNOTE_INSERT:
691 case LFUN_MATH_DISPLAY:
692 case LFUN_MATH_INSERT:
693 case LFUN_MATH_MATRIX:
697 case LFUN_NOMENCL_INSERT:
698 case LFUN_NOMENCL_PRINT:
699 case LFUN_NOTE_INSERT:
701 case LFUN_OPTIONAL_INSERT:
702 case LFUN_PARAGRAPH_PARAMS:
703 case LFUN_PARAGRAPH_PARAMS_APPLY:
704 case LFUN_PARAGRAPH_SPACING:
705 case LFUN_PARAGRAPH_UPDATE:
706 case LFUN_REFERENCE_NEXT:
707 case LFUN_SERVER_GOTO_FILE_ROW:
708 case LFUN_SERVER_NOTIFY:
709 case LFUN_SERVER_SET_XY:
710 case LFUN_SPACE_INSERT:
711 case LFUN_SPECIALCHAR_INSERT:
712 case LFUN_TABULAR_INSERT:
713 case LFUN_TOC_INSERT:
714 case LFUN_WRAP_INSERT:
715 if (layout_->isPassThru()) {
719 return InsetText::getStatus(cur, cmd, flag);
721 case LFUN_INSET_TOGGLE:
722 if (cmd.argument() == "open")
723 flag.enabled(status_ != Open);
724 else if (cmd.argument() == "close")
725 flag.enabled(status_ == Open);
726 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
728 flag.setOnOff(status_ == Open);
734 flag.enabled(!layout_->isForceLtr());
735 return InsetText::getStatus(cur, cmd, flag);
737 case LFUN_BREAK_PARAGRAPH:
738 case LFUN_BREAK_PARAGRAPH_SKIP:
739 flag.enabled(layout_->isMultiPar());
743 return InsetText::getStatus(cur, cmd, flag);
748 void InsetCollapsable::setLabel(docstring const & l)
754 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
758 if (status_ == Collapsed) {
759 cur.leaveInset(*this);
760 mouse_hover_ = false;
765 docstring InsetCollapsable::floatName(
766 string const & type, BufferParams const & bp) const
768 FloatList const & floats = bp.documentClass().floats();
769 FloatList::const_iterator it = floats[type];
771 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
775 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
778 return InsetLayout::Classic;
779 InsetLayout::InsetDecoration const dec = layout_->decoration();
781 case InsetLayout::Classic:
782 case InsetLayout::Minimalistic:
783 case InsetLayout::Conglomerate:
785 case InsetLayout::Default:
788 if (lyxCode() == FLEX_CODE)
789 return InsetLayout::Conglomerate;
790 return InsetLayout::Classic;
794 int InsetCollapsable::latex(odocstream & os,
795 OutputParams const & runparams) const
797 // FIXME: What should we do layout_ is 0?
799 // 2) through an error
803 // This implements the standard way of handling the LaTeX output of
804 // a collapsable inset, either a command or an environment. Standard
805 // collapsable insets should not redefine this, non-standard ones may
807 if (!layout_->latexname().empty()) {
808 if (layout_->latextype() == "command") {
810 if (runparams.moving_arg)
812 os << '\\' << from_utf8(layout_->latexname());
813 if (!layout_->latexparam().empty())
814 os << from_utf8(layout_->latexparam());
816 } else if (layout_->latextype() == "environment") {
817 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
818 if (!layout_->latexparam().empty())
819 os << from_utf8(layout_->latexparam());
822 OutputParams rp = runparams;
823 if (layout_->isPassThru())
825 if (layout_->isNeedProtect())
826 rp.moving_arg = true;
827 int i = InsetText::latex(os, rp);
828 if (!layout_->latexname().empty()) {
829 if (layout_->latextype() == "command") {
831 } else if (layout_->latextype() == "environment") {
832 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
840 void InsetCollapsable::validate(LaTeXFeatures & features) const
845 // Force inclusion of preamble snippet in layout file
846 features.require(to_utf8(layout_->name()));
847 InsetText::validate(features);
851 bool InsetCollapsable::undefined() const
853 docstring const & n = getLayout().name();
854 return n.empty() || n == DocumentClass::emptyInsetLayout().name();
858 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
861 if (decoration() == InsetLayout::Conglomerate)
862 return from_ascii("context-conglomerate");
864 if (geometry() == NoButton)
865 return from_ascii("context-collapsable");
867 Dimension dim = dimensionCollapsed();
868 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
869 return from_ascii("context-collapsable");
871 return InsetText::contextMenu(bv, x, y);