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