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 // 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_);
267 switch (geometry()) {
269 textx = xx + dimc.width();
270 texty = top + textdim_.asc;
271 InsetText::draw(pi, textx, texty);
275 texty = top + dimc.height() + textdim_.asc;
276 InsetText::draw(pi, textx, texty);
282 texty = y + textdim_.asc;
283 InsetText::draw(pi, textx, texty);
288 texty = y + textdim_.asc;
289 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
290 InsetText::draw(pi, textx, texty);
291 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
293 int desc = InsetText::descent();
294 if (status() == Open)
299 pi.pain.line(x, y + desc - 4, x, y + desc,
300 layout_.labelfont.color());
301 if (internalStatus() == Open)
302 pi.pain.line(x, y + desc,
303 x + dim_.wid - 3, y + desc,
304 layout_.labelfont.color());
306 // Make status_ value visible:
307 pi.pain.line(x, y + desc,
309 layout_.labelfont.color());
310 pi.pain.line(x + dim_.wid - 7, y + desc,
311 x + dim_.wid -3, y + desc,
312 layout_.labelfont.color());
314 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
315 layout_.labelfont.color());
317 // the label of the charstyle. Can be toggled.
318 if (status() == Open) {
319 Font font(layout_.labelfont);
320 font.realize(Font(Font::ALL_SANE));
327 docstring s = layout_.labelstring;
328 theFontMetrics(font).rectText(s, w, a, d);
329 pi.pain.rectText(x + (dim_.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(x, y + 4, x, y, layout_.labelfont.color());
339 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
340 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
341 layout_.labelfont.color());
342 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
343 layout_.labelfont.color());
347 setPosCache(pi, x, y);
351 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
353 x += TEXT_TO_INSET_OFFSET;
354 switch (geometry()) {
356 x += dimensionCollapsed().wid;
357 InsetText::drawSelection(pi, x, y);
360 y += dimensionCollapsed().des + textdim_.asc;
361 InsetText::drawSelection(pi, x, y);
368 InsetText::drawSelection(pi, x, y);
374 void InsetCollapsable::cursorPos(BufferView const & bv,
375 CursorSlice const & sl, bool boundary, int & x, int & y) const
377 BOOST_ASSERT(geometry() != ButtonOnly);
379 InsetText::cursorPos(bv, sl, boundary, x, y);
381 switch (geometry()) {
383 x += dimensionCollapsed().wid;
386 y += dimensionCollapsed().height() - ascent()
387 + TEXT_TO_INSET_OFFSET + textdim_.asc;
398 x += TEXT_TO_INSET_OFFSET;
402 Inset::EDITABLE InsetCollapsable::editable() const
404 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
408 bool InsetCollapsable::descendable() const
410 return geometry() != ButtonOnly;
414 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
416 return button_dim.contains(cmd.x, cmd.y);
420 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
423 pos_type const max_length = 15;
424 pos_type const p_siz = paragraphs().begin()->size();
425 pos_type const n = std::min(max_length, p_siz);
428 for (; i < n && j < p_siz; ++j) {
429 if (paragraphs().begin()->isInset(j))
431 label += paragraphs().begin()->getChar(j);
434 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
437 return label.empty() ? l : label;
441 void InsetCollapsable::edit(Cursor & cur, bool left)
443 //lyxerr << "InsetCollapsable: edit left/right" << endl;
445 InsetText::edit(cur, left);
449 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
451 //lyxerr << "InsetCollapsable: edit xy" << endl;
452 if (geometry() == ButtonOnly
453 || (button_dim.contains(x, y)
454 && geometry() != NoButton))
457 return InsetText::editXY(cur, x, y);
461 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
463 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
464 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
466 switch (cmd.action) {
467 case LFUN_MOUSE_PRESS:
468 if (cmd.button() == mouse_button::button1
470 && geometry() != NoButton) {
471 // reset selection if necessary (see bug 3060)
473 cur.bv().cursor().clearSelection();
479 if (geometry() == NoButton)
480 InsetText::doDispatch(cur, cmd);
481 else if (geometry() != ButtonOnly
483 InsetText::doDispatch(cur, cmd);
488 case LFUN_MOUSE_MOTION:
489 case LFUN_MOUSE_DOUBLE:
490 case LFUN_MOUSE_TRIPLE:
491 if (geometry() == NoButton)
492 InsetText::doDispatch(cur, cmd);
493 else if (geometry() != ButtonOnly
495 InsetText::doDispatch(cur, cmd);
500 case LFUN_MOUSE_RELEASE:
501 if (cmd.button() == mouse_button::button3) {
502 // There is no button to right click:
503 if (geometry() == Corners ||
504 geometry() == SubLabel ||
505 geometry() == NoButton) {
506 if (internalStatus() == Open)
507 setStatus(cur, Collapsed);
509 setStatus(cur, Open);
513 // configuration dialog
514 showInsetDialog(&cur.bv());
519 if (geometry() == NoButton) {
520 // The mouse click has to be within the inset!
521 InsetText::doDispatch(cur, cmd);
525 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
526 // if we are selecting, we do not want to
530 // Left button is clicked, the user asks to
531 // toggle the inset visual state.
533 cur.updateFlags(Update::Force | Update::FitCursor);
534 if (geometry() == ButtonOnly) {
535 setStatus(cur, Open);
539 setStatus(cur, Collapsed);
541 cur.bv().cursor() = cur;
545 // The mouse click is within the opened inset.
546 if (geometry() == TopButton
547 || geometry() == LeftButton)
548 InsetText::doDispatch(cur, cmd);
551 case LFUN_INSET_TOGGLE:
552 if (cmd.argument() == "open")
553 setStatus(cur, Open);
554 else if (cmd.argument() == "close")
555 setStatus(cur, Collapsed);
556 else if (cmd.argument() == "toggle" || cmd.argument().empty())
557 if (internalStatus() == Open) {
558 setStatus(cur, Collapsed);
559 if (geometry() == ButtonOnly)
560 cur.top().forwardPos();
562 setStatus(cur, Open);
563 else // if assign or anything else
569 InsetText::doDispatch(cur, cmd);
575 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
576 FuncStatus & flag) const
578 switch (cmd.action) {
580 case LFUN_INSET_TOGGLE:
581 if (cmd.argument() == "open" || cmd.argument() == "close" ||
582 cmd.argument() == "toggle")
589 return InsetText::getStatus(cur, cmd, flag);
594 void InsetCollapsable::setLabel(docstring const & l)
596 layout_.labelstring = l;
600 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
604 if (status_ == Collapsed)
605 cur.leaveInset(*this);
609 void InsetCollapsable::setLabelFont(Font const & font)
611 layout_.labelfont = font;
614 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
616 FloatList const & floats = bp.getTextClass().floats();
617 FloatList::const_iterator it = floats[type];
619 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
623 InsetCollapsable::Decoration InsetCollapsable::decoration() const
625 if (layout_.decoration == "classic")
627 if (layout_.decoration == "minimalistic")
629 if (layout_.decoration == "conglomerate")
631 if (name() == from_ascii("CharStyle"))
637 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
638 OutputParams const & runparams) const
640 // This implements the standard way of handling the LaTeX output of
641 // a collapsable inset, either a command or an environment. Standard
642 // collapsable insets should not redefine this, non-standard ones may
644 if (!layout_.latexname.empty()) {
645 if (layout_.latextype == "command") {
647 os << '\\' << from_utf8(layout_.latexname);
648 if (!layout_.latexparam.empty())
649 os << from_utf8(layout_.latexparam);
651 } else if (layout_.latextype == "environment") {
652 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
653 if (!layout_.latexparam.empty())
654 os << from_utf8(layout_.latexparam);
657 int i = InsetText::latex(buf, os, runparams);
658 if (!layout_.latexname.empty())
659 if (layout_.latextype == "command") {
661 } else if (layout_.latextype == "environment") {
662 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
669 void InsetCollapsable::validate(LaTeXFeatures & features) const
671 // Force inclusion of preamble snippet in layout file
672 features.addPreambleSnippet(layout_.preamble);
673 InsetText::validate(features);