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