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"
22 #include "funcrequest.h"
23 #include "metricsinfo.h"
24 #include "paragraph.h"
25 #include "WordLangTuple.h"
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "frontends/LyXView.h"
32 using lyx::graphics::PreviewLoader;
40 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
41 : UpdatableInset(), inset(bp), collapsed_(collapsed),
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), inset(in.inset), collapsed_(in.collapsed_),
58 labelfont_(in.labelfont_), button_dim(0, 0, 0, 0), label(in.label),
60 autocollapse(in.autocollapse),
62 in_update(false), first_after_edit(false)
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 Dimension dim_collapsed;
142 dimension_collapsed(dim_collapsed);
144 int const aa = ascent();
146 button_dim.x2 = dim_collapsed.width();
148 button_dim.y2 = -aa + dim_collapsed.height();
151 draw_collapsed(pi, x, y);
163 int const bl = y - aa + dim_collapsed.ascent();
166 inset.draw(pi, x, y);
168 draw_collapsed(pi, old_x, bl);
169 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
174 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
176 // by default, we are not inlined-drawing
177 draw(pi, x, y, false);
181 InsetOld::EDITABLE InsetCollapsable::editable() const
183 return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
187 void InsetCollapsable::insetUnlock(BufferView * bv)
191 if (change_label_with_text) {
192 draw_label = get_new_label();
199 inset.insetUnlock(bv);
202 bv->updateInset(this);
206 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
208 FuncRequest cmd1 = cmd;
209 cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
214 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
217 BufferView * bv = cmd.view();
219 if (collapsed_ && cmd.button() != mouse_button::button3) {
221 bv->updateInset(this);
222 bv->buffer()->markDirty();
226 if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
231 bv->unlockInset(this);
233 bv->updateInset(this);
234 bv->buffer()->markDirty();
235 } else if (!collapsed_ && cmd.y > button_dim.y2) {
236 ret = inset.localDispatch(adjustCommand(cmd)) == DISPATCHED;
238 if (cmd.button() == mouse_button::button3 && !ret)
243 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
244 LatexRunParams const & runparams) const
246 return inset.latex(buf, os, runparams);
250 int InsetCollapsable::ascii(Buffer const & buf, ostream & os, int ll) const
252 return inset.ascii(buf, os, ll);
256 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os) const
258 return inset.linuxdoc(buf, os);
262 int InsetCollapsable::docbook(Buffer const & buf, ostream & os, bool mixcont) const
264 return inset.docbook(buf, os, mixcont);
268 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
270 return button_dim.contains(cmd.x, cmd.y);
274 void InsetCollapsable::edit(BufferView * bv, int index)
276 lyxerr << "InsetCollapsable: edit" << endl;
277 if (!bv->lockInset(this))
278 lyxerr << "InsetCollapsable: can't lock index " << index << endl;
279 inset.localDispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
280 first_after_edit = true;
284 dispatch_result InsetCollapsable::localDispatch(FuncRequest const & cmd)
286 //lyxerr << "InsetCollapsable::localDispatch: "
287 // << cmd.action << " '" << cmd.argument << "'\n";
288 BufferView * bv = cmd.view();
289 switch (cmd.action) {
290 case LFUN_INSET_EDIT: {
291 if (!cmd.argument.empty()) {
292 UpdatableInset::localDispatch(cmd);
294 lyxerr << "branch collapsed_" << endl;
296 if (bv->lockInset(this)) {
297 bv->updateInset(this);
298 bv->buffer()->markDirty();
299 inset.localDispatch(cmd);
300 first_after_edit = true;
303 lyxerr << "branch not collapsed_" << endl;
304 if (bv->lockInset(this))
305 inset.localDispatch(cmd);
311 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
313 if (cmd.button() == mouse_button::button3)
316 UpdatableInset::localDispatch(cmd);
320 // set this only here as it should be recollapsed only if
321 // it was already collapsed!
322 first_after_edit = true;
323 if (!bv->lockInset(this))
325 bv->updateInset(this);
326 bv->buffer()->markDirty();
327 inset.localDispatch(cmd);
329 if (!bv->lockInset(this))
331 if (cmd.y <= button_dim.y2) {
332 FuncRequest cmd1 = cmd;
334 inset.localDispatch(cmd1);
336 inset.localDispatch(adjustCommand(cmd));
341 case LFUN_MOUSE_PRESS:
342 if (!collapsed_ && cmd.y > button_dim.y2)
343 inset.localDispatch(adjustCommand(cmd));
346 case LFUN_MOUSE_MOTION:
347 if (!collapsed_ && cmd.y > button_dim.y2)
348 inset.localDispatch(adjustCommand(cmd));
351 case LFUN_MOUSE_RELEASE:
352 lfunMouseRelease(cmd);
356 dispatch_result result = inset.localDispatch(cmd);
357 if (result >= FINISHED)
358 bv->unlockInset(this);
359 first_after_edit = false;
365 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
369 return inset.lockInsetInInset(bv, in);
373 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
377 bv->unlockInset(this);
380 return inset.unlockInsetInInset(bv, in, lr);
384 int InsetCollapsable::insetInInsetY() const
386 return inset.insetInInsetY() - (top_baseline - inset.y());
390 void InsetCollapsable::validate(LaTeXFeatures & features) const
392 inset.validate(features);
396 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
398 inset.getCursor(bv, x, y);
402 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
404 inset.getCursorPos(bv, x , y);
408 UpdatableInset * InsetCollapsable::getLockingInset() const
410 UpdatableInset * in = inset.getLockingInset();
412 return const_cast<InsetCollapsable *>(this);
417 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
421 return inset.getFirstLockingInsetOfType(c);
425 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
426 bool toggleall, bool selectall)
428 inset.setFont(bv, font, toggleall, selectall);
432 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
433 bool const recursive) const
435 return inset.getLyXText(bv, recursive);
439 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
441 inset.deleteLyXText(bv, recursive);
445 void InsetCollapsable::getLabelList(Buffer const & buffer,
446 std::vector<string> & list) const
448 inset.getLabelList(buffer, list);
452 int InsetCollapsable::scroll(bool recursive) const
454 int sx = UpdatableInset::scroll(false);
457 sx += inset.scroll(recursive);
463 ParagraphList * InsetCollapsable::getParagraphs(int i) const
465 return inset.getParagraphs(i);
469 LyXText * InsetCollapsable::getText(int i) const
471 return inset.getText(i);
475 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
477 return inset.cursor(bv);
481 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
484 return const_cast<InsetCollapsable *>(this);
485 return inset.getInsetFromID(id_arg);
489 void InsetCollapsable::open(BufferView * bv)
495 bv->updateInset(this);
499 void InsetCollapsable::close(BufferView * bv) const
505 bv->updateInset(this);
509 void InsetCollapsable::setLabel(string const & l) const
515 void InsetCollapsable::setCollapsed(bool c) const
521 void InsetCollapsable::markErased()
527 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
529 bool found = inset.nextChange(bv, length);
531 if (first_after_edit && !found)
534 first_after_edit = false;
539 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
542 bool found = inset.searchForward(bv, str, cs, mw);
543 if (first_after_edit && !found)
546 first_after_edit = false;
551 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
554 bool found = inset.searchBackward(bv, str, cs, mw);
555 if (first_after_edit && !found)
558 first_after_edit = false;
564 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
566 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
567 if (first_after_edit && word.word().empty())
569 first_after_edit = false;
574 void InsetCollapsable::addPreview(PreviewLoader & loader) const
576 inset.addPreview(loader);