]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
More fixes.
[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 void InsetCollapsable::insetButtonRelease(BufferView * bv,
326                                           int x, int y, int button)
327 {
328         if ((x >= 0)  && (x < button_length) &&
329             (y >= button_top_y) &&  (y <= button_bottom_y))
330         {
331                 if (collapsed_) {
332                         collapsed_ = false;
333 // should not be called on inset open!
334 //                      inset.insetButtonRelease(bv, 0, 0, button);
335                         inset.setUpdateStatus(bv, InsetText::FULL);
336                         bv->updateInset(this, false);
337                 } else {
338                         collapsed_ = true;
339                         bv->unlockInset(this);
340                         bv->updateInset(this, false);
341                 }
342         } else if (!collapsed_ && (y > button_bottom_y)) {
343                 LyXFont font(LyXFont::ALL_SANE);
344                 int yy = ascent(bv, font) + y -
345                     (ascent_collapsed() +
346                      descent_collapsed() +
347                      inset.ascent(bv, font));
348                 inset.insetButtonRelease(bv, x, yy, button);
349         }
350 }
351
352
353 void InsetCollapsable::insetMotionNotify(BufferView * bv,
354                                          int x, int y, int state)
355 {
356         if (y > button_bottom_y) {
357                 LyXFont font(LyXFont::ALL_SANE);
358                 int yy = ascent(bv, font) + y -
359                     (ascent_collapsed() +
360                      descent_collapsed() +
361                      inset.ascent(bv, font));
362                 inset.insetMotionNotify(bv, x, yy, state);
363         }
364 }
365
366
367 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
368 {
369         inset.insetKeyPress(xke);
370 }
371
372
373 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
374                             bool fragile, bool free_spc) const
375 {
376         return inset.latex(buf, os, fragile, free_spc);
377 }
378
379 #if 0
380 int InsetCollapsable::getMaxWidth(BufferView * bv,
381                                   UpdatableInset const * in) const
382 {
383 #if 0
384         int const w = UpdatableInset::getMaxWidth(bv, in);
385
386         if (w < 0) {
387                 // What does a negative max width signify? (Lgb)
388                 // Use the max width of the draw-area (Jug)
389                 return w;
390         }
391         // should be at least 30 pixels !!!
392         return max(30, w - width_collapsed());
393 #else
394         return UpdatableInset::getMaxWidth(bv, in);
395 #endif
396 }
397 #endif
398
399
400 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
401                               bool reinit)
402 {
403         if (in_update) {
404                 if (reinit && owner()) {
405                         owner()->update(bv, font, true);
406                 }
407                 return;
408         }
409         in_update = true;
410         inset.update(bv, font, reinit);
411         if (reinit && owner()) {
412                 owner()->update(bv, font, true);
413         }
414         in_update = false;
415 }
416
417
418 UpdatableInset::RESULT
419 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
420                                 string const & arg)
421 {
422         UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
423         if (result >= FINISHED)
424                 bv->unlockInset(this);
425         first_after_edit = false;
426         return result;
427 }
428
429
430 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
431 {
432         if (&inset == in)
433                 return true;
434         return inset.lockInsetInInset(bv, in);
435 }
436
437
438 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
439                                           bool lr)
440 {
441         if (&inset == in) {
442                 bv->unlockInset(this);
443                 return true;
444         }
445         return inset.unlockInsetInInset(bv, in, lr);
446 }
447
448
449 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
450 {
451         if (&inset == in)
452                 return true;
453         return inset.updateInsetInInset(bv, in);
454 }
455
456
457 unsigned int InsetCollapsable::insetInInsetY()
458 {
459         return inset.insetInInsetY() - (top_baseline - inset.y());
460 }
461
462
463 void InsetCollapsable::validate(LaTeXFeatures & features) const
464 {
465         inset.validate(features);
466 }
467
468
469 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
470 {
471         inset.getCursorPos(bv, x , y);
472 }
473
474
475 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
476 {
477         inset.toggleInsetCursor(bv);
478 }
479
480
481 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
482 {
483         inset.showInsetCursor(bv, show);
484 }
485
486
487 void InsetCollapsable::hideInsetCursor(BufferView * bv)
488 {
489         inset.hideInsetCursor(bv);
490 }
491
492
493 UpdatableInset * InsetCollapsable::getLockingInset() const
494 {
495         UpdatableInset * in = inset.getLockingInset();
496         if (const_cast<InsetText *>(&inset) == in)
497                 return const_cast<InsetCollapsable *>(this);
498         return in;
499 }
500
501
502 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
503 {
504         if (c == lyxCode())
505                 return this;
506         return inset.getFirstLockingInsetOfType(c);
507 }
508
509
510 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
511                                bool toggleall, bool selectall)
512 {
513         inset.setFont(bv, font, toggleall, selectall);
514 }
515
516
517 bool InsetCollapsable::doClearArea() const
518 {
519         return inset.doClearArea();
520 }
521
522
523 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
524                                        bool const recursive) const
525 {
526         return inset.getLyXText(bv, recursive);
527 }
528
529
530 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
531 {
532         inset.deleteLyXText(bv, recursive);
533 }
534
535
536 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
537 {
538         inset.resizeLyXText(bv, force);
539         LyXFont font(LyXFont::ALL_SANE);
540         oldWidth = width(bv, font);
541 }
542
543
544 std::vector<string> const InsetCollapsable::getLabelList() const
545 {
546         return inset.getLabelList();
547 }
548
549
550 bool InsetCollapsable::nodraw() const
551 {
552         return inset.nodraw();
553 }
554
555  
556 int InsetCollapsable::scroll(bool recursive) const
557 {
558         int sx = UpdatableInset::scroll(false);
559
560         if (recursive)
561                 sx += inset.scroll(recursive);
562
563         return sx;
564 }
565
566
567 Paragraph * InsetCollapsable::getParFromID(int id) const
568 {
569         lyxerr[Debug::INFO] << "Looking for paragraph " << id << endl;
570         return inset.getParFromID(id);
571 }
572
573
574 Paragraph * InsetCollapsable::firstParagraph() const
575 {
576         return inset.firstParagraph();
577 }
578
579
580 Paragraph * InsetCollapsable::getFirstParagraph(int i) const
581 {
582         return inset.getFirstParagraph(i);
583 }
584
585
586 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
587 {
588         return inset.cursor(bv);
589 }
590
591
592 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
593 {
594         if (id_arg == id())
595                 return const_cast<InsetCollapsable *>(this);
596         return inset.getInsetFromID(id_arg);
597 }
598
599
600 void InsetCollapsable::open(BufferView * bv)
601 {
602         if (!collapsed_) return;
603         
604         collapsed_ = false;
605         bv->updateInset(this, false);
606 }
607
608
609 void InsetCollapsable::close(BufferView * bv) const
610 {
611         if (collapsed_)
612                 return;
613         
614         collapsed_ = true;
615         bv->updateInset(const_cast<InsetCollapsable *>(this), false);
616 }
617
618
619 void InsetCollapsable::setLabel(string const & l) const
620 {
621         label = l;
622 }
623
624
625 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
626                                      bool const & cs, bool const & mw)
627 {
628         bool found = inset.searchForward(bv, str, cs, mw);
629         if (first_after_edit && !found)
630                 close(bv);
631         first_after_edit = false;
632         return found;
633 }
634
635
636 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
637                                       bool const & cs, bool const & mw)
638 {
639         bool found = inset.searchBackward(bv, str, cs, mw);
640         if (first_after_edit && !found)
641                 close(bv);
642         first_after_edit = false;
643         return found;
644 }
645
646
647 string const InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv,
648                                               float & value) const
649 {
650         string const str = inset.selectNextWordToSpellcheck(bv, value);
651         if (first_after_edit && str.empty())
652                 close(bv);
653         first_after_edit = false;
654         return str;
655 }