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/FileName.h"
39 #include "support/gettext.h"
40 #include "support/lassert.h"
41 #include "support/lstrings.h"
42 #include "support/RefChanger.h"
43 #include "support/TempFile.h"
50 InsetCollapsible::InsetCollapsible(Buffer * buf, InsetText::UsePlain ltype)
51 : InsetText(buf, ltype), status_(Open)
54 setFrameColor(Color_collapsibleframe);
58 // The sole purpose of this copy constructor is to make sure
59 // that the view_ map is not copied and remains empty.
60 InsetCollapsible::InsetCollapsible(InsetCollapsible const & rhs)
63 labelstring_(rhs.labelstring_)
69 InsetCollapsible & InsetCollapsible::operator=(InsetCollapsible const & that)
73 *this = InsetCollapsible(that);
78 InsetCollapsible::~InsetCollapsible()
80 map<BufferView const *, View>::iterator it = view_.begin();
81 map<BufferView const *, View>::iterator end = view_.end();
82 for (; it != end; ++it)
83 if (it->second.mouse_hover_)
84 it->first->clearLastInset(this);
88 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
90 if (decoration() == InsetLayout::CONGLOMERATE)
92 return view_[&bv].auto_open_ ? Open : status_;
96 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
98 switch (decoration()) {
99 case InsetLayout::CLASSIC:
100 if (status(bv) == Open)
101 return view_[&bv].openinlined_ ? LeftButton : TopButton;
104 case InsetLayout::MINIMALISTIC:
105 return status(bv) == Open ? NoButton : ButtonOnly ;
107 case InsetLayout::CONGLOMERATE:
108 return status(bv) == Open ? SubLabel : Corners ;
110 case InsetLayout::DEFAULT:
111 break; // this shouldn't happen
114 // dummy return value to shut down a warning,
115 // this is dead code.
120 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
122 Dimension const dim = dimensionCollapsed(bv);
123 if (geometry(bv) == NoButton)
124 return translateIfPossible(getLayout().labelstring());
125 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
128 return toolTipText();
132 void InsetCollapsible::write(ostream & os) const
148 void InsetCollapsible::read(Lexer & lex)
150 lex.setContext("InsetCollapsible::read");
153 lex >> "status" >> tmp_token;
154 if (tmp_token == "open")
157 InsetText::read(lex);
161 int InsetCollapsible::topOffset(BufferView const * bv) const
163 switch (geometry(*bv)) {
168 return InsetText::topOffset(bv);
172 int InsetCollapsible::bottomOffset(BufferView const * bv) const
174 switch (geometry(*bv)) {
177 return InsetText::bottomOffset(bv) / 4;
179 return InsetText::bottomOffset(bv);
184 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
187 FontInfo labelfont(getLabelfont());
188 labelfont.realize(sane_font);
189 theFontMetrics(labelfont).buttonText(
190 buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
195 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
197 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
199 FontInfo tmpfont = mi.base.font;
200 mi.base.font = getFont();
201 mi.base.font.realize(tmpfont);
203 BufferView const & bv = *mi.base.bv;
205 switch (geometry(bv)) {
207 InsetText::metrics(mi, dim);
210 InsetText::metrics(mi, dim);
213 InsetText::metrics(mi, dim);
214 // consider width of the inset label
215 FontInfo font(getLabelfont());
216 font.realize(sane_font);
222 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
229 if (hasFixedWidth()){
230 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
231 if (mi.base.textwidth < mindim)
232 mi.base.textwidth = mindim;
234 dim = dimensionCollapsed(bv);
235 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
237 InsetText::metrics(mi, textdim);
238 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
239 if (view_[&bv].openinlined_) {
240 // Correct for button width.
241 dim.wid += textdim.wid;
242 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
243 dim.asc = textdim.asc;
245 dim.des += textdim.height() + topOffset(mi.base.bv);
246 dim.wid = max(dim.wid, textdim.wid);
252 mi.base.font = tmpfont;
256 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
259 view_[bv].mouse_hover_ = mouse_hover;
264 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
266 BufferView const & bv = *pi.base.bv;
268 view_[&bv].auto_open_ = bv.cursor().isInside(this);
270 Changer dummy = pi.base.font.change(getFont(), true);
272 // Draw button first -- top, left or only
273 Dimension dimc = dimensionCollapsed(bv);
275 if (geometry(bv) == TopButton ||
276 geometry(bv) == LeftButton ||
277 geometry(bv) == ButtonOnly) {
278 view_[&bv].button_dim_.x1 = x + 0;
279 view_[&bv].button_dim_.x2 = x + dimc.width();
280 view_[&bv].button_dim_.y1 = y - dimc.asc;
281 view_[&bv].button_dim_.y2 = y + dimc.des;
283 FontInfo labelfont = getLabelfont();
284 labelfont.setColor(labelColor());
285 labelfont.realize(pi.base.font);
286 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
287 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
288 Color_buttonframe, Inset::textOffset(pi.base.bv));
289 // Draw the change tracking cue on the label, unless RowPainter already
291 if (canPaintChange(bv))
292 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
294 view_[&bv].button_dim_.x1 = 0;
295 view_[&bv].button_dim_.y1 = 0;
296 view_[&bv].button_dim_.x2 = 0;
297 view_[&bv].button_dim_.y2 = 0;
300 Dimension const textdim = dimensionHelper(bv);
301 int const baseline = y;
303 Geometry g = geometry(bv);
307 if (g == LeftButton) {
308 textx = x + dimc.width();
312 texty = baseline + dimc.des + textdim.asc;
314 // Do not draw the cue for INSERTED -- it is already in the button and
316 Changer cdummy = (pi.change.type == Change::INSERTED)
317 ? make_change(pi.change, Change())
319 InsetText::draw(pi, textx, texty);
327 InsetText::draw(pi, textx, texty);
333 // We will take care of the frame and the change tracking cue
336 Changer cdummy = make_change(pi.change, Change());
337 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
338 InsetText::draw(pi, textx, texty);
339 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
342 int desc = textdim.descent();
344 // Colour the frame according to the change type. (Like for tables.)
345 Color colour = pi.change.changed() ? pi.change.color()
347 const int xx1 = x + leftOffset(pi.base.bv) - 1;
348 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
349 pi.pain.line(xx1, y + desc - 4,
350 xx1, y + desc, colour);
352 pi.pain.line(xx1, y + desc,
353 xx2, y + desc, colour);
355 // Make status_ value visible:
356 pi.pain.line(xx1, y + desc,
357 xx1 + 4, y + desc, colour);
358 pi.pain.line(xx2 - 4, y + desc,
359 xx2, y + desc, colour);
361 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
362 y + desc - 4, colour);
364 // the label below the text. Can be toggled.
366 FontInfo font(getLabelfont());
367 if (pi.change.changed())
368 font.setPaintColor(colour);
369 font.realize(sane_font);
375 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
376 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
377 int const ww = max(textdim.wid, w);
378 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
379 buttonLabel(bv), font, col, Color_none);
382 int const y1 = y - textdim.asc + 3;
383 // a visual cue when the cursor is inside the inset
384 Cursor const & cur = bv.cursor();
385 if (cur.isInside(this)) {
386 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
387 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
388 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
389 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
391 // Strike through the inset if deleted and not already handled by
393 if (pi.change.deleted() && canPaintChange(bv))
394 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
400 void InsetCollapsible::cursorPos(BufferView const & bv,
401 CursorSlice const & sl, bool boundary, int & x, int & y) const
403 if (geometry(bv) == ButtonOnly)
406 InsetText::cursorPos(bv, sl, boundary, x, y);
407 Dimension const textdim = dimensionHelper(bv);
409 switch (geometry(bv)) {
411 x += dimensionCollapsed(bv).wid;
414 y += dimensionCollapsed(bv).des + textdim.asc;
429 bool InsetCollapsible::editable() const
434 switch (decoration()) {
435 case InsetLayout::CLASSIC:
436 case InsetLayout::MINIMALISTIC:
437 return status_ == Open;
444 bool InsetCollapsible::descendable(BufferView const & bv) const
449 return geometry(bv) != ButtonOnly;
453 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
455 return view_[&bv].button_dim_.contains(x, y);
459 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
461 odocstringstream label;
462 pos_type const max_length = 15;
463 pos_type const p_siz = paragraphs().begin()->size();
464 pos_type const n = min(max_length, p_siz);
467 for (; i < n && j < p_siz; ++j) {
468 if (paragraphs().begin()->isDeleted(j))
470 if (paragraphs().begin()->isInset(j)) {
471 if (!paragraphs().begin()->getInset(j)->isChar())
473 paragraphs().begin()->getInset(j)->toString(label);
475 label.put(paragraphs().begin()->getChar(j));
478 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
481 return label.str().empty() ? l : label.str();
485 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
487 //lyxerr << "InsetCollapsible: edit left/right" << endl;
489 InsetText::edit(cur, front, entry_from);
493 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
495 //lyxerr << "InsetCollapsible: edit xy" << endl;
496 if (geometry(cur.bv()) == ButtonOnly
497 || !descendable(cur.bv())
498 || (view_[&cur.bv()].button_dim_.contains(x, y)
499 && geometry(cur.bv()) != NoButton))
502 return InsetText::editXY(cur, x, y);
506 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
508 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
509 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
511 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
513 switch (cmd.action()) {
514 case LFUN_MOUSE_PRESS:
516 switch (cmd.button()) {
517 case mouse_button::button1:
518 case mouse_button::button3:
519 // Pass the command to the enclosing InsetText,
520 // so that the cursor gets set.
523 case mouse_button::none:
524 case mouse_button::button2:
525 case mouse_button::button4:
526 case mouse_button::button5:
528 cur.noScreenUpdate();
531 } else if (geometry(cur.bv()) != ButtonOnly)
532 InsetText::doDispatch(cur, cmd);
537 case LFUN_MOUSE_DOUBLE:
538 case LFUN_MOUSE_TRIPLE:
540 cur.noScreenUpdate();
541 else if (geometry(cur.bv()) != ButtonOnly)
542 InsetText::doDispatch(cur, cmd);
547 case LFUN_MOUSE_RELEASE:
549 // The mouse click has to be within the inset!
550 if (geometry(cur.bv()) != ButtonOnly)
551 InsetText::doDispatch(cur, cmd);
556 if (cmd.button() != mouse_button::button1) {
558 cur.noScreenUpdate();
561 // if we are selecting, we do not want to
565 // Left button is clicked, the user asks to
566 // toggle the inset visual state.
568 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
569 if (geometry(cur.bv()) == ButtonOnly) {
570 setStatus(cur, Open);
574 setStatus(cur, Collapsed);
575 cur.bv().cursor() = cur;
578 case LFUN_INSET_TOGGLE:
579 if (cmd.argument() == "open")
580 setStatus(cur, Open);
581 else if (cmd.argument() == "close")
582 setStatus(cur, Collapsed);
583 else if (cmd.argument() == "toggle" || cmd.argument().empty())
585 setStatus(cur, Collapsed);
587 setStatus(cur, Open);
588 else // if assign or anything else
593 case LFUN_INSET_EDIT: {
595 text().selectAll(cur);
596 string const format =
597 cur.buffer()->params().documentClass().outputFormat();
598 string const ext = theFormats().extension(format);
599 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
600 support::FileName const tempfilename = tempfile_->name();
601 string const name = tempfilename.toFilesystemEncoding();
602 ofdocstream os(name.c_str());
603 os << cur.selectionAsString(false);
605 // Since we lock the inset while the external file is edited,
606 // we need to move the cursor outside and clear any selection inside
607 cur.clearSelection();
609 cur.leaveInset(*this);
610 theFormats().edit(buffer(), tempfilename, format);
613 case LFUN_INSET_END_EDIT: {
614 support::FileName const tempfilename = tempfile_->name();
615 docstring const s = tempfilename.fileContents("UTF-8");
616 cur.recordUndoInset(this);
618 text().selectAll(cur);
619 cap::replaceSelection(cur);
620 cur.text()->insertStringAsLines(cur, s, cur.current_font);
621 // FIXME (gb) it crashes without this
629 InsetText::doDispatch(cur, cmd);
635 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
636 FuncStatus & flag) const
638 switch (cmd.action()) {
639 case LFUN_INSET_TOGGLE:
640 if (cmd.argument() == "open")
641 flag.setEnabled(status_ != Open);
642 else if (cmd.argument() == "close")
643 flag.setEnabled(status_ == Open);
644 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
645 flag.setEnabled(true);
646 flag.setOnOff(status_ == Open);
648 flag.setEnabled(false);
651 case LFUN_INSET_EDIT:
652 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
655 case LFUN_INSET_END_EDIT:
656 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
660 return InsetText::getStatus(cur, cmd, flag);
665 void InsetCollapsible::setLabel(docstring const & l)
671 docstring InsetCollapsible::getLabel() const
673 InsetLayout const & il = getLayout();
674 return labelstring_.empty() ?
675 translateIfPossible(il.labelstring()) : labelstring_;
679 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
681 // indicate changed content in label (#8645)
682 // ✎ U+270E LOWER RIGHT PENCIL
683 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
684 ? docstring(1, 0x270E) : docstring();
685 InsetLayout const & il = getLayout();
686 docstring const label = getLabel();
687 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
688 return indicator + label;
689 return indicator + getNewLabel(label);
693 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
697 if (status_ == Collapsed)
698 cur.leaveInset(*this);
702 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
704 InsetLayout::InsetDecoration const dec = getLayout().decoration();
705 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
709 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
712 string context_menu = contextMenuName();
713 string const it_context_menu = InsetText::contextMenuName();
714 if (decoration() == InsetLayout::CONGLOMERATE)
715 return context_menu + ";" + it_context_menu;
717 string const ic_context_menu = InsetCollapsible::contextMenuName();
718 if (ic_context_menu != context_menu)
719 context_menu += ";" + ic_context_menu;
721 if (geometry(bv) == NoButton)
722 return context_menu + ";" + it_context_menu;
724 Dimension dim = dimensionCollapsed(bv);
725 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
728 return it_context_menu;
732 string InsetCollapsible::contextMenuName() const
734 if (decoration() == InsetLayout::CONGLOMERATE)
735 return "context-conglomerate";
737 return "context-collapsible";
741 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
743 // return false to let RowPainter draw the change tracking cue consistently
744 // with the surrounding text, when the inset is inline: for buttons, for
745 // non-allowMultiPar insets.
746 switch (geometry(bv)) {
749 return allowMultiPar();
759 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
760 UpdateType utype, TocBackend & backend) const
762 bool doing_output = output_active && producesOutput();
763 InsetLayout const & layout = getLayout();
764 if (!layout.addToToc())
765 return InsetText::addToToc(cpit, doing_output, utype, backend);
767 TocBuilder & b = backend.builder(layout.tocType());
768 // Cursor inside the inset
769 DocIterator pit = cpit;
770 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
771 docstring const label = getLabel();
772 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
773 // Proceed with the rest of the inset.
774 InsetText::addToToc(cpit, doing_output, utype, backend);
775 if (layout.isTocCaption()) {
777 text().forOutliner(str, TOC_ENTRY_LENGTH);