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 "Dimension.h"
23 #include "DispatchResult.h"
24 #include "FloatList.h"
25 #include "FuncStatus.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"
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
49 return autoOpen_ ? Open : status_;
53 InsetCollapsable::Geometry InsetCollapsable::geometry() const
55 switch (decoration()) {
57 if (status() == Open) {
66 return status() == Open ? NoButton : ButtonOnly ;
69 return status() == Open ? SubLabel : Corners ;
72 // dummy return value to shut down a warning,
78 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
79 CollapseStatus status, InsetLayout const * il)
80 : InsetText(bp), layout_(il), status_(status),
81 openinlined_(false), autoOpen_(false), mouse_hover_(false)
83 setAutoBreakRows(true);
85 setFrameColor(Color_collapsableframe);
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91 button_dim(rhs.button_dim),
93 labelstring_(rhs.labelstring_),
95 openinlined_(rhs.openinlined_),
96 autoOpen_(rhs.autoOpen_),
97 // the sole purpose of this copy constructor
103 void InsetCollapsable::setLayout(BufferParams const & bp)
105 setLayout(bp.getTextClass().insetlayout(name()));
109 void InsetCollapsable::setLayout(InsetLayout const & il)
112 labelstring_ = layout_->labelstring;
118 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
130 text_.write(buf, os);
134 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
136 bool token_found = false;
139 string const token = lex.getString();
140 if (token == "status") {
142 string const tmp_token = lex.getString();
144 if (tmp_token == "collapsed") {
147 } else if (tmp_token == "open") {
151 lyxerr << "InsetCollapsable::read: Missing status!"
153 // Take countermeasures
154 lex.pushToken(token);
157 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
159 // take countermeasures
160 lex.pushToken(token);
163 InsetText::read(buf, lex);
165 setLayout(buf.params());
168 status_ = isOpen() ? Open : Collapsed;
170 // Force default font, if so requested
171 // This avoids paragraphs in buffer language that would have a
172 // foreign language after a document language change, and it ensures
173 // that all new text in ERT and similar gets the "latex" language,
174 // since new text inherits the language from the last position of the
175 // existing text. As a side effect this makes us also robust against
176 // bugs in LyX that might lead to font changes in ERT in .lyx files.
177 resetParagraphsFont();
181 Dimension InsetCollapsable::dimensionCollapsed() const
183 BOOST_ASSERT(layout_);
185 theFontMetrics(layout_->labelfont).buttonText(
186 labelstring_, dim.wid, dim.asc, dim.des);
191 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
193 BOOST_ASSERT(layout_);
195 autoOpen_ = mi.base.bv->cursor().isInside(this);
197 FontInfo tmpfont = mi.base.font;
198 mi.base.font = layout_->font;
199 mi.base.font.realize(tmpfont);
201 switch (geometry()) {
203 InsetText::metrics(mi, dim);
206 InsetText::metrics(mi, dim);
211 InsetText::metrics(mi, dim);
212 // consider width of the inset label
213 FontInfo font(layout_->labelfont);
214 font.realize(sane_font);
220 theFontMetrics(font).rectText(labelstring_, w, a, d);
227 dim = dimensionCollapsed();
228 if (geometry() == TopButton
229 || geometry() == LeftButton) {
231 InsetText::metrics(mi, textdim);
232 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
234 // Correct for button width.
235 dim.wid += textdim.wid;
236 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
237 dim.asc = textdim.asc;
239 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
240 dim.wid = max(dim.wid, textdim.wid);
246 mi.base.font = tmpfont;
250 bool InsetCollapsable::setMouseHover(bool mouse_hover)
252 mouse_hover_ = mouse_hover;
257 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
259 BOOST_ASSERT(layout_);
261 autoOpen_ = pi.base.bv->cursor().isInside(this);
262 ColorCode const old_color = pi.background_color;
263 pi.background_color = backgroundColor();
265 FontInfo tmpfont = pi.base.font;
266 pi.base.font = layout_->font;
267 pi.base.font.realize(tmpfont);
269 // Draw button first -- top, left or only
270 Dimension dimc = dimensionCollapsed();
272 if (geometry() == TopButton ||
273 geometry() == LeftButton ||
274 geometry() == ButtonOnly) {
275 button_dim.x1 = x + 0;
276 button_dim.x2 = x + dimc.width();
277 button_dim.y1 = y - dimc.asc;
278 button_dim.y2 = y + dimc.des;
280 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
289 Dimension const textdim = InsetText::dimension(*pi.base.bv);
290 int const baseline = y;
292 switch (geometry()) {
294 textx = x + dimc.width();
296 InsetText::draw(pi, textx, texty);
300 texty = baseline + dimc.des + textdim.asc;
301 InsetText::draw(pi, textx, texty);
308 InsetText::draw(pi, textx, texty);
314 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
315 InsetText::draw(pi, textx, texty);
316 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
318 int desc = textdim.descent();
319 if (geometry() == Corners)
322 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
323 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
324 pi.pain.line(xx1, y + desc - 4,
326 layout_->labelfont.color());
328 pi.pain.line(xx1, y + desc,
330 layout_->labelfont.color());
332 // Make status_ value visible:
333 pi.pain.line(xx1, y + desc,
335 layout_->labelfont.color());
336 pi.pain.line(xx2 - 4, y + desc,
338 layout_->labelfont.color());
340 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
341 layout_->labelfont.color());
343 // the label below the text. Can be toggled.
344 if (geometry() == SubLabel) {
345 FontInfo font(layout_->labelfont);
346 font.realize(sane_font);
352 theFontMetrics(font).rectText(labelstring_, w, a, d);
353 int const ww = max(textdim.wid, w);
354 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
355 labelstring_, font, Color_none, Color_none);
359 // a visual cue when the cursor is inside the inset
360 Cursor & cur = pi.base.bv->cursor();
361 if (cur.isInside(this)) {
364 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
365 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
366 pi.pain.line(xx2, y + 4, xx2, y,
367 layout_->labelfont.color());
368 pi.pain.line(xx2 - 4, y, xx2, y,
369 layout_->labelfont.color());
373 pi.background_color = old_color;
375 pi.base.font = tmpfont;
379 void InsetCollapsable::cursorPos(BufferView const & bv,
380 CursorSlice const & sl, bool boundary, int & x, int & y) const
382 if (geometry() == ButtonOnly)
384 BOOST_ASSERT(geometry() != ButtonOnly);
386 InsetText::cursorPos(bv, sl, boundary, x, y);
387 Dimension const textdim = InsetText::dimension(bv);
389 switch (geometry()) {
391 x += dimensionCollapsed().wid;
394 y += dimensionCollapsed().des + textdim.asc;
409 Inset::EDITABLE InsetCollapsable::editable() const
411 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
415 bool InsetCollapsable::descendable() const
417 return geometry() != ButtonOnly;
421 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
423 return button_dim.contains(cmd.x, cmd.y);
427 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
430 pos_type const max_length = 15;
431 pos_type const p_siz = paragraphs().begin()->size();
432 pos_type const n = std::min(max_length, p_siz);
435 for (; i < n && j < p_siz; ++j) {
436 if (paragraphs().begin()->isInset(j))
438 label += paragraphs().begin()->getChar(j);
441 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
444 return label.empty() ? l : label;
448 void InsetCollapsable::edit(Cursor & cur, bool left)
450 //lyxerr << "InsetCollapsable: edit left/right" << endl;
452 InsetText::edit(cur, left);
456 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
458 //lyxerr << "InsetCollapsable: edit xy" << endl;
459 if (geometry() == ButtonOnly
460 || (button_dim.contains(x, y)
461 && geometry() != NoButton))
464 return InsetText::editXY(cur, x, y);
468 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
470 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
471 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
473 switch (cmd.action) {
474 case LFUN_MOUSE_PRESS:
475 if (cmd.button() == mouse_button::button1
477 && geometry() != NoButton) {
478 // reset selection if necessary (see bug 3060)
480 cur.bv().cursor().clearSelection();
486 if (geometry() == NoButton)
487 InsetText::doDispatch(cur, cmd);
488 else if (geometry() != ButtonOnly
490 InsetText::doDispatch(cur, cmd);
495 case LFUN_MOUSE_MOTION:
496 case LFUN_MOUSE_DOUBLE:
497 case LFUN_MOUSE_TRIPLE:
498 if (geometry() == NoButton)
499 InsetText::doDispatch(cur, cmd);
500 else if (geometry() != ButtonOnly
502 InsetText::doDispatch(cur, cmd);
507 case LFUN_MOUSE_RELEASE:
508 if (cmd.button() == mouse_button::button3) {
509 // There is no button to right click:
510 if (geometry() == Corners ||
511 geometry() == SubLabel ||
512 geometry() == NoButton) {
514 setStatus(cur, Collapsed);
516 setStatus(cur, Open);
520 // configuration dialog
521 showInsetDialog(&cur.bv());
526 if (geometry() == NoButton) {
527 // The mouse click has to be within the inset!
528 InsetText::doDispatch(cur, cmd);
532 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
533 // if we are selecting, we do not want to
537 // Left button is clicked, the user asks to
538 // toggle the inset visual state.
540 cur.updateFlags(Update::Force | Update::FitCursor);
541 if (geometry() == ButtonOnly) {
542 setStatus(cur, Open);
546 setStatus(cur, Collapsed);
548 cur.bv().cursor() = cur;
552 // The mouse click is within the opened inset.
553 if (geometry() == TopButton
554 || geometry() == LeftButton)
555 InsetText::doDispatch(cur, cmd);
558 case LFUN_INSET_TOGGLE:
559 if (cmd.argument() == "open")
560 setStatus(cur, Open);
561 else if (cmd.argument() == "close")
562 setStatus(cur, Collapsed);
563 else if (cmd.argument() == "toggle" || cmd.argument().empty())
564 if (status_ == Open) {
565 setStatus(cur, Collapsed);
566 if (geometry() == ButtonOnly)
567 cur.top().forwardPos();
569 setStatus(cur, Open);
570 else // if assign or anything else
576 case LFUN_CLIPBOARD_PASTE:
577 case LFUN_PRIMARY_SELECTION_PASTE: {
578 InsetText::doDispatch(cur, cmd);
579 // Since we can only store plain text, we must reset all
581 // FIXME: Change only the pasted paragraphs
583 resetParagraphsFont();
588 if (layout_ && layout_->forceltr) {
589 // Force any new text to latex_language
590 // FIXME: This should only be necessary in constructor, but
591 // new paragraphs that are created by pressing enter at the
592 // start of an existing paragraph get the buffer language
593 // and not latex_language, so we take this brute force
595 cur.current_font.setLanguage(latex_language);
596 cur.real_current_font.setLanguage(latex_language);
598 InsetText::doDispatch(cur, cmd);
604 bool InsetCollapsable::allowMultiPar() const
606 return layout_->multipar;
610 void InsetCollapsable::resetParagraphsFont()
613 font.fontInfo() = inherit_font;
614 if (layout_->forceltr)
615 font.setLanguage(latex_language);
616 if (layout_->passthru) {
617 ParagraphList::iterator par = paragraphs().begin();
618 ParagraphList::iterator const end = paragraphs().end();
620 par->resetFonts(font);
621 par->params().clear();
628 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
629 FuncStatus & flag) const
631 switch (cmd.action) {
633 case LFUN_ACCENT_ACUTE:
634 case LFUN_ACCENT_BREVE:
635 case LFUN_ACCENT_CARON:
636 case LFUN_ACCENT_CEDILLA:
637 case LFUN_ACCENT_CIRCLE:
638 case LFUN_ACCENT_CIRCUMFLEX:
639 case LFUN_ACCENT_DOT:
640 case LFUN_ACCENT_GRAVE:
641 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
642 case LFUN_ACCENT_MACRON:
643 case LFUN_ACCENT_OGONEK:
644 case LFUN_ACCENT_SPECIAL_CARON:
645 case LFUN_ACCENT_TIE:
646 case LFUN_ACCENT_TILDE:
647 case LFUN_ACCENT_UMLAUT:
648 case LFUN_ACCENT_UNDERBAR:
649 case LFUN_ACCENT_UNDERDOT:
651 case LFUN_BIBITEM_INSERT:
652 case LFUN_BOX_INSERT:
653 case LFUN_BRANCH_INSERT:
654 case LFUN_BREAK_LINE:
655 case LFUN_CAPTION_INSERT:
656 case LFUN_CLEARPAGE_INSERT:
657 case LFUN_CLEARDOUBLEPAGE_INSERT:
658 case LFUN_DEPTH_DECREMENT:
659 case LFUN_DEPTH_INCREMENT:
660 case LFUN_DOTS_INSERT:
661 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
662 case LFUN_ENVIRONMENT_INSERT:
663 case LFUN_ERT_INSERT:
664 case LFUN_FILE_INSERT:
665 case LFUN_FLEX_INSERT:
666 case LFUN_FLOAT_INSERT:
667 case LFUN_FLOAT_LIST:
668 case LFUN_FLOAT_WIDE_INSERT:
670 case LFUN_FONT_TYPEWRITER:
671 case LFUN_FONT_DEFAULT:
673 case LFUN_FONT_FREE_APPLY:
674 case LFUN_FONT_FREE_UPDATE:
676 case LFUN_FONT_ROMAN:
681 case LFUN_FONT_STATE:
682 case LFUN_FONT_UNDERLINE:
683 case LFUN_FOOTNOTE_INSERT:
684 case LFUN_HFILL_INSERT:
685 case LFUN_HYPERLINK_INSERT:
686 case LFUN_HYPHENATION_POINT_INSERT:
687 case LFUN_INDEX_INSERT:
688 case LFUN_INDEX_PRINT:
689 case LFUN_INSET_INSERT:
690 case LFUN_LABEL_GOTO:
691 case LFUN_LABEL_INSERT:
692 case LFUN_LIGATURE_BREAK_INSERT:
693 case LFUN_LINE_INSERT:
694 case LFUN_PAGEBREAK_INSERT:
696 case LFUN_LAYOUT_PARAGRAPH:
697 case LFUN_LAYOUT_TABULAR:
698 case LFUN_MARGINALNOTE_INSERT:
699 case LFUN_MATH_DISPLAY:
700 case LFUN_MATH_INSERT:
701 case LFUN_MATH_MATRIX:
704 case LFUN_MENU_SEPARATOR_INSERT:
706 case LFUN_NOMENCL_INSERT:
707 case LFUN_NOMENCL_PRINT:
708 case LFUN_NOTE_INSERT:
710 case LFUN_OPTIONAL_INSERT:
711 case LFUN_PARAGRAPH_PARAMS:
712 case LFUN_PARAGRAPH_PARAMS_APPLY:
713 case LFUN_PARAGRAPH_SPACING:
714 case LFUN_PARAGRAPH_UPDATE:
715 case LFUN_REFERENCE_NEXT:
716 case LFUN_SERVER_GOTO_FILE_ROW:
717 case LFUN_SERVER_NOTIFY:
718 case LFUN_SERVER_SET_XY:
719 case LFUN_SPACE_INSERT:
720 case LFUN_TABULAR_INSERT:
721 case LFUN_TOC_INSERT:
722 case LFUN_WRAP_INSERT:
723 if (layout_->passthru) {
727 return InsetText::getStatus(cur, cmd, flag);
729 case LFUN_INSET_TOGGLE:
730 if (cmd.argument() == "open" || cmd.argument() == "close" ||
731 cmd.argument() == "toggle")
738 flag.enabled(!layout_->forceltr);
739 return InsetText::getStatus(cur, cmd, flag);
741 case LFUN_BREAK_PARAGRAPH:
742 case LFUN_BREAK_PARAGRAPH_SKIP:
743 flag.enabled(layout_->multipar);
747 return InsetText::getStatus(cur, cmd, flag);
752 void InsetCollapsable::setLabel(docstring const & l)
758 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
762 if (status_ == Collapsed)
763 cur.leaveInset(*this);
767 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
769 FloatList const & floats = bp.getTextClass().floats();
770 FloatList::const_iterator it = floats[type];
772 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
776 InsetCollapsable::Decoration InsetCollapsable::decoration() const
778 if (!layout_ || layout_->decoration == "classic")
780 if (layout_->decoration == "minimalistic")
782 if (layout_->decoration == "conglomerate")
784 if (lyxCode() == FLEX_CODE)
785 // FIXME: Is this really necessary?
791 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
792 OutputParams const & runparams) const
794 // FIXME: What should we do layout_ is 0?
796 // 2) through an error
800 // This implements the standard way of handling the LaTeX output of
801 // a collapsable inset, either a command or an environment. Standard
802 // collapsable insets should not redefine this, non-standard ones may
804 if (!layout_->latexname.empty()) {
805 if (layout_->latextype == "command") {
807 if (runparams.moving_arg)
809 os << '\\' << from_utf8(layout_->latexname);
810 if (!layout_->latexparam.empty())
811 os << from_utf8(layout_->latexparam);
813 } else if (layout_->latextype == "environment") {
814 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
815 if (!layout_->latexparam.empty())
816 os << from_utf8(layout_->latexparam);
819 OutputParams rp = runparams;
820 if (layout_->passthru)
822 if (layout_->needprotect)
823 rp.moving_arg = true;
824 int i = InsetText::latex(buf, os, rp);
825 if (!layout_->latexname.empty()) {
826 if (layout_->latextype == "command") {
828 } else if (layout_->latextype == "environment") {
829 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
837 void InsetCollapsable::validate(LaTeXFeatures & features) const
842 // Force inclusion of preamble snippet in layout file
843 features.require(layout_->name);
844 InsetText::validate(features);