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::~InsetCollapsable()
68 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
69 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
70 for (; it != end; ++it)
72 it->first->clearLastInset(this);
76 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
78 if (decoration() == InsetLayout::CONGLOMERATE)
80 return auto_open_[&bv] ? Open : status_;
84 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
86 switch (decoration()) {
87 case InsetLayout::CLASSIC:
88 if (status(bv) == Open)
89 return openinlined_ ? LeftButton : TopButton;
92 case InsetLayout::MINIMALISTIC:
93 return status(bv) == Open ? NoButton : ButtonOnly ;
95 case InsetLayout::CONGLOMERATE:
96 return status(bv) == Open ? SubLabel : Corners ;
98 case InsetLayout::DEFAULT:
99 break; // this shouldn't happen
102 // dummy return value to shut down a warning,
103 // this is dead code.
108 InsetCollapsable::Geometry InsetCollapsable::geometry() const
110 switch (decoration()) {
111 case InsetLayout::CLASSIC:
113 return openinlined_ ? LeftButton : TopButton;
116 case InsetLayout::MINIMALISTIC:
117 return status_ == Open ? NoButton : ButtonOnly ;
119 case InsetLayout::CONGLOMERATE:
120 return status_ == Open ? SubLabel : Corners ;
122 case InsetLayout::DEFAULT:
123 break; // this shouldn't happen
126 // dummy return value to shut down a warning,
127 // this is dead code.
132 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
134 Dimension dim = dimensionCollapsed(bv);
135 if (geometry(bv) == NoButton)
136 return translateIfPossible(getLayout().labelstring());
137 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
140 return toolTipText();
144 void InsetCollapsable::write(ostream & os) const
160 void InsetCollapsable::read(Lexer & lex)
162 lex.setContext("InsetCollapsable::read");
165 lex >> "status" >> tmp_token;
166 if (tmp_token == "open")
169 InsetText::read(lex);
174 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
177 theFontMetrics(getLayout().labelfont()).buttonText(
178 buttonLabel(bv), dim.wid, dim.asc, dim.des);
183 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
185 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
187 FontInfo tmpfont = mi.base.font;
188 mi.base.font = getLayout().font();
189 mi.base.font.realize(tmpfont);
191 BufferView const & bv = *mi.base.bv;
193 switch (geometry(bv)) {
195 InsetText::metrics(mi, dim);
198 InsetText::metrics(mi, dim);
203 InsetText::metrics(mi, dim);
204 // consider width of the inset label
205 FontInfo font(getLayout().labelfont());
206 font.realize(sane_font);
212 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
219 dim = dimensionCollapsed(bv);
220 if (geometry(bv) == TopButton
221 || geometry(bv) == LeftButton) {
223 InsetText::metrics(mi, textdim);
224 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
226 // Correct for button width.
227 dim.wid += textdim.wid;
228 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
229 dim.asc = textdim.asc;
231 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
232 dim.wid = max(dim.wid, textdim.wid);
238 mi.base.font = tmpfont;
242 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, labelColor());
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, labelColor());
357 pi.pain.line(xx1 + 4, y, xx1, y, labelColor());
358 pi.pain.line(xx2, y + 4, xx2, y,
360 pi.pain.line(xx2 - 4, y, xx2, y,
366 pi.base.font = tmpfont;
370 void InsetCollapsable::cursorPos(BufferView const & bv,
371 CursorSlice const & sl, bool boundary, int & x, int & y) const
373 if (geometry(bv) == ButtonOnly)
376 InsetText::cursorPos(bv, sl, boundary, x, y);
377 Dimension const textdim = InsetText::dimension(bv);
379 switch (geometry(bv)) {
381 x += dimensionCollapsed(bv).wid;
384 y += dimensionCollapsed(bv).des + textdim.asc;
399 bool InsetCollapsable::editable() const
401 return geometry() != ButtonOnly;
405 bool InsetCollapsable::descendable(BufferView const & bv) const
407 return geometry(bv) != ButtonOnly;
411 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
413 return button_dim.contains(cmd.x(), cmd.y());
417 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
420 pos_type const max_length = 15;
421 pos_type const p_siz = paragraphs().begin()->size();
422 pos_type const n = min(max_length, p_siz);
425 for (; i < n && j < p_siz; ++j) {
426 if (paragraphs().begin()->isInset(j))
428 label += paragraphs().begin()->getChar(j);
431 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
434 return label.empty() ? l : label;
438 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
440 //lyxerr << "InsetCollapsable: edit left/right" << endl;
442 InsetText::edit(cur, front, entry_from);
446 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
448 //lyxerr << "InsetCollapsable: edit xy" << endl;
449 if (geometry(cur.bv()) == ButtonOnly
450 || (button_dim.contains(x, y)
451 && geometry(cur.bv()) != NoButton))
454 return InsetText::editXY(cur, x, y);
458 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
460 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
461 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
463 switch (cmd.action()) {
464 case LFUN_MOUSE_PRESS:
465 if (hitButton(cmd)) {
466 switch (cmd.button()) {
467 case mouse_button::button1:
468 case mouse_button::button3:
469 // Pass the command to the enclosing InsetText,
470 // so that the cursor gets set.
473 case mouse_button::none:
474 case mouse_button::button2:
475 case mouse_button::button4:
476 case mouse_button::button5:
481 } else if (geometry(cur.bv()) != ButtonOnly)
482 InsetText::doDispatch(cur, cmd);
487 case LFUN_MOUSE_MOTION:
488 case LFUN_MOUSE_DOUBLE:
489 case LFUN_MOUSE_TRIPLE:
492 else if (geometry(cur.bv()) != ButtonOnly)
493 InsetText::doDispatch(cur, cmd);
498 case LFUN_MOUSE_RELEASE:
499 if (!hitButton(cmd)) {
500 // The mouse click has to be within the inset!
501 if (geometry(cur.bv()) != ButtonOnly)
502 InsetText::doDispatch(cur, cmd);
507 if (cmd.button() != mouse_button::button1) {
512 // if we are selecting, we do not want to
516 // Left button is clicked, the user asks to
517 // toggle the inset visual state.
519 cur.updateFlags(Update::Force | Update::FitCursor);
520 if (geometry(cur.bv()) == ButtonOnly) {
521 setStatus(cur, Open);
525 setStatus(cur, Collapsed);
526 cur.bv().cursor() = cur;
529 case LFUN_INSET_TOGGLE:
530 if (cmd.argument() == "open")
531 setStatus(cur, Open);
532 else if (cmd.argument() == "close")
533 setStatus(cur, Collapsed);
534 else if (cmd.argument() == "toggle" || cmd.argument().empty())
535 if (status_ == Open) {
536 setStatus(cur, Collapsed);
537 if (geometry(cur.bv()) == ButtonOnly)
538 cur.top().forwardPos();
540 setStatus(cur, Open);
541 else // if assign or anything else
547 InsetText::doDispatch(cur, cmd);
553 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
554 FuncStatus & flag) const
556 switch (cmd.action()) {
557 case LFUN_INSET_TOGGLE:
558 if (cmd.argument() == "open")
559 flag.setEnabled(status_ != Open);
560 else if (cmd.argument() == "close")
561 flag.setEnabled(status_ == Open);
562 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
563 flag.setEnabled(true);
564 flag.setOnOff(status_ == Open);
566 flag.setEnabled(false);
570 return InsetText::getStatus(cur, cmd, flag);
575 void InsetCollapsable::setLabel(docstring const & l)
581 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
583 docstring const label = labelstring_.empty() ?
584 translateIfPossible(getLayout().labelstring()) : labelstring_;
585 InsetLayout const & il = getLayout();
586 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
588 return getNewLabel(label);
592 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
596 if (status_ == Collapsed) {
597 cur.leaveInset(*this);
598 mouse_hover_.clear();
603 docstring InsetCollapsable::floatName(string const & type) const
605 BufferParams const & bp = buffer().params();
606 FloatList const & floats = bp.documentClass().floats();
607 FloatList::const_iterator it = floats[type];
609 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
613 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
615 InsetLayout::InsetDecoration const dec = getLayout().decoration();
616 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
620 docstring InsetCollapsable::contextMenu(BufferView const & bv, int x,
623 if (decoration() == InsetLayout::CONGLOMERATE)
624 return from_ascii("context-conglomerate");
626 if (geometry(bv) == NoButton)
627 return from_ascii("context-collapsable");
629 Dimension dim = dimensionCollapsed(bv);
630 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
631 return from_ascii("context-collapsable");
633 return InsetText::contextMenu(bv, x, y);