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"
44 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
46 return autoOpen_ ? Open : status_;
50 InsetCollapsable::Geometry InsetCollapsable::geometry() const
52 switch (decoration()) {
54 if (status() == Open) {
63 return status() == Open ? NoButton : ButtonOnly ;
66 return status() == Open ? SubLabel : Corners ;
69 // dummy return value to shut down a warning,
75 InsetCollapsable::InsetCollapsable
76 (BufferParams const & bp, CollapseStatus status)
77 : InsetText(bp), status_(status),
78 openinlined_(false), autoOpen_(false), mouse_hover_(false)
80 setAutoBreakRows(true);
82 setFrameColor(Color::collapsableframe);
84 // Fallback for lacking inset layout item
85 layout_.bgcolor = Color::background;
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91 button_dim(rhs.button_dim),
93 topbaseline(rhs.topbaseline),
96 openinlined_(rhs.openinlined_),
97 autoOpen_(rhs.autoOpen_),
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 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
175 autoOpen_ = mi.base.bv->cursor().isInside(this);
177 switch (geometry()) {
179 InsetText::metrics(mi, dim);
182 InsetText::metrics(mi, dim);
187 InsetText::metrics(mi, dim);
188 // consider width of the inset label
189 Font font(layout_.labelfont);
190 font.realize(Font(Font::ALL_SANE));
196 docstring s = layout_.labelstring;
197 theFontMetrics(font).rectText(s, w, a, d);
204 dim = dimensionCollapsed();
205 if (geometry() == TopButton
206 || geometry() == LeftButton) {
208 InsetText::metrics(mi, textdim);
209 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
211 // Correct for button width.
212 dim.wid += textdim.wid;
213 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
214 dim.asc = textdim.asc;
216 dim.des += textdim.height() + TEXT_TO_BOTTOM_OFFSET;
217 dim.wid = max(dim.wid, textdim.wid);
225 bool InsetCollapsable::setMouseHover(bool mouse_hover)
227 mouse_hover_ = mouse_hover;
232 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
234 autoOpen_ = pi.base.bv->cursor().isInside(this);
235 int const old_color = pi.background_color;
236 pi.background_color = backgroundColor();
238 // Draw button first -- top, left or only
239 Dimension dimc = dimensionCollapsed();
241 if (geometry() == TopButton ||
242 geometry() == LeftButton ||
243 geometry() == ButtonOnly) {
244 button_dim.x1 = x + 0;
245 button_dim.x2 = x + dimc.width();
246 button_dim.y1 = y - dimc.asc;
247 button_dim.y2 = y + dimc.des;
249 pi.pain.buttonText(x, y, layout_.labelstring, layout_.labelfont, mouse_hover_);
257 Dimension const textdim = InsetText::dimension(*pi.base.bv);
258 int const baseline = y;
260 switch (geometry()) {
262 textx = x + dimc.width();
264 InsetText::draw(pi, textx, texty);
268 texty = baseline + dimc.des + textdim.asc;
269 InsetText::draw(pi, textx, texty);
276 InsetText::draw(pi, textx, texty);
282 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
283 InsetText::draw(pi, textx, texty);
284 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
286 int desc = textdim.descent();
287 if (geometry() == Corners)
290 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
291 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
292 pi.pain.line(xx1, y + desc - 4,
294 layout_.labelfont.color());
295 if (internalStatus() == Open)
296 pi.pain.line(xx1, y + desc,
298 layout_.labelfont.color());
300 // Make status_ value visible:
301 pi.pain.line(xx1, y + desc,
303 layout_.labelfont.color());
304 pi.pain.line(xx2 - 4, y + desc,
306 layout_.labelfont.color());
308 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3, y + desc - 4,
309 layout_.labelfont.color());
311 // the label below the text. Can be toggled.
312 if (geometry() == SubLabel) {
313 Font font(layout_.labelfont);
314 font.realize(Font(Font::ALL_SANE));
320 docstring s = layout_.labelstring;
321 theFontMetrics(font).rectText(s, w, a, d);
322 int const ww = max(textdim.wid, w);
323 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
324 s, font, Color::none, Color::none);
328 // a visual cue when the cursor is inside the inset
329 Cursor & cur = pi.base.bv->cursor();
330 if (cur.isInside(this)) {
333 pi.pain.line(xx1, y + 4, xx1, y, layout_.labelfont.color());
334 pi.pain.line(xx1 + 4, y, xx1, y, layout_.labelfont.color());
335 pi.pain.line(xx2, y + 4, xx2, y,
336 layout_.labelfont.color());
337 pi.pain.line(xx2 - 4, y, xx2, y,
338 layout_.labelfont.color());
342 pi.background_color = old_color;
346 void InsetCollapsable::cursorPos(BufferView const & bv,
347 CursorSlice const & sl, bool boundary, int & x, int & y) const
349 if (geometry() == ButtonOnly)
351 BOOST_ASSERT(geometry() != ButtonOnly);
353 InsetText::cursorPos(bv, sl, boundary, x, y);
354 Dimension const textdim = InsetText::dimension(bv);
356 switch (geometry()) {
358 x += dimensionCollapsed().wid;
361 y += dimensionCollapsed().des + textdim.asc;
376 Inset::EDITABLE InsetCollapsable::editable() const
378 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
382 bool InsetCollapsable::descendable() const
384 return geometry() != ButtonOnly;
388 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
390 return button_dim.contains(cmd.x, cmd.y);
394 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
397 pos_type const max_length = 15;
398 pos_type const p_siz = paragraphs().begin()->size();
399 pos_type const n = std::min(max_length, p_siz);
402 for (; i < n && j < p_siz; ++j) {
403 if (paragraphs().begin()->isInset(j))
405 label += paragraphs().begin()->getChar(j);
408 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
411 return label.empty() ? l : label;
415 void InsetCollapsable::edit(Cursor & cur, bool left)
417 //lyxerr << "InsetCollapsable: edit left/right" << endl;
419 InsetText::edit(cur, left);
423 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
425 //lyxerr << "InsetCollapsable: edit xy" << endl;
426 if (geometry() == ButtonOnly
427 || (button_dim.contains(x, y)
428 && geometry() != NoButton))
431 return InsetText::editXY(cur, x, y);
435 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
437 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
438 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
440 switch (cmd.action) {
441 case LFUN_MOUSE_PRESS:
442 if (cmd.button() == mouse_button::button1
444 && geometry() != NoButton) {
445 // reset selection if necessary (see bug 3060)
447 cur.bv().cursor().clearSelection();
453 if (geometry() == NoButton)
454 InsetText::doDispatch(cur, cmd);
455 else if (geometry() != ButtonOnly
457 InsetText::doDispatch(cur, cmd);
462 case LFUN_MOUSE_MOTION:
463 case LFUN_MOUSE_DOUBLE:
464 case LFUN_MOUSE_TRIPLE:
465 if (geometry() == NoButton)
466 InsetText::doDispatch(cur, cmd);
467 else if (geometry() != ButtonOnly
469 InsetText::doDispatch(cur, cmd);
474 case LFUN_MOUSE_RELEASE:
475 if (cmd.button() == mouse_button::button3) {
476 // There is no button to right click:
477 if (geometry() == Corners ||
478 geometry() == SubLabel ||
479 geometry() == NoButton) {
480 if (internalStatus() == Open)
481 setStatus(cur, Collapsed);
483 setStatus(cur, Open);
487 // configuration dialog
488 showInsetDialog(&cur.bv());
493 if (geometry() == NoButton) {
494 // The mouse click has to be within the inset!
495 InsetText::doDispatch(cur, cmd);
499 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
500 // if we are selecting, we do not want to
504 // Left button is clicked, the user asks to
505 // toggle the inset visual state.
507 cur.updateFlags(Update::Force | Update::FitCursor);
508 if (geometry() == ButtonOnly) {
509 setStatus(cur, Open);
513 setStatus(cur, Collapsed);
515 cur.bv().cursor() = cur;
519 // The mouse click is within the opened inset.
520 if (geometry() == TopButton
521 || geometry() == LeftButton)
522 InsetText::doDispatch(cur, cmd);
525 case LFUN_INSET_TOGGLE:
526 if (cmd.argument() == "open")
527 setStatus(cur, Open);
528 else if (cmd.argument() == "close")
529 setStatus(cur, Collapsed);
530 else if (cmd.argument() == "toggle" || cmd.argument().empty())
531 if (internalStatus() == Open) {
532 setStatus(cur, Collapsed);
533 if (geometry() == ButtonOnly)
534 cur.top().forwardPos();
536 setStatus(cur, Open);
537 else // if assign or anything else
543 InsetText::doDispatch(cur, cmd);
549 bool InsetCollapsable::allowMultiPar() const
551 return layout_.multipar;
555 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
556 FuncStatus & flag) const
558 switch (cmd.action) {
560 case LFUN_INSET_TOGGLE:
561 if (cmd.argument() == "open" || cmd.argument() == "close" ||
562 cmd.argument() == "toggle")
569 return InsetText::getStatus(cur, cmd, flag);
574 void InsetCollapsable::setLabel(docstring const & l)
576 layout_.labelstring = l;
580 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
584 if (status_ == Collapsed)
585 cur.leaveInset(*this);
589 void InsetCollapsable::setLabelFont(Font const & font)
591 layout_.labelfont = font;
594 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
596 FloatList const & floats = bp.getTextClass().floats();
597 FloatList::const_iterator it = floats[type];
599 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
603 InsetCollapsable::Decoration InsetCollapsable::decoration() const
605 if (layout_.decoration == "classic")
607 if (layout_.decoration == "minimalistic")
609 if (layout_.decoration == "conglomerate")
611 if (name() == from_ascii("Flex"))
617 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
618 OutputParams const & runparams) const
620 // This implements the standard way of handling the LaTeX output of
621 // a collapsable inset, either a command or an environment. Standard
622 // collapsable insets should not redefine this, non-standard ones may
624 if (!layout_.latexname.empty()) {
625 if (layout_.latextype == "command") {
627 os << '\\' << from_utf8(layout_.latexname);
628 if (!layout_.latexparam.empty())
629 os << from_utf8(layout_.latexparam);
631 } else if (layout_.latextype == "environment") {
632 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
633 if (!layout_.latexparam.empty())
634 os << from_utf8(layout_.latexparam);
637 OutputParams rp = runparams;
638 if (layout_.verbatim)
640 int i = InsetText::latex(buf, os, rp);
641 if (!layout_.latexname.empty()) {
642 if (layout_.latextype == "command") {
644 } else if (layout_.latextype == "environment") {
645 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";
653 void InsetCollapsable::validate(LaTeXFeatures & features) const
655 // Force inclusion of preamble snippet in layout file
656 features.require(layout_.name);
657 InsetText::validate(features);