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();
190 pain.fillRectangle(tx, ty - 1, w, h + 2);
194 // not needed if collapsed
196 top_baseline = baseline;
198 draw_collapsed(pain, f, baseline, x);
199 inset->draw(bv, f, baseline, x, cleared);
204 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
206 UpdatableInset::Edit(bv, x, y, button);
208 if (collapsed && autocollapse) {
210 if (!bv->lockInset(this))
212 bv->updateInset(this, false);
213 inset->Edit(bv, 0, 0, button);
214 } else if (!collapsed) {
215 if (!bv->lockInset(this))
217 inset->Edit(bv, x-widthCollapsed, y, button);
222 Inset::EDITABLE InsetCollapsable::Editable() const
226 return HIGHLY_EDITABLE;
230 void InsetCollapsable::InsetUnlock(BufferView * bv)
235 inset->InsetUnlock(bv);
236 bv->updateInset(this, false);
240 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
242 if (!collapsed && (x >= button_length)) {
243 inset->InsetButtonPress(bv, x-widthCollapsed, y, button);
248 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
249 int x, int y, int button)
251 if ((x >= 0) && (x < button_length) &&
252 (y >= button_top_y) && (y < button_bottom_y)) {
255 inset->InsetButtonRelease(bv, 0, 0, button);
256 bv->updateInset(this, false);
259 bv->unlockInset(this);
260 bv->updateInset(this, false);
262 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
263 inset->InsetButtonRelease(bv, x-widthCollapsed, y, button);
268 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
269 int x, int y, int state)
271 if (x >= button_length) {
272 inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
277 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
279 inset->InsetKeyPress(xke);
283 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
284 bool fragile, bool free_spc) const
286 return inset->Latex(buf, os, fragile, free_spc);
290 int InsetCollapsable::getMaxWidth(Painter & pain,
291 UpdatableInset const * inset) const
293 int w = UpdatableInset::getMaxWidth(pain,inset);
296 // What does a negative max width signify? (Lgb)
299 // should be at least 30 pixels !!!
300 return max(30, w - widthCollapsed);
305 int InsetCollapsable::getMaxTextWidth(Painter & pain,
306 UpdatableInset const * inset) const
308 return getMaxWidth(pain, inset) - widthCollapsed;
313 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
319 owner()->update(bv, font, true);
322 if (!widthCollapsed) {
323 widthCollapsed = width_collapsed(bv->painter(), font);
324 inset->resizeLyXText(bv);
327 owner()->update(bv, font);
331 if (oldWidth != width(bv, font)) {
332 oldWidth = width(bv, font);
333 inset->resizeLyXText(bv);
336 owner()->update(bv, font);
340 inset->update(bv, font);
344 UpdatableInset::RESULT
345 InsetCollapsable::LocalDispatch(BufferView * bv, int action,
348 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
349 if (result == FINISHED)
350 bv->unlockInset(this);
355 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
359 return inset->LockInsetInInset(bv, in);
363 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
367 bv->unlockInset(this);
370 return inset->UnlockInsetInInset(bv, in, lr);
374 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
378 return inset->UpdateInsetInInset(bv, in);
382 unsigned int InsetCollapsable::InsetInInsetY()
384 return inset->InsetInInsetY();
388 void InsetCollapsable::Validate(LaTeXFeatures & features) const
390 inset->Validate(features);
394 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
396 inset->GetCursorPos(bv, x , y);
400 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
402 inset->ToggleInsetCursor(bv);
406 UpdatableInset * InsetCollapsable::GetLockingInset()
408 UpdatableInset * in = inset->GetLockingInset();
415 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
419 return inset->GetFirstLockingInsetOfType(c);
423 void InsetCollapsable::SetFont(BufferView * bv,
424 LyXFont const & font, bool toggleall)
426 inset->SetFont(bv, font, toggleall);
430 bool InsetCollapsable::doClearArea() const
432 return inset->doClearArea();
436 LyXText * InsetCollapsable::getLyXText(BufferView * bv) const
438 return inset->getLyXText(bv);
442 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
444 inset->deleteLyXText(bv, recursive);
448 void InsetCollapsable::resizeLyXText(BufferView * bv) const
450 inset->resizeLyXText(bv);
451 LyXFont font(LyXFont::ALL_SANE);
452 oldWidth = width(bv, font);