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"
43 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
44 : UpdatableInset(), collapsed_(collapsed), inset(bp),
45 button_length(0), button_top_y(0), button_bottom_y(0),
50 oldWidth(0), in_update(false), first_after_edit(false)
53 inset.setAutoBreakRows(true);
54 inset.setDrawFrame(0, InsetText::ALWAYS);
55 inset.setFrameColor(0, LColor::collapsableframe);
56 setInsetName("Collapsable");
60 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
61 : UpdatableInset(in), collapsed_(in.collapsed_),
62 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
63 button_length(0), button_top_y(0), button_bottom_y(0),
66 autocollapse(in.autocollapse),
68 oldWidth(0), in_update(false), first_after_edit(false)
70 inset.init(&(in.inset));
75 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
77 if (!insetAllowed(in->lyxCode())) {
78 lyxerr << "InsetCollapsable::InsertInset: "
79 "Unable to insert inset." << endl;
82 return inset.insertInset(bv, in);
86 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
88 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
89 inset.writeParagraphData(buf, os);
93 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
97 string const token = lex.getString();
98 if (token == "collapsed") {
100 collapsed_ = lex.getBool();
102 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
104 // Take countermeasures
105 lex.pushToken(token);
108 inset.read(buf, lex);
112 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
114 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
115 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
119 int InsetCollapsable::height_collapsed() const
122 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
123 return dim.asc + dim.des;
127 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
129 dimension_collapsed(dim);
133 inset.metrics(mi, insetdim);
134 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
135 dim.wid = max(dim.wid, insetdim.wid);
139 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
141 pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
145 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
147 lyx::Assert(pi.base.bv);
153 Dimension dim_collapsed;
154 dimension_collapsed(dim_collapsed);
156 int const aa = ascent(pi.base.bv, pi.base.font);
157 button_length = dim_collapsed.width();
159 button_bottom_y = -aa + dim_collapsed.height();
162 draw_collapsed(pi, x, y);
174 int const bl = y - aa + dim_collapsed.ascent();
177 inset.draw(pi, x, y);
179 draw_collapsed(pi, old_x, bl);
180 int const yy = bl + dim_collapsed.descent()
181 + inset.ascent(pi.base.bv, pi.base.font);
182 inset.draw(pi, x, yy);
187 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
189 // by default, we are not inlined-drawing
190 draw(pi, x, y, false);
194 Inset::EDITABLE InsetCollapsable::editable() const
196 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
200 void InsetCollapsable::insetUnlock(BufferView * bv)
204 if (change_label_with_text) {
205 draw_label = get_new_label();
212 inset.insetUnlock(bv);
215 bv->updateInset(this);
219 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
221 LyXFont font(LyXFont::ALL_SANE);
222 FuncRequest cmd1 = cmd;
223 cmd1.y = ascent(cmd.view(), font) + cmd.y -
224 (height_collapsed() + inset.ascent(cmd.view(), font));
229 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
232 BufferView * bv = cmd.view();
234 if (collapsed_ && cmd.button() != mouse_button::button3) {
236 inset.setUpdateStatus(bv, InsetText::FULL);
237 bv->updateInset(this);
238 bv->buffer()->markDirty();
242 if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
243 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
247 inset.setUpdateStatus(bv, 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_bottom_y)) {
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 void InsetCollapsable::update(BufferView * bv, bool reinit)
292 if (reinit && owner()) {
293 owner()->update(bv, true);
298 inset.update(bv, reinit);
299 if (reinit && owner()) {
300 owner()->update(bv, true);
306 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
308 //lyxerr << "InsetCollapsable::localDispatch: " << cmd.action << "\n";
309 BufferView * bv = cmd.view();
310 switch (cmd.action) {
311 case LFUN_INSET_EDIT: {
312 if (!cmd.argument.empty()) {
313 UpdatableInset::localDispatch(cmd);
316 if (bv->lockInset(this)) {
317 inset.setUpdateStatus(bv, InsetText::FULL);
318 bv->updateInset(this);
319 bv->buffer()->markDirty();
320 inset.localDispatch(cmd);
321 first_after_edit = true;
324 if (bv->lockInset(this))
325 inset.localDispatch(cmd);
331 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
333 if (cmd.button() == mouse_button::button3)
336 UpdatableInset::localDispatch(cmd);
340 // set this only here as it should be recollapsed only if
341 // it was already collapsed!
342 first_after_edit = true;
343 if (!bv->lockInset(this))
345 bv->updateInset(this);
346 bv->buffer()->markDirty();
347 inset.localDispatch(cmd);
349 FuncRequest cmd1 = cmd;
350 if (!bv->lockInset(this))
352 if (cmd.y <= button_bottom_y) {
355 LyXFont font(LyXFont::ALL_SANE);
356 cmd1.y = ascent(bv, font) + cmd.y -
357 (height_collapsed() + inset.ascent(bv, font));
359 inset.localDispatch(cmd);
364 case LFUN_MOUSE_PRESS:
365 if (!collapsed_ && cmd.y > button_bottom_y)
366 inset.localDispatch(adjustCommand(cmd));
369 case LFUN_MOUSE_MOTION:
370 if (!collapsed_ && cmd.y > button_bottom_y)
371 inset.localDispatch(adjustCommand(cmd));
374 case LFUN_MOUSE_RELEASE:
375 lfunMouseRelease(cmd);
379 UpdatableInset::RESULT result = inset.localDispatch(cmd);
380 if (result >= FINISHED)
381 bv->unlockInset(this);
382 first_after_edit = false;
388 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
392 return inset.lockInsetInInset(bv, in);
396 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
400 bv->unlockInset(this);
403 return inset.unlockInsetInInset(bv, in, lr);
407 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
411 return inset.updateInsetInInset(bv, in);
415 int InsetCollapsable::insetInInsetY() const
417 return inset.insetInInsetY() - (top_baseline - inset.y());
421 void InsetCollapsable::validate(LaTeXFeatures & features) const
423 inset.validate(features);
427 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
429 inset.getCursor(bv, x, y);
433 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
435 inset.getCursorPos(bv, x , y);
439 UpdatableInset * InsetCollapsable::getLockingInset() const
441 UpdatableInset * in = inset.getLockingInset();
442 if (const_cast<InsetText *>(&inset) == in)
443 return const_cast<InsetCollapsable *>(this);
448 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
452 return inset.getFirstLockingInsetOfType(c);
456 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
457 bool toggleall, bool selectall)
459 inset.setFont(bv, font, toggleall, selectall);
463 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
464 bool const recursive) const
466 return inset.getLyXText(bv, recursive);
470 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
472 inset.deleteLyXText(bv, recursive);
476 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
478 inset.resizeLyXText(bv, force);
479 LyXFont font(LyXFont::ALL_SANE);
480 oldWidth = width(bv, font);
484 vector<string> const InsetCollapsable::getLabelList() const
486 return inset.getLabelList();
490 bool InsetCollapsable::nodraw() const
492 return inset.nodraw();
496 int InsetCollapsable::scroll(bool recursive) const
498 int sx = UpdatableInset::scroll(false);
501 sx += inset.scroll(recursive);
507 ParagraphList * InsetCollapsable::getParagraphs(int i) const
509 return inset.getParagraphs(i);
513 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
515 return inset.cursor(bv);
519 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
522 return const_cast<InsetCollapsable *>(this);
523 return inset.getInsetFromID(id_arg);
527 void InsetCollapsable::open(BufferView * bv)
529 if (!collapsed_) return;
532 bv->updateInset(this);
536 void InsetCollapsable::close(BufferView * bv) const
542 bv->updateInset(const_cast<InsetCollapsable *>(this));
546 void InsetCollapsable::setLabel(string const & l) const
552 void InsetCollapsable::markErased()
558 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
560 bool found = inset.nextChange(bv, length);
562 if (first_after_edit && !found)
565 first_after_edit = false;
570 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
573 bool found = inset.searchForward(bv, str, cs, mw);
574 if (first_after_edit && !found)
577 first_after_edit = false;
582 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
585 bool found = inset.searchBackward(bv, str, cs, mw);
586 if (first_after_edit && !found)
589 first_after_edit = false;
595 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
597 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
598 if (first_after_edit && word.word().empty())
600 first_after_edit = false;
605 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
607 inset.addPreview(loader);
611 void InsetCollapsable::cache(BufferView * bv) const
613 view_ = bv->owner()->view();
617 BufferView * InsetCollapsable::view() const
619 return view_.lock().get();