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"
32 #include "frontends/FontMetrics.h"
33 #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 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
176 autoOpen_ = mi.base.bv->cursor().isInside(this);
177 mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
179 switch (geometry()) {
181 InsetText::metrics(mi, dim);
184 InsetText::metrics(mi, dim);
189 InsetText::metrics(mi, dim);
190 // consider width of the inset label
191 Font font(layout_.labelfont);
192 font.realize(Font(Font::ALL_SANE));
198 docstring s = layout_.labelstring;
199 theFontMetrics(font).rectText(s, w, a, d);
200 dim.wid = max(dim.wid, w);
207 dim = dimensionCollapsed();
208 if (geometry() == TopButton
209 || geometry() == LeftButton) {
211 InsetText::metrics(mi, textdim);
212 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
214 // Correct for button width.
215 dim.wid += textdim.wid;
216 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
217 dim.asc = textdim.asc;
219 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
220 dim.wid = max(dim.wid, textdim.wid);
222 dim.wid = max(dim.wid, mi.base.textwidth);
227 dim.asc += TEXT_TO_INSET_OFFSET;
228 dim.des += TEXT_TO_INSET_OFFSET;
229 dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
230 mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
231 bool const changed = dim_ != dim;
237 bool InsetCollapsable::setMouseHover(bool mouse_hover)
239 mouse_hover_ = mouse_hover;
244 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
246 autoOpen_ = pi.base.bv->cursor().isInside(this);
247 int const old_color = pi.background_color;
248 pi.background_color = backgroundColor();
249 int const xx = x + TEXT_TO_INSET_OFFSET;
251 // Draw button first -- top, left or only
252 Dimension dimc = dimensionCollapsed();
254 if (geometry() == TopButton ||
255 geometry() == LeftButton ||
256 geometry() == ButtonOnly) {
257 button_dim.x1 = xx + 0;
258 button_dim.x2 = xx + dimc.width();
259 button_dim.y1 = y - dimc.asc;
260 button_dim.y2 = y + dimc.des;
262 pi.pain.buttonText(xx, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
270 int const baseline = y;
272 switch (geometry()) {
274 textx = xx + dimc.width();
276 InsetText::draw(pi, textx, texty);
280 texty = baseline + dimc.height();
281 InsetText::draw(pi, textx, texty);
288 InsetText::draw(pi, textx, texty);
294 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
295 InsetText::draw(pi, textx, texty);
296 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
298 int desc = InsetText::descent();
299 if (geometry() == SubLabel)
304 const int xx1 = xx + border_ - 1;
305 const int xx2 = x + dim_.wid - border_
306 - TEXT_TO_INSET_OFFSET + 1;
307 pi.pain.line(xx1, y + desc - 4,
309 layout_.labelfont.color());
310 if (internalStatus() == Open)
311 pi.pain.line(xx1, y + desc,
313 layout_.labelfont.color());
315 // Make status_ value visible:
316 pi.pain.line(xx1, y + desc,
318 layout_.labelfont.color());
319 pi.pain.line(xx2 - 4, y + desc,
321 layout_.labelfont.color());
323 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
324 layout_.labelfont.color());
326 // the label below the text. Can be toggled.
327 if (geometry() == SubLabel) {
328 Font font(layout_.labelfont);
329 font.realize(Font(Font::ALL_SANE));
336 docstring s = layout_.labelstring;
337 theFontMetrics(font).rectText(s, w, a, d);
338 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
339 s, font, Color::none, Color::none);
342 // a visual clue when the cursor is inside the inset
343 Cursor & cur = pi.base.bv->cursor();
344 if (cur.isInside(this)) {
347 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
348 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
349 pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
350 layout_.labelfont.color());
351 pi.pain.line(xx2 - 4, y, xx2, y,
352 layout_.labelfont.color());
356 setPosCache(pi, x, y);
357 pi.background_color = old_color;
361 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
363 x += TEXT_TO_INSET_OFFSET;
364 switch (geometry()) {
366 x += dimensionCollapsed().wid;
367 InsetText::drawSelection(pi, x, y);
370 y += dimensionCollapsed().des;
371 InsetText::drawSelection(pi, x, y);
378 InsetText::drawSelection(pi, x, y);
384 void InsetCollapsable::cursorPos(BufferView const & bv,
385 CursorSlice const & sl, bool boundary, int & x, int & y) const
387 BOOST_ASSERT(geometry() != ButtonOnly);
389 InsetText::cursorPos(bv, sl, boundary, x, y);
391 switch (geometry()) {
393 x += dimensionCollapsed().wid;
396 TextMetrics const & tm = bv.textMetrics(&text_);
397 y += dimensionCollapsed().height() - ascent()
398 + TEXT_TO_INSET_OFFSET + tm.ascent();
410 x += TEXT_TO_INSET_OFFSET;
414 Inset::EDITABLE InsetCollapsable::editable() const
416 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
420 bool InsetCollapsable::descendable() const
422 return geometry() != ButtonOnly;
426 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
428 return button_dim.contains(cmd.x, cmd.y);
432 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
435 pos_type const max_length = 15;
436 pos_type const p_siz = paragraphs().begin()->size();
437 pos_type const n = std::min(max_length, p_siz);
440 for (; i < n && j < p_siz; ++j) {
441 if (paragraphs().begin()->isInset(j))
443 label += paragraphs().begin()->getChar(j);
446 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
449 return label.empty() ? l : label;
453 void InsetCollapsable::edit(Cursor & cur, bool left)
455 //lyxerr << "InsetCollapsable: edit left/right" << endl;
457 InsetText::edit(cur, left);
461 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
463 //lyxerr << "InsetCollapsable: edit xy" << endl;
464 if (geometry() == ButtonOnly
465 || (button_dim.contains(x, y)
466 && geometry() != NoButton))
469 return InsetText::editXY(cur, x, y);
473 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
475 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
476 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
478 switch (cmd.action) {
479 case LFUN_MOUSE_PRESS:
480 if (cmd.button() == mouse_button::button1
482 && geometry() != NoButton) {
483 // reset selection if necessary (see bug 3060)
485 cur.bv().cursor().clearSelection();
491 if (geometry() == NoButton)
492 InsetText::doDispatch(cur, cmd);
493 else if (geometry() != ButtonOnly
495 InsetText::doDispatch(cur, cmd);
500 case LFUN_MOUSE_MOTION:
501 case LFUN_MOUSE_DOUBLE:
502 case LFUN_MOUSE_TRIPLE:
503 if (geometry() == NoButton)
504 InsetText::doDispatch(cur, cmd);
505 else if (geometry() != ButtonOnly
507 InsetText::doDispatch(cur, cmd);
512 case LFUN_MOUSE_RELEASE:
513 if (cmd.button() == mouse_button::button3) {
514 // There is no button to right click:
515 if (geometry() == Corners ||
516 geometry() == SubLabel ||
517 geometry() == NoButton) {
518 if (internalStatus() == Open)
519 setStatus(cur, Collapsed);
521 setStatus(cur, Open);
525 // configuration dialog
526 showInsetDialog(&cur.bv());
531 if (geometry() == NoButton) {
532 // The mouse click has to be within the inset!
533 InsetText::doDispatch(cur, cmd);
537 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
538 // if we are selecting, we do not want to
542 // Left button is clicked, the user asks to
543 // toggle the inset visual state.
545 cur.updateFlags(Update::Force | Update::FitCursor);
546 if (geometry() == ButtonOnly) {
547 setStatus(cur, Open);
551 setStatus(cur, Collapsed);
553 cur.bv().cursor() = cur;
557 // The mouse click is within the opened inset.
558 if (geometry() == TopButton
559 || geometry() == LeftButton)
560 InsetText::doDispatch(cur, cmd);
563 case LFUN_INSET_TOGGLE:
564 if (cmd.argument() == "open")
565 setStatus(cur, Open);
566 else if (cmd.argument() == "close")
567 setStatus(cur, Collapsed);
568 else if (cmd.argument() == "toggle" || cmd.argument().empty())
569 if (internalStatus() == Open) {
570 setStatus(cur, Collapsed);
571 if (geometry() == ButtonOnly)
572 cur.top().forwardPos();
574 setStatus(cur, Open);
575 else // if assign or anything else
581 InsetText::doDispatch(cur, cmd);
587 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
588 FuncStatus & flag) const
590 switch (cmd.action) {
592 case LFUN_INSET_TOGGLE:
593 if (cmd.argument() == "open" || cmd.argument() == "close" ||
594 cmd.argument() == "toggle")
601 return InsetText::getStatus(cur, cmd, flag);
606 void InsetCollapsable::setLabel(docstring const & l)
608 layout_.labelstring = l;
612 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
616 if (status_ == Collapsed)
617 cur.leaveInset(*this);
621 void InsetCollapsable::setLabelFont(Font const & font)
623 layout_.labelfont = font;
626 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
628 FloatList const & floats = bp.getTextClass().floats();
629 FloatList::const_iterator it = floats[type];
631 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
635 InsetCollapsable::Decoration InsetCollapsable::decoration() const
637 if (layout_.decoration == "classic")
639 if (layout_.decoration == "minimalistic")
641 if (layout_.decoration == "conglomerate")
643 if (name() == from_ascii("Flex"))
649 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
650 OutputParams const & runparams) const
652 // This implements the standard way of handling the LaTeX output of
653 // a collapsable inset, either a command or an environment. Standard
654 // collapsable insets should not redefine this, non-standard ones may
656 if (!layout_.latexname.empty()) {
657 if (layout_.latextype == "command") {
659 os << '\\' << from_utf8(layout_.latexname);
660 if (!layout_.latexparam.empty())
661 os << from_utf8(layout_.latexparam);
663 } else if (layout_.latextype == "environment") {
664 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
665 if (!layout_.latexparam.empty())
666 os << from_utf8(layout_.latexparam);
669 int i = InsetText::latex(buf, os, runparams);
670 if (!layout_.latexname.empty())
671 if (layout_.latextype == "command") {
673 } else if (layout_.latextype == "environment") {
674 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
681 void InsetCollapsable::validate(LaTeXFeatures & features) const
683 // Force inclusion of preamble snippet in layout file
684 features.require(layout_.name);
685 InsetText::validate(features);