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 "OutputParams.h"
30 #include "TextClass.h"
31 #include "TocBackend.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
36 #include "support/debug.h"
37 #include "support/docstream.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lstrings.h"
41 #include "support/RefChanger.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 ? NoButton : ButtonOnly ;
106 case InsetLayout::CONGLOMERATE:
107 return status(bv) == Open ? SubLabel : Corners ;
109 case InsetLayout::DEFAULT:
110 break; // this shouldn't happen
113 // dummy return value to shut down a warning,
114 // this is dead code.
119 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
121 Dimension const dim = dimensionCollapsed(bv);
122 if (geometry(bv) == NoButton)
123 return translateIfPossible(getLayout().labelstring());
124 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
127 return toolTipText();
131 void InsetCollapsible::write(ostream & os) const
147 void InsetCollapsible::read(Lexer & lex)
149 lex.setContext("InsetCollapsible::read");
152 lex >> "status" >> tmp_token;
153 if (tmp_token == "open")
156 InsetText::read(lex);
161 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
164 FontInfo labelfont(getLabelfont());
165 labelfont.realize(sane_font);
166 theFontMetrics(labelfont).buttonText(
167 buttonLabel(bv), TEXT_TO_INSET_OFFSET, dim.wid, dim.asc, dim.des);
172 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
174 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
176 FontInfo tmpfont = mi.base.font;
177 mi.base.font = getFont();
178 mi.base.font.realize(tmpfont);
180 BufferView const & bv = *mi.base.bv;
182 switch (geometry(bv)) {
184 InsetText::metrics(mi, dim);
187 InsetText::metrics(mi, dim);
192 InsetText::metrics(mi, dim);
193 // consider width of the inset label
194 FontInfo font(getLabelfont());
195 font.realize(sane_font);
201 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
208 if (hasFixedWidth()){
209 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
210 if (mi.base.textwidth < mindim)
211 mi.base.textwidth = mindim;
213 dim = dimensionCollapsed(bv);
214 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
216 InsetText::metrics(mi, textdim);
217 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
218 if (view_[&bv].openinlined_) {
219 // Correct for button width.
220 dim.wid += textdim.wid;
221 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
222 dim.asc = textdim.asc;
224 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
225 dim.wid = max(dim.wid, textdim.wid);
231 mi.base.font = tmpfont;
235 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
238 view_[bv].mouse_hover_ = mouse_hover;
243 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
245 BufferView const & bv = *pi.base.bv;
247 view_[&bv].auto_open_ = bv.cursor().isInside(this);
249 Changer dummy = pi.base.font.change(getFont(), true);
251 // Draw button first -- top, left or only
252 Dimension dimc = dimensionCollapsed(bv);
254 if (geometry(bv) == TopButton ||
255 geometry(bv) == LeftButton ||
256 geometry(bv) == ButtonOnly) {
257 view_[&bv].button_dim_.x1 = x + 0;
258 view_[&bv].button_dim_.x2 = x + dimc.width();
259 view_[&bv].button_dim_.y1 = y - dimc.asc;
260 view_[&bv].button_dim_.y2 = y + dimc.des;
262 FontInfo labelfont = getLabelfont();
263 labelfont.setColor(labelColor());
264 labelfont.realize(pi.base.font);
265 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
266 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
267 Color_buttonframe, TEXT_TO_INSET_OFFSET);
268 // Draw the change tracking cue on the label, unless RowPainter already
270 if (canPaintChange(bv))
271 pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
273 view_[&bv].button_dim_.x1 = 0;
274 view_[&bv].button_dim_.y1 = 0;
275 view_[&bv].button_dim_.x2 = 0;
276 view_[&bv].button_dim_.y2 = 0;
279 Dimension const textdim = dimensionHelper(bv);
280 int const baseline = y;
282 Geometry g = geometry(bv);
286 if (g == LeftButton) {
287 textx = x + dimc.width();
291 texty = baseline + dimc.des + textdim.asc;
293 // Do not draw the cue for INSERTED -- it is already in the button and
295 Changer cdummy = (pi.change_.type == Change::INSERTED)
296 ? make_change(pi.change_, Change())
298 InsetText::draw(pi, textx, texty);
306 InsetText::draw(pi, textx, texty);
312 // We will take care of the frame and the change tracking cue
315 Changer cdummy = make_change(pi.change_, Change());
316 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
317 InsetText::draw(pi, textx, texty);
318 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
321 int desc = textdim.descent();
325 // Colour the frame according to the change type. (Like for tables.)
326 Color colour = pi.change_.changed() ? pi.change_.color()
328 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
329 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
330 pi.pain.line(xx1, y + desc - 4,
331 xx1, y + desc, colour);
333 pi.pain.line(xx1, y + desc,
334 xx2, y + desc, colour);
336 // Make status_ value visible:
337 pi.pain.line(xx1, y + desc,
338 xx1 + 4, y + desc, colour);
339 pi.pain.line(xx2 - 4, y + desc,
340 xx2, y + desc, colour);
342 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
343 y + desc - 4, colour);
345 // the label below the text. Can be toggled.
347 FontInfo font(getLabelfont());
348 if (pi.change_.changed())
349 font.setPaintColor(colour);
350 font.realize(sane_font);
356 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
357 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
358 int const ww = max(textdim.wid, w);
359 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
360 buttonLabel(bv), font, col, Color_none);
363 int const y1 = y - textdim.asc + 3;
364 // a visual cue when the cursor is inside the inset
365 Cursor const & cur = bv.cursor();
366 if (cur.isInside(this)) {
367 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
368 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
369 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
370 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
372 // Strike through the inset if deleted and not already handled by
374 if (pi.change_.deleted() && canPaintChange(bv))
375 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
381 void InsetCollapsible::cursorPos(BufferView const & bv,
382 CursorSlice const & sl, bool boundary, int & x, int & y) const
384 if (geometry(bv) == ButtonOnly)
387 InsetText::cursorPos(bv, sl, boundary, x, y);
388 Dimension const textdim = dimensionHelper(bv);
390 switch (geometry(bv)) {
392 x += dimensionCollapsed(bv).wid;
395 y += dimensionCollapsed(bv).des + textdim.asc;
410 bool InsetCollapsible::editable() const
415 switch (decoration()) {
416 case InsetLayout::CLASSIC:
417 case InsetLayout::MINIMALISTIC:
418 return status_ == Open;
425 bool InsetCollapsible::descendable(BufferView const & bv) const
430 return geometry(bv) != ButtonOnly;
434 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
436 return view_[&bv].button_dim_.contains(x, y);
440 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
442 odocstringstream label;
443 pos_type const max_length = 15;
444 pos_type const p_siz = paragraphs().begin()->size();
445 pos_type const n = min(max_length, p_siz);
448 bool changed_content = false;
449 for (; i < n && j < p_siz; ++j) {
450 if (paragraphs().begin()->isChanged(j)) {
451 changed_content = true;
452 if (paragraphs().begin()->isDeleted(j))
455 if (paragraphs().begin()->isInset(j)) {
456 if (!paragraphs().begin()->getInset(j)->isChar())
458 paragraphs().begin()->getInset(j)->toString(label);
460 label.put(paragraphs().begin()->getChar(j));
463 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
467 // indicate changed content in label (#8645)
469 lbl = char_type(0x270E);// ✎ U+270E LOWER RIGHT PENCIL
471 return lbl.empty() ? l : lbl;
475 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
477 //lyxerr << "InsetCollapsible: edit left/right" << endl;
479 InsetText::edit(cur, front, entry_from);
483 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
485 //lyxerr << "InsetCollapsible: edit xy" << endl;
486 if (geometry(cur.bv()) == ButtonOnly
487 || !descendable(cur.bv())
488 || (view_[&cur.bv()].button_dim_.contains(x, y)
489 && geometry(cur.bv()) != NoButton))
492 return InsetText::editXY(cur, x, y);
496 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
498 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
499 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
501 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
503 switch (cmd.action()) {
504 case LFUN_MOUSE_PRESS:
506 switch (cmd.button()) {
507 case mouse_button::button1:
508 case mouse_button::button3:
509 // Pass the command to the enclosing InsetText,
510 // so that the cursor gets set.
513 case mouse_button::none:
514 case mouse_button::button2:
515 case mouse_button::button4:
516 case mouse_button::button5:
518 cur.noScreenUpdate();
521 } else if (geometry(cur.bv()) != ButtonOnly)
522 InsetText::doDispatch(cur, cmd);
527 case LFUN_MOUSE_DOUBLE:
528 case LFUN_MOUSE_TRIPLE:
530 cur.noScreenUpdate();
531 else if (geometry(cur.bv()) != ButtonOnly)
532 InsetText::doDispatch(cur, cmd);
537 case LFUN_MOUSE_RELEASE:
539 // The mouse click has to be within the inset!
540 if (geometry(cur.bv()) != ButtonOnly)
541 InsetText::doDispatch(cur, cmd);
546 if (cmd.button() != mouse_button::button1) {
548 cur.noScreenUpdate();
551 // if we are selecting, we do not want to
555 // Left button is clicked, the user asks to
556 // toggle the inset visual state.
558 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
559 if (geometry(cur.bv()) == ButtonOnly) {
560 setStatus(cur, Open);
564 setStatus(cur, Collapsed);
565 cur.bv().cursor() = cur;
568 case LFUN_INSET_TOGGLE:
569 if (cmd.argument() == "open")
570 setStatus(cur, Open);
571 else if (cmd.argument() == "close")
572 setStatus(cur, Collapsed);
573 else if (cmd.argument() == "toggle" || cmd.argument().empty())
575 setStatus(cur, Collapsed);
577 setStatus(cur, Open);
578 else // if assign or anything else
583 case LFUN_INSET_EDIT: {
585 text().selectAll(cur);
586 string const format =
587 cur.buffer()->params().documentClass().outputFormat();
588 string const ext = theFormats().extension(format);
589 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
590 support::FileName const tempfilename = tempfile_->name();
591 string const name = tempfilename.toFilesystemEncoding();
592 ofdocstream os(name.c_str());
593 os << cur.selectionAsString(false);
595 // Since we lock the inset while the external file is edited,
596 // we need to move the cursor outside and clear any selection inside
597 cur.clearSelection();
599 cur.leaveInset(*this);
600 theFormats().edit(buffer(), tempfilename, format);
603 case LFUN_INSET_END_EDIT: {
604 support::FileName const tempfilename = tempfile_->name();
605 docstring const s = tempfilename.fileContents("UTF-8");
606 cur.recordUndoInset(this);
608 text().selectAll(cur);
609 cap::replaceSelection(cur);
610 cur.text()->insertStringAsLines(cur, s, cur.current_font);
611 // FIXME (gb) it crashes without this
619 InsetText::doDispatch(cur, cmd);
625 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
626 FuncStatus & flag) const
628 switch (cmd.action()) {
629 case LFUN_INSET_TOGGLE:
630 if (cmd.argument() == "open")
631 flag.setEnabled(status_ != Open);
632 else if (cmd.argument() == "close")
633 flag.setEnabled(status_ == Open);
634 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
635 flag.setEnabled(true);
636 flag.setOnOff(status_ == Open);
638 flag.setEnabled(false);
641 case LFUN_INSET_EDIT:
642 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
645 case LFUN_INSET_END_EDIT:
646 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
650 return InsetText::getStatus(cur, cmd, flag);
655 void InsetCollapsible::setLabel(docstring const & l)
661 docstring InsetCollapsible::getLabel() const
663 InsetLayout const & il = getLayout();
664 return labelstring_.empty() ?
665 translateIfPossible(il.labelstring()) : labelstring_;
669 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
671 InsetLayout const & il = getLayout();
672 docstring const label = getLabel();
673 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
675 return getNewLabel(label);
679 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
683 if (status_ == Collapsed)
684 cur.leaveInset(*this);
688 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
690 InsetLayout::InsetDecoration const dec = getLayout().decoration();
691 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
695 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
698 string context_menu = contextMenuName();
699 string const it_context_menu = InsetText::contextMenuName();
700 if (decoration() == InsetLayout::CONGLOMERATE)
701 return context_menu + ";" + it_context_menu;
703 string const ic_context_menu = InsetCollapsible::contextMenuName();
704 if (ic_context_menu != context_menu)
705 context_menu += ";" + ic_context_menu;
707 if (geometry(bv) == NoButton)
708 return context_menu + ";" + it_context_menu;
710 Dimension dim = dimensionCollapsed(bv);
711 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
714 return it_context_menu;
718 string InsetCollapsible::contextMenuName() const
720 if (decoration() == InsetLayout::CONGLOMERATE)
721 return "context-conglomerate";
723 return "context-collapsible";
727 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
729 // return false to let RowPainter draw the change tracking cue consistently
730 // with the surrounding text, when the inset is inline: for buttons, for
731 // non-allowMultiPar insets.
732 switch (geometry(bv)) {
735 return allowMultiPar();
745 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
746 UpdateType utype, TocBackend & backend) const
748 bool doing_output = output_active && producesOutput();
749 InsetLayout const & layout = getLayout();
750 if (!layout.addToToc())
751 return InsetText::addToToc(cpit, doing_output, utype, backend);
753 TocBuilder & b = backend.builder(layout.tocType());
754 // Cursor inside the inset
755 DocIterator pit = cpit;
756 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
757 docstring const label = getLabel();
758 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
759 // Proceed with the rest of the inset.
760 InsetText::addToToc(cpit, doing_output, utype, backend);
761 if (layout.isTocCaption()) {
763 text().forOutliner(str, TOC_ENTRY_LENGTH);