]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsible.cpp
Move code responsible to set TextMetrics outer font to InsetText
[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() == InsetDecoration::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 InsetDecoration::CLASSIC:
99                 if (status(bv) == Open)
100                         return view_[&bv].openinlined_ ? LeftButton : TopButton;
101                 return ButtonOnly;
102
103         case InsetDecoration::MINIMALISTIC: {
104                 return status(bv) == Open ?
105                         (tempfile_ ? LeftButton : NoButton)
106                         : ButtonOnly;
107         }
108
109         case InsetDecoration::CONGLOMERATE:
110                 return status(bv) == Open ? SubLabel : Corners ;
111
112         case InsetDecoration::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         int const offset =
191                 (geometry(bv) != LeftButton && geometry(bv) != TopButton)
192                 ? Inset::textOffset(&bv) : 0;
193         labelfont.realize(sane_font);
194         theFontMetrics(labelfont).buttonText(
195                 buttonLabel(bv), offset, dim.wid, dim.asc, dim.des);
196         return dim;
197 }
198
199
200 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
201 {
202         view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
203
204         BufferView const & bv = *mi.base.bv;
205
206         switch (geometry(bv)) {
207         case NoButton:
208                 InsetText::metrics(mi, dim);
209                 break;
210         case Corners:
211                 InsetText::metrics(mi, dim);
212                 break;
213         case SubLabel: {
214                 InsetText::metrics(mi, dim);
215                 // consider width of the inset label
216                 FontInfo font(getLabelfont());
217                 font.realize(sane_font);
218                 font.decSize();
219                 font.decSize();
220                 int w = 0;
221                 int a = 0;
222                 int d = 0;
223                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
224                 dim.des += a + d;
225                 dim.wid = max(dim.wid, w);
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
255
256 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
257         const
258 {
259         view_[bv].mouse_hover_ = mouse_hover;
260         return true;
261 }
262
263
264 ColorCode InsetCollapsible::backgroundColor(PainterInfo const &) const
265 {
266         return getLayout().bgcolor();
267 }
268
269
270 ColorCode InsetCollapsible::labelColor() const
271 {
272         return getLayout().labelfont().color();
273 }
274
275
276 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
277 {
278         BufferView const & bv = *pi.base.bv;
279
280         view_[&bv].auto_open_ = bv.cursor().isInside(this);
281
282         Changer dummy = pi.base.font.change(getFont(), true);
283
284         // Draw button first -- top, left or only
285         Dimension dimc = dimensionCollapsed(bv);
286
287         if (geometry(bv) == TopButton ||
288             geometry(bv) == LeftButton ||
289             geometry(bv) == ButtonOnly) {
290                 view_[&bv].button_dim_.x1 = x + 0;
291                 view_[&bv].button_dim_.x2 = x + dimc.width();
292                 view_[&bv].button_dim_.y1 = y - dimc.asc;
293                 view_[&bv].button_dim_.y2 = y + dimc.des;
294
295                 FontInfo labelfont = getLabelfont();
296                 labelfont.setColor(labelColor());
297                 labelfont.realize(pi.base.font);
298                 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
299                                    view_[&bv].mouse_hover_ ? Color_buttonhoverbg : Color_buttonbg,
300                                    Color_buttonframe, Inset::textOffset(pi.base.bv));
301                 // Draw the change tracking cue on the label, unless RowPainter already
302                 // takes care of it.
303                 if (canPaintChange(bv))
304                         pi.change.paintCue(pi, x, y, x + dimc.width(), labelfont);
305         } else {
306                 view_[&bv].button_dim_.x1 = 0;
307                 view_[&bv].button_dim_.y1 = 0;
308                 view_[&bv].button_dim_.x2 = 0;
309                 view_[&bv].button_dim_.y2 = 0;
310         }
311
312         Dimension const textdim = dimensionHelper(bv);
313         int const baseline = y;
314         int textx, texty;
315         Geometry g = geometry(bv);
316         switch (g) {
317         case LeftButton:
318         case TopButton: {
319                 if (g == LeftButton) {
320                         textx = x + dimc.width();
321                         texty = baseline;
322                 } else {
323                         textx = x;
324                         texty = baseline + dimc.des + textdim.asc;
325                 }
326                 // Do not draw the cue for INSERTED -- it is already in the button and
327                 // that's enough.
328                 Changer cdummy = (pi.change.type == Change::INSERTED)
329                         ? changeVar(pi.change, Change())
330                         : noChange();
331                 InsetText::draw(pi, textx, texty);
332                 break;
333         }
334         case ButtonOnly:
335                 break;
336         case NoButton:
337                 textx = x;
338                 texty = baseline;
339                 InsetText::draw(pi, textx, texty);
340                 break;
341         case SubLabel:
342         case Corners:
343                 textx = x;
344                 texty = baseline;
345                 // We will take care of the frame and the change tracking cue
346                 // ourselves, below.
347                 {
348                         Changer cdummy = changeVar(pi.change, Change());
349                         const_cast<InsetCollapsible *>(this)->setDrawFrame(false);
350                         InsetText::draw(pi, textx, texty);
351                         const_cast<InsetCollapsible *>(this)->setDrawFrame(true);
352                 }
353
354                 int desc = textdim.descent();
355
356                 // Colour the frame according to the change type. (Like for tables.)
357                 Color colour = pi.change.changed() ? pi.change.color()
358                                                     : Color_foreground;
359                 const int xx1 = x + leftOffset(pi.base.bv) - 1;
360                 const int xx2 = x + textdim.wid - rightOffset(pi.base.bv) + 1;
361                 pi.pain.line(xx1, y + desc - 4,
362                              xx1, y + desc, colour);
363                 if (status_ == Open)
364                         pi.pain.line(xx1, y + desc,
365                                      xx2, y + desc, colour);
366                 else {
367                         // Make status_ value visible:
368                         pi.pain.line(xx1, y + desc,
369                                      xx1 + 4, y + desc, colour);
370                         pi.pain.line(xx2 - 4, y + desc,
371                                      xx2, y + desc, colour);
372                 }
373                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
374                              y + desc - 4, colour);
375
376                 // the label below the text. Can be toggled.
377                 if (g == SubLabel) {
378                         FontInfo font(getLabelfont());
379                         if (pi.change.changed())
380                                 font.setPaintColor(colour);
381                         font.realize(sane_font);
382                         font.decSize();
383                         font.decSize();
384                         int w = 0;
385                         int a = 0;
386                         int d = 0;
387                         Color const col = pi.full_repaint ? Color_none : pi.backgroundColor();
388                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
389                         int const ww = max(textdim.wid, w);
390                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
391                                          buttonLabel(bv), font, col, Color_none);
392                 }
393
394                 int const y1 = y - textdim.asc + 3;
395                 // a visual cue when the cursor is inside the inset
396                 Cursor const & cur = bv.cursor();
397                 if (cur.isInside(this)) {
398                         pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
399                         pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
400                         pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
401                         pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
402                 }
403                 // Strike through the inset if deleted and not already handled by
404                 // RowPainter.
405                 if (pi.change.deleted() && canPaintChange(bv))
406                         pi.change.paintCue(pi, xx1, y1, xx2, y + desc);
407                 break;
408         }
409 }
410
411
412 void InsetCollapsible::cursorPos(BufferView const & bv,
413                 CursorSlice const & sl, bool boundary, int & x, int & y) const
414 {
415         if (geometry(bv) == ButtonOnly)
416                 status_ = Open;
417
418         InsetText::cursorPos(bv, sl, boundary, x, y);
419         Dimension const textdim = dimensionHelper(bv);
420
421         switch (geometry(bv)) {
422         case LeftButton:
423                 x += dimensionCollapsed(bv).wid;
424                 break;
425         case TopButton: {
426                 y += dimensionCollapsed(bv).des + textdim.asc;
427                 break;
428         }
429         case NoButton:
430         case SubLabel:
431         case Corners:
432                 // Do nothing
433                 break;
434         case ButtonOnly:
435                 // Cannot get here
436                 break;
437         }
438 }
439
440
441 bool InsetCollapsible::editable() const
442 {
443         if (tempfile_)
444                 return false;
445         
446         switch (decoration()) {
447         case InsetDecoration::CLASSIC:
448         case InsetDecoration::MINIMALISTIC:
449                 return status_ == Open;
450         default:
451                 return true;
452         }
453 }
454
455
456 bool InsetCollapsible::descendable(BufferView const & bv) const
457 {
458         if (tempfile_)
459                 return false;
460
461         return geometry(bv) != ButtonOnly;
462 }
463
464
465 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
466 {
467         return view_[&bv].button_dim_.contains(x, y);
468 }
469
470
471 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
472 {
473         odocstringstream label;
474         pos_type const max_length = 15;
475         pos_type const p_siz = paragraphs().begin()->size();
476         pos_type const n = min(max_length, p_siz);
477         pos_type i = 0;
478         pos_type j = 0;
479         for (; i < n && j < p_siz; ++j) {
480                 if (paragraphs().begin()->isDeleted(j))
481                         continue;
482                 if (paragraphs().begin()->isInset(j)) {
483                         if (!paragraphs().begin()->getInset(j)->isChar())
484                                 continue;
485                         paragraphs().begin()->getInset(j)->toString(label);
486                 } else
487                         label.put(paragraphs().begin()->getChar(j));
488                 ++i;
489         }
490         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
491                 label << "...";
492         }
493         return label.str().empty() ? l : label.str();
494 }
495
496
497 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
498 {
499         //lyxerr << "InsetCollapsible: edit left/right" << endl;
500         cur.push(*this);
501         InsetText::edit(cur, front, entry_from);
502 }
503
504
505 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
506 {
507         //lyxerr << "InsetCollapsible: edit xy" << endl;
508         if (geometry(cur.bv()) == ButtonOnly
509                 || !descendable(cur.bv())
510             || (view_[&cur.bv()].button_dim_.contains(x, y)
511                 && geometry(cur.bv()) != NoButton))
512                 return this;
513         cur.push(*this);
514         return InsetText::editXY(cur, x, y);
515 }
516
517
518 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
519 {
520         //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
521         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
522
523         bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
524
525         switch (cmd.action()) {
526         case LFUN_MOUSE_PRESS:
527                 if (hitButton) {
528                         switch (cmd.button()) {
529                         case mouse_button::button1:
530                         case mouse_button::button3:
531                                 // Pass the command to the enclosing InsetText,
532                                 // so that the cursor gets set.
533                                 cur.undispatched();
534                                 break;
535                         case mouse_button::none:
536                         case mouse_button::button2:
537                         case mouse_button::button4:
538                         case mouse_button::button5:
539                                 // Nothing to do.
540                                 cur.noScreenUpdate();
541                                 break;
542                         }
543                 } else if (geometry(cur.bv()) != ButtonOnly)
544                         InsetText::doDispatch(cur, cmd);
545                 else
546                         cur.undispatched();
547                 break;
548
549         case LFUN_MOUSE_DOUBLE:
550         case LFUN_MOUSE_TRIPLE:
551                 if (hitButton)
552                         cur.noScreenUpdate();
553                 else if (geometry(cur.bv()) != ButtonOnly)
554                         InsetText::doDispatch(cur, cmd);
555                 else
556                         cur.undispatched();
557                 break;
558
559         case LFUN_MOUSE_RELEASE:
560                 if (!hitButton) {
561                         // The mouse click has to be within the inset!
562                         if (geometry(cur.bv()) != ButtonOnly)
563                                 InsetText::doDispatch(cur, cmd);
564                         else
565                                 cur.undispatched();
566                         break;
567                 }
568                 if (cmd.button() != mouse_button::button1) {
569                         // Nothing to do.
570                         cur.noScreenUpdate();
571                         break;
572                 }
573                 // if we are selecting, we do not want to
574                 // toggle the inset.
575                 if (cur.selection())
576                         break;
577                 // Left button is clicked, the user asks to
578                 // toggle the inset visual state.
579                 cur.dispatched();
580                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
581                 if (geometry(cur.bv()) == ButtonOnly) {
582                         setStatus(cur, Open);
583                         edit(cur, true);
584                 }
585                 else
586                         setStatus(cur, Collapsed);
587                 cur.bv().cursor() = cur;
588                 break;
589
590         case LFUN_INSET_TOGGLE:
591                 if (cmd.argument() == "open")
592                         setStatus(cur, Open);
593                 else if (cmd.argument() == "close")
594                         setStatus(cur, Collapsed);
595                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
596                         if (status_ == Open)
597                                 setStatus(cur, Collapsed);
598                         else
599                                 setStatus(cur, Open);
600                 else // if assign or anything else
601                         cur.undispatched();
602                 cur.dispatched();
603                 break;
604
605         case LFUN_INSET_EDIT: {
606                 cur.push(*this);
607                 text().selectAll(cur);
608                 string const format =
609                         cur.buffer()->params().documentClass().outputFormat();
610                 string const ext = theFormats().extension(format);
611                 tempfile_.reset(new support::TempFile("ert_editXXXXXX." + ext));
612                 support::FileName const tempfilename = tempfile_->name();
613                 string const name = tempfilename.toFilesystemEncoding();
614                 ofdocstream os(name.c_str());
615                 os << cur.selectionAsString(false);
616                 os.close();
617                 // Since we lock the inset while the external file is edited,
618                 // we need to move the cursor outside and clear any selection inside
619                 cur.clearSelection();
620                 cur.pop();
621                 cur.leaveInset(*this);
622                 theFormats().edit(buffer(), tempfilename, format);
623                 break;
624         }
625         case LFUN_INSET_END_EDIT: {
626                 support::FileName const tempfilename = tempfile_->name();
627                 docstring const s = tempfilename.fileContents("UTF-8");
628                 cur.recordUndoInset(this);
629                 cur.push(*this);
630                 text().selectAll(cur);
631                 cap::replaceSelection(cur);
632                 cur.text()->insertStringAsLines(cur, s, cur.current_font);
633                 // FIXME (gb) it crashes without this
634                 cur.fixIfBroken();
635                 tempfile_.reset();
636                 cur.pop();
637                 break;
638         }
639
640         default:
641                 InsetText::doDispatch(cur, cmd);
642                 break;
643         }
644 }
645
646
647 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
648                 FuncStatus & flag) const
649 {
650         switch (cmd.action()) {
651         case LFUN_INSET_TOGGLE:
652                 if (cmd.argument() == "open")
653                         flag.setEnabled(status_ != Open);
654                 else if (cmd.argument() == "close")
655                         flag.setEnabled(status_ == Open);
656                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
657                         flag.setEnabled(true);
658                         flag.setOnOff(status_ == Open);
659                 } else
660                         flag.setEnabled(false);
661                 return true;
662
663         case LFUN_INSET_EDIT:
664                 flag.setEnabled(!buffer().hasReadonlyFlag() &&
665                         getLayout().editExternally() && tempfile_ == nullptr);
666                 return true;
667
668         case LFUN_INSET_END_EDIT:
669                 flag.setEnabled(!buffer().hasReadonlyFlag() &&
670                         getLayout().editExternally() && tempfile_ != nullptr);
671                 return true;
672
673         default:
674                 return InsetText::getStatus(cur, cmd, flag);
675         }
676 }
677
678
679 void InsetCollapsible::setLabel(docstring const & l)
680 {
681         labelstring_ = l;
682 }
683
684
685 docstring InsetCollapsible::getLabel() const
686 {
687         InsetLayout const & il = getLayout();
688         return labelstring_.empty() ?
689                 translateIfPossible(il.labelstring()) : labelstring_;
690 }
691
692
693 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
694 {
695         // U+1F512 LOCK
696         docstring const locked = tempfile_ ? docstring(1, 0x1F512) : docstring();
697         // indicate changed content in label (#8645)
698         // ✎ U+270E LOWER RIGHT PENCIL
699         docstring const indicator = (isChanged() && geometry(bv) == ButtonOnly)
700                 ? docstring(1, 0x270E) : docstring();
701         InsetLayout const & il = getLayout();
702         docstring const label = getLabel();
703         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
704                 return locked + indicator + label;
705         return locked + indicator + getNewLabel(label);
706 }
707
708
709 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
710 {
711         status_ = status;
712         setButtonLabel();
713         if (status_ == Collapsed)
714                 cur.leaveInset(*this);
715 }
716
717
718 InsetDecoration InsetCollapsible::decoration() const
719 {
720         InsetDecoration const dec = getLayout().decoration();
721         return dec == InsetDecoration::DEFAULT ? InsetDecoration::CLASSIC : dec;
722 }
723
724
725 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
726         int y) const
727 {
728         string context_menu = contextMenuName();
729         string const it_context_menu = InsetText::contextMenuName();
730         if (decoration() == InsetDecoration::CONGLOMERATE)
731                 return context_menu + ";" + it_context_menu;
732
733         string const ic_context_menu = InsetCollapsible::contextMenuName();
734         if (ic_context_menu != context_menu)
735                 context_menu += ";" + ic_context_menu;
736
737         if (geometry(bv) == NoButton)
738                 return context_menu + ";" + it_context_menu;
739
740         Dimension dim = dimensionCollapsed(bv);
741         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
742                 return context_menu;
743
744         return it_context_menu;
745 }
746
747
748 string InsetCollapsible::contextMenuName() const
749 {
750         if (decoration() == InsetDecoration::CONGLOMERATE)
751                 return "context-conglomerate";
752         else
753                 return "context-collapsible";
754 }
755
756
757 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
758 {
759         // return false to let RowPainter draw the change tracking cue consistently
760         // with the surrounding text, when the inset is inline: for buttons, for
761         // non-allowMultiPar insets.
762         switch (geometry(bv)) {
763         case Corners:
764         case SubLabel:
765                 return allowMultiPar();
766         case ButtonOnly:
767                 return false;
768         default:
769                 break;
770         }
771         return true;
772 }
773
774
775 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
776                                 UpdateType utype, TocBackend & backend) const
777 {
778         bool doing_output = output_active && producesOutput();
779         InsetLayout const & layout = getLayout();
780         if (!layout.addToToc())
781                 return InsetText::addToToc(cpit, doing_output, utype, backend);
782
783         TocBuilder & b = backend.builder(layout.tocType());
784         // Cursor inside the inset
785         DocIterator pit = cpit;
786         pit.push_back(CursorSlice(const_cast<InsetCollapsible &>(*this)));
787         docstring const label = getLabel();
788         b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
789         // Proceed with the rest of the inset.
790         InsetText::addToToc(cpit, doing_output, utype, backend);
791         if (layout.isTocCaption()) {
792                 docstring str;
793                 text().forOutliner(str, TOC_ENTRY_LENGTH);
794                 b.argumentItem(str);
795         }
796         b.pop();
797 }
798
799
800
801 } // namespace lyx