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);
160 int InsetCollapsible::topOffset(BufferView const * bv) const
162 switch (geometry(*bv)) {
167 return InsetText::topOffset(bv);
171 int InsetCollapsible::bottomOffset(BufferView const * bv) const
173 switch (geometry(*bv)) {
176 return InsetText::bottomOffset(bv) / 4;
178 return InsetText::bottomOffset(bv);
183 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
186 FontInfo labelfont(getLabelfont());
187 labelfont.realize(sane_font);
188 theFontMetrics(labelfont).buttonText(
189 buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
194 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
196 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
198 FontInfo tmpfont = mi.base.font;
199 mi.base.font = getFont();
200 mi.base.font.realize(tmpfont);
202 BufferView const & bv = *mi.base.bv;
204 switch (geometry(bv)) {
206 InsetText::metrics(mi, dim);
209 InsetText::metrics(mi, dim);
212 InsetText::metrics(mi, dim);
213 // consider width of the inset label
214 FontInfo font(getLabelfont());
215 font.realize(sane_font);
221 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
228 if (hasFixedWidth()){
229 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
230 if (mi.base.textwidth < mindim)
231 mi.base.textwidth = mindim;
233 dim = dimensionCollapsed(bv);
234 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
236 InsetText::metrics(mi, textdim);
237 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
238 if (view_[&bv].openinlined_) {
239 // Correct for button width.
240 dim.wid += textdim.wid;
241 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
242 dim.asc = textdim.asc;
244 dim.des += textdim.height() + topOffset(mi.base.bv);
245 dim.wid = max(dim.wid, textdim.wid);
251 mi.base.font = tmpfont;
255 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
258 view_[bv].mouse_hover_ = mouse_hover;
263 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
265 BufferView const & bv = *pi.base.bv;
267 view_[&bv].auto_open_ = bv.cursor().isInside(this);
269 Changer dummy = pi.base.font.change(getFont(), true);
271 // Draw button first -- top, left or only
272 Dimension dimc = dimensionCollapsed(bv);
274 if (geometry(bv) == TopButton ||
275 geometry(bv) == LeftButton ||
276 geometry(bv) == ButtonOnly) {
277 view_[&bv].button_dim_.x1 = x + 0;
278 view_[&bv].button_dim_.x2 = x + dimc.width();
279 view_[&bv].button_dim_.y1 = y - dimc.asc;
280 view_[&bv].button_dim_.y2 = y + dimc.des;
282 FontInfo labelfont = getLabelfont();
283 labelfont.setColor(labelColor());
284 labelfont.realize(pi.base.font);
285 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
286 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
287 Color_buttonframe, Inset::textOffset(pi.base.bv));
288 // Draw the change tracking cue on the label, unless RowPainter already
290 if (canPaintChange(bv))
291 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
293 view_[&bv].button_dim_.x1 = 0;
294 view_[&bv].button_dim_.y1 = 0;
295 view_[&bv].button_dim_.x2 = 0;
296 view_[&bv].button_dim_.y2 = 0;
299 Dimension const textdim = dimensionHelper(bv);
300 int const baseline = y;
302 Geometry g = geometry(bv);
306 if (g == LeftButton) {
307 textx = x + dimc.width();
311 texty = baseline + dimc.des + textdim.asc;
313 // Do not draw the cue for INSERTED -- it is already in the button and
315 Changer cdummy = (pi.change.type == Change::INSERTED)
316 ? make_change(pi.change, Change())
318 InsetText::draw(pi, textx, texty);
326 InsetText::draw(pi, textx, texty);
332 // We will take care of the frame and the change tracking cue
335 Changer cdummy = make_change(pi.change, Change());
336 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
337 InsetText::draw(pi, textx, texty);
338 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
341 int desc = textdim.descent();
343 // Colour the frame according to the change type. (Like for tables.)
344 Color colour = pi.change.changed() ? pi.change.color()
346 const int xx1 = x + leftOffset(pi.base.bv) - 1;
347 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
348 pi.pain.line(xx1, y + desc - 4,
349 xx1, y + desc, colour);
351 pi.pain.line(xx1, y + desc,
352 xx2, y + desc, colour);
354 // Make status_ value visible:
355 pi.pain.line(xx1, y + desc,
356 xx1 + 4, y + desc, colour);
357 pi.pain.line(xx2 - 4, y + desc,
358 xx2, y + desc, colour);
360 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
361 y + desc - 4, colour);
363 // the label below the text. Can be toggled.
365 FontInfo font(getLabelfont());
366 if (pi.change.changed())
367 font.setPaintColor(colour);
368 font.realize(sane_font);
374 Color const col = pi.full_repaint ? Color_none : pi.background_color;
375 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
376 int const ww = max(textdim.wid, w);
377 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
378 buttonLabel(bv), font, col, Color_none);
381 int const y1 = y - textdim.asc + 3;
382 // a visual cue when the cursor is inside the inset
383 Cursor const & cur = bv.cursor();
384 if (cur.isInside(this)) {
385 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
386 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
387 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
388 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
390 // Strike through the inset if deleted and not already handled by
392 if (pi.change.deleted() && canPaintChange(bv))
393 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
399 void InsetCollapsible::cursorPos(BufferView const & bv,
400 CursorSlice const & sl, bool boundary, int & x, int & y) const
402 if (geometry(bv) == ButtonOnly)
405 InsetText::cursorPos(bv, sl, boundary, x, y);
406 Dimension const textdim = dimensionHelper(bv);
408 switch (geometry(bv)) {
410 x += dimensionCollapsed(bv).wid;
413 y += dimensionCollapsed(bv).des + textdim.asc;
428 bool InsetCollapsible::editable() const
433 switch (decoration()) {
434 case InsetLayout::CLASSIC:
435 case InsetLayout::MINIMALISTIC:
436 return status_ == Open;
443 bool InsetCollapsible::descendable(BufferView const & bv) const
448 return geometry(bv) != ButtonOnly;
452 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
454 return view_[&bv].button_dim_.contains(x, y);
458 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
460 odocstringstream label;
461 pos_type const max_length = 15;
462 pos_type const p_siz = paragraphs().begin()->size();
463 pos_type const n = min(max_length, p_siz);
466 for (; i < n && j < p_siz; ++j) {
467 if (paragraphs().begin()->isDeleted(j))
469 if (paragraphs().begin()->isInset(j)) {
470 if (!paragraphs().begin()->getInset(j)->isChar())
472 paragraphs().begin()->getInset(j)->toString(label);
474 label.put(paragraphs().begin()->getChar(j));
477 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
480 return label.str().empty() ? l : label.str();
484 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
486 //lyxerr << "InsetCollapsible: edit left/right" << endl;
488 InsetText::edit(cur, front, entry_from);
492 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
494 //lyxerr << "InsetCollapsible: edit xy" << endl;
495 if (geometry(cur.bv()) == ButtonOnly
496 || !descendable(cur.bv())
497 || (view_[&cur.bv()].button_dim_.contains(x, y)
498 && geometry(cur.bv()) != NoButton))
501 return InsetText::editXY(cur, x, y);
505 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
507 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
508 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
510 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
512 switch (cmd.action()) {
513 case LFUN_MOUSE_PRESS:
515 switch (cmd.button()) {
516 case mouse_button::button1:
517 case mouse_button::button3:
518 // Pass the command to the enclosing InsetText,
519 // so that the cursor gets set.
522 case mouse_button::none:
523 case mouse_button::button2:
524 case mouse_button::button4:
525 case mouse_button::button5:
527 cur.noScreenUpdate();
530 } else if (geometry(cur.bv()) != ButtonOnly)
531 InsetText::doDispatch(cur, cmd);
536 case LFUN_MOUSE_DOUBLE:
537 case LFUN_MOUSE_TRIPLE:
539 cur.noScreenUpdate();
540 else if (geometry(cur.bv()) != ButtonOnly)
541 InsetText::doDispatch(cur, cmd);
546 case LFUN_MOUSE_RELEASE:
548 // The mouse click has to be within the inset!
549 if (geometry(cur.bv()) != ButtonOnly)
550 InsetText::doDispatch(cur, cmd);
555 if (cmd.button() != mouse_button::button1) {
557 cur.noScreenUpdate();
560 // if we are selecting, we do not want to
564 // Left button is clicked, the user asks to
565 // toggle the inset visual state.
567 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
568 if (geometry(cur.bv()) == ButtonOnly) {
569 setStatus(cur, Open);
573 setStatus(cur, Collapsed);
574 cur.bv().cursor() = cur;
577 case LFUN_INSET_TOGGLE:
578 if (cmd.argument() == "open")
579 setStatus(cur, Open);
580 else if (cmd.argument() == "close")
581 setStatus(cur, Collapsed);
582 else if (cmd.argument() == "toggle" || cmd.argument().empty())
584 setStatus(cur, Collapsed);
586 setStatus(cur, Open);
587 else // if assign or anything else
592 case LFUN_INSET_EDIT: {
594 text().selectAll(cur);
595 string const format =
596 cur.buffer()->params().documentClass().outputFormat();
597 string const ext = theFormats().extension(format);
598 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
599 support::FileName const tempfilename = tempfile_->name();
600 string const name = tempfilename.toFilesystemEncoding();
601 ofdocstream os(name.c_str());
602 os << cur.selectionAsString(false);
604 // Since we lock the inset while the external file is edited,
605 // we need to move the cursor outside and clear any selection inside
606 cur.clearSelection();
608 cur.leaveInset(*this);
609 theFormats().edit(buffer(), tempfilename, format);
612 case LFUN_INSET_END_EDIT: {
613 support::FileName const tempfilename = tempfile_->name();
614 docstring const s = tempfilename.fileContents("UTF-8");
615 cur.recordUndoInset(this);
617 text().selectAll(cur);
618 cap::replaceSelection(cur);
619 cur.text()->insertStringAsLines(cur, s, cur.current_font);
620 // FIXME (gb) it crashes without this
628 InsetText::doDispatch(cur, cmd);
634 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
635 FuncStatus & flag) const
637 switch (cmd.action()) {
638 case LFUN_INSET_TOGGLE:
639 if (cmd.argument() == "open")
640 flag.setEnabled(status_ != Open);
641 else if (cmd.argument() == "close")
642 flag.setEnabled(status_ == Open);
643 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
644 flag.setEnabled(true);
645 flag.setOnOff(status_ == Open);
647 flag.setEnabled(false);
650 case LFUN_INSET_EDIT:
651 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
654 case LFUN_INSET_END_EDIT:
655 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
659 return InsetText::getStatus(cur, cmd, flag);
664 void InsetCollapsible::setLabel(docstring const & l)
670 docstring InsetCollapsible::getLabel() const
672 InsetLayout const & il = getLayout();
673 return labelstring_.empty() ?
674 translateIfPossible(il.labelstring()) : labelstring_;
678 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
680 // indicate changed content in label (#8645)
681 // ✎ U+270E LOWER RIGHT PENCIL
682 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
683 ? docstring(1, 0x270E) : docstring();
684 InsetLayout const & il = getLayout();
685 docstring const label = getLabel();
686 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
687 return indicator + label;
688 return indicator + getNewLabel(label);
692 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
696 if (status_ == Collapsed)
697 cur.leaveInset(*this);
701 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
703 InsetLayout::InsetDecoration const dec = getLayout().decoration();
704 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
708 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
711 string context_menu = contextMenuName();
712 string const it_context_menu = InsetText::contextMenuName();
713 if (decoration() == InsetLayout::CONGLOMERATE)
714 return context_menu + ";" + it_context_menu;
716 string const ic_context_menu = InsetCollapsible::contextMenuName();
717 if (ic_context_menu != context_menu)
718 context_menu += ";" + ic_context_menu;
720 if (geometry(bv) == NoButton)
721 return context_menu + ";" + it_context_menu;
723 Dimension dim = dimensionCollapsed(bv);
724 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
727 return it_context_menu;
731 string InsetCollapsible::contextMenuName() const
733 if (decoration() == InsetLayout::CONGLOMERATE)
734 return "context-conglomerate";
736 return "context-collapsible";
740 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
742 // return false to let RowPainter draw the change tracking cue consistently
743 // with the surrounding text, when the inset is inline: for buttons, for
744 // non-allowMultiPar insets.
745 switch (geometry(bv)) {
748 return allowMultiPar();
758 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
759 UpdateType utype, TocBackend & backend) const
761 bool doing_output = output_active && producesOutput();
762 InsetLayout const & layout = getLayout();
763 if (!layout.addToToc())
764 return InsetText::addToToc(cpit, doing_output, utype, backend);
766 TocBuilder & b = backend.builder(layout.tocType());
767 // Cursor inside the inset
768 DocIterator pit = cpit;
769 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
770 docstring const label = getLabel();
771 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
772 // Proceed with the rest of the inset.
773 InsetText::addToToc(cpit, doing_output, utype, backend);
774 if (layout.isTocCaption()) {
776 text().forOutliner(str, TOC_ENTRY_LENGTH);