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 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) const
98 pain.buttonText(0, 0, draw_label, labelfont, false,
99 width, ascent, descent);
104 int InsetCollapsable::descent_collapsed(Painter & pain) const
109 pain.buttonText(0, 0, draw_label, labelfont, false,
110 width, ascent, descent);
115 int InsetCollapsable::width_collapsed(Painter & pain) 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 &) const
128 return ascent_collapsed(bv->painter());
132 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
135 return descent_collapsed(bv->painter());
137 return descent_collapsed(bv->painter())
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
147 return width_collapsed(bv->painter());
149 int widthCollapsed = width_collapsed(bv->painter());
151 return (inset.width(bv, font) > widthCollapsed) ?
152 inset.width(bv, font) : widthCollapsed;
156 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
159 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
160 baseline, draw_label, labelfont, true, width);
161 x += width + TEXT_TO_INSET_OFFSET;
165 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
166 int baseline, float & x, bool cleared) const
171 Painter & pain = bv->painter();
173 button_length = width_collapsed(pain);
174 button_top_y = -ascent(bv, f);
175 button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain) +
176 descent_collapsed(pain);
179 draw_collapsed(pain, baseline, x);
180 x += TEXT_TO_INSET_OFFSET;
187 UpdatableInset::draw(bv, f, baseline, x, cleared);
190 x += static_cast<float>(scroll());
192 if (!cleared && (inset.need_update == InsetText::FULL ||
193 inset.need_update == InsetText::INIT ||
195 top_baseline != baseline))
198 // we don't need anymore to clear here we just have to tell
199 // the underlying LyXText that it should do the RowClear!
200 inset.setUpdateStatus(bv, InsetText::FULL);
201 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
204 int w = owner() ? width(bv, f) : pain.paperWidth();
205 int h = ascent(bv, f) + descent(bv, f);
206 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
207 int const ty = max(0, baseline - ascent(bv, f));
209 if ((ty + h) > pain.paperHeight())
210 h = pain.paperHeight();
211 if ((top_x + w) > pain.paperWidth())
212 w = pain.paperWidth();
214 h += (baseline - ascent(bv, f));
215 pain.fillRectangle(tx, ty - 1, w, h + 2);
221 top_baseline = baseline;
223 int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain);
225 draw_collapsed(pain, bl, old_x);
227 bl + descent_collapsed(pain) + inset.ascent(bv, f),
233 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
236 UpdatableInset::edit(bv, xp, yp, button);
241 if (!bv->lockInset(this))
243 bv->updateInset(this, false);
244 inset.edit(bv, 0, 0, button);
246 if (!bv->lockInset(this))
248 LyXFont font(LyXFont::ALL_SANE);
249 int yy = ascent(bv, font) + yp -
250 (ascent_collapsed(bv->painter()) +
251 descent_collapsed(bv->painter()) +
252 inset.ascent(bv, font));
253 inset.edit(bv, xp, yy, button);
258 void InsetCollapsable::edit(BufferView * bv, bool front)
260 UpdatableInset::edit(bv, front);
265 if (!bv->lockInset(this))
267 bv->updateInset(this, false);
268 inset.edit(bv, front);
270 if (!bv->lockInset(this))
272 inset.edit(bv, front);
277 Inset::EDITABLE InsetCollapsable::editable() const
281 return HIGHLY_EDITABLE;
285 void InsetCollapsable::insetUnlock(BufferView * bv)
288 if (change_label_with_text) {
289 draw_label = get_new_label();
295 inset.insetUnlock(bv);
298 bv->updateInset(this, false);
302 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
305 if (!collapsed_ && (y > button_bottom_y)) {
306 LyXFont font(LyXFont::ALL_SANE);
307 int yy = ascent(bv, font) + y -
308 (ascent_collapsed(bv->painter()) +
309 descent_collapsed(bv->painter()) +
310 inset.ascent(bv, font));
311 inset.insetButtonPress(bv, x, yy, button);
316 void InsetCollapsable::insetButtonRelease(BufferView * bv,
317 int x, int y, int button)
319 if ((x >= 0) && (x < button_length) &&
320 (y >= button_top_y) && (y <= button_bottom_y)) {
324 inset.insetButtonRelease(bv, 0, 0, button);
325 bv->updateInset(this, false);
327 if (change_label_with_text) {
328 draw_label = get_new_label();
333 bv->unlockInset(this);
334 bv->updateInset(this, false);
336 } else if (!collapsed_ && (y > button_top_y)) {
337 LyXFont font(LyXFont::ALL_SANE);
338 int yy = ascent(bv, font) + y -
339 (ascent_collapsed(bv->painter()) +
340 descent_collapsed(bv->painter()) +
341 inset.ascent(bv, font));
342 inset.insetButtonRelease(bv, x, yy, button);
347 void InsetCollapsable::insetMotionNotify(BufferView * bv,
348 int x, int y, int state)
350 if (x > button_bottom_y) {
351 LyXFont font(LyXFont::ALL_SANE);
352 int yy = ascent(bv, font) + y -
353 (ascent_collapsed(bv->painter()) +
354 descent_collapsed(bv->painter()) +
355 inset.ascent(bv, font));
356 inset.insetMotionNotify(bv, x, yy, state);
361 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
363 inset.insetKeyPress(xke);
367 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
368 bool fragile, bool free_spc) const
370 return inset.latex(buf, os, fragile, free_spc);
374 int InsetCollapsable::getMaxWidth(BufferView * bv,
375 UpdatableInset const * inset) const
377 int const w = UpdatableInset::getMaxWidth(bv, inset);
380 // What does a negative max width signify? (Lgb)
381 // Use the max width of the draw-area (Jug)
384 // should be at least 30 pixels !!!
385 return max(30, w - width_collapsed(bv->painter()));
389 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
392 inset.update(bv, font, reinit);
396 UpdatableInset::RESULT
397 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
400 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
401 if (result == FINISHED)
402 bv->unlockInset(this);
407 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
411 return inset.lockInsetInInset(bv, in);
415 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
419 bv->unlockInset(this);
422 return inset.unlockInsetInInset(bv, in, lr);
426 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
430 return inset.updateInsetInInset(bv, in);
434 unsigned int InsetCollapsable::insetInInsetY()
436 return inset.insetInInsetY() - (top_baseline - inset.y());
440 void InsetCollapsable::validate(LaTeXFeatures & features) const
442 inset.validate(features);
446 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
448 inset.getCursorPos(bv, x , y);
452 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
454 inset.toggleInsetCursor(bv);
458 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
460 inset.showInsetCursor(bv, show);
464 void InsetCollapsable::hideInsetCursor(BufferView * bv)
466 inset.hideInsetCursor(bv);
470 UpdatableInset * InsetCollapsable::getLockingInset() const
472 UpdatableInset * in = inset.getLockingInset();
473 if (const_cast<InsetText *>(&inset) == in)
474 return const_cast<InsetCollapsable *>(this);
479 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
483 return inset.getFirstLockingInsetOfType(c);
487 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
488 bool toggleall, bool selectall)
490 inset.setFont(bv, font, toggleall, selectall);
494 bool InsetCollapsable::doClearArea() const
496 return inset.doClearArea();
500 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
501 bool const recursive) const
503 return inset.getLyXText(bv, recursive);
507 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
509 inset.deleteLyXText(bv, recursive);
513 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
515 inset.resizeLyXText(bv, force);
516 LyXFont font(LyXFont::ALL_SANE);
517 oldWidth = width(bv, font);
521 std::vector<string> const InsetCollapsable::getLabelList() const
523 return inset.getLabelList();
527 bool InsetCollapsable::nodraw() const
529 return inset.nodraw();
533 int InsetCollapsable::scroll(bool recursive) const
535 int sx = UpdatableInset::scroll(false);
538 sx += inset.scroll(recursive);
544 Paragraph * InsetCollapsable::getParFromID(int id) const
546 return inset.getParFromID(id);
550 Paragraph * InsetCollapsable::firstParagraph() const
552 return inset.firstParagraph();
556 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
558 return inset.cursor(bv);
562 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
565 return const_cast<InsetCollapsable *>(this);
566 return inset.getInsetFromID(id_arg);
569 void InsetCollapsable::open(BufferView * bv, bool flag)
571 if (flag == !collapsed_)
574 if (collapsed_ && change_label_with_text) {
575 draw_label = get_new_label();
579 bv->updateInset(this, false);
583 void InsetCollapsable::setLabel(string const & l, bool flag)
586 change_label_with_text = flag;
587 if (collapsed_ && change_label_with_text) {
588 draw_label = get_new_label();
595 string InsetCollapsable::get_new_label() const
599 int n = std::min(10, inset.paragraph()->size());
601 for(i=0,j=0; i < n && j < inset.paragraph()->size(); ++j) {
602 if (inset.paragraph()->isInset(j))
604 la += inset.paragraph()->getChar(j);
607 if ((i > 0) && (j < inset.paragraph()->size()))