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"
33 InsetCollapsable::InsetCollapsable()
36 inset = new InsetText;
37 inset->setOwner(this);
41 inset->SetAutoBreakRows(true);
42 inset->SetDrawFrame(0, InsetText::ALWAYS);
43 inset->SetFrameColor(0, LColor::footnoteframe);
44 button_length = button_top_y = button_bottom_y = 0;
45 setInsetName("Collapsable");
46 widthCollapsed = oldWidth = 0;
51 Inset * InsetCollapsable::Clone() const
53 InsetCollapsable * result = new InsetCollapsable();
54 result->inset->init(inset);
55 result->inset->setOwner(result);
57 result->collapsed = collapsed;
62 bool InsetCollapsable::InsertInset(BufferView * bv, Inset * in)
64 if (!InsertInsetAllowed(in)) {
65 lyxerr << "InsetCollapsable::InsertInset: "
66 "Unable to insert inset." << endl;
70 return inset->InsertInset(bv, in);
74 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
76 os << "collapsed " << tostr(collapsed) << "\n";
77 inset->WriteParagraphData(buf, os);
82 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
88 token = lex.GetString();
89 if (token == "collapsed") {
91 collapsed = lex.GetBool();
94 inset->Read(buf, lex);
98 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
100 int width = 0, ascent = 0, descent = 0;
101 pain.buttonText(0, 0, label.c_str(), labelfont, false,
102 width, ascent, descent);
107 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
109 int width = 0, ascent = 0, descent = 0;
110 pain.buttonText(0, 0, label.c_str(), labelfont, false,
111 width, ascent, descent);
116 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
118 int width, ascent, descent;
119 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
120 width, ascent, descent);
121 return width + (2*TEXT_TO_INSET_OFFSET);
125 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
128 return ascent_collapsed(bv->painter(), font);
130 return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
134 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
137 return descent_collapsed(bv->painter(), font);
139 return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
143 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
146 return widthCollapsed;
148 return inset->width(bv, font) + widthCollapsed;
152 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
153 int baseline, float & x) const
156 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
157 baseline, label.c_str(), labelfont, true, width);
158 x += width + TEXT_TO_INSET_OFFSET;
162 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
163 int baseline, float & x, bool cleared) const
165 Painter & pain = bv->painter();
167 button_length = widthCollapsed;
168 button_top_y = -ascent_collapsed(pain, f);
169 button_bottom_y = descent_collapsed(pain, f);
171 draw_collapsed(pain, f, baseline, x);
172 x += TEXT_TO_INSET_OFFSET;
176 if (!cleared && ((inset->need_update == InsetText::FULL) ||
177 (inset->need_update == InsetText::INIT) ||
178 (top_x!=int(x)) || (top_baseline!=baseline))) {
179 int w = owner()? width(bv, f) : pain.paperWidth();
180 int h = ascent(bv,f) + descent(bv, f);
181 int tx = (needFullRow() && !owner())? 0:int(x);
182 int ty = 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, bool fragile, bool free_spc) const
285 return inset->Latex(buf, os, fragile, free_spc);
289 int InsetCollapsable::getMaxWidth(Painter & pain,
290 UpdatableInset const * inset) const
293 w = UpdatableInset::getMaxWidth(pain,inset);
299 // should be at least 30 pixels !!!
302 return w; // - top_x - widthCollapsed;
306 int InsetCollapsable::getMaxTextWidth(Painter & pain,
307 UpdatableInset const * inset) const
309 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->deleteLyXText(bv);
327 owner()->update(bv, font);
331 if (oldWidth != width(bv, font)) {
332 oldWidth = width(bv, font);
333 inset->deleteLyXText(bv);
336 owner()->update(bv, font);
340 inset->update(bv, font);
343 UpdatableInset::RESULT
344 InsetCollapsable::LocalDispatch(BufferView * bv, int action, string const & arg)
346 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
347 if (result == FINISHED)
348 bv->unlockInset(this);
352 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
356 return inset->LockInsetInInset(bv, in);
360 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
364 bv->unlockInset(this);
367 return inset->UnlockInsetInInset(bv, in, lr);
371 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
375 return inset->UpdateInsetInInset(bv, in);
379 int InsetCollapsable::InsetInInsetY()
381 return inset->InsetInInsetY();
385 void InsetCollapsable::Validate(LaTeXFeatures & features) const
387 inset->Validate(features);
391 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
393 inset->GetCursorPos(bv, x , y);
397 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
399 inset->ToggleInsetCursor(bv);
403 UpdatableInset * InsetCollapsable::GetLockingInset()
405 UpdatableInset * in = inset->GetLockingInset();
412 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
416 return inset->GetFirstLockingInsetOfType(c);
420 void InsetCollapsable::SetFont(BufferView * bv,
421 LyXFont const & font, bool toggleall)
423 inset->SetFont(bv, font, toggleall);
426 bool InsetCollapsable::doClearArea() const
428 return inset->doClearArea();
432 LyXText * InsetCollapsable::getLyXText(BufferView * bv) const
434 return inset->getLyXText(bv);
438 void InsetCollapsable::deleteLyXText(BufferView * bv) const
440 inset->deleteLyXText(bv);