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 textdim_(rhs.textdim_),
98 // the sole purpose of this copy constructor
104 void InsetCollapsable::setLayout(BufferParams const & bp)
106 layout_ = getLayout(bp);
110 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
122 text_.write(buf, os);
126 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
128 bool token_found = false;
131 string const token = lex.getString();
132 if (token == "status") {
134 string const tmp_token = lex.getString();
136 if (tmp_token == "collapsed") {
139 } else if (tmp_token == "open") {
143 lyxerr << "InsetCollapsable::read: Missing status!"
145 // Take countermeasures
146 lex.pushToken(token);
149 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
151 // take countermeasures
152 lex.pushToken(token);
155 InsetText::read(buf, lex);
158 status_ = isOpen() ? Open : Collapsed;
164 Dimension InsetCollapsable::dimensionCollapsed() const
167 theFontMetrics(layout_.labelfont).buttonText(
168 layout_.labelstring, dim.wid, dim.asc, dim.des);
173 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
177 autoOpen_ = mi.base.bv->cursor().isInside(this);
178 mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
180 switch (geometry()) {
182 InsetText::metrics(mi, dim);
185 InsetText::metrics(mi, dim);
190 InsetText::metrics(mi, dim);
191 // consider width of the inset label
192 Font font(layout_.labelfont);
193 font.realize(Font(Font::ALL_SANE));
199 docstring s = layout_.labelstring;
200 theFontMetrics(font).rectText(s, w, a, d);
201 dim.wid = max(dim.wid, w);
208 dim = dimensionCollapsed();
209 if (geometry() == TopButton
210 || 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 text_.background_color_ = backgroundColor();
248 const int xx = x + TEXT_TO_INSET_OFFSET;
250 // Draw button first -- top, left or only
251 Dimension dimc = dimensionCollapsed();
252 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
253 if (geometry() == TopButton ||
254 geometry() == LeftButton ||
255 geometry() == ButtonOnly) {
256 button_dim.x1 = xx + 0;
257 button_dim.x2 = xx + dimc.width();
259 button_dim.y2 = top + dimc.height();
261 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
270 switch (geometry()) {
272 textx = xx + dimc.width();
273 texty = top + textdim_.asc;
274 InsetText::draw(pi, textx, texty);
278 texty = top + dimc.height() + textdim_.asc;
279 InsetText::draw(pi, textx, texty);
285 texty = y + textdim_.asc;
286 InsetText::draw(pi, textx, texty);
291 texty = y + textdim_.asc;
292 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
293 InsetText::draw(pi, textx, texty);
294 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
296 int desc = InsetText::descent();
297 if (geometry() == SubLabel)
302 const int xx1 = xx + border_ - 1;
303 const int xx2 = x + dim_.wid - border_
304 - TEXT_TO_INSET_OFFSET + 1;
305 pi.pain.line(xx1, y + desc - 4,
307 layout_.labelfont.color());
308 if (internalStatus() == Open)
309 pi.pain.line(xx1, y + desc,
311 layout_.labelfont.color());
313 // Make status_ value visible:
314 pi.pain.line(xx1, y + desc,
316 layout_.labelfont.color());
317 pi.pain.line(xx2 - 4, y + desc,
319 layout_.labelfont.color());
321 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
322 layout_.labelfont.color());
324 // the label below the text. Can be toggled.
325 if (geometry() == SubLabel) {
326 Font font(layout_.labelfont);
327 font.realize(Font(Font::ALL_SANE));
334 docstring s = layout_.labelstring;
335 theFontMetrics(font).rectText(s, w, a, d);
336 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
337 s, font, Color::none, Color::none);
340 // a visual clue when the cursor is inside the inset
341 Cursor & cur = pi.base.bv->cursor();
342 if (cur.isInside(this)) {
345 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
346 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
347 pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
348 layout_.labelfont.color());
349 pi.pain.line(xx2 - 4, y, xx2, y,
350 layout_.labelfont.color());
354 setPosCache(pi, x, y);
358 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
360 x += TEXT_TO_INSET_OFFSET;
361 switch (geometry()) {
363 x += dimensionCollapsed().wid;
364 InsetText::drawSelection(pi, x, y);
367 y += dimensionCollapsed().des + textdim_.asc;
368 InsetText::drawSelection(pi, x, y);
375 InsetText::drawSelection(pi, x, y);
381 void InsetCollapsable::cursorPos(BufferView const & bv,
382 CursorSlice const & sl, bool boundary, int & x, int & y) const
384 BOOST_ASSERT(geometry() != ButtonOnly);
386 InsetText::cursorPos(bv, sl, boundary, x, y);
388 switch (geometry()) {
390 x += dimensionCollapsed().wid;
393 y += dimensionCollapsed().height() - ascent()
394 + TEXT_TO_INSET_OFFSET + textdim_.asc;
405 x += TEXT_TO_INSET_OFFSET;
409 Inset::EDITABLE InsetCollapsable::editable() const
411 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
415 bool InsetCollapsable::descendable() const
417 return geometry() != ButtonOnly;
421 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
423 return button_dim.contains(cmd.x, cmd.y);
427 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
430 pos_type const max_length = 15;
431 pos_type const p_siz = paragraphs().begin()->size();
432 pos_type const n = std::min(max_length, p_siz);
435 for (; i < n && j < p_siz; ++j) {
436 if (paragraphs().begin()->isInset(j))
438 label += paragraphs().begin()->getChar(j);
441 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
444 return label.empty() ? l : label;
448 void InsetCollapsable::edit(Cursor & cur, bool left)
450 //lyxerr << "InsetCollapsable: edit left/right" << endl;
452 InsetText::edit(cur, left);
456 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
458 //lyxerr << "InsetCollapsable: edit xy" << endl;
459 if (geometry() == ButtonOnly
460 || (button_dim.contains(x, y)
461 && geometry() != NoButton))
464 return InsetText::editXY(cur, x, y);
468 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
470 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
471 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
473 switch (cmd.action) {
474 case LFUN_MOUSE_PRESS:
475 if (cmd.button() == mouse_button::button1
477 && geometry() != NoButton) {
478 // reset selection if necessary (see bug 3060)
480 cur.bv().cursor().clearSelection();
486 if (geometry() == NoButton)
487 InsetText::doDispatch(cur, cmd);
488 else if (geometry() != ButtonOnly
490 InsetText::doDispatch(cur, cmd);
495 case LFUN_MOUSE_MOTION:
496 case LFUN_MOUSE_DOUBLE:
497 case LFUN_MOUSE_TRIPLE:
498 if (geometry() == NoButton)
499 InsetText::doDispatch(cur, cmd);
500 else if (geometry() != ButtonOnly
502 InsetText::doDispatch(cur, cmd);
507 case LFUN_MOUSE_RELEASE:
508 if (cmd.button() == mouse_button::button3) {
509 // There is no button to right click:
510 if (geometry() == Corners ||
511 geometry() == SubLabel ||
512 geometry() == NoButton) {
513 if (internalStatus() == Open)
514 setStatus(cur, Collapsed);
516 setStatus(cur, Open);
520 // configuration dialog
521 showInsetDialog(&cur.bv());
526 if (geometry() == NoButton) {
527 // The mouse click has to be within the inset!
528 InsetText::doDispatch(cur, cmd);
532 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
533 // if we are selecting, we do not want to
537 // Left button is clicked, the user asks to
538 // toggle the inset visual state.
540 cur.updateFlags(Update::Force | Update::FitCursor);
541 if (geometry() == ButtonOnly) {
542 setStatus(cur, Open);
546 setStatus(cur, Collapsed);
548 cur.bv().cursor() = cur;
552 // The mouse click is within the opened inset.
553 if (geometry() == TopButton
554 || geometry() == LeftButton)
555 InsetText::doDispatch(cur, cmd);
558 case LFUN_INSET_TOGGLE:
559 if (cmd.argument() == "open")
560 setStatus(cur, Open);
561 else if (cmd.argument() == "close")
562 setStatus(cur, Collapsed);
563 else if (cmd.argument() == "toggle" || cmd.argument().empty())
564 if (internalStatus() == Open) {
565 setStatus(cur, Collapsed);
566 if (geometry() == ButtonOnly)
567 cur.top().forwardPos();
569 setStatus(cur, Open);
570 else // if assign or anything else
576 InsetText::doDispatch(cur, cmd);
582 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
583 FuncStatus & flag) const
585 switch (cmd.action) {
587 case LFUN_INSET_TOGGLE:
588 if (cmd.argument() == "open" || cmd.argument() == "close" ||
589 cmd.argument() == "toggle")
596 return InsetText::getStatus(cur, cmd, flag);
601 void InsetCollapsable::setLabel(docstring const & l)
603 layout_.labelstring = l;
607 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
611 if (status_ == Collapsed)
612 cur.leaveInset(*this);
616 void InsetCollapsable::setLabelFont(Font const & font)
618 layout_.labelfont = font;
621 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
623 FloatList const & floats = bp.getTextClass().floats();
624 FloatList::const_iterator it = floats[type];
626 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
630 InsetCollapsable::Decoration InsetCollapsable::decoration() const
632 if (layout_.decoration == "classic")
634 if (layout_.decoration == "minimalistic")
636 if (layout_.decoration == "conglomerate")
638 if (name() == from_ascii("CharStyle"))
644 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
645 OutputParams const & runparams) const
647 // This implements the standard way of handling the LaTeX output of
648 // a collapsable inset, either a command or an environment. Standard
649 // collapsable insets should not redefine this, non-standard ones may
651 if (!layout_.latexname.empty()) {
652 if (layout_.latextype == "command") {
654 os << '\\' << from_utf8(layout_.latexname);
655 if (!layout_.latexparam.empty())
656 os << from_utf8(layout_.latexparam);
658 } else if (layout_.latextype == "environment") {
659 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
660 if (!layout_.latexparam.empty())
661 os << from_utf8(layout_.latexparam);
664 int i = InsetText::latex(buf, os, runparams);
665 if (!layout_.latexname.empty())
666 if (layout_.latextype == "command") {
668 } else if (layout_.latextype == "environment") {
669 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
676 void InsetCollapsable::validate(LaTeXFeatures & features) const
678 // Force inclusion of preamble snippet in layout file
679 features.addPreambleSnippet(layout_.preamble);
680 InsetText::validate(features);