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);
204 x += static_cast<float>(scroll());
206 if (!cleared && (inset.need_update == InsetText::FULL ||
207 inset.need_update == InsetText::INIT ||
209 top_baseline != baseline))
211 // we don't need anymore to clear here we just have to tell
212 // the underlying LyXText that it should do the RowClear!
213 inset.setUpdateStatus(bv, InsetText::FULL);
214 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
220 top_baseline = baseline;
222 int const bl = baseline - ascent(bv, f) + ascent_collapsed();
224 draw_collapsed(pain, bl, old_x);
226 bl + descent_collapsed() + inset.ascent(bv, f),
228 if (x < (top_x + button_length + TEXT_TO_INSET_OFFSET))
229 x = top_x + button_length + TEXT_TO_INSET_OFFSET;
233 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
236 UpdatableInset::edit(bv, xp, yp, button);
240 // set this only here as it should be recollapsed only if
241 // it was already collapsed!
242 first_after_edit = true;
243 if (!bv->lockInset(this))
245 bv->updateInset(this, false);
248 if (!bv->lockInset(this))
250 if (yp <= button_bottom_y) {
253 LyXFont font(LyXFont::ALL_SANE);
254 int yy = ascent(bv, font) + yp -
255 (ascent_collapsed() +
256 descent_collapsed() +
257 inset.ascent(bv, font));
258 inset.edit(bv, xp, yy, button);
264 void InsetCollapsable::edit(BufferView * bv, bool front)
266 UpdatableInset::edit(bv, front);
270 if (!bv->lockInset(this))
272 inset.setUpdateStatus(bv, InsetText::FULL);
273 bv->updateInset(this, false);
274 inset.edit(bv, front);
276 if (!bv->lockInset(this))
278 inset.edit(bv, front);
280 first_after_edit = true;
284 Inset::EDITABLE InsetCollapsable::editable() const
288 return HIGHLY_EDITABLE;
292 void InsetCollapsable::insetUnlock(BufferView * bv)
296 if (change_label_with_text) {
297 draw_label = get_new_label();
304 inset.insetUnlock(bv);
307 bv->updateInset(this, false);
311 void InsetCollapsable::insetButtonPress(BufferView * bv,
312 int x, int y, int button)
314 if (!collapsed_ && (y > button_bottom_y)) {
315 LyXFont font(LyXFont::ALL_SANE);
316 int yy = ascent(bv, font) + y -
317 (ascent_collapsed() +
318 descent_collapsed() +
319 inset.ascent(bv, font));
320 inset.insetButtonPress(bv, x, yy, button);
325 bool InsetCollapsable::insetButtonRelease(BufferView * bv,
326 int x, int y, int button)
329 if ((button != 3) && (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 ret = inset.insetButtonRelease(bv, x, yy, button);
351 if ((button == 3) && !ret) {
352 return showInsetDialog(bv);
358 void InsetCollapsable::insetMotionNotify(BufferView * bv,
359 int x, int y, int state)
361 if (y > button_bottom_y) {
362 LyXFont font(LyXFont::ALL_SANE);
363 int yy = ascent(bv, font) + y -
364 (ascent_collapsed() +
365 descent_collapsed() +
366 inset.ascent(bv, font));
367 inset.insetMotionNotify(bv, x, yy, state);
372 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
374 inset.insetKeyPress(xke);
378 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
379 bool fragile, bool free_spc) const
381 return inset.latex(buf, os, fragile, free_spc);
385 int InsetCollapsable::ascii(Buffer const *buf, std::ostream & os, int ll) const
387 return inset.ascii(buf, os, ll);
391 int InsetCollapsable::linuxdoc(Buffer const *buf, std::ostream & os) const
393 return inset.linuxdoc(buf, os);
397 int InsetCollapsable::docbook(Buffer const *buf, std::ostream & os) const
399 return inset.docbook(buf, os);
403 int InsetCollapsable::getMaxWidth(BufferView * bv,
404 UpdatableInset const * in) const
407 int const w = UpdatableInset::getMaxWidth(bv, in);
410 // What does a negative max width signify? (Lgb)
411 // Use the max width of the draw-area (Jug)
414 // should be at least 30 pixels !!!
415 return max(30, w - width_collapsed());
417 return UpdatableInset::getMaxWidth(bv, in);
423 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
427 if (reinit && owner()) {
428 owner()->update(bv, font, true);
433 inset.update(bv, font, reinit);
434 if (reinit && owner()) {
435 owner()->update(bv, font, true);
441 UpdatableInset::RESULT
442 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
445 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
446 if (result >= FINISHED)
447 bv->unlockInset(this);
448 first_after_edit = false;
453 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
457 return inset.lockInsetInInset(bv, in);
461 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
465 bv->unlockInset(this);
468 return inset.unlockInsetInInset(bv, in, lr);
472 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
476 return inset.updateInsetInInset(bv, in);
480 unsigned int InsetCollapsable::insetInInsetY()
482 return inset.insetInInsetY() - (top_baseline - inset.y());
486 void InsetCollapsable::validate(LaTeXFeatures & features) const
488 inset.validate(features);
492 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
494 inset.getCursorPos(bv, x , y);
498 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
500 inset.toggleInsetCursor(bv);
504 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
506 inset.showInsetCursor(bv, show);
510 void InsetCollapsable::hideInsetCursor(BufferView * bv)
512 inset.hideInsetCursor(bv);
516 UpdatableInset * InsetCollapsable::getLockingInset() const
518 UpdatableInset * in = inset.getLockingInset();
519 if (const_cast<InsetText *>(&inset) == in)
520 return const_cast<InsetCollapsable *>(this);
525 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
529 return inset.getFirstLockingInsetOfType(c);
533 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
534 bool toggleall, bool selectall)
536 inset.setFont(bv, font, toggleall, selectall);
540 bool InsetCollapsable::doClearArea() const
542 return inset.doClearArea();
546 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
547 bool const recursive) const
549 return inset.getLyXText(bv, recursive);
553 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
555 inset.deleteLyXText(bv, recursive);
559 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
561 inset.resizeLyXText(bv, force);
562 LyXFont font(LyXFont::ALL_SANE);
563 oldWidth = width(bv, font);
567 std::vector<string> const InsetCollapsable::getLabelList() const
569 return inset.getLabelList();
573 bool InsetCollapsable::nodraw() const
575 return inset.nodraw();
579 int InsetCollapsable::scroll(bool recursive) const
581 int sx = UpdatableInset::scroll(false);
584 sx += inset.scroll(recursive);
590 Paragraph * InsetCollapsable::getParFromID(int id) const
592 lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
593 return inset.getParFromID(id);
597 Paragraph * InsetCollapsable::firstParagraph() const
599 return inset.firstParagraph();
603 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
605 return inset.getFirstParagraph(i);
609 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
611 return inset.cursor(bv);
615 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
618 return const_cast<InsetCollapsable *>(this);
619 return inset.getInsetFromID(id_arg);
623 void InsetCollapsable::open(BufferView * bv)
625 if (!collapsed_) return;
628 bv->updateInset(this, false);
632 void InsetCollapsable::close(BufferView * bv) const
638 bv->updateInset(const_cast<InsetCollapsable *>(this), false);
642 void InsetCollapsable::setLabel(string const & l) const
648 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
651 bool found = inset.searchForward(bv, str, cs, mw);
652 if (first_after_edit && !found)
654 first_after_edit = false;
659 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
662 bool found = inset.searchBackward(bv, str, cs, mw);
663 if (first_after_edit && !found)
665 first_after_edit = false;
670 string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv,
673 string const str = inset.selectNextWordToSpellcheck(bv, value);
674 if (first_after_edit && str.empty())
676 first_after_edit = false;