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"
28 #include "MetricsInfo.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lstrings.h"
41 #include "support/Changer.h"
42 #include "support/TempFile.h"
49 InsetCollapsible::InsetCollapsible(Buffer * buf, InsetText::UsePlain ltype)
50 : InsetText(buf, ltype), status_(Open)
53 setFrameColor(Color_collapsibleframe);
57 // The sole purpose of this copy constructor is to make sure
58 // that the view_ map is not copied and remains empty.
59 InsetCollapsible::InsetCollapsible(InsetCollapsible const & rhs)
62 labelstring_(rhs.labelstring_)
68 InsetCollapsible & InsetCollapsible::operator=(InsetCollapsible const & that)
72 *this = InsetCollapsible(that);
77 InsetCollapsible::~InsetCollapsible()
79 map<BufferView const *, View>::iterator it = view_.begin();
80 map<BufferView const *, View>::iterator end = view_.end();
81 for (; it != end; ++it)
82 if (it->second.mouse_hover_)
83 it->first->clearLastInset(this);
87 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
89 if (decoration() == InsetLayout::CONGLOMERATE)
91 return view_[&bv].auto_open_ ? Open : status_;
95 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
97 switch (decoration()) {
98 case InsetLayout::CLASSIC:
99 if (status(bv) == Open)
100 return view_[&bv].openinlined_ ? LeftButton : TopButton;
103 case InsetLayout::MINIMALISTIC: {
104 return status(bv) == Open ?
105 (tempfile_ ? LeftButton : NoButton)
109 case InsetLayout::CONGLOMERATE:
110 return status(bv) == Open ? SubLabel : Corners ;
112 case InsetLayout::DEFAULT:
113 break; // this shouldn't happen
116 // dummy return value to shut down a warning,
117 // this is dead code.
122 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
124 Dimension const dim = dimensionCollapsed(bv);
125 if (geometry(bv) == NoButton)
126 return translateIfPossible(getLayout().labelstring());
127 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
130 return toolTipText();
134 void InsetCollapsible::write(ostream & os) const
150 void InsetCollapsible::read(Lexer & lex)
152 lex.setContext("InsetCollapsible::read");
155 lex >> "status" >> tmp_token;
156 if (tmp_token == "open")
159 InsetText::read(lex);
163 int InsetCollapsible::topOffset(BufferView const * bv) const
165 switch (geometry(*bv)) {
170 return InsetText::topOffset(bv);
174 int InsetCollapsible::bottomOffset(BufferView const * bv) const
176 switch (geometry(*bv)) {
179 return InsetText::bottomOffset(bv) / 4;
181 return InsetText::bottomOffset(bv);
186 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
189 FontInfo labelfont(getLabelfont());
190 labelfont.realize(sane_font);
191 theFontMetrics(labelfont).buttonText(
192 buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
197 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
199 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
201 FontInfo tmpfont = mi.base.font;
202 mi.base.font = getFont();
203 mi.base.font.realize(tmpfont);
205 BufferView const & bv = *mi.base.bv;
207 switch (geometry(bv)) {
209 InsetText::metrics(mi, dim);
212 InsetText::metrics(mi, dim);
215 InsetText::metrics(mi, dim);
216 // consider width of the inset label
217 FontInfo font(getLabelfont());
218 font.realize(sane_font);
224 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
231 if (hasFixedWidth()){
232 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
233 if (mi.base.textwidth < mindim)
234 mi.base.textwidth = mindim;
236 dim = dimensionCollapsed(bv);
237 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
239 InsetText::metrics(mi, textdim);
240 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
241 if (view_[&bv].openinlined_) {
242 // Correct for button width.
243 dim.wid += textdim.wid;
244 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
245 dim.asc = textdim.asc;
247 dim.des += textdim.height() + topOffset(mi.base.bv);
248 dim.wid = max(dim.wid, textdim.wid);
254 mi.base.font = tmpfont;
258 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
261 view_[bv].mouse_hover_ = mouse_hover;
266 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
268 BufferView const & bv = *pi.base.bv;
270 view_[&bv].auto_open_ = bv.cursor().isInside(this);
272 Changer dummy = pi.base.font.change(getFont(), true);
274 // Draw button first -- top, left or only
275 Dimension dimc = dimensionCollapsed(bv);
277 if (geometry(bv) == TopButton ||
278 geometry(bv) == LeftButton ||
279 geometry(bv) == ButtonOnly) {
280 view_[&bv].button_dim_.x1 = x + 0;
281 view_[&bv].button_dim_.x2 = x + dimc.width();
282 view_[&bv].button_dim_.y1 = y - dimc.asc;
283 view_[&bv].button_dim_.y2 = y + dimc.des;
285 FontInfo labelfont = getLabelfont();
286 labelfont.setColor(labelColor());
287 labelfont.realize(pi.base.font);
288 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
289 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
290 Color_buttonframe, Inset::textOffset(pi.base.bv));
291 // Draw the change tracking cue on the label, unless RowPainter already
293 if (canPaintChange(bv))
294 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
296 view_[&bv].button_dim_.x1 = 0;
297 view_[&bv].button_dim_.y1 = 0;
298 view_[&bv].button_dim_.x2 = 0;
299 view_[&bv].button_dim_.y2 = 0;
302 Dimension const textdim = dimensionHelper(bv);
303 int const baseline = y;
305 Geometry g = geometry(bv);
309 if (g == LeftButton) {
310 textx = x + dimc.width();
314 texty = baseline + dimc.des + textdim.asc;
316 // Do not draw the cue for INSERTED -- it is already in the button and
318 Changer cdummy = (pi.change.type == Change::INSERTED)
319 ? changeVar(pi.change, Change())
321 InsetText::draw(pi, textx, texty);
329 InsetText::draw(pi, textx, texty);
335 // We will take care of the frame and the change tracking cue
338 Changer cdummy = changeVar(pi.change, Change());
339 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
340 InsetText::draw(pi, textx, texty);
341 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
344 int desc = textdim.descent();
346 // Colour the frame according to the change type. (Like for tables.)
347 Color colour = pi.change.changed() ? pi.change.color()
349 const int xx1 = x + leftOffset(pi.base.bv) - 1;
350 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
351 pi.pain.line(xx1, y + desc - 4,
352 xx1, y + desc, colour);
354 pi.pain.line(xx1, y + desc,
355 xx2, y + desc, colour);
357 // Make status_ value visible:
358 pi.pain.line(xx1, y + desc,
359 xx1 + 4, y + desc, colour);
360 pi.pain.line(xx2 - 4, y + desc,
361 xx2, y + desc, colour);
363 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
364 y + desc - 4, colour);
366 // the label below the text. Can be toggled.
368 FontInfo font(getLabelfont());
369 if (pi.change.changed())
370 font.setPaintColor(colour);
371 font.realize(sane_font);
377 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
378 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
379 int const ww = max(textdim.wid, w);
380 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
381 buttonLabel(bv), font, col, Color_none);
384 int const y1 = y - textdim.asc + 3;
385 // a visual cue when the cursor is inside the inset
386 Cursor const & cur = bv.cursor();
387 if (cur.isInside(this)) {
388 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
389 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
390 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
391 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
393 // Strike through the inset if deleted and not already handled by
395 if (pi.change.deleted() && canPaintChange(bv))
396 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
402 void InsetCollapsible::cursorPos(BufferView const & bv,
403 CursorSlice const & sl, bool boundary, int & x, int & y) const
405 if (geometry(bv) == ButtonOnly)
408 InsetText::cursorPos(bv, sl, boundary, x, y);
409 Dimension const textdim = dimensionHelper(bv);
411 switch (geometry(bv)) {
413 x += dimensionCollapsed(bv).wid;
416 y += dimensionCollapsed(bv).des + textdim.asc;
431 bool InsetCollapsible::editable() const
436 switch (decoration()) {
437 case InsetLayout::CLASSIC:
438 case InsetLayout::MINIMALISTIC:
439 return status_ == Open;
446 bool InsetCollapsible::descendable(BufferView const & bv) const
451 return geometry(bv) != ButtonOnly;
455 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
457 return view_[&bv].button_dim_.contains(x, y);
461 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
463 odocstringstream label;
464 pos_type const max_length = 15;
465 pos_type const p_siz = paragraphs().begin()->size();
466 pos_type const n = min(max_length, p_siz);
469 for (; i < n && j < p_siz; ++j) {
470 if (paragraphs().begin()->isDeleted(j))
472 if (paragraphs().begin()->isInset(j)) {
473 if (!paragraphs().begin()->getInset(j)->isChar())
475 paragraphs().begin()->getInset(j)->toString(label);
477 label.put(paragraphs().begin()->getChar(j));
480 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
483 return label.str().empty() ? l : label.str();
487 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
489 //lyxerr << "InsetCollapsible: edit left/right" << endl;
491 InsetText::edit(cur, front, entry_from);
495 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
497 //lyxerr << "InsetCollapsible: edit xy" << endl;
498 if (geometry(cur.bv()) == ButtonOnly
499 || !descendable(cur.bv())
500 || (view_[&cur.bv()].button_dim_.contains(x, y)
501 && geometry(cur.bv()) != NoButton))
504 return InsetText::editXY(cur, x, y);
508 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
510 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
511 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
513 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
515 switch (cmd.action()) {
516 case LFUN_MOUSE_PRESS:
518 switch (cmd.button()) {
519 case mouse_button::button1:
520 case mouse_button::button3:
521 // Pass the command to the enclosing InsetText,
522 // so that the cursor gets set.
525 case mouse_button::none:
526 case mouse_button::button2:
527 case mouse_button::button4:
528 case mouse_button::button5:
530 cur.noScreenUpdate();
533 } else if (geometry(cur.bv()) != ButtonOnly)
534 InsetText::doDispatch(cur, cmd);
539 case LFUN_MOUSE_DOUBLE:
540 case LFUN_MOUSE_TRIPLE:
542 cur.noScreenUpdate();
543 else if (geometry(cur.bv()) != ButtonOnly)
544 InsetText::doDispatch(cur, cmd);
549 case LFUN_MOUSE_RELEASE:
551 // The mouse click has to be within the inset!
552 if (geometry(cur.bv()) != ButtonOnly)
553 InsetText::doDispatch(cur, cmd);
558 if (cmd.button() != mouse_button::button1) {
560 cur.noScreenUpdate();
563 // if we are selecting, we do not want to
567 // Left button is clicked, the user asks to
568 // toggle the inset visual state.
570 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
571 if (geometry(cur.bv()) == ButtonOnly) {
572 setStatus(cur, Open);
576 setStatus(cur, Collapsed);
577 cur.bv().cursor() = cur;
580 case LFUN_INSET_TOGGLE:
581 if (cmd.argument() == "open")
582 setStatus(cur, Open);
583 else if (cmd.argument() == "close")
584 setStatus(cur, Collapsed);
585 else if (cmd.argument() == "toggle" || cmd.argument().empty())
587 setStatus(cur, Collapsed);
589 setStatus(cur, Open);
590 else // if assign or anything else
595 case LFUN_INSET_EDIT: {
597 text().selectAll(cur);
598 string const format =
599 cur.buffer()->params().documentClass().outputFormat();
600 string const ext = theFormats().extension(format);
601 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
602 support::FileName const tempfilename = tempfile_->name();
603 string const name = tempfilename.toFilesystemEncoding();
604 ofdocstream os(name.c_str());
605 os << cur.selectionAsString(false);
607 // Since we lock the inset while the external file is edited,
608 // we need to move the cursor outside and clear any selection inside
609 cur.clearSelection();
611 cur.leaveInset(*this);
612 theFormats().edit(buffer(), tempfilename, format);
615 case LFUN_INSET_END_EDIT: {
616 support::FileName const tempfilename = tempfile_->name();
617 docstring const s = tempfilename.fileContents("UTF-8");
618 cur.recordUndoInset(this);
620 text().selectAll(cur);
621 cap::replaceSelection(cur);
622 cur.text()->insertStringAsLines(cur, s, cur.current_font);
623 // FIXME (gb) it crashes without this
631 InsetText::doDispatch(cur, cmd);
637 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
638 FuncStatus & flag) const
640 switch (cmd.action()) {
641 case LFUN_INSET_TOGGLE:
642 if (cmd.argument() == "open")
643 flag.setEnabled(status_ != Open);
644 else if (cmd.argument() == "close")
645 flag.setEnabled(status_ == Open);
646 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
647 flag.setEnabled(true);
648 flag.setOnOff(status_ == Open);
650 flag.setEnabled(false);
653 case LFUN_INSET_EDIT:
654 flag.setEnabled(!buffer().hasReadonlyFlag() &&
655 getLayout().editExternally() && tempfile_ == nullptr);
658 case LFUN_INSET_END_EDIT:
659 flag.setEnabled(!buffer().hasReadonlyFlag() &&
660 getLayout().editExternally() && tempfile_ != nullptr);
664 return InsetText::getStatus(cur, cmd, flag);
669 void InsetCollapsible::setLabel(docstring const & l)
675 docstring InsetCollapsible::getLabel() const
677 InsetLayout const & il = getLayout();
678 return labelstring_.empty() ?
679 translateIfPossible(il.labelstring()) : labelstring_;
683 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
686 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
687 if (decoration() == InsetLayout::MINIMALISTIC)
689 // indicate changed content in label (#8645)
690 // ✎ U+270E LOWER RIGHT PENCIL
691 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
692 ? docstring(1, 0x270E) : docstring();
693 InsetLayout const & il = getLayout();
694 docstring const label = getLabel();
695 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
696 return indicator + label;
697 return locked + indicator + getNewLabel(label);
701 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
705 if (status_ == Collapsed)
706 cur.leaveInset(*this);
710 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
712 InsetLayout::InsetDecoration const dec = getLayout().decoration();
713 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
717 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
720 string context_menu = contextMenuName();
721 string const it_context_menu = InsetText::contextMenuName();
722 if (decoration() == InsetLayout::CONGLOMERATE)
723 return context_menu + ";" + it_context_menu;
725 string const ic_context_menu = InsetCollapsible::contextMenuName();
726 if (ic_context_menu != context_menu)
727 context_menu += ";" + ic_context_menu;
729 if (geometry(bv) == NoButton)
730 return context_menu + ";" + it_context_menu;
732 Dimension dim = dimensionCollapsed(bv);
733 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
736 return it_context_menu;
740 string InsetCollapsible::contextMenuName() const
742 if (decoration() == InsetLayout::CONGLOMERATE)
743 return "context-conglomerate";
745 return "context-collapsible";
749 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
751 // return false to let RowPainter draw the change tracking cue consistently
752 // with the surrounding text, when the inset is inline: for buttons, for
753 // non-allowMultiPar insets.
754 switch (geometry(bv)) {
757 return allowMultiPar();
767 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
768 UpdateType utype, TocBackend & backend) const
770 bool doing_output = output_active && producesOutput();
771 InsetLayout const & layout = getLayout();
772 if (!layout.addToToc())
773 return InsetText::addToToc(cpit, doing_output, utype, backend);
775 TocBuilder & b = backend.builder(layout.tocType());
776 // Cursor inside the inset
777 DocIterator pit = cpit;
778 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
779 docstring const label = getLabel();
780 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
781 // Proceed with the rest of the inset.
782 InsetText::addToToc(cpit, doing_output, utype, backend);
783 if (layout.isTocCaption()) {
785 text().forOutliner(str, TOC_ENTRY_LENGTH);