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