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