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 "BufferParams.h"
19 #include "BufferView.h"
22 #include "DispatchResult.h"
23 #include "FloatList.h"
24 #include "FuncStatus.h"
28 #include "FuncRequest.h"
29 #include "MetricsInfo.h"
31 #include "frontends/FontMetrics.h"
32 #include "frontends/Painter.h"
37 using graphics::PreviewLoader;
46 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
48 return autoOpen_ ? Open : status_;
52 InsetCollapsable::Geometry InsetCollapsable::geometry() const
54 switch (decoration()) {
56 if (status_ == Open || autoOpen_) {
68 return status_ == Open ? SubLabel : Corners;
71 // dummy return value to shut down a warning,
77 InsetCollapsable::InsetCollapsable
78 (BufferParams const & bp, CollapseStatus status)
79 : InsetText(bp), status_(status),
80 openinlined_(false), autoOpen_(false), mouse_hover_(false)
82 setAutoBreakRows(true);
84 setFrameColor(Color::collapsableframe);
89 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
91 button_dim(rhs.button_dim),
93 topbaseline(rhs.topbaseline),
96 openinlined_(rhs.openinlined_),
97 autoOpen_(rhs.autoOpen_),
98 textdim_(rhs.textdim_),
99 // the sole purpose of this copy constructor
105 void InsetCollapsable::setLayout(BufferParams const & bp)
107 layout_ = getLayout(bp);
111 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
123 text_.write(buf, os);
127 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
129 bool token_found = false;
132 string const token = lex.getString();
133 if (token == "status") {
135 string const tmp_token = lex.getString();
137 if (tmp_token == "collapsed") {
140 } else if (tmp_token == "open") {
144 lyxerr << "InsetCollapsable::read: Missing status!"
146 // Take countermeasures
147 lex.pushToken(token);
150 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
152 // take countermeasures
153 lex.pushToken(token);
156 InsetText::read(buf, lex);
159 status_ = isOpen() ? Open : Collapsed;
165 Dimension InsetCollapsable::dimensionCollapsed() const
168 theFontMetrics(layout_.labelfont).buttonText(
169 layout_.labelstring, dim.wid, dim.asc, dim.des);
174 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
176 autoOpen_ = mi.base.bv->cursor().isInside(this);
177 mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
179 switch (decoration()) {
181 InsetText::metrics(mi, dim);
184 InsetText::metrics(mi, dim);
185 if (status() == Open) {
186 // consider width of the inset label
187 Font font(layout_.labelfont);
188 font.realize(Font(Font::ALL_SANE));
194 docstring s = layout_.labelstring;
195 theFontMetrics(font).rectText(s, w, a, d);
196 dim.wid = max(dim.wid, w);
198 if (status() == Open)
206 dim = dimensionCollapsed();
207 if (geometry() == TopButton
208 || geometry() == LeftButton) {
209 InsetText::metrics(mi, textdim_);
210 // This expression should not contain mi.base.texwidth
211 openinlined_ = !hasFixedWidth()
212 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
214 // Correct for button width, and re-fit
215 mi.base.textwidth -= dim.wid;
216 InsetText::metrics(mi, textdim_);
217 dim.wid += textdim_.wid;
218 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
219 dim.asc = textdim_.asc;
221 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
222 dim.wid = max(dim.wid, textdim_.wid);
224 dim.wid = max(dim.wid, mi.base.textwidth);
229 dim.asc += TEXT_TO_INSET_OFFSET;
230 dim.des += TEXT_TO_INSET_OFFSET;
231 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
232 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
233 bool const changed = dim_ != dim;
239 bool InsetCollapsable::setMouseHover(bool mouse_hover)
241 mouse_hover_ = mouse_hover;
246 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
248 const int xx = x + TEXT_TO_INSET_OFFSET;
250 // Draw button first -- top, left or only
251 Dimension dimc = dimensionCollapsed();
252 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
253 if (decoration() == Classic) {
254 button_dim.x1 = xx + 0;
255 button_dim.x2 = xx + dimc.width();
257 button_dim.y2 = top + dimc.height();
259 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
263 switch (geometry()) {
265 textx = xx + dimc.width();
266 texty = top + textdim_.asc;
267 InsetText::draw(pi, textx, texty);
271 texty = top + dimc.height() + textdim_.asc;
272 InsetText::draw(pi, textx, texty);
278 texty = y + textdim_.asc;
279 InsetText::draw(pi, textx, texty);
284 texty = y + textdim_.asc;
285 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
286 InsetText::draw(pi, textx, texty);
287 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
289 int desc = InsetText::descent();
290 if (status() == Open)
295 pi.pain.line(x, y + desc - 4, x, y + desc,
296 layout_.labelfont.color());
297 if (internalStatus() == Open)
298 pi.pain.line(x, y + desc,
299 x + dim_.wid - 3, y + desc,
300 layout_.labelfont.color());
302 // Make status_ value visible:
303 pi.pain.line(x, y + desc,
305 layout_.labelfont.color());
306 pi.pain.line(x + dim_.wid - 7, y + desc,
307 x + dim_.wid -3, y + desc,
308 layout_.labelfont.color());
310 pi.pain.line(x + dim_.wid - 3, y + desc, x + dim_.wid - 3, y + desc - 4,
311 layout_.labelfont.color());
313 // the label of the charstyle. Can be toggled.
314 if (status() == Open) {
315 Font font(layout_.labelfont);
316 font.realize(Font(Font::ALL_SANE));
323 docstring s = layout_.labelstring;
324 theFontMetrics(font).rectText(s, w, a, d);
325 pi.pain.rectText(x + (dim_.wid - w) / 2, y + desc + a,
326 s, font, Color::none, Color::none);
329 // a visual cue when the cursor is inside the inset
330 Cursor & cur = pi.base.bv->cursor();
331 if (cur.isInside(this)) {
334 pi.pain.line(x, y + 4, x, y, layout_.labelfont.color());
335 pi.pain.line(x + 4, y, x, y, layout_.labelfont.color());
336 pi.pain.line(x + dim_.wid - 3, y + 4, x + dim_.wid - 3, y,
337 layout_.labelfont.color());
338 pi.pain.line(x + dim_.wid - 7, y, x + dim_.wid - 3, y,
339 layout_.labelfont.color());
343 setPosCache(pi, x, y);
347 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
349 x += TEXT_TO_INSET_OFFSET;
350 switch (geometry()) {
352 x += dimensionCollapsed().wid;
353 InsetText::drawSelection(pi, x, y);
356 y += dimensionCollapsed().des + textdim_.asc;
357 InsetText::drawSelection(pi, x, y);
364 InsetText::drawSelection(pi, x, y);
370 void InsetCollapsable::cursorPos(BufferView const & bv,
371 CursorSlice const & sl, bool boundary, int & x, int & y) const
373 BOOST_ASSERT(geometry() != ButtonOnly);
375 InsetText::cursorPos(bv, sl, boundary, x, y);
377 switch (geometry()) {
379 x += dimensionCollapsed().wid;
382 y += dimensionCollapsed().height() - ascent()
383 + TEXT_TO_INSET_OFFSET + textdim_.asc;
394 x += TEXT_TO_INSET_OFFSET;
398 Inset::EDITABLE InsetCollapsable::editable() const
400 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
404 bool InsetCollapsable::descendable() const
406 return geometry() != ButtonOnly;
410 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
412 return button_dim.contains(cmd.x, cmd.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 left)
439 //lyxerr << "InsetCollapsable: edit left/right" << endl;
441 InsetText::edit(cur, left);
445 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
447 //lyxerr << "InsetCollapsable: edit xy" << endl;
448 if (geometry() == ButtonOnly
449 || (button_dim.contains(x, y)
450 && decoration() != Minimalistic))
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 switch (cmd.action) {
463 case LFUN_MOUSE_PRESS:
464 if (cmd.button() == mouse_button::button1
466 && decoration() != Minimalistic) {
467 // reset selection if necessary (see bug 3060)
469 cur.bv().cursor().clearSelection();
475 if (decoration() == Minimalistic)
476 InsetText::doDispatch(cur, cmd);
477 else if (geometry() != ButtonOnly
479 InsetText::doDispatch(cur, cmd);
484 case LFUN_MOUSE_MOTION:
485 case LFUN_MOUSE_DOUBLE:
486 case LFUN_MOUSE_TRIPLE:
487 if (decoration() == Minimalistic)
488 InsetText::doDispatch(cur, cmd);
489 else if (geometry() != ButtonOnly
491 InsetText::doDispatch(cur, cmd);
496 case LFUN_MOUSE_RELEASE:
497 if (cmd.button() == mouse_button::button3) {
498 if (decoration() == Conglomerate) {
500 if (internalStatus() == Open)
501 setStatus(cur, Collapsed);
503 setStatus(cur, Open);
507 // configuration dialog
508 showInsetDialog(&cur.bv());
513 if (decoration() == Minimalistic) {
514 // The mouse click has to be within the inset!
515 InsetText::doDispatch(cur, cmd);
519 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
520 // if we are selecting, we do not want to
524 // Left button is clicked, the user asks to
525 // toggle the inset visual state.
527 cur.updateFlags(Update::Force | Update::FitCursor);
528 if (geometry() == ButtonOnly) {
529 setStatus(cur, Open);
533 setStatus(cur, Collapsed);
535 cur.bv().cursor() = cur;
539 // The mouse click is within the opened inset.
540 if (geometry() == TopButton
541 || geometry() == LeftButton)
542 InsetText::doDispatch(cur, cmd);
545 case LFUN_INSET_TOGGLE:
546 if (cmd.argument() == "open")
547 setStatus(cur, Open);
548 else if (cmd.argument() == "close")
549 setStatus(cur, Collapsed);
550 else if (cmd.argument() == "toggle" || cmd.argument().empty())
551 if (internalStatus() == Open) {
552 setStatus(cur, Collapsed);
553 if (geometry() == ButtonOnly)
554 cur.top().forwardPos();
556 setStatus(cur, Open);
557 else // if assign or anything else
563 InsetText::doDispatch(cur, cmd);
569 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
570 FuncStatus & flag) const
572 switch (cmd.action) {
574 case LFUN_INSET_TOGGLE:
575 if (cmd.argument() == "open" || cmd.argument() == "close" ||
576 cmd.argument() == "toggle")
583 return InsetText::getStatus(cur, cmd, flag);
588 void InsetCollapsable::setLabel(docstring const & l)
590 layout_.labelstring = l;
594 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
598 if (status_ == Collapsed)
599 cur.leaveInset(*this);
600 // Because the collapse status is part of the inset and thus an
601 // integral part of the Buffer contents a changed status must be
602 // signaled to all views of current buffer.
603 cur.bv().buffer().changed();
607 void InsetCollapsable::setLabelFont(Font const & font)
609 layout_.labelfont = font;
612 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
614 FloatList const & floats = bp.getTextClass().floats();
615 FloatList::const_iterator it = floats[type];
617 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());
622 int InsetCollapsable::latex(Buffer const & buf, odocstream & os,
623 OutputParams const & runparams) const
625 // This implements the standard way of handling the LaTeX output of
626 // a collapsable inset, either a command or an environment. Standard
627 // collapsable insets should not redefine this, non-standard ones may
629 if (!layout_.latexname.empty()) {
630 if (layout_.latextype == "command") {
632 os << '\\' << from_utf8(layout_.latexname);
633 if (!layout_.latexparam.empty())
634 os << from_utf8(layout_.latexparam);
636 } else if (layout_.latextype == "environment") {
637 os << "%\n\\begin{" << from_utf8(layout_.latexname) << "}\n";
638 if (!layout_.latexparam.empty())
639 os << from_utf8(layout_.latexparam);
642 int i = InsetText::latex(buf, os, runparams);
643 if (!layout_.latexname.empty())
644 if (layout_.latextype == "command") {
646 } else if (layout_.latextype == "environment") {
647 os << "\n\\end{" << from_utf8(layout_.latexname) << "}\n";