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 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
84 if (!insetAllowed(in->lyxCode())) {
85 lyxerr << "InsetCollapsable::InsertInset: "
86 "Unable to insert inset." << endl;
89 return inset.insertInset(bv, in);
93 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
95 os << "collapsed " << tostr(collapsed_) << "\n";
96 inset.writeParagraphData(buf, os);
101 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
105 string const token = lex.GetString();
106 if (token == "collapsed") {
108 collapsed_ = lex.GetBool();
110 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
112 // Take countermeasures
113 lex.pushToken(token);
116 inset.read(buf, lex);
118 if (collapsed_ && change_label_with_text) {
119 draw_label = get_new_label();
127 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
128 int InsetCollapsable::ascent_collapsed() const
134 pain.buttonText(0, 0, draw_label, labelfont, false,
135 width, ascent, descent);
137 lyxfont::buttonText(label, labelfont, width, ascent, descent);
143 //int InsetCollapsable::descent_collapsed(Painter & pain) const
144 int InsetCollapsable::descent_collapsed() const
150 pain.buttonText(0, 0, draw_label, labelfont, false,
151 width, ascent, descent);
153 lyxfont::buttonText(label, labelfont, width, ascent, descent);
159 //int InsetCollapsable::width_collapsed(Painter & pain) const
160 int InsetCollapsable::width_collapsed() const
166 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
167 width, ascent, descent);
169 lyxfont::buttonText(label, labelfont, width, ascent, descent);
171 return width + (2*TEXT_TO_INSET_OFFSET);
175 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
177 return ascent_collapsed();
181 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
184 return descent_collapsed();
186 return descent_collapsed()
187 + inset.descent(bv, font)
188 + inset.ascent(bv, font)
189 + TEXT_TO_BOTTOM_OFFSET;
193 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
196 return width_collapsed();
198 int widthCollapsed = width_collapsed();
200 return (inset.width(bv, font) > widthCollapsed) ?
201 inset.width(bv, font) : widthCollapsed;
205 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
209 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
210 baseline, draw_label, labelfont, true, width);
211 x += width + TEXT_TO_INSET_OFFSET;
213 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
214 baseline, label, labelfont);
215 x += width_collapsed();
220 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
221 int baseline, float & x, bool cleared) const
226 Painter & pain = bv->painter();
228 button_length = width_collapsed();
229 button_top_y = -ascent(bv, f);
230 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
234 draw_collapsed(pain, baseline, x);
235 x += TEXT_TO_INSET_OFFSET;
242 UpdatableInset::draw(bv, f, baseline, x, cleared);
245 x += static_cast<float>(scroll());
247 if (!cleared && (inset.need_update == InsetText::FULL ||
248 inset.need_update == InsetText::INIT ||
250 top_baseline != baseline))
253 // we don't need anymore to clear here we just have to tell
254 // the underlying LyXText that it should do the RowClear!
255 inset.setUpdateStatus(bv, InsetText::FULL);
256 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
259 int w = owner() ? width(bv, f) : pain.paperWidth();
260 int h = ascent(bv, f) + descent(bv, f);
261 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
262 int const ty = max(0, baseline - ascent(bv, f));
264 if ((ty + h) > pain.paperHeight())
265 h = pain.paperHeight();
266 if ((top_x + w) > pain.paperWidth())
267 w = pain.paperWidth();
269 h += (baseline - ascent(bv, f));
270 pain.fillRectangle(tx, ty - 1, w, h + 2);
276 top_baseline = baseline;
278 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
280 draw_collapsed(pain, bl, old_x);
282 bl + descent_collapsed() + inset.ascent(bv, f),
288 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
291 UpdatableInset::edit(bv, xp, yp, button);
298 if (!bv->lockInset(this))
300 bv->updateInset(this, false);
303 if (!bv->lockInset(this))
305 if (yp <= button_bottom_y) {
308 LyXFont font(LyXFont::ALL_SANE);
309 int yy = ascent(bv, font) + yp -
310 (ascent_collapsed() +
311 descent_collapsed() +
312 inset.ascent(bv, font));
313 inset.edit(bv, xp, yy, button);
319 void InsetCollapsable::edit(BufferView * bv, bool front)
321 UpdatableInset::edit(bv, front);
328 if (!bv->lockInset(this))
330 inset.setUpdateStatus(bv, InsetText::FULL);
331 bv->updateInset(this, false);
332 inset.edit(bv, front);
334 if (!bv->lockInset(this))
336 inset.edit(bv, front);
341 Inset::EDITABLE InsetCollapsable::editable() const
345 return HIGHLY_EDITABLE;
349 void InsetCollapsable::insetUnlock(BufferView * bv)
353 if (change_label_with_text) {
354 draw_label = get_new_label();
361 inset.insetUnlock(bv);
364 bv->updateInset(this, false);
368 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
371 if (!collapsed_ && (y > button_bottom_y)) {
372 LyXFont font(LyXFont::ALL_SANE);
373 int yy = ascent(bv, font) + y -
374 (ascent_collapsed() +
375 descent_collapsed() +
376 inset.ascent(bv, font));
377 inset.insetButtonPress(bv, x, yy, button);
382 void InsetCollapsable::insetButtonRelease(BufferView * bv,
383 int x, int y, int button)
385 if ((x >= 0) && (x < button_length) &&
386 (y >= button_top_y) && (y <= button_bottom_y)) {
392 inset.insetButtonRelease(bv, 0, 0, button);
393 inset.setUpdateStatus(bv, InsetText::FULL);
394 bv->updateInset(this, false);
397 if (change_label_with_text) {
398 draw_label = get_new_label();
404 bv->unlockInset(this);
405 bv->updateInset(this, false);
407 } else if (!collapsed_ && (y > button_bottom_y)) {
408 LyXFont font(LyXFont::ALL_SANE);
409 int yy = ascent(bv, font) + y -
410 (ascent_collapsed() +
411 descent_collapsed() +
412 inset.ascent(bv, font));
413 inset.insetButtonRelease(bv, x, yy, button);
418 void InsetCollapsable::insetMotionNotify(BufferView * bv,
419 int x, int y, int state)
421 if (y > button_bottom_y) {
422 LyXFont font(LyXFont::ALL_SANE);
423 int yy = ascent(bv, font) + y -
424 (ascent_collapsed() +
425 descent_collapsed() +
426 inset.ascent(bv, font));
427 inset.insetMotionNotify(bv, x, yy, state);
432 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
434 inset.insetKeyPress(xke);
438 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
439 bool fragile, bool free_spc) const
441 return inset.latex(buf, os, fragile, free_spc);
445 int InsetCollapsable::getMaxWidth(BufferView * bv,
446 UpdatableInset const * inset) const
448 int const w = UpdatableInset::getMaxWidth(bv, inset);
451 // What does a negative max width signify? (Lgb)
452 // Use the max width of the draw-area (Jug)
455 // should be at least 30 pixels !!!
456 return max(30, w - width_collapsed());
460 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
463 inset.update(bv, font, reinit);
467 UpdatableInset::RESULT
468 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
471 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
472 if (result == FINISHED)
473 bv->unlockInset(this);
478 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
482 return inset.lockInsetInInset(bv, in);
486 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
490 bv->unlockInset(this);
493 return inset.unlockInsetInInset(bv, in, lr);
497 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
501 return inset.updateInsetInInset(bv, in);
505 unsigned int InsetCollapsable::insetInInsetY()
507 return inset.insetInInsetY() - (top_baseline - inset.y());
511 void InsetCollapsable::validate(LaTeXFeatures & features) const
513 inset.validate(features);
517 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
519 inset.getCursorPos(bv, x , y);
523 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
525 inset.toggleInsetCursor(bv);
529 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
531 inset.showInsetCursor(bv, show);
535 void InsetCollapsable::hideInsetCursor(BufferView * bv)
537 inset.hideInsetCursor(bv);
541 UpdatableInset * InsetCollapsable::getLockingInset() const
543 UpdatableInset * in = inset.getLockingInset();
544 if (const_cast<InsetText *>(&inset) == in)
545 return const_cast<InsetCollapsable *>(this);
550 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
554 return inset.getFirstLockingInsetOfType(c);
558 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
559 bool toggleall, bool selectall)
561 inset.setFont(bv, font, toggleall, selectall);
565 bool InsetCollapsable::doClearArea() const
567 return inset.doClearArea();
571 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
572 bool const recursive) const
574 return inset.getLyXText(bv, recursive);
578 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
580 inset.deleteLyXText(bv, recursive);
584 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
586 inset.resizeLyXText(bv, force);
587 LyXFont font(LyXFont::ALL_SANE);
588 oldWidth = width(bv, font);
592 std::vector<string> const InsetCollapsable::getLabelList() const
594 return inset.getLabelList();
598 bool InsetCollapsable::nodraw() const
600 return inset.nodraw();
604 int InsetCollapsable::scroll(bool recursive) const
606 int sx = UpdatableInset::scroll(false);
609 sx += inset.scroll(recursive);
615 Paragraph * InsetCollapsable::getParFromID(int id) const
617 return inset.getParFromID(id);
621 Paragraph * InsetCollapsable::firstParagraph() const
623 return inset.firstParagraph();
627 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
629 return inset.cursor(bv);
633 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
636 return const_cast<InsetCollapsable *>(this);
637 return inset.getInsetFromID(id_arg);
642 void InsetCollapsable::open(BufferView * bv, bool flag)
644 if (flag == !collapsed_)
648 if (collapsed_ && change_label_with_text) {
649 draw_label = get_new_label();
654 bv->updateInset(this, false);
657 void InsetCollapsable::open(BufferView * bv)
659 if (!collapsed_) return;
662 bv->updateInset(this, false);
666 void InsetCollapsable::close(BufferView * bv)
668 if (collapsed_) return;
671 bv->updateInset(this, false);
677 void InsetCollapsable::setLabel(string const & l, bool flag)
680 change_label_with_text = flag;
681 if (collapsed_ && change_label_with_text) {
682 draw_label = get_new_label();
688 void InsetCollapsable::setLabel(string const & l)
696 string const InsetCollapsable::get_new_label() const
699 Paragraph::size_type const max_length = 15;
701 int n = std::min(max_length, inset.paragraph()->size());
704 for(; i < n && j < inset.paragraph()->size(); ++j) {
705 if (inset.paragraph()->isInset(j))
707 la += inset.paragraph()->getChar(j);
710 if ((i > 0) && (j < inset.paragraph()->size()))