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