]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
for a change...
[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                 // set this only here as it should be recollapsed only if
242                 // it was already collapsed!
243                 first_after_edit = true;
244                 if (!bv->lockInset(this))
245                         return;
246                 bv->updateInset(this, false);
247                 inset.edit(bv);
248         } else {
249                 if (!bv->lockInset(this))
250                         return;
251                 if (yp <= button_bottom_y) {
252                         inset.edit(bv);
253                 } else {
254                         LyXFont font(LyXFont::ALL_SANE);
255                         int yy = ascent(bv, font) + yp -
256                                 (ascent_collapsed() +
257                                  descent_collapsed() +
258                                  inset.ascent(bv, font));
259                         inset.edit(bv, xp, yy, button);
260                 }
261         }
262 }
263
264
265 void InsetCollapsable::edit(BufferView * bv, bool front)
266 {
267         UpdatableInset::edit(bv, front);
268
269         if (collapsed_) {
270                 collapsed_ = false;
271                 if (!bv->lockInset(this))
272                         return;
273                 inset.setUpdateStatus(bv, InsetText::FULL);
274                 bv->updateInset(this, false);
275                 inset.edit(bv, front);
276         } else {
277                 if (!bv->lockInset(this))
278                         return;
279                 inset.edit(bv, front);
280         }
281         first_after_edit = true;
282 }
283
284
285 Inset::EDITABLE InsetCollapsable::editable() const
286 {
287         if (collapsed_)
288                 return IS_EDITABLE;
289         return HIGHLY_EDITABLE;
290 }
291
292
293 void InsetCollapsable::insetUnlock(BufferView * bv)
294 {
295 #if 0
296         if (autocollapse) {
297                 if (change_label_with_text) {
298                         draw_label = get_new_label();
299                 } else {
300                         draw_label = label;
301                 }
302                 collapsed_ = true;
303         }
304 #endif
305         inset.insetUnlock(bv);
306         if (scroll())
307                 scroll(bv, 0.0F);
308         bv->updateInset(this, false);
309 }
310
311
312 void InsetCollapsable::insetButtonPress(BufferView * bv,
313                                         int x, int y, int button)
314 {
315         if (!collapsed_ && (y > button_bottom_y)) {
316                 LyXFont font(LyXFont::ALL_SANE);
317                 int yy = ascent(bv, font) + y -
318                     (ascent_collapsed() +
319                      descent_collapsed() +
320                      inset.ascent(bv, font));
321                 inset.insetButtonPress(bv, x, yy, button);
322         }
323 }
324
325
326 void InsetCollapsable::insetButtonRelease(BufferView * bv,
327                                           int x, int y, int button)
328 {
329         if ((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                 inset.insetButtonRelease(bv, x, yy, button);
350         }
351 }
352
353
354 void InsetCollapsable::insetMotionNotify(BufferView * bv,
355                                          int x, int y, int state)
356 {
357         if (y > button_bottom_y) {
358                 LyXFont font(LyXFont::ALL_SANE);
359                 int yy = ascent(bv, font) + y -
360                     (ascent_collapsed() +
361                      descent_collapsed() +
362                      inset.ascent(bv, font));
363                 inset.insetMotionNotify(bv, x, yy, state);
364         }
365 }
366
367
368 void InsetCollapsable::insetKeyPress(XKeyEvent * xke)
369 {
370         inset.insetKeyPress(xke);
371 }
372
373
374 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
375                             bool fragile, bool free_spc) const
376 {
377         return inset.latex(buf, os, fragile, free_spc);
378 }
379
380
381 int InsetCollapsable::getMaxWidth(BufferView * bv,
382                                   UpdatableInset const * in) const
383 {
384 #if 0
385         int const w = UpdatableInset::getMaxWidth(bv, in);
386
387         if (w < 0) {
388                 // What does a negative max width signify? (Lgb)
389                 // Use the max width of the draw-area (Jug)
390                 return w;
391         }
392         // should be at least 30 pixels !!!
393         return max(30, w - width_collapsed());
394 #else
395         return UpdatableInset::getMaxWidth(bv, in);
396 #endif
397 }
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 }