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"
35 InsetCollapsable::InsetCollapsable(bool collapsed)
36 : UpdatableInset(), collapsed_(collapsed),
37 button_length(0), button_top_y(0), button_bottom_y(0),
38 label("Label"), draw_label(label), autocollapse(true),
39 widthCollapsed(0), oldWidth(0), need_update(FULL),
40 inlined(false), change_label_with_text(false)
45 inset.setAutoBreakRows(true);
46 inset.setDrawFrame(0, InsetText::ALWAYS);
47 inset.setFrameColor(0, LColor::collapsableframe);
48 setInsetName("Collapsable");
52 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
54 if (!insetAllowed(in->lyxCode())) {
55 lyxerr << "InsetCollapsable::InsertInset: "
56 "Unable to insert inset." << endl;
59 return inset.insertInset(bv, in);
63 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
65 os << "collapsed " << tostr(collapsed_) << "\n";
66 inset.writeParagraphData(buf, os);
71 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
75 string const token = lex.GetString();
76 if (token == "collapsed") {
78 collapsed_ = lex.GetBool();
80 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
85 if (collapsed_ && change_label_with_text) {
86 draw_label = get_new_label();
93 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
98 pain.buttonText(0, 0, draw_label, labelfont, false,
99 width, ascent, descent);
104 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
109 pain.buttonText(0, 0, draw_label, labelfont, false,
110 width, ascent, descent);
115 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
120 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
121 width, ascent, descent);
122 return width + (2*TEXT_TO_INSET_OFFSET);
126 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
128 return ascent_collapsed(bv->painter(), font);
132 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
135 return descent_collapsed(bv->painter(), font);
137 return descent_collapsed(bv->painter(), font)
138 + inset.descent(bv, font)
139 + inset.ascent(bv, font)
140 + TEXT_TO_BOTTOM_OFFSET;
144 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
146 widthCollapsed = width_collapsed(bv->painter(), font);
149 return widthCollapsed;
151 return (inset.width(bv, font) > widthCollapsed) ?
152 inset.width(bv, font) : widthCollapsed;
156 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
157 int baseline, float & x) const
160 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
161 baseline, draw_label, labelfont, true, width);
162 x += width + TEXT_TO_INSET_OFFSET;
166 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
167 int baseline, float & x, bool cleared) const
172 Painter & pain = bv->painter();
175 widthCollapsed = width_collapsed(pain, f);
177 button_length = widthCollapsed;
178 button_top_y = -ascent(bv, f);
179 button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain,f) +
180 descent_collapsed(pain, f);
183 draw_collapsed(pain, f, baseline, x);
184 x += TEXT_TO_INSET_OFFSET;
191 UpdatableInset::draw(bv, f, baseline, x, cleared);
194 x += static_cast<float>(scroll());
196 if (!cleared && (inset.need_update == InsetText::FULL ||
197 inset.need_update == InsetText::INIT ||
199 top_baseline != baseline))
202 // we don't need anymore to clear here we just have to tell
203 // the underlying LyXText that it should do the RowClear!
204 inset.setUpdateStatus(bv, InsetText::FULL);
205 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
208 int w = owner() ? width(bv, f) : pain.paperWidth();
209 int h = ascent(bv, f) + descent(bv, f);
210 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
211 int const ty = max(0, baseline - ascent(bv, f));
213 if ((ty + h) > pain.paperHeight())
214 h = pain.paperHeight();
215 if ((top_x + w) > pain.paperWidth())
216 w = pain.paperWidth();
218 h += (baseline - ascent(bv, f));
219 pain.fillRectangle(tx, ty - 1, w, h + 2);
225 top_baseline = baseline;
227 int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain, f);
229 draw_collapsed(pain, f, bl, old_x);
231 bl + descent_collapsed(pain, f) + inset.ascent(bv, f),
237 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
240 UpdatableInset::edit(bv, xp, yp, button);
245 if (!bv->lockInset(this))
247 bv->updateInset(this, false);
248 inset.edit(bv, 0, 0, button);
250 if (!bv->lockInset(this))
252 LyXFont font(LyXFont::ALL_SANE);
253 int yy = ascent(bv, font) + yp -
254 (ascent_collapsed(bv->painter(), font) +
255 descent_collapsed(bv->painter(), font) +
256 inset.ascent(bv, font));
257 inset.edit(bv, xp, yy, button);
262 void InsetCollapsable::edit(BufferView * bv, bool front)
264 UpdatableInset::edit(bv, front);
269 if (!bv->lockInset(this))
271 bv->updateInset(this, false);
272 inset.edit(bv, front);
274 if (!bv->lockInset(this))
276 inset.edit(bv, front);
281 Inset::EDITABLE InsetCollapsable::editable() const
285 return HIGHLY_EDITABLE;
289 void InsetCollapsable::insetUnlock(BufferView * bv)
292 if (change_label_with_text) {
293 draw_label = get_new_label();
299 inset.insetUnlock(bv);
302 bv->updateInset(this, false);
306 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
309 if (!collapsed_ && (y > button_bottom_y)) {
310 LyXFont font(LyXFont::ALL_SANE);
311 int yy = ascent(bv, font) + y -
312 (ascent_collapsed(bv->painter(), font) +
313 descent_collapsed(bv->painter(), font) +
314 inset.ascent(bv, font));
315 inset.insetButtonPress(bv, x, yy, button);
320 void InsetCollapsable::insetButtonRelease(BufferView * bv,
321 int x, int y, int button)
323 if ((x >= 0) && (x < button_length) &&
324 (y >= button_top_y) && (y <= button_bottom_y)) {
328 inset.insetButtonRelease(bv, 0, 0, button);
329 bv->updateInset(this, false);
331 if (change_label_with_text) {
332 draw_label = get_new_label();
337 bv->unlockInset(this);
338 bv->updateInset(this, false);
340 } else if (!collapsed_ && (y > button_top_y)) {
341 LyXFont font(LyXFont::ALL_SANE);
342 int yy = ascent(bv, font) + y -
343 (ascent_collapsed(bv->painter(), font) +
344 descent_collapsed(bv->painter(), font) +
345 inset.ascent(bv, font));
346 inset.insetButtonRelease(bv, x, yy, button);
351 void InsetCollapsable::insetMotionNotify(BufferView * bv,
352 int x, int y, int state)
354 if (x > button_bottom_y) {
355 LyXFont font(LyXFont::ALL_SANE);
356 int yy = ascent(bv, font) + y -
357 (ascent_collapsed(bv->painter(), font) +
358 descent_collapsed(bv->painter(), font) +
359 inset.ascent(bv, font));
360 inset.insetMotionNotify(bv, x, yy, state);
365 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
367 inset.insetKeyPress(xke);
371 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
372 bool fragile, bool free_spc) const
374 return inset.latex(buf, os, fragile, free_spc);
378 int InsetCollapsable::getMaxWidth(BufferView * bv,
379 UpdatableInset const * inset) const
381 int const w = UpdatableInset::getMaxWidth(bv, inset);
384 // What does a negative max width signify? (Lgb)
385 // Use the max width of the draw-area (Jug)
388 // should be at least 30 pixels !!!
389 return max(30, w - widthCollapsed);
393 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
400 owner()->update(bv, font, true);
403 if (!widthCollapsed) {
404 widthCollapsed = width_collapsed(bv->painter(), font);
405 inset.resizeLyXText(bv);
408 owner()->update(bv, font);
412 if (oldWidth != width(bv, font)) {
413 oldWidth = width(bv, font);
414 inset.resizeLyXText(bv);
417 owner()->update(bv, font);
423 widthCollapsed = width_collapsed(bv->painter(), font);
424 inset.update(bv, font, reinit);
429 UpdatableInset::RESULT
430 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
433 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
434 if (result == FINISHED)
435 bv->unlockInset(this);
440 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
444 return inset.lockInsetInInset(bv, in);
448 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
452 bv->unlockInset(this);
455 return inset.unlockInsetInInset(bv, in, lr);
459 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
463 return inset.updateInsetInInset(bv, in);
467 unsigned int InsetCollapsable::insetInInsetY()
469 return inset.insetInInsetY() - (top_baseline - inset.y());
473 void InsetCollapsable::validate(LaTeXFeatures & features) const
475 inset.validate(features);
479 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
481 inset.getCursorPos(bv, x , y);
485 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
487 inset.toggleInsetCursor(bv);
491 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
493 inset.showInsetCursor(bv, show);
497 void InsetCollapsable::hideInsetCursor(BufferView * bv)
499 inset.hideInsetCursor(bv);
503 UpdatableInset * InsetCollapsable::getLockingInset() const
505 UpdatableInset * in = inset.getLockingInset();
506 if (const_cast<InsetText *>(&inset) == in)
507 return const_cast<InsetCollapsable *>(this);
512 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
516 return inset.getFirstLockingInsetOfType(c);
520 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
521 bool toggleall, bool selectall)
523 inset.setFont(bv, font, toggleall, selectall);
527 bool InsetCollapsable::doClearArea() const
529 return inset.doClearArea();
533 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
534 bool const recursive) const
536 return inset.getLyXText(bv, recursive);
540 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
542 inset.deleteLyXText(bv, recursive);
546 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
548 inset.resizeLyXText(bv, force);
549 LyXFont font(LyXFont::ALL_SANE);
550 oldWidth = width(bv, font);
554 std::vector<string> const InsetCollapsable::getLabelList() const
556 return inset.getLabelList();
560 bool InsetCollapsable::nodraw() const
562 return inset.nodraw();
566 int InsetCollapsable::scroll(bool recursive) const
568 int sx = UpdatableInset::scroll(false);
571 sx += inset.scroll(recursive);
577 Paragraph * InsetCollapsable::getParFromID(int id) const
579 return inset.getParFromID(id);
583 Paragraph * InsetCollapsable::firstParagraph() const
585 return inset.firstParagraph();
589 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
591 return inset.cursor(bv);
595 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
598 return const_cast<InsetCollapsable *>(this);
599 return inset.getInsetFromID(id_arg);
602 void InsetCollapsable::open(BufferView * bv, bool flag)
604 if (flag == !collapsed_)
607 if (collapsed_ && change_label_with_text) {
608 draw_label = get_new_label();
612 bv->updateInset(this, false);
616 void InsetCollapsable::setLabel(string const & l, bool flag)
619 change_label_with_text = flag;
620 if (collapsed_ && change_label_with_text) {
621 draw_label = get_new_label();
628 string InsetCollapsable::get_new_label() const
632 int n = std::min(10, inset.paragraph()->size());
634 for(i=0,j=0; i < n && j < inset.paragraph()->size(); ++j) {
635 if (inset.paragraph()->isInset(j))
637 la += inset.paragraph()->getChar(j);
640 if ((i > 0) && (j < inset.paragraph()->size()))