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(true),
40 oldWidth(0), need_update(FULL),
41 inlined(false), change_label_with_text(false)
46 inset.setAutoBreakRows(true);
47 inset.setDrawFrame(0, InsetText::ALWAYS);
48 inset.setFrameColor(0, LColor::collapsableframe);
49 setInsetName("Collapsable");
53 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
55 if (!insetAllowed(in->lyxCode())) {
56 lyxerr << "InsetCollapsable::InsertInset: "
57 "Unable to insert inset." << endl;
60 return inset.insertInset(bv, in);
64 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
66 os << "collapsed " << tostr(collapsed_) << "\n";
67 inset.writeParagraphData(buf, os);
72 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
76 string const token = lex.GetString();
77 if (token == "collapsed") {
79 collapsed_ = lex.GetBool();
81 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
86 if (collapsed_ && change_label_with_text) {
87 draw_label = get_new_label();
94 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
95 int InsetCollapsable::ascent_collapsed(Painter & /*pain*/) const
101 pain.buttonText(0, 0, draw_label, labelfont, false,
102 width, ascent, descent);
104 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
110 //int InsetCollapsable::descent_collapsed(Painter & pain) const
111 int InsetCollapsable::descent_collapsed(Painter & /*pain*/) const
117 pain.buttonText(0, 0, draw_label, labelfont, false,
118 width, ascent, descent);
120 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
126 //int InsetCollapsable::width_collapsed(Painter & pain) const
127 int InsetCollapsable::width_collapsed(Painter & /*pain*/) const
133 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
134 width, ascent, descent);
136 lyxfont::buttonText(draw_label, labelfont, width, ascent, descent);
138 return width + (2*TEXT_TO_INSET_OFFSET);
142 int InsetCollapsable::ascent(BufferView * bv, LyXFont const &) const
144 return ascent_collapsed(bv->painter());
148 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
151 return descent_collapsed(bv->painter());
153 return descent_collapsed(bv->painter())
154 + inset.descent(bv, font)
155 + inset.ascent(bv, font)
156 + TEXT_TO_BOTTOM_OFFSET;
160 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
163 return width_collapsed(bv->painter());
165 int widthCollapsed = width_collapsed(bv->painter());
167 return (inset.width(bv, font) > widthCollapsed) ?
168 inset.width(bv, font) : widthCollapsed;
172 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
176 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
177 baseline, draw_label, labelfont, true, width);
179 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
180 baseline, draw_label, labelfont);
183 x += width + TEXT_TO_INSET_OFFSET;
185 x += width_collapsed(pain) + TEXT_TO_INSET_OFFSET;
190 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
191 int baseline, float & x, bool cleared) const
196 Painter & pain = bv->painter();
198 button_length = width_collapsed(pain);
199 button_top_y = -ascent(bv, f);
200 button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain) +
201 descent_collapsed(pain);
204 draw_collapsed(pain, baseline, x);
205 x += TEXT_TO_INSET_OFFSET;
212 UpdatableInset::draw(bv, f, baseline, x, cleared);
215 x += static_cast<float>(scroll());
217 if (!cleared && (inset.need_update == InsetText::FULL ||
218 inset.need_update == InsetText::INIT ||
220 top_baseline != baseline))
223 // we don't need anymore to clear here we just have to tell
224 // the underlying LyXText that it should do the RowClear!
225 inset.setUpdateStatus(bv, InsetText::FULL);
226 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
229 int w = owner() ? width(bv, f) : pain.paperWidth();
230 int h = ascent(bv, f) + descent(bv, f);
231 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
232 int const ty = max(0, baseline - ascent(bv, f));
234 if ((ty + h) > pain.paperHeight())
235 h = pain.paperHeight();
236 if ((top_x + w) > pain.paperWidth())
237 w = pain.paperWidth();
239 h += (baseline - ascent(bv, f));
240 pain.fillRectangle(tx, ty - 1, w, h + 2);
246 top_baseline = baseline;
248 int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain);
250 draw_collapsed(pain, bl, old_x);
252 bl + descent_collapsed(pain) + inset.ascent(bv, f),
258 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
261 UpdatableInset::edit(bv, xp, yp, button);
266 if (!bv->lockInset(this))
268 bv->updateInset(this, false);
269 inset.edit(bv, 0, 0, button);
271 if (!bv->lockInset(this))
273 LyXFont font(LyXFont::ALL_SANE);
274 int yy = ascent(bv, font) + yp -
275 (ascent_collapsed(bv->painter()) +
276 descent_collapsed(bv->painter()) +
277 inset.ascent(bv, font));
278 inset.edit(bv, xp, yy, button);
283 void InsetCollapsable::edit(BufferView * bv, bool front)
285 UpdatableInset::edit(bv, front);
290 if (!bv->lockInset(this))
292 inset.setUpdateStatus(bv, InsetText::FULL);
293 bv->updateInset(this, false);
294 inset.edit(bv, front);
296 if (!bv->lockInset(this))
298 inset.edit(bv, front);
303 Inset::EDITABLE InsetCollapsable::editable() const
307 return HIGHLY_EDITABLE;
311 void InsetCollapsable::insetUnlock(BufferView * bv)
314 if (change_label_with_text) {
315 draw_label = get_new_label();
321 inset.insetUnlock(bv);
324 bv->updateInset(this, false);
328 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
331 if (!collapsed_ && (y > button_bottom_y)) {
332 LyXFont font(LyXFont::ALL_SANE);
333 int yy = ascent(bv, font) + y -
334 (ascent_collapsed(bv->painter()) +
335 descent_collapsed(bv->painter()) +
336 inset.ascent(bv, font));
337 inset.insetButtonPress(bv, x, yy, button);
342 void InsetCollapsable::insetButtonRelease(BufferView * bv,
343 int x, int y, int button)
345 if ((x >= 0) && (x < button_length) &&
346 (y >= button_top_y) && (y <= button_bottom_y)) {
350 inset.insetButtonRelease(bv, 0, 0, button);
351 inset.setUpdateStatus(bv, InsetText::FULL);
352 bv->updateInset(this, false);
354 if (change_label_with_text) {
355 draw_label = get_new_label();
360 bv->unlockInset(this);
361 bv->updateInset(this, false);
363 } else if (!collapsed_ && (y > button_top_y)) {
364 LyXFont font(LyXFont::ALL_SANE);
365 int yy = ascent(bv, font) + y -
366 (ascent_collapsed(bv->painter()) +
367 descent_collapsed(bv->painter()) +
368 inset.ascent(bv, font));
369 inset.insetButtonRelease(bv, x, yy, button);
374 void InsetCollapsable::insetMotionNotify(BufferView * bv,
375 int x, int y, int state)
377 if (x > button_bottom_y) {
378 LyXFont font(LyXFont::ALL_SANE);
379 int yy = ascent(bv, font) + y -
380 (ascent_collapsed(bv->painter()) +
381 descent_collapsed(bv->painter()) +
382 inset.ascent(bv, font));
383 inset.insetMotionNotify(bv, x, yy, state);
388 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
390 inset.insetKeyPress(xke);
394 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
395 bool fragile, bool free_spc) const
397 return inset.latex(buf, os, fragile, free_spc);
401 int InsetCollapsable::getMaxWidth(BufferView * bv,
402 UpdatableInset const * inset) const
404 int const w = UpdatableInset::getMaxWidth(bv, inset);
407 // What does a negative max width signify? (Lgb)
408 // Use the max width of the draw-area (Jug)
411 // should be at least 30 pixels !!!
412 return max(30, w - width_collapsed(bv->painter()));
416 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
419 inset.update(bv, font, reinit);
423 UpdatableInset::RESULT
424 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
427 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
428 if (result == FINISHED)
429 bv->unlockInset(this);
434 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
438 return inset.lockInsetInInset(bv, in);
442 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
446 bv->unlockInset(this);
449 return inset.unlockInsetInInset(bv, in, lr);
453 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
457 return inset.updateInsetInInset(bv, in);
461 unsigned int InsetCollapsable::insetInInsetY()
463 return inset.insetInInsetY() - (top_baseline - inset.y());
467 void InsetCollapsable::validate(LaTeXFeatures & features) const
469 inset.validate(features);
473 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
475 inset.getCursorPos(bv, x , y);
479 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
481 inset.toggleInsetCursor(bv);
485 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
487 inset.showInsetCursor(bv, show);
491 void InsetCollapsable::hideInsetCursor(BufferView * bv)
493 inset.hideInsetCursor(bv);
497 UpdatableInset * InsetCollapsable::getLockingInset() const
499 UpdatableInset * in = inset.getLockingInset();
500 if (const_cast<InsetText *>(&inset) == in)
501 return const_cast<InsetCollapsable *>(this);
506 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
510 return inset.getFirstLockingInsetOfType(c);
514 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
515 bool toggleall, bool selectall)
517 inset.setFont(bv, font, toggleall, selectall);
521 bool InsetCollapsable::doClearArea() const
523 return inset.doClearArea();
527 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
528 bool const recursive) const
530 return inset.getLyXText(bv, recursive);
534 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
536 inset.deleteLyXText(bv, recursive);
540 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
542 inset.resizeLyXText(bv, force);
543 LyXFont font(LyXFont::ALL_SANE);
544 oldWidth = width(bv, font);
548 std::vector<string> const InsetCollapsable::getLabelList() const
550 return inset.getLabelList();
554 bool InsetCollapsable::nodraw() const
556 return inset.nodraw();
560 int InsetCollapsable::scroll(bool recursive) const
562 int sx = UpdatableInset::scroll(false);
565 sx += inset.scroll(recursive);
571 Paragraph * InsetCollapsable::getParFromID(int id) const
573 return inset.getParFromID(id);
577 Paragraph * InsetCollapsable::firstParagraph() const
579 return inset.firstParagraph();
583 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
585 return inset.cursor(bv);
589 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
592 return const_cast<InsetCollapsable *>(this);
593 return inset.getInsetFromID(id_arg);
596 void InsetCollapsable::open(BufferView * bv, bool flag)
598 if (flag == !collapsed_)
601 if (collapsed_ && change_label_with_text) {
602 draw_label = get_new_label();
606 bv->updateInset(this, false);
610 void InsetCollapsable::setLabel(string const & l, bool flag)
613 change_label_with_text = flag;
614 if (collapsed_ && change_label_with_text) {
615 draw_label = get_new_label();
622 string InsetCollapsable::get_new_label() const
625 Paragraph::size_type const max_length = 10;
627 int n = std::min(max_length, inset.paragraph()->size());
629 for(i=0,j=0; i < n && j < inset.paragraph()->size(); ++j) {
630 if (inset.paragraph()->isInset(j))
632 la += inset.paragraph()->getChar(j);
635 if ((i > 0) && (j < inset.paragraph()->size()))