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