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:
57 return openinlined_ ? LeftButton : TopButton;
60 case InsetLayout::Minimalistic:
61 return status() == Open ? NoButton : ButtonOnly ;
63 case InsetLayout::Conglomerate:
64 return status() == Open ? SubLabel : Corners ;
66 case InsetLayout::Default:
67 break; // this shouldn't happen
70 // dummy return value to shut down a warning,
76 InsetCollapsable::InsetCollapsable(Buffer const & buf,
77 CollapseStatus status, DocumentClass * dc)
78 : InsetText(buf), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
82 setAutoBreakRows(true);
84 setFrameColor(Color_collapsableframe);
85 paragraphs().back().setLayout(buf.params().documentClass().emptyLayout());
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
92 labelstring_(rhs.labelstring_),
93 button_dim(rhs.button_dim),
95 openinlined_(rhs.openinlined_),
96 autoOpen_(rhs.autoOpen_),
97 // the sole purpose of this copy constructor
103 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
105 Dimension dim = dimensionCollapsed();
106 if (geometry() == NoButton)
107 return layout_->labelstring();
108 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
113 return _("Left-click to collapse the inset");
115 return _("Left-click to open the inset");
121 void InsetCollapsable::setLayout(BufferParams const & bp)
123 setLayout(bp.documentClassPtr());
127 void InsetCollapsable::setLayout(DocumentClass const * const dc)
130 layout_ = &(dc->insetLayout(name()));
131 labelstring_ = layout_->labelstring();
133 layout_ = &DocumentClass::emptyInsetLayout();
134 labelstring_ = _("UNDEFINED");
141 void InsetCollapsable::write(ostream & os) const
153 text_.write(buffer(), os);
157 void InsetCollapsable::read(Lexer & lex)
159 bool token_found = false;
162 string const token = lex.getString();
163 if (token == "status") {
165 string const tmp_token = lex.getString();
167 if (tmp_token == "collapsed") {
170 } else if (tmp_token == "open") {
174 lyxerr << "InsetCollapsable::read: Missing status!"
176 // Take countermeasures
177 lex.pushToken(token);
180 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
181 // take countermeasures
182 lex.pushToken(token);
185 // this must be set before we enter InsetText::read()
186 setLayout(buffer().params());
188 InsetText::read(lex);
191 status_ = isOpen() ? Open : Collapsed;
193 // Force default font, if so requested
194 // This avoids paragraphs in buffer language that would have a
195 // foreign language after a document language change, and it ensures
196 // that all new text in ERT and similar gets the "latex" language,
197 // since new text inherits the language from the last position of the
198 // existing text. As a side effect this makes us also robust against
199 // bugs in LyX that might lead to font changes in ERT in .lyx files.
200 resetParagraphsFont();
204 Dimension InsetCollapsable::dimensionCollapsed() const
206 BOOST_ASSERT(layout_);
208 theFontMetrics(layout_->labelfont()).buttonText(
209 labelstring_, dim.wid, dim.asc, dim.des);
214 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
216 BOOST_ASSERT(layout_);
218 autoOpen_ = mi.base.bv->cursor().isInside(this);
220 FontInfo tmpfont = mi.base.font;
221 mi.base.font = layout_->font();
222 mi.base.font.realize(tmpfont);
224 switch (geometry()) {
226 InsetText::metrics(mi, dim);
229 InsetText::metrics(mi, dim);
234 InsetText::metrics(mi, dim);
235 // consider width of the inset label
236 FontInfo font(layout_->labelfont());
237 font.realize(sane_font);
243 theFontMetrics(font).rectText(labelstring_, w, a, d);
250 dim = dimensionCollapsed();
251 if (geometry() == TopButton || geometry() == LeftButton) {
253 InsetText::metrics(mi, textdim);
254 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
256 // Correct for button width.
257 dim.wid += textdim.wid;
258 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
259 dim.asc = textdim.asc;
261 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
262 dim.wid = max(dim.wid, textdim.wid);
268 mi.base.font = tmpfont;
272 bool InsetCollapsable::setMouseHover(bool mouse_hover)
274 mouse_hover_ = mouse_hover;
279 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
281 BOOST_ASSERT(layout_);
283 autoOpen_ = pi.base.bv->cursor().isInside(this);
284 ColorCode const old_color = pi.background_color;
285 pi.background_color = backgroundColor();
287 FontInfo tmpfont = pi.base.font;
288 pi.base.font = layout_->font();
289 pi.base.font.realize(tmpfont);
291 // Draw button first -- top, left or only
292 Dimension dimc = dimensionCollapsed();
294 if (geometry() == TopButton ||
295 geometry() == LeftButton ||
296 geometry() == ButtonOnly) {
297 button_dim.x1 = x + 0;
298 button_dim.x2 = x + dimc.width();
299 button_dim.y1 = y - dimc.asc;
300 button_dim.y2 = y + dimc.des;
302 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
311 Dimension const textdim = InsetText::dimension(*pi.base.bv);
312 int const baseline = y;
314 switch (geometry()) {
316 textx = x + dimc.width();
318 InsetText::draw(pi, textx, texty);
322 texty = baseline + dimc.des + textdim.asc;
323 InsetText::draw(pi, textx, texty);
330 InsetText::draw(pi, textx, texty);
336 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
337 InsetText::draw(pi, textx, texty);
338 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
340 int desc = textdim.descent();
341 if (geometry() == Corners)
344 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
345 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
346 pi.pain.line(xx1, y + desc - 4,
348 layout_->labelfont().color());
350 pi.pain.line(xx1, y + desc,
352 layout_->labelfont().color());
354 // Make status_ value visible:
355 pi.pain.line(xx1, y + desc,
357 layout_->labelfont().color());
358 pi.pain.line(xx2 - 4, y + desc,
360 layout_->labelfont().color());
362 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
363 y + desc - 4, layout_->labelfont().color());
365 // the label below the text. Can be toggled.
366 if (geometry() == SubLabel) {
367 FontInfo font(layout_->labelfont());
368 font.realize(sane_font);
374 theFontMetrics(font).rectText(labelstring_, w, a, d);
375 int const ww = max(textdim.wid, w);
376 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
377 labelstring_, font, Color_none, Color_none);
381 // a visual cue when the cursor is inside the inset
382 Cursor & cur = pi.base.bv->cursor();
383 if (cur.isInside(this)) {
386 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
387 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
388 pi.pain.line(xx2, y + 4, xx2, y,
389 layout_->labelfont().color());
390 pi.pain.line(xx2 - 4, y, xx2, y,
391 layout_->labelfont().color());
395 pi.background_color = old_color;
397 pi.base.font = tmpfont;
401 void InsetCollapsable::cursorPos(BufferView const & bv,
402 CursorSlice const & sl, bool boundary, int & x, int & y) const
404 if (geometry() == ButtonOnly)
406 BOOST_ASSERT(geometry() != ButtonOnly);
408 InsetText::cursorPos(bv, sl, boundary, x, y);
409 Dimension const textdim = InsetText::dimension(bv);
411 switch (geometry()) {
413 x += dimensionCollapsed().wid;
416 y += dimensionCollapsed().des + textdim.asc;
431 Inset::EDITABLE InsetCollapsable::editable() const
433 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
437 bool InsetCollapsable::descendable() const
439 return geometry() != ButtonOnly;
443 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
445 return button_dim.contains(cmd.x, cmd.y);
449 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
452 pos_type const max_length = 15;
453 pos_type const p_siz = paragraphs().begin()->size();
454 pos_type const n = min(max_length, p_siz);
457 for (; i < n && j < p_siz; ++j) {
458 if (paragraphs().begin()->isInset(j))
460 label += paragraphs().begin()->getChar(j);
463 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
466 return label.empty() ? l : label;
470 void InsetCollapsable::edit(
471 Cursor & cur, bool front, EntryDirection entry_from)
473 //lyxerr << "InsetCollapsable: edit left/right" << endl;
475 InsetText::edit(cur, front, entry_from);
479 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
481 //lyxerr << "InsetCollapsable: edit xy" << endl;
482 if (geometry() == ButtonOnly
483 || (button_dim.contains(x, y)
484 && geometry() != NoButton))
487 return InsetText::editXY(cur, x, y);
491 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
493 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
494 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
496 switch (cmd.action) {
497 case LFUN_MOUSE_PRESS:
498 if (cmd.button() == mouse_button::button1
500 && geometry() != NoButton) {
501 // reset selection if necessary (see bug 3060)
503 cur.bv().cursor().clearSelection();
509 if (geometry() == NoButton)
510 InsetText::doDispatch(cur, cmd);
511 else if (geometry() != ButtonOnly
513 InsetText::doDispatch(cur, cmd);
518 case LFUN_MOUSE_MOTION:
519 case LFUN_MOUSE_DOUBLE:
520 case LFUN_MOUSE_TRIPLE:
521 if (geometry() == NoButton)
522 InsetText::doDispatch(cur, cmd);
523 else if (geometry() != ButtonOnly
525 InsetText::doDispatch(cur, cmd);
530 case LFUN_MOUSE_RELEASE:
531 if (cmd.button() == mouse_button::button3) {
532 // There is no button to right click:
533 if (decoration() == InsetLayout::Minimalistic ||
534 geometry() == Corners ||
535 geometry() == SubLabel ||
536 geometry() == NoButton
539 setStatus(cur, Collapsed);
541 setStatus(cur, Open);
545 // configuration dialog
546 showInsetDialog(&cur.bv());
551 if (geometry() == NoButton) {
552 // The mouse click has to be within the inset!
553 InsetText::doDispatch(cur, cmd);
557 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
558 // if we are selecting, we do not want to
562 // Left button is clicked, the user asks to
563 // toggle the inset visual state.
565 cur.updateFlags(Update::Force | Update::FitCursor);
566 if (geometry() == ButtonOnly) {
567 setStatus(cur, Open);
571 setStatus(cur, Collapsed);
573 cur.bv().cursor() = cur;
577 // The mouse click is within the opened inset.
578 if (geometry() == TopButton
579 || geometry() == LeftButton)
580 InsetText::doDispatch(cur, cmd);
583 case LFUN_INSET_TOGGLE:
584 if (cmd.argument() == "open")
585 setStatus(cur, Open);
586 else if (cmd.argument() == "close")
587 setStatus(cur, Collapsed);
588 else if (cmd.argument() == "toggle" || cmd.argument().empty())
589 if (status_ == Open) {
590 setStatus(cur, Collapsed);
591 if (geometry() == ButtonOnly)
592 cur.top().forwardPos();
594 setStatus(cur, Open);
595 else // if assign or anything else
601 case LFUN_CLIPBOARD_PASTE:
602 case LFUN_PRIMARY_SELECTION_PASTE: {
603 InsetText::doDispatch(cur, cmd);
604 // Since we can only store plain text, we must reset all
606 // FIXME: Change only the pasted paragraphs
608 resetParagraphsFont();
613 if (layout_ && layout_->isForceLtr()) {
614 // Force any new text to latex_language
615 // FIXME: This should only be necessary in constructor, but
616 // new paragraphs that are created by pressing enter at the
617 // start of an existing paragraph get the buffer language
618 // and not latex_language, so we take this brute force
620 cur.current_font.setLanguage(latex_language);
621 cur.real_current_font.setLanguage(latex_language);
623 InsetText::doDispatch(cur, cmd);
629 bool InsetCollapsable::allowMultiPar() const
631 return layout_->isMultiPar();
635 void InsetCollapsable::resetParagraphsFont()
638 font.fontInfo() = inherit_font;
639 if (layout_->isForceLtr())
640 font.setLanguage(latex_language);
641 if (layout_->isPassThru()) {
642 ParagraphList::iterator par = paragraphs().begin();
643 ParagraphList::iterator const end = paragraphs().end();
645 par->resetFonts(font);
646 par->params().clear();
653 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
654 FuncStatus & flag) const
656 switch (cmd.action) {
658 case LFUN_ACCENT_ACUTE:
659 case LFUN_ACCENT_BREVE:
660 case LFUN_ACCENT_CARON:
661 case LFUN_ACCENT_CEDILLA:
662 case LFUN_ACCENT_CIRCLE:
663 case LFUN_ACCENT_CIRCUMFLEX:
664 case LFUN_ACCENT_DOT:
665 case LFUN_ACCENT_GRAVE:
666 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
667 case LFUN_ACCENT_MACRON:
668 case LFUN_ACCENT_OGONEK:
669 case LFUN_ACCENT_SPECIAL_CARON:
670 case LFUN_ACCENT_TIE:
671 case LFUN_ACCENT_TILDE:
672 case LFUN_ACCENT_UMLAUT:
673 case LFUN_ACCENT_UNDERBAR:
674 case LFUN_ACCENT_UNDERDOT:
676 case LFUN_BIBITEM_INSERT:
677 case LFUN_BOX_INSERT:
678 case LFUN_BRANCH_INSERT:
680 case LFUN_CAPTION_INSERT:
681 case LFUN_CLEARPAGE_INSERT:
682 case LFUN_CLEARDOUBLEPAGE_INSERT:
683 case LFUN_DEPTH_DECREMENT:
684 case LFUN_DEPTH_INCREMENT:
685 case LFUN_ENVIRONMENT_INSERT:
686 case LFUN_ERT_INSERT:
687 case LFUN_FILE_INSERT:
688 case LFUN_FLEX_INSERT:
689 case LFUN_FLOAT_INSERT:
690 case LFUN_FLOAT_LIST:
691 case LFUN_FLOAT_WIDE_INSERT:
693 case LFUN_FONT_TYPEWRITER:
694 case LFUN_FONT_DEFAULT:
696 case LFUN_FONT_FREE_APPLY:
697 case LFUN_FONT_FREE_UPDATE:
699 case LFUN_FONT_ROMAN:
704 case LFUN_FONT_STATE:
705 case LFUN_FONT_UNDERLINE:
706 case LFUN_FOOTNOTE_INSERT:
707 case LFUN_HFILL_INSERT:
708 case LFUN_HYPERLINK_INSERT:
709 case LFUN_INDEX_INSERT:
710 case LFUN_INDEX_PRINT:
711 case LFUN_INSET_INSERT:
712 case LFUN_LABEL_GOTO:
713 case LFUN_LABEL_INSERT:
714 case LFUN_LINE_INSERT:
715 case LFUN_NEWPAGE_INSERT:
716 case LFUN_PAGEBREAK_INSERT:
718 case LFUN_LAYOUT_PARAGRAPH:
719 case LFUN_LAYOUT_TABULAR:
720 case LFUN_MARGINALNOTE_INSERT:
721 case LFUN_MATH_DISPLAY:
722 case LFUN_MATH_INSERT:
723 case LFUN_MATH_MATRIX:
727 case LFUN_NOMENCL_INSERT:
728 case LFUN_NOMENCL_PRINT:
729 case LFUN_NOTE_INSERT:
731 case LFUN_OPTIONAL_INSERT:
732 case LFUN_PARAGRAPH_PARAMS:
733 case LFUN_PARAGRAPH_PARAMS_APPLY:
734 case LFUN_PARAGRAPH_SPACING:
735 case LFUN_PARAGRAPH_UPDATE:
736 case LFUN_REFERENCE_NEXT:
737 case LFUN_SERVER_GOTO_FILE_ROW:
738 case LFUN_SERVER_NOTIFY:
739 case LFUN_SERVER_SET_XY:
740 case LFUN_SPACE_INSERT:
741 case LFUN_SPECIALCHAR_INSERT:
742 case LFUN_TABULAR_INSERT:
743 case LFUN_TOC_INSERT:
744 case LFUN_WRAP_INSERT:
745 if (layout_->isPassThru()) {
749 return InsetText::getStatus(cur, cmd, flag);
751 case LFUN_INSET_TOGGLE:
752 if (cmd.argument() == "open" || cmd.argument() == "close" ||
753 cmd.argument() == "toggle")
760 flag.enabled(!layout_->isForceLtr());
761 return InsetText::getStatus(cur, cmd, flag);
763 case LFUN_BREAK_PARAGRAPH:
764 case LFUN_BREAK_PARAGRAPH_SKIP:
765 flag.enabled(layout_->isMultiPar());
769 return InsetText::getStatus(cur, cmd, flag);
774 void InsetCollapsable::setLabel(docstring const & l)
780 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
784 if (status_ == Collapsed)
785 cur.leaveInset(*this);
789 docstring InsetCollapsable::floatName(
790 string const & type, BufferParams const & bp) const
792 FloatList const & floats = bp.documentClass().floats();
793 FloatList::const_iterator it = floats[type];
795 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
799 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
802 return InsetLayout::Classic;
803 InsetLayout::InsetDecoration const dec = layout_->decoration();
805 case InsetLayout::Classic:
806 case InsetLayout::Minimalistic:
807 case InsetLayout::Conglomerate:
809 case InsetLayout::Default:
812 if (lyxCode() == FLEX_CODE)
813 // FIXME: Is this really necessary?
814 return InsetLayout::Conglomerate;
815 return InsetLayout::Classic;
819 int InsetCollapsable::latex(odocstream & os,
820 OutputParams const & runparams) const
822 // FIXME: What should we do layout_ is 0?
824 // 2) through an error
828 // This implements the standard way of handling the LaTeX output of
829 // a collapsable inset, either a command or an environment. Standard
830 // collapsable insets should not redefine this, non-standard ones may
832 if (!layout_->latexname().empty()) {
833 if (layout_->latextype() == "command") {
835 if (runparams.moving_arg)
837 os << '\\' << from_utf8(layout_->latexname());
838 if (!layout_->latexparam().empty())
839 os << from_utf8(layout_->latexparam());
841 } else if (layout_->latextype() == "environment") {
842 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
843 if (!layout_->latexparam().empty())
844 os << from_utf8(layout_->latexparam());
847 OutputParams rp = runparams;
848 if (layout_->isPassThru())
850 if (layout_->isNeedProtect())
851 rp.moving_arg = true;
852 int i = InsetText::latex(os, rp);
853 if (!layout_->latexname().empty()) {
854 if (layout_->latextype() == "command") {
856 } else if (layout_->latextype() == "environment") {
857 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
865 void InsetCollapsable::validate(LaTeXFeatures & features) const
870 // Force inclusion of preamble snippet in layout file
871 features.require(to_utf8(layout_->name()));
872 InsetText::validate(features);
876 bool InsetCollapsable::undefined() const
878 docstring const & n = getLayout().name();
879 return n.empty() || n == DocumentClass::emptyInsetLayout().name();
883 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
886 if (geometry() != NoButton) {
887 Dimension dim = dimensionCollapsed();
888 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
892 return InsetText::contextMenu(bv, x, y);