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 : inset(bp), label("Label"), status_(status), openinlined_(false)
47 inset.setAutoBreakRows(true);
48 inset.setDrawFrame(InsetText::ALWAYS);
49 inset.setFrameColor(LColor::collapsableframe);
50 setInsetName("Collapsable");
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
56 : UpdatableInset(in), inset(in.inset),
57 labelfont_(in.labelfont_), label(in.label), status_(in.status_)
64 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
79 inset.text_.write(buf, os);
83 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
85 bool token_found = false;
88 string const token = lex.getString();
89 if (token == "status") {
91 string const tmp_token = lex.getString();
93 if (tmp_token == "inlined") {
96 } else if (tmp_token == "collapsed") {
99 } else if (tmp_token == "open") {
103 lyxerr << "InsetCollapsable::read: Missing status!"
105 // Take countermeasures
106 lex.pushToken(token);
109 lyxerr << "InsetCollapsable::Read: Missing 'status'-tag!"
111 // take countermeasures
112 lex.pushToken(token);
115 inset.read(buf, lex);
118 status_ = isOpen() ? Open : Collapsed;
124 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
126 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
132 if (status_ == Inlined) {
133 inset.metrics(mi, dim);
135 dimension_collapsed(dim);
136 if (status_ == Open) {
138 inset.metrics(mi, insetdim);
139 openinlined_ = (insetdim.wid + dim.wid <= mi.base.textwidth);
141 dim.wid += insetdim.wid;
142 dim.des = max(dim.des, insetdim.des);
143 dim.asc = max(dim.asc, insetdim.asc);
145 dim.des += insetdim.height()
146 + TEXT_TO_BOTTOM_OFFSET;
147 dim.wid = max(dim.wid, insetdim.wid);
155 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
157 pi.pain.buttonText(x, y, label, labelfont_);
161 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
163 setPosCache(pi, x, y);
165 if (status_ == Inlined) {
166 inset.draw(pi, x, y);
169 dimension_collapsed(dimc);
170 int const aa = ascent();
171 button_dim.x1 = x + 0;
172 button_dim.x2 = x + dimc.width();
173 button_dim.y1 = y - aa + pi.base.bv->top_y();
174 button_dim.y2 = y - aa + pi.base.bv->top_y() + dimc.height();
176 draw_collapsed(pi, x, y);
177 if (status_ == Open) {
182 inset.draw(pi, x + dimc.width(), y - aa + inset.ascent());
184 inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
190 void InsetCollapsable::drawSelection(PainterInfo & pi, int x, int y) const
192 inset.drawSelection(pi, x, y);
196 InsetOld::EDITABLE InsetCollapsable::editable() const
198 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
202 bool InsetCollapsable::descendable() const
204 return status_ != Collapsed;
208 void InsetCollapsable::lfunMouseRelease(LCursor & cur, FuncRequest & cmd)
210 if (cmd.button() == mouse_button::button3) {
211 showInsetDialog(&cur.bv());
218 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
224 FuncRequest cmd1 = cmd;
225 if (hitButton(cmd1)) {
226 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
227 setStatus(Collapsed);
228 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
231 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
232 inset.dispatch(cur, cmd);
237 inset.dispatch(cur, cmd);
243 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
244 OutputParams const & runparams) const
246 return inset.latex(buf, os, runparams);
250 int InsetCollapsable::plaintext(Buffer const & buf, ostream & os,
251 OutputParams const & runparams) const
253 return inset.plaintext(buf, os, runparams);
257 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
258 OutputParams const & runparams) const
260 return inset.linuxdoc(buf, os, runparams);
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
265 OutputParams const & runparams) const
267 return inset.docbook(buf, os, runparams);
271 bool InsetCollapsable::hitButton(FuncRequest & cmd) const
273 return button_dim.contains(cmd.x, cmd.y);
277 string const InsetCollapsable::getNewLabel(string const & l) const
280 pos_type const max_length = 15;
281 pos_type const p_siz = inset.paragraphs().begin()->size();
282 pos_type const n = min(max_length, p_siz);
285 for( ; i < n && j < p_siz; ++j) {
286 if (inset.paragraphs().begin()->isInset(j))
288 label += inset.paragraphs().begin()->getChar(j);
291 if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
294 return label.empty() ? l : label;
298 void InsetCollapsable::edit(LCursor & cur, bool left)
300 //lyxerr << "InsetCollapsable: edit left/right" << endl;
302 inset.edit(cur, left);
307 InsetBase * InsetCollapsable::editXY(LCursor & cur, int x, int y)
310 //lyxerr << "InsetCollapsable: edit xy" << endl;
311 if (status_ == Collapsed) {
313 inset.edit(cur, true);
315 //we are not calling edit(x,y) because there are no coordinates in the
316 //inset yet. I personally think it's ok. (ab)
319 return inset.editXY(cur, x, y);
323 void InsetCollapsable::priv_dispatch(LCursor & cur, FuncRequest & cmd)
325 //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
326 // << " button y: " << button_dim.y2 << endl;
327 switch (cmd.action) {
328 case LFUN_MOUSE_PRESS:
329 if (status_ == Inlined)
330 inset.dispatch(cur, cmd);
331 else if (status_ == Open && !hitButton(cmd))
332 inset.dispatch(cur, cmd);
335 case LFUN_MOUSE_MOTION:
336 if (status_ == Inlined)
337 inset.dispatch(cur, cmd);
338 else if (status_ == Open && !hitButton(cmd))
339 inset.dispatch(cur, cmd);
342 case LFUN_MOUSE_RELEASE:
343 lfunMouseRelease(cur, cmd);
346 case LFUN_INSET_TOGGLE:
347 if (inset.text_.toggleInset(cur))
349 if (status_ == Open) {
353 setStatus(Collapsed);
354 cmd = FuncRequest(LFUN_FINISHED_RIGHT);
358 inset.dispatch(cur, cmd);
364 void InsetCollapsable::validate(LaTeXFeatures & features) const
366 inset.validate(features);
370 void InsetCollapsable::getCursorPos(CursorSlice const & cur,
371 int & x, int & y) const
373 inset.getCursorPos(cur, x, y);
377 void InsetCollapsable::getLabelList(Buffer const & buffer,
378 std::vector<string> & list) const
380 inset.getLabelList(buffer, list);
384 int InsetCollapsable::scroll(bool recursive) const
386 int sx = UpdatableInset::scroll(false);
389 sx += inset.scroll(false);
395 size_t InsetCollapsable::nargs() const
397 return inset.nargs();
401 LyXText * InsetCollapsable::getText(int i) const
403 return inset.getText(i);
407 void InsetCollapsable::open()
409 if (status_ == Collapsed) // ...but not inlined
414 void InsetCollapsable::close()
416 setStatus(Collapsed);
420 void InsetCollapsable::setLabel(string const & l)
426 void InsetCollapsable::setStatus(CollapseStatus st)
433 void InsetCollapsable::markErased()
439 void InsetCollapsable::addPreview(PreviewLoader & loader) const
441 inset.addPreview(loader);
445 bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
447 return inset.insetAllowed(code);
451 void InsetCollapsable::setLabelFont(LyXFont & font)
457 void InsetCollapsable::scroll(BufferView & bv, float sx) const
459 UpdatableInset::scroll(bv, sx);
463 void InsetCollapsable::scroll(BufferView & bv, int offset) const
465 UpdatableInset::scroll(bv, offset);
469 Box const & InsetCollapsable::buttonDim() const
475 void InsetCollapsable::setBackgroundColor(LColor_color color)
477 InsetOld::setBackgroundColor(color);
478 inset.setBackgroundColor(color);