1 /* This file is part of
2 * ======================================================
4 * LyX, The Document Processor
6 * Copyright (C) 1998 The LyX Team.
8 *======================================================*/
13 #pragma implementation
16 #include "insetcollapsable.h"
19 #include "BufferView.h"
21 #include "support/LOstream.h"
25 InsetCollapsable::InsetCollapsable(Buffer * bf)
32 framecolor = LColor::footnoteframe;
33 widthOffset = 6; // 2+2 (behind+back), 1+1 (frame)
34 button_length = button_top_y = button_bottom_y = 0;
35 setInsetName("Collapsable");
39 Inset * InsetCollapsable::Clone() const
41 InsetCollapsable * result = new InsetCollapsable(buffer);
42 result->init(buffer, this);
44 result->collapsed = collapsed;
48 void InsetCollapsable::Write(ostream & os) const
50 os << getInsetName() << "\n\ncollapsed ";
55 WriteParagraphData(os);
59 void InsetCollapsable::Read(LyXLex & lex)
63 string token = lex.GetString();
64 if (token == "collapsed") {
66 collapsed = lex.GetBool();
73 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
75 int width = 0, ascent = 0, descent = 0;
76 pain.buttonText(0, 0, label.c_str(), labelfont, false,
77 width, ascent, descent);
82 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
84 int width = 0, ascent = 0, descent = 0;
85 pain.buttonText(0, 0, label.c_str(), labelfont, false,
86 width, ascent, descent);
91 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
93 int width, ascent, descent;
94 pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label.c_str(), labelfont, false,
95 width, ascent, descent);
96 return width + (2*TEXT_TO_INSET_OFFSET);
100 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
103 return ascent_collapsed(pain, font);
105 return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
109 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
112 return descent_collapsed(pain, font);
114 return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
118 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
121 return width_collapsed(pain, font);
123 return InsetText::width(pain, font) + width_collapsed(pain,font) +
128 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
129 int baseline, float & x) const
132 pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
133 baseline-ascent(pain, font)+ascent_collapsed(pain, font),
134 label.c_str(), labelfont, true, width);
135 x += width + (2 * TEXT_TO_INSET_OFFSET);
139 void InsetCollapsable::draw(Painter & pain, LyXFont const & f,
140 int baseline, float & x) const
142 button_length = width_collapsed(pain, labelfont) + 2;
143 button_top_y = -ascent_collapsed(pain, f);
144 button_bottom_y = descent_collapsed(pain, f);
146 top_baseline = baseline;
148 draw_collapsed(pain, f, baseline, x);
152 draw_collapsed(pain, f, baseline, x);
153 x -= TEXT_TO_INSET_OFFSET;
155 int w = InsetText::width(pain, f) + TEXT_TO_INSET_OFFSET;
156 int h = ascent(pain,f) + descent(pain, f);
158 pain.rectangle(int(x), baseline - ascent(pain, f), w, h, framecolor);
160 x += TEXT_TO_INSET_OFFSET;
161 drawTextXOffset = int(x) - top_x;
162 InsetText::draw(pain, f, baseline, x);
166 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
168 if (collapsed && autocollapse) {
170 UpdateLocal(bv, true);
171 InsetText::Edit(bv, 0, 0, button);
172 } else if (!collapsed) {
173 InsetText::Edit(bv, x, y, button);
178 Inset::EDITABLE InsetCollapsable::Editable() const
182 return HIGHLY_EDITABLE;
186 void InsetCollapsable::InsetUnlock(BufferView * bv)
191 InsetText::InsetUnlock(bv);
192 UpdateLocal(bv, false);
196 void InsetCollapsable::UpdateLocal(BufferView * bv, bool flag)
198 InsetText::UpdateLocal(bv, flag);
202 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
204 if (!collapsed && (x >= button_length)) {
205 InsetText::InsetButtonPress(bv, x, y, button);
210 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
211 int x, int y, int button)
213 if ((x >= 0) && (x < button_length) &&
214 (y >= button_top_y) && (y < button_bottom_y)) {
217 InsetText::InsetButtonRelease(bv, 0, 0, button);
218 UpdateLocal(bv, true);
221 UpdateLocal(bv, false);
222 bv->unlockInset(this);
224 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
225 InsetText::InsetButtonRelease(bv, x, y, button);
230 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
231 int x, int y, int state)
233 if (x >= button_length) {
234 InsetText::InsetMotionNotify(bv, x, y, state);
239 int InsetCollapsable::getMaxWidth(Painter & pain,
240 UpdatableInset const * inset) const
242 if ((this == inset) && !owner())
243 return pain.paperWidth();
245 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
247 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
248 width_collapsed(pain, labelfont) - widthOffset;
250 return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
254 int InsetCollapsable::getMaxTextWidth(Painter & pain,
255 UpdatableInset const * inset) const
257 return getMaxWidth(pain, inset) -
258 width_collapsed(pain, labelfont) - widthOffset;