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