]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
insetfloat work + stuff
[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 #include "font.h"
28 #include "lyxlex.h"
29
30 class LyXText;
31
32 using std::ostream;
33 using std::endl;
34 using std::max;
35
36
37 InsetCollapsable::InsetCollapsable(bool collapsed)
38         : UpdatableInset(), collapsed_(collapsed), 
39           button_length(0), button_top_y(0), button_bottom_y(0),
40           label("Label"),
41 #if 0
42         autocollapse(false),
43 #endif
44           oldWidth(0), need_update(FULL),
45           inlined(false)
46 {
47         inset.setOwner(this);
48         inset.setAutoBreakRows(true);
49         inset.setDrawFrame(0, InsetText::ALWAYS);
50         inset.setFrameColor(0, LColor::collapsableframe);
51         setInsetName("Collapsable");
52 }
53
54
55 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in, bool same_id)
56         : UpdatableInset(in, same_id), collapsed_(in.collapsed_), 
57           framecolor(in.framecolor), labelfont(in.labelfont),
58           button_length(0), button_top_y(0), button_bottom_y(0),
59           label(in.label),
60 #if 0
61         autocollapse(in.autocollapse),
62 #endif
63           oldWidth(0), need_update(FULL),
64           inlined(in.inlined)
65 {
66         inset.init(&(in.inset), same_id);
67         inset.setOwner(this);
68 }
69
70
71 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
72 {
73         if (!insetAllowed(in->lyxCode())) {
74                 lyxerr << "InsetCollapsable::InsertInset: "
75                         "Unable to insert inset." << endl;
76                 return false;
77         }
78         return inset.insertInset(bv, in);
79 }
80
81
82 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
83 {
84         os << "collapsed " << tostr(collapsed_) << "\n";
85         inset.writeParagraphData(buf, os);
86 }
87
88
89
90 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
91 {
92         if (lex.IsOK()) {
93                 lex.next();
94                 string const token = lex.GetString();
95                 if (token == "collapsed") {
96                         lex.next();
97                         collapsed_ = lex.GetBool();
98                 } else {
99                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
100                                << endl;
101                         // Take countermeasures
102                         lex.pushToken(token);
103                 }
104         }
105         inset.read(buf, lex);
106 }
107
108
109 int InsetCollapsable::ascent_collapsed() const
110 {
111         int width = 0;
112         int ascent = 0;
113         int descent = 0;
114         lyxfont::buttonText(label, labelfont, width, ascent, descent);
115         return ascent;
116 }
117
118
119 int InsetCollapsable::descent_collapsed() const
120 {
121         int width = 0;
122         int ascent = 0;
123         int descent = 0;
124         lyxfont::buttonText(label, labelfont, width, ascent, descent);
125         return descent;
126 }
127
128
129 //int InsetCollapsable::width_collapsed(Painter & pain) const
130 int InsetCollapsable::width_collapsed() const
131 {
132         int width;
133         int ascent;
134         int descent;
135         lyxfont::buttonText(label, labelfont, width, ascent, descent);
136         return width + (2*TEXT_TO_INSET_OFFSET);
137 }
138
139
140 int InsetCollapsable::ascent(BufferView * /*bv*/, LyXFont const &) const
141 {
142         return ascent_collapsed();
143 }
144
145
146 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
147 {
148         if (collapsed_) 
149                 return descent_collapsed();
150
151         return descent_collapsed()
152                 + inset.descent(bv, font)
153                 + inset.ascent(bv, font)
154                 + TEXT_TO_BOTTOM_OFFSET;
155 }
156
157
158 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
159 {
160         if (collapsed_) 
161                 return width_collapsed();
162
163         int widthCollapsed = width_collapsed();
164
165         return (inset.width(bv, font) > widthCollapsed) ?
166                 inset.width(bv, font) : widthCollapsed;
167 }
168
169
170 void InsetCollapsable::draw_collapsed(Painter & pain,
171                                       int baseline, float & x) const
172 {
173         pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
174                         baseline, label, labelfont);
175         x += width_collapsed();
176 }
177
178
179 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
180                             int baseline, float & x, bool cleared) const
181 {
182         if (nodraw())
183                 return;
184
185         Painter & pain = bv->painter();
186
187         button_length = width_collapsed();
188         button_top_y = -ascent(bv, f);
189         button_bottom_y = -ascent(bv, f) + ascent_collapsed() +
190                 descent_collapsed();
191
192         if (collapsed_) {
193                 draw_collapsed(pain, baseline, x);
194                 x += TEXT_TO_INSET_OFFSET;
195                 return;
196         }
197
198         float old_x = x;
199
200 #if 0
201         UpdatableInset::draw(bv, f, baseline, x, cleared);
202 #else
203         if (!owner())
204                 x += static_cast<float>(scroll());
205 #endif
206         if (!cleared && (inset.need_update == InsetText::FULL ||
207                          inset.need_update == InsetText::INIT ||
208                          top_x != int(x) ||
209                          top_baseline != baseline))
210         {
211 #if 1
212                 // we don't need anymore to clear here we just have to tell
213                 // the underlying LyXText that it should do the RowClear!
214                 inset.setUpdateStatus(bv, InsetText::FULL);
215                 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
216                 return;
217 #else
218                 int w =  owner() ? width(bv, f) : pain.paperWidth();
219                 int h = ascent(bv, f) + descent(bv, f);
220                 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
221                 int const ty = max(0, baseline - ascent(bv, f));
222
223                 if ((ty + h) > pain.paperHeight())
224                         h = pain.paperHeight();
225                 if ((top_x + w) > pain.paperWidth())
226                         w = pain.paperWidth();
227                 if (baseline < 0)
228                         h += (baseline - ascent(bv, f));
229                 pain.fillRectangle(tx, ty - 1, w, h + 2);
230                 cleared = true;
231 #endif
232         }
233
234         top_x = int(x);
235         top_baseline = baseline;
236
237         int const bl = baseline - ascent(bv, f) + ascent_collapsed();
238
239         draw_collapsed(pain, bl, old_x);
240         inset.draw(bv, f, 
241                    bl + descent_collapsed() + inset.ascent(bv, f),
242                    x, cleared);
243         need_update = NONE;
244 }
245
246
247 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
248                             unsigned int button)
249 {
250         UpdatableInset::edit(bv, xp, yp, button);
251
252         if (collapsed_) {
253                 collapsed_ = false;
254                 if (!bv->lockInset(this))
255                         return;
256                 bv->updateInset(this, false);
257                 inset.edit(bv);
258         } else {
259                 if (!bv->lockInset(this))
260                         return;
261                 if (yp <= button_bottom_y) {
262                         inset.edit(bv);
263                 } else {
264                         LyXFont font(LyXFont::ALL_SANE);
265                         int yy = ascent(bv, font) + yp -
266                                 (ascent_collapsed() +
267                                  descent_collapsed() +
268                                  inset.ascent(bv, font));
269                         inset.edit(bv, xp, yy, button);
270                 }
271         }
272 }
273
274
275 void InsetCollapsable::edit(BufferView * bv, bool front)
276 {
277         UpdatableInset::edit(bv, front);
278
279         if (collapsed_) {
280                 collapsed_ = false;
281                 if (!bv->lockInset(this))
282                         return;
283                 inset.setUpdateStatus(bv, InsetText::FULL);
284                 bv->updateInset(this, false);
285                 inset.edit(bv, front);
286         } else {
287                 if (!bv->lockInset(this))
288                         return;
289                 inset.edit(bv, front);
290         }
291 }
292
293
294 Inset::EDITABLE InsetCollapsable::editable() const
295 {
296         if (collapsed_)
297                 return IS_EDITABLE;
298         return HIGHLY_EDITABLE;
299 }
300
301
302 void InsetCollapsable::insetUnlock(BufferView * bv)
303 {
304 #if 0
305         if (autocollapse) {
306                 if (change_label_with_text) {
307                         draw_label = get_new_label();
308                 } else {
309                         draw_label = label;
310                 }
311                 collapsed_ = true;
312         }
313 #endif
314         inset.insetUnlock(bv);
315         if (scroll())
316                 scroll(bv, 0.0F);
317         bv->updateInset(this, false);
318 }
319
320
321 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
322                                         int button)
323 {
324         if (!collapsed_ && (y > button_bottom_y)) {
325                 LyXFont font(LyXFont::ALL_SANE);
326                 int yy = ascent(bv, font) + y -
327                     (ascent_collapsed() +
328                      descent_collapsed() +
329                      inset.ascent(bv, font));
330                 inset.insetButtonPress(bv, x, yy, button);
331         }
332 }
333
334
335 void InsetCollapsable::insetButtonRelease(BufferView * bv,
336                                           int x, int y, int button)
337 {
338         if ((x >= 0)  && (x < button_length) &&
339             (y >= button_top_y) &&  (y <= button_bottom_y)) {
340                 if (collapsed_) {
341                         collapsed_ = false;
342                         inset.insetButtonRelease(bv, 0, 0, button);
343                         inset.setUpdateStatus(bv, InsetText::FULL);
344                         bv->updateInset(this, false);
345                 } else {
346                         collapsed_ = true;
347                         bv->unlockInset(this);
348                         bv->updateInset(this, false);
349                 }
350         } else if (!collapsed_ && (y > button_bottom_y)) {
351                 LyXFont font(LyXFont::ALL_SANE);
352                 int yy = ascent(bv, font) + y -
353                     (ascent_collapsed() +
354                      descent_collapsed() +
355                      inset.ascent(bv, font));
356                 inset.insetButtonRelease(bv, x, yy, button);
357         }
358 }
359
360
361 void InsetCollapsable::insetMotionNotify(BufferView * bv,
362                                          int x, int y, int state)
363 {
364         if (y > button_bottom_y) {
365                 LyXFont font(LyXFont::ALL_SANE);
366                 int yy = ascent(bv, font) + y -
367                     (ascent_collapsed() +
368                      descent_collapsed() +
369                      inset.ascent(bv, font));
370                 inset.insetMotionNotify(bv, x, yy, state);
371         }
372 }
373
374
375 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
376 {
377         inset.insetKeyPress(xke);
378 }
379
380
381 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
382                             bool fragile, bool free_spc) const
383 {
384         return inset.latex(buf, os, fragile, free_spc);
385 }
386
387
388 int InsetCollapsable::getMaxWidth(BufferView * bv,
389                                   UpdatableInset const * inset) const
390 {
391         int const w = UpdatableInset::getMaxWidth(bv, inset);
392
393         if (w < 0) {
394                 // What does a negative max width signify? (Lgb)
395                 // Use the max width of the draw-area (Jug)
396                 return w;
397         }
398         // should be at least 30 pixels !!!
399         return max(30, w - width_collapsed());
400 }
401
402
403 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
404                               bool reinit)
405 {
406         inset.update(bv, font, reinit);
407 }
408
409
410 UpdatableInset::RESULT
411 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
412                                 string const & arg)
413 {
414         UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
415         if (result == FINISHED)
416                 bv->unlockInset(this);
417         return result;
418 }
419
420
421 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
422 {
423         if (&inset == in)
424                 return true;
425         return inset.lockInsetInInset(bv, in);
426 }
427
428
429 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
430                                           bool lr)
431 {
432         if (&inset == in) {
433                 bv->unlockInset(this);
434                 return true;
435         }
436         return inset.unlockInsetInInset(bv, in, lr);
437 }
438
439
440 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
441 {
442         if (&inset == in)
443                 return true;
444         return inset.updateInsetInInset(bv, in);
445 }
446
447
448 unsigned int InsetCollapsable::insetInInsetY()
449 {
450         return inset.insetInInsetY() - (top_baseline - inset.y());
451 }
452
453
454 void InsetCollapsable::validate(LaTeXFeatures & features) const
455 {
456         inset.validate(features);
457 }
458
459
460 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
461 {
462         inset.getCursorPos(bv, x , y);
463 }
464
465
466 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
467 {
468         inset.toggleInsetCursor(bv);
469 }
470
471
472 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
473 {
474         inset.showInsetCursor(bv, show);
475 }
476
477
478 void InsetCollapsable::hideInsetCursor(BufferView * bv)
479 {
480         inset.hideInsetCursor(bv);
481 }
482
483
484 UpdatableInset * InsetCollapsable::getLockingInset() const
485 {
486         UpdatableInset * in = inset.getLockingInset();
487         if (const_cast<InsetText *>(&inset) == in)
488                 return const_cast<InsetCollapsable *>(this);
489         return in;
490 }
491
492
493 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
494 {
495         if (c == lyxCode())
496                 return this;
497         return inset.getFirstLockingInsetOfType(c);
498 }
499
500
501 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
502                                bool toggleall, bool selectall)
503 {
504         inset.setFont(bv, font, toggleall, selectall);
505 }
506
507
508 bool InsetCollapsable::doClearArea() const
509 {
510         return inset.doClearArea();
511 }
512
513
514 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
515                                        bool const recursive) const
516 {
517         return inset.getLyXText(bv, recursive);
518 }
519
520
521 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
522 {
523         inset.deleteLyXText(bv, recursive);
524 }
525
526
527 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
528 {
529         inset.resizeLyXText(bv, force);
530         LyXFont font(LyXFont::ALL_SANE);
531         oldWidth = width(bv, font);
532 }
533
534
535 std::vector<string> const InsetCollapsable::getLabelList() const
536 {
537         return inset.getLabelList();
538 }
539
540
541 bool InsetCollapsable::nodraw() const
542 {
543         return inset.nodraw();
544 }
545
546  
547 int InsetCollapsable::scroll(bool recursive) const
548 {
549         int sx = UpdatableInset::scroll(false);
550
551         if (recursive)
552                 sx += inset.scroll(recursive);
553
554         return sx;
555 }
556
557
558 Paragraph * InsetCollapsable::getParFromID(int id) const
559 {
560         return inset.getParFromID(id);
561 }
562
563
564 Paragraph * InsetCollapsable::firstParagraph() const
565 {
566         return inset.firstParagraph();
567 }
568
569
570 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
571 {
572         return inset.cursor(bv);
573 }
574
575
576 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
577 {
578         if (id_arg == id())
579                 return const_cast<InsetCollapsable *>(this);
580         return inset.getInsetFromID(id_arg);
581 }
582
583
584 void InsetCollapsable::open(BufferView * bv)
585 {
586         if (!collapsed_) return;
587         
588         collapsed_ = false;
589         bv->updateInset(this, false);
590 }
591
592
593 void InsetCollapsable::close(BufferView * bv)
594 {
595         if (collapsed_) return;
596         
597         collapsed_ = true;
598         bv->updateInset(this, false);
599 }
600
601
602 void InsetCollapsable::setLabel(string const & l)
603 {
604         label = l;
605 }