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);
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 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << "\n";
130 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);
141 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
143 pi.pain.buttonText(x, 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();
160 button_dim.x2 = dim_collapsed.width();
162 button_dim.y2 = -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 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
188 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
190 // by default, we are not inlined-drawing
191 draw(pi, x, y, false);
195 InsetOld::EDITABLE InsetCollapsable::editable() const
197 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
201 void InsetCollapsable::insetUnlock(BufferView * bv)
205 if (change_label_with_text) {
206 draw_label = get_new_label();
213 inset.insetUnlock(bv);
216 bv->updateInset(this);
220 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
222 FuncRequest cmd1 = cmd;
223 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
228 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
231 BufferView * bv = cmd.view();
233 if (collapsed_ && cmd.button() != mouse_button::button3) {
235 inset.setUpdateStatus(InsetText::FULL);
236 bv->updateInset(this);
237 bv->buffer()->markDirty();
241 if (cmd.button() != mouse_button::button3 && hitButton(cmd))
245 inset.setUpdateStatus(InsetText::FULL);
246 bv->updateInset(this);
247 bv->buffer()->markDirty();
250 bv->unlockInset(this);
251 bv->updateInset(this);
252 bv->buffer()->markDirty();
254 } else if (!collapsed_ && (cmd.y > button_dim.y2)) {
255 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
257 if (cmd.button() == mouse_button::button3 && !ret)
262 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
263 LatexRunParams const & runparams) const
265 return inset.latex(buf, os, runparams);
269 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
271 return inset.ascii(buf, os, ll);
275 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
277 return inset.linuxdoc(buf, os);
281 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
283 return inset.docbook(buf, os, mixcont);
287 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
289 return button_dim.contained(cmd.x, cmd.y);
293 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
295 lyxerr << "InsetCollapsable::localDispatch: "
296 << cmd.action << " '" << cmd.argument << "'\n";
297 BufferView * bv = cmd.view();
298 switch (cmd.action) {
299 case LFUN_INSET_EDIT: {
300 if (!cmd.argument.empty()) {
301 UpdatableInset::localDispatch(cmd);
303 lyxerr << "branch collapsed_\n";
305 if (bv->lockInset(this)) {
306 inset.setUpdateStatus(InsetText::FULL);
307 bv->updateInset(this);
308 bv->buffer()->markDirty();
309 inset.localDispatch(cmd);
310 first_after_edit = true;
313 lyxerr << "branch not collapsed_\n";
314 if (bv->lockInset(this))
315 inset.localDispatch(cmd);
321 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
323 if (cmd.button() == mouse_button::button3)
326 UpdatableInset::localDispatch(cmd);
330 // set this only here as it should be recollapsed only if
331 // it was already collapsed!
332 first_after_edit = true;
333 if (!bv->lockInset(this))
335 bv->updateInset(this);
336 bv->buffer()->markDirty();
337 inset.localDispatch(cmd);
339 FuncRequest cmd1 = cmd;
340 if (!bv->lockInset(this))
342 if (cmd.y <= button_dim.y2) {
345 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
347 inset.localDispatch(cmd);
352 case LFUN_MOUSE_PRESS:
353 if (!collapsed_ && cmd.y > button_dim.y2)
354 inset.localDispatch(adjustCommand(cmd));
357 case LFUN_MOUSE_MOTION:
358 if (!collapsed_ && cmd.y > button_dim.y2)
359 inset.localDispatch(adjustCommand(cmd));
362 case LFUN_MOUSE_RELEASE:
363 lfunMouseRelease(cmd);
367 UpdatableInset::RESULT result = inset.localDispatch(cmd);
368 if (result >= FINISHED)
369 bv->unlockInset(this);
370 first_after_edit = false;
376 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
380 return inset.lockInsetInInset(bv, in);
384 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
388 bv->unlockInset(this);
391 return inset.unlockInsetInInset(bv, in, lr);
395 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
399 return inset.updateInsetInInset(bv, in);
403 int InsetCollapsable::insetInInsetY() const
405 return inset.insetInInsetY() - (top_baseline - inset.y());
409 void InsetCollapsable::validate(LaTeXFeatures & features) const
411 inset.validate(features);
415 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
417 inset.getCursor(bv, x, y);
421 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
423 inset.getCursorPos(bv, x , y);
427 UpdatableInset * InsetCollapsable::getLockingInset() const
429 UpdatableInset * in = inset.getLockingInset();
431 return const_cast<InsetCollapsable *>(this);
436 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
440 return inset.getFirstLockingInsetOfType(c);
444 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
445 bool toggleall, bool selectall)
447 inset.setFont(bv, font, toggleall, selectall);
451 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
452 bool const recursive) const
454 return inset.getLyXText(bv, recursive);
458 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
460 inset.deleteLyXText(bv, recursive);
464 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
466 inset.resizeLyXText(bv, force);
471 void InsetCollapsable::getLabelList(std::vector<string> & list) const
473 inset.getLabelList(list);
477 bool InsetCollapsable::nodraw() const
479 return inset.nodraw();
483 int InsetCollapsable::scroll(bool recursive) const
485 int sx = UpdatableInset::scroll(false);
488 sx += inset.scroll(recursive);
494 ParagraphList * InsetCollapsable::getParagraphs(int i) const
496 return inset.getParagraphs(i);
500 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
502 return inset.cursor(bv);
506 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
509 return const_cast<InsetCollapsable *>(this);
510 return inset.getInsetFromID(id_arg);
514 void InsetCollapsable::open(BufferView * bv)
516 if (!collapsed_) return;
519 bv->updateInset(this);
523 void InsetCollapsable::close(BufferView * bv) const
529 bv->updateInset(const_cast<InsetCollapsable *>(this));
533 void InsetCollapsable::setLabel(string const & l) const
539 void InsetCollapsable::markErased()
545 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
547 bool found = inset.nextChange(bv, length);
549 if (first_after_edit && !found)
552 first_after_edit = false;
557 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
560 bool found = inset.searchForward(bv, str, cs, mw);
561 if (first_after_edit && !found)
564 first_after_edit = false;
569 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
572 bool found = inset.searchBackward(bv, str, cs, mw);
573 if (first_after_edit && !found)
576 first_after_edit = false;
582 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
584 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
585 if (first_after_edit && word.word().empty())
587 first_after_edit = false;
592 void InsetCollapsable::addPreview(PreviewLoader & loader) const
594 inset.addPreview(loader);
598 void InsetCollapsable::cache(BufferView * bv) const
600 view_ = bv->owner()->view();
604 BufferView * InsetCollapsable::view() const
606 return view_.lock().get();