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