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 "Dimension.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "InsetLayout.h"
27 #include "LaTeXFeatures.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "TextClass.h"
33 #include "TextClassList.h"
35 #include "frontends/FontMetrics.h"
36 #include "frontends/Painter.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
49 return autoOpen_ ? Open : status_;
53 InsetCollapsable::Geometry InsetCollapsable::geometry() const
55 switch (decoration()) {
56 case InsetLayout::Classic:
57 if (status() == Open) {
65 case InsetLayout::Minimalistic:
66 return status() == Open ? NoButton : ButtonOnly ;
68 case InsetLayout::Conglomerate:
69 return status() == Open ? SubLabel : Corners ;
71 case InsetLayout::Default:
72 break; // this shouldn't happen
75 // dummy return value to shut down a warning,
81 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
82 CollapseStatus status, TextClassIndex tc)
83 : InsetText(bp), textClass_(tc), status_(status),
84 openinlined_(false), autoOpen_(false), mouse_hover_(false)
87 setAutoBreakRows(true);
89 setFrameColor(Color_collapsableframe);
90 paragraphs().back().setLayout(bp.textClass().emptyLayout());
94 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
96 textClass_(rhs.textClass_),
98 labelstring_(rhs.labelstring_),
99 button_dim(rhs.button_dim),
100 status_(rhs.status_),
101 openinlined_(rhs.openinlined_),
102 autoOpen_(rhs.autoOpen_),
103 // the sole purpose of this copy constructor
109 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
111 Dimension dim = dimensionCollapsed();
112 if (geometry() == NoButton)
113 return layout_->labelstring();
114 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
119 return _("Left-click to collapse the inset");
121 return _("Left-click to open the inset");
127 void InsetCollapsable::setLayout(BufferParams const & bp)
129 setLayout(bp.textClassIndex());
133 void InsetCollapsable::setLayout(TextClassIndex tcindex)
135 textClass_ = tcindex;
136 if (tcindex != TextClassIndex(-1)) {
137 layout_ = &textclasslist[tcindex].insetLayout(name());
138 labelstring_ = layout_->labelstring();
140 layout_ = &TextClass::emptyInsetLayout();
141 labelstring_ = _("UNDEFINED");
148 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
160 text_.write(buf, os);
164 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
166 bool token_found = false;
169 string const token = lex.getString();
170 if (token == "status") {
172 string const tmp_token = lex.getString();
174 if (tmp_token == "collapsed") {
177 } else if (tmp_token == "open") {
181 lyxerr << "InsetCollapsable::read: Missing status!"
183 // Take countermeasures
184 lex.pushToken(token);
187 LYXERR0("InsetCollapsable::read: Missing 'status'-tag!");
188 // take countermeasures
189 lex.pushToken(token);
193 // this must be set before we enter InsetText::read()
194 setLayout(buf.params());
196 InsetText::read(buf, lex);
199 status_ = isOpen() ? Open : Collapsed;
201 // Force default font, if so requested
202 // This avoids paragraphs in buffer language that would have a
203 // foreign language after a document language change, and it ensures
204 // that all new text in ERT and similar gets the "latex" language,
205 // since new text inherits the language from the last position of the
206 // existing text. As a side effect this makes us also robust against
207 // bugs in LyX that might lead to font changes in ERT in .lyx files.
208 resetParagraphsFont();
212 Dimension InsetCollapsable::dimensionCollapsed() const
214 BOOST_ASSERT(layout_);
216 theFontMetrics(layout_->labelfont()).buttonText(
217 labelstring_, dim.wid, dim.asc, dim.des);
222 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
224 BOOST_ASSERT(layout_);
226 autoOpen_ = mi.base.bv->cursor().isInside(this);
228 FontInfo tmpfont = mi.base.font;
229 mi.base.font = layout_->font();
230 mi.base.font.realize(tmpfont);
232 switch (geometry()) {
234 InsetText::metrics(mi, dim);
237 InsetText::metrics(mi, dim);
242 InsetText::metrics(mi, dim);
243 // consider width of the inset label
244 FontInfo font(layout_->labelfont());
245 font.realize(sane_font);
251 theFontMetrics(font).rectText(labelstring_, w, a, d);
258 dim = dimensionCollapsed();
259 if (geometry() == TopButton || geometry() == LeftButton) {
261 InsetText::metrics(mi, textdim);
262 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
264 // Correct for button width.
265 dim.wid += textdim.wid;
266 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
267 dim.asc = textdim.asc;
269 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
270 dim.wid = max(dim.wid, textdim.wid);
276 mi.base.font = tmpfont;
280 bool InsetCollapsable::setMouseHover(bool mouse_hover)
282 mouse_hover_ = mouse_hover;
287 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
289 BOOST_ASSERT(layout_);
291 autoOpen_ = pi.base.bv->cursor().isInside(this);
292 ColorCode const old_color = pi.background_color;
293 pi.background_color = backgroundColor();
295 FontInfo tmpfont = pi.base.font;
296 pi.base.font = layout_->font();
297 pi.base.font.realize(tmpfont);
299 // Draw button first -- top, left or only
300 Dimension dimc = dimensionCollapsed();
302 if (geometry() == TopButton ||
303 geometry() == LeftButton ||
304 geometry() == ButtonOnly) {
305 button_dim.x1 = x + 0;
306 button_dim.x2 = x + dimc.width();
307 button_dim.y1 = y - dimc.asc;
308 button_dim.y2 = y + dimc.des;
310 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
319 Dimension const textdim = InsetText::dimension(*pi.base.bv);
320 int const baseline = y;
322 switch (geometry()) {
324 textx = x + dimc.width();
326 InsetText::draw(pi, textx, texty);
330 texty = baseline + dimc.des + textdim.asc;
331 InsetText::draw(pi, textx, texty);
338 InsetText::draw(pi, textx, texty);
344 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
345 InsetText::draw(pi, textx, texty);
346 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
348 int desc = textdim.descent();
349 if (geometry() == Corners)
352 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
353 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
354 pi.pain.line(xx1, y + desc - 4,
356 layout_->labelfont().color());
358 pi.pain.line(xx1, y + desc,
360 layout_->labelfont().color());
362 // Make status_ value visible:
363 pi.pain.line(xx1, y + desc,
365 layout_->labelfont().color());
366 pi.pain.line(xx2 - 4, y + desc,
368 layout_->labelfont().color());
370 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
371 y + desc - 4, layout_->labelfont().color());
373 // the label below the text. Can be toggled.
374 if (geometry() == SubLabel) {
375 FontInfo font(layout_->labelfont());
376 font.realize(sane_font);
382 theFontMetrics(font).rectText(labelstring_, w, a, d);
383 int const ww = max(textdim.wid, w);
384 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
385 labelstring_, font, Color_none, Color_none);
389 // a visual cue when the cursor is inside the inset
390 Cursor & cur = pi.base.bv->cursor();
391 if (cur.isInside(this)) {
394 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
395 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
396 pi.pain.line(xx2, y + 4, xx2, y,
397 layout_->labelfont().color());
398 pi.pain.line(xx2 - 4, y, xx2, y,
399 layout_->labelfont().color());
403 pi.background_color = old_color;
405 pi.base.font = tmpfont;
409 void InsetCollapsable::cursorPos(BufferView const & bv,
410 CursorSlice const & sl, bool boundary, int & x, int & y) const
412 if (geometry() == ButtonOnly)
414 BOOST_ASSERT(geometry() != ButtonOnly);
416 InsetText::cursorPos(bv, sl, boundary, x, y);
417 Dimension const textdim = InsetText::dimension(bv);
419 switch (geometry()) {
421 x += dimensionCollapsed().wid;
424 y += dimensionCollapsed().des + textdim.asc;
439 Inset::EDITABLE InsetCollapsable::editable() const
441 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
445 bool InsetCollapsable::descendable() const
447 return geometry() != ButtonOnly;
451 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
453 return button_dim.contains(cmd.x, cmd.y);
457 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
460 pos_type const max_length = 15;
461 pos_type const p_siz = paragraphs().begin()->size();
462 pos_type const n = min(max_length, p_siz);
465 for (; i < n && j < p_siz; ++j) {
466 if (paragraphs().begin()->isInset(j))
468 label += paragraphs().begin()->getChar(j);
471 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
474 return label.empty() ? l : label;
478 void InsetCollapsable::edit(
479 Cursor & cur, bool front, EntryDirection entry_from)
481 //lyxerr << "InsetCollapsable: edit left/right" << endl;
483 InsetText::edit(cur, front, entry_from);
487 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
489 //lyxerr << "InsetCollapsable: edit xy" << endl;
490 if (geometry() == ButtonOnly
491 || (button_dim.contains(x, y)
492 && geometry() != NoButton))
495 return InsetText::editXY(cur, x, y);
499 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
501 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
502 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
504 switch (cmd.action) {
505 case LFUN_MOUSE_PRESS:
506 if (cmd.button() == mouse_button::button1
508 && geometry() != NoButton) {
509 // reset selection if necessary (see bug 3060)
511 cur.bv().cursor().clearSelection();
517 if (geometry() == NoButton)
518 InsetText::doDispatch(cur, cmd);
519 else if (geometry() != ButtonOnly
521 InsetText::doDispatch(cur, cmd);
526 case LFUN_MOUSE_MOTION:
527 case LFUN_MOUSE_DOUBLE:
528 case LFUN_MOUSE_TRIPLE:
529 if (geometry() == NoButton)
530 InsetText::doDispatch(cur, cmd);
531 else if (geometry() != ButtonOnly
533 InsetText::doDispatch(cur, cmd);
538 case LFUN_MOUSE_RELEASE:
539 if (cmd.button() == mouse_button::button3) {
540 // There is no button to right click:
541 if (decoration() == InsetLayout::Minimalistic ||
542 geometry() == Corners ||
543 geometry() == SubLabel ||
544 geometry() == NoButton
547 setStatus(cur, Collapsed);
549 setStatus(cur, Open);
553 // configuration dialog
554 showInsetDialog(&cur.bv());
559 if (geometry() == NoButton) {
560 // The mouse click has to be within the inset!
561 InsetText::doDispatch(cur, cmd);
565 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
566 // if we are selecting, we do not want to
570 // Left button is clicked, the user asks to
571 // toggle the inset visual state.
573 cur.updateFlags(Update::Force | Update::FitCursor);
574 if (geometry() == ButtonOnly) {
575 setStatus(cur, Open);
579 setStatus(cur, Collapsed);
581 cur.bv().cursor() = cur;
585 // The mouse click is within the opened inset.
586 if (geometry() == TopButton
587 || geometry() == LeftButton)
588 InsetText::doDispatch(cur, cmd);
591 case LFUN_INSET_TOGGLE:
592 if (cmd.argument() == "open")
593 setStatus(cur, Open);
594 else if (cmd.argument() == "close")
595 setStatus(cur, Collapsed);
596 else if (cmd.argument() == "toggle" || cmd.argument().empty())
597 if (status_ == Open) {
598 setStatus(cur, Collapsed);
599 if (geometry() == ButtonOnly)
600 cur.top().forwardPos();
602 setStatus(cur, Open);
603 else // if assign or anything else
609 case LFUN_CLIPBOARD_PASTE:
610 case LFUN_PRIMARY_SELECTION_PASTE: {
611 InsetText::doDispatch(cur, cmd);
612 // Since we can only store plain text, we must reset all
614 // FIXME: Change only the pasted paragraphs
616 resetParagraphsFont();
621 if (layout_ && layout_->isForceLtr()) {
622 // Force any new text to latex_language
623 // FIXME: This should only be necessary in constructor, but
624 // new paragraphs that are created by pressing enter at the
625 // start of an existing paragraph get the buffer language
626 // and not latex_language, so we take this brute force
628 cur.current_font.setLanguage(latex_language);
629 cur.real_current_font.setLanguage(latex_language);
631 InsetText::doDispatch(cur, cmd);
637 bool InsetCollapsable::allowMultiPar() const
639 return layout_->isMultiPar();
643 void InsetCollapsable::resetParagraphsFont()
646 font.fontInfo() = inherit_font;
647 if (layout_->isForceLtr())
648 font.setLanguage(latex_language);
649 if (layout_->isPassThru()) {
650 ParagraphList::iterator par = paragraphs().begin();
651 ParagraphList::iterator const end = paragraphs().end();
653 par->resetFonts(font);
654 par->params().clear();
661 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
662 FuncStatus & flag) const
664 switch (cmd.action) {
666 case LFUN_ACCENT_ACUTE:
667 case LFUN_ACCENT_BREVE:
668 case LFUN_ACCENT_CARON:
669 case LFUN_ACCENT_CEDILLA:
670 case LFUN_ACCENT_CIRCLE:
671 case LFUN_ACCENT_CIRCUMFLEX:
672 case LFUN_ACCENT_DOT:
673 case LFUN_ACCENT_GRAVE:
674 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
675 case LFUN_ACCENT_MACRON:
676 case LFUN_ACCENT_OGONEK:
677 case LFUN_ACCENT_SPECIAL_CARON:
678 case LFUN_ACCENT_TIE:
679 case LFUN_ACCENT_TILDE:
680 case LFUN_ACCENT_UMLAUT:
681 case LFUN_ACCENT_UNDERBAR:
682 case LFUN_ACCENT_UNDERDOT:
684 case LFUN_BIBITEM_INSERT:
685 case LFUN_BOX_INSERT:
686 case LFUN_BRANCH_INSERT:
688 case LFUN_CAPTION_INSERT:
689 case LFUN_CLEARPAGE_INSERT:
690 case LFUN_CLEARDOUBLEPAGE_INSERT:
691 case LFUN_DEPTH_DECREMENT:
692 case LFUN_DEPTH_INCREMENT:
693 case LFUN_ENVIRONMENT_INSERT:
694 case LFUN_ERT_INSERT:
695 case LFUN_FILE_INSERT:
696 case LFUN_FLEX_INSERT:
697 case LFUN_FLOAT_INSERT:
698 case LFUN_FLOAT_LIST:
699 case LFUN_FLOAT_WIDE_INSERT:
701 case LFUN_FONT_TYPEWRITER:
702 case LFUN_FONT_DEFAULT:
704 case LFUN_FONT_FREE_APPLY:
705 case LFUN_FONT_FREE_UPDATE:
707 case LFUN_FONT_ROMAN:
712 case LFUN_FONT_STATE:
713 case LFUN_FONT_UNDERLINE:
714 case LFUN_FOOTNOTE_INSERT:
715 case LFUN_HFILL_INSERT:
716 case LFUN_HYPERLINK_INSERT:
717 case LFUN_INDEX_INSERT:
718 case LFUN_INDEX_PRINT:
719 case LFUN_INSET_INSERT:
720 case LFUN_LABEL_GOTO:
721 case LFUN_LABEL_INSERT:
722 case LFUN_LINE_INSERT:
723 case LFUN_NEWPAGE_INSERT:
724 case LFUN_PAGEBREAK_INSERT:
726 case LFUN_LAYOUT_PARAGRAPH:
727 case LFUN_LAYOUT_TABULAR:
728 case LFUN_MARGINALNOTE_INSERT:
729 case LFUN_MATH_DISPLAY:
730 case LFUN_MATH_INSERT:
731 case LFUN_MATH_MATRIX:
735 case LFUN_NOMENCL_INSERT:
736 case LFUN_NOMENCL_PRINT:
737 case LFUN_NOTE_INSERT:
739 case LFUN_OPTIONAL_INSERT:
740 case LFUN_PARAGRAPH_PARAMS:
741 case LFUN_PARAGRAPH_PARAMS_APPLY:
742 case LFUN_PARAGRAPH_SPACING:
743 case LFUN_PARAGRAPH_UPDATE:
744 case LFUN_REFERENCE_NEXT:
745 case LFUN_SERVER_GOTO_FILE_ROW:
746 case LFUN_SERVER_NOTIFY:
747 case LFUN_SERVER_SET_XY:
748 case LFUN_SPACE_INSERT:
749 case LFUN_SPECIALCHAR_INSERT:
750 case LFUN_TABULAR_INSERT:
751 case LFUN_TOC_INSERT:
752 case LFUN_WRAP_INSERT:
753 if (layout_->isPassThru()) {
757 return InsetText::getStatus(cur, cmd, flag);
759 case LFUN_INSET_TOGGLE:
760 if (cmd.argument() == "open" || cmd.argument() == "close" ||
761 cmd.argument() == "toggle")
768 flag.enabled(!layout_->isForceLtr());
769 return InsetText::getStatus(cur, cmd, flag);
771 case LFUN_BREAK_PARAGRAPH:
772 case LFUN_BREAK_PARAGRAPH_SKIP:
773 flag.enabled(layout_->isMultiPar());
777 return InsetText::getStatus(cur, cmd, flag);
782 void InsetCollapsable::setLabel(docstring const & l)
788 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
792 if (status_ == Collapsed)
793 cur.leaveInset(*this);
797 docstring InsetCollapsable::floatName(
798 string const & type, BufferParams const & bp) const
800 FloatList const & floats = bp.textClass().floats();
801 FloatList::const_iterator it = floats[type];
803 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
807 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
810 return InsetLayout::Classic;
811 InsetLayout::InsetDecoration const dec = layout_->decoration();
813 case InsetLayout::Classic:
814 case InsetLayout::Minimalistic:
815 case InsetLayout::Conglomerate:
817 case InsetLayout::Default:
820 if (lyxCode() == FLEX_CODE)
821 // FIXME: Is this really necessary?
822 return InsetLayout::Conglomerate;
823 return InsetLayout::Classic;
827 bool InsetCollapsable::isMacroScope(Buffer const &) const
829 // layout_ == 0 leads to no latex output, so ignore
830 // the macros outside
834 // see InsetCollapsable::latex(...) below. In these case
835 // an environment is opened there
836 if (!layout_->latexname().empty())
843 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
844 OutputParams const & runparams) const
846 // FIXME: What should we do layout_ is 0?
848 // 2) through an error
852 // This implements the standard way of handling the LaTeX output of
853 // a collapsable inset, either a command or an environment. Standard
854 // collapsable insets should not redefine this, non-standard ones may
856 if (!layout_->latexname().empty()) {
857 if (layout_->latextype() == "command") {
859 if (runparams.moving_arg)
861 os << '\\' << from_utf8(layout_->latexname());
862 if (!layout_->latexparam().empty())
863 os << from_utf8(layout_->latexparam());
865 } else if (layout_->latextype() == "environment") {
866 os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
867 if (!layout_->latexparam().empty())
868 os << from_utf8(layout_->latexparam());
871 OutputParams rp = runparams;
872 if (layout_->isPassThru())
874 if (layout_->isNeedProtect())
875 rp.moving_arg = true;
876 int i = InsetText::latex(buf, os, rp);
877 if (!layout_->latexname().empty()) {
878 if (layout_->latextype() == "command") {
880 } else if (layout_->latextype() == "environment") {
881 os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
889 void InsetCollapsable::validate(LaTeXFeatures & features) const
894 // Force inclusion of preamble snippet in layout file
895 features.require(to_utf8(layout_->name()));
896 InsetText::validate(features);
900 bool InsetCollapsable::undefined() const
902 docstring const & n = getLayout().name();
903 return n.empty() || n == TextClass::emptyInsetLayout().name();