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"
29 #include "TextClass.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
34 #include "support/debug.h"
35 #include "support/docstream.h"
36 #include "support/gettext.h"
37 #include "support/lassert.h"
38 #include "support/lstrings.h"
45 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
46 : InsetText(buf, ltype), status_(Open), openinlined_(false)
48 setAutoBreakRows(true);
50 setFrameColor(Color_collapsableframe);
54 // The sole purpose of this copy constructor is to make sure
55 // that the mouse_hover_ map is not copied and remains empty.
56 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
59 labelstring_(rhs.labelstring_),
60 button_dim(rhs.button_dim),
61 openinlined_(rhs.openinlined_)
65 InsetCollapsable::~InsetCollapsable()
67 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
68 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
69 for (; it != end; ++it)
71 it->first->clearLastInset(this);
75 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
77 if (decoration() == InsetLayout::CONGLOMERATE)
79 return auto_open_[&bv] ? Open : status_;
83 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
85 switch (decoration()) {
86 case InsetLayout::CLASSIC:
87 if (status(bv) == Open)
88 return openinlined_ ? LeftButton : TopButton;
91 case InsetLayout::MINIMALISTIC:
92 return status(bv) == Open ? NoButton : ButtonOnly ;
94 case InsetLayout::CONGLOMERATE:
95 return status(bv) == Open ? SubLabel : Corners ;
97 case InsetLayout::DEFAULT:
98 break; // this shouldn't happen
101 // dummy return value to shut down a warning,
102 // this is dead code.
107 InsetCollapsable::Geometry InsetCollapsable::geometry() const
109 switch (decoration()) {
110 case InsetLayout::CLASSIC:
112 return openinlined_ ? LeftButton : TopButton;
115 case InsetLayout::MINIMALISTIC:
116 return status_ == Open ? NoButton : ButtonOnly ;
118 case InsetLayout::CONGLOMERATE:
119 return status_ == Open ? SubLabel : Corners ;
121 case InsetLayout::DEFAULT:
122 break; // this shouldn't happen
125 // dummy return value to shut down a warning,
126 // this is dead code.
131 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
133 Dimension dim = dimensionCollapsed(bv);
134 if (geometry(bv) == NoButton)
135 return translateIfPossible(getLayout().labelstring());
136 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
139 return toolTipText();
143 void InsetCollapsable::write(ostream & os) const
159 void InsetCollapsable::read(Lexer & lex)
161 lex.setContext("InsetCollapsable::read");
164 lex >> "status" >> tmp_token;
165 if (tmp_token == "open")
168 InsetText::read(lex);
173 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
176 theFontMetrics(getLayout().labelfont()).buttonText(
177 buttonLabel(bv), dim.wid, dim.asc, dim.des);
182 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
184 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
186 FontInfo tmpfont = mi.base.font;
187 mi.base.font = getLayout().font();
188 mi.base.font.realize(tmpfont);
190 BufferView const & bv = *mi.base.bv;
192 switch (geometry(bv)) {
194 InsetText::metrics(mi, dim);
197 InsetText::metrics(mi, dim);
202 InsetText::metrics(mi, dim);
203 // consider width of the inset label
204 FontInfo font(getLayout().labelfont());
205 font.realize(sane_font);
211 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
218 dim = dimensionCollapsed(bv);
219 if (geometry(bv) == TopButton
220 || geometry(bv) == LeftButton) {
222 InsetText::metrics(mi, textdim);
223 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
225 // Correct for button width.
226 dim.wid += textdim.wid;
227 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
228 dim.asc = textdim.asc;
230 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
231 dim.wid = max(dim.wid, textdim.wid);
237 mi.base.font = tmpfont;
241 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
244 mouse_hover_[bv] = mouse_hover;
249 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
251 BufferView const & bv = *pi.base.bv;
253 auto_open_[&bv] = bv.cursor().isInside(this);
255 FontInfo tmpfont = pi.base.font;
256 pi.base.font = getLayout().font();
257 pi.base.font.realize(tmpfont);
259 // Draw button first -- top, left or only
260 Dimension dimc = dimensionCollapsed(bv);
262 if (geometry(bv) == TopButton ||
263 geometry(bv) == LeftButton ||
264 geometry(bv) == ButtonOnly) {
265 button_dim.x1 = x + 0;
266 button_dim.x2 = x + dimc.width();
267 button_dim.y1 = y - dimc.asc;
268 button_dim.y2 = y + dimc.des;
270 FontInfo labelfont = getLayout().labelfont();
271 labelfont.setColor(labelColor());
272 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
281 Dimension const textdim = InsetText::dimension(bv);
282 int const baseline = y;
284 switch (geometry(bv)) {
286 textx = x + dimc.width();
288 InsetText::draw(pi, textx, texty);
292 texty = baseline + dimc.des + textdim.asc;
293 InsetText::draw(pi, textx, texty);
300 InsetText::draw(pi, textx, texty);
306 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
307 InsetText::draw(pi, textx, texty);
308 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
310 int desc = textdim.descent();
311 if (geometry(bv) == Corners)
314 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
315 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
316 pi.pain.line(xx1, y + desc - 4,
320 pi.pain.line(xx1, y + desc,
324 // Make status_ value visible:
325 pi.pain.line(xx1, y + desc,
328 pi.pain.line(xx2 - 4, y + desc,
332 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
333 y + desc - 4, Color_foreground);
335 // the label below the text. Can be toggled.
336 if (geometry(bv) == SubLabel) {
337 FontInfo font(getLayout().labelfont());
338 font.realize(sane_font);
344 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
345 int const ww = max(textdim.wid, w);
346 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
347 buttonLabel(bv), font, Color_none, Color_none);
351 // a visual cue when the cursor is inside the inset
352 Cursor const & cur = bv.cursor();
353 if (cur.isInside(this)) {
356 pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
357 pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
358 pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
359 pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
364 pi.base.font = tmpfont;
368 void InsetCollapsable::cursorPos(BufferView const & bv,
369 CursorSlice const & sl, bool boundary, int & x, int & y) const
371 if (geometry(bv) == ButtonOnly)
374 InsetText::cursorPos(bv, sl, boundary, x, y);
375 Dimension const textdim = InsetText::dimension(bv);
377 switch (geometry(bv)) {
379 x += dimensionCollapsed(bv).wid;
382 y += dimensionCollapsed(bv).des + textdim.asc;
397 bool InsetCollapsable::editable() const
399 return geometry() != ButtonOnly;
403 bool InsetCollapsable::descendable(BufferView const & bv) const
405 return geometry(bv) != ButtonOnly;
409 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
411 return button_dim.contains(cmd.x(), cmd.y());
415 bool InsetCollapsable::clickable(int x, int y) const
417 FuncRequest cmd(LFUN_NOACTION, x, y, mouse_button::none);
418 return hitButton(cmd);
422 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
425 pos_type const max_length = 15;
426 pos_type const p_siz = paragraphs().begin()->size();
427 pos_type const n = min(max_length, p_siz);
430 for (; i < n && j < p_siz; ++j) {
431 if (paragraphs().begin()->isInset(j))
433 label += paragraphs().begin()->getChar(j);
436 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
439 return label.empty() ? l : label;
443 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
445 //lyxerr << "InsetCollapsable: edit left/right" << endl;
447 InsetText::edit(cur, front, entry_from);
451 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
453 //lyxerr << "InsetCollapsable: edit xy" << endl;
454 if (geometry(cur.bv()) == ButtonOnly
455 || (button_dim.contains(x, y)
456 && geometry(cur.bv()) != NoButton))
459 return InsetText::editXY(cur, x, y);
463 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
465 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
466 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
468 switch (cmd.action()) {
469 case LFUN_MOUSE_PRESS:
470 if (hitButton(cmd)) {
471 switch (cmd.button()) {
472 case mouse_button::button1:
473 case mouse_button::button3:
474 // Pass the command to the enclosing InsetText,
475 // so that the cursor gets set.
478 case mouse_button::none:
479 case mouse_button::button2:
480 case mouse_button::button4:
481 case mouse_button::button5:
483 cur.noScreenUpdate();
486 } else if (geometry(cur.bv()) != ButtonOnly)
487 InsetText::doDispatch(cur, cmd);
492 case LFUN_MOUSE_MOTION:
493 case LFUN_MOUSE_DOUBLE:
494 case LFUN_MOUSE_TRIPLE:
496 cur.noScreenUpdate();
497 else if (geometry(cur.bv()) != ButtonOnly)
498 InsetText::doDispatch(cur, cmd);
503 case LFUN_MOUSE_RELEASE:
504 if (!hitButton(cmd)) {
505 // The mouse click has to be within the inset!
506 if (geometry(cur.bv()) != ButtonOnly)
507 InsetText::doDispatch(cur, cmd);
512 if (cmd.button() != mouse_button::button1) {
514 cur.noScreenUpdate();
517 // if we are selecting, we do not want to
521 // Left button is clicked, the user asks to
522 // toggle the inset visual state.
524 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
525 if (geometry(cur.bv()) == ButtonOnly) {
526 setStatus(cur, Open);
530 setStatus(cur, Collapsed);
531 cur.bv().cursor() = cur;
534 case LFUN_INSET_TOGGLE:
535 if (cmd.argument() == "open")
536 setStatus(cur, Open);
537 else if (cmd.argument() == "close")
538 setStatus(cur, Collapsed);
539 else if (cmd.argument() == "toggle" || cmd.argument().empty())
540 if (status_ == Open) {
541 setStatus(cur, Collapsed);
542 if (geometry(cur.bv()) == ButtonOnly)
543 cur.top().forwardPos();
545 setStatus(cur, Open);
546 else // if assign or anything else
552 InsetText::doDispatch(cur, cmd);
558 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
559 FuncStatus & flag) const
561 switch (cmd.action()) {
562 case LFUN_INSET_TOGGLE:
563 if (cmd.argument() == "open")
564 flag.setEnabled(status_ != Open);
565 else if (cmd.argument() == "close")
566 flag.setEnabled(status_ == Open);
567 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
568 flag.setEnabled(true);
569 flag.setOnOff(status_ == Open);
571 flag.setEnabled(false);
575 return InsetText::getStatus(cur, cmd, flag);
580 void InsetCollapsable::setLabel(docstring const & l)
586 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
588 docstring const label = labelstring_.empty() ?
589 translateIfPossible(getLayout().labelstring()) : labelstring_;
590 InsetLayout const & il = getLayout();
591 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
593 return getNewLabel(label);
597 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
601 if (status_ == Collapsed)
602 cur.leaveInset(*this);
606 docstring InsetCollapsable::floatName(string const & type) const
608 BufferParams const & bp = buffer().params();
609 FloatList const & floats = bp.documentClass().floats();
610 FloatList::const_iterator it = floats[type];
612 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
616 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
618 InsetLayout::InsetDecoration const dec = getLayout().decoration();
619 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
623 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
626 if (decoration() == InsetLayout::CONGLOMERATE)
627 return from_ascii("context-conglomerate");
629 if (geometry(bv) == NoButton)
630 return from_ascii("context-collapsable");
632 Dimension dim = dimensionCollapsed(bv);
633 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
634 return from_ascii("context-collapsable");
636 return InsetText::contextMenu(bv, x, y);