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