]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
596a8cb39f502457ccae00c119d9338801ef8d96
[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
28 class LyXText;
29
30 using std::ostream;
31 using std::endl;
32 using std::max;
33
34
35 InsetCollapsable::InsetCollapsable(bool collapsed)
36         : UpdatableInset(), collapsed_(collapsed), 
37           button_length(0), button_top_y(0), button_bottom_y(0),
38           label("Label"), draw_label(label), autocollapse(true), 
39           oldWidth(0), need_update(FULL),
40           inlined(false), change_label_with_text(false)
41           
42
43 {
44         inset.setOwner(this);
45         inset.setAutoBreakRows(true);
46         inset.setDrawFrame(0, InsetText::ALWAYS);
47         inset.setFrameColor(0, LColor::collapsableframe);
48         setInsetName("Collapsable");
49 }
50
51
52 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
53 {
54         if (!insetAllowed(in->lyxCode())) {
55                 lyxerr << "InsetCollapsable::InsertInset: "
56                         "Unable to insert inset." << endl;
57                 return false;
58         }
59         return inset.insertInset(bv, in);
60 }
61
62
63 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
64 {
65         os << "collapsed " << tostr(collapsed_) << "\n";
66         inset.writeParagraphData(buf, os);
67 }
68
69
70
71 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
72 {
73         if (lex.IsOK()) {
74                 lex.next();
75                 string const token = lex.GetString();
76                 if (token == "collapsed") {
77                         lex.next();
78                         collapsed_ = lex.GetBool();
79                 } else {
80                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
81                                << endl;
82                 }
83         }
84         inset.read(buf, lex);
85         if (collapsed_ && change_label_with_text) {
86                 draw_label = get_new_label();
87         } else {
88                 draw_label = label;
89         }
90 }
91
92
93 int InsetCollapsable::ascent_collapsed(Painter & pain) const
94 {
95         int width = 0;
96         int ascent = 0;
97         int descent = 0;
98         pain.buttonText(0, 0, draw_label, labelfont, false, 
99                         width, ascent, descent);
100         return ascent;
101 }
102
103
104 int InsetCollapsable::descent_collapsed(Painter & pain) const
105 {
106         int width = 0;
107         int ascent = 0;
108         int descent = 0;
109         pain.buttonText(0, 0, draw_label, labelfont, false, 
110                         width, ascent, descent);
111         return descent;
112 }
113
114
115 int InsetCollapsable::width_collapsed(Painter & pain) const
116 {
117         int width;
118         int ascent;
119         int descent;
120         pain.buttonText(TEXT_TO_INSET_OFFSET, 0, draw_label, labelfont, false,
121                         width, ascent, descent);
122         return width + (2*TEXT_TO_INSET_OFFSET);
123 }
124
125
126 int InsetCollapsable::ascent(BufferView * bv, LyXFont const &) const
127 {
128         return ascent_collapsed(bv->painter());
129 }
130
131
132 int InsetCollapsable::descent(BufferView * bv, LyXFont const & font) const
133 {
134         if (collapsed_) 
135                 return descent_collapsed(bv->painter());
136
137         return descent_collapsed(bv->painter())
138                 + inset.descent(bv, font)
139                 + inset.ascent(bv, font)
140                 + TEXT_TO_BOTTOM_OFFSET;
141 }
142
143
144 int InsetCollapsable::width(BufferView * bv, LyXFont const & font) const
145 {
146         if (collapsed_) 
147                 return width_collapsed(bv->painter());
148
149         int widthCollapsed = width_collapsed(bv->painter());
150
151         return (inset.width(bv, font) > widthCollapsed) ?
152                 inset.width(bv, font) : widthCollapsed;
153 }
154
155
156 void InsetCollapsable::draw_collapsed(Painter & pain, int baseline, float & x) const
157 {
158         int width = 0;
159         pain.buttonText(int(x) + TEXT_TO_INSET_OFFSET,
160                         baseline, draw_label, labelfont, true, width);
161         x += width + TEXT_TO_INSET_OFFSET;
162 }
163
164
165 void InsetCollapsable::draw(BufferView * bv, LyXFont const & f, 
166                             int baseline, float & x, bool cleared) const
167 {
168         if (nodraw())
169                 return;
170
171         Painter & pain = bv->painter();
172
173         button_length = width_collapsed(pain);
174         button_top_y = -ascent(bv, f);
175         button_bottom_y = -ascent(bv, f) + ascent_collapsed(pain) +
176                 descent_collapsed(pain);
177
178         if (collapsed_) {
179                 draw_collapsed(pain, baseline, x);
180                 x += TEXT_TO_INSET_OFFSET;
181                 return;
182         }
183
184         float old_x = x;
185
186 #if 0
187         UpdatableInset::draw(bv, f, baseline, x, cleared);
188 #else
189         if (!owner())
190                 x += static_cast<float>(scroll());
191 #endif
192         if (!cleared && (inset.need_update == InsetText::FULL ||
193                          inset.need_update == InsetText::INIT ||
194                          top_x != int(x) ||
195                          top_baseline != baseline))
196         {
197 #if 1
198                 // we don't need anymore to clear here we just have to tell
199                 // the underlying LyXText that it should do the RowClear!
200                 inset.setUpdateStatus(bv, InsetText::FULL);
201                 bv->text->status(bv, LyXText::CHANGED_IN_DRAW);
202                 return;
203 #else
204                 int w =  owner() ? width(bv, f) : pain.paperWidth();
205                 int h = ascent(bv, f) + descent(bv, f);
206                 int const tx = (needFullRow() && !owner()) ? 0 : int(x);
207                 int const ty = max(0, baseline - ascent(bv, f));
208
209                 if ((ty + h) > pain.paperHeight())
210                         h = pain.paperHeight();
211                 if ((top_x + w) > pain.paperWidth())
212                         w = pain.paperWidth();
213                 if (baseline < 0)
214                         h += (baseline - ascent(bv, f));
215                 pain.fillRectangle(tx, ty - 1, w, h + 2);
216                 cleared = true;
217 #endif
218         }
219
220         top_x = int(x);
221         top_baseline = baseline;
222
223         int const bl = baseline - ascent(bv, f) + ascent_collapsed(pain);
224
225         draw_collapsed(pain, bl, old_x);
226         inset.draw(bv, f, 
227                    bl + descent_collapsed(pain) + inset.ascent(bv, f),
228                    x, cleared);
229         need_update = NONE;
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                 draw_label = label;
240                 collapsed_ = false;
241                 if (!bv->lockInset(this))
242                         return;
243                 bv->updateInset(this, false);
244                 inset.edit(bv, 0, 0, button);
245         } else {
246                 if (!bv->lockInset(this))
247                         return;
248                 LyXFont font(LyXFont::ALL_SANE);
249                 int yy = ascent(bv, font) + yp -
250                     (ascent_collapsed(bv->painter()) +
251                      descent_collapsed(bv->painter()) +
252                      inset.ascent(bv, font));
253                 inset.edit(bv, xp, yy, button);
254         }
255 }
256
257
258 void InsetCollapsable::edit(BufferView * bv, bool front)
259 {
260         UpdatableInset::edit(bv, front);
261
262         if (collapsed_) {
263                 draw_label = label;
264                 collapsed_ = false;
265                 if (!bv->lockInset(this))
266                         return;
267                 bv->updateInset(this, false);
268                 inset.edit(bv, front);
269         } else {
270                 if (!bv->lockInset(this))
271                         return;
272                 inset.edit(bv, front);
273         }
274 }
275
276
277 Inset::EDITABLE InsetCollapsable::editable() const
278 {
279         if (collapsed_)
280                 return IS_EDITABLE;
281         return HIGHLY_EDITABLE;
282 }
283
284
285 void InsetCollapsable::insetUnlock(BufferView * bv)
286 {
287         if (autocollapse) {
288                 if (change_label_with_text) {
289                         draw_label = get_new_label();
290                 } else {
291                         draw_label = label;
292                 }
293                 collapsed_ = true;
294         }
295         inset.insetUnlock(bv);
296         if (scroll())
297                 scroll(bv, 0.0F);
298         bv->updateInset(this, false);
299 }
300
301
302 void InsetCollapsable::insetButtonPress(BufferView * bv, int x, int y,
303                                         int button)
304 {
305         if (!collapsed_ && (y > button_bottom_y)) {
306                 LyXFont font(LyXFont::ALL_SANE);
307                 int yy = ascent(bv, font) + y -
308                     (ascent_collapsed(bv->painter()) +
309                      descent_collapsed(bv->painter()) +
310                      inset.ascent(bv, font));
311                 inset.insetButtonPress(bv, x, yy, button);
312         }
313 }
314
315
316 void InsetCollapsable::insetButtonRelease(BufferView * bv,
317                                           int x, int y, int button)
318 {
319         if ((x >= 0)  && (x < button_length) &&
320             (y >= button_top_y) &&  (y <= button_bottom_y)) {
321                 if (collapsed_) {
322                         draw_label = label;
323                         collapsed_ = false;
324                         inset.insetButtonRelease(bv, 0, 0, button);
325                         bv->updateInset(this, false);
326                 } else {
327                         if (change_label_with_text) {
328                                 draw_label = get_new_label();
329                         } else {
330                                 draw_label = label;
331                         }
332                         collapsed_ = true;
333                         bv->unlockInset(this);
334                         bv->updateInset(this, false);
335                 }
336         } else if (!collapsed_ && (y > button_top_y)) {
337                 LyXFont font(LyXFont::ALL_SANE);
338                 int yy = ascent(bv, font) + y -
339                     (ascent_collapsed(bv->painter()) +
340                      descent_collapsed(bv->painter()) +
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 (x > button_bottom_y) {
351                 LyXFont font(LyXFont::ALL_SANE);
352                 int yy = ascent(bv, font) + y -
353                     (ascent_collapsed(bv->painter()) +
354                      descent_collapsed(bv->painter()) +
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(bv->painter()));
386 }
387
388
389 void InsetCollapsable::update(BufferView * bv, LyXFont const & font,
390                               bool reinit)
391 {
392         inset.update(bv, font, reinit);
393 }
394
395
396 UpdatableInset::RESULT
397 InsetCollapsable::localDispatch(BufferView * bv, kb_action action,
398                                 string const & arg)
399 {
400         UpdatableInset::RESULT result = inset.localDispatch(bv, action, arg);
401         if (result == FINISHED)
402                 bv->unlockInset(this);
403         return result;
404 }
405
406
407 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
408 {
409         if (&inset == in)
410                 return true;
411         return inset.lockInsetInInset(bv, in);
412 }
413
414
415 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
416                                           bool lr)
417 {
418         if (&inset == in) {
419                 bv->unlockInset(this);
420                 return true;
421         }
422         return inset.unlockInsetInInset(bv, in, lr);
423 }
424
425
426 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
427 {
428         if (&inset == in)
429                 return true;
430         return inset.updateInsetInInset(bv, in);
431 }
432
433
434 unsigned int InsetCollapsable::insetInInsetY()
435 {
436         return inset.insetInInsetY() - (top_baseline - inset.y());
437 }
438
439
440 void InsetCollapsable::validate(LaTeXFeatures & features) const
441 {
442         inset.validate(features);
443 }
444
445
446 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
447 {
448         inset.getCursorPos(bv, x , y);
449 }
450
451
452 void InsetCollapsable::toggleInsetCursor(BufferView * bv)
453 {
454         inset.toggleInsetCursor(bv);
455 }
456
457
458 void InsetCollapsable::showInsetCursor(BufferView * bv, bool show)
459 {
460         inset.showInsetCursor(bv, show);
461 }
462
463
464 void InsetCollapsable::hideInsetCursor(BufferView * bv)
465 {
466         inset.hideInsetCursor(bv);
467 }
468
469
470 UpdatableInset * InsetCollapsable::getLockingInset() const
471 {
472         UpdatableInset * in = inset.getLockingInset();
473         if (const_cast<InsetText *>(&inset) == in)
474                 return const_cast<InsetCollapsable *>(this);
475         return in;
476 }
477
478
479 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
480 {
481         if (c == lyxCode())
482                 return this;
483         return inset.getFirstLockingInsetOfType(c);
484 }
485
486
487 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
488                                bool toggleall, bool selectall)
489 {
490         inset.setFont(bv, font, toggleall, selectall);
491 }
492
493
494 bool InsetCollapsable::doClearArea() const
495 {
496         return inset.doClearArea();
497 }
498
499
500 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
501                                        bool const recursive) const
502 {
503         return inset.getLyXText(bv, recursive);
504 }
505
506
507 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
508 {
509         inset.deleteLyXText(bv, recursive);
510 }
511
512
513 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
514 {
515         inset.resizeLyXText(bv, force);
516         LyXFont font(LyXFont::ALL_SANE);
517         oldWidth = width(bv, font);
518 }
519
520
521 std::vector<string> const InsetCollapsable::getLabelList() const
522 {
523         return inset.getLabelList();
524 }
525
526
527 bool InsetCollapsable::nodraw() const
528 {
529         return inset.nodraw();
530 }
531
532  
533 int InsetCollapsable::scroll(bool recursive) const
534 {
535         int sx = UpdatableInset::scroll(false);
536
537         if (recursive)
538                 sx += inset.scroll(recursive);
539
540         return sx;
541 }
542
543
544 Paragraph * InsetCollapsable::getParFromID(int id) const
545 {
546         return inset.getParFromID(id);
547 }
548
549
550 Paragraph * InsetCollapsable::firstParagraph() const
551 {
552         return inset.firstParagraph();
553 }
554
555
556 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
557 {
558         return inset.cursor(bv);
559 }
560
561
562 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
563 {
564         if (id_arg == id())
565                 return const_cast<InsetCollapsable *>(this);
566         return inset.getInsetFromID(id_arg);
567 }
568
569 void InsetCollapsable::open(BufferView * bv, bool flag)
570 {
571         if (flag == !collapsed_)
572                 return;
573         collapsed_ = !flag;
574         if (collapsed_ && change_label_with_text) {
575                 draw_label = get_new_label();
576         } else {
577                 draw_label = label;
578         }
579         bv->updateInset(this, false);
580 }
581
582
583 void InsetCollapsable::setLabel(string const & l, bool flag)
584 {
585         label = l;
586         change_label_with_text = flag;
587         if (collapsed_ && change_label_with_text) {
588                 draw_label = get_new_label();
589         } else {
590                 draw_label = label;
591         }
592 }
593
594
595 string InsetCollapsable::get_new_label() const
596 {
597         string la;
598
599         int n = std::min(10, inset.paragraph()->size());
600         int i,j;
601         for(i=0,j=0; i < n && j < inset.paragraph()->size(); ++j) {
602                 if (inset.paragraph()->isInset(j))
603                         continue;
604                 la += inset.paragraph()->getChar(j);
605                 ++i;
606         }
607         if ((i > 0) && (j < inset.paragraph()->size()))
608                 la += "...";
609         if (la.empty())
610                 la = label;
611         return la;
612 }