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),
40 need_update(NONE), label("Label"),
44 oldWidth(0), in_update(false)
47 inset.setAutoBreakRows(true);
48 inset.setDrawFrame(0, InsetText::ALWAYS);
49 inset.setFrameColor(0, LColor::collapsableframe);
50 setInsetName("Collapsable");
54 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
55 : UpdatableInset(in, same_id), collapsed_(in.collapsed_),
56 framecolor(in.framecolor), labelfont(in.labelfont),
57 button_length(0), button_top_y(0), button_bottom_y(0),
58 need_update(NONE), label(in.label),
60 autocollapse(in.autocollapse),
62 oldWidth(0), in_update(false)
64 inset.init(&(in.inset), same_id);
69 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
71 if (!insetAllowed(in->lyxCode())) {
72 lyxerr << "InsetCollapsable::InsertInset: "
73 "Unable to insert inset." << endl;
76 return inset.insertInset(bv, in);
80 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
82 os << "collapsed " << tostr(collapsed_) << "\n";
83 inset.writeParagraphData(buf, os);
88 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
92 string const token = lex.getString();
93 if (token == "collapsed") {
95 collapsed_ = lex.getBool();
97 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
99 // Take countermeasures
100 lex.pushToken(token);
103 inset.read(buf, lex);
107 int InsetCollapsable::ascent_collapsed() const
112 lyxfont::buttonText(label, labelfont, width, ascent, descent);
117 int InsetCollapsable::descent_collapsed() const
122 lyxfont::buttonText(label, labelfont, width, ascent, descent);
127 //int InsetCollapsable::width_collapsed(Painter & pain) const
128 int InsetCollapsable::width_collapsed() const
133 lyxfont::buttonText(label, labelfont, width, ascent, descent);
134 return width + (2*TEXT_TO_INSET_OFFSET);
138 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
140 return ascent_collapsed();
144 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
147 return descent_collapsed();
149 return descent_collapsed()
150 + inset.descent(bv, font)
151 + inset.ascent(bv, font)
152 + TEXT_TO_BOTTOM_OFFSET;
156 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
159 return width_collapsed();
161 int widthCollapsed = width_collapsed();
163 return (inset.width(bv, font) > widthCollapsed) ?
164 inset.width(bv, font) : widthCollapsed;
168 void InsetCollapsable::draw_collapsed(Painter & pain,
169 int baseline, float & x) const
171 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
172 baseline, label, labelfont);
173 x += width_collapsed();
177 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
178 int baseline, float & x, bool cleared) const
180 if (need_update != NONE) {
181 const_cast<InsetText *>(&inset)->update(bv, f, true);
182 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
189 Painter & pain = bv->painter();
191 button_length = width_collapsed();
192 button_top_y = -ascent(bv, f);
193 button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
197 draw_collapsed(pain, baseline, x);
198 x += TEXT_TO_INSET_OFFSET;
205 x += static_cast<float>(scroll());
207 if (!cleared && (inset.need_update == InsetText::FULL ||
208 inset.need_update == InsetText::INIT ||
210 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);
221 top_baseline = baseline;
223 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
225 draw_collapsed(pain, bl, old_x);
227 bl + descent_collapsed() + inset.ascent(bv, f),
229 if (x < (top_x + button_length + TEXT_TO_INSET_OFFSET))
230 x = top_x + button_length + TEXT_TO_INSET_OFFSET;
234 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
237 UpdatableInset::edit(bv, xp, yp, button);
241 // set this only here as it should be recollapsed only if
242 // it was already collapsed!
243 first_after_edit = true;
244 if (!bv->lockInset(this))
246 bv->updateInset(this, false);
249 if (!bv->lockInset(this))
251 if (yp <= button_bottom_y) {
254 LyXFont font(LyXFont::ALL_SANE);
255 int yy = ascent(bv, font) + yp -
256 (ascent_collapsed() +
257 descent_collapsed() +
258 inset.ascent(bv, font));
259 inset.edit(bv, xp, yy, button);
265 void InsetCollapsable::edit(BufferView * bv, bool front)
267 UpdatableInset::edit(bv, front);
271 if (!bv->lockInset(this))
273 inset.setUpdateStatus(bv, InsetText::FULL);
274 bv->updateInset(this, false);
275 inset.edit(bv, front);
277 if (!bv->lockInset(this))
279 inset.edit(bv, front);
281 first_after_edit = true;
285 Inset::EDITABLE InsetCollapsable::editable() const
289 return HIGHLY_EDITABLE;
293 void InsetCollapsable::insetUnlock(BufferView * bv)
297 if (change_label_with_text) {
298 draw_label = get_new_label();
305 inset.insetUnlock(bv);
308 bv->updateInset(this, false);
312 void InsetCollapsable::insetButtonPress(BufferView * bv,
313 int x, int y, int button)
315 if (!collapsed_ && (y > button_bottom_y)) {
316 LyXFont font(LyXFont::ALL_SANE);
317 int yy = ascent(bv, font) + y -
318 (ascent_collapsed() +
319 descent_collapsed() +
320 inset.ascent(bv, font));
321 inset.insetButtonPress(bv, x, yy, button);
326 void InsetCollapsable::insetButtonRelease(BufferView * bv,
327 int x, int y, int button)
329 if ((x >= 0) && (x < button_length) &&
330 (y >= button_top_y) && (y <= button_bottom_y))
334 // should not be called on inset open!
335 // inset.insetButtonRelease(bv, 0, 0, button);
336 inset.setUpdateStatus(bv, InsetText::FULL);
337 bv->updateInset(this, false);
340 bv->unlockInset(this);
341 bv->updateInset(this, false);
343 } else if (!collapsed_ && (y > button_bottom_y)) {
344 LyXFont font(LyXFont::ALL_SANE);
345 int yy = ascent(bv, font) + y -
346 (ascent_collapsed() +
347 descent_collapsed() +
348 inset.ascent(bv, font));
349 inset.insetButtonRelease(bv, x, yy, button);
354 void InsetCollapsable::insetMotionNotify(BufferView * bv,
355 int x, int y, int state)
357 if (y > button_bottom_y) {
358 LyXFont font(LyXFont::ALL_SANE);
359 int yy = ascent(bv, font) + y -
360 (ascent_collapsed() +
361 descent_collapsed() +
362 inset.ascent(bv, font));
363 inset.insetMotionNotify(bv, x, yy, state);
368 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
370 inset.insetKeyPress(xke);
374 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
375 bool fragile, bool free_spc) const
377 return inset.latex(buf, os, fragile, free_spc);
381 int InsetCollapsable::getMaxWidth(BufferView * bv,
382 UpdatableInset const * in) const
385 int const w = UpdatableInset::getMaxWidth(bv, in);
388 // What does a negative max width signify? (Lgb)
389 // Use the max width of the draw-area (Jug)
392 // should be at least 30 pixels !!!
393 return max(30, w - width_collapsed());
395 return UpdatableInset::getMaxWidth(bv, in);
400 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
404 if (reinit && owner()) {
405 owner()->update(bv, font, true);
410 inset.update(bv, font, reinit);
411 if (reinit && owner()) {
412 owner()->update(bv, font, true);
418 UpdatableInset::RESULT
419 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
422 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
423 if (result >= FINISHED)
424 bv->unlockInset(this);
425 first_after_edit = false;
430 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
434 return inset.lockInsetInInset(bv, in);
438 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
442 bv->unlockInset(this);
445 return inset.unlockInsetInInset(bv, in, lr);
449 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
453 return inset.updateInsetInInset(bv, in);
457 unsigned int InsetCollapsable::insetInInsetY()
459 return inset.insetInInsetY() - (top_baseline - inset.y());
463 void InsetCollapsable::validate(LaTeXFeatures & features) const
465 inset.validate(features);
469 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
471 inset.getCursorPos(bv, x , y);
475 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
477 inset.toggleInsetCursor(bv);
481 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
483 inset.showInsetCursor(bv, show);
487 void InsetCollapsable::hideInsetCursor(BufferView * bv)
489 inset.hideInsetCursor(bv);
493 UpdatableInset * InsetCollapsable::getLockingInset() const
495 UpdatableInset * in = inset.getLockingInset();
496 if (const_cast<InsetText *>(&inset) == in)
497 return const_cast<InsetCollapsable *>(this);
502 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
506 return inset.getFirstLockingInsetOfType(c);
510 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
511 bool toggleall, bool selectall)
513 inset.setFont(bv, font, toggleall, selectall);
517 bool InsetCollapsable::doClearArea() const
519 return inset.doClearArea();
523 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
524 bool const recursive) const
526 return inset.getLyXText(bv, recursive);
530 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
532 inset.deleteLyXText(bv, recursive);
536 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
538 inset.resizeLyXText(bv, force);
539 LyXFont font(LyXFont::ALL_SANE);
540 oldWidth = width(bv, font);
544 std::vector<string> const InsetCollapsable::getLabelList() const
546 return inset.getLabelList();
550 bool InsetCollapsable::nodraw() const
552 return inset.nodraw();
556 int InsetCollapsable::scroll(bool recursive) const
558 int sx = UpdatableInset::scroll(false);
561 sx += inset.scroll(recursive);
567 Paragraph * InsetCollapsable::getParFromID(int id) const
569 lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
570 return inset.getParFromID(id);
574 Paragraph * InsetCollapsable::firstParagraph() const
576 return inset.firstParagraph();
580 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
582 return inset.getFirstParagraph(i);
586 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
588 return inset.cursor(bv);
592 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
595 return const_cast<InsetCollapsable *>(this);
596 return inset.getInsetFromID(id_arg);
600 void InsetCollapsable::open(BufferView * bv)
602 if (!collapsed_) return;
605 bv->updateInset(this, false);
609 void InsetCollapsable::close(BufferView * bv) const
615 bv->updateInset(const_cast<InsetCollapsable *>(this), false);
619 void InsetCollapsable::setLabel(string const & l) const
625 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
626 bool const & cs, bool const & mw)
628 bool found = inset.searchForward(bv, str, cs, mw);
629 if (first_after_edit && !found)
631 first_after_edit = false;
636 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
637 bool const & cs, bool const & mw)
639 bool found = inset.searchBackward(bv, str, cs, mw);
640 if (first_after_edit && !found)
642 first_after_edit = false;
647 string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv,
650 string const str = inset.selectNextWordToSpellcheck(bv, value);
651 if (first_after_edit && str.empty())
653 first_after_edit = false;