]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
1889b8a1694c4417886d2f85e36f4dbf4021818e
[lyx.git] / src / insets / insetcollapsable.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright (C) 1998 The LyX Team.
7  *
8  * ======================================================
9  */
10
11 #include <config.h>
12
13 #ifdef __GNUG__
14 #pragma implementation
15 #endif
16
17 #include "insetcollapsable.h"
18 #include "gettext.h"
19 #include "lyxfont.h"
20 #include "BufferView.h"
21 #include "Painter.h"
22 #include "support/LOstream.h"
23 #include "support/lstrings.h"
24
25 using std::ostream;
26
27 InsetCollapsable::InsetCollapsable()
28         : InsetText()
29 {
30     collapsed = true;
31     label = "Label";
32     autocollapse = true;
33     autoBreakRows = true;
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");
38 }
39
40
41 Inset * InsetCollapsable::Clone() const
42 {
43     InsetCollapsable * result = new InsetCollapsable();
44     result->init(this);
45
46     result->collapsed = collapsed;
47     return result;
48 }
49
50
51 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
52 {
53     os << "collapsed " << tostr(!display()) << "\n";
54     WriteParagraphData(buf, os);
55 }
56
57
58 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
59 {
60     if (lex.IsOK()) {
61         lex.next();
62         string token = lex.GetString();
63         if (token == "collapsed") {
64             lex.next();
65             collapsed = lex.GetBool();
66         }
67     }
68     InsetText::Read(buf, lex);
69 }
70
71
72 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
73 {
74     int width = 0, ascent = 0, descent = 0;
75     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
76                     width, ascent, descent);
77     return ascent;
78 }
79
80
81 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
82 {
83     int width = 0, ascent = 0, descent = 0;
84     pain.buttonText(0, 0, label.c_str(), labelfont, false, 
85                     width, ascent, descent);
86     return descent;
87 }
88
89
90 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
91 {
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);
96 }
97
98
99 int InsetCollapsable::ascent(Painter & pain, LyXFont const & font) const
100 {
101     if (collapsed) 
102         return ascent_collapsed(pain, font);
103     else 
104         return InsetText::ascent(pain, font) + TEXT_TO_TOP_OFFSET;
105 }
106
107
108 int InsetCollapsable::descent(Painter & pain, LyXFont const & font) const
109 {
110     if (collapsed) 
111         return descent_collapsed(pain, font);
112     else 
113         return InsetText::descent(pain, font) + TEXT_TO_BOTTOM_OFFSET;
114 }
115
116
117 int InsetCollapsable::width(Painter & pain, LyXFont const & font) const
118 {
119     if (collapsed) 
120         return width_collapsed(pain, font);
121
122     return InsetText::width(pain, font) + width_collapsed(pain,font) +
123             widthOffset;
124 }
125
126
127 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const & font,
128                                       int baseline, float & x) const
129 {
130     int width = 0;
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);
135 }
136
137
138 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
139                             int baseline, float & x, bool cleared) const
140 {
141     Painter & pain = bv->painter();
142
143     button_length = width_collapsed(pain, labelfont) + 2;
144     button_top_y = -ascent_collapsed(pain, f);
145     button_bottom_y = descent_collapsed(pain, f);
146     if (collapsed) {
147         draw_collapsed(pain, f, baseline, x);
148         return;
149     }
150
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);
157         
158         if (ty < 0)
159             ty = 0;
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);
165         cleared = true;
166     }
167
168     // not needed if collapsed
169     topx = int(x);
170     topbaseline = baseline;
171
172     int w =  InsetText::width(pain, f) + (2 * TEXT_TO_INSET_OFFSET);
173     int h = ascent(pain,f) + descent(pain, f);
174
175     draw_collapsed(pain, f, baseline, x);
176     x -= TEXT_TO_INSET_OFFSET;
177
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);
182 }
183
184
185 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
186 {
187     if (collapsed && autocollapse) {
188         collapsed = false;
189         UpdateLocal(bv, FULL, false);
190         InsetText::Edit(bv, 0, 0, button);
191     } else if (!collapsed) {
192         InsetText::Edit(bv, x, y, button);
193     }
194 }
195
196
197 Inset::EDITABLE InsetCollapsable::Editable() const
198 {
199         if (collapsed)
200                 return IS_EDITABLE;
201         return HIGHLY_EDITABLE;
202 }
203
204
205 void InsetCollapsable::InsetUnlock(BufferView * bv)
206 {
207     if (autocollapse) {
208         collapsed = true;
209     }
210     InsetText::InsetUnlock(bv);
211     UpdateLocal(bv, FULL, false);
212 }
213
214
215 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
216 {
217     if (!collapsed && (x >= button_length)) {
218         InsetText::InsetButtonPress(bv, x, y, button);
219     }
220 }
221
222
223 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
224                                           int x, int y, int button)
225 {
226     if ((x >= 0)  && (x < button_length) &&
227         (y >= button_top_y) &&  (y < button_bottom_y)) {
228         if (collapsed) {
229             collapsed = false;
230             InsetText::InsetButtonRelease(bv, 0, 0, button);
231             UpdateLocal(bv, FULL, false);
232         } else {
233             collapsed = true;
234             UpdateLocal(bv, FULL, false);
235             bv->unlockInset(this);
236         }
237     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
238         InsetText::InsetButtonRelease(bv, x, y, button);
239     }
240 }
241
242
243 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
244                                          int x, int y, int state)
245 {
246     if (x >= button_length) {
247         InsetText::InsetMotionNotify(bv, x, y, state);
248     }
249 }
250
251
252 int InsetCollapsable::getMaxWidth(Painter & pain,
253                                   UpdatableInset const * inset) const
254 {
255     if ((this == inset) && !owner())
256         return pain.paperWidth();
257     if (this == inset)
258         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset);
259     if (owner())
260         return static_cast<UpdatableInset*>(owner())->getMaxWidth(pain,inset)-
261                 width_collapsed(pain, labelfont) - widthOffset;
262
263     return pain.paperWidth()-width_collapsed(pain, labelfont) - widthOffset;
264 }
265
266
267 int InsetCollapsable::getMaxTextWidth(Painter & pain,
268                                       UpdatableInset const * inset) const
269 {
270     return getMaxWidth(pain, inset) -
271         width_collapsed(pain, labelfont) - widthOffset;
272 }
273
274
275 void InsetCollapsable::update(BufferView * bv,
276                               LyXFont const & font, bool dodraw)
277 {
278     drawTextXOffset = width_collapsed(bv->painter(), font);
279     InsetText::update(bv, font, dodraw);
280 }