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!"
119 // Take countermeasures
120 lex.pushToken(token);
123 inset.read(buf, lex);
125 if (collapsed_ && change_label_with_text) {
126 draw_label = get_new_label();
134 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
135 int InsetCollapsable::ascent_collapsed() const
141 pain.buttonText(0, 0, draw_label, labelfont, false,
142 width, ascent, descent);
144 lyxfont::buttonText(label, labelfont, width, ascent, descent);
150 //int InsetCollapsable::descent_collapsed(Painter & pain) const
151 int InsetCollapsable::descent_collapsed() const
157 pain.buttonText(0, 0, draw_label, labelfont, false,
158 width, ascent, descent);
160 lyxfont::buttonText(label, labelfont, width, ascent, descent);
166 //int InsetCollapsable::width_collapsed(Painter & pain) const
167 int InsetCollapsable::width_collapsed() const
173 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
174 width, ascent, descent);
176 lyxfont::buttonText(label, labelfont, width, ascent, descent);
178 return width + (2*TEXT_TO_INSET_OFFSET);
182 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
184 return ascent_collapsed();
188 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
191 return descent_collapsed();
193 return descent_collapsed()
194 + inset.descent(bv, font)
195 + inset.ascent(bv, font)
196 + TEXT_TO_BOTTOM_OFFSET;
200 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
203 return width_collapsed();
205 int widthCollapsed = width_collapsed();
207 return (inset.width(bv, font) > widthCollapsed) ?
208 inset.width(bv, font) : widthCollapsed;
212 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
216 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
217 baseline, draw_label, labelfont, true, width);
218 x += width + TEXT_TO_INSET_OFFSET;
220 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
221 baseline, label, labelfont);
222 x += width_collapsed();
227 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
228 int baseline, float & x, bool cleared) const
233 Painter & pain = bv->painter();
235 button_length = width_collapsed();
236 button_top_y = -ascent(bv, f);
237 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
241 draw_collapsed(pain, baseline, x);
242 x += TEXT_TO_INSET_OFFSET;
249 UpdatableInset::draw(bv, f, baseline, x, cleared);
252 x += static_cast<float>(scroll());
254 if (!cleared && (inset.need_update == InsetText::FULL ||
255 inset.need_update == InsetText::INIT ||
257 top_baseline != baseline))
260 // we don't need anymore to clear here we just have to tell
261 // the underlying LyXText that it should do the RowClear!
262 inset.setUpdateStatus(bv, InsetText::FULL);
263 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
266 int w = owner() ? width(bv, f) : pain.paperWidth();
267 int h = ascent(bv, f) + descent(bv, f);
268 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
269 int const ty = max(0, baseline - ascent(bv, f));
271 if ((ty + h) > pain.paperHeight())
272 h = pain.paperHeight();
273 if ((top_x + w) > pain.paperWidth())
274 w = pain.paperWidth();
276 h += (baseline - ascent(bv, f));
277 pain.fillRectangle(tx, ty - 1, w, h + 2);
283 top_baseline = baseline;
285 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
287 draw_collapsed(pain, bl, old_x);
289 bl + descent_collapsed() + inset.ascent(bv, f),
295 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
298 UpdatableInset::edit(bv, xp, yp, button);
305 if (!bv->lockInset(this))
307 bv->updateInset(this, false);
310 if (!bv->lockInset(this))
312 if (yp <= button_bottom_y) {
315 LyXFont font(LyXFont::ALL_SANE);
316 int yy = ascent(bv, font) + yp -
317 (ascent_collapsed() +
318 descent_collapsed() +
319 inset.ascent(bv, font));
320 inset.edit(bv, xp, yy, button);
326 void InsetCollapsable::edit(BufferView * bv, bool front)
328 UpdatableInset::edit(bv, front);
335 if (!bv->lockInset(this))
337 inset.setUpdateStatus(bv, InsetText::FULL);
338 bv->updateInset(this, false);
339 inset.edit(bv, front);
341 if (!bv->lockInset(this))
343 inset.edit(bv, front);
348 Inset::EDITABLE InsetCollapsable::editable() const
352 return HIGHLY_EDITABLE;
356 void InsetCollapsable::insetUnlock(BufferView * bv)
360 if (change_label_with_text) {
361 draw_label = get_new_label();
368 inset.insetUnlock(bv);
371 bv->updateInset(this, false);
375 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
378 if (!collapsed_ && (y > button_bottom_y)) {
379 LyXFont font(LyXFont::ALL_SANE);
380 int yy = ascent(bv, font) + y -
381 (ascent_collapsed() +
382 descent_collapsed() +
383 inset.ascent(bv, font));
384 inset.insetButtonPress(bv, x, yy, button);
389 void InsetCollapsable::insetButtonRelease(BufferView * bv,
390 int x, int y, int button)
392 if ((x >= 0) && (x < button_length) &&
393 (y >= button_top_y) && (y <= button_bottom_y)) {
399 inset.insetButtonRelease(bv, 0, 0, button);
400 inset.setUpdateStatus(bv, InsetText::FULL);
401 bv->updateInset(this, false);
404 if (change_label_with_text) {
405 draw_label = get_new_label();
411 bv->unlockInset(this);
412 bv->updateInset(this, false);
414 } else if (!collapsed_ && (y > button_bottom_y)) {
415 LyXFont font(LyXFont::ALL_SANE);
416 int yy = ascent(bv, font) + y -
417 (ascent_collapsed() +
418 descent_collapsed() +
419 inset.ascent(bv, font));
420 inset.insetButtonRelease(bv, x, yy, button);
425 void InsetCollapsable::insetMotionNotify(BufferView * bv,
426 int x, int y, int state)
428 if (y > button_bottom_y) {
429 LyXFont font(LyXFont::ALL_SANE);
430 int yy = ascent(bv, font) + y -
431 (ascent_collapsed() +
432 descent_collapsed() +
433 inset.ascent(bv, font));
434 inset.insetMotionNotify(bv, x, yy, state);
439 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
441 inset.insetKeyPress(xke);
445 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
446 bool fragile, bool free_spc) const
448 return inset.latex(buf, os, fragile, free_spc);
452 int InsetCollapsable::getMaxWidth(BufferView * bv,
453 UpdatableInset const * inset) const
455 int const w = UpdatableInset::getMaxWidth(bv, inset);
458 // What does a negative max width signify? (Lgb)
459 // Use the max width of the draw-area (Jug)
462 // should be at least 30 pixels !!!
463 return max(30, w - width_collapsed());
467 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
470 inset.update(bv, font, reinit);
474 UpdatableInset::RESULT
475 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
478 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
479 if (result == FINISHED)
480 bv->unlockInset(this);
485 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
489 return inset.lockInsetInInset(bv, in);
493 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
497 bv->unlockInset(this);
500 return inset.unlockInsetInInset(bv, in, lr);
504 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
508 return inset.updateInsetInInset(bv, in);
512 unsigned int InsetCollapsable::insetInInsetY()
514 return inset.insetInInsetY() - (top_baseline - inset.y());
518 void InsetCollapsable::validate(LaTeXFeatures & features) const
520 inset.validate(features);
524 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
526 inset.getCursorPos(bv, x , y);
530 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
532 inset.toggleInsetCursor(bv);
536 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
538 inset.showInsetCursor(bv, show);
542 void InsetCollapsable::hideInsetCursor(BufferView * bv)
544 inset.hideInsetCursor(bv);
548 UpdatableInset * InsetCollapsable::getLockingInset() const
550 UpdatableInset * in = inset.getLockingInset();
551 if (const_cast<InsetText *>(&inset) == in)
552 return const_cast<InsetCollapsable *>(this);
557 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
561 return inset.getFirstLockingInsetOfType(c);
565 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
566 bool toggleall, bool selectall)
568 inset.setFont(bv, font, toggleall, selectall);
572 bool InsetCollapsable::doClearArea() const
574 return inset.doClearArea();
578 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
579 bool const recursive) const
581 return inset.getLyXText(bv, recursive);
585 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
587 inset.deleteLyXText(bv, recursive);
591 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
593 inset.resizeLyXText(bv, force);
594 LyXFont font(LyXFont::ALL_SANE);
595 oldWidth = width(bv, font);
599 std::vector<string> const InsetCollapsable::getLabelList() const
601 return inset.getLabelList();
605 bool InsetCollapsable::nodraw() const
607 return inset.nodraw();
611 int InsetCollapsable::scroll(bool recursive) const
613 int sx = UpdatableInset::scroll(false);
616 sx += inset.scroll(recursive);
622 Paragraph * InsetCollapsable::getParFromID(int id) const
624 return inset.getParFromID(id);
628 Paragraph * InsetCollapsable::firstParagraph() const
630 return inset.firstParagraph();
634 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
636 return inset.cursor(bv);
640 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
643 return const_cast<InsetCollapsable *>(this);
644 return inset.getInsetFromID(id_arg);
649 void InsetCollapsable::open(BufferView * bv, bool flag)
651 if (flag == !collapsed_)
655 if (collapsed_ && change_label_with_text) {
656 draw_label = get_new_label();
661 bv->updateInset(this, false);
664 void InsetCollapsable::open(BufferView * bv)
666 if (!collapsed_) return;
669 bv->updateInset(this, false);
673 void InsetCollapsable::close(BufferView * bv)
675 if (collapsed_) return;
678 bv->updateInset(this, false);
684 void InsetCollapsable::setLabel(string const & l, bool flag)
687 change_label_with_text = flag;
688 if (collapsed_ && change_label_with_text) {
689 draw_label = get_new_label();
695 void InsetCollapsable::setLabel(string const & l)
703 string const InsetCollapsable::get_new_label() const
706 Paragraph::size_type const max_length = 15;
708 int n = std::min(max_length, inset.paragraph()->size());
711 for(; i < n && j < inset.paragraph()->size(); ++j) {
712 if (inset.paragraph()->isInset(j))
714 la += inset.paragraph()->getChar(j);
717 if ((i > 0) && (j < inset.paragraph()->size()))