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