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()) {
182 InsetText::metrics(mi, dim);
185 dim = dimensionCollapsed();
186 if (geometry() == TopButton
187 || geometry() == LeftButton) {
188 InsetText::metrics(mi, textdim_);
189 // This expression should not contain mi.base.texwidth
190 openinlined_ = !hasFixedWidth()
191 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
193 // Correct for button width, and re-fit
194 mi.base.textwidth -= dim.wid;
195 InsetText::metrics(mi, textdim_);
196 dim.wid += textdim_.wid;
197 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
198 dim.asc = textdim_.asc;
200 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
201 dim.wid = max(dim.wid, textdim_.wid);
203 dim.wid = max(dim.wid, mi.base.textwidth);
208 dim.asc += TEXT_TO_INSET_OFFSET;
209 dim.des += TEXT_TO_INSET_OFFSET;
210 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
211 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
212 bool const changed = dim_ != dim;
218 bool InsetCollapsable::setMouseHover(bool mouse_hover)
220 mouse_hover_ = mouse_hover;
225 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
227 const int xx = x + TEXT_TO_INSET_OFFSET;
229 // Draw button first -- top, left or only
230 Dimension dimc = dimensionCollapsed();
231 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
232 if (decoration() == Classic) {
233 button_dim.x1 = xx + 0;
234 button_dim.x2 = xx + dimc.width();
236 button_dim.y2 = top + dimc.height();
238 pi.pain.buttonText(xx, top + dimc.asc, layout_.labelstring, layout_.labelfont, mouse_hover_);
242 switch (geometry()) {
244 textx = xx + dimc.width();
245 texty = top + textdim_.asc;
246 InsetText::draw(pi, textx, texty);
250 texty = top + dimc.height() + textdim_.asc;
251 InsetText::draw(pi, textx, texty);
257 texty = y + textdim_.asc;
258 InsetText::draw(pi, textx, texty);
262 // FIXME add handling of SubLabel, Corners
263 // still in CharStyle
265 texty = y + textdim_.asc;
266 const_cast<InsetCollapsable *>(this)->setDrawFrame(false);
267 InsetText::draw(pi, textx, texty);
268 const_cast<InsetCollapsable *>(this)->setDrawFrame(true);
271 setPosCache(pi, x, y);
275 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
277 x += TEXT_TO_INSET_OFFSET;
278 switch (geometry()) {
280 x += dimensionCollapsed().wid;
281 InsetText::drawSelection(pi, x, y);
284 y += dimensionCollapsed().des + textdim_.asc;
285 InsetText::drawSelection(pi, x, y);
292 InsetText::drawSelection(pi, x, y);
298 void InsetCollapsable::cursorPos(BufferView const & bv,
299 CursorSlice const & sl, bool boundary, int & x, int & y) const
301 BOOST_ASSERT(geometry() != ButtonOnly);
303 InsetText::cursorPos(bv, sl, boundary, x, y);
305 switch (geometry()) {
307 x += dimensionCollapsed().wid;
310 y += dimensionCollapsed().height() - ascent()
311 + TEXT_TO_INSET_OFFSET + textdim_.asc;
322 x += TEXT_TO_INSET_OFFSET;
326 Inset::EDITABLE InsetCollapsable::editable() const
328 return geometry() != ButtonOnly? HIGHLY_EDITABLE : IS_EDITABLE;
332 bool InsetCollapsable::descendable() const
334 return geometry() != ButtonOnly;
338 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
340 return button_dim.contains(cmd.x, cmd.y);
344 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
347 pos_type const max_length = 15;
348 pos_type const p_siz = paragraphs().begin()->size();
349 pos_type const n = min(max_length, p_siz);
352 for (; i < n && j < p_siz; ++j) {
353 if (paragraphs().begin()->isInset(j))
355 label += paragraphs().begin()->getChar(j);
358 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
361 return label.empty() ? l : label;
365 void InsetCollapsable::edit(Cursor & cur, bool left)
367 //lyxerr << "InsetCollapsable: edit left/right" << endl;
369 InsetText::edit(cur, left);
373 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
375 //lyxerr << "InsetCollapsable: edit xy" << endl;
376 if (geometry() == ButtonOnly
377 || (button_dim.contains(x, y)
378 && decoration() != Minimalistic))
381 return InsetText::editXY(cur, x, y);
385 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
387 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
388 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
390 switch (cmd.action) {
391 case LFUN_MOUSE_PRESS:
392 if (cmd.button() == mouse_button::button1
394 && decoration() != Minimalistic) {
395 // reset selection if necessary (see bug 3060)
397 cur.bv().cursor().clearSelection();
403 if (decoration() == Minimalistic)
404 InsetText::doDispatch(cur, cmd);
405 else if (geometry() != ButtonOnly
407 InsetText::doDispatch(cur, cmd);
412 case LFUN_MOUSE_MOTION:
413 case LFUN_MOUSE_DOUBLE:
414 case LFUN_MOUSE_TRIPLE:
415 if (decoration() == Minimalistic)
416 InsetText::doDispatch(cur, cmd);
417 else if (geometry() != ButtonOnly
419 InsetText::doDispatch(cur, cmd);
424 case LFUN_MOUSE_RELEASE:
425 if (cmd.button() == mouse_button::button3) {
426 // Open the Inset configuration dialog
427 showInsetDialog(&cur.bv());
431 if (decoration() == Minimalistic) {
432 // The mouse click has to be within the inset!
433 InsetText::doDispatch(cur, cmd);
437 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
438 // if we are selecting, we do not want to
442 // Left button is clicked, the user asks to
443 // toggle the inset visual state.
445 cur.updateFlags(Update::Force | Update::FitCursor);
446 if (geometry() == ButtonOnly) {
447 setStatus(cur, Open);
451 setStatus(cur, Collapsed);
453 cur.bv().cursor() = cur;
457 // The mouse click is within the opened inset.
458 if (geometry() == TopButton
459 || geometry() == LeftButton)
460 InsetText::doDispatch(cur, cmd);
463 case LFUN_INSET_TOGGLE:
464 if (cmd.argument() == "open")
465 setStatus(cur, Open);
466 else if (cmd.argument() == "close")
467 setStatus(cur, Collapsed);
468 else if (cmd.argument() == "toggle" || cmd.argument().empty())
470 setStatus(cur, Collapsed);
471 cur.top().forwardPos();
474 setStatus(cur, Open);
475 else // if assign or anything else
481 InsetText::doDispatch(cur, cmd);
487 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
488 FuncStatus & flag) const
490 switch (cmd.action) {
492 case LFUN_INSET_TOGGLE:
493 if (cmd.argument() == "open" || cmd.argument() == "close" ||
494 cmd.argument() == "toggle")
501 return InsetText::getStatus(cur, cmd, flag);
506 void InsetCollapsable::setLabel(docstring const & l)
508 layout_.labelstring = l;
512 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
516 if (status_ == Collapsed)
517 cur.leaveInset(*this);
518 // Because the collapse status is part of the inset and thus an
519 // integral part of the Buffer contents a changed status must be
520 // signaled to all views of current buffer.
521 cur.bv().buffer()->changed();
525 void InsetCollapsable::setLabelFont(Font const & font)
527 layout_.labelfont = font;
530 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
532 FloatList const & floats = bp.getTextClass().floats();
533 FloatList::const_iterator it = floats[type];
535 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());