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),
232 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
235 UpdatableInset::edit(bv, xp, yp, button);
239 if (!bv->lockInset(this))
241 bv->updateInset(this, true);
244 if (!bv->lockInset(this))
246 if (yp <= button_bottom_y) {
249 LyXFont font(LyXFont::ALL_SANE);
250 int yy = ascent(bv, font) + yp -
251 (ascent_collapsed() +
252 descent_collapsed() +
253 inset.ascent(bv, font));
254 inset.edit(bv, xp, yy, button);
257 first_after_edit = true;
261 void InsetCollapsable::edit(BufferView * bv, bool front)
263 UpdatableInset::edit(bv, front);
267 if (!bv->lockInset(this))
269 inset.setUpdateStatus(bv, InsetText::FULL);
270 bv->updateInset(this, true);
271 inset.edit(bv, front);
273 if (!bv->lockInset(this))
275 inset.edit(bv, front);
277 first_after_edit = true;
281 Inset::EDITABLE InsetCollapsable::editable() const
285 return HIGHLY_EDITABLE;
289 void InsetCollapsable::insetUnlock(BufferView * bv)
293 if (change_label_with_text) {
294 draw_label = get_new_label();
301 inset.insetUnlock(bv);
304 bv->updateInset(this, false);
308 void InsetCollapsable::insetButtonPress(BufferView * bv,
309 int x, int y, int button)
311 if (!collapsed_ && (y > button_bottom_y)) {
312 LyXFont font(LyXFont::ALL_SANE);
313 int yy = ascent(bv, font) + y -
314 (ascent_collapsed() +
315 descent_collapsed() +
316 inset.ascent(bv, font));
317 inset.insetButtonPress(bv, x, yy, button);
322 void InsetCollapsable::insetButtonRelease(BufferView * bv,
323 int x, int y, int button)
325 if ((x >= 0) && (x < button_length) &&
326 (y >= button_top_y) && (y <= button_bottom_y))
330 // should not be called on inset open!
331 // inset.insetButtonRelease(bv, 0, 0, button);
332 inset.setUpdateStatus(bv, InsetText::FULL);
333 bv->updateInset(this, true);
336 bv->unlockInset(this);
337 bv->updateInset(this, true);
339 } else if (!collapsed_ && (y > button_bottom_y)) {
340 LyXFont font(LyXFont::ALL_SANE);
341 int yy = ascent(bv, font) + y -
342 (ascent_collapsed() +
343 descent_collapsed() +
344 inset.ascent(bv, font));
345 inset.insetButtonRelease(bv, x, yy, button);
350 void InsetCollapsable::insetMotionNotify(BufferView * bv,
351 int x, int y, int state)
353 if (y > button_bottom_y) {
354 LyXFont font(LyXFont::ALL_SANE);
355 int yy = ascent(bv, font) + y -
356 (ascent_collapsed() +
357 descent_collapsed() +
358 inset.ascent(bv, font));
359 inset.insetMotionNotify(bv, x, yy, state);
364 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
366 inset.insetKeyPress(xke);
370 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
371 bool fragile, bool free_spc) const
373 return inset.latex(buf, os, fragile, free_spc);
377 int InsetCollapsable::getMaxWidth(BufferView * bv,
378 UpdatableInset const * inset) const
381 int const w = UpdatableInset::getMaxWidth(bv, inset);
384 // What does a negative max width signify? (Lgb)
385 // Use the max width of the draw-area (Jug)
388 // should be at least 30 pixels !!!
389 return max(30, w - width_collapsed());
391 return UpdatableInset::getMaxWidth(bv, inset);
396 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
400 if (reinit && owner()) {
401 owner()->update(bv, font, true);
406 inset.update(bv, font, reinit);
407 if (reinit && owner()) {
408 owner()->update(bv, font, true);
414 UpdatableInset::RESULT
415 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
418 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
419 if (result == FINISHED)
420 bv->unlockInset(this);
421 first_after_edit = false;
426 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
430 return inset.lockInsetInInset(bv, in);
434 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
438 bv->unlockInset(this);
441 return inset.unlockInsetInInset(bv, in, lr);
445 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
449 return inset.updateInsetInInset(bv, in);
453 unsigned int InsetCollapsable::insetInInsetY()
455 return inset.insetInInsetY() - (top_baseline - inset.y());
459 void InsetCollapsable::validate(LaTeXFeatures & features) const
461 inset.validate(features);
465 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
467 inset.getCursorPos(bv, x , y);
471 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
473 inset.toggleInsetCursor(bv);
477 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
479 inset.showInsetCursor(bv, show);
483 void InsetCollapsable::hideInsetCursor(BufferView * bv)
485 inset.hideInsetCursor(bv);
489 UpdatableInset * InsetCollapsable::getLockingInset() const
491 UpdatableInset * in = inset.getLockingInset();
492 if (const_cast<InsetText *>(&inset) == in)
493 return const_cast<InsetCollapsable *>(this);
498 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
502 return inset.getFirstLockingInsetOfType(c);
506 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
507 bool toggleall, bool selectall)
509 inset.setFont(bv, font, toggleall, selectall);
513 bool InsetCollapsable::doClearArea() const
515 return inset.doClearArea();
519 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
520 bool const recursive) const
522 return inset.getLyXText(bv, recursive);
526 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
528 inset.deleteLyXText(bv, recursive);
532 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
534 inset.resizeLyXText(bv, force);
535 LyXFont font(LyXFont::ALL_SANE);
536 oldWidth = width(bv, font);
540 std::vector<string> const InsetCollapsable::getLabelList() const
542 return inset.getLabelList();
546 bool InsetCollapsable::nodraw() const
548 return inset.nodraw();
552 int InsetCollapsable::scroll(bool recursive) const
554 int sx = UpdatableInset::scroll(false);
557 sx += inset.scroll(recursive);
563 Paragraph * InsetCollapsable::getParFromID(int id) const
565 lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
566 return inset.getParFromID(id);
570 Paragraph * InsetCollapsable::firstParagraph() const
572 return inset.firstParagraph();
576 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
578 return inset.cursor(bv);
582 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
585 return const_cast<InsetCollapsable *>(this);
586 return inset.getInsetFromID(id_arg);
590 void InsetCollapsable::open(BufferView * bv)
592 if (!collapsed_) return;
595 bv->updateInset(this, true);
599 void InsetCollapsable::close(BufferView * bv) const
605 bv->updateInset(const_cast<InsetCollapsable *>(this), true);
609 void InsetCollapsable::setLabel(string const & l) const
615 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
616 bool const & cs, bool const & mw)
618 bool found = inset.searchForward(bv, str, cs, mw);
619 if (first_after_edit && !found)
621 first_after_edit = false;
624 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
625 bool const & cs, bool const & mw)
627 bool found = inset.searchBackward(bv, str, cs, mw);
628 if (first_after_edit && !found)
630 first_after_edit = false;
635 string const InsetCollapsable::selectNextWord(BufferView * bv, float & value) const
637 string str = inset.selectNextWord(bv, value);
638 if (first_after_edit && str.empty())
640 first_after_edit = false;