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 "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"
40 #include "support/lassert.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.plainLayout());
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::plainInsetLayout();
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)) {
493 switch (cmd.button()) {
494 case mouse_button::button1:
495 case mouse_button::button3:
496 // Pass the command to the enclosing InsetText,
497 // so that the cursor gets set.
500 case mouse_button::none:
501 case mouse_button::button2:
502 case mouse_button::button4:
503 case mouse_button::button5:
508 } else if (geometry() != ButtonOnly)
509 InsetText::doDispatch(cur, cmd);
514 case LFUN_MOUSE_MOTION:
515 case LFUN_MOUSE_DOUBLE:
516 case LFUN_MOUSE_TRIPLE:
519 else if (geometry() != ButtonOnly)
520 InsetText::doDispatch(cur, cmd);
525 case LFUN_MOUSE_RELEASE:
526 if (!hitButton(cmd)) {
527 // The mouse click has to be within the inset!
528 if (geometry() != ButtonOnly)
529 InsetText::doDispatch(cur, cmd);
534 if (cmd.button() != mouse_button::button1) {
539 // if we are selecting, we do not want to
543 // Left button is clicked, the user asks to
544 // toggle the inset visual state.
546 cur.updateFlags(Update::Force | Update::FitCursor);
547 if (geometry() == ButtonOnly) {
548 setStatus(cur, Open);
552 setStatus(cur, Collapsed);
553 cur.bv().cursor() = cur;
556 case LFUN_INSET_TOGGLE:
557 if (cmd.argument() == "open")
558 setStatus(cur, Open);
559 else if (cmd.argument() == "close")
560 setStatus(cur, Collapsed);
561 else if (cmd.argument() == "toggle" || cmd.argument().empty())
562 if (status_ == Open) {
563 setStatus(cur, Collapsed);
564 if (geometry() == ButtonOnly)
565 cur.top().forwardPos();
567 setStatus(cur, Open);
568 else // if assign or anything else
574 case LFUN_CLIPBOARD_PASTE:
575 case LFUN_PRIMARY_SELECTION_PASTE: {
576 InsetText::doDispatch(cur, cmd);
577 // Since we can only store plain text, we must reset all
579 // FIXME: Change only the pasted paragraphs
581 resetParagraphsFont();
586 if (layout_ && layout_->isForceLtr()) {
587 // Force any new text to latex_language
588 // FIXME: This should only be necessary in constructor, but
589 // new paragraphs that are created by pressing enter at the
590 // start of an existing paragraph get the buffer language
591 // and not latex_language, so we take this brute force
593 cur.current_font.setLanguage(latex_language);
594 cur.real_current_font.setLanguage(latex_language);
596 InsetText::doDispatch(cur, cmd);
602 bool InsetCollapsable::allowMultiPar() const
604 return layout_->isMultiPar();
608 void InsetCollapsable::resetParagraphsFont()
611 font.fontInfo() = inherit_font;
612 if (layout_->isForceLtr())
613 font.setLanguage(latex_language);
614 if (layout_->isPassThru()) {
615 ParagraphList::iterator par = paragraphs().begin();
616 ParagraphList::iterator const end = paragraphs().end();
618 par->resetFonts(font);
619 par->params().clear();
626 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
627 FuncStatus & flag) const
629 switch (cmd.action) {
631 case LFUN_ACCENT_ACUTE:
632 case LFUN_ACCENT_BREVE:
633 case LFUN_ACCENT_CARON:
634 case LFUN_ACCENT_CEDILLA:
635 case LFUN_ACCENT_CIRCLE:
636 case LFUN_ACCENT_CIRCUMFLEX:
637 case LFUN_ACCENT_DOT:
638 case LFUN_ACCENT_GRAVE:
639 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
640 case LFUN_ACCENT_MACRON:
641 case LFUN_ACCENT_OGONEK:
642 case LFUN_ACCENT_TIE:
643 case LFUN_ACCENT_TILDE:
644 case LFUN_ACCENT_UMLAUT:
645 case LFUN_ACCENT_UNDERBAR:
646 case LFUN_ACCENT_UNDERDOT:
648 case LFUN_BOX_INSERT:
649 case LFUN_BRANCH_INSERT:
650 case LFUN_NEWLINE_INSERT:
651 case LFUN_CAPTION_INSERT:
652 case LFUN_DEPTH_DECREMENT:
653 case LFUN_DEPTH_INCREMENT:
654 case LFUN_ERT_INSERT:
655 case LFUN_FILE_INSERT:
656 case LFUN_FLEX_INSERT:
657 case LFUN_FLOAT_INSERT:
658 case LFUN_FLOAT_LIST_INSERT:
659 case LFUN_FLOAT_WIDE_INSERT:
661 case LFUN_FONT_TYPEWRITER:
662 case LFUN_FONT_DEFAULT:
664 case LFUN_TEXTSTYLE_APPLY:
665 case LFUN_TEXTSTYLE_UPDATE:
667 case LFUN_FONT_ROMAN:
672 case LFUN_FONT_STATE:
673 case LFUN_FONT_UNDERLINE:
674 case LFUN_FOOTNOTE_INSERT:
675 case LFUN_HYPERLINK_INSERT:
676 case LFUN_INDEX_INSERT:
677 case LFUN_INDEX_PRINT:
678 case LFUN_INSET_INSERT:
679 case LFUN_LABEL_GOTO:
680 case LFUN_LABEL_INSERT:
681 case LFUN_LINE_INSERT:
682 case LFUN_NEWPAGE_INSERT:
684 case LFUN_LAYOUT_PARAGRAPH:
685 case LFUN_LAYOUT_TABULAR:
686 case LFUN_MARGINALNOTE_INSERT:
687 case LFUN_MATH_DISPLAY:
688 case LFUN_MATH_INSERT:
689 case LFUN_MATH_MATRIX:
693 case LFUN_NOMENCL_INSERT:
694 case LFUN_NOMENCL_PRINT:
695 case LFUN_NOTE_INSERT:
697 case LFUN_OPTIONAL_INSERT:
698 case LFUN_PARAGRAPH_PARAMS:
699 case LFUN_PARAGRAPH_PARAMS_APPLY:
700 case LFUN_PARAGRAPH_SPACING:
701 case LFUN_PARAGRAPH_UPDATE:
702 case LFUN_REFERENCE_NEXT:
703 case LFUN_SERVER_GOTO_FILE_ROW:
704 case LFUN_SERVER_NOTIFY:
705 case LFUN_SERVER_SET_XY:
706 case LFUN_SPACE_INSERT:
707 case LFUN_SPECIALCHAR_INSERT:
708 case LFUN_TABULAR_INSERT:
709 case LFUN_TOC_INSERT:
710 case LFUN_WRAP_INSERT:
711 if (layout_->isPassThru()) {
712 flag.setEnabled(false);
715 return InsetText::getStatus(cur, cmd, flag);
717 case LFUN_INSET_TOGGLE:
718 if (cmd.argument() == "open")
719 flag.setEnabled(status_ != Open);
720 else if (cmd.argument() == "close")
721 flag.setEnabled(status_ == Open);
722 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
723 flag.setEnabled(true);
724 flag.setOnOff(status_ == Open);
726 flag.setEnabled(false);
730 flag.setEnabled(!layout_->isForceLtr());
731 return InsetText::getStatus(cur, cmd, flag);
733 case LFUN_BREAK_PARAGRAPH:
734 flag.setEnabled(layout_->isMultiPar());
738 return InsetText::getStatus(cur, cmd, flag);
743 void InsetCollapsable::setLabel(docstring const & l)
749 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
753 if (status_ == Collapsed) {
754 cur.leaveInset(*this);
755 mouse_hover_ = false;
760 docstring InsetCollapsable::floatName(
761 string const & type, BufferParams const & bp) const
763 FloatList const & floats = bp.documentClass().floats();
764 FloatList::const_iterator it = floats[type];
766 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
770 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
773 return InsetLayout::Classic;
774 InsetLayout::InsetDecoration const dec = layout_->decoration();
776 case InsetLayout::Classic:
777 case InsetLayout::Minimalistic:
778 case InsetLayout::Conglomerate:
780 case InsetLayout::Default:
783 if (lyxCode() == FLEX_CODE)
784 return InsetLayout::Conglomerate;
785 return InsetLayout::Classic;
789 int InsetCollapsable::latex(odocstream & os,
790 OutputParams const & runparams) const
792 // FIXME: What should we do layout_ is 0?
794 // 2) through an error
798 // This implements the standard way of handling the LaTeX output of
799 // a collapsable inset, either a command or an environment. Standard
800 // collapsable insets should not redefine this, non-standard ones may
802 if (!layout_->latexname().empty()) {
803 if (layout_->latextype() == "command") {
805 if (runparams.moving_arg)
807 os << '\\' << from_utf8(layout_->latexname());
808 if (!layout_->latexparam().empty())
809 os << from_utf8(layout_->latexparam());
811 } else if (layout_->latextype() == "environment") {
812 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
813 if (!layout_->latexparam().empty())
814 os << from_utf8(layout_->latexparam());
817 OutputParams rp = runparams;
818 if (layout_->isPassThru())
820 if (layout_->isNeedProtect())
821 rp.moving_arg = true;
822 int i = InsetText::latex(os, rp);
823 if (!layout_->latexname().empty()) {
824 if (layout_->latextype() == "command") {
826 } else if (layout_->latextype() == "environment") {
827 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
835 void InsetCollapsable::validate(LaTeXFeatures & features) const
840 // Force inclusion of preamble snippet in layout file
841 features.require(to_utf8(layout_->name()));
842 InsetText::validate(features);
846 bool InsetCollapsable::undefined() const
848 docstring const & n = getLayout().name();
849 return n.empty() || n == DocumentClass::plainInsetLayout().name();
853 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
856 if (decoration() == InsetLayout::Conglomerate)
857 return from_ascii("context-conglomerate");
859 if (geometry() == NoButton)
860 return from_ascii("context-collapsable");
862 Dimension dim = dimensionCollapsed();
863 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
864 return from_ascii("context-collapsable");
866 return InsetText::contextMenu(bv, x, y);