]> git.lyx.org Git - features.git/blob - src/insets/insetcollapsable.C
3377fe3236961a420f6a960e04aa4ee6fb791a9c
[features.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
39 using std::vector;
40 using std::ostream;
41 using std::endl;
42 using std::max;
43
44
45 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
46         : UpdatableInset(), collapsed_(collapsed), inset(bp),
47           button_length(0), button_top_y(0), button_bottom_y(0),
48           label("Label"),
49 #if 0
50         autocollapse(false),
51 #endif
52           oldWidth(0), in_update(false), first_after_edit(false)
53 {
54         inset.setOwner(this);
55         inset.setAutoBreakRows(true);
56         inset.setDrawFrame(0, InsetText::ALWAYS);
57         inset.setFrameColor(0, LColor::collapsableframe);
58         setInsetName("Collapsable");
59 }
60
61
62 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
63         : UpdatableInset(in), collapsed_(in.collapsed_),
64           framecolor(in.framecolor), labelfont(in.labelfont), inset(in.inset),
65           button_length(0), button_top_y(0), button_bottom_y(0),
66           label(in.label),
67 #if 0
68           autocollapse(in.autocollapse),
69 #endif
70           oldWidth(0), in_update(false), first_after_edit(false)
71 {
72         inset.init(&(in.inset));
73         inset.setOwner(this);
74 }
75
76
77 bool InsetCollapsable::insertInset(BufferView * bv, Inset * in)
78 {
79         if (!insetAllowed(in->lyxCode())) {
80                 lyxerr << "InsetCollapsable::InsertInset: "
81                         "Unable to insert inset." << endl;
82                 return false;
83         }
84         return inset.insertInset(bv, in);
85 }
86
87
88 void InsetCollapsable::write(Buffer const * buf, ostream & os) const
89 {
90         os << "collapsed " << (collapsed_ ? "true" : "false") << "\n";
91         inset.writeParagraphData(buf, os);
92 }
93
94
95 void InsetCollapsable::read(Buffer const * buf, LyXLex & lex)
96 {
97         if (lex.isOK()) {
98                 lex.next();
99                 string const token = lex.getString();
100                 if (token == "collapsed") {
101                         lex.next();
102                         collapsed_ = lex.getBool();
103                 } else {
104                         lyxerr << "InsetCollapsable::Read: Missing collapsed!"
105                                << endl;
106                         // Take countermeasures
107                         lex.pushToken(token);
108                 }
109         }
110         inset.read(buf, lex);
111 }
112
113
114 void InsetCollapsable::dimension_collapsed(Dimension & dim) const
115 {
116         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
117         dim.wid += 2 * TEXT_TO_INSET_OFFSET;
118 }
119
120
121 int InsetCollapsable::height_collapsed() const
122 {
123         Dimension dim;
124         font_metrics::buttonText(label, labelfont, dim.wid, dim.asc, dim.des);
125         return dim.asc + dim.des;
126 }
127
128
129 void InsetCollapsable::metrics(MetricsInfo & mi, Dimension & dim) const
130 {
131         dimension_collapsed(dim);
132         if (collapsed_)
133                 return;
134         Dimension insetdim;
135         inset.metrics(mi, insetdim);
136         dim.des += insetdim.height() + TEXT_TO_BOTTOM_OFFSET;
137         dim.wid = max(dim.wid, insetdim.wid);
138 }
139
140
141 void InsetCollapsable::draw_collapsed(PainterInfo & pi, int x, int y) const
142 {
143         pi.pain.buttonText(x + TEXT_TO_INSET_OFFSET, y, label, labelfont);
144 }
145
146
147 void InsetCollapsable::draw(PainterInfo & pi, int x, int y, bool inlined) const
148 {
149         Assert(pi.base.bv);
150         cache(pi.base.bv);
151
152         if (nodraw())
153                 return;
154
155         Dimension dim_collapsed;
156         dimension_collapsed(dim_collapsed);
157
158         int const aa    = ascent(pi.base.bv, pi.base.font);
159         button_length   = dim_collapsed.width();
160         button_top_y    = -aa;
161         button_bottom_y = -aa + dim_collapsed.height();
162
163         if (!isOpen()) {
164                 draw_collapsed(pi, x, y);
165                 return;
166         }
167
168         int old_x = x;
169
170         if (!owner())
171                 x += scroll();
172
173         top_x = x;
174         top_baseline = y;
175
176         int const bl = y - aa + dim_collapsed.ascent();
177
178         if (inlined) {
179                 inset.draw(pi, x, y);
180         } else {
181                 draw_collapsed(pi, old_x, bl);
182                 int const yy = bl + dim_collapsed.descent()
183                         + inset.ascent(pi.base.bv, pi.base.font);
184                 inset.draw(pi, x, yy);
185         }
186 }
187
188
189 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
190 {
191         // by default, we are not inlined-drawing
192         draw(pi, x, y, false);
193 }
194
195
196 Inset::EDITABLE InsetCollapsable::editable() const
197 {
198         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
199 }
200
201
202 void InsetCollapsable::insetUnlock(BufferView * bv)
203 {
204 #if 0
205         if (autocollapse) {
206                 if (change_label_with_text) {
207                         draw_label = get_new_label();
208                 } else {
209                         draw_label = label;
210                 }
211                 collapsed_ = true;
212         }
213 #endif
214         inset.insetUnlock(bv);
215         if (scroll())
216                 scroll(bv, 0.0F);
217         bv->updateInset(this);
218 }
219
220
221 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
222 {
223         LyXFont font(LyXFont::ALL_SANE);
224         FuncRequest cmd1 = cmd;
225         cmd1.y = ascent(cmd.view(), font) + cmd.y -
226             (height_collapsed() + inset.ascent(cmd.view(), font));
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(bv, 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(bv, 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 void InsetCollapsable::update(BufferView * bv, bool reinit)
292 {
293         if (in_update) {
294                 if (reinit && owner()) {
295                         owner()->update(bv, true);
296                 }
297                 return;
298         }
299         in_update = true;
300         inset.update(bv, reinit);
301         if (reinit && owner()) {
302                 owner()->update(bv, true);
303         }
304         in_update = false;
305 }
306
307
308 Inset::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
309 {
310         //lyxerr << "InsetCollapsable::localDispatch: " << cmd.action << "\n";
311         BufferView * bv = cmd.view();
312         switch (cmd.action) {
313                 case LFUN_INSET_EDIT: {
314                         if (!cmd.argument.empty()) {
315                                 UpdatableInset::localDispatch(cmd);
316                                 if (collapsed_) {
317                                         collapsed_ = false;
318                                         if (bv->lockInset(this)) {
319                                                 inset.setUpdateStatus(bv, InsetText::FULL);
320                                                 bv->updateInset(this);
321                                                 bv->buffer()->markDirty();
322                                                 inset.localDispatch(cmd);
323                                                 first_after_edit = true;
324                                         }
325                                 } else {
326                                         if (bv->lockInset(this))
327                                                 inset.localDispatch(cmd);
328                                 }
329                                 return DISPATCHED;
330                         }
331
332 #ifdef WITH_WARNINGS
333 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
334 #endif
335                         if (cmd.button() == mouse_button::button3)
336                                 return DISPATCHED;
337
338                         UpdatableInset::localDispatch(cmd);
339
340                         if (collapsed_) {
341                                 collapsed_ = false;
342                                 // set this only here as it should be recollapsed only if
343                                 // it was already collapsed!
344                                 first_after_edit = true;
345                                 if (!bv->lockInset(this))
346                                         return DISPATCHED;
347                                 bv->updateInset(this);
348                                 bv->buffer()->markDirty();
349                                 inset.localDispatch(cmd);
350                         } else {
351                                 FuncRequest cmd1 = cmd;
352                                 if (!bv->lockInset(this))
353                                         return DISPATCHED;
354                                 if (cmd.y <= button_bottom_y) {
355                                         cmd1.y = 0;
356                                 } else {
357                                         LyXFont font(LyXFont::ALL_SANE);
358                                         cmd1.y = ascent(bv, font) + cmd.y -
359                                                 (height_collapsed() + inset.ascent(bv, font));
360                                 }
361                                 inset.localDispatch(cmd);
362                         }
363                         return DISPATCHED;
364                 }
365
366                 case LFUN_MOUSE_PRESS:
367                         if (!collapsed_ && cmd.y > button_bottom_y)
368                                 inset.localDispatch(adjustCommand(cmd));
369                         return DISPATCHED;
370
371                 case LFUN_MOUSE_MOTION:
372                         if (!collapsed_ && cmd.y > button_bottom_y)
373                                 inset.localDispatch(adjustCommand(cmd));
374                         return DISPATCHED;
375
376                 case LFUN_MOUSE_RELEASE:
377                         lfunMouseRelease(cmd);
378                         return DISPATCHED;
379
380                 default:
381                         UpdatableInset::RESULT result = inset.localDispatch(cmd);
382                         if (result >= FINISHED)
383                                 bv->unlockInset(this);
384                         first_after_edit = false;
385                         return result;
386         }
387 }
388
389
390 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
391 {
392         if (&inset == in)
393                 return true;
394         return inset.lockInsetInInset(bv, in);
395 }
396
397
398 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
399                                           bool lr)
400 {
401         if (&inset == in) {
402                 bv->unlockInset(this);
403                 return true;
404         }
405         return inset.unlockInsetInInset(bv, in, lr);
406 }
407
408
409 bool InsetCollapsable::updateInsetInInset(BufferView * bv, Inset *in)
410 {
411         if (in == this)
412                 return true;
413         return inset.updateInsetInInset(bv, in);
414 }
415
416
417 int InsetCollapsable::insetInInsetY() const
418 {
419         return inset.insetInInsetY() - (top_baseline - inset.y());
420 }
421
422
423 void InsetCollapsable::validate(LaTeXFeatures & features) const
424 {
425         inset.validate(features);
426 }
427
428
429 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
430 {
431         inset.getCursor(bv, x, y);
432 }
433
434
435 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
436 {
437         inset.getCursorPos(bv, x , y);
438 }
439
440
441 UpdatableInset * InsetCollapsable::getLockingInset() const
442 {
443         UpdatableInset * in = inset.getLockingInset();
444         if (const_cast<InsetText *>(&inset) == in)
445                 return const_cast<InsetCollapsable *>(this);
446         return in;
447 }
448
449
450 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(Inset::Code c)
451 {
452         if (c == lyxCode())
453                 return this;
454         return inset.getFirstLockingInsetOfType(c);
455 }
456
457
458 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
459                                bool toggleall, bool selectall)
460 {
461         inset.setFont(bv, font, toggleall, selectall);
462 }
463
464
465 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
466                                        bool const recursive) const
467 {
468         return inset.getLyXText(bv, recursive);
469 }
470
471
472 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
473 {
474         inset.deleteLyXText(bv, recursive);
475 }
476
477
478 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
479 {
480         inset.resizeLyXText(bv, force);
481         LyXFont font(LyXFont::ALL_SANE);
482         oldWidth = width(bv, font);
483 }
484
485
486 void InsetCollapsable::getLabelList(std::vector<string> & list) const
487 {
488         inset.getLabelList(list);
489 }
490
491
492 bool InsetCollapsable::nodraw() const
493 {
494         return inset.nodraw();
495 }
496
497
498 int InsetCollapsable::scroll(bool recursive) const
499 {
500         int sx = UpdatableInset::scroll(false);
501
502         if (recursive)
503                 sx += inset.scroll(recursive);
504
505         return sx;
506 }
507
508
509 ParagraphList * InsetCollapsable::getParagraphs(int i) const
510 {
511         return inset.getParagraphs(i);
512 }
513
514
515 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
516 {
517         return inset.cursor(bv);
518 }
519
520
521 Inset * InsetCollapsable::getInsetFromID(int id_arg) const
522 {
523         if (id_arg == id())
524                 return const_cast<InsetCollapsable *>(this);
525         return inset.getInsetFromID(id_arg);
526 }
527
528
529 void InsetCollapsable::open(BufferView * bv)
530 {
531         if (!collapsed_) return;
532
533         collapsed_ = false;
534         bv->updateInset(this);
535 }
536
537
538 void InsetCollapsable::close(BufferView * bv) const
539 {
540         if (collapsed_)
541                 return;
542
543         collapsed_ = true;
544         bv->updateInset(const_cast<InsetCollapsable *>(this));
545 }
546
547
548 void InsetCollapsable::setLabel(string const & l) const
549 {
550         label = l;
551 }
552
553
554 void InsetCollapsable::markErased()
555 {
556         inset.markErased();
557 }
558
559
560 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
561 {
562         bool found = inset.nextChange(bv, length);
563
564         if (first_after_edit && !found)
565                 close(bv);
566         else if (!found)
567                 first_after_edit = false;
568         return found;
569 }
570
571
572 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
573                                      bool cs, bool mw)
574 {
575         bool found = inset.searchForward(bv, str, cs, mw);
576         if (first_after_edit && !found)
577                 close(bv);
578         else if (!found)
579                 first_after_edit = false;
580         return found;
581 }
582
583
584 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
585                                       bool cs, bool mw)
586 {
587         bool found = inset.searchBackward(bv, str, cs, mw);
588         if (first_after_edit && !found)
589                 close(bv);
590         else if (!found)
591                 first_after_edit = false;
592         return found;
593 }
594
595
596 WordLangTuple const
597 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
598 {
599         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
600         if (first_after_edit && word.word().empty())
601                 close(bv);
602         first_after_edit = false;
603         return word;
604 }
605
606
607 void InsetCollapsable::addPreview(grfx::PreviewLoader & loader) const
608 {
609         inset.addPreview(loader);
610 }
611
612
613 void InsetCollapsable::cache(BufferView * bv) const
614 {
615         view_ = bv->owner()->view();
616 }
617
618
619 BufferView * InsetCollapsable::view() const
620 {
621         return view_.lock().get();
622 }