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 CollapseStatus status)
81 : InsetText(buf), status_(status),
82 openinlined_(false), autoOpen_(false), mouse_hover_(false)
84 DocumentClass const & dc = buf.params().documentClass();
86 setAutoBreakRows(true);
88 setFrameColor(Color_collapsableframe);
89 paragraphs().back().setLayout(dc.plainLayout());
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 translateIfPossible(layout_->labelstring());
112 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen())
115 OutputParams rp(&buffer().params().encoding());
116 odocstringstream ods;
117 InsetText::plaintext(ods, rp);
118 docstring content_tip = ods.str();
119 // shorten it if necessary
120 if (content_tip.size() > 200)
121 content_tip = content_tip.substr(0, 200) + "...";
126 void InsetCollapsable::setLayout(BufferParams const & bp)
128 setLayout(bp.documentClassPtr());
132 void InsetCollapsable::setLayout(DocumentClass const * const dc)
135 layout_ = &(dc->insetLayout(name()));
136 labelstring_ = translateIfPossible(layout_->labelstring());
138 layout_ = &DocumentClass::plainInsetLayout();
139 labelstring_ = _("UNDEFINED");
146 void InsetCollapsable::write(ostream & os) const
158 text().write(buffer(), os);
162 void InsetCollapsable::read(Lexer & lex)
164 lex.setContext("InsetCollapsable::read");
167 lex >> "status" >> tmp_token;
168 if (tmp_token == "open")
171 // this must be set before we enter InsetText::read()
172 setLayout(buffer().params());
174 InsetText::read(lex);
176 // Force default font, if so requested
177 // This avoids paragraphs in buffer language that would have a
178 // foreign language after a document language change, and it ensures
179 // that all new text in ERT and similar gets the "latex" language,
180 // since new text inherits the language from the last position of the
181 // existing text. As a side effect this makes us also robust against
182 // bugs in LyX that might lead to font changes in ERT in .lyx files.
183 resetParagraphsFont();
187 Dimension InsetCollapsable::dimensionCollapsed() const
189 LASSERT(layout_, /**/);
191 theFontMetrics(layout_->labelfont()).buttonText(
192 labelstring_, dim.wid, dim.asc, dim.des);
197 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
199 LASSERT(layout_, /**/);
201 autoOpen_ = mi.base.bv->cursor().isInside(this);
203 FontInfo tmpfont = mi.base.font;
204 mi.base.font = layout_->font();
205 mi.base.font.realize(tmpfont);
207 switch (geometry()) {
209 InsetText::metrics(mi, dim);
212 InsetText::metrics(mi, dim);
217 InsetText::metrics(mi, dim);
218 // consider width of the inset label
219 FontInfo font(layout_->labelfont());
220 font.realize(sane_font);
226 theFontMetrics(font).rectText(labelstring_, w, a, d);
233 dim = dimensionCollapsed();
234 if (geometry() == TopButton || geometry() == LeftButton) {
236 InsetText::metrics(mi, textdim);
237 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
239 // Correct for button width.
240 dim.wid += textdim.wid;
241 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
242 dim.asc = textdim.asc;
244 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
245 dim.wid = max(dim.wid, textdim.wid);
251 mi.base.font = tmpfont;
255 bool InsetCollapsable::setMouseHover(bool mouse_hover)
257 mouse_hover_ = mouse_hover;
262 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
264 LASSERT(layout_, /**/);
266 autoOpen_ = pi.base.bv->cursor().isInside(this);
267 ColorCode const old_color = pi.background_color;
268 pi.background_color = backgroundColor();
270 FontInfo tmpfont = pi.base.font;
271 pi.base.font = layout_->font();
272 pi.base.font.realize(tmpfont);
274 // Draw button first -- top, left or only
275 Dimension dimc = dimensionCollapsed();
277 if (geometry() == TopButton ||
278 geometry() == LeftButton ||
279 geometry() == ButtonOnly) {
280 button_dim.x1 = x + 0;
281 button_dim.x2 = x + dimc.width();
282 button_dim.y1 = y - dimc.asc;
283 button_dim.y2 = y + dimc.des;
285 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
294 Dimension const textdim = InsetText::dimension(*pi.base.bv);
295 int const baseline = y;
297 switch (geometry()) {
299 textx = x + dimc.width();
301 InsetText::draw(pi, textx, texty);
305 texty = baseline + dimc.des + textdim.asc;
306 InsetText::draw(pi, textx, texty);
313 InsetText::draw(pi, textx, texty);
319 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
320 InsetText::draw(pi, textx, texty);
321 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
323 int desc = textdim.descent();
324 if (geometry() == Corners)
327 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
328 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
329 pi.pain.line(xx1, y + desc - 4,
331 layout_->labelfont().color());
333 pi.pain.line(xx1, y + desc,
335 layout_->labelfont().color());
337 // Make status_ value visible:
338 pi.pain.line(xx1, y + desc,
340 layout_->labelfont().color());
341 pi.pain.line(xx2 - 4, y + desc,
343 layout_->labelfont().color());
345 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
346 y + desc - 4, layout_->labelfont().color());
348 // the label below the text. Can be toggled.
349 if (geometry() == SubLabel) {
350 FontInfo font(layout_->labelfont());
351 font.realize(sane_font);
357 theFontMetrics(font).rectText(labelstring_, w, a, d);
358 int const ww = max(textdim.wid, w);
359 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
360 labelstring_, font, Color_none, Color_none);
364 // a visual cue when the cursor is inside the inset
365 Cursor & cur = pi.base.bv->cursor();
366 if (cur.isInside(this)) {
369 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
370 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
371 pi.pain.line(xx2, y + 4, xx2, y,
372 layout_->labelfont().color());
373 pi.pain.line(xx2 - 4, y, xx2, y,
374 layout_->labelfont().color());
378 pi.background_color = old_color;
380 pi.base.font = tmpfont;
384 void InsetCollapsable::cursorPos(BufferView const & bv,
385 CursorSlice const & sl, bool boundary, int & x, int & y) const
387 if (geometry() == ButtonOnly)
389 LASSERT(geometry() != ButtonOnly, /**/);
391 InsetText::cursorPos(bv, sl, boundary, x, y);
392 Dimension const textdim = InsetText::dimension(bv);
394 switch (geometry()) {
396 x += dimensionCollapsed().wid;
399 y += dimensionCollapsed().des + textdim.asc;
414 Inset::EDITABLE InsetCollapsable::editable() const
416 return geometry() != ButtonOnly ? HIGHLY_EDITABLE : IS_EDITABLE;
420 bool InsetCollapsable::descendable() const
422 return geometry() != ButtonOnly;
426 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
428 return button_dim.contains(cmd.x, cmd.y);
432 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
435 pos_type const max_length = 15;
436 pos_type const p_siz = paragraphs().begin()->size();
437 pos_type const n = min(max_length, p_siz);
440 for (; i < n && j < p_siz; ++j) {
441 if (paragraphs().begin()->isInset(j))
443 label += paragraphs().begin()->getChar(j);
446 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
449 return label.empty() ? l : label;
453 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
455 //lyxerr << "InsetCollapsable: edit left/right" << endl;
457 InsetText::edit(cur, front, entry_from);
461 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
463 //lyxerr << "InsetCollapsable: edit xy" << endl;
464 if (geometry() == ButtonOnly
465 || (button_dim.contains(x, y)
466 && geometry() != NoButton))
469 return InsetText::editXY(cur, x, y);
473 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
475 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
476 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
478 switch (cmd.action) {
479 case LFUN_MOUSE_PRESS:
480 if (hitButton(cmd)) {
481 switch (cmd.button()) {
482 case mouse_button::button1:
483 case mouse_button::button3:
484 // Pass the command to the enclosing InsetText,
485 // so that the cursor gets set.
488 case mouse_button::none:
489 case mouse_button::button2:
490 case mouse_button::button4:
491 case mouse_button::button5:
496 } else if (geometry() != ButtonOnly)
497 InsetText::doDispatch(cur, cmd);
502 case LFUN_MOUSE_MOTION:
503 case LFUN_MOUSE_DOUBLE:
504 case LFUN_MOUSE_TRIPLE:
507 else if (geometry() != ButtonOnly)
508 InsetText::doDispatch(cur, cmd);
513 case LFUN_MOUSE_RELEASE:
514 if (!hitButton(cmd)) {
515 // The mouse click has to be within the inset!
516 if (geometry() != ButtonOnly)
517 InsetText::doDispatch(cur, cmd);
522 if (cmd.button() != mouse_button::button1) {
527 // if we are selecting, we do not want to
531 // Left button is clicked, the user asks to
532 // toggle the inset visual state.
534 cur.updateFlags(Update::Force | Update::FitCursor);
535 if (geometry() == ButtonOnly) {
536 setStatus(cur, Open);
540 setStatus(cur, Collapsed);
541 cur.bv().cursor() = cur;
544 case LFUN_INSET_TOGGLE:
545 if (cmd.argument() == "open")
546 setStatus(cur, Open);
547 else if (cmd.argument() == "close")
548 setStatus(cur, Collapsed);
549 else if (cmd.argument() == "toggle" || cmd.argument().empty())
550 if (status_ == Open) {
551 setStatus(cur, Collapsed);
552 if (geometry() == ButtonOnly)
553 cur.top().forwardPos();
555 setStatus(cur, Open);
556 else // if assign or anything else
562 case LFUN_CLIPBOARD_PASTE:
563 case LFUN_PRIMARY_SELECTION_PASTE: {
564 InsetText::doDispatch(cur, cmd);
565 // Since we can only store plain text, we must reset all
567 // FIXME: Change only the pasted paragraphs
569 resetParagraphsFont();
574 if (layout_ && layout_->isForceLtr()) {
575 // Force any new text to latex_language
576 // FIXME: This should only be necessary in constructor, but
577 // new paragraphs that are created by pressing enter at the
578 // start of an existing paragraph get the buffer language
579 // and not latex_language, so we take this brute force
581 cur.current_font.setLanguage(latex_language);
582 cur.real_current_font.setLanguage(latex_language);
584 InsetText::doDispatch(cur, cmd);
590 bool InsetCollapsable::allowMultiPar() const
592 return layout_->isMultiPar();
596 void InsetCollapsable::resetParagraphsFont()
599 font.fontInfo() = inherit_font;
600 if (layout_->isForceLtr())
601 font.setLanguage(latex_language);
602 if (layout_->isPassThru()) {
603 ParagraphList::iterator par = paragraphs().begin();
604 ParagraphList::iterator const end = paragraphs().end();
606 par->resetFonts(font);
607 par->params().clear();
614 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
615 FuncStatus & flag) const
617 switch (cmd.action) {
619 case LFUN_ACCENT_ACUTE:
620 case LFUN_ACCENT_BREVE:
621 case LFUN_ACCENT_CARON:
622 case LFUN_ACCENT_CEDILLA:
623 case LFUN_ACCENT_CIRCLE:
624 case LFUN_ACCENT_CIRCUMFLEX:
625 case LFUN_ACCENT_DOT:
626 case LFUN_ACCENT_GRAVE:
627 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
628 case LFUN_ACCENT_MACRON:
629 case LFUN_ACCENT_OGONEK:
630 case LFUN_ACCENT_TIE:
631 case LFUN_ACCENT_TILDE:
632 case LFUN_ACCENT_UMLAUT:
633 case LFUN_ACCENT_UNDERBAR:
634 case LFUN_ACCENT_UNDERDOT:
636 case LFUN_BOX_INSERT:
637 case LFUN_BRANCH_INSERT:
638 case LFUN_NEWLINE_INSERT:
639 case LFUN_CAPTION_INSERT:
640 case LFUN_DEPTH_DECREMENT:
641 case LFUN_DEPTH_INCREMENT:
642 case LFUN_ERT_INSERT:
643 case LFUN_FILE_INSERT:
644 case LFUN_FLEX_INSERT:
645 case LFUN_FLOAT_INSERT:
646 case LFUN_FLOAT_LIST_INSERT:
647 case LFUN_FLOAT_WIDE_INSERT:
649 case LFUN_FONT_TYPEWRITER:
650 case LFUN_FONT_DEFAULT:
652 case LFUN_TEXTSTYLE_APPLY:
653 case LFUN_TEXTSTYLE_UPDATE:
655 case LFUN_FONT_ROMAN:
660 case LFUN_FONT_STATE:
661 case LFUN_FONT_UNDERLINE:
662 case LFUN_FOOTNOTE_INSERT:
663 case LFUN_HYPERLINK_INSERT:
664 case LFUN_INDEX_INSERT:
665 case LFUN_INDEX_PRINT:
666 case LFUN_INSET_INSERT:
667 case LFUN_LABEL_GOTO:
668 case LFUN_LABEL_INSERT:
669 case LFUN_LINE_INSERT:
670 case LFUN_NEWPAGE_INSERT:
672 case LFUN_LAYOUT_PARAGRAPH:
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_PARAGRAPH_PARAMS:
687 case LFUN_PARAGRAPH_PARAMS_APPLY:
688 case LFUN_PARAGRAPH_SPACING:
689 case LFUN_PARAGRAPH_UPDATE:
690 case LFUN_REFERENCE_NEXT:
691 case LFUN_SERVER_GOTO_FILE_ROW:
692 case LFUN_SERVER_NOTIFY:
693 case LFUN_SERVER_SET_XY:
694 case LFUN_SPACE_INSERT:
695 case LFUN_SPECIALCHAR_INSERT:
696 case LFUN_TABULAR_INSERT:
697 case LFUN_TOC_INSERT:
698 case LFUN_WRAP_INSERT:
699 if (layout_->isPassThru()) {
700 flag.setEnabled(false);
703 return InsetText::getStatus(cur, cmd, flag);
705 case LFUN_INSET_TOGGLE:
706 if (cmd.argument() == "open")
707 flag.setEnabled(status_ != Open);
708 else if (cmd.argument() == "close")
709 flag.setEnabled(status_ == Open);
710 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
711 flag.setEnabled(true);
712 flag.setOnOff(status_ == Open);
714 flag.setEnabled(false);
718 flag.setEnabled(!layout_->isForceLtr());
719 return InsetText::getStatus(cur, cmd, flag);
721 case LFUN_BREAK_PARAGRAPH:
722 flag.setEnabled(layout_->isMultiPar());
726 return InsetText::getStatus(cur, cmd, flag);
731 void InsetCollapsable::setLabel(docstring const & l)
737 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
741 if (status_ == Collapsed) {
742 cur.leaveInset(*this);
743 mouse_hover_ = false;
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::plainInsetLayout().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);