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 const 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 FontInfo labelfont(getLabelfont());
177 labelfont.realize(sane_font);
178 theFontMetrics(labelfont).buttonText(
179 buttonLabel(bv), dim.wid, dim.asc, dim.des);
184 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
186 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
188 FontInfo tmpfont = mi.base.font;
189 mi.base.font = getFont();
190 mi.base.font.realize(tmpfont);
192 BufferView const & bv = *mi.base.bv;
194 switch (geometry(bv)) {
196 InsetText::metrics(mi, dim);
199 InsetText::metrics(mi, dim);
204 InsetText::metrics(mi, dim);
205 // consider width of the inset label
206 FontInfo font(getLabelfont());
207 font.realize(sane_font);
213 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
220 dim = dimensionCollapsed(bv);
221 if (geometry(bv) == TopButton
222 || geometry(bv) == LeftButton) {
224 InsetText::metrics(mi, textdim);
225 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
227 // Correct for button width.
228 dim.wid += textdim.wid;
229 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
230 dim.asc = textdim.asc;
232 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
233 dim.wid = max(dim.wid, textdim.wid);
239 mi.base.font = tmpfont;
243 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
246 mouse_hover_[bv] = mouse_hover;
251 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
253 BufferView const & bv = *pi.base.bv;
255 auto_open_[&bv] = bv.cursor().isInside(this);
257 FontInfo tmpfont = pi.base.font;
258 pi.base.font = getFont();
259 pi.base.font.realize(tmpfont);
261 // Draw button first -- top, left or only
262 Dimension dimc = dimensionCollapsed(bv);
264 if (geometry(bv) == TopButton ||
265 geometry(bv) == LeftButton ||
266 geometry(bv) == ButtonOnly) {
267 button_dim.x1 = x + 0;
268 button_dim.x2 = x + dimc.width();
269 button_dim.y1 = y - dimc.asc;
270 button_dim.y2 = y + dimc.des;
272 FontInfo labelfont = getLabelfont();
273 labelfont.setColor(labelColor());
274 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
283 Dimension const textdim = InsetText::dimension(bv);
284 int const baseline = y;
286 switch (geometry(bv)) {
288 textx = x + dimc.width();
290 InsetText::draw(pi, textx, texty);
294 texty = baseline + dimc.des + textdim.asc;
295 InsetText::draw(pi, textx, texty);
302 InsetText::draw(pi, textx, texty);
308 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
309 InsetText::draw(pi, textx, texty);
310 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
312 int desc = textdim.descent();
313 if (geometry(bv) == Corners)
316 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
317 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
318 pi.pain.line(xx1, y + desc - 4,
322 pi.pain.line(xx1, y + desc,
326 // Make status_ value visible:
327 pi.pain.line(xx1, y + desc,
330 pi.pain.line(xx2 - 4, y + desc,
334 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
335 y + desc - 4, Color_foreground);
337 // the label below the text. Can be toggled.
338 if (geometry(bv) == SubLabel) {
339 FontInfo font(getLabelfont());
340 font.realize(sane_font);
346 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
347 int const ww = max(textdim.wid, w);
348 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
349 buttonLabel(bv), font, Color_none, Color_none);
353 // a visual cue when the cursor is inside the inset
354 Cursor const & cur = bv.cursor();
355 if (cur.isInside(this)) {
358 pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
359 pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
360 pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
361 pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
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 bool InsetCollapsable::clickable(int x, int y) const
419 FuncRequest cmd(LFUN_NOACTION, x, y, mouse_button::none);
420 return hitButton(cmd);
424 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
427 pos_type const max_length = 15;
428 pos_type const p_siz = paragraphs().begin()->size();
429 pos_type const n = min(max_length, p_siz);
432 for (; i < n && j < p_siz; ++j) {
433 if (paragraphs().begin()->isInset(j))
435 label += paragraphs().begin()->getChar(j);
438 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
441 return label.empty() ? l : label;
445 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
447 //lyxerr << "InsetCollapsable: edit left/right" << endl;
449 InsetText::edit(cur, front, entry_from);
453 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
455 //lyxerr << "InsetCollapsable: edit xy" << endl;
456 if (geometry(cur.bv()) == ButtonOnly
457 || (button_dim.contains(x, y)
458 && geometry(cur.bv()) != NoButton))
461 return InsetText::editXY(cur, x, y);
465 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
467 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
468 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
470 switch (cmd.action()) {
471 case LFUN_MOUSE_PRESS:
472 if (hitButton(cmd)) {
473 switch (cmd.button()) {
474 case mouse_button::button1:
475 case mouse_button::button3:
476 // Pass the command to the enclosing InsetText,
477 // so that the cursor gets set.
480 case mouse_button::none:
481 case mouse_button::button2:
482 case mouse_button::button4:
483 case mouse_button::button5:
485 cur.noScreenUpdate();
488 } else if (geometry(cur.bv()) != ButtonOnly)
489 InsetText::doDispatch(cur, cmd);
494 case LFUN_MOUSE_MOTION:
495 case LFUN_MOUSE_DOUBLE:
496 case LFUN_MOUSE_TRIPLE:
498 cur.noScreenUpdate();
499 else if (geometry(cur.bv()) != ButtonOnly)
500 InsetText::doDispatch(cur, cmd);
505 case LFUN_MOUSE_RELEASE:
506 if (!hitButton(cmd)) {
507 // The mouse click has to be within the inset!
508 if (geometry(cur.bv()) != ButtonOnly)
509 InsetText::doDispatch(cur, cmd);
514 if (cmd.button() != mouse_button::button1) {
516 cur.noScreenUpdate();
519 // if we are selecting, we do not want to
523 // Left button is clicked, the user asks to
524 // toggle the inset visual state.
526 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
527 if (geometry(cur.bv()) == ButtonOnly) {
528 setStatus(cur, Open);
532 setStatus(cur, Collapsed);
533 cur.bv().cursor() = cur;
536 case LFUN_INSET_TOGGLE:
537 if (cmd.argument() == "open")
538 setStatus(cur, Open);
539 else if (cmd.argument() == "close")
540 setStatus(cur, Collapsed);
541 else if (cmd.argument() == "toggle" || cmd.argument().empty())
543 setStatus(cur, Collapsed);
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 InsetLayout const & il = getLayout();
589 docstring const label = labelstring_.empty() ?
590 translateIfPossible(il.labelstring()) : labelstring_;
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 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
626 string context_menu = contextMenuName();
627 string const it_context_menu = InsetText::contextMenuName();
628 if (decoration() == InsetLayout::CONGLOMERATE)
629 return context_menu + ";" + it_context_menu;
631 string const ic_context_menu = InsetCollapsable::contextMenuName();
632 if (ic_context_menu != context_menu)
633 context_menu += ";" + ic_context_menu;
635 if (geometry(bv) == NoButton)
636 return context_menu + ";" + it_context_menu;
638 Dimension dim = dimensionCollapsed(bv);
639 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
642 return it_context_menu;
646 string InsetCollapsable::contextMenuName() const
648 if (decoration() == InsetLayout::CONGLOMERATE)
649 return "context-conglomerate";
651 return "context-collapsable";