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 || autoOpen_) {
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 (decoration()) {
182 InsetText::metrics(mi, dim);
185 InsetText::metrics(mi, dim);
186 if (status() == Open) {
187 // consider width of the inset label
188 Font font(layout_.labelfont);
189 font.realize(Font(Font::ALL_SANE));
195 docstring s = layout_.labelstring;
196 theFontMetrics(font).rectText(s, w, a, d);
197 dim.wid = max(dim.wid, w);
199 if (status() == Open)
207 dim = dimensionCollapsed();
208 if (geometry() == TopButton
209 || geometry() == LeftButton) {
210 InsetText::metrics(mi, textdim_);
211 // This expression should not contain mi.base.texwidth
212 openinlined_ = !hasFixedWidth()
213 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
215 // Correct for button width, and re-fit
216 mi.base.textwidth -= dim.wid;
217 InsetText::metrics(mi, textdim_);
218 dim.wid += textdim_.wid;
219 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
220 dim.asc = textdim_.asc;
222 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
223 dim.wid = max(dim.wid, textdim_.wid);
225 dim.wid = max(dim.wid, mi.base.textwidth);
230 dim.asc += TEXT_TO_INSET_OFFSET;
231 dim.des += TEXT_TO_INSET_OFFSET;
232 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
233 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
234 bool const changed = dim_ != dim;
240 bool InsetCollapsable::setMouseHover(bool mouse_hover)
242 mouse_hover_ = mouse_hover;
247 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
249 autoOpen_ = pi.base.bv->cursor().isInside(this);
250 text_.background_color_ = backgroundColor();
251 const int xx = x + TEXT_TO_INSET_OFFSET;
253 // Draw button first -- top, left or only
254 Dimension dimc = dimensionCollapsed();
255 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
256 if (decoration() == Classic) {
257 button_dim.x1 = xx + 0;
258 button_dim.x2 = xx + dimc.width();
260 button_dim.y2 = top + dimc.height();
262 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
266 switch (geometry()) {
268 textx = xx + dimc.width();
269 texty = top + textdim_.asc;
270 InsetText::draw(pi, textx, texty);
274 texty = top + dimc.height() + textdim_.asc;
275 InsetText::draw(pi, textx, texty);
281 texty = y + textdim_.asc;
282 InsetText::draw(pi, textx, texty);
287 texty = y + textdim_.asc;
288 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
289 InsetText::draw(pi, textx, texty);
290 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
292 int desc = InsetText::descent();
293 if (status() == Open)
298 pi.pain.line(x, y + desc - 4, x, y + desc,
299 layout_.labelfont.color());
300 if (internalStatus() == Open)
301 pi.pain.line(x, y + desc,
302 x + dim_.wid - 3, y + desc,
303 layout_.labelfont.color());
305 // Make status_ value visible:
306 pi.pain.line(x, y + desc,
308 layout_.labelfont.color());
309 pi.pain.line(x + dim_.wid - 7, y + desc,
310 x + dim_.wid -3, y + desc,
311 layout_.labelfont.color());
313 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
314 layout_.labelfont.color());
316 // the label of the charstyle. Can be toggled.
317 if (status() == Open) {
318 Font font(layout_.labelfont);
319 font.realize(Font(Font::ALL_SANE));
326 docstring s = layout_.labelstring;
327 theFontMetrics(font).rectText(s, w, a, d);
328 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
329 s, font, Color::none, Color::none);
332 // a visual cue when the cursor is inside the inset
333 Cursor & cur = pi.base.bv->cursor();
334 if (cur.isInside(this)) {
337 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
338 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
339 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
340 layout_.labelfont.color());
341 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
342 layout_.labelfont.color());
346 setPosCache(pi, x, y);
350 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
352 x += TEXT_TO_INSET_OFFSET;
353 switch (geometry()) {
355 x += dimensionCollapsed().wid;
356 InsetText::drawSelection(pi, x, y);
359 y += dimensionCollapsed().des + textdim_.asc;
360 InsetText::drawSelection(pi, x, y);
367 InsetText::drawSelection(pi, x, y);
373 void InsetCollapsable::cursorPos(BufferView const & bv,
374 CursorSlice const & sl, bool boundary, int & x, int & y) const
376 BOOST_ASSERT(geometry() != ButtonOnly);
378 InsetText::cursorPos(bv, sl, boundary, x, y);
380 switch (geometry()) {
382 x += dimensionCollapsed().wid;
385 y += dimensionCollapsed().height() - ascent()
386 + TEXT_TO_INSET_OFFSET + textdim_.asc;
397 x += TEXT_TO_INSET_OFFSET;
401 Inset::EDITABLE InsetCollapsable::editable() const
403 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
407 bool InsetCollapsable::descendable() const
409 return geometry() != ButtonOnly;
413 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
415 return button_dim.contains(cmd.x, cmd.y);
419 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
422 pos_type const max_length = 15;
423 pos_type const p_siz = paragraphs().begin()->size();
424 pos_type const n = std::min(max_length, p_siz);
427 for (; i < n && j < p_siz; ++j) {
428 if (paragraphs().begin()->isInset(j))
430 label += paragraphs().begin()->getChar(j);
433 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
436 return label.empty() ? l : label;
440 void InsetCollapsable::edit(Cursor & cur, bool left)
442 //lyxerr << "InsetCollapsable: edit left/right" << endl;
444 InsetText::edit(cur, left);
448 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
450 //lyxerr << "InsetCollapsable: edit xy" << endl;
451 if (geometry() == ButtonOnly
452 || (button_dim.contains(x, y)
453 && decoration() != Minimalistic))
456 return InsetText::editXY(cur, x, y);
460 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
462 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
463 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
465 switch (cmd.action) {
466 case LFUN_MOUSE_PRESS:
467 if (cmd.button() == mouse_button::button1
469 && decoration() != Minimalistic) {
470 // reset selection if necessary (see bug 3060)
472 cur.bv().cursor().clearSelection();
478 if (decoration() == Minimalistic)
479 InsetText::doDispatch(cur, cmd);
480 else if (geometry() != ButtonOnly
482 InsetText::doDispatch(cur, cmd);
487 case LFUN_MOUSE_MOTION:
488 case LFUN_MOUSE_DOUBLE:
489 case LFUN_MOUSE_TRIPLE:
490 if (decoration() == Minimalistic)
491 InsetText::doDispatch(cur, cmd);
492 else if (geometry() != ButtonOnly
494 InsetText::doDispatch(cur, cmd);
499 case LFUN_MOUSE_RELEASE:
500 if (cmd.button() == mouse_button::button3) {
501 if (decoration() == Conglomerate) {
502 if (internalStatus() == Open)
503 setStatus(cur, Collapsed);
505 setStatus(cur, Open);
509 // configuration dialog
510 showInsetDialog(&cur.bv());
515 if (decoration() == Minimalistic) {
516 // The mouse click has to be within the inset!
517 InsetText::doDispatch(cur, cmd);
521 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
522 // if we are selecting, we do not want to
526 // Left button is clicked, the user asks to
527 // toggle the inset visual state.
529 cur.updateFlags(Update::Force | Update::FitCursor);
530 if (geometry() == ButtonOnly) {
531 setStatus(cur, Open);
535 setStatus(cur, Collapsed);
537 cur.bv().cursor() = cur;
541 // The mouse click is within the opened inset.
542 if (geometry() == TopButton
543 || geometry() == LeftButton)
544 InsetText::doDispatch(cur, cmd);
547 case LFUN_INSET_TOGGLE:
548 if (cmd.argument() == "open")
549 setStatus(cur, Open);
550 else if (cmd.argument() == "close")
551 setStatus(cur, Collapsed);
552 else if (cmd.argument() == "toggle" || cmd.argument().empty())
553 if (internalStatus() == Open) {
554 setStatus(cur, Collapsed);
555 if (geometry() == ButtonOnly)
556 cur.top().forwardPos();
558 setStatus(cur, Open);
559 else // if assign or anything else
565 InsetText::doDispatch(cur, cmd);
571 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
572 FuncStatus & flag) const
574 switch (cmd.action) {
576 case LFUN_INSET_TOGGLE:
577 if (cmd.argument() == "open" || cmd.argument() == "close" ||
578 cmd.argument() == "toggle")
585 return InsetText::getStatus(cur, cmd, flag);
590 void InsetCollapsable::setLabel(docstring const & l)
592 layout_.labelstring = l;
596 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
600 if (status_ == Collapsed)
601 cur.leaveInset(*this);
605 void InsetCollapsable::setLabelFont(Font const & font)
607 layout_.labelfont = font;
610 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
612 FloatList const & floats = bp.getTextClass().floats();
613 FloatList::const_iterator it = floats[type];
615 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
619 InsetCollapsable::Decoration InsetCollapsable::decoration() const
621 if (layout_.decoration == "classic")
623 if (layout_.decoration == "minimalistic")
625 if (layout_.decoration == "conglomerate")
627 if (name() == from_ascii("CharStyle"))
633 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
634 OutputParams const & runparams) const
636 // This implements the standard way of handling the LaTeX output of
637 // a collapsable inset, either a command or an environment. Standard
638 // collapsable insets should not redefine this, non-standard ones may
640 if (!layout_.latexname.empty()) {
641 if (layout_.latextype == "command") {
643 os << '\\' << from_utf8(layout_.latexname);
644 if (!layout_.latexparam.empty())
645 os << from_utf8(layout_.latexparam);
647 } else if (layout_.latextype == "environment") {
648 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
649 if (!layout_.latexparam.empty())
650 os << from_utf8(layout_.latexparam);
653 int i = InsetText::latex(buf, os, runparams);
654 if (!layout_.latexname.empty())
655 if (layout_.latextype == "command") {
657 } else if (layout_.latextype == "environment") {
658 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
665 void InsetCollapsable::validate(LaTeXFeatures & features) const
667 // Force inclusion of preamble snippet in layout file
668 features.addPreambleSnippet(layout_.preamble);
669 InsetText::validate(features);