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 // This expression should not contain mi.base.texwidth
213 openinlined_ = !hasFixedWidth()
214 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
216 // Correct for button width.
217 dim.wid += textdim_.wid;
218 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
219 dim.asc = textdim_.asc;
221 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
222 dim.wid = max(dim.wid, textdim_.wid);
224 dim.wid = max(dim.wid, mi.base.textwidth);
229 dim.asc += TEXT_TO_INSET_OFFSET;
230 dim.des += TEXT_TO_INSET_OFFSET;
231 dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
232 mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
233 bool const changed = dim_ != dim;
239 bool InsetCollapsable::setMouseHover(bool mouse_hover)
241 mouse_hover_ = mouse_hover;
246 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
248 autoOpen_ = pi.base.bv->cursor().isInside(this);
249 text_.background_color_ = backgroundColor();
250 const int xx = x + TEXT_TO_INSET_OFFSET;
252 // Draw button first -- top, left or only
253 Dimension dimc = dimensionCollapsed();
254 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
255 if (geometry() == TopButton ||
256 geometry() == LeftButton ||
257 geometry() == ButtonOnly) {
258 button_dim.x1 = xx + 0;
259 button_dim.x2 = xx + dimc.width();
261 button_dim.y2 = top + dimc.height();
263 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
272 switch (geometry()) {
274 textx = xx + dimc.width();
275 texty = top + textdim_.asc;
276 InsetText::draw(pi, textx, texty);
280 texty = top + dimc.height() + textdim_.asc;
281 InsetText::draw(pi, textx, texty);
287 texty = y + textdim_.asc;
288 InsetText::draw(pi, textx, texty);
293 texty = y + textdim_.asc;
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 pi.pain.line(x, y + desc - 4, x, y + desc,
305 layout_.labelfont.color());
306 if (internalStatus() == Open)
307 pi.pain.line(x, y + desc,
308 x + dim_.wid - 3, y + desc,
309 layout_.labelfont.color());
311 // Make status_ value visible:
312 pi.pain.line(x, y + desc,
314 layout_.labelfont.color());
315 pi.pain.line(x + dim_.wid - 7, y + desc,
316 x + dim_.wid -3, y + desc,
317 layout_.labelfont.color());
319 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
320 layout_.labelfont.color());
322 // the label below the text. Can be toggled.
323 if (geometry() == SubLabel) {
324 Font font(layout_.labelfont);
325 font.realize(Font(Font::ALL_SANE));
332 docstring s = layout_.labelstring;
333 theFontMetrics(font).rectText(s, w, a, d);
334 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
335 s, font, Color::none, Color::none);
338 // a visual clue when the cursor is inside the inset
339 Cursor & cur = pi.base.bv->cursor();
340 if (cur.isInside(this)) {
343 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
344 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
345 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
346 layout_.labelfont.color());
347 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
348 layout_.labelfont.color());
352 setPosCache(pi, x, y);
356 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
358 x += TEXT_TO_INSET_OFFSET;
359 switch (geometry()) {
361 x += dimensionCollapsed().wid;
362 InsetText::drawSelection(pi, x, y);
365 y += dimensionCollapsed().des + textdim_.asc;
366 InsetText::drawSelection(pi, x, y);
373 InsetText::drawSelection(pi, x, y);
379 void InsetCollapsable::cursorPos(BufferView const & bv,
380 CursorSlice const & sl, bool boundary, int & x, int & y) const
382 BOOST_ASSERT(geometry() != ButtonOnly);
384 InsetText::cursorPos(bv, sl, boundary, x, y);
386 switch (geometry()) {
388 x += dimensionCollapsed().wid;
391 y += dimensionCollapsed().height() - ascent()
392 + TEXT_TO_INSET_OFFSET + textdim_.asc;
403 x += TEXT_TO_INSET_OFFSET;
407 Inset::EDITABLE InsetCollapsable::editable() const
409 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
413 bool InsetCollapsable::descendable() const
415 return geometry() != ButtonOnly;
419 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
421 return button_dim.contains(cmd.x, cmd.y);
425 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
428 pos_type const max_length = 15;
429 pos_type const p_siz = paragraphs().begin()->size();
430 pos_type const n = std::min(max_length, p_siz);
433 for (; i < n && j < p_siz; ++j) {
434 if (paragraphs().begin()->isInset(j))
436 label += paragraphs().begin()->getChar(j);
439 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
442 return label.empty() ? l : label;
446 void InsetCollapsable::edit(Cursor & cur, bool left)
448 //lyxerr << "InsetCollapsable: edit left/right" << endl;
450 InsetText::edit(cur, left);
454 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
456 //lyxerr << "InsetCollapsable: edit xy" << endl;
457 if (geometry() == ButtonOnly
458 || (button_dim.contains(x, y)
459 && geometry() != NoButton))
462 return InsetText::editXY(cur, x, y);
466 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
468 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
469 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
471 switch (cmd.action) {
472 case LFUN_MOUSE_PRESS:
473 if (cmd.button() == mouse_button::button1
475 && geometry() != NoButton) {
476 // reset selection if necessary (see bug 3060)
478 cur.bv().cursor().clearSelection();
484 if (geometry() == NoButton)
485 InsetText::doDispatch(cur, cmd);
486 else if (geometry() != ButtonOnly
488 InsetText::doDispatch(cur, cmd);
493 case LFUN_MOUSE_MOTION:
494 case LFUN_MOUSE_DOUBLE:
495 case LFUN_MOUSE_TRIPLE:
496 if (geometry() == NoButton)
497 InsetText::doDispatch(cur, cmd);
498 else if (geometry() != ButtonOnly
500 InsetText::doDispatch(cur, cmd);
505 case LFUN_MOUSE_RELEASE:
506 if (cmd.button() == mouse_button::button3) {
507 // There is no button to right click:
508 if (geometry() == Corners ||
509 geometry() == SubLabel ||
510 geometry() == NoButton) {
511 if (internalStatus() == Open)
512 setStatus(cur, Collapsed);
514 setStatus(cur, Open);
518 // configuration dialog
519 showInsetDialog(&cur.bv());
524 if (geometry() == NoButton) {
525 // The mouse click has to be within the inset!
526 InsetText::doDispatch(cur, cmd);
530 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
531 // if we are selecting, we do not want to
535 // Left button is clicked, the user asks to
536 // toggle the inset visual state.
538 cur.updateFlags(Update::Force | Update::FitCursor);
539 if (geometry() == ButtonOnly) {
540 setStatus(cur, Open);
544 setStatus(cur, Collapsed);
546 cur.bv().cursor() = cur;
550 // The mouse click is within the opened inset.
551 if (geometry() == TopButton
552 || geometry() == LeftButton)
553 InsetText::doDispatch(cur, cmd);
556 case LFUN_INSET_TOGGLE:
557 if (cmd.argument() == "open")
558 setStatus(cur, Open);
559 else if (cmd.argument() == "close")
560 setStatus(cur, Collapsed);
561 else if (cmd.argument() == "toggle" || cmd.argument().empty())
562 if (internalStatus() == Open) {
563 setStatus(cur, Collapsed);
564 if (geometry() == ButtonOnly)
565 cur.top().forwardPos();
567 setStatus(cur, Open);
568 else // if assign or anything else
574 InsetText::doDispatch(cur, cmd);
580 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
581 FuncStatus & flag) const
583 switch (cmd.action) {
585 case LFUN_INSET_TOGGLE:
586 if (cmd.argument() == "open" || cmd.argument() == "close" ||
587 cmd.argument() == "toggle")
594 return InsetText::getStatus(cur, cmd, flag);
599 void InsetCollapsable::setLabel(docstring const & l)
601 layout_.labelstring = l;
605 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
609 if (status_ == Collapsed)
610 cur.leaveInset(*this);
614 void InsetCollapsable::setLabelFont(Font const & font)
616 layout_.labelfont = font;
619 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
621 FloatList const & floats = bp.getTextClass().floats();
622 FloatList::const_iterator it = floats[type];
624 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
628 InsetCollapsable::Decoration InsetCollapsable::decoration() const
630 if (layout_.decoration == "classic")
632 if (layout_.decoration == "minimalistic")
634 if (layout_.decoration == "conglomerate")
636 if (name() == from_ascii("CharStyle"))
642 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
643 OutputParams const & runparams) const
645 // This implements the standard way of handling the LaTeX output of
646 // a collapsable inset, either a command or an environment. Standard
647 // collapsable insets should not redefine this, non-standard ones may
649 if (!layout_.latexname.empty()) {
650 if (layout_.latextype == "command") {
652 os << '\\' << from_utf8(layout_.latexname);
653 if (!layout_.latexparam.empty())
654 os << from_utf8(layout_.latexparam);
656 } else if (layout_.latextype == "environment") {
657 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
658 if (!layout_.latexparam.empty())
659 os << from_utf8(layout_.latexparam);
662 int i = InsetText::latex(buf, os, runparams);
663 if (!layout_.latexname.empty())
664 if (layout_.latextype == "command") {
666 } else if (layout_.latextype == "environment") {
667 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
674 void InsetCollapsable::validate(LaTeXFeatures & features) const
676 // Force inclusion of preamble snippet in layout file
677 features.addPreambleSnippet(layout_.preamble);
678 InsetText::validate(features);