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 -= (int) (1.5 * 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);
177 dim.wid = max(dim.wid, mi.base.textwidth);
181 dim.asc += TEXT_TO_INSET_OFFSET;
182 dim.des += TEXT_TO_INSET_OFFSET;
183 dim.wid += (int) (1.5 * TEXT_TO_INSET_OFFSET);
184 mi.base.textwidth += (int) (1.5 * TEXT_TO_INSET_OFFSET);
185 bool const changed = dim_ != dim;
191 bool InsetCollapsable::setMouseHover(bool mouse_hover)
193 mouse_hover_ = mouse_hover;
198 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
200 const int xx = x + TEXT_TO_INSET_OFFSET;
201 if (status() == Inlined) {
202 InsetText::draw(pi, xx, y);
204 Dimension dimc = dimensionCollapsed();
205 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
206 button_dim.x1 = xx + 0;
207 button_dim.x2 = xx + dimc.width();
209 button_dim.y2 = top + dimc.height();
211 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_, mouse_hover_);
213 if (status() == Open) {
216 textx = xx + dimc.width();
217 texty = top + textdim_.asc;
220 texty = top + dimc.height() + textdim_.asc;
222 InsetText::draw(pi, textx, texty);
225 setPosCache(pi, x, y);
229 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
231 x += TEXT_TO_INSET_OFFSET;
232 if (status() == Open) {
234 x += dimensionCollapsed().wid;
236 y += dimensionCollapsed().des + textdim_.asc;
238 if (status() != Collapsed)
239 InsetText::drawSelection(pi, x, y);
243 void InsetCollapsable::cursorPos(BufferView const & bv,
244 CursorSlice const & sl, bool boundary, int & x, int & y) const
246 BOOST_ASSERT(status() != Collapsed);
248 InsetText::cursorPos(bv, sl, boundary, x, y);
250 if (status() == Open) {
252 x += dimensionCollapsed().wid;
254 y += dimensionCollapsed().height() - ascent()
255 + TEXT_TO_INSET_OFFSET + textdim_.asc;
257 x += TEXT_TO_INSET_OFFSET;
261 InsetBase::EDITABLE InsetCollapsable::editable() const
263 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
267 bool InsetCollapsable::descendable() const
269 return status() != Collapsed;
273 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
275 return button_dim.contains(cmd.x, cmd.y);
279 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
282 pos_type const max_length = 15;
283 pos_type const p_siz = paragraphs().begin()->size();
284 pos_type const n = min(max_length, p_siz);
287 for (; i < n && j < p_siz; ++j) {
288 if (paragraphs().begin()->isInset(j))
290 label += paragraphs().begin()->getChar(j);
293 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
296 return label.empty() ? l : label;
300 void InsetCollapsable::edit(LCursor & cur, bool left)
302 //lyxerr << "InsetCollapsable: edit left/right" << endl;
304 InsetText::edit(cur, left);
308 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
310 //lyxerr << "InsetCollapsable: edit xy" << endl;
311 if (status() == Collapsed || (button_dim.contains(x, y) && status() != Inlined))
314 return InsetText::editXY(cur, x, y);
318 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
320 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
321 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
323 switch (cmd.action) {
324 case LFUN_MOUSE_PRESS:
325 if (cmd.button() == mouse_button::button1 && hitButton(cmd) && status() != Inlined) {
326 // reset selection if necessary (see bug 3060)
328 cur.bv().cursor().clearSelection();
334 if (status() == Inlined)
335 InsetText::doDispatch(cur, cmd);
336 else if (status() == Open && !hitButton(cmd))
337 InsetText::doDispatch(cur, cmd);
342 case LFUN_MOUSE_MOTION:
343 case LFUN_MOUSE_DOUBLE:
344 case LFUN_MOUSE_TRIPLE:
345 if (status_ == Inlined)
346 InsetText::doDispatch(cur, cmd);
347 else if (status() && !hitButton(cmd))
348 InsetText::doDispatch(cur, cmd);
353 case LFUN_MOUSE_RELEASE:
354 if (cmd.button() == mouse_button::button3) {
355 // Open the Inset configuration dialog
356 showInsetDialog(&cur.bv());
360 if (status() == Inlined) {
361 // The mouse click has to be within the inset!
362 InsetText::doDispatch(cur, cmd);
366 if (cmd.button() == mouse_button::button1 && hitButton(cmd)) {
367 // if we are selecting, we do not want to
371 // Left button is clicked, the user asks to
372 // toggle the inset visual state.
374 cur.updateFlags(Update::Force | Update::FitCursor);
375 if (status() == Collapsed) {
376 setStatus(cur, Open);
380 setStatus(cur, Collapsed);
382 cur.bv().cursor() = cur;
386 // The mouse click is within the opened inset.
387 if (status() == Open)
388 InsetText::doDispatch(cur, cmd);
391 case LFUN_INSET_TOGGLE:
392 if (cmd.argument() == "open")
393 setStatus(cur, Open);
394 else if (cmd.argument() == "close")
395 setStatus(cur, Collapsed);
396 else if (cmd.argument() == "toggle" || cmd.argument().empty())
398 setStatus(cur, Collapsed);
399 cur.forwardPosNoDescend();
402 setStatus(cur, Open);
403 else // if assign or anything else
409 InsetText::doDispatch(cur, cmd);
415 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
416 FuncStatus & flag) const
418 switch (cmd.action) {
420 case LFUN_INSET_TOGGLE:
421 if (cmd.argument() == "open" || cmd.argument() == "close" ||
422 cmd.argument() == "toggle")
429 return InsetText::getStatus(cur, cmd, flag);
434 void InsetCollapsable::setLabel(docstring const & l)
440 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
444 if (status_ == Collapsed)
445 cur.leaveInset(*this);
446 // Because we save CollapseStatus in lyx file, change of status
447 // should lead to a dirty buffer. (This fixes bug 2993).
448 cur.bv().buffer()->markDirty();
452 void InsetCollapsable::setLabelFont(LyXFont & font)
457 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp) const
459 FloatList const & floats = bp.getLyXTextClass().floats();
460 FloatList::const_iterator it = floats[type];
462 return (it == floats.end()) ? from_ascii(type) : _(it->second.name());