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