1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright 1998-2001 The LyX Team.
8 * ======================================================
14 #pragma implementation
17 #include "insetcollapsable.h"
20 #include "BufferView.h"
22 #include "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
36 InsetCollapsable::InsetCollapsable(bool collapsed)
37 : UpdatableInset(), collapsed_(collapsed),
38 button_length(0), button_top_y(0), button_bottom_y(0),
46 oldWidth(0), need_update(FULL),
49 , change_label_with_text(false)
53 inset.setAutoBreakRows(true);
54 inset.setDrawFrame(0, InsetText::ALWAYS);
55 inset.setFrameColor(0, LColor::collapsableframe);
56 setInsetName("Collapsable");
60 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
61 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
62 framecolor(in.framecolor), labelfont(in.labelfont),
63 button_length(0), button_top_y(0), button_bottom_y(0),
69 autocollapse(in.autocollapse),
71 oldWidth(0), need_update(FULL),
74 , change_label_with_text(in.change_label_with_text)
77 inset.init(&(in.inset), same_id);
82 Inset * InsetCollapsable::clone(Buffer const &, bool same_id) const
84 return new InsetCollapsable(*const_cast<InsetCollapsable *>(this),
89 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
91 if (!insetAllowed(in->lyxCode())) {
92 lyxerr << "InsetCollapsable::InsertInset: "
93 "Unable to insert inset." << endl;
96 return inset.insertInset(bv, in);
100 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
102 os << "collapsed " << tostr(collapsed_) << "\n";
103 inset.writeParagraphData(buf, os);
108 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
112 string const token = lex.GetString();
113 if (token == "collapsed") {
115 collapsed_ = lex.GetBool();
117 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
121 inset.read(buf, lex);
123 if (collapsed_ && change_label_with_text) {
124 draw_label = get_new_label();
132 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
133 int InsetCollapsable::ascent_collapsed() const
139 pain.buttonText(0, 0, draw_label, labelfont, false,
140 width, ascent, descent);
142 lyxfont::buttonText(label, labelfont, width, ascent, descent);
148 //int InsetCollapsable::descent_collapsed(Painter & pain) const
149 int InsetCollapsable::descent_collapsed() const
155 pain.buttonText(0, 0, draw_label, labelfont, false,
156 width, ascent, descent);
158 lyxfont::buttonText(label, labelfont, width, ascent, descent);
164 //int InsetCollapsable::width_collapsed(Painter & pain) const
165 int InsetCollapsable::width_collapsed() const
171 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
172 width, ascent, descent);
174 lyxfont::buttonText(label, labelfont, width, ascent, descent);
176 return width + (2*TEXT_TO_INSET_OFFSET);
180 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
182 return ascent_collapsed();
186 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
189 return descent_collapsed();
191 return descent_collapsed()
192 + inset.descent(bv, font)
193 + inset.ascent(bv, font)
194 + TEXT_TO_BOTTOM_OFFSET;
198 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
201 return width_collapsed();
203 int widthCollapsed = width_collapsed();
205 return (inset.width(bv, font) > widthCollapsed) ?
206 inset.width(bv, font) : widthCollapsed;
210 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
214 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
215 baseline, draw_label, labelfont, true, width);
216 x += width + TEXT_TO_INSET_OFFSET;
218 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
219 baseline, label, labelfont);
220 x += width_collapsed();
225 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
226 int baseline, float & x, bool cleared) const
231 Painter & pain = bv->painter();
233 button_length = width_collapsed();
234 button_top_y = -ascent(bv, f);
235 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
239 draw_collapsed(pain, baseline, x);
240 x += TEXT_TO_INSET_OFFSET;
247 UpdatableInset::draw(bv, f, baseline, x, cleared);
250 x += static_cast<float>(scroll());
252 if (!cleared && (inset.need_update == InsetText::FULL ||
253 inset.need_update == InsetText::INIT ||
255 top_baseline != baseline))
258 // we don't need anymore to clear here we just have to tell
259 // the underlying LyXText that it should do the RowClear!
260 inset.setUpdateStatus(bv, InsetText::FULL);
261 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
264 int w = owner() ? width(bv, f) : pain.paperWidth();
265 int h = ascent(bv, f) + descent(bv, f);
266 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
267 int const ty = max(0, baseline - ascent(bv, f));
269 if ((ty + h) > pain.paperHeight())
270 h = pain.paperHeight();
271 if ((top_x + w) > pain.paperWidth())
272 w = pain.paperWidth();
274 h += (baseline - ascent(bv, f));
275 pain.fillRectangle(tx, ty - 1, w, h + 2);
281 top_baseline = baseline;
283 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
285 draw_collapsed(pain, bl, old_x);
287 bl + descent_collapsed() + inset.ascent(bv, f),
293 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
296 UpdatableInset::edit(bv, xp, yp, button);
303 if (!bv->lockInset(this))
305 bv->updateInset(this, false);
308 if (!bv->lockInset(this))
310 if (yp <= button_bottom_y) {
313 LyXFont font(LyXFont::ALL_SANE);
314 int yy = ascent(bv, font) + yp -
315 (ascent_collapsed() +
316 descent_collapsed() +
317 inset.ascent(bv, font));
318 inset.edit(bv, xp, yy, button);
324 void InsetCollapsable::edit(BufferView * bv, bool front)
326 UpdatableInset::edit(bv, front);
333 if (!bv->lockInset(this))
335 inset.setUpdateStatus(bv, InsetText::FULL);
336 bv->updateInset(this, false);
337 inset.edit(bv, front);
339 if (!bv->lockInset(this))
341 inset.edit(bv, front);
346 Inset::EDITABLE InsetCollapsable::editable() const
350 return HIGHLY_EDITABLE;
354 void InsetCollapsable::insetUnlock(BufferView * bv)
358 if (change_label_with_text) {
359 draw_label = get_new_label();
366 inset.insetUnlock(bv);
369 bv->updateInset(this, false);
373 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
376 if (!collapsed_ && (y > button_bottom_y)) {
377 LyXFont font(LyXFont::ALL_SANE);
378 int yy = ascent(bv, font) + y -
379 (ascent_collapsed() +
380 descent_collapsed() +
381 inset.ascent(bv, font));
382 inset.insetButtonPress(bv, x, yy, button);
387 void InsetCollapsable::insetButtonRelease(BufferView * bv,
388 int x, int y, int button)
390 if ((x >= 0) && (x < button_length) &&
391 (y >= button_top_y) && (y <= button_bottom_y)) {
397 inset.insetButtonRelease(bv, 0, 0, button);
398 inset.setUpdateStatus(bv, InsetText::FULL);
399 bv->updateInset(this, false);
402 if (change_label_with_text) {
403 draw_label = get_new_label();
409 bv->unlockInset(this);
410 bv->updateInset(this, false);
412 } else if (!collapsed_ && (y > button_bottom_y)) {
413 LyXFont font(LyXFont::ALL_SANE);
414 int yy = ascent(bv, font) + y -
415 (ascent_collapsed() +
416 descent_collapsed() +
417 inset.ascent(bv, font));
418 inset.insetButtonRelease(bv, x, yy, button);
423 void InsetCollapsable::insetMotionNotify(BufferView * bv,
424 int x, int y, int state)
426 if (y > button_bottom_y) {
427 LyXFont font(LyXFont::ALL_SANE);
428 int yy = ascent(bv, font) + y -
429 (ascent_collapsed() +
430 descent_collapsed() +
431 inset.ascent(bv, font));
432 inset.insetMotionNotify(bv, x, yy, state);
437 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
439 inset.insetKeyPress(xke);
443 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
444 bool fragile, bool free_spc) const
446 return inset.latex(buf, os, fragile, free_spc);
450 int InsetCollapsable::getMaxWidth(BufferView * bv,
451 UpdatableInset const * inset) const
453 int const w = UpdatableInset::getMaxWidth(bv, inset);
456 // What does a negative max width signify? (Lgb)
457 // Use the max width of the draw-area (Jug)
460 // should be at least 30 pixels !!!
461 return max(30, w - width_collapsed());
465 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
468 inset.update(bv, font, reinit);
472 UpdatableInset::RESULT
473 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
476 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
477 if (result == FINISHED)
478 bv->unlockInset(this);
483 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
487 return inset.lockInsetInInset(bv, in);
491 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
495 bv->unlockInset(this);
498 return inset.unlockInsetInInset(bv, in, lr);
502 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
506 return inset.updateInsetInInset(bv, in);
510 unsigned int InsetCollapsable::insetInInsetY()
512 return inset.insetInInsetY() - (top_baseline - inset.y());
516 void InsetCollapsable::validate(LaTeXFeatures & features) const
518 inset.validate(features);
522 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
524 inset.getCursorPos(bv, x , y);
528 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
530 inset.toggleInsetCursor(bv);
534 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
536 inset.showInsetCursor(bv, show);
540 void InsetCollapsable::hideInsetCursor(BufferView * bv)
542 inset.hideInsetCursor(bv);
546 UpdatableInset * InsetCollapsable::getLockingInset() const
548 UpdatableInset * in = inset.getLockingInset();
549 if (const_cast<InsetText *>(&inset) == in)
550 return const_cast<InsetCollapsable *>(this);
555 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
559 return inset.getFirstLockingInsetOfType(c);
563 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
564 bool toggleall, bool selectall)
566 inset.setFont(bv, font, toggleall, selectall);
570 bool InsetCollapsable::doClearArea() const
572 return inset.doClearArea();
576 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
577 bool const recursive) const
579 return inset.getLyXText(bv, recursive);
583 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
585 inset.deleteLyXText(bv, recursive);
589 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
591 inset.resizeLyXText(bv, force);
592 LyXFont font(LyXFont::ALL_SANE);
593 oldWidth = width(bv, font);
597 std::vector<string> const InsetCollapsable::getLabelList() const
599 return inset.getLabelList();
603 bool InsetCollapsable::nodraw() const
605 return inset.nodraw();
609 int InsetCollapsable::scroll(bool recursive) const
611 int sx = UpdatableInset::scroll(false);
614 sx += inset.scroll(recursive);
620 Paragraph * InsetCollapsable::getParFromID(int id) const
622 return inset.getParFromID(id);
626 Paragraph * InsetCollapsable::firstParagraph() const
628 return inset.firstParagraph();
632 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
634 return inset.cursor(bv);
638 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
641 return const_cast<InsetCollapsable *>(this);
642 return inset.getInsetFromID(id_arg);
647 void InsetCollapsable::open(BufferView * bv, bool flag)
649 if (flag == !collapsed_)
653 if (collapsed_ && change_label_with_text) {
654 draw_label = get_new_label();
659 bv->updateInset(this, false);
662 void InsetCollapsable::open(BufferView * bv)
664 if (!collapsed_) return;
667 bv->updateInset(this, false);
671 void InsetCollapsable::close(BufferView * bv)
673 if (collapsed_) return;
676 bv->updateInset(this, false);
682 void InsetCollapsable::setLabel(string const & l, bool flag)
685 change_label_with_text = flag;
686 if (collapsed_ && change_label_with_text) {
687 draw_label = get_new_label();
693 void InsetCollapsable::setLabel(string const & l)
701 string const InsetCollapsable::get_new_label() const
704 Paragraph::size_type const max_length = 15;
706 int n = std::min(max_length, inset.paragraph()->size());
709 for(; i < n && j < inset.paragraph()->size(); ++j) {
710 if (inset.paragraph()->isInset(j))
712 la += inset.paragraph()->getChar(j);
715 if ((i > 0) && (j < inset.paragraph()->size()))