]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsible.cpp
Use LOCK symbol with Minimalistic decoration, too.
[lyx.git] / src / insets / InsetCollapsible.cpp
1 /**
2  * \file InsetCollapsible.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCollapsible.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "CutAndPaste.h"
21 #include "Cursor.h"
22 #include "Dimension.h"
23 #include "Format.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "Lexer.h"
28 #include "MetricsInfo.h"
29 #include "TextClass.h"
30 #include "TocBackend.h"
31
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
34
35 #include "support/debug.h"
36 #include "support/docstream.h"
37 #include "support/FileName.h"
38 #include "support/gettext.h"
39 #include "support/lassert.h"
40 #include "support/lstrings.h"
41 #include "support/Changer.h"
42 #include "support/TempFile.h"
43
44 using namespace std;
45
46
47 namespace lyx {
48
49 InsetCollapsible::InsetCollapsible(Buffer * buf, InsetText::UsePlain ltype)
50         : InsetText(buf, ltype), status_(Open)
51 {
52         setDrawFrame(true);
53         setFrameColor(Color_collapsibleframe);
54 }
55
56
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)
60         : InsetText(rhs),
61           status_(rhs.status_),
62           labelstring_(rhs.labelstring_)
63 {
64         tempfile_.reset();
65 }
66
67
68 InsetCollapsible & InsetCollapsible::operator=(InsetCollapsible const & that)
69 {
70         if (&that == this)
71                 return *this;
72         *this = InsetCollapsible(that);
73         return *this;
74 }
75
76
77 InsetCollapsible::~InsetCollapsible()
78 {
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);
84 }
85
86
87 InsetCollapsible::CollapseStatus InsetCollapsible::status(BufferView const & bv) const
88 {
89         if (decoration() == InsetLayout::CONGLOMERATE)
90                 return status_;
91         return view_[&bv].auto_open_ ? Open : status_;
92 }
93
94
95 InsetCollapsible::Geometry InsetCollapsible::geometry(BufferView const & bv) const
96 {
97         switch (decoration()) {
98         case InsetLayout::CLASSIC:
99                 if (status(bv) == Open)
100                         return view_[&bv].openinlined_ ? LeftButton : TopButton;
101                 return ButtonOnly;
102
103         case InsetLayout::MINIMALISTIC: {
104                 return status(bv) == Open ?
105                         (tempfile_ ? LeftButton : NoButton)
106                         : ButtonOnly;
107         }
108
109         case InsetLayout::CONGLOMERATE:
110                 return status(bv) == Open ? SubLabel : Corners ;
111
112         case InsetLayout::DEFAULT:
113                 break; // this shouldn't happen
114         }
115
116         // dummy return value to shut down a warning,
117         // this is dead code.
118         return NoButton;
119 }
120
121
122 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
123 {
124         Dimension const dim = dimensionCollapsed(bv);
125         if (geometry(bv) == NoButton)
126                 return translateIfPossible(getLayout().labelstring());
127         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
128                 return docstring();
129
130         return toolTipText();
131 }
132
133
134 void InsetCollapsible::write(ostream & os) const
135 {
136         os << "status ";
137         switch (status_) {
138         case Open:
139                 os << "open";
140                 break;
141         case Collapsed:
142                 os << "collapsed";
143                 break;
144         }
145         os << "\n";
146         text().write(os);
147 }
148
149
150 void InsetCollapsible::read(Lexer & lex)
151 {
152         lex.setContext("InsetCollapsible::read");
153         string tmp_token;
154         status_ = Collapsed;
155         lex >> "status" >> tmp_token;
156         if (tmp_token == "open")
157                 status_ = Open;
158
159         InsetText::read(lex);
160         setButtonLabel();
161 }
162
163 int InsetCollapsible::topOffset(BufferView const * bv) const
164 {
165         switch (geometry(*bv)) {
166         case Corners:
167         case SubLabel:
168                 return 0;
169         default:
170                 return InsetText::topOffset(bv);
171         }
172 }
173
174 int InsetCollapsible::bottomOffset(BufferView const * bv) const
175 {
176         switch (geometry(*bv)) {
177         case Corners:
178         case SubLabel:
179                 return InsetText::bottomOffset(bv) / 4;
180         default:
181                 return InsetText::bottomOffset(bv);
182         }
183 }
184
185
186 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
187 {
188         Dimension dim;
189         FontInfo labelfont(getLabelfont());
190         labelfont.realize(sane_font);
191         theFontMetrics(labelfont).buttonText(
192                 buttonLabel(bv), Inset::textOffset(&bv), dim.wid, dim.asc, dim.des);
193         return dim;
194 }
195
196
197 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
198 {
199         view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
200
201         FontInfo tmpfont = mi.base.font;
202         mi.base.font = getFont();
203         mi.base.font.realize(tmpfont);
204
205         BufferView const & bv = *mi.base.bv;
206
207         switch (geometry(bv)) {
208         case NoButton:
209                 InsetText::metrics(mi, dim);
210                 break;
211         case Corners:
212                 InsetText::metrics(mi, dim);
213                 break;
214         case SubLabel: {
215                 InsetText::metrics(mi, dim);
216                 // consider width of the inset label
217                 FontInfo font(getLabelfont());
218                 font.realize(sane_font);
219                 font.decSize();
220                 font.decSize();
221                 int w = 0;
222                 int a = 0;
223                 int d = 0;
224                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
225                 dim.des += a + d;
226                 break;
227                 }
228         case TopButton:
229         case LeftButton:
230         case ButtonOnly:
231                 if (hasFixedWidth()){
232                         int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
233                         if (mi.base.textwidth < mindim)
234                                 mi.base.textwidth = mindim;
235                 }
236                 dim = dimensionCollapsed(bv);
237                 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
238                         Dimension textdim;
239                         InsetText::metrics(mi, textdim);
240                         view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
241                         if (view_[&bv].openinlined_) {
242                                 // Correct for button width.
243                                 dim.wid += textdim.wid;
244                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
245                                 dim.asc = textdim.asc;
246                         } else {
247                                 dim.des += textdim.height() + topOffset(mi.base.bv);
248                                 dim.wid = max(dim.wid, textdim.wid);
249                         }
250                 }
251                 break;
252         }
253
254         mi.base.font = tmpfont;
255 }
256
257
258 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
259         const
260 {
261         view_[bv].mouse_hover_ = mouse_hover;
262         return true;
263 }
264
265
266 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
267 {
268         BufferView const & bv = *pi.base.bv;
269
270         view_[&bv].auto_open_ = bv.cursor().isInside(this);
271
272         Changer dummy = pi.base.font.change(getFont(), true);
273
274         // Draw button first -- top, left or only
275         Dimension dimc = dimensionCollapsed(bv);
276
277         if (geometry(bv) == TopButton ||
278             geometry(bv) == LeftButton ||
279             geometry(bv) == ButtonOnly) {
280                 view_[&bv].button_dim_.x1 = x + 0;
281                 view_[&bv].button_dim_.x2 = x + dimc.width();
282                 view_[&bv].button_dim_.y1 = y - dimc.asc;
283                 view_[&bv].button_dim_.y2 = y + dimc.des;
284
285                 FontInfo labelfont = getLabelfont();
286                 labelfont.setColor(labelColor());
287                 labelfont.realize(pi.base.font);
288                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
289                                    view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
290                                    Color_buttonframe, Inset::textOffset(pi.base.bv));
291                 // Draw the change tracking cue on the label, unless RowPainter already
292                 // takes care of it.
293                 if (canPaintChange(bv))
294                         pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
295         } else {
296                 view_[&bv].button_dim_.x1 = 0;
297                 view_[&bv].button_dim_.y1 = 0;
298                 view_[&bv].button_dim_.x2 = 0;
299                 view_[&bv].button_dim_.y2 = 0;
300         }
301
302         Dimension const textdim = dimensionHelper(bv);
303         int const baseline = y;
304         int textx, texty;
305         Geometry g = geometry(bv);
306         switch (g) {
307         case LeftButton:
308         case TopButton: {
309                 if (g == LeftButton) {
310                         textx = x + dimc.width();
311                         texty = baseline;
312                 } else {
313                         textx = x;
314                         texty = baseline + dimc.des + textdim.asc;
315                 }
316                 // Do not draw the cue for INSERTED -- it is already in the button and
317                 // that's enough.
318                 Changer cdummy = (pi.change.type == Change::INSERTED)
319                         ? changeVar(pi.change, Change())
320                         : noChange();
321                 InsetText::draw(pi, textx, texty);
322                 break;
323         }
324         case ButtonOnly:
325                 break;
326         case NoButton:
327                 textx = x;
328                 texty = baseline;
329                 InsetText::draw(pi, textx, texty);
330                 break;
331         case SubLabel:
332         case Corners:
333                 textx = x;
334                 texty = baseline;
335                 // We will take care of the frame and the change tracking cue
336                 // ourselves, below.
337                 {
338                         Changer cdummy = changeVar(pi.change, Change());
339                         const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
340                         InsetText::draw(pi, textx, texty);
341                         const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
342                 }
343
344                 int desc = textdim.descent();
345
346                 // Colour the frame according to the change type. (Like for tables.)
347                 Color colour = pi.change.changed() ? pi.change.color()
348                                                     : Color_foreground;
349                 const int xx1 = x + leftOffset(pi.base.bv) - 1;
350                 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
351                 pi.pain.line(xx1, y + desc - 4,
352                              xx1, y + desc, colour);
353                 if (status_ == Open)
354                         pi.pain.line(xx1, y + desc,
355                                      xx2, y + desc, colour);
356                 else {
357                         // Make status_ value visible:
358                         pi.pain.line(xx1, y + desc,
359                                      xx1 + 4, y + desc, colour);
360                         pi.pain.line(xx2 - 4, y + desc,
361                                      xx2, y + desc, colour);
362                 }
363                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
364                              y + desc - 4, colour);
365
366                 // the label below the text. Can be toggled.
367                 if (g == SubLabel) {
368                         FontInfo font(getLabelfont());
369                         if (pi.change.changed())
370                                 font.setPaintColor(colour);
371                         font.realize(sane_font);
372                         font.decSize();
373                         font.decSize();
374                         int w = 0;
375                         int a = 0;
376                         int d = 0;
377                         Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
378                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
379                         int const ww = max(textdim.wid, w);
380                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
381                                          buttonLabel(bv), font, col, Color_none);
382                 }
383
384                 int const y1 = y - textdim.asc + 3;
385                 // a visual cue when the cursor is inside the inset
386                 Cursor const & cur = bv.cursor();
387                 if (cur.isInside(this)) {
388                         pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
389                         pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
390                         pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
391                         pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
392                 }
393                 // Strike through the inset if deleted and not already handled by
394                 // RowPainter.
395                 if (pi.change.deleted() && canPaintChange(bv))
396                         pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
397                 break;
398         }
399 }
400
401
402 void InsetCollapsible::cursorPos(BufferView const & bv,
403                 CursorSlice const & sl, bool boundary, int & x, int & y) const
404 {
405         if (geometry(bv) == ButtonOnly)
406                 status_ = Open;
407
408         InsetText::cursorPos(bv, sl, boundary, x, y);
409         Dimension const textdim = dimensionHelper(bv);
410
411         switch (geometry(bv)) {
412         case LeftButton:
413                 x += dimensionCollapsed(bv).wid;
414                 break;
415         case TopButton: {
416                 y += dimensionCollapsed(bv).des + textdim.asc;
417                 break;
418         }
419         case NoButton:
420         case SubLabel:
421         case Corners:
422                 // Do nothing
423                 break;
424         case ButtonOnly:
425                 // Cannot get here
426                 break;
427         }
428 }
429
430
431 bool InsetCollapsible::editable() const
432 {
433         if (tempfile_)
434                 return false;
435         
436         switch (decoration()) {
437         case InsetLayout::CLASSIC:
438         case InsetLayout::MINIMALISTIC:
439                 return status_ == Open;
440         default:
441                 return true;
442         }
443 }
444
445
446 bool InsetCollapsible::descendable(BufferView const & bv) const
447 {
448         if (tempfile_)
449                 return false;
450
451         return geometry(bv) != ButtonOnly;
452 }
453
454
455 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
456 {
457         return view_[&bv].button_dim_.contains(x, y);
458 }
459
460
461 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
462 {
463         odocstringstream label;
464         pos_type const max_length = 15;
465         pos_type const p_siz = paragraphs().begin()->size();
466         pos_type const n = min(max_length, p_siz);
467         pos_type i = 0;
468         pos_type j = 0;
469         for (; i < n && j < p_siz; ++j) {
470                 if (paragraphs().begin()->isDeleted(j))
471                         continue;
472                 if (paragraphs().begin()->isInset(j)) {
473                         if (!paragraphs().begin()->getInset(j)->isChar())
474                                 continue;
475                         paragraphs().begin()->getInset(j)->toString(label);
476                 } else
477                         label.put(paragraphs().begin()->getChar(j));
478                 ++i;
479         }
480         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
481                 label << "...";
482         }
483         return label.str().empty() ? l : label.str();
484 }
485
486
487 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
488 {
489         //lyxerr << "InsetCollapsible: edit left/right" << endl;
490         cur.push(*this);
491         InsetText::edit(cur, front, entry_from);
492 }
493
494
495 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
496 {
497         //lyxerr << "InsetCollapsible: edit xy" << endl;
498         if (geometry(cur.bv()) == ButtonOnly
499                 || !descendable(cur.bv())
500             || (view_[&cur.bv()].button_dim_.contains(x, y)
501                 && geometry(cur.bv()) != NoButton))
502                 return this;
503         cur.push(*this);
504         return InsetText::editXY(cur, x, y);
505 }
506
507
508 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
509 {
510         //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
511         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
512
513         bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
514
515         switch (cmd.action()) {
516         case LFUN_MOUSE_PRESS:
517                 if (hitButton) {
518                         switch (cmd.button()) {
519                         case mouse_button::button1:
520                         case mouse_button::button3:
521                                 // Pass the command to the enclosing InsetText,
522                                 // so that the cursor gets set.
523                                 cur.undispatched();
524                                 break;
525                         case mouse_button::none:
526                         case mouse_button::button2:
527                         case mouse_button::button4:
528                         case mouse_button::button5:
529                                 // Nothing to do.
530                                 cur.noScreenUpdate();
531                                 break;
532                         }
533                 } else if (geometry(cur.bv()) != ButtonOnly)
534                         InsetText::doDispatch(cur, cmd);
535                 else
536                         cur.undispatched();
537                 break;
538
539         case LFUN_MOUSE_DOUBLE:
540         case LFUN_MOUSE_TRIPLE:
541                 if (hitButton)
542                         cur.noScreenUpdate();
543                 else if (geometry(cur.bv()) != ButtonOnly)
544                         InsetText::doDispatch(cur, cmd);
545                 else
546                         cur.undispatched();
547                 break;
548
549         case LFUN_MOUSE_RELEASE:
550                 if (!hitButton) {
551                         // The mouse click has to be within the inset!
552                         if (geometry(cur.bv()) != ButtonOnly)
553                                 InsetText::doDispatch(cur, cmd);
554                         else
555                                 cur.undispatched();
556                         break;
557                 }
558                 if (cmd.button() != mouse_button::button1) {
559                         // Nothing to do.
560                         cur.noScreenUpdate();
561                         break;
562                 }
563                 // if we are selecting, we do not want to
564                 // toggle the inset.
565                 if (cur.selection())
566                         break;
567                 // Left button is clicked, the user asks to
568                 // toggle the inset visual state.
569                 cur.dispatched();
570                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
571                 if (geometry(cur.bv()) == ButtonOnly) {
572                         setStatus(cur, Open);
573                         edit(cur, true);
574                 }
575                 else
576                         setStatus(cur, Collapsed);
577                 cur.bv().cursor() = cur;
578                 break;
579
580         case LFUN_INSET_TOGGLE:
581                 if (cmd.argument() == "open")
582                         setStatus(cur, Open);
583                 else if (cmd.argument() == "close")
584                         setStatus(cur, Collapsed);
585                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
586                         if (status_ == Open)
587                                 setStatus(cur, Collapsed);
588                         else
589                                 setStatus(cur, Open);
590                 else // if assign or anything else
591                         cur.undispatched();
592                 cur.dispatched();
593                 break;
594
595         case LFUN_INSET_EDIT: {
596                 cur.push(*this);
597                 text().selectAll(cur);
598                 string const format =
599                         cur.buffer()->params().documentClass().outputFormat();
600                 string const ext = theFormats().extension(format);
601                 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
602                 support::FileName const tempfilename = tempfile_->name();
603                 string const name = tempfilename.toFilesystemEncoding();
604                 ofdocstream os(name.c_str());
605                 os << cur.selectionAsString(false);
606                 os.close();
607                 // Since we lock the inset while the external file is edited,
608                 // we need to move the cursor outside and clear any selection inside
609                 cur.clearSelection();
610                 cur.pop();
611                 cur.leaveInset(*this);
612                 theFormats().edit(buffer(), tempfilename, format);
613                 break;
614         }
615         case LFUN_INSET_END_EDIT: {
616                 support::FileName const tempfilename = tempfile_->name();
617                 docstring const s = tempfilename.fileContents("UTF-8");
618                 cur.recordUndoInset(this);
619                 cur.push(*this);
620                 text().selectAll(cur);
621                 cap::replaceSelection(cur);
622                 cur.text()->insertStringAsLines(cur, s, cur.current_font);
623                 // FIXME (gb) it crashes without this
624                 cur.fixIfBroken();
625                 tempfile_.reset();
626                 cur.pop();
627                 break;
628         }
629
630         default:
631                 InsetText::doDispatch(cur, cmd);
632                 break;
633         }
634 }
635
636
637 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
638                 FuncStatus & flag) const
639 {
640         switch (cmd.action()) {
641         case LFUN_INSET_TOGGLE:
642                 if (cmd.argument() == "open")
643                         flag.setEnabled(status_ != Open);
644                 else if (cmd.argument() == "close")
645                         flag.setEnabled(status_ == Open);
646                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
647                         flag.setEnabled(true);
648                         flag.setOnOff(status_ == Open);
649                 } else
650                         flag.setEnabled(false);
651                 return true;
652
653         case LFUN_INSET_EDIT:
654                 flag.setEnabled(getLayout().editExternally() && tempfile_ == nullptr);
655                 return true;
656
657         case LFUN_INSET_END_EDIT:
658                 flag.setEnabled(getLayout().editExternally() && tempfile_ != nullptr);
659                 return true;
660
661         default:
662                 return InsetText::getStatus(cur, cmd, flag);
663         }
664 }
665
666
667 void InsetCollapsible::setLabel(docstring const & l)
668 {
669         labelstring_ = l;
670 }
671
672
673 docstring InsetCollapsible::getLabel() const
674 {
675         InsetLayout const & il = getLayout();
676         return labelstring_.empty() ?
677                 translateIfPossible(il.labelstring()) : labelstring_;
678 }
679
680
681 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
682 {
683         // U+1F512 LOCK
684         docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
685         if (decoration() == InsetLayout::MINIMALISTIC)
686                 return locked;
687         // indicate changed content in label (#8645)
688         // ✎ U+270E LOWER RIGHT PENCIL
689         docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
690                 ? docstring(1, 0x270E) : docstring();
691         InsetLayout const & il = getLayout();
692         docstring const label = getLabel();
693         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
694                 return indicator + label;
695         return locked + indicator + getNewLabel(label);
696 }
697
698
699 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
700 {
701         status_ = status;
702         setButtonLabel();
703         if (status_ == Collapsed)
704                 cur.leaveInset(*this);
705 }
706
707
708 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
709 {
710         InsetLayout::InsetDecoration const dec = getLayout().decoration();
711         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
712 }
713
714
715 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
716         int y) const
717 {
718         string context_menu = contextMenuName();
719         string const it_context_menu = InsetText::contextMenuName();
720         if (decoration() == InsetLayout::CONGLOMERATE)
721                 return context_menu + ";" + it_context_menu;
722
723         string const ic_context_menu = InsetCollapsible::contextMenuName();
724         if (ic_context_menu != context_menu)
725                 context_menu += ";" + ic_context_menu;
726
727         if (geometry(bv) == NoButton)
728                 return context_menu + ";" + it_context_menu;
729
730         Dimension dim = dimensionCollapsed(bv);
731         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
732                 return context_menu;
733
734         return it_context_menu;
735 }
736
737
738 string InsetCollapsible::contextMenuName() const
739 {
740         if (decoration() == InsetLayout::CONGLOMERATE)
741                 return "context-conglomerate";
742         else
743                 return "context-collapsible";
744 }
745
746
747 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
748 {
749         // return false to let RowPainter draw the change tracking cue consistently
750         // with the surrounding text, when the inset is inline: for buttons, for
751         // non-allowMultiPar insets.
752         switch (geometry(bv)) {
753         case Corners:
754         case SubLabel:
755                 return allowMultiPar();
756         case ButtonOnly:
757                 return false;
758         default:
759                 break;
760         }
761         return true;
762 }
763
764
765 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
766                                 UpdateType utype, TocBackend & backend) const
767 {
768         bool doing_output = output_active && producesOutput();
769         InsetLayout const & layout = getLayout();
770         if (!layout.addToToc())
771                 return InsetText::addToToc(cpit, doing_output, utype, backend);
772
773         TocBuilder & b = backend.builder(layout.tocType());
774         // Cursor inside the inset
775         DocIterator pit = cpit;
776         pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
777         docstring const label = getLabel();
778         b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
779         // Proceed with the rest of the inset.
780         InsetText::addToToc(cpit, doing_output, utype, backend);
781         if (layout.isTocCaption()) {
782                 docstring str;
783                 text().forOutliner(str, TOC_ENTRY_LENGTH);
784                 b.argumentItem(str);
785         }
786         b.pop();
787 }
788
789
790
791 } // namespace lyx