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
152 Dimension dim_collapsed;
153 dimension_collapsed(dim_collapsed);
155 int const aa = ascent();
157 button_dim.x2 = dim_collapsed.width();
159 button_dim.y2 = -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 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
185 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
187 // by default, we are not inlined-drawing
188 draw(pi, x, y, false);
192 InsetOld::EDITABLE InsetCollapsable::editable() const
194 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
198 void InsetCollapsable::insetUnlock(BufferView * bv)
202 if (change_label_with_text) {
203 draw_label = get_new_label();
210 inset.insetUnlock(bv);
213 bv->updateInset(this);
217 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
219 FuncRequest cmd1 = cmd;
220 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
225 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
228 BufferView * bv = cmd.view();
230 if (collapsed_ && cmd.button() != mouse_button::button3) {
232 inset.setUpdateStatus(InsetText::FULL);
233 bv->updateInset(this);
234 bv->buffer()->markDirty();
238 if (cmd.button() != mouse_button::button3 && hitButton(cmd))
242 inset.setUpdateStatus(InsetText::FULL);
243 bv->updateInset(this);
244 bv->buffer()->markDirty();
247 bv->unlockInset(this);
248 bv->updateInset(this);
249 bv->buffer()->markDirty();
251 } else if (!collapsed_ && (cmd.y > button_dim.y2)) {
252 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
254 if (cmd.button() == mouse_button::button3 && !ret)
259 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
260 LatexRunParams const & runparams) const
262 return inset.latex(buf, os, runparams);
266 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
268 return inset.ascii(buf, os, ll);
272 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
274 return inset.linuxdoc(buf, os);
278 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
280 return inset.docbook(buf, os, mixcont);
284 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
286 return button_dim.contained(cmd.x, cmd.y);
290 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
292 //lyxerr << "InsetCollapsable::localDispatch: "
293 // << cmd.action << " '" << cmd.argument << "'\n";
294 BufferView * bv = cmd.view();
295 switch (cmd.action) {
296 case LFUN_INSET_EDIT: {
297 if (!cmd.argument.empty()) {
298 UpdatableInset::localDispatch(cmd);
300 lyxerr << "branch collapsed_\n";
302 if (bv->lockInset(this)) {
303 inset.setUpdateStatus(InsetText::FULL);
304 bv->updateInset(this);
305 bv->buffer()->markDirty();
306 inset.localDispatch(cmd);
307 first_after_edit = true;
310 lyxerr << "branch not collapsed_\n";
311 if (bv->lockInset(this))
312 inset.localDispatch(cmd);
318 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
320 if (cmd.button() == mouse_button::button3)
323 UpdatableInset::localDispatch(cmd);
327 // set this only here as it should be recollapsed only if
328 // it was already collapsed!
329 first_after_edit = true;
330 if (!bv->lockInset(this))
332 bv->updateInset(this);
333 bv->buffer()->markDirty();
334 inset.localDispatch(cmd);
336 FuncRequest cmd1 = cmd;
337 if (!bv->lockInset(this))
339 if (cmd.y <= button_dim.y2) {
342 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
344 inset.localDispatch(cmd);
349 case LFUN_MOUSE_PRESS:
350 if (!collapsed_ && cmd.y > button_dim.y2)
351 inset.localDispatch(adjustCommand(cmd));
354 case LFUN_MOUSE_MOTION:
355 if (!collapsed_ && cmd.y > button_dim.y2)
356 inset.localDispatch(adjustCommand(cmd));
359 case LFUN_MOUSE_RELEASE:
360 lfunMouseRelease(cmd);
364 UpdatableInset::RESULT result = inset.localDispatch(cmd);
365 if (result >= FINISHED)
366 bv->unlockInset(this);
367 first_after_edit = false;
373 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
377 return inset.lockInsetInInset(bv, in);
381 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
385 bv->unlockInset(this);
388 return inset.unlockInsetInInset(bv, in, lr);
392 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
396 return inset.updateInsetInInset(bv, in);
400 int InsetCollapsable::insetInInsetY() const
402 return inset.insetInInsetY() - (top_baseline - inset.y());
406 void InsetCollapsable::validate(LaTeXFeatures & features) const
408 inset.validate(features);
412 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
414 inset.getCursor(bv, x, y);
418 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
420 inset.getCursorPos(bv, x , y);
424 UpdatableInset * InsetCollapsable::getLockingInset() const
426 UpdatableInset * in = inset.getLockingInset();
428 return const_cast<InsetCollapsable *>(this);
433 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
437 return inset.getFirstLockingInsetOfType(c);
441 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
442 bool toggleall, bool selectall)
444 inset.setFont(bv, font, toggleall, selectall);
448 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
449 bool const recursive) const
451 return inset.getLyXText(bv, recursive);
455 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
457 inset.deleteLyXText(bv, recursive);
461 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
463 inset.resizeLyXText(bv, force);
468 void InsetCollapsable::getLabelList(std::vector<string> & list) const
470 inset.getLabelList(list);
474 int InsetCollapsable::scroll(bool recursive) const
476 int sx = UpdatableInset::scroll(false);
479 sx += inset.scroll(recursive);
485 ParagraphList * InsetCollapsable::getParagraphs(int i) const
487 return inset.getParagraphs(i);
491 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
493 return inset.cursor(bv);
497 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
500 return const_cast<InsetCollapsable *>(this);
501 return inset.getInsetFromID(id_arg);
505 void InsetCollapsable::open(BufferView * bv)
507 if (!collapsed_) return;
510 bv->updateInset(this);
514 void InsetCollapsable::close(BufferView * bv) const
520 bv->updateInset(const_cast<InsetCollapsable *>(this));
524 void InsetCollapsable::setLabel(string const & l) const
530 void InsetCollapsable::markErased()
536 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
538 bool found = inset.nextChange(bv, length);
540 if (first_after_edit && !found)
543 first_after_edit = false;
548 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
551 bool found = inset.searchForward(bv, str, cs, mw);
552 if (first_after_edit && !found)
555 first_after_edit = false;
560 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
563 bool found = inset.searchBackward(bv, str, cs, mw);
564 if (first_after_edit && !found)
567 first_after_edit = false;
573 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
575 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
576 if (first_after_edit && word.word().empty())
578 first_after_edit = false;
583 void InsetCollapsable::addPreview(PreviewLoader & loader) const
585 inset.addPreview(loader);
589 void InsetCollapsable::cache(BufferView * bv) const
591 view_ = bv->owner()->view();
595 BufferView * InsetCollapsable::view() const
597 return view_.lock().get();