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_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, InsetOld * 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 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << "\n";
133 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);
144 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
146 pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
150 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
158 Dimension dim_collapsed;
159 dimension_collapsed(dim_collapsed);
161 int const aa = ascent();
162 button_length = dim_collapsed.width();
164 button_bottom_y = -aa + dim_collapsed.height();
167 draw_collapsed(pi, x, y);
179 int const bl = y - aa + dim_collapsed.ascent();
182 inset.draw(pi, x, y);
184 draw_collapsed(pi, old_x, bl);
185 int const yy = bl + dim_collapsed.descent() + inset.ascent();
186 inset.draw(pi, x, yy);
191 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
193 // by default, we are not inlined-drawing
194 draw(pi, x, y, false);
198 InsetOld::EDITABLE InsetCollapsable::editable() const
200 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
204 void InsetCollapsable::insetUnlock(BufferView * bv)
208 if (change_label_with_text) {
209 draw_label = get_new_label();
216 inset.insetUnlock(bv);
219 bv->updateInset(this);
223 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
225 FuncRequest cmd1 = cmd;
226 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
231 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
234 BufferView * bv = cmd.view();
236 if (collapsed_ && cmd.button() != mouse_button::button3) {
238 inset.setUpdateStatus(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(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 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
293 lyxerr << "InsetCollapsable::localDispatch: "
294 << cmd.action << " '" << cmd.argument << "'\n";
295 BufferView * bv = cmd.view();
296 switch (cmd.action) {
297 case LFUN_INSET_EDIT: {
298 if (!cmd.argument.empty()) {
299 UpdatableInset::localDispatch(cmd);
301 lyxerr << "branch collapsed_\n";
303 if (bv->lockInset(this)) {
304 inset.setUpdateStatus(InsetText::FULL);
305 bv->updateInset(this);
306 bv->buffer()->markDirty();
307 inset.localDispatch(cmd);
308 first_after_edit = true;
311 lyxerr << "branch not collapsed_\n";
312 if (bv->lockInset(this))
313 inset.localDispatch(cmd);
319 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
321 if (cmd.button() == mouse_button::button3)
324 UpdatableInset::localDispatch(cmd);
328 // set this only here as it should be recollapsed only if
329 // it was already collapsed!
330 first_after_edit = true;
331 if (!bv->lockInset(this))
333 bv->updateInset(this);
334 bv->buffer()->markDirty();
335 inset.localDispatch(cmd);
337 FuncRequest cmd1 = cmd;
338 if (!bv->lockInset(this))
340 if (cmd.y <= button_bottom_y) {
343 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
345 inset.localDispatch(cmd);
350 case LFUN_MOUSE_PRESS:
351 if (!collapsed_ && cmd.y > button_bottom_y)
352 inset.localDispatch(adjustCommand(cmd));
355 case LFUN_MOUSE_MOTION:
356 if (!collapsed_ && cmd.y > button_bottom_y)
357 inset.localDispatch(adjustCommand(cmd));
360 case LFUN_MOUSE_RELEASE:
361 lfunMouseRelease(cmd);
365 UpdatableInset::RESULT result = inset.localDispatch(cmd);
366 if (result >= FINISHED)
367 bv->unlockInset(this);
368 first_after_edit = false;
374 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
378 return inset.lockInsetInInset(bv, in);
382 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
386 bv->unlockInset(this);
389 return inset.unlockInsetInInset(bv, in, lr);
393 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
397 return inset.updateInsetInInset(bv, in);
401 int InsetCollapsable::insetInInsetY() const
403 return inset.insetInInsetY() - (top_baseline - inset.y());
407 void InsetCollapsable::validate(LaTeXFeatures & features) const
409 inset.validate(features);
413 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
415 inset.getCursor(bv, x, y);
419 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
421 inset.getCursorPos(bv, x , y);
425 UpdatableInset * InsetCollapsable::getLockingInset() const
427 UpdatableInset * in = inset.getLockingInset();
429 return const_cast<InsetCollapsable *>(this);
434 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
438 return inset.getFirstLockingInsetOfType(c);
442 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
443 bool toggleall, bool selectall)
445 inset.setFont(bv, font, toggleall, selectall);
449 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
450 bool const recursive) const
452 return inset.getLyXText(bv, recursive);
456 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
458 inset.deleteLyXText(bv, recursive);
462 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
464 inset.resizeLyXText(bv, force);
469 void InsetCollapsable::getLabelList(std::vector<string> & list) const
471 inset.getLabelList(list);
475 bool InsetCollapsable::nodraw() const
477 return inset.nodraw();
481 int InsetCollapsable::scroll(bool recursive) const
483 int sx = UpdatableInset::scroll(false);
486 sx += inset.scroll(recursive);
492 ParagraphList * InsetCollapsable::getParagraphs(int i) const
494 return inset.getParagraphs(i);
498 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
500 return inset.cursor(bv);
504 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
507 return const_cast<InsetCollapsable *>(this);
508 return inset.getInsetFromID(id_arg);
512 void InsetCollapsable::open(BufferView * bv)
514 if (!collapsed_) return;
517 bv->updateInset(this);
521 void InsetCollapsable::close(BufferView * bv) const
527 bv->updateInset(const_cast<InsetCollapsable *>(this));
531 void InsetCollapsable::setLabel(string const & l) const
537 void InsetCollapsable::markErased()
543 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
545 bool found = inset.nextChange(bv, length);
547 if (first_after_edit && !found)
550 first_after_edit = false;
555 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
558 bool found = inset.searchForward(bv, str, cs, mw);
559 if (first_after_edit && !found)
562 first_after_edit = false;
567 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
570 bool found = inset.searchBackward(bv, str, cs, mw);
571 if (first_after_edit && !found)
574 first_after_edit = false;
580 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
582 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
583 if (first_after_edit && word.word().empty())
585 first_after_edit = false;
590 void InsetCollapsable::addPreview(PreviewLoader & loader) const
592 inset.addPreview(loader);
596 void InsetCollapsable::cache(BufferView * bv) const
598 view_ = bv->owner()->view();
602 BufferView * InsetCollapsable::view() const
604 return view_.lock().get();