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