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