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