]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
remove unneeded @includes from BufferView.h
[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
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
36
37
38 namespace lyx {
39
40 using std::endl;
41 using std::max;
42 using std::ostream;
43 using std::string;
44
45
46 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
47 {
48         return autoOpen_ ? Open : status_;
49 }
50
51
52 InsetCollapsable::Geometry InsetCollapsable::geometry() const
53 {
54         switch (decoration()) {
55         case Classic:
56                 if (status() == Open) {
57                         if (openinlined_)
58                                 return LeftButton;
59                         else
60                                 return TopButton;
61                 } else
62                         return ButtonOnly;
63
64         case Minimalistic:
65                 return status() == Open ? NoButton : ButtonOnly ;
66
67         case Conglomerate:
68                 return status() == Open ? SubLabel : Corners ;
69         }
70
71         // dummy return value to shut down a warning,
72         // this is dead code.
73         return NoButton;
74 }
75
76
77 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
78                 CollapseStatus status, InsetLayout const * il)
79         : InsetText(bp), layout_(il), status_(status),
80           openinlined_(false), autoOpen_(false), mouse_hover_(false)
81 {
82         setAutoBreakRows(true);
83         setDrawFrame(true);
84         setFrameColor(Color_collapsableframe);
85 }
86
87
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
89         : InsetText(rhs),
90                 button_dim(rhs.button_dim),
91                 topx(rhs.topx),
92                 topbaseline(rhs.topbaseline),
93                 layout_(rhs.layout_),
94                 labelstring_(rhs.labelstring_),
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 void  InsetCollapsable::setLayout(BufferParams const & bp)
105 {
106         setLayout(bp.getTextClass().insetlayout(name()));
107 }
108
109
110 void InsetCollapsable::setLayout(InsetLayout const & il)
111 {
112         layout_ = &il;
113         labelstring_ = layout_->labelstring;
114
115         setButtonLabel();
116 }
117
118
119 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
120 {
121         os << "status ";
122         switch (status_) {
123         case Open:
124                 os << "open";
125                 break;
126         case Collapsed:
127                 os << "collapsed";
128                 break;
129         }
130         os << "\n";
131         text_.write(buf, os);
132 }
133
134
135 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
136 {
137         bool token_found = false;
138         if (lex.isOK()) {
139                 lex.next();
140                 string const token = lex.getString();
141                 if (token == "status") {
142                         lex.next();
143                         string const tmp_token = lex.getString();
144
145                         if (tmp_token == "collapsed") {
146                                 status_ = Collapsed;
147                                 token_found = true;
148                         } else if (tmp_token == "open") {
149                                 status_ = Open;
150                                 token_found = true;
151                         } else {
152                                 lyxerr << "InsetCollapsable::read: Missing status!"
153                                        << endl;
154                                 // Take countermeasures
155                                 lex.pushToken(token);
156                         }
157                 } else {
158                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
159                                    << endl;
160                         // take countermeasures
161                         lex.pushToken(token);
162                 }
163         }
164         InsetText::read(buf, lex);
165
166         setLayout(buf.params());
167
168         if (!token_found)
169                 status_ = isOpen() ? Open : Collapsed;
170
171         // Force default font, if so requested
172         // This avoids paragraphs in buffer language that would have a
173         // foreign language after a document language change, and it ensures
174         // that all new text in ERT and similar gets the "latex" language,
175         // since new text inherits the language from the last position of the
176         // existing text.  As a side effect this makes us also robust against
177         // bugs in LyX that might lead to font changes in ERT in .lyx files.
178         resetParagraphsFont();
179 }
180
181
182 Dimension InsetCollapsable::dimensionCollapsed() const
183 {
184         BOOST_ASSERT(layout_);
185         Dimension dim;
186         theFontMetrics(layout_->labelfont).buttonText(
187                 labelstring_, dim.wid, dim.asc, dim.des);
188         return dim;
189 }
190
191
192 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
193 {
194         BOOST_ASSERT(layout_);
195
196         autoOpen_ = mi.base.bv->cursor().isInside(this);
197
198         FontInfo tmpfont = mi.base.font;
199         mi.base.font = layout_->font;
200         mi.base.font.realize(tmpfont);
201
202         switch (geometry()) {
203         case NoButton:
204                 InsetText::metrics(mi, dim);
205                 break;
206         case Corners:
207                 InsetText::metrics(mi, dim);
208                 dim.des -= 3;
209                 dim.asc -= 1;
210                 break;
211         case SubLabel: {
212                 InsetText::metrics(mi, dim);
213                 // consider width of the inset label
214                 FontInfo font(layout_->labelfont);
215                 font.realize(sane_font);
216                 font.decSize();
217                 font.decSize();
218                 int w = 0;
219                 int a = 0;
220                 int d = 0;
221                 theFontMetrics(font).rectText(labelstring_, w, a, d);
222                 dim.des += a + d;
223                 break;
224                 }
225         case TopButton:
226         case LeftButton:
227         case ButtonOnly:
228                 dim = dimensionCollapsed();
229                 if (geometry() == TopButton
230                  || geometry() == LeftButton) {
231                         Dimension textdim;
232                         InsetText::metrics(mi, textdim);
233                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
234                         if (openinlined_) {
235                                 // Correct for button width.
236                                 dim.wid += textdim.wid;
237                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
238                                 dim.asc = textdim.asc;
239                         } else {
240                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
241                                 dim.wid = max(dim.wid, textdim.wid);
242                         }
243                 }
244                 break;
245         }
246
247         mi.base.font = tmpfont;
248 }
249
250
251 bool InsetCollapsable::setMouseHover(bool mouse_hover)
252 {
253         mouse_hover_ = mouse_hover;
254         return true;
255 }
256
257
258 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
259 {
260         BOOST_ASSERT(layout_);
261
262         autoOpen_ = pi.base.bv->cursor().isInside(this);
263         ColorCode const old_color = pi.background_color;
264         pi.background_color = backgroundColor();
265
266         FontInfo tmpfont = pi.base.font;
267         pi.base.font = layout_->font;
268         pi.base.font.realize(tmpfont);
269
270         // Draw button first -- top, left or only
271         Dimension dimc = dimensionCollapsed();
272
273         if (geometry() == TopButton ||
274             geometry() == LeftButton ||
275             geometry() == ButtonOnly) {
276                 button_dim.x1 = x + 0;
277                 button_dim.x2 = x + dimc.width();
278                 button_dim.y1 = y - dimc.asc;
279                 button_dim.y2 = y + dimc.des;
280
281                 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
282                         mouse_hover_);
283         } else {
284                 button_dim.x1 = 0;
285                 button_dim.y1 = 0;
286                 button_dim.x2 = 0;
287                 button_dim.y2 = 0;
288         }
289
290         Dimension const textdim = InsetText::dimension(*pi.base.bv);
291         int const baseline = y;
292         int textx, texty;
293         switch (geometry()) {
294         case LeftButton:
295                 textx = x + dimc.width();
296                 texty = baseline;
297                 InsetText::draw(pi, textx, texty);
298                 break;
299         case TopButton:
300                 textx = x;
301                 texty = baseline + dimc.des + textdim.asc;
302                 InsetText::draw(pi, textx, texty);
303                 break;
304         case ButtonOnly:
305                 break;
306         case NoButton:
307                 textx = x;
308                 texty = baseline;
309                 InsetText::draw(pi, textx, texty);
310                 break;
311         case SubLabel:
312         case Corners:
313                 textx = x;
314                 texty = baseline;
315                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
316                 InsetText::draw(pi, textx, texty);
317                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
318
319                 int desc = textdim.descent();
320                 if (geometry() == Corners)
321                         desc -= 3;
322
323                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
324                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
325                 pi.pain.line(xx1, y + desc - 4, 
326                              xx1, y + desc, 
327                         layout_->labelfont.color());
328                 if (internalStatus() == Open)
329                         pi.pain.line(xx1, y + desc, 
330                                 xx2, y + desc,
331                                 layout_->labelfont.color());
332                 else {
333                         // Make status_ value visible:
334                         pi.pain.line(xx1, y + desc,
335                                 xx1 + 4, y + desc,
336                                 layout_->labelfont.color());
337                         pi.pain.line(xx2 - 4, y + desc,
338                                 xx2, y + desc,
339                                 layout_->labelfont.color());
340                 }
341                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
342                         layout_->labelfont.color());
343
344                 // the label below the text. Can be toggled.
345                 if (geometry() == SubLabel) {
346                         FontInfo font(layout_->labelfont);
347                         font.realize(sane_font);
348                         font.decSize();
349                         font.decSize();
350                         int w = 0;
351                         int a = 0;
352                         int d = 0;
353                         theFontMetrics(font).rectText(labelstring_, w, a, d);
354                         int const ww = max(textdim.wid, w);
355                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
356                                 labelstring_, font, Color_none, Color_none);
357                         desc += d;
358                 }
359
360                 // a visual cue when the cursor is inside the inset
361                 Cursor & cur = pi.base.bv->cursor();
362                 if (cur.isInside(this)) {
363                         y -= textdim.asc;
364                         y += 3;
365                         pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
366                         pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
367                         pi.pain.line(xx2, y + 4, xx2, y,
368                                 layout_->labelfont.color());
369                         pi.pain.line(xx2 - 4, y, xx2, y,
370                                 layout_->labelfont.color());
371                 }
372                 break;
373         }
374         pi.background_color = old_color;
375
376         pi.base.font = tmpfont;
377 }
378
379
380 void InsetCollapsable::cursorPos(BufferView const & bv,
381                 CursorSlice const & sl, bool boundary, int & x, int & y) const
382 {
383         if (geometry() == ButtonOnly)
384                 status_ = Open;
385         BOOST_ASSERT(geometry() != ButtonOnly);
386
387         InsetText::cursorPos(bv, sl, boundary, x, y);
388         Dimension const textdim = InsetText::dimension(bv);
389
390         switch (geometry()) {
391         case LeftButton:
392                 x += dimensionCollapsed().wid;
393                 break;
394         case TopButton: {
395                 y += dimensionCollapsed().des + textdim.asc;
396                 break;
397         }
398         case NoButton:
399         case SubLabel:
400         case Corners:
401                 // Do nothing
402                 break;
403         case ButtonOnly:
404                 // Cannot get here
405                 break;
406         }
407 }
408
409
410 Inset::EDITABLE InsetCollapsable::editable() const
411 {
412         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
413 }
414
415
416 bool InsetCollapsable::descendable() const
417 {
418         return geometry() != ButtonOnly;
419 }
420
421
422 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
423 {
424         return button_dim.contains(cmd.x, cmd.y);
425 }
426
427
428 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
429 {
430         docstring label;
431         pos_type const max_length = 15;
432         pos_type const p_siz = paragraphs().begin()->size();
433         pos_type const n = std::min(max_length, p_siz);
434         pos_type i = 0;
435         pos_type j = 0;
436         for (; i < n && j < p_siz; ++j) {
437                 if (paragraphs().begin()->isInset(j))
438                         continue;
439                 label += paragraphs().begin()->getChar(j);
440                 ++i;
441         }
442         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
443                 label += "...";
444         }
445         return label.empty() ? l : label;
446 }
447
448
449 void InsetCollapsable::edit(Cursor & cur, bool left)
450 {
451         //lyxerr << "InsetCollapsable: edit left/right" << endl;
452         cur.push(*this);
453         InsetText::edit(cur, left);
454 }
455
456
457 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
458 {
459         //lyxerr << "InsetCollapsable: edit xy" << endl;
460         if (geometry() == ButtonOnly
461          || (button_dim.contains(x, y) 
462           && geometry() != NoButton))
463                 return this;
464         cur.push(*this);
465         return InsetText::editXY(cur, x, y);
466 }
467
468
469 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
470 {
471         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
472         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
473
474         switch (cmd.action) {
475         case LFUN_MOUSE_PRESS:
476                 if (cmd.button() == mouse_button::button1 
477                  && hitButton(cmd) 
478                  && geometry() != NoButton) {
479                         // reset selection if necessary (see bug 3060)
480                         if (cur.selection())
481                                 cur.bv().cursor().clearSelection();
482                         else
483                                 cur.noUpdate();
484                         cur.dispatched();
485                         break;
486                 }
487                 if (geometry() == NoButton)
488                         InsetText::doDispatch(cur, cmd);
489                 else if (geometry() != ButtonOnly 
490                      && !hitButton(cmd))
491                         InsetText::doDispatch(cur, cmd);
492                 else
493                         cur.undispatched();
494                 break;
495
496         case LFUN_MOUSE_MOTION:
497         case LFUN_MOUSE_DOUBLE:
498         case LFUN_MOUSE_TRIPLE:
499                 if (geometry() == NoButton)
500                         InsetText::doDispatch(cur, cmd);
501                 else if (geometry() != ButtonOnly
502                      && !hitButton(cmd))
503                         InsetText::doDispatch(cur, cmd);
504                 else
505                         cur.undispatched();
506                 break;
507
508         case LFUN_MOUSE_RELEASE:
509                 if (cmd.button() == mouse_button::button3) {
510                         // There is no button to right click:
511                         if (geometry() == Corners ||
512                             geometry() == SubLabel ||
513                             geometry() == NoButton)  {
514                                 if (internalStatus() == Open)
515                                         setStatus(cur, Collapsed);
516                                 else
517                                         setStatus(cur, Open);
518                                 break;
519                         } else {
520                                 // Open the Inset 
521                                 // configuration dialog
522                                 showInsetDialog(&cur.bv());
523                                 break;
524                         }
525                 }
526
527                 if (geometry() == NoButton) {
528                         // The mouse click has to be within the inset!
529                         InsetText::doDispatch(cur, cmd);
530                         break;
531                 }
532
533                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
534                         // if we are selecting, we do not want to
535                         // toggle the inset.
536                         if (cur.selection())
537                                 break;
538                         // Left button is clicked, the user asks to
539                         // toggle the inset visual state.
540                         cur.dispatched();
541                         cur.updateFlags(Update::Force | Update::FitCursor);
542                         if (geometry() == ButtonOnly) {
543                                 setStatus(cur, Open);
544                                 edit(cur, true);
545                         }
546                         else {
547                                 setStatus(cur, Collapsed);
548                         }
549                         cur.bv().cursor() = cur;
550                         break;
551                 }
552
553                 // The mouse click is within the opened inset.
554                 if (geometry() == TopButton
555                  || geometry() == LeftButton)
556                         InsetText::doDispatch(cur, cmd);
557                 break;
558
559         case LFUN_INSET_TOGGLE:
560                 if (cmd.argument() == "open")
561                         setStatus(cur, Open);
562                 else if (cmd.argument() == "close")
563                         setStatus(cur, Collapsed);
564                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
565                         if (internalStatus() == Open) {
566                                 setStatus(cur, Collapsed);
567                                 if (geometry() == ButtonOnly)
568                                         cur.top().forwardPos();
569                         } else
570                                 setStatus(cur, Open);
571                 else // if assign or anything else
572                         cur.undispatched();
573                 cur.dispatched();
574                 break;
575
576         case LFUN_PASTE:
577         case LFUN_CLIPBOARD_PASTE:
578         case LFUN_PRIMARY_SELECTION_PASTE: {
579                 InsetText::doDispatch(cur, cmd);
580                 // Since we can only store plain text, we must reset all
581                 // attributes.
582                 // FIXME: Change only the pasted paragraphs
583
584                 resetParagraphsFont();
585                 break;
586         }
587
588         default:
589                 if (layout_ && layout_->forceltr) {
590                         // Force any new text to latex_language
591                         // FIXME: This should only be necessary in constructor, but
592                         // new paragraphs that are created by pressing enter at the
593                         // start of an existing paragraph get the buffer language
594                         // and not latex_language, so we take this brute force
595                         // approach.
596                         cur.current_font.setLanguage(latex_language);
597                         cur.real_current_font.setLanguage(latex_language);
598                 }
599                 InsetText::doDispatch(cur, cmd);
600                 break;
601         }
602 }
603
604
605 bool InsetCollapsable::allowMultiPar() const
606 {
607         return layout_->multipar;
608 }
609
610
611 void InsetCollapsable::resetParagraphsFont()
612 {
613         Font font;
614         font.fontInfo() = sane_font;
615         if (layout_->forceltr)
616                 font.setLanguage(latex_language);
617         if (layout_->passthru) {
618                 ParagraphList::iterator par = paragraphs().begin();
619                 ParagraphList::iterator const end = paragraphs().end();
620                 while (par != end) {
621                         par->resetFonts(font);
622                         par->params().clear();
623                         ++par;
624                 }
625         }
626 }
627
628
629 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
630                 FuncStatus & flag) const
631 {
632         switch (cmd.action) {
633         // suppress these
634         case LFUN_ACCENT_ACUTE:
635         case LFUN_ACCENT_BREVE:
636         case LFUN_ACCENT_CARON:
637         case LFUN_ACCENT_CEDILLA:
638         case LFUN_ACCENT_CIRCLE:
639         case LFUN_ACCENT_CIRCUMFLEX:
640         case LFUN_ACCENT_DOT:
641         case LFUN_ACCENT_GRAVE:
642         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
643         case LFUN_ACCENT_MACRON:
644         case LFUN_ACCENT_OGONEK:
645         case LFUN_ACCENT_SPECIAL_CARON:
646         case LFUN_ACCENT_TIE:
647         case LFUN_ACCENT_TILDE:
648         case LFUN_ACCENT_UMLAUT:
649         case LFUN_ACCENT_UNDERBAR:
650         case LFUN_ACCENT_UNDERDOT:
651         case LFUN_APPENDIX:
652         case LFUN_BIBITEM_INSERT:
653         case LFUN_BOX_INSERT:
654         case LFUN_BRANCH_INSERT:
655         case LFUN_BREAK_LINE:
656         case LFUN_CAPTION_INSERT:
657         case LFUN_CLEARPAGE_INSERT:
658         case LFUN_CLEARDOUBLEPAGE_INSERT:
659         case LFUN_DEPTH_DECREMENT:
660         case LFUN_DEPTH_INCREMENT:
661         case LFUN_DOTS_INSERT:
662         case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
663         case LFUN_ENVIRONMENT_INSERT:
664         case LFUN_ERT_INSERT:
665         case LFUN_FILE_INSERT:
666         case LFUN_FLEX_INSERT:
667         case LFUN_FLOAT_INSERT:
668         case LFUN_FLOAT_LIST:
669         case LFUN_FLOAT_WIDE_INSERT:
670         case LFUN_FONT_BOLD:
671         case LFUN_FONT_TYPEWRITER:
672         case LFUN_FONT_DEFAULT:
673         case LFUN_FONT_EMPH:
674         case LFUN_FONT_FREE_APPLY:
675         case LFUN_FONT_FREE_UPDATE:
676         case LFUN_FONT_NOUN:
677         case LFUN_FONT_ROMAN:
678         case LFUN_FONT_SANS:
679         case LFUN_FONT_FRAK:
680         case LFUN_FONT_ITAL:
681         case LFUN_FONT_SIZE:
682         case LFUN_FONT_STATE:
683         case LFUN_FONT_UNDERLINE:
684         case LFUN_FOOTNOTE_INSERT:
685         case LFUN_HFILL_INSERT:
686         case LFUN_HYPERLINK_INSERT:
687         case LFUN_HYPHENATION_POINT_INSERT:
688         case LFUN_INDEX_INSERT:
689         case LFUN_INDEX_PRINT:
690         case LFUN_INSET_INSERT:
691         case LFUN_LABEL_GOTO:
692         case LFUN_LABEL_INSERT:
693         case LFUN_LIGATURE_BREAK_INSERT:
694         case LFUN_LINE_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