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"
30 #include "Paragraph.h"
32 #include "frontends/FontMetrics.h"
33 #include "frontends/Painter.h"
38 using graphics::PreviewLoader;
47 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
49 return (autoOpen_ && status_ != Inlined) ? Open : status_;
53 InsetCollapsable::InsetCollapsable
54 (BufferParams const & bp, CollapseStatus status)
55 : InsetText(bp), label(from_ascii("Label")), status_(status),
56 openinlined_(false), autoOpen_(false), mouse_hover_(false)
58 setAutoBreakRows(true);
60 setFrameColor(Color::collapsableframe);
65 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs)
67 labelfont_(rhs.labelfont_),
68 button_dim(rhs.button_dim),
70 topbaseline(rhs.topbaseline),
73 openinlined_(rhs.openinlined_),
74 autoOpen_(rhs.autoOpen_),
75 textdim_(rhs.textdim_),
76 // the sole purpose of this copy constructor
82 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
101 void InsetCollapsable::read(Buffer const & buf, Lexer & lex)
103 bool token_found = false;
106 string const token = lex.getString();
107 if (token == "status") {
109 string const tmp_token = lex.getString();
111 if (tmp_token == "inlined") {
114 } else if (tmp_token == "collapsed") {
117 } else if (tmp_token == "open") {
121 lyxerr << "InsetCollapsable::read: Missing status!"
123 // Take countermeasures
124 lex.pushToken(token);
127 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
129 // take countermeasures
130 lex.pushToken(token);
133 InsetText::read(buf, lex);
136 status_ = isOpen() ? Open : Collapsed;
142 Dimension InsetCollapsable::dimensionCollapsed() const
145 theFontMetrics(labelfont_).buttonText(
146 label, dim.wid, dim.asc, dim.des);
151 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
153 autoOpen_ = mi.base.bv->cursor().isInside(this);
154 mi.base.textwidth -= (int) (1.5 * TEXT_TO_INSET_OFFSET);
156 if (status() == Inlined) {
157 InsetText::metrics(mi, dim);
159 dim = dimensionCollapsed();
160 if (status() == Open) {
161 InsetText::metrics(mi, textdim_);
162 // This expression should not contain mi.base.texwidth
163 openinlined_ = !hasFixedWidth()
164 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
166 // Correct for button width, and re-fit
167 mi.base.textwidth -= dim.wid;
168 InsetText::metrics(mi, textdim_);
169 dim.wid += textdim_.wid;
170 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
171 dim.asc = textdim_.asc;
173 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
174 dim.wid = max(dim.wid, textdim_.wid);
176 dim.wid = max(dim.wid, mi.base.textwidth);
180 dim.asc += TEXT_TO_INSET_OFFSET;
181 dim.des += TEXT_TO_INSET_OFFSET;
182 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
183 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
184 bool const changed = dim_ != dim;
190 bool InsetCollapsable::setMouseHover(bool mouse_hover)
192 mouse_hover_ = mouse_hover;
197 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
199 const int xx = x + TEXT_TO_INSET_OFFSET;
200 if (status() == Inlined) {
201 InsetText::draw(pi, xx, y);
203 Dimension dimc = dimensionCollapsed();
204 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
205 button_dim.x1 = xx + 0;
206 button_dim.x2 = xx + dimc.width();
208 button_dim.y2 = top + dimc.height();
210 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
212 if (status() == Open) {
215 textx = xx + dimc.width();
216 texty = top + textdim_.asc;
219 texty = top + dimc.height() + textdim_.asc;
221 InsetText::draw(pi, textx, texty);
224 setPosCache(pi, x, y);
228 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
230 x += TEXT_TO_INSET_OFFSET;
231 if (status() == Open) {
233 x += dimensionCollapsed().wid;
235 y += dimensionCollapsed().des + textdim_.asc;
237 if (status() != Collapsed)
238 InsetText::drawSelection(pi, x, y);
242 void InsetCollapsable::cursorPos(BufferView const & bv,
243 CursorSlice const & sl, bool boundary, int & x, int & y) const
245 BOOST_ASSERT(status() != Collapsed);
247 InsetText::cursorPos(bv, sl, boundary, x, y);
249 if (status() == Open) {
251 x += dimensionCollapsed().wid;
253 y += dimensionCollapsed().height() - ascent()
254 + TEXT_TO_INSET_OFFSET + textdim_.asc;
256 x += TEXT_TO_INSET_OFFSET;
260 Inset::EDITABLE InsetCollapsable::editable() const
262 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
266 bool InsetCollapsable::descendable() const
268 return status() != Collapsed;
272 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
274 return button_dim.contains(cmd.x, cmd.y);
278 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
281 pos_type const max_length = 15;
282 pos_type const p_siz = paragraphs().begin()->size();
283 pos_type const n = min(max_length, p_siz);
286 for (; i < n && j < p_siz; ++j) {
287 if (paragraphs().begin()->isInset(j))
289 label += paragraphs().begin()->getChar(j);
292 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
295 return label.empty() ? l : label;
299 void InsetCollapsable::edit(Cursor & cur, bool left)
301 //lyxerr << "InsetCollapsable: edit left/right" << endl;
303 InsetText::edit(cur, left);
307 Inset * InsetCollapsable::editXY(Cursor & cur, int x, int y)
309 //lyxerr << "InsetCollapsable: edit xy" << endl;
310 if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
313 return InsetText::editXY(cur, x, y);
317 void InsetCollapsable::doDispatch(Cursor & cur, FuncRequest & cmd)
319 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
320 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
322 switch (cmd.action) {
323 case LFUN_MOUSE_PRESS:
324 if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
325 // reset selection if necessary (see bug 3060)
327 cur.bv().cursor().clearSelection();
333 if (status() == Inlined)
334 InsetText::doDispatch(cur, cmd);
335 else if (status() == Open && !hitButton(cmd))
336 InsetText::doDispatch(cur, cmd);
341 case LFUN_MOUSE_MOTION:
342 case LFUN_MOUSE_DOUBLE:
343 case LFUN_MOUSE_TRIPLE:
344 if (status_ == Inlined)
345 InsetText::doDispatch(cur, cmd);
346 else if (status() && !hitButton(cmd))
347 InsetText::doDispatch(cur, cmd);
352 case LFUN_MOUSE_RELEASE:
353 if (cmd.button() == mouse_button::button3) {
354 // Open the Inset configuration dialog
355 showInsetDialog(&cur.bv());
359 if (status() == Inlined) {
360 // The mouse click has to be within the inset!
361 InsetText::doDispatch(cur, cmd);
365 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
366 // if we are selecting, we do not want to
370 // Left button is clicked, the user asks to
371 // toggle the inset visual state.
373 cur.updateFlags(Update::Force | Update::FitCursor);
374 if (status() == Collapsed) {
375 setStatus(cur, Open);
379 setStatus(cur, Collapsed);
381 cur.bv().cursor() = cur;
385 // The mouse click is within the opened inset.
386 if (status() == Open)
387 InsetText::doDispatch(cur, cmd);
390 case LFUN_INSET_TOGGLE:
391 if (cmd.argument() == "open")
392 setStatus(cur, Open);
393 else if (cmd.argument() == "close")
394 setStatus(cur, Collapsed);
395 else if (cmd.argument() == "toggle" || cmd.argument().empty())
397 setStatus(cur, Collapsed);
398 cur.forwardPosNoDescend();
401 setStatus(cur, Open);
402 else // if assign or anything else
408 InsetText::doDispatch(cur, cmd);
414 bool InsetCollapsable::getStatus(Cursor & cur, FuncRequest const & cmd,
415 FuncStatus & flag) const
417 switch (cmd.action) {
419 case LFUN_INSET_TOGGLE:
420 if (cmd.argument() == "open" || cmd.argument() == "close" ||
421 cmd.argument() == "toggle")
428 return InsetText::getStatus(cur, cmd, flag);
433 void InsetCollapsable::setLabel(docstring const & l)
439 void InsetCollapsable::setStatus(Cursor & cur, CollapseStatus status)
443 if (status_ == Collapsed)
444 cur.leaveInset(*this);
445 // Because the collapse status is part of the inset and thus an
446 // integral part of the Buffer contents a changed status must be
447 // signaled to all views of current buffer.
448 cur.bv().buffer()->changed();
452 void InsetCollapsable::setLabelFont(Font & font)
457 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
459 FloatList const & floats = bp.getTextClass().floats();
460 FloatList::const_iterator it = floats[type];
462 return (it == floats.end()) ? from_ascii(type) : bp.B_(it->second.name());