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, DocumentClass * dc)
82 : InsetText(bp), status_(status),
83 openinlined_(false), autoOpen_(false), mouse_hover_(false)
86 setAutoBreakRows(true);
88 setFrameColor(Color_collapsableframe);
89 paragraphs().back().setLayout(bp.documentClass().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 layout_->labelstring();
112 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
117 return _("Left-click to collapse the inset");
119 return _("Left-click to open the inset");
125 void InsetCollapsable::setLayout(BufferParams const & bp)
127 setLayout(bp.documentClassPtr());
131 void InsetCollapsable::setLayout(DocumentClass const * const dc)
134 layout_ = &(dc->insetLayout(name()));
135 labelstring_ = layout_->labelstring();
137 layout_ = &TextClass::emptyInsetLayout();
138 labelstring_ = _("UNDEFINED");
145 void InsetCollapsable::write(ostream & os) const
157 text_.write(buffer(), os);
161 void InsetCollapsable::read(Lexer & lex)
163 bool token_found = false;
166 string const token = lex.getString();
167 if (token == "status") {
169 string const tmp_token = lex.getString();
171 if (tmp_token == "collapsed") {
174 } else if (tmp_token == "open") {
178 lyxerr << "InsetCollapsable::read: Missing status!"
180 // Take countermeasures
181 lex.pushToken(token);
184 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
185 // take countermeasures
186 lex.pushToken(token);
189 // this must be set before we enter InsetText::read()
190 setLayout(buffer().params());
192 InsetText::read(lex);
195 status_ = isOpen() ? Open : Collapsed;
197 // Force default font, if so requested
198 // This avoids paragraphs in buffer language that would have a
199 // foreign language after a document language change, and it ensures
200 // that all new text in ERT and similar gets the "latex" language,
201 // since new text inherits the language from the last position of the
202 // existing text. As a side effect this makes us also robust against
203 // bugs in LyX that might lead to font changes in ERT in .lyx files.
204 resetParagraphsFont();
208 Dimension InsetCollapsable::dimensionCollapsed() const
210 BOOST_ASSERT(layout_);
212 theFontMetrics(layout_->labelfont()).buttonText(
213 labelstring_, dim.wid, dim.asc, dim.des);
218 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
220 BOOST_ASSERT(layout_);
222 autoOpen_ = mi.base.bv->cursor().isInside(this);
224 FontInfo tmpfont = mi.base.font;
225 mi.base.font = layout_->font();
226 mi.base.font.realize(tmpfont);
228 switch (geometry()) {
230 InsetText::metrics(mi, dim);
233 InsetText::metrics(mi, dim);
238 InsetText::metrics(mi, dim);
239 // consider width of the inset label
240 FontInfo font(layout_->labelfont());
241 font.realize(sane_font);
247 theFontMetrics(font).rectText(labelstring_, w, a, d);
254 dim = dimensionCollapsed();
255 if (geometry() == TopButton || geometry() == LeftButton) {
257 InsetText::metrics(mi, textdim);
258 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
260 // Correct for button width.
261 dim.wid += textdim.wid;
262 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
263 dim.asc = textdim.asc;
265 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
266 dim.wid = max(dim.wid, textdim.wid);
272 mi.base.font = tmpfont;
276 bool InsetCollapsable::setMouseHover(bool mouse_hover)
278 mouse_hover_ = mouse_hover;
283 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
285 BOOST_ASSERT(layout_);
287 autoOpen_ = pi.base.bv->cursor().isInside(this);
288 ColorCode const old_color = pi.background_color;
289 pi.background_color = backgroundColor();
291 FontInfo tmpfont = pi.base.font;
292 pi.base.font = layout_->font();
293 pi.base.font.realize(tmpfont);
295 // Draw button first -- top, left or only
296 Dimension dimc = dimensionCollapsed();
298 if (geometry() == TopButton ||
299 geometry() == LeftButton ||
300 geometry() == ButtonOnly) {
301 button_dim.x1 = x + 0;
302 button_dim.x2 = x + dimc.width();
303 button_dim.y1 = y - dimc.asc;
304 button_dim.y2 = y + dimc.des;
306 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
315 Dimension const textdim = InsetText::dimension(*pi.base.bv);
316 int const baseline = y;
318 switch (geometry()) {
320 textx = x + dimc.width();
322 InsetText::draw(pi, textx, texty);
326 texty = baseline + dimc.des + textdim.asc;
327 InsetText::draw(pi, textx, texty);
334 InsetText::draw(pi, textx, texty);
340 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
341 InsetText::draw(pi, textx, texty);
342 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
344 int desc = textdim.descent();
345 if (geometry() == Corners)
348 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
349 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
350 pi.pain.line(xx1, y + desc - 4,
352 layout_->labelfont().color());
354 pi.pain.line(xx1, y + desc,
356 layout_->labelfont().color());
358 // Make status_ value visible:
359 pi.pain.line(xx1, y + desc,
361 layout_->labelfont().color());
362 pi.pain.line(xx2 - 4, y + desc,
364 layout_->labelfont().color());
366 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
367 y + desc - 4, layout_->labelfont().color());
369 // the label below the text. Can be toggled.
370 if (geometry() == SubLabel) {
371 FontInfo font(layout_->labelfont());
372 font.realize(sane_font);
378 theFontMetrics(font).rectText(labelstring_, w, a, d);
379 int const ww = max(textdim.wid, w);
380 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
381 labelstring_, font, Color_none, Color_none);
385 // a visual cue when the cursor is inside the inset
386 Cursor & cur = pi.base.bv->cursor();
387 if (cur.isInside(this)) {
390 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
391 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
392 pi.pain.line(xx2, y + 4, xx2, y,
393 layout_->labelfont().color());
394 pi.pain.line(xx2 - 4, y, xx2, y,
395 layout_->labelfont().color());
399 pi.background_color = old_color;
401 pi.base.font = tmpfont;
405 void InsetCollapsable::cursorPos(BufferView const & bv,
406 CursorSlice const & sl, bool boundary, int & x, int & y) const
408 if (geometry() == ButtonOnly)
410 BOOST_ASSERT(geometry() != ButtonOnly);
412 InsetText::cursorPos(bv, sl, boundary, x, y);
413 Dimension const textdim = InsetText::dimension(bv);
415 switch (geometry()) {
417 x += dimensionCollapsed().wid;
420 y += dimensionCollapsed().des + textdim.asc;
435 Inset::EDITABLE InsetCollapsable::editable() const
437 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
441 bool InsetCollapsable::descendable() const
443 return geometry() != ButtonOnly;
447 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
449 return button_dim.contains(cmd.x, cmd.y);
453 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
456 pos_type const max_length = 15;
457 pos_type const p_siz = paragraphs().begin()->size();
458 pos_type const n = min(max_length, p_siz);
461 for (; i < n && j < p_siz; ++j) {
462 if (paragraphs().begin()->isInset(j))
464 label += paragraphs().begin()->getChar(j);
467 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
470 return label.empty() ? l : label;
474 void InsetCollapsable::edit(
475 Cursor & cur, bool front, EntryDirection entry_from)
477 //lyxerr << "InsetCollapsable: edit left/right" << endl;
479 InsetText::edit(cur, front, entry_from);
483 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
485 //lyxerr << "InsetCollapsable: edit xy" << endl;
486 if (geometry() == ButtonOnly
487 || (button_dim.contains(x, y)
488 && geometry() != NoButton))
491 return InsetText::editXY(cur, x, y);
495 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
497 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
498 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
500 switch (cmd.action) {
501 case LFUN_MOUSE_PRESS:
502 if (cmd.button() == mouse_button::button1
504 && geometry() != NoButton) {
505 // reset selection if necessary (see bug 3060)
507 cur.bv().cursor().clearSelection();
513 if (geometry() == NoButton)
514 InsetText::doDispatch(cur, cmd);
515 else if (geometry() != ButtonOnly
517 InsetText::doDispatch(cur, cmd);
522 case LFUN_MOUSE_MOTION:
523 case LFUN_MOUSE_DOUBLE:
524 case LFUN_MOUSE_TRIPLE:
525 if (geometry() == NoButton)
526 InsetText::doDispatch(cur, cmd);
527 else if (geometry() != ButtonOnly
529 InsetText::doDispatch(cur, cmd);
534 case LFUN_MOUSE_RELEASE:
535 if (cmd.button() == mouse_button::button3) {
536 // There is no button to right click:
537 if (decoration() == InsetLayout::Minimalistic ||
538 geometry() == Corners ||
539 geometry() == SubLabel ||
540 geometry() == NoButton
543 setStatus(cur, Collapsed);
545 setStatus(cur, Open);
549 // configuration dialog
550 showInsetDialog(&cur.bv());
555 if (geometry() == NoButton) {
556 // The mouse click has to be within the inset!
557 InsetText::doDispatch(cur, cmd);
561 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
562 // if we are selecting, we do not want to
566 // Left button is clicked, the user asks to
567 // toggle the inset visual state.
569 cur.updateFlags(Update::Force | Update::FitCursor);
570 if (geometry() == ButtonOnly) {
571 setStatus(cur, Open);
575 setStatus(cur, Collapsed);
577 cur.bv().cursor() = cur;
581 // The mouse click is within the opened inset.
582 if (geometry() == TopButton
583 || geometry() == LeftButton)
584 InsetText::doDispatch(cur, cmd);
587 case LFUN_INSET_TOGGLE:
588 if (cmd.argument() == "open")
589 setStatus(cur, Open);
590 else if (cmd.argument() == "close")
591 setStatus(cur, Collapsed);
592 else if (cmd.argument() == "toggle" || cmd.argument().empty())
593 if (status_ == Open) {
594 setStatus(cur, Collapsed);
595 if (geometry() == ButtonOnly)
596 cur.top().forwardPos();
598 setStatus(cur, Open);
599 else // if assign or anything else
605 case LFUN_CLIPBOARD_PASTE:
606 case LFUN_PRIMARY_SELECTION_PASTE: {
607 InsetText::doDispatch(cur, cmd);
608 // Since we can only store plain text, we must reset all
610 // FIXME: Change only the pasted paragraphs
612 resetParagraphsFont();
617 if (layout_ && layout_->isForceLtr()) {
618 // Force any new text to latex_language
619 // FIXME: This should only be necessary in constructor, but
620 // new paragraphs that are created by pressing enter at the
621 // start of an existing paragraph get the buffer language
622 // and not latex_language, so we take this brute force
624 cur.current_font.setLanguage(latex_language);
625 cur.real_current_font.setLanguage(latex_language);
627 InsetText::doDispatch(cur, cmd);
633 bool InsetCollapsable::allowMultiPar() const
635 return layout_->isMultiPar();
639 void InsetCollapsable::resetParagraphsFont()
642 font.fontInfo() = inherit_font;
643 if (layout_->isForceLtr())
644 font.setLanguage(latex_language);
645 if (layout_->isPassThru()) {
646 ParagraphList::iterator par = paragraphs().begin();
647 ParagraphList::iterator const end = paragraphs().end();
649 par->resetFonts(font);
650 par->params().clear();
657 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
658 FuncStatus & flag) const
660 switch (cmd.action) {
662 case LFUN_ACCENT_ACUTE:
663 case LFUN_ACCENT_BREVE:
664 case LFUN_ACCENT_CARON:
665 case LFUN_ACCENT_CEDILLA:
666 case LFUN_ACCENT_CIRCLE:
667 case LFUN_ACCENT_CIRCUMFLEX:
668 case LFUN_ACCENT_DOT:
669 case LFUN_ACCENT_GRAVE:
670 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
671 case LFUN_ACCENT_MACRON:
672 case LFUN_ACCENT_OGONEK:
673 case LFUN_ACCENT_SPECIAL_CARON:
674 case LFUN_ACCENT_TIE:
675 case LFUN_ACCENT_TILDE:
676 case LFUN_ACCENT_UMLAUT:
677 case LFUN_ACCENT_UNDERBAR:
678 case LFUN_ACCENT_UNDERDOT:
680 case LFUN_BIBITEM_INSERT:
681 case LFUN_BOX_INSERT:
682 case LFUN_BRANCH_INSERT:
684 case LFUN_CAPTION_INSERT:
685 case LFUN_CLEARPAGE_INSERT:
686 case LFUN_CLEARDOUBLEPAGE_INSERT:
687 case LFUN_DEPTH_DECREMENT:
688 case LFUN_DEPTH_INCREMENT:
689 case LFUN_ENVIRONMENT_INSERT:
690 case LFUN_ERT_INSERT:
691 case LFUN_FILE_INSERT:
692 case LFUN_FLEX_INSERT:
693 case LFUN_FLOAT_INSERT:
694 case LFUN_FLOAT_LIST:
695 case LFUN_FLOAT_WIDE_INSERT:
697 case LFUN_FONT_TYPEWRITER:
698 case LFUN_FONT_DEFAULT:
700 case LFUN_FONT_FREE_APPLY:
701 case LFUN_FONT_FREE_UPDATE:
703 case LFUN_FONT_ROMAN:
708 case LFUN_FONT_STATE:
709 case LFUN_FONT_UNDERLINE:
710 case LFUN_FOOTNOTE_INSERT:
711 case LFUN_HFILL_INSERT:
712 case LFUN_HYPERLINK_INSERT:
713 case LFUN_INDEX_INSERT:
714 case LFUN_INDEX_PRINT:
715 case LFUN_INSET_INSERT:
716 case LFUN_LABEL_GOTO:
717 case LFUN_LABEL_INSERT:
718 case LFUN_LINE_INSERT:
719 case LFUN_NEWPAGE_INSERT:
720 case LFUN_PAGEBREAK_INSERT:
722 case LFUN_LAYOUT_PARAGRAPH:
723 case LFUN_LAYOUT_TABULAR:
724 case LFUN_MARGINALNOTE_INSERT:
725 case LFUN_MATH_DISPLAY:
726 case LFUN_MATH_INSERT:
727 case LFUN_MATH_MATRIX:
731 case LFUN_NOMENCL_INSERT:
732 case LFUN_NOMENCL_PRINT:
733 case LFUN_NOTE_INSERT:
735 case LFUN_OPTIONAL_INSERT:
736 case LFUN_PARAGRAPH_PARAMS:
737 case LFUN_PARAGRAPH_PARAMS_APPLY:
738 case LFUN_PARAGRAPH_SPACING:
739 case LFUN_PARAGRAPH_UPDATE:
740 case LFUN_REFERENCE_NEXT:
741 case LFUN_SERVER_GOTO_FILE_ROW:
742 case LFUN_SERVER_NOTIFY:
743 case LFUN_SERVER_SET_XY:
744 case LFUN_SPACE_INSERT:
745 case LFUN_SPECIALCHAR_INSERT:
746 case LFUN_TABULAR_INSERT:
747 case LFUN_TOC_INSERT:
748 case LFUN_WRAP_INSERT:
749 if (layout_->isPassThru()) {
753 return InsetText::getStatus(cur, cmd, flag);
755 case LFUN_INSET_TOGGLE:
756 if (cmd.argument() == "open" || cmd.argument() == "close" ||
757 cmd.argument() == "toggle")
764 flag.enabled(!layout_->isForceLtr());
765 return InsetText::getStatus(cur, cmd, flag);
767 case LFUN_BREAK_PARAGRAPH:
768 case LFUN_BREAK_PARAGRAPH_SKIP:
769 flag.enabled(layout_->isMultiPar());
773 return InsetText::getStatus(cur, cmd, flag);
778 void InsetCollapsable::setLabel(docstring const & l)
784 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
788 if (status_ == Collapsed)
789 cur.leaveInset(*this);
793 docstring InsetCollapsable::floatName(
794 string const & type, BufferParams const & bp) const
796 FloatList const & floats = bp.documentClass().floats();
797 FloatList::const_iterator it = floats[type];
799 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
803 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
806 return InsetLayout::Classic;
807 InsetLayout::InsetDecoration const dec = layout_->decoration();
809 case InsetLayout::Classic:
810 case InsetLayout::Minimalistic:
811 case InsetLayout::Conglomerate:
813 case InsetLayout::Default:
816 if (lyxCode() == FLEX_CODE)
817 // FIXME: Is this really necessary?
818 return InsetLayout::Conglomerate;
819 return InsetLayout::Classic;
823 bool InsetCollapsable::isMacroScope() const
825 // layout_ == 0 leads to no latex output, so ignore
826 // the macros outside
830 // see InsetCollapsable::latex(...) below. In these case
831 // an environment is opened there
832 if (!layout_->latexname().empty())
839 int InsetCollapsable::latex(odocstream & os,
840 OutputParams const & runparams) const
842 // FIXME: What should we do layout_ is 0?
844 // 2) through an error
848 // This implements the standard way of handling the LaTeX output of
849 // a collapsable inset, either a command or an environment. Standard
850 // collapsable insets should not redefine this, non-standard ones may
852 if (!layout_->latexname().empty()) {
853 if (layout_->latextype() == "command") {
855 if (runparams.moving_arg)
857 os << '\\' << from_utf8(layout_->latexname());
858 if (!layout_->latexparam().empty())
859 os << from_utf8(layout_->latexparam());
861 } else if (layout_->latextype() == "environment") {
862 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
863 if (!layout_->latexparam().empty())
864 os << from_utf8(layout_->latexparam());
867 OutputParams rp = runparams;
868 if (layout_->isPassThru())
870 if (layout_->isNeedProtect())
871 rp.moving_arg = true;
872 int i = InsetText::latex(os, rp);
873 if (!layout_->latexname().empty()) {
874 if (layout_->latextype() == "command") {
876 } else if (layout_->latextype() == "environment") {
877 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
885 void InsetCollapsable::validate(LaTeXFeatures & features) const
890 // Force inclusion of preamble snippet in layout file
891 features.require(to_utf8(layout_->name()));
892 InsetText::validate(features);
896 bool InsetCollapsable::undefined() const
898 docstring const & n = getLayout().name();
899 return n.empty() || n == TextClass::emptyInsetLayout().name();