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), openinlined_(false)
47 setAutoBreakRows(true);
49 setFrameColor(Color_collapsableframe);
53 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
56 labelstring_(rhs.labelstring_),
57 button_dim(rhs.button_dim),
58 openinlined_(rhs.openinlined_),
59 auto_open_(rhs.auto_open_),
60 // the sole purpose of this copy constructor
66 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
68 if (decoration() == InsetLayout::CONGLOMERATE)
70 return auto_open_[&bv] ? Open : status_;
74 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
76 switch (decoration()) {
77 case InsetLayout::CLASSIC:
78 if (status(bv) == Open)
79 return openinlined_ ? LeftButton : TopButton;
82 case InsetLayout::MINIMALISTIC:
83 return status(bv) == Open ? NoButton : ButtonOnly ;
85 case InsetLayout::CONGLOMERATE:
86 return status(bv) == Open ? SubLabel : Corners ;
88 case InsetLayout::DEFAULT:
89 break; // this shouldn't happen
92 // dummy return value to shut down a warning,
98 InsetCollapsable::Geometry InsetCollapsable::geometry() const
100 switch (decoration()) {
101 case InsetLayout::CLASSIC:
103 return openinlined_ ? LeftButton : TopButton;
106 case InsetLayout::MINIMALISTIC:
107 return status_ == Open ? NoButton : ButtonOnly ;
109 case InsetLayout::CONGLOMERATE:
110 return status_ == Open ? SubLabel : Corners ;
112 case InsetLayout::DEFAULT:
113 break; // this shouldn't happen
116 // dummy return value to shut down a warning,
117 // this is dead code.
122 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
124 Dimension dim = dimensionCollapsed(bv);
125 if (geometry(bv) == NoButton)
126 return translateIfPossible(getLayout().labelstring());
127 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
130 return toolTipText();
134 void InsetCollapsable::write(ostream & os) const
150 void InsetCollapsable::read(Lexer & lex)
152 lex.setContext("InsetCollapsable::read");
155 lex >> "status" >> tmp_token;
156 if (tmp_token == "open")
159 InsetText::read(lex);
164 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
167 theFontMetrics(getLayout().labelfont()).buttonText(
168 buttonLabel(bv), dim.wid, dim.asc, dim.des);
173 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
175 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
177 FontInfo tmpfont = mi.base.font;
178 mi.base.font = getLayout().font();
179 mi.base.font.realize(tmpfont);
181 BufferView const & bv = *mi.base.bv;
183 switch (geometry(bv)) {
185 InsetText::metrics(mi, dim);
188 InsetText::metrics(mi, dim);
193 InsetText::metrics(mi, dim);
194 // consider width of the inset label
195 FontInfo font(getLayout().labelfont());
196 font.realize(sane_font);
202 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
209 dim = dimensionCollapsed(bv);
210 if (geometry(bv) == TopButton
211 || geometry(bv) == LeftButton) {
213 InsetText::metrics(mi, textdim);
214 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
216 // Correct for button width.
217 dim.wid += textdim.wid;
218 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
219 dim.asc = textdim.asc;
221 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
222 dim.wid = max(dim.wid, textdim.wid);
228 mi.base.font = tmpfont;
232 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
234 mouse_hover_[bv] = mouse_hover;
239 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
241 BufferView const & bv = *pi.base.bv;
243 auto_open_[&bv] = bv.cursor().isInside(this);
245 FontInfo tmpfont = pi.base.font;
246 pi.base.font = getLayout().font();
247 pi.base.font.realize(tmpfont);
249 // Draw button first -- top, left or only
250 Dimension dimc = dimensionCollapsed(bv);
252 if (geometry(bv) == TopButton ||
253 geometry(bv) == LeftButton ||
254 geometry(bv) == ButtonOnly) {
255 button_dim.x1 = x + 0;
256 button_dim.x2 = x + dimc.width();
257 button_dim.y1 = y - dimc.asc;
258 button_dim.y2 = y + dimc.des;
260 FontInfo labelfont = getLayout().labelfont();
261 labelfont.setColor(labelColor());
262 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
271 Dimension const textdim = InsetText::dimension(bv);
272 int const baseline = y;
274 switch (geometry(bv)) {
276 textx = x + dimc.width();
278 InsetText::draw(pi, textx, texty);
282 texty = baseline + dimc.des + textdim.asc;
283 InsetText::draw(pi, textx, texty);
290 InsetText::draw(pi, textx, texty);
296 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
297 InsetText::draw(pi, textx, texty);
298 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
300 int desc = textdim.descent();
301 if (geometry(bv) == Corners)
304 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
305 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
306 pi.pain.line(xx1, y + desc - 4,
310 pi.pain.line(xx1, y + desc,
314 // Make status_ value visible:
315 pi.pain.line(xx1, y + desc,
318 pi.pain.line(xx2 - 4, y + desc,
322 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
323 y + desc - 4, labelColor());
325 // the label below the text. Can be toggled.
326 if (geometry(bv) == SubLabel) {
327 FontInfo font(getLayout().labelfont());
328 font.realize(sane_font);
334 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
335 int const ww = max(textdim.wid, w);
336 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
337 buttonLabel(bv), font, Color_none, Color_none);
341 // a visual cue when the cursor is inside the inset
342 Cursor const & cur = bv.cursor();
343 if (cur.isInside(this)) {
346 pi.pain.line(xx1, y + 4, xx1, y, labelColor());
347 pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
348 pi.pain.line(xx2, y + 4, xx2, y,
350 pi.pain.line(xx2 - 4, y, xx2, y,
356 pi.base.font = tmpfont;
360 void InsetCollapsable::cursorPos(BufferView const & bv,
361 CursorSlice const & sl, bool boundary, int & x, int & y) const
363 if (geometry(bv) == ButtonOnly)
366 InsetText::cursorPos(bv, sl, boundary, x, y);
367 Dimension const textdim = InsetText::dimension(bv);
369 switch (geometry(bv)) {
371 x += dimensionCollapsed(bv).wid;
374 y += dimensionCollapsed(bv).des + textdim.asc;
389 bool InsetCollapsable::editable() const
391 return geometry() != ButtonOnly;
395 bool InsetCollapsable::descendable(BufferView const & bv) const
397 return geometry(bv) != ButtonOnly;
401 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
403 return button_dim.contains(cmd.x(), cmd.y());
407 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
410 pos_type const max_length = 15;
411 pos_type const p_siz = paragraphs().begin()->size();
412 pos_type const n = min(max_length, p_siz);
415 for (; i < n && j < p_siz; ++j) {
416 if (paragraphs().begin()->isInset(j))
418 label += paragraphs().begin()->getChar(j);
421 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
424 return label.empty() ? l : label;
428 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
430 //lyxerr << "InsetCollapsable: edit left/right" << endl;
432 InsetText::edit(cur, front, entry_from);
436 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
438 //lyxerr << "InsetCollapsable: edit xy" << endl;
439 if (geometry(cur.bv()) == ButtonOnly
440 || (button_dim.contains(x, y)
441 && geometry(cur.bv()) != NoButton))
444 return InsetText::editXY(cur, x, y);
448 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
450 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
451 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
453 switch (cmd.action()) {
454 case LFUN_MOUSE_PRESS:
455 if (hitButton(cmd)) {
456 switch (cmd.button()) {
457 case mouse_button::button1:
458 case mouse_button::button3:
459 // Pass the command to the enclosing InsetText,
460 // so that the cursor gets set.
463 case mouse_button::none:
464 case mouse_button::button2:
465 case mouse_button::button4:
466 case mouse_button::button5:
471 } else if (geometry(cur.bv()) != ButtonOnly)
472 InsetText::doDispatch(cur, cmd);
477 case LFUN_MOUSE_MOTION:
478 case LFUN_MOUSE_DOUBLE:
479 case LFUN_MOUSE_TRIPLE:
482 else if (geometry(cur.bv()) != ButtonOnly)
483 InsetText::doDispatch(cur, cmd);
488 case LFUN_MOUSE_RELEASE:
489 if (!hitButton(cmd)) {
490 // The mouse click has to be within the inset!
491 if (geometry(cur.bv()) != ButtonOnly)
492 InsetText::doDispatch(cur, cmd);
497 if (cmd.button() != mouse_button::button1) {
502 // if we are selecting, we do not want to
506 // Left button is clicked, the user asks to
507 // toggle the inset visual state.
509 cur.updateFlags(Update::Force | Update::FitCursor);
510 if (geometry(cur.bv()) == ButtonOnly) {
511 setStatus(cur, Open);
515 setStatus(cur, Collapsed);
516 cur.bv().cursor() = cur;
519 case LFUN_INSET_TOGGLE:
520 if (cmd.argument() == "open")
521 setStatus(cur, Open);
522 else if (cmd.argument() == "close")
523 setStatus(cur, Collapsed);
524 else if (cmd.argument() == "toggle" || cmd.argument().empty())
525 if (status_ == Open) {
526 setStatus(cur, Collapsed);
527 if (geometry(cur.bv()) == ButtonOnly)
528 cur.top().forwardPos();
530 setStatus(cur, Open);
531 else // if assign or anything else
537 InsetText::doDispatch(cur, cmd);
543 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
544 FuncStatus & flag) const
546 switch (cmd.action()) {
547 case LFUN_INSET_TOGGLE:
548 if (cmd.argument() == "open")
549 flag.setEnabled(status_ != Open);
550 else if (cmd.argument() == "close")
551 flag.setEnabled(status_ == Open);
552 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
553 flag.setEnabled(true);
554 flag.setOnOff(status_ == Open);
556 flag.setEnabled(false);
560 return InsetText::getStatus(cur, cmd, flag);
565 void InsetCollapsable::setLabel(docstring const & l)
571 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
573 docstring const label = labelstring_.empty() ?
574 translateIfPossible(getLayout().labelstring()) : labelstring_;
575 InsetLayout const & il = getLayout();
576 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
578 return getNewLabel(label);
582 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
586 if (status_ == Collapsed) {
587 cur.leaveInset(*this);
588 mouse_hover_.clear();
593 docstring InsetCollapsable::floatName(string const & type) const
595 BufferParams const & bp = buffer().params();
596 FloatList const & floats = bp.documentClass().floats();
597 FloatList::const_iterator it = floats[type];
599 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
603 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
605 InsetLayout::InsetDecoration const dec = getLayout().decoration();
606 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
610 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
613 if (decoration() == InsetLayout::CONGLOMERATE)
614 return from_ascii("context-conglomerate");
616 if (geometry(bv) == NoButton)
617 return from_ascii("context-collapsable");
619 Dimension dim = dimensionCollapsed(bv);
620 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
621 return from_ascii("context-collapsable");
623 return InsetText::contextMenu(bv, x, y);