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"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
46 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
48 return autoOpen_ ? Open : status_;
52 InsetCollapsable::Geometry InsetCollapsable::geometry() const
54 switch (decoration()) {
56 if (status() == Open) {
65 return status() == Open ? NoButton : ButtonOnly ;
68 return status() == Open ? SubLabel : Corners ;
71 // dummy return value to shut down a warning,
77 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
78 CollapseStatus status, InsetLayout const * il)
79 : InsetText(bp), layout_(il), status_(status),
80 openinlined_(false), autoOpen_(false), mouse_hover_(false)
82 setAutoBreakRows(true);
84 setFrameColor(Color_collapsableframe);
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
90 button_dim(rhs.button_dim),
92 labelstring_(rhs.labelstring_),
94 openinlined_(rhs.openinlined_),
95 autoOpen_(rhs.autoOpen_),
96 // the sole purpose of this copy constructor
102 void InsetCollapsable::setLayout(BufferParams const & bp)
104 setLayout(bp.getTextClass().insetlayout(name()));
108 void InsetCollapsable::setLayout(InsetLayout const & il)
111 labelstring_ = layout_->labelstring;
117 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
129 text_.write(buf, os);
133 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
135 bool token_found = false;
138 string const token = lex.getString();
139 if (token == "status") {
141 string const tmp_token = lex.getString();
143 if (tmp_token == "collapsed") {
146 } else if (tmp_token == "open") {
150 lyxerr << "InsetCollapsable::read: Missing status!"
152 // Take countermeasures
153 lex.pushToken(token);
156 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
158 // take countermeasures
159 lex.pushToken(token);
162 InsetText::read(buf, lex);
164 setLayout(buf.params());
167 status_ = isOpen() ? Open : Collapsed;
169 // Force default font, if so requested
170 // This avoids paragraphs in buffer language that would have a
171 // foreign language after a document language change, and it ensures
172 // that all new text in ERT and similar gets the "latex" language,
173 // since new text inherits the language from the last position of the
174 // existing text. As a side effect this makes us also robust against
175 // bugs in LyX that might lead to font changes in ERT in .lyx files.
176 resetParagraphsFont();
180 Dimension InsetCollapsable::dimensionCollapsed() const
182 BOOST_ASSERT(layout_);
184 theFontMetrics(layout_->labelfont).buttonText(
185 labelstring_, dim.wid, dim.asc, dim.des);
190 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
192 BOOST_ASSERT(layout_);
194 autoOpen_ = mi.base.bv->cursor().isInside(this);
196 FontInfo tmpfont = mi.base.font;
197 mi.base.font = layout_->font;
198 mi.base.font.realize(tmpfont);
200 switch (geometry()) {
202 InsetText::metrics(mi, dim);
205 InsetText::metrics(mi, dim);
210 InsetText::metrics(mi, dim);
211 // consider width of the inset label
212 FontInfo font(layout_->labelfont);
213 font.realize(sane_font);
219 theFontMetrics(font).rectText(labelstring_, w, a, d);
226 dim = dimensionCollapsed();
227 if (geometry() == TopButton
228 || geometry() == LeftButton) {
230 InsetText::metrics(mi, textdim);
231 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
233 // Correct for button width.
234 dim.wid += textdim.wid;
235 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
236 dim.asc = textdim.asc;
238 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
239 dim.wid = max(dim.wid, textdim.wid);
245 mi.base.font = tmpfont;
249 bool InsetCollapsable::setMouseHover(bool mouse_hover)
251 mouse_hover_ = mouse_hover;
256 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
258 BOOST_ASSERT(layout_);
260 autoOpen_ = pi.base.bv->cursor().isInside(this);
261 ColorCode const old_color = pi.background_color;
262 pi.background_color = backgroundColor();
264 FontInfo tmpfont = pi.base.font;
265 pi.base.font = layout_->font;
266 pi.base.font.realize(tmpfont);
268 // Draw button first -- top, left or only
269 Dimension dimc = dimensionCollapsed();
271 if (geometry() == TopButton ||
272 geometry() == LeftButton ||
273 geometry() == ButtonOnly) {
274 button_dim.x1 = x + 0;
275 button_dim.x2 = x + dimc.width();
276 button_dim.y1 = y - dimc.asc;
277 button_dim.y2 = y + dimc.des;
279 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
288 Dimension const textdim = InsetText::dimension(*pi.base.bv);
289 int const baseline = y;
291 switch (geometry()) {
293 textx = x + dimc.width();
295 InsetText::draw(pi, textx, texty);
299 texty = baseline + dimc.des + textdim.asc;
300 InsetText::draw(pi, textx, texty);
307 InsetText::draw(pi, textx, texty);
313 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
314 InsetText::draw(pi, textx, texty);
315 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
317 int desc = textdim.descent();
318 if (geometry() == Corners)
321 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
322 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
323 pi.pain.line(xx1, y + desc - 4,
325 layout_->labelfont.color());
327 pi.pain.line(xx1, y + desc,
329 layout_->labelfont.color());
331 // Make status_ value visible:
332 pi.pain.line(xx1, y + desc,
334 layout_->labelfont.color());
335 pi.pain.line(xx2 - 4, y + desc,
337 layout_->labelfont.color());
339 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
340 layout_->labelfont.color());
342 // the label below the text. Can be toggled.
343 if (geometry() == SubLabel) {
344 FontInfo font(layout_->labelfont);
345 font.realize(sane_font);
351 theFontMetrics(font).rectText(labelstring_, w, a, d);
352 int const ww = max(textdim.wid, w);
353 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
354 labelstring_, font, Color_none, Color_none);
358 // a visual cue when the cursor is inside the inset
359 Cursor & cur = pi.base.bv->cursor();
360 if (cur.isInside(this)) {
363 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
364 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
365 pi.pain.line(xx2, y + 4, xx2, y,
366 layout_->labelfont.color());
367 pi.pain.line(xx2 - 4, y, xx2, y,
368 layout_->labelfont.color());
372 pi.background_color = old_color;
374 pi.base.font = tmpfont;
378 void InsetCollapsable::cursorPos(BufferView const & bv,
379 CursorSlice const & sl, bool boundary, int & x, int & y) const
381 if (geometry() == ButtonOnly)
383 BOOST_ASSERT(geometry() != ButtonOnly);
385 InsetText::cursorPos(bv, sl, boundary, x, y);
386 Dimension const textdim = InsetText::dimension(bv);
388 switch (geometry()) {
390 x += dimensionCollapsed().wid;
393 y += dimensionCollapsed().des + textdim.asc;
408 Inset::EDITABLE InsetCollapsable::editable() const
410 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
414 bool InsetCollapsable::descendable() const
416 return geometry() != ButtonOnly;
420 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
422 return button_dim.contains(cmd.x, cmd.y);
426 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
429 pos_type const max_length = 15;
430 pos_type const p_siz = paragraphs().begin()->size();
431 pos_type const n = std::min(max_length, p_siz);
434 for (; i < n && j < p_siz; ++j) {
435 if (paragraphs().begin()->isInset(j))
437 label += paragraphs().begin()->getChar(j);
440 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
443 return label.empty() ? l : label;
447 void InsetCollapsable::edit(Cursor & cur, bool left)
449 //lyxerr << "InsetCollapsable: edit left/right" << endl;
451 InsetText::edit(cur, left);
455 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
457 //lyxerr << "InsetCollapsable: edit xy" << endl;
458 if (geometry() == ButtonOnly
459 || (button_dim.contains(x, y)
460 && geometry() != NoButton))
463 return InsetText::editXY(cur, x, y);
467 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
469 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
470 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
472 switch (cmd.action) {
473 case LFUN_MOUSE_PRESS:
474 if (cmd.button() == mouse_button::button1
476 && geometry() != NoButton) {
477 // reset selection if necessary (see bug 3060)
479 cur.bv().cursor().clearSelection();
485 if (geometry() == NoButton)
486 InsetText::doDispatch(cur, cmd);
487 else if (geometry() != ButtonOnly
489 InsetText::doDispatch(cur, cmd);
494 case LFUN_MOUSE_MOTION:
495 case LFUN_MOUSE_DOUBLE:
496 case LFUN_MOUSE_TRIPLE:
497 if (geometry() == NoButton)
498 InsetText::doDispatch(cur, cmd);
499 else if (geometry() != ButtonOnly
501 InsetText::doDispatch(cur, cmd);
506 case LFUN_MOUSE_RELEASE:
507 if (cmd.button() == mouse_button::button3) {
508 // There is no button to right click:
509 if (geometry() == Corners ||
510 geometry() == SubLabel ||
511 geometry() == NoButton) {
513 setStatus(cur, Collapsed);
515 setStatus(cur, Open);
519 // configuration dialog
520 showInsetDialog(&cur.bv());
525 if (geometry() == NoButton) {
526 // The mouse click has to be within the inset!
527 InsetText::doDispatch(cur, cmd);
531 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
532 // if we are selecting, we do not want to
536 // Left button is clicked, the user asks to
537 // toggle the inset visual state.
539 cur.updateFlags(Update::Force | Update::FitCursor);
540 if (geometry() == ButtonOnly) {
541 setStatus(cur, Open);
545 setStatus(cur, Collapsed);
547 cur.bv().cursor() = cur;
551 // The mouse click is within the opened inset.
552 if (geometry() == TopButton
553 || geometry() == LeftButton)
554 InsetText::doDispatch(cur, cmd);
557 case LFUN_INSET_TOGGLE:
558 if (cmd.argument() == "open")
559 setStatus(cur, Open);
560 else if (cmd.argument() == "close")
561 setStatus(cur, Collapsed);
562 else if (cmd.argument() == "toggle" || cmd.argument().empty())
563 if (status_ == Open) {
564 setStatus(cur, Collapsed);
565 if (geometry() == ButtonOnly)
566 cur.top().forwardPos();
568 setStatus(cur, Open);
569 else // if assign or anything else
575 case LFUN_CLIPBOARD_PASTE:
576 case LFUN_PRIMARY_SELECTION_PASTE: {
577 InsetText::doDispatch(cur, cmd);
578 // Since we can only store plain text, we must reset all
580 // FIXME: Change only the pasted paragraphs
582 resetParagraphsFont();
587 if (layout_ && layout_->forceltr) {
588 // Force any new text to latex_language
589 // FIXME: This should only be necessary in constructor, but
590 // new paragraphs that are created by pressing enter at the
591 // start of an existing paragraph get the buffer language
592 // and not latex_language, so we take this brute force
594 cur.current_font.setLanguage(latex_language);
595 cur.real_current_font.setLanguage(latex_language);
597 InsetText::doDispatch(cur, cmd);
603 bool InsetCollapsable::allowMultiPar() const
605 return layout_->multipar;
609 void InsetCollapsable::resetParagraphsFont()
612 font.fontInfo() = sane_font;
613 if (layout_->forceltr)
614 font.setLanguage(latex_language);
615 if (layout_->passthru) {
616 ParagraphList::iterator par = paragraphs().begin();
617 ParagraphList::iterator const end = paragraphs().end();
619 par->resetFonts(font);
620 par->params().clear();
627 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
628 FuncStatus & flag) const
630 switch (cmd.action) {
632 case LFUN_ACCENT_ACUTE:
633 case LFUN_ACCENT_BREVE:
634 case LFUN_ACCENT_CARON:
635 case LFUN_ACCENT_CEDILLA:
636 case LFUN_ACCENT_CIRCLE:
637 case LFUN_ACCENT_CIRCUMFLEX:
638 case LFUN_ACCENT_DOT:
639 case LFUN_ACCENT_GRAVE:
640 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
641 case LFUN_ACCENT_MACRON:
642 case LFUN_ACCENT_OGONEK:
643 case LFUN_ACCENT_SPECIAL_CARON:
644 case LFUN_ACCENT_TIE:
645 case LFUN_ACCENT_TILDE:
646 case LFUN_ACCENT_UMLAUT:
647 case LFUN_ACCENT_UNDERBAR:
648 case LFUN_ACCENT_UNDERDOT:
650 case LFUN_BIBITEM_INSERT:
651 case LFUN_BOX_INSERT:
652 case LFUN_BRANCH_INSERT:
653 case LFUN_BREAK_LINE:
654 case LFUN_CAPTION_INSERT:
655 case LFUN_CLEARPAGE_INSERT:
656 case LFUN_CLEARDOUBLEPAGE_INSERT:
657 case LFUN_DEPTH_DECREMENT:
658 case LFUN_DEPTH_INCREMENT:
659 case LFUN_DOTS_INSERT:
660 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
661 case LFUN_ENVIRONMENT_INSERT:
662 case LFUN_ERT_INSERT:
663 case LFUN_FILE_INSERT:
664 case LFUN_FLEX_INSERT:
665 case LFUN_FLOAT_INSERT:
666 case LFUN_FLOAT_LIST:
667 case LFUN_FLOAT_WIDE_INSERT:
669 case LFUN_FONT_TYPEWRITER:
670 case LFUN_FONT_DEFAULT:
672 case LFUN_FONT_FREE_APPLY:
673 case LFUN_FONT_FREE_UPDATE:
675 case LFUN_FONT_ROMAN:
680 case LFUN_FONT_STATE:
681 case LFUN_FONT_UNDERLINE:
682 case LFUN_FOOTNOTE_INSERT:
683 case LFUN_HFILL_INSERT:
684 case LFUN_HYPERLINK_INSERT:
685 case LFUN_HYPHENATION_POINT_INSERT:
686 case LFUN_INDEX_INSERT:
687 case LFUN_INDEX_PRINT:
688 case LFUN_INSET_INSERT:
689 case LFUN_LABEL_GOTO:
690 case LFUN_LABEL_INSERT:
691 case LFUN_LIGATURE_BREAK_INSERT:
692 case LFUN_LINE_INSERT:
693 case LFUN_PAGEBREAK_INSERT:
695 case LFUN_LAYOUT_PARAGRAPH:
696 case LFUN_LAYOUT_TABULAR:
697 case LFUN_MARGINALNOTE_INSERT:
698 case LFUN_MATH_DISPLAY:
699 case LFUN_MATH_INSERT:
700 case LFUN_MATH_MATRIX:
703 case LFUN_MENU_SEPARATOR_INSERT:
705 case LFUN_NOMENCL_INSERT:
706 case LFUN_NOMENCL_PRINT:
707 case LFUN_NOTE_INSERT:
709 case LFUN_OPTIONAL_INSERT:
710 case LFUN_PARAGRAPH_PARAMS:
711 case LFUN_PARAGRAPH_PARAMS_APPLY:
712 case LFUN_PARAGRAPH_SPACING:
713 case LFUN_PARAGRAPH_UPDATE:
714 case LFUN_REFERENCE_NEXT:
715 case LFUN_SERVER_GOTO_FILE_ROW:
716 case LFUN_SERVER_NOTIFY:
717 case LFUN_SERVER_SET_XY:
718 case LFUN_SPACE_INSERT:
719 case LFUN_TABULAR_INSERT:
720 case LFUN_TOC_INSERT:
721 case LFUN_WRAP_INSERT:
722 if (layout_->passthru) {
726 return InsetText::getStatus(cur, cmd, flag);
728 case LFUN_INSET_TOGGLE:
729 if (cmd.argument() == "open" || cmd.argument() == "close" ||
730 cmd.argument() == "toggle")
737 flag.enabled(!layout_->forceltr);
738 return InsetText::getStatus(cur, cmd, flag);
740 case LFUN_BREAK_PARAGRAPH:
741 case LFUN_BREAK_PARAGRAPH_SKIP:
742 flag.enabled(layout_->multipar);
746 return InsetText::getStatus(cur, cmd, flag);
751 void InsetCollapsable::setLabel(docstring const & l)
757 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
761 if (status_ == Collapsed)
762 cur.leaveInset(*this);
766 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
768 FloatList const & floats = bp.getTextClass().floats();
769 FloatList::const_iterator it = floats[type];
771 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
775 InsetCollapsable::Decoration InsetCollapsable::decoration() const
777 if (!layout_ || layout_->decoration == "classic")
779 if (layout_->decoration == "minimalistic")
781 if (layout_->decoration == "conglomerate")
783 if (lyxCode() == FLEX_CODE)
784 // FIXME: Is this really necessary?
790 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
791 OutputParams const & runparams) const
793 // FIXME: What should we do layout_ is 0?
795 // 2) through an error
799 // This implements the standard way of handling the LaTeX output of
800 // a collapsable inset, either a command or an environment. Standard
801 // collapsable insets should not redefine this, non-standard ones may
803 if (!layout_->latexname.empty()) {
804 if (layout_->latextype == "command") {
806 if (runparams.moving_arg)
808 os << '\\' << from_utf8(layout_->latexname);
809 if (!layout_->latexparam.empty())
810 os << from_utf8(layout_->latexparam);
812 } else if (layout_->latextype == "environment") {
813 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
814 if (!layout_->latexparam.empty())
815 os << from_utf8(layout_->latexparam);
818 OutputParams rp = runparams;
819 if (layout_->passthru)
821 if (layout_->needprotect)
822 rp.moving_arg = true;
823 int i = InsetText::latex(buf, os, rp);
824 if (!layout_->latexname.empty()) {
825 if (layout_->latextype == "command") {
827 } else if (layout_->latextype == "environment") {
828 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
836 void InsetCollapsable::validate(LaTeXFeatures & features) const
841 // Force inclusion of preamble snippet in layout file
842 features.require(layout_->name);
843 InsetText::validate(features);