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);
186 InsetText::metrics(mi, dim);
187 if (status() == Open) {
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);
200 if (status() == Open)
210 dim = dimensionCollapsed();
211 if (geometry() == TopButton
212 || geometry() == LeftButton) {
213 InsetText::metrics(mi, textdim_);
214 // This expression should not contain mi.base.texwidth
215 openinlined_ = !hasFixedWidth()
216 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
218 // FIXME: this is not ideal but we need to clear it
219 // out because the Row::changed() status is reset.
220 mi.base.bv->textMetrics(&text_).clear();
221 // Correct for button width, and re-fit
222 mi.base.textwidth -= dim.wid;
223 InsetText::metrics(mi, textdim_);
224 dim.wid += textdim_.wid;
225 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
226 dim.asc = textdim_.asc;
228 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
229 dim.wid = max(dim.wid, textdim_.wid);
231 dim.wid = max(dim.wid, mi.base.textwidth);
236 dim.asc += TEXT_TO_INSET_OFFSET;
237 dim.des += TEXT_TO_INSET_OFFSET;
238 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
239 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
240 bool const changed = dim_ != dim;
246 bool InsetCollapsable::setMouseHover(bool mouse_hover)
248 mouse_hover_ = mouse_hover;
253 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
255 autoOpen_ = pi.base.bv->cursor().isInside(this);
256 text_.background_color_ = backgroundColor();
257 const int xx = x + TEXT_TO_INSET_OFFSET;
259 // Draw button first -- top, left or only
260 Dimension dimc = dimensionCollapsed();
261 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
262 if (decoration() == Classic) {
263 button_dim.x1 = xx + 0;
264 button_dim.x2 = xx + dimc.width();
266 button_dim.y2 = top + dimc.height();
268 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 (status() == Open)
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 of the charstyle. Can be toggled.
323 if (status() == Open) {
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 cue 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 && decoration() != Minimalistic))
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 (decoration() == Minimalistic)
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 if (decoration() == Conglomerate) {
508 if (internalStatus() == Open)
509 setStatus(cur, Collapsed);
511 setStatus(cur, Open);
515 // configuration dialog
516 showInsetDialog(&cur.bv());
521 if (geometry() == NoButton) {
522 // The mouse click has to be within the inset!
523 InsetText::doDispatch(cur, cmd);
527 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
528 // if we are selecting, we do not want to
532 // Left button is clicked, the user asks to
533 // toggle the inset visual state.
535 cur.updateFlags(Update::Force | Update::FitCursor);
536 if (geometry() == ButtonOnly) {
537 setStatus(cur, Open);
541 setStatus(cur, Collapsed);
543 cur.bv().cursor() = cur;
547 // The mouse click is within the opened inset.
548 if (geometry() == TopButton
549 || geometry() == LeftButton)
550 InsetText::doDispatch(cur, cmd);
553 case LFUN_INSET_TOGGLE:
554 if (cmd.argument() == "open")
555 setStatus(cur, Open);
556 else if (cmd.argument() == "close")
557 setStatus(cur, Collapsed);
558 else if (cmd.argument() == "toggle" || cmd.argument().empty())
559 if (internalStatus() == Open) {
560 setStatus(cur, Collapsed);
561 if (geometry() == ButtonOnly)
562 cur.top().forwardPos();
564 setStatus(cur, Open);
565 else // if assign or anything else
571 InsetText::doDispatch(cur, cmd);
577 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
578 FuncStatus & flag) const
580 switch (cmd.action) {
582 case LFUN_INSET_TOGGLE:
583 if (cmd.argument() == "open" || cmd.argument() == "close" ||
584 cmd.argument() == "toggle")
591 return InsetText::getStatus(cur, cmd, flag);
596 void InsetCollapsable::setLabel(docstring const & l)
598 layout_.labelstring = l;
602 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
606 if (status_ == Collapsed)
607 cur.leaveInset(*this);
611 void InsetCollapsable::setLabelFont(Font const & font)
613 layout_.labelfont = font;
616 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
618 FloatList const & floats = bp.getTextClass().floats();
619 FloatList::const_iterator it = floats[type];
621 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
625 InsetCollapsable::Decoration InsetCollapsable::decoration() const
627 if (layout_.decoration == "classic")
629 if (layout_.decoration == "minimalistic")
631 if (layout_.decoration == "conglomerate")
633 if (name() == from_ascii("CharStyle"))
639 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
640 OutputParams const & runparams) const
642 // This implements the standard way of handling the LaTeX output of
643 // a collapsable inset, either a command or an environment. Standard
644 // collapsable insets should not redefine this, non-standard ones may
646 if (!layout_.latexname.empty()) {
647 if (layout_.latextype == "command") {
649 os << '\\' << from_utf8(layout_.latexname);
650 if (!layout_.latexparam.empty())
651 os << from_utf8(layout_.latexparam);
653 } else if (layout_.latextype == "environment") {
654 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
655 if (!layout_.latexparam.empty())
656 os << from_utf8(layout_.latexparam);
659 int i = InsetText::latex(buf, os, runparams);
660 if (!layout_.latexname.empty())
661 if (layout_.latextype == "command") {
663 } else if (layout_.latextype == "environment") {
664 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
671 void InsetCollapsable::validate(LaTeXFeatures & features) const
673 // Force inclusion of preamble snippet in layout file
674 features.addPreambleSnippet(layout_.preamble);
675 InsetText::validate(features);