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 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
132 autoOpen_ = mi.base.bv->cursor().isInside(this);
133 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
135 if (status() == Inlined) {
136 InsetText::metrics(mi, dim);
138 dim = dimensionCollapsed();
139 if (status() == Open) {
140 InsetText::metrics(mi, textdim_);
141 openinlined_ = textdim_.wid + 2 * dim.wid <= mi.base.textwidth;
143 dim.wid += textdim_.wid;
144 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
145 dim.asc = textdim_.asc;
147 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
148 dim.wid = max(dim.wid, textdim_.wid);
152 dim.asc += TEXT_TO_INSET_OFFSET;
153 dim.des += TEXT_TO_INSET_OFFSET;
154 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
155 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
160 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
162 const int xx = x + TEXT_TO_INSET_OFFSET;
163 if (status() == Inlined) {
164 InsetText::draw(pi, xx, y);
166 Dimension dimc = dimensionCollapsed();
167 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
168 button_dim.x1 = xx + 0;
169 button_dim.x2 = xx + dimc.width();
171 button_dim.y2 = top + dimc.height();
173 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
175 if (status() == Open) {
178 textx = xx + dimc.width();
179 texty = top + textdim_.asc;
182 texty = top + dimc.height() + textdim_.asc;
184 InsetText::draw(pi, textx, texty);
187 setPosCache(pi, x, y);
191 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
193 x += TEXT_TO_INSET_OFFSET;
194 if (status() == Open) {
196 x += dimensionCollapsed().wid;
198 y += dimensionCollapsed().des + textdim_.asc;
200 if (status() != Collapsed)
201 InsetText::drawSelection(pi, x, y);
205 void InsetCollapsable::cursorPos
206 (CursorSlice const & sl, bool boundary, int & x, int & y) const
208 BOOST_ASSERT(status() != Collapsed);
210 InsetText::cursorPos(sl, boundary, x, y);
212 if (status() == Open) {
214 x += dimensionCollapsed().wid;
216 y += dimensionCollapsed().height() - ascent()
217 + TEXT_TO_INSET_OFFSET + textdim_.asc;
219 x += TEXT_TO_INSET_OFFSET;
223 InsetBase::EDITABLE InsetCollapsable::editable() const
225 return status() != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
229 bool InsetCollapsable::descendable() const
231 return status() != Collapsed;
235 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
237 return button_dim.contains(cmd.x, cmd.y);
241 string const InsetCollapsable::getNewLabel(string const & l) const
244 pos_type const max_length = 15;
245 pos_type const p_siz = paragraphs().begin()->size();
246 pos_type const n = min(max_length, p_siz);
249 for (; i < n && j < p_siz; ++j) {
250 if (paragraphs().begin()->isInset(j))
252 label += paragraphs().begin()->getChar(j);
255 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
258 return label.empty() ? l : label;
262 void InsetCollapsable::edit(LCursor & cur, bool left)
264 //lyxerr << "InsetCollapsable: edit left/right" << endl;
266 InsetText::edit(cur, left);
270 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
272 //lyxerr << "InsetCollapsable: edit xy" << endl;
273 if (status() == Collapsed)
276 return InsetText::editXY(cur, x, y);
280 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
282 //lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
283 // << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
285 switch (cmd.action) {
286 case LFUN_MOUSE_PRESS:
287 if (status() == Inlined)
288 InsetText::doDispatch(cur, cmd);
289 else if (status() == Open && !hitButton(cmd))
290 InsetText::doDispatch(cur, cmd);
295 case LFUN_MOUSE_MOTION:
296 case LFUN_MOUSE_DOUBLE:
297 case LFUN_MOUSE_TRIPLE:
298 if (status_ == Inlined)
299 InsetText::doDispatch(cur, cmd);
300 else if (status() && !hitButton(cmd))
301 InsetText::doDispatch(cur, cmd);
306 case LFUN_MOUSE_RELEASE:
307 if (cmd.button() == mouse_button::button3) {
308 showInsetDialog(&cur.bv());
315 //lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
316 setStatus(cur, Open);
318 cur.bv().cursor() = cur;
322 if (hitButton(cmd)) {
323 //lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
324 setStatus(cur, Collapsed);
325 cur.bv().cursor() = cur;
327 //lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
328 InsetText::doDispatch(cur, cmd);
334 //lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
335 InsetText::doDispatch(cur, cmd);
340 case LFUN_INSET_TOGGLE:
341 if (cmd.argument == "open")
342 setStatus(cur, Open);
343 else if (cmd.argument == "close")
344 setStatus(cur, Collapsed);
345 else if (cmd.argument == "toggle" || cmd.argument.empty())
347 setStatus(cur, Collapsed);
348 cur.forwardPosNoDescend();
351 setStatus(cur, Open);
352 else // if assign or anything else
358 InsetText::doDispatch(cur, cmd);
364 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
365 FuncStatus & flag) const
367 switch (cmd.action) {
369 case LFUN_INSET_TOGGLE:
370 if (cmd.argument == "open" || cmd.argument == "close" ||
371 cmd.argument == "toggle")
378 return InsetText::getStatus(cur, cmd, flag);
383 void InsetCollapsable::setLabel(string const & l)
389 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
393 if (status_ == Collapsed)
394 cur.leaveInset(*this);
398 void InsetCollapsable::setLabelFont(LyXFont & font)