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/Application.h"
33 #include "frontends/FontLoader.h"
34 #include "frontends/FontMetrics.h"
35 #include "frontends/Painter.h"
38 using lyx::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(lyx::from_ascii("Label")), status_(status),
56 openinlined_(false), autoOpen_(false)
58 setAutoBreakRows(true);
60 setFrameColor(LColor::collapsableframe);
61 setInsetName("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 docstring dlab(label.begin(), label.end());
130 theApp->fontLoader().metrics(labelfont_).buttonText(
131 dlab, dim.wid, dim.asc, dim.des);
136 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
138 autoOpen_ = mi.base.bv->cursor().isInside(this);
139 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
141 if (status() == Inlined) {
142 InsetText::metrics(mi, dim);
144 dim = dimensionCollapsed();
145 if (status() == Open) {
146 InsetText::metrics(mi, textdim_);
147 // This expression should not contain mi.base.texwidth
148 openinlined_ = textdim_.wid < 0.5 * mi.base.bv->workWidth();
150 // Correct for button width, and re-fit
151 mi.base.textwidth -= dim.wid;
152 InsetText::metrics(mi, textdim_);
153 dim.wid += textdim_.wid;
154 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
155 dim.asc = textdim_.asc;
157 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
158 dim.wid = max(dim.wid, textdim_.wid);
162 dim.asc += TEXT_TO_INSET_OFFSET;
163 dim.des += TEXT_TO_INSET_OFFSET;
164 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
165 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
170 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
172 const int xx = x + TEXT_TO_INSET_OFFSET;
173 if (status() == Inlined) {
174 InsetText::draw(pi, xx, y);
176 Dimension dimc = dimensionCollapsed();
177 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
178 button_dim.x1 = xx + 0;
179 button_dim.x2 = xx + dimc.width();
181 button_dim.y2 = top + dimc.height();
183 docstring dlab(label.begin(), label.end());
184 pi.pain.buttonText(xx, top + dimc.asc, dlab, labelfont_);
186 if (status() == Open) {
189 textx = xx + dimc.width();
190 texty = top + textdim_.asc;
193 texty = top + dimc.height() + textdim_.asc;
195 InsetText::draw(pi, textx, texty);
198 setPosCache(pi, x, y);
202 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
204 x += TEXT_TO_INSET_OFFSET;
205 if (status() == Open) {
207 x += dimensionCollapsed().wid;
209 y += dimensionCollapsed().des + textdim_.asc;
211 if (status() != Collapsed)
212 InsetText::drawSelection(pi, x, y);
216 void InsetCollapsable::cursorPos
217 (CursorSlice const & sl, bool boundary, int & x, int & y) const
219 BOOST_ASSERT(status() != Collapsed);
221 InsetText::cursorPos(sl, boundary, x, y);
223 if (status() == Open) {
225 x += dimensionCollapsed().wid;
227 y += dimensionCollapsed().height() - ascent()
228 + TEXT_TO_INSET_OFFSET + textdim_.asc;
230 x += TEXT_TO_INSET_OFFSET;
234 InsetBase::EDITABLE InsetCollapsable::editable() const
236 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
240 bool InsetCollapsable::descendable() const
242 return status() != Collapsed;
246 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
248 return button_dim.contains(cmd.x, cmd.y);
252 docstring const InsetCollapsable::getNewLabel(docstring const & l) const
255 pos_type const max_length = 15;
256 pos_type const p_siz = paragraphs().begin()->size();
257 pos_type const n = min(max_length, p_siz);
260 for (; i < n && j < p_siz; ++j) {
261 if (paragraphs().begin()->isInset(j))
263 label += paragraphs().begin()->getChar(j);
266 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
269 return label.empty() ? l : label;
273 void InsetCollapsable::edit(LCursor & cur, bool left)
275 //lyxerr << "InsetCollapsable: edit left/right" << endl;
277 InsetText::edit(cur, left);
281 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
283 //lyxerr << "InsetCollapsable: edit xy" << endl;
284 if (status() == Collapsed)
287 return InsetText::editXY(cur, x, y);
291 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
293 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
294 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
296 switch (cmd.action) {
297 case LFUN_MOUSE_PRESS:
298 if (status() == Inlined)
299 InsetText::doDispatch(cur, cmd);
300 else if (status() == Open && !hitButton(cmd))
301 InsetText::doDispatch(cur, cmd);
306 case LFUN_MOUSE_MOTION:
307 case LFUN_MOUSE_DOUBLE:
308 case LFUN_MOUSE_TRIPLE:
309 if (status_ == Inlined)
310 InsetText::doDispatch(cur, cmd);
311 else if (status() && !hitButton(cmd))
312 InsetText::doDispatch(cur, cmd);
317 case LFUN_MOUSE_RELEASE:
318 if (cmd.button() == mouse_button::button3) {
319 showInsetDialog(&cur.bv());
326 //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
327 setStatus(cur, Open);
329 cur.bv().cursor() = cur;
333 if (hitButton(cmd)) {
334 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
335 setStatus(cur, Collapsed);
336 cur.bv().cursor() = cur;
338 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
339 InsetText::doDispatch(cur, cmd);
345 //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
346 InsetText::doDispatch(cur, cmd);
351 case LFUN_INSET_TOGGLE:
352 if (cmd.argument() == "open")
353 setStatus(cur, Open);
354 else if (cmd.argument() == "close")
355 setStatus(cur, Collapsed);
356 else if (cmd.argument() == "toggle" || cmd.argument().empty())
358 setStatus(cur, Collapsed);
359 cur.forwardPosNoDescend();
362 setStatus(cur, Open);
363 else // if assign or anything else
369 InsetText::doDispatch(cur, cmd);
375 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
376 FuncStatus & flag) const
378 switch (cmd.action) {
380 case LFUN_INSET_TOGGLE:
381 if (cmd.argument() == "open" || cmd.argument() == "close" ||
382 cmd.argument() == "toggle")
389 return InsetText::getStatus(cur, cmd, flag);
394 void InsetCollapsable::setLabel(docstring const & l)
400 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
404 if (status_ == Collapsed)
405 cur.leaveInset(*this);
409 void InsetCollapsable::setLabelFont(LyXFont & font)
414 docstring InsetCollapsable::floatName(string const & type, BufferParams const & bp)
416 FloatList const & floats = bp.getLyXTextClass().floats();
417 FloatList::const_iterator it = floats[type];
419 return (it == floats.end()) ? lyx::from_ascii(type) : _(it->second.name());