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 "BufferView.h"
20 #include "Dimension.h"
21 #include "FuncRequest.h"
22 #include "FuncStatus.h"
23 #include "InsetLayout.h"
25 #include "MetricsInfo.h"
26 #include "OutputParams.h"
28 #include "frontends/FontMetrics.h"
29 #include "frontends/Painter.h"
31 #include "support/debug.h"
32 #include "support/docstream.h"
33 #include "support/gettext.h"
34 #include "support/lassert.h"
35 #include "support/lstrings.h"
42 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
43 : InsetText(buf, ltype), status_(Open), openinlined_(false)
46 setFrameColor(Color_collapsableframe);
50 // The sole purpose of this copy constructor is to make sure
51 // that the mouse_hover_ map is not copied and remains empty.
52 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
55 labelstring_(rhs.labelstring_),
56 button_dim(rhs.button_dim),
57 openinlined_(rhs.openinlined_)
61 InsetCollapsable::~InsetCollapsable()
63 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
64 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
65 for (; it != end; ++it)
67 it->first->clearLastInset(this);
71 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
73 if (decoration() == InsetLayout::CONGLOMERATE)
75 return auto_open_[&bv] ? Open : status_;
79 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
81 switch (decoration()) {
82 case InsetLayout::CLASSIC:
83 if (status(bv) == Open)
84 return openinlined_ ? LeftButton : TopButton;
87 case InsetLayout::MINIMALISTIC:
88 return status(bv) == Open ? NoButton : ButtonOnly ;
90 case InsetLayout::CONGLOMERATE:
91 return status(bv) == Open ? SubLabel : Corners ;
93 case InsetLayout::DEFAULT:
94 break; // this shouldn't happen
97 // dummy return value to shut down a warning,
103 InsetCollapsable::Geometry InsetCollapsable::geometry() const
105 switch (decoration()) {
106 case InsetLayout::CLASSIC:
108 return openinlined_ ? LeftButton : TopButton;
111 case InsetLayout::MINIMALISTIC:
112 return status_ == Open ? NoButton : ButtonOnly ;
114 case InsetLayout::CONGLOMERATE:
115 return status_ == Open ? SubLabel : Corners ;
117 case InsetLayout::DEFAULT:
118 break; // this shouldn't happen
121 // dummy return value to shut down a warning,
122 // this is dead code.
127 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
129 Dimension const dim = dimensionCollapsed(bv);
130 if (geometry(bv) == NoButton)
131 return translateIfPossible(getLayout().labelstring());
132 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
135 return toolTipText();
139 void InsetCollapsable::write(ostream & os) const
155 void InsetCollapsable::read(Lexer & lex)
157 lex.setContext("InsetCollapsable::read");
160 lex >> "status" >> tmp_token;
161 if (tmp_token == "open")
164 InsetText::read(lex);
169 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
172 FontInfo labelfont(getLabelfont());
173 labelfont.realize(sane_font);
174 theFontMetrics(labelfont).buttonText(
175 buttonLabel(bv), dim.wid, dim.asc, dim.des);
180 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
182 auto_open_[mi.base.bv] = mi.base.bv->cursor().isInside(this);
184 FontInfo tmpfont = mi.base.font;
185 mi.base.font = getFont();
186 mi.base.font.realize(tmpfont);
188 BufferView const & bv = *mi.base.bv;
190 switch (geometry(bv)) {
192 InsetText::metrics(mi, dim);
195 InsetText::metrics(mi, dim);
200 InsetText::metrics(mi, dim);
201 // consider width of the inset label
202 FontInfo font(getLabelfont());
203 font.realize(sane_font);
209 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
216 dim = dimensionCollapsed(bv);
217 if (geometry(bv) == TopButton
218 || geometry(bv) == LeftButton) {
220 InsetText::metrics(mi, textdim);
221 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
223 // Correct for button width.
224 dim.wid += textdim.wid;
225 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
226 dim.asc = textdim.asc;
228 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
229 dim.wid = max(dim.wid, textdim.wid);
235 mi.base.font = tmpfont;
239 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 = getFont();
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 = getLabelfont();
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, Color_foreground);
333 // the label below the text. Can be toggled.
334 if (geometry(bv) == SubLabel) {
335 FontInfo font(getLabelfont());
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, Color_foreground);
355 pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
356 pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
357 pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
362 pi.base.font = tmpfont;
366 void InsetCollapsable::cursorPos(BufferView const & bv,
367 CursorSlice const & sl, bool boundary, int & x, int & y) const
369 if (geometry(bv) == ButtonOnly)
372 InsetText::cursorPos(bv, sl, boundary, x, y);
373 Dimension const textdim = InsetText::dimension(bv);
375 switch (geometry(bv)) {
377 x += dimensionCollapsed(bv).wid;
380 y += dimensionCollapsed(bv).des + textdim.asc;
395 bool InsetCollapsable::editable() const
397 return geometry() != ButtonOnly;
401 bool InsetCollapsable::descendable(BufferView const & bv) const
403 return geometry(bv) != ButtonOnly;
407 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
409 return button_dim.contains(cmd.x(), cmd.y());
413 bool InsetCollapsable::clickable(int x, int y) const
415 FuncRequest cmd(LFUN_NOACTION, x, y, mouse_button::none);
416 return hitButton(cmd);
420 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
423 pos_type const max_length = 15;
424 pos_type const p_siz = paragraphs().begin()->size();
425 pos_type const n = min(max_length, p_siz);
428 for (; i < n && j < p_siz; ++j) {
429 if (paragraphs().begin()->isInset(j))
431 label += paragraphs().begin()->getChar(j);
434 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
437 return label.empty() ? l : label;
441 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
443 //lyxerr << "InsetCollapsable: edit left/right" << endl;
445 InsetText::edit(cur, front, entry_from);
449 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
451 //lyxerr << "InsetCollapsable: edit xy" << endl;
452 if (geometry(cur.bv()) == ButtonOnly
453 || (button_dim.contains(x, y)
454 && geometry(cur.bv()) != NoButton))
457 return InsetText::editXY(cur, x, y);
461 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
463 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
464 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
466 switch (cmd.action()) {
467 case LFUN_MOUSE_PRESS:
468 if (hitButton(cmd)) {
469 switch (cmd.button()) {
470 case mouse_button::button1:
471 case mouse_button::button3:
472 // Pass the command to the enclosing InsetText,
473 // so that the cursor gets set.
476 case mouse_button::none:
477 case mouse_button::button2:
478 case mouse_button::button4:
479 case mouse_button::button5:
481 cur.noScreenUpdate();
484 } else if (geometry(cur.bv()) != ButtonOnly)
485 InsetText::doDispatch(cur, cmd);
490 case LFUN_MOUSE_MOTION:
491 case LFUN_MOUSE_DOUBLE:
492 case LFUN_MOUSE_TRIPLE:
494 cur.noScreenUpdate();
495 else if (geometry(cur.bv()) != ButtonOnly)
496 InsetText::doDispatch(cur, cmd);
501 case LFUN_MOUSE_RELEASE:
502 if (!hitButton(cmd)) {
503 // The mouse click has to be within the inset!
504 if (geometry(cur.bv()) != ButtonOnly)
505 InsetText::doDispatch(cur, cmd);
510 if (cmd.button() != mouse_button::button1) {
512 cur.noScreenUpdate();
515 // if we are selecting, we do not want to
519 // Left button is clicked, the user asks to
520 // toggle the inset visual state.
522 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
523 if (geometry(cur.bv()) == ButtonOnly) {
524 setStatus(cur, Open);
528 setStatus(cur, Collapsed);
529 cur.bv().cursor() = cur;
532 case LFUN_INSET_TOGGLE:
533 if (cmd.argument() == "open")
534 setStatus(cur, Open);
535 else if (cmd.argument() == "close")
536 setStatus(cur, Collapsed);
537 else if (cmd.argument() == "toggle" || cmd.argument().empty())
539 setStatus(cur, Collapsed);
541 setStatus(cur, Open);
542 else // if assign or anything else
548 InsetText::doDispatch(cur, cmd);
554 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
555 FuncStatus & flag) const
557 switch (cmd.action()) {
558 case LFUN_INSET_TOGGLE:
559 if (cmd.argument() == "open")
560 flag.setEnabled(status_ != Open);
561 else if (cmd.argument() == "close")
562 flag.setEnabled(status_ == Open);
563 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
564 flag.setEnabled(true);
565 flag.setOnOff(status_ == Open);
567 flag.setEnabled(false);
571 return InsetText::getStatus(cur, cmd, flag);
576 void InsetCollapsable::setLabel(docstring const & l)
582 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
584 InsetLayout const & il = getLayout();
585 docstring const label = labelstring_.empty() ?
586 translateIfPossible(il.labelstring()) : labelstring_;
587 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
589 return getNewLabel(label);
593 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
597 if (status_ == Collapsed)
598 cur.leaveInset(*this);
602 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
604 InsetLayout::InsetDecoration const dec = getLayout().decoration();
605 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
609 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
612 string context_menu = contextMenuName();
613 string const it_context_menu = InsetText::contextMenuName();
614 if (decoration() == InsetLayout::CONGLOMERATE)
615 return context_menu + ";" + it_context_menu;
617 string const ic_context_menu = InsetCollapsable::contextMenuName();
618 if (ic_context_menu != context_menu)
619 context_menu += ";" + ic_context_menu;
621 if (geometry(bv) == NoButton)
622 return context_menu + ";" + it_context_menu;
624 Dimension dim = dimensionCollapsed(bv);
625 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
628 return it_context_menu;
632 string InsetCollapsable::contextMenuName() const
634 if (decoration() == InsetLayout::CONGLOMERATE)
635 return "context-conglomerate";
637 return "context-collapsable";