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"
24 #include "funcrequest.h"
25 #include "metricsinfo.h"
26 #include "paragraph.h"
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "frontends/LyXView.h"
33 using lyx::graphics::PreviewLoader;
42 InsetCollapsable::InsetCollapsable(BufferParams const & bp,
43 CollapseStatus status)
44 : InsetText(bp), label("Label"), status_(status), openinlined_(false)
46 setAutoBreakRows(true);
47 setDrawFrame(InsetText::ALWAYS);
48 setFrameColor(LColor::collapsableframe);
49 setInsetName("Collapsable");
54 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
73 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
75 bool token_found = false;
78 string const token = lex.getString();
79 if (token == "status") {
81 string const tmp_token = lex.getString();
83 if (tmp_token == "inlined") {
86 } else if (tmp_token == "collapsed") {
89 } else if (tmp_token == "open") {
93 lyxerr << "InsetCollapsable::read: Missing status!"
95 // Take countermeasures
99 lyxerr << "InsetCollapsable::read: Missing 'status'-tag!"
101 // take countermeasures
102 lex.pushToken(token);
105 InsetText::read(buf, lex);
108 status_ = isOpen() ? Open : Collapsed;
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
116 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
120 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
122 if (status_ == Inlined) {
123 InsetText::metrics(mi, dim);
125 dimension_collapsed(dim);
126 if (status_ == Open) {
128 InsetText::metrics(mi, insetdim);
129 openinlined_ = (insetdim.wid + dim.wid <= mi.base.textwidth);
131 dim.wid += insetdim.wid;
132 dim.des = max(dim.des, insetdim.des);
133 dim.asc = max(dim.asc, insetdim.asc);
135 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
136 dim.wid = max(dim.wid, insetdim.wid);
144 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
146 pi.pain.buttonText(x, y, label, labelfont_);
150 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
152 setPosCache(pi, x, y);
154 if (status_ == Inlined) {
155 InsetText::draw(pi, x, y);
158 dimension_collapsed(dimc);
159 int const aa = ascent();
160 button_dim.x1 = x + 0;
161 button_dim.x2 = x + dimc.width();
162 button_dim.y1 = y - aa + pi.base.bv->top_y();
163 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
165 draw_collapsed(pi, x, y);
166 if (status_ == Open) {
169 InsetText::draw(pi, x + dimc.width(), y - aa + InsetText::ascent());
171 InsetText::draw(pi, x, y - aa + dimc.height() + InsetText::ascent());
177 InsetOld::EDITABLE InsetCollapsable::editable() const
179 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
183 bool InsetCollapsable::descendable() const
185 return status_ != Collapsed;
189 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
191 return button_dim.contains(cmd.x, cmd.y);
195 string const InsetCollapsable::getNewLabel(string const & l) const
198 pos_type const max_length = 15;
199 pos_type const p_siz = paragraphs().begin()->size();
200 pos_type const n = min(max_length, p_siz);
203 for( ; i < n && j < p_siz; ++j) {
204 if (paragraphs().begin()->isInset(j))
206 label += paragraphs().begin()->getChar(j);
209 if (paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
212 return label.empty() ? l : label;
216 void InsetCollapsable::edit(LCursor & cur, bool left)
218 //lyxerr << "InsetCollapsable: edit left/right" << endl;
220 InsetText::edit(cur, left);
225 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
228 //lyxerr << "InsetCollapsable: edit xy" << endl;
229 if (status_ == Collapsed) {
231 // We are not calling editXY() because the row cache of the
232 // inset might be invalid. 'Entering from the left' should be
234 InsetText::edit(cur, true);
237 return InsetText::editXY(cur, x, y);
241 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
243 //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
244 // << " button y: " << button_dim.y2 << endl;
245 switch (cmd.action) {
246 case LFUN_MOUSE_PRESS:
247 if (status_ == Inlined)
248 InsetText::priv_dispatch(cur, cmd);
249 else if (status_ == Open && !hitButton(cmd))
250 InsetText::priv_dispatch(cur, cmd);
253 case LFUN_MOUSE_MOTION:
254 if (status_ == Inlined)
255 InsetText::priv_dispatch(cur, cmd);
256 else if (status_ == Open && !hitButton(cmd))
257 InsetText::priv_dispatch(cur, cmd);
260 case LFUN_MOUSE_RELEASE:
261 if (cmd.button() == mouse_button::button3) {
262 showInsetDialog(&cur.bv());
269 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
275 FuncRequest cmd1 = cmd;
276 if (hitButton(cmd1)) {
277 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
278 setStatus(Collapsed);
280 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
282 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
283 InsetText::priv_dispatch(cur, cmd);
289 lyxerr << "InsetCollapsable::lfunMouseRelease 4" << endl;
290 InsetText::priv_dispatch(cur, cmd);
295 case LFUN_INSET_TOGGLE:
296 if (InsetText::text_.toggleInset(cur))
298 if (status_ == Open) {
302 setStatus(Collapsed);
304 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
308 InsetText::priv_dispatch(cur, cmd);
314 int InsetCollapsable::scroll(bool recursive) const
316 int sx = UpdatableInset::scroll(false);
319 sx += InsetText::scroll(false);
325 void InsetCollapsable::open()
327 if (status_ == Collapsed) // ...but not inlined
332 void InsetCollapsable::close()
334 setStatus(Collapsed);
338 void InsetCollapsable::setLabel(string const & l)
344 void InsetCollapsable::setStatus(CollapseStatus st)
351 void InsetCollapsable::setLabelFont(LyXFont & font)
357 void InsetCollapsable::scroll(BufferView & bv, float sx) const
359 UpdatableInset::scroll(bv, sx);
363 void InsetCollapsable::scroll(BufferView & bv, int offset) const
365 UpdatableInset::scroll(bv, offset);
369 Box const & InsetCollapsable::buttonDim() const