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"
18 #include "BufferView.h"
21 #include "funcrequest.h"
22 #include "metricsinfo.h"
23 #include "paragraph.h"
24 #include "WordLangTuple.h"
26 #include "frontends/font_metrics.h"
27 #include "frontends/Painter.h"
28 #include "frontends/LyXView.h"
30 #include "support/LAssert.h"
32 using namespace lyx::support;
33 using namespace lyx::graphics;
40 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
41 : UpdatableInset(), collapsed_(collapsed), inset(bp),
42 button_dim(0, 0, 0, 0), label("Label"),
46 in_update(false), first_after_edit(false)
49 inset.setAutoBreakRows(true);
50 inset.setDrawFrame(InsetText::ALWAYS);
51 inset.setFrameColor(LColor::collapsableframe);
52 setInsetName("Collapsable");
56 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
57 : UpdatableInset(in), collapsed_(in.collapsed_),
58 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
59 button_dim(0, 0, 0, 0), label(in.label),
61 autocollapse(in.autocollapse),
63 in_update(false), first_after_edit(false)
65 inset.init(&(in.inset));
70 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
72 if (!insetAllowed(in->lyxCode())) {
73 lyxerr << "InsetCollapsable::InsertInset: "
74 "Unable to insert inset." << endl;
77 return inset.insertInset(bv, in);
81 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
83 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
84 inset.writeParagraphData(buf, os);
88 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
92 string const token = lex.getString();
93 if (token == "collapsed") {
95 collapsed_ = lex.getBool();
97 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
99 // Take countermeasures
100 lex.pushToken(token);
103 inset.read(buf, lex);
107 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
109 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
113 int InsetCollapsable::height_collapsed() const
116 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
117 return dim.asc + dim.des;
121 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
123 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
124 dimension_collapsed(dim);
127 inset.metrics(mi, insetdim);
128 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
129 dim.wid = max(dim.wid, insetdim.wid);
135 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
137 pi.pain.buttonText(x, y, label, labelfont);
141 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
146 Dimension dim_collapsed;
147 dimension_collapsed(dim_collapsed);
149 int const aa = ascent();
151 button_dim.x2 = dim_collapsed.width();
153 button_dim.y2 = -aa + dim_collapsed.height();
156 draw_collapsed(pi, x, y);
168 int const bl = y - aa + dim_collapsed.ascent();
171 inset.draw(pi, x, y);
173 draw_collapsed(pi, old_x, bl);
174 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
179 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
181 // by default, we are not inlined-drawing
182 draw(pi, x, y, false);
186 InsetOld::EDITABLE InsetCollapsable::editable() const
188 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
192 void InsetCollapsable::insetUnlock(BufferView * bv)
196 if (change_label_with_text) {
197 draw_label = get_new_label();
204 inset.insetUnlock(bv);
207 bv->updateInset(this);
211 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
213 FuncRequest cmd1 = cmd;
214 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
219 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
222 BufferView * bv = cmd.view();
224 if (collapsed_ && cmd.button() != mouse_button::button3) {
226 bv->updateInset(this);
227 bv->buffer()->markDirty();
231 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
236 bv->unlockInset(this);
238 bv->updateInset(this);
239 bv->buffer()->markDirty();
240 } else if (!collapsed_ && cmd.y > button_dim.y2) {
241 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
243 if (cmd.button() == mouse_button::button3 && !ret)
248 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
249 LatexRunParams const & runparams) const
251 return inset.latex(buf, os, runparams);
255 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
257 return inset.ascii(buf, os, ll);
261 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
263 return inset.linuxdoc(buf, os);
267 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
269 return inset.docbook(buf, os, mixcont);
273 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
275 return button_dim.contains(cmd.x, cmd.y);
279 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
281 //lyxerr << "InsetCollapsable::localDispatch: "
282 // << cmd.action << " '" << cmd.argument << "'\n";
283 BufferView * bv = cmd.view();
284 switch (cmd.action) {
285 case LFUN_INSET_EDIT: {
286 if (!cmd.argument.empty()) {
287 UpdatableInset::localDispatch(cmd);
289 lyxerr << "branch collapsed_" << endl;
291 if (bv->lockInset(this)) {
292 bv->updateInset(this);
293 bv->buffer()->markDirty();
294 inset.localDispatch(cmd);
295 first_after_edit = true;
298 lyxerr << "branch not collapsed_" << endl;
299 if (bv->lockInset(this))
300 inset.localDispatch(cmd);
306 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
308 if (cmd.button() == mouse_button::button3)
311 UpdatableInset::localDispatch(cmd);
315 // set this only here as it should be recollapsed only if
316 // it was already collapsed!
317 first_after_edit = true;
318 if (!bv->lockInset(this))
320 bv->updateInset(this);
321 bv->buffer()->markDirty();
322 inset.localDispatch(cmd);
324 FuncRequest cmd1 = cmd;
325 if (!bv->lockInset(this))
327 if (cmd.y <= button_dim.y2) {
330 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
332 inset.localDispatch(cmd);
337 case LFUN_MOUSE_PRESS:
338 if (!collapsed_ && cmd.y > button_dim.y2)
339 inset.localDispatch(adjustCommand(cmd));
342 case LFUN_MOUSE_MOTION:
343 if (!collapsed_ && cmd.y > button_dim.y2)
344 inset.localDispatch(adjustCommand(cmd));
347 case LFUN_MOUSE_RELEASE:
348 lfunMouseRelease(cmd);
352 UpdatableInset::RESULT result = inset.localDispatch(cmd);
353 if (result >= FINISHED)
354 bv->unlockInset(this);
355 first_after_edit = false;
361 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
365 return inset.lockInsetInInset(bv, in);
369 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
373 bv->unlockInset(this);
376 return inset.unlockInsetInInset(bv, in, lr);
380 int InsetCollapsable::insetInInsetY() const
382 return inset.insetInInsetY() - (top_baseline - inset.y());
386 void InsetCollapsable::validate(LaTeXFeatures & features) const
388 inset.validate(features);
392 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
394 inset.getCursor(bv, x, y);
398 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
400 inset.getCursorPos(bv, x , y);
404 UpdatableInset * InsetCollapsable::getLockingInset() const
406 UpdatableInset * in = inset.getLockingInset();
408 return const_cast<InsetCollapsable *>(this);
413 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
417 return inset.getFirstLockingInsetOfType(c);
421 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
422 bool toggleall, bool selectall)
424 inset.setFont(bv, font, toggleall, selectall);
428 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
429 bool const recursive) const
431 return inset.getLyXText(bv, recursive);
435 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
437 inset.deleteLyXText(bv, recursive);
441 void InsetCollapsable::getLabelList(std::vector<string> & list) const
443 inset.getLabelList(list);
447 int InsetCollapsable::scroll(bool recursive) const
449 int sx = UpdatableInset::scroll(false);
452 sx += inset.scroll(recursive);
458 ParagraphList * InsetCollapsable::getParagraphs(int i) const
460 return inset.getParagraphs(i);
464 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
466 return inset.cursor(bv);
470 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
473 return const_cast<InsetCollapsable *>(this);
474 return inset.getInsetFromID(id_arg);
478 void InsetCollapsable::open(BufferView * bv)
484 bv->updateInset(this);
488 void InsetCollapsable::close(BufferView * bv) const
494 bv->updateInset(this);
498 void InsetCollapsable::setLabel(string const & l) const
504 void InsetCollapsable::markErased()
510 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
512 bool found = inset.nextChange(bv, length);
514 if (first_after_edit && !found)
517 first_after_edit = false;
522 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
525 bool found = inset.searchForward(bv, str, cs, mw);
526 if (first_after_edit && !found)
529 first_after_edit = false;
534 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
537 bool found = inset.searchBackward(bv, str, cs, mw);
538 if (first_after_edit && !found)
541 first_after_edit = false;
547 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
549 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
550 if (first_after_edit && word.word().empty())
552 first_after_edit = false;
557 void InsetCollapsable::addPreview(PreviewLoader & loader) const
559 inset.addPreview(loader);
563 void InsetCollapsable::cache(BufferView * bv) const
565 view_ = bv->owner()->view();
569 BufferView * InsetCollapsable::view() const
571 return view_.lock().get();