]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
* src/insets/InsetCollapsable.cpp:
[lyx.git] / src / insets / InsetCollapsable.cpp
1 /**
2  * \file InsetCollapsable.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 "InsetCollapsable.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "Dimension.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
25 #include "InsetLayout.h"
26 #include "Language.h"
27 #include "LaTeXFeatures.h"
28 #include "Lexer.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
32 #include "TextClass.h"
33
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37 #include "support/assert.h"
38 #include "support/debug.h"
39 #include "support/docstream.h"
40 #include "support/gettext.h"
41
42 using namespace std;
43
44
45 namespace lyx {
46
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
48 {
49         if (decoration() == InsetLayout::Conglomerate)
50                 return status_;
51         return autoOpen_ ? Open : status_;
52 }
53
54
55 InsetCollapsable::Geometry InsetCollapsable::geometry() const
56 {
57         switch (decoration()) {
58         case InsetLayout::Classic:
59                 if (status() == Open)
60                         return openinlined_ ? LeftButton : TopButton;
61                 return ButtonOnly;
62
63         case InsetLayout::Minimalistic:
64                 return status() == Open ? NoButton : ButtonOnly ;
65
66         case InsetLayout::Conglomerate:
67                 return status() == Open ? SubLabel : Corners ;
68
69         case InsetLayout::Default:
70                 break; // this shouldn't happen
71         }
72
73         // dummy return value to shut down a warning,
74         // this is dead code.
75         return NoButton;
76 }
77
78
79 InsetCollapsable::InsetCollapsable(Buffer const & buf,
80                 CollapseStatus status)
81         : InsetText(buf), status_(status),
82           openinlined_(false), autoOpen_(false), mouse_hover_(false)
83 {
84         DocumentClass const & dc = buf.params().documentClass();
85         setLayout(&dc);
86         setAutoBreakRows(true);
87         setDrawFrame(true);
88         setFrameColor(Color_collapsableframe);
89         paragraphs().back().setLayout(dc.emptyLayout());
90 }
91
92
93 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
94         : InsetText(rhs),
95                 layout_(rhs.layout_),
96                 labelstring_(rhs.labelstring_),
97                 button_dim(rhs.button_dim),
98                 status_(rhs.status_),
99                 openinlined_(rhs.openinlined_),
100                 autoOpen_(rhs.autoOpen_),
101                 // the sole purpose of this copy constructor
102                 mouse_hover_(false)
103 {
104 }
105
106
107 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
108 {
109         Dimension dim = dimensionCollapsed();
110         if (geometry() == NoButton)
111                 return translateIfPossible(layout_->labelstring());
112         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des)
113                 return docstring();
114
115         switch (status_) {
116         case Open:
117                 return _("Left-click to collapse the inset");
118         case Collapsed:
119                 return _("Left-click to open the inset");
120         }
121         return docstring();
122 }
123
124
125 void InsetCollapsable::setLayout(BufferParams const & bp)
126 {
127         setLayout(bp.documentClassPtr());
128 }
129
130
131 void InsetCollapsable::setLayout(DocumentClass const * const dc)
132 {
133         if (dc) {
134                 layout_ = &(dc->insetLayout(name()));
135                 labelstring_ = translateIfPossible(layout_->labelstring());
136         } else {
137                 layout_ = &DocumentClass::emptyInsetLayout();
138                 labelstring_ = _("UNDEFINED");
139         }
140
141         setButtonLabel();
142 }
143
144
145 void InsetCollapsable::write(ostream & os) const
146 {
147         os << "status ";
148         switch (status_) {
149         case Open:
150                 os << "open";
151                 break;
152         case Collapsed:
153                 os << "collapsed";
154                 break;
155         }
156         os << "\n";
157         text_.write(buffer(), os);
158 }
159
160
161 void InsetCollapsable::read(Lexer & lex)
162 {
163         lex.setContext("InsetCollapsable::read");
164         string tmp_token;
165         status_ = Collapsed;
166         lex >> "status" >> tmp_token;
167         if (tmp_token == "open")
168                 status_ = Open;
169
170         // this must be set before we enter InsetText::read()
171         setLayout(buffer().params());
172
173         InsetText::read(lex);
174
175         // Force default font, if so requested
176         // This avoids paragraphs in buffer language that would have a
177         // foreign language after a document language change, and it ensures
178         // that all new text in ERT and similar gets the "latex" language,
179         // since new text inherits the language from the last position of the
180         // existing text.  As a side effect this makes us also robust against
181         // bugs in LyX that might lead to font changes in ERT in .lyx files.
182         resetParagraphsFont();
183 }
184
185
186 Dimension InsetCollapsable::dimensionCollapsed() const
187 {
188         LASSERT(layout_, /**/);
189         Dimension dim;
190         theFontMetrics(layout_->labelfont()).buttonText(
191                 labelstring_, dim.wid, dim.asc, dim.des);
192         return dim;
193 }
194
195
196 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
197 {
198         LASSERT(layout_, /**/);
199
200         autoOpen_ = mi.base.bv->cursor().isInside(this);
201
202         FontInfo tmpfont = mi.base.font;
203         mi.base.font = layout_->font();
204         mi.base.font.realize(tmpfont);
205
206         switch (geometry()) {
207         case NoButton:
208                 InsetText::metrics(mi, dim);
209                 break;
210         case Corners:
211                 InsetText::metrics(mi, dim);
212                 dim.des -= 3;
213                 dim.asc -= 1;
214                 break;
215         case SubLabel: {
216                 InsetText::metrics(mi, dim);
217                 // consider width of the inset label
218                 FontInfo font(layout_->labelfont());
219                 font.realize(sane_font);
220                 font.decSize();
221                 font.decSize();
222                 int w = 0;
223                 int a = 0;
224                 int d = 0;
225                 theFontMetrics(font).rectText(labelstring_, w, a, d);
226                 dim.des += a + d;
227                 break;
228                 }
229         case TopButton:
230         case LeftButton:
231         case ButtonOnly:
232                 dim = dimensionCollapsed();
233                 if (geometry() == TopButton || geometry() == LeftButton) {
234                         Dimension textdim;
235                         InsetText::metrics(mi, textdim);
236                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
237                         if (openinlined_) {
238                                 // Correct for button width.
239                                 dim.wid += textdim.wid;
240                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
241                                 dim.asc = textdim.asc;
242                         } else {
243                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
244                                 dim.wid = max(dim.wid, textdim.wid);
245                         }
246                 }
247                 break;
248         }
249
250         mi.base.font = tmpfont;
251 }
252
253
254 bool InsetCollapsable::setMouseHover(bool mouse_hover)
255 {
256         mouse_hover_ = mouse_hover;
257         return true;
258 }
259
260
261 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
262 {
263         LASSERT(layout_, /**/);
264
265         autoOpen_ = pi.base.bv->cursor().isInside(this);
266         ColorCode const old_color = pi.background_color;
267         pi.background_color = backgroundColor();
268
269         FontInfo tmpfont = pi.base.font;
270         pi.base.font = layout_->font();
271         pi.base.font.realize(tmpfont);
272
273         // Draw button first -- top, left or only
274         Dimension dimc = dimensionCollapsed();
275
276         if (geometry() == TopButton ||
277             geometry() == LeftButton ||
278             geometry() == ButtonOnly) {
279                 button_dim.x1 = x + 0;
280                 button_dim.x2 = x + dimc.width();
281                 button_dim.y1 = y - dimc.asc;
282                 button_dim.y2 = y + dimc.des;
283
284                 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
285                         mouse_hover_);
286         } else {
287                 button_dim.x1 = 0;
288                 button_dim.y1 = 0;
289                 button_dim.x2 = 0;
290                 button_dim.y2 = 0;
291         }
292
293         Dimension const textdim = InsetText::dimension(*pi.base.bv);
294         int const baseline = y;
295         int textx, texty;
296         switch (geometry()) {
297         case LeftButton:
298                 textx = x + dimc.width();
299                 texty = baseline;
300                 InsetText::draw(pi, textx, texty);
301                 break;
302         case TopButton:
303                 textx = x;
304                 texty = baseline + dimc.des + textdim.asc;
305                 InsetText::draw(pi, textx, texty);
306                 break;
307         case ButtonOnly:
308                 break;
309         case NoButton:
310                 textx = x;
311                 texty = baseline;
312                 InsetText::draw(pi, textx, texty);
313                 break;
314         case SubLabel:
315         case Corners:
316                 textx = x;
317                 texty = baseline;
318                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
319                 InsetText::draw(pi, textx, texty);
320                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
321
322                 int desc = textdim.descent();
323                 if (geometry() == Corners)
324                         desc -= 3;
325
326                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
327                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
328                 pi.pain.line(xx1, y + desc - 4, 
329                              xx1, y + desc, 
330                         layout_->labelfont().color());
331                 if (status_ == Open)
332                         pi.pain.line(xx1, y + desc, 
333                                 xx2, y + desc,
334                                 layout_->labelfont().color());
335                 else {
336                         // Make status_ value visible:
337                         pi.pain.line(xx1, y + desc,
338                                 xx1 + 4, y + desc,
339                                 layout_->labelfont().color());
340                         pi.pain.line(xx2 - 4, y + desc,
341                                 xx2, y + desc,
342                                 layout_->labelfont().color());
343                 }
344                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
345                         y + desc - 4, layout_->labelfont().color());
346
347                 // the label below the text. Can be toggled.
348                 if (geometry() == SubLabel) {
349                         FontInfo font(layout_->labelfont());
350                         font.realize(sane_font);
351                         font.decSize();
352                         font.decSize();
353                         int w = 0;
354                         int a = 0;
355                         int d = 0;
356                         theFontMetrics(font).rectText(labelstring_, w, a, d);
357                         int const ww = max(textdim.wid, w);
358                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
359                                 labelstring_, font, Color_none, Color_none);
360                         desc += d;
361                 }
362
363                 // a visual cue when the cursor is inside the inset
364                 Cursor & cur = pi.base.bv->cursor();
365                 if (cur.isInside(this)) {
366                         y -= textdim.asc;
367                         y += 3;
368                         pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
369                         pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
370                         pi.pain.line(xx2, y + 4, xx2, y,
371                                 layout_->labelfont().color());
372                         pi.pain.line(xx2 - 4, y, xx2, y,
373                                 layout_->labelfont().color());
374                 }
375                 break;
376         }
377         pi.background_color = old_color;
378
379         pi.base.font = tmpfont;
380 }
381
382
383 void InsetCollapsable::cursorPos(BufferView const & bv,
384                 CursorSlice const & sl, bool boundary, int & x, int & y) const
385 {
386         if (geometry() == ButtonOnly)
387                 status_ = Open;
388         LASSERT(geometry() != ButtonOnly, /**/);
389
390         InsetText::cursorPos(bv, sl, boundary, x, y);
391         Dimension const textdim = InsetText::dimension(bv);
392
393         switch (geometry()) {
394         case LeftButton:
395                 x += dimensionCollapsed().wid;
396                 break;
397         case TopButton: {
398                 y += dimensionCollapsed().des + textdim.asc;
399                 break;
400         }
401         case NoButton:
402         case SubLabel:
403         case Corners:
404                 // Do nothing
405                 break;
406         case ButtonOnly:
407                 // Cannot get here
408                 break;
409         }
410 }
411
412
413 Inset::EDITABLE InsetCollapsable::editable() const
414 {
415         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
416 }
417
418
419 bool InsetCollapsable::descendable() const
420 {
421         return geometry() != ButtonOnly;
422 }
423
424
425 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
426 {
427         return button_dim.contains(cmd.x, cmd.y);
428 }
429
430
431 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
432 {
433         docstring label;
434         pos_type const max_length = 15;
435         pos_type const p_siz = paragraphs().begin()->size();
436         pos_type const n = min(max_length, p_siz);
437         pos_type i = 0;
438         pos_type j = 0;
439         for (; i < n && j < p_siz; ++j) {
440                 if (paragraphs().begin()->isInset(j))
441                         continue;
442                 label += paragraphs().begin()->getChar(j);
443                 ++i;
444         }
445         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
446                 label += "...";
447         }
448         return label.empty() ? l : label;
449 }
450
451
452 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
453 {
454         //lyxerr << "InsetCollapsable: edit left/right" << endl;
455         cur.push(*this);
456         InsetText::edit(cur, front, entry_from);
457 }
458
459
460 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
461 {
462         //lyxerr << "InsetCollapsable: edit xy" << endl;
463         if (geometry() == ButtonOnly
464          || (button_dim.contains(x, y) 
465           && geometry() != NoButton))
466                 return this;
467         cur.push(*this);
468         return InsetText::editXY(cur, x, y);
469 }
470
471
472 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
473 {
474         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
475         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
476
477         switch (cmd.action) {
478         case LFUN_MOUSE_PRESS:
479                 if (hitButton(cmd) && geometry() != NoButton) {
480                         switch (cmd.button()) {
481                         case mouse_button::button1:
482                                 // reset selection if necessary (see bug 3060)
483                                 if (cur.selection())
484                                         cur.bv().cursor().clearSelection();
485                                 else
486                                         cur.noUpdate();
487                                 cur.dispatched();
488                                 return;
489                         case mouse_button::none:
490                         case mouse_button::button2:
491                         case mouse_button::button3:
492                         case mouse_button::button4:
493                         case mouse_button::button5:
494                                 // Nothing to do.
495                                 cur.undispatched();
496                                 return;
497                         }
498                 }
499                 if (geometry() == NoButton 
500                         || (geometry() != ButtonOnly && !hitButton(cmd)))
501                         InsetText::doDispatch(cur, cmd);
502                 else
503                         cur.undispatched();
504                 break;
505
506         case LFUN_MOUSE_MOTION:
507         case LFUN_MOUSE_DOUBLE:
508         case LFUN_MOUSE_TRIPLE:
509                 if (geometry() == NoButton)
510                         InsetText::doDispatch(cur, cmd);
511                 else if (geometry() != ButtonOnly
512                      && !hitButton(cmd))
513                         InsetText::doDispatch(cur, cmd);
514                 else
515                         cur.undispatched();
516                 break;
517
518         case LFUN_MOUSE_RELEASE:
519                 if (geometry() == NoButton || !hitButton(cmd)) {
520                         // The mouse click has to be within the inset!
521                         InsetText::doDispatch(cur, cmd);
522                         break;
523                 }
524                 if (cmd.button() != mouse_button::button1) {
525                         cur.dispatched();
526                         break;
527                 }
528                 // if we are selecting, we do not want to
529                 // toggle the inset.
530                 if (cur.selection())
531                         break;
532                 // Left button is clicked, the user asks to
533                 // toggle the inset visual state.
534                 cur.dispatched();
535                 cur.updateFlags(Update::Force | Update::FitCursor);
536                 if (geometry() == ButtonOnly) {
537                         setStatus(cur, Open);
538                         edit(cur, true);
539                 }
540                 else
541                         setStatus(cur, Collapsed);
542                 cur.bv().cursor() = cur;
543                 break;
544
545         case LFUN_INSET_TOGGLE:
546                 if (cmd.argument() == "open")
547                         setStatus(cur, Open);
548                 else if (cmd.argument() == "close")
549                         setStatus(cur, Collapsed);
550                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
551                         if (status_ == Open) {
552                                 setStatus(cur, Collapsed);
553                                 if (geometry() == ButtonOnly)
554                                         cur.top().forwardPos();
555                         } else
556                                 setStatus(cur, Open);
557                 else // if assign or anything else
558                         cur.undispatched();
559                 cur.dispatched();
560                 break;
561
562         case LFUN_PASTE:
563         case LFUN_CLIPBOARD_PASTE:
564         case LFUN_PRIMARY_SELECTION_PASTE: {
565                 InsetText::doDispatch(cur, cmd);
566                 // Since we can only store plain text, we must reset all
567                 // attributes.
568                 // FIXME: Change only the pasted paragraphs
569
570                 resetParagraphsFont();
571                 break;
572         }
573
574         default:
575                 if (layout_ && layout_->isForceLtr()) {
576                         // Force any new text to latex_language
577                         // FIXME: This should only be necessary in constructor, but
578                         // new paragraphs that are created by pressing enter at the
579                         // start of an existing paragraph get the buffer language
580                         // and not latex_language, so we take this brute force
581                         // approach.
582                         cur.current_font.setLanguage(latex_language);
583                         cur.real_current_font.setLanguage(latex_language);
584                 }
585                 InsetText::doDispatch(cur, cmd);
586                 break;
587         }
588 }
589
590
591 bool InsetCollapsable::allowMultiPar() const
592 {
593         return layout_->isMultiPar();
594 }
595
596
597 void InsetCollapsable::resetParagraphsFont()
598 {
599         Font font;
600         font.fontInfo() = inherit_font;
601         if (layout_->isForceLtr())
602                 font.setLanguage(latex_language);
603         if (layout_->isPassThru()) {
604                 ParagraphList::iterator par = paragraphs().begin();
605                 ParagraphList::iterator const end = paragraphs().end();
606                 while (par != end) {
607                         par->resetFonts(font);
608                         par->params().clear();
609                         ++par;
610                 }
611         }
612 }
613
614
615 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
616                 FuncStatus & flag) const
617 {
618         switch (cmd.action) {
619         // suppress these
620         case LFUN_ACCENT_ACUTE:
621         case LFUN_ACCENT_BREVE:
622         case LFUN_ACCENT_CARON:
623         case LFUN_ACCENT_CEDILLA:
624         case LFUN_ACCENT_CIRCLE:
625         case LFUN_ACCENT_CIRCUMFLEX:
626         case LFUN_ACCENT_DOT:
627         case LFUN_ACCENT_GRAVE:
628         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
629         case LFUN_ACCENT_MACRON:
630         case LFUN_ACCENT_OGONEK:
631         case LFUN_ACCENT_SPECIAL_CARON:
632         case LFUN_ACCENT_TIE:
633         case LFUN_ACCENT_TILDE:
634         case LFUN_ACCENT_UMLAUT:
635         case LFUN_ACCENT_UNDERBAR:
636         case LFUN_ACCENT_UNDERDOT:
637         case LFUN_APPENDIX:
638         case LFUN_BIBITEM_INSERT:
639         case LFUN_BOX_INSERT:
640         case LFUN_BRANCH_INSERT:
641         case LFUN_NEWLINE_INSERT:
642         case LFUN_CAPTION_INSERT:
643         case LFUN_DEPTH_DECREMENT:
644         case LFUN_DEPTH_INCREMENT:
645         case LFUN_ENVIRONMENT_INSERT:
646         case LFUN_ERT_INSERT:
647         case LFUN_FILE_INSERT:
648         case LFUN_FLEX_INSERT:
649         case LFUN_FLOAT_INSERT:
650         case LFUN_FLOAT_LIST:
651         case LFUN_FLOAT_WIDE_INSERT:
652         case LFUN_FONT_BOLD:
653         case LFUN_FONT_TYPEWRITER:
654         case LFUN_FONT_DEFAULT:
655         case LFUN_FONT_EMPH:
656         case LFUN_FONT_FREE_APPLY:
657         case LFUN_FONT_FREE_UPDATE:
658         case LFUN_FONT_NOUN:
659         case LFUN_FONT_ROMAN:
660         case LFUN_FONT_SANS:
661         case LFUN_FONT_FRAK:
662         case LFUN_FONT_ITAL:
663         case LFUN_FONT_SIZE:
664         case LFUN_FONT_STATE:
665         case LFUN_FONT_UNDERLINE:
666         case LFUN_FOOTNOTE_INSERT:
667         case LFUN_HYPERLINK_INSERT:
668         case LFUN_INDEX_INSERT:
669         case LFUN_INDEX_PRINT:
670         case LFUN_INSET_INSERT:
671         case LFUN_LABEL_GOTO:
672         case LFUN_LABEL_INSERT:
673         case LFUN_LINE_INSERT:
674         case LFUN_NEWPAGE_INSERT:
675         case LFUN_LAYOUT:
676         case LFUN_LAYOUT_PARAGRAPH:
677         case LFUN_LAYOUT_TABULAR:
678         case LFUN_MARGINALNOTE_INSERT:
679         case LFUN_MATH_DISPLAY:
680         case LFUN_MATH_INSERT:
681         case LFUN_MATH_MATRIX:
682         case LFUN_MATH_MODE:
683         case LFUN_MENU_OPEN:
684         case LFUN_NOACTION:
685         case LFUN_NOMENCL_INSERT:
686         case LFUN_NOMENCL_PRINT:
687         case LFUN_NOTE_INSERT:
688         case LFUN_NOTE_NEXT:
689         case LFUN_OPTIONAL_INSERT:
690         case LFUN_PARAGRAPH_PARAMS:
691         case LFUN_PARAGRAPH_PARAMS_APPLY:
692         case LFUN_PARAGRAPH_SPACING:
693         case LFUN_PARAGRAPH_UPDATE:
694         case LFUN_REFERENCE_NEXT:
695         case LFUN_SERVER_GOTO_FILE_ROW:
696         case LFUN_SERVER_NOTIFY:
697         case LFUN_SERVER_SET_XY:
698         case LFUN_SPACE_INSERT:
699         case LFUN_SPECIALCHAR_INSERT:
700         case LFUN_TABULAR_INSERT:
701         case LFUN_TOC_INSERT:
702         case LFUN_WRAP_INSERT:
703                 if (layout_->isPassThru()) {
704                         flag.enabled(false);
705                         return true;
706                 }
707                 return InsetText::getStatus(cur, cmd, flag);
708
709         case LFUN_INSET_TOGGLE:
710                 if ((cmd.argument() == "open" && status_ != Open)
711                       || (cmd.argument() == "close" && status_ == Open)
712                       || cmd.argument() == "toggle" || cmd.argument().empty())
713                                 flag.enabled(true);
714                 else
715                         flag.enabled(false);
716                 return true;
717
718         case LFUN_LANGUAGE:
719                 flag.enabled(!layout_->isForceLtr());
720                 return InsetText::getStatus(cur, cmd, flag);
721
722         case LFUN_BREAK_PARAGRAPH:
723         case LFUN_BREAK_PARAGRAPH_SKIP:
724                 flag.enabled(layout_->isMultiPar());
725                 return true;
726
727         default:
728                 return InsetText::getStatus(cur, cmd, flag);
729         }
730 }
731
732
733 void InsetCollapsable::setLabel(docstring const & l)
734 {
735         labelstring_ = l;
736 }
737
738
739 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
740 {
741         status_ = status;
742         setButtonLabel();
743         if (status_ == Collapsed)
744                 cur.leaveInset(*this);
745 }
746
747
748 docstring InsetCollapsable::floatName(
749                 string const & type, BufferParams const & bp) const
750 {
751         FloatList const & floats = bp.documentClass().floats();
752         FloatList::const_iterator it = floats[type];
753         // FIXME UNICODE
754         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
755 }
756
757
758 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
759 {
760         if (!layout_)
761                 return InsetLayout::Classic;
762         InsetLayout::InsetDecoration const dec = layout_->decoration();
763         switch (dec) {
764         case InsetLayout::Classic:
765         case InsetLayout::Minimalistic:
766         case InsetLayout::Conglomerate:
767                 return dec;
768         case InsetLayout::Default:
769                 break;
770         }
771         if (lyxCode() == FLEX_CODE)
772                 return InsetLayout::Conglomerate;
773         return InsetLayout::Classic;
774 }
775
776
777 int InsetCollapsable::latex(odocstream & os,
778                           OutputParams const & runparams) const
779 {
780         // FIXME: What should we do layout_ is 0?
781         // 1) assert
782         // 2) through an error
783         if (!layout_)
784                 return 0;
785
786         // This implements the standard way of handling the LaTeX output of
787         // a collapsable inset, either a command or an environment. Standard 
788         // collapsable insets should not redefine this, non-standard ones may
789         // call this.
790         if (!layout_->latexname().empty()) {
791                 if (layout_->latextype() == "command") {
792                         // FIXME UNICODE
793                         if (runparams.moving_arg)
794                                 os << "\\protect";
795                         os << '\\' << from_utf8(layout_->latexname());
796                         if (!layout_->latexparam().empty())
797                                 os << from_utf8(layout_->latexparam());
798                         os << '{';
799                 } else if (layout_->latextype() == "environment") {
800                         os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
801                         if (!layout_->latexparam().empty())
802                                 os << from_utf8(layout_->latexparam());
803                 }
804         }
805         OutputParams rp = runparams;
806         if (layout_->isPassThru())
807                 rp.verbatim = true;
808         if (layout_->isNeedProtect())
809                 rp.moving_arg = true;
810         int i = InsetText::latex(os, rp);
811         if (!layout_->latexname().empty()) {
812                 if (layout_->latextype() == "command") {
813                         os << "}";
814                 } else if (layout_->latextype() == "environment") {
815                         os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
816                         i += 4;
817                 }
818         }
819         return i;
820 }
821
822
823 void InsetCollapsable::validate(LaTeXFeatures & features) const
824 {
825         if (!layout_)
826                 return;
827
828         // Force inclusion of preamble snippet in layout file
829         features.require(to_utf8(layout_->name()));
830         InsetText::validate(features);
831 }
832
833
834 bool InsetCollapsable::undefined() const
835 {
836         docstring const & n = getLayout().name();
837         return n.empty() || n == DocumentClass::emptyInsetLayout().name();
838 }
839
840
841 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
842         int y) const
843 {
844         if (decoration() == InsetLayout::Conglomerate)
845                 return from_ascii("context-conglomerate");
846
847         if (geometry() == NoButton)
848                 return from_ascii("context-collapsable");
849
850         Dimension dim = dimensionCollapsed();
851         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
852                 return from_ascii("context-collapsable");
853
854         return InsetText::contextMenu(bv, x, y);
855 }
856
857 } // namespace lyx