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 const int xx = x + TEXT_TO_INSET_OFFSET;
251 // Draw button first -- top, left or only
252 Dimension dimc = dimensionCollapsed();
253 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
254 if (decoration() == Classic) {
255 button_dim.x1 = xx + 0;
256 button_dim.x2 = xx + dimc.width();
258 button_dim.y2 = top + dimc.height();
260 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
264 switch (geometry()) {
266 textx = xx + dimc.width();
267 texty = top + textdim_.asc;
268 InsetText::draw(pi, textx, texty);
272 texty = top + dimc.height() + textdim_.asc;
273 InsetText::draw(pi, textx, texty);
279 texty = y + textdim_.asc;
280 InsetText::draw(pi, textx, texty);
285 texty = y + textdim_.asc;
286 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
287 InsetText::draw(pi, textx, texty);
288 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
290 int desc = InsetText::descent();
291 if (status() == Open)
296 pi.pain.line(x, y + desc - 4, x, y + desc,
297 layout_.labelfont.color());
298 if (internalStatus() == Open)
299 pi.pain.line(x, y + desc,
300 x + dim_.wid - 3, y + desc,
301 layout_.labelfont.color());
303 // Make status_ value visible:
304 pi.pain.line(x, y + desc,
306 layout_.labelfont.color());
307 pi.pain.line(x + dim_.wid - 7, y + desc,
308 x + dim_.wid -3, y + desc,
309 layout_.labelfont.color());
311 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
312 layout_.labelfont.color());
314 // the label of the charstyle. Can be toggled.
315 if (status() == Open) {
316 Font font(layout_.labelfont);
317 font.realize(Font(Font::ALL_SANE));
324 docstring s = layout_.labelstring;
325 theFontMetrics(font).rectText(s, w, a, d);
326 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
327 s, font, Color::none, Color::none);
330 // a visual cue when the cursor is inside the inset
331 Cursor & cur = pi.base.bv->cursor();
332 if (cur.isInside(this)) {
335 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
336 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
337 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
338 layout_.labelfont.color());
339 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
340 layout_.labelfont.color());
344 setPosCache(pi, x, y);
348 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
350 x += TEXT_TO_INSET_OFFSET;
351 switch (geometry()) {
353 x += dimensionCollapsed().wid;
354 InsetText::drawSelection(pi, x, y);
357 y += dimensionCollapsed().des + textdim_.asc;
358 InsetText::drawSelection(pi, x, y);
365 InsetText::drawSelection(pi, x, y);
371 void InsetCollapsable::cursorPos(BufferView const & bv,
372 CursorSlice const & sl, bool boundary, int & x, int & y) const
374 BOOST_ASSERT(geometry() != ButtonOnly);
376 InsetText::cursorPos(bv, sl, boundary, x, y);
378 switch (geometry()) {
380 x += dimensionCollapsed().wid;
383 y += dimensionCollapsed().height() - ascent()
384 + TEXT_TO_INSET_OFFSET + textdim_.asc;
395 x += TEXT_TO_INSET_OFFSET;
399 Inset::EDITABLE InsetCollapsable::editable() const
401 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
405 bool InsetCollapsable::descendable() const
407 return geometry() != ButtonOnly;
411 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
413 return button_dim.contains(cmd.x, cmd.y);
417 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
420 pos_type const max_length = 15;
421 pos_type const p_siz = paragraphs().begin()->size();
422 pos_type const n = min(max_length, p_siz);
425 for (; i < n && j < p_siz; ++j) {
426 if (paragraphs().begin()->isInset(j))
428 label += paragraphs().begin()->getChar(j);
431 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
434 return label.empty() ? l : label;
438 void InsetCollapsable::edit(Cursor & cur, bool left)
440 //lyxerr << "InsetCollapsable: edit left/right" << endl;
442 InsetText::edit(cur, left);
446 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
448 //lyxerr << "InsetCollapsable: edit xy" << endl;
449 if (geometry() == ButtonOnly
450 || (button_dim.contains(x, y)
451 && decoration() != Minimalistic))
454 return InsetText::editXY(cur, x, y);
458 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
460 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
461 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
463 switch (cmd.action) {
464 case LFUN_MOUSE_PRESS:
465 if (cmd.button() == mouse_button::button1
467 && decoration() != Minimalistic) {
468 // reset selection if necessary (see bug 3060)
470 cur.bv().cursor().clearSelection();
476 if (decoration() == Minimalistic)
477 InsetText::doDispatch(cur, cmd);
478 else if (geometry() != ButtonOnly
480 InsetText::doDispatch(cur, cmd);
485 case LFUN_MOUSE_MOTION:
486 case LFUN_MOUSE_DOUBLE:
487 case LFUN_MOUSE_TRIPLE:
488 if (decoration() == Minimalistic)
489 InsetText::doDispatch(cur, cmd);
490 else if (geometry() != ButtonOnly
492 InsetText::doDispatch(cur, cmd);
497 case LFUN_MOUSE_RELEASE:
498 if (cmd.button() == mouse_button::button3) {
499 if (decoration() == Conglomerate) {
501 if (internalStatus() == Open)
502 setStatus(cur, Collapsed);
504 setStatus(cur, Open);
508 // configuration dialog
509 showInsetDialog(&cur.bv());
514 if (decoration() == Minimalistic) {
515 // The mouse click has to be within the inset!
516 InsetText::doDispatch(cur, cmd);
520 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
521 // if we are selecting, we do not want to
525 // Left button is clicked, the user asks to
526 // toggle the inset visual state.
528 cur.updateFlags(Update::Force | Update::FitCursor);
529 if (geometry() == ButtonOnly) {
530 setStatus(cur, Open);
534 setStatus(cur, Collapsed);
536 cur.bv().cursor() = cur;
540 // The mouse click is within the opened inset.
541 if (geometry() == TopButton
542 || geometry() == LeftButton)
543 InsetText::doDispatch(cur, cmd);
546 case LFUN_INSET_TOGGLE:
547 if (cmd.argument() == "open")
548 setStatus(cur, Open);
549 else if (cmd.argument() == "close")
550 setStatus(cur, Collapsed);
551 else if (cmd.argument() == "toggle" || cmd.argument().empty())
552 if (internalStatus() == Open) {
553 setStatus(cur, Collapsed);
554 if (geometry() == ButtonOnly)
555 cur.top().forwardPos();
557 setStatus(cur, Open);
558 else // if assign or anything else
564 InsetText::doDispatch(cur, cmd);
570 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
571 FuncStatus & flag) const
573 switch (cmd.action) {
575 case LFUN_INSET_TOGGLE:
576 if (cmd.argument() == "open" || cmd.argument() == "close" ||
577 cmd.argument() == "toggle")
584 return InsetText::getStatus(cur, cmd, flag);
589 void InsetCollapsable::setLabel(docstring const & l)
591 layout_.labelstring = l;
595 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
599 if (status_ == Collapsed)
600 cur.leaveInset(*this);
601 // Because the collapse status is part of the inset and thus an
602 // integral part of the Buffer contents a changed status must be
603 // signaled to all views of current buffer.
604 cur.bv().buffer().changed();
608 void InsetCollapsable::setLabelFont(Font const & font)
610 layout_.labelfont = font;
613 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
615 FloatList const & floats = bp.getTextClass().floats();
616 FloatList::const_iterator it = floats[type];
618 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
623 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
624 OutputParams const & runparams) const
626 // This implements the standard way of handling the LaTeX output of
627 // a collapsable inset, either a command or an environment. Standard
628 // collapsable insets should not redefine this, non-standard ones may
630 if (!layout_.latexname.empty()) {
631 if (layout_.latextype == "command") {
633 os << '\\' << from_utf8(layout_.latexname);
634 if (!layout_.latexparam.empty())
635 os << from_utf8(layout_.latexparam);
637 } else if (layout_.latextype == "environment") {
638 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
639 if (!layout_.latexparam.empty())
640 os << from_utf8(layout_.latexparam);
643 int i = InsetText::latex(buf, os, runparams);
644 if (!layout_.latexname.empty())
645 if (layout_.latextype == "command") {
647 } else if (layout_.latextype == "environment") {
648 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
655 void InsetCollapsable::validate(LaTeXFeatures & features) const
657 // Force inclusion of preamble snippet in layout file
658 features.addPreambleSnippet(layout_.preamble);
659 InsetText::validate(features);