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"
30 InsetCollapsable::InsetCollapsable()
33 inset = new InsetText;
34 inset->setOwner(this);
38 inset->SetAutoBreakRows(true);
39 inset->SetDrawFrame(0, InsetText::ALWAYS);
40 inset->SetFrameColor(0, LColor::footnoteframe);
41 button_length = button_top_y = button_bottom_y = 0;
42 setInsetName("Collapsable");
43 widthCollapsed = oldWidth = 0;
48 Inset * InsetCollapsable::Clone() const
50 InsetCollapsable * result = new InsetCollapsable();
51 result->inset->init(inset);
52 result->inset->setOwner(result);
54 result->collapsed = collapsed;
59 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
61 os << "collapsed " << tostr(collapsed) << "\n";
62 inset->WriteParagraphData(buf, os);
67 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
71 string token = lex.GetString();
72 if (token == "collapsed") {
74 collapsed = lex.GetBool();
77 inset->Read(buf, lex);
81 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
83 int width = 0, ascent = 0, descent = 0;
84 pain.buttonText(0, 0, label.c_str(), labelfont, false,
85 width, ascent, descent);
90 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
92 int width = 0, ascent = 0, descent = 0;
93 pain.buttonText(0, 0, label.c_str(), labelfont, false,
94 width, ascent, descent);
99 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
101 int width, ascent, descent;
102 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
103 width, ascent, descent);
104 return width + (2*TEXT_TO_INSET_OFFSET);
108 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
111 return ascent_collapsed(pain, font);
113 return inset->ascent(pain, font) + TEXT_TO_TOP_OFFSET;
117 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
120 return descent_collapsed(pain, font);
122 return inset->descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
126 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
129 return widthCollapsed;
131 return inset->width(pain, font) + widthCollapsed;
135 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
136 int baseline, float & x) const
139 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
140 baseline, label.c_str(), labelfont, true, width);
141 x += width + TEXT_TO_INSET_OFFSET;
145 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
146 int baseline, float & x, bool cleared) const
148 Painter & pain = bv->painter();
150 button_length = widthCollapsed;
151 button_top_y = -ascent_collapsed(pain, f);
152 button_bottom_y = descent_collapsed(pain, f);
154 draw_collapsed(pain, f, baseline, x);
155 x += TEXT_TO_INSET_OFFSET;
159 if (!cleared && ((need_update == FULL) ||
160 (top_x!=int(x)) || (top_baseline!=baseline))) {
161 int w = owner()? width(pain, f) : pain.paperWidth();
162 int h = ascent(pain,f) + descent(pain, f);
163 int tx = (display() && !owner())? 0:int(x);
164 int ty = baseline - ascent(pain,f);
168 if ((ty + h) > pain.paperHeight())
169 h = pain.paperHeight();
170 if ((top_x + w) > pain.paperWidth())
171 w = pain.paperWidth();
172 pain.fillRectangle(tx, ty-1, w, h+2);
176 // not needed if collapsed
178 top_baseline = baseline;
180 draw_collapsed(pain, f, baseline, x);
181 inset->draw(bv, f, baseline, x, cleared);
186 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
188 UpdatableInset::Edit(bv, x, y, button);
190 if (collapsed && autocollapse) {
192 if (!bv->lockInset(this))
194 bv->updateInset(this, false);
195 inset->Edit(bv, 0, 0, button);
196 } else if (!collapsed) {
197 if (!bv->lockInset(this))
199 inset->Edit(bv, x-widthCollapsed, y, button);
204 Inset::EDITABLE InsetCollapsable::Editable() const
208 return HIGHLY_EDITABLE;
212 void InsetCollapsable::InsetUnlock(BufferView * bv)
217 inset->InsetUnlock(bv);
218 bv->updateInset(this, false);
222 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
224 if (!collapsed && (x >= button_length)) {
225 inset->InsetButtonPress(bv, x-widthCollapsed, y, button);
230 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
231 int x, int y, int button)
233 if ((x >= 0) && (x < button_length) &&
234 (y >= button_top_y) && (y < button_bottom_y)) {
237 inset->InsetButtonRelease(bv, 0, 0, button);
238 bv->updateInset(this, false);
241 bv->unlockInset(this);
242 bv->updateInset(this, false);
244 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
245 inset->InsetButtonRelease(bv, x-widthCollapsed, y, button);
250 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
251 int x, int y, int state)
253 if (x >= button_length) {
254 inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
259 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
261 inset->InsetKeyPress(xke);
265 int InsetCollapsable::Latex(Buffer const * buf, ostream & os, bool fragile, bool free_spc) const
267 return inset->Latex(buf, os, fragile, free_spc);
271 int InsetCollapsable::getMaxWidth(Painter & pain,
272 UpdatableInset const * inset) const
276 w = static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
278 w = pain.paperWidth();
283 return w; // - top_x - widthCollapsed;
287 int InsetCollapsable::getMaxTextWidth(Painter & pain,
288 UpdatableInset const * inset) const
290 return getMaxWidth(pain, inset) - widthCollapsed;
294 void InsetCollapsable::update(BufferView * bv,
295 LyXFont const & font, bool dodraw)
297 if (!widthCollapsed) {
298 widthCollapsed = width_collapsed(bv->painter(), font);
299 inset->deleteLyXText(bv);
302 owner()->update(bv, font, dodraw);
306 if (oldWidth != width(bv->painter(), font)) {
307 oldWidth = width(bv->painter(), font);
308 inset->deleteLyXText(bv);
311 owner()->update(bv, font, dodraw);
315 inset->update(bv, font, dodraw);
318 UpdatableInset::RESULT
319 InsetCollapsable::LocalDispatch(BufferView * bv, int action, string const & arg)
321 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
322 if (result == FINISHED)
323 bv->unlockInset(this);
327 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
331 return inset->LockInsetInInset(bv, in);
335 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
339 bv->unlockInset(this);
342 return inset->UnlockInsetInInset(bv, in, lr);
346 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
350 return inset->UpdateInsetInInset(bv, in);
354 int InsetCollapsable::InsetInInsetY()
356 return inset->InsetInInsetY();
360 void InsetCollapsable::Validate(LaTeXFeatures & features) const
362 inset->Validate(features);
366 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
368 inset->GetCursorPos(bv, x , y);
372 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
374 inset->ToggleInsetCursor(bv);
378 UpdatableInset * InsetCollapsable::GetLockingInset()
380 UpdatableInset *in = inset->GetLockingInset();
387 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
391 return inset->GetFirstLockingInsetOfType(c);
395 void InsetCollapsable::SetFont(BufferView * bv, LyXFont const & font, bool toggleall)
397 inset->SetFont(bv, font, toggleall);
400 bool InsetCollapsable::doClearArea() const
402 return inset->doClearArea();
406 LyXText * InsetCollapsable::getLyXText(BufferView * bv) const
408 return inset->getLyXText(bv);
412 void InsetCollapsable::deleteLyXText(BufferView * bv)
414 inset->deleteLyXText(bv);