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 = (pi.change_.type == Change::INSERTED)
278 ? make_change(pi.change_, Change())
280 InsetText::draw(pi, textx, texty);
288 InsetText::draw(pi, textx, texty);
294 { // We will take care of the frame and the change tracking cue
296 Changer dummy = make_change(pi.change_, Change());
297 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
298 InsetText::draw(pi, textx, texty);
299 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
302 int desc = textdim.descent();
306 // Colour the frame according to the change type. (Like for tables.)
307 Color colour = pi.change_.changed() ? pi.change_.color()
309 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
310 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
311 pi.pain.line(xx1, y + desc - 4,
312 xx1, y + desc, colour);
314 pi.pain.line(xx1, y + desc,
315 xx2, y + desc, colour);
317 // Make status_ value visible:
318 pi.pain.line(xx1, y + desc,
319 xx1 + 4, y + desc, colour);
320 pi.pain.line(xx2 - 4, y + desc,
321 xx2, y + desc, colour);
323 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
324 y + desc - 4, colour);
326 // the label below the text. Can be toggled.
328 FontInfo font(getLabelfont());
329 if (pi.change_.changed())
330 font.setPaintColor(colour);
331 font.realize(sane_font);
337 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
338 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
339 int const ww = max(textdim.wid, w);
340 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
341 buttonLabel(bv), font, col, Color_none);
344 int const y1 = y - textdim.asc + 3;
345 // a visual cue when the cursor is inside the inset
346 Cursor const & cur = bv.cursor();
347 if (cur.isInside(this)) {
348 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
349 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
350 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
351 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
353 // Strike through the inset if deleted and not already handled by
355 if (pi.change_.deleted() && canPaintChange(bv))
356 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
362 void InsetCollapsable::cursorPos(BufferView const & bv,
363 CursorSlice const & sl, bool boundary, int & x, int & y) const
365 if (geometry(bv) == ButtonOnly)
368 InsetText::cursorPos(bv, sl, boundary, x, y);
369 Dimension const textdim = InsetText::dimension(bv);
371 switch (geometry(bv)) {
373 x += dimensionCollapsed(bv).wid;
376 y += dimensionCollapsed(bv).des + textdim.asc;
391 bool InsetCollapsable::editable() const
393 switch (decoration()) {
394 case InsetLayout::CLASSIC:
395 case InsetLayout::MINIMALISTIC:
396 return status_ == Open;
403 bool InsetCollapsable::descendable(BufferView const & bv) const
405 return geometry(bv) != ButtonOnly;
409 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
411 return view_[&bv].button_dim_.contains(x, y);
415 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
418 pos_type const max_length = 15;
419 pos_type const p_siz = paragraphs().begin()->size();
420 pos_type const n = min(max_length, p_siz);
423 for (; i < n && j < p_siz; ++j) {
424 if (paragraphs().begin()->isInset(j))
426 label += paragraphs().begin()->getChar(j);
429 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
432 return label.empty() ? l : label;
436 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
438 //lyxerr << "InsetCollapsable: edit left/right" << endl;
440 InsetText::edit(cur, front, entry_from);
444 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
446 //lyxerr << "InsetCollapsable: edit xy" << endl;
447 if (geometry(cur.bv()) == ButtonOnly
448 || (view_[&cur.bv()].button_dim_.contains(x, y)
449 && geometry(cur.bv()) != NoButton))
452 return InsetText::editXY(cur, x, y);
456 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
458 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
459 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
461 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
463 switch (cmd.action()) {
464 case LFUN_MOUSE_PRESS:
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:
478 cur.noScreenUpdate();
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:
491 cur.noScreenUpdate();
492 else if (geometry(cur.bv()) != ButtonOnly)
493 InsetText::doDispatch(cur, cmd);
498 case LFUN_MOUSE_RELEASE:
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) {
509 cur.noScreenUpdate();
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.screenUpdateFlags(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())
536 setStatus(cur, Collapsed);
538 setStatus(cur, Open);
539 else // if assign or anything else
545 InsetText::doDispatch(cur, cmd);
551 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
552 FuncStatus & flag) const
554 switch (cmd.action()) {
555 case LFUN_INSET_TOGGLE:
556 if (cmd.argument() == "open")
557 flag.setEnabled(status_ != Open);
558 else if (cmd.argument() == "close")
559 flag.setEnabled(status_ == Open);
560 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
561 flag.setEnabled(true);
562 flag.setOnOff(status_ == Open);
564 flag.setEnabled(false);
568 return InsetText::getStatus(cur, cmd, flag);
573 void InsetCollapsable::setLabel(docstring const & l)
579 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
581 InsetLayout const & il = getLayout();
582 docstring const label = labelstring_.empty() ?
583 translateIfPossible(il.labelstring()) : labelstring_;
584 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
586 return getNewLabel(label);
590 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
594 if (status_ == Collapsed)
595 cur.leaveInset(*this);
599 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
601 InsetLayout::InsetDecoration const dec = getLayout().decoration();
602 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
606 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
609 string context_menu = contextMenuName();
610 string const it_context_menu = InsetText::contextMenuName();
611 if (decoration() == InsetLayout::CONGLOMERATE)
612 return context_menu + ";" + it_context_menu;
614 string const ic_context_menu = InsetCollapsable::contextMenuName();
615 if (ic_context_menu != context_menu)
616 context_menu += ";" + ic_context_menu;
618 if (geometry(bv) == NoButton)
619 return context_menu + ";" + it_context_menu;
621 Dimension dim = dimensionCollapsed(bv);
622 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
625 return it_context_menu;
629 string InsetCollapsable::contextMenuName() const
631 if (decoration() == InsetLayout::CONGLOMERATE)
632 return "context-conglomerate";
634 return "context-collapsable";
638 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
640 // return false to let RowPainter draw the change tracking cue consistently
641 // with the surrounding text, when the inset is inline: for buttons, for
642 // non-allowMultiPar insets.
643 switch (geometry(bv)) {
646 return allowMultiPar();