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() == InsetDecoration::CONGLOMERATE)
91 return view_[&bv].auto_open_ ? Open : status_;
95 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
97 switch (decoration()) {
98 case InsetDecoration::CLASSIC:
99 if (status(bv) == Open)
100 return view_[&bv].openinlined_ ? LeftButton : TopButton;
103 case InsetDecoration::MINIMALISTIC: {
104 return status(bv) == Open ?
105 (tempfile_ ? LeftButton : NoButton)
109 case InsetDecoration::CONGLOMERATE:
110 return status(bv) == Open ? SubLabel : Corners ;
112 case InsetDecoration::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());
191 (geometry(bv) != LeftButton && geometry(bv) != TopButton)
192 ? Inset::textOffset(&bv) : 0;
193 labelfont.realize(sane_font);
194 theFontMetrics(labelfont).buttonText(
195 buttonLabel(bv), offset, dim.wid, dim.asc, dim.des);
200 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
202 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
204 BufferView const & bv = *mi.base.bv;
206 switch (geometry(bv)) {
208 InsetText::metrics(mi, dim);
211 InsetText::metrics(mi, dim);
214 InsetText::metrics(mi, dim);
215 // consider width of the inset label
216 FontInfo font(getLabelfont());
217 font.realize(sane_font);
223 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
225 dim.wid = max(dim.wid, w);
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);
256 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
259 view_[bv].mouse_hover_ = mouse_hover;
264 ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
266 return getLayout().bgcolor();
270 ColorCode InsetCollapsible::labelColor() const
272 return getLayout().labelfont().color();
276 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
278 BufferView const & bv = *pi.base.bv;
280 view_[&bv].auto_open_ = bv.cursor().isInside(this);
282 Changer dummy = pi.base.font.change(getFont(), true);
284 // Draw button first -- top, left or only
285 Dimension dimc = dimensionCollapsed(bv);
287 if (geometry(bv) == TopButton ||
288 geometry(bv) == LeftButton ||
289 geometry(bv) == ButtonOnly) {
290 view_[&bv].button_dim_.x1 = x + 0;
291 view_[&bv].button_dim_.x2 = x + dimc.width();
292 view_[&bv].button_dim_.y1 = y - dimc.asc;
293 view_[&bv].button_dim_.y2 = y + dimc.des;
295 FontInfo labelfont = getLabelfont();
296 labelfont.setColor(labelColor());
297 labelfont.realize(pi.base.font);
298 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
299 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
300 Color_buttonframe, Inset::textOffset(pi.base.bv));
301 // Draw the change tracking cue on the label, unless RowPainter already
303 if (canPaintChange(bv))
304 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
306 view_[&bv].button_dim_.x1 = 0;
307 view_[&bv].button_dim_.y1 = 0;
308 view_[&bv].button_dim_.x2 = 0;
309 view_[&bv].button_dim_.y2 = 0;
312 Dimension const textdim = dimensionHelper(bv);
313 int const baseline = y;
315 Geometry g = geometry(bv);
319 if (g == LeftButton) {
320 textx = x + dimc.width();
324 texty = baseline + dimc.des + textdim.asc;
326 // Do not draw the cue for INSERTED -- it is already in the button and
328 Changer cdummy = (pi.change.type == Change::INSERTED)
329 ? changeVar(pi.change, Change())
331 InsetText::draw(pi, textx, texty);
339 InsetText::draw(pi, textx, texty);
345 // We will take care of the frame and the change tracking cue
348 Changer cdummy = changeVar(pi.change, Change());
349 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
350 InsetText::draw(pi, textx, texty);
351 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
354 int desc = textdim.descent();
356 // Colour the frame according to the change type. (Like for tables.)
357 Color colour = pi.change.changed() ? pi.change.color()
359 const int xx1 = x + leftOffset(pi.base.bv) - 1;
360 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
361 pi.pain.line(xx1, y + desc - 4,
362 xx1, y + desc, colour);
364 pi.pain.line(xx1, y + desc,
365 xx2, y + desc, colour);
367 // Make status_ value visible:
368 pi.pain.line(xx1, y + desc,
369 xx1 + 4, y + desc, colour);
370 pi.pain.line(xx2 - 4, y + desc,
371 xx2, y + desc, colour);
373 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
374 y + desc - 4, colour);
376 // the label below the text. Can be toggled.
378 FontInfo font(getLabelfont());
379 if (pi.change.changed())
380 font.setPaintColor(colour);
381 font.realize(sane_font);
387 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
388 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
389 int const ww = max(textdim.wid, w);
390 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
391 buttonLabel(bv), font, col, Color_none);
394 int const y1 = y - textdim.asc + 3;
395 // a visual cue when the cursor is inside the inset
396 Cursor const & cur = bv.cursor();
397 if (cur.isInside(this)) {
398 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
399 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
400 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
401 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
403 // Strike through the inset if deleted and not already handled by
405 if (pi.change.deleted() && canPaintChange(bv))
406 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
412 void InsetCollapsible::cursorPos(BufferView const & bv,
413 CursorSlice const & sl, bool boundary, int & x, int & y) const
415 if (geometry(bv) == ButtonOnly)
418 InsetText::cursorPos(bv, sl, boundary, x, y);
419 Dimension const textdim = dimensionHelper(bv);
421 switch (geometry(bv)) {
423 x += dimensionCollapsed(bv).wid;
426 y += dimensionCollapsed(bv).des + textdim.asc;
441 bool InsetCollapsible::editable() const
446 switch (decoration()) {
447 case InsetDecoration::CLASSIC:
448 case InsetDecoration::MINIMALISTIC:
449 return status_ == Open;
456 bool InsetCollapsible::descendable(BufferView const & bv) const
461 return geometry(bv) != ButtonOnly;
465 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
467 return view_[&bv].button_dim_.contains(x, y);
471 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
473 odocstringstream label;
474 pos_type const max_length = 15;
475 pos_type const p_siz = paragraphs().begin()->size();
476 pos_type const n = min(max_length, p_siz);
479 for (; i < n && j < p_siz; ++j) {
480 if (paragraphs().begin()->isDeleted(j))
482 if (paragraphs().begin()->isInset(j)) {
483 if (!paragraphs().begin()->getInset(j)->isChar())
485 paragraphs().begin()->getInset(j)->toString(label);
487 label.put(paragraphs().begin()->getChar(j));
490 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
493 return label.str().empty() ? l : label.str();
497 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
499 //lyxerr << "InsetCollapsible: edit left/right" << endl;
501 InsetText::edit(cur, front, entry_from);
505 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
507 //lyxerr << "InsetCollapsible: edit xy" << endl;
508 if (geometry(cur.bv()) == ButtonOnly
509 || !descendable(cur.bv())
510 || (view_[&cur.bv()].button_dim_.contains(x, y)
511 && geometry(cur.bv()) != NoButton))
514 return InsetText::editXY(cur, x, y);
518 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
520 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
521 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
523 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
525 switch (cmd.action()) {
526 case LFUN_MOUSE_PRESS:
528 switch (cmd.button()) {
529 case mouse_button::button1:
530 case mouse_button::button3:
531 // Pass the command to the enclosing InsetText,
532 // so that the cursor gets set.
535 case mouse_button::none:
536 case mouse_button::button2:
537 case mouse_button::button4:
538 case mouse_button::button5:
540 cur.noScreenUpdate();
543 } else if (geometry(cur.bv()) != ButtonOnly)
544 InsetText::doDispatch(cur, cmd);
549 case LFUN_MOUSE_DOUBLE:
550 case LFUN_MOUSE_TRIPLE:
552 cur.noScreenUpdate();
553 else if (geometry(cur.bv()) != ButtonOnly)
554 InsetText::doDispatch(cur, cmd);
559 case LFUN_MOUSE_RELEASE:
561 // The mouse click has to be within the inset!
562 if (geometry(cur.bv()) != ButtonOnly)
563 InsetText::doDispatch(cur, cmd);
568 if (cmd.button() != mouse_button::button1) {
570 cur.noScreenUpdate();
573 // if we are selecting, we do not want to
577 // Left button is clicked, the user asks to
578 // toggle the inset visual state.
580 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
581 if (geometry(cur.bv()) == ButtonOnly) {
582 setStatus(cur, Open);
586 setStatus(cur, Collapsed);
587 cur.bv().cursor() = cur;
590 case LFUN_INSET_TOGGLE:
591 if (cmd.argument() == "open")
592 setStatus(cur, Open);
593 else if (cmd.argument() == "close")
594 setStatus(cur, Collapsed);
595 else if (cmd.argument() == "toggle" || cmd.argument().empty())
597 setStatus(cur, Collapsed);
599 setStatus(cur, Open);
600 else // if assign or anything else
605 case LFUN_INSET_EDIT: {
607 text().selectAll(cur);
608 string const format =
609 cur.buffer()->params().documentClass().outputFormat();
610 string const ext = theFormats().extension(format);
611 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
612 support::FileName const tempfilename = tempfile_->name();
613 string const name = tempfilename.toFilesystemEncoding();
614 ofdocstream os(name.c_str());
615 os << cur.selectionAsString(false);
617 // Since we lock the inset while the external file is edited,
618 // we need to move the cursor outside and clear any selection inside
619 cur.clearSelection();
621 cur.leaveInset(*this);
622 theFormats().edit(buffer(), tempfilename, format);
625 case LFUN_INSET_END_EDIT: {
626 support::FileName const tempfilename = tempfile_->name();
627 docstring const s = tempfilename.fileContents("UTF-8");
628 cur.recordUndoInset(this);
630 text().selectAll(cur);
631 cap::replaceSelection(cur);
632 cur.text()->insertStringAsLines(cur, s, cur.current_font);
633 // FIXME (gb) it crashes without this
641 InsetText::doDispatch(cur, cmd);
647 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
648 FuncStatus & flag) const
650 switch (cmd.action()) {
651 case LFUN_INSET_TOGGLE:
652 if (cmd.argument() == "open")
653 flag.setEnabled(status_ != Open);
654 else if (cmd.argument() == "close")
655 flag.setEnabled(status_ == Open);
656 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
657 flag.setEnabled(true);
658 flag.setOnOff(status_ == Open);
660 flag.setEnabled(false);
663 case LFUN_INSET_EDIT:
664 flag.setEnabled(!buffer().hasReadonlyFlag() &&
665 getLayout().editExternally() && tempfile_ == nullptr);
668 case LFUN_INSET_END_EDIT:
669 flag.setEnabled(!buffer().hasReadonlyFlag() &&
670 getLayout().editExternally() && tempfile_ != nullptr);
674 return InsetText::getStatus(cur, cmd, flag);
679 void InsetCollapsible::setLabel(docstring const & l)
685 docstring InsetCollapsible::getLabel() const
687 InsetLayout const & il = getLayout();
688 return labelstring_.empty() ?
689 translateIfPossible(il.labelstring()) : labelstring_;
693 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
696 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
697 // indicate changed content in label (#8645)
698 // ✎ U+270E LOWER RIGHT PENCIL
699 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
700 ? docstring(1, 0x270E) : docstring();
701 InsetLayout const & il = getLayout();
702 docstring const label = getLabel();
703 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
704 return locked + indicator + label;
705 return locked + indicator + getNewLabel(label);
709 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
713 if (status_ == Collapsed)
714 cur.leaveInset(*this);
718 InsetDecoration InsetCollapsible::decoration() const
720 InsetDecoration const dec = getLayout().decoration();
721 return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
725 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
728 string context_menu = contextMenuName();
729 string const it_context_menu = InsetText::contextMenuName();
730 if (decoration() == InsetDecoration::CONGLOMERATE)
731 return context_menu + ";" + it_context_menu;
733 string const ic_context_menu = InsetCollapsible::contextMenuName();
734 if (ic_context_menu != context_menu)
735 context_menu += ";" + ic_context_menu;
737 if (geometry(bv) == NoButton)
738 return context_menu + ";" + it_context_menu;
740 Dimension dim = dimensionCollapsed(bv);
741 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
744 return it_context_menu;
748 string InsetCollapsible::contextMenuName() const
750 if (decoration() == InsetDecoration::CONGLOMERATE)
751 return "context-conglomerate";
753 return "context-collapsible";
757 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
759 // return false to let RowPainter draw the change tracking cue consistently
760 // with the surrounding text, when the inset is inline: for buttons, for
761 // non-allowMultiPar insets.
762 switch (geometry(bv)) {
765 return allowMultiPar();
775 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
776 UpdateType utype, TocBackend & backend) const
778 bool doing_output = output_active && producesOutput();
779 InsetLayout const & layout = getLayout();
780 if (!layout.addToToc())
781 return InsetText::addToToc(cpit, doing_output, utype, backend);
783 TocBuilder & b = backend.builder(layout.tocType());
784 // Cursor inside the inset
785 DocIterator pit = cpit;
786 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
787 docstring const label = getLabel();
788 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
789 // Proceed with the rest of the inset.
790 InsetText::addToToc(cpit, doing_output, utype, backend);
791 if (layout.isTocCaption()) {
793 text().forOutliner(str, TOC_ENTRY_LENGTH);