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()
42 inset.setAutoBreakRows(true);
43 inset.setDrawFrame(0, InsetText::ALWAYS);
44 inset.setFrameColor(0, LColor::collapsableframe);
45 button_length = button_top_y = button_bottom_y = 0;
46 setInsetName("Collapsable");
47 widthCollapsed = oldWidth = 0;
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!"
88 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
93 pain.buttonText(0, 0, label, labelfont, false,
94 width, ascent, descent);
99 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
104 pain.buttonText(0, 0, label, labelfont, false,
105 width, ascent, descent);
110 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
115 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label, labelfont, false,
116 width, ascent, descent);
117 return width + (2*TEXT_TO_INSET_OFFSET);
121 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
123 return ascent_collapsed(bv->painter(), font);
127 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
130 return descent_collapsed(bv->painter(), font);
132 return descent_collapsed(bv->painter(), font)
133 + inset.descent(bv, font)
134 + inset.ascent(bv, font)
135 + TEXT_TO_BOTTOM_OFFSET;
139 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
142 return widthCollapsed;
144 return (inset.width(bv, font) > widthCollapsed) ?
145 inset.width(bv, font) : widthCollapsed;
149 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
150 int baseline, float & x) const
153 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
154 baseline, label, labelfont, true, width);
155 x += width + TEXT_TO_INSET_OFFSET;
159 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
160 int baseline, float & x, bool cleared) const
165 Painter & pain = bv->painter();
167 button_length = widthCollapsed;
168 button_top_y = -ascent(bv, f);
169 button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain,f) +
170 descent_collapsed(pain, f);
173 draw_collapsed(pain, f, baseline, x);
174 x += TEXT_TO_INSET_OFFSET;
181 UpdatableInset::draw(bv, f, baseline, x, cleared);
184 x += static_cast<float>(scroll());
186 if (!cleared && (inset.need_update == InsetText::FULL ||
187 inset.need_update == InsetText::INIT ||
189 top_baseline != baseline))
192 // we don't need anymore to clear here we just have to tell
193 // the underlying LyXText that it should do the RowClear!
194 inset.setUpdateStatus(bv, InsetText::FULL);
195 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
198 int w = owner() ? width(bv, f) : pain.paperWidth();
199 int h = ascent(bv, f) + descent(bv, f);
200 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
201 int const ty = max(0, baseline - ascent(bv, f));
203 if ((ty + h) > pain.paperHeight())
204 h = pain.paperHeight();
205 if ((top_x + w) > pain.paperWidth())
206 w = pain.paperWidth();
208 h += (baseline - ascent(bv, f));
209 pain.fillRectangle(tx, ty - 1, w, h + 2);
215 top_baseline = baseline;
217 int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain, f);
219 draw_collapsed(pain, f, bl, old_x);
221 bl + descent_collapsed(pain, f) + inset.ascent(bv, f),
227 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
230 UpdatableInset::edit(bv, xp, yp, button);
234 if (!bv->lockInset(this))
236 bv->updateInset(this, false);
237 inset.edit(bv, 0, 0, button);
239 if (!bv->lockInset(this))
241 LyXFont font(LyXFont::ALL_SANE);
242 int yy = ascent(bv, font) + yp -
243 (ascent_collapsed(bv->painter(), font) +
244 descent_collapsed(bv->painter(), font) +
245 inset.ascent(bv, font));
246 inset.edit(bv, xp, yy, button);
251 void InsetCollapsable::edit(BufferView * bv, bool front)
253 UpdatableInset::edit(bv, front);
257 if (!bv->lockInset(this))
259 bv->updateInset(this, false);
260 inset.edit(bv, front);
262 if (!bv->lockInset(this))
264 inset.edit(bv, front);
269 Inset::EDITABLE InsetCollapsable::editable() const
273 return HIGHLY_EDITABLE;
277 void InsetCollapsable::insetUnlock(BufferView * bv)
282 inset.insetUnlock(bv);
285 bv->updateInset(this, false);
289 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
292 if (!collapsed_ && (y > button_bottom_y)) {
293 LyXFont font(LyXFont::ALL_SANE);
294 int yy = ascent(bv, font) + y -
295 (ascent_collapsed(bv->painter(), font) +
296 descent_collapsed(bv->painter(), font) +
297 inset.ascent(bv, font));
298 inset.insetButtonPress(bv, x, yy, button);
303 void InsetCollapsable::insetButtonRelease(BufferView * bv,
304 int x, int y, int button)
306 if ((x >= 0) && (x < button_length) &&
307 (y >= button_top_y) && (y <= button_bottom_y)) {
310 inset.insetButtonRelease(bv, 0, 0, button);
311 bv->updateInset(this, false);
314 bv->unlockInset(this);
315 bv->updateInset(this, false);
317 } else if (!collapsed_ && (y > button_top_y)) {
318 LyXFont font(LyXFont::ALL_SANE);
319 int yy = ascent(bv, font) + y -
320 (ascent_collapsed(bv->painter(), font) +
321 descent_collapsed(bv->painter(), font) +
322 inset.ascent(bv, font));
323 inset.insetButtonRelease(bv, x, yy, button);
328 void InsetCollapsable::insetMotionNotify(BufferView * bv,
329 int x, int y, int state)
331 if (x > button_bottom_y) {
332 LyXFont font(LyXFont::ALL_SANE);
333 int yy = ascent(bv, font) + y -
334 (ascent_collapsed(bv->painter(), font) +
335 descent_collapsed(bv->painter(), font) +
336 inset.ascent(bv, font));
337 inset.insetMotionNotify(bv, x, yy, state);
342 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
344 inset.insetKeyPress(xke);
348 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
349 bool fragile, bool free_spc) const
351 return inset.latex(buf, os, fragile, free_spc);
355 int InsetCollapsable::getMaxWidth(BufferView * bv,
356 UpdatableInset const * inset) const
358 int const w = UpdatableInset::getMaxWidth(bv, inset);
361 // What does a negative max width signify? (Lgb)
362 // Use the max width of the draw-area (Jug)
365 // should be at least 30 pixels !!!
366 return max(30, w - widthCollapsed);
370 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
377 owner()->update(bv, font, true);
380 if (!widthCollapsed) {
381 widthCollapsed = width_collapsed(bv->painter(), font);
382 inset.resizeLyXText(bv);
385 owner()->update(bv, font);
389 if (oldWidth != width(bv, font)) {
390 oldWidth = width(bv, font);
391 inset.resizeLyXText(bv);
394 owner()->update(bv, font);
400 widthCollapsed = width_collapsed(bv->painter(), font);
401 inset.update(bv, font, reinit);
406 UpdatableInset::RESULT
407 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
410 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
411 if (result == FINISHED)
412 bv->unlockInset(this);
417 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
421 return inset.lockInsetInInset(bv, in);
425 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
429 bv->unlockInset(this);
432 return inset.unlockInsetInInset(bv, in, lr);
436 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
440 return inset.updateInsetInInset(bv, in);
444 unsigned int InsetCollapsable::insetInInsetY()
446 return inset.insetInInsetY() - (top_baseline - inset.y());
450 void InsetCollapsable::validate(LaTeXFeatures & features) const
452 inset.validate(features);
456 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
458 inset.getCursorPos(bv, x , y);
462 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
464 inset.toggleInsetCursor(bv);
468 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
470 inset.showInsetCursor(bv, show);
474 void InsetCollapsable::hideInsetCursor(BufferView * bv)
476 inset.hideInsetCursor(bv);
480 UpdatableInset * InsetCollapsable::getLockingInset() const
482 UpdatableInset * in = inset.getLockingInset();
483 if (const_cast<InsetText *>(&inset) == in)
484 return const_cast<InsetCollapsable *>(this);
489 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
493 return inset.getFirstLockingInsetOfType(c);
497 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
498 bool toggleall, bool selectall)
500 inset.setFont(bv, font, toggleall, selectall);
504 bool InsetCollapsable::doClearArea() const
506 return inset.doClearArea();
510 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
511 bool const recursive) const
513 return inset.getLyXText(bv, recursive);
517 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
519 inset.deleteLyXText(bv, recursive);
523 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
525 inset.resizeLyXText(bv, force);
526 LyXFont font(LyXFont::ALL_SANE);
527 oldWidth = width(bv, font);
531 std::vector<string> const InsetCollapsable::getLabelList() const
533 return inset.getLabelList();
537 bool InsetCollapsable::nodraw() const
539 return inset.nodraw();
543 int InsetCollapsable::scroll(bool recursive) const
545 int sx = UpdatableInset::scroll(false);
548 sx += inset.scroll(recursive);
554 Paragraph * InsetCollapsable::getParFromID(int id) const
556 return inset.getParFromID(id);
560 Paragraph * InsetCollapsable::firstParagraph() const
562 return inset.firstParagraph();
566 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
568 return inset.cursor(bv);
572 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
575 return const_cast<InsetCollapsable *>(this);
576 return inset.getInsetFromID(id_arg);
579 void InsetCollapsable::open(BufferView * bv, bool flag)
581 if (flag == !collapsed_)
584 bv->updateInset(this, false);