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 "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.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
77 (BufferParams const & bp, CollapseStatus status)
78 : InsetText(bp), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
81 setAutoBreakRows(true);
83 setFrameColor(Color_collapsableframe);
87 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
89 button_dim(rhs.button_dim),
91 topbaseline(rhs.topbaseline),
94 openinlined_(rhs.openinlined_),
95 autoOpen_(rhs.autoOpen_),
96 // the sole purpose of this copy constructor
102 void InsetCollapsable::setLayout(BufferParams const & bp)
104 // FIXME: put this in the InsetLayout parsing?
105 // Fallback for lacking inset layout background
106 layout_.bgcolor = Color_background;
108 layout_ = getLayout(bp);
110 // FIXME: put this in the InsetLayout parsing?
111 layout_.labelfont.realize(sane_font);
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);
165 status_ = isOpen() ? Open : Collapsed;
167 setLayout(buf.params());
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
183 theFontMetrics(layout_.labelfont).buttonText(
184 layout_.labelstring, dim.wid, dim.asc, dim.des);
189 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
191 autoOpen_ = mi.base.bv->cursor().isInside(this);
193 FontInfo tmpfont = mi.base.font;
194 mi.base.font = layout_.font;
195 mi.base.font.realize(tmpfont);
197 switch (geometry()) {
199 InsetText::metrics(mi, dim);
202 InsetText::metrics(mi, dim);
207 InsetText::metrics(mi, dim);
208 // consider width of the inset label
209 FontInfo font(layout_.labelfont);
210 font.realize(sane_font);
216 docstring s = layout_.labelstring;
217 theFontMetrics(font).rectText(s, 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 autoOpen_ = pi.base.bv->cursor().isInside(this);
257 ColorCode const old_color = pi.background_color;
258 pi.background_color = backgroundColor();
260 FontInfo tmpfont = pi.base.font;
261 pi.base.font = layout_.font;
262 pi.base.font.realize(tmpfont);
264 // Draw button first -- top, left or only
265 Dimension dimc = dimensionCollapsed();
267 if (geometry() == TopButton ||
268 geometry() == LeftButton ||
269 geometry() == ButtonOnly) {
270 button_dim.x1 = x + 0;
271 button_dim.x2 = x + dimc.width();
272 button_dim.y1 = y - dimc.asc;
273 button_dim.y2 = y + dimc.des;
275 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
283 Dimension const textdim = InsetText::dimension(*pi.base.bv);
284 int const baseline = y;
286 switch (geometry()) {
288 textx = x + dimc.width();
290 InsetText::draw(pi, textx, texty);
294 texty = baseline + dimc.des + textdim.asc;
295 InsetText::draw(pi, textx, texty);
302 InsetText::draw(pi, textx, texty);
308 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
309 InsetText::draw(pi, textx, texty);
310 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
312 int desc = textdim.descent();
313 if (geometry() == Corners)
316 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
317 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
318 pi.pain.line(xx1, y + desc - 4,
320 layout_.labelfont.color());
321 if (internalStatus() == Open)
322 pi.pain.line(xx1, y + desc,
324 layout_.labelfont.color());
326 // Make status_ value visible:
327 pi.pain.line(xx1, y + desc,
329 layout_.labelfont.color());
330 pi.pain.line(xx2 - 4, y + desc,
332 layout_.labelfont.color());
334 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
335 layout_.labelfont.color());
337 // the label below the text. Can be toggled.
338 if (geometry() == SubLabel) {
339 FontInfo font(layout_.labelfont);
340 font.realize(sane_font);
346 docstring s = layout_.labelstring;
347 theFontMetrics(font).rectText(s, w, a, d);
348 int const ww = max(textdim.wid, w);
349 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
350 s, font, Color_none, Color_none);
354 // a visual cue when the cursor is inside the inset
355 Cursor & cur = pi.base.bv->cursor();
356 if (cur.isInside(this)) {
359 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
360 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
361 pi.pain.line(xx2, y + 4, xx2, y,
362 layout_.labelfont.color());
363 pi.pain.line(xx2 - 4, y, xx2, y,
364 layout_.labelfont.color());
368 pi.background_color = old_color;
370 pi.base.font = tmpfont;
374 void InsetCollapsable::cursorPos(BufferView const & bv,
375 CursorSlice const & sl, bool boundary, int & x, int & y) const
377 if (geometry() == ButtonOnly)
379 BOOST_ASSERT(geometry() != ButtonOnly);
381 InsetText::cursorPos(bv, sl, boundary, x, y);
382 Dimension const textdim = InsetText::dimension(bv);
384 switch (geometry()) {
386 x += dimensionCollapsed().wid;
389 y += dimensionCollapsed().des + textdim.asc;
404 Inset::EDITABLE InsetCollapsable::editable() const
406 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
410 bool InsetCollapsable::descendable() const
412 return geometry() != ButtonOnly;
416 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
418 return button_dim.contains(cmd.x, cmd.y);
422 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
425 pos_type const max_length = 15;
426 pos_type const p_siz = paragraphs().begin()->size();
427 pos_type const n = std::min(max_length, p_siz);
430 for (; i < n && j < p_siz; ++j) {
431 if (paragraphs().begin()->isInset(j))
433 label += paragraphs().begin()->getChar(j);
436 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
439 return label.empty() ? l : label;
443 void InsetCollapsable::edit(Cursor & cur, bool left)
445 //lyxerr << "InsetCollapsable: edit left/right" << endl;
447 InsetText::edit(cur, left);
451 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
453 //lyxerr << "InsetCollapsable: edit xy" << endl;
454 if (geometry() == ButtonOnly
455 || (button_dim.contains(x, y)
456 && geometry() != NoButton))
459 return InsetText::editXY(cur, x, y);
463 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
465 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
466 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
468 switch (cmd.action) {
469 case LFUN_MOUSE_PRESS:
470 if (cmd.button() == mouse_button::button1
472 && geometry() != NoButton) {
473 // reset selection if necessary (see bug 3060)
475 cur.bv().cursor().clearSelection();
481 if (geometry() == NoButton)
482 InsetText::doDispatch(cur, cmd);
483 else if (geometry() != ButtonOnly
485 InsetText::doDispatch(cur, cmd);
490 case LFUN_MOUSE_MOTION:
491 case LFUN_MOUSE_DOUBLE:
492 case LFUN_MOUSE_TRIPLE:
493 if (geometry() == NoButton)
494 InsetText::doDispatch(cur, cmd);
495 else if (geometry() != ButtonOnly
497 InsetText::doDispatch(cur, cmd);
502 case LFUN_MOUSE_RELEASE:
503 if (cmd.button() == mouse_button::button3) {
504 // There is no button to right click:
505 if (geometry() == Corners ||
506 geometry() == SubLabel ||
507 geometry() == NoButton) {
508 if (internalStatus() == Open)
509 setStatus(cur, Collapsed);
511 setStatus(cur, Open);
515 // configuration dialog
516 showInsetDialog(&cur.bv());
521 if (geometry() == NoButton) {
522 // The mouse click has to be within the inset!
523 InsetText::doDispatch(cur, cmd);
527 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
528 // if we are selecting, we do not want to
532 // Left button is clicked, the user asks to
533 // toggle the inset visual state.
535 cur.updateFlags(Update::Force | Update::FitCursor);
536 if (geometry() == ButtonOnly) {
537 setStatus(cur, Open);
541 setStatus(cur, Collapsed);
543 cur.bv().cursor() = cur;
547 // The mouse click is within the opened inset.
548 if (geometry() == TopButton
549 || geometry() == LeftButton)
550 InsetText::doDispatch(cur, cmd);
553 case LFUN_INSET_TOGGLE:
554 if (cmd.argument() == "open")
555 setStatus(cur, Open);
556 else if (cmd.argument() == "close")
557 setStatus(cur, Collapsed);
558 else if (cmd.argument() == "toggle" || cmd.argument().empty())
559 if (internalStatus() == Open) {
560 setStatus(cur, Collapsed);
561 if (geometry() == ButtonOnly)
562 cur.top().forwardPos();
564 setStatus(cur, Open);
565 else // if assign or anything else
571 case LFUN_CLIPBOARD_PASTE:
572 case LFUN_PRIMARY_SELECTION_PASTE: {
573 InsetText::doDispatch(cur, cmd);
574 // Since we can only store plain text, we must reset all
576 // FIXME: Change only the pasted paragraphs
578 resetParagraphsFont();
583 if (layout_.forceltr) {
584 // Force any new text to latex_language
585 // FIXME: This should only be necessary in constructor, but
586 // new paragraphs that are created by pressing enter at the
587 // start of an existing paragraph get the buffer language
588 // and not latex_language, so we take this brute force
590 cur.current_font.setLanguage(latex_language);
591 cur.real_current_font.setLanguage(latex_language);
593 InsetText::doDispatch(cur, cmd);
599 bool InsetCollapsable::allowMultiPar() const
601 return layout_.multipar;
605 void InsetCollapsable::resetParagraphsFont()
608 font.fontInfo() = sane_font;
609 if (layout_.forceltr)
610 font.setLanguage(latex_language);
611 if (layout_.passthru) {
612 ParagraphList::iterator par = paragraphs().begin();
613 ParagraphList::iterator const end = paragraphs().end();
615 par->resetFonts(font);
616 par->params().clear();
623 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
624 FuncStatus & flag) const
626 switch (cmd.action) {
628 case LFUN_ACCENT_ACUTE:
629 case LFUN_ACCENT_BREVE:
630 case LFUN_ACCENT_CARON:
631 case LFUN_ACCENT_CEDILLA:
632 case LFUN_ACCENT_CIRCLE:
633 case LFUN_ACCENT_CIRCUMFLEX:
634 case LFUN_ACCENT_DOT:
635 case LFUN_ACCENT_GRAVE:
636 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
637 case LFUN_ACCENT_MACRON:
638 case LFUN_ACCENT_OGONEK:
639 case LFUN_ACCENT_SPECIAL_CARON:
640 case LFUN_ACCENT_TIE:
641 case LFUN_ACCENT_TILDE:
642 case LFUN_ACCENT_UMLAUT:
643 case LFUN_ACCENT_UNDERBAR:
644 case LFUN_ACCENT_UNDERDOT:
646 case LFUN_BIBITEM_INSERT:
647 case LFUN_BOX_INSERT:
648 case LFUN_BRANCH_INSERT:
649 case LFUN_BREAK_LINE:
650 case LFUN_CAPTION_INSERT:
651 case LFUN_CLEARPAGE_INSERT:
652 case LFUN_CLEARDOUBLEPAGE_INSERT:
653 case LFUN_DEPTH_DECREMENT:
654 case LFUN_DEPTH_INCREMENT:
655 case LFUN_DOTS_INSERT:
656 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
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_HYPHENATION_POINT_INSERT:
682 case LFUN_INDEX_INSERT:
683 case LFUN_INDEX_PRINT:
684 case LFUN_INSET_INSERT:
685 case LFUN_LABEL_GOTO:
686 case LFUN_LABEL_INSERT:
687 case LFUN_LIGATURE_BREAK_INSERT:
688 case LFUN_LINE_INSERT:
689 case LFUN_PAGEBREAK_INSERT:
691 case LFUN_LAYOUT_PARAGRAPH:
692 case LFUN_LAYOUT_TABULAR:
693 case LFUN_MARGINALNOTE_INSERT:
694 case LFUN_MATH_DISPLAY:
695 case LFUN_MATH_INSERT:
696 case LFUN_MATH_MATRIX:
699 case LFUN_MENU_SEPARATOR_INSERT:
701 case LFUN_NOMENCL_INSERT:
702 case LFUN_NOMENCL_PRINT:
703 case LFUN_NOTE_INSERT:
705 case LFUN_OPTIONAL_INSERT:
706 case LFUN_PARAGRAPH_PARAMS:
707 case LFUN_PARAGRAPH_PARAMS_APPLY:
708 case LFUN_PARAGRAPH_SPACING:
709 case LFUN_PARAGRAPH_UPDATE:
710 case LFUN_REFERENCE_NEXT:
711 case LFUN_SERVER_GOTO_FILE_ROW:
712 case LFUN_SERVER_NOTIFY:
713 case LFUN_SERVER_SET_XY:
714 case LFUN_SPACE_INSERT:
715 case LFUN_TABULAR_INSERT:
716 case LFUN_TOC_INSERT:
717 case LFUN_WRAP_INSERT:
718 if (layout_.passthru) {
722 return InsetText::getStatus(cur, cmd, flag);
724 case LFUN_INSET_TOGGLE:
725 if (cmd.argument() == "open" || cmd.argument() == "close" ||
726 cmd.argument() == "toggle")
733 flag.enabled(!layout_.forceltr);
734 return InsetText::getStatus(cur, cmd, flag);
736 case LFUN_BREAK_PARAGRAPH:
737 case LFUN_BREAK_PARAGRAPH_SKIP:
738 flag.enabled(layout_.multipar);
742 return InsetText::getStatus(cur, cmd, flag);
747 void InsetCollapsable::setLabel(docstring const & l)
749 layout_.labelstring = l;
753 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
757 if (status_ == Collapsed)
758 cur.leaveInset(*this);
762 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
764 FloatList const & floats = bp.getTextClass().floats();
765 FloatList::const_iterator it = floats[type];
767 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
771 InsetCollapsable::Decoration InsetCollapsable::decoration() const
773 if (layout_.decoration == "classic")
775 if (layout_.decoration == "minimalistic")
777 if (layout_.decoration == "conglomerate")
779 if (name() == from_ascii("Flex"))
785 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
786 OutputParams const & runparams) const
788 // This implements the standard way of handling the LaTeX output of
789 // a collapsable inset, either a command or an environment. Standard
790 // collapsable insets should not redefine this, non-standard ones may
792 if (!layout_.latexname.empty()) {
793 if (layout_.latextype == "command") {
795 if (runparams.moving_arg)
797 os << '\\' << from_utf8(layout_.latexname);
798 if (!layout_.latexparam.empty())
799 os << from_utf8(layout_.latexparam);
801 } else if (layout_.latextype == "environment") {
802 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
803 if (!layout_.latexparam.empty())
804 os << from_utf8(layout_.latexparam);
807 OutputParams rp = runparams;
808 if (layout_.passthru)
810 if (layout_.needprotect)
811 rp.moving_arg = true;
812 int i = InsetText::latex(buf, os, rp);
813 if (!layout_.latexname.empty()) {
814 if (layout_.latextype == "command") {
816 } else if (layout_.latextype == "environment") {
817 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
825 void InsetCollapsable::validate(LaTeXFeatures & features) const
827 // Force inclusion of preamble snippet in layout file
828 features.require(layout_.name);
829 InsetText::validate(features);