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
16 #include "insetcollapsable.h"
17 #include "insettext.h"
19 #include "BufferView.h"
25 #include "WordLangTuple.h"
26 #include "funcrequest.h"
29 #include "frontends/font_metrics.h"
30 #include "frontends/Painter.h"
31 #include "frontends/LyXView.h"
33 #include "support/LAssert.h"
34 #include "support/LOstream.h"
35 #include "support/lstrings.h"
46 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
47 : UpdatableInset(), collapsed_(collapsed), inset(bp),
48 button_length(0), button_top_y(0), button_bottom_y(0),
53 oldWidth(0), in_update(false), first_after_edit(false)
56 inset.setAutoBreakRows(true);
57 inset.setDrawFrame(0, InsetText::ALWAYS);
58 inset.setFrameColor(0, LColor::collapsableframe);
59 setInsetName("Collapsable");
63 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
64 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
65 framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
66 button_length(0), button_top_y(0), button_bottom_y(0),
69 autocollapse(in.autocollapse),
71 oldWidth(0), in_update(false), first_after_edit(false)
73 inset.init(&(in.inset), same_id);
78 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
80 if (!insetAllowed(in->lyxCode())) {
81 lyxerr << "InsetCollapsable::InsertInset: "
82 "Unable to insert inset." << endl;
85 return inset.insertInset(bv, in);
89 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
91 os << "collapsed " << tostr(collapsed_) << "\n";
92 inset.writeParagraphData(buf, os);
97 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
101 string const token = lex.getString();
102 if (token == "collapsed") {
104 collapsed_ = lex.getBool();
106 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
108 // Take countermeasures
109 lex.pushToken(token);
112 inset.read(buf, lex);
116 int InsetCollapsable::ascent_collapsed() const
121 font_metrics::buttonText(label, labelfont, width, ascent, descent);
126 int InsetCollapsable::descent_collapsed() const
131 font_metrics::buttonText(label, labelfont, width, ascent, descent);
136 //int InsetCollapsable::width_collapsed(Painter & pain) const
137 int InsetCollapsable::width_collapsed() const
142 font_metrics::buttonText(label, labelfont, width, ascent, descent);
143 return width + 2 * TEXT_TO_INSET_OFFSET;
147 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
149 return ascent_collapsed();
153 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
156 return descent_collapsed();
158 return descent_collapsed()
159 + inset.descent(bv, font)
160 + inset.ascent(bv, font)
161 + TEXT_TO_BOTTOM_OFFSET;
165 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
168 return width_collapsed();
170 int const collapsed_width = width_collapsed();
171 int const contents_width = inset.width(bv, font);
173 return max(collapsed_width, contents_width);
177 void InsetCollapsable::draw_collapsed(Painter & pain,
178 int baseline, float & x) const
180 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
181 baseline, label, labelfont);
182 x += width_collapsed();
186 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
187 int baseline, float & x, bool inlined) const
195 Painter & pain = bv->painter();
197 button_length = width_collapsed();
198 button_top_y = -ascent(bv, f);
199 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
203 draw_collapsed(pain, baseline, x);
213 top_baseline = baseline;
215 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
218 inset.draw(bv, f, baseline, x);
220 draw_collapsed(pain, bl, old_x);
221 inset.draw(bv, f, bl + descent_collapsed() + inset.ascent(bv, f), x);
222 // contained inset may be shorter than the button
223 if (x < (top_x + button_length + TEXT_TO_INSET_OFFSET))
224 x = top_x + button_length + TEXT_TO_INSET_OFFSET;
229 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
230 int baseline, float & x) const
232 // by default, we are not inlined-drawing
233 draw(bv, f, baseline, x, false);
237 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
238 mouse_button::state button)
241 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
243 if (button == mouse_button::button3)
246 UpdatableInset::edit(bv, xp, yp, button);
250 // set this only here as it should be recollapsed only if
251 // it was already collapsed!
252 first_after_edit = true;
253 if (!bv->lockInset(this))
255 bv->updateInset(this);
256 bv->buffer()->markDirty();
259 if (!bv->lockInset(this))
261 if (yp <= button_bottom_y) {
262 inset.edit(bv, xp, 0, button);
264 LyXFont font(LyXFont::ALL_SANE);
265 int yy = ascent(bv, font) + yp -
266 (ascent_collapsed() +
267 descent_collapsed() +
268 inset.ascent(bv, font));
269 inset.edit(bv, xp, yy, button);
275 void InsetCollapsable::edit(BufferView * bv, bool front)
277 UpdatableInset::edit(bv, front);
281 if (!bv->lockInset(this))
283 inset.setUpdateStatus(bv, InsetText::FULL);
284 bv->updateInset(this);
285 bv->buffer()->markDirty();
286 inset.edit(bv, front);
287 first_after_edit = true;
289 if (!bv->lockInset(this))
291 inset.edit(bv, front);
296 Inset::EDITABLE InsetCollapsable::editable() const
300 return HIGHLY_EDITABLE;
304 void InsetCollapsable::insetUnlock(BufferView * bv)
308 if (change_label_with_text) {
309 draw_label = get_new_label();
316 inset.insetUnlock(bv);
319 bv->updateInset(this);
323 void InsetCollapsable::lfunMousePress(FuncRequest const & cmd)
325 if (!collapsed_ && (cmd.y > button_bottom_y)) {
326 LyXFont font(LyXFont::ALL_SANE);
327 FuncRequest cmd1 = cmd;
328 cmd1.y = ascent(cmd.view(), font) + cmd.y -
329 (ascent_collapsed() +
330 descent_collapsed() +
331 inset.ascent(cmd.view(), font));
332 inset.localDispatch(cmd1);
337 bool InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
340 BufferView * bv = cmd.view();
341 if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
342 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
346 // should not be called on inset open!
347 // inset.insetButtonRelease(bv, 0, 0, button);
348 inset.setUpdateStatus(bv, InsetText::FULL);
349 bv->updateInset(this);
350 bv->buffer()->markDirty();
353 bv->unlockInset(this);
354 bv->updateInset(this);
355 bv->buffer()->markDirty();
357 } else if (!collapsed_ && (cmd.y > button_bottom_y)) {
358 LyXFont font(LyXFont::ALL_SANE);
359 FuncRequest cmd1 = cmd;
360 cmd1.y = ascent(cmd.view(), font) + cmd.y -
361 (ascent_collapsed() +
362 descent_collapsed() +
363 inset.ascent(cmd.view(), font));
364 ret = (inset.localDispatch(cmd1) == DISPATCHED);
366 if (cmd.button() == mouse_button::button3 && !ret)
367 return showInsetDialog(bv);
372 void InsetCollapsable::lfunMouseMotion(FuncRequest const & cmd)
374 if (cmd.y > button_bottom_y) {
375 LyXFont font(LyXFont::ALL_SANE);
376 FuncRequest cmd1 = cmd;
377 cmd1.y = ascent(cmd.view(), font) + cmd.y -
378 (ascent_collapsed() +
379 descent_collapsed() +
380 inset.ascent(cmd.view(), font));
381 inset.localDispatch(cmd1);
386 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
387 bool fragile, bool free_spc) const
389 return inset.latex(buf, os, fragile, free_spc);
393 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
395 return inset.ascii(buf, os, ll);
399 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
401 return inset.linuxdoc(buf, os);
405 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
407 return inset.docbook(buf, os, mixcont);
411 void InsetCollapsable::update(BufferView * bv, bool reinit)
414 if (reinit && owner()) {
415 owner()->update(bv, true);
420 inset.update(bv, reinit);
421 if (reinit && owner()) {
422 owner()->update(bv, true);
428 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
430 switch (cmd.action) {
432 case LFUN_MOUSE_PRESS:
436 case LFUN_MOUSE_MOTION:
437 lfunMouseMotion(cmd);
440 case LFUN_MOUSE_RELEASE:
441 lfunMouseRelease(cmd);
445 UpdatableInset::RESULT result = inset.localDispatch(cmd);
446 if (result >= FINISHED)
447 cmd.view()->unlockInset(this);
448 first_after_edit = false;
455 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
459 return inset.lockInsetInInset(bv, in);
463 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
467 bv->unlockInset(this);
470 return inset.unlockInsetInInset(bv, in, lr);
474 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
478 return inset.updateInsetInInset(bv, in);
482 int InsetCollapsable::insetInInsetY() const
484 return inset.insetInInsetY() - (top_baseline - inset.y());
488 void InsetCollapsable::validate(LaTeXFeatures & features) const
490 inset.validate(features);
494 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
496 inset.getCursorPos(bv, x , y);
500 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
502 inset.toggleInsetCursor(bv);
506 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
508 inset.showInsetCursor(bv, show);
512 void InsetCollapsable::hideInsetCursor(BufferView * bv)
514 inset.hideInsetCursor(bv);
518 UpdatableInset * InsetCollapsable::getLockingInset() const
520 UpdatableInset * in = inset.getLockingInset();
521 if (const_cast<InsetText *>(&inset) == in)
522 return const_cast<InsetCollapsable *>(this);
527 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
531 return inset.getFirstLockingInsetOfType(c);
535 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
536 bool toggleall, bool selectall)
538 inset.setFont(bv, font, toggleall, selectall);
542 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
543 bool const recursive) const
545 return inset.getLyXText(bv, recursive);
549 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
551 inset.deleteLyXText(bv, recursive);
555 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
557 inset.resizeLyXText(bv, force);
558 LyXFont font(LyXFont::ALL_SANE);
559 oldWidth = width(bv, font);
563 vector<string> const InsetCollapsable::getLabelList() const
565 return inset.getLabelList();
569 bool InsetCollapsable::nodraw() const
571 return inset.nodraw();
575 int InsetCollapsable::scroll(bool recursive) const
577 int sx = UpdatableInset::scroll(false);
580 sx += inset.scroll(recursive);
586 Paragraph * InsetCollapsable::firstParagraph() const
588 return inset.firstParagraph();
592 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
594 return inset.getFirstParagraph(i);
598 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
600 return inset.cursor(bv);
604 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
607 return const_cast<InsetCollapsable *>(this);
608 return inset.getInsetFromID(id_arg);
612 void InsetCollapsable::open(BufferView * bv)
614 if (!collapsed_) return;
617 bv->updateInset(this);
621 void InsetCollapsable::close(BufferView * bv) const
627 bv->updateInset(const_cast<InsetCollapsable *>(this));
631 void InsetCollapsable::setLabel(string const & l) const
637 void InsetCollapsable::markErased()
643 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
645 bool found = inset.nextChange(bv, length);
647 if (first_after_edit && !found)
650 first_after_edit = false;
655 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
658 bool found = inset.searchForward(bv, str, cs, mw);
659 if (first_after_edit && !found)
662 first_after_edit = false;
667 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
670 bool found = inset.searchBackward(bv, str, cs, mw);
671 if (first_after_edit && !found)
674 first_after_edit = false;
680 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
682 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
683 if (first_after_edit && word.word().empty())
685 first_after_edit = false;
690 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
692 inset.addPreview(loader);
696 void InsetCollapsable::cache(BufferView * bv) const
698 view_ = bv->owner()->view();
702 BufferView * InsetCollapsable::view() const
704 return view_.lock().get();