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),
49 need_update(NONE), label("Label"),
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),
67 need_update(NONE), label(in.label),
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 widthCollapsed = width_collapsed();
172 return (inset.width(bv, font) > widthCollapsed) ?
173 inset.width(bv, font) : widthCollapsed;
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) const
192 if (need_update != NONE) {
193 const_cast<InsetText *>(&inset)->update(bv, f, true);
194 bv->text->postChangedInDraw();
201 Painter & pain = bv->painter();
203 button_length = width_collapsed();
204 button_top_y = -ascent(bv, f);
205 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
209 draw_collapsed(pain, baseline, x);
216 x += static_cast<float>(scroll());
220 top_baseline = baseline;
222 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
224 draw_collapsed(pain, bl, old_x);
225 inset.draw(bv, f, bl + descent_collapsed() + inset.ascent(bv, f), x);
226 if (x < (top_x + button_length + TEXT_TO_INSET_OFFSET))
227 x = top_x + button_length + TEXT_TO_INSET_OFFSET;
231 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
232 mouse_button::state button)
235 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
237 if (button == mouse_button::button3)
240 UpdatableInset::edit(bv, xp, yp, button);
244 // set this only here as it should be recollapsed only if
245 // it was already collapsed!
246 first_after_edit = true;
247 if (!bv->lockInset(this))
249 bv->updateInset(this);
250 bv->buffer()->markDirty();
253 if (!bv->lockInset(this))
255 if (yp <= button_bottom_y) {
256 inset.edit(bv, xp, 0, button);
258 LyXFont font(LyXFont::ALL_SANE);
259 int yy = ascent(bv, font) + yp -
260 (ascent_collapsed() +
261 descent_collapsed() +
262 inset.ascent(bv, font));
263 inset.edit(bv, xp, yy, button);
269 void InsetCollapsable::edit(BufferView * bv, bool front)
271 UpdatableInset::edit(bv, front);
275 if (!bv->lockInset(this))
277 inset.setUpdateStatus(bv, InsetText::FULL);
278 bv->updateInset(this);
279 bv->buffer()->markDirty();
280 inset.edit(bv, front);
281 first_after_edit = true;
283 if (!bv->lockInset(this))
285 inset.edit(bv, front);
290 Inset::EDITABLE InsetCollapsable::editable() const
294 return HIGHLY_EDITABLE;
298 void InsetCollapsable::insetUnlock(BufferView * bv)
302 if (change_label_with_text) {
303 draw_label = get_new_label();
310 inset.insetUnlock(bv);
313 bv->updateInset(this);
317 void InsetCollapsable::lfunMousePress(FuncRequest const & cmd)
319 if (!collapsed_ && (cmd.y > button_bottom_y)) {
320 LyXFont font(LyXFont::ALL_SANE);
321 FuncRequest cmd1 = cmd;
322 cmd1.y = ascent(cmd.view(), font) + cmd.y -
323 (ascent_collapsed() +
324 descent_collapsed() +
325 inset.ascent(cmd.view(), font));
326 inset.localDispatch(cmd1);
331 bool InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
334 BufferView * bv = cmd.view();
335 if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
336 (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
340 // should not be called on inset open!
341 // inset.insetButtonRelease(bv, 0, 0, button);
342 inset.setUpdateStatus(bv, InsetText::FULL);
343 bv->updateInset(this);
344 bv->buffer()->markDirty();
347 bv->unlockInset(this);
348 bv->updateInset(this);
349 bv->buffer()->markDirty();
351 } else if (!collapsed_ && (cmd.y > button_bottom_y)) {
352 LyXFont font(LyXFont::ALL_SANE);
353 FuncRequest cmd1 = cmd;
354 cmd1.y = ascent(cmd.view(), font) + cmd.y -
355 (ascent_collapsed() +
356 descent_collapsed() +
357 inset.ascent(cmd.view(), font));
358 ret = (inset.localDispatch(cmd1) == DISPATCHED);
360 if (cmd.button() == mouse_button::button3 && !ret)
361 return showInsetDialog(bv);
366 void InsetCollapsable::lfunMouseMotion(FuncRequest const & cmd)
368 if (cmd.y > button_bottom_y) {
369 LyXFont font(LyXFont::ALL_SANE);
370 FuncRequest cmd1 = cmd;
371 cmd1.y = ascent(cmd.view(), font) + cmd.y -
372 (ascent_collapsed() +
373 descent_collapsed() +
374 inset.ascent(cmd.view(), font));
375 inset.localDispatch(cmd1);
380 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
381 bool fragile, bool free_spc) const
383 return inset.latex(buf, os, fragile, free_spc);
387 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
389 return inset.ascii(buf, os, ll);
393 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
395 return inset.linuxdoc(buf, os);
399 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
401 return inset.docbook(buf, os, mixcont);
405 int InsetCollapsable::getMaxWidth(BufferView * bv,
406 UpdatableInset const * in) const
409 int const w = UpdatableInset::getMaxWidth(bv, in);
412 // What does a negative max width signify? (Lgb)
413 // Use the max width of the draw-area (Jug)
416 // should be at least 30 pixels !!!
417 return max(30, w - width_collapsed());
419 return UpdatableInset::getMaxWidth(bv, in);
425 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
429 if (reinit && owner()) {
430 owner()->update(bv, font, true);
435 inset.update(bv, font, reinit);
436 if (reinit && owner()) {
437 owner()->update(bv, font, true);
443 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
445 switch (cmd.action) {
447 case LFUN_MOUSE_PRESS:
451 case LFUN_MOUSE_MOTION:
452 lfunMouseMotion(cmd);
455 case LFUN_MOUSE_RELEASE:
456 lfunMouseRelease(cmd);
460 UpdatableInset::RESULT result = inset.localDispatch(cmd);
461 if (result >= FINISHED)
462 cmd.view()->unlockInset(this);
463 first_after_edit = false;
470 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
474 return inset.lockInsetInInset(bv, in);
478 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
482 bv->unlockInset(this);
485 return inset.unlockInsetInInset(bv, in, lr);
489 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
493 return inset.updateInsetInInset(bv, in);
497 int InsetCollapsable::insetInInsetY() const
499 return inset.insetInInsetY() - (top_baseline - inset.y());
503 void InsetCollapsable::validate(LaTeXFeatures & features) const
505 inset.validate(features);
509 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
511 inset.getCursorPos(bv, x , y);
515 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
517 inset.toggleInsetCursor(bv);
521 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
523 inset.showInsetCursor(bv, show);
527 void InsetCollapsable::hideInsetCursor(BufferView * bv)
529 inset.hideInsetCursor(bv);
533 UpdatableInset * InsetCollapsable::getLockingInset() const
535 UpdatableInset * in = inset.getLockingInset();
536 if (const_cast<InsetText *>(&inset) == in)
537 return const_cast<InsetCollapsable *>(this);
542 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
546 return inset.getFirstLockingInsetOfType(c);
550 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
551 bool toggleall, bool selectall)
553 inset.setFont(bv, font, toggleall, selectall);
557 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
558 bool const recursive) const
560 return inset.getLyXText(bv, recursive);
564 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
566 inset.deleteLyXText(bv, recursive);
570 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
572 inset.resizeLyXText(bv, force);
573 LyXFont font(LyXFont::ALL_SANE);
574 oldWidth = width(bv, font);
578 vector<string> const InsetCollapsable::getLabelList() const
580 return inset.getLabelList();
584 bool InsetCollapsable::nodraw() const
586 return inset.nodraw();
590 int InsetCollapsable::scroll(bool recursive) const
592 int sx = UpdatableInset::scroll(false);
595 sx += inset.scroll(recursive);
601 Paragraph * InsetCollapsable::firstParagraph() const
603 return inset.firstParagraph();
607 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
609 return inset.getFirstParagraph(i);
613 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
615 return inset.cursor(bv);
619 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
622 return const_cast<InsetCollapsable *>(this);
623 return inset.getInsetFromID(id_arg);
627 void InsetCollapsable::open(BufferView * bv)
629 if (!collapsed_) return;
632 bv->updateInset(this);
636 void InsetCollapsable::close(BufferView * bv) const
642 bv->updateInset(const_cast<InsetCollapsable *>(this));
646 void InsetCollapsable::setLabel(string const & l) const
652 void InsetCollapsable::markErased()
658 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
660 bool found = inset.nextChange(bv, length);
662 if (first_after_edit && !found)
665 first_after_edit = false;
670 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
673 bool found = inset.searchForward(bv, str, cs, mw);
674 if (first_after_edit && !found)
677 first_after_edit = false;
682 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
685 bool found = inset.searchBackward(bv, str, cs, mw);
686 if (first_after_edit && !found)
689 first_after_edit = false;
695 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
697 WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
698 if (first_after_edit && word.word().empty())
700 first_after_edit = false;
705 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
707 inset.addPreview(loader);
711 void InsetCollapsable::cache(BufferView * bv) const
713 view_ = bv->owner()->view();
717 BufferView * InsetCollapsable::view() const
719 return view_.lock().get();