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 (geometry() == NoButton)
107 return layout_->labelstring;
108 else if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
113 return _("Left-click to collapse the inset");
115 return _("Left-click to open the inset");
121 void InsetCollapsable::setLayout(BufferParams const & bp)
123 setLayout(bp.getTextClassPtr());
127 void InsetCollapsable::setLayout(TextClassPtr tc)
130 if ( tc.get() != 0 ) {
131 layout_ = &tc->insetlayout(name());
132 labelstring_ = layout_->labelstring;
135 labelstring_ = _("UNDEFINED");
142 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
154 text_.write(buf, os);
158 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
160 bool token_found = false;
163 string const token = lex.getString();
164 if (token == "status") {
166 string const tmp_token = lex.getString();
168 if (tmp_token == "collapsed") {
171 } else if (tmp_token == "open") {
175 lyxerr << "InsetCollapsable::read: Missing status!"
177 // Take countermeasures
178 lex.pushToken(token);
181 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
182 // take countermeasures
183 lex.pushToken(token);
186 InsetText::read(buf, lex);
188 setLayout(buf.params());
191 status_ = isOpen() ? Open : Collapsed;
193 // Force default font, if so requested
194 // This avoids paragraphs in buffer language that would have a
195 // foreign language after a document language change, and it ensures
196 // that all new text in ERT and similar gets the "latex" language,
197 // since new text inherits the language from the last position of the
198 // existing text. As a side effect this makes us also robust against
199 // bugs in LyX that might lead to font changes in ERT in .lyx files.
200 resetParagraphsFont();
204 Dimension InsetCollapsable::dimensionCollapsed() const
206 BOOST_ASSERT(layout_);
208 theFontMetrics(layout_->labelfont).buttonText(
209 labelstring_, dim.wid, dim.asc, dim.des);
214 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
216 BOOST_ASSERT(layout_);
218 autoOpen_ = mi.base.bv->cursor().isInside(this);
220 FontInfo tmpfont = mi.base.font;
221 mi.base.font = layout_->font;
222 mi.base.font.realize(tmpfont);
224 switch (geometry()) {
226 InsetText::metrics(mi, dim);
229 InsetText::metrics(mi, dim);
234 InsetText::metrics(mi, dim);
235 // consider width of the inset label
236 FontInfo font(layout_->labelfont);
237 font.realize(sane_font);
243 theFontMetrics(font).rectText(labelstring_, w, a, d);
250 dim = dimensionCollapsed();
251 if (geometry() == TopButton
252 || geometry() == LeftButton) {
254 InsetText::metrics(mi, textdim);
255 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
257 // Correct for button width.
258 dim.wid += textdim.wid;
259 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
260 dim.asc = textdim.asc;
262 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
263 dim.wid = max(dim.wid, textdim.wid);
269 mi.base.font = tmpfont;
273 bool InsetCollapsable::setMouseHover(bool mouse_hover)
275 mouse_hover_ = mouse_hover;
280 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
282 BOOST_ASSERT(layout_);
284 autoOpen_ = pi.base.bv->cursor().isInside(this);
285 ColorCode const old_color = pi.background_color;
286 pi.background_color = backgroundColor();
288 FontInfo tmpfont = pi.base.font;
289 pi.base.font = layout_->font;
290 pi.base.font.realize(tmpfont);
292 // Draw button first -- top, left or only
293 Dimension dimc = dimensionCollapsed();
295 if (geometry() == TopButton ||
296 geometry() == LeftButton ||
297 geometry() == ButtonOnly) {
298 button_dim.x1 = x + 0;
299 button_dim.x2 = x + dimc.width();
300 button_dim.y1 = y - dimc.asc;
301 button_dim.y2 = y + dimc.des;
303 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
312 Dimension const textdim = InsetText::dimension(*pi.base.bv);
313 int const baseline = y;
315 switch (geometry()) {
317 textx = x + dimc.width();
319 InsetText::draw(pi, textx, texty);
323 texty = baseline + dimc.des + textdim.asc;
324 InsetText::draw(pi, textx, texty);
331 InsetText::draw(pi, textx, texty);
337 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
338 InsetText::draw(pi, textx, texty);
339 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
341 int desc = textdim.descent();
342 if (geometry() == Corners)
345 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
346 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
347 pi.pain.line(xx1, y + desc - 4,
349 layout_->labelfont.color());
351 pi.pain.line(xx1, y + desc,
353 layout_->labelfont.color());
355 // Make status_ value visible:
356 pi.pain.line(xx1, y + desc,
358 layout_->labelfont.color());
359 pi.pain.line(xx2 - 4, y + desc,
361 layout_->labelfont.color());
363 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
364 layout_->labelfont.color());
366 // the label below the text. Can be toggled.
367 if (geometry() == SubLabel) {
368 FontInfo font(layout_->labelfont);
369 font.realize(sane_font);
375 theFontMetrics(font).rectText(labelstring_, w, a, d);
376 int const ww = max(textdim.wid, w);
377 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
378 labelstring_, font, Color_none, Color_none);
382 // a visual cue when the cursor is inside the inset
383 Cursor & cur = pi.base.bv->cursor();
384 if (cur.isInside(this)) {
387 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
388 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
389 pi.pain.line(xx2, y + 4, xx2, y,
390 layout_->labelfont.color());
391 pi.pain.line(xx2 - 4, y, xx2, y,
392 layout_->labelfont.color());
396 pi.background_color = old_color;
398 pi.base.font = tmpfont;
402 void InsetCollapsable::cursorPos(BufferView const & bv,
403 CursorSlice const & sl, bool boundary, int & x, int & y) const
405 if (geometry() == ButtonOnly)
407 BOOST_ASSERT(geometry() != ButtonOnly);
409 InsetText::cursorPos(bv, sl, boundary, x, y);
410 Dimension const textdim = InsetText::dimension(bv);
412 switch (geometry()) {
414 x += dimensionCollapsed().wid;
417 y += dimensionCollapsed().des + textdim.asc;
432 Inset::EDITABLE InsetCollapsable::editable() const
434 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
438 bool InsetCollapsable::descendable() const
440 return geometry() != ButtonOnly;
444 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
446 return button_dim.contains(cmd.x, cmd.y);
450 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
453 pos_type const max_length = 15;
454 pos_type const p_siz = paragraphs().begin()->size();
455 pos_type const n = min(max_length, p_siz);
458 for (; i < n && j < p_siz; ++j) {
459 if (paragraphs().begin()->isInset(j))
461 label += paragraphs().begin()->getChar(j);
464 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
467 return label.empty() ? l : label;
471 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
473 //lyxerr << "InsetCollapsable: edit left/right" << endl;
475 InsetText::edit(cur, front, entry_from);
479 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
481 //lyxerr << "InsetCollapsable: edit xy" << endl;
482 if (geometry() == ButtonOnly
483 || (button_dim.contains(x, y)
484 && geometry() != NoButton))
487 return InsetText::editXY(cur, x, y);
491 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
493 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
494 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
496 switch (cmd.action) {
497 case LFUN_MOUSE_PRESS:
498 if (cmd.button() == mouse_button::button1
500 && geometry() != NoButton) {
501 // reset selection if necessary (see bug 3060)
503 cur.bv().cursor().clearSelection();
509 if (geometry() == NoButton)
510 InsetText::doDispatch(cur, cmd);
511 else if (geometry() != ButtonOnly
513 InsetText::doDispatch(cur, cmd);
518 case LFUN_MOUSE_MOTION:
519 case LFUN_MOUSE_DOUBLE:
520 case LFUN_MOUSE_TRIPLE:
521 if (geometry() == NoButton)
522 InsetText::doDispatch(cur, cmd);
523 else if (geometry() != ButtonOnly
525 InsetText::doDispatch(cur, cmd);
530 case LFUN_MOUSE_RELEASE:
531 if (cmd.button() == mouse_button::button3) {
532 // There is no button to right click:
533 if (decoration() == Minimalistic ||
534 geometry() == Corners ||
535 geometry() == SubLabel ||
536 geometry() == NoButton
539 setStatus(cur, Collapsed);
541 setStatus(cur, Open);
545 // configuration dialog
546 showInsetDialog(&cur.bv());
551 if (geometry() == NoButton) {
552 // The mouse click has to be within the inset!
553 InsetText::doDispatch(cur, cmd);
557 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
558 // if we are selecting, we do not want to
562 // Left button is clicked, the user asks to
563 // toggle the inset visual state.
565 cur.updateFlags(Update::Force | Update::FitCursor);
566 if (geometry() == ButtonOnly) {
567 setStatus(cur, Open);
571 setStatus(cur, Collapsed);
573 cur.bv().cursor() = cur;
577 // The mouse click is within the opened inset.
578 if (geometry() == TopButton
579 || geometry() == LeftButton)
580 InsetText::doDispatch(cur, cmd);
583 case LFUN_INSET_TOGGLE:
584 if (cmd.argument() == "open")
585 setStatus(cur, Open);
586 else if (cmd.argument() == "close")
587 setStatus(cur, Collapsed);
588 else if (cmd.argument() == "toggle" || cmd.argument().empty())
589 if (status_ == Open) {
590 setStatus(cur, Collapsed);
591 if (geometry() == ButtonOnly)
592 cur.top().forwardPos();
594 setStatus(cur, Open);
595 else // if assign or anything else
601 case LFUN_CLIPBOARD_PASTE:
602 case LFUN_PRIMARY_SELECTION_PASTE: {
603 InsetText::doDispatch(cur, cmd);
604 // Since we can only store plain text, we must reset all
606 // FIXME: Change only the pasted paragraphs
608 resetParagraphsFont();
613 if (layout_ && layout_->forceltr) {
614 // Force any new text to latex_language
615 // FIXME: This should only be necessary in constructor, but
616 // new paragraphs that are created by pressing enter at the
617 // start of an existing paragraph get the buffer language
618 // and not latex_language, so we take this brute force
620 cur.current_font.setLanguage(latex_language);
621 cur.real_current_font.setLanguage(latex_language);
623 InsetText::doDispatch(cur, cmd);
629 bool InsetCollapsable::allowMultiPar() const
631 return layout_->multipar;
635 void InsetCollapsable::resetParagraphsFont()
638 font.fontInfo() = inherit_font;
639 if (layout_->forceltr)
640 font.setLanguage(latex_language);
641 if (layout_->passthru) {
642 ParagraphList::iterator par = paragraphs().begin();
643 ParagraphList::iterator const end = paragraphs().end();
645 par->resetFonts(font);
646 par->params().clear();
653 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
654 FuncStatus & flag) const
656 switch (cmd.action) {
658 case LFUN_ACCENT_ACUTE:
659 case LFUN_ACCENT_BREVE:
660 case LFUN_ACCENT_CARON:
661 case LFUN_ACCENT_CEDILLA:
662 case LFUN_ACCENT_CIRCLE:
663 case LFUN_ACCENT_CIRCUMFLEX:
664 case LFUN_ACCENT_DOT:
665 case LFUN_ACCENT_GRAVE:
666 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
667 case LFUN_ACCENT_MACRON:
668 case LFUN_ACCENT_OGONEK:
669 case LFUN_ACCENT_SPECIAL_CARON:
670 case LFUN_ACCENT_TIE:
671 case LFUN_ACCENT_TILDE:
672 case LFUN_ACCENT_UMLAUT:
673 case LFUN_ACCENT_UNDERBAR:
674 case LFUN_ACCENT_UNDERDOT:
676 case LFUN_BIBITEM_INSERT:
677 case LFUN_BOX_INSERT:
678 case LFUN_BRANCH_INSERT:
680 case LFUN_CAPTION_INSERT:
681 case LFUN_CLEARPAGE_INSERT:
682 case LFUN_CLEARDOUBLEPAGE_INSERT:
683 case LFUN_DEPTH_DECREMENT:
684 case LFUN_DEPTH_INCREMENT:
685 case LFUN_ENVIRONMENT_INSERT:
686 case LFUN_ERT_INSERT:
687 case LFUN_FILE_INSERT:
688 case LFUN_FLEX_INSERT:
689 case LFUN_FLOAT_INSERT:
690 case LFUN_FLOAT_LIST:
691 case LFUN_FLOAT_WIDE_INSERT:
693 case LFUN_FONT_TYPEWRITER:
694 case LFUN_FONT_DEFAULT:
696 case LFUN_FONT_FREE_APPLY:
697 case LFUN_FONT_FREE_UPDATE:
699 case LFUN_FONT_ROMAN:
704 case LFUN_FONT_STATE:
705 case LFUN_FONT_UNDERLINE:
706 case LFUN_FOOTNOTE_INSERT:
707 case LFUN_HFILL_INSERT:
708 case LFUN_HYPERLINK_INSERT:
709 case LFUN_INDEX_INSERT:
710 case LFUN_INDEX_PRINT:
711 case LFUN_INSET_INSERT:
712 case LFUN_LABEL_GOTO:
713 case LFUN_LABEL_INSERT:
714 case LFUN_LINE_INSERT:
715 case LFUN_NEWPAGE_INSERT:
716 case LFUN_PAGEBREAK_INSERT:
718 case LFUN_LAYOUT_PARAGRAPH:
719 case LFUN_LAYOUT_TABULAR:
720 case LFUN_MARGINALNOTE_INSERT:
721 case LFUN_MATH_DISPLAY:
722 case LFUN_MATH_INSERT:
723 case LFUN_MATH_MATRIX:
727 case LFUN_NOMENCL_INSERT:
728 case LFUN_NOMENCL_PRINT:
729 case LFUN_NOTE_INSERT:
731 case LFUN_OPTIONAL_INSERT:
732 case LFUN_PARAGRAPH_PARAMS:
733 case LFUN_PARAGRAPH_PARAMS_APPLY:
734 case LFUN_PARAGRAPH_SPACING:
735 case LFUN_PARAGRAPH_UPDATE:
736 case LFUN_REFERENCE_NEXT:
737 case LFUN_SERVER_GOTO_FILE_ROW:
738 case LFUN_SERVER_NOTIFY:
739 case LFUN_SERVER_SET_XY:
740 case LFUN_SPACE_INSERT:
741 case LFUN_SPECIALCHAR_INSERT:
742 case LFUN_TABULAR_INSERT:
743 case LFUN_TOC_INSERT:
744 case LFUN_WRAP_INSERT:
745 if (layout_->passthru) {
749 return InsetText::getStatus(cur, cmd, flag);
751 case LFUN_INSET_TOGGLE:
752 if (cmd.argument() == "open" || cmd.argument() == "close" ||
753 cmd.argument() == "toggle")
760 flag.enabled(!layout_->forceltr);
761 return InsetText::getStatus(cur, cmd, flag);
763 case LFUN_BREAK_PARAGRAPH:
764 case LFUN_BREAK_PARAGRAPH_SKIP:
765 flag.enabled(layout_->multipar);
769 return InsetText::getStatus(cur, cmd, flag);
774 void InsetCollapsable::setLabel(docstring const & l)
780 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
784 if (status_ == Collapsed)
785 cur.leaveInset(*this);
789 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
791 FloatList const & floats = bp.getTextClass().floats();
792 FloatList::const_iterator it = floats[type];
794 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
798 InsetCollapsable::Decoration InsetCollapsable::decoration() const
800 if (!layout_ || layout_->decoration == "classic")
802 if (layout_->decoration == "minimalistic")
804 if (layout_->decoration == "conglomerate")
806 if (lyxCode() == FLEX_CODE)
807 // FIXME: Is this really necessary?
813 bool InsetCollapsable::isMacroScope(Buffer const &) const
815 // layout_ == 0 leads to no latex output, so ignore
816 // the macros outside
820 // see InsetCollapsable::latex(...) below. In these case
821 // an environment is opened there
822 if (!layout_->latexname.empty())
829 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
830 OutputParams const & runparams) const
832 // FIXME: What should we do layout_ is 0?
834 // 2) through an error
838 // This implements the standard way of handling the LaTeX output of
839 // a collapsable inset, either a command or an environment. Standard
840 // collapsable insets should not redefine this, non-standard ones may
842 if (!layout_->latexname.empty()) {
843 if (layout_->latextype == "command") {
845 if (runparams.moving_arg)
847 os << '\\' << from_utf8(layout_->latexname);
848 if (!layout_->latexparam.empty())
849 os << from_utf8(layout_->latexparam);
851 } else if (layout_->latextype == "environment") {
852 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
853 if (!layout_->latexparam.empty())
854 os << from_utf8(layout_->latexparam);
857 OutputParams rp = runparams;
858 if (layout_->passthru)
860 if (layout_->needprotect)
861 rp.moving_arg = true;
862 int i = InsetText::latex(buf, os, rp);
863 if (!layout_->latexname.empty()) {
864 if (layout_->latextype == "command") {
866 } else if (layout_->latextype == "environment") {
867 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
875 void InsetCollapsable::validate(LaTeXFeatures & features) const
880 // Force inclusion of preamble snippet in layout file
881 features.require(layout_->name);
882 InsetText::validate(features);