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