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());
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 ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
268 return getLayout().bgcolor();
272 ColorCode InsetCollapsible::labelColor() const
274 return getLayout().labelfont().color();
278 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
280 BufferView const & bv = *pi.base.bv;
282 view_[&bv].auto_open_ = bv.cursor().isInside(this);
284 Changer dummy = pi.base.font.change(getFont(), true);
286 // Draw button first -- top, left or only
287 Dimension dimc = dimensionCollapsed(bv);
289 if (geometry(bv) == TopButton ||
290 geometry(bv) == LeftButton ||
291 geometry(bv) == ButtonOnly) {
292 view_[&bv].button_dim_.x1 = x + 0;
293 view_[&bv].button_dim_.x2 = x + dimc.width();
294 view_[&bv].button_dim_.y1 = y - dimc.asc;
295 view_[&bv].button_dim_.y2 = y + dimc.des;
297 FontInfo labelfont = getLabelfont();
298 labelfont.setColor(labelColor());
299 labelfont.realize(pi.base.font);
300 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
301 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
302 Color_buttonframe, Inset::textOffset(pi.base.bv));
303 // Draw the change tracking cue on the label, unless RowPainter already
305 if (canPaintChange(bv))
306 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
308 view_[&bv].button_dim_.x1 = 0;
309 view_[&bv].button_dim_.y1 = 0;
310 view_[&bv].button_dim_.x2 = 0;
311 view_[&bv].button_dim_.y2 = 0;
314 Dimension const textdim = dimensionHelper(bv);
315 int const baseline = y;
317 Geometry g = geometry(bv);
321 if (g == LeftButton) {
322 textx = x + dimc.width();
326 texty = baseline + dimc.des + textdim.asc;
328 // Do not draw the cue for INSERTED -- it is already in the button and
330 Changer cdummy = (pi.change.type == Change::INSERTED)
331 ? changeVar(pi.change, Change())
333 InsetText::draw(pi, textx, texty);
341 InsetText::draw(pi, textx, texty);
347 // We will take care of the frame and the change tracking cue
350 Changer cdummy = changeVar(pi.change, Change());
351 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
352 InsetText::draw(pi, textx, texty);
353 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
356 int desc = textdim.descent();
358 // Colour the frame according to the change type. (Like for tables.)
359 Color colour = pi.change.changed() ? pi.change.color()
361 const int xx1 = x + leftOffset(pi.base.bv) - 1;
362 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
363 pi.pain.line(xx1, y + desc - 4,
364 xx1, y + desc, colour);
366 pi.pain.line(xx1, y + desc,
367 xx2, y + desc, colour);
369 // Make status_ value visible:
370 pi.pain.line(xx1, y + desc,
371 xx1 + 4, y + desc, colour);
372 pi.pain.line(xx2 - 4, y + desc,
373 xx2, y + desc, colour);
375 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
376 y + desc - 4, colour);
378 // the label below the text. Can be toggled.
380 FontInfo font(getLabelfont());
381 if (pi.change.changed())
382 font.setPaintColor(colour);
383 font.realize(sane_font);
389 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
390 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
391 int const ww = max(textdim.wid, w);
392 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
393 buttonLabel(bv), font, col, Color_none);
396 int const y1 = y - textdim.asc + 3;
397 // a visual cue when the cursor is inside the inset
398 Cursor const & cur = bv.cursor();
399 if (cur.isInside(this)) {
400 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
401 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
402 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
403 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
405 // Strike through the inset if deleted and not already handled by
407 if (pi.change.deleted() && canPaintChange(bv))
408 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
414 void InsetCollapsible::cursorPos(BufferView const & bv,
415 CursorSlice const & sl, bool boundary, int & x, int & y) const
417 if (geometry(bv) == ButtonOnly)
420 InsetText::cursorPos(bv, sl, boundary, x, y);
421 Dimension const textdim = dimensionHelper(bv);
423 switch (geometry(bv)) {
425 x += dimensionCollapsed(bv).wid;
428 y += dimensionCollapsed(bv).des + textdim.asc;
443 bool InsetCollapsible::editable() const
448 switch (decoration()) {
449 case InsetDecoration::CLASSIC:
450 case InsetDecoration::MINIMALISTIC:
451 return status_ == Open;
458 bool InsetCollapsible::descendable(BufferView const & bv) const
463 return geometry(bv) != ButtonOnly;
467 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
469 return view_[&bv].button_dim_.contains(x, y);
473 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
475 odocstringstream label;
476 pos_type const max_length = 15;
477 pos_type const p_siz = paragraphs().begin()->size();
478 pos_type const n = min(max_length, p_siz);
481 for (; i < n && j < p_siz; ++j) {
482 if (paragraphs().begin()->isDeleted(j))
484 if (paragraphs().begin()->isInset(j)) {
485 if (!paragraphs().begin()->getInset(j)->isChar())
487 paragraphs().begin()->getInset(j)->toString(label);
489 label.put(paragraphs().begin()->getChar(j));
492 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
495 return label.str().empty() ? l : label.str();
499 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
501 //lyxerr << "InsetCollapsible: edit left/right" << endl;
503 InsetText::edit(cur, front, entry_from);
507 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
509 //lyxerr << "InsetCollapsible: edit xy" << endl;
510 if (geometry(cur.bv()) == ButtonOnly
511 || !descendable(cur.bv())
512 || (view_[&cur.bv()].button_dim_.contains(x, y)
513 && geometry(cur.bv()) != NoButton))
516 return InsetText::editXY(cur, x, y);
520 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
522 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
523 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
525 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
527 switch (cmd.action()) {
528 case LFUN_MOUSE_PRESS:
530 switch (cmd.button()) {
531 case mouse_button::button1:
532 case mouse_button::button3:
533 // Pass the command to the enclosing InsetText,
534 // so that the cursor gets set.
537 case mouse_button::none:
538 case mouse_button::button2:
539 case mouse_button::button4:
540 case mouse_button::button5:
542 cur.noScreenUpdate();
545 } else if (geometry(cur.bv()) != ButtonOnly)
546 InsetText::doDispatch(cur, cmd);
551 case LFUN_MOUSE_DOUBLE:
552 case LFUN_MOUSE_TRIPLE:
554 cur.noScreenUpdate();
555 else if (geometry(cur.bv()) != ButtonOnly)
556 InsetText::doDispatch(cur, cmd);
561 case LFUN_MOUSE_RELEASE:
563 // The mouse click has to be within the inset!
564 if (geometry(cur.bv()) != ButtonOnly)
565 InsetText::doDispatch(cur, cmd);
570 if (cmd.button() != mouse_button::button1) {
572 cur.noScreenUpdate();
575 // if we are selecting, we do not want to
579 // Left button is clicked, the user asks to
580 // toggle the inset visual state.
582 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
583 if (geometry(cur.bv()) == ButtonOnly) {
584 setStatus(cur, Open);
588 setStatus(cur, Collapsed);
589 cur.bv().cursor() = cur;
592 case LFUN_INSET_TOGGLE:
593 if (cmd.argument() == "open")
594 setStatus(cur, Open);
595 else if (cmd.argument() == "close")
596 setStatus(cur, Collapsed);
597 else if (cmd.argument() == "toggle" || cmd.argument().empty())
599 setStatus(cur, Collapsed);
601 setStatus(cur, Open);
602 else // if assign or anything else
607 case LFUN_INSET_EDIT: {
609 text().selectAll(cur);
610 string const format =
611 cur.buffer()->params().documentClass().outputFormat();
612 string const ext = theFormats().extension(format);
613 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
614 support::FileName const tempfilename = tempfile_->name();
615 string const name = tempfilename.toFilesystemEncoding();
616 ofdocstream os(name.c_str());
617 os << cur.selectionAsString(false);
619 // Since we lock the inset while the external file is edited,
620 // we need to move the cursor outside and clear any selection inside
621 cur.clearSelection();
623 cur.leaveInset(*this);
624 theFormats().edit(buffer(), tempfilename, format);
627 case LFUN_INSET_END_EDIT: {
628 support::FileName const tempfilename = tempfile_->name();
629 docstring const s = tempfilename.fileContents("UTF-8");
630 cur.recordUndoInset(this);
632 text().selectAll(cur);
633 cap::replaceSelection(cur);
634 cur.text()->insertStringAsLines(cur, s, cur.current_font);
635 // FIXME (gb) it crashes without this
643 InsetText::doDispatch(cur, cmd);
649 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
650 FuncStatus & flag) const
652 switch (cmd.action()) {
653 case LFUN_INSET_TOGGLE:
654 if (cmd.argument() == "open")
655 flag.setEnabled(status_ != Open);
656 else if (cmd.argument() == "close")
657 flag.setEnabled(status_ == Open);
658 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
659 flag.setEnabled(true);
660 flag.setOnOff(status_ == Open);
662 flag.setEnabled(false);
665 case LFUN_INSET_EDIT:
666 flag.setEnabled(!buffer().hasReadonlyFlag() &&
667 getLayout().editExternally() && tempfile_ == nullptr);
670 case LFUN_INSET_END_EDIT:
671 flag.setEnabled(!buffer().hasReadonlyFlag() &&
672 getLayout().editExternally() && tempfile_ != nullptr);
676 return InsetText::getStatus(cur, cmd, flag);
681 void InsetCollapsible::setLabel(docstring const & l)
687 docstring InsetCollapsible::getLabel() const
689 InsetLayout const & il = getLayout();
690 return labelstring_.empty() ?
691 translateIfPossible(il.labelstring()) : labelstring_;
695 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
698 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
699 if (decoration() == InsetDecoration::MINIMALISTIC)
701 // indicate changed content in label (#8645)
702 // ✎ U+270E LOWER RIGHT PENCIL
703 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
704 ? docstring(1, 0x270E) : docstring();
705 InsetLayout const & il = getLayout();
706 docstring const label = getLabel();
707 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
708 return locked + indicator + label;
709 return locked + indicator + getNewLabel(label);
713 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
717 if (status_ == Collapsed)
718 cur.leaveInset(*this);
722 InsetDecoration InsetCollapsible::decoration() const
724 InsetDecoration const dec = getLayout().decoration();
725 return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
729 FontInfo InsetCollapsible::getFont() const
731 return getLayout().font();
735 FontInfo InsetCollapsible::getLabelfont() const
737 return getLayout().labelfont();
741 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
744 string context_menu = contextMenuName();
745 string const it_context_menu = InsetText::contextMenuName();
746 if (decoration() == InsetDecoration::CONGLOMERATE)
747 return context_menu + ";" + it_context_menu;
749 string const ic_context_menu = InsetCollapsible::contextMenuName();
750 if (ic_context_menu != context_menu)
751 context_menu += ";" + ic_context_menu;
753 if (geometry(bv) == NoButton)
754 return context_menu + ";" + it_context_menu;
756 Dimension dim = dimensionCollapsed(bv);
757 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
760 return it_context_menu;
764 string InsetCollapsible::contextMenuName() const
766 if (decoration() == InsetDecoration::CONGLOMERATE)
767 return "context-conglomerate";
769 return "context-collapsible";
773 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
775 // return false to let RowPainter draw the change tracking cue consistently
776 // with the surrounding text, when the inset is inline: for buttons, for
777 // non-allowMultiPar insets.
778 switch (geometry(bv)) {
781 return allowMultiPar();
791 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
792 UpdateType utype, TocBackend & backend) const
794 bool doing_output = output_active && producesOutput();
795 InsetLayout const & layout = getLayout();
796 if (!layout.addToToc())
797 return InsetText::addToToc(cpit, doing_output, utype, backend);
799 TocBuilder & b = backend.builder(layout.tocType());
800 // Cursor inside the inset
801 DocIterator pit = cpit;
802 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
803 docstring const label = getLabel();
804 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
805 // Proceed with the rest of the inset.
806 InsetText::addToToc(cpit, doing_output, utype, backend);
807 if (layout.isTocCaption()) {
809 text().forOutliner(str, TOC_ENTRY_LENGTH);