]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
2870b5d3bdfed4473dabe6d1f29ba9f987b5553a
[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 "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()
36 {
37     inset = new InsetText;
38     inset->setOwner(this);
39     collapsed = true;
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 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, ascent = 0, descent = 0;
103     pain.buttonText(0, 0, label, labelfont, false, 
104                     width, ascent, descent);
105     return ascent;
106 }
107
108
109 int InsetCollapsable::descent_collapsed(Painter & pain, LyXFont const &) const
110 {
111     int width = 0, ascent = 0, descent = 0;
112     pain.buttonText(0, 0, label, labelfont, false, 
113                     width, ascent, descent);
114     return descent;
115 }
116
117
118 int InsetCollapsable::width_collapsed(Painter & pain, LyXFont const &) const
119 {
120     int width, ascent, descent;
121     pain.buttonText(TEXT_TO_INSET_OFFSET, 0, label, labelfont, false,
122                     width, ascent, descent);
123     return width + (2*TEXT_TO_INSET_OFFSET);
124 }
125
126
127 int InsetCollapsable::ascent(BufferView * bv, LyXFont const & font) const
128 {
129     if (collapsed) 
130         return ascent_collapsed(bv->painter(), font);
131     else 
132         return inset->ascent(bv, font) + TEXT_TO_TOP_OFFSET;
133 }
134
135
136 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
137 {
138     if (collapsed) 
139         return descent_collapsed(bv->painter(), font);
140     else 
141         return inset->descent(bv, font) + TEXT_TO_BOTTOM_OFFSET;
142 }
143
144
145 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
146 {
147     if (collapsed) 
148         return widthCollapsed;
149
150     return inset->width(bv, font) + widthCollapsed;
151 }
152
153
154 void InsetCollapsable::draw_collapsed(Painter & pain, LyXFont const &,
155                                       int baseline, float & x) const
156 {
157     int width = 0;
158     pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
159                     baseline, label, labelfont, true, width);
160     x += width + TEXT_TO_INSET_OFFSET;
161 }
162
163
164 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
165                             int baseline, float & x, bool cleared) const
166 {
167     Painter & pain = bv->painter();
168
169     button_length = widthCollapsed;
170     button_top_y = -ascent_collapsed(pain, f);
171     button_bottom_y = descent_collapsed(pain, f);
172     if (collapsed) {
173         draw_collapsed(pain, f, baseline, x);
174         x += TEXT_TO_INSET_OFFSET;
175         return;
176     }
177
178     if (!cleared && ((inset->need_update == InsetText::FULL) ||
179                      (inset->need_update == InsetText::INIT) ||
180                      (top_x!=int(x)) || (top_baseline!=baseline))) {
181         int w =  owner() ? width(bv, f) : pain.paperWidth();
182         int h = ascent(bv, f) + descent(bv, f);
183         int tx = (needFullRow() && !owner()) ? 0 : int(x);
184         int ty = max(0, baseline - ascent(bv, f));
185         
186         if ((ty + h) > pain.paperHeight())
187             h = pain.paperHeight();
188         if ((top_x + w) > pain.paperWidth())
189             w = pain.paperWidth();
190         pain.fillRectangle(tx, ty - 1, w, h + 2);
191         cleared = true;
192     }
193
194     // not needed if collapsed
195     top_x = int(x);
196     top_baseline = baseline;
197
198     draw_collapsed(pain, f, baseline, x);
199     inset->draw(bv, f, baseline, x, cleared);
200     need_update = NONE;
201 }
202
203
204 void InsetCollapsable::Edit(BufferView * bv, int x, int y, unsigned int button)
205 {
206     UpdatableInset::Edit(bv, x, y, button);
207
208     if (collapsed && autocollapse) {
209         collapsed = false;
210         if (!bv->lockInset(this))
211             return;
212         bv->updateInset(this, false);
213         inset->Edit(bv, 0, 0, button);
214     } else if (!collapsed) {
215         if (!bv->lockInset(this))
216             return;
217         inset->Edit(bv, x-widthCollapsed, y, button);
218     }
219 }
220
221
222 Inset::EDITABLE InsetCollapsable::Editable() const
223 {
224         if (collapsed)
225                 return IS_EDITABLE;
226         return HIGHLY_EDITABLE;
227 }
228
229
230 void InsetCollapsable::InsetUnlock(BufferView * bv)
231 {
232     if (autocollapse) {
233         collapsed = true;
234     }
235     inset->InsetUnlock(bv);
236     bv->updateInset(this, false);
237 }
238
239
240 void InsetCollapsable::InsetButtonPress(BufferView * bv,int x,int y,int button)
241 {
242     if (!collapsed && (x >= button_length)) {
243         inset->InsetButtonPress(bv, x-widthCollapsed, y, button);
244     }
245 }
246
247
248 void InsetCollapsable::InsetButtonRelease(BufferView * bv,
249                                           int x, int y, int button)
250 {
251     if ((x >= 0)  && (x < button_length) &&
252         (y >= button_top_y) &&  (y < button_bottom_y)) {
253         if (collapsed) {
254             collapsed = false;
255             inset->InsetButtonRelease(bv, 0, 0, button);
256             bv->updateInset(this, false);
257         } else {
258             collapsed = true;
259             bv->unlockInset(this);
260             bv->updateInset(this, false);
261         }
262     } else if (!collapsed && (x >= button_length) && (y >= button_top_y)) {
263         inset->InsetButtonRelease(bv, x-widthCollapsed, y, button);
264     }
265 }
266
267
268 void InsetCollapsable::InsetMotionNotify(BufferView * bv,
269                                          int x, int y, int state)
270 {
271     if (x >= button_length) {
272         inset->InsetMotionNotify(bv, x-widthCollapsed, y, state);
273     }
274 }
275
276
277 void InsetCollapsable::InsetKeyPress(XKeyEvent * xke)
278 {
279     inset->InsetKeyPress(xke);
280 }
281
282
283 int InsetCollapsable::Latex(Buffer const * buf, ostream & os,
284                             bool fragile, bool free_spc) const
285 {
286     return inset->Latex(buf, os, fragile, free_spc);
287 }
288
289
290 int InsetCollapsable::getMaxWidth(Painter & pain,
291                                   UpdatableInset const * inset) const
292 {
293     int w = UpdatableInset::getMaxWidth(pain,inset);
294
295     if (w < 0) {
296         // What does a negative max width signify? (Lgb)
297         return w;
298     }
299     // should be at least 30 pixels !!!
300     return max(30, w - widthCollapsed);
301 }
302
303
304 #if 0
305 int InsetCollapsable::getMaxTextWidth(Painter & pain,
306                                       UpdatableInset const * inset) const
307 {
308     return getMaxWidth(pain, inset) - widthCollapsed;
309 }
310 #endif
311
312
313 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
314                               bool reinit)
315 {
316     if (reinit) {
317         need_update = FULL;
318         if (owner())
319             owner()->update(bv, font, true);
320         return;
321     }
322     if (!widthCollapsed) {
323         widthCollapsed = width_collapsed(bv->painter(), font);
324         inset->resizeLyXText(bv);
325         need_update = FULL;
326         if (owner()) {
327                 owner()->update(bv, font);
328                 return;
329         }
330     }
331     if (oldWidth != width(bv, font)) {
332         oldWidth = width(bv, font);
333         inset->resizeLyXText(bv);
334         need_update = FULL;
335         if (owner()) {
336                 owner()->update(bv, font);
337                 return;
338         }
339     }
340     inset->update(bv, font);
341 }
342
343
344 UpdatableInset::RESULT
345 InsetCollapsable::LocalDispatch(BufferView * bv, int action,
346                                 string const & arg)
347 {
348     UpdatableInset::RESULT result = inset->LocalDispatch(bv, action, arg);
349     if (result == FINISHED)
350         bv->unlockInset(this);
351     return result;
352 }
353
354
355 bool InsetCollapsable::LockInsetInInset(BufferView * bv, UpdatableInset * in)
356 {
357     if (inset == in)
358         return true;
359     return inset->LockInsetInInset(bv, in);
360 }
361
362
363 bool InsetCollapsable::UnlockInsetInInset(BufferView * bv, UpdatableInset * in,
364                                           bool lr)
365 {
366     if (inset == in) {
367         bv->unlockInset(this);
368         return true;
369     }
370     return inset->UnlockInsetInInset(bv, in, lr);
371 }
372
373
374 bool InsetCollapsable::UpdateInsetInInset(BufferView * bv, Inset *in)
375 {
376     if (in == inset)
377         return true;
378     return inset->UpdateInsetInInset(bv, in);
379 }
380
381
382 unsigned int InsetCollapsable::InsetInInsetY()
383 {
384     return inset->InsetInInsetY();
385 }
386
387
388 void InsetCollapsable::Validate(LaTeXFeatures & features) const
389 {
390     inset->Validate(features);
391 }
392
393
394 void InsetCollapsable::GetCursorPos(BufferView * bv, int & x, int & y) const
395 {
396     inset->GetCursorPos(bv, x , y);
397 }
398
399
400 void InsetCollapsable::ToggleInsetCursor(BufferView * bv)
401 {
402     inset->ToggleInsetCursor(bv);
403 }
404
405
406 UpdatableInset * InsetCollapsable::GetLockingInset()
407 {
408     UpdatableInset * in = inset->GetLockingInset();
409     if (inset == in)
410         return this;
411     return in;
412 }
413
414
415 UpdatableInset * InsetCollapsable::GetFirstLockingInsetOfType(Inset::Code c)
416 {
417     if (c == LyxCode())
418         return this;
419     return inset->GetFirstLockingInsetOfType(c);
420 }
421
422
423 void InsetCollapsable::SetFont(BufferView * bv,
424                                LyXFont const & font, bool toggleall)
425 {
426     inset->SetFont(bv, font, toggleall);
427 }
428
429
430 bool InsetCollapsable::doClearArea() const
431 {
432     return inset->doClearArea();
433 }
434
435
436 LyXText * InsetCollapsable::getLyXText(BufferView * bv) const
437 {
438     return inset->getLyXText(bv);
439 }
440
441
442 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
443 {
444     inset->deleteLyXText(bv, recursive);
445 }
446
447
448 void InsetCollapsable::resizeLyXText(BufferView * bv) const
449 {
450     inset->resizeLyXText(bv);
451     LyXFont font(LyXFont::ALL_SANE);
452     oldWidth = width(bv, font);
453 }