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"
32 InsetCollapsable::InsetCollapsable()
35 inset = new InsetText;
36 inset->setOwner(this);
40 inset->SetAutoBreakRows(true);
41 inset->SetDrawFrame(0, InsetText::ALWAYS);
42 inset->SetFrameColor(0, LColor::footnoteframe);
43 button_length = button_top_y = button_bottom_y = 0;
44 setInsetName("Collapsable");
45 widthCollapsed = oldWidth = 0;
50 Inset * InsetCollapsable::Clone() const
52 InsetCollapsable * result = new InsetCollapsable();
53 result->inset->init(inset);
54 result->inset->setOwner(result);
56 result->collapsed = collapsed;
61 bool InsetCollapsable::InsertInset(BufferView * bv, Inset * in)
63 if (!InsertInsetAllowed(in)) {
64 lyxerr << "InsetCollapsable::InsertInset: "
65 "Unable to insert inset." << endl;
69 return inset->InsertInset(bv, in);
73 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
75 os << "collapsed " << tostr(collapsed) << "\n";
76 inset->WriteParagraphData(buf, os);
81 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
87 token = lex.GetString();
88 if (token == "collapsed") {
90 collapsed = lex.GetBool();
93 inset->Read(buf, lex);
97 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
99 int width = 0, ascent = 0, descent = 0;
100 pain.buttonText(0, 0, label.c_str(), labelfont, false,
101 width, ascent, descent);
106 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
108 int width = 0, ascent = 0, descent = 0;
109 pain.buttonText(0, 0, label.c_str(), labelfont, false,
110 width, ascent, descent);
115 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
117 int width, ascent, descent;
118 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
119 width, ascent, descent);
120 return width + (2*TEXT_TO_INSET_OFFSET);
124 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
127 return ascent_collapsed(bv->painter(), font);
129 return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
133 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
136 return descent_collapsed(bv->painter(), font);
138 return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
142 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
145 return widthCollapsed;
147 return inset->width(bv, font) + widthCollapsed;
151 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
152 int baseline, float & x) const
155 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
156 baseline, label.c_str(), labelfont, true, width);
157 x += width + TEXT_TO_INSET_OFFSET;
161 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
162 int baseline, float & x, bool cleared) const
164 Painter & pain = bv->painter();
166 button_length = widthCollapsed;
167 button_top_y = -ascent_collapsed(pain, f);
168 button_bottom_y = descent_collapsed(pain, f);
170 draw_collapsed(pain, f, baseline, x);
171 x += TEXT_TO_INSET_OFFSET;
175 if (!cleared && ((inset->need_update == InsetText::FULL) ||
176 (inset->need_update == InsetText::INIT) ||
177 (top_x!=int(x)) || (top_baseline!=baseline))) {
178 int w = owner()? width(bv, f) : pain.paperWidth();
179 int h = ascent(bv,f) + descent(bv, f);
180 int tx = (needFullRow() && !owner())? 0:int(x);
181 int ty = baseline - ascent(bv,f);
185 if ((ty + h) > pain.paperHeight())
186 h = pain.paperHeight();
187 if ((top_x + w) > pain.paperWidth())
188 w = pain.paperWidth();
189 pain.fillRectangle(tx, ty-1, w, h+2);
193 // not needed if collapsed
195 top_baseline = baseline;
197 draw_collapsed(pain, f, baseline, x);
198 inset->draw(bv, f, baseline, x, cleared);
203 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
205 UpdatableInset::Edit(bv, x, y, button);
207 if (collapsed && autocollapse) {
209 if (!bv->lockInset(this))
211 bv->updateInset(this, false);
212 inset->Edit(bv, 0, 0, button);
213 } else if (!collapsed) {
214 if (!bv->lockInset(this))
216 inset->Edit(bv, x-widthCollapsed, y, button);
221 Inset::EDITABLE InsetCollapsable::Editable() const
225 return HIGHLY_EDITABLE;
229 void InsetCollapsable::InsetUnlock(BufferView * bv)
234 inset->InsetUnlock(bv);
235 bv->updateInset(this, false);
239 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
241 if (!collapsed && (x >= button_length)) {
242 inset->InsetButtonPress(bv, x-widthCollapsed, y, button);
247 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
248 int x, int y, int button)
250 if ((x >= 0) && (x < button_length) &&
251 (y >= button_top_y) && (y < button_bottom_y)) {
254 inset->InsetButtonRelease(bv, 0, 0, button);
255 bv->updateInset(this, false);
258 bv->unlockInset(this);
259 bv->updateInset(this, false);
261 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
262 inset->InsetButtonRelease(bv, x-widthCollapsed, y, button);
267 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
268 int x, int y, int state)
270 if (x >= button_length) {
271 inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
276 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
278 inset->InsetKeyPress(xke);
282 int InsetCollapsable::Latex(Buffer const * buf, ostream & os, bool fragile, bool free_spc) const
284 return inset->Latex(buf, os, fragile, free_spc);
288 int InsetCollapsable::getMaxWidth(Painter & pain,
289 UpdatableInset const * inset) const
292 w = UpdatableInset::getMaxWidth(pain,inset);
298 // should be at least 30 pixels !!!
301 return w; // - top_x - widthCollapsed;
305 int InsetCollapsable::getMaxTextWidth(Painter & pain,
306 UpdatableInset const * inset) const
308 return getMaxWidth(pain, inset) - widthCollapsed;
312 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
315 if (!widthCollapsed) {
316 widthCollapsed = width_collapsed(bv->painter(), font);
317 inset->deleteLyXText(bv);
320 owner()->update(bv, font, dodraw);
324 if (oldWidth != width(bv, font)) {
325 oldWidth = width(bv, font);
326 inset->deleteLyXText(bv);
329 owner()->update(bv, font, dodraw);
333 inset->update(bv, font, dodraw);
336 UpdatableInset::RESULT
337 InsetCollapsable::LocalDispatch(BufferView * bv, int action, string const & arg)
339 UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
340 if (result == FINISHED)
341 bv->unlockInset(this);
345 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
349 return inset->LockInsetInInset(bv, in);
353 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
357 bv->unlockInset(this);
360 return inset->UnlockInsetInInset(bv, in, lr);
364 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
368 return inset->UpdateInsetInInset(bv, in);
372 int InsetCollapsable::InsetInInsetY()
374 return inset->InsetInInsetY();
378 void InsetCollapsable::Validate(LaTeXFeatures & features) const
380 inset->Validate(features);
384 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
386 inset->GetCursorPos(bv, x , y);
390 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
392 inset->ToggleInsetCursor(bv);
396 UpdatableInset * InsetCollapsable::GetLockingInset()
398 UpdatableInset * in = inset->GetLockingInset();
405 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
409 return inset->GetFirstLockingInsetOfType(c);
413 void InsetCollapsable::SetFont(BufferView * bv,
414 LyXFont const & font, bool toggleall)
416 inset->SetFont(bv, font, toggleall);
419 bool InsetCollapsable::doClearArea() const
421 return inset->doClearArea();
425 LyXText * InsetCollapsable::getLyXText(BufferView * bv) const
427 return inset->getLyXText(bv);
431 void InsetCollapsable::deleteLyXText(BufferView * bv)
433 inset->deleteLyXText(bv);