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::cursorPos(BufferView const & bv,
355 CursorSlice const & sl, bool boundary, int & x, int & y) const
357 BOOST_ASSERT(geometry() != ButtonOnly);
359 InsetText::cursorPos(bv, sl, boundary, x, y);
360 Dimension const textdim = InsetText::dimension(bv);
362 switch (geometry()) {
364 x += dimensionCollapsed().wid;
367 y += dimensionCollapsed().des + textdim.asc;
382 Inset::EDITABLE InsetCollapsable::editable() const
384 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
388 bool InsetCollapsable::descendable() const
390 return geometry() != ButtonOnly;
394 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
396 return button_dim.contains(cmd.x, cmd.y);
400 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
403 pos_type const max_length = 15;
404 pos_type const p_siz = paragraphs().begin()->size();
405 pos_type const n = std::min(max_length, p_siz);
408 for (; i < n && j < p_siz; ++j) {
409 if (paragraphs().begin()->isInset(j))
411 label += paragraphs().begin()->getChar(j);
414 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
417 return label.empty() ? l : label;
421 void InsetCollapsable::edit(Cursor & cur, bool left)
423 //lyxerr << "InsetCollapsable: edit left/right" << endl;
425 InsetText::edit(cur, left);
429 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
431 //lyxerr << "InsetCollapsable: edit xy" << endl;
432 if (geometry() == ButtonOnly
433 || (button_dim.contains(x, y)
434 && geometry() != NoButton))
437 return InsetText::editXY(cur, x, y);
441 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
443 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
444 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
446 switch (cmd.action) {
447 case LFUN_MOUSE_PRESS:
448 if (cmd.button() == mouse_button::button1
450 && geometry() != NoButton) {
451 // reset selection if necessary (see bug 3060)
453 cur.bv().cursor().clearSelection();
459 if (geometry() == NoButton)
460 InsetText::doDispatch(cur, cmd);
461 else if (geometry() != ButtonOnly
463 InsetText::doDispatch(cur, cmd);
468 case LFUN_MOUSE_MOTION:
469 case LFUN_MOUSE_DOUBLE:
470 case LFUN_MOUSE_TRIPLE:
471 if (geometry() == NoButton)
472 InsetText::doDispatch(cur, cmd);
473 else if (geometry() != ButtonOnly
475 InsetText::doDispatch(cur, cmd);
480 case LFUN_MOUSE_RELEASE:
481 if (cmd.button() == mouse_button::button3) {
482 // There is no button to right click:
483 if (geometry() == Corners ||
484 geometry() == SubLabel ||
485 geometry() == NoButton) {
486 if (internalStatus() == Open)
487 setStatus(cur, Collapsed);
489 setStatus(cur, Open);
493 // configuration dialog
494 showInsetDialog(&cur.bv());
499 if (geometry() == NoButton) {
500 // The mouse click has to be within the inset!
501 InsetText::doDispatch(cur, cmd);
505 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
506 // if we are selecting, we do not want to
510 // Left button is clicked, the user asks to
511 // toggle the inset visual state.
513 cur.updateFlags(Update::Force | Update::FitCursor);
514 if (geometry() == ButtonOnly) {
515 setStatus(cur, Open);
519 setStatus(cur, Collapsed);
521 cur.bv().cursor() = cur;
525 // The mouse click is within the opened inset.
526 if (geometry() == TopButton
527 || geometry() == LeftButton)
528 InsetText::doDispatch(cur, cmd);
531 case LFUN_INSET_TOGGLE:
532 if (cmd.argument() == "open")
533 setStatus(cur, Open);
534 else if (cmd.argument() == "close")
535 setStatus(cur, Collapsed);
536 else if (cmd.argument() == "toggle" || cmd.argument().empty())
537 if (internalStatus() == Open) {
538 setStatus(cur, Collapsed);
539 if (geometry() == ButtonOnly)
540 cur.top().forwardPos();
542 setStatus(cur, Open);
543 else // if assign or anything else
549 InsetText::doDispatch(cur, cmd);
555 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
556 FuncStatus & flag) const
558 switch (cmd.action) {
560 case LFUN_INSET_TOGGLE:
561 if (cmd.argument() == "open" || cmd.argument() == "close" ||
562 cmd.argument() == "toggle")
569 return InsetText::getStatus(cur, cmd, flag);
574 void InsetCollapsable::setLabel(docstring const & l)
576 layout_.labelstring = l;
580 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
584 if (status_ == Collapsed)
585 cur.leaveInset(*this);
589 void InsetCollapsable::setLabelFont(Font const & font)
591 layout_.labelfont = font;
594 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
596 FloatList const & floats = bp.getTextClass().floats();
597 FloatList::const_iterator it = floats[type];
599 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
603 InsetCollapsable::Decoration InsetCollapsable::decoration() const
605 if (layout_.decoration == "classic")
607 if (layout_.decoration == "minimalistic")
609 if (layout_.decoration == "conglomerate")
611 if (name() == from_ascii("Flex"))
617 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
618 OutputParams const & runparams) const
620 // This implements the standard way of handling the LaTeX output of
621 // a collapsable inset, either a command or an environment. Standard
622 // collapsable insets should not redefine this, non-standard ones may
624 if (!layout_.latexname.empty()) {
625 if (layout_.latextype == "command") {
627 os << '\\' << from_utf8(layout_.latexname);
628 if (!layout_.latexparam.empty())
629 os << from_utf8(layout_.latexparam);
631 } else if (layout_.latextype == "environment") {
632 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
633 if (!layout_.latexparam.empty())
634 os << from_utf8(layout_.latexparam);
637 int i = InsetText::latex(buf, os, runparams);
638 if (!layout_.latexname.empty()) {
639 if (layout_.latextype == "command") {
641 } else if (layout_.latextype == "environment") {
642 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
650 void InsetCollapsable::validate(LaTeXFeatures & features) const
652 // Force inclusion of preamble snippet in layout file
653 features.require(layout_.name);
654 InsetText::validate(features);