]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
123e48fa0d5936871474cb266f71c45fd0b52d3a
[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 "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(BufferView const & bv) const
50 {
51         if (decoration() == InsetLayout::CONGLOMERATE)
52                 return status_;
53         return auto_open_[&bv] ? Open : status_;
54 }
55
56
57 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
58 {
59         switch (decoration()) {
60         case InsetLayout::CLASSIC:
61                 if (status(bv) == Open)
62                         return openinlined_ ? LeftButton : TopButton;
63                 return ButtonOnly;
64
65         case InsetLayout::MINIMALISTIC:
66                 return status(bv) == Open ? NoButton : ButtonOnly ;
67
68         case InsetLayout::CONGLOMERATE:
69                 return status(bv) == 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::Geometry InsetCollapsable::geometry() const
82 {
83         switch (decoration()) {
84         case InsetLayout::CLASSIC:
85                 if (status_ == Open)
86                         return openinlined_ ? LeftButton : TopButton;
87                 return ButtonOnly;
88
89         case InsetLayout::MINIMALISTIC:
90                 return status_ == Open ? NoButton : ButtonOnly ;
91
92         case InsetLayout::CONGLOMERATE:
93                 return status_ == Open ? SubLabel : Corners ;
94
95         case InsetLayout::DEFAULT:
96                 break; // this shouldn't happen
97         }
98
99         // dummy return value to shut down a warning,
100         // this is dead code.
101         return NoButton;
102 }
103
104
105 InsetCollapsable::InsetCollapsable(Buffer const & buf)
106         : InsetText(buf), status_(Inset::Open),
107           openinlined_(false), mouse_hover_(false)
108 {
109         DocumentClass const & dc = buf.params().documentClass();
110         setLayout(&dc);
111         setAutoBreakRows(true);
112         setDrawFrame(true);
113         setFrameColor(Color_collapsableframe);
114         paragraphs().back().setPlainLayout(dc); 
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 content_tip = ods.str();
144         // shorten it if necessary
145         if (content_tip.size() > 200)
146                 content_tip = content_tip.substr(0, 200) + "...";
147         return content_tip;
148 }
149
150
151 void InsetCollapsable::setLayout(BufferParams const & bp)
152 {
153         setLayout(bp.documentClassPtr());
154 }
155
156
157 void InsetCollapsable::setLayout(DocumentClass const * const dc)
158 {
159         if (dc) {
160                 layout_ = &(dc->insetLayout(name()));
161                 labelstring_ = translateIfPossible(layout_->labelstring());
162         } else {
163                 layout_ = &DocumentClass::plainInsetLayout();
164                 labelstring_ = _("UNDEFINED");
165         }
166
167         setButtonLabel();
168 }
169
170
171 void InsetCollapsable::write(ostream & os) const
172 {
173         os << "status ";
174         switch (status_) {
175         case Open:
176                 os << "open";
177                 break;
178         case Collapsed:
179                 os << "collapsed";
180                 break;
181         }
182         os << "\n";
183         text().write(buffer(), os);
184 }
185
186
187 void InsetCollapsable::read(Lexer & lex)
188 {
189         lex.setContext("InsetCollapsable::read");
190         string tmp_token;
191         status_ = Collapsed;
192         lex >> "status" >> tmp_token;
193         if (tmp_token == "open")
194                 status_ = Open;
195
196         // this must be set before we enter InsetText::read()
197         setLayout(buffer().params());
198         InsetText::read(lex);
199         // set button label again as the inset contents was not read yet at
200         // setLayout() time.
201         setButtonLabel();
202
203         // Force default font, if so requested
204         // This avoids paragraphs in buffer language that would have a
205         // foreign language after a document language change, and it ensures
206         // that all new text in ERT and similar gets the "latex" language,
207         // since new text inherits the language from the last position of the
208         // existing text.  As a side effect this makes us also robust against
209         // bugs in LyX that might lead to font changes in ERT in .lyx files.
210         resetParagraphsFont();
211 }
212
213
214 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
215 {
216         LASSERT(layout_, /**/);
217         Dimension dim;
218         theFontMetrics(layout_->labelfont()).buttonText(
219                 buttonLabel(bv), dim.wid, dim.asc, dim.des);
220         return dim;
221 }
222
223
224 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
225 {
226         LASSERT(layout_, /**/);
227
228         auto_open_[mi.base.bv] =  mi.base.bv->cursor().isInside(this);
229
230         FontInfo tmpfont = mi.base.font;
231         mi.base.font = layout_->font();
232         mi.base.font.realize(tmpfont);
233
234         BufferView const & bv = *mi.base.bv;
235
236         switch (geometry(bv)) {
237         case NoButton:
238                 InsetText::metrics(mi, dim);
239                 break;
240         case Corners:
241                 InsetText::metrics(mi, dim);
242                 dim.des -= 3;
243                 dim.asc -= 1;
244                 break;
245         case SubLabel: {
246                 InsetText::metrics(mi, dim);
247                 // consider width of the inset label
248                 FontInfo font(layout_->labelfont());
249                 font.realize(sane_font);
250                 font.decSize();
251                 font.decSize();
252                 int w = 0;
253                 int a = 0;
254                 int d = 0;
255                 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
256                 dim.des += a + d;
257                 break;
258                 }
259         case TopButton:
260         case LeftButton:
261         case ButtonOnly:
262                 dim = dimensionCollapsed(bv);
263                 if (geometry(bv) == TopButton 
264                           || geometry(bv) == LeftButton) {
265                         Dimension textdim;
266                         InsetText::metrics(mi, textdim);
267                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
268                         if (openinlined_) {
269                                 // Correct for button width.
270                                 dim.wid += textdim.wid;
271                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
272                                 dim.asc = textdim.asc;
273                         } else {
274                                 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
275                                 dim.wid = max(dim.wid, textdim.wid);
276                         }
277                 }
278                 break;
279         }
280
281         mi.base.font = tmpfont;
282 }
283
284
285 bool InsetCollapsable::setMouseHover(bool mouse_hover)
286 {
287         mouse_hover_ = mouse_hover;
288         return true;
289 }
290
291
292 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
293 {
294         LASSERT(layout_, /**/);
295         BufferView const & bv = *pi.base.bv;
296
297         auto_open_[&bv] =  bv.cursor().isInside(this);
298
299         FontInfo tmpfont = pi.base.font;
300         pi.base.font = layout_->font();
301         pi.base.font.realize(tmpfont);
302
303         // Draw button first -- top, left or only
304         Dimension dimc = dimensionCollapsed(bv);
305
306         if (geometry(*pi.base.bv) == TopButton ||
307             geometry(*pi.base.bv) == LeftButton ||
308             geometry(*pi.base.bv) == ButtonOnly) {
309                 button_dim.x1 = x + 0;
310                 button_dim.x2 = x + dimc.width();
311                 button_dim.y1 = y - dimc.asc;
312                 button_dim.y2 = y + dimc.des;
313
314                 pi.pain.buttonText(x, y, buttonLabel(bv), layout_->labelfont(),
315                         mouse_hover_);
316         } else {
317                 button_dim.x1 = 0;
318                 button_dim.y1 = 0;
319                 button_dim.x2 = 0;
320                 button_dim.y2 = 0;
321         }
322
323         Dimension const textdim = InsetText::dimension(bv);
324         int const baseline = y;
325         int textx, texty;
326         switch (geometry(bv)) {
327         case LeftButton:
328                 textx = x + dimc.width();
329                 texty = baseline;
330                 InsetText::draw(pi, textx, texty);
331                 break;
332         case TopButton:
333                 textx = x;
334                 texty = baseline + dimc.des + textdim.asc;
335                 InsetText::draw(pi, textx, texty);
336                 break;
337         case ButtonOnly:
338                 break;
339         case NoButton:
340                 textx = x;
341                 texty = baseline;
342                 InsetText::draw(pi, textx, texty);
343                 break;
344         case SubLabel:
345         case Corners:
346                 textx = x;
347                 texty = baseline;
348                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
349                 InsetText::draw(pi, textx, texty);
350                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
351
352                 int desc = textdim.descent();
353                 if (geometry(bv) == Corners)
354                         desc -= 3;
355
356                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
357                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
358                 pi.pain.line(xx1, y + desc - 4, 
359                              xx1, y + desc, 
360                         layout_->labelfont().color());
361                 if (status_ == Open)
362                         pi.pain.line(xx1, y + desc, 
363                                 xx2, y + desc,
364                                 layout_->labelfont().color());
365                 else {
366                         // Make status_ value visible:
367                         pi.pain.line(xx1, y + desc,
368                                 xx1 + 4, y + desc,
369                                 layout_->labelfont().color());
370                         pi.pain.line(xx2 - 4, y + desc,
371                                 xx2, y + desc,
372                                 layout_->labelfont().color());
373                 }
374                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
375                         y + desc - 4, layout_->labelfont().color());
376
377                 // the label below the text. Can be toggled.
378                 if (geometry(bv) == SubLabel) {
379                         FontInfo font(layout_->labelfont());
380                         font.realize(sane_font);
381                         font.decSize();
382                         font.decSize();
383                         int w = 0;
384                         int a = 0;
385                         int d = 0;
386                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
387                         int const ww = max(textdim.wid, w);
388                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
389                                 buttonLabel(bv), font, Color_none, Color_none);
390                         desc += d;
391                 }
392
393                 // a visual cue when the cursor is inside the inset
394                 Cursor const & cur = bv.cursor();
395                 if (cur.isInside(this)) {
396                         y -= textdim.asc;
397                         y += 3;
398                         pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont().color());
399                         pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont().color());
400                         pi.pain.line(xx2, y + 4, xx2, y,
401                                 layout_->labelfont().color());
402                         pi.pain.line(xx2 - 4, y, xx2, y,
403                                 layout_->labelfont().color());
404                 }
405                 break;
406         }
407
408         pi.base.font = tmpfont;
409 }
410
411
412 void InsetCollapsable::cursorPos(BufferView const & bv,
413                 CursorSlice const & sl, bool boundary, int & x, int & y) const
414 {
415         if (geometry(bv) == ButtonOnly)
416                 status_ = Open;
417         LASSERT(geometry(bv) != ButtonOnly, /**/);
418
419         InsetText::cursorPos(bv, sl, boundary, x, y);
420         Dimension const textdim = InsetText::dimension(bv);
421
422         switch (geometry(bv)) {
423         case LeftButton:
424                 x += dimensionCollapsed(bv).wid;
425                 break;
426         case TopButton: {
427                 y += dimensionCollapsed(bv).des + textdim.asc;
428                 break;
429         }
430         case NoButton:
431         case SubLabel:
432         case Corners:
433                 // Do nothing
434                 break;
435         case ButtonOnly:
436                 // Cannot get here
437                 break;
438         }
439 }
440
441
442 Inset::EDITABLE InsetCollapsable::editable() const
443 {
444         return geometry() != ButtonOnly ? HIGHLY_EDITABLE : IS_EDITABLE;
445 }
446
447
448 bool InsetCollapsable::descendable() const
449 {
450         return geometry() != ButtonOnly;
451 }
452
453
454 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
455 {
456         return button_dim.contains(cmd.x, cmd.y);
457 }
458
459
460 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
461 {
462         docstring label;
463         pos_type const max_length = 15;
464         pos_type const p_siz = paragraphs().begin()->size();
465         pos_type const n = min(max_length, p_siz);
466         pos_type i = 0;
467         pos_type j = 0;
468         for (; i < n && j < p_siz; ++j) {
469                 if (paragraphs().begin()->isInset(j))
470                         continue;
471                 label += paragraphs().begin()->getChar(j);
472                 ++i;
473         }
474         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
475                 label += "...";
476         }
477         return label.empty() ? l : label;
478 }
479
480
481 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
482 {
483         //lyxerr << "InsetCollapsable: edit left/right" << endl;
484         cur.push(*this);
485         InsetText::edit(cur, front, entry_from);
486 }
487
488
489 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
490 {
491         //lyxerr << "InsetCollapsable: edit xy" << endl;
492         if (geometry(cur.bv()) == ButtonOnly
493          || (button_dim.contains(x, y) 
494           && geometry(cur.bv()) != NoButton))
495                 return this;
496         cur.push(*this);
497         return InsetText::editXY(cur, x, y);
498 }
499
500
501 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
502 {
503         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
504         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
505
506         switch (cmd.action) {
507         case LFUN_MOUSE_PRESS:
508                 if (hitButton(cmd)) {
509                         switch (cmd.button()) {
510                         case mouse_button::button1:
511                         case mouse_button::button3:
512                                 // Pass the command to the enclosing InsetText,
513                                 // so that the cursor gets set.
514                                 cur.undispatched();
515                                 break;
516                         case mouse_button::none:
517                         case mouse_button::button2:
518                         case mouse_button::button4:
519                         case mouse_button::button5:
520                                 // Nothing to do.
521                                 cur.noUpdate();
522                                 break;
523                         }
524                 } else if (geometry(cur.bv()) != ButtonOnly)
525                         InsetText::doDispatch(cur, cmd);
526                 else
527                         cur.undispatched();
528                 break;
529
530         case LFUN_MOUSE_MOTION:
531         case LFUN_MOUSE_DOUBLE:
532         case LFUN_MOUSE_TRIPLE:
533                 if (hitButton(cmd)) 
534                         cur.noUpdate();
535                 else if (geometry(cur.bv()) != ButtonOnly)
536                         InsetText::doDispatch(cur, cmd);
537                 else
538                         cur.undispatched();
539                 break;
540
541         case LFUN_MOUSE_RELEASE:
542                 if (!hitButton(cmd)) {
543                         // The mouse click has to be within the inset!
544                         if (geometry(cur.bv()) != ButtonOnly)
545                                 InsetText::doDispatch(cur, cmd);
546                         else
547                                 cur.undispatched();                     
548                         break;
549                 }
550                 if (cmd.button() != mouse_button::button1) {
551                         // Nothing to do.
552                         cur.noUpdate();
553                         break;
554                 }
555                 // if we are selecting, we do not want to
556                 // toggle the inset.
557                 if (cur.selection())
558                         break;
559                 // Left button is clicked, the user asks to
560                 // toggle the inset visual state.
561                 cur.dispatched();
562                 cur.updateFlags(Update::Force | Update::FitCursor);
563                 if (geometry(cur.bv()) == ButtonOnly) {
564                         setStatus(cur, Open);
565                         edit(cur, true);
566                 }
567                 else
568                         setStatus(cur, Collapsed);
569                 cur.bv().cursor() = cur;
570                 break;
571
572         case LFUN_INSET_TOGGLE:
573                 if (cmd.argument() == "open")
574                         setStatus(cur, Open);
575                 else if (cmd.argument() == "close")
576                         setStatus(cur, Collapsed);
577                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
578                         if (status_ == Open) {
579                                 setStatus(cur, Collapsed);
580                                 if (geometry(cur.bv()) == ButtonOnly)
581                                         cur.top().forwardPos();
582                         } else
583                                 setStatus(cur, Open);
584                 else // if assign or anything else
585                         cur.undispatched();
586                 cur.dispatched();
587                 break;
588
589         case LFUN_PASTE:
590         case LFUN_CLIPBOARD_PASTE:
591         case LFUN_PRIMARY_SELECTION_PASTE: {
592                 InsetText::doDispatch(cur, cmd);
593                 // Since we can only store plain text, we must reset all
594                 // attributes.
595                 // FIXME: Change only the pasted paragraphs
596
597                 resetParagraphsFont();
598                 break;
599         }
600
601         default:
602                 if (layout_ && layout_->isForceLtr()) {
603                         // Force any new text to latex_language
604                         // FIXME: This should only be necessary in constructor, but
605                         // new paragraphs that are created by pressing enter at the
606                         // start of an existing paragraph get the buffer language
607                         // and not latex_language, so we take this brute force
608                         // approach.
609                         cur.current_font.setLanguage(latex_language);
610                         cur.real_current_font.setLanguage(latex_language);
611                 }
612                 InsetText::doDispatch(cur, cmd);
613                 break;
614         }
615 }
616
617
618 bool InsetCollapsable::allowMultiPar() const
619 {
620         return layout_->isMultiPar();
621 }
622
623
624 void InsetCollapsable::resetParagraphsFont()
625 {
626         Font font;
627         font.fontInfo() = inherit_font;
628         if (layout_->isForceLtr())
629                 font.setLanguage(latex_language);
630         if (layout_->isPassThru()) {
631                 ParagraphList::iterator par = paragraphs().begin();
632                 ParagraphList::iterator const end = paragraphs().end();
633                 while (par != end) {
634                         par->resetFonts(font);
635                         par->params().clear();
636                         ++par;
637                 }
638         }
639 }
640
641
642 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
643                 FuncStatus & flag) const
644 {
645         switch (cmd.action) {
646         // FIXME At present, these are being enabled and disabled according to
647         // whether PASSTHRU has been set in the InsetLayout. This makes some
648         // sense, but there are other checks that should really be done. E.g.,
649         // one should not be able to inset IndexPrint inside an optional argument!!
650         case LFUN_ACCENT_ACUTE:
651         case LFUN_ACCENT_BREVE:
652         case LFUN_ACCENT_CARON:
653         case LFUN_ACCENT_CEDILLA:
654         case LFUN_ACCENT_CIRCLE:
655         case LFUN_ACCENT_CIRCUMFLEX:
656         case LFUN_ACCENT_DOT:
657         case LFUN_ACCENT_GRAVE:
658         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
659         case LFUN_ACCENT_MACRON:
660         case LFUN_ACCENT_OGONEK:
661         case LFUN_ACCENT_TIE:
662         case LFUN_ACCENT_TILDE:
663         case LFUN_ACCENT_UMLAUT:
664         case LFUN_ACCENT_UNDERBAR:
665         case LFUN_ACCENT_UNDERDOT:
666         case LFUN_APPENDIX:
667         case LFUN_BOX_INSERT:
668         case LFUN_BRANCH_INSERT:
669         case LFUN_NEWLINE_INSERT:
670         case LFUN_CAPTION_INSERT:
671         case LFUN_DEPTH_DECREMENT:
672         case LFUN_DEPTH_INCREMENT:
673         case LFUN_ERT_INSERT:
674         case LFUN_FILE_INSERT:
675         case LFUN_FLEX_INSERT:
676         case LFUN_FLOAT_INSERT:
677         case LFUN_FLOAT_LIST_INSERT:
678         case LFUN_FLOAT_WIDE_INSERT:
679         case LFUN_FONT_BOLD:
680         case LFUN_FONT_BOLDSYMBOL:
681         case LFUN_FONT_TYPEWRITER:
682         case LFUN_FONT_DEFAULT:
683         case LFUN_FONT_EMPH:
684         case LFUN_TEXTSTYLE_APPLY:
685         case LFUN_TEXTSTYLE_UPDATE:
686         case LFUN_FONT_NOUN:
687         case LFUN_FONT_ROMAN:
688         case LFUN_FONT_SANS:
689         case LFUN_FONT_FRAK:
690         case LFUN_FONT_ITAL:
691         case LFUN_FONT_SIZE:
692         case LFUN_FONT_STATE:
693         case LFUN_FONT_UNDERLINE:
694         case LFUN_FOOTNOTE_INSERT:
695         case LFUN_HYPERLINK_INSERT:
696         case LFUN_INDEX_INSERT:
697         case LFUN_INDEX_PRINT:
698         case LFUN_INSET_INSERT:
699         case LFUN_LABEL_GOTO:
700         case LFUN_LABEL_INSERT:
701         case LFUN_LINE_INSERT:
702         case LFUN_NEWPAGE_INSERT:
703         case LFUN_LAYOUT_TABULAR:
704         case LFUN_MARGINALNOTE_INSERT:
705         case LFUN_MATH_DISPLAY:
706         case LFUN_MATH_INSERT:
707         case LFUN_MATH_MATRIX:
708         case LFUN_MATH_MODE:
709         case LFUN_MENU_OPEN:
710         case LFUN_NOACTION:
711         case LFUN_NOMENCL_INSERT:
712         case LFUN_NOMENCL_PRINT:
713         case LFUN_NOTE_INSERT:
714         case LFUN_NOTE_NEXT:
715         case LFUN_OPTIONAL_INSERT:
716         case LFUN_PHANTOM_INSERT:
717         case LFUN_REFERENCE_NEXT:
718         case LFUN_SERVER_GOTO_FILE_ROW:
719         case LFUN_SERVER_NOTIFY:
720         case LFUN_SERVER_SET_XY:
721         case LFUN_SPACE_INSERT:
722         case LFUN_SPECIALCHAR_INSERT:
723         case LFUN_TABULAR_INSERT:
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 void InsetCollapsable::validate(LaTeXFeatures & features) const
876 {
877         string const preamble = getLayout().preamble();
878         if (!preamble.empty())
879                 features.addPreambleSnippet(preamble);
880         features.require(getLayout().requires());
881         InsetText::validate(features);
882 }
883
884
885 bool InsetCollapsable::undefined() const
886 {
887         docstring const & n = getLayout().name();
888         return n.empty() || n == DocumentClass::plainInsetLayout().name();
889 }
890
891
892 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
893         int y) const
894 {
895         if (decoration() == InsetLayout::CONGLOMERATE)
896                 return from_ascii("context-conglomerate");
897
898         if (geometry(bv) == NoButton)
899                 return from_ascii("context-collapsable");
900
901         Dimension dim = dimensionCollapsed(bv);
902         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
903                 return from_ascii("context-collapsable");
904
905         return InsetText::contextMenu(bv, x, y);
906 }
907
908 void InsetCollapsable::tocString(odocstream & os) const
909 {
910         if (!getLayout().isInToc())
911                 return;
912         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
913 }
914
915
916 } // namespace lyx