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