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);
90 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
92 button_dim(rhs.button_dim),
94 topbaseline(rhs.topbaseline),
97 openinlined_(rhs.openinlined_),
98 autoOpen_(rhs.autoOpen_),
99 textdim_(rhs.textdim_),
100 // the sole purpose of this copy constructor
106 void InsetCollapsable::setLayout(BufferParams const & bp)
108 layout_ = getLayout(bp);
112 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
124 text_.write(buf, os);
128 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
130 bool token_found = false;
133 string const token = lex.getString();
134 if (token == "status") {
136 string const tmp_token = lex.getString();
138 if (tmp_token == "collapsed") {
141 } else if (tmp_token == "open") {
145 lyxerr << "InsetCollapsable::read: Missing status!"
147 // Take countermeasures
148 lex.pushToken(token);
151 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
153 // take countermeasures
154 lex.pushToken(token);
157 InsetText::read(buf, lex);
160 status_ = isOpen() ? Open : Collapsed;
166 Dimension InsetCollapsable::dimensionCollapsed() const
169 theFontMetrics(layout_.labelfont).buttonText(
170 layout_.labelstring, dim.wid, dim.asc, dim.des);
175 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);
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 = 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);
602 // Because the collapse status is part of the inset and thus an
603 // integral part of the Buffer contents a changed status must be
604 // signaled to all views of current buffer.
605 cur.bv().buffer().changed();
609 void InsetCollapsable::setLabelFont(Font const & font)
611 layout_.labelfont = font;
614 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
616 FloatList const & floats = bp.getTextClass().floats();
617 FloatList::const_iterator it = floats[type];
619 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
624 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
625 OutputParams const & runparams) const
627 // This implements the standard way of handling the LaTeX output of
628 // a collapsable inset, either a command or an environment. Standard
629 // collapsable insets should not redefine this, non-standard ones may
631 if (!layout_.latexname.empty()) {
632 if (layout_.latextype == "command") {
634 os << '\\' << from_utf8(layout_.latexname);
635 if (!layout_.latexparam.empty())
636 os << from_utf8(layout_.latexparam);
638 } else if (layout_.latextype == "environment") {
639 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
640 if (!layout_.latexparam.empty())
641 os << from_utf8(layout_.latexparam);
644 int i = InsetText::latex(buf, os, runparams);
645 if (!layout_.latexname.empty())
646 if (layout_.latextype == "command") {
648 } else if (layout_.latextype == "environment") {
649 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
656 void InsetCollapsable::validate(LaTeXFeatures & features) const
658 // Force inclusion of preamble snippet in layout file
659 features.addPreambleSnippet(layout_.preamble);
660 InsetText::validate(features);