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)
80 : InsetText(buf), status_(status),
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().setLayout(dc.emptyLayout());
92 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
95 labelstring_(rhs.labelstring_),
96 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 layout_->labelstring();
111 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
116 return _("Left-click to collapse the inset");
118 return _("Left-click to open the inset");
124 void InsetCollapsable::setLayout(BufferParams const & bp)
126 setLayout(bp.documentClassPtr());
130 void InsetCollapsable::setLayout(DocumentClass const * const dc)
133 layout_ = &(dc->insetLayout(name()));
134 labelstring_ = layout_->labelstring();
136 layout_ = &DocumentClass::emptyInsetLayout();
137 labelstring_ = _("UNDEFINED");
144 void InsetCollapsable::write(ostream & os) const
156 text_.write(buffer(), os);
160 void InsetCollapsable::read(Lexer & lex)
162 lex.setContext("InsetCollapsable::read");
165 lex >> "status" >> tmp_token;
166 if (tmp_token == "open")
169 // this must be set before we enter InsetText::read()
170 setLayout(buffer().params());
172 InsetText::read(lex);
174 // Force default font, if so requested
175 // This avoids paragraphs in buffer language that would have a
176 // foreign language after a document language change, and it ensures
177 // that all new text in ERT and similar gets the "latex" language,
178 // since new text inherits the language from the last position of the
179 // existing text. As a side effect this makes us also robust against
180 // bugs in LyX that might lead to font changes in ERT in .lyx files.
181 resetParagraphsFont();
185 Dimension InsetCollapsable::dimensionCollapsed() const
187 BOOST_ASSERT(layout_);
189 theFontMetrics(layout_->labelfont()).buttonText(
190 labelstring_, dim.wid, dim.asc, dim.des);
195 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
197 BOOST_ASSERT(layout_);
199 autoOpen_ = mi.base.bv->cursor().isInside(this);
201 FontInfo tmpfont = mi.base.font;
202 mi.base.font = layout_->font();
203 mi.base.font.realize(tmpfont);
205 switch (geometry()) {
207 InsetText::metrics(mi, dim);
210 InsetText::metrics(mi, dim);
215 InsetText::metrics(mi, dim);
216 // consider width of the inset label
217 FontInfo font(layout_->labelfont());
218 font.realize(sane_font);
224 theFontMetrics(font).rectText(labelstring_, w, a, d);
231 dim = dimensionCollapsed();
232 if (geometry() == TopButton || geometry() == LeftButton) {
234 InsetText::metrics(mi, textdim);
235 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
237 // Correct for button width.
238 dim.wid += textdim.wid;
239 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
240 dim.asc = textdim.asc;
242 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
243 dim.wid = max(dim.wid, textdim.wid);
249 mi.base.font = tmpfont;
253 bool InsetCollapsable::setMouseHover(bool mouse_hover)
255 mouse_hover_ = mouse_hover;
260 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
262 BOOST_ASSERT(layout_);
264 autoOpen_ = pi.base.bv->cursor().isInside(this);
265 ColorCode const old_color = pi.background_color;
266 pi.background_color = backgroundColor();
268 FontInfo tmpfont = pi.base.font;
269 pi.base.font = layout_->font();
270 pi.base.font.realize(tmpfont);
272 // Draw button first -- top, left or only
273 Dimension dimc = dimensionCollapsed();
275 if (geometry() == TopButton ||
276 geometry() == LeftButton ||
277 geometry() == ButtonOnly) {
278 button_dim.x1 = x + 0;
279 button_dim.x2 = x + dimc.width();
280 button_dim.y1 = y - dimc.asc;
281 button_dim.y2 = y + dimc.des;
283 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
292 Dimension const textdim = InsetText::dimension(*pi.base.bv);
293 int const baseline = y;
295 switch (geometry()) {
297 textx = x + dimc.width();
299 InsetText::draw(pi, textx, texty);
303 texty = baseline + dimc.des + textdim.asc;
304 InsetText::draw(pi, textx, texty);
311 InsetText::draw(pi, textx, texty);
317 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
318 InsetText::draw(pi, textx, texty);
319 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
321 int desc = textdim.descent();
322 if (geometry() == Corners)
325 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
326 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
327 pi.pain.line(xx1, y + desc - 4,
329 layout_->labelfont().color());
331 pi.pain.line(xx1, y + desc,
333 layout_->labelfont().color());
335 // Make status_ value visible:
336 pi.pain.line(xx1, y + desc,
338 layout_->labelfont().color());
339 pi.pain.line(xx2 - 4, y + desc,
341 layout_->labelfont().color());
343 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
344 y + desc - 4, layout_->labelfont().color());
346 // the label below the text. Can be toggled.
347 if (geometry() == SubLabel) {
348 FontInfo font(layout_->labelfont());
349 font.realize(sane_font);
355 theFontMetrics(font).rectText(labelstring_, w, a, d);
356 int const ww = max(textdim.wid, w);
357 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
358 labelstring_, font, Color_none, Color_none);
362 // a visual cue when the cursor is inside the inset
363 Cursor & cur = pi.base.bv->cursor();
364 if (cur.isInside(this)) {
367 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
368 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
369 pi.pain.line(xx2, y + 4, xx2, y,
370 layout_->labelfont().color());
371 pi.pain.line(xx2 - 4, y, xx2, y,
372 layout_->labelfont().color());
376 pi.background_color = old_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 BOOST_ASSERT(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(
452 Cursor & cur, bool front, EntryDirection entry_from)
454 //lyxerr << "InsetCollapsable: edit left/right" << endl;
456 InsetText::edit(cur, front, entry_from);
460 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
462 //lyxerr << "InsetCollapsable: edit xy" << endl;
463 if (geometry() == ButtonOnly
464 || (button_dim.contains(x, y)
465 && geometry() != NoButton))
468 return InsetText::editXY(cur, x, y);
472 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
474 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
475 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
477 switch (cmd.action) {
478 case LFUN_MOUSE_PRESS:
479 if (hitButton(cmd) && geometry() != NoButton) {
480 switch (cmd.button()) {
481 case mouse_button::button1:
482 // reset selection if necessary (see bug 3060)
484 cur.bv().cursor().clearSelection();
489 case mouse_button::none:
490 case mouse_button::button2:
491 case mouse_button::button3:
492 case mouse_button::button4:
493 case mouse_button::button5:
499 if (geometry() == NoButton
500 || (geometry() != ButtonOnly && !hitButton(cmd)))
501 InsetText::doDispatch(cur, cmd);
506 case LFUN_MOUSE_MOTION:
507 case LFUN_MOUSE_DOUBLE:
508 case LFUN_MOUSE_TRIPLE:
509 if (geometry() == NoButton)
510 InsetText::doDispatch(cur, cmd);
511 else if (geometry() != ButtonOnly
513 InsetText::doDispatch(cur, cmd);
518 case LFUN_MOUSE_RELEASE:
519 if (geometry() == NoButton || !hitButton(cmd)) {
520 // The mouse click has to be within the inset!
521 InsetText::doDispatch(cur, cmd);
524 if (cmd.button() != mouse_button::button1) {
528 // if we are selecting, we do not want to
532 // Left button is clicked, the user asks to
533 // toggle the inset visual state.
535 cur.updateFlags(Update::Force | Update::FitCursor);
536 if (geometry() == ButtonOnly) {
537 setStatus(cur, Open);
541 setStatus(cur, Collapsed);
542 cur.bv().cursor() = cur;
545 case LFUN_INSET_TOGGLE:
546 if (cmd.argument() == "open")
547 setStatus(cur, Open);
548 else if (cmd.argument() == "close")
549 setStatus(cur, Collapsed);
550 else if (cmd.argument() == "toggle" || cmd.argument().empty())
551 if (status_ == Open) {
552 setStatus(cur, Collapsed);
553 if (geometry() == ButtonOnly)
554 cur.top().forwardPos();
556 setStatus(cur, Open);
557 else // if assign or anything else
563 case LFUN_CLIPBOARD_PASTE:
564 case LFUN_PRIMARY_SELECTION_PASTE: {
565 InsetText::doDispatch(cur, cmd);
566 // Since we can only store plain text, we must reset all
568 // FIXME: Change only the pasted paragraphs
570 resetParagraphsFont();
575 if (layout_ && layout_->isForceLtr()) {
576 // Force any new text to latex_language
577 // FIXME: This should only be necessary in constructor, but
578 // new paragraphs that are created by pressing enter at the
579 // start of an existing paragraph get the buffer language
580 // and not latex_language, so we take this brute force
582 cur.current_font.setLanguage(latex_language);
583 cur.real_current_font.setLanguage(latex_language);
585 InsetText::doDispatch(cur, cmd);
591 bool InsetCollapsable::allowMultiPar() const
593 return layout_->isMultiPar();
597 void InsetCollapsable::resetParagraphsFont()
600 font.fontInfo() = inherit_font;
601 if (layout_->isForceLtr())
602 font.setLanguage(latex_language);
603 if (layout_->isPassThru()) {
604 ParagraphList::iterator par = paragraphs().begin();
605 ParagraphList::iterator const end = paragraphs().end();
607 par->resetFonts(font);
608 par->params().clear();
615 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
616 FuncStatus & flag) const
618 switch (cmd.action) {
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_SPECIAL_CARON:
632 case LFUN_ACCENT_TIE:
633 case LFUN_ACCENT_TILDE:
634 case LFUN_ACCENT_UMLAUT:
635 case LFUN_ACCENT_UNDERBAR:
636 case LFUN_ACCENT_UNDERDOT:
638 case LFUN_BIBITEM_INSERT:
639 case LFUN_BOX_INSERT:
640 case LFUN_BRANCH_INSERT:
641 case LFUN_NEWLINE_INSERT:
642 case LFUN_CAPTION_INSERT:
643 case LFUN_DEPTH_DECREMENT:
644 case LFUN_DEPTH_INCREMENT:
645 case LFUN_ENVIRONMENT_INSERT:
646 case LFUN_ERT_INSERT:
647 case LFUN_FILE_INSERT:
648 case LFUN_FLEX_INSERT:
649 case LFUN_FLOAT_INSERT:
650 case LFUN_FLOAT_LIST:
651 case LFUN_FLOAT_WIDE_INSERT:
653 case LFUN_FONT_TYPEWRITER:
654 case LFUN_FONT_DEFAULT:
656 case LFUN_FONT_FREE_APPLY:
657 case LFUN_FONT_FREE_UPDATE:
659 case LFUN_FONT_ROMAN:
664 case LFUN_FONT_STATE:
665 case LFUN_FONT_UNDERLINE:
666 case LFUN_FOOTNOTE_INSERT:
667 case LFUN_HYPERLINK_INSERT:
668 case LFUN_INDEX_INSERT:
669 case LFUN_INDEX_PRINT:
670 case LFUN_INSET_INSERT:
671 case LFUN_LABEL_GOTO:
672 case LFUN_LABEL_INSERT:
673 case LFUN_LINE_INSERT:
674 case LFUN_NEWPAGE_INSERT:
676 case LFUN_LAYOUT_PARAGRAPH:
677 case LFUN_LAYOUT_TABULAR:
678 case LFUN_MARGINALNOTE_INSERT:
679 case LFUN_MATH_DISPLAY:
680 case LFUN_MATH_INSERT:
681 case LFUN_MATH_MATRIX:
685 case LFUN_NOMENCL_INSERT:
686 case LFUN_NOMENCL_PRINT:
687 case LFUN_NOTE_INSERT:
689 case LFUN_OPTIONAL_INSERT:
690 case LFUN_PARAGRAPH_PARAMS:
691 case LFUN_PARAGRAPH_PARAMS_APPLY:
692 case LFUN_PARAGRAPH_SPACING:
693 case LFUN_PARAGRAPH_UPDATE:
694 case LFUN_REFERENCE_NEXT:
695 case LFUN_SERVER_GOTO_FILE_ROW:
696 case LFUN_SERVER_NOTIFY:
697 case LFUN_SERVER_SET_XY:
698 case LFUN_SPACE_INSERT:
699 case LFUN_SPECIALCHAR_INSERT:
700 case LFUN_TABULAR_INSERT:
701 case LFUN_TOC_INSERT:
702 case LFUN_WRAP_INSERT:
703 if (layout_->isPassThru()) {
707 return InsetText::getStatus(cur, cmd, flag);
709 case LFUN_INSET_TOGGLE:
710 if ((cmd.argument() == "open" && status_ != Open)
711 || (cmd.argument() == "close" && status_ == Open)
712 || cmd.argument() == "toggle" || cmd.argument().empty())
719 flag.enabled(!layout_->isForceLtr());
720 return InsetText::getStatus(cur, cmd, flag);
722 case LFUN_BREAK_PARAGRAPH:
723 case LFUN_BREAK_PARAGRAPH_SKIP:
724 flag.enabled(layout_->isMultiPar());
728 return InsetText::getStatus(cur, cmd, flag);
733 void InsetCollapsable::setLabel(docstring const & l)
739 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
743 if (status_ == Collapsed)
744 cur.leaveInset(*this);
748 docstring InsetCollapsable::floatName(
749 string const & type, BufferParams const & bp) const
751 FloatList const & floats = bp.documentClass().floats();
752 FloatList::const_iterator it = floats[type];
754 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
758 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
761 return InsetLayout::Classic;
762 InsetLayout::InsetDecoration const dec = layout_->decoration();
764 case InsetLayout::Classic:
765 case InsetLayout::Minimalistic:
766 case InsetLayout::Conglomerate:
768 case InsetLayout::Default:
771 if (lyxCode() == FLEX_CODE)
772 return InsetLayout::Conglomerate;
773 return InsetLayout::Classic;
777 int InsetCollapsable::latex(odocstream & os,
778 OutputParams const & runparams) const
780 // FIXME: What should we do layout_ is 0?
782 // 2) through an error
786 // This implements the standard way of handling the LaTeX output of
787 // a collapsable inset, either a command or an environment. Standard
788 // collapsable insets should not redefine this, non-standard ones may
790 if (!layout_->latexname().empty()) {
791 if (layout_->latextype() == "command") {
793 if (runparams.moving_arg)
795 os << '\\' << from_utf8(layout_->latexname());
796 if (!layout_->latexparam().empty())
797 os << from_utf8(layout_->latexparam());
799 } else if (layout_->latextype() == "environment") {
800 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
801 if (!layout_->latexparam().empty())
802 os << from_utf8(layout_->latexparam());
805 OutputParams rp = runparams;
806 if (layout_->isPassThru())
808 if (layout_->isNeedProtect())
809 rp.moving_arg = true;
810 int i = InsetText::latex(os, rp);
811 if (!layout_->latexname().empty()) {
812 if (layout_->latextype() == "command") {
814 } else if (layout_->latextype() == "environment") {
815 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
823 void InsetCollapsable::validate(LaTeXFeatures & features) const
828 // Force inclusion of preamble snippet in layout file
829 features.require(to_utf8(layout_->name()));
830 InsetText::validate(features);
834 bool InsetCollapsable::undefined() const
836 docstring const & n = getLayout().name();
837 return n.empty() || n == DocumentClass::emptyInsetLayout().name();
841 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
844 if (decoration() == InsetLayout::Conglomerate)
845 return from_ascii("context-conglomerate");
847 if (geometry() == NoButton)
848 return from_ascii("context-collapsable");
850 Dimension dim = dimensionCollapsed();
851 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
852 return from_ascii("context-collapsable");
854 return InsetText::contextMenu(bv, x, y);