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