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),
39 label("Label"), draw_label(label), autocollapse(false),
40 oldWidth(0), need_update(FULL),
41 inlined(false), change_label_with_text(false)
44 inset.setAutoBreakRows(true);
45 inset.setDrawFrame(0, InsetText::ALWAYS);
46 inset.setFrameColor(0, LColor::collapsableframe);
47 setInsetName("Collapsable");
51 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
52 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
53 framecolor(in.framecolor), labelfont(in.labelfont),
54 button_length(0), button_top_y(0), button_bottom_y(0),
55 label(in.label), draw_label(label), autocollapse(in.autocollapse),
56 oldWidth(0), need_update(FULL),
57 inlined(in.inlined), change_label_with_text(in.change_label_with_text)
59 inset.init(&(in.inset), same_id);
64 Inset * InsetCollapsable::clone(Buffer const &, bool same_id) const
66 return new InsetCollapsable(*const_cast<InsetCollapsable *>(this),
71 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
73 if (!insetAllowed(in->lyxCode())) {
74 lyxerr << "InsetCollapsable::InsertInset: "
75 "Unable to insert inset." << endl;
78 return inset.insertInset(bv, in);
82 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
84 os << "collapsed " << tostr(collapsed_) << "\n";
85 inset.writeParagraphData(buf, os);
90 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
94 string const token = lex.GetString();
95 if (token == "collapsed") {
97 collapsed_ = lex.GetBool();
99 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
103 inset.read(buf, lex);
104 if (collapsed_ && change_label_with_text) {
105 draw_label = get_new_label();
112 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
113 int InsetCollapsable::ascent_collapsed(Painter & /*pain*/) const
119 pain.buttonText(0, 0, draw_label, labelfont, false,
120 width, ascent, descent);
122 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
128 //int InsetCollapsable::descent_collapsed(Painter & pain) const
129 int InsetCollapsable::descent_collapsed(Painter & /*pain*/) const
135 pain.buttonText(0, 0, draw_label, labelfont, false,
136 width, ascent, descent);
138 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
144 //int InsetCollapsable::width_collapsed(Painter & pain) const
145 int InsetCollapsable::width_collapsed(Painter & /*pain*/) const
151 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
152 width, ascent, descent);
154 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
156 return width + (2*TEXT_TO_INSET_OFFSET);
160 int InsetCollapsable::ascent(BufferView * bv, LyXFont const &) const
162 return ascent_collapsed(bv->painter());
166 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
169 return descent_collapsed(bv->painter());
171 return descent_collapsed(bv->painter())
172 + inset.descent(bv, font)
173 + inset.ascent(bv, font)
174 + TEXT_TO_BOTTOM_OFFSET;
178 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
181 return width_collapsed(bv->painter());
183 int widthCollapsed = width_collapsed(bv->painter());
185 return (inset.width(bv, font) > widthCollapsed) ?
186 inset.width(bv, font) : widthCollapsed;
190 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
194 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
195 baseline, draw_label, labelfont, true, width);
197 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
198 baseline, draw_label, labelfont);
201 x += width + TEXT_TO_INSET_OFFSET;
203 x += width_collapsed(pain) + TEXT_TO_INSET_OFFSET;
208 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
209 int baseline, float & x, bool cleared) const
214 Painter & pain = bv->painter();
216 button_length = width_collapsed(pain);
217 button_top_y = -ascent(bv, f);
218 button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain) +
219 descent_collapsed(pain);
222 draw_collapsed(pain, baseline, x);
223 x += TEXT_TO_INSET_OFFSET;
230 UpdatableInset::draw(bv, f, baseline, x, cleared);
233 x += static_cast<float>(scroll());
235 if (!cleared && (inset.need_update == InsetText::FULL ||
236 inset.need_update == InsetText::INIT ||
238 top_baseline != baseline))
241 // we don't need anymore to clear here we just have to tell
242 // the underlying LyXText that it should do the RowClear!
243 inset.setUpdateStatus(bv, InsetText::FULL);
244 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
247 int w = owner() ? width(bv, f) : pain.paperWidth();
248 int h = ascent(bv, f) + descent(bv, f);
249 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
250 int const ty = max(0, baseline - ascent(bv, f));
252 if ((ty + h) > pain.paperHeight())
253 h = pain.paperHeight();
254 if ((top_x + w) > pain.paperWidth())
255 w = pain.paperWidth();
257 h += (baseline - ascent(bv, f));
258 pain.fillRectangle(tx, ty - 1, w, h + 2);
264 top_baseline = baseline;
266 int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain);
268 draw_collapsed(pain, bl, old_x);
270 bl + descent_collapsed(pain) + inset.ascent(bv, f),
276 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
279 UpdatableInset::edit(bv, xp, yp, button);
284 if (!bv->lockInset(this))
286 bv->updateInset(this, false);
289 if (!bv->lockInset(this))
291 if (yp <= button_bottom_y) {
294 LyXFont font(LyXFont::ALL_SANE);
295 int yy = ascent(bv, font) + yp -
296 (ascent_collapsed(bv->painter()) +
297 descent_collapsed(bv->painter()) +
298 inset.ascent(bv, font));
299 inset.edit(bv, xp, yy, button);
305 void InsetCollapsable::edit(BufferView * bv, bool front)
307 UpdatableInset::edit(bv, front);
312 if (!bv->lockInset(this))
314 inset.setUpdateStatus(bv, InsetText::FULL);
315 bv->updateInset(this, false);
316 inset.edit(bv, front);
318 if (!bv->lockInset(this))
320 inset.edit(bv, front);
325 Inset::EDITABLE InsetCollapsable::editable() const
329 return HIGHLY_EDITABLE;
333 void InsetCollapsable::insetUnlock(BufferView * bv)
336 if (change_label_with_text) {
337 draw_label = get_new_label();
343 inset.insetUnlock(bv);
346 bv->updateInset(this, false);
350 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
353 if (!collapsed_ && (y > button_bottom_y)) {
354 LyXFont font(LyXFont::ALL_SANE);
355 int yy = ascent(bv, font) + y -
356 (ascent_collapsed(bv->painter()) +
357 descent_collapsed(bv->painter()) +
358 inset.ascent(bv, font));
359 inset.insetButtonPress(bv, x, yy, button);
364 void InsetCollapsable::insetButtonRelease(BufferView * bv,
365 int x, int y, int button)
367 if ((x >= 0) && (x < button_length) &&
368 (y >= button_top_y) && (y <= button_bottom_y)) {
372 inset.insetButtonRelease(bv, 0, 0, button);
373 inset.setUpdateStatus(bv, InsetText::FULL);
374 bv->updateInset(this, false);
376 if (change_label_with_text) {
377 draw_label = get_new_label();
382 bv->unlockInset(this);
383 bv->updateInset(this, false);
385 } else if (!collapsed_ && (y > button_bottom_y)) {
386 LyXFont font(LyXFont::ALL_SANE);
387 int yy = ascent(bv, font) + y -
388 (ascent_collapsed(bv->painter()) +
389 descent_collapsed(bv->painter()) +
390 inset.ascent(bv, font));
391 inset.insetButtonRelease(bv, x, yy, button);
396 void InsetCollapsable::insetMotionNotify(BufferView * bv,
397 int x, int y, int state)
399 if (y > button_bottom_y) {
400 LyXFont font(LyXFont::ALL_SANE);
401 int yy = ascent(bv, font) + y -
402 (ascent_collapsed(bv->painter()) +
403 descent_collapsed(bv->painter()) +
404 inset.ascent(bv, font));
405 inset.insetMotionNotify(bv, x, yy, state);
410 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
412 inset.insetKeyPress(xke);
416 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
417 bool fragile, bool free_spc) const
419 return inset.latex(buf, os, fragile, free_spc);
423 int InsetCollapsable::getMaxWidth(BufferView * bv,
424 UpdatableInset const * inset) const
426 int const w = UpdatableInset::getMaxWidth(bv, inset);
429 // What does a negative max width signify? (Lgb)
430 // Use the max width of the draw-area (Jug)
433 // should be at least 30 pixels !!!
434 return max(30, w - width_collapsed(bv->painter()));
438 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
441 inset.update(bv, font, reinit);
445 UpdatableInset::RESULT
446 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
449 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
450 if (result == FINISHED)
451 bv->unlockInset(this);
456 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
460 return inset.lockInsetInInset(bv, in);
464 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
468 bv->unlockInset(this);
471 return inset.unlockInsetInInset(bv, in, lr);
475 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
479 return inset.updateInsetInInset(bv, in);
483 unsigned int InsetCollapsable::insetInInsetY()
485 return inset.insetInInsetY() - (top_baseline - inset.y());
489 void InsetCollapsable::validate(LaTeXFeatures & features) const
491 inset.validate(features);
495 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
497 inset.getCursorPos(bv, x , y);
501 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
503 inset.toggleInsetCursor(bv);
507 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
509 inset.showInsetCursor(bv, show);
513 void InsetCollapsable::hideInsetCursor(BufferView * bv)
515 inset.hideInsetCursor(bv);
519 UpdatableInset * InsetCollapsable::getLockingInset() const
521 UpdatableInset * in = inset.getLockingInset();
522 if (const_cast<InsetText *>(&inset) == in)
523 return const_cast<InsetCollapsable *>(this);
528 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
532 return inset.getFirstLockingInsetOfType(c);
536 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
537 bool toggleall, bool selectall)
539 inset.setFont(bv, font, toggleall, selectall);
543 bool InsetCollapsable::doClearArea() const
545 return inset.doClearArea();
549 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
550 bool const recursive) const
552 return inset.getLyXText(bv, recursive);
556 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
558 inset.deleteLyXText(bv, recursive);
562 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
564 inset.resizeLyXText(bv, force);
565 LyXFont font(LyXFont::ALL_SANE);
566 oldWidth = width(bv, font);
570 std::vector<string> const InsetCollapsable::getLabelList() const
572 return inset.getLabelList();
576 bool InsetCollapsable::nodraw() const
578 return inset.nodraw();
582 int InsetCollapsable::scroll(bool recursive) const
584 int sx = UpdatableInset::scroll(false);
587 sx += inset.scroll(recursive);
593 Paragraph * InsetCollapsable::getParFromID(int id) const
595 return inset.getParFromID(id);
599 Paragraph * InsetCollapsable::firstParagraph() const
601 return inset.firstParagraph();
605 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
607 return inset.cursor(bv);
611 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
614 return const_cast<InsetCollapsable *>(this);
615 return inset.getInsetFromID(id_arg);
618 void InsetCollapsable::open(BufferView * bv, bool flag)
620 if (flag == !collapsed_)
623 if (collapsed_ && change_label_with_text) {
624 draw_label = get_new_label();
628 bv->updateInset(this, false);
632 void InsetCollapsable::setLabel(string const & l, bool flag)
635 change_label_with_text = flag;
636 if (collapsed_ && change_label_with_text) {
637 draw_label = get_new_label();
644 string InsetCollapsable::get_new_label() const
647 Paragraph::size_type const max_length = 15;
649 int n = std::min(max_length, inset.paragraph()->size());
651 for(i=0,j=0; i < n && j < inset.paragraph()->size(); ++j) {
652 if (inset.paragraph()->isInset(j))
654 la += inset.paragraph()->getChar(j);
657 if ((i > 0) && (j < inset.paragraph()->size()))