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() : InsetText()
31 framecolor = LColor::footnoteframe;
32 widthOffset = 6; // 2+2 (behind+back), 1+1 (frame)
33 button_length = button_top_y = button_bottom_y = 0;
34 setInsetName("Collapsable");
38 Inset * InsetCollapsable::Clone() const
40 InsetCollapsable * result = new InsetCollapsable();
43 result->collapsed = collapsed;
47 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
49 os << getInsetName() << "\n\ncollapsed ";
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(Painter & pain, LyXFont const & f,
139 int baseline, float & x) const
141 button_length = width_collapsed(pain, labelfont) + 2;
142 button_top_y = -ascent_collapsed(pain, f);
143 button_bottom_y = descent_collapsed(pain, f);
145 top_baseline = baseline;
147 draw_collapsed(pain, f, baseline, x);
151 draw_collapsed(pain, f, baseline, x);
152 x -= TEXT_TO_INSET_OFFSET;
154 int w = InsetText::width(pain, f) + 2 * TEXT_TO_INSET_OFFSET;
155 int h = ascent(pain,f) + descent(pain, f);
157 pain.rectangle(int(x), baseline - ascent(pain, f), w, h, framecolor);
159 x += TEXT_TO_INSET_OFFSET;
160 drawTextXOffset = int(x) - top_x;
161 InsetText::draw(pain, f, baseline, x);
165 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
167 if (collapsed && autocollapse) {
169 UpdateLocal(bv, true, false);
170 InsetText::Edit(bv, 0, 0, button);
171 } else if (!collapsed) {
172 InsetText::Edit(bv, x, y, button);
177 Inset::EDITABLE InsetCollapsable::Editable() const
181 return HIGHLY_EDITABLE;
185 void InsetCollapsable::InsetUnlock(BufferView * bv)
190 InsetText::InsetUnlock(bv);
191 UpdateLocal(bv, false, false);
195 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
197 if (!collapsed && (x >= button_length)) {
198 InsetText::InsetButtonPress(bv, x, y, button);
203 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
204 int x, int y, int button)
206 if ((x >= 0) && (x < button_length) &&
207 (y >= button_top_y) && (y < button_bottom_y)) {
210 InsetText::InsetButtonRelease(bv, 0, 0, button);
211 UpdateLocal(bv, true, false);
214 UpdateLocal(bv, false, false);
215 bv->unlockInset(this);
217 } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
218 InsetText::InsetButtonRelease(bv, x, y, button);
223 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
224 int x, int y, int state)
226 if (x >= button_length) {
227 InsetText::InsetMotionNotify(bv, x, y, state);
232 int InsetCollapsable::getMaxWidth(Painter & pain,
233 UpdatableInset const * inset) const
235 if ((this == inset) && !owner())
236 return pain.paperWidth();
238 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
240 return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
241 width_collapsed(pain, labelfont) - widthOffset;
243 return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
247 int InsetCollapsable::getMaxTextWidth(Painter & pain,
248 UpdatableInset const * inset) const
250 return getMaxWidth(pain, inset) -
251 width_collapsed(pain, labelfont) - widthOffset;