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"
42 InsetCollapsable::InsetCollapsable(Buffer * buf, InsetText::UsePlain ltype)
43 : InsetText(buf, ltype), status_(Open)
46 setFrameColor(Color_collapsableframe);
50 // The sole purpose of this copy constructor is to make sure
51 // that the view_ map is not copied and remains empty.
52 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
55 labelstring_(rhs.labelstring_)
59 InsetCollapsable::~InsetCollapsable()
61 map<BufferView const *, View>::iterator it = view_.begin();
62 map<BufferView const *, View>::iterator end = view_.end();
63 for (; it != end; ++it)
64 if (it->second.mouse_hover_)
65 it->first->clearLastInset(this);
69 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
71 if (decoration() == InsetLayout::CONGLOMERATE)
73 return view_[&bv].auto_open_ ? Open : status_;
77 InsetCollapsable::Geometry InsetCollapsable::geometry(BufferView const & bv) const
79 switch (decoration()) {
80 case InsetLayout::CLASSIC:
81 if (status(bv) == Open)
82 return view_[&bv].openinlined_ ? LeftButton : TopButton;
85 case InsetLayout::MINIMALISTIC:
86 return status(bv) == Open ? NoButton : ButtonOnly ;
88 case InsetLayout::CONGLOMERATE:
89 return status(bv) == Open ? SubLabel : Corners ;
91 case InsetLayout::DEFAULT:
92 break; // this shouldn't happen
95 // dummy return value to shut down a warning,
101 docstring InsetCollapsable::toolTip(BufferView const & bv, int x, int y) const
103 Dimension const dim = dimensionCollapsed(bv);
104 if (geometry(bv) == NoButton)
105 return translateIfPossible(getLayout().labelstring());
106 if (x > xo(bv) + dim.wid || y > yo(bv) + dim.des || isOpen(bv))
109 return toolTipText();
113 void InsetCollapsable::write(ostream & os) const
129 void InsetCollapsable::read(Lexer & lex)
131 lex.setContext("InsetCollapsable::read");
134 lex >> "status" >> tmp_token;
135 if (tmp_token == "open")
138 InsetText::read(lex);
143 Dimension InsetCollapsable::dimensionCollapsed(BufferView const & bv) const
146 FontInfo labelfont(getLabelfont());
147 labelfont.realize(sane_font);
148 theFontMetrics(labelfont).buttonText(
149 buttonLabel(bv), dim.wid, dim.asc, dim.des);
154 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
156 view_[mi.base.bv].auto_open_ = mi.base.bv->cursor().isInside(this);
158 FontInfo tmpfont = mi.base.font;
159 mi.base.font = getFont();
160 mi.base.font.realize(tmpfont);
162 BufferView const & bv = *mi.base.bv;
164 switch (geometry(bv)) {
166 InsetText::metrics(mi, dim);
169 InsetText::metrics(mi, dim);
174 InsetText::metrics(mi, dim);
175 // consider width of the inset label
176 FontInfo font(getLabelfont());
177 font.realize(sane_font);
183 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
190 if (hasFixedWidth()){
191 int const mindim = view_[&bv].button_dim_.x2 - view_[&bv].button_dim_.x1;
192 if (mi.base.textwidth < mindim)
193 mi.base.textwidth = mindim;
195 dim = dimensionCollapsed(bv);
196 if (geometry(bv) == TopButton || geometry(bv) == LeftButton) {
198 InsetText::metrics(mi, textdim);
199 view_[&bv].openinlined_ = (textdim.wid + dim.wid) < mi.base.textwidth;
200 if (view_[&bv].openinlined_) {
201 // Correct for button width.
202 dim.wid += textdim.wid;
203 dim.des = max(dim.des - textdim.asc + dim.asc, textdim.des);
204 dim.asc = textdim.asc;
206 dim.des += textdim.height() + TEXT_TO_INSET_OFFSET;
207 dim.wid = max(dim.wid, textdim.wid);
213 mi.base.font = tmpfont;
217 bool InsetCollapsable::setMouseHover(BufferView const * bv, bool mouse_hover)
220 view_[bv].mouse_hover_ = mouse_hover;
225 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
227 BufferView const & bv = *pi.base.bv;
229 view_[&bv].auto_open_ = bv.cursor().isInside(this);
231 FontInfo tmpfont = pi.base.font;
232 pi.base.font = getFont();
233 pi.base.font.realize(tmpfont);
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 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
249 view_[&bv].mouse_hover_);
251 view_[&bv].button_dim_.x1 = 0;
252 view_[&bv].button_dim_.y1 = 0;
253 view_[&bv].button_dim_.x2 = 0;
254 view_[&bv].button_dim_.y2 = 0;
257 Dimension const textdim = InsetText::dimension(bv);
258 int const baseline = y;
260 switch (geometry(bv)) {
262 textx = x + dimc.width();
264 InsetText::draw(pi, textx, texty);
268 texty = baseline + dimc.des + textdim.asc;
269 InsetText::draw(pi, textx, texty);
276 InsetText::draw(pi, textx, texty);
282 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
283 InsetText::draw(pi, textx, texty);
284 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
286 int desc = textdim.descent();
287 if (geometry(bv) == Corners)
290 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
291 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
292 pi.pain.line(xx1, y + desc - 4,
296 pi.pain.line(xx1, y + desc,
300 // Make status_ value visible:
301 pi.pain.line(xx1, y + desc,
304 pi.pain.line(xx2 - 4, y + desc,
308 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
309 y + desc - 4, Color_foreground);
311 // the label below the text. Can be toggled.
312 if (geometry(bv) == SubLabel) {
313 FontInfo font(getLabelfont());
314 font.realize(sane_font);
320 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
321 int const ww = max(textdim.wid, w);
322 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
323 buttonLabel(bv), font, Color_none, Color_none);
326 // a visual cue when the cursor is inside the inset
327 Cursor const & cur = bv.cursor();
328 if (cur.isInside(this)) {
331 pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
332 pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
333 pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
334 pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
339 pi.base.font = tmpfont;
343 void InsetCollapsable::cursorPos(BufferView const & bv,
344 CursorSlice const & sl, bool boundary, int & x, int & y) const
346 if (geometry(bv) == ButtonOnly)
349 InsetText::cursorPos(bv, sl, boundary, x, y);
350 Dimension const textdim = InsetText::dimension(bv);
352 switch (geometry(bv)) {
354 x += dimensionCollapsed(bv).wid;
357 y += dimensionCollapsed(bv).des + textdim.asc;
372 bool InsetCollapsable::editable() const
374 switch (decoration()) {
375 case InsetLayout::CLASSIC:
376 case InsetLayout::MINIMALISTIC:
377 return status_ == Open;
384 bool InsetCollapsable::descendable(BufferView const & bv) const
386 return geometry(bv) != ButtonOnly;
390 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
392 return view_[&bv].button_dim_.contains(x, y);
396 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
399 pos_type const max_length = 15;
400 pos_type const p_siz = paragraphs().begin()->size();
401 pos_type const n = min(max_length, p_siz);
404 for (; i < n && j < p_siz; ++j) {
405 if (paragraphs().begin()->isInset(j))
407 label += paragraphs().begin()->getChar(j);
410 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
413 return label.empty() ? l : label;
417 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
419 //lyxerr << "InsetCollapsable: edit left/right" << endl;
421 InsetText::edit(cur, front, entry_from);
425 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
427 //lyxerr << "InsetCollapsable: edit xy" << endl;
428 if (geometry(cur.bv()) == ButtonOnly
429 || (view_[&cur.bv()].button_dim_.contains(x, y)
430 && geometry(cur.bv()) != NoButton))
433 return InsetText::editXY(cur, x, y);
437 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
439 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
440 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
442 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
444 switch (cmd.action()) {
445 case LFUN_MOUSE_PRESS:
447 switch (cmd.button()) {
448 case mouse_button::button1:
449 case mouse_button::button3:
450 // Pass the command to the enclosing InsetText,
451 // so that the cursor gets set.
454 case mouse_button::none:
455 case mouse_button::button2:
456 case mouse_button::button4:
457 case mouse_button::button5:
459 cur.noScreenUpdate();
462 } else if (geometry(cur.bv()) != ButtonOnly)
463 InsetText::doDispatch(cur, cmd);
468 case LFUN_MOUSE_MOTION:
469 case LFUN_MOUSE_DOUBLE:
470 case LFUN_MOUSE_TRIPLE:
472 cur.noScreenUpdate();
473 else if (geometry(cur.bv()) != ButtonOnly)
474 InsetText::doDispatch(cur, cmd);
479 case LFUN_MOUSE_RELEASE:
481 // The mouse click has to be within the inset!
482 if (geometry(cur.bv()) != ButtonOnly)
483 InsetText::doDispatch(cur, cmd);
488 if (cmd.button() != mouse_button::button1) {
490 cur.noScreenUpdate();
493 // if we are selecting, we do not want to
497 // Left button is clicked, the user asks to
498 // toggle the inset visual state.
500 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
501 if (geometry(cur.bv()) == ButtonOnly) {
502 setStatus(cur, Open);
506 setStatus(cur, Collapsed);
507 cur.bv().cursor() = cur;
510 case LFUN_INSET_TOGGLE:
511 if (cmd.argument() == "open")
512 setStatus(cur, Open);
513 else if (cmd.argument() == "close")
514 setStatus(cur, Collapsed);
515 else if (cmd.argument() == "toggle" || cmd.argument().empty())
517 setStatus(cur, Collapsed);
519 setStatus(cur, Open);
520 else // if assign or anything else
526 InsetText::doDispatch(cur, cmd);
532 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
533 FuncStatus & flag) const
535 switch (cmd.action()) {
536 case LFUN_INSET_TOGGLE:
537 if (cmd.argument() == "open")
538 flag.setEnabled(status_ != Open);
539 else if (cmd.argument() == "close")
540 flag.setEnabled(status_ == Open);
541 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
542 flag.setEnabled(true);
543 flag.setOnOff(status_ == Open);
545 flag.setEnabled(false);
549 return InsetText::getStatus(cur, cmd, flag);
554 void InsetCollapsable::setLabel(docstring const & l)
560 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
562 InsetLayout const & il = getLayout();
563 docstring const label = labelstring_.empty() ?
564 translateIfPossible(il.labelstring()) : labelstring_;
565 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
567 return getNewLabel(label);
571 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
575 if (status_ == Collapsed)
576 cur.leaveInset(*this);
580 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
582 InsetLayout::InsetDecoration const dec = getLayout().decoration();
583 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
587 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
590 string context_menu = contextMenuName();
591 string const it_context_menu = InsetText::contextMenuName();
592 if (decoration() == InsetLayout::CONGLOMERATE)
593 return context_menu + ";" + it_context_menu;
595 string const ic_context_menu = InsetCollapsable::contextMenuName();
596 if (ic_context_menu != context_menu)
597 context_menu += ";" + ic_context_menu;
599 if (geometry(bv) == NoButton)
600 return context_menu + ";" + it_context_menu;
602 Dimension dim = dimensionCollapsed(bv);
603 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
606 return it_context_menu;
610 string InsetCollapsable::contextMenuName() const
612 if (decoration() == InsetLayout::CONGLOMERATE)
613 return "context-conglomerate";
615 return "context-collapsable";
619 bool InsetCollapsable::canPaintChange(BufferView const & bv) const
621 switch (geometry(bv)) {
625 // these cases are handled by RowPainter since the inset is inline.
630 // TODO: implement the drawing in the remaining cases