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