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"
44 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
46 return autoOpen_ ? Open : status_;
50 InsetCollapsable::Geometry InsetCollapsable::geometry() const
52 switch (decoration()) {
54 if (status() == Open) {
63 return status() == Open ? NoButton : ButtonOnly ;
66 return status() == Open ? SubLabel : Corners ;
69 // dummy return value to shut down a warning,
75 InsetCollapsable::InsetCollapsable
76 (BufferParams const & bp, CollapseStatus status)
77 : InsetText(bp), status_(status),
78 openinlined_(false), autoOpen_(false), mouse_hover_(false)
80 setAutoBreakRows(true);
82 setFrameColor(Color::collapsableframe);
84 // Fallback for lacking inset layout item
85 layout_.bgcolor = Color::background;
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91 button_dim(rhs.button_dim),
93 topbaseline(rhs.topbaseline),
96 openinlined_(rhs.openinlined_),
97 autoOpen_(rhs.autoOpen_),
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
175 autoOpen_ = mi.base.bv->cursor().isInside(this);
177 switch (geometry()) {
179 InsetText::metrics(mi, dim);
182 InsetText::metrics(mi, dim);
187 InsetText::metrics(mi, dim);
188 // consider width of the inset label
189 Font font(layout_.labelfont);
190 font.realize(Font(Font::ALL_SANE));
196 docstring s = layout_.labelstring;
197 theFontMetrics(font).rectText(s, w, a, d);
198 dim.wid = max(dim.wid, w);
205 dim = dimensionCollapsed();
206 if (geometry() == TopButton
207 || geometry() == LeftButton) {
209 InsetText::metrics(mi, textdim);
210 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
212 // Correct for button width.
213 dim.wid += textdim.wid;
214 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
215 dim.asc = textdim.asc;
217 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
218 dim.wid = max(dim.wid, textdim.wid);
224 bool const changed = dim_ != dim;
230 bool InsetCollapsable::setMouseHover(bool mouse_hover)
232 mouse_hover_ = mouse_hover;
237 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
239 autoOpen_ = pi.base.bv->cursor().isInside(this);
240 int const old_color = pi.background_color;
241 pi.background_color = backgroundColor();
243 // Draw button first -- top, left or only
244 Dimension dimc = dimensionCollapsed();
246 if (geometry() == TopButton ||
247 geometry() == LeftButton ||
248 geometry() == ButtonOnly) {
249 button_dim.x1 = x + 0;
250 button_dim.x2 = x + dimc.width();
251 button_dim.y1 = y - dimc.asc;
252 button_dim.y2 = y + dimc.des;
254 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
262 Dimension const textdim = InsetText::dimension(*pi.base.bv);
263 int const baseline = y;
265 switch (geometry()) {
267 textx = x + dimc.width();
269 InsetText::draw(pi, textx, texty);
273 texty = baseline + dimc.des + textdim.asc;
274 InsetText::draw(pi, textx, texty);
281 InsetText::draw(pi, textx, texty);
287 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
288 InsetText::draw(pi, textx, texty);
289 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
291 int desc = InsetText::descent();
292 if (geometry() == SubLabel)
297 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
298 const int xx2 = x + textdim.wid - 2 * TEXT_TO_INSET_OFFSET - 1;
299 pi.pain.line(xx1, y + desc - 4,
301 layout_.labelfont.color());
302 if (internalStatus() == Open)
303 pi.pain.line(xx1, y + desc,
305 layout_.labelfont.color());
307 // Make status_ value visible:
308 pi.pain.line(xx1, y + desc,
310 layout_.labelfont.color());
311 pi.pain.line(xx2 - 4, y + desc,
313 layout_.labelfont.color());
315 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
316 layout_.labelfont.color());
318 // the label below the text. Can be toggled.
319 if (geometry() == SubLabel) {
320 Font font(layout_.labelfont);
321 font.realize(Font(Font::ALL_SANE));
327 docstring s = layout_.labelstring;
328 theFontMetrics(font).rectText(s, w, a, d);
329 pi.pain.rectText(x + (textdim.wid - w) / 2, y + desc + a,
330 s, font, Color::none, Color::none);
333 // a visual clue when the cursor is inside the inset
334 Cursor & cur = pi.base.bv->cursor();
335 if (cur.isInside(this)) {
338 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
339 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
340 pi.pain.line(xx2, y + 4, x + textdim.wid - 3, y,
341 layout_.labelfont.color());
342 pi.pain.line(xx2 - 4, y, xx2, y,
343 layout_.labelfont.color());
347 pi.background_color = old_color;
351 void InsetCollapsable::cursorPos(BufferView const & bv,
352 CursorSlice const & sl, bool boundary, int & x, int & y) const
354 BOOST_ASSERT(geometry() != ButtonOnly);
356 InsetText::cursorPos(bv, sl, boundary, x, y);
357 Dimension const textdim = InsetText::dimension(bv);
359 switch (geometry()) {
361 x += dimensionCollapsed().wid;
364 y += dimensionCollapsed().des + textdim.asc;
379 Inset::EDITABLE InsetCollapsable::editable() const
381 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
385 bool InsetCollapsable::descendable() const
387 return geometry() != ButtonOnly;
391 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
393 return button_dim.contains(cmd.x, cmd.y);
397 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
400 pos_type const max_length = 15;
401 pos_type const p_siz = paragraphs().begin()->size();
402 pos_type const n = std::min(max_length, p_siz);
405 for (; i < n && j < p_siz; ++j) {
406 if (paragraphs().begin()->isInset(j))
408 label += paragraphs().begin()->getChar(j);
411 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
414 return label.empty() ? l : label;
418 void InsetCollapsable::edit(Cursor & cur, bool left)
420 //lyxerr << "InsetCollapsable: edit left/right" << endl;
422 InsetText::edit(cur, left);
426 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
428 //lyxerr << "InsetCollapsable: edit xy" << endl;
429 if (geometry() == ButtonOnly
430 || (button_dim.contains(x, y)
431 && geometry() != NoButton))
434 return InsetText::editXY(cur, x, y);
438 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
440 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
441 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
443 switch (cmd.action) {
444 case LFUN_MOUSE_PRESS:
445 if (cmd.button() == mouse_button::button1
447 && geometry() != NoButton) {
448 // reset selection if necessary (see bug 3060)
450 cur.bv().cursor().clearSelection();
456 if (geometry() == NoButton)
457 InsetText::doDispatch(cur, cmd);
458 else if (geometry() != ButtonOnly
460 InsetText::doDispatch(cur, cmd);
465 case LFUN_MOUSE_MOTION:
466 case LFUN_MOUSE_DOUBLE:
467 case LFUN_MOUSE_TRIPLE:
468 if (geometry() == NoButton)
469 InsetText::doDispatch(cur, cmd);
470 else if (geometry() != ButtonOnly
472 InsetText::doDispatch(cur, cmd);
477 case LFUN_MOUSE_RELEASE:
478 if (cmd.button() == mouse_button::button3) {
479 // There is no button to right click:
480 if (geometry() == Corners ||
481 geometry() == SubLabel ||
482 geometry() == NoButton) {
483 if (internalStatus() == Open)
484 setStatus(cur, Collapsed);
486 setStatus(cur, Open);
490 // configuration dialog
491 showInsetDialog(&cur.bv());
496 if (geometry() == NoButton) {
497 // The mouse click has to be within the inset!
498 InsetText::doDispatch(cur, cmd);
502 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
503 // if we are selecting, we do not want to
507 // Left button is clicked, the user asks to
508 // toggle the inset visual state.
510 cur.updateFlags(Update::Force | Update::FitCursor);
511 if (geometry() == ButtonOnly) {
512 setStatus(cur, Open);
516 setStatus(cur, Collapsed);
518 cur.bv().cursor() = cur;
522 // The mouse click is within the opened inset.
523 if (geometry() == TopButton
524 || geometry() == LeftButton)
525 InsetText::doDispatch(cur, cmd);
528 case LFUN_INSET_TOGGLE:
529 if (cmd.argument() == "open")
530 setStatus(cur, Open);
531 else if (cmd.argument() == "close")
532 setStatus(cur, Collapsed);
533 else if (cmd.argument() == "toggle" || cmd.argument().empty())
534 if (internalStatus() == Open) {
535 setStatus(cur, Collapsed);
536 if (geometry() == ButtonOnly)
537 cur.top().forwardPos();
539 setStatus(cur, Open);
540 else // if assign or anything else
546 InsetText::doDispatch(cur, cmd);
552 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
553 FuncStatus & flag) const
555 switch (cmd.action) {
557 case LFUN_INSET_TOGGLE:
558 if (cmd.argument() == "open" || cmd.argument() == "close" ||
559 cmd.argument() == "toggle")
566 return InsetText::getStatus(cur, cmd, flag);
571 void InsetCollapsable::setLabel(docstring const & l)
573 layout_.labelstring = l;
577 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
581 if (status_ == Collapsed)
582 cur.leaveInset(*this);
586 void InsetCollapsable::setLabelFont(Font const & font)
588 layout_.labelfont = font;
591 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
593 FloatList const & floats = bp.getTextClass().floats();
594 FloatList::const_iterator it = floats[type];
596 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
600 InsetCollapsable::Decoration InsetCollapsable::decoration() const
602 if (layout_.decoration == "classic")
604 if (layout_.decoration == "minimalistic")
606 if (layout_.decoration == "conglomerate")
608 if (name() == from_ascii("Flex"))
614 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
615 OutputParams const & runparams) const
617 // This implements the standard way of handling the LaTeX output of
618 // a collapsable inset, either a command or an environment. Standard
619 // collapsable insets should not redefine this, non-standard ones may
621 if (!layout_.latexname.empty()) {
622 if (layout_.latextype == "command") {
624 os << '\\' << from_utf8(layout_.latexname);
625 if (!layout_.latexparam.empty())
626 os << from_utf8(layout_.latexparam);
628 } else if (layout_.latextype == "environment") {
629 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
630 if (!layout_.latexparam.empty())
631 os << from_utf8(layout_.latexparam);
634 int i = InsetText::latex(buf, os, runparams);
635 if (!layout_.latexname.empty()) {
636 if (layout_.latextype == "command") {
638 } else if (layout_.latextype == "environment") {
639 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
647 void InsetCollapsable::validate(LaTeXFeatures & features) const
649 // Force inclusion of preamble snippet in layout file
650 features.require(layout_.name);
651 InsetText::validate(features);