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 const & 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 for (; i < n && j < p_siz; ++j) {
449 if (paragraphs().begin()->isDeleted(j))
451 if (paragraphs().begin()->isInset(j)) {
452 if (!paragraphs().begin()->getInset(j)->isChar())
454 paragraphs().begin()->getInset(j)->toString(label);
456 label.put(paragraphs().begin()->getChar(j));
459 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
462 docstring const lbl = label.str();
463 return lbl.empty() ? l : lbl;
467 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
469 //lyxerr << "InsetCollapsible: edit left/right" << endl;
471 InsetText::edit(cur, front, entry_from);
475 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
477 //lyxerr << "InsetCollapsible: edit xy" << endl;
478 if (geometry(cur.bv()) == ButtonOnly
479 || !descendable(cur.bv())
480 || (view_[&cur.bv()].button_dim_.contains(x, y)
481 && geometry(cur.bv()) != NoButton))
484 return InsetText::editXY(cur, x, y);
488 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
490 //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
491 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
493 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
495 switch (cmd.action()) {
496 case LFUN_MOUSE_PRESS:
498 switch (cmd.button()) {
499 case mouse_button::button1:
500 case mouse_button::button3:
501 // Pass the command to the enclosing InsetText,
502 // so that the cursor gets set.
505 case mouse_button::none:
506 case mouse_button::button2:
507 case mouse_button::button4:
508 case mouse_button::button5:
510 cur.noScreenUpdate();
513 } else if (geometry(cur.bv()) != ButtonOnly)
514 InsetText::doDispatch(cur, cmd);
519 case LFUN_MOUSE_DOUBLE:
520 case LFUN_MOUSE_TRIPLE:
522 cur.noScreenUpdate();
523 else if (geometry(cur.bv()) != ButtonOnly)
524 InsetText::doDispatch(cur, cmd);
529 case LFUN_MOUSE_RELEASE:
531 // The mouse click has to be within the inset!
532 if (geometry(cur.bv()) != ButtonOnly)
533 InsetText::doDispatch(cur, cmd);
538 if (cmd.button() != mouse_button::button1) {
540 cur.noScreenUpdate();
543 // if we are selecting, we do not want to
547 // Left button is clicked, the user asks to
548 // toggle the inset visual state.
550 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
551 if (geometry(cur.bv()) == ButtonOnly) {
552 setStatus(cur, Open);
556 setStatus(cur, Collapsed);
557 cur.bv().cursor() = cur;
560 case LFUN_INSET_TOGGLE:
561 if (cmd.argument() == "open")
562 setStatus(cur, Open);
563 else if (cmd.argument() == "close")
564 setStatus(cur, Collapsed);
565 else if (cmd.argument() == "toggle" || cmd.argument().empty())
567 setStatus(cur, Collapsed);
569 setStatus(cur, Open);
570 else // if assign or anything else
575 case LFUN_INSET_EDIT: {
577 text().selectAll(cur);
578 string const format =
579 cur.buffer()->params().documentClass().outputFormat();
580 string const ext = theFormats().extension(format);
581 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
582 support::FileName const tempfilename = tempfile_->name();
583 string const name = tempfilename.toFilesystemEncoding();
584 ofdocstream os(name.c_str());
585 os << cur.selectionAsString(false);
587 // Since we lock the inset while the external file is edited,
588 // we need to move the cursor outside and clear any selection inside
589 cur.clearSelection();
591 cur.leaveInset(*this);
592 theFormats().edit(buffer(), tempfilename, format);
595 case LFUN_INSET_END_EDIT: {
596 support::FileName const tempfilename = tempfile_->name();
597 docstring const s = tempfilename.fileContents("UTF-8");
598 cur.recordUndoInset(this);
600 text().selectAll(cur);
601 cap::replaceSelection(cur);
602 cur.text()->insertStringAsLines(cur, s, cur.current_font);
603 // FIXME (gb) it crashes without this
611 InsetText::doDispatch(cur, cmd);
617 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
618 FuncStatus & flag) const
620 switch (cmd.action()) {
621 case LFUN_INSET_TOGGLE:
622 if (cmd.argument() == "open")
623 flag.setEnabled(status_ != Open);
624 else if (cmd.argument() == "close")
625 flag.setEnabled(status_ == Open);
626 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
627 flag.setEnabled(true);
628 flag.setOnOff(status_ == Open);
630 flag.setEnabled(false);
633 case LFUN_INSET_EDIT:
634 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
637 case LFUN_INSET_END_EDIT:
638 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
642 return InsetText::getStatus(cur, cmd, flag);
647 void InsetCollapsible::setLabel(docstring const & l)
653 docstring InsetCollapsible::getLabel() const
655 InsetLayout const & il = getLayout();
656 return labelstring_.empty() ?
657 translateIfPossible(il.labelstring()) : labelstring_;
661 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
663 InsetLayout const & il = getLayout();
664 docstring const label = getLabel();
665 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
667 return getNewLabel(label);
671 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
675 if (status_ == Collapsed)
676 cur.leaveInset(*this);
680 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
682 InsetLayout::InsetDecoration const dec = getLayout().decoration();
683 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
687 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
690 string context_menu = contextMenuName();
691 string const it_context_menu = InsetText::contextMenuName();
692 if (decoration() == InsetLayout::CONGLOMERATE)
693 return context_menu + ";" + it_context_menu;
695 string const ic_context_menu = InsetCollapsible::contextMenuName();
696 if (ic_context_menu != context_menu)
697 context_menu += ";" + ic_context_menu;
699 if (geometry(bv) == NoButton)
700 return context_menu + ";" + it_context_menu;
702 Dimension dim = dimensionCollapsed(bv);
703 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
706 return it_context_menu;
710 string InsetCollapsible::contextMenuName() const
712 if (decoration() == InsetLayout::CONGLOMERATE)
713 return "context-conglomerate";
715 return "context-collapsible";
719 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
721 // return false to let RowPainter draw the change tracking cue consistently
722 // with the surrounding text, when the inset is inline: for buttons, for
723 // non-allowMultiPar insets.
724 switch (geometry(bv)) {
727 return allowMultiPar();
737 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
738 UpdateType utype, TocBackend & backend) const
740 bool doing_output = output_active && producesOutput();
741 InsetLayout const & layout = getLayout();
742 if (!layout.addToToc())
743 return InsetText::addToToc(cpit, doing_output, utype, backend);
745 TocBuilder & b = backend.builder(layout.tocType());
746 // Cursor inside the inset
747 DocIterator pit = cpit;
748 pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
749 docstring const label = getLabel();
750 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
751 // Proceed with the rest of the inset.
752 InsetText::addToToc(cpit, doing_output, utype, backend);
753 if (layout.isTocCaption()) {
755 text().forOutliner(str, TOC_ENTRY_LENGTH);