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 "support/debug.h"
22 #include "Dimension.h"
23 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncStatus.h"
26 #include "support/gettext.h"
28 #include "LaTeXFeatures.h"
30 #include "FuncRequest.h"
31 #include "MetricsInfo.h"
32 #include "ParagraphParameters.h"
33 #include "TextClass.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
38 #include "support/docstream.h"
45 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
47 return autoOpen_ ? Open : status_;
51 InsetCollapsable::Geometry InsetCollapsable::geometry() const
53 switch (decoration()) {
55 if (status() == Open) {
64 return status() == Open ? NoButton : ButtonOnly ;
67 return status() == Open ? SubLabel : Corners ;
70 // dummy return value to shut down a warning,
76 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
77 CollapseStatus status, TextClassPtr tc)
78 : InsetText(bp), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
82 setAutoBreakRows(true);
84 setFrameColor(Color_collapsableframe);
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
90 textClass_(rhs.textClass_),
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 (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
111 return _("Left-click to collapse the inset");
113 return _("Left-click to open the inset");
119 void InsetCollapsable::setLayout(BufferParams const & bp)
121 setLayout(bp.getTextClassPtr());
125 void InsetCollapsable::setLayout(TextClassPtr tc)
128 if ( tc.get() != 0 ) {
129 layout_ = &tc->insetlayout(name());
130 labelstring_ = layout_->labelstring;
133 labelstring_ = _("UNDEFINED");
140 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
152 text_.write(buf, os);
156 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
158 bool token_found = false;
161 string const token = lex.getString();
162 if (token == "status") {
164 string const tmp_token = lex.getString();
166 if (tmp_token == "collapsed") {
169 } else if (tmp_token == "open") {
173 lyxerr << "InsetCollapsable::read: Missing status!"
175 // Take countermeasures
176 lex.pushToken(token);
179 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
180 // take countermeasures
181 lex.pushToken(token);
184 InsetText::read(buf, lex);
186 setLayout(buf.params());
189 status_ = isOpen() ? Open : Collapsed;
191 // Force default font, if so requested
192 // This avoids paragraphs in buffer language that would have a
193 // foreign language after a document language change, and it ensures
194 // that all new text in ERT and similar gets the "latex" language,
195 // since new text inherits the language from the last position of the
196 // existing text. As a side effect this makes us also robust against
197 // bugs in LyX that might lead to font changes in ERT in .lyx files.
198 resetParagraphsFont();
202 Dimension InsetCollapsable::dimensionCollapsed() const
204 BOOST_ASSERT(layout_);
206 theFontMetrics(layout_->labelfont).buttonText(
207 labelstring_, dim.wid, dim.asc, dim.des);
212 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
214 BOOST_ASSERT(layout_);
216 autoOpen_ = mi.base.bv->cursor().isInside(this);
218 FontInfo tmpfont = mi.base.font;
219 mi.base.font = layout_->font;
220 mi.base.font.realize(tmpfont);
222 switch (geometry()) {
224 InsetText::metrics(mi, dim);
227 InsetText::metrics(mi, dim);
232 InsetText::metrics(mi, dim);
233 // consider width of the inset label
234 FontInfo font(layout_->labelfont);
235 font.realize(sane_font);
241 theFontMetrics(font).rectText(labelstring_, w, a, d);
248 dim = dimensionCollapsed();
249 if (geometry() == TopButton
250 || geometry() == LeftButton) {
252 InsetText::metrics(mi, textdim);
253 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
255 // Correct for button width.
256 dim.wid += textdim.wid;
257 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
258 dim.asc = textdim.asc;
260 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
261 dim.wid = max(dim.wid, textdim.wid);
267 mi.base.font = tmpfont;
271 bool InsetCollapsable::setMouseHover(bool mouse_hover)
273 mouse_hover_ = mouse_hover;
278 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
280 BOOST_ASSERT(layout_);
282 autoOpen_ = pi.base.bv->cursor().isInside(this);
283 ColorCode const old_color = pi.background_color;
284 pi.background_color = backgroundColor();
286 FontInfo tmpfont = pi.base.font;
287 pi.base.font = layout_->font;
288 pi.base.font.realize(tmpfont);
290 // Draw button first -- top, left or only
291 Dimension dimc = dimensionCollapsed();
293 if (geometry() == TopButton ||
294 geometry() == LeftButton ||
295 geometry() == ButtonOnly) {
296 button_dim.x1 = x + 0;
297 button_dim.x2 = x + dimc.width();
298 button_dim.y1 = y - dimc.asc;
299 button_dim.y2 = y + dimc.des;
301 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
310 Dimension const textdim = InsetText::dimension(*pi.base.bv);
311 int const baseline = y;
313 switch (geometry()) {
315 textx = x + dimc.width();
317 InsetText::draw(pi, textx, texty);
321 texty = baseline + dimc.des + textdim.asc;
322 InsetText::draw(pi, textx, texty);
329 InsetText::draw(pi, textx, texty);
335 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
336 InsetText::draw(pi, textx, texty);
337 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
339 int desc = textdim.descent();
340 if (geometry() == Corners)
343 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
344 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
345 pi.pain.line(xx1, y + desc - 4,
347 layout_->labelfont.color());
349 pi.pain.line(xx1, y + desc,
351 layout_->labelfont.color());
353 // Make status_ value visible:
354 pi.pain.line(xx1, y + desc,
356 layout_->labelfont.color());
357 pi.pain.line(xx2 - 4, y + desc,
359 layout_->labelfont.color());
361 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
362 layout_->labelfont.color());
364 // the label below the text. Can be toggled.
365 if (geometry() == SubLabel) {
366 FontInfo font(layout_->labelfont);
367 font.realize(sane_font);
373 theFontMetrics(font).rectText(labelstring_, w, a, d);
374 int const ww = max(textdim.wid, w);
375 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
376 labelstring_, font, Color_none, Color_none);
380 // a visual cue when the cursor is inside the inset
381 Cursor & cur = pi.base.bv->cursor();
382 if (cur.isInside(this)) {
385 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
386 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
387 pi.pain.line(xx2, y + 4, xx2, y,
388 layout_->labelfont.color());
389 pi.pain.line(xx2 - 4, y, xx2, y,
390 layout_->labelfont.color());
394 pi.background_color = old_color;
396 pi.base.font = tmpfont;
400 void InsetCollapsable::cursorPos(BufferView const & bv,
401 CursorSlice const & sl, bool boundary, int & x, int & y) const
403 if (geometry() == ButtonOnly)
405 BOOST_ASSERT(geometry() != ButtonOnly);
407 InsetText::cursorPos(bv, sl, boundary, x, y);
408 Dimension const textdim = InsetText::dimension(bv);
410 switch (geometry()) {
412 x += dimensionCollapsed().wid;
415 y += dimensionCollapsed().des + textdim.asc;
430 Inset::EDITABLE InsetCollapsable::editable() const
432 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
436 bool InsetCollapsable::descendable() const
438 return geometry() != ButtonOnly;
442 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
444 return button_dim.contains(cmd.x, cmd.y);
448 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
451 pos_type const max_length = 15;
452 pos_type const p_siz = paragraphs().begin()->size();
453 pos_type const n = min(max_length, p_siz);
456 for (; i < n && j < p_siz; ++j) {
457 if (paragraphs().begin()->isInset(j))
459 label += paragraphs().begin()->getChar(j);
462 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
465 return label.empty() ? l : label;
469 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
471 //lyxerr << "InsetCollapsable: edit left/right" << endl;
473 InsetText::edit(cur, front, entry_from);
477 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
479 //lyxerr << "InsetCollapsable: edit xy" << endl;
480 if (geometry() == ButtonOnly
481 || (button_dim.contains(x, y)
482 && geometry() != NoButton))
485 return InsetText::editXY(cur, x, y);
489 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
491 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
492 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
494 switch (cmd.action) {
495 case LFUN_MOUSE_PRESS:
496 if (cmd.button() == mouse_button::button1
498 && geometry() != NoButton) {
499 // reset selection if necessary (see bug 3060)
501 cur.bv().cursor().clearSelection();
507 if (geometry() == NoButton)
508 InsetText::doDispatch(cur, cmd);
509 else if (geometry() != ButtonOnly
511 InsetText::doDispatch(cur, cmd);
516 case LFUN_MOUSE_MOTION:
517 case LFUN_MOUSE_DOUBLE:
518 case LFUN_MOUSE_TRIPLE:
519 if (geometry() == NoButton)
520 InsetText::doDispatch(cur, cmd);
521 else if (geometry() != ButtonOnly
523 InsetText::doDispatch(cur, cmd);
528 case LFUN_MOUSE_RELEASE:
529 if (cmd.button() == mouse_button::button3) {
530 // There is no button to right click:
531 if (decoration() == Minimalistic ||
532 geometry() == Corners ||
533 geometry() == SubLabel ||
534 geometry() == NoButton
537 setStatus(cur, Collapsed);
539 setStatus(cur, Open);
543 // configuration dialog
544 showInsetDialog(&cur.bv());
549 if (geometry() == NoButton) {
550 // The mouse click has to be within the inset!
551 InsetText::doDispatch(cur, cmd);
555 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
556 // if we are selecting, we do not want to
560 // Left button is clicked, the user asks to
561 // toggle the inset visual state.
563 cur.updateFlags(Update::Force | Update::FitCursor);
564 if (geometry() == ButtonOnly) {
565 setStatus(cur, Open);
569 setStatus(cur, Collapsed);
571 cur.bv().cursor() = cur;
575 // The mouse click is within the opened inset.
576 if (geometry() == TopButton
577 || geometry() == LeftButton)
578 InsetText::doDispatch(cur, cmd);
581 case LFUN_INSET_TOGGLE:
582 if (cmd.argument() == "open")
583 setStatus(cur, Open);
584 else if (cmd.argument() == "close")
585 setStatus(cur, Collapsed);
586 else if (cmd.argument() == "toggle" || cmd.argument().empty())
587 if (status_ == Open) {
588 setStatus(cur, Collapsed);
589 if (geometry() == ButtonOnly)
590 cur.top().forwardPos();
592 setStatus(cur, Open);
593 else // if assign or anything else
599 case LFUN_CLIPBOARD_PASTE:
600 case LFUN_PRIMARY_SELECTION_PASTE: {
601 InsetText::doDispatch(cur, cmd);
602 // Since we can only store plain text, we must reset all
604 // FIXME: Change only the pasted paragraphs
606 resetParagraphsFont();
611 if (layout_ && layout_->forceltr) {
612 // Force any new text to latex_language
613 // FIXME: This should only be necessary in constructor, but
614 // new paragraphs that are created by pressing enter at the
615 // start of an existing paragraph get the buffer language
616 // and not latex_language, so we take this brute force
618 cur.current_font.setLanguage(latex_language);
619 cur.real_current_font.setLanguage(latex_language);
621 InsetText::doDispatch(cur, cmd);
627 bool InsetCollapsable::allowMultiPar() const
629 return layout_->multipar;
633 void InsetCollapsable::resetParagraphsFont()
636 font.fontInfo() = inherit_font;
637 if (layout_->forceltr)
638 font.setLanguage(latex_language);
639 if (layout_->passthru) {
640 ParagraphList::iterator par = paragraphs().begin();
641 ParagraphList::iterator const end = paragraphs().end();
643 par->resetFonts(font);
644 par->params().clear();
651 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
652 FuncStatus & flag) const
654 switch (cmd.action) {
656 case LFUN_ACCENT_ACUTE:
657 case LFUN_ACCENT_BREVE:
658 case LFUN_ACCENT_CARON:
659 case LFUN_ACCENT_CEDILLA:
660 case LFUN_ACCENT_CIRCLE:
661 case LFUN_ACCENT_CIRCUMFLEX:
662 case LFUN_ACCENT_DOT:
663 case LFUN_ACCENT_GRAVE:
664 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
665 case LFUN_ACCENT_MACRON:
666 case LFUN_ACCENT_OGONEK:
667 case LFUN_ACCENT_SPECIAL_CARON:
668 case LFUN_ACCENT_TIE:
669 case LFUN_ACCENT_TILDE:
670 case LFUN_ACCENT_UMLAUT:
671 case LFUN_ACCENT_UNDERBAR:
672 case LFUN_ACCENT_UNDERDOT:
674 case LFUN_BIBITEM_INSERT:
675 case LFUN_BOX_INSERT:
676 case LFUN_BRANCH_INSERT:
678 case LFUN_CAPTION_INSERT:
679 case LFUN_CLEARPAGE_INSERT:
680 case LFUN_CLEARDOUBLEPAGE_INSERT:
681 case LFUN_DEPTH_DECREMENT:
682 case LFUN_DEPTH_INCREMENT:
683 case LFUN_ENVIRONMENT_INSERT:
684 case LFUN_ERT_INSERT:
685 case LFUN_FILE_INSERT:
686 case LFUN_FLEX_INSERT:
687 case LFUN_FLOAT_INSERT:
688 case LFUN_FLOAT_LIST:
689 case LFUN_FLOAT_WIDE_INSERT:
691 case LFUN_FONT_TYPEWRITER:
692 case LFUN_FONT_DEFAULT:
694 case LFUN_FONT_FREE_APPLY:
695 case LFUN_FONT_FREE_UPDATE:
697 case LFUN_FONT_ROMAN:
702 case LFUN_FONT_STATE:
703 case LFUN_FONT_UNDERLINE:
704 case LFUN_FOOTNOTE_INSERT:
705 case LFUN_HFILL_INSERT:
706 case LFUN_HYPERLINK_INSERT:
707 case LFUN_INDEX_INSERT:
708 case LFUN_INDEX_PRINT:
709 case LFUN_INSET_INSERT:
710 case LFUN_LABEL_GOTO:
711 case LFUN_LABEL_INSERT:
712 case LFUN_LINE_INSERT:
713 case LFUN_NEWPAGE_INSERT:
714 case LFUN_PAGEBREAK_INSERT:
716 case LFUN_LAYOUT_PARAGRAPH:
717 case LFUN_LAYOUT_TABULAR:
718 case LFUN_MARGINALNOTE_INSERT:
719 case LFUN_MATH_DISPLAY:
720 case LFUN_MATH_INSERT:
721 case LFUN_MATH_MATRIX:
725 case LFUN_NOMENCL_INSERT:
726 case LFUN_NOMENCL_PRINT:
727 case LFUN_NOTE_INSERT:
729 case LFUN_OPTIONAL_INSERT:
730 case LFUN_PARAGRAPH_PARAMS:
731 case LFUN_PARAGRAPH_PARAMS_APPLY:
732 case LFUN_PARAGRAPH_SPACING:
733 case LFUN_PARAGRAPH_UPDATE:
734 case LFUN_REFERENCE_NEXT:
735 case LFUN_SERVER_GOTO_FILE_ROW:
736 case LFUN_SERVER_NOTIFY:
737 case LFUN_SERVER_SET_XY:
738 case LFUN_SPACE_INSERT:
739 case LFUN_SPECIALCHAR_INSERT:
740 case LFUN_TABULAR_INSERT:
741 case LFUN_TOC_INSERT:
742 case LFUN_WRAP_INSERT:
743 if (layout_->passthru) {
747 return InsetText::getStatus(cur, cmd, flag);
749 case LFUN_INSET_TOGGLE:
750 if (cmd.argument() == "open" || cmd.argument() == "close" ||
751 cmd.argument() == "toggle")
758 flag.enabled(!layout_->forceltr);
759 return InsetText::getStatus(cur, cmd, flag);
761 case LFUN_BREAK_PARAGRAPH:
762 case LFUN_BREAK_PARAGRAPH_SKIP:
763 flag.enabled(layout_->multipar);
767 return InsetText::getStatus(cur, cmd, flag);
772 void InsetCollapsable::setLabel(docstring const & l)
778 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
782 if (status_ == Collapsed)
783 cur.leaveInset(*this);
787 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
789 FloatList const & floats = bp.getTextClass().floats();
790 FloatList::const_iterator it = floats[type];
792 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
796 InsetCollapsable::Decoration InsetCollapsable::decoration() const
798 if (!layout_ || layout_->decoration == "classic")
800 if (layout_->decoration == "minimalistic")
802 if (layout_->decoration == "conglomerate")
804 if (lyxCode() == FLEX_CODE)
805 // FIXME: Is this really necessary?
811 bool InsetCollapsable::isMacroScope(Buffer const &) const
813 // layout_ == 0 leads to no latex output, so ignore
814 // the macros outside
818 // see InsetCollapsable::latex(...) below. In these case
819 // an environment is opened there
820 if (!layout_->latexname.empty())
827 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
828 OutputParams const & runparams) const
830 // FIXME: What should we do layout_ is 0?
832 // 2) through an error
836 // This implements the standard way of handling the LaTeX output of
837 // a collapsable inset, either a command or an environment. Standard
838 // collapsable insets should not redefine this, non-standard ones may
840 if (!layout_->latexname.empty()) {
841 if (layout_->latextype == "command") {
843 if (runparams.moving_arg)
845 os << '\\' << from_utf8(layout_->latexname);
846 if (!layout_->latexparam.empty())
847 os << from_utf8(layout_->latexparam);
849 } else if (layout_->latextype == "environment") {
850 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
851 if (!layout_->latexparam.empty())
852 os << from_utf8(layout_->latexparam);
855 OutputParams rp = runparams;
856 if (layout_->passthru)
858 if (layout_->needprotect)
859 rp.moving_arg = true;
860 int i = InsetText::latex(buf, os, rp);
861 if (!layout_->latexname.empty()) {
862 if (layout_->latextype == "command") {
864 } else if (layout_->latextype == "environment") {
865 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
873 void InsetCollapsable::validate(LaTeXFeatures & features) const
878 // Force inclusion of preamble snippet in layout file
879 features.require(layout_->name);
880 InsetText::validate(features);