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