1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1998 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()
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 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
102 int width = 0, ascent = 0, descent = 0;
103 pain.buttonText(0, 0, label, labelfont, false,
104 width, ascent, descent);
109 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
111 int width = 0, ascent = 0, descent = 0;
112 pain.buttonText(0, 0, label, labelfont, false,
113 width, ascent, descent);
118 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
120 int width, ascent, descent;
121 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label, labelfont, false,
122 width, ascent, descent);
123 return width + (2*TEXT_TO_INSET_OFFSET);
127 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
130 return ascent_collapsed(bv->painter(), font);
132 return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
136 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
139 return descent_collapsed(bv->painter(), font);
141 return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
145 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
148 return widthCollapsed;
150 return inset->width(bv, font) + widthCollapsed;
154 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
155 int baseline, float & x) const
158 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
159 baseline, label, labelfont, true, width);
160 x += width + TEXT_TO_INSET_OFFSET;
164 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
165 int baseline, float & x, bool cleared) const
167 Painter & pain = bv->painter();
169 button_length = widthCollapsed;
170 button_top_y = -ascent_collapsed(pain, f);
171 button_bottom_y = descent_collapsed(pain, f);
173 draw_collapsed(pain, f, baseline, x);
174 x += TEXT_TO_INSET_OFFSET;
178 if (!cleared && ((inset->need_update == InsetText::FULL) ||
179 (inset->need_update == InsetText::INIT) ||
180 (top_x!=int(x)) || (top_baseline!=baseline))) {
181 int w = owner() ? width(bv, f) : pain.paperWidth();
182 int h = ascent(bv, f) + descent(bv, f);
183 int tx = (needFullRow() && !owner()) ? 0 : int(x);
184 int ty = max(0, baseline - ascent(bv, f));
186 if ((ty + h) > pain.paperHeight())
187 h = pain.paperHeight();
188 if ((top_x + w) > pain.paperWidth())
189 w = pain.paperWidth();
191 h += (baseline - ascent(bv, f));
192 pain.fillRectangle(tx, ty - 1, w, h + 2);
196 // not needed if collapsed
198 top_baseline = baseline;
200 draw_collapsed(pain, f, baseline, x);
201 inset->draw(bv, f, baseline, x, cleared);
206 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
208 UpdatableInset::Edit(bv, x, y, button);
210 if (collapsed && autocollapse) {
212 if (!bv->lockInset(this))
214 bv->updateInset(this, false);
215 inset->Edit(bv, 0, 0, button);
216 } else if (!collapsed) {
217 if (!bv->lockInset(this))
219 inset->Edit(bv, x-widthCollapsed, y, button);
224 Inset::EDITABLE InsetCollapsable::Editable() const
228 return HIGHLY_EDITABLE;
232 void InsetCollapsable::InsetUnlock(BufferView * bv)
237 inset->InsetUnlock(bv);
238 bv->updateInset(this, false);
242 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
244 if (!collapsed && (x >= button_length)) {
245 inset->InsetButtonPress(bv, x-widthCollapsed, y, button);
250 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
251 int x, int y, int button)
253 if ((x >= 0) && (x < button_length) &&
254 (y >= button_top_y) && (y < button_bottom_y)) {
257 inset->InsetButtonRelease(bv, 0, 0, button);
258 bv->updateInset(this, false);
261 bv->unlockInset(this);
262 bv->updateInset(this, false);
264 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
265 inset->InsetButtonRelease(bv, x-widthCollapsed, y, button);
270 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
271 int x, int y, int state)
273 if (x >= button_length) {
274 inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
279 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
281 inset->InsetKeyPress(xke);
285 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
286 bool fragile, bool free_spc) const
288 return inset->Latex(buf, os, fragile, free_spc);
292 int InsetCollapsable::getMaxWidth(Painter & pain,
293 UpdatableInset const * inset) const
295 int w = UpdatableInset::getMaxWidth(pain,inset);
298 // What does a negative max width signify? (Lgb)
301 // should be at least 30 pixels !!!
302 return max(30, w - widthCollapsed);
307 int InsetCollapsable::getMaxTextWidth(Painter & pain,
308 UpdatableInset const * inset) const
310 return getMaxWidth(pain, inset) - widthCollapsed;
315 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
321 owner()->update(bv, font, true);
324 if (!widthCollapsed) {
325 widthCollapsed = width_collapsed(bv->painter(), font);
326 inset->resizeLyXText(bv);
329 owner()->update(bv, font);
333 if (oldWidth != width(bv, font)) {
334 oldWidth = width(bv, font);
335 inset->resizeLyXText(bv);
338 owner()->update(bv, font);
342 inset->update(bv, font);
346 UpdatableInset::RESULT
347 InsetCollapsable::LocalDispatch(BufferView * bv, int action,
350 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
351 if (result == FINISHED)
352 bv->unlockInset(this);
357 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
361 return inset->LockInsetInInset(bv, in);
365 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
369 bv->unlockInset(this);
372 return inset->UnlockInsetInInset(bv, in, lr);
376 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
380 return inset->UpdateInsetInInset(bv, in);
384 unsigned int InsetCollapsable::InsetInInsetY()
386 return inset->InsetInInsetY();
390 void InsetCollapsable::Validate(LaTeXFeatures & features) const
392 inset->Validate(features);
396 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
398 inset->GetCursorPos(bv, x , y);
402 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
404 inset->ToggleInsetCursor(bv);
408 UpdatableInset * InsetCollapsable::GetLockingInset()
410 UpdatableInset * in = inset->GetLockingInset();
417 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
421 return inset->GetFirstLockingInsetOfType(c);
425 void InsetCollapsable::SetFont(BufferView * bv,
426 LyXFont const & font, bool toggleall)
428 inset->SetFont(bv, font, toggleall);
432 bool InsetCollapsable::doClearArea() const
434 return inset->doClearArea();
438 LyXText * InsetCollapsable::getLyXText(BufferView const * bv) const
440 return inset->getLyXText(bv);
444 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
446 inset->deleteLyXText(bv, recursive);
450 void InsetCollapsable::resizeLyXText(BufferView * bv) const
452 inset->resizeLyXText(bv);
453 LyXFont font(LyXFont::ALL_SANE);
454 oldWidth = width(bv, font);