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 if (hasFixedWidth()){
217 int const mindim = button_dim.x2 - button_dim.x1;
218 if (mi.base.textwidth < mindim)
219 mi.base.textwidth = mindim;
221 dim = dimensionCollapsed(bv);
222 if (geometry(bv) == TopButton
223 || geometry(bv) == LeftButton) {
225 InsetText::metrics(mi, textdim);
226 openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
228 // Correct for button width.
229 dim.wid += textdim.wid;
230 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
231 dim.asc = textdim.asc;
233 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
234 dim.wid = max(dim.wid, textdim.wid);
240 mi.base.font = tmpfont;
244 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
247 mouse_hover_[bv] = mouse_hover;
252 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
254 BufferView const & bv = *pi.base.bv;
256 auto_open_[&bv] = bv.cursor().isInside(this);
258 FontInfo tmpfont = pi.base.font;
259 pi.base.font = getFont();
260 pi.base.font.realize(tmpfont);
262 // Draw button first -- top, left or only
263 Dimension dimc = dimensionCollapsed(bv);
265 if (geometry(bv) == TopButton ||
266 geometry(bv) == LeftButton ||
267 geometry(bv) == ButtonOnly) {
268 button_dim.x1 = x + 0;
269 button_dim.x2 = x + dimc.width();
270 button_dim.y1 = y - dimc.asc;
271 button_dim.y2 = y + dimc.des;
273 FontInfo labelfont = getLabelfont();
274 labelfont.setColor(labelColor());
275 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
284 Dimension const textdim = InsetText::dimension(bv);
285 int const baseline = y;
287 switch (geometry(bv)) {
289 textx = x + dimc.width();
291 InsetText::draw(pi, textx, texty);
295 texty = baseline + dimc.des + textdim.asc;
296 InsetText::draw(pi, textx, texty);
303 InsetText::draw(pi, textx, texty);
309 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
310 InsetText::draw(pi, textx, texty);
311 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
313 int desc = textdim.descent();
314 if (geometry(bv) == Corners)
317 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
318 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
319 pi.pain.line(xx1, y + desc - 4,
323 pi.pain.line(xx1, y + desc,
327 // Make status_ value visible:
328 pi.pain.line(xx1, y + desc,
331 pi.pain.line(xx2 - 4, y + desc,
335 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
336 y + desc - 4, Color_foreground);
338 // the label below the text. Can be toggled.
339 if (geometry(bv) == SubLabel) {
340 FontInfo font(getLabelfont());
341 font.realize(sane_font);
347 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
348 int const ww = max(textdim.wid, w);
349 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
350 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 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
608 InsetLayout::InsetDecoration const dec = getLayout().decoration();
609 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
613 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
616 string context_menu = contextMenuName();
617 string const it_context_menu = InsetText::contextMenuName();
618 if (decoration() == InsetLayout::CONGLOMERATE)
619 return context_menu + ";" + it_context_menu;
621 string const ic_context_menu = InsetCollapsable::contextMenuName();
622 if (ic_context_menu != context_menu)
623 context_menu += ";" + ic_context_menu;
625 if (geometry(bv) == NoButton)
626 return context_menu + ";" + it_context_menu;
628 Dimension dim = dimensionCollapsed(bv);
629 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
632 return it_context_menu;
636 string InsetCollapsable::contextMenuName() const
638 if (decoration() == InsetLayout::CONGLOMERATE)
639 return "context-conglomerate";
641 return "context-collapsable";