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"
37 InsetCollapsable::InsetCollapsable(bool collapsed)
38 : UpdatableInset(), collapsed_(collapsed),
39 button_length(0), button_top_y(0), button_bottom_y(0),
47 oldWidth(0), need_update(FULL),
50 , change_label_with_text(false)
54 inset.setAutoBreakRows(true);
55 inset.setDrawFrame(0, InsetText::ALWAYS);
56 inset.setFrameColor(0, LColor::collapsableframe);
57 setInsetName("Collapsable");
61 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
62 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
63 framecolor(in.framecolor), labelfont(in.labelfont),
64 button_length(0), button_top_y(0), button_bottom_y(0),
70 autocollapse(in.autocollapse),
72 oldWidth(0), need_update(FULL),
75 , change_label_with_text(in.change_label_with_text)
78 inset.init(&(in.inset), same_id);
83 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
85 if (!insetAllowed(in->lyxCode())) {
86 lyxerr << "InsetCollapsable::InsertInset: "
87 "Unable to insert inset." << endl;
90 return inset.insertInset(bv, in);
94 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
96 os << "collapsed " << tostr(collapsed_) << "\n";
97 inset.writeParagraphData(buf, os);
102 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
106 string const token = lex.GetString();
107 if (token == "collapsed") {
109 collapsed_ = lex.GetBool();
111 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
113 // Take countermeasures
114 lex.pushToken(token);
117 inset.read(buf, lex);
119 if (collapsed_ && change_label_with_text) {
120 draw_label = get_new_label();
128 //int InsetCollapsable::ascent_collapsed(Painter & pain) const
129 int InsetCollapsable::ascent_collapsed() const
135 pain.buttonText(0, 0, draw_label, labelfont, false,
136 width, ascent, descent);
138 lyxfont::buttonText(label, labelfont, width, ascent, descent);
144 //int InsetCollapsable::descent_collapsed(Painter & pain) const
145 int InsetCollapsable::descent_collapsed() const
151 pain.buttonText(0, 0, draw_label, labelfont, false,
152 width, ascent, descent);
154 lyxfont::buttonText(label, labelfont, width, ascent, descent);
160 //int InsetCollapsable::width_collapsed(Painter & pain) const
161 int InsetCollapsable::width_collapsed() const
167 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
168 width, ascent, descent);
170 lyxfont::buttonText(label, labelfont, width, ascent, descent);
172 return width + (2*TEXT_TO_INSET_OFFSET);
176 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
178 return ascent_collapsed();
182 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
185 return descent_collapsed();
187 return descent_collapsed()
188 + inset.descent(bv, font)
189 + inset.ascent(bv, font)
190 + TEXT_TO_BOTTOM_OFFSET;
194 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
197 return width_collapsed();
199 int widthCollapsed = width_collapsed();
201 return (inset.width(bv, font) > widthCollapsed) ?
202 inset.width(bv, font) : widthCollapsed;
206 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
210 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
211 baseline, draw_label, labelfont, true, width);
212 x += width + TEXT_TO_INSET_OFFSET;
214 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
215 baseline, label, labelfont);
216 x += width_collapsed();
221 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
222 int baseline, float & x, bool cleared) const
227 Painter & pain = bv->painter();
229 button_length = width_collapsed();
230 button_top_y = -ascent(bv, f);
231 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
235 draw_collapsed(pain, baseline, x);
236 x += TEXT_TO_INSET_OFFSET;
243 UpdatableInset::draw(bv, f, baseline, x, cleared);
246 x += static_cast<float>(scroll());
248 if (!cleared && (inset.need_update == InsetText::FULL ||
249 inset.need_update == InsetText::INIT ||
251 top_baseline != baseline))
254 // we don't need anymore to clear here we just have to tell
255 // the underlying LyXText that it should do the RowClear!
256 inset.setUpdateStatus(bv, InsetText::FULL);
257 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
260 int w = owner() ? width(bv, f) : pain.paperWidth();
261 int h = ascent(bv, f) + descent(bv, f);
262 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
263 int const ty = max(0, baseline - ascent(bv, f));
265 if ((ty + h) > pain.paperHeight())
266 h = pain.paperHeight();
267 if ((top_x + w) > pain.paperWidth())
268 w = pain.paperWidth();
270 h += (baseline - ascent(bv, f));
271 pain.fillRectangle(tx, ty - 1, w, h + 2);
277 top_baseline = baseline;
279 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
281 draw_collapsed(pain, bl, old_x);
283 bl + descent_collapsed() + inset.ascent(bv, f),
289 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
292 UpdatableInset::edit(bv, xp, yp, button);
299 if (!bv->lockInset(this))
301 bv->updateInset(this, false);
304 if (!bv->lockInset(this))
306 if (yp <= button_bottom_y) {
309 LyXFont font(LyXFont::ALL_SANE);
310 int yy = ascent(bv, font) + yp -
311 (ascent_collapsed() +
312 descent_collapsed() +
313 inset.ascent(bv, font));
314 inset.edit(bv, xp, yy, button);
320 void InsetCollapsable::edit(BufferView * bv, bool front)
322 UpdatableInset::edit(bv, front);
329 if (!bv->lockInset(this))
331 inset.setUpdateStatus(bv, InsetText::FULL);
332 bv->updateInset(this, false);
333 inset.edit(bv, front);
335 if (!bv->lockInset(this))
337 inset.edit(bv, front);
342 Inset::EDITABLE InsetCollapsable::editable() const
346 return HIGHLY_EDITABLE;
350 void InsetCollapsable::insetUnlock(BufferView * bv)
354 if (change_label_with_text) {
355 draw_label = get_new_label();
362 inset.insetUnlock(bv);
365 bv->updateInset(this, false);
369 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
372 if (!collapsed_ && (y > button_bottom_y)) {
373 LyXFont font(LyXFont::ALL_SANE);
374 int yy = ascent(bv, font) + y -
375 (ascent_collapsed() +
376 descent_collapsed() +
377 inset.ascent(bv, font));
378 inset.insetButtonPress(bv, x, yy, button);
383 void InsetCollapsable::insetButtonRelease(BufferView * bv,
384 int x, int y, int button)
386 if ((x >= 0) && (x < button_length) &&
387 (y >= button_top_y) && (y <= button_bottom_y)) {
393 inset.insetButtonRelease(bv, 0, 0, button);
394 inset.setUpdateStatus(bv, InsetText::FULL);
395 bv->updateInset(this, false);
398 if (change_label_with_text) {
399 draw_label = get_new_label();
405 bv->unlockInset(this);
406 bv->updateInset(this, false);
408 } else if (!collapsed_ && (y > button_bottom_y)) {
409 LyXFont font(LyXFont::ALL_SANE);
410 int yy = ascent(bv, font) + y -
411 (ascent_collapsed() +
412 descent_collapsed() +
413 inset.ascent(bv, font));
414 inset.insetButtonRelease(bv, x, yy, button);
419 void InsetCollapsable::insetMotionNotify(BufferView * bv,
420 int x, int y, int state)
422 if (y > button_bottom_y) {
423 LyXFont font(LyXFont::ALL_SANE);
424 int yy = ascent(bv, font) + y -
425 (ascent_collapsed() +
426 descent_collapsed() +
427 inset.ascent(bv, font));
428 inset.insetMotionNotify(bv, x, yy, state);
433 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
435 inset.insetKeyPress(xke);
439 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
440 bool fragile, bool free_spc) const
442 return inset.latex(buf, os, fragile, free_spc);
446 int InsetCollapsable::getMaxWidth(BufferView * bv,
447 UpdatableInset const * inset) const
449 int const w = UpdatableInset::getMaxWidth(bv, inset);
452 // What does a negative max width signify? (Lgb)
453 // Use the max width of the draw-area (Jug)
456 // should be at least 30 pixels !!!
457 return max(30, w - width_collapsed());
461 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
464 inset.update(bv, font, reinit);
468 UpdatableInset::RESULT
469 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
472 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
473 if (result == FINISHED)
474 bv->unlockInset(this);
479 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
483 return inset.lockInsetInInset(bv, in);
487 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
491 bv->unlockInset(this);
494 return inset.unlockInsetInInset(bv, in, lr);
498 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
502 return inset.updateInsetInInset(bv, in);
506 unsigned int InsetCollapsable::insetInInsetY()
508 return inset.insetInInsetY() - (top_baseline - inset.y());
512 void InsetCollapsable::validate(LaTeXFeatures & features) const
514 inset.validate(features);
518 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
520 inset.getCursorPos(bv, x , y);
524 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
526 inset.toggleInsetCursor(bv);
530 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
532 inset.showInsetCursor(bv, show);
536 void InsetCollapsable::hideInsetCursor(BufferView * bv)
538 inset.hideInsetCursor(bv);
542 UpdatableInset * InsetCollapsable::getLockingInset() const
544 UpdatableInset * in = inset.getLockingInset();
545 if (const_cast<InsetText *>(&inset) == in)
546 return const_cast<InsetCollapsable *>(this);
551 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
555 return inset.getFirstLockingInsetOfType(c);
559 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
560 bool toggleall, bool selectall)
562 inset.setFont(bv, font, toggleall, selectall);
566 bool InsetCollapsable::doClearArea() const
568 return inset.doClearArea();
572 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
573 bool const recursive) const
575 return inset.getLyXText(bv, recursive);
579 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
581 inset.deleteLyXText(bv, recursive);
585 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
587 inset.resizeLyXText(bv, force);
588 LyXFont font(LyXFont::ALL_SANE);
589 oldWidth = width(bv, font);
593 std::vector<string> const InsetCollapsable::getLabelList() const
595 return inset.getLabelList();
599 bool InsetCollapsable::nodraw() const
601 return inset.nodraw();
605 int InsetCollapsable::scroll(bool recursive) const
607 int sx = UpdatableInset::scroll(false);
610 sx += inset.scroll(recursive);
616 Paragraph * InsetCollapsable::getParFromID(int id) const
618 return inset.getParFromID(id);
622 Paragraph * InsetCollapsable::firstParagraph() const
624 return inset.firstParagraph();
628 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
630 return inset.cursor(bv);
634 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
637 return const_cast<InsetCollapsable *>(this);
638 return inset.getInsetFromID(id_arg);
643 void InsetCollapsable::open(BufferView * bv, bool flag)
645 if (flag == !collapsed_)
649 if (collapsed_ && change_label_with_text) {
650 draw_label = get_new_label();
655 bv->updateInset(this, false);
658 void InsetCollapsable::open(BufferView * bv)
660 if (!collapsed_) return;
663 bv->updateInset(this, false);
667 void InsetCollapsable::close(BufferView * bv)
669 if (collapsed_) return;
672 bv->updateInset(this, false);
678 void InsetCollapsable::setLabel(string const & l, bool flag)
681 change_label_with_text = flag;
682 if (collapsed_ && change_label_with_text) {
683 draw_label = get_new_label();
689 void InsetCollapsable::setLabel(string const & l)
697 string const InsetCollapsable::get_new_label() const
700 Paragraph::size_type const max_length = 15;
702 int n = std::min(max_length, inset.paragraph()->size());
705 for(; i < n && j < inset.paragraph()->size(); ++j) {
706 if (inset.paragraph()->isInset(j))
708 la += inset.paragraph()->getChar(j);
711 if ((i > 0) && (j < inset.paragraph()->size()))