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 : InsetText(buf), status_(Inset::Open),
81 openinlined_(false), autoOpen_(false), mouse_hover_(false)
83 DocumentClass const & dc = buf.params().documentClass();
85 setAutoBreakRows(true);
87 setFrameColor(Color_collapsableframe);
88 paragraphs().back().setPlainLayout(dc);
92 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
96 labelstring_(rhs.labelstring_),
97 button_dim(rhs.button_dim),
98 openinlined_(rhs.openinlined_),
99 autoOpen_(rhs.autoOpen_),
100 // the sole purpose of this copy constructor
106 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
108 Dimension dim = dimensionCollapsed();
109 if (geometry() == NoButton)
110 return translateIfPossible(layout_->labelstring());
111 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen())
114 OutputParams rp(&buffer().params().encoding());
115 odocstringstream ods;
116 InsetText::plaintext(ods, rp);
117 docstring content_tip = ods.str();
118 // shorten it if necessary
119 if (content_tip.size() > 200)
120 content_tip = content_tip.substr(0, 200) + "...";
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_ = translateIfPossible(layout_->labelstring());
137 layout_ = &DocumentClass::plainInsetLayout();
138 labelstring_ = _("UNDEFINED");
145 void InsetCollapsable::write(ostream & os) const
157 text().write(buffer(), os);
161 void InsetCollapsable::read(Lexer & lex)
163 lex.setContext("InsetCollapsable::read");
166 lex >> "status" >> tmp_token;
167 if (tmp_token == "open")
170 // this must be set before we enter InsetText::read()
171 setLayout(buffer().params());
172 InsetText::read(lex);
173 // set button label again as the inset contents was not read yet at
177 // Force default font, if so requested
178 // This avoids paragraphs in buffer language that would have a
179 // foreign language after a document language change, and it ensures
180 // that all new text in ERT and similar gets the "latex" language,
181 // since new text inherits the language from the last position of the
182 // existing text. As a side effect this makes us also robust against
183 // bugs in LyX that might lead to font changes in ERT in .lyx files.
184 resetParagraphsFont();
188 Dimension InsetCollapsable::dimensionCollapsed() const
190 LASSERT(layout_, /**/);
192 theFontMetrics(layout_->labelfont()).buttonText(
193 labelstring_, dim.wid, dim.asc, dim.des);
198 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
200 LASSERT(layout_, /**/);
202 autoOpen_ = mi.base.bv->cursor().isInside(this);
204 FontInfo tmpfont = mi.base.font;
205 mi.base.font = layout_->font();
206 mi.base.font.realize(tmpfont);
208 switch (geometry()) {
210 InsetText::metrics(mi, dim);
213 InsetText::metrics(mi, dim);
218 InsetText::metrics(mi, dim);
219 // consider width of the inset label
220 FontInfo font(layout_->labelfont());
221 font.realize(sane_font);
227 theFontMetrics(font).rectText(labelstring_, w, a, d);
234 dim = dimensionCollapsed();
235 if (geometry() == TopButton || geometry() == LeftButton) {
237 InsetText::metrics(mi, textdim);
238 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
240 // Correct for button width.
241 dim.wid += textdim.wid;
242 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
243 dim.asc = textdim.asc;
245 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
246 dim.wid = max(dim.wid, textdim.wid);
252 mi.base.font = tmpfont;
256 bool InsetCollapsable::setMouseHover(bool mouse_hover)
258 mouse_hover_ = mouse_hover;
263 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
265 LASSERT(layout_, /**/);
267 autoOpen_ = pi.base.bv->cursor().isInside(this);
269 FontInfo tmpfont = pi.base.font;
270 pi.base.font = layout_->font();
271 pi.base.font.realize(tmpfont);
273 // Draw button first -- top, left or only
274 Dimension dimc = dimensionCollapsed();
276 if (geometry() == TopButton ||
277 geometry() == LeftButton ||
278 geometry() == ButtonOnly) {
279 button_dim.x1 = x + 0;
280 button_dim.x2 = x + dimc.width();
281 button_dim.y1 = y - dimc.asc;
282 button_dim.y2 = y + dimc.des;
284 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
293 Dimension const textdim = InsetText::dimension(*pi.base.bv);
294 int const baseline = y;
296 switch (geometry()) {
298 textx = x + dimc.width();
300 InsetText::draw(pi, textx, texty);
304 texty = baseline + dimc.des + textdim.asc;
305 InsetText::draw(pi, textx, texty);
312 InsetText::draw(pi, textx, texty);
318 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
319 InsetText::draw(pi, textx, texty);
320 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
322 int desc = textdim.descent();
323 if (geometry() == Corners)
326 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
327 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
328 pi.pain.line(xx1, y + desc - 4,
330 layout_->labelfont().color());
332 pi.pain.line(xx1, y + desc,
334 layout_->labelfont().color());
336 // Make status_ value visible:
337 pi.pain.line(xx1, y + desc,
339 layout_->labelfont().color());
340 pi.pain.line(xx2 - 4, y + desc,
342 layout_->labelfont().color());
344 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
345 y + desc - 4, layout_->labelfont().color());
347 // the label below the text. Can be toggled.
348 if (geometry() == SubLabel) {
349 FontInfo font(layout_->labelfont());
350 font.realize(sane_font);
356 theFontMetrics(font).rectText(labelstring_, w, a, d);
357 int const ww = max(textdim.wid, w);
358 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
359 labelstring_, font, Color_none, Color_none);
363 // a visual cue when the cursor is inside the inset
364 Cursor & cur = pi.base.bv->cursor();
365 if (cur.isInside(this)) {
368 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
369 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
370 pi.pain.line(xx2, y + 4, xx2, y,
371 layout_->labelfont().color());
372 pi.pain.line(xx2 - 4, y, xx2, y,
373 layout_->labelfont().color());
378 pi.base.font = tmpfont;
382 void InsetCollapsable::cursorPos(BufferView const & bv,
383 CursorSlice const & sl, bool boundary, int & x, int & y) const
385 if (geometry() == ButtonOnly)
387 LASSERT(geometry() != ButtonOnly, /**/);
389 InsetText::cursorPos(bv, sl, boundary, x, y);
390 Dimension const textdim = InsetText::dimension(bv);
392 switch (geometry()) {
394 x += dimensionCollapsed().wid;
397 y += dimensionCollapsed().des + textdim.asc;
412 Inset::EDITABLE InsetCollapsable::editable() const
414 return geometry() != ButtonOnly ? HIGHLY_EDITABLE : IS_EDITABLE;
418 bool InsetCollapsable::descendable() const
420 return geometry() != ButtonOnly;
424 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
426 return button_dim.contains(cmd.x, cmd.y);
430 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
433 pos_type const max_length = 15;
434 pos_type const p_siz = paragraphs().begin()->size();
435 pos_type const n = min(max_length, p_siz);
438 for (; i < n && j < p_siz; ++j) {
439 if (paragraphs().begin()->isInset(j))
441 label += paragraphs().begin()->getChar(j);
444 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
447 return label.empty() ? l : label;
451 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
453 //lyxerr << "InsetCollapsable: edit left/right" << endl;
455 InsetText::edit(cur, front, entry_from);
459 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
461 //lyxerr << "InsetCollapsable: edit xy" << endl;
462 if (geometry() == ButtonOnly
463 || (button_dim.contains(x, y)
464 && geometry() != NoButton))
467 return InsetText::editXY(cur, x, y);
471 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
473 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
474 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
476 switch (cmd.action) {
477 case LFUN_MOUSE_PRESS:
478 if (hitButton(cmd)) {
479 switch (cmd.button()) {
480 case mouse_button::button1:
481 case mouse_button::button3:
482 // Pass the command to the enclosing InsetText,
483 // so that the cursor gets set.
486 case mouse_button::none:
487 case mouse_button::button2:
488 case mouse_button::button4:
489 case mouse_button::button5:
494 } else if (geometry() != ButtonOnly)
495 InsetText::doDispatch(cur, cmd);
500 case LFUN_MOUSE_MOTION:
501 case LFUN_MOUSE_DOUBLE:
502 case LFUN_MOUSE_TRIPLE:
505 else if (geometry() != ButtonOnly)
506 InsetText::doDispatch(cur, cmd);
511 case LFUN_MOUSE_RELEASE:
512 if (!hitButton(cmd)) {
513 // The mouse click has to be within the inset!
514 if (geometry() != ButtonOnly)
515 InsetText::doDispatch(cur, cmd);
520 if (cmd.button() != mouse_button::button1) {
525 // if we are selecting, we do not want to
529 // Left button is clicked, the user asks to
530 // toggle the inset visual state.
532 cur.updateFlags(Update::Force | Update::FitCursor);
533 if (geometry() == ButtonOnly) {
534 setStatus(cur, Open);
538 setStatus(cur, Collapsed);
539 cur.bv().cursor() = cur;
542 case LFUN_INSET_TOGGLE:
543 if (cmd.argument() == "open")
544 setStatus(cur, Open);
545 else if (cmd.argument() == "close")
546 setStatus(cur, Collapsed);
547 else if (cmd.argument() == "toggle" || cmd.argument().empty())
548 if (status_ == Open) {
549 setStatus(cur, Collapsed);
550 if (geometry() == ButtonOnly)
551 cur.top().forwardPos();
553 setStatus(cur, Open);
554 else // if assign or anything else
560 case LFUN_CLIPBOARD_PASTE:
561 case LFUN_PRIMARY_SELECTION_PASTE: {
562 InsetText::doDispatch(cur, cmd);
563 // Since we can only store plain text, we must reset all
565 // FIXME: Change only the pasted paragraphs
567 resetParagraphsFont();
572 if (layout_ && layout_->isForceLtr()) {
573 // Force any new text to latex_language
574 // FIXME: This should only be necessary in constructor, but
575 // new paragraphs that are created by pressing enter at the
576 // start of an existing paragraph get the buffer language
577 // and not latex_language, so we take this brute force
579 cur.current_font.setLanguage(latex_language);
580 cur.real_current_font.setLanguage(latex_language);
582 InsetText::doDispatch(cur, cmd);
588 bool InsetCollapsable::allowMultiPar() const
590 return layout_->isMultiPar();
594 void InsetCollapsable::resetParagraphsFont()
597 font.fontInfo() = inherit_font;
598 if (layout_->isForceLtr())
599 font.setLanguage(latex_language);
600 if (layout_->isPassThru()) {
601 ParagraphList::iterator par = paragraphs().begin();
602 ParagraphList::iterator const end = paragraphs().end();
604 par->resetFonts(font);
605 par->params().clear();
612 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
613 FuncStatus & flag) const
615 switch (cmd.action) {
616 // FIXME At present, these are being enabled and disabled according to
617 // whether PASSTHRU has been set in the InsetLayout. This makes some
618 // sense, but there are other checks that should really be done. E.g.,
619 // one should not be able to inset IndexPrint inside an optional argument!!
620 case LFUN_ACCENT_ACUTE:
621 case LFUN_ACCENT_BREVE:
622 case LFUN_ACCENT_CARON:
623 case LFUN_ACCENT_CEDILLA:
624 case LFUN_ACCENT_CIRCLE:
625 case LFUN_ACCENT_CIRCUMFLEX:
626 case LFUN_ACCENT_DOT:
627 case LFUN_ACCENT_GRAVE:
628 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
629 case LFUN_ACCENT_MACRON:
630 case LFUN_ACCENT_OGONEK:
631 case LFUN_ACCENT_TIE:
632 case LFUN_ACCENT_TILDE:
633 case LFUN_ACCENT_UMLAUT:
634 case LFUN_ACCENT_UNDERBAR:
635 case LFUN_ACCENT_UNDERDOT:
637 case LFUN_BOX_INSERT:
638 case LFUN_BRANCH_INSERT:
639 case LFUN_NEWLINE_INSERT:
640 case LFUN_CAPTION_INSERT:
641 case LFUN_DEPTH_DECREMENT:
642 case LFUN_DEPTH_INCREMENT:
643 case LFUN_ERT_INSERT:
644 case LFUN_FILE_INSERT:
645 case LFUN_FLEX_INSERT:
646 case LFUN_FLOAT_INSERT:
647 case LFUN_FLOAT_LIST_INSERT:
648 case LFUN_FLOAT_WIDE_INSERT:
650 case LFUN_FONT_BOLDSYMBOL:
651 case LFUN_FONT_TYPEWRITER:
652 case LFUN_FONT_DEFAULT:
654 case LFUN_TEXTSTYLE_APPLY:
655 case LFUN_TEXTSTYLE_UPDATE:
657 case LFUN_FONT_ROMAN:
662 case LFUN_FONT_STATE:
663 case LFUN_FONT_UNDERLINE:
664 case LFUN_FOOTNOTE_INSERT:
665 case LFUN_HYPERLINK_INSERT:
666 case LFUN_INDEX_INSERT:
667 case LFUN_INDEX_PRINT:
668 case LFUN_INSET_INSERT:
669 case LFUN_LABEL_GOTO:
670 case LFUN_LABEL_INSERT:
671 case LFUN_LINE_INSERT:
672 case LFUN_NEWPAGE_INSERT:
673 case LFUN_LAYOUT_TABULAR:
674 case LFUN_MARGINALNOTE_INSERT:
675 case LFUN_MATH_DISPLAY:
676 case LFUN_MATH_INSERT:
677 case LFUN_MATH_MATRIX:
681 case LFUN_NOMENCL_INSERT:
682 case LFUN_NOMENCL_PRINT:
683 case LFUN_NOTE_INSERT:
685 case LFUN_OPTIONAL_INSERT:
686 case LFUN_PHANTOM_INSERT:
687 case LFUN_REFERENCE_NEXT:
688 case LFUN_SERVER_GOTO_FILE_ROW:
689 case LFUN_SERVER_NOTIFY:
690 case LFUN_SERVER_SET_XY:
691 case LFUN_SPACE_INSERT:
692 case LFUN_SPECIALCHAR_INSERT:
693 case LFUN_TABULAR_INSERT:
694 case LFUN_TOC_INSERT:
695 case LFUN_WRAP_INSERT:
696 if (layout_->isPassThru()) {
697 flag.setEnabled(false);
700 return InsetText::getStatus(cur, cmd, flag);
702 case LFUN_INSET_TOGGLE:
703 if (cmd.argument() == "open")
704 flag.setEnabled(status_ != Open);
705 else if (cmd.argument() == "close")
706 flag.setEnabled(status_ == Open);
707 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
708 flag.setEnabled(true);
709 flag.setOnOff(status_ == Open);
711 flag.setEnabled(false);
715 flag.setEnabled(!layout_->isForceLtr());
716 return InsetText::getStatus(cur, cmd, flag);
718 case LFUN_BREAK_PARAGRAPH:
719 flag.setEnabled(layout_->isMultiPar());
723 return InsetText::getStatus(cur, cmd, flag);
728 void InsetCollapsable::setLabel(docstring const & l)
734 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
738 if (status_ == Collapsed) {
739 cur.leaveInset(*this);
740 mouse_hover_ = false;
745 docstring InsetCollapsable::floatName(
746 string const & type, BufferParams const & bp) const
748 FloatList const & floats = bp.documentClass().floats();
749 FloatList::const_iterator it = floats[type];
751 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
755 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
758 return InsetLayout::CLASSIC;
759 InsetLayout::InsetDecoration const dec = layout_->decoration();
761 case InsetLayout::CLASSIC:
762 case InsetLayout::MINIMALISTIC:
763 case InsetLayout::CONGLOMERATE:
765 case InsetLayout::DEFAULT:
768 if (lyxCode() == FLEX_CODE)
769 return InsetLayout::CONGLOMERATE;
770 return InsetLayout::CLASSIC;
774 int InsetCollapsable::latex(odocstream & os,
775 OutputParams const & runparams) const
777 // FIXME: What should we do layout_ is 0?
779 // 2) through an error
783 // This implements the standard way of handling the LaTeX output of
784 // a collapsable inset, either a command or an environment. Standard
785 // collapsable insets should not redefine this, non-standard ones may
787 if (!layout_->latexname().empty()) {
788 if (layout_->latextype() == InsetLayout::COMMAND) {
790 if (runparams.moving_arg)
792 os << '\\' << from_utf8(layout_->latexname());
793 if (!layout_->latexparam().empty())
794 os << from_utf8(layout_->latexparam());
796 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
797 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
798 if (!layout_->latexparam().empty())
799 os << from_utf8(layout_->latexparam());
802 OutputParams rp = runparams;
803 if (layout_->isPassThru())
805 if (layout_->isNeedProtect())
806 rp.moving_arg = true;
807 int i = InsetText::latex(os, rp);
808 if (!layout_->latexname().empty()) {
809 if (layout_->latextype() == InsetLayout::COMMAND) {
811 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
812 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
820 void InsetCollapsable::validate(LaTeXFeatures & features) const
822 string const preamble = getLayout().preamble();
823 if (!preamble.empty())
824 features.addPreambleSnippet(preamble);
825 features.require(getLayout().requires());
826 InsetText::validate(features);
830 bool InsetCollapsable::undefined() const
832 docstring const & n = getLayout().name();
833 return n.empty() || n == DocumentClass::plainInsetLayout().name();
837 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
840 if (decoration() == InsetLayout::CONGLOMERATE)
841 return from_ascii("context-conglomerate");
843 if (geometry() == NoButton)
844 return from_ascii("context-collapsable");
846 Dimension dim = dimensionCollapsed();
847 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
848 return from_ascii("context-collapsable");
850 return InsetText::contextMenu(bv, x, y);