]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Applied Edwins patch, fixes to free memory read in insettext, partial fix
[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                 x += TEXT_TO_INSET_OFFSET;
199                 return;
200         }
201
202         float old_x = x;
203
204         if (!owner())
205                 x += static_cast<float>(scroll());
206
207         if (!cleared && (inset.need_update == InsetText::FULL ||
208                          inset.need_update == InsetText::INIT ||
209                          top_x != int(x) ||
210                          top_baseline != baseline))
211         {
212                 // we don't need anymore to clear here we just have to tell
213                 // the underlying LyXText that it should do the RowClear!
214                 inset.setUpdateStatus(bv, InsetText::FULL);
215                 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
216                 return;
217         }
218
219         top_x = int(x);
220         topx_set = true;
221         top_baseline = baseline;
222
223         int const bl = baseline - ascent(bv, f) + ascent_collapsed();
224         
225         draw_collapsed(pain, bl, old_x);
226         inset.draw(bv, f, 
227                            bl + descent_collapsed() + inset.ascent(bv, f),
228                            x, cleared);
229 }
230
231
232 void InsetCollapsable::edit(BufferView * bv, int xp, int yp,
233                             unsigned int button)
234 {
235         UpdatableInset::edit(bv, xp, yp, button);
236
237         if (collapsed_) {
238                 collapsed_ = false;
239                 if (!bv->lockInset(this))
240                         return;
241                 bv->updateInset(this, true);
242                 inset.edit(bv);
243         } else {
244                 if (!bv->lockInset(this))
245                         return;
246                 if (yp <= button_bottom_y) {
247                         inset.edit(bv);
248                 } else {
249                         LyXFont font(LyXFont::ALL_SANE);
250                         int yy = ascent(bv, font) + yp -
251                                 (ascent_collapsed() +
252                                  descent_collapsed() +
253                                  inset.ascent(bv, font));
254                         inset.edit(bv, xp, yy, button);
255                 }
256         }
257         first_after_edit = true;
258 }
259
260
261 void InsetCollapsable::edit(BufferView * bv, bool front)
262 {
263         UpdatableInset::edit(bv, front);
264
265         if (collapsed_) {
266                 collapsed_ = false;
267                 if (!bv->lockInset(this))
268                         return;
269                 inset.setUpdateStatus(bv, InsetText::FULL);
270                 bv->updateInset(this, true);
271                 inset.edit(bv, front);
272         } else {
273                 if (!bv->lockInset(this))
274                         return;
275                 inset.edit(bv, front);
276         }
277         first_after_edit = true;
278 }
279
280
281 Inset::EDITABLE InsetCollapsable::editable() const
282 {
283         if (collapsed_)
284                 return IS_EDITABLE;
285         return HIGHLY_EDITABLE;
286 }
287
288
289 void InsetCollapsable::insetUnlock(BufferView * bv)
290 {
291 #if 0
292         if (autocollapse) {
293                 if (change_label_with_text) {
294                         draw_label = get_new_label();
295                 } else {
296                         draw_label = label;
297                 }
298                 collapsed_ = true;
299         }
300 #endif
301         inset.insetUnlock(bv);
302         if (scroll())
303                 scroll(bv, 0.0F);
304         bv->updateInset(this, false);
305 }
306
307
308 void InsetCollapsable::insetButtonPress(BufferView * bv,
309                                         int x, int y, int button)
310 {
311         if (!collapsed_ && (y > button_bottom_y)) {
312                 LyXFont font(LyXFont::ALL_SANE);
313                 int yy = ascent(bv, font) + y -
314                     (ascent_collapsed() +
315                      descent_collapsed() +
316                      inset.ascent(bv, font));
317                 inset.insetButtonPress(bv, x, yy, button);
318         }
319 }
320
321
322 void InsetCollapsable::insetButtonRelease(BufferView * bv,
323                                           int x, int y, int button)
324 {
325         if ((x >= 0)  && (x < button_length) &&
326             (y >= button_top_y) &&  (y <= button_bottom_y))
327         {
328                 if (collapsed_) {
329                         collapsed_ = false;
330 // should not be called on inset open!
331 //                      inset.insetButtonRelease(bv, 0, 0, button);
332                         inset.setUpdateStatus(bv, InsetText::FULL);
333                         bv->updateInset(this, true);
334                 } else {
335                         collapsed_ = true;
336                         bv->unlockInset(this);
337                         bv->updateInset(this, true);
338                 }
339         } else if (!collapsed_ && (y > button_bottom_y)) {
340                 LyXFont font(LyXFont::ALL_SANE);
341                 int yy = ascent(bv, font) + y -
342                     (ascent_collapsed() +
343                      descent_collapsed() +
344                      inset.ascent(bv, font));
345                 inset.insetButtonRelease(bv, x, yy, button);
346         }
347 }
348
349
350 void InsetCollapsable::insetMotionNotify(BufferView * bv,
351                                          int x, int y, int state)
352 {
353         if (y > button_bottom_y) {
354                 LyXFont font(LyXFont::ALL_SANE);
355                 int yy = ascent(bv, font) + y -
356                     (ascent_collapsed() +
357                      descent_collapsed() +
358                      inset.ascent(bv, font));
359                 inset.insetMotionNotify(bv, x, yy, state);
360         }
361 }
362
363
364 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
365 {
366         inset.insetKeyPress(xke);
367 }
368
369
370 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
371                             bool fragile, bool free_spc) const
372 {
373         return inset.latex(buf, os, fragile, free_spc);
374 }
375
376
377 int InsetCollapsable::getMaxWidth(BufferView * bv,
378                                   UpdatableInset const * inset) const
379 {
380 #if 0
381         int const w = UpdatableInset::getMaxWidth(bv, inset);
382
383         if (w < 0) {
384                 // What does a negative max width signify? (Lgb)
385                 // Use the max width of the draw-area (Jug)
386                 return w;
387         }
388         // should be at least 30 pixels !!!
389         return max(30, w - width_collapsed());
390 #else
391         return UpdatableInset::getMaxWidth(bv, inset);
392 #endif
393 }
394
395
396 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
397                               bool reinit)
398 {
399         if (in_update) {
400                 if (reinit && owner()) {
401                         owner()->update(bv, font, true);
402                 }
403                 return;
404         }
405         in_update = true;
406         inset.update(bv, font, reinit);
407         if (reinit && owner()) {
408                 owner()->update(bv, font, true);
409         }
410         in_update = false;
411 }
412
413
414 UpdatableInset::RESULT
415 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
416                                 string const & arg)
417 {
418         UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
419         if (result == FINISHED)
420                 bv->unlockInset(this);
421         first_after_edit = false;
422         return result;
423 }
424
425
426 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
427 {
428         if (&inset == in)
429                 return true;
430         return inset.lockInsetInInset(bv, in);
431 }
432
433
434 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
435                                           bool lr)
436 {
437         if (&inset == in) {
438                 bv->unlockInset(this);
439                 return true;
440         }
441         return inset.unlockInsetInInset(bv, in, lr);
442 }
443
444
445 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
446 {
447         if (&inset == in)
448                 return true;
449         return inset.updateInsetInInset(bv, in);
450 }
451
452
453 unsigned int InsetCollapsable::insetInInsetY()
454 {
455         return inset.insetInInsetY() - (top_baseline - inset.y());
456 }
457
458
459 void InsetCollapsable::validate(LaTeXFeatures & features) const
460 {
461         inset.validate(features);
462 }
463
464
465 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
466 {
467         inset.getCursorPos(bv, x , y);
468 }
469
470
471 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
472 {
473         inset.toggleInsetCursor(bv);
474 }
475
476
477 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
478 {
479         inset.showInsetCursor(bv, show);
480 }
481
482
483 void InsetCollapsable::hideInsetCursor(BufferView * bv)
484 {
485         inset.hideInsetCursor(bv);
486 }
487
488
489 UpdatableInset * InsetCollapsable::getLockingInset() const
490 {
491         UpdatableInset * in = inset.getLockingInset();
492         if (const_cast<InsetText *>(&inset) == in)
493                 return const_cast<InsetCollapsable *>(this);
494         return in;
495 }
496
497
498 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
499 {
500         if (c == lyxCode())
501                 return this;
502         return inset.getFirstLockingInsetOfType(c);
503 }
504
505
506 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
507                                bool toggleall, bool selectall)
508 {
509         inset.setFont(bv, font, toggleall, selectall);
510 }
511
512
513 bool InsetCollapsable::doClearArea() const
514 {
515         return inset.doClearArea();
516 }
517
518
519 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
520                                        bool const recursive) const
521 {
522         return inset.getLyXText(bv, recursive);
523 }
524
525
526 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
527 {
528         inset.deleteLyXText(bv, recursive);
529 }
530
531
532 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
533 {
534         inset.resizeLyXText(bv, force);
535         LyXFont font(LyXFont::ALL_SANE);
536         oldWidth = width(bv, font);
537 }
538
539
540 std::vector<string> const InsetCollapsable::getLabelList() const
541 {
542         return inset.getLabelList();
543 }
544
545
546 bool InsetCollapsable::nodraw() const
547 {
548         return inset.nodraw();
549 }
550
551  
552 int InsetCollapsable::scroll(bool recursive) const
553 {
554         int sx = UpdatableInset::scroll(false);
555
556         if (recursive)
557                 sx += inset.scroll(recursive);
558
559         return sx;
560 }
561
562
563 Paragraph * InsetCollapsable::getParFromID(int id) const
564 {
565         lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
566         return inset.getParFromID(id);
567 }
568
569
570 Paragraph * InsetCollapsable::firstParagraph() const
571 {
572         return inset.firstParagraph();
573 }
574
575
576 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
577 {
578         return inset.cursor(bv);
579 }
580
581
582 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
583 {
584         if (id_arg == id())
585                 return const_cast<InsetCollapsable *>(this);
586         return inset.getInsetFromID(id_arg);
587 }
588
589
590 void InsetCollapsable::open(BufferView * bv)
591 {
592         if (!collapsed_) return;
593         
594         collapsed_ = false;
595         bv->updateInset(this, true);
596 }
597
598
599 void InsetCollapsable::close(BufferView * bv) const
600 {
601         if (collapsed_)
602                 return;
603         
604         collapsed_ = true;
605         bv->updateInset(const_cast<InsetCollapsable *>(this), true);
606 }
607
608
609 void InsetCollapsable::setLabel(string const & l) const
610 {
611         label = l;
612 }
613
614
615 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
616                                      bool const & cs, bool const & mw)
617 {
618         bool found = inset.searchForward(bv, str, cs, mw);
619         if (first_after_edit && !found)
620                 close(bv);
621         first_after_edit = false;
622         return found;
623 }
624 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
625                                       bool const & cs, bool const & mw)
626 {
627         bool found = inset.searchBackward(bv, str, cs, mw);
628         if (first_after_edit && !found)
629                 close(bv);
630         first_after_edit = false;
631         return found;
632 }
633
634
635 string const InsetCollapsable::selectNextWord(BufferView * bv, float & value) const
636 {
637         string str = inset.selectNextWord(bv, value);
638         if (first_after_edit && str.empty())
639                 close(bv);
640         first_after_edit = false;
641         return str;
642 }