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