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