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