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),
44 oldWidth(0), need_update(FULL),
48 inset.setAutoBreakRows(true);
49 inset.setDrawFrame(0, InsetText::ALWAYS);
50 inset.setFrameColor(0, LColor::collapsableframe);
51 setInsetName("Collapsable");
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
56 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
57 framecolor(in.framecolor), labelfont(in.labelfont),
58 button_length(0), button_top_y(0), button_bottom_y(0),
61 autocollapse(in.autocollapse),
63 oldWidth(0), need_update(FULL),
66 inset.init(&(in.inset), same_id);
71 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
73 if (!insetAllowed(in->lyxCode())) {
74 lyxerr << "InsetCollapsable::InsertInset: "
75 "Unable to insert inset." << endl;
78 return inset.insertInset(bv, in);
82 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
84 os << "collapsed " << tostr(collapsed_) << "\n";
85 inset.writeParagraphData(buf, os);
90 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
94 string const token = lex.GetString();
95 if (token == "collapsed") {
97 collapsed_ = lex.GetBool();
99 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
101 // Take countermeasures
102 lex.pushToken(token);
105 inset.read(buf, lex);
109 int InsetCollapsable::ascent_collapsed() const
114 lyxfont::buttonText(label, labelfont, width, ascent, descent);
119 int InsetCollapsable::descent_collapsed() const
124 lyxfont::buttonText(label, labelfont, width, ascent, descent);
129 //int InsetCollapsable::width_collapsed(Painter & pain) const
130 int InsetCollapsable::width_collapsed() const
135 lyxfont::buttonText(label, labelfont, width, ascent, descent);
136 return width + (2*TEXT_TO_INSET_OFFSET);
140 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
142 return ascent_collapsed();
146 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
149 return descent_collapsed();
151 return descent_collapsed()
152 + inset.descent(bv, font)
153 + inset.ascent(bv, font)
154 + TEXT_TO_BOTTOM_OFFSET;
158 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
161 return width_collapsed();
163 int widthCollapsed = width_collapsed();
165 return (inset.width(bv, font) > widthCollapsed) ?
166 inset.width(bv, font) : widthCollapsed;
170 void InsetCollapsable::draw_collapsed(Painter & pain,
171 int baseline, float & x) const
173 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
174 baseline, label, labelfont);
175 x += width_collapsed();
179 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
180 int baseline, float & x, bool cleared) const
185 Painter & pain = bv->painter();
187 button_length = width_collapsed();
188 button_top_y = -ascent(bv, f);
189 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
193 draw_collapsed(pain, baseline, x);
194 x += TEXT_TO_INSET_OFFSET;
201 UpdatableInset::draw(bv, f, baseline, x, cleared);
204 x += static_cast<float>(scroll());
206 if (!cleared && (inset.need_update == InsetText::FULL ||
207 inset.need_update == InsetText::INIT ||
209 top_baseline != baseline))
212 // we don't need anymore to clear here we just have to tell
213 // the underlying LyXText that it should do the RowClear!
214 inset.setUpdateStatus(bv, InsetText::FULL);
215 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
218 int w = owner() ? width(bv, f) : pain.paperWidth();
219 int h = ascent(bv, f) + descent(bv, f);
220 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
221 int const ty = max(0, baseline - ascent(bv, f));
223 if ((ty + h) > pain.paperHeight())
224 h = pain.paperHeight();
225 if ((top_x + w) > pain.paperWidth())
226 w = pain.paperWidth();
228 h += (baseline - ascent(bv, f));
229 pain.fillRectangle(tx, ty - 1, w, h + 2);
235 top_baseline = baseline;
237 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
239 draw_collapsed(pain, bl, old_x);
241 bl + descent_collapsed() + inset.ascent(bv, f),
247 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
250 UpdatableInset::edit(bv, xp, yp, button);
254 if (!bv->lockInset(this))
256 bv->updateInset(this, false);
259 if (!bv->lockInset(this))
261 if (yp <= button_bottom_y) {
264 LyXFont font(LyXFont::ALL_SANE);
265 int yy = ascent(bv, font) + yp -
266 (ascent_collapsed() +
267 descent_collapsed() +
268 inset.ascent(bv, font));
269 inset.edit(bv, xp, yy, button);
275 void InsetCollapsable::edit(BufferView * bv, bool front)
277 UpdatableInset::edit(bv, front);
281 if (!bv->lockInset(this))
283 inset.setUpdateStatus(bv, InsetText::FULL);
284 bv->updateInset(this, false);
285 inset.edit(bv, front);
287 if (!bv->lockInset(this))
289 inset.edit(bv, front);
294 Inset::EDITABLE InsetCollapsable::editable() const
298 return HIGHLY_EDITABLE;
302 void InsetCollapsable::insetUnlock(BufferView * bv)
306 if (change_label_with_text) {
307 draw_label = get_new_label();
314 inset.insetUnlock(bv);
317 bv->updateInset(this, false);
321 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
324 if (!collapsed_ && (y > button_bottom_y)) {
325 LyXFont font(LyXFont::ALL_SANE);
326 int yy = ascent(bv, font) + y -
327 (ascent_collapsed() +
328 descent_collapsed() +
329 inset.ascent(bv, font));
330 inset.insetButtonPress(bv, x, yy, button);
335 void InsetCollapsable::insetButtonRelease(BufferView * bv,
336 int x, int y, int button)
338 if ((x >= 0) && (x < button_length) &&
339 (y >= button_top_y) && (y <= button_bottom_y)) {
342 inset.insetButtonRelease(bv, 0, 0, button);
343 inset.setUpdateStatus(bv, InsetText::FULL);
344 bv->updateInset(this, false);
347 bv->unlockInset(this);
348 bv->updateInset(this, false);
350 } else if (!collapsed_ && (y > button_bottom_y)) {
351 LyXFont font(LyXFont::ALL_SANE);
352 int yy = ascent(bv, font) + y -
353 (ascent_collapsed() +
354 descent_collapsed() +
355 inset.ascent(bv, font));
356 inset.insetButtonRelease(bv, x, yy, button);
361 void InsetCollapsable::insetMotionNotify(BufferView * bv,
362 int x, int y, int state)
364 if (y > button_bottom_y) {
365 LyXFont font(LyXFont::ALL_SANE);
366 int yy = ascent(bv, font) + y -
367 (ascent_collapsed() +
368 descent_collapsed() +
369 inset.ascent(bv, font));
370 inset.insetMotionNotify(bv, x, yy, state);
375 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
377 inset.insetKeyPress(xke);
381 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
382 bool fragile, bool free_spc) const
384 return inset.latex(buf, os, fragile, free_spc);
388 int InsetCollapsable::getMaxWidth(BufferView * bv,
389 UpdatableInset const * inset) const
391 int const w = UpdatableInset::getMaxWidth(bv, inset);
394 // What does a negative max width signify? (Lgb)
395 // Use the max width of the draw-area (Jug)
398 // should be at least 30 pixels !!!
399 return max(30, w - width_collapsed());
403 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
406 inset.update(bv, font, reinit);
410 UpdatableInset::RESULT
411 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
414 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
415 if (result == FINISHED)
416 bv->unlockInset(this);
421 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
425 return inset.lockInsetInInset(bv, in);
429 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
433 bv->unlockInset(this);
436 return inset.unlockInsetInInset(bv, in, lr);
440 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
444 return inset.updateInsetInInset(bv, in);
448 unsigned int InsetCollapsable::insetInInsetY()
450 return inset.insetInInsetY() - (top_baseline - inset.y());
454 void InsetCollapsable::validate(LaTeXFeatures & features) const
456 inset.validate(features);
460 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
462 inset.getCursorPos(bv, x , y);
466 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
468 inset.toggleInsetCursor(bv);
472 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
474 inset.showInsetCursor(bv, show);
478 void InsetCollapsable::hideInsetCursor(BufferView * bv)
480 inset.hideInsetCursor(bv);
484 UpdatableInset * InsetCollapsable::getLockingInset() const
486 UpdatableInset * in = inset.getLockingInset();
487 if (const_cast<InsetText *>(&inset) == in)
488 return const_cast<InsetCollapsable *>(this);
493 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
497 return inset.getFirstLockingInsetOfType(c);
501 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
502 bool toggleall, bool selectall)
504 inset.setFont(bv, font, toggleall, selectall);
508 bool InsetCollapsable::doClearArea() const
510 return inset.doClearArea();
514 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
515 bool const recursive) const
517 return inset.getLyXText(bv, recursive);
521 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
523 inset.deleteLyXText(bv, recursive);
527 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
529 inset.resizeLyXText(bv, force);
530 LyXFont font(LyXFont::ALL_SANE);
531 oldWidth = width(bv, font);
535 std::vector<string> const InsetCollapsable::getLabelList() const
537 return inset.getLabelList();
541 bool InsetCollapsable::nodraw() const
543 return inset.nodraw();
547 int InsetCollapsable::scroll(bool recursive) const
549 int sx = UpdatableInset::scroll(false);
552 sx += inset.scroll(recursive);
558 Paragraph * InsetCollapsable::getParFromID(int id) const
560 return inset.getParFromID(id);
564 Paragraph * InsetCollapsable::firstParagraph() const
566 return inset.firstParagraph();
570 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
572 return inset.cursor(bv);
576 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
579 return const_cast<InsetCollapsable *>(this);
580 return inset.getInsetFromID(id_arg);
584 void InsetCollapsable::open(BufferView * bv)
586 if (!collapsed_) return;
589 bv->updateInset(this, false);
593 void InsetCollapsable::close(BufferView * bv)
595 if (collapsed_) return;
598 bv->updateInset(this, false);
602 void InsetCollapsable::setLabel(string const & l)