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);
220 dim.wid = max(dim.wid, mi.base.textwidth);
226 bool const changed = dim_ != dim;
232 bool InsetCollapsable::setMouseHover(bool mouse_hover)
234 mouse_hover_ = mouse_hover;
239 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
241 autoOpen_ = pi.base.bv->cursor().isInside(this);
242 int const old_color = pi.background_color;
243 pi.background_color = backgroundColor();
245 // Draw button first -- top, left or only
246 Dimension dimc = dimensionCollapsed();
248 if (geometry() == TopButton ||
249 geometry() == LeftButton ||
250 geometry() == ButtonOnly) {
251 button_dim.x1 = x + 0;
252 button_dim.x2 = x + dimc.width();
253 button_dim.y1 = y - dimc.asc;
254 button_dim.y2 = y + dimc.des;
256 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
264 Dimension const textdim = InsetText::dimension(*pi.base.bv);
265 int const baseline = y;
267 switch (geometry()) {
269 textx = x + dimc.width();
271 InsetText::draw(pi, textx, texty);
275 texty = baseline + dimc.des + textdim.asc;
276 InsetText::draw(pi, textx, texty);
283 InsetText::draw(pi, textx, texty);
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 (geometry() == SubLabel)
299 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
300 const int xx2 = x + dim_.wid - 2 * TEXT_TO_INSET_OFFSET - 1;
301 pi.pain.line(xx1, y + desc - 4,
303 layout_.labelfont.color());
304 if (internalStatus() == Open)
305 pi.pain.line(xx1, y + desc,
307 layout_.labelfont.color());
309 // Make status_ value visible:
310 pi.pain.line(xx1, y + desc,
312 layout_.labelfont.color());
313 pi.pain.line(xx2 - 4, y + desc,
315 layout_.labelfont.color());
317 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
318 layout_.labelfont.color());
320 // the label below the text. Can be toggled.
321 if (geometry() == SubLabel) {
322 Font font(layout_.labelfont);
323 font.realize(Font(Font::ALL_SANE));
330 docstring s = layout_.labelstring;
331 theFontMetrics(font).rectText(s, w, a, d);
332 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
333 s, font, Color::none, Color::none);
336 // a visual clue when the cursor is inside the inset
337 Cursor & cur = pi.base.bv->cursor();
338 if (cur.isInside(this)) {
341 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
342 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
343 pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
344 layout_.labelfont.color());
345 pi.pain.line(xx2 - 4, y, xx2, y,
346 layout_.labelfont.color());
350 pi.background_color = old_color;
354 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
356 switch (geometry()) {
358 x += dimensionCollapsed().wid;
359 InsetText::drawSelection(pi, x, y);
362 y += dimensionCollapsed().des;
363 InsetText::drawSelection(pi, x, y);
370 InsetText::drawSelection(pi, x, y);
376 void InsetCollapsable::cursorPos(BufferView const & bv,
377 CursorSlice const & sl, bool boundary, int & x, int & y) const
379 BOOST_ASSERT(geometry() != ButtonOnly);
381 InsetText::cursorPos(bv, sl, boundary, x, y);
382 Dimension const textdim = InsetText::dimension(bv);
384 switch (geometry()) {
386 x += dimensionCollapsed().wid;
389 y += dimensionCollapsed().des + textdim.asc;
404 Inset::EDITABLE InsetCollapsable::editable() const
406 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
410 bool InsetCollapsable::descendable() const
412 return geometry() != ButtonOnly;
416 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
418 return button_dim.contains(cmd.x, cmd.y);
422 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
425 pos_type const max_length = 15;
426 pos_type const p_siz = paragraphs().begin()->size();
427 pos_type const n = std::min(max_length, p_siz);
430 for (; i < n && j < p_siz; ++j) {
431 if (paragraphs().begin()->isInset(j))
433 label += paragraphs().begin()->getChar(j);
436 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
439 return label.empty() ? l : label;
443 void InsetCollapsable::edit(Cursor & cur, bool left)
445 //lyxerr << "InsetCollapsable: edit left/right" << endl;
447 InsetText::edit(cur, left);
451 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
453 //lyxerr << "InsetCollapsable: edit xy" << endl;
454 if (geometry() == ButtonOnly
455 || (button_dim.contains(x, y)
456 && geometry() != NoButton))
459 return InsetText::editXY(cur, x, y);
463 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
465 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
466 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
468 switch (cmd.action) {
469 case LFUN_MOUSE_PRESS:
470 if (cmd.button() == mouse_button::button1
472 && geometry() != NoButton) {
473 // reset selection if necessary (see bug 3060)
475 cur.bv().cursor().clearSelection();
481 if (geometry() == NoButton)
482 InsetText::doDispatch(cur, cmd);
483 else if (geometry() != ButtonOnly
485 InsetText::doDispatch(cur, cmd);
490 case LFUN_MOUSE_MOTION:
491 case LFUN_MOUSE_DOUBLE:
492 case LFUN_MOUSE_TRIPLE:
493 if (geometry() == NoButton)
494 InsetText::doDispatch(cur, cmd);
495 else if (geometry() != ButtonOnly
497 InsetText::doDispatch(cur, cmd);
502 case LFUN_MOUSE_RELEASE:
503 if (cmd.button() == mouse_button::button3) {
504 // There is no button to right click:
505 if (geometry() == Corners ||
506 geometry() == SubLabel ||
507 geometry() == NoButton) {
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("Flex"))
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";
672 void InsetCollapsable::validate(LaTeXFeatures & features) const
674 // Force inclusion of preamble snippet in layout file
675 features.require(layout_.name);
676 InsetText::validate(features);