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"
34 InsetCollapsable::InsetCollapsable()
35 : UpdatableInset(), inset(new InsetText)
37 //inset = new InsetText;
38 inset->setOwner(this);
42 inset->SetAutoBreakRows(true);
43 inset->SetDrawFrame(0, InsetText::ALWAYS);
44 inset->SetFrameColor(0, LColor::footnoteframe);
45 button_length = button_top_y = button_bottom_y = 0;
46 setInsetName("Collapsable");
47 widthCollapsed = oldWidth = 0;
52 Inset * InsetCollapsable::Clone(Buffer const &) const
54 InsetCollapsable * result = new InsetCollapsable();
55 result->inset->init(inset);
56 result->inset->setOwner(result);
58 result->collapsed = collapsed;
63 bool InsetCollapsable::InsertInset(BufferView * bv, Inset * in)
65 if (!InsertInsetAllowed(in)) {
66 lyxerr << "InsetCollapsable::InsertInset: "
67 "Unable to insert inset." << endl;
71 return inset->InsertInset(bv, in);
75 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
77 os << "collapsed " << tostr(collapsed) << "\n";
78 inset->WriteParagraphData(buf, os);
83 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
87 string const token = lex.GetString();
88 if (token == "collapsed") {
90 collapsed = lex.GetBool();
92 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
96 inset->Read(buf, lex);
100 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
105 pain.buttonText(0, 0, label, labelfont, false,
106 width, ascent, descent);
111 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
116 pain.buttonText(0, 0, label, labelfont, false,
117 width, ascent, descent);
122 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
127 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label, labelfont, false,
128 width, ascent, descent);
129 return width + (2*TEXT_TO_INSET_OFFSET);
133 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
136 return ascent_collapsed(bv->painter(), font);
138 return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
142 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
145 return descent_collapsed(bv->painter(), font);
147 return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
151 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
154 return widthCollapsed;
156 return inset->width(bv, font) + widthCollapsed;
160 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
161 int baseline, float & x) const
164 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
165 baseline, label, labelfont, true, width);
166 x += width + TEXT_TO_INSET_OFFSET;
170 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
171 int baseline, float & x, bool cleared) const
173 Painter & pain = bv->painter();
175 button_length = widthCollapsed;
176 button_top_y = -ascent_collapsed(pain, f);
177 button_bottom_y = descent_collapsed(pain, f);
179 draw_collapsed(pain, f, baseline, x);
180 x += TEXT_TO_INSET_OFFSET;
184 if (!cleared && ((inset->need_update == InsetText::FULL) ||
185 (inset->need_update == InsetText::INIT) ||
186 (top_x != int(x)) || (top_baseline != baseline))) {
187 int w = owner() ? width(bv, f) : pain.paperWidth();
188 int h = ascent(bv, f) + descent(bv, f);
189 int tx = (needFullRow() && !owner()) ? 0 : int(x);
190 int ty = max(0, baseline - ascent(bv, f));
192 if ((ty + h) > pain.paperHeight())
193 h = pain.paperHeight();
194 if ((top_x + w) > pain.paperWidth())
195 w = pain.paperWidth();
197 h += (baseline - ascent(bv, f));
198 pain.fillRectangle(tx, ty - 1, w, h + 2);
203 top_baseline = baseline;
205 draw_collapsed(pain, f,
206 baseline - ascent(bv, f) + ascent_collapsed(pain, f),
209 baseline - ascent(bv, f) + ascent_collapsed(pain, f),
215 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
217 UpdatableInset::Edit(bv, x, y, button);
219 if (collapsed && autocollapse) {
221 if (!bv->lockInset(this))
223 bv->updateInset(this, false);
224 inset->Edit(bv, 0, 0, button);
225 } else if (!collapsed) {
226 if (!bv->lockInset(this))
228 inset->Edit(bv, x - widthCollapsed, y, button);
233 Inset::EDITABLE InsetCollapsable::Editable() const
237 return HIGHLY_EDITABLE;
241 void InsetCollapsable::InsetUnlock(BufferView * bv)
246 inset->InsetUnlock(bv);
247 bv->updateInset(this, false);
251 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
253 if (!collapsed && (x >= button_length)) {
254 inset->InsetButtonPress(bv, x - widthCollapsed, y, button);
259 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
260 int x, int y, int button)
262 if ((x >= 0) && (x < button_length) &&
263 (y >= button_top_y) && (y < button_bottom_y)) {
266 inset->InsetButtonRelease(bv, 0, 0, button);
267 bv->updateInset(this, false);
270 bv->unlockInset(this);
271 bv->updateInset(this, false);
273 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
274 inset->InsetButtonRelease(bv, x - widthCollapsed, y, button);
279 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
280 int x, int y, int state)
282 if (x >= button_length) {
283 inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
288 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
290 inset->InsetKeyPress(xke);
294 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
295 bool fragile, bool free_spc) const
297 return inset->Latex(buf, os, fragile, free_spc);
301 int InsetCollapsable::getMaxWidth(Painter & pain,
302 UpdatableInset const * inset) const
304 int const w = UpdatableInset::getMaxWidth(pain, inset);
307 // What does a negative max width signify? (Lgb)
310 // should be at least 30 pixels !!!
311 return max(30, w - widthCollapsed);
316 int InsetCollapsable::getMaxTextWidth(Painter & pain,
317 UpdatableInset const * inset) const
319 return getMaxWidth(pain, inset) - widthCollapsed;
324 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
330 owner()->update(bv, font, true);
333 if (!widthCollapsed) {
334 widthCollapsed = width_collapsed(bv->painter(), font);
335 inset->resizeLyXText(bv);
338 owner()->update(bv, font);
342 if (oldWidth != width(bv, font)) {
343 oldWidth = width(bv, font);
344 inset->resizeLyXText(bv);
347 owner()->update(bv, font);
351 inset->update(bv, font);
355 UpdatableInset::RESULT
356 InsetCollapsable::LocalDispatch(BufferView * bv, kb_action action,
359 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
360 if (result == FINISHED)
361 bv->unlockInset(this);
366 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
370 return inset->LockInsetInInset(bv, in);
374 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
378 bv->unlockInset(this);
381 return inset->UnlockInsetInInset(bv, in, lr);
385 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
389 return inset->UpdateInsetInInset(bv, in);
393 unsigned int InsetCollapsable::InsetInInsetY()
395 return inset->InsetInInsetY() - (top_baseline - inset->y());
399 void InsetCollapsable::Validate(LaTeXFeatures & features) const
401 inset->Validate(features);
405 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
407 inset->GetCursorPos(bv, x , y);
411 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
413 inset->ToggleInsetCursor(bv);
417 UpdatableInset * InsetCollapsable::GetLockingInset()
419 UpdatableInset * in = inset->GetLockingInset();
426 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
430 return inset->GetFirstLockingInsetOfType(c);
434 void InsetCollapsable::SetFont(BufferView * bv,
435 LyXFont const & font, bool toggleall)
437 inset->SetFont(bv, font, toggleall);
441 bool InsetCollapsable::doClearArea() const
443 return inset->doClearArea();
447 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
448 bool const recursive) const
450 return inset->getLyXText(bv, recursive);
454 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
456 inset->deleteLyXText(bv, recursive);
460 void InsetCollapsable::resizeLyXText(BufferView * bv) const
462 inset->resizeLyXText(bv);
463 LyXFont font(LyXFont::ALL_SANE);
464 oldWidth = width(bv, font);