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 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
337 int const ww = max(textdim.wid, w);
338 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
339 buttonLabel(bv), font, Color_none, Color_none);
342 int const y1 = y - textdim.asc + 3;
343 // a visual cue when the cursor is inside the inset
344 Cursor const & cur = bv.cursor();
345 if (cur.isInside(this)) {
346 pi.pain.line(xx1, y1 + 4, xx1, y1, colour);
347 pi.pain.line(xx1 + 4, y1, xx1, y1, colour);
348 pi.pain.line(xx2, y1 + 4, xx2, y1, colour);
349 pi.pain.line(xx2 - 4, y1, xx2, y1, colour);
351 // Strike through the inset if deleted and not already handled by
353 if (pi.change_.deleted() && canPaintChange(bv))
354 pi.change_.paintCue(pi, xx1, y1, xx2, y + desc);
360 void InsetCollapsable::cursorPos(BufferView const & bv,
361 CursorSlice const & sl, bool boundary, int & x, int & y) const
363 if (geometry(bv) == ButtonOnly)
366 InsetText::cursorPos(bv, sl, boundary, x, y);
367 Dimension const textdim = InsetText::dimension(bv);
369 switch (geometry(bv)) {
371 x += dimensionCollapsed(bv).wid;
374 y += dimensionCollapsed(bv).des + textdim.asc;
389 bool InsetCollapsable::editable() const
391 switch (decoration()) {
392 case InsetLayout::CLASSIC:
393 case InsetLayout::MINIMALISTIC:
394 return status_ == Open;
401 bool InsetCollapsable::descendable(BufferView const & bv) const
403 return geometry(bv) != ButtonOnly;
407 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
409 return view_[&bv].button_dim_.contains(x, y);
413 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
416 pos_type const max_length = 15;
417 pos_type const p_siz = paragraphs().begin()->size();
418 pos_type const n = min(max_length, p_siz);
421 for (; i < n && j < p_siz; ++j) {
422 if (paragraphs().begin()->isInset(j))
424 label += paragraphs().begin()->getChar(j);
427 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
430 return label.empty() ? l : label;
434 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
436 //lyxerr << "InsetCollapsable: edit left/right" << endl;
438 InsetText::edit(cur, front, entry_from);
442 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
444 //lyxerr << "InsetCollapsable: edit xy" << endl;
445 if (geometry(cur.bv()) == ButtonOnly
446 || (view_[&cur.bv()].button_dim_.contains(x, y)
447 && geometry(cur.bv()) != NoButton))
450 return InsetText::editXY(cur, x, y);
454 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
456 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
457 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
459 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
461 switch (cmd.action()) {
462 case LFUN_MOUSE_PRESS:
464 switch (cmd.button()) {
465 case mouse_button::button1:
466 case mouse_button::button3:
467 // Pass the command to the enclosing InsetText,
468 // so that the cursor gets set.
471 case mouse_button::none:
472 case mouse_button::button2:
473 case mouse_button::button4:
474 case mouse_button::button5:
476 cur.noScreenUpdate();
479 } else if (geometry(cur.bv()) != ButtonOnly)
480 InsetText::doDispatch(cur, cmd);
485 case LFUN_MOUSE_MOTION:
486 case LFUN_MOUSE_DOUBLE:
487 case LFUN_MOUSE_TRIPLE:
489 cur.noScreenUpdate();
490 else if (geometry(cur.bv()) != ButtonOnly)
491 InsetText::doDispatch(cur, cmd);
496 case LFUN_MOUSE_RELEASE:
498 // The mouse click has to be within the inset!
499 if (geometry(cur.bv()) != ButtonOnly)
500 InsetText::doDispatch(cur, cmd);
505 if (cmd.button() != mouse_button::button1) {
507 cur.noScreenUpdate();
510 // if we are selecting, we do not want to
514 // Left button is clicked, the user asks to
515 // toggle the inset visual state.
517 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
518 if (geometry(cur.bv()) == ButtonOnly) {
519 setStatus(cur, Open);
523 setStatus(cur, Collapsed);
524 cur.bv().cursor() = cur;
527 case LFUN_INSET_TOGGLE:
528 if (cmd.argument() == "open")
529 setStatus(cur, Open);
530 else if (cmd.argument() == "close")
531 setStatus(cur, Collapsed);
532 else if (cmd.argument() == "toggle" || cmd.argument().empty())
534 setStatus(cur, Collapsed);
536 setStatus(cur, Open);
537 else // if assign or anything else
543 InsetText::doDispatch(cur, cmd);
549 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
550 FuncStatus & flag) const
552 switch (cmd.action()) {
553 case LFUN_INSET_TOGGLE:
554 if (cmd.argument() == "open")
555 flag.setEnabled(status_ != Open);
556 else if (cmd.argument() == "close")
557 flag.setEnabled(status_ == Open);
558 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
559 flag.setEnabled(true);
560 flag.setOnOff(status_ == Open);
562 flag.setEnabled(false);
566 return InsetText::getStatus(cur, cmd, flag);
571 void InsetCollapsable::setLabel(docstring const & l)
577 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
579 InsetLayout const & il = getLayout();
580 docstring const label = labelstring_.empty() ?
581 translateIfPossible(il.labelstring()) : labelstring_;
582 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
584 return getNewLabel(label);
588 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
592 if (status_ == Collapsed)
593 cur.leaveInset(*this);
597 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
599 InsetLayout::InsetDecoration const dec = getLayout().decoration();
600 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
604 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
607 string context_menu = contextMenuName();
608 string const it_context_menu = InsetText::contextMenuName();
609 if (decoration() == InsetLayout::CONGLOMERATE)
610 return context_menu + ";" + it_context_menu;
612 string const ic_context_menu = InsetCollapsable::contextMenuName();
613 if (ic_context_menu != context_menu)
614 context_menu += ";" + ic_context_menu;
616 if (geometry(bv) == NoButton)
617 return context_menu + ";" + it_context_menu;
619 Dimension dim = dimensionCollapsed(bv);
620 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
623 return it_context_menu;
627 string InsetCollapsable::contextMenuName() const
629 if (decoration() == InsetLayout::CONGLOMERATE)
630 return "context-conglomerate";
632 return "context-collapsable";
636 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
638 // return false to let RowPainter draw the change tracking cue consistently
639 // with the surrounding text, when the inset is inline: for buttons, for
640 // non-allowMultiPar insets.
641 switch (geometry(bv)) {
644 return allowMultiPar();