]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
Move some stuff from InsetFlex to InsetCollapsable, and make use of the
[features.git] / src / insets / InsetCollapsable.cpp
1 /**
2  * \file InsetCollapsable.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS.
11  */
12
13 #include <config.h>
14
15 #include "InsetCollapsable.h"
16
17 #include "Buffer.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
20 #include "Cursor.h"
21 #include "Dimension.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
26 #include "InsetLayout.h"
27 #include "Language.h"
28 #include "LaTeXFeatures.h"
29 #include "Lexer.h"
30 #include "MetricsInfo.h"
31 #include "paragraph_funcs.h"
32 #include "ParagraphParameters.h"
33 #include "sgml.h"
34 #include "TextClass.h"
35
36 #include "frontends/FontMetrics.h"
37 #include "frontends/Painter.h"
38
39 #include "support/debug.h"
40 #include "support/docstream.h"
41 #include "support/gettext.h"
42 #include "support/lassert.h"
43
44 using namespace std;
45
46
47 namespace lyx {
48
49 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
50 {
51         if (decoration() == InsetLayout::CONGLOMERATE)
52                 return status_;
53         return autoOpen_ ? Open : status_;
54 }
55
56
57 InsetCollapsable::Geometry InsetCollapsable::geometry() const
58 {
59         switch (decoration()) {
60         case InsetLayout::CLASSIC:
61                 if (status() == Open)
62                         return openinlined_ ? LeftButton : TopButton;
63                 return ButtonOnly;
64
65         case InsetLayout::MINIMALISTIC:
66                 return status() == Open ? NoButton : ButtonOnly ;
67
68         case InsetLayout::CONGLOMERATE:
69                 return status() == Open ? SubLabel : Corners ;
70
71         case InsetLayout::DEFAULT:
72                 break; // this shouldn't happen
73         }
74
75         // dummy return value to shut down a warning,
76         // this is dead code.
77         return NoButton;
78 }
79
80
81 InsetCollapsable::InsetCollapsable(Buffer const & buf)
82         : InsetText(buf), status_(Inset::Open),
83           openinlined_(false), autoOpen_(false), mouse_hover_(false)
84 {
85         DocumentClass const & dc = buf.params().documentClass();
86         setLayout(&dc);
87         setAutoBreakRows(true);
88         setDrawFrame(true);
89         setFrameColor(Color_collapsableframe);
90         paragraphs().back().setPlainLayout(dc); 
91 }
92
93
94 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
95         : InsetText(rhs),
96           status_(rhs.status_),
97           layout_(rhs.layout_),
98           labelstring_(rhs.labelstring_),
99           button_dim(rhs.button_dim),
100           openinlined_(rhs.openinlined_),
101           autoOpen_(rhs.autoOpen_),
102           // the sole purpose of this copy constructor
103           mouse_hover_(false)
104 {
105 }
106
107
108 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
109 {
110         Dimension dim = dimensionCollapsed();
111         if (geometry() == NoButton)
112                 return translateIfPossible(layout_->labelstring());
113         if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen())
114                 return docstring();
115
116         OutputParams rp(&buffer().params().encoding());
117         odocstringstream ods;
118         InsetText::plaintext(ods, rp);
119         docstring content_tip = ods.str();
120         // shorten it if necessary
121         if (content_tip.size() > 200)
122                 content_tip = content_tip.substr(0, 200) + "...";
123         return content_tip;
124 }
125
126
127 void InsetCollapsable::setLayout(BufferParams const & bp)
128 {
129         setLayout(bp.documentClassPtr());
130 }
131
132
133 void InsetCollapsable::setLayout(DocumentClass const * const dc)
134 {
135         if (dc) {
136                 layout_ = &(dc->insetLayout(name()));
137                 labelstring_ = translateIfPossible(layout_->labelstring());
138         } else {
139                 layout_ = &DocumentClass::plainInsetLayout();
140                 labelstring_ = _("UNDEFINED");
141         }
142
143         setButtonLabel();
144 }
145
146
147 void InsetCollapsable::write(ostream & os) const
148 {
149         os << "status ";
150         switch (status_) {
151         case Open:
152                 os << "open";
153                 break;
154         case Collapsed:
155                 os << "collapsed";
156                 break;
157         }
158         os << "\n";
159         text().write(buffer(), os);
160 }
161
162
163 void InsetCollapsable::read(Lexer & lex)
164 {
165         lex.setContext("InsetCollapsable::read");
166         string tmp_token;
167         status_ = Collapsed;
168         lex >> "status" >> tmp_token;
169         if (tmp_token == "open")
170                 status_ = Open;
171
172         // this must be set before we enter InsetText::read()
173         setLayout(buffer().params());
174         InsetText::read(lex);
175         // set button label again as the inset contents was not read yet at
176         // setLayout() time.
177         setButtonLabel();
178
179         // Force default font, if so requested
180         // This avoids paragraphs in buffer language that would have a
181         // foreign language after a document language change, and it ensures
182         // that all new text in ERT and similar gets the "latex" language,
183         // since new text inherits the language from the last position of the
184         // existing text.  As a side effect this makes us also robust against
185         // bugs in LyX that might lead to font changes in ERT in .lyx files.
186         resetParagraphsFont();
187 }
188
189
190 Dimension InsetCollapsable::dimensionCollapsed() const
191 {
192         LASSERT(layout_, /**/);
193         Dimension dim;
194         theFontMetrics(layout_->labelfont()).buttonText(
195                 labelstring_, dim.wid, dim.asc, dim.des);
196         return dim;
197 }
198
199
200 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
201 {
202         LASSERT(layout_, /**/);
203
204         autoOpen_ = mi.base.bv->cursor().isInside(this);
205
206         FontInfo tmpfont = mi.base.font;
207         mi.base.font = layout_->font();
208         mi.base.font.realize(tmpfont);
209
210         switch (geometry()) {
211         case NoButton:
212                 InsetText::metrics(mi, dim);
213                 break;
214         case Corners:
215                 InsetText::metrics(mi, dim);
216                 dim.des -= 3;
217                 dim.asc -= 1;
218                 break;
219         case SubLabel: {
220                 InsetText::metrics(mi, dim);
221                 // consider width of the inset label
222                 FontInfo font(layout_->labelfont());
223                 font.realize(sane_font);
224                 font.decSize();
225                 font.decSize();
226                 int w = 0;
227                 int a = 0;
228                 int d = 0;
229                 theFontMetrics(font).rectText(labelstring_, w, a, d);
230                 dim.des += a + d;
231                 break;
232                 }
233         case TopButton:
234         case LeftButton:
235         case ButtonOnly:
236                 dim = dimensionCollapsed();
237                 if (geometry() == TopButton || geometry() == LeftButton) {
238                         Dimension textdim;
239                         InsetText::metrics(mi, textdim);
240                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
241                         if (openinlined_) {
242                                 // Correct for button width.
243                                 dim.wid += textdim.wid;
244                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
245                                 dim.asc = textdim.asc;
246                         } else {
247                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
248                                 dim.wid = max(dim.wid, textdim.wid);
249                         }
250                 }
251                 break;
252         }
253
254         mi.base.font = tmpfont;
255 }
256
257
258 bool InsetCollapsable::setMouseHover(bool mouse_hover)
259 {
260         mouse_hover_ = mouse_hover;
261         return true;
262 }
263
264
265 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
266 {
267         LASSERT(layout_, /**/);
268
269         autoOpen_ = pi.base.bv->cursor().isInside(this);
270
271         FontInfo tmpfont = pi.base.font;
272         pi.base.font = layout_->font();
273         pi.base.font.realize(tmpfont);
274
275         // Draw button first -- top, left or only
276         Dimension dimc = dimensionCollapsed();
277
278         if (geometry() == TopButton ||
279             geometry() == LeftButton ||
280             geometry() == ButtonOnly) {
281                 button_dim.x1 = x + 0;
282                 button_dim.x2 = x + dimc.width();
283                 button_dim.y1 = y - dimc.asc;
284                 button_dim.y2 = y + dimc.des;
285
286                 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont(),
287                         mouse_hover_);
288         } else {
289                 button_dim.x1 = 0;
290                 button_dim.y1 = 0;
291                 button_dim.x2 = 0;
292                 button_dim.y2 = 0;
293         }
294
295         Dimension const textdim = InsetText::dimension(*pi.base.bv);
296         int const baseline = y;
297         int textx, texty;
298         switch (geometry()) {
299         case LeftButton:
300                 textx = x + dimc.width();
301                 texty = baseline;
302                 InsetText::draw(pi, textx, texty);
303                 break;
304         case TopButton:
305                 textx = x;
306                 texty = baseline + dimc.des + textdim.asc;
307                 InsetText::draw(pi, textx, texty);
308                 break;
309         case ButtonOnly:
310                 break;
311         case NoButton:
312                 textx = x;
313                 texty = baseline;
314                 InsetText::draw(pi, textx, texty);
315                 break;
316         case SubLabel:
317         case Corners:
318                 textx = x;
319                 texty = baseline;
320                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
321                 InsetText::draw(pi, textx, texty);
322                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
323
324                 int desc = textdim.descent();
325                 if (geometry() == Corners)
326                         desc -= 3;
327
328                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
329                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
330                 pi.pain.line(xx1, y + desc - 4, 
331                              xx1, y + desc, 
332                         layout_->labelfont().color());
333                 if (status_ == Open)
334                         pi.pain.line(xx1, y + desc, 
335                                 xx2, y + desc,
336                                 layout_->labelfont().color());
337                 else {
338                         // Make status_ value visible:
339                         pi.pain.line(xx1, y + desc,
340                                 xx1 + 4, y + desc,
341                                 layout_->labelfont().color());
342                         pi.pain.line(xx2 - 4, y + desc,
343                                 xx2, y + desc,
344                                 layout_->labelfont().color());
345                 }
346                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
347                         y + desc - 4, layout_->labelfont().color());
348
349                 // the label below the text. Can be toggled.
350                 if (geometry() == SubLabel) {
351                         FontInfo font(layout_->labelfont());
352                         font.realize(sane_font);
353                         font.decSize();
354                         font.decSize();
355                         int w = 0;
356                         int a = 0;
357                         int d = 0;
358                         theFontMetrics(font).rectText(labelstring_, w, a, d);
359                         int const ww = max(textdim.wid, w);
360                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
361                                 labelstring_, font, Color_none, Color_none);
362                         desc += d;
363                 }
364
365                 // a visual cue when the cursor is inside the inset
366                 Cursor & cur = pi.base.bv->cursor();
367                 if (cur.isInside(this)) {
368                         y -= textdim.asc;
369                         y += 3;
370                         pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
371                         pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
372                         pi.pain.line(xx2, y + 4, xx2, y,
373                                 layout_->labelfont().color());
374                         pi.pain.line(xx2 - 4, y, xx2, y,
375                                 layout_->labelfont().color());
376                 }
377                 break;
378         }
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_PHANTOM_INSERT:
689         case LFUN_REFERENCE_NEXT:
690         case LFUN_SERVER_GOTO_FILE_ROW:
691         case LFUN_SERVER_NOTIFY:
692         case LFUN_SERVER_SET_XY:
693         case LFUN_SPACE_INSERT:
694         case LFUN_SPECIALCHAR_INSERT:
695         case LFUN_TABULAR_INSERT:
696         case LFUN_TOC_INSERT:
697         case LFUN_WRAP_INSERT:
698                 if (layout_->isPassThru()) {
699                         flag.setEnabled(false);
700                         return true;
701                 }
702                 return InsetText::getStatus(cur, cmd, flag);
703
704         case LFUN_INSET_TOGGLE:
705                 if (cmd.argument() == "open")
706                         flag.setEnabled(status_ != Open);
707                 else if (cmd.argument() == "close")
708                         flag.setEnabled(status_ == Open);
709                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
710                         flag.setEnabled(true);
711                         flag.setOnOff(status_ == Open);
712                 } else
713                         flag.setEnabled(false);
714                 return true;
715
716         case LFUN_LANGUAGE:
717                 flag.setEnabled(!layout_->isForceLtr());
718                 return InsetText::getStatus(cur, cmd, flag);
719
720         case LFUN_BREAK_PARAGRAPH:
721                 flag.setEnabled(layout_->isMultiPar());
722                 return true;
723
724         default:
725                 return InsetText::getStatus(cur, cmd, flag);
726         }
727 }
728
729
730 void InsetCollapsable::setLabel(docstring const & l)
731 {
732         labelstring_ = l;
733 }
734
735
736 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
737 {
738         status_ = status;
739         setButtonLabel();
740         if (status_ == Collapsed) {
741                 cur.leaveInset(*this);
742                 mouse_hover_ = false;
743         }
744 }
745
746
747 docstring InsetCollapsable::floatName(
748                 string const & type, BufferParams const & bp) const
749 {
750         FloatList const & floats = bp.documentClass().floats();
751         FloatList::const_iterator it = floats[type];
752         // FIXME UNICODE
753         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
754 }
755
756
757 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
758 {
759         if (!layout_)
760                 return InsetLayout::CLASSIC;
761         InsetLayout::InsetDecoration const dec = layout_->decoration();
762         switch (dec) {
763         case InsetLayout::CLASSIC:
764         case InsetLayout::MINIMALISTIC:
765         case InsetLayout::CONGLOMERATE:
766                 return dec;
767         case InsetLayout::DEFAULT:
768                 break;
769         }
770         if (lyxCode() == FLEX_CODE)
771                 return InsetLayout::CONGLOMERATE;
772         return InsetLayout::CLASSIC;
773 }
774
775
776 int InsetCollapsable::latex(odocstream & os,
777                           OutputParams const & runparams) const
778 {
779         // FIXME: What should we do layout_ is 0?
780         // 1) assert
781         // 2) throw an error
782         if (!layout_)
783                 return 0;
784
785         // This implements the standard way of handling the LaTeX output of
786         // a collapsable inset, either a command or an environment. Standard 
787         // collapsable insets should not redefine this, non-standard ones may
788         // call this.
789         if (!layout_->latexname().empty()) {
790                 if (layout_->latextype() == InsetLayout::COMMAND) {
791                         // FIXME UNICODE
792                         if (runparams.moving_arg)
793                                 os << "\\protect";
794                         os << '\\' << from_utf8(layout_->latexname());
795                         if (!layout_->latexparam().empty())
796                                 os << from_utf8(layout_->latexparam());
797                         os << '{';
798                 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
799                         os << "%\n\\begin{" << from_utf8(layout_->latexname()) << "}\n";
800                         if (!layout_->latexparam().empty())
801                                 os << from_utf8(layout_->latexparam());
802                 }
803         }
804         OutputParams rp = runparams;
805         if (layout_->isPassThru())
806                 rp.verbatim = true;
807         if (layout_->isNeedProtect())
808                 rp.moving_arg = true;
809         int i = InsetText::latex(os, rp);
810         if (!layout_->latexname().empty()) {
811                 if (layout_->latextype() == InsetLayout::COMMAND) {
812                         os << "}";
813                 } else if (layout_->latextype() == InsetLayout::ENVIRONMENT) {
814                         os << "\n\\end{" << from_utf8(layout_->latexname()) << "}\n";
815                         i += 4;
816                 }
817         }
818         return i;
819 }
820
821
822 // FIXME It seems as if it ought to be possible to do this more simply,
823 // maybe by calling InsetText::docbook() in the middle there.
824 int InsetCollapsable::docbook(odocstream & os, OutputParams const & runparams) const
825 {
826         ParagraphList::const_iterator const beg = paragraphs().begin();
827         ParagraphList::const_iterator par = paragraphs().begin();
828         ParagraphList::const_iterator const end = paragraphs().end();
829
830         if (!undefined())
831                 sgml::openTag(os, getLayout().latexname(),
832                               par->getID(buffer(), runparams) + getLayout().latexparam());
833
834         for (; par != end; ++par) {
835                 par->simpleDocBookOnePar(buffer(), os, runparams,
836                                          outerFont(distance(beg, par),
837                                                    paragraphs()));
838         }
839
840         if (!undefined())
841                 sgml::closeTag(os, getLayout().latexname());
842
843         return 0;
844 }
845
846
847 void InsetCollapsable::validate(LaTeXFeatures & features) const
848 {
849         string const preamble = getLayout().preamble();
850         if (!preamble.empty())
851                 features.addPreambleSnippet(preamble);
852         features.require(getLayout().requires());
853         InsetText::validate(features);
854 }
855
856
857 bool InsetCollapsable::undefined() const
858 {
859         docstring const & n = getLayout().name();
860         return n.empty() || n == DocumentClass::plainInsetLayout().name();
861 }
862
863
864 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
865         int y) const
866 {
867         if (decoration() == InsetLayout::CONGLOMERATE)
868                 return from_ascii("context-conglomerate");
869
870         if (geometry() == NoButton)
871                 return from_ascii("context-collapsable");
872
873         Dimension dim = dimensionCollapsed();
874         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
875                 return from_ascii("context-collapsable");
876
877         return InsetText::contextMenu(bv, x, y);
878 }
879
880 void InsetCollapsable::tocString(odocstream & os) const
881 {
882         if (!getLayout().isInToc())
883                 return;
884         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
885 }
886
887
888 } // namespace lyx