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