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