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