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