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 // The sole purpose of this copy constructor is to make sure
54 // that the mouse_hover_ map is not copied and remains empty.
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
58 labelstring_(rhs.labelstring_),
59 button_dim(rhs.button_dim),
60 openinlined_(rhs.openinlined_)
64 InsetCollapsable::~InsetCollapsable()
66 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
67 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
68 for (; it != end; ++it)
70 it->first->clearLastInset(this);
74 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
76 if (decoration() == InsetLayout::CONGLOMERATE)
78 return auto_open_[&bv] ? Open : status_;
82 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
84 switch (decoration()) {
85 case InsetLayout::CLASSIC:
86 if (status(bv) == Open)
87 return openinlined_ ? LeftButton : TopButton;
90 case InsetLayout::MINIMALISTIC:
91 return status(bv) == Open ? NoButton : ButtonOnly ;
93 case InsetLayout::CONGLOMERATE:
94 return status(bv) == Open ? SubLabel : Corners ;
96 case InsetLayout::DEFAULT:
97 break; // this shouldn't happen
100 // dummy return value to shut down a warning,
101 // this is dead code.
106 InsetCollapsable::Geometry InsetCollapsable::geometry() const
108 switch (decoration()) {
109 case InsetLayout::CLASSIC:
111 return openinlined_ ? LeftButton : TopButton;
114 case InsetLayout::MINIMALISTIC:
115 return status_ == Open ? NoButton : ButtonOnly ;
117 case InsetLayout::CONGLOMERATE:
118 return status_ == Open ? SubLabel : Corners ;
120 case InsetLayout::DEFAULT:
121 break; // this shouldn't happen
124 // dummy return value to shut down a warning,
125 // this is dead code.
130 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
132 Dimension dim = dimensionCollapsed(bv);
133 if (geometry(bv) == NoButton)
134 return translateIfPossible(getLayout().labelstring());
135 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
138 return toolTipText();
142 void InsetCollapsable::write(ostream & os) const
158 void InsetCollapsable::read(Lexer & lex)
160 lex.setContext("InsetCollapsable::read");
163 lex >> "status" >> tmp_token;
164 if (tmp_token == "open")
167 InsetText::read(lex);
172 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
175 theFontMetrics(getLayout().labelfont()).buttonText(
176 buttonLabel(bv), dim.wid, dim.asc, dim.des);
181 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
183 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
185 FontInfo tmpfont = mi.base.font;
186 mi.base.font = getLayout().font();
187 mi.base.font.realize(tmpfont);
189 BufferView const & bv = *mi.base.bv;
191 switch (geometry(bv)) {
193 InsetText::metrics(mi, dim);
196 InsetText::metrics(mi, dim);
201 InsetText::metrics(mi, dim);
202 // consider width of the inset label
203 FontInfo font(getLayout().labelfont());
204 font.realize(sane_font);
210 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
217 dim = dimensionCollapsed(bv);
218 if (geometry(bv) == TopButton
219 || geometry(bv) == LeftButton) {
221 InsetText::metrics(mi, textdim);
222 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
224 // Correct for button width.
225 dim.wid += textdim.wid;
226 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
227 dim.asc = textdim.asc;
229 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
230 dim.wid = max(dim.wid, textdim.wid);
236 mi.base.font = tmpfont;
240 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
242 mouse_hover_[bv] = mouse_hover;
247 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
249 BufferView const & bv = *pi.base.bv;
251 auto_open_[&bv] = bv.cursor().isInside(this);
253 FontInfo tmpfont = pi.base.font;
254 pi.base.font = getLayout().font();
255 pi.base.font.realize(tmpfont);
257 // Draw button first -- top, left or only
258 Dimension dimc = dimensionCollapsed(bv);
260 if (geometry(bv) == TopButton ||
261 geometry(bv) == LeftButton ||
262 geometry(bv) == ButtonOnly) {
263 button_dim.x1 = x + 0;
264 button_dim.x2 = x + dimc.width();
265 button_dim.y1 = y - dimc.asc;
266 button_dim.y2 = y + dimc.des;
268 FontInfo labelfont = getLayout().labelfont();
269 labelfont.setColor(labelColor());
270 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
279 Dimension const textdim = InsetText::dimension(bv);
280 int const baseline = y;
282 switch (geometry(bv)) {
284 textx = x + dimc.width();
286 InsetText::draw(pi, textx, texty);
290 texty = baseline + dimc.des + textdim.asc;
291 InsetText::draw(pi, textx, texty);
298 InsetText::draw(pi, textx, texty);
304 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
305 InsetText::draw(pi, textx, texty);
306 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
308 int desc = textdim.descent();
309 if (geometry(bv) == Corners)
312 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
313 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
314 pi.pain.line(xx1, y + desc - 4,
318 pi.pain.line(xx1, y + desc,
322 // Make status_ value visible:
323 pi.pain.line(xx1, y + desc,
326 pi.pain.line(xx2 - 4, y + desc,
330 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
331 y + desc - 4, labelColor());
333 // the label below the text. Can be toggled.
334 if (geometry(bv) == SubLabel) {
335 FontInfo font(getLayout().labelfont());
336 font.realize(sane_font);
342 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
343 int const ww = max(textdim.wid, w);
344 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
345 buttonLabel(bv), font, Color_none, Color_none);
349 // a visual cue when the cursor is inside the inset
350 Cursor const & cur = bv.cursor();
351 if (cur.isInside(this)) {
354 pi.pain.line(xx1, y + 4, xx1, y, labelColor());
355 pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
356 pi.pain.line(xx2, y + 4, xx2, y,
358 pi.pain.line(xx2 - 4, y, xx2, y,
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 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
418 pos_type const max_length = 15;
419 pos_type const p_siz = paragraphs().begin()->size();
420 pos_type const n = min(max_length, p_siz);
423 for (; i < n && j < p_siz; ++j) {
424 if (paragraphs().begin()->isInset(j))
426 label += paragraphs().begin()->getChar(j);
429 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
432 return label.empty() ? l : label;
436 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
438 //lyxerr << "InsetCollapsable: edit left/right" << endl;
440 InsetText::edit(cur, front, entry_from);
444 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
446 //lyxerr << "InsetCollapsable: edit xy" << endl;
447 if (geometry(cur.bv()) == ButtonOnly
448 || (button_dim.contains(x, y)
449 && geometry(cur.bv()) != NoButton))
452 return InsetText::editXY(cur, x, y);
456 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
458 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
459 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
461 switch (cmd.action()) {
462 case LFUN_MOUSE_PRESS:
463 if (hitButton(cmd)) {
464 switch (cmd.button()) {
465 case mouse_button::button1:
466 case mouse_button::button3:
467 // Pass the command to the enclosing InsetText,
468 // so that the cursor gets set.
471 case mouse_button::none:
472 case mouse_button::button2:
473 case mouse_button::button4:
474 case mouse_button::button5:
476 cur.noScreenUpdate();
479 } else if (geometry(cur.bv()) != ButtonOnly)
480 InsetText::doDispatch(cur, cmd);
485 case LFUN_MOUSE_MOTION:
486 case LFUN_MOUSE_DOUBLE:
487 case LFUN_MOUSE_TRIPLE:
489 cur.noScreenUpdate();
490 else if (geometry(cur.bv()) != ButtonOnly)
491 InsetText::doDispatch(cur, cmd);
496 case LFUN_MOUSE_RELEASE:
497 if (!hitButton(cmd)) {
498 // The mouse click has to be within the inset!
499 if (geometry(cur.bv()) != ButtonOnly)
500 InsetText::doDispatch(cur, cmd);
505 if (cmd.button() != mouse_button::button1) {
507 cur.noScreenUpdate();
510 // if we are selecting, we do not want to
514 // Left button is clicked, the user asks to
515 // toggle the inset visual state.
517 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
518 if (geometry(cur.bv()) == ButtonOnly) {
519 setStatus(cur, Open);
523 setStatus(cur, Collapsed);
524 cur.bv().cursor() = cur;
527 case LFUN_INSET_TOGGLE:
528 if (cmd.argument() == "open")
529 setStatus(cur, Open);
530 else if (cmd.argument() == "close")
531 setStatus(cur, Collapsed);
532 else if (cmd.argument() == "toggle" || cmd.argument().empty())
533 if (status_ == Open) {
534 setStatus(cur, Collapsed);
535 if (geometry(cur.bv()) == ButtonOnly)
536 cur.top().forwardPos();
538 setStatus(cur, Open);
539 else // if assign or anything else
545 InsetText::doDispatch(cur, cmd);
551 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
552 FuncStatus & flag) const
554 switch (cmd.action()) {
555 case LFUN_INSET_TOGGLE:
556 if (cmd.argument() == "open")
557 flag.setEnabled(status_ != Open);
558 else if (cmd.argument() == "close")
559 flag.setEnabled(status_ == Open);
560 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
561 flag.setEnabled(true);
562 flag.setOnOff(status_ == Open);
564 flag.setEnabled(false);
568 return InsetText::getStatus(cur, cmd, flag);
573 void InsetCollapsable::setLabel(docstring const & l)
579 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
581 docstring const label = labelstring_.empty() ?
582 translateIfPossible(getLayout().labelstring()) : labelstring_;
583 InsetLayout const & il = getLayout();
584 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
586 return getNewLabel(label);
590 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
594 if (status_ == Collapsed)
595 cur.leaveInset(*this);
599 docstring InsetCollapsable::floatName(string const & type) const
601 BufferParams const & bp = buffer().params();
602 FloatList const & floats = bp.documentClass().floats();
603 FloatList::const_iterator it = floats[type];
605 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
609 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
611 InsetLayout::InsetDecoration const dec = getLayout().decoration();
612 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
616 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
619 if (decoration() == InsetLayout::CONGLOMERATE)
620 return from_ascii("context-conglomerate");
622 if (geometry(bv) == NoButton)
623 return from_ascii("context-collapsable");
625 Dimension dim = dimensionCollapsed(bv);
626 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
627 return from_ascii("context-collapsable");
629 return InsetText::contextMenu(bv, x, y);