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::getMaxWidth(BufferView * bv,
386 UpdatableInset const * in) const
389 int const w = UpdatableInset::getMaxWidth(bv, in);
392 // What does a negative max width signify? (Lgb)
393 // Use the max width of the draw-area (Jug)
396 // should be at least 30 pixels !!!
397 return max(30, w - width_collapsed());
399 return UpdatableInset::getMaxWidth(bv, in);
405 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
409 if (reinit && owner()) {
410 owner()->update(bv, font, true);
415 inset.update(bv, font, reinit);
416 if (reinit && owner()) {
417 owner()->update(bv, font, true);
423 UpdatableInset::RESULT
424 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
427 UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
428 if (result >= FINISHED)
429 bv->unlockInset(this);
430 first_after_edit = false;
435 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
439 return inset.lockInsetInInset(bv, in);
443 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
447 bv->unlockInset(this);
450 return inset.unlockInsetInInset(bv, in, lr);
454 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
458 return inset.updateInsetInInset(bv, in);
462 unsigned int InsetCollapsable::insetInInsetY()
464 return inset.insetInInsetY() - (top_baseline - inset.y());
468 void InsetCollapsable::validate(LaTeXFeatures & features) const
470 inset.validate(features);
474 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
476 inset.getCursorPos(bv, x , y);
480 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
482 inset.toggleInsetCursor(bv);
486 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
488 inset.showInsetCursor(bv, show);
492 void InsetCollapsable::hideInsetCursor(BufferView * bv)
494 inset.hideInsetCursor(bv);
498 UpdatableInset * InsetCollapsable::getLockingInset() const
500 UpdatableInset * in = inset.getLockingInset();
501 if (const_cast<InsetText *>(&inset) == in)
502 return const_cast<InsetCollapsable *>(this);
507 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
511 return inset.getFirstLockingInsetOfType(c);
515 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
516 bool toggleall, bool selectall)
518 inset.setFont(bv, font, toggleall, selectall);
522 bool InsetCollapsable::doClearArea() const
524 return inset.doClearArea();
528 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
529 bool const recursive) const
531 return inset.getLyXText(bv, recursive);
535 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
537 inset.deleteLyXText(bv, recursive);
541 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
543 inset.resizeLyXText(bv, force);
544 LyXFont font(LyXFont::ALL_SANE);
545 oldWidth = width(bv, font);
549 std::vector<string> const InsetCollapsable::getLabelList() const
551 return inset.getLabelList();
555 bool InsetCollapsable::nodraw() const
557 return inset.nodraw();
561 int InsetCollapsable::scroll(bool recursive) const
563 int sx = UpdatableInset::scroll(false);
566 sx += inset.scroll(recursive);
572 Paragraph * InsetCollapsable::getParFromID(int id) const
574 lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
575 return inset.getParFromID(id);
579 Paragraph * InsetCollapsable::firstParagraph() const
581 return inset.firstParagraph();
585 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
587 return inset.getFirstParagraph(i);
591 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
593 return inset.cursor(bv);
597 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
600 return const_cast<InsetCollapsable *>(this);
601 return inset.getInsetFromID(id_arg);
605 void InsetCollapsable::open(BufferView * bv)
607 if (!collapsed_) return;
610 bv->updateInset(this, false);
614 void InsetCollapsable::close(BufferView * bv) const
620 bv->updateInset(const_cast<InsetCollapsable *>(this), false);
624 void InsetCollapsable::setLabel(string const & l) const
630 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
631 bool const & cs, bool const & mw)
633 bool found = inset.searchForward(bv, str, cs, mw);
634 if (first_after_edit && !found)
636 first_after_edit = false;
641 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
642 bool const & cs, bool const & mw)
644 bool found = inset.searchBackward(bv, str, cs, mw);
645 if (first_after_edit && !found)
647 first_after_edit = false;
652 string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv,
655 string const str = inset.selectNextWordToSpellcheck(bv, value);
656 if (first_after_edit && str.empty())
658 first_after_edit = false;