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"
20 #include "dispatchresult.h"
23 #include "funcrequest.h"
24 #include "metricsinfo.h"
25 #include "paragraph.h"
26 #include "WordLangTuple.h"
28 #include "frontends/font_metrics.h"
29 #include "frontends/Painter.h"
30 #include "frontends/LyXView.h"
33 using lyx::graphics::PreviewLoader;
41 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
42 : UpdatableInset(), inset(bp), collapsed_(collapsed),
43 button_dim(0, 0, 0, 0), label("Label"),
47 in_update(false), first_after_edit(false)
50 inset.setAutoBreakRows(true);
51 inset.setDrawFrame(InsetText::ALWAYS);
52 inset.setFrameColor(LColor::collapsableframe);
53 setInsetName("Collapsable");
57 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
58 : UpdatableInset(in), inset(in.inset), collapsed_(in.collapsed_),
59 labelfont_(in.labelfont_), button_dim(0, 0, 0, 0), label(in.label),
61 autocollapse(in.autocollapse),
63 in_update(false), first_after_edit(false)
69 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
71 if (!insetAllowed(in->lyxCode())) {
72 lyxerr << "InsetCollapsable::InsertInset: "
73 "Unable to insert inset." << endl;
76 return inset.insertInset(bv, in);
80 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
82 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
83 inset.writeParagraphData(buf, os);
87 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
91 string const token = lex.getString();
92 if (token == "collapsed") {
94 collapsed_ = lex.getBool();
96 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
98 // Take countermeasures
102 inset.read(buf, lex);
106 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
108 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
112 int InsetCollapsable::height_collapsed() const
115 font_metrics::buttonText(label, labelfont_, dim.wid, dim.asc, dim.des);
116 return dim.asc + dim.des;
120 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
122 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
123 dimension_collapsed(dim);
126 inset.metrics(mi, insetdim);
127 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
128 dim.wid = max(dim.wid, insetdim.wid);
134 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
136 pi.pain.buttonText(x, y, label, labelfont_);
140 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
142 Dimension dim_collapsed;
143 dimension_collapsed(dim_collapsed);
145 int const aa = ascent();
147 button_dim.x2 = dim_collapsed.width();
149 button_dim.y2 = -aa + dim_collapsed.height();
152 draw_collapsed(pi, x, y);
164 int const bl = y - aa + dim_collapsed.ascent();
167 inset.draw(pi, x, y);
169 draw_collapsed(pi, old_x, bl);
170 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
175 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
177 // by default, we are not inlined-drawing
178 draw(pi, x, y, false);
182 InsetOld::EDITABLE InsetCollapsable::editable() const
184 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
188 void InsetCollapsable::insetUnlock(BufferView * bv)
192 if (change_label_with_text) {
193 draw_label = get_new_label();
200 inset.insetUnlock(bv);
203 bv->updateInset(this);
207 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
209 FuncRequest cmd1 = cmd;
210 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
215 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
218 BufferView * bv = cmd.view();
220 if (collapsed_ && cmd.button() != mouse_button::button3) {
222 bv->updateInset(this);
223 bv->buffer()->markDirty();
227 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
232 bv->unlockInset(this);
234 bv->updateInset(this);
235 bv->buffer()->markDirty();
236 } else if (!collapsed_ && cmd.y > button_dim.y2) {
237 ret = inset.dispatch(adjustCommand(cmd)) == DispatchResult(DISPATCHED);
239 if (cmd.button() == mouse_button::button3 && !ret)
244 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
245 LatexRunParams const & runparams) const
247 return inset.latex(buf, os, runparams);
251 int InsetCollapsable::ascii(Buffer const & buf, ostream & os,
252 LatexRunParams const & runparams) const
254 return inset.ascii(buf, os, runparams);
258 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
259 LatexRunParams const & runparams) const
261 return inset.linuxdoc(buf, os, runparams);
265 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
266 LatexRunParams const & runparams) const
268 return inset.docbook(buf, os, runparams);
272 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
274 return button_dim.contains(cmd.x, cmd.y);
278 void InsetCollapsable::edit(BufferView * bv, int index)
280 lyxerr << "InsetCollapsable: edit" << endl;
281 if (!bv->lockInset(this))
282 lyxerr << "InsetCollapsable: can't lock index " << index << endl;
283 inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
284 first_after_edit = true;
289 InsetCollapsable::priv_dispatch(FuncRequest const & cmd,
290 idx_type & idx, pos_type & pos)
292 //lyxerr << "InsetCollapsable::localDispatch: "
293 // << cmd.action << " '" << cmd.argument << "'\n";
294 BufferView * bv = cmd.view();
295 switch (cmd.action) {
296 case LFUN_INSET_EDIT: {
297 if (!cmd.argument.empty()) {
298 UpdatableInset::priv_dispatch(cmd, idx, pos);
300 lyxerr << "branch collapsed_" << endl;
302 if (bv->lockInset(this)) {
303 bv->updateInset(this);
304 bv->buffer()->markDirty();
306 first_after_edit = true;
309 lyxerr << "branch not collapsed_" << endl;
310 if (bv->lockInset(this))
313 return DispatchResult(DISPATCHED);
317 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
319 if (cmd.button() == mouse_button::button3)
320 return DispatchResult(DISPATCHED);
322 UpdatableInset::priv_dispatch(cmd, idx, pos);
326 // set this only here as it should be recollapsed only if
327 // it was already collapsed!
328 first_after_edit = true;
329 if (!bv->lockInset(this))
330 return DispatchResult(DISPATCHED);
331 bv->updateInset(this);
332 bv->buffer()->markDirty();
335 if (!bv->lockInset(this))
336 return DispatchResult(DISPATCHED);
337 if (cmd.y <= button_dim.y2) {
338 FuncRequest cmd1 = cmd;
340 inset.dispatch(cmd1);
342 inset.dispatch(adjustCommand(cmd));
344 return DispatchResult(DISPATCHED);
347 case LFUN_MOUSE_PRESS:
348 if (!collapsed_ && cmd.y > button_dim.y2)
349 inset.dispatch(adjustCommand(cmd));
350 return DispatchResult(DISPATCHED);
352 case LFUN_MOUSE_MOTION:
353 if (!collapsed_ && cmd.y > button_dim.y2)
354 inset.dispatch(adjustCommand(cmd));
355 return DispatchResult(DISPATCHED);
357 case LFUN_MOUSE_RELEASE:
358 lfunMouseRelease(cmd);
359 return DispatchResult(DISPATCHED);
362 DispatchResult result = inset.dispatch(cmd);
363 if (result >= DispatchResult(FINISHED))
364 bv->unlockInset(this);
365 first_after_edit = false;
371 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
375 return inset.lockInsetInInset(bv, in);
379 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
383 bv->unlockInset(this);
386 return inset.unlockInsetInInset(bv, in, lr);
390 int InsetCollapsable::insetInInsetY() const
392 return inset.insetInInsetY() - (top_baseline - inset.y());
396 void InsetCollapsable::validate(LaTeXFeatures & features) const
398 inset.validate(features);
402 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
404 inset.getCursor(bv, x, y);
408 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
410 inset.getCursorPos(bv, x , y);
414 UpdatableInset * InsetCollapsable::getLockingInset() const
416 UpdatableInset * in = inset.getLockingInset();
418 return const_cast<InsetCollapsable *>(this);
423 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
427 return inset.getFirstLockingInsetOfType(c);
431 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
432 bool toggleall, bool selectall)
434 inset.setFont(bv, font, toggleall, selectall);
438 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
439 bool const recursive) const
441 return inset.getLyXText(bv, recursive);
445 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
447 inset.deleteLyXText(bv, recursive);
451 void InsetCollapsable::getLabelList(Buffer const & buffer,
452 std::vector<string> & list) const
454 inset.getLabelList(buffer, list);
458 int InsetCollapsable::scroll(bool recursive) const
460 int sx = UpdatableInset::scroll(false);
463 sx += inset.scroll(recursive);
469 ParagraphList * InsetCollapsable::getParagraphs(int i) const
471 return inset.getParagraphs(i);
475 int InsetCollapsable::numParagraphs() const
477 return inset.numParagraphs();
481 LyXText * InsetCollapsable::getText(int i) const
483 return inset.getText(i);
487 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
489 return inset.cursor(bv);
493 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
496 return const_cast<InsetCollapsable *>(this);
497 return inset.getInsetFromID(id_arg);
501 void InsetCollapsable::open(BufferView * bv)
507 bv->updateInset(this);
511 void InsetCollapsable::close(BufferView * bv) const
517 bv->updateInset(this);
521 void InsetCollapsable::setLabel(string const & l) const
527 void InsetCollapsable::setCollapsed(bool c) const
533 void InsetCollapsable::markErased()
539 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
541 bool found = inset.nextChange(bv, length);
543 if (first_after_edit && !found)
546 first_after_edit = false;
551 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
554 bool found = inset.searchForward(bv, str, cs, mw);
555 if (first_after_edit && !found)
558 first_after_edit = false;
563 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
566 bool found = inset.searchBackward(bv, str, cs, mw);
567 if (first_after_edit && !found)
570 first_after_edit = false;
576 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
578 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
579 if (first_after_edit && word.word().empty())
581 first_after_edit = false;
586 void InsetCollapsable::addPreview(PreviewLoader & loader) const
588 inset.addPreview(loader);