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