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