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 int const offset = Inset::textOffset(&bv);
192 theFontMetrics(labelfont).buttonText(
193 buttonLabel(bv), offset, dim.wid, dim.asc, dim.des);
194 // remove spacing on the right for left buttons; we also do it for
195 // TopButton (although it is not useful per se), because
196 // openinlined_ is not always set properly at this point.
197 Geometry const geom = geometry(bv);
198 if (geom == LeftButton || geom == TopButton)
199 // this form makes a difference if offset is even
200 dim.wid -= offset - offset / 2;
205 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
207 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
209 BufferView const & bv = *mi.base.bv;
211 switch (geometry(bv)) {
213 InsetText::metrics(mi, dim);
216 InsetText::metrics(mi, dim);
219 InsetText::metrics(mi, dim);
220 // consider width of the inset label
221 FontInfo font(getLabelfont());
222 font.realize(sane_font);
228 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
230 dim.wid = max(dim.wid, w);
236 if (hasFixedWidth()){
237 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
238 if (mi.base.textwidth < mindim)
239 mi.base.textwidth = mindim;
241 dim = dimensionCollapsed(bv);
242 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
244 InsetText::metrics(mi, textdim);
245 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
246 if (view_[&bv].openinlined_) {
247 // Correct for button width but remove spacing before frame
248 dim.wid += textdim.wid - leftOffset(mi.base.bv) / 2;
249 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
250 dim.asc = textdim.asc;
252 dim.des += textdim.height() + topOffset(mi.base.bv);
253 dim.wid = max(dim.wid, textdim.wid);
261 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
264 view_[bv].mouse_hover_ = mouse_hover;
269 ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
271 return getLayout().bgcolor();
275 ColorCode InsetCollapsible::labelColor() const
277 return getLayout().labelfont().color();
281 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
283 BufferView const & bv = *pi.base.bv;
285 view_[&bv].auto_open_ = bv.cursor().isInside(this);
287 Changer dummy = pi.base.font.change(getFont(), true);
289 // Draw button first -- top, left or only
290 Dimension dimc = dimensionCollapsed(bv);
292 if (geometry(bv) == TopButton ||
293 geometry(bv) == LeftButton ||
294 geometry(bv) == ButtonOnly) {
295 view_[&bv].button_dim_.x1 = x + 0;
296 view_[&bv].button_dim_.x2 = x + dimc.width();
297 view_[&bv].button_dim_.y1 = y - dimc.asc;
298 view_[&bv].button_dim_.y2 = y + dimc.des;
300 FontInfo labelfont = getLabelfont();
301 labelfont.setColor(labelColor());
302 labelfont.realize(pi.base.font);
303 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
304 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
305 Color_buttonframe, Inset::textOffset(pi.base.bv));
306 // Draw the change tracking cue on the label, unless RowPainter already
308 if (canPaintChange(bv))
309 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
311 view_[&bv].button_dim_.x1 = 0;
312 view_[&bv].button_dim_.y1 = 0;
313 view_[&bv].button_dim_.x2 = 0;
314 view_[&bv].button_dim_.y2 = 0;
317 Dimension const textdim = dimensionHelper(bv);
318 int const baseline = y;
320 Geometry g = geometry(bv);
324 if (g == LeftButton) {
325 // correct for spacing added before the frame in
326 // InsetText::draw. We want the button to touch the frame.
327 textx = x + dimc.width() - leftOffset(pi.base.bv) / 2;
331 texty = baseline + dimc.des + textdim.asc;
333 // Do not draw the cue for INSERTED -- it is already in the button and
335 Changer cdummy = (pi.change.type == Change::INSERTED)
336 ? changeVar(pi.change, Change())
338 InsetText::draw(pi, textx, texty);
346 InsetText::draw(pi, textx, texty);
352 // We will take care of the frame and the change tracking cue
355 Changer cdummy = changeVar(pi.change, Change());
356 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
357 InsetText::draw(pi, textx, texty);
358 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
361 int desc = textdim.descent();
363 // Colour the frame according to the change type. (Like for tables.)
364 Color colour = pi.change.changed() ? pi.change.color()
366 const int xx1 = x + leftOffset(pi.base.bv) - 1;
367 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
368 pi.pain.line(xx1, y + desc - 4,
369 xx1, y + desc, colour);
371 pi.pain.line(xx1, y + desc,
372 xx2, y + desc, colour);
374 // Make status_ value visible:
375 pi.pain.line(xx1, y + desc,
376 xx1 + 4, y + desc, colour);
377 pi.pain.line(xx2 - 4, y + desc,
378 xx2, y + desc, colour);
380 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
381 y + desc - 4, colour);
383 // the label below the text. Can be toggled.
385 FontInfo font(getLabelfont());
386 if (pi.change.changed())
387 font.setPaintColor(colour);
388 font.realize(sane_font);
394 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
395 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
396 int const ww = max(textdim.wid, w);
397 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
398 buttonLabel(bv), font, col, Color_none);
401 int const y1 = y - textdim.asc + 3;
402 // a visual cue when the cursor is inside the inset
403 Cursor const & cur = bv.cursor();
404 if (cur.isInside(this)) {
405 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
406 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
407 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
408 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
410 // Strike through the inset if deleted and not already handled by
412 if (pi.change.deleted() && canPaintChange(bv))
413 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
419 void InsetCollapsible::cursorPos(BufferView const & bv,
420 CursorSlice const & sl, bool boundary, int & x, int & y) const
422 if (geometry(bv) == ButtonOnly)
425 InsetText::cursorPos(bv, sl, boundary, x, y);
426 Dimension const textdim = dimensionHelper(bv);
428 switch (geometry(bv)) {
430 x += dimensionCollapsed(bv).wid - leftOffset(&bv) / 2;
433 y += dimensionCollapsed(bv).des + textdim.asc;
448 bool InsetCollapsible::editable() const
453 switch (decoration()) {
454 case InsetDecoration::CLASSIC:
455 case InsetDecoration::MINIMALISTIC:
456 return status_ == Open;
463 bool InsetCollapsible::descendable(BufferView const & bv) const
468 return geometry(bv) != ButtonOnly;
472 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
474 return view_[&bv].button_dim_.contains(x, y);
478 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
480 odocstringstream label;
481 pos_type const max_length = 15;
482 pos_type const p_siz = paragraphs().begin()->size();
483 pos_type const n = min(max_length, p_siz);
486 for (; i < n && j < p_siz; ++j) {
487 if (paragraphs().begin()->isDeleted(j))
489 if (paragraphs().begin()->isInset(j)) {
490 if (!paragraphs().begin()->getInset(j)->isChar())
492 paragraphs().begin()->getInset(j)->toString(label);
494 label.put(paragraphs().begin()->getChar(j));
497 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
500 return label.str().empty() ? l : label.str();
504 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
506 //lyxerr << "InsetCollapsible: edit left/right" << endl;
508 InsetText::edit(cur, front, entry_from);
512 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
514 //lyxerr << "InsetCollapsible: edit xy" << endl;
515 if (geometry(cur.bv()) == ButtonOnly
516 || !descendable(cur.bv())
517 || (view_[&cur.bv()].button_dim_.contains(x, y)
518 && geometry(cur.bv()) != NoButton))
521 return InsetText::editXY(cur, x, y);
525 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
527 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
528 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
530 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
532 switch (cmd.action()) {
533 case LFUN_MOUSE_PRESS:
535 switch (cmd.button()) {
536 case mouse_button::button1:
537 case mouse_button::button3:
538 // Pass the command to the enclosing InsetText,
539 // so that the cursor gets set.
542 case mouse_button::none:
543 case mouse_button::button2:
544 case mouse_button::button4:
545 case mouse_button::button5:
547 cur.noScreenUpdate();
550 } else if (geometry(cur.bv()) != ButtonOnly)
551 InsetText::doDispatch(cur, cmd);
556 case LFUN_MOUSE_DOUBLE:
557 case LFUN_MOUSE_TRIPLE:
559 cur.noScreenUpdate();
560 else if (geometry(cur.bv()) != ButtonOnly)
561 InsetText::doDispatch(cur, cmd);
566 case LFUN_MOUSE_RELEASE:
568 // The mouse click has to be within the inset!
569 if (geometry(cur.bv()) != ButtonOnly)
570 InsetText::doDispatch(cur, cmd);
575 if (cmd.button() != mouse_button::button1) {
577 cur.noScreenUpdate();
580 // if we are selecting, we do not want to
584 // Left button is clicked, the user asks to
585 // toggle the inset visual state.
587 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
588 if (geometry(cur.bv()) == ButtonOnly) {
589 setStatus(cur, Open);
593 setStatus(cur, Collapsed);
594 cur.bv().cursor() = cur;
597 case LFUN_INSET_TOGGLE:
598 if (cmd.argument() == "open")
599 setStatus(cur, Open);
600 else if (cmd.argument() == "close")
601 setStatus(cur, Collapsed);
602 else if (cmd.argument() == "toggle" || cmd.argument().empty())
604 setStatus(cur, Collapsed);
606 setStatus(cur, Open);
607 else // if assign or anything else
612 case LFUN_INSET_EDIT: {
614 text().selectAll(cur);
615 string const format =
616 cur.buffer()->params().documentClass().outputFormat();
617 string const ext = theFormats().extension(format);
618 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
619 support::FileName const tempfilename = tempfile_->name();
620 string const name = tempfilename.toFilesystemEncoding();
621 ofdocstream os(name.c_str());
622 os << cur.selectionAsString(false);
624 // Since we lock the inset while the external file is edited,
625 // we need to move the cursor outside and clear any selection inside
626 cur.clearSelection();
628 cur.leaveInset(*this);
629 theFormats().edit(buffer(), tempfilename, format);
632 case LFUN_INSET_END_EDIT: {
633 support::FileName const tempfilename = tempfile_->name();
634 docstring const s = tempfilename.fileContents("UTF-8");
635 cur.recordUndoInset(this);
637 text().selectAll(cur);
638 cap::replaceSelection(cur);
639 cur.text()->insertStringAsLines(cur, s, cur.current_font);
640 // FIXME (gb) it crashes without this
648 InsetText::doDispatch(cur, cmd);
654 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
655 FuncStatus & flag) const
657 switch (cmd.action()) {
658 case LFUN_INSET_TOGGLE:
659 if (cmd.argument() == "open")
660 flag.setEnabled(status_ != Open);
661 else if (cmd.argument() == "close")
662 flag.setEnabled(status_ == Open);
663 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
664 flag.setEnabled(true);
665 flag.setOnOff(status_ == Open);
667 flag.setEnabled(false);
670 case LFUN_INSET_EDIT:
671 flag.setEnabled(!buffer().hasReadonlyFlag() &&
672 getLayout().editExternally() && tempfile_ == nullptr);
675 case LFUN_INSET_END_EDIT:
676 flag.setEnabled(!buffer().hasReadonlyFlag() &&
677 getLayout().editExternally() && tempfile_ != nullptr);
681 return InsetText::getStatus(cur, cmd, flag);
686 void InsetCollapsible::setLabel(docstring const & l)
692 docstring InsetCollapsible::getLabel() const
694 InsetLayout const & il = getLayout();
695 return labelstring_.empty() ?
696 translateIfPossible(il.labelstring()) : labelstring_;
700 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
703 docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
704 // indicate changed content in label (#8645)
705 // ✎ U+270E LOWER RIGHT PENCIL
706 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
707 ? docstring(1, 0x270E) : docstring();
708 InsetLayout const & il = getLayout();
709 docstring const label = getLabel();
710 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
711 return locked + indicator + label;
712 return locked + indicator + getNewLabel(label);
716 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
720 if (status_ == Collapsed)
721 cur.leaveInset(*this);
725 InsetDecoration InsetCollapsible::decoration() const
727 InsetDecoration const dec = getLayout().decoration();
728 return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
732 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
735 string context_menu = contextMenuName();
736 string const it_context_menu = InsetText::contextMenuName();
737 if (decoration() == InsetDecoration::CONGLOMERATE)
738 return context_menu + ";" + it_context_menu;
740 string const ic_context_menu = InsetCollapsible::contextMenuName();
741 if (ic_context_menu != context_menu)
742 context_menu += ";" + ic_context_menu;
744 if (geometry(bv) == NoButton)
745 return context_menu + ";" + it_context_menu;
747 Dimension dim = dimensionCollapsed(bv);
748 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
751 return it_context_menu;
755 string InsetCollapsible::contextMenuName() const
757 if (decoration() == InsetDecoration::CONGLOMERATE)
758 return "context-conglomerate";
760 return "context-collapsible";
764 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
766 // return false to let RowPainter draw the change tracking cue consistently
767 // with the surrounding text, when the inset is inline: for buttons, for
768 // non-allowMultiPar insets.
769 switch (geometry(bv)) {
772 return allowMultiPar();
782 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
783 UpdateType utype, TocBackend & backend) const
785 bool doing_output = output_active && producesOutput();
786 InsetLayout const & layout = getLayout();
787 if (!layout.addToToc())
788 return InsetText::addToToc(cpit, doing_output, utype, backend);
790 TocBuilder & b = backend.builder(layout.tocType());
791 // Cursor inside the inset
792 DocIterator pit = cpit;
793 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
794 docstring const label = getLabel();
795 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
796 // Proceed with the rest of the inset.
797 InsetText::addToToc(cpit, doing_output, utype, backend);
798 if (layout.isTocCaption()) {
800 text().forOutliner(str, TOC_ENTRY_LENGTH);