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"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.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
77 (BufferParams const & bp, CollapseStatus status)
78 : InsetText(bp), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
81 setAutoBreakRows(true);
83 setFrameColor(Color_collapsableframe);
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91 button_dim(rhs.button_dim),
93 topbaseline(rhs.topbaseline),
96 openinlined_(rhs.openinlined_),
97 autoOpen_(rhs.autoOpen_),
98 // the sole purpose of this copy constructor
104 void InsetCollapsable::setLayout(BufferParams const & bp)
106 // FIXME: put this in the InsetLayout parsing?
107 // Fallback for lacking inset layout background
108 layout_.bgcolor = Color_background;
110 layout_ = getLayout(bp);
111 if (layout_.labelfont != inherit_font)
114 // FIXME: put this in the InsetLayout parsing?
115 // Fallback for lacking inset layout labelfont.
116 layout_.labelfont = sane_font;
117 layout_.labelfont.decSize();
118 layout_.labelfont.decSize();
119 layout_.labelfont.setColor(Color_collapsable);
123 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
135 text_.write(buf, os);
139 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
141 bool token_found = false;
144 string const token = lex.getString();
145 if (token == "status") {
147 string const tmp_token = lex.getString();
149 if (tmp_token == "collapsed") {
152 } else if (tmp_token == "open") {
156 lyxerr << "InsetCollapsable::read: Missing status!"
158 // Take countermeasures
159 lex.pushToken(token);
162 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
164 // take countermeasures
165 lex.pushToken(token);
168 InsetText::read(buf, lex);
171 status_ = isOpen() ? Open : Collapsed;
174 setLayout(buf.params());
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
190 theFontMetrics(layout_.labelfont).buttonText(
191 layout_.labelstring, dim.wid, dim.asc, dim.des);
196 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
198 autoOpen_ = mi.base.bv->cursor().isInside(this);
200 FontInfo tmpfont = mi.base.font;
201 getDrawFont(mi.base.font);
202 mi.base.font.realize(tmpfont);
204 switch (geometry()) {
206 InsetText::metrics(mi, dim);
209 InsetText::metrics(mi, dim);
214 InsetText::metrics(mi, dim);
215 // consider width of the inset label
216 FontInfo font(layout_.labelfont);
217 font.realize(sane_font);
223 docstring s = layout_.labelstring;
224 theFontMetrics(font).rectText(s, w, a, d);
231 dim = dimensionCollapsed();
232 if (geometry() == TopButton
233 || geometry() == LeftButton) {
235 InsetText::metrics(mi, textdim);
236 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
238 // Correct for button width.
239 dim.wid += textdim.wid;
240 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
241 dim.asc = textdim.asc;
243 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
244 dim.wid = max(dim.wid, textdim.wid);
250 mi.base.font = tmpfont;
254 bool InsetCollapsable::setMouseHover(bool mouse_hover)
256 mouse_hover_ = mouse_hover;
261 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
263 autoOpen_ = pi.base.bv->cursor().isInside(this);
264 ColorCode const old_color = pi.background_color;
265 pi.background_color = backgroundColor();
267 FontInfo tmpfont = pi.base.font;
268 getDrawFont(pi.base.font);
269 pi.base.font.realize(tmpfont);
271 // Draw button first -- top, left or only
272 Dimension dimc = dimensionCollapsed();
274 if (geometry() == TopButton ||
275 geometry() == LeftButton ||
276 geometry() == ButtonOnly) {
277 button_dim.x1 = x + 0;
278 button_dim.x2 = x + dimc.width();
279 button_dim.y1 = y - dimc.asc;
280 button_dim.y2 = y + dimc.des;
282 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
290 Dimension const textdim = InsetText::dimension(*pi.base.bv);
291 int const baseline = y;
293 switch (geometry()) {
295 textx = x + dimc.width();
297 InsetText::draw(pi, textx, texty);
301 texty = baseline + dimc.des + textdim.asc;
302 InsetText::draw(pi, textx, texty);
309 InsetText::draw(pi, textx, texty);
315 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
316 InsetText::draw(pi, textx, texty);
317 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
319 int desc = textdim.descent();
320 if (geometry() == Corners)
323 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
324 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
325 pi.pain.line(xx1, y + desc - 4,
327 layout_.labelfont.color());
328 if (internalStatus() == Open)
329 pi.pain.line(xx1, y + desc,
331 layout_.labelfont.color());
333 // Make status_ value visible:
334 pi.pain.line(xx1, y + desc,
336 layout_.labelfont.color());
337 pi.pain.line(xx2 - 4, y + desc,
339 layout_.labelfont.color());
341 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
342 layout_.labelfont.color());
344 // the label below the text. Can be toggled.
345 if (geometry() == SubLabel) {
346 FontInfo font(layout_.labelfont);
347 font.realize(sane_font);
353 docstring s = layout_.labelstring;
354 theFontMetrics(font).rectText(s, w, a, d);
355 int const ww = max(textdim.wid, w);
356 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
357 s, font, Color_none, Color_none);
361 // a visual cue when the cursor is inside the inset
362 Cursor & cur = pi.base.bv->cursor();
363 if (cur.isInside(this)) {
366 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
367 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
368 pi.pain.line(xx2, y + 4, xx2, y,
369 layout_.labelfont.color());
370 pi.pain.line(xx2 - 4, y, xx2, y,
371 layout_.labelfont.color());
375 pi.background_color = old_color;
377 pi.base.font = tmpfont;
381 void InsetCollapsable::cursorPos(BufferView const & bv,
382 CursorSlice const & sl, bool boundary, int & x, int & y) const
384 if (geometry() == ButtonOnly)
386 BOOST_ASSERT(geometry() != ButtonOnly);
388 InsetText::cursorPos(bv, sl, boundary, x, y);
389 Dimension const textdim = InsetText::dimension(bv);
391 switch (geometry()) {
393 x += dimensionCollapsed().wid;
396 y += dimensionCollapsed().des + textdim.asc;
411 Inset::EDITABLE InsetCollapsable::editable() const
413 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
417 bool InsetCollapsable::descendable() const
419 return geometry() != ButtonOnly;
423 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
425 return button_dim.contains(cmd.x, cmd.y);
429 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
432 pos_type const max_length = 15;
433 pos_type const p_siz = paragraphs().begin()->size();
434 pos_type const n = std::min(max_length, p_siz);
437 for (; i < n && j < p_siz; ++j) {
438 if (paragraphs().begin()->isInset(j))
440 label += paragraphs().begin()->getChar(j);
443 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
446 return label.empty() ? l : label;
450 void InsetCollapsable::edit(Cursor & cur, bool left)
452 //lyxerr << "InsetCollapsable: edit left/right" << endl;
454 InsetText::edit(cur, left);
458 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
460 //lyxerr << "InsetCollapsable: edit xy" << endl;
461 if (geometry() == ButtonOnly
462 || (button_dim.contains(x, y)
463 && geometry() != NoButton))
466 return InsetText::editXY(cur, x, y);
470 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
472 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
473 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
475 switch (cmd.action) {
476 case LFUN_MOUSE_PRESS:
477 if (cmd.button() == mouse_button::button1
479 && geometry() != NoButton) {
480 // reset selection if necessary (see bug 3060)
482 cur.bv().cursor().clearSelection();
488 if (geometry() == NoButton)
489 InsetText::doDispatch(cur, cmd);
490 else if (geometry() != ButtonOnly
492 InsetText::doDispatch(cur, cmd);
497 case LFUN_MOUSE_MOTION:
498 case LFUN_MOUSE_DOUBLE:
499 case LFUN_MOUSE_TRIPLE:
500 if (geometry() == NoButton)
501 InsetText::doDispatch(cur, cmd);
502 else if (geometry() != ButtonOnly
504 InsetText::doDispatch(cur, cmd);
509 case LFUN_MOUSE_RELEASE:
510 if (cmd.button() == mouse_button::button3) {
511 // There is no button to right click:
512 if (geometry() == Corners ||
513 geometry() == SubLabel ||
514 geometry() == NoButton) {
515 if (internalStatus() == Open)
516 setStatus(cur, Collapsed);
518 setStatus(cur, Open);
522 // configuration dialog
523 showInsetDialog(&cur.bv());
528 if (geometry() == NoButton) {
529 // The mouse click has to be within the inset!
530 InsetText::doDispatch(cur, cmd);
534 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
535 // if we are selecting, we do not want to
539 // Left button is clicked, the user asks to
540 // toggle the inset visual state.
542 cur.updateFlags(Update::Force | Update::FitCursor);
543 if (geometry() == ButtonOnly) {
544 setStatus(cur, Open);
548 setStatus(cur, Collapsed);
550 cur.bv().cursor() = cur;
554 // The mouse click is within the opened inset.
555 if (geometry() == TopButton
556 || geometry() == LeftButton)
557 InsetText::doDispatch(cur, cmd);
560 case LFUN_INSET_TOGGLE:
561 if (cmd.argument() == "open")
562 setStatus(cur, Open);
563 else if (cmd.argument() == "close")
564 setStatus(cur, Collapsed);
565 else if (cmd.argument() == "toggle" || cmd.argument().empty())
566 if (internalStatus() == Open) {
567 setStatus(cur, Collapsed);
568 if (geometry() == ButtonOnly)
569 cur.top().forwardPos();
571 setStatus(cur, Open);
572 else // if assign or anything else
578 case LFUN_CLIPBOARD_PASTE:
579 case LFUN_PRIMARY_SELECTION_PASTE: {
580 InsetText::doDispatch(cur, cmd);
581 // Since we can only store plain text, we must reset all
583 // FIXME: Change only the pasted paragraphs
585 resetParagraphsFont();
590 if (layout_.forceltr) {
591 // Force any new text to latex_language
592 // FIXME: This should only be necessary in constructor, but
593 // new paragraphs that are created by pressing enter at the
594 // start of an existing paragraph get the buffer language
595 // and not latex_language, so we take this brute force
597 cur.current_font.setLanguage(latex_language);
598 cur.real_current_font.setLanguage(latex_language);
600 InsetText::doDispatch(cur, cmd);
606 bool InsetCollapsable::allowMultiPar() const
608 return layout_.multipar;
612 void InsetCollapsable::resetParagraphsFont()
615 font.fontInfo() = sane_font;
616 if (layout_.forceltr)
617 font.setLanguage(latex_language);
618 if (layout_.passthru) {
619 ParagraphList::iterator par = paragraphs().begin();
620 ParagraphList::iterator const end = paragraphs().end();
622 par->resetFonts(font);
623 par->params().clear();
630 void InsetCollapsable::getDrawFont(FontInfo & font) const
636 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
637 FuncStatus & flag) const
639 switch (cmd.action) {
641 case LFUN_ACCENT_ACUTE:
642 case LFUN_ACCENT_BREVE:
643 case LFUN_ACCENT_CARON:
644 case LFUN_ACCENT_CEDILLA:
645 case LFUN_ACCENT_CIRCLE:
646 case LFUN_ACCENT_CIRCUMFLEX:
647 case LFUN_ACCENT_DOT:
648 case LFUN_ACCENT_GRAVE:
649 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
650 case LFUN_ACCENT_MACRON:
651 case LFUN_ACCENT_OGONEK:
652 case LFUN_ACCENT_SPECIAL_CARON:
653 case LFUN_ACCENT_TIE:
654 case LFUN_ACCENT_TILDE:
655 case LFUN_ACCENT_UMLAUT:
656 case LFUN_ACCENT_UNDERBAR:
657 case LFUN_ACCENT_UNDERDOT:
659 case LFUN_BIBITEM_INSERT:
660 case LFUN_BOX_INSERT:
661 case LFUN_BRANCH_INSERT:
662 case LFUN_BREAK_LINE:
663 case LFUN_CAPTION_INSERT:
664 case LFUN_CLEARPAGE_INSERT:
665 case LFUN_CLEARDOUBLEPAGE_INSERT:
666 case LFUN_DEPTH_DECREMENT:
667 case LFUN_DEPTH_INCREMENT:
668 case LFUN_DOTS_INSERT:
669 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
670 case LFUN_ENVIRONMENT_INSERT:
671 case LFUN_ERT_INSERT:
672 case LFUN_FILE_INSERT:
673 case LFUN_FLEX_INSERT:
674 case LFUN_FLOAT_INSERT:
675 case LFUN_FLOAT_LIST:
676 case LFUN_FLOAT_WIDE_INSERT:
678 case LFUN_FONT_TYPEWRITER:
679 case LFUN_FONT_DEFAULT:
681 case LFUN_FONT_FREE_APPLY:
682 case LFUN_FONT_FREE_UPDATE:
684 case LFUN_FONT_ROMAN:
689 case LFUN_FONT_STATE:
690 case LFUN_FONT_UNDERLINE:
691 case LFUN_FOOTNOTE_INSERT:
692 case LFUN_HFILL_INSERT:
693 case LFUN_HYPERLINK_INSERT:
694 case LFUN_HYPHENATION_POINT_INSERT:
695 case LFUN_INDEX_INSERT:
696 case LFUN_INDEX_PRINT:
697 case LFUN_INSET_INSERT:
698 case LFUN_LABEL_GOTO:
699 case LFUN_LABEL_INSERT:
700 case LFUN_LIGATURE_BREAK_INSERT:
701 case LFUN_LINE_INSERT:
702 case LFUN_PAGEBREAK_INSERT:
704 case LFUN_LAYOUT_PARAGRAPH:
705 case LFUN_LAYOUT_TABULAR:
706 case LFUN_MARGINALNOTE_INSERT:
707 case LFUN_MATH_DISPLAY:
708 case LFUN_MATH_INSERT:
709 case LFUN_MATH_MATRIX:
712 case LFUN_MENU_SEPARATOR_INSERT:
714 case LFUN_NOMENCL_INSERT:
715 case LFUN_NOMENCL_PRINT:
716 case LFUN_NOTE_INSERT:
718 case LFUN_OPTIONAL_INSERT:
719 case LFUN_PARAGRAPH_PARAMS:
720 case LFUN_PARAGRAPH_PARAMS_APPLY:
721 case LFUN_PARAGRAPH_SPACING:
722 case LFUN_PARAGRAPH_UPDATE:
723 case LFUN_REFERENCE_NEXT:
724 case LFUN_SERVER_GOTO_FILE_ROW:
725 case LFUN_SERVER_NOTIFY:
726 case LFUN_SERVER_SET_XY:
727 case LFUN_SPACE_INSERT:
728 case LFUN_TABULAR_INSERT:
729 case LFUN_TOC_INSERT:
730 case LFUN_WRAP_INSERT:
731 if (layout_.passthru) {
735 return InsetText::getStatus(cur, cmd, flag);
737 case LFUN_INSET_TOGGLE:
738 if (cmd.argument() == "open" || cmd.argument() == "close" ||
739 cmd.argument() == "toggle")
746 flag.enabled(!layout_.forceltr);
747 return InsetText::getStatus(cur, cmd, flag);
749 case LFUN_BREAK_PARAGRAPH:
750 case LFUN_BREAK_PARAGRAPH_SKIP:
751 flag.enabled(layout_.multipar);
755 return InsetText::getStatus(cur, cmd, flag);
760 void InsetCollapsable::setLabel(docstring const & l)
762 layout_.labelstring = l;
766 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
770 if (status_ == Collapsed)
771 cur.leaveInset(*this);
775 void InsetCollapsable::setLabelFont(FontInfo const & font)
777 layout_.labelfont = font;
781 void InsetCollapsable::setLabelColor(ColorCode code)
783 layout_.labelfont.setColor(code);
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_.decoration == "classic")
800 if (layout_.decoration == "minimalistic")
802 if (layout_.decoration == "conglomerate")
804 if (name() == from_ascii("Flex"))
810 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
811 OutputParams const & runparams) const
813 // This implements the standard way of handling the LaTeX output of
814 // a collapsable inset, either a command or an environment. Standard
815 // collapsable insets should not redefine this, non-standard ones may
817 if (!layout_.latexname.empty()) {
818 if (layout_.latextype == "command") {
820 if (runparams.moving_arg)
822 os << '\\' << from_utf8(layout_.latexname);
823 if (!layout_.latexparam.empty())
824 os << from_utf8(layout_.latexparam);
826 } else if (layout_.latextype == "environment") {
827 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
828 if (!layout_.latexparam.empty())
829 os << from_utf8(layout_.latexparam);
832 OutputParams rp = runparams;
833 if (layout_.passthru)
835 if (layout_.needprotect)
836 rp.moving_arg = true;
837 int i = InsetText::latex(buf, os, rp);
838 if (!layout_.latexname.empty()) {
839 if (layout_.latextype == "command") {
841 } else if (layout_.latextype == "environment") {
842 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
850 void InsetCollapsable::validate(LaTeXFeatures & features) const
852 // Force inclusion of preamble snippet in layout file
853 features.require(layout_.name);
854 InsetText::validate(features);