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, InsetLayout const * il)
78 : InsetText(bp), layout_(il), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
81 setAutoBreakRows(true);
83 setFrameColor(Color_collapsableframe);
87 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
90 labelstring_(rhs.labelstring_),
91 button_dim(rhs.button_dim),
93 openinlined_(rhs.openinlined_),
94 autoOpen_(rhs.autoOpen_),
95 // the sole purpose of this copy constructor
101 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
103 Dimension dim = dimensionCollapsed();
104 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
109 return _("Left-click to collapse the inset");
111 return _("Left-click to open the inset");
117 void InsetCollapsable::setLayout(BufferParams const & bp)
119 setLayout(bp.getTextClass().insetlayout(name()));
123 void InsetCollapsable::setLayout(InsetLayout const & il)
126 labelstring_ = layout_->labelstring;
132 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
144 text_.write(buf, os);
148 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
150 bool token_found = false;
153 string const token = lex.getString();
154 if (token == "status") {
156 string const tmp_token = lex.getString();
158 if (tmp_token == "collapsed") {
161 } else if (tmp_token == "open") {
165 lyxerr << "InsetCollapsable::read: Missing status!"
167 // Take countermeasures
168 lex.pushToken(token);
171 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
172 // take countermeasures
173 lex.pushToken(token);
176 InsetText::read(buf, lex);
178 setLayout(buf.params());
181 status_ = isOpen() ? Open : Collapsed;
183 // Force default font, if so requested
184 // This avoids paragraphs in buffer language that would have a
185 // foreign language after a document language change, and it ensures
186 // that all new text in ERT and similar gets the "latex" language,
187 // since new text inherits the language from the last position of the
188 // existing text. As a side effect this makes us also robust against
189 // bugs in LyX that might lead to font changes in ERT in .lyx files.
190 resetParagraphsFont();
194 Dimension InsetCollapsable::dimensionCollapsed() const
196 BOOST_ASSERT(layout_);
198 theFontMetrics(layout_->labelfont).buttonText(
199 labelstring_, dim.wid, dim.asc, dim.des);
204 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
206 BOOST_ASSERT(layout_);
208 autoOpen_ = mi.base.bv->cursor().isInside(this);
210 FontInfo tmpfont = mi.base.font;
211 mi.base.font = layout_->font;
212 mi.base.font.realize(tmpfont);
214 switch (geometry()) {
216 InsetText::metrics(mi, dim);
219 InsetText::metrics(mi, dim);
224 InsetText::metrics(mi, dim);
225 // consider width of the inset label
226 FontInfo font(layout_->labelfont);
227 font.realize(sane_font);
233 theFontMetrics(font).rectText(labelstring_, w, a, d);
240 dim = dimensionCollapsed();
241 if (geometry() == TopButton
242 || geometry() == LeftButton) {
244 InsetText::metrics(mi, textdim);
245 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
247 // Correct for button width.
248 dim.wid += textdim.wid;
249 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
250 dim.asc = textdim.asc;
252 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
253 dim.wid = max(dim.wid, textdim.wid);
259 mi.base.font = tmpfont;
263 bool InsetCollapsable::setMouseHover(bool mouse_hover)
265 mouse_hover_ = mouse_hover;
270 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
272 BOOST_ASSERT(layout_);
274 autoOpen_ = pi.base.bv->cursor().isInside(this);
275 ColorCode const old_color = pi.background_color;
276 pi.background_color = backgroundColor();
278 FontInfo tmpfont = pi.base.font;
279 pi.base.font = layout_->font;
280 pi.base.font.realize(tmpfont);
282 // Draw button first -- top, left or only
283 Dimension dimc = dimensionCollapsed();
285 if (geometry() == TopButton ||
286 geometry() == LeftButton ||
287 geometry() == ButtonOnly) {
288 button_dim.x1 = x + 0;
289 button_dim.x2 = x + dimc.width();
290 button_dim.y1 = y - dimc.asc;
291 button_dim.y2 = y + dimc.des;
293 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
302 Dimension const textdim = InsetText::dimension(*pi.base.bv);
303 int const baseline = y;
305 switch (geometry()) {
307 textx = x + dimc.width();
309 InsetText::draw(pi, textx, texty);
313 texty = baseline + dimc.des + textdim.asc;
314 InsetText::draw(pi, textx, texty);
321 InsetText::draw(pi, textx, texty);
327 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
328 InsetText::draw(pi, textx, texty);
329 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
331 int desc = textdim.descent();
332 if (geometry() == Corners)
335 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
336 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
337 pi.pain.line(xx1, y + desc - 4,
339 layout_->labelfont.color());
341 pi.pain.line(xx1, y + desc,
343 layout_->labelfont.color());
345 // Make status_ value visible:
346 pi.pain.line(xx1, y + desc,
348 layout_->labelfont.color());
349 pi.pain.line(xx2 - 4, y + desc,
351 layout_->labelfont.color());
353 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
354 layout_->labelfont.color());
356 // the label below the text. Can be toggled.
357 if (geometry() == SubLabel) {
358 FontInfo font(layout_->labelfont);
359 font.realize(sane_font);
365 theFontMetrics(font).rectText(labelstring_, w, a, d);
366 int const ww = max(textdim.wid, w);
367 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
368 labelstring_, font, Color_none, Color_none);
372 // a visual cue when the cursor is inside the inset
373 Cursor & cur = pi.base.bv->cursor();
374 if (cur.isInside(this)) {
377 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
378 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
379 pi.pain.line(xx2, y + 4, xx2, y,
380 layout_->labelfont.color());
381 pi.pain.line(xx2 - 4, y, xx2, y,
382 layout_->labelfont.color());
386 pi.background_color = old_color;
388 pi.base.font = tmpfont;
392 void InsetCollapsable::cursorPos(BufferView const & bv,
393 CursorSlice const & sl, bool boundary, int & x, int & y) const
395 if (geometry() == ButtonOnly)
397 BOOST_ASSERT(geometry() != ButtonOnly);
399 InsetText::cursorPos(bv, sl, boundary, x, y);
400 Dimension const textdim = InsetText::dimension(bv);
402 switch (geometry()) {
404 x += dimensionCollapsed().wid;
407 y += dimensionCollapsed().des + textdim.asc;
422 Inset::EDITABLE InsetCollapsable::editable() const
424 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
428 bool InsetCollapsable::descendable() const
430 return geometry() != ButtonOnly;
434 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
436 return button_dim.contains(cmd.x, cmd.y);
440 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
443 pos_type const max_length = 15;
444 pos_type const p_siz = paragraphs().begin()->size();
445 pos_type const n = min(max_length, p_siz);
448 for (; i < n && j < p_siz; ++j) {
449 if (paragraphs().begin()->isInset(j))
451 label += paragraphs().begin()->getChar(j);
454 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
457 return label.empty() ? l : label;
461 void InsetCollapsable::edit(Cursor & cur, bool left)
463 //lyxerr << "InsetCollapsable: edit left/right" << endl;
465 InsetText::edit(cur, left);
469 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
471 //lyxerr << "InsetCollapsable: edit xy" << endl;
472 if (geometry() == ButtonOnly
473 || (button_dim.contains(x, y)
474 && geometry() != NoButton))
477 return InsetText::editXY(cur, x, y);
481 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
483 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
484 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
486 switch (cmd.action) {
487 case LFUN_MOUSE_PRESS:
488 if (cmd.button() == mouse_button::button1
490 && geometry() != NoButton) {
491 // reset selection if necessary (see bug 3060)
493 cur.bv().cursor().clearSelection();
499 if (geometry() == NoButton)
500 InsetText::doDispatch(cur, cmd);
501 else if (geometry() != ButtonOnly
503 InsetText::doDispatch(cur, cmd);
508 case LFUN_MOUSE_MOTION:
509 case LFUN_MOUSE_DOUBLE:
510 case LFUN_MOUSE_TRIPLE:
511 if (geometry() == NoButton)
512 InsetText::doDispatch(cur, cmd);
513 else if (geometry() != ButtonOnly
515 InsetText::doDispatch(cur, cmd);
520 case LFUN_MOUSE_RELEASE:
521 if (cmd.button() == mouse_button::button3) {
522 // There is no button to right click:
523 if (geometry() == Corners ||
524 geometry() == SubLabel ||
525 geometry() == NoButton) {
527 setStatus(cur, Collapsed);
529 setStatus(cur, Open);
533 // configuration dialog
534 showInsetDialog(&cur.bv());
539 if (geometry() == NoButton) {
540 // The mouse click has to be within the inset!
541 InsetText::doDispatch(cur, cmd);
545 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
546 // if we are selecting, we do not want to
550 // Left button is clicked, the user asks to
551 // toggle the inset visual state.
553 cur.updateFlags(Update::Force | Update::FitCursor);
554 if (geometry() == ButtonOnly) {
555 setStatus(cur, Open);
559 setStatus(cur, Collapsed);
561 cur.bv().cursor() = cur;
565 // The mouse click is within the opened inset.
566 if (geometry() == TopButton
567 || geometry() == LeftButton)
568 InsetText::doDispatch(cur, cmd);
571 case LFUN_INSET_TOGGLE:
572 if (cmd.argument() == "open")
573 setStatus(cur, Open);
574 else if (cmd.argument() == "close")
575 setStatus(cur, Collapsed);
576 else if (cmd.argument() == "toggle" || cmd.argument().empty())
577 if (status_ == Open) {
578 setStatus(cur, Collapsed);
579 if (geometry() == ButtonOnly)
580 cur.top().forwardPos();
582 setStatus(cur, Open);
583 else // if assign or anything else
589 case LFUN_CLIPBOARD_PASTE:
590 case LFUN_PRIMARY_SELECTION_PASTE: {
591 InsetText::doDispatch(cur, cmd);
592 // Since we can only store plain text, we must reset all
594 // FIXME: Change only the pasted paragraphs
596 resetParagraphsFont();
601 if (layout_ && layout_->forceltr) {
602 // Force any new text to latex_language
603 // FIXME: This should only be necessary in constructor, but
604 // new paragraphs that are created by pressing enter at the
605 // start of an existing paragraph get the buffer language
606 // and not latex_language, so we take this brute force
608 cur.current_font.setLanguage(latex_language);
609 cur.real_current_font.setLanguage(latex_language);
611 InsetText::doDispatch(cur, cmd);
617 bool InsetCollapsable::allowMultiPar() const
619 return layout_->multipar;
623 void InsetCollapsable::resetParagraphsFont()
626 font.fontInfo() = inherit_font;
627 if (layout_->forceltr)
628 font.setLanguage(latex_language);
629 if (layout_->passthru) {
630 ParagraphList::iterator par = paragraphs().begin();
631 ParagraphList::iterator const end = paragraphs().end();
633 par->resetFonts(font);
634 par->params().clear();
641 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
642 FuncStatus & flag) const
644 switch (cmd.action) {
646 case LFUN_ACCENT_ACUTE:
647 case LFUN_ACCENT_BREVE:
648 case LFUN_ACCENT_CARON:
649 case LFUN_ACCENT_CEDILLA:
650 case LFUN_ACCENT_CIRCLE:
651 case LFUN_ACCENT_CIRCUMFLEX:
652 case LFUN_ACCENT_DOT:
653 case LFUN_ACCENT_GRAVE:
654 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
655 case LFUN_ACCENT_MACRON:
656 case LFUN_ACCENT_OGONEK:
657 case LFUN_ACCENT_SPECIAL_CARON:
658 case LFUN_ACCENT_TIE:
659 case LFUN_ACCENT_TILDE:
660 case LFUN_ACCENT_UMLAUT:
661 case LFUN_ACCENT_UNDERBAR:
662 case LFUN_ACCENT_UNDERDOT:
664 case LFUN_BIBITEM_INSERT:
665 case LFUN_BOX_INSERT:
666 case LFUN_BRANCH_INSERT:
668 case LFUN_CAPTION_INSERT:
669 case LFUN_CLEARPAGE_INSERT:
670 case LFUN_CLEARDOUBLEPAGE_INSERT:
671 case LFUN_DEPTH_DECREMENT:
672 case LFUN_DEPTH_INCREMENT:
673 case LFUN_ENVIRONMENT_INSERT:
674 case LFUN_ERT_INSERT:
675 case LFUN_FILE_INSERT:
676 case LFUN_FLEX_INSERT:
677 case LFUN_FLOAT_INSERT:
678 case LFUN_FLOAT_LIST:
679 case LFUN_FLOAT_WIDE_INSERT:
681 case LFUN_FONT_TYPEWRITER:
682 case LFUN_FONT_DEFAULT:
684 case LFUN_FONT_FREE_APPLY:
685 case LFUN_FONT_FREE_UPDATE:
687 case LFUN_FONT_ROMAN:
692 case LFUN_FONT_STATE:
693 case LFUN_FONT_UNDERLINE:
694 case LFUN_FOOTNOTE_INSERT:
695 case LFUN_HFILL_INSERT:
696 case LFUN_HYPERLINK_INSERT:
697 case LFUN_INDEX_INSERT:
698 case LFUN_INDEX_PRINT:
699 case LFUN_INSET_INSERT:
700 case LFUN_LABEL_GOTO:
701 case LFUN_LABEL_INSERT:
702 case LFUN_LINE_INSERT:
703 case LFUN_NEWPAGE_INSERT:
704 case LFUN_PAGEBREAK_INSERT:
706 case LFUN_LAYOUT_PARAGRAPH:
707 case LFUN_LAYOUT_TABULAR:
708 case LFUN_MARGINALNOTE_INSERT:
709 case LFUN_MATH_DISPLAY:
710 case LFUN_MATH_INSERT:
711 case LFUN_MATH_MATRIX:
715 case LFUN_NOMENCL_INSERT:
716 case LFUN_NOMENCL_PRINT:
717 case LFUN_NOTE_INSERT:
719 case LFUN_OPTIONAL_INSERT:
720 case LFUN_PARAGRAPH_PARAMS:
721 case LFUN_PARAGRAPH_PARAMS_APPLY:
722 case LFUN_PARAGRAPH_SPACING:
723 case LFUN_PARAGRAPH_UPDATE:
724 case LFUN_REFERENCE_NEXT:
725 case LFUN_SERVER_GOTO_FILE_ROW:
726 case LFUN_SERVER_NOTIFY:
727 case LFUN_SERVER_SET_XY:
728 case LFUN_SPACE_INSERT:
729 case LFUN_SPECIALCHAR_INSERT:
730 case LFUN_TABULAR_INSERT:
731 case LFUN_TOC_INSERT:
732 case LFUN_WRAP_INSERT:
733 if (layout_->passthru) {
737 return InsetText::getStatus(cur, cmd, flag);
739 case LFUN_INSET_TOGGLE:
740 if (cmd.argument() == "open" || cmd.argument() == "close" ||
741 cmd.argument() == "toggle")
748 flag.enabled(!layout_->forceltr);
749 return InsetText::getStatus(cur, cmd, flag);
751 case LFUN_BREAK_PARAGRAPH:
752 case LFUN_BREAK_PARAGRAPH_SKIP:
753 flag.enabled(layout_->multipar);
757 return InsetText::getStatus(cur, cmd, flag);
762 void InsetCollapsable::setLabel(docstring const & l)
768 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
772 if (status_ == Collapsed)
773 cur.leaveInset(*this);
777 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
779 FloatList const & floats = bp.getTextClass().floats();
780 FloatList::const_iterator it = floats[type];
782 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
786 InsetCollapsable::Decoration InsetCollapsable::decoration() const
788 if (!layout_ || layout_->decoration == "classic")
790 if (layout_->decoration == "minimalistic")
792 if (layout_->decoration == "conglomerate")
794 if (lyxCode() == FLEX_CODE)
795 // FIXME: Is this really necessary?
801 bool InsetCollapsable::isMacroScope(Buffer const & buf) const
803 // layout_ == 0 leads to no latex output, so ignore
804 // the macros outside
808 // see InsetCollapsable::latex(...) below. In these case
809 // an environment is opened there
810 if (!layout_->latexname.empty())
817 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
818 OutputParams const & runparams) const
820 // FIXME: What should we do layout_ is 0?
822 // 2) through an error
826 // This implements the standard way of handling the LaTeX output of
827 // a collapsable inset, either a command or an environment. Standard
828 // collapsable insets should not redefine this, non-standard ones may
830 if (!layout_->latexname.empty()) {
831 if (layout_->latextype == "command") {
833 if (runparams.moving_arg)
835 os << '\\' << from_utf8(layout_->latexname);
836 if (!layout_->latexparam.empty())
837 os << from_utf8(layout_->latexparam);
839 } else if (layout_->latextype == "environment") {
840 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
841 if (!layout_->latexparam.empty())
842 os << from_utf8(layout_->latexparam);
845 OutputParams rp = runparams;
846 if (layout_->passthru)
848 if (layout_->needprotect)
849 rp.moving_arg = true;
850 int i = InsetText::latex(buf, os, rp);
851 if (!layout_->latexname.empty()) {
852 if (layout_->latextype == "command") {
854 } else if (layout_->latextype == "environment") {
855 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
863 void InsetCollapsable::validate(LaTeXFeatures & features) const
868 // Force inclusion of preamble snippet in layout file
869 features.require(layout_->name);
870 InsetText::validate(features);