2 * \file InsetCollapsible.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 "InsetCollapsible.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
22 #include "Dimension.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "MetricsInfo.h"
28 #include "TextClass.h"
29 #include "TocBackend.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/FileName.h"
37 #include "support/gettext.h"
38 #include "support/lassert.h"
39 #include "support/Lexer.h"
40 #include "support/lstrings.h"
41 #include "support/Changer.h"
42 #include "support/TempFile.h"
51 InsetCollapsible::InsetCollapsible(Buffer * buf, InsetText::UsePlain ltype)
52 : InsetText(buf, ltype), status_(Open)
55 setFrameColor(Color_collapsibleframe);
59 // The sole purpose of this copy constructor is to make sure
60 // that the view_ map is not copied and remains empty.
61 InsetCollapsible::InsetCollapsible(InsetCollapsible const & rhs)
64 labelstring_(rhs.labelstring_)
70 InsetCollapsible & InsetCollapsible::operator=(InsetCollapsible const & that)
74 *this = InsetCollapsible(that);
79 InsetCollapsible::~InsetCollapsible()
81 map<BufferView const *, View>::iterator it = view_.begin();
82 map<BufferView const *, View>::iterator end = view_.end();
83 for (; it != end; ++it)
84 if (it->second.mouse_hover_)
85 it->first->clearLastInset(this);
89 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
91 if (decoration() == InsetDecoration::CONGLOMERATE)
93 return view_[&bv].auto_open_ ? Open : status_;
97 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
99 switch (decoration()) {
100 case InsetDecoration::CLASSIC:
101 if (status(bv) == Open)
102 return view_[&bv].openinlined_ ? LeftButton : TopButton;
105 case InsetDecoration::MINIMALISTIC: {
106 return status(bv) == Open ?
107 (tempfile_ ? LeftButton : NoButton)
111 case InsetDecoration::CONGLOMERATE:
112 return status(bv) == Open ? SubLabel : Corners ;
114 case InsetDecoration::DEFAULT:
115 break; // this shouldn't happen
118 // dummy return value to shut down a warning,
119 // this is dead code.
124 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
126 Dimension const dim = dimensionCollapsed(bv);
127 if (geometry(bv) == NoButton)
128 return translateIfPossible(getLayout().labelstring());
129 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
132 return toolTipText();
136 void InsetCollapsible::write(ostream & os) const
152 void InsetCollapsible::read(Lexer & lex)
154 lex.setContext("InsetCollapsible::read");
157 lex >> "status" >> tmp_token;
158 if (tmp_token == "open")
161 InsetText::read(lex);
165 int InsetCollapsible::topOffset(BufferView const * bv) const
167 switch (geometry(*bv)) {
172 return InsetText::topOffset(bv);
176 int InsetCollapsible::bottomOffset(BufferView const * bv) const
178 switch (geometry(*bv)) {
181 return InsetText::bottomOffset(bv) / 4;
183 return InsetText::bottomOffset(bv);
188 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
191 FontInfo labelfont(getLabelfont());
192 labelfont.realize(sane_font);
193 int const offset = Inset::textOffset(&bv);
194 theFontMetrics(labelfont).buttonText(
195 buttonLabel(bv), offset, dim.wid, dim.asc, dim.des);
196 // remove spacing on the right for left buttons; we also do it for
197 // TopButton (although it is not useful per se), because
198 // openinlined_ is not always set properly at this point.
199 Geometry const geom = geometry(bv);
200 if (geom == LeftButton || geom == TopButton)
201 // this form makes a difference if offset is even
202 dim.wid -= offset - offset / 2;
207 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
209 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
211 BufferView const & bv = *mi.base.bv;
213 switch (geometry(bv)) {
215 InsetText::metrics(mi, dim);
218 InsetText::metrics(mi, dim);
221 InsetText::metrics(mi, dim);
222 // consider width of the inset label
223 FontInfo font(getLabelfont());
224 font.realize(sane_font);
230 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
232 dim.wid = max(dim.wid, w);
238 if (hasFixedWidth()){
239 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
240 if (mi.base.textwidth < mindim)
241 mi.base.textwidth = mindim;
243 dim = dimensionCollapsed(bv);
244 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
246 InsetText::metrics(mi, textdim);
247 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
248 if (view_[&bv].openinlined_) {
249 // Correct for button width but remove spacing before frame
250 dim.wid += textdim.wid - leftOffset(mi.base.bv) / 2;
251 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
252 dim.asc = textdim.asc;
254 dim.des += textdim.height() + topOffset(mi.base.bv);
255 dim.wid = max(dim.wid, textdim.wid);
263 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
266 view_[bv].mouse_hover_ = mouse_hover;
271 ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
273 return getLayout().bgcolor();
277 ColorCode InsetCollapsible::labelColor() const
279 return getLayout().labelfont().color();
283 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
285 BufferView const & bv = *pi.base.bv;
287 view_[&bv].auto_open_ = bv.cursor().isInside(this);
289 Changer dummy = pi.base.font.change(getFont(), true);
291 // Draw button first -- top, left or only
292 Dimension dimc = dimensionCollapsed(bv);
294 if (geometry(bv) == TopButton ||
295 geometry(bv) == LeftButton ||
296 geometry(bv) == ButtonOnly) {
297 view_[&bv].button_dim_.x1 = x + 0;
298 view_[&bv].button_dim_.x2 = x + dimc.width();
299 view_[&bv].button_dim_.y1 = y - dimc.asc;
300 view_[&bv].button_dim_.y2 = y + dimc.des;
302 FontInfo labelfont = getLabelfont();
303 labelfont.setColor(labelColor());
304 labelfont.realize(pi.base.font);
305 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
306 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
307 Color_buttonframe, Inset::textOffset(pi.base.bv));
308 // Draw the change tracking cue on the label, unless RowPainter already
310 if (canPaintChange(bv))
311 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
313 view_[&bv].button_dim_.x1 = 0;
314 view_[&bv].button_dim_.y1 = 0;
315 view_[&bv].button_dim_.x2 = 0;
316 view_[&bv].button_dim_.y2 = 0;
319 Dimension const textdim = dimensionHelper(bv);
320 int const baseline = y;
322 Geometry g = geometry(bv);
326 if (g == LeftButton) {
327 // correct for spacing added before the frame in
328 // InsetText::draw. We want the button to touch the frame.
329 textx = x + dimc.width() - leftOffset(pi.base.bv) / 2;
333 texty = baseline + dimc.des + textdim.asc;
335 // Do not draw the cue for INSERTED -- it is already in the button and
337 Changer cdummy = (pi.change.type == Change::INSERTED)
338 ? changeVar(pi.change, Change())
340 InsetText::draw(pi, textx, texty);
348 InsetText::draw(pi, textx, texty);
354 // We will take care of the frame and the change tracking cue
357 Changer cdummy = changeVar(pi.change, Change());
358 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
359 InsetText::draw(pi, textx, texty);
360 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
363 int desc = textdim.descent();
365 // Colour the frame according to the change type. (Like for tables.)
366 Color colour = pi.change.changed() ? pi.change.color()
368 const int xx1 = x + leftOffset(pi.base.bv) - 1;
369 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
370 pi.pain.line(xx1, y + desc - 4,
371 xx1, y + desc, colour);
373 pi.pain.line(xx1, y + desc,
374 xx2, y + desc, colour);
376 // Make status_ value visible:
377 pi.pain.line(xx1, y + desc,
378 xx1 + 4, y + desc, colour);
379 pi.pain.line(xx2 - 4, y + desc,
380 xx2, y + desc, colour);
382 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
383 y + desc - 4, colour);
385 // the label below the text. Can be toggled.
387 FontInfo font(getLabelfont());
388 if (pi.change.changed())
389 font.setPaintColor(colour);
390 font.realize(sane_font);
396 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
397 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
398 int const ww = max(textdim.wid, w);
399 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
400 buttonLabel(bv), font, col, Color_none);
403 int const y1 = y - textdim.asc + 3;
404 // a visual cue when the cursor is inside the inset
405 Cursor const & cur = bv.cursor();
406 if (cur.isInside(this)) {
407 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
408 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
409 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
410 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
412 // Strike through the inset if deleted and not already handled by
414 if (pi.change.deleted() && canPaintChange(bv))
415 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
421 void InsetCollapsible::cursorPos(BufferView const & bv,
422 CursorSlice const & sl, bool boundary, int & x, int & y) const
424 if (geometry(bv) == ButtonOnly)
427 InsetText::cursorPos(bv, sl, boundary, x, y);
428 Dimension const textdim = dimensionHelper(bv);
430 switch (geometry(bv)) {
432 x += dimensionCollapsed(bv).wid - leftOffset(&bv) / 2;
435 y += dimensionCollapsed(bv).des + textdim.asc;
450 bool InsetCollapsible::editable() const
455 switch (decoration()) {
456 case InsetDecoration::CLASSIC:
457 case InsetDecoration::MINIMALISTIC:
458 return status_ == Open;
465 bool InsetCollapsible::descendable(BufferView const & bv) const
470 return geometry(bv) != ButtonOnly;
474 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
476 return view_[&bv].button_dim_.contains(x, y);
480 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
482 odocstringstream label;
483 pos_type const max_length = 15;
484 pos_type const p_siz = paragraphs().begin()->size();
485 pos_type const n = min(max_length, p_siz);
488 for (; i < n && j < p_siz; ++j) {
489 if (paragraphs().begin()->isDeleted(j))
491 if (paragraphs().begin()->isInset(j)) {
492 if (!paragraphs().begin()->getInset(j)->isChar())
494 paragraphs().begin()->getInset(j)->toString(label);
496 label.put(paragraphs().begin()->getChar(j));
499 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
502 return label.str().empty() ? l : label.str();
506 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
508 //lyxerr << "InsetCollapsible: edit left/right" << endl;
509 // We might have a selection if we moved the mouse on the button only
510 cur.clearSelection();
512 InsetText::edit(cur, front, entry_from);
516 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
518 //lyxerr << "InsetCollapsible: edit xy" << endl;
519 if (geometry(cur.bv()) == ButtonOnly
520 || !descendable(cur.bv())
521 || (view_[&cur.bv()].button_dim_.contains(x, y)
522 && geometry(cur.bv()) != NoButton))
525 return InsetText::editXY(cur, x, y);
529 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
531 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
532 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
534 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
536 switch (cmd.action()) {
537 case LFUN_MOUSE_PRESS:
539 switch (cmd.button()) {
540 case mouse_button::button1:
541 case mouse_button::button3:
542 // Pass the command to the enclosing InsetText,
543 // so that the cursor gets set.
546 case mouse_button::none:
547 case mouse_button::button2:
548 case mouse_button::button4:
549 case mouse_button::button5:
551 cur.noScreenUpdate();
554 } else if (geometry(cur.bv()) != ButtonOnly)
555 InsetText::doDispatch(cur, cmd);
560 case LFUN_MOUSE_DOUBLE:
561 case LFUN_MOUSE_TRIPLE:
563 cur.noScreenUpdate();
564 else if (geometry(cur.bv()) != ButtonOnly)
565 InsetText::doDispatch(cur, cmd);
570 case LFUN_MOUSE_RELEASE:
572 // The mouse click has to be within the inset!
573 if (geometry(cur.bv()) != ButtonOnly)
574 InsetText::doDispatch(cur, cmd);
579 if (cmd.button() != mouse_button::button1) {
581 cur.noScreenUpdate();
584 // If we are selecting, we do not want to toggle the inset
585 // except if the selection started at the inset button we're still on.
586 // The latter addresses #12820.
587 if (cur.selection() && !clickable(cur.bv(), cur.bv().cursor().xClickPos(),
588 cur.bv().cursor().yClickPos()))
590 // Left button is clicked, the user asks to
591 // toggle the inset visual state.
593 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
594 if (geometry(cur.bv()) == ButtonOnly) {
595 setStatus(cur, Open);
599 setStatus(cur, Collapsed);
600 cur.bv().cursor() = cur;
603 case LFUN_INSET_TOGGLE:
604 if (cmd.argument() == "open")
605 setStatus(cur, Open);
606 else if (cmd.argument() == "close")
607 setStatus(cur, Collapsed);
608 else if (cmd.argument() == "toggle" || cmd.argument().empty())
610 setStatus(cur, Collapsed);
612 setStatus(cur, Open);
613 else // if assign or anything else
618 case LFUN_INSET_EDIT: {
620 text().selectAll(cur);
621 string const format =
622 cur.buffer()->params().documentClass().outputFormat();
623 string const ext = theFormats().extension(format);
624 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
625 support::FileName const tempfilename = tempfile_->name();
626 string const name = tempfilename.toFilesystemEncoding();
627 ofdocstream os(name.c_str());
628 os << cur.selectionAsString(false);
630 // Since we lock the inset while the external file is edited,
631 // we need to move the cursor outside and clear any selection inside
632 cur.clearSelection();
634 cur.leaveInset(*this);
636 if (cmd.argument() == "nogui")
637 cur.message(from_utf8(name));
639 theFormats().edit(buffer(), tempfilename, format);
643 case LFUN_INSET_END_EDIT: {
644 support::FileName const tempfilename = tempfile_->name();
645 docstring const s = tempfilename.fileContents("UTF-8");
646 cur.recordUndoInset(this);
648 text().selectAll(cur);
649 cap::replaceSelection(cur);
650 cur.text()->insertStringAsLines(cur, s, cur.current_font);
651 // FIXME (gb) it crashes without this
659 InsetText::doDispatch(cur, cmd);
665 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
666 FuncStatus & flag) const
668 switch (cmd.action()) {
669 case LFUN_INSET_TOGGLE:
670 if (cmd.argument() == "open")
671 flag.setEnabled(status_ != Open);
672 else if (cmd.argument() == "close")
673 flag.setEnabled(status_ == Open);
674 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
675 flag.setEnabled(true);
676 flag.setOnOff(status_ == Open);
678 flag.setEnabled(false);
681 case LFUN_INSET_EDIT:
682 flag.setEnabled(!buffer().hasReadonlyFlag() &&
683 getLayout().editExternally() && tempfile_ == nullptr);
686 case LFUN_INSET_END_EDIT:
687 flag.setEnabled(!buffer().hasReadonlyFlag() &&
688 getLayout().editExternally() && tempfile_ != nullptr);
692 return InsetText::getStatus(cur, cmd, flag);
697 void InsetCollapsible::setLabel(docstring const & l)
703 docstring InsetCollapsible::getLabel() const
705 InsetLayout const & il = getLayout();
706 return labelstring_.empty() ?
707 translateIfPossible(il.labelstring()) : labelstring_;
711 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
714 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
715 // indicate changed content in label (#8645)
716 // ✎ U+270E LOWER RIGHT PENCIL
717 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
718 ? docstring(1, 0x270E) : docstring();
719 InsetLayout const & il = getLayout();
720 docstring const label = getLabel();
721 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
722 return locked + indicator + label;
723 return locked + indicator + getNewLabel(label);
727 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
731 if (status_ == Collapsed) {
732 cur.leaveInset(*this);
733 // if cursor was inside the inset, it was now moved outside (#12830)
734 cur.setCurrentFont();
739 InsetDecoration InsetCollapsible::decoration() const
741 InsetDecoration const dec = getLayout().decoration();
742 return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
746 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
749 string context_menu = contextMenuName();
750 string const it_context_menu = InsetText::contextMenuName();
751 if (decoration() == InsetDecoration::CONGLOMERATE)
752 return context_menu + ";" + it_context_menu;
754 string const ic_context_menu = InsetCollapsible::contextMenuName();
755 if (ic_context_menu != context_menu)
756 context_menu += ";" + ic_context_menu;
758 if (geometry(bv) == NoButton)
759 return context_menu + ";" + it_context_menu;
761 Dimension dim = dimensionCollapsed(bv);
762 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
765 return context_menu + ";" + it_context_menu;
769 string InsetCollapsible::contextMenuName() const
771 if (decoration() == InsetDecoration::CONGLOMERATE)
772 return "context-conglomerate";
774 return "context-collapsible";
778 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
780 // return false to let RowPainter draw the change tracking cue consistently
781 // with the surrounding text, when the inset is inline: for buttons, for
782 // non-allowMultiPar insets.
783 switch (geometry(bv)) {
786 return allowMultiPar();
796 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
797 UpdateType utype, TocBackend & backend) const
799 bool doing_output = output_active && producesOutput();
800 InsetLayout const & layout = getLayout();
801 if (!layout.addToToc())
802 return InsetText::addToToc(cpit, doing_output, utype, backend);
804 TocBuilder & b = backend.builder(layout.tocType());
805 // Cursor inside the inset
806 DocIterator pit = cpit;
807 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
808 docstring const label = getLabel();
809 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
810 // Proceed with the rest of the inset.
811 InsetText::addToToc(cpit, doing_output, utype, backend);
812 if (layout.isTocCaption()) {
814 text().forOutliner(str, TOC_ENTRY_LENGTH);