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 "BufferView.h"
21 #include "dispatchresult.h"
22 #include "FuncStatus.h"
25 #include "funcrequest.h"
26 #include "metricsinfo.h"
27 #include "paragraph.h"
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31 #include "frontends/LyXView.h"
34 using lyx::graphics::PreviewLoader;
43 InsetCollapsable::CollapseStatus InsetCollapsable::status() const
45 return (autoOpen_ && status_ != Inlined) ? Open : status_;
49 InsetCollapsable::InsetCollapsable
50 (BufferParams const & bp, CollapseStatus status)
51 : InsetText(bp), label("Label"), status_(status),
52 openinlined_(false), autoOpen_(false)
54 setAutoBreakRows(true);
56 setFrameColor(LColor::collapsableframe);
57 setInsetName("Collapsable");
62 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
81 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
83 bool token_found = false;
86 string const token = lex.getString();
87 if (token == "status") {
89 string const tmp_token = lex.getString();
91 if (tmp_token == "inlined") {
94 } else if (tmp_token == "collapsed") {
97 } else if (tmp_token == "open") {
101 lyxerr << "InsetCollapsable::read: Missing status!"
103 // Take countermeasures
104 lex.pushToken(token);
107 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
109 // take countermeasures
110 lex.pushToken(token);
113 InsetText::read(buf, lex);
116 status_ = isOpen() ? Open : Collapsed;
122 Dimension InsetCollapsable::dimensionCollapsed() const
125 docstring dlab(label.begin(), label.end());
126 font_metrics::buttonText(dlab, labelfont_, dim.wid, dim.asc, dim.des);
131 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
133 autoOpen_ = mi.base.bv->cursor().isInside(this);
134 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
136 if (status() == Inlined) {
137 InsetText::metrics(mi, dim);
139 dim = dimensionCollapsed();
140 if (status() == Open) {
141 InsetText::metrics(mi, textdim_);
142 // This expression should not contain mi.base.texwidth
143 openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth();
145 // Correct for button width, and re-fit
146 mi.base.textwidth -= dim.wid;
147 InsetText::metrics(mi, textdim_);
148 dim.wid += textdim_.wid;
149 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
150 dim.asc = textdim_.asc;
152 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
153 dim.wid = max(dim.wid, textdim_.wid);
157 dim.asc += TEXT_TO_INSET_OFFSET;
158 dim.des += TEXT_TO_INSET_OFFSET;
159 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
160 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
165 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
167 const int xx = x + TEXT_TO_INSET_OFFSET;
168 if (status() == Inlined) {
169 InsetText::draw(pi, xx, y);
171 Dimension dimc = dimensionCollapsed();
172 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
173 button_dim.x1 = xx + 0;
174 button_dim.x2 = xx + dimc.width();
176 button_dim.y2 = top + dimc.height();
178 docstring dlab(label.begin(), label.end());
179 pi.pain.buttonText(xx, top + dimc.asc, dlab, labelfont_);
181 if (status() == Open) {
184 textx = xx + dimc.width();
185 texty = top + textdim_.asc;
188 texty = top + dimc.height() + textdim_.asc;
190 InsetText::draw(pi, textx, texty);
193 setPosCache(pi, x, y);
197 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
199 x += TEXT_TO_INSET_OFFSET;
200 if (status() == Open) {
202 x += dimensionCollapsed().wid;
204 y += dimensionCollapsed().des + textdim_.asc;
206 if (status() != Collapsed)
207 InsetText::drawSelection(pi, x, y);
211 void InsetCollapsable::cursorPos
212 (CursorSlice const & sl, bool boundary, int & x, int & y) const
214 BOOST_ASSERT(status() != Collapsed);
216 InsetText::cursorPos(sl, boundary, x, y);
218 if (status() == Open) {
220 x += dimensionCollapsed().wid;
222 y += dimensionCollapsed().height() - ascent()
223 + TEXT_TO_INSET_OFFSET + textdim_.asc;
225 x += TEXT_TO_INSET_OFFSET;
229 InsetBase::EDITABLE InsetCollapsable::editable() const
231 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
235 bool InsetCollapsable::descendable() const
237 return status() != Collapsed;
241 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
243 return button_dim.contains(cmd.x, cmd.y);
247 string const InsetCollapsable::getNewLabel(string const & l) const
250 pos_type const max_length = 15;
251 pos_type const p_siz = paragraphs().begin()->size();
252 pos_type const n = min(max_length, p_siz);
255 for (; i < n && j < p_siz; ++j) {
256 if (paragraphs().begin()->isInset(j))
258 label += paragraphs().begin()->getChar(j);
261 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
264 return label.empty() ? l : label;
268 void InsetCollapsable::edit(LCursor & cur, bool left)
270 //lyxerr << "InsetCollapsable: edit left/right" << endl;
272 InsetText::edit(cur, left);
276 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
278 //lyxerr << "InsetCollapsable: edit xy" << endl;
279 if (status() == Collapsed)
282 return InsetText::editXY(cur, x, y);
286 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
288 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
289 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
291 switch (cmd.action) {
292 case LFUN_MOUSE_PRESS:
293 if (status() == Inlined)
294 InsetText::doDispatch(cur, cmd);
295 else if (status() == Open && !hitButton(cmd))
296 InsetText::doDispatch(cur, cmd);
301 case LFUN_MOUSE_MOTION:
302 case LFUN_MOUSE_DOUBLE:
303 case LFUN_MOUSE_TRIPLE:
304 if (status_ == Inlined)
305 InsetText::doDispatch(cur, cmd);
306 else if (status() && !hitButton(cmd))
307 InsetText::doDispatch(cur, cmd);
312 case LFUN_MOUSE_RELEASE:
313 if (cmd.button() == mouse_button::button3) {
314 showInsetDialog(&cur.bv());
321 //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
322 setStatus(cur, Open);
324 cur.bv().cursor() = cur;
328 if (hitButton(cmd)) {
329 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
330 setStatus(cur, Collapsed);
331 cur.bv().cursor() = cur;
333 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
334 InsetText::doDispatch(cur, cmd);
340 //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
341 InsetText::doDispatch(cur, cmd);
346 case LFUN_INSET_TOGGLE:
347 if (cmd.argument() == "open")
348 setStatus(cur, Open);
349 else if (cmd.argument() == "close")
350 setStatus(cur, Collapsed);
351 else if (cmd.argument() == "toggle" || cmd.argument().empty())
353 setStatus(cur, Collapsed);
354 cur.forwardPosNoDescend();
357 setStatus(cur, Open);
358 else // if assign or anything else
364 InsetText::doDispatch(cur, cmd);
370 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
371 FuncStatus & flag) const
373 switch (cmd.action) {
375 case LFUN_INSET_TOGGLE:
376 if (cmd.argument() == "open" || cmd.argument() == "close" ||
377 cmd.argument() == "toggle")
384 return InsetText::getStatus(cur, cmd, flag);
389 void InsetCollapsable::setLabel(string const & l)
395 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
399 if (status_ == Collapsed)
400 cur.leaveInset(*this);
404 void InsetCollapsable::setLabelFont(LyXFont & font)