]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
a359a94972b88c77770f0a5265437e2c89f6a169
[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     // not needed if collapsed
203     top_x = int(x);
204     top_baseline = baseline;
205
206 #if 0
207     draw_collapsed(pain, f, baseline, x);
208     inset->draw(bv, f, baseline, x, cleared);
209 #else
210 #warning Jürgen, can you have a look at this? (Lgb)
211     // the intention is quite clear if you set the positon in a minipage you
212     // want the minipage drawn according to that. but as you can see the
213     // cursor is wrongly placed.
214     draw_collapsed(pain, f,
215                    baseline - ascent(bv, f) + ascent_collapsed(pain, f),
216                    x);
217     inset->draw(bv, f,
218                 baseline - ascent(bv, f) + ascent_collapsed(pain, f),
219                 x, cleared);
220 #endif
221     need_update = NONE;
222 }
223
224
225 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
226 {
227     UpdatableInset::Edit(bv, x, y, button);
228
229     if (collapsed && autocollapse) {
230         collapsed = false;
231         if (!bv->lockInset(this))
232             return;
233         bv->updateInset(this, false);
234         inset->Edit(bv, 0, 0, button);
235     } else if (!collapsed) {
236         if (!bv->lockInset(this))
237             return;
238         inset->Edit(bv, x - widthCollapsed, y, button);
239     }
240 }
241
242
243 Inset::EDITABLE InsetCollapsable::Editable() const
244 {
245         if (collapsed)
246                 return IS_EDITABLE;
247         return HIGHLY_EDITABLE;
248 }
249
250
251 void InsetCollapsable::InsetUnlock(BufferView * bv)
252 {
253     if (autocollapse) {
254         collapsed = true;
255     }
256     inset->InsetUnlock(bv);
257     bv->updateInset(this, false);
258 }
259
260
261 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
262 {
263     if (!collapsed && (x >= button_length)) {
264         inset->InsetButtonPress(bv, x - widthCollapsed, y, button);
265     }
266 }
267
268
269 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
270                                           int x, int y, int button)
271 {
272     if ((x >= 0)  && (x < button_length) &&
273         (y >= button_top_y) &&  (y < button_bottom_y)) {
274         if (collapsed) {
275             collapsed = false;
276             inset->InsetButtonRelease(bv, 0, 0, button);
277             bv->updateInset(this, false);
278         } else {
279             collapsed = true;
280             bv->unlockInset(this);
281             bv->updateInset(this, false);
282         }
283     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
284         inset->InsetButtonRelease(bv, x - widthCollapsed, y, button);
285     }
286 }
287
288
289 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
290                                          int x, int y, int state)
291 {
292     if (x >= button_length) {
293         inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
294     }
295 }
296
297
298 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
299 {
300     inset->InsetKeyPress(xke);
301 }
302
303
304 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
305                             bool fragile, bool free_spc) const
306 {
307     return inset->Latex(buf, os, fragile, free_spc);
308 }
309
310
311 int InsetCollapsable::getMaxWidth(Painter & pain,
312                                   UpdatableInset const * inset) const
313 {
314     int const w = UpdatableInset::getMaxWidth(pain, inset);
315
316     if (w < 0) {
317         // What does a negative max width signify? (Lgb)
318         return w;
319     }
320     // should be at least 30 pixels !!!
321     return max(30, w - widthCollapsed);
322 }
323
324
325 #if 0
326 int InsetCollapsable::getMaxTextWidth(Painter & pain,
327                                       UpdatableInset const * inset) const
328 {
329     return getMaxWidth(pain, inset) - widthCollapsed;
330 }
331 #endif
332
333
334 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
335                               bool reinit)
336 {
337     if (reinit) {
338         need_update = FULL;
339         if (owner())
340             owner()->update(bv, font, true);
341         return;
342     }
343     if (!widthCollapsed) {
344         widthCollapsed = width_collapsed(bv->painter(), font);
345         inset->resizeLyXText(bv);
346         need_update = FULL;
347         if (owner()) {
348                 owner()->update(bv, font);
349                 return;
350         }
351     }
352     if (oldWidth != width(bv, font)) {
353         oldWidth = width(bv, font);
354         inset->resizeLyXText(bv);
355         need_update = FULL;
356         if (owner()) {
357                 owner()->update(bv, font);
358                 return;
359         }
360     }
361     inset->update(bv, font);
362 }
363
364
365 UpdatableInset::RESULT
366 InsetCollapsable::LocalDispatch(BufferView * bv, kb_action action,
367                                 string const & arg)
368 {
369     UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
370     if (result == FINISHED)
371         bv->unlockInset(this);
372     return result;
373 }
374
375
376 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
377 {
378     if (inset == in)
379         return true;
380     return inset->LockInsetInInset(bv, in);
381 }
382
383
384 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
385                                           bool lr)
386 {
387     if (inset == in) {
388         bv->unlockInset(this);
389         return true;
390     }
391     return inset->UnlockInsetInInset(bv, in, lr);
392 }
393
394
395 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
396 {
397     if (in == inset)
398         return true;
399     return inset->UpdateInsetInInset(bv, in);
400 }
401
402
403 unsigned int InsetCollapsable::InsetInInsetY()
404 {
405     return inset->InsetInInsetY();
406 }
407
408
409 void InsetCollapsable::Validate(LaTeXFeatures & features) const
410 {
411     inset->Validate(features);
412 }
413
414
415 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
416 {
417     inset->GetCursorPos(bv, x , y);
418 }
419
420
421 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
422 {
423     inset->ToggleInsetCursor(bv);
424 }
425
426
427 UpdatableInset * InsetCollapsable::GetLockingInset()
428 {
429     UpdatableInset * in = inset->GetLockingInset();
430     if (inset == in)
431         return this;
432     return in;
433 }
434
435
436 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
437 {
438     if (c == LyxCode())
439         return this;
440     return inset->GetFirstLockingInsetOfType(c);
441 }
442
443
444 void InsetCollapsable::SetFont(BufferView * bv,
445                                LyXFont const & font, bool toggleall)
446 {
447     inset->SetFont(bv, font, toggleall);
448 }
449
450
451 bool InsetCollapsable::doClearArea() const
452 {
453     return inset->doClearArea();
454 }
455
456
457 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
458                                        bool const recursive) const
459 {
460     return inset->getLyXText(bv, recursive);
461 }
462
463
464 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
465 {
466     inset->deleteLyXText(bv, recursive);
467 }
468
469
470 void InsetCollapsable::resizeLyXText(BufferView * bv) const
471 {
472     inset->resizeLyXText(bv);
473     LyXFont font(LyXFont::ALL_SANE);
474     oldWidth = width(bv, font);
475 }