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