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) {
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 // FIXME: this is not ideal but we need to clear it
217 // out because the Row::changed() status is reset.
218 mi.base.bv->textMetrics(&text_).clear();
219 // Correct for button width, and re-fit
220 mi.base.textwidth -= dim.wid;
221 InsetText::metrics(mi, textdim_);
222 mi.base.textwidth += dim.wid;
223 dim.wid += textdim_.wid;
224 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
225 dim.asc = textdim_.asc;
227 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
228 dim.wid = max(dim.wid, textdim_.wid);
230 dim.wid = max(dim.wid, mi.base.textwidth);
235 dim.asc += TEXT_TO_INSET_OFFSET;
236 dim.des += TEXT_TO_INSET_OFFSET;
237 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
238 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
239 bool const changed = dim_ != dim;
245 bool InsetCollapsable::setMouseHover(bool mouse_hover)
247 mouse_hover_ = mouse_hover;
252 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
254 autoOpen_ = pi.base.bv->cursor().isInside(this);
255 text_.background_color_ = backgroundColor();
256 const int xx = x + TEXT_TO_INSET_OFFSET;
258 // Draw button first -- top, left or only
259 Dimension dimc = dimensionCollapsed();
260 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
261 if (geometry() == TopButton ||
262 geometry() == LeftButton ||
263 geometry() == ButtonOnly) {
264 button_dim.x1 = xx + 0;
265 button_dim.x2 = xx + dimc.width();
267 button_dim.y2 = top + dimc.height();
269 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
273 switch (geometry()) {
275 textx = xx + dimc.width();
276 texty = top + textdim_.asc;
277 InsetText::draw(pi, textx, texty);
281 texty = top + dimc.height() + textdim_.asc;
282 InsetText::draw(pi, textx, texty);
288 texty = y + textdim_.asc;
289 InsetText::draw(pi, textx, texty);
294 texty = y + textdim_.asc;
295 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
296 InsetText::draw(pi, textx, texty);
297 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
299 int desc = InsetText::descent();
300 if (status() == Open)
305 pi.pain.line(x, y + desc - 4, x, y + desc,
306 layout_.labelfont.color());
307 if (internalStatus() == Open)
308 pi.pain.line(x, y + desc,
309 x + dim_.wid - 3, y + desc,
310 layout_.labelfont.color());
312 // Make status_ value visible:
313 pi.pain.line(x, y + desc,
315 layout_.labelfont.color());
316 pi.pain.line(x + dim_.wid - 7, y + desc,
317 x + dim_.wid -3, y + desc,
318 layout_.labelfont.color());
320 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
321 layout_.labelfont.color());
323 // the label of the charstyle. Can be toggled.
324 if (status() == Open) {
325 Font font(layout_.labelfont);
326 font.realize(Font(Font::ALL_SANE));
333 docstring s = layout_.labelstring;
334 theFontMetrics(font).rectText(s, w, a, d);
335 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
336 s, font, Color::none, Color::none);
339 // a visual cue when the cursor is inside the inset
340 Cursor & cur = pi.base.bv->cursor();
341 if (cur.isInside(this)) {
344 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
345 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
346 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
347 layout_.labelfont.color());
348 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
349 layout_.labelfont.color());
353 setPosCache(pi, x, y);
357 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
359 x += TEXT_TO_INSET_OFFSET;
360 switch (geometry()) {
362 x += dimensionCollapsed().wid;
363 InsetText::drawSelection(pi, x, y);
366 y += dimensionCollapsed().des + textdim_.asc;
367 InsetText::drawSelection(pi, x, y);
374 InsetText::drawSelection(pi, x, y);
380 void InsetCollapsable::cursorPos(BufferView const & bv,
381 CursorSlice const & sl, bool boundary, int & x, int & y) const
383 BOOST_ASSERT(geometry() != ButtonOnly);
385 InsetText::cursorPos(bv, sl, boundary, x, y);
387 switch (geometry()) {
389 x += dimensionCollapsed().wid;
392 y += dimensionCollapsed().height() - ascent()
393 + TEXT_TO_INSET_OFFSET + textdim_.asc;
404 x += TEXT_TO_INSET_OFFSET;
408 Inset::EDITABLE InsetCollapsable::editable() const
410 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
414 bool InsetCollapsable::descendable() const
416 return geometry() != ButtonOnly;
420 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
422 return button_dim.contains(cmd.x, cmd.y);
426 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
429 pos_type const max_length = 15;
430 pos_type const p_siz = paragraphs().begin()->size();
431 pos_type const n = std::min(max_length, p_siz);
434 for (; i < n && j < p_siz; ++j) {
435 if (paragraphs().begin()->isInset(j))
437 label += paragraphs().begin()->getChar(j);
440 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
443 return label.empty() ? l : label;
447 void InsetCollapsable::edit(Cursor & cur, bool left)
449 //lyxerr << "InsetCollapsable: edit left/right" << endl;
451 InsetText::edit(cur, left);
455 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
457 //lyxerr << "InsetCollapsable: edit xy" << endl;
458 if (geometry() == ButtonOnly
459 || (button_dim.contains(x, y)
460 && geometry() != NoButton))
463 return InsetText::editXY(cur, x, y);
467 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
469 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
470 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
472 switch (cmd.action) {
473 case LFUN_MOUSE_PRESS:
474 if (cmd.button() == mouse_button::button1
476 && geometry() != NoButton) {
477 // reset selection if necessary (see bug 3060)
479 cur.bv().cursor().clearSelection();
485 if (geometry() == NoButton)
486 InsetText::doDispatch(cur, cmd);
487 else if (geometry() != ButtonOnly
489 InsetText::doDispatch(cur, cmd);
494 case LFUN_MOUSE_MOTION:
495 case LFUN_MOUSE_DOUBLE:
496 case LFUN_MOUSE_TRIPLE:
497 if (geometry() == NoButton)
498 InsetText::doDispatch(cur, cmd);
499 else if (geometry() != ButtonOnly
501 InsetText::doDispatch(cur, cmd);
506 case LFUN_MOUSE_RELEASE:
507 if (cmd.button() == mouse_button::button3) {
508 // There is no button to right click:
509 if (geometry() == Corners ||
510 geometry() == SubLabel ||
511 geometry() == NoButton) {
512 if (internalStatus() == Open)
513 setStatus(cur, Collapsed);
515 setStatus(cur, Open);
519 // configuration dialog
520 showInsetDialog(&cur.bv());
525 if (geometry() == NoButton) {
526 // The mouse click has to be within the inset!
527 InsetText::doDispatch(cur, cmd);
531 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
532 // if we are selecting, we do not want to
536 // Left button is clicked, the user asks to
537 // toggle the inset visual state.
539 cur.updateFlags(Update::Force | Update::FitCursor);
540 if (geometry() == ButtonOnly) {
541 setStatus(cur, Open);
545 setStatus(cur, Collapsed);
547 cur.bv().cursor() = cur;
551 // The mouse click is within the opened inset.
552 if (geometry() == TopButton
553 || geometry() == LeftButton)
554 InsetText::doDispatch(cur, cmd);
557 case LFUN_INSET_TOGGLE:
558 if (cmd.argument() == "open")
559 setStatus(cur, Open);
560 else if (cmd.argument() == "close")
561 setStatus(cur, Collapsed);
562 else if (cmd.argument() == "toggle" || cmd.argument().empty())
563 if (internalStatus() == Open) {
564 setStatus(cur, Collapsed);
565 if (geometry() == ButtonOnly)
566 cur.top().forwardPos();
568 setStatus(cur, Open);
569 else // if assign or anything else
575 InsetText::doDispatch(cur, cmd);
581 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
582 FuncStatus & flag) const
584 switch (cmd.action) {
586 case LFUN_INSET_TOGGLE:
587 if (cmd.argument() == "open" || cmd.argument() == "close" ||
588 cmd.argument() == "toggle")
595 return InsetText::getStatus(cur, cmd, flag);
600 void InsetCollapsable::setLabel(docstring const & l)
602 layout_.labelstring = l;
606 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
610 if (status_ == Collapsed)
611 cur.leaveInset(*this);
615 void InsetCollapsable::setLabelFont(Font const & font)
617 layout_.labelfont = font;
620 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
622 FloatList const & floats = bp.getTextClass().floats();
623 FloatList::const_iterator it = floats[type];
625 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
629 InsetCollapsable::Decoration InsetCollapsable::decoration() const
631 if (layout_.decoration == "classic")
633 if (layout_.decoration == "minimalistic")
635 if (layout_.decoration == "conglomerate")
637 if (name() == from_ascii("CharStyle"))
643 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
644 OutputParams const & runparams) const
646 // This implements the standard way of handling the LaTeX output of
647 // a collapsable inset, either a command or an environment. Standard
648 // collapsable insets should not redefine this, non-standard ones may
650 if (!layout_.latexname.empty()) {
651 if (layout_.latextype == "command") {
653 os << '\\' << from_utf8(layout_.latexname);
654 if (!layout_.latexparam.empty())
655 os << from_utf8(layout_.latexparam);
657 } else if (layout_.latextype == "environment") {
658 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
659 if (!layout_.latexparam.empty())
660 os << from_utf8(layout_.latexparam);
663 int i = InsetText::latex(buf, os, runparams);
664 if (!layout_.latexname.empty())
665 if (layout_.latextype == "command") {
667 } else if (layout_.latextype == "environment") {
668 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
675 void InsetCollapsable::validate(LaTeXFeatures & features) const
677 // Force inclusion of preamble snippet in layout file
678 features.addPreambleSnippet(layout_.preamble);
679 InsetText::validate(features);