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