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)
58 setAutoBreakRows(true);
60 setFrameColor(LColor::collapsableframe);
61 setInsetName(from_ascii("Collapsable"));
66 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
85 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
87 bool token_found = false;
90 string const token = lex.getString();
91 if (token == "status") {
93 string const tmp_token = lex.getString();
95 if (tmp_token == "inlined") {
98 } else if (tmp_token == "collapsed") {
101 } else if (tmp_token == "open") {
105 lyxerr << "InsetCollapsable::read: Missing status!"
107 // Take countermeasures
108 lex.pushToken(token);
111 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
113 // take countermeasures
114 lex.pushToken(token);
117 InsetText::read(buf, lex);
120 status_ = isOpen() ? Open : Collapsed;
126 Dimension InsetCollapsable::dimensionCollapsed() const
129 theFontMetrics(labelfont_).buttonText(
130 label, dim.wid, dim.asc, dim.des);
135 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
137 autoOpen_ = mi.base.bv->cursor().isInside(this);
138 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
140 if (status() == Inlined) {
141 InsetText::metrics(mi, dim);
143 dim = dimensionCollapsed();
144 if (status() == Open) {
145 InsetText::metrics(mi, textdim_);
146 // This expression should not contain mi.base.texwidth
147 openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth();
149 // Correct for button width, and re-fit
150 mi.base.textwidth -= dim.wid;
151 InsetText::metrics(mi, textdim_);
152 dim.wid += textdim_.wid;
153 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
154 dim.asc = textdim_.asc;
156 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
157 dim.wid = max(dim.wid, textdim_.wid);
161 dim.asc += TEXT_TO_INSET_OFFSET;
162 dim.des += TEXT_TO_INSET_OFFSET;
163 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
164 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
169 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
171 const int xx = x + TEXT_TO_INSET_OFFSET;
172 if (status() == Inlined) {
173 InsetText::draw(pi, xx, y);
175 Dimension dimc = dimensionCollapsed();
176 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
177 button_dim.x1 = xx + 0;
178 button_dim.x2 = xx + dimc.width();
180 button_dim.y2 = top + dimc.height();
182 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
184 if (status() == Open) {
187 textx = xx + dimc.width();
188 texty = top + textdim_.asc;
191 texty = top + dimc.height() + textdim_.asc;
193 InsetText::draw(pi, textx, texty);
196 setPosCache(pi, x, y);
200 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
202 x += TEXT_TO_INSET_OFFSET;
203 if (status() == Open) {
205 x += dimensionCollapsed().wid;
207 y += dimensionCollapsed().des + textdim_.asc;
209 if (status() != Collapsed)
210 InsetText::drawSelection(pi, x, y);
214 void InsetCollapsable::cursorPos(BufferView const & bv,
215 CursorSlice const & sl, bool boundary, int & x, int & y) const
217 BOOST_ASSERT(status() != Collapsed);
219 InsetText::cursorPos(bv, sl, boundary, x, y);
221 if (status() == Open) {
223 x += dimensionCollapsed().wid;
225 y += dimensionCollapsed().height() - ascent()
226 + TEXT_TO_INSET_OFFSET + textdim_.asc;
228 x += TEXT_TO_INSET_OFFSET;
232 InsetBase::EDITABLE InsetCollapsable::editable() const
234 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
238 bool InsetCollapsable::descendable() const
240 return status() != Collapsed;
244 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
246 return button_dim.contains(cmd.x, cmd.y);
250 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
253 pos_type const max_length = 15;
254 pos_type const p_siz = paragraphs().begin()->size();
255 pos_type const n = min(max_length, p_siz);
258 for (; i < n && j < p_siz; ++j) {
259 if (paragraphs().begin()->isInset(j))
261 label += paragraphs().begin()->getChar(j);
264 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
267 return label.empty() ? l : label;
271 void InsetCollapsable::edit(LCursor & cur, bool left)
273 //lyxerr << "InsetCollapsable: edit left/right" << endl;
275 InsetText::edit(cur, left);
279 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
281 //lyxerr << "InsetCollapsable: edit xy" << endl;
282 if (status() == Collapsed)
285 return InsetText::editXY(cur, x, y);
289 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
291 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
292 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
294 switch (cmd.action) {
295 case LFUN_MOUSE_PRESS:
296 if (status() == Inlined)
297 InsetText::doDispatch(cur, cmd);
298 else if (status() == Open && !hitButton(cmd))
299 InsetText::doDispatch(cur, cmd);
304 case LFUN_MOUSE_MOTION:
305 case LFUN_MOUSE_DOUBLE:
306 case LFUN_MOUSE_TRIPLE:
307 if (status_ == Inlined)
308 InsetText::doDispatch(cur, cmd);
309 else if (status() && !hitButton(cmd))
310 InsetText::doDispatch(cur, cmd);
315 case LFUN_MOUSE_RELEASE:
316 if (cmd.button() == mouse_button::button3) {
317 showInsetDialog(&cur.bv());
324 //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
325 setStatus(cur, Open);
327 cur.bv().cursor() = cur;
331 if (hitButton(cmd)) {
332 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
333 setStatus(cur, Collapsed);
334 cur.bv().cursor() = cur;
336 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
337 InsetText::doDispatch(cur, cmd);
343 //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
344 InsetText::doDispatch(cur, cmd);
349 case LFUN_INSET_TOGGLE:
350 if (cmd.argument() == "open")
351 setStatus(cur, Open);
352 else if (cmd.argument() == "close")
353 setStatus(cur, Collapsed);
354 else if (cmd.argument() == "toggle" || cmd.argument().empty())
356 setStatus(cur, Collapsed);
357 cur.forwardPosNoDescend();
360 setStatus(cur, Open);
361 else // if assign or anything else
367 InsetText::doDispatch(cur, cmd);
373 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
374 FuncStatus & flag) const
376 switch (cmd.action) {
378 case LFUN_INSET_TOGGLE:
379 if (cmd.argument() == "open" || cmd.argument() == "close" ||
380 cmd.argument() == "toggle")
387 return InsetText::getStatus(cur, cmd, flag);
392 void InsetCollapsable::setLabel(docstring const & l)
398 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
402 if (status_ == Collapsed)
403 cur.leaveInset(*this);
407 void InsetCollapsable::setLabelFont(LyXFont & font)
412 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
414 FloatList const & floats = bp.getLyXTextClass().floats();
415 FloatList::const_iterator it = floats[type];
417 return (it == floats.end()) ? from_ascii(type) : _(it->second.name());