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 mouse_hover_ map is not copied and remains empty.
52 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
55 labelstring_(rhs.labelstring_),
56 button_dim_(rhs.button_dim_),
57 openinlined_(rhs.openinlined_)
61 InsetCollapsable::~InsetCollapsable()
63 map<BufferView const *, bool>::iterator it = mouse_hover_.begin();
64 map<BufferView const *, bool>::iterator end = mouse_hover_.end();
65 for (; it != end; ++it)
67 it->first->clearLastInset(this);
71 InsetCollapsable::CollapseStatus InsetCollapsable::status(BufferView const & bv) const
73 if (decoration() == InsetLayout::CONGLOMERATE)
75 return auto_open_[&bv] ? 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 openinlined_[&bv] ? 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 auto_open_[mi.base.bv] = 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 = button_dim_[&bv].x2 - button_dim_[&bv].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 openinlined_[&bv] = (textdim.wid + dim.wid) < mi.base.textwidth;
202 if (openinlined_[&bv]) {
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 mouse_hover_[bv] = mouse_hover;
227 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
229 BufferView const & bv = *pi.base.bv;
231 auto_open_[&bv] = bv.cursor().isInside(this);
233 FontInfo tmpfont = pi.base.font;
234 pi.base.font = getFont();
235 pi.base.font.realize(tmpfont);
237 // Draw button first -- top, left or only
238 Dimension dimc = dimensionCollapsed(bv);
240 if (geometry(bv) == TopButton ||
241 geometry(bv) == LeftButton ||
242 geometry(bv) == ButtonOnly) {
243 button_dim_[&bv].x1 = x + 0;
244 button_dim_[&bv].x2 = x + dimc.width();
245 button_dim_[&bv].y1 = y - dimc.asc;
246 button_dim_[&bv].y2 = y + dimc.des;
248 FontInfo labelfont = getLabelfont();
249 labelfont.setColor(labelColor());
250 pi.pain.buttonText(x, y, buttonLabel(bv), labelfont,
253 button_dim_[&bv].x1 = 0;
254 button_dim_[&bv].y1 = 0;
255 button_dim_[&bv].x2 = 0;
256 button_dim_[&bv].y2 = 0;
259 Dimension const textdim = InsetText::dimension(bv);
260 int const baseline = y;
262 switch (geometry(bv)) {
264 textx = x + dimc.width();
266 InsetText::draw(pi, textx, texty);
270 texty = baseline + dimc.des + textdim.asc;
271 InsetText::draw(pi, textx, texty);
278 InsetText::draw(pi, textx, texty);
284 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
285 InsetText::draw(pi, textx, texty);
286 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
288 int desc = textdim.descent();
289 if (geometry(bv) == Corners)
292 const int xx1 = x + TEXT_TO_INSET_OFFSET - 1;
293 const int xx2 = x + textdim.wid - TEXT_TO_INSET_OFFSET + 1;
294 pi.pain.line(xx1, y + desc - 4,
298 pi.pain.line(xx1, y + desc,
302 // Make status_ value visible:
303 pi.pain.line(xx1, y + desc,
306 pi.pain.line(xx2 - 4, y + desc,
310 pi.pain.line(x + textdim.wid - 3, y + desc, x + textdim.wid - 3,
311 y + desc - 4, Color_foreground);
313 // the label below the text. Can be toggled.
314 if (geometry(bv) == SubLabel) {
315 FontInfo font(getLabelfont());
316 font.realize(sane_font);
322 theFontMetrics(font).rectText(buttonLabel(bv), w, a, d);
323 int const ww = max(textdim.wid, w);
324 pi.pain.rectText(x + (ww - w) / 2, y + desc + a,
325 buttonLabel(bv), font, Color_none, Color_none);
328 // a visual cue when the cursor is inside the inset
329 Cursor const & cur = bv.cursor();
330 if (cur.isInside(this)) {
333 pi.pain.line(xx1, y + 4, xx1, y, Color_foreground);
334 pi.pain.line(xx1 + 4, y, xx1, y, Color_foreground);
335 pi.pain.line(xx2, y + 4, xx2, y, Color_foreground);
336 pi.pain.line(xx2 - 4, y, xx2, y, Color_foreground);
341 pi.base.font = tmpfont;
345 void InsetCollapsable::cursorPos(BufferView const & bv,
346 CursorSlice const & sl, bool boundary, int & x, int & y) const
348 if (geometry(bv) == ButtonOnly)
351 InsetText::cursorPos(bv, sl, boundary, x, y);
352 Dimension const textdim = InsetText::dimension(bv);
354 switch (geometry(bv)) {
356 x += dimensionCollapsed(bv).wid;
359 y += dimensionCollapsed(bv).des + textdim.asc;
374 bool InsetCollapsable::editable() const
376 switch (decoration()) {
377 case InsetLayout::CLASSIC:
378 case InsetLayout::MINIMALISTIC:
379 return status_ == Open;
386 bool InsetCollapsable::descendable(BufferView const & bv) const
388 return geometry(bv) != ButtonOnly;
392 bool InsetCollapsable::clickable(BufferView const & bv, int x, int y) const
394 return button_dim_[&bv].contains(x, y);
398 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
401 pos_type const max_length = 15;
402 pos_type const p_siz = paragraphs().begin()->size();
403 pos_type const n = min(max_length, p_siz);
406 for (; i < n && j < p_siz; ++j) {
407 if (paragraphs().begin()->isInset(j))
409 label += paragraphs().begin()->getChar(j);
412 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
415 return label.empty() ? l : label;
419 void InsetCollapsable::edit(Cursor & cur, bool front, EntryDirection entry_from)
421 //lyxerr << "InsetCollapsable: edit left/right" << endl;
423 InsetText::edit(cur, front, entry_from);
427 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
429 //lyxerr << "InsetCollapsable: edit xy" << endl;
430 if (geometry(cur.bv()) == ButtonOnly
431 || (button_dim_[&cur.bv()].contains(x, y)
432 && geometry(cur.bv()) != NoButton))
435 return InsetText::editXY(cur, x, y);
439 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
441 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
442 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
444 bool const hitButton = clickable(cur.bv(), cmd.x(), cmd.y());
446 switch (cmd.action()) {
447 case LFUN_MOUSE_PRESS:
449 switch (cmd.button()) {
450 case mouse_button::button1:
451 case mouse_button::button3:
452 // Pass the command to the enclosing InsetText,
453 // so that the cursor gets set.
456 case mouse_button::none:
457 case mouse_button::button2:
458 case mouse_button::button4:
459 case mouse_button::button5:
461 cur.noScreenUpdate();
464 } else if (geometry(cur.bv()) != ButtonOnly)
465 InsetText::doDispatch(cur, cmd);
470 case LFUN_MOUSE_MOTION:
471 case LFUN_MOUSE_DOUBLE:
472 case LFUN_MOUSE_TRIPLE:
474 cur.noScreenUpdate();
475 else if (geometry(cur.bv()) != ButtonOnly)
476 InsetText::doDispatch(cur, cmd);
481 case LFUN_MOUSE_RELEASE:
483 // The mouse click has to be within the inset!
484 if (geometry(cur.bv()) != ButtonOnly)
485 InsetText::doDispatch(cur, cmd);
490 if (cmd.button() != mouse_button::button1) {
492 cur.noScreenUpdate();
495 // if we are selecting, we do not want to
499 // Left button is clicked, the user asks to
500 // toggle the inset visual state.
502 cur.screenUpdateFlags(Update::Force | Update::FitCursor);
503 if (geometry(cur.bv()) == ButtonOnly) {
504 setStatus(cur, Open);
508 setStatus(cur, Collapsed);
509 cur.bv().cursor() = cur;
512 case LFUN_INSET_TOGGLE:
513 if (cmd.argument() == "open")
514 setStatus(cur, Open);
515 else if (cmd.argument() == "close")
516 setStatus(cur, Collapsed);
517 else if (cmd.argument() == "toggle" || cmd.argument().empty())
519 setStatus(cur, Collapsed);
521 setStatus(cur, Open);
522 else // if assign or anything else
528 InsetText::doDispatch(cur, cmd);
534 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
535 FuncStatus & flag) const
537 switch (cmd.action()) {
538 case LFUN_INSET_TOGGLE:
539 if (cmd.argument() == "open")
540 flag.setEnabled(status_ != Open);
541 else if (cmd.argument() == "close")
542 flag.setEnabled(status_ == Open);
543 else if (cmd.argument() == "toggle" || cmd.argument().empty()) {
544 flag.setEnabled(true);
545 flag.setOnOff(status_ == Open);
547 flag.setEnabled(false);
551 return InsetText::getStatus(cur, cmd, flag);
556 void InsetCollapsable::setLabel(docstring const & l)
562 docstring const InsetCollapsable::buttonLabel(BufferView const & bv) const
564 InsetLayout const & il = getLayout();
565 docstring const label = labelstring_.empty() ?
566 translateIfPossible(il.labelstring()) : labelstring_;
567 if (!il.contentaslabel() || geometry(bv) != ButtonOnly)
569 return getNewLabel(label);
573 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
577 if (status_ == Collapsed)
578 cur.leaveInset(*this);
582 InsetLayout::InsetDecoration InsetCollapsable::decoration() const
584 InsetLayout::InsetDecoration const dec = getLayout().decoration();
585 return dec == InsetLayout::DEFAULT ? InsetLayout::CLASSIC : dec;
589 string InsetCollapsable::contextMenu(BufferView const & bv, int x,
592 string context_menu = contextMenuName();
593 string const it_context_menu = InsetText::contextMenuName();
594 if (decoration() == InsetLayout::CONGLOMERATE)
595 return context_menu + ";" + it_context_menu;
597 string const ic_context_menu = InsetCollapsable::contextMenuName();
598 if (ic_context_menu != context_menu)
599 context_menu += ";" + ic_context_menu;
601 if (geometry(bv) == NoButton)
602 return context_menu + ";" + it_context_menu;
604 Dimension dim = dimensionCollapsed(bv);
605 if (x < xo(bv) + dim.wid && y < yo(bv) + dim.des)
608 return it_context_menu;
612 string InsetCollapsable::contextMenuName() const
614 if (decoration() == InsetLayout::CONGLOMERATE)
615 return "context-conglomerate";
617 return "context-collapsable";