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;
38 using namespace lyx::graphics;
46 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
47 : UpdatableInset(), collapsed_(collapsed), inset(bp),
48 button_dim(0, 0, 0, 0), label("Label"),
52 oldWidth(0), in_update(false), first_after_edit(false)
55 inset.setAutoBreakRows(true);
56 inset.setDrawFrame(0, InsetText::ALWAYS);
57 inset.setFrameColor(0, LColor::collapsableframe);
58 setInsetName("Collapsable");
62 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
63 : UpdatableInset(in), collapsed_(in.collapsed_),
64 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
65 button_dim(0, 0, 0, 0), label(in.label),
67 autocollapse(in.autocollapse),
69 oldWidth(0), in_update(false), first_after_edit(false)
71 inset.init(&(in.inset));
76 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
78 if (!insetAllowed(in->lyxCode())) {
79 lyxerr << "InsetCollapsable::InsertInset: "
80 "Unable to insert inset." << endl;
83 return inset.insertInset(bv, in);
87 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
89 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
90 inset.writeParagraphData(buf, os);
94 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
98 string const token = lex.getString();
99 if (token == "collapsed") {
101 collapsed_ = lex.getBool();
103 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
105 // Take countermeasures
106 lex.pushToken(token);
109 inset.read(buf, lex);
113 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
115 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
116 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
120 int InsetCollapsable::height_collapsed() const
123 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
124 return dim.asc + dim.des;
128 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
130 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << "\n";
131 dimension_collapsed(dim);
134 inset.metrics(mi, insetdim);
135 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
136 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();
161 button_dim.x2 = dim_collapsed.width();
163 button_dim.y2 = -aa + dim_collapsed.height();
166 draw_collapsed(pi, x, y);
178 int const bl = y - aa + dim_collapsed.ascent();
181 inset.draw(pi, x, y);
183 draw_collapsed(pi, old_x, bl);
184 int const yy = bl + dim_collapsed.descent() + inset.ascent();
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 InsetOld::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 FuncRequest cmd1 = cmd;
225 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
230 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
233 BufferView * bv = cmd.view();
235 if (collapsed_ && cmd.button() != mouse_button::button3) {
237 inset.setUpdateStatus(InsetText::FULL);
238 bv->updateInset(this);
239 bv->buffer()->markDirty();
243 if (cmd.button() != mouse_button::button3 && hitButton(cmd))
247 inset.setUpdateStatus(InsetText::FULL);
248 bv->updateInset(this);
249 bv->buffer()->markDirty();
252 bv->unlockInset(this);
253 bv->updateInset(this);
254 bv->buffer()->markDirty();
256 } else if (!collapsed_ && (cmd.y > button_dim.y2)) {
257 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
259 if (cmd.button() == mouse_button::button3 && !ret)
264 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
265 LatexRunParams const & runparams) const
267 return inset.latex(buf, os, runparams);
271 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
273 return inset.ascii(buf, os, ll);
277 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
279 return inset.linuxdoc(buf, os);
283 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
285 return inset.docbook(buf, os, mixcont);
289 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
291 return button_dim.contained(cmd.x, cmd.y);
295 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
297 lyxerr << "InsetCollapsable::localDispatch: "
298 << cmd.action << " '" << cmd.argument << "'\n";
299 BufferView * bv = cmd.view();
300 switch (cmd.action) {
301 case LFUN_INSET_EDIT: {
302 if (!cmd.argument.empty()) {
303 UpdatableInset::localDispatch(cmd);
305 lyxerr << "branch collapsed_\n";
307 if (bv->lockInset(this)) {
308 inset.setUpdateStatus(InsetText::FULL);
309 bv->updateInset(this);
310 bv->buffer()->markDirty();
311 inset.localDispatch(cmd);
312 first_after_edit = true;
315 lyxerr << "branch not collapsed_\n";
316 if (bv->lockInset(this))
317 inset.localDispatch(cmd);
323 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
325 if (cmd.button() == mouse_button::button3)
328 UpdatableInset::localDispatch(cmd);
332 // set this only here as it should be recollapsed only if
333 // it was already collapsed!
334 first_after_edit = true;
335 if (!bv->lockInset(this))
337 bv->updateInset(this);
338 bv->buffer()->markDirty();
339 inset.localDispatch(cmd);
341 FuncRequest cmd1 = cmd;
342 if (!bv->lockInset(this))
344 if (cmd.y <= button_dim.y2) {
347 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
349 inset.localDispatch(cmd);
354 case LFUN_MOUSE_PRESS:
355 if (!collapsed_ && cmd.y > button_dim.y2)
356 inset.localDispatch(adjustCommand(cmd));
359 case LFUN_MOUSE_MOTION:
360 if (!collapsed_ && cmd.y > button_dim.y2)
361 inset.localDispatch(adjustCommand(cmd));
364 case LFUN_MOUSE_RELEASE:
365 lfunMouseRelease(cmd);
369 UpdatableInset::RESULT result = inset.localDispatch(cmd);
370 if (result >= FINISHED)
371 bv->unlockInset(this);
372 first_after_edit = false;
378 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
382 return inset.lockInsetInInset(bv, in);
386 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
390 bv->unlockInset(this);
393 return inset.unlockInsetInInset(bv, in, lr);
397 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
401 return inset.updateInsetInInset(bv, in);
405 int InsetCollapsable::insetInInsetY() const
407 return inset.insetInInsetY() - (top_baseline - inset.y());
411 void InsetCollapsable::validate(LaTeXFeatures & features) const
413 inset.validate(features);
417 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
419 inset.getCursor(bv, x, y);
423 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
425 inset.getCursorPos(bv, x , y);
429 UpdatableInset * InsetCollapsable::getLockingInset() const
431 UpdatableInset * in = inset.getLockingInset();
433 return const_cast<InsetCollapsable *>(this);
438 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
442 return inset.getFirstLockingInsetOfType(c);
446 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
447 bool toggleall, bool selectall)
449 inset.setFont(bv, font, toggleall, selectall);
453 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
454 bool const recursive) const
456 return inset.getLyXText(bv, recursive);
460 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
462 inset.deleteLyXText(bv, recursive);
466 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
468 inset.resizeLyXText(bv, force);
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 InsetOld * 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(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();