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 "support/LOstream.h"
23 #include "support/lstrings.h"
27 InsetCollapsable::InsetCollapsable()
34 framecolor = LColor::footnoteframe;
35 widthOffset = 2 * TEXT_TO_INSET_OFFSET + 2; // 1+1 (frame)
36 button_length = button_top_y = button_bottom_y = 0;
37 setInsetName("Collapsable");
41 Inset * InsetCollapsable::Clone() const
43 InsetCollapsable * result = new InsetCollapsable();
46 result->collapsed = collapsed;
51 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
53 os << "collapsed " << tostr(!display()) << "\n";
54 WriteParagraphData(buf, os);
58 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
62 string token = lex.GetString();
63 if (token == "collapsed") {
65 collapsed = lex.GetBool();
68 InsetText::Read(buf, lex);
72 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
74 int width = 0, ascent = 0, descent = 0;
75 pain.buttonText(0, 0, label.c_str(), labelfont, false,
76 width, ascent, descent);
81 int InsetCollapsable::descent_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::width_collapsed(Painter & pain, LyXFont const &) const
92 int width, ascent, descent;
93 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
94 width, ascent, descent);
95 return width + (2*TEXT_TO_INSET_OFFSET);
99 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
102 return ascent_collapsed(pain, font);
104 return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
108 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
111 return descent_collapsed(pain, font);
113 return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
117 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
120 return width_collapsed(pain, font);
122 return InsetText::width(pain, font) + width_collapsed(pain,font) +
127 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
128 int baseline, float & x) const
131 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
132 baseline-ascent(pain, font)+ascent_collapsed(pain, font),
133 label.c_str(), labelfont, true, width);
134 x += width + (2 * TEXT_TO_INSET_OFFSET);
138 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f,
139 int baseline, float & x, bool cleared) const
141 Painter & pain = bv->painter();
143 button_length = width_collapsed(pain, labelfont) + 2;
144 button_top_y = -ascent_collapsed(pain, f);
145 button_bottom_y = descent_collapsed(pain, f);
147 draw_collapsed(pain, f, baseline, x);
151 if (!cleared && ((need_update==FULL) || (topx!=int(x)) ||
152 (topbaseline!=baseline))) {
153 int w = width(pain, f);
154 int h = ascent(pain,f) + descent(pain, f);
155 int tx = display()? 0:topx;
156 int ty = baseline - ascent(pain,f);
160 if ((ty + h) > pain.paperHeight())
161 h = pain.paperHeight();
162 if ((topx + w) > pain.paperWidth())
163 w = pain.paperWidth();
164 pain.fillRectangle(tx, ty-1, w, h+2);
168 // not needed if collapsed
170 topbaseline = baseline;
172 int w = InsetText::width(pain, f) + (2 * TEXT_TO_INSET_OFFSET);
173 int h = ascent(pain,f) + descent(pain, f);
175 draw_collapsed(pain, f, baseline, x);
176 x -= TEXT_TO_INSET_OFFSET;
178 int save_x = static_cast<int>(x);
179 x += TEXT_TO_INSET_OFFSET;
180 InsetText::draw(bv, f, baseline, x, cleared);
181 pain.rectangle(save_x, baseline - ascent(pain, f), w, h, framecolor);
185 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
187 if (collapsed && autocollapse) {
189 UpdateLocal(bv, FULL, false);
190 InsetText::Edit(bv, 0, 0, button);
191 } else if (!collapsed) {
192 InsetText::Edit(bv, x, y, button);
197 Inset::EDITABLE InsetCollapsable::Editable() const
201 return HIGHLY_EDITABLE;
205 void InsetCollapsable::InsetUnlock(BufferView * bv)
210 InsetText::InsetUnlock(bv);
211 UpdateLocal(bv, FULL, false);
215 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
217 if (!collapsed && (x >= button_length)) {
218 InsetText::InsetButtonPress(bv, x, y, button);
223 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
224 int x, int y, int button)
226 if ((x >= 0) && (x < button_length) &&
227 (y >= button_top_y) && (y < button_bottom_y)) {
230 InsetText::InsetButtonRelease(bv, 0, 0, button);
231 UpdateLocal(bv, FULL, false);
234 UpdateLocal(bv, FULL, false);
235 bv->unlockInset(this);
237 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
238 InsetText::InsetButtonRelease(bv, x, y, button);
243 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
244 int x, int y, int state)
246 if (x >= button_length) {
247 InsetText::InsetMotionNotify(bv, x, y, state);
252 int InsetCollapsable::getMaxWidth(Painter & pain,
253 UpdatableInset const * inset) const
255 if ((this == inset) && !owner())
256 return pain.paperWidth();
258 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
260 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
261 width_collapsed(pain, labelfont) - widthOffset;
263 return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
267 int InsetCollapsable::getMaxTextWidth(Painter & pain,
268 UpdatableInset const * inset) const
270 return getMaxWidth(pain, inset) -
271 width_collapsed(pain, labelfont) - widthOffset;
275 void InsetCollapsable::update(BufferView * bv,
276 LyXFont const & font, bool dodraw)
278 drawTextXOffset = width_collapsed(bv->painter(), font);
279 InsetText::update(bv, font, dodraw);