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;
45 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
46 : UpdatableInset(), collapsed_(collapsed), inset(bp),
47 button_length(0), button_top_y(0), button_bottom_y(0),
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_length(0), button_top_y(0), button_bottom_y(0),
68 autocollapse(in.autocollapse),
70 oldWidth(0), in_update(false), first_after_edit(false)
72 inset.init(&(in.inset));
77 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
79 if (!insetAllowed(in->lyxCode())) {
80 lyxerr << "InsetCollapsable::InsertInset: "
81 "Unable to insert inset." << endl;
84 return inset.insertInset(bv, in);
88 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
90 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
91 inset.writeParagraphData(buf, os);
95 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
99 string const token = lex.getString();
100 if (token == "collapsed") {
102 collapsed_ = lex.getBool();
104 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
106 // Take countermeasures
107 lex.pushToken(token);
110 inset.read(buf, lex);
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
116 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
117 dim.wid += 2 * TEXT_TO_INSET_OFFSET;
121 int InsetCollapsable::height_collapsed() const
124 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
125 return dim.asc + dim.des;
129 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
131 dimension_collapsed(dim);
135 inset.metrics(mi, insetdim);
136 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
137 dim.wid = max(dim.wid, insetdim.wid);
141 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
143 pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
147 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
155 Dimension dim_collapsed;
156 dimension_collapsed(dim_collapsed);
158 int const aa = ascent(pi.base.bv, pi.base.font);
159 button_length = dim_collapsed.width();
161 button_bottom_y = -aa + dim_collapsed.height();
164 draw_collapsed(pi, x, y);
176 int const bl = y - aa + dim_collapsed.ascent();
179 inset.draw(pi, x, y);
181 draw_collapsed(pi, old_x, bl);
182 int const yy = bl + dim_collapsed.descent()
183 + inset.ascent(pi.base.bv, pi.base.font);
184 inset.draw(pi, x, yy);
189 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
191 // by default, we are not inlined-drawing
192 draw(pi, x, y, false);
196 Inset::EDITABLE InsetCollapsable::editable() const
198 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
202 void InsetCollapsable::insetUnlock(BufferView * bv)
206 if (change_label_with_text) {
207 draw_label = get_new_label();
214 inset.insetUnlock(bv);
217 bv->updateInset(this);
221 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
223 LyXFont font(LyXFont::ALL_SANE);
224 FuncRequest cmd1 = cmd;
225 cmd1.y = ascent(cmd.view(), font) + cmd.y -
226 (height_collapsed() + inset.ascent(cmd.view(), font));
231 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
234 BufferView * bv = cmd.view();
236 if (collapsed_ && cmd.button() != mouse_button::button3) {
238 inset.setUpdateStatus(bv, InsetText::FULL);
239 bv->updateInset(this);
240 bv->buffer()->markDirty();
244 if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
245 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
249 inset.setUpdateStatus(bv, InsetText::FULL);
250 bv->updateInset(this);
251 bv->buffer()->markDirty();
254 bv->unlockInset(this);
255 bv->updateInset(this);
256 bv->buffer()->markDirty();
258 } else if (!collapsed_ && (cmd.y > button_bottom_y)) {
259 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
261 if (cmd.button() == mouse_button::button3 && !ret)
266 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
267 LatexRunParams const & runparams) const
269 return inset.latex(buf, os, runparams);
273 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
275 return inset.ascii(buf, os, ll);
279 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
281 return inset.linuxdoc(buf, os);
285 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
287 return inset.docbook(buf, os, mixcont);
291 void InsetCollapsable::update(BufferView * bv, bool reinit)
294 if (reinit && owner()) {
295 owner()->update(bv, true);
300 inset.update(bv, reinit);
301 if (reinit && owner()) {
302 owner()->update(bv, true);
308 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
310 //lyxerr << "InsetCollapsable::localDispatch: " << cmd.action << "\n";
311 BufferView * bv = cmd.view();
312 switch (cmd.action) {
313 case LFUN_INSET_EDIT: {
314 if (!cmd.argument.empty()) {
315 UpdatableInset::localDispatch(cmd);
318 if (bv->lockInset(this)) {
319 inset.setUpdateStatus(bv, InsetText::FULL);
320 bv->updateInset(this);
321 bv->buffer()->markDirty();
322 inset.localDispatch(cmd);
323 first_after_edit = true;
326 if (bv->lockInset(this))
327 inset.localDispatch(cmd);
333 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
335 if (cmd.button() == mouse_button::button3)
338 UpdatableInset::localDispatch(cmd);
342 // set this only here as it should be recollapsed only if
343 // it was already collapsed!
344 first_after_edit = true;
345 if (!bv->lockInset(this))
347 bv->updateInset(this);
348 bv->buffer()->markDirty();
349 inset.localDispatch(cmd);
351 FuncRequest cmd1 = cmd;
352 if (!bv->lockInset(this))
354 if (cmd.y <= button_bottom_y) {
357 LyXFont font(LyXFont::ALL_SANE);
358 cmd1.y = ascent(bv, font) + cmd.y -
359 (height_collapsed() + inset.ascent(bv, font));
361 inset.localDispatch(cmd);
366 case LFUN_MOUSE_PRESS:
367 if (!collapsed_ && cmd.y > button_bottom_y)
368 inset.localDispatch(adjustCommand(cmd));
371 case LFUN_MOUSE_MOTION:
372 if (!collapsed_ && cmd.y > button_bottom_y)
373 inset.localDispatch(adjustCommand(cmd));
376 case LFUN_MOUSE_RELEASE:
377 lfunMouseRelease(cmd);
381 UpdatableInset::RESULT result = inset.localDispatch(cmd);
382 if (result >= FINISHED)
383 bv->unlockInset(this);
384 first_after_edit = false;
390 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
394 return inset.lockInsetInInset(bv, in);
398 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
402 bv->unlockInset(this);
405 return inset.unlockInsetInInset(bv, in, lr);
409 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
413 return inset.updateInsetInInset(bv, in);
417 int InsetCollapsable::insetInInsetY() const
419 return inset.insetInInsetY() - (top_baseline - inset.y());
423 void InsetCollapsable::validate(LaTeXFeatures & features) const
425 inset.validate(features);
429 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
431 inset.getCursor(bv, x, y);
435 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
437 inset.getCursorPos(bv, x , y);
441 UpdatableInset * InsetCollapsable::getLockingInset() const
443 UpdatableInset * in = inset.getLockingInset();
444 if (const_cast<InsetText *>(&inset) == in)
445 return const_cast<InsetCollapsable *>(this);
450 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
454 return inset.getFirstLockingInsetOfType(c);
458 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
459 bool toggleall, bool selectall)
461 inset.setFont(bv, font, toggleall, selectall);
465 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
466 bool const recursive) const
468 return inset.getLyXText(bv, recursive);
472 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
474 inset.deleteLyXText(bv, recursive);
478 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
480 inset.resizeLyXText(bv, force);
481 LyXFont font(LyXFont::ALL_SANE);
482 oldWidth = width(bv, font);
486 void InsetCollapsable::getLabelList(std::vector<string> & list) const
488 inset.getLabelList(list);
492 bool InsetCollapsable::nodraw() const
494 return inset.nodraw();
498 int InsetCollapsable::scroll(bool recursive) const
500 int sx = UpdatableInset::scroll(false);
503 sx += inset.scroll(recursive);
509 ParagraphList * InsetCollapsable::getParagraphs(int i) const
511 return inset.getParagraphs(i);
515 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
517 return inset.cursor(bv);
521 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
524 return const_cast<InsetCollapsable *>(this);
525 return inset.getInsetFromID(id_arg);
529 void InsetCollapsable::open(BufferView * bv)
531 if (!collapsed_) return;
534 bv->updateInset(this);
538 void InsetCollapsable::close(BufferView * bv) const
544 bv->updateInset(const_cast<InsetCollapsable *>(this));
548 void InsetCollapsable::setLabel(string const & l) const
554 void InsetCollapsable::markErased()
560 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
562 bool found = inset.nextChange(bv, length);
564 if (first_after_edit && !found)
567 first_after_edit = false;
572 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
575 bool found = inset.searchForward(bv, str, cs, mw);
576 if (first_after_edit && !found)
579 first_after_edit = false;
584 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
587 bool found = inset.searchBackward(bv, str, cs, mw);
588 if (first_after_edit && !found)
591 first_after_edit = false;
597 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
599 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
600 if (first_after_edit && word.word().empty())
602 first_after_edit = false;
607 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
609 inset.addPreview(loader);
613 void InsetCollapsable::cache(BufferView * bv) const
615 view_ = bv->owner()->view();
619 BufferView * InsetCollapsable::view() const
621 return view_.lock().get();