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