]> git.lyx.org Git - lyx.git/blob - src/insets/InsetCollapsable.cpp
Fix thinko: we need to apply the default font here, not the inset's font commands
[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         setButtonLabel();
85         // Fallback for lacking inset layout item
86         layout_.bgcolor = Color_background;
87 }
88
89
90 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91         : InsetText(rhs),
92                 button_dim(rhs.button_dim),
93                 topx(rhs.topx),
94                 topbaseline(rhs.topbaseline),
95                 layout_(rhs.layout_),
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 void  InsetCollapsable::setLayout(BufferParams const & bp)
106 {
107         layout_ = getLayout(bp);
108 }
109
110
111 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
112 {
113         os << "status ";
114         switch (status_) {
115         case Open:
116                 os << "open";
117                 break;
118         case Collapsed:
119                 os << "collapsed";
120                 break;
121         }
122         os << "\n";
123         text_.write(buf, os);
124 }
125
126
127 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
128 {
129         bool token_found = false;
130         if (lex.isOK()) {
131                 lex.next();
132                 string const token = lex.getString();
133                 if (token == "status") {
134                         lex.next();
135                         string const tmp_token = lex.getString();
136
137                         if (tmp_token == "collapsed") {
138                                 status_ = Collapsed;
139                                 token_found = true;
140                         } else if (tmp_token == "open") {
141                                 status_ = Open;
142                                 token_found = true;
143                         } else {
144                                 lyxerr << "InsetCollapsable::read: Missing status!"
145                                        << endl;
146                                 // Take countermeasures
147                                 lex.pushToken(token);
148                         }
149                 } else {
150                         lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
151                                    << endl;
152                         // take countermeasures
153                         lex.pushToken(token);
154                 }
155         }
156         InsetText::read(buf, lex);
157
158         if (!token_found)
159                 status_ = isOpen() ? Open : Collapsed;
160
161         setButtonLabel();
162
163         // Force default font, if so requested
164         // This avoids paragraphs in buffer language that would have a
165         // foreign language after a document language change, and it ensures
166         // that all new text in ERT and similar gets the "latex" language,
167         // since new text inherits the language from the last position of the
168         // existing text.  As a side effect this makes us also robust against
169         // bugs in LyX that might lead to font changes in ERT in .lyx files.
170         resetParagraphsFont();
171 }
172
173
174 Dimension InsetCollapsable::dimensionCollapsed() const
175 {
176         Dimension dim;
177         theFontMetrics(layout_.labelfont).buttonText(
178                 layout_.labelstring, dim.wid, dim.asc, dim.des);
179         return dim;
180 }
181
182
183 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
184 {
185         autoOpen_ = mi.base.bv->cursor().isInside(this);
186
187         FontInfo tmpfont = mi.base.font;
188         getDrawFont(mi.base.font);
189         mi.base.font.realize(tmpfont);
190
191         switch (geometry()) {
192         case NoButton:
193                 InsetText::metrics(mi, dim);
194                 break;
195         case Corners:
196                 InsetText::metrics(mi, dim);
197                 dim.des -= 3;
198                 dim.asc -= 1;
199                 break;
200         case SubLabel: {
201                 InsetText::metrics(mi, dim);
202                 // consider width of the inset label
203                 FontInfo font(layout_.labelfont);
204                 font.realize(sane_font);
205                 font.decSize();
206                 font.decSize();
207                 int w = 0;
208                 int a = 0;
209                 int d = 0;
210                 docstring s = layout_.labelstring;
211                 theFontMetrics(font).rectText(s, w, a, d);
212                 dim.des += a + d;
213                 break;
214                 }
215         case TopButton:
216         case LeftButton:
217         case ButtonOnly:
218                 dim = dimensionCollapsed();
219                 if (geometry() == TopButton
220                  || geometry() == LeftButton) {
221                         Dimension textdim;
222                         InsetText::metrics(mi, textdim);
223                         openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
224                         if (openinlined_) {
225                                 // Correct for button width.
226                                 dim.wid += textdim.wid;
227                                 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
228                                 dim.asc = textdim.asc;
229                         } else {
230                                 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
231                                 dim.wid = max(dim.wid, textdim.wid);
232                         }
233                 }
234                 break;
235         }
236
237         mi.base.font = tmpfont;
238 }
239
240
241 bool InsetCollapsable::setMouseHover(bool mouse_hover)
242 {
243         mouse_hover_ = mouse_hover;
244         return true;
245 }
246
247
248 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
249 {
250         autoOpen_ = pi.base.bv->cursor().isInside(this);
251         ColorCode const old_color = pi.background_color;
252         pi.background_color = backgroundColor();
253
254         FontInfo tmpfont = pi.base.font;
255         getDrawFont(pi.base.font);
256         pi.base.font.realize(tmpfont);
257
258         // Draw button first -- top, left or only
259         Dimension dimc = dimensionCollapsed();
260
261         if (geometry() == TopButton ||
262             geometry() == LeftButton ||
263             geometry() == ButtonOnly) {
264                 button_dim.x1 = x + 0;
265                 button_dim.x2 = x + dimc.width();
266                 button_dim.y1 = y - dimc.asc;
267                 button_dim.y2 = y + dimc.des;
268
269                 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
270         } else {
271                 button_dim.x1 = 0;
272                 button_dim.y1 = 0;
273                 button_dim.x2 = 0;
274                 button_dim.y2 = 0;
275         }
276
277         Dimension const textdim = InsetText::dimension(*pi.base.bv);
278         int const baseline = y;
279         int textx, texty;
280         switch (geometry()) {
281         case LeftButton:
282                 textx = x + dimc.width();
283                 texty = baseline;
284                 InsetText::draw(pi, textx, texty);
285                 break;
286         case TopButton:
287                 textx = x;
288                 texty = baseline + dimc.des + textdim.asc;
289                 InsetText::draw(pi, textx, texty);
290                 break;
291         case ButtonOnly:
292                 break;
293         case NoButton:
294                 textx = x;
295                 texty = baseline;
296                 InsetText::draw(pi, textx, texty);
297                 break;
298         case SubLabel:
299         case Corners:
300                 textx = x;
301                 texty = baseline;
302                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
303                 InsetText::draw(pi, textx, texty);
304                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
305
306                 int desc = textdim.descent();
307                 if (geometry() == Corners)
308                         desc -= 3;
309
310                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
311                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
312                 pi.pain.line(xx1, y + desc - 4, 
313                              xx1, y + desc, 
314                         layout_.labelfont.color());
315                 if (internalStatus() == Open)
316                         pi.pain.line(xx1, y + desc, 
317                                 xx2, y + desc,
318                                 layout_.labelfont.color());
319                 else {
320                         // Make status_ value visible:
321                         pi.pain.line(xx1, y + desc,
322                                 xx1 + 4, y + desc,
323                                 layout_.labelfont.color());
324                         pi.pain.line(xx2 - 4, y + desc,
325                                 xx2, y + desc,
326                                 layout_.labelfont.color());
327                 }
328                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
329                         layout_.labelfont.color());
330
331                 // the label below the text. Can be toggled.
332                 if (geometry() == SubLabel) {
333                         FontInfo font(layout_.labelfont);
334                         font.realize(sane_font);
335                         font.decSize();
336                         font.decSize();
337                         int w = 0;
338                         int a = 0;
339                         int d = 0;
340                         docstring s = layout_.labelstring;
341                         theFontMetrics(font).rectText(s, w, a, d);
342                         int const ww = max(textdim.wid, w);
343                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
344                                 s, font, Color_none, Color_none);
345                         desc += d;
346                 }
347
348                 // a visual cue when the cursor is inside the inset
349                 Cursor & cur = pi.base.bv->cursor();
350                 if (cur.isInside(this)) {
351                         y -= textdim.asc;
352                         y += 3;
353                         pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
354                         pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
355                         pi.pain.line(xx2, y + 4, xx2, y,
356                                 layout_.labelfont.color());
357                         pi.pain.line(xx2 - 4, y, xx2, y,
358                                 layout_.labelfont.color());
359                 }
360                 break;
361         }
362         pi.background_color = old_color;
363
364         pi.base.font = tmpfont;
365 }
366
367
368 void InsetCollapsable::cursorPos(BufferView const & bv,
369                 CursorSlice const & sl, bool boundary, int & x, int & y) const
370 {
371         if (geometry() == ButtonOnly)
372                 status_ = Open;
373         BOOST_ASSERT(geometry() != ButtonOnly);
374
375         InsetText::cursorPos(bv, sl, boundary, x, y);
376         Dimension const textdim = InsetText::dimension(bv);
377
378         switch (geometry()) {
379         case LeftButton:
380                 x += dimensionCollapsed().wid;
381                 break;
382         case TopButton: {
383                 y += dimensionCollapsed().des + textdim.asc;
384                 break;
385         }
386         case NoButton:
387         case SubLabel:
388         case Corners:
389                 // Do nothing
390                 break;
391         case ButtonOnly:
392                 // Cannot get here
393                 break;
394         }
395 }
396
397
398 Inset::EDITABLE InsetCollapsable::editable() const
399 {
400         return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
401 }
402
403
404 bool InsetCollapsable::descendable() const
405 {
406         return geometry() != ButtonOnly;
407 }
408
409
410 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
411 {
412         return button_dim.contains(cmd.x, cmd.y);
413 }
414
415
416 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
417 {
418         docstring label;
419         pos_type const max_length = 15;
420         pos_type const p_siz = paragraphs().begin()->size();
421         pos_type const n = std::min(max_length, p_siz);
422         pos_type i = 0;
423         pos_type j = 0;
424         for (; i < n && j < p_siz; ++j) {
425                 if (paragraphs().begin()->isInset(j))
426                         continue;
427                 label += paragraphs().begin()->getChar(j);
428                 ++i;
429         }
430         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
431                 label += "...";
432         }
433         return label.empty() ? l : label;
434 }
435
436
437 void InsetCollapsable::edit(Cursor & cur, bool left)
438 {
439         //lyxerr << "InsetCollapsable: edit left/right" << endl;
440         cur.push(*this);
441         InsetText::edit(cur, left);
442 }
443
444
445 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
446 {
447         //lyxerr << "InsetCollapsable: edit xy" << endl;
448         if (geometry() == ButtonOnly
449          || (button_dim.contains(x, y) 
450           && geometry() != NoButton))
451                 return this;
452         cur.push(*this);
453         return InsetText::editXY(cur, x, y);
454 }
455
456
457 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
458 {
459         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
460         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
461
462         switch (cmd.action) {
463         case LFUN_MOUSE_PRESS:
464                 if (cmd.button() == mouse_button::button1 
465                  && hitButton(cmd) 
466                  && geometry() != NoButton) {
467                         // reset selection if necessary (see bug 3060)
468                         if (cur.selection())
469                                 cur.bv().cursor().clearSelection();
470                         else
471                                 cur.noUpdate();
472                         cur.dispatched();
473                         break;
474                 }
475                 if (geometry() == NoButton)
476                         InsetText::doDispatch(cur, cmd);
477                 else if (geometry() != ButtonOnly 
478                      && !hitButton(cmd))
479                         InsetText::doDispatch(cur, cmd);
480                 else
481                         cur.undispatched();
482                 break;
483
484         case LFUN_MOUSE_MOTION:
485         case LFUN_MOUSE_DOUBLE:
486         case LFUN_MOUSE_TRIPLE:
487                 if (geometry() == NoButton)
488                         InsetText::doDispatch(cur, cmd);
489                 else if (geometry() != ButtonOnly
490                      && !hitButton(cmd))
491                         InsetText::doDispatch(cur, cmd);
492                 else
493                         cur.undispatched();
494                 break;
495
496         case LFUN_MOUSE_RELEASE:
497                 if (cmd.button() == mouse_button::button3) {
498                         // There is no button to right click:
499                         if (geometry() == Corners ||
500                             geometry() == SubLabel ||
501                             geometry() == NoButton)  {
502                                 if (internalStatus() == Open)
503                                         setStatus(cur, Collapsed);
504                                 else
505                                         setStatus(cur, Open);
506                                 break;
507                         } else {
508                                 // Open the Inset 
509                                 // configuration dialog
510                                 showInsetDialog(&cur.bv());
511                                 break;
512                         }
513                 }
514
515                 if (geometry() == NoButton) {
516                         // The mouse click has to be within the inset!
517                         InsetText::doDispatch(cur, cmd);
518                         break;
519                 }
520
521                 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
522                         // if we are selecting, we do not want to
523                         // toggle the inset.
524                         if (cur.selection())
525                                 break;
526                         // Left button is clicked, the user asks to
527                         // toggle the inset visual state.
528                         cur.dispatched();
529                         cur.updateFlags(Update::Force | Update::FitCursor);
530                         if (geometry() == ButtonOnly) {
531                                 setStatus(cur, Open);
532                                 edit(cur, true);
533                         }
534                         else {
535                                 setStatus(cur, Collapsed);
536                         }
537                         cur.bv().cursor() = cur;
538                         break;
539                 }
540
541                 // The mouse click is within the opened inset.
542                 if (geometry() == TopButton
543                  || geometry() == LeftButton)
544                         InsetText::doDispatch(cur, cmd);
545                 break;
546
547         case LFUN_INSET_TOGGLE:
548                 if (cmd.argument() == "open")
549                         setStatus(cur, Open);
550                 else if (cmd.argument() == "close")
551                         setStatus(cur, Collapsed);
552                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
553                         if (internalStatus() == Open) {
554                                 setStatus(cur, Collapsed);
555                                 if (geometry() == ButtonOnly)
556                                         cur.top().forwardPos();
557                         } else
558                                 setStatus(cur, Open);
559                 else // if assign or anything else
560                         cur.undispatched();
561                 cur.dispatched();
562                 break;
563
564         case LFUN_PASTE:
565         case LFUN_CLIPBOARD_PASTE:
566         case LFUN_PRIMARY_SELECTION_PASTE: {
567                 InsetText::doDispatch(cur, cmd);
568                 // Since we can only store plain text, we must reset all
569                 // attributes.
570                 // FIXME: Change only the pasted paragraphs
571
572                 resetParagraphsFont();
573                 break;
574         }
575
576         default:
577                 if (layout_.forceltr) {
578                         // Force any new text to latex_language
579                         // FIXME: This should only be necessary in constructor, but
580                         // new paragraphs that are created by pressing enter at the
581                         // start of an existing paragraph get the buffer language
582                         // and not latex_language, so we take this brute force
583                         // approach.
584                         cur.current_font.setLanguage(latex_language);
585                         cur.real_current_font.setLanguage(latex_language);
586                 }
587                 InsetText::doDispatch(cur, cmd);
588                 break;
589         }
590 }
591
592
593 bool InsetCollapsable::allowMultiPar() const
594 {
595         return layout_.multipar;
596 }
597
598
599 void InsetCollapsable::resetParagraphsFont()
600 {
601         Font font;
602         font.fontInfo() = sane_font;
603         if (layout_.forceltr)
604                 font.setLanguage(latex_language);
605         if (layout_.passthru) {
606                 ParagraphList::iterator par = paragraphs().begin();
607                 ParagraphList::iterator const end = paragraphs().end();
608                 while (par != end) {
609                         par->resetFonts(font);
610                         par->params().clear();
611                         ++par;
612                 }
613         }
614 }
615
616
617 void InsetCollapsable::getDrawFont(FontInfo & font) const
618 {
619         font = layout_.font;
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 void InsetCollapsable::setLabelFont(FontInfo const & font)
763 {
764         layout_.labelfont = font;
765 }
766
767 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
768 {
769         FloatList const & floats = bp.getTextClass().floats();
770         FloatList::const_iterator it = floats[type];
771         // FIXME UNICODE
772         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
773 }
774
775
776 InsetCollapsable::Decoration InsetCollapsable::decoration() const
777 {
778         if (layout_.decoration == "classic")
779                 return Classic;
780         if (layout_.decoration == "minimalistic")
781                 return Minimalistic;
782         if (layout_.decoration == "conglomerate")
783                 return Conglomerate;
784         if (name() == from_ascii("Flex"))
785                 return Conglomerate;
786         return Classic;
787 }
788
789
790 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
791                           OutputParams const & runparams) const
792 {
793         // This implements the standard way of handling the LaTeX output of
794         // a collapsable inset, either a command or an environment. Standard 
795         // collapsable insets should not redefine this, non-standard ones may
796         // call this.
797         if (!layout_.latexname.empty()) {
798                 if (layout_.latextype == "command") {
799                         // FIXME UNICODE
800                         os << '\\' << from_utf8(layout_.latexname);
801                         if (!layout_.latexparam.empty())
802                                 os << from_utf8(layout_.latexparam);
803                         os << '{';
804                 } else if (layout_.latextype == "environment") {
805                         os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
806                         if (!layout_.latexparam.empty())
807                                 os << from_utf8(layout_.latexparam);
808                 }
809         }
810         OutputParams rp = runparams;
811         if (layout_.passthru)
812                 rp.verbatim = true;
813         int i = InsetText::latex(buf, os, rp);
814         if (!layout_.latexname.empty()) {
815                 if (layout_.latextype == "command") {
816                         os << "}";
817                 } else if (layout_.latextype == "environment") {
818                         os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
819                         i += 4;
820                 }
821         }
822         return i;
823 }
824
825
826 void InsetCollapsable::validate(LaTeXFeatures & features) const
827 {
828         // Force inclusion of preamble snippet in layout file
829         features.require(layout_.name);
830         InsetText::validate(features);
831 }
832
833
834 } // namespace lyx