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