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 using namespace lyx::graphics;
37 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
38 : UpdatableInset(), collapsed_(collapsed), inset(bp),
39 button_dim(0, 0, 0, 0), label("Label"),
43 in_update(false), first_after_edit(false)
46 inset.setAutoBreakRows(true);
47 inset.setDrawFrame(InsetText::ALWAYS);
48 inset.setFrameColor(LColor::collapsableframe);
49 setInsetName("Collapsable");
53 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
54 : UpdatableInset(in), collapsed_(in.collapsed_),
55 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
56 button_dim(0, 0, 0, 0), label(in.label),
58 autocollapse(in.autocollapse),
60 in_update(false), first_after_edit(false)
62 inset.init(&(in.inset));
67 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
69 if (!insetAllowed(in->lyxCode())) {
70 lyxerr << "InsetCollapsable::InsertInset: "
71 "Unable to insert inset." << endl;
74 return inset.insertInset(bv, in);
78 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
80 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
81 inset.writeParagraphData(buf, os);
85 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
89 string const token = lex.getString();
90 if (token == "collapsed") {
92 collapsed_ = lex.getBool();
94 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
96 // Take countermeasures
100 inset.read(buf, lex);
104 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
106 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
110 int InsetCollapsable::height_collapsed() const
113 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
114 return dim.asc + dim.des;
118 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
120 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
121 dimension_collapsed(dim);
124 inset.metrics(mi, insetdim);
125 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
126 dim.wid = max(dim.wid, insetdim.wid);
132 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
134 pi.pain.buttonText(x, y, label, labelfont);
138 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
140 BOOST_ASSERT(pi.base.bv);
143 Dimension dim_collapsed;
144 dimension_collapsed(dim_collapsed);
146 int const aa = ascent();
148 button_dim.x2 = dim_collapsed.width();
150 button_dim.y2 = -aa + dim_collapsed.height();
153 draw_collapsed(pi, x, y);
165 int const bl = y - aa + dim_collapsed.ascent();
168 inset.draw(pi, x, y);
170 draw_collapsed(pi, old_x, bl);
171 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
176 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
178 // by default, we are not inlined-drawing
179 draw(pi, x, y, false);
183 InsetOld::EDITABLE InsetCollapsable::editable() const
185 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
189 void InsetCollapsable::insetUnlock(BufferView * bv)
193 if (change_label_with_text) {
194 draw_label = get_new_label();
201 inset.insetUnlock(bv);
204 bv->updateInset(this);
208 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
210 FuncRequest cmd1 = cmd;
211 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
216 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
219 BufferView * bv = cmd.view();
221 if (collapsed_ && cmd.button() != mouse_button::button3) {
223 bv->updateInset(this);
224 bv->buffer()->markDirty();
228 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
233 bv->unlockInset(this);
235 bv->updateInset(this);
236 bv->buffer()->markDirty();
237 } else if (!collapsed_ && cmd.y > button_dim.y2) {
238 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
240 if (cmd.button() == mouse_button::button3 && !ret)
245 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
246 LatexRunParams const & runparams) const
248 return inset.latex(buf, os, runparams);
252 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
254 return inset.ascii(buf, os, ll);
258 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
260 return inset.linuxdoc(buf, os);
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
266 return inset.docbook(buf, os, mixcont);
270 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
272 return button_dim.contains(cmd.x, cmd.y);
276 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
278 //lyxerr << "InsetCollapsable::localDispatch: "
279 // << cmd.action << " '" << cmd.argument << "'\n";
280 BufferView * bv = cmd.view();
281 switch (cmd.action) {
282 case LFUN_INSET_EDIT: {
283 if (!cmd.argument.empty()) {
284 UpdatableInset::localDispatch(cmd);
286 lyxerr << "branch collapsed_" << endl;
288 if (bv->lockInset(this)) {
289 bv->updateInset(this);
290 bv->buffer()->markDirty();
291 inset.localDispatch(cmd);
292 first_after_edit = true;
295 lyxerr << "branch not collapsed_" << endl;
296 if (bv->lockInset(this))
297 inset.localDispatch(cmd);
303 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
305 if (cmd.button() == mouse_button::button3)
308 UpdatableInset::localDispatch(cmd);
312 // set this only here as it should be recollapsed only if
313 // it was already collapsed!
314 first_after_edit = true;
315 if (!bv->lockInset(this))
317 bv->updateInset(this);
318 bv->buffer()->markDirty();
319 inset.localDispatch(cmd);
321 FuncRequest cmd1 = cmd;
322 if (!bv->lockInset(this))
324 if (cmd.y <= button_dim.y2) {
327 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
329 inset.localDispatch(cmd);
334 case LFUN_MOUSE_PRESS:
335 if (!collapsed_ && cmd.y > button_dim.y2)
336 inset.localDispatch(adjustCommand(cmd));
339 case LFUN_MOUSE_MOTION:
340 if (!collapsed_ && cmd.y > button_dim.y2)
341 inset.localDispatch(adjustCommand(cmd));
344 case LFUN_MOUSE_RELEASE:
345 lfunMouseRelease(cmd);
349 UpdatableInset::RESULT result = inset.localDispatch(cmd);
350 if (result >= FINISHED)
351 bv->unlockInset(this);
352 first_after_edit = false;
358 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
362 return inset.lockInsetInInset(bv, in);
366 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
370 bv->unlockInset(this);
373 return inset.unlockInsetInInset(bv, in, lr);
377 int InsetCollapsable::insetInInsetY() const
379 return inset.insetInInsetY() - (top_baseline - inset.y());
383 void InsetCollapsable::validate(LaTeXFeatures & features) const
385 inset.validate(features);
389 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
391 inset.getCursor(bv, x, y);
395 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
397 inset.getCursorPos(bv, x , y);
401 UpdatableInset * InsetCollapsable::getLockingInset() const
403 UpdatableInset * in = inset.getLockingInset();
405 return const_cast<InsetCollapsable *>(this);
410 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
414 return inset.getFirstLockingInsetOfType(c);
418 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
419 bool toggleall, bool selectall)
421 inset.setFont(bv, font, toggleall, selectall);
425 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
426 bool const recursive) const
428 return inset.getLyXText(bv, recursive);
432 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
434 inset.deleteLyXText(bv, recursive);
438 void InsetCollapsable::getLabelList(std::vector<string> & list) const
440 inset.getLabelList(list);
444 int InsetCollapsable::scroll(bool recursive) const
446 int sx = UpdatableInset::scroll(false);
449 sx += inset.scroll(recursive);
455 ParagraphList * InsetCollapsable::getParagraphs(int i) const
457 return inset.getParagraphs(i);
461 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
463 return inset.cursor(bv);
467 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
470 return const_cast<InsetCollapsable *>(this);
471 return inset.getInsetFromID(id_arg);
475 void InsetCollapsable::open(BufferView * bv)
481 bv->updateInset(this);
485 void InsetCollapsable::close(BufferView * bv) const
491 bv->updateInset(this);
495 void InsetCollapsable::setLabel(string const & l) const
501 void InsetCollapsable::markErased()
507 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
509 bool found = inset.nextChange(bv, length);
511 if (first_after_edit && !found)
514 first_after_edit = false;
519 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
522 bool found = inset.searchForward(bv, str, cs, mw);
523 if (first_after_edit && !found)
526 first_after_edit = false;
531 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
534 bool found = inset.searchBackward(bv, str, cs, mw);
535 if (first_after_edit && !found)
538 first_after_edit = false;
544 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
546 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
547 if (first_after_edit && word.word().empty())
549 first_after_edit = false;
554 void InsetCollapsable::addPreview(PreviewLoader & loader) const
556 inset.addPreview(loader);
560 void InsetCollapsable::cache(BufferView * bv) const
562 view_ = bv->owner()->view();
566 BufferView * InsetCollapsable::view() const
568 return view_.lock().get();