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(bv, 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(bv, 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 void InsetCollapsable::update(BufferView * bv, bool reinit)
295 if (reinit && owner()) {
296 owner()->update(bv, true);
301 inset.update(bv, reinit);
302 if (reinit && owner()) {
303 owner()->update(bv, true);
309 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
311 //lyxerr << "InsetCollapsable::localDispatch: " << cmd.action << "\n";
312 BufferView * bv = cmd.view();
313 switch (cmd.action) {
314 case LFUN_INSET_EDIT: {
315 if (!cmd.argument.empty()) {
316 UpdatableInset::localDispatch(cmd);
319 if (bv->lockInset(this)) {
320 inset.setUpdateStatus(bv, InsetText::FULL);
321 bv->updateInset(this);
322 bv->buffer()->markDirty();
323 inset.localDispatch(cmd);
324 first_after_edit = true;
327 if (bv->lockInset(this))
328 inset.localDispatch(cmd);
334 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
336 if (cmd.button() == mouse_button::button3)
339 UpdatableInset::localDispatch(cmd);
343 // set this only here as it should be recollapsed only if
344 // it was already collapsed!
345 first_after_edit = true;
346 if (!bv->lockInset(this))
348 bv->updateInset(this);
349 bv->buffer()->markDirty();
350 inset.localDispatch(cmd);
352 FuncRequest cmd1 = cmd;
353 if (!bv->lockInset(this))
355 if (cmd.y <= button_bottom_y) {
358 LyXFont font(LyXFont::ALL_SANE);
359 cmd1.y = ascent(bv, font) + cmd.y -
360 (height_collapsed() + inset.ascent(bv, font));
362 inset.localDispatch(cmd);
367 case LFUN_MOUSE_PRESS:
368 if (!collapsed_ && cmd.y > button_bottom_y)
369 inset.localDispatch(adjustCommand(cmd));
372 case LFUN_MOUSE_MOTION:
373 if (!collapsed_ && cmd.y > button_bottom_y)
374 inset.localDispatch(adjustCommand(cmd));
377 case LFUN_MOUSE_RELEASE:
378 lfunMouseRelease(cmd);
382 UpdatableInset::RESULT result = inset.localDispatch(cmd);
383 if (result >= FINISHED)
384 bv->unlockInset(this);
385 first_after_edit = false;
391 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
395 return inset.lockInsetInInset(bv, in);
399 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
403 bv->unlockInset(this);
406 return inset.unlockInsetInInset(bv, in, lr);
410 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
414 return inset.updateInsetInInset(bv, in);
418 int InsetCollapsable::insetInInsetY() const
420 return inset.insetInInsetY() - (top_baseline - inset.y());
424 void InsetCollapsable::validate(LaTeXFeatures & features) const
426 inset.validate(features);
430 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
432 inset.getCursor(bv, x, y);
436 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
438 inset.getCursorPos(bv, x , y);
442 UpdatableInset * InsetCollapsable::getLockingInset() const
444 UpdatableInset * in = inset.getLockingInset();
445 if (const_cast<InsetText *>(&inset) == in)
446 return const_cast<InsetCollapsable *>(this);
451 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
455 return inset.getFirstLockingInsetOfType(c);
459 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
460 bool toggleall, bool selectall)
462 inset.setFont(bv, font, toggleall, selectall);
466 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
467 bool const recursive) const
469 return inset.getLyXText(bv, recursive);
473 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
475 inset.deleteLyXText(bv, recursive);
479 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
481 inset.resizeLyXText(bv, force);
482 LyXFont font(LyXFont::ALL_SANE);
483 oldWidth = width(bv, font);
487 void InsetCollapsable::getLabelList(std::vector<string> & list) const
489 inset.getLabelList(list);
493 bool InsetCollapsable::nodraw() const
495 return inset.nodraw();
499 int InsetCollapsable::scroll(bool recursive) const
501 int sx = UpdatableInset::scroll(false);
504 sx += inset.scroll(recursive);
510 ParagraphList * InsetCollapsable::getParagraphs(int i) const
512 return inset.getParagraphs(i);
516 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
518 return inset.cursor(bv);
522 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
525 return const_cast<InsetCollapsable *>(this);
526 return inset.getInsetFromID(id_arg);
530 void InsetCollapsable::open(BufferView * bv)
532 if (!collapsed_) return;
535 bv->updateInset(this);
539 void InsetCollapsable::close(BufferView * bv) const
545 bv->updateInset(const_cast<InsetCollapsable *>(this));
549 void InsetCollapsable::setLabel(string const & l) const
555 void InsetCollapsable::markErased()
561 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
563 bool found = inset.nextChange(bv, length);
565 if (first_after_edit && !found)
568 first_after_edit = false;
573 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
576 bool found = inset.searchForward(bv, str, cs, mw);
577 if (first_after_edit && !found)
580 first_after_edit = false;
585 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
588 bool found = inset.searchBackward(bv, str, cs, mw);
589 if (first_after_edit && !found)
592 first_after_edit = false;
598 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
600 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
601 if (first_after_edit && word.word().empty())
603 first_after_edit = false;
608 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
610 inset.addPreview(loader);
614 void InsetCollapsable::cache(BufferView * bv) const
616 view_ = bv->owner()->view();
620 BufferView * InsetCollapsable::view() const
622 return view_.lock().get();