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 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
248 view_[&bv].mouse_hover_);
249 // Draw the change tracking cue on the label, unless RowPainter already
251 if (canPaintChange(bv))
252 pi.change_.paintCue(pi, x, y, x + dimc.width(), labelfont);
254 view_[&bv].button_dim_.x1 = 0;
255 view_[&bv].button_dim_.y1 = 0;
256 view_[&bv].button_dim_.x2 = 0;
257 view_[&bv].button_dim_.y2 = 0;
260 Dimension const textdim = InsetText::dimension(bv);
261 int const baseline = y;
263 Geometry g = geometry(bv);
267 if (g == LeftButton) {
268 textx = x + dimc.width();
272 texty = baseline + dimc.des + textdim.asc;
274 // Do not draw the cue for INSERTED -- it is already in the button and
276 Changer dummy = make_change(pi.change_, Change(),
277 pi.change_.type == Change::INSERTED);
278 InsetText::draw(pi, textx, texty);
286 InsetText::draw(pi, textx, texty);
292 { // We will take care of the frame and the change tracking cue
294 Changer dummy = make_change(pi.change_, Change());
295 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
296 InsetText::draw(pi, textx, texty);
297 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
300 int desc = textdim.descent();
304 // Colour the frame according to the change type. (Like for tables.)
305 Color colour = pi.change_.changed() ? pi.change_.color()
307 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
308 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
309 pi.pain.line(xx1, y + desc - 4,
310 xx1, y + desc, colour);
312 pi.pain.line(xx1, y + desc,
313 xx2, y + desc, colour);
315 // Make status_ value visible:
316 pi.pain.line(xx1, y + desc,
317 xx1 + 4, y + desc, colour);
318 pi.pain.line(xx2 - 4, y + desc,
319 xx2, y + desc, colour);
321 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
322 y + desc - 4, colour);
324 // the label below the text. Can be toggled.
326 FontInfo font(getLabelfont());
327 if (pi.change_.changed())
328 font.setPaintColor(colour);
329 font.realize(sane_font);
335 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
336 int const ww = max(textdim.wid, w);
337 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
338 buttonLabel(bv), font, Color_none, Color_none);
341 int const y1 = y - textdim.asc + 3;
342 // a visual cue when the cursor is inside the inset
343 Cursor const & cur = bv.cursor();
344 if (cur.isInside(this)) {
345 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
346 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
347 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
348 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
350 // Strike through the inset if deleted and not already handled by
352 if (pi.change_.deleted() && canPaintChange(bv))
353 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
359 void InsetCollapsable::cursorPos(BufferView const & bv,
360 CursorSlice const & sl, bool boundary, int & x, int & y) const
362 if (geometry(bv) == ButtonOnly)
365 InsetText::cursorPos(bv, sl, boundary, x, y);
366 Dimension const textdim = InsetText::dimension(bv);
368 switch (geometry(bv)) {
370 x += dimensionCollapsed(bv).wid;
373 y += dimensionCollapsed(bv).des + textdim.asc;
388 bool InsetCollapsable::editable() const
390 switch (decoration()) {
391 case InsetLayout::CLASSIC:
392 case InsetLayout::MINIMALISTIC:
393 return status_ == Open;
400 bool InsetCollapsable::descendable(BufferView const & bv) const
402 return geometry(bv) != ButtonOnly;
406 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
408 return view_[&bv].button_dim_.contains(x, y);
412 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
415 pos_type const max_length = 15;
416 pos_type const p_siz = paragraphs().begin()->size();
417 pos_type const n = min(max_length, p_siz);
420 for (; i < n && j < p_siz; ++j) {
421 if (paragraphs().begin()->isInset(j))
423 label += paragraphs().begin()->getChar(j);
426 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
429 return label.empty() ? l : label;
433 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
435 //lyxerr << "InsetCollapsable: edit left/right" << endl;
437 InsetText::edit(cur, front, entry_from);
441 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
443 //lyxerr << "InsetCollapsable: edit xy" << endl;
444 if (geometry(cur.bv()) == ButtonOnly
445 || (view_[&cur.bv()].button_dim_.contains(x, y)
446 && geometry(cur.bv()) != NoButton))
449 return InsetText::editXY(cur, x, y);
453 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
455 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
456 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
458 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
460 switch (cmd.action()) {
461 case LFUN_MOUSE_PRESS:
463 switch (cmd.button()) {
464 case mouse_button::button1:
465 case mouse_button::button3:
466 // Pass the command to the enclosing InsetText,
467 // so that the cursor gets set.
470 case mouse_button::none:
471 case mouse_button::button2:
472 case mouse_button::button4:
473 case mouse_button::button5:
475 cur.noScreenUpdate();
478 } else if (geometry(cur.bv()) != ButtonOnly)
479 InsetText::doDispatch(cur, cmd);
484 case LFUN_MOUSE_MOTION:
485 case LFUN_MOUSE_DOUBLE:
486 case LFUN_MOUSE_TRIPLE:
488 cur.noScreenUpdate();
489 else if (geometry(cur.bv()) != ButtonOnly)
490 InsetText::doDispatch(cur, cmd);
495 case LFUN_MOUSE_RELEASE:
497 // The mouse click has to be within the inset!
498 if (geometry(cur.bv()) != ButtonOnly)
499 InsetText::doDispatch(cur, cmd);
504 if (cmd.button() != mouse_button::button1) {
506 cur.noScreenUpdate();
509 // if we are selecting, we do not want to
513 // Left button is clicked, the user asks to
514 // toggle the inset visual state.
516 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
517 if (geometry(cur.bv()) == ButtonOnly) {
518 setStatus(cur, Open);
522 setStatus(cur, Collapsed);
523 cur.bv().cursor() = cur;
526 case LFUN_INSET_TOGGLE:
527 if (cmd.argument() == "open")
528 setStatus(cur, Open);
529 else if (cmd.argument() == "close")
530 setStatus(cur, Collapsed);
531 else if (cmd.argument() == "toggle" || cmd.argument().empty())
533 setStatus(cur, Collapsed);
535 setStatus(cur, Open);
536 else // if assign or anything else
542 InsetText::doDispatch(cur, cmd);
548 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
549 FuncStatus & flag) const
551 switch (cmd.action()) {
552 case LFUN_INSET_TOGGLE:
553 if (cmd.argument() == "open")
554 flag.setEnabled(status_ != Open);
555 else if (cmd.argument() == "close")
556 flag.setEnabled(status_ == Open);
557 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
558 flag.setEnabled(true);
559 flag.setOnOff(status_ == Open);
561 flag.setEnabled(false);
565 return InsetText::getStatus(cur, cmd, flag);
570 void InsetCollapsable::setLabel(docstring const & l)
576 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
578 InsetLayout const & il = getLayout();
579 docstring const label = labelstring_.empty() ?
580 translateIfPossible(il.labelstring()) : labelstring_;
581 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
583 return getNewLabel(label);
587 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
591 if (status_ == Collapsed)
592 cur.leaveInset(*this);
596 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
598 InsetLayout::InsetDecoration const dec = getLayout().decoration();
599 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
603 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
606 string context_menu = contextMenuName();
607 string const it_context_menu = InsetText::contextMenuName();
608 if (decoration() == InsetLayout::CONGLOMERATE)
609 return context_menu + ";" + it_context_menu;
611 string const ic_context_menu = InsetCollapsable::contextMenuName();
612 if (ic_context_menu != context_menu)
613 context_menu += ";" + ic_context_menu;
615 if (geometry(bv) == NoButton)
616 return context_menu + ";" + it_context_menu;
618 Dimension dim = dimensionCollapsed(bv);
619 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
622 return it_context_menu;
626 string InsetCollapsable::contextMenuName() const
628 if (decoration() == InsetLayout::CONGLOMERATE)
629 return "context-conglomerate";
631 return "context-collapsable";
635 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
637 // return false to let RowPainter draw the change tracking cue consistently
638 // with the surrounding text, when the inset is inline: for buttons, for
639 // non-allowMultiPar insets.
640 switch (geometry(bv)) {
643 return allowMultiPar();