]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
050e11fe6ddcedc9c306ac5c9eb473c1a5704b9e
[lyx.git] / src / insets / insetcollapsable.C
1 /**
2  * \file insetcollapsable.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Alejandro Aguilar Sierra
7  * \author Jürgen Vigna
8  * \author Lars Gullik Bjønnes
9  *
10  * Full author contact details are available in file CREDITS
11  */
12
13 #include <config.h>
14
15 #include "insetcollapsable.h"
16 #include "insettext.h"
17
18 #include "BufferView.h"
19 #include "debug.h"
20 #include "dimension.h"
21 #include "gettext.h"
22 #include "lyxfont.h"
23 #include "lyxlex.h"
24 #include "lyxtext.h"
25 #include "WordLangTuple.h"
26 #include "funcrequest.h"
27 #include "buffer.h"
28 #include "metricsinfo.h"
29
30 #include "frontends/font_metrics.h"
31 #include "frontends/Painter.h"
32 #include "frontends/LyXView.h"
33
34 #include "support/LAssert.h"
35 #include "support/LOstream.h"
36
37 using namespace lyx::support;
38 using namespace lyx::graphics;
39
40 using std::vector;
41 using std::ostream;
42 using std::endl;
43 using std::max;
44
45
46 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
47         : UpdatableInset(), collapsed_(collapsed), inset(bp),
48           button_length(0), button_top_y(0), button_bottom_y(0),
49           label("Label"),
50 #if 0
51         autocollapse(false),
52 #endif
53           oldWidth(0), in_update(false), first_after_edit(false)
54 {
55         inset.setOwner(this);
56         inset.setAutoBreakRows(true);
57         inset.setDrawFrame(0, InsetText::ALWAYS);
58         inset.setFrameColor(0, LColor::collapsableframe);
59         setInsetName("Collapsable");
60 }
61
62
63 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
64         : UpdatableInset(in), collapsed_(in.collapsed_),
65           framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
66           button_length(0), button_top_y(0), button_bottom_y(0),
67           label(in.label),
68 #if 0
69           autocollapse(in.autocollapse),
70 #endif
71           oldWidth(0), in_update(false), first_after_edit(false)
72 {
73         inset.init(&(in.inset));
74         inset.setOwner(this);
75 }
76
77
78 bool InsetCollapsable::insertInset(BufferView * bv, InsetOld * in)
79 {
80         if (!insetAllowed(in->lyxCode())) {
81                 lyxerr << "InsetCollapsable::InsertInset: "
82                         "Unable to insert inset." << endl;
83                 return false;
84         }
85         return inset.insertInset(bv, in);
86 }
87
88
89 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
90 {
91         os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
92         inset.writeParagraphData(buf, os);
93 }
94
95
96 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
97 {
98         if (lex.isOK()) {
99                 lex.next();
100                 string const token = lex.getString();
101                 if (token == "collapsed") {
102                         lex.next();
103                         collapsed_ = lex.getBool();
104                 } else {
105                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
106                                << endl;
107                         // Take countermeasures
108                         lex.pushToken(token);
109                 }
110         }
111         inset.read(buf, lex);
112 }
113
114
115 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
116 {
117         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
118         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
119 }
120
121
122 int InsetCollapsable::height_collapsed() const
123 {
124         Dimension dim;
125         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
126         return dim.asc + dim.des;
127 }
128
129
130 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
131 {
132         //lyxerr << "InsetCollapsable::metrics:  width: " << mi.base.textwidth << "\n";
133         dimension_collapsed(dim);
134         if (!collapsed_) {
135                 Dimension insetdim;
136                 inset.metrics(mi, insetdim);
137                 dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
138                 dim.wid = max(dim.wid, insetdim.wid);
139         }
140         dim_ = dim;
141 }
142
143
144 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
145 {
146         pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
147 }
148
149
150 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
151 {
152         Assert(pi.base.bv);
153         cache(pi.base.bv);
154
155         if (nodraw())
156                 return;
157
158         Dimension dim_collapsed;
159         dimension_collapsed(dim_collapsed);
160
161         int const aa    = ascent();
162         button_length   = dim_collapsed.width();
163         button_top_y    = -aa;
164         button_bottom_y = -aa + dim_collapsed.height();
165
166         if (!isOpen()) {
167                 draw_collapsed(pi, x, y);
168                 return;
169         }
170
171         int old_x = x;
172
173         if (!owner())
174                 x += scroll();
175
176         top_x = x;
177         top_baseline = y;
178
179         int const bl = y - aa + dim_collapsed.ascent();
180
181         if (inlined) {
182                 inset.draw(pi, x, y);
183         } else {
184                 draw_collapsed(pi, old_x, bl);
185                 int const yy = bl + dim_collapsed.descent() + inset.ascent();
186                 inset.draw(pi, x, yy);
187         }
188 }
189
190
191 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
192 {
193         // by default, we are not inlined-drawing
194         draw(pi, x, y, false);
195 }
196
197
198 InsetOld::EDITABLE InsetCollapsable::editable() const
199 {
200         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
201 }
202
203
204 void InsetCollapsable::insetUnlock(BufferView * bv)
205 {
206 #if 0
207         if (autocollapse) {
208                 if (change_label_with_text) {
209                         draw_label = get_new_label();
210                 } else {
211                         draw_label = label;
212                 }
213                 collapsed_ = true;
214         }
215 #endif
216         inset.insetUnlock(bv);
217         if (scroll())
218                 scroll(bv, 0.0F);
219         bv->updateInset(this);
220 }
221
222
223 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
224 {
225         FuncRequest cmd1 = cmd;
226         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
227         return cmd1;
228 }
229
230
231 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
232 {
233         bool ret = false;
234         BufferView * bv = cmd.view();
235
236         if (collapsed_ && cmd.button() != mouse_button::button3) {
237                 collapsed_ = false;
238                 inset.setUpdateStatus(InsetText::FULL);
239                 bv->updateInset(this);
240                 bv->buffer()->markDirty();
241                 return;
242         }
243
244         if ((cmd.button() != mouse_button::button3) && (cmd.x < button_length) &&
245             (cmd.y >= button_top_y) && (cmd.y <= button_bottom_y))
246         {
247                 if (collapsed_) {
248                         collapsed_ = false;
249                         inset.setUpdateStatus(InsetText::FULL);
250                         bv->updateInset(this);
251                         bv->buffer()->markDirty();
252                 } else {
253                         collapsed_ = true;
254                         bv->unlockInset(this);
255                         bv->updateInset(this);
256                         bv->buffer()->markDirty();
257                 }
258         } else if (!collapsed_ && (cmd.y > button_bottom_y)) {
259                 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
260         }
261         if (cmd.button() == mouse_button::button3 && !ret)
262                 showInsetDialog(bv);
263 }
264
265
266 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
267                             LatexRunParams const & runparams) const
268 {
269         return inset.latex(buf, os, runparams);
270 }
271
272
273 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
274 {
275         return inset.ascii(buf, os, ll);
276 }
277
278
279 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
280 {
281         return inset.linuxdoc(buf, os);
282 }
283
284
285 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
286 {
287         return inset.docbook(buf, os, mixcont);
288 }
289
290
291 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
292 {
293         lyxerr << "InsetCollapsable::localDispatch: "
294                 << cmd.action << " '" << cmd.argument << "'\n";
295         BufferView * bv = cmd.view();
296         switch (cmd.action) {
297                 case LFUN_INSET_EDIT: {
298                         if (!cmd.argument.empty()) {
299                                 UpdatableInset::localDispatch(cmd);
300                                 if (collapsed_) {
301                                         lyxerr << "branch collapsed_\n";
302                                         collapsed_ = false;
303                                         if (bv->lockInset(this)) {
304                                                 inset.setUpdateStatus(InsetText::FULL);
305                                                 bv->updateInset(this);
306                                                 bv->buffer()->markDirty();
307                                                 inset.localDispatch(cmd);
308                                                 first_after_edit = true;
309                                         }
310                                 } else {
311                                         lyxerr << "branch not collapsed_\n";
312                                         if (bv->lockInset(this))
313                                                 inset.localDispatch(cmd);
314                                 }
315                                 return DISPATCHED;
316                         }
317
318 #ifdef WITH_WARNINGS
319 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
320 #endif
321                         if (cmd.button() == mouse_button::button3)
322                                 return DISPATCHED;
323
324                         UpdatableInset::localDispatch(cmd);
325
326                         if (collapsed_) {
327                                 collapsed_ = false;
328                                 // set this only here as it should be recollapsed only if
329                                 // it was already collapsed!
330                                 first_after_edit = true;
331                                 if (!bv->lockInset(this))
332                                         return DISPATCHED;
333                                 bv->updateInset(this);
334                                 bv->buffer()->markDirty();
335                                 inset.localDispatch(cmd);
336                         } else {
337                                 FuncRequest cmd1 = cmd;
338                                 if (!bv->lockInset(this))
339                                         return DISPATCHED;
340                                 if (cmd.y <= button_bottom_y) {
341                                         cmd1.y = 0;
342                                 } else {
343                                         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
344                                 }
345                                 inset.localDispatch(cmd);
346                         }
347                         return DISPATCHED;
348                 }
349
350                 case LFUN_MOUSE_PRESS:
351                         if (!collapsed_ && cmd.y > button_bottom_y)
352                                 inset.localDispatch(adjustCommand(cmd));
353                         return DISPATCHED;
354
355                 case LFUN_MOUSE_MOTION:
356                         if (!collapsed_ && cmd.y > button_bottom_y)
357                                 inset.localDispatch(adjustCommand(cmd));
358                         return DISPATCHED;
359
360                 case LFUN_MOUSE_RELEASE:
361                         lfunMouseRelease(cmd);
362                         return DISPATCHED;
363
364                 default:
365                         UpdatableInset::RESULT result = inset.localDispatch(cmd);
366                         if (result >= FINISHED)
367                                 bv->unlockInset(this);
368                         first_after_edit = false;
369                         return result;
370         }
371 }
372
373
374 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
375 {
376         if (&inset == in)
377                 return true;
378         return inset.lockInsetInInset(bv, in);
379 }
380
381
382 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
383                                           bool lr)
384 {
385         if (&inset == in) {
386                 bv->unlockInset(this);
387                 return true;
388         }
389         return inset.unlockInsetInInset(bv, in, lr);
390 }
391
392
393 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
394 {
395         if (in == this)
396                 return true;
397         return inset.updateInsetInInset(bv, in);
398 }
399
400
401 int InsetCollapsable::insetInInsetY() const
402 {
403         return inset.insetInInsetY() - (top_baseline - inset.y());
404 }
405
406
407 void InsetCollapsable::validate(LaTeXFeatures & features) const
408 {
409         inset.validate(features);
410 }
411
412
413 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
414 {
415         inset.getCursor(bv, x, y);
416 }
417
418
419 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
420 {
421         inset.getCursorPos(bv, x , y);
422 }
423
424
425 UpdatableInset * InsetCollapsable::getLockingInset() const
426 {
427         UpdatableInset * in = inset.getLockingInset();
428         if (&inset == in)
429                 return const_cast<InsetCollapsable *>(this);
430         return in;
431 }
432
433
434 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
435 {
436         if (c == lyxCode())
437                 return this;
438         return inset.getFirstLockingInsetOfType(c);
439 }
440
441
442 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
443                                bool toggleall, bool selectall)
444 {
445         inset.setFont(bv, font, toggleall, selectall);
446 }
447
448
449 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
450                                        bool const recursive) const
451 {
452         return inset.getLyXText(bv, recursive);
453 }
454
455
456 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
457 {
458         inset.deleteLyXText(bv, recursive);
459 }
460
461
462 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
463 {
464         inset.resizeLyXText(bv, force);
465         oldWidth = width();
466 }
467
468
469 void InsetCollapsable::getLabelList(std::vector<string> & list) const
470 {
471         inset.getLabelList(list);
472 }
473
474
475 bool InsetCollapsable::nodraw() const
476 {
477         return inset.nodraw();
478 }
479
480
481 int InsetCollapsable::scroll(bool recursive) const
482 {
483         int sx = UpdatableInset::scroll(false);
484
485         if (recursive)
486                 sx += inset.scroll(recursive);
487
488         return sx;
489 }
490
491
492 ParagraphList * InsetCollapsable::getParagraphs(int i) const
493 {
494         return inset.getParagraphs(i);
495 }
496
497
498 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
499 {
500         return inset.cursor(bv);
501 }
502
503
504 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
505 {
506         if (id_arg == id())
507                 return const_cast<InsetCollapsable *>(this);
508         return inset.getInsetFromID(id_arg);
509 }
510
511
512 void InsetCollapsable::open(BufferView * bv)
513 {
514         if (!collapsed_) return;
515
516         collapsed_ = false;
517         bv->updateInset(this);
518 }
519
520
521 void InsetCollapsable::close(BufferView * bv) const
522 {
523         if (collapsed_)
524                 return;
525
526         collapsed_ = true;
527         bv->updateInset(const_cast<InsetCollapsable *>(this));
528 }
529
530
531 void InsetCollapsable::setLabel(string const & l) const
532 {
533         label = l;
534 }
535
536
537 void InsetCollapsable::markErased()
538 {
539         inset.markErased();
540 }
541
542
543 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
544 {
545         bool found = inset.nextChange(bv, length);
546
547         if (first_after_edit && !found)
548                 close(bv);
549         else if (!found)
550                 first_after_edit = false;
551         return found;
552 }
553
554
555 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
556                                      bool cs, bool mw)
557 {
558         bool found = inset.searchForward(bv, str, cs, mw);
559         if (first_after_edit && !found)
560                 close(bv);
561         else if (!found)
562                 first_after_edit = false;
563         return found;
564 }
565
566
567 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
568                                       bool cs, bool mw)
569 {
570         bool found = inset.searchBackward(bv, str, cs, mw);
571         if (first_after_edit && !found)
572                 close(bv);
573         else if (!found)
574                 first_after_edit = false;
575         return found;
576 }
577
578
579 WordLangTuple const
580 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
581 {
582         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
583         if (first_after_edit && word.word().empty())
584                 close(bv);
585         first_after_edit = false;
586         return word;
587 }
588
589
590 void InsetCollapsable::addPreview(PreviewLoader & loader) const
591 {
592         inset.addPreview(loader);
593 }
594
595
596 void InsetCollapsable::cache(BufferView * bv) const
597 {
598         view_ = bv->owner()->view();
599 }
600
601
602 BufferView * InsetCollapsable::view() const
603 {
604         return view_.lock().get();
605 }