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)) {
175 return InsetText::topOffset(bv) / 4;
177 return InsetText::topOffset(bv);
182 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
185 FontInfo labelfont(getLabelfont());
186 labelfont.realize(sane_font);
187 theFontMetrics(labelfont).buttonText(
188 buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
193 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
195 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
197 FontInfo tmpfont = mi.base.font;
198 mi.base.font = getFont();
199 mi.base.font.realize(tmpfont);
201 BufferView const & bv = *mi.base.bv;
203 switch (geometry(bv)) {
205 InsetText::metrics(mi, dim);
208 InsetText::metrics(mi, dim);
211 InsetText::metrics(mi, dim);
212 // consider width of the inset label
213 FontInfo font(getLabelfont());
214 font.realize(sane_font);
220 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
227 if (hasFixedWidth()){
228 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
229 if (mi.base.textwidth < mindim)
230 mi.base.textwidth = mindim;
232 dim = dimensionCollapsed(bv);
233 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
235 InsetText::metrics(mi, textdim);
236 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
237 if (view_[&bv].openinlined_) {
238 // Correct for button width.
239 dim.wid += textdim.wid;
240 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
241 dim.asc = textdim.asc;
243 dim.des += textdim.height() + topOffset(mi.base.bv);
244 dim.wid = max(dim.wid, textdim.wid);
250 mi.base.font = tmpfont;
254 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
257 view_[bv].mouse_hover_ = mouse_hover;
262 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
264 BufferView const & bv = *pi.base.bv;
266 view_[&bv].auto_open_ = bv.cursor().isInside(this);
268 Changer dummy = pi.base.font.change(getFont(), true);
270 // Draw button first -- top, left or only
271 Dimension dimc = dimensionCollapsed(bv);
273 if (geometry(bv) == TopButton ||
274 geometry(bv) == LeftButton ||
275 geometry(bv) == ButtonOnly) {
276 view_[&bv].button_dim_.x1 = x + 0;
277 view_[&bv].button_dim_.x2 = x + dimc.width();
278 view_[&bv].button_dim_.y1 = y - dimc.asc;
279 view_[&bv].button_dim_.y2 = y + dimc.des;
281 FontInfo labelfont = getLabelfont();
282 labelfont.setColor(labelColor());
283 labelfont.realize(pi.base.font);
284 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
285 view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
286 Color_buttonframe, Inset::textOffset(pi.base.bv));
287 // Draw the change tracking cue on the label, unless RowPainter already
289 if (canPaintChange(bv))
290 pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
292 view_[&bv].button_dim_.x1 = 0;
293 view_[&bv].button_dim_.y1 = 0;
294 view_[&bv].button_dim_.x2 = 0;
295 view_[&bv].button_dim_.y2 = 0;
298 Dimension const textdim = dimensionHelper(bv);
299 int const baseline = y;
301 Geometry g = geometry(bv);
305 if (g == LeftButton) {
306 textx = x + dimc.width();
310 texty = baseline + dimc.des + textdim.asc;
312 // Do not draw the cue for INSERTED -- it is already in the button and
314 Changer cdummy = (pi.change.type == Change::INSERTED)
315 ? make_change(pi.change, Change())
317 InsetText::draw(pi, textx, texty);
325 InsetText::draw(pi, textx, texty);
331 // We will take care of the frame and the change tracking cue
334 Changer cdummy = make_change(pi.change, Change());
335 const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
336 InsetText::draw(pi, textx, texty);
337 const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
340 int desc = textdim.descent();
342 // Colour the frame according to the change type. (Like for tables.)
343 Color colour = pi.change.changed() ? pi.change.color()
345 const int xx1 = x + leftOffset(pi.base.bv) - 1;
346 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
347 pi.pain.line(xx1, y + desc - 4,
348 xx1, y + desc, colour);
350 pi.pain.line(xx1, y + desc,
351 xx2, y + desc, colour);
353 // Make status_ value visible:
354 pi.pain.line(xx1, y + desc,
355 xx1 + 4, y + desc, colour);
356 pi.pain.line(xx2 - 4, y + desc,
357 xx2, y + desc, colour);
359 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
360 y + desc - 4, colour);
362 // the label below the text. Can be toggled.
364 FontInfo font(getLabelfont());
365 if (pi.change.changed())
366 font.setPaintColor(colour);
367 font.realize(sane_font);
373 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
374 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
375 int const ww = max(textdim.wid, w);
376 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
377 buttonLabel(bv), font, col, Color_none);
380 int const y1 = y - textdim.asc + 3;
381 // a visual cue when the cursor is inside the inset
382 Cursor const & cur = bv.cursor();
383 if (cur.isInside(this)) {
384 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
385 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
386 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
387 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
389 // Strike through the inset if deleted and not already handled by
391 if (pi.change.deleted() && canPaintChange(bv))
392 pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
398 void InsetCollapsible::cursorPos(BufferView const & bv,
399 CursorSlice const & sl, bool boundary, int & x, int & y) const
401 if (geometry(bv) == ButtonOnly)
404 InsetText::cursorPos(bv, sl, boundary, x, y);
405 Dimension const textdim = dimensionHelper(bv);
407 switch (geometry(bv)) {
409 x += dimensionCollapsed(bv).wid;
412 y += dimensionCollapsed(bv).des + textdim.asc;
427 bool InsetCollapsible::editable() const
432 switch (decoration()) {
433 case InsetLayout::CLASSIC:
434 case InsetLayout::MINIMALISTIC:
435 return status_ == Open;
442 bool InsetCollapsible::descendable(BufferView const & bv) const
447 return geometry(bv) != ButtonOnly;
451 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
453 return view_[&bv].button_dim_.contains(x, y);
457 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
459 odocstringstream label;
460 pos_type const max_length = 15;
461 pos_type const p_siz = paragraphs().begin()->size();
462 pos_type const n = min(max_length, p_siz);
465 for (; i < n && j < p_siz; ++j) {
466 if (paragraphs().begin()->isDeleted(j))
468 if (paragraphs().begin()->isInset(j)) {
469 if (!paragraphs().begin()->getInset(j)->isChar())
471 paragraphs().begin()->getInset(j)->toString(label);
473 label.put(paragraphs().begin()->getChar(j));
476 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
479 return label.str().empty() ? l : label.str();
483 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
485 //lyxerr << "InsetCollapsible: edit left/right" << endl;
487 InsetText::edit(cur, front, entry_from);
491 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
493 //lyxerr << "InsetCollapsible: edit xy" << endl;
494 if (geometry(cur.bv()) == ButtonOnly
495 || !descendable(cur.bv())
496 || (view_[&cur.bv()].button_dim_.contains(x, y)
497 && geometry(cur.bv()) != NoButton))
500 return InsetText::editXY(cur, x, y);
504 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
506 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
507 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
509 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
511 switch (cmd.action()) {
512 case LFUN_MOUSE_PRESS:
514 switch (cmd.button()) {
515 case mouse_button::button1:
516 case mouse_button::button3:
517 // Pass the command to the enclosing InsetText,
518 // so that the cursor gets set.
521 case mouse_button::none:
522 case mouse_button::button2:
523 case mouse_button::button4:
524 case mouse_button::button5:
526 cur.noScreenUpdate();
529 } else if (geometry(cur.bv()) != ButtonOnly)
530 InsetText::doDispatch(cur, cmd);
535 case LFUN_MOUSE_DOUBLE:
536 case LFUN_MOUSE_TRIPLE:
538 cur.noScreenUpdate();
539 else if (geometry(cur.bv()) != ButtonOnly)
540 InsetText::doDispatch(cur, cmd);
545 case LFUN_MOUSE_RELEASE:
547 // The mouse click has to be within the inset!
548 if (geometry(cur.bv()) != ButtonOnly)
549 InsetText::doDispatch(cur, cmd);
554 if (cmd.button() != mouse_button::button1) {
556 cur.noScreenUpdate();
559 // if we are selecting, we do not want to
563 // Left button is clicked, the user asks to
564 // toggle the inset visual state.
566 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
567 if (geometry(cur.bv()) == ButtonOnly) {
568 setStatus(cur, Open);
572 setStatus(cur, Collapsed);
573 cur.bv().cursor() = cur;
576 case LFUN_INSET_TOGGLE:
577 if (cmd.argument() == "open")
578 setStatus(cur, Open);
579 else if (cmd.argument() == "close")
580 setStatus(cur, Collapsed);
581 else if (cmd.argument() == "toggle" || cmd.argument().empty())
583 setStatus(cur, Collapsed);
585 setStatus(cur, Open);
586 else // if assign or anything else
591 case LFUN_INSET_EDIT: {
593 text().selectAll(cur);
594 string const format =
595 cur.buffer()->params().documentClass().outputFormat();
596 string const ext = theFormats().extension(format);
597 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
598 support::FileName const tempfilename = tempfile_->name();
599 string const name = tempfilename.toFilesystemEncoding();
600 ofdocstream os(name.c_str());
601 os << cur.selectionAsString(false);
603 // Since we lock the inset while the external file is edited,
604 // we need to move the cursor outside and clear any selection inside
605 cur.clearSelection();
607 cur.leaveInset(*this);
608 theFormats().edit(buffer(), tempfilename, format);
611 case LFUN_INSET_END_EDIT: {
612 support::FileName const tempfilename = tempfile_->name();
613 docstring const s = tempfilename.fileContents("UTF-8");
614 cur.recordUndoInset(this);
616 text().selectAll(cur);
617 cap::replaceSelection(cur);
618 cur.text()->insertStringAsLines(cur, s, cur.current_font);
619 // FIXME (gb) it crashes without this
627 InsetText::doDispatch(cur, cmd);
633 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
634 FuncStatus & flag) const
636 switch (cmd.action()) {
637 case LFUN_INSET_TOGGLE:
638 if (cmd.argument() == "open")
639 flag.setEnabled(status_ != Open);
640 else if (cmd.argument() == "close")
641 flag.setEnabled(status_ == Open);
642 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
643 flag.setEnabled(true);
644 flag.setOnOff(status_ == Open);
646 flag.setEnabled(false);
649 case LFUN_INSET_EDIT:
650 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
653 case LFUN_INSET_END_EDIT:
654 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
658 return InsetText::getStatus(cur, cmd, flag);
663 void InsetCollapsible::setLabel(docstring const & l)
669 docstring InsetCollapsible::getLabel() const
671 InsetLayout const & il = getLayout();
672 return labelstring_.empty() ?
673 translateIfPossible(il.labelstring()) : labelstring_;
677 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
679 // indicate changed content in label (#8645)
680 // ✎ U+270E LOWER RIGHT PENCIL
681 docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
682 ? docstring(1, 0x270E) : docstring();
683 InsetLayout const & il = getLayout();
684 docstring const label = getLabel();
685 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
686 return indicator + label;
687 return indicator + getNewLabel(label);
691 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
695 if (status_ == Collapsed)
696 cur.leaveInset(*this);
700 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
702 InsetLayout::InsetDecoration const dec = getLayout().decoration();
703 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
707 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
710 string context_menu = contextMenuName();
711 string const it_context_menu = InsetText::contextMenuName();
712 if (decoration() == InsetLayout::CONGLOMERATE)
713 return context_menu + ";" + it_context_menu;
715 string const ic_context_menu = InsetCollapsible::contextMenuName();
716 if (ic_context_menu != context_menu)
717 context_menu += ";" + ic_context_menu;
719 if (geometry(bv) == NoButton)
720 return context_menu + ";" + it_context_menu;
722 Dimension dim = dimensionCollapsed(bv);
723 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
726 return it_context_menu;
730 string InsetCollapsible::contextMenuName() const
732 if (decoration() == InsetLayout::CONGLOMERATE)
733 return "context-conglomerate";
735 return "context-collapsible";
739 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
741 // return false to let RowPainter draw the change tracking cue consistently
742 // with the surrounding text, when the inset is inline: for buttons, for
743 // non-allowMultiPar insets.
744 switch (geometry(bv)) {
747 return allowMultiPar();
757 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
758 UpdateType utype, TocBackend & backend) const
760 bool doing_output = output_active && producesOutput();
761 InsetLayout const & layout = getLayout();
762 if (!layout.addToToc())
763 return InsetText::addToToc(cpit, doing_output, utype, backend);
765 TocBuilder & b = backend.builder(layout.tocType());
766 // Cursor inside the inset
767 DocIterator pit = cpit;
768 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
769 docstring const label = getLabel();
770 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
771 // Proceed with the rest of the inset.
772 InsetText::addToToc(cpit, doing_output, utype, backend);
773 if (layout.isTocCaption()) {
775 text().forOutliner(str, TOC_ENTRY_LENGTH);