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