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"
16 #include "insettext.h"
18 #include "BufferView.h"
20 #include "dimension.h"
25 #include "WordLangTuple.h"
26 #include "funcrequest.h"
28 #include "metricsinfo.h"
30 #include "frontends/font_metrics.h"
31 #include "frontends/Painter.h"
32 #include "frontends/LyXView.h"
34 #include "support/LAssert.h"
35 #include "support/LOstream.h"
37 using namespace lyx::support;
44 namespace grfx = lyx::graphics;
46 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
47 : UpdatableInset(), collapsed_(collapsed), inset(bp),
48 button_length(0), button_top_y(0), button_bottom_y(0),
53 oldWidth(0), in_update(false), first_after_edit(false)
56 inset.setAutoBreakRows(true);
57 inset.setDrawFrame(0, InsetText::ALWAYS);
58 inset.setFrameColor(0, LColor::collapsableframe);
59 setInsetName("Collapsable");
63 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
64 : UpdatableInset(in), collapsed_(in.collapsed_),
65 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
66 button_length(0), button_top_y(0), button_bottom_y(0),
69 autocollapse(in.autocollapse),
71 oldWidth(0), in_update(false), first_after_edit(false)
73 inset.init(&(in.inset));
78 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
80 if (!insetAllowed(in->lyxCode())) {
81 lyxerr << "InsetCollapsable::InsertInset: "
82 "Unable to insert inset." << endl;
85 return inset.insertInset(bv, in);
89 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
91 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
92 inset.writeParagraphData(buf, os);
96 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
100 string const token = lex.getString();
101 if (token == "collapsed") {
103 collapsed_ = lex.getBool();
105 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
107 // Take countermeasures
108 lex.pushToken(token);
111 inset.read(buf, lex);
115 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
117 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
118 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
122 int InsetCollapsable::height_collapsed() const
125 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
126 return dim.asc + dim.des;
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
132 dimension_collapsed(dim);
136 inset.metrics(mi, insetdim);
137 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
138 dim.wid = max(dim.wid, insetdim.wid);
142 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
144 pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
148 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
156 Dimension dim_collapsed;
157 dimension_collapsed(dim_collapsed);
159 int const aa = ascent(pi.base.bv, pi.base.font);
160 button_length = dim_collapsed.width();
162 button_bottom_y = -aa + dim_collapsed.height();
165 draw_collapsed(pi, x, y);
177 int const bl = y - aa + dim_collapsed.ascent();
180 inset.draw(pi, x, y);
182 draw_collapsed(pi, old_x, bl);
183 int const yy = bl + dim_collapsed.descent()
184 + inset.ascent(pi.base.bv, pi.base.font);
185 inset.draw(pi, x, yy);
190 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
192 // by default, we are not inlined-drawing
193 draw(pi, x, y, false);
197 Inset::EDITABLE InsetCollapsable::editable() const
199 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
203 void InsetCollapsable::insetUnlock(BufferView * bv)
207 if (change_label_with_text) {
208 draw_label = get_new_label();
215 inset.insetUnlock(bv);
218 bv->updateInset(this);
222 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
224 LyXFont font(LyXFont::ALL_SANE);
225 FuncRequest cmd1 = cmd;
226 cmd1.y = ascent(cmd.view(), font) + cmd.y -
227 (height_collapsed() + inset.ascent(cmd.view(), font));
232 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
235 BufferView * bv = cmd.view();
237 if (collapsed_ && cmd.button() != mouse_button::button3) {
239 inset.setUpdateStatus(InsetText::FULL);
240 bv->updateInset(this);
241 bv->buffer()->markDirty();
245 if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
246 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
250 inset.setUpdateStatus(InsetText::FULL);
251 bv->updateInset(this);
252 bv->buffer()->markDirty();
255 bv->unlockInset(this);
256 bv->updateInset(this);
257 bv->buffer()->markDirty();
259 } else if (!collapsed_ && (cmd.y > button_bottom_y)) {
260 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
262 if (cmd.button() == mouse_button::button3 && !ret)
267 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
268 LatexRunParams const & runparams) const
270 return inset.latex(buf, os, runparams);
274 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
276 return inset.ascii(buf, os, ll);
280 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
282 return inset.linuxdoc(buf, os);
286 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
288 return inset.docbook(buf, os, mixcont);
292 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
294 lyxerr << "InsetCollapsable::localDispatch: "
295 << cmd.action << " '" << cmd.argument << "'\n";
296 BufferView * bv = cmd.view();
297 switch (cmd.action) {
298 case LFUN_INSET_EDIT: {
299 if (!cmd.argument.empty()) {
300 UpdatableInset::localDispatch(cmd);
302 lyxerr << "branch collapsed_\n";
304 if (bv->lockInset(this)) {
305 inset.setUpdateStatus(InsetText::FULL);
306 bv->updateInset(this);
307 bv->buffer()->markDirty();
308 inset.localDispatch(cmd);
309 first_after_edit = true;
312 lyxerr << "branch not collapsed_\n";
313 if (bv->lockInset(this))
314 inset.localDispatch(cmd);
320 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
322 if (cmd.button() == mouse_button::button3)
325 UpdatableInset::localDispatch(cmd);
329 // set this only here as it should be recollapsed only if
330 // it was already collapsed!
331 first_after_edit = true;
332 if (!bv->lockInset(this))
334 bv->updateInset(this);
335 bv->buffer()->markDirty();
336 inset.localDispatch(cmd);
338 FuncRequest cmd1 = cmd;
339 if (!bv->lockInset(this))
341 if (cmd.y <= button_bottom_y) {
344 LyXFont font(LyXFont::ALL_SANE);
345 cmd1.y = ascent(bv, font) + cmd.y -
346 (height_collapsed() + inset.ascent(bv, font));
348 inset.localDispatch(cmd);
353 case LFUN_MOUSE_PRESS:
354 if (!collapsed_ && cmd.y > button_bottom_y)
355 inset.localDispatch(adjustCommand(cmd));
358 case LFUN_MOUSE_MOTION:
359 if (!collapsed_ && cmd.y > button_bottom_y)
360 inset.localDispatch(adjustCommand(cmd));
363 case LFUN_MOUSE_RELEASE:
364 lfunMouseRelease(cmd);
368 UpdatableInset::RESULT result = inset.localDispatch(cmd);
369 if (result >= FINISHED)
370 bv->unlockInset(this);
371 first_after_edit = false;
377 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
381 return inset.lockInsetInInset(bv, in);
385 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
389 bv->unlockInset(this);
392 return inset.unlockInsetInInset(bv, in, lr);
396 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
400 return inset.updateInsetInInset(bv, in);
404 int InsetCollapsable::insetInInsetY() const
406 return inset.insetInInsetY() - (top_baseline - inset.y());
410 void InsetCollapsable::validate(LaTeXFeatures & features) const
412 inset.validate(features);
416 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
418 inset.getCursor(bv, x, y);
422 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
424 inset.getCursorPos(bv, x , y);
428 UpdatableInset * InsetCollapsable::getLockingInset() const
430 UpdatableInset * in = inset.getLockingInset();
431 if (const_cast<InsetText *>(&inset) == in)
432 return const_cast<InsetCollapsable *>(this);
437 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
441 return inset.getFirstLockingInsetOfType(c);
445 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
446 bool toggleall, bool selectall)
448 inset.setFont(bv, font, toggleall, selectall);
452 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
453 bool const recursive) const
455 return inset.getLyXText(bv, recursive);
459 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
461 inset.deleteLyXText(bv, recursive);
465 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
467 inset.resizeLyXText(bv, force);
468 LyXFont font(LyXFont::ALL_SANE);
469 oldWidth = width(bv, font);
473 void InsetCollapsable::getLabelList(std::vector<string> & list) const
475 inset.getLabelList(list);
479 bool InsetCollapsable::nodraw() const
481 return inset.nodraw();
485 int InsetCollapsable::scroll(bool recursive) const
487 int sx = UpdatableInset::scroll(false);
490 sx += inset.scroll(recursive);
496 ParagraphList * InsetCollapsable::getParagraphs(int i) const
498 return inset.getParagraphs(i);
502 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
504 return inset.cursor(bv);
508 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
511 return const_cast<InsetCollapsable *>(this);
512 return inset.getInsetFromID(id_arg);
516 void InsetCollapsable::open(BufferView * bv)
518 if (!collapsed_) return;
521 bv->updateInset(this);
525 void InsetCollapsable::close(BufferView * bv) const
531 bv->updateInset(const_cast<InsetCollapsable *>(this));
535 void InsetCollapsable::setLabel(string const & l) const
541 void InsetCollapsable::markErased()
547 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
549 bool found = inset.nextChange(bv, length);
551 if (first_after_edit && !found)
554 first_after_edit = false;
559 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
562 bool found = inset.searchForward(bv, str, cs, mw);
563 if (first_after_edit && !found)
566 first_after_edit = false;
571 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
574 bool found = inset.searchBackward(bv, str, cs, mw);
575 if (first_after_edit && !found)
578 first_after_edit = false;
584 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
586 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
587 if (first_after_edit && word.word().empty())
589 first_after_edit = false;
594 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
596 inset.addPreview(loader);
600 void InsetCollapsable::cache(BufferView * bv) const
602 view_ = bv->owner()->view();
606 BufferView * InsetCollapsable::view() const
608 return view_.lock().get();