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::InsetCollapsable
44 (BufferParams const & bp, CollapseStatus status)
45 : InsetText(bp), label("Label"), status_(status), openinlined_(false)
47 setAutoBreakRows(true);
49 setFrameColor(LColor::collapsableframe);
50 setInsetName("Collapsable");
55 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
74 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
76 bool token_found = false;
79 string const token = lex.getString();
80 if (token == "status") {
82 string const tmp_token = lex.getString();
84 if (tmp_token == "inlined") {
87 } else if (tmp_token == "collapsed") {
90 } else if (tmp_token == "open") {
94 lyxerr << "InsetCollapsable::read: Missing status!"
96 // Take countermeasures
100 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
102 // take countermeasures
103 lex.pushToken(token);
106 InsetText::read(buf, lex);
109 status_ = isOpen() ? Open : Collapsed;
115 Dimension InsetCollapsable::dimensionCollapsed() const
118 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
123 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
125 mi.base.textwidth -= 2 * TEXT_TO_INSET_OFFSET;
126 if (status_ == Inlined) {
127 InsetText::metrics(mi, dim);
129 dim = dimensionCollapsed();
130 if (status_ == Open) {
131 InsetText::metrics(mi, textdim_);
132 openinlined_ = (textdim_.wid + dim.wid <= mi.base.textwidth);
134 dim.wid += textdim_.wid;
135 dim.des = max(dim.des - textdim_.asc + dim.asc, textdim_.des);
136 dim.asc = textdim_.asc;
138 dim.des += textdim_.height() + TEXT_TO_BOTTOM_OFFSET;
139 dim.wid = max(dim.wid, textdim_.wid);
143 dim.asc += TEXT_TO_INSET_OFFSET;
144 dim.des += TEXT_TO_INSET_OFFSET;
145 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
146 mi.base.textwidth += 2 * TEXT_TO_INSET_OFFSET;
151 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
153 const int xx = x + TEXT_TO_INSET_OFFSET;
154 if (status_ == Inlined) {
155 InsetText::draw(pi, xx, y);
157 Dimension dimc = dimensionCollapsed();
158 int const top = y - ascent() + TEXT_TO_INSET_OFFSET;
159 button_dim.x1 = xx + 0;
160 button_dim.x2 = xx + dimc.width();
162 button_dim.y2 = top + dimc.height();
164 pi.pain.buttonText(xx, top + dimc.asc, label, labelfont_);
165 if (status_ == Open) {
168 textx = xx + dimc.width();
169 texty = top + textdim_.asc;
172 texty = top + dimc.height() + textdim_.asc;
174 InsetText::draw(pi, textx, texty);
177 setPosCache(pi, x, y);
181 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
183 x += TEXT_TO_INSET_OFFSET;
184 if (status_ == Open) {
186 x += dimensionCollapsed().wid;
188 y += dimensionCollapsed().des + textdim_.asc;
190 if (status_ != Collapsed)
191 InsetText::drawSelection(pi, x, y);
195 void InsetCollapsable::getCursorPos
196 (CursorSlice const & sl, int & x, int & y) const
198 if (status_ == Collapsed) {
204 InsetText::getCursorPos(sl, x, y);
205 if (status_ == Open) {
207 x += dimensionCollapsed().wid;
209 y += dimensionCollapsed().height() - ascent() + TEXT_TO_INSET_OFFSET + textdim_.asc;
212 x += TEXT_TO_INSET_OFFSET;
216 InsetBase::EDITABLE InsetCollapsable::editable() const
218 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
222 bool InsetCollapsable::descendable() const
224 return status_ != Collapsed;
228 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
230 return button_dim.contains(cmd.x, cmd.y);
234 string const InsetCollapsable::getNewLabel(string const & l) const
237 pos_type const max_length = 15;
238 pos_type const p_siz = paragraphs().begin()->size();
239 pos_type const n = min(max_length, p_siz);
242 for( ; i < n && j < p_siz; ++j) {
243 if (paragraphs().begin()->isInset(j))
245 label += paragraphs().begin()->getChar(j);
248 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
251 return label.empty() ? l : label;
255 void InsetCollapsable::edit(LCursor & cur, bool left)
257 //lyxerr << "InsetCollapsable: edit left/right" << endl;
259 InsetText::edit(cur, left);
264 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y) const
266 //lyxerr << "InsetCollapsable: edit xy" << endl;
267 if (status_ == Collapsed) {
268 return const_cast<InsetCollapsable*>(this);
270 cur.push(const_cast<InsetCollapsable&>(*this));
271 return InsetText::editXY(cur, x, y);
275 void InsetCollapsable::doDispatch(LCursor & cur, FuncRequest & cmd)
277 lyxerr << "InsetCollapsable::doDispatch (begin): cmd: " << cmd
278 << " cur: " << cur << " bvcur: " << cur.bv().cursor() << endl;
280 switch (cmd.action) {
281 case LFUN_MOUSE_PRESS:
282 if (status_ == Inlined)
283 InsetText::doDispatch(cur, cmd);
284 else if (status_ == Open && !hitButton(cmd))
285 InsetText::doDispatch(cur, cmd);
290 case LFUN_MOUSE_MOTION:
291 case LFUN_MOUSE_DOUBLE:
292 case LFUN_MOUSE_TRIPLE:
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_RELEASE:
302 if (cmd.button() == mouse_button::button3) {
303 showInsetDialog(&cur.bv());
310 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
311 setStatus(cur, Open);
313 cur.bv().cursor() = cur;
317 if (hitButton(cmd)) {
318 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
319 setStatus(cur, Collapsed);
320 cur.bv().cursor() = cur;
322 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
323 InsetText::doDispatch(cur, cmd);
329 lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
330 InsetText::doDispatch(cur, cmd);
335 case LFUN_INSET_TOGGLE:
336 if (cmd.argument == "open")
337 setStatus(cur, Open);
338 else if (cmd.argument == "close")
339 setStatus(cur, Collapsed);
340 else if (cmd.argument == "toggle" || cmd.argument.empty())
341 setStatus(cur, isOpen() ? Collapsed : Open);
342 else // if assign or anything else
348 InsetText::doDispatch(cur, cmd);
354 bool InsetCollapsable::getStatus(LCursor & cur, FuncRequest const & cmd,
355 FuncStatus & flag) const
357 switch (cmd.action) {
359 case LFUN_INSET_TOGGLE:
360 if (cmd.argument == "open" || cmd.argument == "close" ||
361 cmd.argument == "toggle")
368 return InsetText::getStatus(cur, cmd, flag);
373 int InsetCollapsable::scroll(bool recursive) const
375 int sx = UpdatableInset::scroll(false);
378 sx += InsetText::scroll(false);
384 void InsetCollapsable::setLabel(string const & l)
390 void InsetCollapsable::setStatus(LCursor & cur, CollapseStatus status)
394 if (status_ == Collapsed)
395 cur.leaveInset(*this);
399 void InsetCollapsable::setLabelFont(LyXFont & font)
405 void InsetCollapsable::scroll(BufferView & bv, double sx) const
407 UpdatableInset::scroll(bv, sx);
411 void InsetCollapsable::scroll(BufferView & bv, int offset) const
413 UpdatableInset::scroll(bv, offset);