]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
2802e0507d64b53bb3ed1dbdeac1b054aa4fd37e
[lyx.git] / src / insets / insetcollapsable.C
1 /* This file is part of
2  * ======================================================
3  * 
4  *           LyX, The Document Processor
5  *       
6  *          Copyright 1998-2001 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 "insets/insettext.h"
23 #include "support/LOstream.h"
24 #include "support/lstrings.h"
25 #include "debug.h"
26 #include "lyxtext.h"
27
28 class LyXText;
29
30 using std::ostream;
31 using std::endl;
32 using std::max;
33
34 InsetCollapsable::InsetCollapsable()
35         : UpdatableInset(), inset(new InsetText)
36 {
37         //inset = new InsetText;
38     inset->setOwner(this);
39     collapsed = false;
40     label = "Label";
41     autocollapse = true;
42     inset->SetAutoBreakRows(true);
43     inset->SetDrawFrame(0, InsetText::ALWAYS);
44     inset->SetFrameColor(0, LColor::footnoteframe);
45     button_length = button_top_y = button_bottom_y = 0;
46     setInsetName("Collapsable");
47     widthCollapsed = oldWidth = 0;
48     need_update = FULL;
49 }
50
51
52 Inset * InsetCollapsable::Clone(Buffer const &) const
53 {
54     InsetCollapsable * result = new InsetCollapsable();
55     result->inset->init(inset);
56     result->inset->setOwner(result);
57
58     result->collapsed = collapsed;
59     return result;
60 }
61
62
63 bool InsetCollapsable::InsertInset(BufferView * bv, Inset * in)
64 {
65     if (!InsertInsetAllowed(in)) {
66         lyxerr << "InsetCollapsable::InsertInset: "
67                 "Unable to insert inset." << endl;
68         return false;
69     }
70     
71     return inset->InsertInset(bv, in);
72 }
73
74
75 void InsetCollapsable::Write(Buffer const * buf, ostream & os) const
76 {
77     os << "collapsed " << tostr(collapsed) << "\n";
78     inset->WriteParagraphData(buf, os);
79 }
80
81
82
83 void InsetCollapsable::Read(Buffer const * buf, LyXLex & lex)
84 {
85     if (lex.IsOK()) {
86         lex.next();
87         string const token = lex.GetString();
88         if (token == "collapsed") {
89             lex.next();
90             collapsed = lex.GetBool();
91         } else {
92                 lyxerr << "InsetCollapsable::Read: Missing collapsed!"
93                        << endl;
94         }
95     }
96     inset->Read(buf, lex);
97 }
98
99
100 int InsetCollapsable::ascent_collapsed(Painter & pain, LyXFont const &) const
101 {
102     int width = 0;
103     int ascent = 0;
104     int descent = 0;
105     pain.buttonText(0, 0, label, labelfont, false, 
106                     width, ascent, descent);
107     return ascent;
108 }
109
110
111 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
112 {
113     int width = 0;
114     int ascent = 0;
115     int descent = 0;
116     pain.buttonText(0, 0, label, labelfont, false, 
117                     width, ascent, descent);
118     return descent;
119 }
120
121
122 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
123 {
124     int width;
125     int ascent;
126     int descent;
127     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label, labelfont, false,
128                     width, ascent, descent);
129     return width + (2*TEXT_TO_INSET_OFFSET);
130 }
131
132
133 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
134 {
135     if (collapsed) 
136         return ascent_collapsed(bv->painter(), font);
137     else 
138         return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
139 }
140
141
142 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
143 {
144     if (collapsed) 
145         return descent_collapsed(bv->painter(), font);
146     else 
147         return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
148 }
149
150
151 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
152 {
153     if (collapsed) 
154         return widthCollapsed;
155
156     return inset->width(bv, font) + widthCollapsed;
157 }
158
159
160 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
161                                       int baseline, float & x) const
162 {
163     int width = 0;
164     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
165                     baseline, label, labelfont, true, width);
166     x += width + TEXT_TO_INSET_OFFSET;
167 }
168
169
170 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
171                             int baseline, float & x, bool cleared) const
172 {
173     Painter & pain = bv->painter();
174
175     button_length = widthCollapsed;
176     button_top_y = -ascent_collapsed(pain, f);
177     button_bottom_y = descent_collapsed(pain, f);
178     if (collapsed) {
179         draw_collapsed(pain, f, baseline, x);
180         x += TEXT_TO_INSET_OFFSET;
181         return;
182     }
183
184     if (!cleared && ((inset->need_update == InsetText::FULL) ||
185                      (inset->need_update == InsetText::INIT) ||
186                      (top_x != int(x)) || (top_baseline != baseline))) {
187         int w =  owner() ? width(bv, f) : pain.paperWidth();
188         int h = ascent(bv, f) + descent(bv, f);
189         int tx = (needFullRow() && !owner()) ? 0 : int(x);
190         int ty = max(0, baseline - ascent(bv, f));
191         
192         if ((ty + h) > pain.paperHeight())
193             h = pain.paperHeight();
194         if ((top_x + w) > pain.paperWidth())
195             w = pain.paperWidth();
196         if (baseline < 0)
197             h += (baseline - ascent(bv, f));
198         pain.fillRectangle(tx, ty - 1, w, h + 2);
199         cleared = true;
200     }
201
202     top_x = int(x);
203     top_baseline = baseline;
204
205     draw_collapsed(pain, f,
206                    baseline - ascent(bv, f) + ascent_collapsed(pain, f),
207                    x);
208     inset->draw(bv, f, 
209                 baseline - ascent(bv, f) + ascent_collapsed(pain, f),
210                 x, cleared);
211     need_update = NONE;
212 }
213
214
215 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
216 {
217     UpdatableInset::Edit(bv, x, y, button);
218
219     if (collapsed && autocollapse) {
220         collapsed = false;
221         if (!bv->lockInset(this))
222             return;
223         bv->updateInset(this, false);
224         inset->Edit(bv, 0, 0, button);
225     } else if (!collapsed) {
226         if (!bv->lockInset(this))
227             return;
228         inset->Edit(bv, x - widthCollapsed, y, button);
229     }
230 }
231
232
233 Inset::EDITABLE InsetCollapsable::Editable() const
234 {
235         if (collapsed)
236                 return IS_EDITABLE;
237         return HIGHLY_EDITABLE;
238 }
239
240
241 void InsetCollapsable::InsetUnlock(BufferView * bv)
242 {
243     if (autocollapse) {
244         collapsed = true;
245     }
246     inset->InsetUnlock(bv);
247     bv->updateInset(this, false);
248 }
249
250
251 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
252 {
253     if (!collapsed && (x >= button_length)) {
254         inset->InsetButtonPress(bv, x - widthCollapsed, y, button);
255     }
256 }
257
258
259 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
260                                           int x, int y, int button)
261 {
262     if ((x >= 0)  && (x < button_length) &&
263         (y >= button_top_y) &&  (y < button_bottom_y)) {
264         if (collapsed) {
265             collapsed = false;
266             inset->InsetButtonRelease(bv, 0, 0, button);
267             bv->updateInset(this, false);
268         } else {
269             collapsed = true;
270             bv->unlockInset(this);
271             bv->updateInset(this, false);
272         }
273     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
274         inset->InsetButtonRelease(bv, x - widthCollapsed, y, button);
275     }
276 }
277
278
279 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
280                                          int x, int y, int state)
281 {
282     if (x >= button_length) {
283         inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
284     }
285 }
286
287
288 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
289 {
290     inset->InsetKeyPress(xke);
291 }
292
293
294 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
295                             bool fragile, bool free_spc) const
296 {
297     return inset->Latex(buf, os, fragile, free_spc);
298 }
299
300
301 int InsetCollapsable::getMaxWidth(Painter & pain,
302                                   UpdatableInset const * inset) const
303 {
304     int const w = UpdatableInset::getMaxWidth(pain, inset);
305
306     if (w < 0) {
307         // What does a negative max width signify? (Lgb)
308         return w;
309     }
310     // should be at least 30 pixels !!!
311     return max(30, w - widthCollapsed);
312 }
313
314
315 #if 0
316 int InsetCollapsable::getMaxTextWidth(Painter & pain,
317                                       UpdatableInset const * inset) const
318 {
319     return getMaxWidth(pain, inset) - widthCollapsed;
320 }
321 #endif
322
323
324 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
325                               bool reinit)
326 {
327     if (reinit) {
328         need_update = FULL;
329         if (owner())
330             owner()->update(bv, font, true);
331         return;
332     }
333     if (!widthCollapsed) {
334         widthCollapsed = width_collapsed(bv->painter(), font);
335         inset->resizeLyXText(bv);
336         need_update = FULL;
337         if (owner()) {
338                 owner()->update(bv, font);
339                 return;
340         }
341     }
342     if (oldWidth != width(bv, font)) {
343         oldWidth = width(bv, font);
344         inset->resizeLyXText(bv);
345         need_update = FULL;
346         if (owner()) {
347                 owner()->update(bv, font);
348                 return;
349         }
350     }
351     inset->update(bv, font);
352 }
353
354
355 UpdatableInset::RESULT
356 InsetCollapsable::LocalDispatch(BufferView * bv, kb_action action,
357                                 string const & arg)
358 {
359     UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
360     if (result == FINISHED)
361         bv->unlockInset(this);
362     return result;
363 }
364
365
366 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
367 {
368     if (inset == in)
369         return true;
370     return inset->LockInsetInInset(bv, in);
371 }
372
373
374 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
375                                           bool lr)
376 {
377     if (inset == in) {
378         bv->unlockInset(this);
379         return true;
380     }
381     return inset->UnlockInsetInInset(bv, in, lr);
382 }
383
384
385 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
386 {
387     if (in == inset)
388         return true;
389     return inset->UpdateInsetInInset(bv, in);
390 }
391
392
393 unsigned int InsetCollapsable::InsetInInsetY()
394 {
395     return inset->InsetInInsetY() - (top_baseline - inset->y());
396 }
397
398
399 void InsetCollapsable::Validate(LaTeXFeatures & features) const
400 {
401     inset->Validate(features);
402 }
403
404
405 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
406 {
407     inset->GetCursorPos(bv, x , y);
408 }
409
410
411 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
412 {
413     inset->ToggleInsetCursor(bv);
414 }
415
416
417 UpdatableInset * InsetCollapsable::GetLockingInset()
418 {
419     UpdatableInset * in = inset->GetLockingInset();
420     if (inset == in)
421         return this;
422     return in;
423 }
424
425
426 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
427 {
428     if (c == LyxCode())
429         return this;
430     return inset->GetFirstLockingInsetOfType(c);
431 }
432
433
434 void InsetCollapsable::SetFont(BufferView * bv,
435                                LyXFont const & font, bool toggleall)
436 {
437     inset->SetFont(bv, font, toggleall);
438 }
439
440
441 bool InsetCollapsable::doClearArea() const
442 {
443     return inset->doClearArea();
444 }
445
446
447 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
448                                        bool const recursive) const
449 {
450     return inset->getLyXText(bv, recursive);
451 }
452
453
454 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
455 {
456     inset->deleteLyXText(bv, recursive);
457 }
458
459
460 void InsetCollapsable::resizeLyXText(BufferView * bv) const
461 {
462     inset->resizeLyXText(bv);
463     LyXFont font(LyXFont::ALL_SANE);
464     oldWidth = width(bv, font);
465 }