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"
38 using graphics::PreviewLoader;
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
49 return autoOpen_ ? Open : status_;
53 InsetCollapsable::Geometry InsetCollapsable::geometry() const
55 switch (decoration()) {
57 if (status_ == Open || autoOpen_) {
69 return status_ == Open ? SubLabel : Corners;
72 // dummy return value to shut down a warning,
78 InsetCollapsable::InsetCollapsable
79 (BufferParams const & bp, CollapseStatus status)
80 : InsetText(bp), status_(status),
81 openinlined_(false), autoOpen_(false), mouse_hover_(false)
83 setAutoBreakRows(true);
85 setFrameColor(Color::collapsableframe);
87 // Fallback for lacking inset layout item
88 layout_.bgcolor = Color::background;
92 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
94 button_dim(rhs.button_dim),
96 topbaseline(rhs.topbaseline),
99 openinlined_(rhs.openinlined_),
100 autoOpen_(rhs.autoOpen_),
101 textdim_(rhs.textdim_),
102 // the sole purpose of this copy constructor
108 void InsetCollapsable::setLayout(BufferParams const & bp)
110 layout_ = getLayout(bp);
114 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
126 text_.write(buf, os);
130 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
132 bool token_found = false;
135 string const token = lex.getString();
136 if (token == "status") {
138 string const tmp_token = lex.getString();
140 if (tmp_token == "collapsed") {
143 } else if (tmp_token == "open") {
147 lyxerr << "InsetCollapsable::read: Missing status!"
149 // Take countermeasures
150 lex.pushToken(token);
153 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
155 // take countermeasures
156 lex.pushToken(token);
159 InsetText::read(buf, lex);
162 status_ = isOpen() ? Open : Collapsed;
168 Dimension InsetCollapsable::dimensionCollapsed() const
171 theFontMetrics(layout_.labelfont).buttonText(
172 layout_.labelstring, dim.wid, dim.asc, dim.des);
177 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
179 autoOpen_ = mi.base.bv->cursor().isInside(this);
180 mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
182 switch (decoration()) {
184 InsetText::metrics(mi, dim);
187 InsetText::metrics(mi, dim);
188 if (status() == Open) {
189 // consider width of the inset label
190 Font font(layout_.labelfont);
191 font.realize(Font(Font::ALL_SANE));
197 docstring s = layout_.labelstring;
198 theFontMetrics(font).rectText(s, w, a, d);
199 dim.wid = max(dim.wid, w);
201 if (status() == Open)
209 dim = dimensionCollapsed();
210 if (geometry() == TopButton
211 || geometry() == LeftButton) {
212 InsetText::metrics(mi, textdim_);
213 // This expression should not contain mi.base.texwidth
214 openinlined_ = !hasFixedWidth()
215 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
217 // Correct for button width, and re-fit
218 mi.base.textwidth -= dim.wid;
219 InsetText::metrics(mi, textdim_);
220 dim.wid += textdim_.wid;
221 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
222 dim.asc = textdim_.asc;
224 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
225 dim.wid = max(dim.wid, textdim_.wid);
227 dim.wid = max(dim.wid, mi.base.textwidth);
232 dim.asc += TEXT_TO_INSET_OFFSET;
233 dim.des += TEXT_TO_INSET_OFFSET;
234 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
235 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
236 bool const changed = dim_ != dim;
242 bool InsetCollapsable::setMouseHover(bool mouse_hover)
244 mouse_hover_ = mouse_hover;
249 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
251 autoOpen_ = pi.base.bv->cursor().isInside(this);
252 text_.background_color_ = backgroundColor();
253 const int xx = x + TEXT_TO_INSET_OFFSET;
255 // Draw button first -- top, left or only
256 Dimension dimc = dimensionCollapsed();
257 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
258 if (decoration() == Classic) {
259 button_dim.x1 = xx + 0;
260 button_dim.x2 = xx + dimc.width();
262 button_dim.y2 = top + dimc.height();
264 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
268 switch (geometry()) {
270 textx = xx + dimc.width();
271 texty = top + textdim_.asc;
272 InsetText::draw(pi, textx, texty);
276 texty = top + dimc.height() + textdim_.asc;
277 InsetText::draw(pi, textx, texty);
283 texty = y + textdim_.asc;
284 InsetText::draw(pi, textx, texty);
289 texty = y + textdim_.asc;
290 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
291 InsetText::draw(pi, textx, texty);
292 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
294 int desc = InsetText::descent();
295 if (status() == Open)
300 pi.pain.line(x, y + desc - 4, x, y + desc,
301 layout_.labelfont.color());
302 if (internalStatus() == Open)
303 pi.pain.line(x, y + desc,
304 x + dim_.wid - 3, y + desc,
305 layout_.labelfont.color());
307 // Make status_ value visible:
308 pi.pain.line(x, y + desc,
310 layout_.labelfont.color());
311 pi.pain.line(x + dim_.wid - 7, y + desc,
312 x + dim_.wid -3, y + desc,
313 layout_.labelfont.color());
315 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
316 layout_.labelfont.color());
318 // the label of the charstyle. Can be toggled.
319 if (status() == Open) {
320 Font font(layout_.labelfont);
321 font.realize(Font(Font::ALL_SANE));
328 docstring s = layout_.labelstring;
329 theFontMetrics(font).rectText(s, w, a, d);
330 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
331 s, font, Color::none, Color::none);
334 // a visual cue when the cursor is inside the inset
335 Cursor & cur = pi.base.bv->cursor();
336 if (cur.isInside(this)) {
339 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
340 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
341 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
342 layout_.labelfont.color());
343 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
344 layout_.labelfont.color());
348 setPosCache(pi, x, y);
352 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
354 x += TEXT_TO_INSET_OFFSET;
355 switch (geometry()) {
357 x += dimensionCollapsed().wid;
358 InsetText::drawSelection(pi, x, y);
361 y += dimensionCollapsed().des + textdim_.asc;
362 InsetText::drawSelection(pi, x, y);
369 InsetText::drawSelection(pi, x, y);
375 void InsetCollapsable::cursorPos(BufferView const & bv,
376 CursorSlice const & sl, bool boundary, int & x, int & y) const
378 BOOST_ASSERT(geometry() != ButtonOnly);
380 InsetText::cursorPos(bv, sl, boundary, x, y);
382 switch (geometry()) {
384 x += dimensionCollapsed().wid;
387 y += dimensionCollapsed().height() - ascent()
388 + TEXT_TO_INSET_OFFSET + textdim_.asc;
399 x += TEXT_TO_INSET_OFFSET;
403 Inset::EDITABLE InsetCollapsable::editable() const
405 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
409 bool InsetCollapsable::descendable() const
411 return geometry() != ButtonOnly;
415 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
417 return button_dim.contains(cmd.x, cmd.y);
421 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
424 pos_type const max_length = 15;
425 pos_type const p_siz = paragraphs().begin()->size();
426 pos_type const n = min(max_length, p_siz);
429 for (; i < n && j < p_siz; ++j) {
430 if (paragraphs().begin()->isInset(j))
432 label += paragraphs().begin()->getChar(j);
435 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
438 return label.empty() ? l : label;
442 void InsetCollapsable::edit(Cursor & cur, bool left)
444 //lyxerr << "InsetCollapsable: edit left/right" << endl;
446 InsetText::edit(cur, left);
450 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
452 //lyxerr << "InsetCollapsable: edit xy" << endl;
453 if (geometry() == ButtonOnly
454 || (button_dim.contains(x, y)
455 && decoration() != Minimalistic))
458 return InsetText::editXY(cur, x, y);
462 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
464 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
465 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
467 switch (cmd.action) {
468 case LFUN_MOUSE_PRESS:
469 if (cmd.button() == mouse_button::button1
471 && decoration() != Minimalistic) {
472 // reset selection if necessary (see bug 3060)
474 cur.bv().cursor().clearSelection();
480 if (decoration() == Minimalistic)
481 InsetText::doDispatch(cur, cmd);
482 else if (geometry() != ButtonOnly
484 InsetText::doDispatch(cur, cmd);
489 case LFUN_MOUSE_MOTION:
490 case LFUN_MOUSE_DOUBLE:
491 case LFUN_MOUSE_TRIPLE:
492 if (decoration() == Minimalistic)
493 InsetText::doDispatch(cur, cmd);
494 else if (geometry() != ButtonOnly
496 InsetText::doDispatch(cur, cmd);
501 case LFUN_MOUSE_RELEASE:
502 if (cmd.button() == mouse_button::button3) {
503 if (decoration() == Conglomerate) {
504 if (internalStatus() == Open)
505 setStatus(cur, Collapsed);
507 setStatus(cur, Open);
511 // configuration dialog
512 showInsetDialog(&cur.bv());
517 if (decoration() == Minimalistic) {
518 // The mouse click has to be within the inset!
519 InsetText::doDispatch(cur, cmd);
523 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
524 // if we are selecting, we do not want to
528 // Left button is clicked, the user asks to
529 // toggle the inset visual state.
531 cur.updateFlags(Update::Force | Update::FitCursor);
532 if (geometry() == ButtonOnly) {
533 setStatus(cur, Open);
537 setStatus(cur, Collapsed);
539 cur.bv().cursor() = cur;
543 // The mouse click is within the opened inset.
544 if (geometry() == TopButton
545 || geometry() == LeftButton)
546 InsetText::doDispatch(cur, cmd);
549 case LFUN_INSET_TOGGLE:
550 if (cmd.argument() == "open")
551 setStatus(cur, Open);
552 else if (cmd.argument() == "close")
553 setStatus(cur, Collapsed);
554 else if (cmd.argument() == "toggle" || cmd.argument().empty())
555 if (internalStatus() == Open) {
556 setStatus(cur, Collapsed);
557 if (geometry() == ButtonOnly)
558 cur.top().forwardPos();
560 setStatus(cur, Open);
561 else // if assign or anything else
567 InsetText::doDispatch(cur, cmd);
573 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
574 FuncStatus & flag) const
576 switch (cmd.action) {
578 case LFUN_INSET_TOGGLE:
579 if (cmd.argument() == "open" || cmd.argument() == "close" ||
580 cmd.argument() == "toggle")
587 return InsetText::getStatus(cur, cmd, flag);
592 void InsetCollapsable::setLabel(docstring const & l)
594 layout_.labelstring = l;
598 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
602 if (status_ == Collapsed)
603 cur.leaveInset(*this);
607 void InsetCollapsable::setLabelFont(Font const & font)
609 layout_.labelfont = font;
612 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
614 FloatList const & floats = bp.getTextClass().floats();
615 FloatList::const_iterator it = floats[type];
617 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
621 InsetCollapsable::Decoration InsetCollapsable::decoration() const
623 if (layout_.decoration == "classic")
625 if (layout_.decoration == "minimalistic")
627 if (layout_.decoration == "conglomerate")
629 if (name() == from_ascii("CharStyle"))
635 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
636 OutputParams const & runparams) const
638 // This implements the standard way of handling the LaTeX output of
639 // a collapsable inset, either a command or an environment. Standard
640 // collapsable insets should not redefine this, non-standard ones may
642 if (!layout_.latexname.empty()) {
643 if (layout_.latextype == "command") {
645 os << '\\' << from_utf8(layout_.latexname);
646 if (!layout_.latexparam.empty())
647 os << from_utf8(layout_.latexparam);
649 } else if (layout_.latextype == "environment") {
650 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
651 if (!layout_.latexparam.empty())
652 os << from_utf8(layout_.latexparam);
655 int i = InsetText::latex(buf, os, runparams);
656 if (!layout_.latexname.empty())
657 if (layout_.latextype == "command") {
659 } else if (layout_.latextype == "environment") {
660 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
667 void InsetCollapsable::validate(LaTeXFeatures & features) const
669 // Force inclusion of preamble snippet in layout file
670 features.addPreambleSnippet(layout_.preamble);
671 InsetText::validate(features);