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) {
62 return status() == Open ? NoButton : ButtonOnly ;
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 // the sole purpose of this copy constructor
103 void InsetCollapsable::setLayout(BufferParams const & bp)
105 layout_ = getLayout(bp);
109 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
121 text_.write(buf, os);
125 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
127 bool token_found = false;
130 string const token = lex.getString();
131 if (token == "status") {
133 string const tmp_token = lex.getString();
135 if (tmp_token == "collapsed") {
138 } else if (tmp_token == "open") {
142 lyxerr << "InsetCollapsable::read: Missing status!"
144 // Take countermeasures
145 lex.pushToken(token);
148 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
150 // take countermeasures
151 lex.pushToken(token);
154 InsetText::read(buf, lex);
157 status_ = isOpen() ? Open : Collapsed;
163 Dimension InsetCollapsable::dimensionCollapsed() const
166 theFontMetrics(layout_.labelfont).buttonText(
167 layout_.labelstring, dim.wid, dim.asc, dim.des);
172 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
176 autoOpen_ = mi.base.bv->cursor().isInside(this);
177 mi.base.textwidth -= int(1.5 * TEXT_TO_INSET_OFFSET);
179 switch (geometry()) {
181 InsetText::metrics(mi, dim);
184 InsetText::metrics(mi, dim);
189 InsetText::metrics(mi, dim);
190 // consider width of the inset label
191 Font font(layout_.labelfont);
192 font.realize(Font(Font::ALL_SANE));
198 docstring s = layout_.labelstring;
199 theFontMetrics(font).rectText(s, w, a, d);
200 dim.wid = max(dim.wid, w);
207 dim = dimensionCollapsed();
208 if (geometry() == TopButton
209 || geometry() == LeftButton) {
211 InsetText::metrics(mi, textdim);
212 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
214 // Correct for button width.
215 dim.wid += textdim.wid;
216 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
217 dim.asc = textdim.asc;
219 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
220 dim.wid = max(dim.wid, textdim.wid);
222 dim.wid = max(dim.wid, mi.base.textwidth);
227 dim.asc += TEXT_TO_INSET_OFFSET;
228 dim.des += TEXT_TO_INSET_OFFSET;
229 dim.wid += int(1.5 * TEXT_TO_INSET_OFFSET);
230 mi.base.textwidth += int(1.5 * TEXT_TO_INSET_OFFSET);
231 bool const changed = dim_ != dim;
237 bool InsetCollapsable::setMouseHover(bool mouse_hover)
239 mouse_hover_ = mouse_hover;
244 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
246 autoOpen_ = pi.base.bv->cursor().isInside(this);
247 int const old_color = pi.background_color;
248 pi.background_color = backgroundColor();
249 int const xx = x + TEXT_TO_INSET_OFFSET;
251 // Draw button first -- top, left or only
252 Dimension dimc = dimensionCollapsed();
254 if (geometry() == TopButton ||
255 geometry() == LeftButton ||
256 geometry() == ButtonOnly) {
257 button_dim.x1 = xx + 0;
258 button_dim.x2 = xx + dimc.width();
259 button_dim.y1 = y - dimc.asc;
260 button_dim.y2 = y + dimc.des;
262 pi.pain.buttonText(xx, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
270 TextMetrics const & tm = pi.base.bv->textMetrics(&text_);
271 int const baseline = y - tm.ascent();
273 switch (geometry()) {
275 textx = xx + dimc.width();
277 InsetText::draw(pi, textx, texty);
281 texty = baseline + dimc.height();
282 InsetText::draw(pi, textx, texty);
289 InsetText::draw(pi, textx, texty);
295 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
296 InsetText::draw(pi, textx, texty);
297 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
299 int desc = InsetText::descent();
300 if (geometry() == SubLabel)
305 const int xx1 = xx + border_ - 1;
306 const int xx2 = x + dim_.wid - border_
307 - TEXT_TO_INSET_OFFSET + 1;
308 pi.pain.line(xx1, y + desc - 4,
310 layout_.labelfont.color());
311 if (internalStatus() == Open)
312 pi.pain.line(xx1, y + desc,
314 layout_.labelfont.color());
316 // Make status_ value visible:
317 pi.pain.line(xx1, y + desc,
319 layout_.labelfont.color());
320 pi.pain.line(xx2 - 4, y + desc,
322 layout_.labelfont.color());
324 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
325 layout_.labelfont.color());
327 // the label below the text. Can be toggled.
328 if (geometry() == SubLabel) {
329 Font font(layout_.labelfont);
330 font.realize(Font(Font::ALL_SANE));
337 docstring s = layout_.labelstring;
338 theFontMetrics(font).rectText(s, w, a, d);
339 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
340 s, font, Color::none, Color::none);
343 // a visual clue when the cursor is inside the inset
344 Cursor & cur = pi.base.bv->cursor();
345 if (cur.isInside(this)) {
348 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
349 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
350 pi.pain.line(xx2, y + 4, x + dim_.wid - 3, y,
351 layout_.labelfont.color());
352 pi.pain.line(xx2 - 4, y, xx2, y,
353 layout_.labelfont.color());
357 setPosCache(pi, x, y);
358 pi.background_color = old_color;
362 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
364 x += TEXT_TO_INSET_OFFSET;
365 switch (geometry()) {
367 x += dimensionCollapsed().wid;
368 InsetText::drawSelection(pi, x, y);
371 y += dimensionCollapsed().des;
372 InsetText::drawSelection(pi, x, y);
379 InsetText::drawSelection(pi, x, y);
385 void InsetCollapsable::cursorPos(BufferView const & bv,
386 CursorSlice const & sl, bool boundary, int & x, int & y) const
388 BOOST_ASSERT(geometry() != ButtonOnly);
390 InsetText::cursorPos(bv, sl, boundary, x, y);
392 switch (geometry()) {
394 x += dimensionCollapsed().wid;
397 TextMetrics const & tm = bv.textMetrics(&text_);
398 y += dimensionCollapsed().height() - ascent()
399 + TEXT_TO_INSET_OFFSET + tm.ascent();
411 x += TEXT_TO_INSET_OFFSET;
415 Inset::EDITABLE InsetCollapsable::editable() const
417 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
421 bool InsetCollapsable::descendable() const
423 return geometry() != ButtonOnly;
427 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
429 return button_dim.contains(cmd.x, cmd.y);
433 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
436 pos_type const max_length = 15;
437 pos_type const p_siz = paragraphs().begin()->size();
438 pos_type const n = std::min(max_length, p_siz);
441 for (; i < n && j < p_siz; ++j) {
442 if (paragraphs().begin()->isInset(j))
444 label += paragraphs().begin()->getChar(j);
447 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
450 return label.empty() ? l : label;
454 void InsetCollapsable::edit(Cursor & cur, bool left)
456 //lyxerr << "InsetCollapsable: edit left/right" << endl;
458 InsetText::edit(cur, left);
462 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
464 //lyxerr << "InsetCollapsable: edit xy" << endl;
465 if (geometry() == ButtonOnly
466 || (button_dim.contains(x, y)
467 && geometry() != NoButton))
470 return InsetText::editXY(cur, x, y);
474 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
476 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
477 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
479 switch (cmd.action) {
480 case LFUN_MOUSE_PRESS:
481 if (cmd.button() == mouse_button::button1
483 && geometry() != NoButton) {
484 // reset selection if necessary (see bug 3060)
486 cur.bv().cursor().clearSelection();
492 if (geometry() == NoButton)
493 InsetText::doDispatch(cur, cmd);
494 else if (geometry() != ButtonOnly
496 InsetText::doDispatch(cur, cmd);
501 case LFUN_MOUSE_MOTION:
502 case LFUN_MOUSE_DOUBLE:
503 case LFUN_MOUSE_TRIPLE:
504 if (geometry() == NoButton)
505 InsetText::doDispatch(cur, cmd);
506 else if (geometry() != ButtonOnly
508 InsetText::doDispatch(cur, cmd);
513 case LFUN_MOUSE_RELEASE:
514 if (cmd.button() == mouse_button::button3) {
515 // There is no button to right click:
516 if (geometry() == Corners ||
517 geometry() == SubLabel ||
518 geometry() == NoButton) {
519 if (internalStatus() == Open)
520 setStatus(cur, Collapsed);
522 setStatus(cur, Open);
526 // configuration dialog
527 showInsetDialog(&cur.bv());
532 if (geometry() == NoButton) {
533 // The mouse click has to be within the inset!
534 InsetText::doDispatch(cur, cmd);
538 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
539 // if we are selecting, we do not want to
543 // Left button is clicked, the user asks to
544 // toggle the inset visual state.
546 cur.updateFlags(Update::Force | Update::FitCursor);
547 if (geometry() == ButtonOnly) {
548 setStatus(cur, Open);
552 setStatus(cur, Collapsed);
554 cur.bv().cursor() = cur;
558 // The mouse click is within the opened inset.
559 if (geometry() == TopButton
560 || geometry() == LeftButton)
561 InsetText::doDispatch(cur, cmd);
564 case LFUN_INSET_TOGGLE:
565 if (cmd.argument() == "open")
566 setStatus(cur, Open);
567 else if (cmd.argument() == "close")
568 setStatus(cur, Collapsed);
569 else if (cmd.argument() == "toggle" || cmd.argument().empty())
570 if (internalStatus() == Open) {
571 setStatus(cur, Collapsed);
572 if (geometry() == ButtonOnly)
573 cur.top().forwardPos();
575 setStatus(cur, Open);
576 else // if assign or anything else
582 InsetText::doDispatch(cur, cmd);
588 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
589 FuncStatus & flag) const
591 switch (cmd.action) {
593 case LFUN_INSET_TOGGLE:
594 if (cmd.argument() == "open" || cmd.argument() == "close" ||
595 cmd.argument() == "toggle")
602 return InsetText::getStatus(cur, cmd, flag);
607 void InsetCollapsable::setLabel(docstring const & l)
609 layout_.labelstring = l;
613 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
617 if (status_ == Collapsed)
618 cur.leaveInset(*this);
622 void InsetCollapsable::setLabelFont(Font const & font)
624 layout_.labelfont = font;
627 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
629 FloatList const & floats = bp.getTextClass().floats();
630 FloatList::const_iterator it = floats[type];
632 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
636 InsetCollapsable::Decoration InsetCollapsable::decoration() const
638 if (layout_.decoration == "classic")
640 if (layout_.decoration == "minimalistic")
642 if (layout_.decoration == "conglomerate")
644 if (name() == from_ascii("Flex"))
650 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
651 OutputParams const & runparams) const
653 // This implements the standard way of handling the LaTeX output of
654 // a collapsable inset, either a command or an environment. Standard
655 // collapsable insets should not redefine this, non-standard ones may
657 if (!layout_.latexname.empty()) {
658 if (layout_.latextype == "command") {
660 os << '\\' << from_utf8(layout_.latexname);
661 if (!layout_.latexparam.empty())
662 os << from_utf8(layout_.latexparam);
664 } else if (layout_.latextype == "environment") {
665 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
666 if (!layout_.latexparam.empty())
667 os << from_utf8(layout_.latexparam);
670 int i = InsetText::latex(buf, os, runparams);
671 if (!layout_.latexname.empty())
672 if (layout_.latextype == "command") {
674 } else if (layout_.latextype == "environment") {
675 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
682 void InsetCollapsable::validate(LaTeXFeatures & features) const
684 // Force inclusion of preamble snippet in layout file
685 features.require(layout_.name);
686 InsetText::validate(features);