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"
26 #include "LaTeXFeatures.h"
28 #include "FuncRequest.h"
29 #include "MetricsInfo.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
45 return autoOpen_ ? Open : status_;
49 InsetCollapsable::Geometry InsetCollapsable::geometry() const
51 switch (decoration()) {
53 if (status() == Open) {
62 return status() == Open ? NoButton : ButtonOnly ;
65 return status() == Open ? SubLabel : Corners ;
68 // dummy return value to shut down a warning,
74 InsetCollapsable::InsetCollapsable
75 (BufferParams const & bp, CollapseStatus status)
76 : InsetText(bp), status_(status),
77 openinlined_(false), autoOpen_(false), mouse_hover_(false)
79 setAutoBreakRows(true);
81 setFrameColor(Color_collapsableframe);
83 // Fallback for lacking inset layout item
84 layout_.bgcolor = Color_background;
88 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
90 button_dim(rhs.button_dim),
92 topbaseline(rhs.topbaseline),
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);
109 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
121 text_.write(buf, os);
125 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
127 bool token_found = false;
130 string const token = lex.getString();
131 if (token == "status") {
133 string const tmp_token = lex.getString();
135 if (tmp_token == "collapsed") {
138 } else if (tmp_token == "open") {
142 lyxerr << "InsetCollapsable::read: Missing status!"
144 // Take countermeasures
145 lex.pushToken(token);
148 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
150 // take countermeasures
151 lex.pushToken(token);
154 InsetText::read(buf, lex);
157 status_ = isOpen() ? Open : Collapsed;
163 Dimension InsetCollapsable::dimensionCollapsed() const
166 theFontMetrics(layout_.labelfont).buttonText(
167 layout_.labelstring, dim.wid, dim.asc, dim.des);
172 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
174 autoOpen_ = mi.base.bv->cursor().isInside(this);
176 switch (geometry()) {
178 InsetText::metrics(mi, dim);
181 InsetText::metrics(mi, dim);
186 InsetText::metrics(mi, dim);
187 // consider width of the inset label
188 Font font(layout_.labelfont);
189 font.realize(Font(Font::ALL_SANE));
195 docstring s = layout_.labelstring;
196 theFontMetrics(font).rectText(s, w, a, d);
203 dim = dimensionCollapsed();
204 if (geometry() == TopButton
205 || geometry() == LeftButton) {
207 InsetText::metrics(mi, textdim);
208 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
210 // Correct for button width.
211 dim.wid += textdim.wid;
212 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
213 dim.asc = textdim.asc;
215 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
216 dim.wid = max(dim.wid, textdim.wid);
224 bool InsetCollapsable::setMouseHover(bool mouse_hover)
226 mouse_hover_ = mouse_hover;
231 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
233 autoOpen_ = pi.base.bv->cursor().isInside(this);
234 int const old_color = pi.background_color;
235 pi.background_color = backgroundColor();
237 // Draw button first -- top, left or only
238 Dimension dimc = dimensionCollapsed();
240 if (geometry() == TopButton ||
241 geometry() == LeftButton ||
242 geometry() == ButtonOnly) {
243 button_dim.x1 = x + 0;
244 button_dim.x2 = x + dimc.width();
245 button_dim.y1 = y - dimc.asc;
246 button_dim.y2 = y + dimc.des;
248 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
256 Dimension const textdim = InsetText::dimension(*pi.base.bv);
257 int const baseline = y;
259 switch (geometry()) {
261 textx = x + dimc.width();
263 InsetText::draw(pi, textx, texty);
267 texty = baseline + dimc.des + textdim.asc;
268 InsetText::draw(pi, textx, texty);
275 InsetText::draw(pi, textx, texty);
281 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
282 InsetText::draw(pi, textx, texty);
283 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
285 int desc = textdim.descent();
286 if (geometry() == Corners)
289 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
290 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
291 pi.pain.line(xx1, y + desc - 4,
293 layout_.labelfont.color());
294 if (internalStatus() == Open)
295 pi.pain.line(xx1, y + desc,
297 layout_.labelfont.color());
299 // Make status_ value visible:
300 pi.pain.line(xx1, y + desc,
302 layout_.labelfont.color());
303 pi.pain.line(xx2 - 4, y + desc,
305 layout_.labelfont.color());
307 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
308 layout_.labelfont.color());
310 // the label below the text. Can be toggled.
311 if (geometry() == SubLabel) {
312 Font font(layout_.labelfont);
313 font.realize(Font(Font::ALL_SANE));
319 docstring s = layout_.labelstring;
320 theFontMetrics(font).rectText(s, w, a, d);
321 int const ww = max(textdim.wid, w);
322 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
323 s, font, Color_none, Color_none);
327 // a visual cue when the cursor is inside the inset
328 Cursor & cur = pi.base.bv->cursor();
329 if (cur.isInside(this)) {
332 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
333 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
334 pi.pain.line(xx2, y + 4, xx2, y,
335 layout_.labelfont.color());
336 pi.pain.line(xx2 - 4, y, xx2, y,
337 layout_.labelfont.color());
341 pi.background_color = old_color;
345 void InsetCollapsable::cursorPos(BufferView const & bv,
346 CursorSlice const & sl, bool boundary, int & x, int & y) const
348 if (geometry() == ButtonOnly)
350 BOOST_ASSERT(geometry() != ButtonOnly);
352 InsetText::cursorPos(bv, sl, boundary, x, y);
353 Dimension const textdim = InsetText::dimension(bv);
355 switch (geometry()) {
357 x += dimensionCollapsed().wid;
360 y += dimensionCollapsed().des + textdim.asc;
375 Inset::EDITABLE InsetCollapsable::editable() const
377 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
381 bool InsetCollapsable::descendable() const
383 return geometry() != ButtonOnly;
387 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
389 return button_dim.contains(cmd.x, cmd.y);
393 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
396 pos_type const max_length = 15;
397 pos_type const p_siz = paragraphs().begin()->size();
398 pos_type const n = std::min(max_length, p_siz);
401 for (; i < n && j < p_siz; ++j) {
402 if (paragraphs().begin()->isInset(j))
404 label += paragraphs().begin()->getChar(j);
407 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
410 return label.empty() ? l : label;
414 void InsetCollapsable::edit(Cursor & cur, bool left)
416 //lyxerr << "InsetCollapsable: edit left/right" << endl;
418 InsetText::edit(cur, left);
422 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
424 //lyxerr << "InsetCollapsable: edit xy" << endl;
425 if (geometry() == ButtonOnly
426 || (button_dim.contains(x, y)
427 && geometry() != NoButton))
430 return InsetText::editXY(cur, x, y);
434 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
436 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
437 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
439 switch (cmd.action) {
440 case LFUN_MOUSE_PRESS:
441 if (cmd.button() == mouse_button::button1
443 && geometry() != NoButton) {
444 // reset selection if necessary (see bug 3060)
446 cur.bv().cursor().clearSelection();
452 if (geometry() == NoButton)
453 InsetText::doDispatch(cur, cmd);
454 else if (geometry() != ButtonOnly
456 InsetText::doDispatch(cur, cmd);
461 case LFUN_MOUSE_MOTION:
462 case LFUN_MOUSE_DOUBLE:
463 case LFUN_MOUSE_TRIPLE:
464 if (geometry() == NoButton)
465 InsetText::doDispatch(cur, cmd);
466 else if (geometry() != ButtonOnly
468 InsetText::doDispatch(cur, cmd);
473 case LFUN_MOUSE_RELEASE:
474 if (cmd.button() == mouse_button::button3) {
475 // There is no button to right click:
476 if (geometry() == Corners ||
477 geometry() == SubLabel ||
478 geometry() == NoButton) {
479 if (internalStatus() == Open)
480 setStatus(cur, Collapsed);
482 setStatus(cur, Open);
486 // configuration dialog
487 showInsetDialog(&cur.bv());
492 if (geometry() == NoButton) {
493 // The mouse click has to be within the inset!
494 InsetText::doDispatch(cur, cmd);
498 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
499 // if we are selecting, we do not want to
503 // Left button is clicked, the user asks to
504 // toggle the inset visual state.
506 cur.updateFlags(Update::Force | Update::FitCursor);
507 if (geometry() == ButtonOnly) {
508 setStatus(cur, Open);
512 setStatus(cur, Collapsed);
514 cur.bv().cursor() = cur;
518 // The mouse click is within the opened inset.
519 if (geometry() == TopButton
520 || geometry() == LeftButton)
521 InsetText::doDispatch(cur, cmd);
524 case LFUN_INSET_TOGGLE:
525 if (cmd.argument() == "open")
526 setStatus(cur, Open);
527 else if (cmd.argument() == "close")
528 setStatus(cur, Collapsed);
529 else if (cmd.argument() == "toggle" || cmd.argument().empty())
530 if (internalStatus() == Open) {
531 setStatus(cur, Collapsed);
532 if (geometry() == ButtonOnly)
533 cur.top().forwardPos();
535 setStatus(cur, Open);
536 else // if assign or anything else
542 InsetText::doDispatch(cur, cmd);
548 bool InsetCollapsable::allowMultiPar() const
550 return layout_.multipar;
554 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
555 FuncStatus & flag) const
557 switch (cmd.action) {
559 case LFUN_ACCENT_ACUTE:
560 case LFUN_ACCENT_BREVE:
561 case LFUN_ACCENT_CARON:
562 case LFUN_ACCENT_CEDILLA:
563 case LFUN_ACCENT_CIRCLE:
564 case LFUN_ACCENT_CIRCUMFLEX:
565 case LFUN_ACCENT_DOT:
566 case LFUN_ACCENT_GRAVE:
567 case LFUN_ACCENT_HUNGARIAN_UMLAUT:
568 case LFUN_ACCENT_MACRON:
569 case LFUN_ACCENT_OGONEK:
570 case LFUN_ACCENT_SPECIAL_CARON:
571 case LFUN_ACCENT_TIE:
572 case LFUN_ACCENT_TILDE:
573 case LFUN_ACCENT_UMLAUT:
574 case LFUN_ACCENT_UNDERBAR:
575 case LFUN_ACCENT_UNDERDOT:
577 case LFUN_BIBITEM_INSERT:
578 case LFUN_BOX_INSERT:
579 case LFUN_BRANCH_INSERT:
580 case LFUN_BREAK_LINE:
581 case LFUN_CAPTION_INSERT:
582 case LFUN_CLEARPAGE_INSERT:
583 case LFUN_CLEARDOUBLEPAGE_INSERT:
584 case LFUN_DEPTH_DECREMENT:
585 case LFUN_DEPTH_INCREMENT:
586 case LFUN_DOTS_INSERT:
587 case LFUN_END_OF_SENTENCE_PERIOD_INSERT:
588 case LFUN_ENVIRONMENT_INSERT:
589 case LFUN_ERT_INSERT:
590 case LFUN_FILE_INSERT:
591 case LFUN_FLEX_INSERT:
592 case LFUN_FLOAT_INSERT:
593 case LFUN_FLOAT_LIST:
594 case LFUN_FLOAT_WIDE_INSERT:
596 case LFUN_FONT_TYPEWRITER:
597 case LFUN_FONT_DEFAULT:
599 case LFUN_FONT_FREE_APPLY:
600 case LFUN_FONT_FREE_UPDATE:
602 case LFUN_FONT_ROMAN:
607 case LFUN_FONT_STATE:
608 case LFUN_FONT_UNDERLINE:
609 case LFUN_FOOTNOTE_INSERT:
610 case LFUN_HFILL_INSERT:
611 case LFUN_HYPERLINK_INSERT:
612 case LFUN_HYPHENATION_POINT_INSERT:
613 case LFUN_INDEX_INSERT:
614 case LFUN_INDEX_PRINT:
615 case LFUN_INSET_INSERT:
616 case LFUN_LABEL_GOTO:
617 case LFUN_LABEL_INSERT:
618 case LFUN_LIGATURE_BREAK_INSERT:
619 case LFUN_LINE_INSERT:
620 case LFUN_PAGEBREAK_INSERT:
623 case LFUN_LAYOUT_PARAGRAPH:
624 case LFUN_LAYOUT_TABULAR:
625 case LFUN_MARGINALNOTE_INSERT:
626 case LFUN_MATH_DISPLAY:
627 case LFUN_MATH_INSERT:
628 case LFUN_MATH_MATRIX:
631 case LFUN_MENU_SEPARATOR_INSERT:
633 case LFUN_NOMENCL_INSERT:
634 case LFUN_NOMENCL_PRINT:
635 case LFUN_NOTE_INSERT:
637 case LFUN_OPTIONAL_INSERT:
638 case LFUN_PARAGRAPH_PARAMS:
639 case LFUN_PARAGRAPH_PARAMS_APPLY:
640 case LFUN_PARAGRAPH_SPACING:
641 case LFUN_PARAGRAPH_UPDATE:
642 case LFUN_REFERENCE_NEXT:
643 case LFUN_SERVER_GOTO_FILE_ROW:
644 case LFUN_SERVER_NOTIFY:
645 case LFUN_SERVER_SET_XY:
646 case LFUN_SPACE_INSERT:
647 case LFUN_TABULAR_INSERT:
648 case LFUN_TOC_INSERT:
649 case LFUN_WRAP_INSERT:
650 if (layout_.passthru) {
654 return InsetText::getStatus(cur, cmd, flag);
656 case LFUN_INSET_TOGGLE:
657 if (cmd.argument() == "open" || cmd.argument() == "close" ||
658 cmd.argument() == "toggle")
665 return InsetText::getStatus(cur, cmd, flag);
670 void InsetCollapsable::setLabel(docstring const & l)
672 layout_.labelstring = l;
676 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
680 if (status_ == Collapsed)
681 cur.leaveInset(*this);
685 void InsetCollapsable::setLabelFont(Font const & font)
687 layout_.labelfont = font;
690 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
692 FloatList const & floats = bp.getTextClass().floats();
693 FloatList::const_iterator it = floats[type];
695 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
699 InsetCollapsable::Decoration InsetCollapsable::decoration() const
701 if (layout_.decoration == "classic")
703 if (layout_.decoration == "minimalistic")
705 if (layout_.decoration == "conglomerate")
707 if (name() == from_ascii("Flex"))
713 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
714 OutputParams const & runparams) const
716 // This implements the standard way of handling the LaTeX output of
717 // a collapsable inset, either a command or an environment. Standard
718 // collapsable insets should not redefine this, non-standard ones may
720 if (!layout_.latexname.empty()) {
721 if (layout_.latextype == "command") {
723 os << '\\' << from_utf8(layout_.latexname);
724 if (!layout_.latexparam.empty())
725 os << from_utf8(layout_.latexparam);
727 } else if (layout_.latextype == "environment") {
728 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
729 if (!layout_.latexparam.empty())
730 os << from_utf8(layout_.latexparam);
733 OutputParams rp = runparams;
734 if (layout_.passthru)
736 int i = InsetText::latex(buf, os, rp);
737 if (!layout_.latexname.empty()) {
738 if (layout_.latextype == "command") {
740 } else if (layout_.latextype == "environment") {
741 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
749 void InsetCollapsable::validate(LaTeXFeatures & features) const
751 // Force inclusion of preamble snippet in layout file
752 features.require(layout_.name);
753 InsetText::validate(features);