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