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