2 * \file InsetCollapsable.cpp
3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
6 * \author Alejandro Aguilar Sierra
8 * \author Lars Gullik Bjønnes
10 * Full author contact details are available in file CREDITS.
15 #include "InsetCollapsable.h"
18 #include "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
27 #include "LaTeXFeatures.h"
29 #include "FuncRequest.h"
30 #include "MetricsInfo.h"
31 #include "ParagraphParameters.h"
33 #include "frontends/FontMetrics.h"
34 #include "frontends/Painter.h"
45 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
47 return autoOpen_ ? Open : status_;
51 InsetCollapsable::Geometry InsetCollapsable::geometry() const
53 switch (decoration()) {
55 if (status() == Open) {
64 return status() == Open ? NoButton : ButtonOnly ;
67 return status() == Open ? SubLabel : Corners ;
70 // dummy return value to shut down a warning,
76 InsetCollapsable::InsetCollapsable
77 (BufferParams const & bp, CollapseStatus status)
78 : InsetText(bp), status_(status),
79 openinlined_(false), autoOpen_(false), mouse_hover_(false)
81 setAutoBreakRows(true);
83 setFrameColor(Color_collapsableframe);
87 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
89 button_dim(rhs.button_dim),
91 topbaseline(rhs.topbaseline),
93 labelstring_(rhs.labelstring_),
95 openinlined_(rhs.openinlined_),
96 autoOpen_(rhs.autoOpen_),
97 // the sole purpose of this copy constructor
103 void InsetCollapsable::setLayout(BufferParams const & bp)
105 layout_ = &getLayout(bp);
106 labelstring_ = layout_->labelstring;
112 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
124 text_.write(buf, os);
128 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
130 bool token_found = false;
133 string const token = lex.getString();
134 if (token == "status") {
136 string const tmp_token = lex.getString();
138 if (tmp_token == "collapsed") {
141 } else if (tmp_token == "open") {
145 lyxerr << "InsetCollapsable::read: Missing status!"
147 // Take countermeasures
148 lex.pushToken(token);
151 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
153 // take countermeasures
154 lex.pushToken(token);
157 InsetText::read(buf, lex);
160 status_ = isOpen() ? Open : Collapsed;
162 setLayout(buf.params());
164 // Force default font, if so requested
165 // This avoids paragraphs in buffer language that would have a
166 // foreign language after a document language change, and it ensures
167 // that all new text in ERT and similar gets the "latex" language,
168 // since new text inherits the language from the last position of the
169 // existing text. As a side effect this makes us also robust against
170 // bugs in LyX that might lead to font changes in ERT in .lyx files.
171 resetParagraphsFont();
175 Dimension InsetCollapsable::dimensionCollapsed() const
178 theFontMetrics(layout_->labelfont).buttonText(
179 labelstring_, dim.wid, dim.asc, dim.des);
184 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
186 autoOpen_ = mi.base.bv->cursor().isInside(this);
188 FontInfo tmpfont = mi.base.font;
189 mi.base.font = layout_->font;
190 mi.base.font.realize(tmpfont);
192 switch (geometry()) {
194 InsetText::metrics(mi, dim);
197 InsetText::metrics(mi, dim);
202 InsetText::metrics(mi, dim);
203 // consider width of the inset label
204 FontInfo font(layout_->labelfont);
205 font.realize(sane_font);
211 theFontMetrics(font).rectText(labelstring_, w, a, d);
218 dim = dimensionCollapsed();
219 if (geometry() == TopButton
220 || geometry() == LeftButton) {
222 InsetText::metrics(mi, textdim);
223 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
225 // Correct for button width.
226 dim.wid += textdim.wid;
227 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
228 dim.asc = textdim.asc;
230 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
231 dim.wid = max(dim.wid, textdim.wid);
237 mi.base.font = tmpfont;
241 bool InsetCollapsable::setMouseHover(bool mouse_hover)
243 mouse_hover_ = mouse_hover;
248 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
250 autoOpen_ = pi.base.bv->cursor().isInside(this);
251 ColorCode const old_color = pi.background_color;
252 pi.background_color = backgroundColor();
254 FontInfo tmpfont = pi.base.font;
255 pi.base.font = layout_->font;
256 pi.base.font.realize(tmpfont);
258 // Draw button first -- top, left or only
259 Dimension dimc = dimensionCollapsed();
261 if (geometry() == TopButton ||
262 geometry() == LeftButton ||
263 geometry() == ButtonOnly) {
264 button_dim.x1 = x + 0;
265 button_dim.x2 = x + dimc.width();
266 button_dim.y1 = y - dimc.asc;
267 button_dim.y2 = y + dimc.des;
269 pi.pain.buttonText(x, y, labelstring_, layout_->labelfont,
278 Dimension const textdim = InsetText::dimension(*pi.base.bv);
279 int const baseline = y;
281 switch (geometry()) {
283 textx = x + dimc.width();
285 InsetText::draw(pi, textx, texty);
289 texty = baseline + dimc.des + textdim.asc;
290 InsetText::draw(pi, textx, texty);
297 InsetText::draw(pi, textx, texty);
303 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
304 InsetText::draw(pi, textx, texty);
305 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
307 int desc = textdim.descent();
308 if (geometry() == Corners)
311 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
312 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
313 pi.pain.line(xx1, y + desc - 4,
315 layout_->labelfont.color());
316 if (internalStatus() == Open)
317 pi.pain.line(xx1, y + desc,
319 layout_->labelfont.color());
321 // Make status_ value visible:
322 pi.pain.line(xx1, y + desc,
324 layout_->labelfont.color());
325 pi.pain.line(xx2 - 4, y + desc,
327 layout_->labelfont.color());
329 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
330 layout_->labelfont.color());
332 // the label below the text. Can be toggled.
333 if (geometry() == SubLabel) {
334 FontInfo font(layout_->labelfont);
335 font.realize(sane_font);
341 theFontMetrics(font).rectText(labelstring_, w, a, d);
342 int const ww = max(textdim.wid, w);
343 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
344 labelstring_, font, Color_none, Color_none);
348 // a visual cue when the cursor is inside the inset
349 Cursor & cur = pi.base.bv->cursor();
350 if (cur.isInside(this)) {
353 pi.pain.line(xx1, y + 4, xx1, y, layout_->labelfont.color());
354 pi.pain.line(xx1 + 4, y, xx1, y, layout_->labelfont.color());
355 pi.pain.line(xx2, y + 4, xx2, y,
356 layout_->labelfont.color());
357 pi.pain.line(xx2 - 4, y, xx2, y,
358 layout_->labelfont.color());
362 pi.background_color = old_color;
364 pi.base.font = tmpfont;
368 void InsetCollapsable::cursorPos(BufferView const & bv,
369 CursorSlice const & sl, bool boundary, int & x, int & y) const
371 if (geometry() == ButtonOnly)
373 BOOST_ASSERT(geometry() != ButtonOnly);
375 InsetText::cursorPos(bv, sl, boundary, x, y);
376 Dimension const textdim = InsetText::dimension(bv);
378 switch (geometry()) {
380 x += dimensionCollapsed().wid;
383 y += dimensionCollapsed().des + textdim.asc;
398 Inset::EDITABLE InsetCollapsable::editable() const
400 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
404 bool InsetCollapsable::descendable() const
406 return geometry() != ButtonOnly;
410 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
412 return button_dim.contains(cmd.x, cmd.y);
416 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
419 pos_type const max_length = 15;
420 pos_type const p_siz = paragraphs().begin()->size();
421 pos_type const n = std::min(max_length, p_siz);
424 for (; i < n && j < p_siz; ++j) {
425 if (paragraphs().begin()->isInset(j))
427 label += paragraphs().begin()->getChar(j);
430 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
433 return label.empty() ? l : label;
437 void InsetCollapsable::edit(Cursor & cur, bool left)
439 //lyxerr << "InsetCollapsable: edit left/right" << endl;
441 InsetText::edit(cur, left);
445 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
447 //lyxerr << "InsetCollapsable: edit xy" << endl;
448 if (geometry() == ButtonOnly
449 || (button_dim.contains(x, y)
450 && geometry() != NoButton))
453 return InsetText::editXY(cur, x, y);
457 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
459 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
460 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
462 switch (cmd.action) {
463 case LFUN_MOUSE_PRESS:
464 if (cmd.button() == mouse_button::button1
466 && geometry() != NoButton) {
467 // reset selection if necessary (see bug 3060)
469 cur.bv().cursor().clearSelection();
475 if (geometry() == NoButton)
476 InsetText::doDispatch(cur, cmd);
477 else if (geometry() != ButtonOnly
479 InsetText::doDispatch(cur, cmd);
484 case LFUN_MOUSE_MOTION:
485 case LFUN_MOUSE_DOUBLE:
486 case LFUN_MOUSE_TRIPLE:
487 if (geometry() == NoButton)
488 InsetText::doDispatch(cur, cmd);
489 else if (geometry() != ButtonOnly
491 InsetText::doDispatch(cur, cmd);
496 case LFUN_MOUSE_RELEASE:
497 if (cmd.button() == mouse_button::button3) {
498 // There is no button to right click:
499 if (geometry() == Corners ||
500 geometry() == SubLabel ||
501 geometry() == NoButton) {
502 if (internalStatus() == Open)
503 setStatus(cur, Collapsed);
505 setStatus(cur, Open);
509 // configuration dialog
510 showInsetDialog(&cur.bv());
515 if (geometry() == NoButton) {
516 // The mouse click has to be within the inset!
517 InsetText::doDispatch(cur, cmd);
521 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
522 // if we are selecting, we do not want to
526 // Left button is clicked, the user asks to
527 // toggle the inset visual state.
529 cur.updateFlags(Update::Force | Update::FitCursor);
530 if (geometry() == ButtonOnly) {
531 setStatus(cur, Open);
535 setStatus(cur, Collapsed);
537 cur.bv().cursor() = cur;
541 // The mouse click is within the opened inset.
542 if (geometry() == TopButton
543 || geometry() == LeftButton)
544 InsetText::doDispatch(cur, cmd);
547 case LFUN_INSET_TOGGLE:
548 if (cmd.argument() == "open")
549 setStatus(cur, Open);
550 else if (cmd.argument() == "close")
551 setStatus(cur, Collapsed);
552 else if (cmd.argument() == "toggle" || cmd.argument().empty())
553 if (internalStatus() == Open) {
554 setStatus(cur, Collapsed);
555 if (geometry() == ButtonOnly)
556 cur.top().forwardPos();
558 setStatus(cur, Open);
559 else // if assign or anything else
565 case LFUN_CLIPBOARD_PASTE:
566 case LFUN_PRIMARY_SELECTION_PASTE: {
567 InsetText::doDispatch(cur, cmd);
568 // Since we can only store plain text, we must reset all
570 // FIXME: Change only the pasted paragraphs
572 resetParagraphsFont();
577 if (layout_->forceltr) {
578 // Force any new text to latex_language
579 // FIXME: This should only be necessary in constructor, but
580 // new paragraphs that are created by pressing enter at the
581 // start of an existing paragraph get the buffer language
582 // and not latex_language, so we take this brute force
584 cur.current_font.setLanguage(latex_language);
585 cur.real_current_font.setLanguage(latex_language);
587 InsetText::doDispatch(cur, cmd);
593 bool InsetCollapsable::allowMultiPar() const
595 return layout_->multipar;
599 void InsetCollapsable::resetParagraphsFont()
602 font.fontInfo() = sane_font;
603 if (layout_->forceltr)
604 font.setLanguage(latex_language);
605 if (layout_->passthru) {
606 ParagraphList::iterator par = paragraphs().begin();
607 ParagraphList::iterator const end = paragraphs().end();
609 par->resetFonts(font);
610 par->params().clear();
617 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
618 FuncStatus & flag) const
620 switch (cmd.action) {
622 case LFUN_ACCENT_ACUTE:
623 case LFUN_ACCENT_BREVE:
624 case LFUN_ACCENT_CARON:
625 case LFUN_ACCENT_CEDILLA:
626 case LFUN_ACCENT_CIRCLE:
627 case LFUN_ACCENT_CIRCUMFLEX:
628 case LFUN_ACCENT_DOT:
629 case LFUN_ACCENT_GRAVE:
630 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
631 case LFUN_ACCENT_MACRON:
632 case LFUN_ACCENT_OGONEK:
633 case LFUN_ACCENT_SPECIAL_CARON:
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:
640 case LFUN_BIBITEM_INSERT:
641 case LFUN_BOX_INSERT:
642 case LFUN_BRANCH_INSERT:
643 case LFUN_BREAK_LINE:
644 case LFUN_CAPTION_INSERT:
645 case LFUN_CLEARPAGE_INSERT:
646 case LFUN_CLEARDOUBLEPAGE_INSERT:
647 case LFUN_DEPTH_DECREMENT:
648 case LFUN_DEPTH_INCREMENT:
649 case LFUN_DOTS_INSERT:
650 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
651 case LFUN_ENVIRONMENT_INSERT:
652 case LFUN_ERT_INSERT:
653 case LFUN_FILE_INSERT:
654 case LFUN_FLEX_INSERT:
655 case LFUN_FLOAT_INSERT:
656 case LFUN_FLOAT_LIST:
657 case LFUN_FLOAT_WIDE_INSERT:
659 case LFUN_FONT_TYPEWRITER:
660 case LFUN_FONT_DEFAULT:
662 case LFUN_FONT_FREE_APPLY:
663 case LFUN_FONT_FREE_UPDATE:
665 case LFUN_FONT_ROMAN:
670 case LFUN_FONT_STATE:
671 case LFUN_FONT_UNDERLINE:
672 case LFUN_FOOTNOTE_INSERT:
673 case LFUN_HFILL_INSERT:
674 case LFUN_HYPERLINK_INSERT:
675 case LFUN_HYPHENATION_POINT_INSERT:
676 case LFUN_INDEX_INSERT:
677 case LFUN_INDEX_PRINT:
678 case LFUN_INSET_INSERT:
679 case LFUN_LABEL_GOTO:
680 case LFUN_LABEL_INSERT:
681 case LFUN_LIGATURE_BREAK_INSERT:
682 case LFUN_LINE_INSERT:
683 case LFUN_PAGEBREAK_INSERT:
685 case LFUN_LAYOUT_PARAGRAPH:
686 case LFUN_LAYOUT_TABULAR:
687 case LFUN_MARGINALNOTE_INSERT:
688 case LFUN_MATH_DISPLAY:
689 case LFUN_MATH_INSERT:
690 case LFUN_MATH_MATRIX:
693 case LFUN_MENU_SEPARATOR_INSERT:
695 case LFUN_NOMENCL_INSERT:
696 case LFUN_NOMENCL_PRINT:
697 case LFUN_NOTE_INSERT:
699 case LFUN_OPTIONAL_INSERT:
700 case LFUN_PARAGRAPH_PARAMS:
701 case LFUN_PARAGRAPH_PARAMS_APPLY:
702 case LFUN_PARAGRAPH_SPACING:
703 case LFUN_PARAGRAPH_UPDATE:
704 case LFUN_REFERENCE_NEXT:
705 case LFUN_SERVER_GOTO_FILE_ROW:
706 case LFUN_SERVER_NOTIFY:
707 case LFUN_SERVER_SET_XY:
708 case LFUN_SPACE_INSERT:
709 case LFUN_TABULAR_INSERT:
710 case LFUN_TOC_INSERT:
711 case LFUN_WRAP_INSERT:
712 if (layout_->passthru) {
716 return InsetText::getStatus(cur, cmd, flag);
718 case LFUN_INSET_TOGGLE:
719 if (cmd.argument() == "open" || cmd.argument() == "close" ||
720 cmd.argument() == "toggle")
727 flag.enabled(!layout_->forceltr);
728 return InsetText::getStatus(cur, cmd, flag);
730 case LFUN_BREAK_PARAGRAPH:
731 case LFUN_BREAK_PARAGRAPH_SKIP:
732 flag.enabled(layout_->multipar);
736 return InsetText::getStatus(cur, cmd, flag);
741 void InsetCollapsable::setLabel(docstring const & l)
747 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
751 if (status_ == Collapsed)
752 cur.leaveInset(*this);
756 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
758 FloatList const & floats = bp.getTextClass().floats();
759 FloatList::const_iterator it = floats[type];
761 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
765 InsetCollapsable::Decoration InsetCollapsable::decoration() const
767 if (layout_->decoration == "classic")
769 if (layout_->decoration == "minimalistic")
771 if (layout_->decoration == "conglomerate")
773 if (name() == from_ascii("Flex"))
779 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
780 OutputParams const & runparams) const
782 // This implements the standard way of handling the LaTeX output of
783 // a collapsable inset, either a command or an environment. Standard
784 // collapsable insets should not redefine this, non-standard ones may
786 if (!layout_->latexname.empty()) {
787 if (layout_->latextype == "command") {
789 if (runparams.moving_arg)
791 os << '\\' << from_utf8(layout_->latexname);
792 if (!layout_->latexparam.empty())
793 os << from_utf8(layout_->latexparam);
795 } else if (layout_->latextype == "environment") {
796 os << "%\n\\begin{" << from_utf8(layout_->latexname) << "}\n";
797 if (!layout_->latexparam.empty())
798 os << from_utf8(layout_->latexparam);
801 OutputParams rp = runparams;
802 if (layout_->passthru)
804 if (layout_->needprotect)
805 rp.moving_arg = true;
806 int i = InsetText::latex(buf, os, rp);
807 if (!layout_->latexname.empty()) {
808 if (layout_->latextype == "command") {
810 } else if (layout_->latextype == "environment") {
811 os << "\n\\end{" << from_utf8(layout_->latexname) << "}\n";
819 void InsetCollapsable::validate(LaTeXFeatures & features) const
821 // Force inclusion of preamble snippet in layout file
822 features.require(layout_->name);
823 InsetText::validate(features);