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_ && status_ != Inlined) ? Open : status_;
52 InsetCollapsable::InsetCollapsable
53 (BufferParams const & bp, CollapseStatus status)
54 : InsetText(bp), label(from_ascii("Label")), status_(status),
55 openinlined_(false), autoOpen_(false), mouse_hover_(false)
57 setAutoBreakRows(true);
59 setFrameColor(Color::collapsableframe);
64 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
67 button_dim(rhs.button_dim),
69 topbaseline(rhs.topbaseline),
72 openinlined_(rhs.openinlined_),
73 autoOpen_(rhs.autoOpen_),
74 textdim_(rhs.textdim_),
75 // the sole purpose of this copy constructor
81 void InsetCollapsable::setLayout(BufferParams const & bp)
83 setLabelFont(getLayout(bp).labelfont);
84 setLabel(getLayout(bp).labelstring);
88 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
103 text_.write(buf, os);
107 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
109 bool token_found = false;
112 string const token = lex.getString();
113 if (token == "status") {
115 string const tmp_token = lex.getString();
117 if (tmp_token == "inlined") {
120 } else if (tmp_token == "collapsed") {
123 } else if (tmp_token == "open") {
127 lyxerr << "InsetCollapsable::read: Missing status!"
129 // Take countermeasures
130 lex.pushToken(token);
133 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
135 // take countermeasures
136 lex.pushToken(token);
139 InsetText::read(buf, lex);
142 status_ = isOpen() ? Open : Collapsed;
148 Dimension InsetCollapsable::dimensionCollapsed() const
151 theFontMetrics(layout_.labelfont).buttonText(
152 label, dim.wid, dim.asc, dim.des);
157 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
159 autoOpen_ = mi.base.bv->cursor().isInside(this);
160 mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
162 if (status() == Inlined) {
163 InsetText::metrics(mi, dim);
165 dim = dimensionCollapsed();
166 if (status() == Open) {
167 InsetText::metrics(mi, textdim_);
168 // This expression should not contain mi.base.texwidth
169 openinlined_ = !hasFixedWidth()
170 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
172 // Correct for button width, and re-fit
173 mi.base.textwidth -= dim.wid;
174 InsetText::metrics(mi, textdim_);
175 dim.wid += textdim_.wid;
176 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
177 dim.asc = textdim_.asc;
179 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
180 dim.wid = max(dim.wid, textdim_.wid);
182 dim.wid = max(dim.wid, mi.base.textwidth);
186 dim.asc += TEXT_TO_INSET_OFFSET;
187 dim.des += TEXT_TO_INSET_OFFSET;
188 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
189 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
190 bool const changed = dim_ != dim;
196 bool InsetCollapsable::setMouseHover(bool mouse_hover)
198 mouse_hover_ = mouse_hover;
203 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
205 const int xx = x + TEXT_TO_INSET_OFFSET;
206 if (status() == Inlined) {
207 InsetText::draw(pi, xx, y);
209 Dimension dimc = dimensionCollapsed();
210 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
211 button_dim.x1 = xx + 0;
212 button_dim.x2 = xx + dimc.width();
214 button_dim.y2 = top + dimc.height();
216 pi.pain.buttonText(xx, top + dimc.asc, label, layout_.labelfont, mouse_hover_);
218 if (status() == Open) {
221 textx = xx + dimc.width();
222 texty = top + textdim_.asc;
225 texty = top + dimc.height() + textdim_.asc;
227 InsetText::draw(pi, textx, texty);
230 setPosCache(pi, x, y);
234 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
236 x += TEXT_TO_INSET_OFFSET;
237 if (status() == Open) {
239 x += dimensionCollapsed().wid;
241 y += dimensionCollapsed().des + textdim_.asc;
243 if (status() != Collapsed)
244 InsetText::drawSelection(pi, x, y);
248 void InsetCollapsable::cursorPos(BufferView const & bv,
249 CursorSlice const & sl, bool boundary, int & x, int & y) const
251 BOOST_ASSERT(status() != Collapsed);
253 InsetText::cursorPos(bv, sl, boundary, x, y);
255 if (status() == Open) {
257 x += dimensionCollapsed().wid;
259 y += dimensionCollapsed().height() - ascent()
260 + TEXT_TO_INSET_OFFSET + textdim_.asc;
262 x += TEXT_TO_INSET_OFFSET;
266 Inset::EDITABLE InsetCollapsable::editable() const
268 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
272 bool InsetCollapsable::descendable() const
274 return status() != Collapsed;
278 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
280 return button_dim.contains(cmd.x, cmd.y);
284 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
287 pos_type const max_length = 15;
288 pos_type const p_siz = paragraphs().begin()->size();
289 pos_type const n = min(max_length, p_siz);
292 for (; i < n && j < p_siz; ++j) {
293 if (paragraphs().begin()->isInset(j))
295 label += paragraphs().begin()->getChar(j);
298 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
301 return label.empty() ? l : label;
305 void InsetCollapsable::edit(Cursor & cur, bool left)
307 //lyxerr << "InsetCollapsable: edit left/right" << endl;
309 InsetText::edit(cur, left);
313 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
315 //lyxerr << "InsetCollapsable: edit xy" << endl;
316 if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
319 return InsetText::editXY(cur, x, y);
323 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
325 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
326 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
328 switch (cmd.action) {
329 case LFUN_MOUSE_PRESS:
330 if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
331 // reset selection if necessary (see bug 3060)
333 cur.bv().cursor().clearSelection();
339 if (status() == Inlined)
340 InsetText::doDispatch(cur, cmd);
341 else if (status() == Open && !hitButton(cmd))
342 InsetText::doDispatch(cur, cmd);
347 case LFUN_MOUSE_MOTION:
348 case LFUN_MOUSE_DOUBLE:
349 case LFUN_MOUSE_TRIPLE:
350 if (status_ == Inlined)
351 InsetText::doDispatch(cur, cmd);
352 else if (status() && !hitButton(cmd))
353 InsetText::doDispatch(cur, cmd);
358 case LFUN_MOUSE_RELEASE:
359 if (cmd.button() == mouse_button::button3) {
360 // Open the Inset configuration dialog
361 showInsetDialog(&cur.bv());
365 if (status() == Inlined) {
366 // The mouse click has to be within the inset!
367 InsetText::doDispatch(cur, cmd);
371 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
372 // if we are selecting, we do not want to
376 // Left button is clicked, the user asks to
377 // toggle the inset visual state.
379 cur.updateFlags(Update::Force | Update::FitCursor);
380 if (status() == Collapsed) {
381 setStatus(cur, Open);
385 setStatus(cur, Collapsed);
387 cur.bv().cursor() = cur;
391 // The mouse click is within the opened inset.
392 if (status() == Open)
393 InsetText::doDispatch(cur, cmd);
396 case LFUN_INSET_TOGGLE:
397 if (cmd.argument() == "open")
398 setStatus(cur, Open);
399 else if (cmd.argument() == "close")
400 setStatus(cur, Collapsed);
401 else if (cmd.argument() == "toggle" || cmd.argument().empty())
403 setStatus(cur, Collapsed);
404 cur.top().forwardPos();
407 setStatus(cur, Open);
408 else // if assign or anything else
414 InsetText::doDispatch(cur, cmd);
420 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
421 FuncStatus & flag) const
423 switch (cmd.action) {
425 case LFUN_INSET_TOGGLE:
426 if (cmd.argument() == "open" || cmd.argument() == "close" ||
427 cmd.argument() == "toggle")
434 return InsetText::getStatus(cur, cmd, flag);
439 void InsetCollapsable::setLabel(docstring const & l)
445 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
449 if (status_ == Collapsed)
450 cur.leaveInset(*this);
451 // Because the collapse status is part of the inset and thus an
452 // integral part of the Buffer contents a changed status must be
453 // signaled to all views of current buffer.
454 cur.bv().buffer()->changed();
458 void InsetCollapsable::setLabelFont(Font const & font)
460 layout_.labelfont = font;
463 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
465 FloatList const & floats = bp.getTextClass().floats();
466 FloatList::const_iterator it = floats[type];
468 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());