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 left)
471 //lyxerr << "InsetCollapsable: edit left/right" << endl;
473 InsetText::edit(cur, left);
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 (geometry() == Corners ||
532 geometry() == SubLabel ||
533 geometry() == NoButton) {
535 setStatus(cur, Collapsed);
537 setStatus(cur, Open);
541 // configuration dialog
542 showInsetDialog(&cur.bv());
547 if (geometry() == NoButton) {
548 // The mouse click has to be within the inset!
549 InsetText::doDispatch(cur, cmd);
553 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
554 // if we are selecting, we do not want to
558 // Left button is clicked, the user asks to
559 // toggle the inset visual state.
561 cur.updateFlags(Update::Force | Update::FitCursor);
562 if (geometry() == ButtonOnly) {
563 setStatus(cur, Open);
567 setStatus(cur, Collapsed);
569 cur.bv().cursor() = cur;
573 // The mouse click is within the opened inset.
574 if (geometry() == TopButton
575 || geometry() == LeftButton)
576 InsetText::doDispatch(cur, cmd);
579 case LFUN_INSET_TOGGLE:
580 if (cmd.argument() == "open")
581 setStatus(cur, Open);
582 else if (cmd.argument() == "close")
583 setStatus(cur, Collapsed);
584 else if (cmd.argument() == "toggle" || cmd.argument().empty())
585 if (status_ == Open) {
586 setStatus(cur, Collapsed);
587 if (geometry() == ButtonOnly)
588 cur.top().forwardPos();
590 setStatus(cur, Open);
591 else // if assign or anything else
597 case LFUN_CLIPBOARD_PASTE:
598 case LFUN_PRIMARY_SELECTION_PASTE: {
599 InsetText::doDispatch(cur, cmd);
600 // Since we can only store plain text, we must reset all
602 // FIXME: Change only the pasted paragraphs
604 resetParagraphsFont();
609 if (layout_ && layout_->forceltr) {
610 // Force any new text to latex_language
611 // FIXME: This should only be necessary in constructor, but
612 // new paragraphs that are created by pressing enter at the
613 // start of an existing paragraph get the buffer language
614 // and not latex_language, so we take this brute force
616 cur.current_font.setLanguage(latex_language);
617 cur.real_current_font.setLanguage(latex_language);
619 InsetText::doDispatch(cur, cmd);
625 bool InsetCollapsable::allowMultiPar() const
627 return layout_->multipar;
631 void InsetCollapsable::resetParagraphsFont()
634 font.fontInfo() = inherit_font;
635 if (layout_->forceltr)
636 font.setLanguage(latex_language);
637 if (layout_->passthru) {
638 ParagraphList::iterator par = paragraphs().begin();
639 ParagraphList::iterator const end = paragraphs().end();
641 par->resetFonts(font);
642 par->params().clear();
649 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
650 FuncStatus & flag) const
652 switch (cmd.action) {
654 case LFUN_ACCENT_ACUTE:
655 case LFUN_ACCENT_BREVE:
656 case LFUN_ACCENT_CARON:
657 case LFUN_ACCENT_CEDILLA:
658 case LFUN_ACCENT_CIRCLE:
659 case LFUN_ACCENT_CIRCUMFLEX:
660 case LFUN_ACCENT_DOT:
661 case LFUN_ACCENT_GRAVE:
662 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
663 case LFUN_ACCENT_MACRON:
664 case LFUN_ACCENT_OGONEK:
665 case LFUN_ACCENT_SPECIAL_CARON:
666 case LFUN_ACCENT_TIE:
667 case LFUN_ACCENT_TILDE:
668 case LFUN_ACCENT_UMLAUT:
669 case LFUN_ACCENT_UNDERBAR:
670 case LFUN_ACCENT_UNDERDOT:
672 case LFUN_BIBITEM_INSERT:
673 case LFUN_BOX_INSERT:
674 case LFUN_BRANCH_INSERT:
676 case LFUN_CAPTION_INSERT:
677 case LFUN_CLEARPAGE_INSERT:
678 case LFUN_CLEARDOUBLEPAGE_INSERT:
679 case LFUN_DEPTH_DECREMENT:
680 case LFUN_DEPTH_INCREMENT:
681 case LFUN_ENVIRONMENT_INSERT:
682 case LFUN_ERT_INSERT:
683 case LFUN_FILE_INSERT:
684 case LFUN_FLEX_INSERT:
685 case LFUN_FLOAT_INSERT:
686 case LFUN_FLOAT_LIST:
687 case LFUN_FLOAT_WIDE_INSERT:
689 case LFUN_FONT_TYPEWRITER:
690 case LFUN_FONT_DEFAULT:
692 case LFUN_FONT_FREE_APPLY:
693 case LFUN_FONT_FREE_UPDATE:
695 case LFUN_FONT_ROMAN:
700 case LFUN_FONT_STATE:
701 case LFUN_FONT_UNDERLINE:
702 case LFUN_FOOTNOTE_INSERT:
703 case LFUN_HFILL_INSERT:
704 case LFUN_HYPERLINK_INSERT:
705 case LFUN_INDEX_INSERT:
706 case LFUN_INDEX_PRINT:
707 case LFUN_INSET_INSERT:
708 case LFUN_LABEL_GOTO:
709 case LFUN_LABEL_INSERT:
710 case LFUN_LINE_INSERT:
711 case LFUN_NEWPAGE_INSERT:
712 case LFUN_PAGEBREAK_INSERT:
714 case LFUN_LAYOUT_PARAGRAPH:
715 case LFUN_LAYOUT_TABULAR:
716 case LFUN_MARGINALNOTE_INSERT:
717 case LFUN_MATH_DISPLAY:
718 case LFUN_MATH_INSERT:
719 case LFUN_MATH_MATRIX:
723 case LFUN_NOMENCL_INSERT:
724 case LFUN_NOMENCL_PRINT:
725 case LFUN_NOTE_INSERT:
727 case LFUN_OPTIONAL_INSERT:
728 case LFUN_PARAGRAPH_PARAMS:
729 case LFUN_PARAGRAPH_PARAMS_APPLY:
730 case LFUN_PARAGRAPH_SPACING:
731 case LFUN_PARAGRAPH_UPDATE:
732 case LFUN_REFERENCE_NEXT:
733 case LFUN_SERVER_GOTO_FILE_ROW:
734 case LFUN_SERVER_NOTIFY:
735 case LFUN_SERVER_SET_XY:
736 case LFUN_SPACE_INSERT:
737 case LFUN_SPECIALCHAR_INSERT:
738 case LFUN_TABULAR_INSERT:
739 case LFUN_TOC_INSERT:
740 case LFUN_WRAP_INSERT:
741 if (layout_->passthru) {
745 return InsetText::getStatus(cur, cmd, flag);
747 case LFUN_INSET_TOGGLE:
748 if (cmd.argument() == "open" || cmd.argument() == "close" ||
749 cmd.argument() == "toggle")
756 flag.enabled(!layout_->forceltr);
757 return InsetText::getStatus(cur, cmd, flag);
759 case LFUN_BREAK_PARAGRAPH:
760 case LFUN_BREAK_PARAGRAPH_SKIP:
761 flag.enabled(layout_->multipar);
765 return InsetText::getStatus(cur, cmd, flag);
770 void InsetCollapsable::setLabel(docstring const & l)
776 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
780 if (status_ == Collapsed)
781 cur.leaveInset(*this);
785 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
787 FloatList const & floats = bp.getTextClass().floats();
788 FloatList::const_iterator it = floats[type];
790 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
794 InsetCollapsable::Decoration InsetCollapsable::decoration() const
796 if (!layout_ || layout_->decoration == "classic")
798 if (layout_->decoration == "minimalistic")
800 if (layout_->decoration == "conglomerate")
802 if (lyxCode() == FLEX_CODE)
803 // FIXME: Is this really necessary?
809 bool InsetCollapsable::isMacroScope(Buffer const &) const
811 // layout_ == 0 leads to no latex output, so ignore
812 // the macros outside
816 // see InsetCollapsable::latex(...) below. In these case
817 // an environment is opened there
818 if (!layout_->latexname.empty())
825 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
826 OutputParams const & runparams) const
828 // FIXME: What should we do layout_ is 0?
830 // 2) through an error
834 // This implements the standard way of handling the LaTeX output of
835 // a collapsable inset, either a command or an environment. Standard
836 // collapsable insets should not redefine this, non-standard ones may
838 if (!layout_->latexname.empty()) {
839 if (layout_->latextype == "command") {
841 if (runparams.moving_arg)
843 os << '\\' << from_utf8(layout_->latexname);
844 if (!layout_->latexparam.empty())
845 os << from_utf8(layout_->latexparam);
847 } else if (layout_->latextype == "environment") {
848 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
849 if (!layout_->latexparam.empty())
850 os << from_utf8(layout_->latexparam);
853 OutputParams rp = runparams;
854 if (layout_->passthru)
856 if (layout_->needprotect)
857 rp.moving_arg = true;
858 int i = InsetText::latex(buf, os, rp);
859 if (!layout_->latexname.empty()) {
860 if (layout_->latextype == "command") {
862 } else if (layout_->latextype == "environment") {
863 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
871 void InsetCollapsable::validate(LaTeXFeatures & features) const
876 // Force inclusion of preamble snippet in layout file
877 features.require(layout_->name);
878 InsetText::validate(features);