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 * buf, InsetText::UsePlain ltype)
45 : InsetText(buf, ltype), status_(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 return toolTipText();
135 void InsetCollapsable::write(ostream & os) const
151 void InsetCollapsable::read(Lexer & lex)
153 lex.setContext("InsetCollapsable::read");
156 lex >> "status" >> tmp_token;
157 if (tmp_token == "open")
160 InsetText::read(lex);
165 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
168 theFontMetrics(getLayout().labelfont()).buttonText(
169 buttonLabel(bv), dim.wid, dim.asc, dim.des);
174 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
176 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
178 FontInfo tmpfont = mi.base.font;
179 mi.base.font = getLayout().font();
180 mi.base.font.realize(tmpfont);
182 BufferView const & bv = *mi.base.bv;
184 switch (geometry(bv)) {
186 InsetText::metrics(mi, dim);
189 InsetText::metrics(mi, dim);
194 InsetText::metrics(mi, dim);
195 // consider width of the inset label
196 FontInfo font(getLayout().labelfont());
197 font.realize(sane_font);
203 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
210 dim = dimensionCollapsed(bv);
211 if (geometry(bv) == TopButton
212 || geometry(bv) == LeftButton) {
214 InsetText::metrics(mi, textdim);
215 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
217 // Correct for button width.
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_INSET_OFFSET;
223 dim.wid = max(dim.wid, textdim.wid);
229 mi.base.font = tmpfont;
233 bool InsetCollapsable::setMouseHover(bool mouse_hover)
235 mouse_hover_ = mouse_hover;
240 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
242 BufferView const & bv = *pi.base.bv;
244 auto_open_[&bv] = bv.cursor().isInside(this);
246 FontInfo tmpfont = pi.base.font;
247 pi.base.font = getLayout().font();
248 pi.base.font.realize(tmpfont);
250 // Draw button first -- top, left or only
251 Dimension dimc = dimensionCollapsed(bv);
253 if (geometry(bv) == TopButton ||
254 geometry(bv) == LeftButton ||
255 geometry(bv) == ButtonOnly) {
256 button_dim.x1 = x + 0;
257 button_dim.x2 = x + dimc.width();
258 button_dim.y1 = y - dimc.asc;
259 button_dim.y2 = y + dimc.des;
261 FontInfo labelfont = getLayout().labelfont();
262 labelfont.setColor(labelColor());
263 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
272 Dimension const textdim = InsetText::dimension(bv);
273 int const baseline = y;
275 switch (geometry(bv)) {
277 textx = x + dimc.width();
279 InsetText::draw(pi, textx, texty);
283 texty = baseline + dimc.des + textdim.asc;
284 InsetText::draw(pi, textx, texty);
291 InsetText::draw(pi, textx, texty);
297 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
298 InsetText::draw(pi, textx, texty);
299 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
301 int desc = textdim.descent();
302 if (geometry(bv) == Corners)
305 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
306 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
307 pi.pain.line(xx1, y + desc - 4,
311 pi.pain.line(xx1, y + desc,
315 // Make status_ value visible:
316 pi.pain.line(xx1, y + desc,
319 pi.pain.line(xx2 - 4, y + desc,
323 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
324 y + desc - 4, labelColor());
326 // the label below the text. Can be toggled.
327 if (geometry(bv) == SubLabel) {
328 FontInfo font(getLayout().labelfont());
329 font.realize(sane_font);
335 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
336 int const ww = max(textdim.wid, w);
337 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
338 buttonLabel(bv), font, Color_none, Color_none);
342 // a visual cue when the cursor is inside the inset
343 Cursor const & cur = bv.cursor();
344 if (cur.isInside(this)) {
347 pi.pain.line(xx1, y + 4, xx1, y, labelColor());
348 pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
349 pi.pain.line(xx2, y + 4, xx2, y,
351 pi.pain.line(xx2 - 4, y, xx2, y,
357 pi.base.font = tmpfont;
361 void InsetCollapsable::cursorPos(BufferView const & bv,
362 CursorSlice const & sl, bool boundary, int & x, int & y) const
364 if (geometry(bv) == ButtonOnly)
367 InsetText::cursorPos(bv, sl, boundary, x, y);
368 Dimension const textdim = InsetText::dimension(bv);
370 switch (geometry(bv)) {
372 x += dimensionCollapsed(bv).wid;
375 y += dimensionCollapsed(bv).des + textdim.asc;
390 bool InsetCollapsable::editable() const
392 return geometry() != ButtonOnly;
396 bool InsetCollapsable::descendable(BufferView const & bv) const
398 return geometry(bv) != ButtonOnly;
402 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
404 return button_dim.contains(cmd.x(), cmd.y());
408 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
411 pos_type const max_length = 15;
412 pos_type const p_siz = paragraphs().begin()->size();
413 pos_type const n = min(max_length, p_siz);
416 for (; i < n && j < p_siz; ++j) {
417 if (paragraphs().begin()->isInset(j))
419 label += paragraphs().begin()->getChar(j);
422 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
425 return label.empty() ? l : label;
429 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
431 //lyxerr << "InsetCollapsable: edit left/right" << endl;
433 InsetText::edit(cur, front, entry_from);
437 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
439 //lyxerr << "InsetCollapsable: edit xy" << endl;
440 if (geometry(cur.bv()) == ButtonOnly
441 || (button_dim.contains(x, y)
442 && geometry(cur.bv()) != NoButton))
445 return InsetText::editXY(cur, x, y);
449 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
451 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
452 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
454 switch (cmd.action()) {
455 case LFUN_MOUSE_PRESS:
456 if (hitButton(cmd)) {
457 switch (cmd.button()) {
458 case mouse_button::button1:
459 case mouse_button::button3:
460 // Pass the command to the enclosing InsetText,
461 // so that the cursor gets set.
464 case mouse_button::none:
465 case mouse_button::button2:
466 case mouse_button::button4:
467 case mouse_button::button5:
472 } else if (geometry(cur.bv()) != ButtonOnly)
473 InsetText::doDispatch(cur, cmd);
478 case LFUN_MOUSE_MOTION:
479 case LFUN_MOUSE_DOUBLE:
480 case LFUN_MOUSE_TRIPLE:
483 else if (geometry(cur.bv()) != ButtonOnly)
484 InsetText::doDispatch(cur, cmd);
489 case LFUN_MOUSE_RELEASE:
490 if (!hitButton(cmd)) {
491 // The mouse click has to be within the inset!
492 if (geometry(cur.bv()) != ButtonOnly)
493 InsetText::doDispatch(cur, cmd);
498 if (cmd.button() != mouse_button::button1) {
503 // if we are selecting, we do not want to
507 // Left button is clicked, the user asks to
508 // toggle the inset visual state.
510 cur.updateFlags(Update::Force | Update::FitCursor);
511 if (geometry(cur.bv()) == ButtonOnly) {
512 setStatus(cur, Open);
516 setStatus(cur, Collapsed);
517 cur.bv().cursor() = cur;
520 case LFUN_INSET_TOGGLE:
521 if (cmd.argument() == "open")
522 setStatus(cur, Open);
523 else if (cmd.argument() == "close")
524 setStatus(cur, Collapsed);
525 else if (cmd.argument() == "toggle" || cmd.argument().empty())
526 if (status_ == Open) {
527 setStatus(cur, Collapsed);
528 if (geometry(cur.bv()) == ButtonOnly)
529 cur.top().forwardPos();
531 setStatus(cur, Open);
532 else // if assign or anything else
538 InsetText::doDispatch(cur, cmd);
544 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
545 FuncStatus & flag) const
547 switch (cmd.action()) {
548 case LFUN_INSET_TOGGLE:
549 if (cmd.argument() == "open")
550 flag.setEnabled(status_ != Open);
551 else if (cmd.argument() == "close")
552 flag.setEnabled(status_ == Open);
553 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
554 flag.setEnabled(true);
555 flag.setOnOff(status_ == Open);
557 flag.setEnabled(false);
561 return InsetText::getStatus(cur, cmd, flag);
566 void InsetCollapsable::setLabel(docstring const & l)
572 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
574 docstring const label = labelstring_.empty() ?
575 translateIfPossible(getLayout().labelstring()) : labelstring_;
576 InsetLayout const & il = getLayout();
577 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
579 return getNewLabel(label);
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);