]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Fixed small drawing bug in InsetText.
[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
385 int InsetCollapsable::ascii(Buffer const *buf, std::ostream & os, int ll) const
386 {
387         return inset.ascii(buf, os, ll);
388 }
389
390
391 int InsetCollapsable::linuxdoc(Buffer const *buf, std::ostream & os) const
392 {
393         return inset.linuxdoc(buf, os);
394 }
395
396
397 int InsetCollapsable::docbook(Buffer const *buf, std::ostream & os) const
398 {
399         return inset.docbook(buf, os);
400 }
401
402 #if 0
403 int InsetCollapsable::getMaxWidth(BufferView * bv,
404                                   UpdatableInset const * in) const
405 {
406 #if 0
407         int const w = UpdatableInset::getMaxWidth(bv, in);
408
409         if (w < 0) {
410                 // What does a negative max width signify? (Lgb)
411                 // Use the max width of the draw-area (Jug)
412                 return w;
413         }
414         // should be at least 30 pixels !!!
415         return max(30, w - width_collapsed());
416 #else
417         return UpdatableInset::getMaxWidth(bv, in);
418 #endif
419 }
420 #endif
421
422
423 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
424                               bool reinit)
425 {
426         if (in_update) {
427                 if (reinit && owner()) {
428                         owner()->update(bv, font, true);
429                 }
430                 return;
431         }
432         in_update = true;
433         inset.update(bv, font, reinit);
434         if (reinit && owner()) {
435                 owner()->update(bv, font, true);
436         }
437         in_update = false;
438 }
439
440
441 UpdatableInset::RESULT
442 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
443                                 string const & arg)
444 {
445         UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
446         if (result >= FINISHED)
447                 bv->unlockInset(this);
448         first_after_edit = false;
449         return result;
450 }
451
452
453 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
454 {
455         if (&inset == in)
456                 return true;
457         return inset.lockInsetInInset(bv, in);
458 }
459
460
461 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
462                                           bool lr)
463 {
464         if (&inset == in) {
465                 bv->unlockInset(this);
466                 return true;
467         }
468         return inset.unlockInsetInInset(bv, in, lr);
469 }
470
471
472 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
473 {
474         if (in == this)
475                 return true;
476         return inset.updateInsetInInset(bv, in);
477 }
478
479
480 unsigned int InsetCollapsable::insetInInsetY()
481 {
482         return inset.insetInInsetY() - (top_baseline - inset.y());
483 }
484
485
486 void InsetCollapsable::validate(LaTeXFeatures & features) const
487 {
488         inset.validate(features);
489 }
490
491
492 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
493 {
494         inset.getCursorPos(bv, x , y);
495 }
496
497
498 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
499 {
500         inset.toggleInsetCursor(bv);
501 }
502
503
504 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
505 {
506         inset.showInsetCursor(bv, show);
507 }
508
509
510 void InsetCollapsable::hideInsetCursor(BufferView * bv)
511 {
512         inset.hideInsetCursor(bv);
513 }
514
515
516 UpdatableInset * InsetCollapsable::getLockingInset() const
517 {
518         UpdatableInset * in = inset.getLockingInset();
519         if (const_cast<InsetText *>(&inset) == in)
520                 return const_cast<InsetCollapsable *>(this);
521         return in;
522 }
523
524
525 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
526 {
527         if (c == lyxCode())
528                 return this;
529         return inset.getFirstLockingInsetOfType(c);
530 }
531
532
533 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
534                                bool toggleall, bool selectall)
535 {
536         inset.setFont(bv, font, toggleall, selectall);
537 }
538
539
540 bool InsetCollapsable::doClearArea() const
541 {
542         return inset.doClearArea();
543 }
544
545
546 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
547                                        bool const recursive) const
548 {
549         return inset.getLyXText(bv, recursive);
550 }
551
552
553 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
554 {
555         inset.deleteLyXText(bv, recursive);
556 }
557
558
559 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
560 {
561         inset.resizeLyXText(bv, force);
562         LyXFont font(LyXFont::ALL_SANE);
563         oldWidth = width(bv, font);
564 }
565
566
567 std::vector<string> const InsetCollapsable::getLabelList() const
568 {
569         return inset.getLabelList();
570 }
571
572
573 bool InsetCollapsable::nodraw() const
574 {
575         return inset.nodraw();
576 }
577
578  
579 int InsetCollapsable::scroll(bool recursive) const
580 {
581         int sx = UpdatableInset::scroll(false);
582
583         if (recursive)
584                 sx += inset.scroll(recursive);
585
586         return sx;
587 }
588
589
590 Paragraph * InsetCollapsable::getParFromID(int id) const
591 {
592         lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
593         return inset.getParFromID(id);
594 }
595
596
597 Paragraph * InsetCollapsable::firstParagraph() const
598 {
599         return inset.firstParagraph();
600 }
601
602
603 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
604 {
605         return inset.getFirstParagraph(i);
606 }
607
608
609 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
610 {
611         return inset.cursor(bv);
612 }
613
614
615 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
616 {
617         if (id_arg == id())
618                 return const_cast<InsetCollapsable *>(this);
619         return inset.getInsetFromID(id_arg);
620 }
621
622
623 void InsetCollapsable::open(BufferView * bv)
624 {
625         if (!collapsed_) return;
626         
627         collapsed_ = false;
628         bv->updateInset(this, false);
629 }
630
631
632 void InsetCollapsable::close(BufferView * bv) const
633 {
634         if (collapsed_)
635                 return;
636         
637         collapsed_ = true;
638         bv->updateInset(const_cast<InsetCollapsable *>(this), false);
639 }
640
641
642 void InsetCollapsable::setLabel(string const & l) const
643 {
644         label = l;
645 }
646
647
648 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
649                                      bool cs, bool mw)
650 {
651         bool found = inset.searchForward(bv, str, cs, mw);
652         if (first_after_edit && !found)
653                 close(bv);
654         first_after_edit = false;
655         return found;
656 }
657
658
659 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
660                                       bool cs, bool mw)
661 {
662         bool found = inset.searchBackward(bv, str, cs, mw);
663         if (first_after_edit && !found)
664                 close(bv);
665         first_after_edit = false;
666         return found;
667 }
668
669
670 string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv,
671                                               float & value) const
672 {
673         string const str = inset.selectNextWordToSpellcheck(bv, value);
674         if (first_after_edit && str.empty())
675                 close(bv);
676         first_after_edit = false;
677         return str;
678 }