]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsible.cpp
Properly terminate command
[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 "OutputParams.h"
30 #include "TextClass.h"
31 #include "TocBackend.h"
32
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
35
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"
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 const & 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 ? NoButton : ButtonOnly ;
105
106         case InsetLayout::CONGLOMERATE:
107                 return status(bv) == Open ? SubLabel : Corners ;
108
109         case InsetLayout::DEFAULT:
110                 break; // this shouldn't happen
111         }
112
113         // dummy return value to shut down a warning,
114         // this is dead code.
115         return NoButton;
116 }
117
118
119 docstring InsetCollapsible::toolTip(BufferView const & bv, int x, int y) const
120 {
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))
125                 return docstring();
126
127         return toolTipText();
128 }
129
130
131 void InsetCollapsible::write(ostream & os) const
132 {
133         os << "status ";
134         switch (status_) {
135         case Open:
136                 os << "open";
137                 break;
138         case Collapsed:
139                 os << "collapsed";
140                 break;
141         }
142         os << "\n";
143         text().write(os);
144 }
145
146
147 void InsetCollapsible::read(Lexer & lex)
148 {
149         lex.setContext("InsetCollapsible::read");
150         string tmp_token;
151         status_ = Collapsed;
152         lex >> "status" >> tmp_token;
153         if (tmp_token == "open")
154                 status_ = Open;
155
156         InsetText::read(lex);
157         setButtonLabel();
158 }
159
160
161 Dimension InsetCollapsible::dimensionCollapsed(BufferView const & bv) const
162 {
163         Dimension dim;
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);
168         return dim;
169 }
170
171
172 void InsetCollapsible::metrics(MetricsInfo & mi, Dimension & dim) const
173 {
174         view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
175
176         FontInfo tmpfont = mi.base.font;
177         mi.base.font = getFont();
178         mi.base.font.realize(tmpfont);
179
180         BufferView const & bv = *mi.base.bv;
181
182         switch (geometry(bv)) {
183         case NoButton:
184                 InsetText::metrics(mi, dim);
185                 break;
186         case Corners:
187                 InsetText::metrics(mi, dim);
188                 dim.des -= 3;
189                 dim.asc -= 1;
190                 break;
191         case SubLabel: {
192                 InsetText::metrics(mi, dim);
193                 // consider width of the inset label
194                 FontInfo font(getLabelfont());
195                 font.realize(sane_font);
196                 font.decSize();
197                 font.decSize();
198                 int w = 0;
199                 int a = 0;
200                 int d = 0;
201                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
202                 dim.des += a + d;
203                 break;
204                 }
205         case TopButton:
206         case LeftButton:
207         case ButtonOnly:
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;
212                 }
213                 dim = dimensionCollapsed(bv);
214                 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
215                         Dimension textdim;
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;
223                         } else {
224                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
225                                 dim.wid = max(dim.wid, textdim.wid);
226                         }
227                 }
228                 break;
229         }
230
231         mi.base.font = tmpfont;
232 }
233
234
235 bool InsetCollapsible::setMouseHover(BufferView const * bv, bool mouse_hover)
236         const
237 {
238         view_[bv].mouse_hover_ = mouse_hover;
239         return true;
240 }
241
242
243 void InsetCollapsible::draw(PainterInfo & pi, int x, int y) const
244 {
245         BufferView const & bv = *pi.base.bv;
246
247         view_[&bv].auto_open_ = bv.cursor().isInside(this);
248
249         Changer dummy = pi.base.font.change(getFont(), true);
250
251         // Draw button first -- top, left or only
252         Dimension dimc = dimensionCollapsed(bv);
253
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;
261
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
269                 // takes care of it.
270                 if (canPaintChange(bv))
271                         pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
272         } else {
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;
277         }
278
279         Dimension const textdim = dimensionHelper(bv);
280         int const baseline = y;
281         int textx, texty;
282         Geometry g = geometry(bv);
283         switch (g) {
284         case LeftButton:
285         case TopButton: {
286                 if (g == LeftButton) {
287                         textx = x + dimc.width();
288                         texty = baseline;
289                 } else {
290                         textx = x;
291                         texty = baseline + dimc.des + textdim.asc;
292                 }
293                 // Do not draw the cue for INSERTED -- it is already in the button and
294                 // that's enough.
295                 Changer cdummy = (pi.change_.type == Change::INSERTED)
296                         ? make_change(pi.change_, Change())
297                         : Changer();
298                 InsetText::draw(pi, textx, texty);
299                 break;
300         }
301         case ButtonOnly:
302                 break;
303         case NoButton:
304                 textx = x;
305                 texty = baseline;
306                 InsetText::draw(pi, textx, texty);
307                 break;
308         case SubLabel:
309         case Corners:
310                 textx = x;
311                 texty = baseline;
312                 // We will take care of the frame and the change tracking cue
313                 // ourselves, below.
314                 {
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);
319                 }
320
321                 int desc = textdim.descent();
322                 if (g == Corners)
323                         desc -= 3;
324
325                 // Colour the frame according to the change type. (Like for tables.)
326                 Color colour = pi.change_.changed() ? pi.change_.color()
327                                                     : Color_foreground;
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);
332                 if (status_ == Open)
333                         pi.pain.line(xx1, y + desc,
334                                      xx2, y + desc, colour);
335                 else {
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);
341                 }
342                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
343                              y + desc - 4, colour);
344
345                 // the label below the text. Can be toggled.
346                 if (g == SubLabel) {
347                         FontInfo font(getLabelfont());
348                         if (pi.change_.changed())
349                                 font.setPaintColor(colour);
350                         font.realize(sane_font);
351                         font.decSize();
352                         font.decSize();
353                         int w = 0;
354                         int a = 0;
355                         int d = 0;
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);
361                 }
362
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);
371                 }
372                 // Strike through the inset if deleted and not already handled by
373                 // RowPainter.
374                 if (pi.change_.deleted() && canPaintChange(bv))
375                         pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
376                 break;
377         }
378 }
379
380
381 void InsetCollapsible::cursorPos(BufferView const & bv,
382                 CursorSlice const & sl, bool boundary, int & x, int & y) const
383 {
384         if (geometry(bv) == ButtonOnly)
385                 status_ = Open;
386
387         InsetText::cursorPos(bv, sl, boundary, x, y);
388         Dimension const textdim = dimensionHelper(bv);
389
390         switch (geometry(bv)) {
391         case LeftButton:
392                 x += dimensionCollapsed(bv).wid;
393                 break;
394         case TopButton: {
395                 y += dimensionCollapsed(bv).des + textdim.asc;
396                 break;
397         }
398         case NoButton:
399         case SubLabel:
400         case Corners:
401                 // Do nothing
402                 break;
403         case ButtonOnly:
404                 // Cannot get here
405                 break;
406         }
407 }
408
409
410 bool InsetCollapsible::editable() const
411 {
412         if (tempfile_)
413                 return false;
414         
415         switch (decoration()) {
416         case InsetLayout::CLASSIC:
417         case InsetLayout::MINIMALISTIC:
418                 return status_ == Open;
419         default:
420                 return true;
421         }
422 }
423
424
425 bool InsetCollapsible::descendable(BufferView const & bv) const
426 {
427         if (tempfile_)
428                 return false;
429
430         return geometry(bv) != ButtonOnly;
431 }
432
433
434 bool InsetCollapsible::clickable(BufferView const & bv, int x, int y) const
435 {
436         return view_[&bv].button_dim_.contains(x, y);
437 }
438
439
440 docstring const InsetCollapsible::getNewLabel(docstring const & l) const
441 {
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);
446         pos_type i = 0;
447         pos_type j = 0;
448         for (; i < n && j < p_siz; ++j) {
449                 if (paragraphs().begin()->isDeleted(j))
450                         continue;
451                 if (paragraphs().begin()->isInset(j)) {
452                         if (!paragraphs().begin()->getInset(j)->isChar())
453                                 continue;
454                         paragraphs().begin()->getInset(j)->toString(label);
455                 } else
456                         label.put(paragraphs().begin()->getChar(j));
457                 ++i;
458         }
459         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
460                 label << "...";
461         }
462         docstring const lbl = label.str();
463         return lbl.empty() ? l : lbl;
464 }
465
466
467 void InsetCollapsible::edit(Cursor & cur, bool front, EntryDirection entry_from)
468 {
469         //lyxerr << "InsetCollapsible: edit left/right" << endl;
470         cur.push(*this);
471         InsetText::edit(cur, front, entry_from);
472 }
473
474
475 Inset * InsetCollapsible::editXY(Cursor & cur, int x, int y)
476 {
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))
482                 return this;
483         cur.push(*this);
484         return InsetText::editXY(cur, x, y);
485 }
486
487
488 void InsetCollapsible::doDispatch(Cursor & cur, FuncRequest & cmd)
489 {
490         //lyxerr << "InsetCollapsible::doDispatch (begin): cmd: " << cmd
491         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
492
493         bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
494
495         switch (cmd.action()) {
496         case LFUN_MOUSE_PRESS:
497                 if (hitButton) {
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.
503                                 cur.undispatched();
504                                 break;
505                         case mouse_button::none:
506                         case mouse_button::button2:
507                         case mouse_button::button4:
508                         case mouse_button::button5:
509                                 // Nothing to do.
510                                 cur.noScreenUpdate();
511                                 break;
512                         }
513                 } else if (geometry(cur.bv()) != ButtonOnly)
514                         InsetText::doDispatch(cur, cmd);
515                 else
516                         cur.undispatched();
517                 break;
518
519         case LFUN_MOUSE_DOUBLE:
520         case LFUN_MOUSE_TRIPLE:
521                 if (hitButton)
522                         cur.noScreenUpdate();
523                 else if (geometry(cur.bv()) != ButtonOnly)
524                         InsetText::doDispatch(cur, cmd);
525                 else
526                         cur.undispatched();
527                 break;
528
529         case LFUN_MOUSE_RELEASE:
530                 if (!hitButton) {
531                         // The mouse click has to be within the inset!
532                         if (geometry(cur.bv()) != ButtonOnly)
533                                 InsetText::doDispatch(cur, cmd);
534                         else
535                                 cur.undispatched();
536                         break;
537                 }
538                 if (cmd.button() != mouse_button::button1) {
539                         // Nothing to do.
540                         cur.noScreenUpdate();
541                         break;
542                 }
543                 // if we are selecting, we do not want to
544                 // toggle the inset.
545                 if (cur.selection())
546                         break;
547                 // Left button is clicked, the user asks to
548                 // toggle the inset visual state.
549                 cur.dispatched();
550                 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
551                 if (geometry(cur.bv()) == ButtonOnly) {
552                         setStatus(cur, Open);
553                         edit(cur, true);
554                 }
555                 else
556                         setStatus(cur, Collapsed);
557                 cur.bv().cursor() = cur;
558                 break;
559
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())
566                         if (status_ == Open)
567                                 setStatus(cur, Collapsed);
568                         else
569                                 setStatus(cur, Open);
570                 else // if assign or anything else
571                         cur.undispatched();
572                 cur.dispatched();
573                 break;
574
575         case LFUN_INSET_EDIT: {
576                 cur.push(*this);
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);
586                 os.close();
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();
590                 cur.pop();
591                 cur.leaveInset(*this);
592                 theFormats().edit(buffer(), tempfilename, format);
593                 break;
594         }
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);
599                 cur.push(*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
604                 cur.fixIfBroken();
605                 tempfile_.reset();
606                 cur.pop();
607                 break;
608         }
609
610         default:
611                 InsetText::doDispatch(cur, cmd);
612                 break;
613         }
614 }
615
616
617 bool InsetCollapsible::getStatus(Cursor & cur, FuncRequest const & cmd,
618                 FuncStatus & flag) const
619 {
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);
629                 } else
630                         flag.setEnabled(false);
631                 return true;
632
633         case LFUN_INSET_EDIT:
634                 flag.setEnabled(getLayout().editExternally() && tempfile_ == 0);
635                 return true;
636
637         case LFUN_INSET_END_EDIT:
638                 flag.setEnabled(getLayout().editExternally() && tempfile_ != 0);
639                 return true;
640
641         default:
642                 return InsetText::getStatus(cur, cmd, flag);
643         }
644 }
645
646
647 void InsetCollapsible::setLabel(docstring const & l)
648 {
649         labelstring_ = l;
650 }
651
652
653 docstring InsetCollapsible::getLabel() const
654 {
655         InsetLayout const & il = getLayout();
656         return labelstring_.empty() ?
657                 translateIfPossible(il.labelstring()) : labelstring_;
658 }
659
660
661 docstring const InsetCollapsible::buttonLabel(BufferView const & bv) const
662 {
663         InsetLayout const & il = getLayout();
664         docstring const label = getLabel();
665         if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
666                 return label;
667         return getNewLabel(label);
668 }
669
670
671 void InsetCollapsible::setStatus(Cursor & cur, CollapseStatus status)
672 {
673         status_ = status;
674         setButtonLabel();
675         if (status_ == Collapsed)
676                 cur.leaveInset(*this);
677 }
678
679
680 InsetLayout::InsetDecoration InsetCollapsible::decoration() const
681 {
682         InsetLayout::InsetDecoration const dec = getLayout().decoration();
683         return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
684 }
685
686
687 string InsetCollapsible::contextMenu(BufferView const & bv, int x,
688         int y) const
689 {
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;
694
695         string const ic_context_menu = InsetCollapsible::contextMenuName();
696         if (ic_context_menu != context_menu)
697                 context_menu += ";" + ic_context_menu;
698
699         if (geometry(bv) == NoButton)
700                 return context_menu + ";" + it_context_menu;
701
702         Dimension dim = dimensionCollapsed(bv);
703         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
704                 return context_menu;
705
706         return it_context_menu;
707 }
708
709
710 string InsetCollapsible::contextMenuName() const
711 {
712         if (decoration() == InsetLayout::CONGLOMERATE)
713                 return "context-conglomerate";
714         else
715                 return "context-collapsible";
716 }
717
718
719 bool InsetCollapsible::canPaintChange(BufferView const & bv) const
720 {
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)) {
725         case Corners:
726         case SubLabel:
727                 return allowMultiPar();
728         case ButtonOnly:
729                 return false;
730         default:
731                 break;
732         }
733         return true;
734 }
735
736
737 void InsetCollapsible::addToToc(DocIterator const & cpit, bool output_active,
738                                 UpdateType utype, TocBackend & backend) const
739 {
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);
744
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()) {
754                 docstring str;
755                 text().forOutliner(str, TOC_ENTRY_LENGTH);
756                 b.argumentItem(str);
757         }
758         b.pop();
759 }
760
761
762
763 } // namespace lyx