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);
224 bool const changed = dim_ != dim;
230 bool InsetCollapsable::setMouseHover(bool mouse_hover)
232 mouse_hover_ = mouse_hover;
237 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
239 autoOpen_ = pi.base.bv->cursor().isInside(this);
240 int const old_color = pi.background_color;
241 pi.background_color = backgroundColor();
243 // Draw button first -- top, left or only
244 Dimension dimc = dimensionCollapsed();
246 if (geometry() == TopButton ||
247 geometry() == LeftButton ||
248 geometry() == ButtonOnly) {
249 button_dim.x1 = x + 0;
250 button_dim.x2 = x + dimc.width();
251 button_dim.y1 = y - dimc.asc;
252 button_dim.y2 = y + dimc.des;
254 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
262 Dimension const textdim = InsetText::dimension(*pi.base.bv);
263 int const baseline = y;
265 switch (geometry()) {
267 textx = x + dimc.width();
269 InsetText::draw(pi, textx, texty);
273 texty = baseline + dimc.des + textdim.asc;
274 InsetText::draw(pi, textx, texty);
281 InsetText::draw(pi, textx, texty);
287 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
288 InsetText::draw(pi, textx, texty);
289 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
291 int desc = InsetText::descent();
292 if (geometry() == SubLabel)
297 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
298 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
299 pi.pain.line(xx1, y + desc - 4,
301 layout_.labelfont.color());
302 if (internalStatus() == Open)
303 pi.pain.line(xx1, y + desc,
305 layout_.labelfont.color());
307 // Make status_ value visible:
308 pi.pain.line(xx1, y + desc,
310 layout_.labelfont.color());
311 pi.pain.line(xx2 - 4, y + desc,
313 layout_.labelfont.color());
315 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
316 layout_.labelfont.color());
318 // the label below the text. Can be toggled.
319 if (geometry() == SubLabel) {
320 Font font(layout_.labelfont);
321 font.realize(Font(Font::ALL_SANE));
327 docstring s = layout_.labelstring;
328 theFontMetrics(font).rectText(s, w, a, d);
329 pi.pain.rectText(x + (textdim.wid - w) / 2, y + desc + a,
330 s, font, Color::none, Color::none);
333 // a visual cue when the cursor is inside the inset
334 Cursor & cur = pi.base.bv->cursor();
335 if (cur.isInside(this)) {
338 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
339 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
340 pi.pain.line(xx2, y + 4, xx2, y,
341 layout_.labelfont.color());
342 pi.pain.line(xx2 - 4, y, xx2, y,
343 layout_.labelfont.color());
347 pi.background_color = old_color;
351 void InsetCollapsable::cursorPos(BufferView const & bv,
352 CursorSlice const & sl, bool boundary, int & x, int & y) const
354 if (geometry() == ButtonOnly)
356 BOOST_ASSERT(geometry() != ButtonOnly);
358 InsetText::cursorPos(bv, sl, boundary, x, y);
359 Dimension const textdim = InsetText::dimension(bv);
361 switch (geometry()) {
363 x += dimensionCollapsed().wid;
366 y += dimensionCollapsed().des + textdim.asc;
381 Inset::EDITABLE InsetCollapsable::editable() const
383 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
387 bool InsetCollapsable::descendable() const
389 return geometry() != ButtonOnly;
393 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
395 return button_dim.contains(cmd.x, cmd.y);
399 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
402 pos_type const max_length = 15;
403 pos_type const p_siz = paragraphs().begin()->size();
404 pos_type const n = std::min(max_length, p_siz);
407 for (; i < n && j < p_siz; ++j) {
408 if (paragraphs().begin()->isInset(j))
410 label += paragraphs().begin()->getChar(j);
413 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
416 return label.empty() ? l : label;
420 void InsetCollapsable::edit(Cursor & cur, bool left)
422 //lyxerr << "InsetCollapsable: edit left/right" << endl;
424 InsetText::edit(cur, left);
428 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
430 //lyxerr << "InsetCollapsable: edit xy" << endl;
431 if (geometry() == ButtonOnly
432 || (button_dim.contains(x, y)
433 && geometry() != NoButton))
436 return InsetText::editXY(cur, x, y);
440 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
442 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
443 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
445 switch (cmd.action) {
446 case LFUN_MOUSE_PRESS:
447 if (cmd.button() == mouse_button::button1
449 && geometry() != NoButton) {
450 // reset selection if necessary (see bug 3060)
452 cur.bv().cursor().clearSelection();
458 if (geometry() == NoButton)
459 InsetText::doDispatch(cur, cmd);
460 else if (geometry() != ButtonOnly
462 InsetText::doDispatch(cur, cmd);
467 case LFUN_MOUSE_MOTION:
468 case LFUN_MOUSE_DOUBLE:
469 case LFUN_MOUSE_TRIPLE:
470 if (geometry() == NoButton)
471 InsetText::doDispatch(cur, cmd);
472 else if (geometry() != ButtonOnly
474 InsetText::doDispatch(cur, cmd);
479 case LFUN_MOUSE_RELEASE:
480 if (cmd.button() == mouse_button::button3) {
481 // There is no button to right click:
482 if (geometry() == Corners ||
483 geometry() == SubLabel ||
484 geometry() == NoButton) {
485 if (internalStatus() == Open)
486 setStatus(cur, Collapsed);
488 setStatus(cur, Open);
492 // configuration dialog
493 showInsetDialog(&cur.bv());
498 if (geometry() == NoButton) {
499 // The mouse click has to be within the inset!
500 InsetText::doDispatch(cur, cmd);
504 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
505 // if we are selecting, we do not want to
509 // Left button is clicked, the user asks to
510 // toggle the inset visual state.
512 cur.updateFlags(Update::Force | Update::FitCursor);
513 if (geometry() == ButtonOnly) {
514 setStatus(cur, Open);
518 setStatus(cur, Collapsed);
520 cur.bv().cursor() = cur;
524 // The mouse click is within the opened inset.
525 if (geometry() == TopButton
526 || geometry() == LeftButton)
527 InsetText::doDispatch(cur, cmd);
530 case LFUN_INSET_TOGGLE:
531 if (cmd.argument() == "open")
532 setStatus(cur, Open);
533 else if (cmd.argument() == "close")
534 setStatus(cur, Collapsed);
535 else if (cmd.argument() == "toggle" || cmd.argument().empty())
536 if (internalStatus() == Open) {
537 setStatus(cur, Collapsed);
538 if (geometry() == ButtonOnly)
539 cur.top().forwardPos();
541 setStatus(cur, Open);
542 else // if assign or anything else
548 InsetText::doDispatch(cur, cmd);
554 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
555 FuncStatus & flag) const
557 switch (cmd.action) {
559 case LFUN_INSET_TOGGLE:
560 if (cmd.argument() == "open" || cmd.argument() == "close" ||
561 cmd.argument() == "toggle")
568 return InsetText::getStatus(cur, cmd, flag);
573 void InsetCollapsable::setLabel(docstring const & l)
575 layout_.labelstring = l;
579 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
583 if (status_ == Collapsed)
584 cur.leaveInset(*this);
588 void InsetCollapsable::setLabelFont(Font const & font)
590 layout_.labelfont = font;
593 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
595 FloatList const & floats = bp.getTextClass().floats();
596 FloatList::const_iterator it = floats[type];
598 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
602 InsetCollapsable::Decoration InsetCollapsable::decoration() const
604 if (layout_.decoration == "classic")
606 if (layout_.decoration == "minimalistic")
608 if (layout_.decoration == "conglomerate")
610 if (name() == from_ascii("Flex"))
616 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
617 OutputParams const & runparams) const
619 // This implements the standard way of handling the LaTeX output of
620 // a collapsable inset, either a command or an environment. Standard
621 // collapsable insets should not redefine this, non-standard ones may
623 if (!layout_.latexname.empty()) {
624 if (layout_.latextype == "command") {
626 os << '\\' << from_utf8(layout_.latexname);
627 if (!layout_.latexparam.empty())
628 os << from_utf8(layout_.latexparam);
630 } else if (layout_.latextype == "environment") {
631 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
632 if (!layout_.latexparam.empty())
633 os << from_utf8(layout_.latexparam);
636 int i = InsetText::latex(buf, os, runparams);
637 if (!layout_.latexname.empty()) {
638 if (layout_.latextype == "command") {
640 } else if (layout_.latextype == "environment") {
641 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
649 void InsetCollapsable::validate(LaTeXFeatures & features) const
651 // Force inclusion of preamble snippet in layout file
652 features.require(layout_.name);
653 InsetText::validate(features);