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"
21 #include "Dimension.h"
22 #include "FloatList.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "InsetLayout.h"
27 #include "MetricsInfo.h"
28 #include "OutputParams.h"
30 #include "frontends/FontMetrics.h"
31 #include "frontends/Painter.h"
33 #include "support/debug.h"
34 #include "support/docstream.h"
35 #include "support/gettext.h"
36 #include "support/lassert.h"
37 #include "support/lstrings.h"
44 InsetCollapsable::InsetCollapsable(Buffer const & buf, InsetText::UsePlain ltype)
45 : InsetText(buf, ltype), status_(Inset::Open),
46 openinlined_(false), mouse_hover_(false)
48 setAutoBreakRows(true);
50 setFrameColor(Color_collapsableframe);
54 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
57 labelstring_(rhs.labelstring_),
58 button_dim(rhs.button_dim),
59 openinlined_(rhs.openinlined_),
60 auto_open_(rhs.auto_open_),
61 // the sole purpose of this copy constructor
67 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
69 if (decoration() == InsetLayout::CONGLOMERATE)
71 return auto_open_[&bv] ? Open : status_;
75 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
77 switch (decoration()) {
78 case InsetLayout::CLASSIC:
79 if (status(bv) == Open)
80 return openinlined_ ? LeftButton : TopButton;
83 case InsetLayout::MINIMALISTIC:
84 return status(bv) == Open ? NoButton : ButtonOnly ;
86 case InsetLayout::CONGLOMERATE:
87 return status(bv) == Open ? SubLabel : Corners ;
89 case InsetLayout::DEFAULT:
90 break; // this shouldn't happen
93 // dummy return value to shut down a warning,
99 InsetCollapsable::Geometry InsetCollapsable::geometry() const
101 switch (decoration()) {
102 case InsetLayout::CLASSIC:
104 return openinlined_ ? LeftButton : TopButton;
107 case InsetLayout::MINIMALISTIC:
108 return status_ == Open ? NoButton : ButtonOnly ;
110 case InsetLayout::CONGLOMERATE:
111 return status_ == Open ? SubLabel : Corners ;
113 case InsetLayout::DEFAULT:
114 break; // this shouldn't happen
117 // dummy return value to shut down a warning,
118 // this is dead code.
123 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
125 Dimension dim = dimensionCollapsed(bv);
126 if (geometry(bv) == NoButton)
127 return translateIfPossible(getLayout().labelstring());
128 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
131 OutputParams rp(&buffer().params().encoding());
132 odocstringstream ods;
133 InsetText::plaintext(ods, rp);
134 docstring const content_tip = ods.str();
135 return support::wrapParas(content_tip, 4);
139 void InsetCollapsable::write(ostream & os) const
155 void InsetCollapsable::read(Lexer & lex)
157 lex.setContext("InsetCollapsable::read");
160 lex >> "status" >> tmp_token;
161 if (tmp_token == "open")
164 InsetText::read(lex);
169 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
172 theFontMetrics(getLayout().labelfont()).buttonText(
173 buttonLabel(bv), dim.wid, dim.asc, dim.des);
178 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
180 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
182 FontInfo tmpfont = mi.base.font;
183 mi.base.font = getLayout().font();
184 mi.base.font.realize(tmpfont);
186 BufferView const & bv = *mi.base.bv;
188 switch (geometry(bv)) {
190 InsetText::metrics(mi, dim);
193 InsetText::metrics(mi, dim);
198 InsetText::metrics(mi, dim);
199 // consider width of the inset label
200 FontInfo font(getLayout().labelfont());
201 font.realize(sane_font);
207 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
214 dim = dimensionCollapsed(bv);
215 if (geometry(bv) == TopButton
216 || geometry(bv) == LeftButton) {
218 InsetText::metrics(mi, textdim);
219 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
221 // Correct for button width.
222 dim.wid += textdim.wid;
223 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
224 dim.asc = textdim.asc;
226 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
227 dim.wid = max(dim.wid, textdim.wid);
233 mi.base.font = tmpfont;
237 bool InsetCollapsable::setMouseHover(bool mouse_hover)
239 mouse_hover_ = mouse_hover;
244 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
246 BufferView const & bv = *pi.base.bv;
248 auto_open_[&bv] = bv.cursor().isInside(this);
250 FontInfo tmpfont = pi.base.font;
251 pi.base.font = getLayout().font();
252 pi.base.font.realize(tmpfont);
254 // Draw button first -- top, left or only
255 Dimension dimc = dimensionCollapsed(bv);
257 if (geometry(bv) == TopButton ||
258 geometry(bv) == LeftButton ||
259 geometry(bv) == ButtonOnly) {
260 button_dim.x1 = x + 0;
261 button_dim.x2 = x + dimc.width();
262 button_dim.y1 = y - dimc.asc;
263 button_dim.y2 = y + dimc.des;
265 FontInfo labelfont = getLayout().labelfont();
266 labelfont.setColor(labelColor());
267 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
276 Dimension const textdim = InsetText::dimension(bv);
277 int const baseline = y;
279 switch (geometry(bv)) {
281 textx = x + dimc.width();
283 InsetText::draw(pi, textx, texty);
287 texty = baseline + dimc.des + textdim.asc;
288 InsetText::draw(pi, textx, texty);
295 InsetText::draw(pi, textx, texty);
301 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
302 InsetText::draw(pi, textx, texty);
303 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
305 int desc = textdim.descent();
306 if (geometry(bv) == Corners)
309 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
310 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
311 pi.pain.line(xx1, y + desc - 4,
315 pi.pain.line(xx1, y + desc,
319 // Make status_ value visible:
320 pi.pain.line(xx1, y + desc,
323 pi.pain.line(xx2 - 4, y + desc,
327 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
328 y + desc - 4, labelColor());
330 // the label below the text. Can be toggled.
331 if (geometry(bv) == SubLabel) {
332 FontInfo font(getLayout().labelfont());
333 font.realize(sane_font);
339 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
340 int const ww = max(textdim.wid, w);
341 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
342 buttonLabel(bv), font, Color_none, Color_none);
346 // a visual cue when the cursor is inside the inset
347 Cursor const & cur = bv.cursor();
348 if (cur.isInside(this)) {
351 pi.pain.line(xx1, y + 4, xx1, y, labelColor());
352 pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
353 pi.pain.line(xx2, y + 4, xx2, y,
355 pi.pain.line(xx2 - 4, y, xx2, y,
361 pi.base.font = tmpfont;
365 void InsetCollapsable::cursorPos(BufferView const & bv,
366 CursorSlice const & sl, bool boundary, int & x, int & y) const
368 if (geometry(bv) == ButtonOnly)
371 InsetText::cursorPos(bv, sl, boundary, x, y);
372 Dimension const textdim = InsetText::dimension(bv);
374 switch (geometry(bv)) {
376 x += dimensionCollapsed(bv).wid;
379 y += dimensionCollapsed(bv).des + textdim.asc;
394 bool InsetCollapsable::editable() const
396 return geometry() != ButtonOnly;
400 bool InsetCollapsable::descendable(BufferView const & bv) const
402 return geometry(bv) != ButtonOnly;
406 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
408 return button_dim.contains(cmd.x, cmd.y);
412 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
415 pos_type const max_length = 15;
416 pos_type const p_siz = paragraphs().begin()->size();
417 pos_type const n = min(max_length, p_siz);
420 for (; i < n && j < p_siz; ++j) {
421 if (paragraphs().begin()->isInset(j))
423 label += paragraphs().begin()->getChar(j);
426 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
429 return label.empty() ? l : label;
433 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
435 //lyxerr << "InsetCollapsable: edit left/right" << endl;
437 InsetText::edit(cur, front, entry_from);
441 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
443 //lyxerr << "InsetCollapsable: edit xy" << endl;
444 if (geometry(cur.bv()) == ButtonOnly
445 || (button_dim.contains(x, y)
446 && geometry(cur.bv()) != NoButton))
449 return InsetText::editXY(cur, x, y);
453 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
455 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
456 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
458 switch (cmd.action) {
459 case LFUN_MOUSE_PRESS:
460 if (hitButton(cmd)) {
461 switch (cmd.button()) {
462 case mouse_button::button1:
463 case mouse_button::button3:
464 // Pass the command to the enclosing InsetText,
465 // so that the cursor gets set.
468 case mouse_button::none:
469 case mouse_button::button2:
470 case mouse_button::button4:
471 case mouse_button::button5:
476 } else if (geometry(cur.bv()) != ButtonOnly)
477 InsetText::doDispatch(cur, cmd);
482 case LFUN_MOUSE_MOTION:
483 case LFUN_MOUSE_DOUBLE:
484 case LFUN_MOUSE_TRIPLE:
487 else if (geometry(cur.bv()) != ButtonOnly)
488 InsetText::doDispatch(cur, cmd);
493 case LFUN_MOUSE_RELEASE:
494 if (!hitButton(cmd)) {
495 // The mouse click has to be within the inset!
496 if (geometry(cur.bv()) != ButtonOnly)
497 InsetText::doDispatch(cur, cmd);
502 if (cmd.button() != mouse_button::button1) {
507 // if we are selecting, we do not want to
511 // Left button is clicked, the user asks to
512 // toggle the inset visual state.
514 cur.updateFlags(Update::Force | Update::FitCursor);
515 if (geometry(cur.bv()) == ButtonOnly) {
516 setStatus(cur, Open);
520 setStatus(cur, Collapsed);
521 cur.bv().cursor() = cur;
524 case LFUN_INSET_TOGGLE:
525 if (cmd.argument() == "open")
526 setStatus(cur, Open);
527 else if (cmd.argument() == "close")
528 setStatus(cur, Collapsed);
529 else if (cmd.argument() == "toggle" || cmd.argument().empty())
530 if (status_ == Open) {
531 setStatus(cur, Collapsed);
532 if (geometry(cur.bv()) == ButtonOnly)
533 cur.top().forwardPos();
535 setStatus(cur, Open);
536 else // if assign or anything else
542 InsetText::doDispatch(cur, cmd);
548 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
549 FuncStatus & flag) const
551 switch (cmd.action) {
552 case LFUN_INSET_TOGGLE:
553 if (cmd.argument() == "open")
554 flag.setEnabled(status_ != Open);
555 else if (cmd.argument() == "close")
556 flag.setEnabled(status_ == Open);
557 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
558 flag.setEnabled(true);
559 flag.setOnOff(status_ == Open);
561 flag.setEnabled(false);
565 return InsetText::getStatus(cur, cmd, flag);
570 void InsetCollapsable::setLabel(docstring const & l)
576 docstring const InsetCollapsable::buttonLabel(BufferView const &) const
578 return labelstring_.empty() ?
579 translateIfPossible(getLayout().labelstring()) : labelstring_;
583 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
587 if (status_ == Collapsed) {
588 cur.leaveInset(*this);
589 mouse_hover_ = false;
594 docstring InsetCollapsable::floatName(string const & type) const
596 BufferParams const & bp = buffer().params();
597 FloatList const & floats = bp.documentClass().floats();
598 FloatList::const_iterator it = floats[type];
600 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
604 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
606 InsetLayout::InsetDecoration const dec = getLayout().decoration();
607 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
611 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
614 if (decoration() == InsetLayout::CONGLOMERATE)
615 return from_ascii("context-conglomerate");
617 if (geometry(bv) == NoButton)
618 return from_ascii("context-collapsable");
620 Dimension dim = dimensionCollapsed(bv);
621 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
622 return from_ascii("context-collapsable");
624 return InsetText::contextMenu(bv, x, y);