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 lyx::graphics::PreviewLoader;
38 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
39 : UpdatableInset(), collapsed_(collapsed), inset(bp),
40 button_dim(0, 0, 0, 0), label("Label"),
44 in_update(false), first_after_edit(false)
47 inset.setAutoBreakRows(true);
48 inset.setDrawFrame(InsetText::ALWAYS);
49 inset.setFrameColor(LColor::collapsableframe);
50 setInsetName("Collapsable");
54 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
55 : UpdatableInset(in), collapsed_(in.collapsed_),
56 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
57 button_dim(0, 0, 0, 0), label(in.label),
59 autocollapse(in.autocollapse),
61 in_update(false), first_after_edit(false)
63 inset.init(&(in.inset));
68 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
70 if (!insetAllowed(in->lyxCode())) {
71 lyxerr << "InsetCollapsable::InsertInset: "
72 "Unable to insert inset." << endl;
75 return inset.insertInset(bv, in);
79 void InsetCollapsable::write(Buffer const & buf, ostream & os) const
81 os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
82 inset.writeParagraphData(buf, os);
86 void InsetCollapsable::read(Buffer const & buf, LyXLex & lex)
90 string const token = lex.getString();
91 if (token == "collapsed") {
93 collapsed_ = lex.getBool();
95 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
97 // Take countermeasures
101 inset.read(buf, lex);
105 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
107 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
111 int InsetCollapsable::height_collapsed() const
114 font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
115 return dim.asc + dim.des;
119 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
121 //lyxerr << "InsetCollapsable::metrics: width: " << mi.base.textwidth << endl;
122 dimension_collapsed(dim);
125 inset.metrics(mi, insetdim);
126 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
127 dim.wid = max(dim.wid, insetdim.wid);
133 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
135 pi.pain.buttonText(x, y, label, labelfont);
139 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
141 BOOST_ASSERT(pi.base.bv);
144 Dimension dim_collapsed;
145 dimension_collapsed(dim_collapsed);
147 int const aa = ascent();
149 button_dim.x2 = dim_collapsed.width();
151 button_dim.y2 = -aa + dim_collapsed.height();
154 draw_collapsed(pi, x, y);
166 int const bl = y - aa + dim_collapsed.ascent();
169 inset.draw(pi, x, y);
171 draw_collapsed(pi, old_x, bl);
172 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
177 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
179 // by default, we are not inlined-drawing
180 draw(pi, x, y, false);
184 InsetOld::EDITABLE InsetCollapsable::editable() const
186 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
190 void InsetCollapsable::insetUnlock(BufferView * bv)
194 if (change_label_with_text) {
195 draw_label = get_new_label();
202 inset.insetUnlock(bv);
205 bv->updateInset(this);
209 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
211 FuncRequest cmd1 = cmd;
212 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
217 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
220 BufferView * bv = cmd.view();
222 if (collapsed_ && cmd.button() != mouse_button::button3) {
224 bv->updateInset(this);
225 bv->buffer()->markDirty();
229 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
234 bv->unlockInset(this);
236 bv->updateInset(this);
237 bv->buffer()->markDirty();
238 } else if (!collapsed_ && cmd.y > button_dim.y2) {
239 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
241 if (cmd.button() == mouse_button::button3 && !ret)
246 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
247 LatexRunParams const & runparams) const
249 return inset.latex(buf, os, runparams);
253 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
255 return inset.ascii(buf, os, ll);
259 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
261 return inset.linuxdoc(buf, os);
265 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
267 return inset.docbook(buf, os, mixcont);
271 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
273 return button_dim.contains(cmd.x, cmd.y);
277 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
279 //lyxerr << "InsetCollapsable::localDispatch: "
280 // << cmd.action << " '" << cmd.argument << "'\n";
281 BufferView * bv = cmd.view();
282 switch (cmd.action) {
283 case LFUN_INSET_EDIT: {
284 if (!cmd.argument.empty()) {
285 UpdatableInset::localDispatch(cmd);
287 lyxerr << "branch collapsed_" << endl;
289 if (bv->lockInset(this)) {
290 bv->updateInset(this);
291 bv->buffer()->markDirty();
292 inset.localDispatch(cmd);
293 first_after_edit = true;
296 lyxerr << "branch not collapsed_" << endl;
297 if (bv->lockInset(this))
298 inset.localDispatch(cmd);
304 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
306 if (cmd.button() == mouse_button::button3)
309 UpdatableInset::localDispatch(cmd);
313 // set this only here as it should be recollapsed only if
314 // it was already collapsed!
315 first_after_edit = true;
316 if (!bv->lockInset(this))
318 bv->updateInset(this);
319 bv->buffer()->markDirty();
320 inset.localDispatch(cmd);
322 if (!bv->lockInset(this))
324 if (cmd.y <= button_dim.y2) {
325 FuncRequest cmd1 = cmd;
327 inset.localDispatch(cmd1);
329 inset.localDispatch(adjustCommand(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();