]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Revert r27129. We can't get rid of these until we have something else positive to do.
[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 "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.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/debug.h"
38 #include "support/docstream.h"
39 #include "support/gettext.h"
40 #include "support/lassert.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         : InsetText(buf), status_(Inset::Open),
81           openinlined_(false), autoOpen_(false), mouse_hover_(false)
82 {
83         DocumentClass const & dc = buf.params().documentClass();
84         setLayout(&dc);
85         setAutoBreakRows(true);
86         setDrawFrame(true);
87         setFrameColor(Color_collapsableframe);
88         paragraphs().back().setLayout(dc.plainLayout()); 
89 }
90
91
92 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
93         : InsetText(rhs),
94                 layout_(rhs.layout_),
95                 labelstring_(rhs.labelstring_),
96                 button_dim(rhs.button_dim),
97                 status_(rhs.status_),
98                 openinlined_(rhs.openinlined_),
99                 autoOpen_(rhs.autoOpen_),
100                 // the sole purpose of this copy constructor
101                 mouse_hover_(false)
102 {
103 }
104
105
106 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
107 {
108         Dimension dim = dimensionCollapsed();
109         if (geometry() == NoButton)
110                 return translateIfPossible(layout_->labelstring());
111         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen())
112                 return docstring();
113
114         OutputParams rp(&buffer().params().encoding());
115         odocstringstream ods;
116         InsetText::plaintext(ods, rp);
117         docstring content_tip = ods.str();
118         // shorten it if necessary
119         if (content_tip.size() > 200)
120                 content_tip = content_tip.substr(0, 200) + "...";
121         return content_tip;
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::plainInsetLayout();
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         InsetText::read(lex);
173         // set button label again as the inset contents was not read yet at
174         // setLayout() time.
175         setButtonLabel();
176
177         // Force default font, if so requested
178         // This avoids paragraphs in buffer language that would have a
179         // foreign language after a document language change, and it ensures
180         // that all new text in ERT and similar gets the "latex" language,
181         // since new text inherits the language from the last position of the
182         // existing text.  As a side effect this makes us also robust against
183         // bugs in LyX that might lead to font changes in ERT in .lyx files.
184         resetParagraphsFont();
185 }
186
187
188 Dimension InsetCollapsable::dimensionCollapsed() const
189 {
190         LASSERT(layout_, /**/);
191         Dimension dim;
192         theFontMetrics(layout_->labelfont()).buttonText(
193                 labelstring_, dim.wid, dim.asc, dim.des);
194         return dim;
195 }
196
197
198 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
199 {
200         LASSERT(layout_, /**/);
201
202         autoOpen_ = mi.base.bv->cursor().isInside(this);
203
204         FontInfo tmpfont = mi.base.font;
205         mi.base.font = layout_->font();
206         mi.base.font.realize(tmpfont);
207
208         switch (geometry()) {
209         case NoButton:
210                 InsetText::metrics(mi, dim);
211                 break;
212         case Corners:
213                 InsetText::metrics(mi, dim);
214                 dim.des -= 3;
215                 dim.asc -= 1;
216                 break;
217         case SubLabel: {
218                 InsetText::metrics(mi, dim);
219                 // consider width of the inset label
220                 FontInfo font(layout_->labelfont());
221                 font.realize(sane_font);
222                 font.decSize();
223                 font.decSize();
224                 int w = 0;
225                 int a = 0;
226                 int d = 0;
227                 theFontMetrics(font).rectText(labelstring_, w, a, d);
228                 dim.des += a + d;
229                 break;
230                 }
231         case TopButton:
232         case LeftButton:
233         case ButtonOnly:
234                 dim = dimensionCollapsed();
235                 if (geometry() == TopButton || geometry() == LeftButton) {
236                         Dimension textdim;
237                         InsetText::metrics(mi, textdim);
238                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
239                         if (openinlined_) {
240                                 // Correct for button width.
241                                 dim.wid += textdim.wid;
242                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
243                                 dim.asc = textdim.asc;
244                         } else {
245                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
246                                 dim.wid = max(dim.wid, textdim.wid);
247                         }
248                 }
249                 break;
250         }
251
252         mi.base.font = tmpfont;
253 }
254
255
256 bool InsetCollapsable::setMouseHover(bool mouse_hover)
257 {
258         mouse_hover_ = mouse_hover;
259         return true;
260 }
261
262
263 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
264 {
265         LASSERT(layout_, /**/);
266
267         autoOpen_ = pi.base.bv->cursor().isInside(this);
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
378         pi.base.font = tmpfont;
379 }
380
381
382 void InsetCollapsable::cursorPos(BufferView const & bv,
383                 CursorSlice const & sl, bool boundary, int & x, int & y) const
384 {
385         if (geometry() == ButtonOnly)
386                 status_ = Open;
387         LASSERT(geometry() != ButtonOnly, /**/);
388
389         InsetText::cursorPos(bv, sl, boundary, x, y);
390         Dimension const textdim = InsetText::dimension(bv);
391
392         switch (geometry()) {
393         case LeftButton:
394                 x += dimensionCollapsed().wid;
395                 break;
396         case TopButton: {
397                 y += dimensionCollapsed().des + textdim.asc;
398                 break;
399         }
400         case NoButton:
401         case SubLabel:
402         case Corners:
403                 // Do nothing
404                 break;
405         case ButtonOnly:
406                 // Cannot get here
407                 break;
408         }
409 }
410
411
412 Inset::EDITABLE InsetCollapsable::editable() const
413 {
414         return geometry() != ButtonOnly ? HIGHLY_EDITABLE : IS_EDITABLE;
415 }
416
417
418 bool InsetCollapsable::descendable() const
419 {
420         return geometry() != ButtonOnly;
421 }
422
423
424 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
425 {
426         return button_dim.contains(cmd.x, cmd.y);
427 }
428
429
430 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
431 {
432         docstring label;
433         pos_type const max_length = 15;
434         pos_type const p_siz = paragraphs().begin()->size();
435         pos_type const n = min(max_length, p_siz);
436         pos_type i = 0;
437         pos_type j = 0;
438         for (; i < n && j < p_siz; ++j) {
439                 if (paragraphs().begin()->isInset(j))
440                         continue;
441                 label += paragraphs().begin()->getChar(j);
442                 ++i;
443         }
444         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
445                 label += "...";
446         }
447         return label.empty() ? l : label;
448 }
449
450
451 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
452 {
453         //lyxerr << "InsetCollapsable: edit left/right" << endl;
454         cur.push(*this);
455         InsetText::edit(cur, front, entry_from);
456 }
457
458
459 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
460 {
461         //lyxerr << "InsetCollapsable: edit xy" << endl;
462         if (geometry() == ButtonOnly
463          || (button_dim.contains(x, y) 
464           && geometry() != NoButton))
465                 return this;
466         cur.push(*this);
467         return InsetText::editXY(cur, x, y);
468 }
469
470
471 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
472 {
473         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
474         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
475
476         switch (cmd.action) {
477         case LFUN_MOUSE_PRESS:
478                 if (hitButton(cmd)) {
479                         switch (cmd.button()) {
480                         case mouse_button::button1:
481                         case mouse_button::button3:
482                                 // Pass the command to the enclosing InsetText,
483                                 // so that the cursor gets set.
484                                 cur.undispatched();
485                                 break;
486                         case mouse_button::none:
487                         case mouse_button::button2:
488                         case mouse_button::button4:
489                         case mouse_button::button5:
490                                 // Nothing to do.
491                                 cur.noUpdate();
492                                 break;
493                         }
494                 } else if (geometry() != ButtonOnly)
495                         InsetText::doDispatch(cur, cmd);
496                 else
497                         cur.undispatched();
498                 break;
499
500         case LFUN_MOUSE_MOTION:
501         case LFUN_MOUSE_DOUBLE:
502         case LFUN_MOUSE_TRIPLE:
503                 if (hitButton(cmd)) 
504                         cur.noUpdate();
505                 else if (geometry() != ButtonOnly)
506                         InsetText::doDispatch(cur, cmd);
507                 else
508                         cur.undispatched();
509                 break;
510
511         case LFUN_MOUSE_RELEASE:
512                 if (!hitButton(cmd)) {
513                         // The mouse click has to be within the inset!
514                         if (geometry() != ButtonOnly)
515                                 InsetText::doDispatch(cur, cmd);
516                         else
517                                 cur.undispatched();                     
518                         break;
519                 }
520                 if (cmd.button() != mouse_button::button1) {
521                         // Nothing to do.
522                         cur.noUpdate();
523                         break;
524                 }
525                 // if we are selecting, we do not want to
526                 // toggle the inset.
527                 if (cur.selection())
528                         break;
529                 // Left button is clicked, the user asks to
530                 // toggle the inset visual state.
531                 cur.dispatched();
532                 cur.updateFlags(Update::Force | Update::FitCursor);
533                 if (geometry() == ButtonOnly) {
534                         setStatus(cur, Open);
535                         edit(cur, true);
536                 }
537                 else
538                         setStatus(cur, Collapsed);
539                 cur.bv().cursor() = cur;
540                 break;
541
542         case LFUN_INSET_TOGGLE:
543                 if (cmd.argument() == "open")
544                         setStatus(cur, Open);
545                 else if (cmd.argument() == "close")
546                         setStatus(cur, Collapsed);
547                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
548                         if (status_ == Open) {
549                                 setStatus(cur, Collapsed);
550                                 if (geometry() == ButtonOnly)
551                                         cur.top().forwardPos();
552                         } else
553                                 setStatus(cur, Open);
554                 else // if assign or anything else
555                         cur.undispatched();
556                 cur.dispatched();
557                 break;
558
559         case LFUN_PASTE:
560         case LFUN_CLIPBOARD_PASTE:
561         case LFUN_PRIMARY_SELECTION_PASTE: {
562                 InsetText::doDispatch(cur, cmd);
563                 // Since we can only store plain text, we must reset all
564                 // attributes.
565                 // FIXME: Change only the pasted paragraphs
566
567                 resetParagraphsFont();
568                 break;
569         }
570
571         default:
572                 if (layout_ && layout_->isForceLtr()) {
573                         // Force any new text to latex_language
574                         // FIXME: This should only be necessary in constructor, but
575                         // new paragraphs that are created by pressing enter at the
576                         // start of an existing paragraph get the buffer language
577                         // and not latex_language, so we take this brute force
578                         // approach.
579                         cur.current_font.setLanguage(latex_language);
580                         cur.real_current_font.setLanguage(latex_language);
581                 }
582                 InsetText::doDispatch(cur, cmd);
583                 break;
584         }
585 }
586
587
588 bool InsetCollapsable::allowMultiPar() const
589 {
590         return layout_->isMultiPar();
591 }
592
593
594 void InsetCollapsable::resetParagraphsFont()
595 {
596         Font font;
597         font.fontInfo() = inherit_font;
598         if (layout_->isForceLtr())
599                 font.setLanguage(latex_language);
600         if (layout_->isPassThru()) {
601                 ParagraphList::iterator par = paragraphs().begin();
602                 ParagraphList::iterator const end = paragraphs().end();
603                 while (par != end) {
604                         par->resetFonts(font);
605                         par->params().clear();
606                         ++par;
607                 }
608         }
609 }
610
611
612 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
613                 FuncStatus & flag) const
614 {
615         switch (cmd.action) {
616         // FIXME At present, these are being enabled and disabled according to
617         // whether PASSTHRU has been set in the InsetLayout. This makes some
618         // sense, but there are other checks that should really be done. E.g.,
619         // one should not be able to inset IndexPrint inside an optional argument!!
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_TIE:
632         case LFUN_ACCENT_TILDE:
633         case LFUN_ACCENT_UMLAUT:
634         case LFUN_ACCENT_UNDERBAR:
635         case LFUN_ACCENT_UNDERDOT:
636         case LFUN_APPENDIX:
637         case LFUN_BOX_INSERT:
638         case LFUN_BRANCH_INSERT:
639         case LFUN_NEWLINE_INSERT:
640         case LFUN_CAPTION_INSERT:
641         case LFUN_DEPTH_DECREMENT:
642         case LFUN_DEPTH_INCREMENT:
643         case LFUN_ERT_INSERT:
644         case LFUN_FILE_INSERT:
645         case LFUN_FLEX_INSERT:
646         case LFUN_FLOAT_INSERT:
647         case LFUN_FLOAT_LIST_INSERT:
648         case LFUN_FLOAT_WIDE_INSERT:
649         case LFUN_FONT_BOLD:
650         case LFUN_FONT_BOLDSYMBOL:
651         case LFUN_FONT_TYPEWRITER:
652         case LFUN_FONT_DEFAULT:
653         case LFUN_FONT_EMPH:
654         case LFUN_TEXTSTYLE_APPLY:
655         case LFUN_TEXTSTYLE_UPDATE:
656         case LFUN_FONT_NOUN:
657         case LFUN_FONT_ROMAN:
658         case LFUN_FONT_SANS:
659         case LFUN_FONT_FRAK:
660         case LFUN_FONT_ITAL:
661         case LFUN_FONT_SIZE:
662         case LFUN_FONT_STATE:
663         case LFUN_FONT_UNDERLINE:
664         case LFUN_FOOTNOTE_INSERT:
665         case LFUN_HYPERLINK_INSERT:
666         case LFUN_INDEX_INSERT:
667         case LFUN_INDEX_PRINT:
668         case LFUN_INSET_INSERT:
669         case LFUN_LABEL_GOTO:
670         case LFUN_LABEL_INSERT:
671         case LFUN_LINE_INSERT:
672         case LFUN_NEWPAGE_INSERT:
673         case LFUN_LAYOUT_TABULAR:
674         case LFUN_MARGINALNOTE_INSERT:
675         case LFUN_MATH_DISPLAY:
676         case LFUN_MATH_INSERT:
677         case LFUN_MATH_MATRIX:
678         case LFUN_MATH_MODE:
679         case LFUN_MENU_OPEN:
680         case LFUN_NOACTION:
681         case LFUN_NOMENCL_INSERT:
682         case LFUN_NOMENCL_PRINT:
683         case LFUN_NOTE_INSERT:
684         case LFUN_NOTE_NEXT:
685         case LFUN_OPTIONAL_INSERT:
686         case LFUN_REFERENCE_NEXT:
687         case LFUN_SERVER_GOTO_FILE_ROW:
688         case LFUN_SERVER_NOTIFY:
689         case LFUN_SERVER_SET_XY:
690         case LFUN_SPACE_INSERT:
691         case LFUN_SPECIALCHAR_INSERT:
692         case LFUN_TABULAR_INSERT:
693         case LFUN_TOC_INSERT:
694         case LFUN_WRAP_INSERT:
695                 if (layout_->isPassThru()) {
696                         flag.setEnabled(false);
697                         return true;
698                 }
699                 return InsetText::getStatus(cur, cmd, flag);
700
701         case LFUN_INSET_TOGGLE:
702                 if (cmd.argument() == "open")
703                         flag.setEnabled(status_ != Open);
704                 else if (cmd.argument() == "close")
705                         flag.setEnabled(status_ == Open);
706                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
707                         flag.setEnabled(true);
708                         flag.setOnOff(status_ == Open);
709                 } else
710                         flag.setEnabled(false);
711                 return true;
712
713         case LFUN_LANGUAGE:
714                 flag.setEnabled(!layout_->isForceLtr());
715                 return InsetText::getStatus(cur, cmd, flag);
716
717         case LFUN_BREAK_PARAGRAPH:
718                 flag.setEnabled(layout_->isMultiPar());
719                 return true;
720
721         default:
722                 return InsetText::getStatus(cur, cmd, flag);
723         }
724 }
725
726
727 void InsetCollapsable::setLabel(docstring const & l)
728 {
729         labelstring_ = l;
730 }
731
732
733 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
734 {
735         status_ = status;
736         setButtonLabel();
737         if (status_ == Collapsed) {
738                 cur.leaveInset(*this);
739                 mouse_hover_ = false;
740         }
741 }
742
743
744 docstring InsetCollapsable::floatName(
745                 string const & type, BufferParams const & bp) const
746 {
747         FloatList const & floats = bp.documentClass().floats();
748         FloatList::const_iterator it = floats[type];
749         // FIXME UNICODE
750         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
751 }
752
753
754 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
755 {
756         if (!layout_)
757                 return InsetLayout::CLASSIC;
758         InsetLayout::InsetDecoration const dec = layout_->decoration();
759         switch (dec) {
760         case InsetLayout::CLASSIC:
761         case InsetLayout::MINIMALISTIC:
762         case InsetLayout::CONGLOMERATE:
763                 return dec;
764         case InsetLayout::DEFAULT:
765                 break;
766         }
767         if (lyxCode() == FLEX_CODE)
768                 return InsetLayout::CONGLOMERATE;
769         return InsetLayout::CLASSIC;
770 }
771
772
773 int InsetCollapsable::latex(odocstream & os,
774                           OutputParams const & runparams) const
775 {
776         // FIXME: What should we do layout_ is 0?
777         // 1) assert
778         // 2) through an error
779         if (!layout_)
780                 return 0;
781
782         // This implements the standard way of handling the LaTeX output of
783         // a collapsable inset, either a command or an environment. Standard 
784         // collapsable insets should not redefine this, non-standard ones may
785         // call this.
786         if (!layout_->latexname().empty()) {
787                 if (layout_->latextype() == InsetLayout::COMMAND) {
788                         // FIXME UNICODE
789                         if (runparams.moving_arg)
790                                 os << "\\protect";
791                         os << '\\' << from_utf8(layout_->latexname());
792                         if (!layout_->latexparam().empty())
793                                 os << from_utf8(layout_->latexparam());
794                         os << '{';
795                 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
796                         os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
797                         if (!layout_->latexparam().empty())
798                                 os << from_utf8(layout_->latexparam());
799                 }
800         }
801         OutputParams rp = runparams;
802         if (layout_->isPassThru())
803                 rp.verbatim = true;
804         if (layout_->isNeedProtect())
805                 rp.moving_arg = true;
806         int i = InsetText::latex(os, rp);
807         if (!layout_->latexname().empty()) {
808                 if (layout_->latextype() == InsetLayout::COMMAND) {
809                         os << "}";
810                 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
811                         os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
812                         i += 4;
813                 }
814         }
815         return i;
816 }
817
818
819 void InsetCollapsable::validate(LaTeXFeatures & features) const
820 {
821         string const preamble = getLayout().preamble();
822         if (!preamble.empty())
823                 features.addPreambleSnippet(preamble);
824         features.require(getLayout().requires());
825         InsetText::validate(features);
826 }
827
828
829 bool InsetCollapsable::undefined() const
830 {
831         docstring const & n = getLayout().name();
832         return n.empty() || n == DocumentClass::plainInsetLayout().name();
833 }
834
835
836 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
837         int y) const
838 {
839         if (decoration() == InsetLayout::CONGLOMERATE)
840                 return from_ascii("context-conglomerate");
841
842         if (geometry() == NoButton)
843                 return from_ascii("context-collapsable");
844
845         Dimension dim = dimensionCollapsed();
846         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
847                 return from_ascii("context-collapsable");
848
849         return InsetText::contextMenu(bv, x, y);
850 }
851
852 } // namespace lyx