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, CollapseStatus status)
43 : UpdatableInset(), inset(bp), status_(status),
47 inset.setAutoBreakRows(true);
48 inset.setDrawFrame(InsetText::ALWAYS);
49 inset.setFrameColor(LColor::collapsableframe);
50 setInsetName("Collapsable");
56 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
57 : UpdatableInset(in), inset(in.inset), status_(in.status_),
58 labelfont_(in.labelfont_), label(in.label)
65 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
80 os << "status " << st << "\n";
81 inset.text_.write(buf, os);
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 inset.read(buf, lex);
130 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
132 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
136 int InsetCollapsable::height_collapsed() const
139 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
140 return dim.asc + dim.des;
144 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
146 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
147 if (status_ == Inlined) {
148 inset.metrics(mi, dim);
150 dimension_collapsed(dim);
151 if (status_ == Open) {
153 inset.metrics(mi, insetdim);
154 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
155 dim.wid = max(dim.wid, insetdim.wid);
159 //lyxerr << "InsetCollapsable::metrics: dim.wid: " << dim.wid << endl;
163 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
165 pi.pain.buttonText(x, y, label, labelfont_);
169 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
174 if (status_ == Inlined) {
175 inset.draw(pi, x, y);
178 dimension_collapsed(dimc);
180 int const aa = ascent();
182 button_dim.x2 = dimc.width();
184 button_dim.y2 = -aa + dimc.height();
186 draw_collapsed(pi, x, y);
187 if (status_ == Open) {
190 inset.draw(pi, x, y - aa + dimc.height() + inset.ascent());
196 InsetOld::EDITABLE InsetCollapsable::editable() const
198 return status_ != Collapsed ? HIGHLY_EDITABLE : IS_EDITABLE;
202 bool InsetCollapsable::descendable() const
204 return status_ != Collapsed;
208 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
210 FuncRequest cmd1 = cmd;
211 cmd1.y += ascent() - height_collapsed();
216 DispatchResult InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
218 BufferView * bv = cmd.view();
220 if (cmd.button() == mouse_button::button3) {
221 lyxerr << "InsetCollapsable::lfunMouseRelease 0" << endl;
223 return DispatchResult(true, true);
228 lyxerr << "InsetCollapsable::lfunMouseRelease 1" << endl;
231 return DispatchResult(true, true);
234 if (hitButton(cmd)) {
235 lyxerr << "InsetCollapsable::lfunMouseRelease 2" << endl;
236 setStatus(Collapsed);
237 return DispatchResult(false, FINISHED_RIGHT);
239 lyxerr << "InsetCollapsable::lfunMouseRelease 3" << endl;
240 return inset.dispatch(adjustCommand(cmd));
244 return inset.dispatch(cmd);
247 return DispatchResult(true, true);
251 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
252 OutputParams const & runparams) const
254 return inset.latex(buf, os, runparams);
258 int InsetCollapsable::plaintext(Buffer const & buf, ostream & os,
259 OutputParams const & runparams) const
261 return inset.plaintext(buf, os, runparams);
265 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
266 OutputParams const & runparams) const
268 return inset.linuxdoc(buf, os, runparams);
272 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
273 OutputParams const & runparams) const
275 return inset.docbook(buf, os, runparams);
279 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
281 return button_dim.contains(cmd.x, cmd.y);
285 string const InsetCollapsable::getNewLabel(string const & l) const
288 pos_type const max_length = 15;
289 pos_type const p_siz = inset.paragraphs().begin()->size();
290 pos_type const n = min(max_length, p_siz);
293 for( ; i < n && j < p_siz; ++j) {
294 if (inset.paragraphs().begin()->isInset(j))
296 la += inset.paragraphs().begin()->getChar(j);
299 if (inset.paragraphs().size() > 1 || (i > 0 && j < p_siz)) {
302 return la.empty() ? l : la;
306 void InsetCollapsable::edit(BufferView * bv, bool left)
308 lyxerr << "InsetCollapsable: edit left/right" << endl;
309 inset.edit(bv, left);
311 bv->fullCursor().push(this);
315 void InsetCollapsable::edit(BufferView * bv, int x, int y)
317 lyxerr << "InsetCollapsable: edit xy" << endl;
318 if (status_ == Collapsed) {
321 if (y <= button_dim.y2)
324 y += inset.ascent() - height_collapsed();
326 inset.edit(bv, x, y);
327 bv->fullCursor().push(this);
332 InsetCollapsable::priv_dispatch(FuncRequest const & cmd, idx_type &, pos_type &)
334 //lyxerr << "\nInsetCollapsable::priv_dispatch (begin): cmd: " << cmd
335 // << " button y: " << button_dim.y2 << endl;
336 switch (cmd.action) {
337 case LFUN_MOUSE_PRESS:
338 if (status_ == Inlined)
340 else if (status_ == Open && cmd.y > button_dim.y2)
341 inset.dispatch(adjustCommand(cmd));
342 return DispatchResult(true, true);
344 case LFUN_MOUSE_MOTION:
345 if (status_ == Inlined)
347 else if (status_ == Open && cmd.y > button_dim.y2)
348 inset.dispatch(adjustCommand(cmd));
349 return DispatchResult(true, true);
351 case LFUN_MOUSE_RELEASE:
352 return lfunMouseRelease(cmd);
354 case LFUN_INSET_TOGGLE:
355 if (inset.text_.toggleInset())
356 return DispatchResult(true, true);
357 if (status_ == Open) {
359 return DispatchResult(true, true);
361 setStatus(Collapsed);
362 return DispatchResult(false, FINISHED_RIGHT);
366 return inset.dispatch(adjustCommand(cmd));
368 //lyxerr << "InsetCollapsable::priv_dispatch (end)" << endl;
372 void InsetCollapsable::validate(LaTeXFeatures & features) const
374 inset.validate(features);
378 void InsetCollapsable::getCursorPos(int cell, int & x, int & y) const
380 inset.getCursorPos(cell, x, y);
381 if (status_ != Inlined)
382 y += - ascent() + height_collapsed() + inset.ascent();
386 void InsetCollapsable::getLabelList(Buffer const & buffer,
387 std::vector<string> & list) const
389 inset.getLabelList(buffer, list);
393 int InsetCollapsable::scroll(bool recursive) const
395 int sx = UpdatableInset::scroll(false);
398 sx += inset.scroll(false);
404 int InsetCollapsable::numParagraphs() const
406 return inset.numParagraphs();
410 LyXText * InsetCollapsable::getText(int i) const
412 return inset.getText(i);
416 void InsetCollapsable::open()
418 if (status_ == Collapsed) // ...but not inlined
423 void InsetCollapsable::close()
425 setStatus(Collapsed);
429 void InsetCollapsable::setLabel(string const & l)
435 void InsetCollapsable::setStatus(CollapseStatus st)
442 void InsetCollapsable::markErased()
448 void InsetCollapsable::addPreview(PreviewLoader & loader) const
450 inset.addPreview(loader);
454 bool InsetCollapsable::insetAllowed(InsetOld::Code code) const
456 return inset.insetAllowed(code);
460 void InsetCollapsable::setLabelFont(LyXFont & font)
466 void InsetCollapsable::scroll(BufferView * bv, float sx) const
468 UpdatableInset::scroll(bv, sx);
472 void InsetCollapsable::scroll(BufferView * bv, int offset) const
474 UpdatableInset::scroll(bv, offset);
478 Box const & InsetCollapsable::buttonDim() const
484 void InsetCollapsable::setBackgroundColor(LColor_color color)
486 InsetOld::setBackgroundColor(color);
487 inset.setBackgroundColor(color);