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