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 void InsetCollapsable::setLayout(BufferParams const & bp)
103 setLayout(bp.getTextClass().insetlayout(name()));
107 void InsetCollapsable::setLayout(InsetLayout const & il)
110 labelstring_ = layout_->labelstring;
116 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
128 text_.write(buf, os);
132 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
134 bool token_found = false;
137 string const token = lex.getString();
138 if (token == "status") {
140 string const tmp_token = lex.getString();
142 if (tmp_token == "collapsed") {
145 } else if (tmp_token == "open") {
149 lyxerr << "InsetCollapsable::read: Missing status!"
151 // Take countermeasures
152 lex.pushToken(token);
155 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
156 // take countermeasures
157 lex.pushToken(token);
160 InsetText::read(buf, lex);
162 setLayout(buf.params());
165 status_ = isOpen() ? Open : Collapsed;
167 // Force default font, if so requested
168 // This avoids paragraphs in buffer language that would have a
169 // foreign language after a document language change, and it ensures
170 // that all new text in ERT and similar gets the "latex" language,
171 // since new text inherits the language from the last position of the
172 // existing text. As a side effect this makes us also robust against
173 // bugs in LyX that might lead to font changes in ERT in .lyx files.
174 resetParagraphsFont();
178 Dimension InsetCollapsable::dimensionCollapsed() const
180 BOOST_ASSERT(layout_);
182 theFontMetrics(layout_->labelfont).buttonText(
183 labelstring_, dim.wid, dim.asc, dim.des);
188 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
190 BOOST_ASSERT(layout_);
192 autoOpen_ = mi.base.bv->cursor().isInside(this);
194 FontInfo tmpfont = mi.base.font;
195 mi.base.font = layout_->font;
196 mi.base.font.realize(tmpfont);
198 switch (geometry()) {
200 InsetText::metrics(mi, dim);
203 InsetText::metrics(mi, dim);
208 InsetText::metrics(mi, dim);
209 // consider width of the inset label
210 FontInfo font(layout_->labelfont);
211 font.realize(sane_font);
217 theFontMetrics(font).rectText(labelstring_, w, a, d);
224 dim = dimensionCollapsed();
225 if (geometry() == TopButton
226 || geometry() == LeftButton) {
228 InsetText::metrics(mi, textdim);
229 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
231 // Correct for button width.
232 dim.wid += textdim.wid;
233 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
234 dim.asc = textdim.asc;
236 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
237 dim.wid = max(dim.wid, textdim.wid);
243 mi.base.font = tmpfont;
247 bool InsetCollapsable::setMouseHover(bool mouse_hover)
249 mouse_hover_ = mouse_hover;
254 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
256 BOOST_ASSERT(layout_);
258 autoOpen_ = pi.base.bv->cursor().isInside(this);
259 ColorCode const old_color = pi.background_color;
260 pi.background_color = backgroundColor();
262 FontInfo tmpfont = pi.base.font;
263 pi.base.font = layout_->font;
264 pi.base.font.realize(tmpfont);
266 // Draw button first -- top, left or only
267 Dimension dimc = dimensionCollapsed();
269 if (geometry() == TopButton ||
270 geometry() == LeftButton ||
271 geometry() == ButtonOnly) {
272 button_dim.x1 = x + 0;
273 button_dim.x2 = x + dimc.width();
274 button_dim.y1 = y - dimc.asc;
275 button_dim.y2 = y + dimc.des;
277 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
286 Dimension const textdim = InsetText::dimension(*pi.base.bv);
287 int const baseline = y;
289 switch (geometry()) {
291 textx = x + dimc.width();
293 InsetText::draw(pi, textx, texty);
297 texty = baseline + dimc.des + textdim.asc;
298 InsetText::draw(pi, textx, texty);
305 InsetText::draw(pi, textx, texty);
311 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
312 InsetText::draw(pi, textx, texty);
313 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
315 int desc = textdim.descent();
316 if (geometry() == Corners)
319 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
320 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
321 pi.pain.line(xx1, y + desc - 4,
323 layout_->labelfont.color());
325 pi.pain.line(xx1, y + desc,
327 layout_->labelfont.color());
329 // Make status_ value visible:
330 pi.pain.line(xx1, y + desc,
332 layout_->labelfont.color());
333 pi.pain.line(xx2 - 4, y + desc,
335 layout_->labelfont.color());
337 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
338 layout_->labelfont.color());
340 // the label below the text. Can be toggled.
341 if (geometry() == SubLabel) {
342 FontInfo font(layout_->labelfont);
343 font.realize(sane_font);
349 theFontMetrics(font).rectText(labelstring_, w, a, d);
350 int const ww = max(textdim.wid, w);
351 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
352 labelstring_, font, Color_none, Color_none);
356 // a visual cue when the cursor is inside the inset
357 Cursor & cur = pi.base.bv->cursor();
358 if (cur.isInside(this)) {
361 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
362 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
363 pi.pain.line(xx2, y + 4, xx2, y,
364 layout_->labelfont.color());
365 pi.pain.line(xx2 - 4, y, xx2, y,
366 layout_->labelfont.color());
370 pi.background_color = old_color;
372 pi.base.font = tmpfont;
376 void InsetCollapsable::cursorPos(BufferView const & bv,
377 CursorSlice const & sl, bool boundary, int & x, int & y) const
379 if (geometry() == ButtonOnly)
381 BOOST_ASSERT(geometry() != ButtonOnly);
383 InsetText::cursorPos(bv, sl, boundary, x, y);
384 Dimension const textdim = InsetText::dimension(bv);
386 switch (geometry()) {
388 x += dimensionCollapsed().wid;
391 y += dimensionCollapsed().des + textdim.asc;
406 Inset::EDITABLE InsetCollapsable::editable() const
408 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
412 bool InsetCollapsable::descendable() const
414 return geometry() != ButtonOnly;
418 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
420 return button_dim.contains(cmd.x, cmd.y);
424 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
427 pos_type const max_length = 15;
428 pos_type const p_siz = paragraphs().begin()->size();
429 pos_type const n = min(max_length, p_siz);
432 for (; i < n && j < p_siz; ++j) {
433 if (paragraphs().begin()->isInset(j))
435 label += paragraphs().begin()->getChar(j);
438 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
441 return label.empty() ? l : label;
445 void InsetCollapsable::edit(Cursor & cur, bool left)
447 //lyxerr << "InsetCollapsable: edit left/right" << endl;
449 InsetText::edit(cur, left);
453 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
455 //lyxerr << "InsetCollapsable: edit xy" << endl;
456 if (geometry() == ButtonOnly
457 || (button_dim.contains(x, y)
458 && geometry() != NoButton))
461 return InsetText::editXY(cur, x, y);
465 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
467 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
468 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
470 switch (cmd.action) {
471 case LFUN_MOUSE_PRESS:
472 if (cmd.button() == mouse_button::button1
474 && geometry() != NoButton) {
475 // reset selection if necessary (see bug 3060)
477 cur.bv().cursor().clearSelection();
483 if (geometry() == NoButton)
484 InsetText::doDispatch(cur, cmd);
485 else if (geometry() != ButtonOnly
487 InsetText::doDispatch(cur, cmd);
492 case LFUN_MOUSE_MOTION:
493 case LFUN_MOUSE_DOUBLE:
494 case LFUN_MOUSE_TRIPLE:
495 if (geometry() == NoButton)
496 InsetText::doDispatch(cur, cmd);
497 else if (geometry() != ButtonOnly
499 InsetText::doDispatch(cur, cmd);
504 case LFUN_MOUSE_RELEASE:
505 if (cmd.button() == mouse_button::button3) {
506 // There is no button to right click:
507 if (geometry() == Corners ||
508 geometry() == SubLabel ||
509 geometry() == NoButton) {
511 setStatus(cur, Collapsed);
513 setStatus(cur, Open);
517 // configuration dialog
518 showInsetDialog(&cur.bv());
523 if (geometry() == NoButton) {
524 // The mouse click has to be within the inset!
525 InsetText::doDispatch(cur, cmd);
529 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
530 // if we are selecting, we do not want to
534 // Left button is clicked, the user asks to
535 // toggle the inset visual state.
537 cur.updateFlags(Update::Force | Update::FitCursor);
538 if (geometry() == ButtonOnly) {
539 setStatus(cur, Open);
543 setStatus(cur, Collapsed);
545 cur.bv().cursor() = cur;
549 // The mouse click is within the opened inset.
550 if (geometry() == TopButton
551 || geometry() == LeftButton)
552 InsetText::doDispatch(cur, cmd);
555 case LFUN_INSET_TOGGLE:
556 if (cmd.argument() == "open")
557 setStatus(cur, Open);
558 else if (cmd.argument() == "close")
559 setStatus(cur, Collapsed);
560 else if (cmd.argument() == "toggle" || cmd.argument().empty())
561 if (status_ == Open) {
562 setStatus(cur, Collapsed);
563 if (geometry() == ButtonOnly)
564 cur.top().forwardPos();
566 setStatus(cur, Open);
567 else // if assign or anything else
573 case LFUN_CLIPBOARD_PASTE:
574 case LFUN_PRIMARY_SELECTION_PASTE: {
575 InsetText::doDispatch(cur, cmd);
576 // Since we can only store plain text, we must reset all
578 // FIXME: Change only the pasted paragraphs
580 resetParagraphsFont();
585 if (layout_ && layout_->forceltr) {
586 // Force any new text to latex_language
587 // FIXME: This should only be necessary in constructor, but
588 // new paragraphs that are created by pressing enter at the
589 // start of an existing paragraph get the buffer language
590 // and not latex_language, so we take this brute force
592 cur.current_font.setLanguage(latex_language);
593 cur.real_current_font.setLanguage(latex_language);
595 InsetText::doDispatch(cur, cmd);
601 bool InsetCollapsable::allowMultiPar() const
603 return layout_->multipar;
607 void InsetCollapsable::resetParagraphsFont()
610 font.fontInfo() = inherit_font;
611 if (layout_->forceltr)
612 font.setLanguage(latex_language);
613 if (layout_->passthru) {
614 ParagraphList::iterator par = paragraphs().begin();
615 ParagraphList::iterator const end = paragraphs().end();
617 par->resetFonts(font);
618 par->params().clear();
625 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
626 FuncStatus & flag) const
628 switch (cmd.action) {
630 case LFUN_ACCENT_ACUTE:
631 case LFUN_ACCENT_BREVE:
632 case LFUN_ACCENT_CARON:
633 case LFUN_ACCENT_CEDILLA:
634 case LFUN_ACCENT_CIRCLE:
635 case LFUN_ACCENT_CIRCUMFLEX:
636 case LFUN_ACCENT_DOT:
637 case LFUN_ACCENT_GRAVE:
638 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
639 case LFUN_ACCENT_MACRON:
640 case LFUN_ACCENT_OGONEK:
641 case LFUN_ACCENT_SPECIAL_CARON:
642 case LFUN_ACCENT_TIE:
643 case LFUN_ACCENT_TILDE:
644 case LFUN_ACCENT_UMLAUT:
645 case LFUN_ACCENT_UNDERBAR:
646 case LFUN_ACCENT_UNDERDOT:
648 case LFUN_BIBITEM_INSERT:
649 case LFUN_BOX_INSERT:
650 case LFUN_BRANCH_INSERT:
652 case LFUN_CAPTION_INSERT:
653 case LFUN_CLEARPAGE_INSERT:
654 case LFUN_CLEARDOUBLEPAGE_INSERT:
655 case LFUN_DEPTH_DECREMENT:
656 case LFUN_DEPTH_INCREMENT:
657 case LFUN_ENVIRONMENT_INSERT:
658 case LFUN_ERT_INSERT:
659 case LFUN_FILE_INSERT:
660 case LFUN_FLEX_INSERT:
661 case LFUN_FLOAT_INSERT:
662 case LFUN_FLOAT_LIST:
663 case LFUN_FLOAT_WIDE_INSERT:
665 case LFUN_FONT_TYPEWRITER:
666 case LFUN_FONT_DEFAULT:
668 case LFUN_FONT_FREE_APPLY:
669 case LFUN_FONT_FREE_UPDATE:
671 case LFUN_FONT_ROMAN:
676 case LFUN_FONT_STATE:
677 case LFUN_FONT_UNDERLINE:
678 case LFUN_FOOTNOTE_INSERT:
679 case LFUN_HFILL_INSERT:
680 case LFUN_HYPERLINK_INSERT:
681 case LFUN_INDEX_INSERT:
682 case LFUN_INDEX_PRINT:
683 case LFUN_INSET_INSERT:
684 case LFUN_LABEL_GOTO:
685 case LFUN_LABEL_INSERT:
686 case LFUN_LINE_INSERT:
687 case LFUN_NEWPAGE_INSERT:
688 case LFUN_PAGEBREAK_INSERT:
690 case LFUN_LAYOUT_PARAGRAPH:
691 case LFUN_LAYOUT_TABULAR:
692 case LFUN_MARGINALNOTE_INSERT:
693 case LFUN_MATH_DISPLAY:
694 case LFUN_MATH_INSERT:
695 case LFUN_MATH_MATRIX:
699 case LFUN_NOMENCL_INSERT:
700 case LFUN_NOMENCL_PRINT:
701 case LFUN_NOTE_INSERT:
703 case LFUN_OPTIONAL_INSERT:
704 case LFUN_PARAGRAPH_PARAMS:
705 case LFUN_PARAGRAPH_PARAMS_APPLY:
706 case LFUN_PARAGRAPH_SPACING:
707 case LFUN_PARAGRAPH_UPDATE:
708 case LFUN_REFERENCE_NEXT:
709 case LFUN_SERVER_GOTO_FILE_ROW:
710 case LFUN_SERVER_NOTIFY:
711 case LFUN_SERVER_SET_XY:
712 case LFUN_SPACE_INSERT:
713 case LFUN_SPECIALCHAR_INSERT:
714 case LFUN_TABULAR_INSERT:
715 case LFUN_TOC_INSERT:
716 case LFUN_WRAP_INSERT:
717 if (layout_->passthru) {
721 return InsetText::getStatus(cur, cmd, flag);
723 case LFUN_INSET_TOGGLE:
724 if (cmd.argument() == "open" || cmd.argument() == "close" ||
725 cmd.argument() == "toggle")
732 flag.enabled(!layout_->forceltr);
733 return InsetText::getStatus(cur, cmd, flag);
735 case LFUN_BREAK_PARAGRAPH:
736 case LFUN_BREAK_PARAGRAPH_SKIP:
737 flag.enabled(layout_->multipar);
741 return InsetText::getStatus(cur, cmd, flag);
746 void InsetCollapsable::setLabel(docstring const & l)
752 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
756 if (status_ == Collapsed)
757 cur.leaveInset(*this);
761 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
763 FloatList const & floats = bp.getTextClass().floats();
764 FloatList::const_iterator it = floats[type];
766 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
770 InsetCollapsable::Decoration InsetCollapsable::decoration() const
772 if (!layout_ || layout_->decoration == "classic")
774 if (layout_->decoration == "minimalistic")
776 if (layout_->decoration == "conglomerate")
778 if (lyxCode() == FLEX_CODE)
779 // FIXME: Is this really necessary?
785 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
786 OutputParams const & runparams) const
788 // FIXME: What should we do layout_ is 0?
790 // 2) through an error
794 // This implements the standard way of handling the LaTeX output of
795 // a collapsable inset, either a command or an environment. Standard
796 // collapsable insets should not redefine this, non-standard ones may
798 if (!layout_->latexname.empty()) {
799 if (layout_->latextype == "command") {
801 if (runparams.moving_arg)
803 os << '\\' << from_utf8(layout_->latexname);
804 if (!layout_->latexparam.empty())
805 os << from_utf8(layout_->latexparam);
807 } else if (layout_->latextype == "environment") {
808 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
809 if (!layout_->latexparam.empty())
810 os << from_utf8(layout_->latexparam);
813 OutputParams rp = runparams;
814 if (layout_->passthru)
816 if (layout_->needprotect)
817 rp.moving_arg = true;
818 int i = InsetText::latex(buf, os, rp);
819 if (!layout_->latexname.empty()) {
820 if (layout_->latextype == "command") {
822 } else if (layout_->latextype == "environment") {
823 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
831 void InsetCollapsable::validate(LaTeXFeatures & features) const
836 // Force inclusion of preamble snippet in layout file
837 features.require(layout_->name);
838 InsetText::validate(features);