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