2 * \file insetcollapsable.C
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(LColor::collapsableframe);
61 setInsetName(from_ascii("Collapsable"));
66 InsetCollapsable::InsetCollapsable(InsetCollapsable const & rhs):
68 labelfont_(rhs.labelfont_),
69 button_dim(rhs.button_dim),
71 topbaseline(rhs.topbaseline),
74 openinlined_(rhs.openinlined_),
75 autoOpen_(rhs.autoOpen_),
76 textdim_(rhs.textdim_),
77 // the sole purpose of this copy constructor
83 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
102 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
104 bool token_found = false;
107 string const token = lex.getString();
108 if (token == "status") {
110 string const tmp_token = lex.getString();
112 if (tmp_token == "inlined") {
115 } else if (tmp_token == "collapsed") {
118 } else if (tmp_token == "open") {
122 lyxerr << "InsetCollapsable::read: Missing status!"
124 // Take countermeasures
125 lex.pushToken(token);
128 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
130 // take countermeasures
131 lex.pushToken(token);
134 InsetText::read(buf, lex);
137 status_ = isOpen() ? Open : Collapsed;
143 Dimension InsetCollapsable::dimensionCollapsed() const
146 theFontMetrics(labelfont_).buttonText(
147 label, dim.wid, dim.asc, dim.des);
152 bool InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
154 autoOpen_ = mi.base.bv->cursor().isInside(this);
155 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
157 if (status() == Inlined) {
158 InsetText::metrics(mi, dim);
160 dim = dimensionCollapsed();
161 if (status() == Open) {
162 InsetText::metrics(mi, textdim_);
163 // This expression should not contain mi.base.texwidth
164 openinlined_ = !hasFixedWidth()
165 && textdim_.wid < 0.5 * mi.base.bv->workWidth();
167 // Correct for button width, and re-fit
168 mi.base.textwidth -= dim.wid;
169 InsetText::metrics(mi, textdim_);
170 dim.wid += textdim_.wid;
171 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
172 dim.asc = textdim_.asc;
174 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
175 dim.wid = max(dim.wid, textdim_.wid);
179 dim.asc += TEXT_TO_INSET_OFFSET;
180 dim.des += TEXT_TO_INSET_OFFSET;
181 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
182 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
183 bool const changed = dim_ != dim;
189 bool InsetCollapsable::setMouseHover(bool mouse_hover)
191 mouse_hover_ = mouse_hover;
196 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
198 const int xx = x + TEXT_TO_INSET_OFFSET;
199 if (status() == Inlined) {
200 InsetText::draw(pi, xx, y);
202 Dimension dimc = dimensionCollapsed();
203 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
204 button_dim.x1 = xx + 0;
205 button_dim.x2 = xx + dimc.width();
207 button_dim.y2 = top + dimc.height();
209 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
211 if (status() == Open) {
214 textx = xx + dimc.width();
215 texty = top + textdim_.asc;
218 texty = top + dimc.height() + textdim_.asc;
220 InsetText::draw(pi, textx, texty);
223 setPosCache(pi, x, y);
227 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
229 x += TEXT_TO_INSET_OFFSET;
230 if (status() == Open) {
232 x += dimensionCollapsed().wid;
234 y += dimensionCollapsed().des + textdim_.asc;
236 if (status() != Collapsed)
237 InsetText::drawSelection(pi, x, y);
241 void InsetCollapsable::cursorPos(BufferView const & bv,
242 CursorSlice const & sl, bool boundary, int & x, int & y) const
244 BOOST_ASSERT(status() != Collapsed);
246 InsetText::cursorPos(bv, sl, boundary, x, y);
248 if (status() == Open) {
250 x += dimensionCollapsed().wid;
252 y += dimensionCollapsed().height() - ascent()
253 + TEXT_TO_INSET_OFFSET + textdim_.asc;
255 x += TEXT_TO_INSET_OFFSET;
259 InsetBase::EDITABLE InsetCollapsable::editable() const
261 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
265 bool InsetCollapsable::descendable() const
267 return status() != Collapsed;
271 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
273 return button_dim.contains(cmd.x, cmd.y);
277 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
280 pos_type const max_length = 15;
281 pos_type const p_siz = paragraphs().begin()->size();
282 pos_type const n = min(max_length, p_siz);
285 for (; i < n && j < p_siz; ++j) {
286 if (paragraphs().begin()->isInset(j))
288 label += paragraphs().begin()->getChar(j);
291 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
294 return label.empty() ? l : label;
298 void InsetCollapsable::edit(LCursor & cur, bool left)
300 //lyxerr << "InsetCollapsable: edit left/right" << endl;
302 InsetText::edit(cur, left);
306 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
308 //lyxerr << "InsetCollapsable: edit xy" << endl;
309 if (status() == Collapsed || button_dim.contains(x, y))
312 return InsetText::editXY(cur, x, y);
316 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
318 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
319 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
321 switch (cmd.action) {
322 case LFUN_MOUSE_PRESS:
323 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
328 if (status() == Inlined)
329 InsetText::doDispatch(cur, cmd);
330 else if (status() == Open && !hitButton(cmd))
331 InsetText::doDispatch(cur, cmd);
336 case LFUN_MOUSE_MOTION:
337 case LFUN_MOUSE_DOUBLE:
338 case LFUN_MOUSE_TRIPLE:
339 if (status_ == Inlined)
340 InsetText::doDispatch(cur, cmd);
341 else if (status() && !hitButton(cmd))
342 InsetText::doDispatch(cur, cmd);
347 case LFUN_MOUSE_RELEASE:
348 if (cmd.button() == mouse_button::button3) {
349 // Open the Inset configuration dialog
350 showInsetDialog(&cur.bv());
354 if (status() == Inlined) {
355 // The mouse click has to be within the inset!
356 InsetText::doDispatch(cur, cmd);
360 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
361 // if we are selecting, we do not want to
365 // Left button is clicked, the user asks to
366 // toggle the inset visual state.
368 cur.updateFlags(Update::Force | Update::FitCursor);
369 if (status() == Collapsed) {
370 setStatus(cur, Open);
374 setStatus(cur, Collapsed);
376 cur.bv().cursor() = cur;
380 // The mouse click is within the opened inset.
381 if (status() == Open)
382 InsetText::doDispatch(cur, cmd);
385 case LFUN_INSET_TOGGLE:
386 if (cmd.argument() == "open")
387 setStatus(cur, Open);
388 else if (cmd.argument() == "close")
389 setStatus(cur, Collapsed);
390 else if (cmd.argument() == "toggle" || cmd.argument().empty())
392 setStatus(cur, Collapsed);
393 cur.forwardPosNoDescend();
396 setStatus(cur, Open);
397 else // if assign or anything else
403 InsetText::doDispatch(cur, cmd);
409 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
410 FuncStatus & flag) const
412 switch (cmd.action) {
414 case LFUN_INSET_TOGGLE:
415 if (cmd.argument() == "open" || cmd.argument() == "close" ||
416 cmd.argument() == "toggle")
423 return InsetText::getStatus(cur, cmd, flag);
428 void InsetCollapsable::setLabel(docstring const & l)
434 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
438 if (status_ == Collapsed)
439 cur.leaveInset(*this);
443 void InsetCollapsable::setLabelFont(LyXFont & font)
448 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
450 FloatList const & floats = bp.getLyXTextClass().floats();
451 FloatList::const_iterator it = floats[type];
453 return (it == floats.end()) ? from_ascii(type) : _(it->second.name());