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"
49 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
51 return autoOpen_ ? Open : status_;
55 InsetCollapsable::Geometry InsetCollapsable::geometry() const
57 switch (decoration()) {
59 if (status() == Open) {
68 return status() == Open ? NoButton : ButtonOnly ;
71 return status() == Open ? SubLabel : Corners ;
74 // dummy return value to shut down a warning,
80 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
81 CollapseStatus status, InsetLayout const * il)
82 : InsetText(bp), layout_(il), status_(status),
83 openinlined_(false), autoOpen_(false), mouse_hover_(false)
85 setAutoBreakRows(true);
87 setFrameColor(Color_collapsableframe);
91 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
94 labelstring_(rhs.labelstring_),
95 button_dim(rhs.button_dim),
97 openinlined_(rhs.openinlined_),
98 autoOpen_(rhs.autoOpen_),
99 // the sole purpose of this copy constructor
105 void InsetCollapsable::setLayout(BufferParams const & bp)
107 setLayout(bp.getTextClass().insetlayout(name()));
111 void InsetCollapsable::setLayout(InsetLayout const & il)
114 labelstring_ = layout_->labelstring;
120 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
132 text_.write(buf, os);
136 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
138 bool token_found = false;
141 string const token = lex.getString();
142 if (token == "status") {
144 string const tmp_token = lex.getString();
146 if (tmp_token == "collapsed") {
149 } else if (tmp_token == "open") {
153 lyxerr << "InsetCollapsable::read: Missing status!"
155 // Take countermeasures
156 lex.pushToken(token);
159 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
160 // take countermeasures
161 lex.pushToken(token);
164 InsetText::read(buf, lex);
166 setLayout(buf.params());
169 status_ = isOpen() ? Open : Collapsed;
171 // Force default font, if so requested
172 // This avoids paragraphs in buffer language that would have a
173 // foreign language after a document language change, and it ensures
174 // that all new text in ERT and similar gets the "latex" language,
175 // since new text inherits the language from the last position of the
176 // existing text. As a side effect this makes us also robust against
177 // bugs in LyX that might lead to font changes in ERT in .lyx files.
178 resetParagraphsFont();
182 Dimension InsetCollapsable::dimensionCollapsed() const
184 BOOST_ASSERT(layout_);
186 theFontMetrics(layout_->labelfont).buttonText(
187 labelstring_, dim.wid, dim.asc, dim.des);
192 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
194 BOOST_ASSERT(layout_);
196 autoOpen_ = mi.base.bv->cursor().isInside(this);
198 FontInfo tmpfont = mi.base.font;
199 mi.base.font = layout_->font;
200 mi.base.font.realize(tmpfont);
202 switch (geometry()) {
204 InsetText::metrics(mi, dim);
207 InsetText::metrics(mi, dim);
212 InsetText::metrics(mi, dim);
213 // consider width of the inset label
214 FontInfo font(layout_->labelfont);
215 font.realize(sane_font);
221 theFontMetrics(font).rectText(labelstring_, w, a, d);
228 dim = dimensionCollapsed();
229 if (geometry() == TopButton
230 || geometry() == LeftButton) {
232 InsetText::metrics(mi, textdim);
233 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
235 // Correct for button width.
236 dim.wid += textdim.wid;
237 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
238 dim.asc = textdim.asc;
240 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
241 dim.wid = max(dim.wid, textdim.wid);
247 mi.base.font = tmpfont;
251 bool InsetCollapsable::setMouseHover(bool mouse_hover)
253 mouse_hover_ = mouse_hover;
258 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
260 BOOST_ASSERT(layout_);
262 autoOpen_ = pi.base.bv->cursor().isInside(this);
263 ColorCode const old_color = pi.background_color;
264 pi.background_color = backgroundColor();
266 FontInfo tmpfont = pi.base.font;
267 pi.base.font = layout_->font;
268 pi.base.font.realize(tmpfont);
270 // Draw button first -- top, left or only
271 Dimension dimc = dimensionCollapsed();
273 if (geometry() == TopButton ||
274 geometry() == LeftButton ||
275 geometry() == ButtonOnly) {
276 button_dim.x1 = x + 0;
277 button_dim.x2 = x + dimc.width();
278 button_dim.y1 = y - dimc.asc;
279 button_dim.y2 = y + dimc.des;
281 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
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());
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 theFontMetrics(font).rectText(labelstring_, w, a, d);
354 int const ww = max(textdim.wid, w);
355 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
356 labelstring_, font, Color_none, Color_none);
360 // a visual cue when the cursor is inside the inset
361 Cursor & cur = pi.base.bv->cursor();
362 if (cur.isInside(this)) {
365 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
366 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
367 pi.pain.line(xx2, y + 4, xx2, y,
368 layout_->labelfont.color());
369 pi.pain.line(xx2 - 4, y, xx2, y,
370 layout_->labelfont.color());
374 pi.background_color = old_color;
376 pi.base.font = tmpfont;
380 void InsetCollapsable::cursorPos(BufferView const & bv,
381 CursorSlice const & sl, bool boundary, int & x, int & y) const
383 if (geometry() == ButtonOnly)
385 BOOST_ASSERT(geometry() != ButtonOnly);
387 InsetText::cursorPos(bv, sl, boundary, x, y);
388 Dimension const textdim = InsetText::dimension(bv);
390 switch (geometry()) {
392 x += dimensionCollapsed().wid;
395 y += dimensionCollapsed().des + textdim.asc;
410 Inset::EDITABLE InsetCollapsable::editable() const
412 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
416 bool InsetCollapsable::descendable() const
418 return geometry() != ButtonOnly;
422 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
424 return button_dim.contains(cmd.x, cmd.y);
428 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
431 pos_type const max_length = 15;
432 pos_type const p_siz = paragraphs().begin()->size();
433 pos_type const n = std::min(max_length, p_siz);
436 for (; i < n && j < p_siz; ++j) {
437 if (paragraphs().begin()->isInset(j))
439 label += paragraphs().begin()->getChar(j);
442 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
445 return label.empty() ? l : label;
449 void InsetCollapsable::edit(Cursor & cur, bool left)
451 //lyxerr << "InsetCollapsable: edit left/right" << endl;
453 InsetText::edit(cur, left);
457 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
459 //lyxerr << "InsetCollapsable: edit xy" << endl;
460 if (geometry() == ButtonOnly
461 || (button_dim.contains(x, y)
462 && geometry() != NoButton))
465 return InsetText::editXY(cur, x, y);
469 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
471 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
472 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
474 switch (cmd.action) {
475 case LFUN_MOUSE_PRESS:
476 if (cmd.button() == mouse_button::button1
478 && geometry() != NoButton) {
479 // reset selection if necessary (see bug 3060)
481 cur.bv().cursor().clearSelection();
487 if (geometry() == NoButton)
488 InsetText::doDispatch(cur, cmd);
489 else if (geometry() != ButtonOnly
491 InsetText::doDispatch(cur, cmd);
496 case LFUN_MOUSE_MOTION:
497 case LFUN_MOUSE_DOUBLE:
498 case LFUN_MOUSE_TRIPLE:
499 if (geometry() == NoButton)
500 InsetText::doDispatch(cur, cmd);
501 else if (geometry() != ButtonOnly
503 InsetText::doDispatch(cur, cmd);
508 case LFUN_MOUSE_RELEASE:
509 if (cmd.button() == mouse_button::button3) {
510 // There is no button to right click:
511 if (geometry() == Corners ||
512 geometry() == SubLabel ||
513 geometry() == NoButton) {
515 setStatus(cur, Collapsed);
517 setStatus(cur, Open);
521 // configuration dialog
522 showInsetDialog(&cur.bv());
527 if (geometry() == NoButton) {
528 // The mouse click has to be within the inset!
529 InsetText::doDispatch(cur, cmd);
533 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
534 // if we are selecting, we do not want to
538 // Left button is clicked, the user asks to
539 // toggle the inset visual state.
541 cur.updateFlags(Update::Force | Update::FitCursor);
542 if (geometry() == ButtonOnly) {
543 setStatus(cur, Open);
547 setStatus(cur, Collapsed);
549 cur.bv().cursor() = cur;
553 // The mouse click is within the opened inset.
554 if (geometry() == TopButton
555 || geometry() == LeftButton)
556 InsetText::doDispatch(cur, cmd);
559 case LFUN_INSET_TOGGLE:
560 if (cmd.argument() == "open")
561 setStatus(cur, Open);
562 else if (cmd.argument() == "close")
563 setStatus(cur, Collapsed);
564 else if (cmd.argument() == "toggle" || cmd.argument().empty())
565 if (status_ == Open) {
566 setStatus(cur, Collapsed);
567 if (geometry() == ButtonOnly)
568 cur.top().forwardPos();
570 setStatus(cur, Open);
571 else // if assign or anything else
577 case LFUN_CLIPBOARD_PASTE:
578 case LFUN_PRIMARY_SELECTION_PASTE: {
579 InsetText::doDispatch(cur, cmd);
580 // Since we can only store plain text, we must reset all
582 // FIXME: Change only the pasted paragraphs
584 resetParagraphsFont();
589 if (layout_ && layout_->forceltr) {
590 // Force any new text to latex_language
591 // FIXME: This should only be necessary in constructor, but
592 // new paragraphs that are created by pressing enter at the
593 // start of an existing paragraph get the buffer language
594 // and not latex_language, so we take this brute force
596 cur.current_font.setLanguage(latex_language);
597 cur.real_current_font.setLanguage(latex_language);
599 InsetText::doDispatch(cur, cmd);
605 bool InsetCollapsable::allowMultiPar() const
607 return layout_->multipar;
611 void InsetCollapsable::resetParagraphsFont()
614 font.fontInfo() = inherit_font;
615 if (layout_->forceltr)
616 font.setLanguage(latex_language);
617 if (layout_->passthru) {
618 ParagraphList::iterator par = paragraphs().begin();
619 ParagraphList::iterator const end = paragraphs().end();
621 par->resetFonts(font);
622 par->params().clear();
629 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
630 FuncStatus & flag) const
632 switch (cmd.action) {
634 case LFUN_ACCENT_ACUTE:
635 case LFUN_ACCENT_BREVE:
636 case LFUN_ACCENT_CARON:
637 case LFUN_ACCENT_CEDILLA:
638 case LFUN_ACCENT_CIRCLE:
639 case LFUN_ACCENT_CIRCUMFLEX:
640 case LFUN_ACCENT_DOT:
641 case LFUN_ACCENT_GRAVE:
642 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
643 case LFUN_ACCENT_MACRON:
644 case LFUN_ACCENT_OGONEK:
645 case LFUN_ACCENT_SPECIAL_CARON:
646 case LFUN_ACCENT_TIE:
647 case LFUN_ACCENT_TILDE:
648 case LFUN_ACCENT_UMLAUT:
649 case LFUN_ACCENT_UNDERBAR:
650 case LFUN_ACCENT_UNDERDOT:
652 case LFUN_BIBITEM_INSERT:
653 case LFUN_BOX_INSERT:
654 case LFUN_BRANCH_INSERT:
656 case LFUN_CAPTION_INSERT:
657 case LFUN_CLEARPAGE_INSERT:
658 case LFUN_CLEARDOUBLEPAGE_INSERT:
659 case LFUN_DEPTH_DECREMENT:
660 case LFUN_DEPTH_INCREMENT:
661 case LFUN_DOTS_INSERT:
662 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
663 case LFUN_ENVIRONMENT_INSERT:
664 case LFUN_ERT_INSERT:
665 case LFUN_FILE_INSERT:
666 case LFUN_FLEX_INSERT:
667 case LFUN_FLOAT_INSERT:
668 case LFUN_FLOAT_LIST:
669 case LFUN_FLOAT_WIDE_INSERT:
671 case LFUN_FONT_TYPEWRITER:
672 case LFUN_FONT_DEFAULT:
674 case LFUN_FONT_FREE_APPLY:
675 case LFUN_FONT_FREE_UPDATE:
677 case LFUN_FONT_ROMAN:
682 case LFUN_FONT_STATE:
683 case LFUN_FONT_UNDERLINE:
684 case LFUN_FOOTNOTE_INSERT:
685 case LFUN_HFILL_INSERT:
686 case LFUN_HYPERLINK_INSERT:
687 case LFUN_HYPHENATION_POINT_INSERT:
688 case LFUN_INDEX_INSERT:
689 case LFUN_INDEX_PRINT:
690 case LFUN_INSET_INSERT:
691 case LFUN_LABEL_GOTO:
692 case LFUN_LABEL_INSERT:
693 case LFUN_LIGATURE_BREAK_INSERT:
694 case LFUN_LINE_INSERT:
695 case LFUN_NEWPAGE_INSERT:
696 case LFUN_PAGEBREAK_INSERT:
698 case LFUN_LAYOUT_PARAGRAPH:
699 case LFUN_LAYOUT_TABULAR:
700 case LFUN_MARGINALNOTE_INSERT:
701 case LFUN_MATH_DISPLAY:
702 case LFUN_MATH_INSERT:
703 case LFUN_MATH_MATRIX:
706 case LFUN_MENU_SEPARATOR_INSERT:
708 case LFUN_NOMENCL_INSERT:
709 case LFUN_NOMENCL_PRINT:
710 case LFUN_NOTE_INSERT:
712 case LFUN_OPTIONAL_INSERT:
713 case LFUN_PARAGRAPH_PARAMS:
714 case LFUN_PARAGRAPH_PARAMS_APPLY:
715 case LFUN_PARAGRAPH_SPACING:
716 case LFUN_PARAGRAPH_UPDATE:
717 case LFUN_REFERENCE_NEXT:
718 case LFUN_SERVER_GOTO_FILE_ROW:
719 case LFUN_SERVER_NOTIFY:
720 case LFUN_SERVER_SET_XY:
721 case LFUN_SPACE_INSERT:
722 case LFUN_TABULAR_INSERT:
723 case LFUN_TOC_INSERT:
724 case LFUN_WRAP_INSERT:
725 if (layout_->passthru) {
729 return InsetText::getStatus(cur, cmd, flag);
731 case LFUN_INSET_TOGGLE:
732 if (cmd.argument() == "open" || cmd.argument() == "close" ||
733 cmd.argument() == "toggle")
740 flag.enabled(!layout_->forceltr);
741 return InsetText::getStatus(cur, cmd, flag);
743 case LFUN_BREAK_PARAGRAPH:
744 case LFUN_BREAK_PARAGRAPH_SKIP:
745 flag.enabled(layout_->multipar);
749 return InsetText::getStatus(cur, cmd, flag);
754 void InsetCollapsable::setLabel(docstring const & l)
760 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
764 if (status_ == Collapsed)
765 cur.leaveInset(*this);
769 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
771 FloatList const & floats = bp.getTextClass().floats();
772 FloatList::const_iterator it = floats[type];
774 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
778 InsetCollapsable::Decoration InsetCollapsable::decoration() const
780 if (!layout_ || layout_->decoration == "classic")
782 if (layout_->decoration == "minimalistic")
784 if (layout_->decoration == "conglomerate")
786 if (lyxCode() == FLEX_CODE)
787 // FIXME: Is this really necessary?
793 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
794 OutputParams const & runparams) const
796 // FIXME: What should we do layout_ is 0?
798 // 2) through an error
802 // This implements the standard way of handling the LaTeX output of
803 // a collapsable inset, either a command or an environment. Standard
804 // collapsable insets should not redefine this, non-standard ones may
806 if (!layout_->latexname.empty()) {
807 if (layout_->latextype == "command") {
809 if (runparams.moving_arg)
811 os << '\\' << from_utf8(layout_->latexname);
812 if (!layout_->latexparam.empty())
813 os << from_utf8(layout_->latexparam);
815 } else if (layout_->latextype == "environment") {
816 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
817 if (!layout_->latexparam.empty())
818 os << from_utf8(layout_->latexparam);
821 OutputParams rp = runparams;
822 if (layout_->passthru)
824 if (layout_->needprotect)
825 rp.moving_arg = true;
826 int i = InsetText::latex(buf, os, rp);
827 if (!layout_->latexname.empty()) {
828 if (layout_->latextype == "command") {
830 } else if (layout_->latextype == "environment") {
831 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
839 void InsetCollapsable::validate(LaTeXFeatures & features) const
844 // Force inclusion of preamble snippet in layout file
845 features.require(layout_->name);
846 InsetText::validate(features);