]> git.lyx.org Git - features.git/blob - src/insets/InsetCollapsable.cpp
First step of InsetLayout cleanup.
[features.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                 pi.pain.buttonText(x, y, buttonLabel(bv), getLayout().labelfont(),
310                         mouse_hover_);
311         } else {
312                 button_dim.x1 = 0;
313                 button_dim.y1 = 0;
314                 button_dim.x2 = 0;
315                 button_dim.y2 = 0;
316         }
317
318         Dimension const textdim = InsetText::dimension(bv);
319         int const baseline = y;
320         int textx, texty;
321         switch (geometry(bv)) {
322         case LeftButton:
323                 textx = x + dimc.width();
324                 texty = baseline;
325                 InsetText::draw(pi, textx, texty);
326                 break;
327         case TopButton:
328                 textx = x;
329                 texty = baseline + dimc.des + textdim.asc;
330                 InsetText::draw(pi, textx, texty);
331                 break;
332         case ButtonOnly:
333                 break;
334         case NoButton:
335                 textx = x;
336                 texty = baseline;
337                 InsetText::draw(pi, textx, texty);
338                 break;
339         case SubLabel:
340         case Corners:
341                 textx = x;
342                 texty = baseline;
343                 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
344                 InsetText::draw(pi, textx, texty);
345                 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
346
347                 int desc = textdim.descent();
348                 if (geometry(bv) == Corners)
349                         desc -= 3;
350
351                 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
352                 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
353                 pi.pain.line(xx1, y + desc - 4, 
354                              xx1, y + desc, 
355                         getLayout().labelfont().color());
356                 if (status_ == Open)
357                         pi.pain.line(xx1, y + desc, 
358                                 xx2, y + desc,
359                                 getLayout().labelfont().color());
360                 else {
361                         // Make status_ value visible:
362                         pi.pain.line(xx1, y + desc,
363                                 xx1 + 4, y + desc,
364                                 getLayout().labelfont().color());
365                         pi.pain.line(xx2 - 4, y + desc,
366                                 xx2, y + desc,
367                                 getLayout().labelfont().color());
368                 }
369                 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, 
370                         y + desc - 4, getLayout().labelfont().color());
371
372                 // the label below the text. Can be toggled.
373                 if (geometry(bv) == SubLabel) {
374                         FontInfo font(getLayout().labelfont());
375                         font.realize(sane_font);
376                         font.decSize();
377                         font.decSize();
378                         int w = 0;
379                         int a = 0;
380                         int d = 0;
381                         theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
382                         int const ww = max(textdim.wid, w);
383                         pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
384                                 buttonLabel(bv), font, Color_none, Color_none);
385                         desc += d;
386                 }
387
388                 // a visual cue when the cursor is inside the inset
389                 Cursor const & cur = bv.cursor();
390                 if (cur.isInside(this)) {
391                         y -= textdim.asc;
392                         y += 3;
393                         pi.pain.line(xx1, y + 4, xx1, y, getLayout().labelfont().color());
394                         pi.pain.line(xx1 + 4, y, xx1, y, getLayout().labelfont().color());
395                         pi.pain.line(xx2, y + 4, xx2, y,
396                                 getLayout().labelfont().color());
397                         pi.pain.line(xx2 - 4, y, xx2, y,
398                                 getLayout().labelfont().color());
399                 }
400                 break;
401         }
402
403         pi.base.font = tmpfont;
404 }
405
406
407 void InsetCollapsable::cursorPos(BufferView const & bv,
408                 CursorSlice const & sl, bool boundary, int & x, int & y) const
409 {
410         if (geometry(bv) == ButtonOnly)
411                 status_ = Open;
412         LASSERT(geometry(bv) != ButtonOnly, /**/);
413
414         InsetText::cursorPos(bv, sl, boundary, x, y);
415         Dimension const textdim = InsetText::dimension(bv);
416
417         switch (geometry(bv)) {
418         case LeftButton:
419                 x += dimensionCollapsed(bv).wid;
420                 break;
421         case TopButton: {
422                 y += dimensionCollapsed(bv).des + textdim.asc;
423                 break;
424         }
425         case NoButton:
426         case SubLabel:
427         case Corners:
428                 // Do nothing
429                 break;
430         case ButtonOnly:
431                 // Cannot get here
432                 break;
433         }
434 }
435
436
437 bool InsetCollapsable::editable() const
438 {
439         return geometry() != ButtonOnly;
440 }
441
442
443 bool InsetCollapsable::descendable() const
444 {
445         return geometry() != ButtonOnly;
446 }
447
448
449 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
450 {
451         return button_dim.contains(cmd.x, cmd.y);
452 }
453
454
455 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
456 {
457         docstring label;
458         pos_type const max_length = 15;
459         pos_type const p_siz = paragraphs().begin()->size();
460         pos_type const n = min(max_length, p_siz);
461         pos_type i = 0;
462         pos_type j = 0;
463         for (; i < n && j < p_siz; ++j) {
464                 if (paragraphs().begin()->isInset(j))
465                         continue;
466                 label += paragraphs().begin()->getChar(j);
467                 ++i;
468         }
469         if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
470                 label += "...";
471         }
472         return label.empty() ? l : label;
473 }
474
475
476 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
477 {
478         //lyxerr << "InsetCollapsable: edit left/right" << endl;
479         cur.push(*this);
480         InsetText::edit(cur, front, entry_from);
481 }
482
483
484 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
485 {
486         //lyxerr << "InsetCollapsable: edit xy" << endl;
487         if (geometry(cur.bv()) == ButtonOnly
488          || (button_dim.contains(x, y) 
489           && geometry(cur.bv()) != NoButton))
490                 return this;
491         cur.push(*this);
492         return InsetText::editXY(cur, x, y);
493 }
494
495
496 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
497 {
498         //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
499         //      << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
500
501         switch (cmd.action) {
502         case LFUN_MOUSE_PRESS:
503                 if (hitButton(cmd)) {
504                         switch (cmd.button()) {
505                         case mouse_button::button1:
506                         case mouse_button::button3:
507                                 // Pass the command to the enclosing InsetText,
508                                 // so that the cursor gets set.
509                                 cur.undispatched();
510                                 break;
511                         case mouse_button::none:
512                         case mouse_button::button2:
513                         case mouse_button::button4:
514                         case mouse_button::button5:
515                                 // Nothing to do.
516                                 cur.noUpdate();
517                                 break;
518                         }
519                 } else if (geometry(cur.bv()) != ButtonOnly)
520                         InsetText::doDispatch(cur, cmd);
521                 else
522                         cur.undispatched();
523                 break;
524
525         case LFUN_MOUSE_MOTION:
526         case LFUN_MOUSE_DOUBLE:
527         case LFUN_MOUSE_TRIPLE:
528                 if (hitButton(cmd)) 
529                         cur.noUpdate();
530                 else if (geometry(cur.bv()) != ButtonOnly)
531                         InsetText::doDispatch(cur, cmd);
532                 else
533                         cur.undispatched();
534                 break;
535
536         case LFUN_MOUSE_RELEASE:
537                 if (!hitButton(cmd)) {
538                         // The mouse click has to be within the inset!
539                         if (geometry(cur.bv()) != ButtonOnly)
540                                 InsetText::doDispatch(cur, cmd);
541                         else
542                                 cur.undispatched();                     
543                         break;
544                 }
545                 if (cmd.button() != mouse_button::button1) {
546                         // Nothing to do.
547                         cur.noUpdate();
548                         break;
549                 }
550                 // if we are selecting, we do not want to
551                 // toggle the inset.
552                 if (cur.selection())
553                         break;
554                 // Left button is clicked, the user asks to
555                 // toggle the inset visual state.
556                 cur.dispatched();
557                 cur.updateFlags(Update::Force | Update::FitCursor);
558                 if (geometry(cur.bv()) == ButtonOnly) {
559                         setStatus(cur, Open);
560                         edit(cur, true);
561                 }
562                 else
563                         setStatus(cur, Collapsed);
564                 cur.bv().cursor() = cur;
565                 break;
566
567         case LFUN_INSET_TOGGLE:
568                 if (cmd.argument() == "open")
569                         setStatus(cur, Open);
570                 else if (cmd.argument() == "close")
571                         setStatus(cur, Collapsed);
572                 else if (cmd.argument() == "toggle" || cmd.argument().empty())
573                         if (status_ == Open) {
574                                 setStatus(cur, Collapsed);
575                                 if (geometry(cur.bv()) == ButtonOnly)
576                                         cur.top().forwardPos();
577                         } else
578                                 setStatus(cur, Open);
579                 else // if assign or anything else
580                         cur.undispatched();
581                 cur.dispatched();
582                 break;
583
584         case LFUN_PASTE:
585         case LFUN_CLIPBOARD_PASTE:
586         case LFUN_SELECTION_PASTE:
587         case LFUN_PRIMARY_SELECTION_PASTE: {
588                 InsetText::doDispatch(cur, cmd);
589                 // Since we can only store plain text, we must reset all
590                 // attributes.
591                 // FIXME: Change only the pasted paragraphs
592
593                 resetParagraphsFont();
594                 break;
595         }
596
597         case LFUN_TAB_INSERT: {
598                 bool const multi_par_selection = cur.selection() &&
599                         cur.selBegin().pit() != cur.selEnd().pit();
600                 if (multi_par_selection) {
601                         // If there is a multi-paragraph selection, a tab is inserted
602                         // at the beginning of each paragraph.
603                         cur.recordUndoSelection();
604                         pit_type const pit_end = cur.selEnd().pit();
605                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
606                                 paragraphs()[pit].insertChar(0, '\t', 
607                                         buffer().params().trackChanges);
608                                 // Update the selection pos to make sure the selection does not
609                                 // change as the inserted tab will increase the logical pos.
610                                 if (cur.anchor_.pit() == pit)
611                                         cur.anchor_.forwardPos();
612                                 if (cur.pit() == pit)
613                                         cur.forwardPos();
614                         }
615                         cur.finishUndo();
616                 } else {
617                         // Maybe we shouldn't allow tabs within a line, because they
618                         // are not (yet) aligned as one might do expect.
619                         FuncRequest cmd(LFUN_SELF_INSERT, from_ascii("\t"));
620                         dispatch(cur, cmd);     
621                 }
622                 break;
623         }
624
625         case LFUN_TAB_DELETE:
626                 if (cur.selection()) {
627                         // If there is a selection, a tab (if present) is removed from
628                         // the beginning of each paragraph.
629                         cur.recordUndoSelection();
630                         pit_type const pit_end = cur.selEnd().pit();
631                         for (pit_type pit = cur.selBegin().pit(); pit <= pit_end; pit++) {
632                                 Paragraph & par = paragraphs()[pit];
633                                 if (par.getChar(0) == '\t') {
634                                         if (cur.pit() == pit)
635                                                 cur.posBackward();
636                                         if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
637                                                 cur.anchor_.backwardPos();
638
639                                         par.eraseChar(0, buffer().params().trackChanges);
640                                 } else 
641                                         // If no tab was present, try to remove up to four spaces.
642                                         for (int n_spaces = 0;
643                                                 par.getChar(0) == ' ' && n_spaces < 4; ++n_spaces) {
644                                                         if (cur.pit() == pit)
645                                                                 cur.posBackward();
646                                                         if (cur.anchor_.pit() == pit && cur.anchor_.pos() > 0 )
647                                                                 cur.anchor_.backwardPos();
648
649                                                         par.eraseChar(0, buffer().params().trackChanges);
650                                         }
651                         }
652                         cur.finishUndo();
653                 } else {
654                         // If there is no selection, try to remove a tab or some spaces 
655                         // before the position of the cursor.
656                         Paragraph & par = paragraphs()[cur.pit()];
657                         pos_type const pos = cur.pos();
658
659                         if (pos == 0)
660                                 break;
661
662                         char_type const c = par.getChar(pos - 1);
663                         cur.recordUndo();
664                         if (c == '\t') {
665                                 cur.posBackward();
666                                 par.eraseChar(cur.pos(), buffer().params().trackChanges);
667                         } else
668                                 for (int n_spaces = 0; cur.pos() > 0
669                                         && par.getChar(cur.pos() - 1) == ' ' && n_spaces < 4;
670                                         ++n_spaces) {
671                                                 cur.posBackward();
672                                                 par.eraseChar(cur.pos(), buffer().params().trackChanges);
673                                 }
674                                 cur.finishUndo();
675                 }
676                 break;
677
678         default:
679                 if (getLayout().isForceLtr()) {
680                         // Force any new text to latex_language
681                         // FIXME: This should only be necessary in constructor, but
682                         // new paragraphs that are created by pressing enter at the
683                         // start of an existing paragraph get the buffer language
684                         // and not latex_language, so we take this brute force
685                         // approach.
686                         cur.current_font.setLanguage(latex_language);
687                         cur.real_current_font.setLanguage(latex_language);
688                 }
689                 InsetText::doDispatch(cur, cmd);
690                 break;
691         }
692 }
693
694
695 bool InsetCollapsable::allowMultiPar() const
696 {
697         return getLayout().isMultiPar();
698 }
699
700
701 void InsetCollapsable::resetParagraphsFont()
702 {
703         Font font(inherit_font, buffer().params().language);
704         if (getLayout().isForceLtr())
705                 font.setLanguage(latex_language);
706         if (getLayout().isPassThru()) {
707                 ParagraphList::iterator par = paragraphs().begin();
708                 ParagraphList::iterator const end = paragraphs().end();
709                 while (par != end) {
710                         par->resetFonts(font);
711                         par->params().clear();
712                         ++par;
713                 }
714         }
715 }
716
717
718 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
719                 FuncStatus & flag) const
720 {
721         switch (cmd.action) {
722         // FIXME At present, these are being enabled and disabled according to
723         // whether PASSTHRU has been set in the InsetLayout. This makes some
724         // sense, but there are other checks that should really be done. E.g.,
725         // one should not be able to inset IndexPrint inside an optional argument!!
726         case LFUN_ACCENT_ACUTE:
727         case LFUN_ACCENT_BREVE:
728         case LFUN_ACCENT_CARON:
729         case LFUN_ACCENT_CEDILLA:
730         case LFUN_ACCENT_CIRCLE:
731         case LFUN_ACCENT_CIRCUMFLEX:
732         case LFUN_ACCENT_DOT:
733         case LFUN_ACCENT_GRAVE:
734         case LFUN_ACCENT_HUNGARIAN_UMLAUT:
735         case LFUN_ACCENT_MACRON:
736         case LFUN_ACCENT_OGONEK:
737         case LFUN_ACCENT_TIE:
738         case LFUN_ACCENT_TILDE:
739         case LFUN_ACCENT_UMLAUT:
740         case LFUN_ACCENT_UNDERBAR:
741         case LFUN_ACCENT_UNDERDOT:
742         case LFUN_APPENDIX:
743         case LFUN_BOX_INSERT:
744         case LFUN_BRANCH_INSERT:
745         case LFUN_CAPTION_INSERT:
746         case LFUN_DEPTH_DECREMENT:
747         case LFUN_DEPTH_INCREMENT:
748         case LFUN_ERT_INSERT:
749         case LFUN_FILE_INSERT:
750         case LFUN_FLEX_INSERT:
751         case LFUN_FLOAT_INSERT:
752         case LFUN_FLOAT_LIST_INSERT:
753         case LFUN_FLOAT_WIDE_INSERT:
754         case LFUN_FONT_BOLD:
755         case LFUN_FONT_BOLDSYMBOL:
756         case LFUN_FONT_TYPEWRITER:
757         case LFUN_FONT_DEFAULT:
758         case LFUN_FONT_EMPH:
759         case LFUN_FONT_NOUN:
760         case LFUN_FONT_ROMAN:
761         case LFUN_FONT_SANS:
762         case LFUN_FONT_FRAK:
763         case LFUN_FONT_ITAL:
764         case LFUN_FONT_SIZE:
765         case LFUN_FONT_STATE:
766         case LFUN_FONT_UNDERLINE:
767         case LFUN_FONT_STRIKEOUT:
768         case LFUN_FONT_UULINE:
769         case LFUN_FONT_UWAVE:
770         case LFUN_FOOTNOTE_INSERT:
771         case LFUN_HYPERLINK_INSERT:
772         case LFUN_INDEX_INSERT:
773         case LFUN_INDEX_PRINT:
774         case LFUN_INSET_INSERT:
775         case LFUN_LABEL_GOTO:
776         case LFUN_LABEL_INSERT:
777         case LFUN_LAYOUT_TABULAR:
778         case LFUN_LINE_INSERT:
779         case LFUN_MARGINALNOTE_INSERT:
780         case LFUN_MATH_DISPLAY:
781         case LFUN_MATH_INSERT:
782         case LFUN_MATH_AMS_MATRIX:
783         case LFUN_MATH_MATRIX:
784         case LFUN_MATH_MODE:
785         case LFUN_MENU_OPEN:
786         case LFUN_NEWLINE_INSERT:
787         case LFUN_NEWPAGE_INSERT:
788         case LFUN_NOACTION:
789         case LFUN_NOMENCL_INSERT:
790         case LFUN_NOMENCL_PRINT:
791         case LFUN_NOTE_INSERT:
792         case LFUN_NOTE_NEXT:
793         case LFUN_OPTIONAL_INSERT:
794         case LFUN_PHANTOM_INSERT:
795         case LFUN_REFERENCE_NEXT:
796         case LFUN_SERVER_GOTO_FILE_ROW:
797         case LFUN_SERVER_NOTIFY:
798         case LFUN_SERVER_SET_XY:
799         case LFUN_SPACE_INSERT:
800         case LFUN_SPECIALCHAR_INSERT:
801         case LFUN_TABULAR_INSERT:
802         case LFUN_TEXTSTYLE_APPLY:
803         case LFUN_TEXTSTYLE_UPDATE:
804         case LFUN_TOC_INSERT:
805         case LFUN_WRAP_INSERT:
806                 if (getLayout().isPassThru()) {
807                         flag.setEnabled(false);
808                         return true;
809                 }
810                 return InsetText::getStatus(cur, cmd, flag);
811
812         case LFUN_INSET_TOGGLE:
813                 if (cmd.argument() == "open")
814                         flag.setEnabled(status_ != Open);
815                 else if (cmd.argument() == "close")
816                         flag.setEnabled(status_ == Open);
817                 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
818                         flag.setEnabled(true);
819                         flag.setOnOff(status_ == Open);
820                 } else
821                         flag.setEnabled(false);
822                 return true;
823
824         case LFUN_LANGUAGE:
825                 flag.setEnabled(!getLayout().isForceLtr());
826                 return InsetText::getStatus(cur, cmd, flag);
827
828         case LFUN_BREAK_PARAGRAPH:
829                 flag.setEnabled(getLayout().isMultiPar());
830                 return true;
831
832         case LFUN_TAB_INSERT:
833         case LFUN_TAB_DELETE:
834                 if (getLayout().isPassThru()) {
835                         flag.setEnabled(true);
836                         return true;
837                 }
838                 return InsetText::getStatus(cur, cmd, flag);
839
840         default:
841                 return InsetText::getStatus(cur, cmd, flag);
842         }
843 }
844
845
846 void InsetCollapsable::setLabel(docstring const & l)
847 {
848         labelstring_ = l;
849 }
850
851
852 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
853 {
854         status_ = status;
855         setButtonLabel();
856         if (status_ == Collapsed) {
857                 cur.leaveInset(*this);
858                 mouse_hover_ = false;
859         }
860 }
861
862
863 docstring InsetCollapsable::floatName(
864                 string const & type, BufferParams const & bp) const
865 {
866         FloatList const & floats = bp.documentClass().floats();
867         FloatList::const_iterator it = floats[type];
868         // FIXME UNICODE
869         return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
870 }
871
872
873 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
874 {
875         InsetLayout::InsetDecoration const dec = getLayout().decoration();
876         switch (dec) {
877         case InsetLayout::CLASSIC:
878         case InsetLayout::MINIMALISTIC:
879         case InsetLayout::CONGLOMERATE:
880                 return dec;
881         case InsetLayout::DEFAULT:
882                 break;
883         }
884         if (lyxCode() == FLEX_CODE)
885                 return InsetLayout::CONGLOMERATE;
886         return InsetLayout::CLASSIC;
887 }
888
889
890 int InsetCollapsable::latex(odocstream & os,
891                           OutputParams const & runparams) const
892 {
893         // This implements the standard way of handling the LaTeX output of
894         // a collapsable inset, either a command or an environment. Standard 
895         // collapsable insets should not redefine this, non-standard ones may
896         // call this.
897         if (!getLayout().latexname().empty()) {
898                 if (getLayout().latextype() == InsetLayout::COMMAND) {
899                         // FIXME UNICODE
900                         if (runparams.moving_arg)
901                                 os << "\\protect";
902                         os << '\\' << from_utf8(getLayout().latexname());
903                         if (!getLayout().latexparam().empty())
904                                 os << from_utf8(getLayout().latexparam());
905                         os << '{';
906                 } else if (getLayout().latextype() == InsetLayout::ENVIRONMENT) {
907                         os << "%\n\\begin{" << from_utf8(getLayout().latexname()) << "}\n";
908                         if (!getLayout().latexparam().empty())
909                                 os << from_utf8(getLayout().latexparam());
910                 }
911         }
912         OutputParams rp = runparams;
913         if (getLayout().isPassThru())
914                 rp.verbatim = true;
915         if (getLayout().isNeedProtect())
916                 rp.moving_arg = true;
917         int i = InsetText::latex(os, rp);
918         if (!getLayout().latexname().empty()) {
919                 if (getLayout().latextype() == InsetLayout::COMMAND) {
920                         os << "}";
921                 } else if (getLayout().latextype() == InsetLayout::ENVIRONMENT) {
922                         os << "\n\\end{" << from_utf8(getLayout().latexname()) << "}\n";
923                         i += 4;
924                 }
925         }
926         return i;
927 }
928
929
930 // FIXME It seems as if it ought to be possible to do this more simply,
931 // maybe by calling InsetText::docbook() in the middle there.
932 int InsetCollapsable::docbook(odocstream & os, OutputParams const & runparams) const
933 {
934         ParagraphList::const_iterator const beg = paragraphs().begin();
935         ParagraphList::const_iterator par = paragraphs().begin();
936         ParagraphList::const_iterator const end = paragraphs().end();
937
938         if (!undefined())
939                 sgml::openTag(os, getLayout().latexname(),
940                               par->getID(buffer(), runparams) + getLayout().latexparam());
941
942         for (; par != end; ++par) {
943                 par->simpleDocBookOnePar(buffer(), os, runparams,
944                                          outerFont(distance(beg, par),
945                                                    paragraphs()));
946         }
947
948         if (!undefined())
949                 sgml::closeTag(os, getLayout().latexname());
950
951         return 0;
952 }
953
954
955 docstring InsetCollapsable::xhtml(odocstream & os, OutputParams const & runparams) const
956 {
957         InsetLayout const & il = getLayout();
958         if (undefined())
959                 return InsetText::xhtml(os, runparams);
960
961         bool const opened = html::openTag(os, il.htmltag(), il.htmlattr());
962         if (!il.counter().empty()) {
963                 BufferParams const & bp = buffer().masterBuffer()->params();
964                 Counters & cntrs = bp.documentClass().counters();
965                 cntrs.step(il.counter());
966                 // FIXME: translate to paragraph language
967                 if (!il.htmllabel().empty())
968                         os << cntrs.counterLabel(from_utf8(il.htmllabel()), bp.language->code());
969         }
970         bool innertag_opened = false;
971         if (!il.htmlinnertag().empty())
972                 innertag_opened = html::openTag(os, il.htmlinnertag(), il.htmlinnerattr());
973         docstring deferred = InsetText::xhtml(os, runparams);
974         if (innertag_opened)
975                 html::closeTag(os, il.htmlinnertag());
976         if (opened)
977                 html::closeTag(os, il.htmltag());
978         return deferred;
979 }
980
981
982 void InsetCollapsable::validate(LaTeXFeatures & features) const
983 {
984         features.useInsetLayout(getLayout());
985         InsetText::validate(features);
986 }
987
988
989 bool InsetCollapsable::undefined() const
990 {
991         docstring const & n = getLayout().name();
992         return n.empty() || n == DocumentClass::plainInsetLayout().name();
993 }
994
995
996 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
997         int y) const
998 {
999         if (decoration() == InsetLayout::CONGLOMERATE)
1000                 return from_ascii("context-conglomerate");
1001
1002         if (geometry(bv) == NoButton)
1003                 return from_ascii("context-collapsable");
1004
1005         Dimension dim = dimensionCollapsed(bv);
1006         if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
1007                 return from_ascii("context-collapsable");
1008
1009         return InsetText::contextMenu(bv, x, y);
1010 }
1011
1012 void InsetCollapsable::tocString(odocstream & os) const
1013 {
1014         if (!getLayout().isInToc())
1015                 return;
1016         os << text().asString(0, 1, AS_STR_LABEL | AS_STR_INSETS);
1017 }
1018
1019
1020 } // namespace lyx