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_ = 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);
178 dim.asc += TEXT_TO_INSET_OFFSET;
179 dim.des += TEXT_TO_INSET_OFFSET;
180 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
181 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
182 bool const changed = dim_ != dim;
188 bool InsetCollapsable::setMouseHover(bool mouse_hover)
190 mouse_hover_ = mouse_hover;
195 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
197 const int xx = x + TEXT_TO_INSET_OFFSET;
198 if (status() == Inlined) {
199 InsetText::draw(pi, xx, y);
201 Dimension dimc = dimensionCollapsed();
202 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
203 button_dim.x1 = xx + 0;
204 button_dim.x2 = xx + dimc.width();
206 button_dim.y2 = top + dimc.height();
208 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
210 if (status() == Open) {
213 textx = xx + dimc.width();
214 texty = top + textdim_.asc;
217 texty = top + dimc.height() + textdim_.asc;
219 InsetText::draw(pi, textx, texty);
222 setPosCache(pi, x, y);
226 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
228 x += TEXT_TO_INSET_OFFSET;
229 if (status() == Open) {
231 x += dimensionCollapsed().wid;
233 y += dimensionCollapsed().des + textdim_.asc;
235 if (status() != Collapsed)
236 InsetText::drawSelection(pi, x, y);
240 void InsetCollapsable::cursorPos(BufferView const & bv,
241 CursorSlice const & sl, bool boundary, int & x, int & y) const
243 BOOST_ASSERT(status() != Collapsed);
245 InsetText::cursorPos(bv, sl, boundary, x, y);
247 if (status() == Open) {
249 x += dimensionCollapsed().wid;
251 y += dimensionCollapsed().height() - ascent()
252 + TEXT_TO_INSET_OFFSET + textdim_.asc;
254 x += TEXT_TO_INSET_OFFSET;
258 InsetBase::EDITABLE InsetCollapsable::editable() const
260 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
264 bool InsetCollapsable::descendable() const
266 return status() != Collapsed;
270 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
272 return button_dim.contains(cmd.x, cmd.y);
276 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
279 pos_type const max_length = 15;
280 pos_type const p_siz = paragraphs().begin()->size();
281 pos_type const n = min(max_length, p_siz);
284 for (; i < n && j < p_siz; ++j) {
285 if (paragraphs().begin()->isInset(j))
287 label += paragraphs().begin()->getChar(j);
290 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
293 return label.empty() ? l : label;
297 void InsetCollapsable::edit(LCursor & cur, bool left)
299 //lyxerr << "InsetCollapsable: edit left/right" << endl;
301 InsetText::edit(cur, left);
305 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
307 //lyxerr << "InsetCollapsable: edit xy" << endl;
308 if (status() == Collapsed || button_dim.contains(x, y))
311 return InsetText::editXY(cur, x, y);
315 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
317 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
318 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
320 switch (cmd.action) {
321 case LFUN_MOUSE_PRESS:
322 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
327 if (status() == Inlined)
328 InsetText::doDispatch(cur, cmd);
329 else if (status() == Open && !hitButton(cmd))
330 InsetText::doDispatch(cur, cmd);
335 case LFUN_MOUSE_MOTION:
336 case LFUN_MOUSE_DOUBLE:
337 case LFUN_MOUSE_TRIPLE:
338 if (status_ == Inlined)
339 InsetText::doDispatch(cur, cmd);
340 else if (status() && !hitButton(cmd))
341 InsetText::doDispatch(cur, cmd);
346 case LFUN_MOUSE_RELEASE:
347 if (cmd.button() == mouse_button::button3) {
348 // Open the Inset configuration dialog
349 showInsetDialog(&cur.bv());
353 if (status() == Inlined) {
354 // The mouse click has to be within the inset!
355 InsetText::doDispatch(cur, cmd);
359 if (cmd.button() == mouse_button::button1 && hitButton(cmd)
360 && !cur.selection()) {
361 // Left button is clicked, the user asks to
362 // toggle the inset visual state.
364 cur.updateFlags(Update::Force | Update::FitCursor);
365 if (status() == Collapsed) {
366 setStatus(cur, Open);
370 setStatus(cur, Collapsed);
372 cur.bv().cursor() = cur;
376 // The mouse click is within the opened inset.
377 InsetText::doDispatch(cur, cmd);
380 case LFUN_INSET_TOGGLE:
381 if (cmd.argument() == "open")
382 setStatus(cur, Open);
383 else if (cmd.argument() == "close")
384 setStatus(cur, Collapsed);
385 else if (cmd.argument() == "toggle" || cmd.argument().empty())
387 setStatus(cur, Collapsed);
388 cur.forwardPosNoDescend();
391 setStatus(cur, Open);
392 else // if assign or anything else
398 InsetText::doDispatch(cur, cmd);
404 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
405 FuncStatus & flag) const
407 switch (cmd.action) {
409 case LFUN_INSET_TOGGLE:
410 if (cmd.argument() == "open" || cmd.argument() == "close" ||
411 cmd.argument() == "toggle")
418 return InsetText::getStatus(cur, cmd, flag);
423 void InsetCollapsable::setLabel(docstring const & l)
429 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
433 if (status_ == Collapsed)
434 cur.leaveInset(*this);
438 void InsetCollapsable::setLabelFont(LyXFont & font)
443 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
445 FloatList const & floats = bp.getLyXTextClass().floats();
446 FloatList::const_iterator it = floats[type];
448 return (it == floats.end()) ? from_ascii(type) : _(it->second.name());