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