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"
36 #include "support/RefChanger.h"
43 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
44 : InsetText(buf, ltype), status_(Open)
47 setFrameColor(Color_collapsableframe);
51 // The sole purpose of this copy constructor is to make sure
52 // that the view_ map is not copied and remains empty.
53 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
56 labelstring_(rhs.labelstring_)
60 InsetCollapsable::~InsetCollapsable()
62 map<BufferView const *, View>::iterator it = view_.begin();
63 map<BufferView const *, View>::iterator end = view_.end();
64 for (; it != end; ++it)
65 if (it->second.mouse_hover_)
66 it->first->clearLastInset(this);
70 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
72 if (decoration() == InsetLayout::CONGLOMERATE)
74 return view_[&bv].auto_open_ ? Open : status_;
78 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
80 switch (decoration()) {
81 case InsetLayout::CLASSIC:
82 if (status(bv) == Open)
83 return view_[&bv].openinlined_ ? LeftButton : TopButton;
86 case InsetLayout::MINIMALISTIC:
87 return status(bv) == Open ? NoButton : ButtonOnly ;
89 case InsetLayout::CONGLOMERATE:
90 return status(bv) == Open ? SubLabel : Corners ;
92 case InsetLayout::DEFAULT:
93 break; // this shouldn't happen
96 // dummy return value to shut down a warning,
102 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
104 Dimension const dim = dimensionCollapsed(bv);
105 if (geometry(bv) == NoButton)
106 return translateIfPossible(getLayout().labelstring());
107 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
110 return toolTipText();
114 void InsetCollapsable::write(ostream & os) const
130 void InsetCollapsable::read(Lexer & lex)
132 lex.setContext("InsetCollapsable::read");
135 lex >> "status" >> tmp_token;
136 if (tmp_token == "open")
139 InsetText::read(lex);
144 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
147 FontInfo labelfont(getLabelfont());
148 labelfont.realize(sane_font);
149 theFontMetrics(labelfont).buttonText(
150 buttonLabel(bv), dim.wid, dim.asc, dim.des);
155 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
157 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
159 FontInfo tmpfont = mi.base.font;
160 mi.base.font = getFont();
161 mi.base.font.realize(tmpfont);
163 BufferView const & bv = *mi.base.bv;
165 switch (geometry(bv)) {
167 InsetText::metrics(mi, dim);
170 InsetText::metrics(mi, dim);
175 InsetText::metrics(mi, dim);
176 // consider width of the inset label
177 FontInfo font(getLabelfont());
178 font.realize(sane_font);
184 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
191 if (hasFixedWidth()){
192 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
193 if (mi.base.textwidth < mindim)
194 mi.base.textwidth = mindim;
196 dim = dimensionCollapsed(bv);
197 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
199 InsetText::metrics(mi, textdim);
200 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
201 if (view_[&bv].openinlined_) {
202 // Correct for button width.
203 dim.wid += textdim.wid;
204 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
205 dim.asc = textdim.asc;
207 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
208 dim.wid = max(dim.wid, textdim.wid);
214 mi.base.font = tmpfont;
218 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
221 view_[bv].mouse_hover_ = mouse_hover;
226 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
228 BufferView const & bv = *pi.base.bv;
230 view_[&bv].auto_open_ = bv.cursor().isInside(this);
232 Changer dummy = pi.base.font.change(getFont(), true);
234 // Draw button first -- top, left or only
235 Dimension dimc = dimensionCollapsed(bv);
237 if (geometry(bv) == TopButton ||
238 geometry(bv) == LeftButton ||
239 geometry(bv) == ButtonOnly) {
240 view_[&bv].button_dim_.x1 = x + 0;
241 view_[&bv].button_dim_.x2 = x + dimc.width();
242 view_[&bv].button_dim_.y1 = y - dimc.asc;
243 view_[&bv].button_dim_.y2 = y + dimc.des;
245 FontInfo labelfont = getLabelfont();
246 labelfont.setColor(labelColor());
247 labelfont.realize(pi.base.font);
248 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
249 view_[&bv].mouse_hover_);
250 // Draw the change tracking cue on the label, unless RowPainter already
252 if (canPaintChange(bv))
253 pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
255 view_[&bv].button_dim_.x1 = 0;
256 view_[&bv].button_dim_.y1 = 0;
257 view_[&bv].button_dim_.x2 = 0;
258 view_[&bv].button_dim_.y2 = 0;
261 Dimension const textdim = InsetText::dimension(bv);
262 int const baseline = y;
264 Geometry g = geometry(bv);
268 if (g == LeftButton) {
269 textx = x + dimc.width();
273 texty = baseline + dimc.des + textdim.asc;
275 // Do not draw the cue for INSERTED -- it is already in the button and
277 Changer dummy = make_change(pi.change_, Change(),
278 pi.change_.type == Change::INSERTED);
279 InsetText::draw(pi, textx, texty);
287 InsetText::draw(pi, textx, texty);
293 { // We will take care of the frame and the change tracking cue
295 Changer dummy = make_change(pi.change_, Change());
296 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
297 InsetText::draw(pi, textx, texty);
298 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
301 int desc = textdim.descent();
305 // Colour the frame according to the change type. (Like for tables.)
306 Color colour = pi.change_.changed() ? pi.change_.color()
308 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
309 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
310 pi.pain.line(xx1, y + desc - 4,
311 xx1, y + desc, colour);
313 pi.pain.line(xx1, y + desc,
314 xx2, y + desc, colour);
316 // Make status_ value visible:
317 pi.pain.line(xx1, y + desc,
318 xx1 + 4, y + desc, colour);
319 pi.pain.line(xx2 - 4, y + desc,
320 xx2, y + desc, colour);
322 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
323 y + desc - 4, colour);
325 // the label below the text. Can be toggled.
327 FontInfo font(getLabelfont());
328 if (pi.change_.changed())
329 font.setPaintColor(colour);
330 font.realize(sane_font);
336 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
337 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
338 int const ww = max(textdim.wid, w);
339 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
340 buttonLabel(bv), font, col, Color_none);
343 int const y1 = y - textdim.asc + 3;
344 // a visual cue when the cursor is inside the inset
345 Cursor const & cur = bv.cursor();
346 if (cur.isInside(this)) {
347 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
348 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
349 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
350 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
352 // Strike through the inset if deleted and not already handled by
354 if (pi.change_.deleted() && canPaintChange(bv))
355 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
361 void InsetCollapsable::cursorPos(BufferView const & bv,
362 CursorSlice const & sl, bool boundary, int & x, int & y) const
364 if (geometry(bv) == ButtonOnly)
367 InsetText::cursorPos(bv, sl, boundary, x, y);
368 Dimension const textdim = InsetText::dimension(bv);
370 switch (geometry(bv)) {
372 x += dimensionCollapsed(bv).wid;
375 y += dimensionCollapsed(bv).des + textdim.asc;
390 bool InsetCollapsable::editable() const
392 switch (decoration()) {
393 case InsetLayout::CLASSIC:
394 case InsetLayout::MINIMALISTIC:
395 return status_ == Open;
402 bool InsetCollapsable::descendable(BufferView const & bv) const
404 return geometry(bv) != ButtonOnly;
408 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
410 return view_[&bv].button_dim_.contains(x, y);
414 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
417 pos_type const max_length = 15;
418 pos_type const p_siz = paragraphs().begin()->size();
419 pos_type const n = min(max_length, p_siz);
422 for (; i < n && j < p_siz; ++j) {
423 if (paragraphs().begin()->isInset(j))
425 label += paragraphs().begin()->getChar(j);
428 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
431 return label.empty() ? l : label;
435 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
437 //lyxerr << "InsetCollapsable: edit left/right" << endl;
439 InsetText::edit(cur, front, entry_from);
443 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
445 //lyxerr << "InsetCollapsable: edit xy" << endl;
446 if (geometry(cur.bv()) == ButtonOnly
447 || (view_[&cur.bv()].button_dim_.contains(x, y)
448 && geometry(cur.bv()) != NoButton))
451 return InsetText::editXY(cur, x, y);
455 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
457 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
458 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
460 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
462 switch (cmd.action()) {
463 case LFUN_MOUSE_PRESS:
465 switch (cmd.button()) {
466 case mouse_button::button1:
467 case mouse_button::button3:
468 // Pass the command to the enclosing InsetText,
469 // so that the cursor gets set.
472 case mouse_button::none:
473 case mouse_button::button2:
474 case mouse_button::button4:
475 case mouse_button::button5:
477 cur.noScreenUpdate();
480 } else if (geometry(cur.bv()) != ButtonOnly)
481 InsetText::doDispatch(cur, cmd);
486 case LFUN_MOUSE_MOTION:
487 case LFUN_MOUSE_DOUBLE:
488 case LFUN_MOUSE_TRIPLE:
490 cur.noScreenUpdate();
491 else if (geometry(cur.bv()) != ButtonOnly)
492 InsetText::doDispatch(cur, cmd);
497 case LFUN_MOUSE_RELEASE:
499 // The mouse click has to be within the inset!
500 if (geometry(cur.bv()) != ButtonOnly)
501 InsetText::doDispatch(cur, cmd);
506 if (cmd.button() != mouse_button::button1) {
508 cur.noScreenUpdate();
511 // if we are selecting, we do not want to
515 // Left button is clicked, the user asks to
516 // toggle the inset visual state.
518 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
519 if (geometry(cur.bv()) == ButtonOnly) {
520 setStatus(cur, Open);
524 setStatus(cur, Collapsed);
525 cur.bv().cursor() = cur;
528 case LFUN_INSET_TOGGLE:
529 if (cmd.argument() == "open")
530 setStatus(cur, Open);
531 else if (cmd.argument() == "close")
532 setStatus(cur, Collapsed);
533 else if (cmd.argument() == "toggle" || cmd.argument().empty())
535 setStatus(cur, Collapsed);
537 setStatus(cur, Open);
538 else // if assign or anything else
544 InsetText::doDispatch(cur, cmd);
550 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
551 FuncStatus & flag) const
553 switch (cmd.action()) {
554 case LFUN_INSET_TOGGLE:
555 if (cmd.argument() == "open")
556 flag.setEnabled(status_ != Open);
557 else if (cmd.argument() == "close")
558 flag.setEnabled(status_ == Open);
559 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
560 flag.setEnabled(true);
561 flag.setOnOff(status_ == Open);
563 flag.setEnabled(false);
567 return InsetText::getStatus(cur, cmd, flag);
572 void InsetCollapsable::setLabel(docstring const & l)
578 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
580 InsetLayout const & il = getLayout();
581 docstring const label = labelstring_.empty() ?
582 translateIfPossible(il.labelstring()) : labelstring_;
583 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
585 return getNewLabel(label);
589 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
593 if (status_ == Collapsed)
594 cur.leaveInset(*this);
598 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
600 InsetLayout::InsetDecoration const dec = getLayout().decoration();
601 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
605 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
608 string context_menu = contextMenuName();
609 string const it_context_menu = InsetText::contextMenuName();
610 if (decoration() == InsetLayout::CONGLOMERATE)
611 return context_menu + ";" + it_context_menu;
613 string const ic_context_menu = InsetCollapsable::contextMenuName();
614 if (ic_context_menu != context_menu)
615 context_menu += ";" + ic_context_menu;
617 if (geometry(bv) == NoButton)
618 return context_menu + ";" + it_context_menu;
620 Dimension dim = dimensionCollapsed(bv);
621 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
624 return it_context_menu;
628 string InsetCollapsable::contextMenuName() const
630 if (decoration() == InsetLayout::CONGLOMERATE)
631 return "context-conglomerate";
633 return "context-collapsable";
637 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
639 // return false to let RowPainter draw the change tracking cue consistently
640 // with the surrounding text, when the inset is inline: for buttons, for
641 // non-allowMultiPar insets.
642 switch (geometry(bv)) {
645 return allowMultiPar();