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"
27 #include "TocBackend.h"
29 #include "frontends/FontMetrics.h"
30 #include "frontends/Painter.h"
32 #include "support/debug.h"
33 #include "support/docstream.h"
34 #include "support/gettext.h"
35 #include "support/lassert.h"
36 #include "support/lstrings.h"
37 #include "support/RefChanger.h"
44 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
45 : InsetText(buf, ltype), status_(Open)
48 setFrameColor(Color_collapsableframe);
52 // The sole purpose of this copy constructor is to make sure
53 // that the view_ map is not copied and remains empty.
54 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
57 labelstring_(rhs.labelstring_)
61 InsetCollapsable::~InsetCollapsable()
63 map<BufferView const *, View>::iterator it = view_.begin();
64 map<BufferView const *, View>::iterator end = view_.end();
65 for (; it != end; ++it)
66 if (it->second.mouse_hover_)
67 it->first->clearLastInset(this);
71 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
73 if (decoration() == InsetLayout::CONGLOMERATE)
75 return view_[&bv].auto_open_ ? 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 view_[&bv].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 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
105 Dimension const dim = dimensionCollapsed(bv);
106 if (geometry(bv) == NoButton)
107 return translateIfPossible(getLayout().labelstring());
108 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
111 return toolTipText();
115 void InsetCollapsable::write(ostream & os) const
131 void InsetCollapsable::read(Lexer & lex)
133 lex.setContext("InsetCollapsable::read");
136 lex >> "status" >> tmp_token;
137 if (tmp_token == "open")
140 InsetText::read(lex);
145 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
148 FontInfo labelfont(getLabelfont());
149 labelfont.realize(sane_font);
150 theFontMetrics(labelfont).buttonText(
151 buttonLabel(bv), dim.wid, dim.asc, dim.des);
156 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
158 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
160 FontInfo tmpfont = mi.base.font;
161 mi.base.font = getFont();
162 mi.base.font.realize(tmpfont);
164 BufferView const & bv = *mi.base.bv;
166 switch (geometry(bv)) {
168 InsetText::metrics(mi, dim);
171 InsetText::metrics(mi, dim);
176 InsetText::metrics(mi, dim);
177 // consider width of the inset label
178 FontInfo font(getLabelfont());
179 font.realize(sane_font);
185 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
192 if (hasFixedWidth()){
193 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
194 if (mi.base.textwidth < mindim)
195 mi.base.textwidth = mindim;
197 dim = dimensionCollapsed(bv);
198 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
200 InsetText::metrics(mi, textdim);
201 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
202 if (view_[&bv].openinlined_) {
203 // Correct for button width.
204 dim.wid += textdim.wid;
205 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
206 dim.asc = textdim.asc;
208 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
209 dim.wid = max(dim.wid, textdim.wid);
215 mi.base.font = tmpfont;
219 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
222 view_[bv].mouse_hover_ = mouse_hover;
227 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
229 BufferView const & bv = *pi.base.bv;
231 view_[&bv].auto_open_ = bv.cursor().isInside(this);
233 Changer dummy = pi.base.font.change(getFont(), true);
235 // Draw button first -- top, left or only
236 Dimension dimc = dimensionCollapsed(bv);
238 if (geometry(bv) == TopButton ||
239 geometry(bv) == LeftButton ||
240 geometry(bv) == ButtonOnly) {
241 view_[&bv].button_dim_.x1 = x + 0;
242 view_[&bv].button_dim_.x2 = x + dimc.width();
243 view_[&bv].button_dim_.y1 = y - dimc.asc;
244 view_[&bv].button_dim_.y2 = y + dimc.des;
246 FontInfo labelfont = getLabelfont();
247 labelfont.setColor(labelColor());
248 labelfont.realize(pi.base.font);
249 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
250 view_[&bv].mouse_hover_);
251 // Draw the change tracking cue on the label, unless RowPainter already
253 if (canPaintChange(bv))
254 pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
256 view_[&bv].button_dim_.x1 = 0;
257 view_[&bv].button_dim_.y1 = 0;
258 view_[&bv].button_dim_.x2 = 0;
259 view_[&bv].button_dim_.y2 = 0;
262 Dimension const textdim = dimensionHelper(bv);
263 int const baseline = y;
265 Geometry g = geometry(bv);
269 if (g == LeftButton) {
270 textx = x + dimc.width();
274 texty = baseline + dimc.des + textdim.asc;
276 // Do not draw the cue for INSERTED -- it is already in the button and
278 Changer dummy = (pi.change_.type == Change::INSERTED)
279 ? make_change(pi.change_, Change())
281 InsetText::draw(pi, textx, texty);
289 InsetText::draw(pi, textx, texty);
295 { // We will take care of the frame and the change tracking cue
297 Changer dummy = make_change(pi.change_, Change());
298 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
299 InsetText::draw(pi, textx, texty);
300 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
303 int desc = textdim.descent();
307 // Colour the frame according to the change type. (Like for tables.)
308 Color colour = pi.change_.changed() ? pi.change_.color()
310 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
311 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
312 pi.pain.line(xx1, y + desc - 4,
313 xx1, y + desc, colour);
315 pi.pain.line(xx1, y + desc,
316 xx2, y + desc, colour);
318 // Make status_ value visible:
319 pi.pain.line(xx1, y + desc,
320 xx1 + 4, y + desc, colour);
321 pi.pain.line(xx2 - 4, y + desc,
322 xx2, y + desc, colour);
324 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
325 y + desc - 4, colour);
327 // the label below the text. Can be toggled.
329 FontInfo font(getLabelfont());
330 if (pi.change_.changed())
331 font.setPaintColor(colour);
332 font.realize(sane_font);
338 Color const col = pi.full_repaint ? Color_none : pi.backgroundColor(this);
339 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
340 int const ww = max(textdim.wid, w);
341 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
342 buttonLabel(bv), font, col, Color_none);
345 int const y1 = y - textdim.asc + 3;
346 // a visual cue when the cursor is inside the inset
347 Cursor const & cur = bv.cursor();
348 if (cur.isInside(this)) {
349 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
350 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
351 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
352 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
354 // Strike through the inset if deleted and not already handled by
356 if (pi.change_.deleted() && canPaintChange(bv))
357 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
363 void InsetCollapsable::cursorPos(BufferView const & bv,
364 CursorSlice const & sl, bool boundary, int & x, int & y) const
366 if (geometry(bv) == ButtonOnly)
369 InsetText::cursorPos(bv, sl, boundary, x, y);
370 Dimension const textdim = dimensionHelper(bv);
372 switch (geometry(bv)) {
374 x += dimensionCollapsed(bv).wid;
377 y += dimensionCollapsed(bv).des + textdim.asc;
392 bool InsetCollapsable::editable() const
394 switch (decoration()) {
395 case InsetLayout::CLASSIC:
396 case InsetLayout::MINIMALISTIC:
397 return status_ == Open;
404 bool InsetCollapsable::descendable(BufferView const & bv) const
406 return geometry(bv) != ButtonOnly;
410 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
412 return view_[&bv].button_dim_.contains(x, y);
416 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
419 pos_type const max_length = 15;
420 pos_type const p_siz = paragraphs().begin()->size();
421 pos_type const n = min(max_length, p_siz);
424 for (; i < n && j < p_siz; ++j) {
425 if (paragraphs().begin()->isInset(j))
427 label += paragraphs().begin()->getChar(j);
430 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
433 return label.empty() ? l : label;
437 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
439 //lyxerr << "InsetCollapsable: edit left/right" << endl;
441 InsetText::edit(cur, front, entry_from);
445 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
447 //lyxerr << "InsetCollapsable: edit xy" << endl;
448 if (geometry(cur.bv()) == ButtonOnly
449 || (view_[&cur.bv()].button_dim_.contains(x, y)
450 && geometry(cur.bv()) != NoButton))
453 return InsetText::editXY(cur, x, y);
457 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
459 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
460 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
462 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
464 switch (cmd.action()) {
465 case LFUN_MOUSE_PRESS:
467 switch (cmd.button()) {
468 case mouse_button::button1:
469 case mouse_button::button3:
470 // Pass the command to the enclosing InsetText,
471 // so that the cursor gets set.
474 case mouse_button::none:
475 case mouse_button::button2:
476 case mouse_button::button4:
477 case mouse_button::button5:
479 cur.noScreenUpdate();
482 } else if (geometry(cur.bv()) != ButtonOnly)
483 InsetText::doDispatch(cur, cmd);
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 InsetCollapsable::getLabel() const
581 InsetLayout const & il = getLayout();
582 return labelstring_.empty() ?
583 translateIfPossible(il.labelstring()) : labelstring_;
587 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
589 InsetLayout const & il = getLayout();
590 docstring const label = getLabel();
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";
645 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
647 // return false to let RowPainter draw the change tracking cue consistently
648 // with the surrounding text, when the inset is inline: for buttons, for
649 // non-allowMultiPar insets.
650 switch (geometry(bv)) {
653 return allowMultiPar();
663 void InsetCollapsable::addToToc(DocIterator const & cpit, bool output_active,
664 UpdateType utype, TocBackend & backend) const
666 bool doing_output = output_active && producesOutput();
667 InsetLayout const & layout = getLayout();
668 if (layout.addToToc()) {
669 TocBuilder & b = backend.builder(layout.tocType());
670 // Cursor inside the inset
671 DocIterator pit = cpit;
672 pit.push_back(CursorSlice(const_cast<InsetCollapsable &>(*this)));
673 docstring const label = getLabel();
674 b.pushItem(pit, label + (label.empty() ? "" : ": "), output_active);
675 // Proceed with the rest of the inset.
676 InsetText::addToToc(cpit, doing_output, utype, backend);
677 if (layout.isTocCaption()) {
679 text().forOutliner(str, TOC_ENTRY_LENGTH);
684 InsetText::addToToc(cpit, doing_output, utype, backend);