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 if (decoration() == InsetLayout::Conglomerate)
50 return autoOpen_ ? Open : status_;
54 InsetCollapsable::Geometry InsetCollapsable::geometry() const
56 switch (decoration()) {
57 case InsetLayout::Classic:
59 return openinlined_ ? LeftButton : TopButton;
62 case InsetLayout::Minimalistic:
63 return status() == Open ? NoButton : ButtonOnly ;
65 case InsetLayout::Conglomerate:
66 return status() == Open ? SubLabel : Corners ;
68 case InsetLayout::Default:
69 break; // this shouldn't happen
72 // dummy return value to shut down a warning,
78 InsetCollapsable::InsetCollapsable(Buffer const & buf,
79 CollapseStatus status, DocumentClass * dc)
80 : InsetText(buf), status_(status),
81 openinlined_(false), autoOpen_(false), mouse_hover_(false)
84 setAutoBreakRows(true);
86 setFrameColor(Color_collapsableframe);
87 paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
91 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
94 labelstring_(rhs.labelstring_),
95 button_dim(rhs.button_dim),
97 openinlined_(rhs.openinlined_),
98 autoOpen_(rhs.autoOpen_),
99 // the sole purpose of this copy constructor
105 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
107 Dimension dim = dimensionCollapsed();
108 if (geometry() == NoButton)
109 return layout_->labelstring();
110 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
115 return _("Left-click to collapse the inset");
117 return _("Left-click to open the inset");
123 void InsetCollapsable::setLayout(BufferParams const & bp)
125 setLayout(bp.documentClassPtr());
129 void InsetCollapsable::setLayout(DocumentClass const * const dc)
132 layout_ = &(dc->insetLayout(name()));
133 labelstring_ = layout_->labelstring();
135 layout_ = &DocumentClass::emptyInsetLayout();
136 labelstring_ = _("UNDEFINED");
143 void InsetCollapsable::write(ostream & os) const
155 text_.write(buffer(), os);
159 void InsetCollapsable::read(Lexer & lex)
161 bool token_found = false;
164 string const token = lex.getString();
165 if (token == "status") {
167 string const tmp_token = lex.getString();
169 if (tmp_token == "collapsed") {
172 } else if (tmp_token == "open") {
176 lyxerr << "InsetCollapsable::read: Missing status!"
178 // Take countermeasures
179 lex.pushToken(token);
182 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
183 // take countermeasures
184 lex.pushToken(token);
187 // this must be set before we enter InsetText::read()
188 setLayout(buffer().params());
190 InsetText::read(lex);
193 status_ = isOpen() ? Open : Collapsed;
195 // Force default font, if so requested
196 // This avoids paragraphs in buffer language that would have a
197 // foreign language after a document language change, and it ensures
198 // that all new text in ERT and similar gets the "latex" language,
199 // since new text inherits the language from the last position of the
200 // existing text. As a side effect this makes us also robust against
201 // bugs in LyX that might lead to font changes in ERT in .lyx files.
202 resetParagraphsFont();
206 Dimension InsetCollapsable::dimensionCollapsed() const
208 BOOST_ASSERT(layout_);
210 theFontMetrics(layout_->labelfont()).buttonText(
211 labelstring_, dim.wid, dim.asc, dim.des);
216 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
218 BOOST_ASSERT(layout_);
220 autoOpen_ = mi.base.bv->cursor().isInside(this);
222 FontInfo tmpfont = mi.base.font;
223 mi.base.font = layout_->font();
224 mi.base.font.realize(tmpfont);
226 switch (geometry()) {
228 InsetText::metrics(mi, dim);
231 InsetText::metrics(mi, dim);
236 InsetText::metrics(mi, dim);
237 // consider width of the inset label
238 FontInfo font(layout_->labelfont());
239 font.realize(sane_font);
245 theFontMetrics(font).rectText(labelstring_, w, a, d);
252 dim = dimensionCollapsed();
253 if (geometry() == TopButton || geometry() == LeftButton) {
255 InsetText::metrics(mi, textdim);
256 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
258 // Correct for button width.
259 dim.wid += textdim.wid;
260 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
261 dim.asc = textdim.asc;
263 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
264 dim.wid = max(dim.wid, textdim.wid);
270 mi.base.font = tmpfont;
274 bool InsetCollapsable::setMouseHover(bool mouse_hover)
276 mouse_hover_ = mouse_hover;
281 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
283 BOOST_ASSERT(layout_);
285 autoOpen_ = pi.base.bv->cursor().isInside(this);
286 ColorCode const old_color = pi.background_color;
287 pi.background_color = backgroundColor();
289 FontInfo tmpfont = pi.base.font;
290 pi.base.font = layout_->font();
291 pi.base.font.realize(tmpfont);
293 // Draw button first -- top, left or only
294 Dimension dimc = dimensionCollapsed();
296 if (geometry() == TopButton ||
297 geometry() == LeftButton ||
298 geometry() == ButtonOnly) {
299 button_dim.x1 = x + 0;
300 button_dim.x2 = x + dimc.width();
301 button_dim.y1 = y - dimc.asc;
302 button_dim.y2 = y + dimc.des;
304 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
313 Dimension const textdim = InsetText::dimension(*pi.base.bv);
314 int const baseline = y;
316 switch (geometry()) {
318 textx = x + dimc.width();
320 InsetText::draw(pi, textx, texty);
324 texty = baseline + dimc.des + textdim.asc;
325 InsetText::draw(pi, textx, texty);
332 InsetText::draw(pi, textx, texty);
338 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
339 InsetText::draw(pi, textx, texty);
340 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
342 int desc = textdim.descent();
343 if (geometry() == Corners)
346 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
347 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
348 pi.pain.line(xx1, y + desc - 4,
350 layout_->labelfont().color());
352 pi.pain.line(xx1, y + desc,
354 layout_->labelfont().color());
356 // Make status_ value visible:
357 pi.pain.line(xx1, y + desc,
359 layout_->labelfont().color());
360 pi.pain.line(xx2 - 4, y + desc,
362 layout_->labelfont().color());
364 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
365 y + desc - 4, layout_->labelfont().color());
367 // the label below the text. Can be toggled.
368 if (geometry() == SubLabel) {
369 FontInfo font(layout_->labelfont());
370 font.realize(sane_font);
376 theFontMetrics(font).rectText(labelstring_, w, a, d);
377 int const ww = max(textdim.wid, w);
378 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
379 labelstring_, font, Color_none, Color_none);
383 // a visual cue when the cursor is inside the inset
384 Cursor & cur = pi.base.bv->cursor();
385 if (cur.isInside(this)) {
388 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
389 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
390 pi.pain.line(xx2, y + 4, xx2, y,
391 layout_->labelfont().color());
392 pi.pain.line(xx2 - 4, y, xx2, y,
393 layout_->labelfont().color());
397 pi.background_color = old_color;
399 pi.base.font = tmpfont;
403 void InsetCollapsable::cursorPos(BufferView const & bv,
404 CursorSlice const & sl, bool boundary, int & x, int & y) const
406 if (geometry() == ButtonOnly)
408 BOOST_ASSERT(geometry() != ButtonOnly);
410 InsetText::cursorPos(bv, sl, boundary, x, y);
411 Dimension const textdim = InsetText::dimension(bv);
413 switch (geometry()) {
415 x += dimensionCollapsed().wid;
418 y += dimensionCollapsed().des + textdim.asc;
433 Inset::EDITABLE InsetCollapsable::editable() const
435 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
439 bool InsetCollapsable::descendable() const
441 return geometry() != ButtonOnly;
445 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
447 return button_dim.contains(cmd.x, cmd.y);
451 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
454 pos_type const max_length = 15;
455 pos_type const p_siz = paragraphs().begin()->size();
456 pos_type const n = min(max_length, p_siz);
459 for (; i < n && j < p_siz; ++j) {
460 if (paragraphs().begin()->isInset(j))
462 label += paragraphs().begin()->getChar(j);
465 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
468 return label.empty() ? l : label;
472 void InsetCollapsable::edit(
473 Cursor & cur, bool front, EntryDirection entry_from)
475 //lyxerr << "InsetCollapsable: edit left/right" << endl;
477 InsetText::edit(cur, front, entry_from);
481 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
483 //lyxerr << "InsetCollapsable: edit xy" << endl;
484 if (geometry() == ButtonOnly
485 || (button_dim.contains(x, y)
486 && geometry() != NoButton))
489 return InsetText::editXY(cur, x, y);
493 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
495 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
496 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
498 switch (cmd.action) {
499 case LFUN_MOUSE_PRESS:
500 if (hitButton(cmd) && geometry() != NoButton) {
501 switch (cmd.button()) {
502 case mouse_button::button1:
503 // reset selection if necessary (see bug 3060)
505 cur.bv().cursor().clearSelection();
510 case mouse_button::button2:
511 case mouse_button::button3:
517 if (geometry() == NoButton
518 || (geometry() != ButtonOnly && !hitButton(cmd)))
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 (geometry() == NoButton) {
538 // The mouse click has to be within the inset!
539 InsetText::doDispatch(cur, cmd);
543 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
544 // if we are selecting, we do not want to
548 // Left button is clicked, the user asks to
549 // toggle the inset visual state.
551 cur.updateFlags(Update::Force | Update::FitCursor);
552 if (geometry() == ButtonOnly) {
553 setStatus(cur, Open);
557 setStatus(cur, Collapsed);
559 cur.bv().cursor() = cur;
563 // The mouse click is within the opened inset.
564 if (geometry() == TopButton
565 || geometry() == LeftButton)
566 InsetText::doDispatch(cur, cmd);
569 case LFUN_INSET_TOGGLE:
570 if (cmd.argument() == "open")
571 setStatus(cur, Open);
572 else if (cmd.argument() == "close")
573 setStatus(cur, Collapsed);
574 else if (cmd.argument() == "toggle" || cmd.argument().empty())
575 if (status_ == Open) {
576 setStatus(cur, Collapsed);
577 if (geometry() == ButtonOnly)
578 cur.top().forwardPos();
580 setStatus(cur, Open);
581 else // if assign or anything else
587 case LFUN_CLIPBOARD_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()
624 font.fontInfo() = inherit_font;
625 if (layout_->isForceLtr())
626 font.setLanguage(latex_language);
627 if (layout_->isPassThru()) {
628 ParagraphList::iterator par = paragraphs().begin();
629 ParagraphList::iterator const end = paragraphs().end();
631 par->resetFonts(font);
632 par->params().clear();
639 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
640 FuncStatus & flag) const
642 switch (cmd.action) {
644 case LFUN_ACCENT_ACUTE:
645 case LFUN_ACCENT_BREVE:
646 case LFUN_ACCENT_CARON:
647 case LFUN_ACCENT_CEDILLA:
648 case LFUN_ACCENT_CIRCLE:
649 case LFUN_ACCENT_CIRCUMFLEX:
650 case LFUN_ACCENT_DOT:
651 case LFUN_ACCENT_GRAVE:
652 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
653 case LFUN_ACCENT_MACRON:
654 case LFUN_ACCENT_OGONEK:
655 case LFUN_ACCENT_SPECIAL_CARON:
656 case LFUN_ACCENT_TIE:
657 case LFUN_ACCENT_TILDE:
658 case LFUN_ACCENT_UMLAUT:
659 case LFUN_ACCENT_UNDERBAR:
660 case LFUN_ACCENT_UNDERDOT:
662 case LFUN_BIBITEM_INSERT:
663 case LFUN_BOX_INSERT:
664 case LFUN_BRANCH_INSERT:
666 case LFUN_CAPTION_INSERT:
667 case LFUN_DEPTH_DECREMENT:
668 case LFUN_DEPTH_INCREMENT:
669 case LFUN_ENVIRONMENT_INSERT:
670 case LFUN_ERT_INSERT:
671 case LFUN_FILE_INSERT:
672 case LFUN_FLEX_INSERT:
673 case LFUN_FLOAT_INSERT:
674 case LFUN_FLOAT_LIST:
675 case LFUN_FLOAT_WIDE_INSERT:
677 case LFUN_FONT_TYPEWRITER:
678 case LFUN_FONT_DEFAULT:
680 case LFUN_FONT_FREE_APPLY:
681 case LFUN_FONT_FREE_UPDATE:
683 case LFUN_FONT_ROMAN:
688 case LFUN_FONT_STATE:
689 case LFUN_FONT_UNDERLINE:
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_LINE_INSERT:
698 case LFUN_NEWPAGE_INSERT:
700 case LFUN_LAYOUT_PARAGRAPH:
701 case LFUN_LAYOUT_TABULAR:
702 case LFUN_MARGINALNOTE_INSERT:
703 case LFUN_MATH_DISPLAY:
704 case LFUN_MATH_INSERT:
705 case LFUN_MATH_MATRIX:
709 case LFUN_NOMENCL_INSERT:
710 case LFUN_NOMENCL_PRINT:
711 case LFUN_NOTE_INSERT:
713 case LFUN_OPTIONAL_INSERT:
714 case LFUN_PARAGRAPH_PARAMS:
715 case LFUN_PARAGRAPH_PARAMS_APPLY:
716 case LFUN_PARAGRAPH_SPACING:
717 case LFUN_PARAGRAPH_UPDATE:
718 case LFUN_REFERENCE_NEXT:
719 case LFUN_SERVER_GOTO_FILE_ROW:
720 case LFUN_SERVER_NOTIFY:
721 case LFUN_SERVER_SET_XY:
722 case LFUN_SPACE_INSERT:
723 case LFUN_SPECIALCHAR_INSERT:
724 case LFUN_TABULAR_INSERT:
725 case LFUN_TOC_INSERT:
726 case LFUN_WRAP_INSERT:
727 if (layout_->isPassThru()) {
731 return InsetText::getStatus(cur, cmd, flag);
733 case LFUN_INSET_TOGGLE:
734 if ((cmd.argument() == "open" && status_ != Open)
735 || (cmd.argument() == "close" && status_ == Open)
736 || cmd.argument() == "toggle" || cmd.argument().empty())
743 flag.enabled(!layout_->isForceLtr());
744 return InsetText::getStatus(cur, cmd, flag);
746 case LFUN_BREAK_PARAGRAPH:
747 case LFUN_BREAK_PARAGRAPH_SKIP:
748 flag.enabled(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);
772 docstring InsetCollapsable::floatName(
773 string const & type, BufferParams const & bp) const
775 FloatList const & floats = bp.documentClass().floats();
776 FloatList::const_iterator it = floats[type];
778 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
782 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
785 return InsetLayout::Classic;
786 InsetLayout::InsetDecoration const dec = layout_->decoration();
788 case InsetLayout::Classic:
789 case InsetLayout::Minimalistic:
790 case InsetLayout::Conglomerate:
792 case InsetLayout::Default:
795 if (lyxCode() == FLEX_CODE)
796 return InsetLayout::Conglomerate;
797 return InsetLayout::Classic;
801 int InsetCollapsable::latex(odocstream & os,
802 OutputParams const & runparams) const
804 // FIXME: What should we do layout_ is 0?
806 // 2) through an error
810 // This implements the standard way of handling the LaTeX output of
811 // a collapsable inset, either a command or an environment. Standard
812 // collapsable insets should not redefine this, non-standard ones may
814 if (!layout_->latexname().empty()) {
815 if (layout_->latextype() == "command") {
817 if (runparams.moving_arg)
819 os << '\\' << from_utf8(layout_->latexname());
820 if (!layout_->latexparam().empty())
821 os << from_utf8(layout_->latexparam());
823 } else if (layout_->latextype() == "environment") {
824 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
825 if (!layout_->latexparam().empty())
826 os << from_utf8(layout_->latexparam());
829 OutputParams rp = runparams;
830 if (layout_->isPassThru())
832 if (layout_->isNeedProtect())
833 rp.moving_arg = true;
834 int i = InsetText::latex(os, rp);
835 if (!layout_->latexname().empty()) {
836 if (layout_->latextype() == "command") {
838 } else if (layout_->latextype() == "environment") {
839 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
847 void InsetCollapsable::validate(LaTeXFeatures & features) const
852 // Force inclusion of preamble snippet in layout file
853 features.require(to_utf8(layout_->name()));
854 InsetText::validate(features);
858 bool InsetCollapsable::undefined() const
860 docstring const & n = getLayout().name();
861 return n.empty() || n == DocumentClass::emptyInsetLayout().name();
865 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
868 if (decoration() == InsetLayout::Conglomerate)
869 return from_ascii("context-conglomerate");
871 if (geometry() == NoButton)
872 return from_ascii("context-collapsable");
874 Dimension dim = dimensionCollapsed();
875 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
876 return from_ascii("context-collapsable");
878 return InsetText::contextMenu(bv, x, y);