]> git.lyx.org Git - features.git/blob - src/insets/insetcollapsable.C
e3a889d80fc81a6f18247cbe0b989341d46d2e73
[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
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
27 #include "frontends/font_metrics.h"
28 #include "frontends/Painter.h"
29 #include "frontends/LyXView.h"
30
31
32 using lyx::graphics::PreviewLoader;
33
34 using std::endl;
35 using std::string;
36 using std::max;
37 using std::ostream;
38
39
40 InsetCollapsable::InsetCollapsable(BufferParams const & bp, bool collapsed)
41         : UpdatableInset(), inset(bp), collapsed_(collapsed),
42           button_dim(0, 0, 0, 0), label("Label"),
43 #if 0
44         autocollapse(false),
45 #endif
46           in_update(false), first_after_edit(false)
47 {
48         inset.setOwner(this);
49         inset.setAutoBreakRows(true);
50         inset.setDrawFrame(InsetText::ALWAYS);
51         inset.setFrameColor(LColor::collapsableframe);
52         setInsetName("Collapsable");
53 }
54
55
56 InsetCollapsable::InsetCollapsable(InsetCollapsable const & in)
57         : UpdatableInset(in), inset(in.inset), collapsed_(in.collapsed_),
58           labelfont_(in.labelfont_), button_dim(0, 0, 0, 0), label(in.label),
59 #if 0
60           autocollapse(in.autocollapse),
61 #endif
62           in_update(false), first_after_edit(false)
63 {
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         Dimension dim_collapsed;
142         dimension_collapsed(dim_collapsed);
143
144         int const aa  = ascent();
145         button_dim.x1 = 0;
146         button_dim.x2 = dim_collapsed.width();
147         button_dim.y1 = -aa;
148         button_dim.y2 = -aa + dim_collapsed.height();
149
150         if (!isOpen()) {
151                 draw_collapsed(pi, x, y);
152                 return;
153         }
154
155         int old_x = x;
156
157         if (!owner())
158                 x += scroll();
159
160         top_x = x;
161         top_baseline = y;
162
163         int const bl = y - aa + dim_collapsed.ascent();
164
165         if (inlined) {
166                 inset.draw(pi, x, y);
167         } else {
168                 draw_collapsed(pi, old_x, bl);
169                 inset.draw(pi, x, bl + dim_collapsed.descent() + inset.ascent());
170         }
171 }
172
173
174 void InsetCollapsable::draw(PainterInfo & pi, int x, int y) const
175 {
176         // by default, we are not inlined-drawing
177         draw(pi, x, y, false);
178 }
179
180
181 InsetOld::EDITABLE InsetCollapsable::editable() const
182 {
183         return collapsed_ ? IS_EDITABLE : HIGHLY_EDITABLE;
184 }
185
186
187 void InsetCollapsable::insetUnlock(BufferView * bv)
188 {
189 #if 0
190         if (autocollapse) {
191                 if (change_label_with_text) {
192                         draw_label = get_new_label();
193                 } else {
194                         draw_label = label;
195                 }
196                 collapsed_ = true;
197         }
198 #endif
199         inset.insetUnlock(bv);
200         if (scroll())
201                 scroll(bv, 0.0F);
202         bv->updateInset(this);
203 }
204
205
206 FuncRequest InsetCollapsable::adjustCommand(FuncRequest const & cmd)
207 {
208         FuncRequest cmd1 = cmd;
209         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
210         return cmd1;
211 }
212
213
214 void InsetCollapsable::lfunMouseRelease(FuncRequest const & cmd)
215 {
216         bool ret = false;
217         BufferView * bv = cmd.view();
218
219         if (collapsed_ && cmd.button() != mouse_button::button3) {
220                 collapsed_ = false;
221                 bv->updateInset(this);
222                 bv->buffer()->markDirty();
223                 return;
224         }
225
226         if (cmd.button() != mouse_button::button3 && hitButton(cmd)) {
227                 if (collapsed_) {
228                         collapsed_ = false;
229                 } else {
230                         collapsed_ = true;
231                         bv->unlockInset(this);
232                 }
233                 bv->updateInset(this);
234                 bv->buffer()->markDirty();
235         } else if (!collapsed_ && cmd.y > button_dim.y2) {
236                 ret = inset.dispatch(adjustCommand(cmd)) == DispatchResult(true, true);
237         }
238         if (cmd.button() == mouse_button::button3 && !ret)
239                 showInsetDialog(bv);
240 }
241
242
243 int InsetCollapsable::latex(Buffer const & buf, ostream & os,
244                             LatexRunParams const & runparams) const
245 {
246         return inset.latex(buf, os, runparams);
247 }
248
249
250 int InsetCollapsable::ascii(Buffer const & buf, ostream & os,
251                             LatexRunParams const & runparams) const
252 {
253         return inset.ascii(buf, os, runparams);
254 }
255
256
257 int InsetCollapsable::linuxdoc(Buffer const & buf, ostream & os,
258                                LatexRunParams const & runparams) const
259 {
260         return inset.linuxdoc(buf, os, runparams);
261 }
262
263
264 int InsetCollapsable::docbook(Buffer const & buf, ostream & os,
265                               LatexRunParams const & runparams) const
266 {
267         return inset.docbook(buf, os, runparams);
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 void InsetCollapsable::edit(BufferView * bv, int index)
278 {
279         lyxerr << "InsetCollapsable: edit" << endl;
280         if (!bv->lockInset(this))
281                 lyxerr << "InsetCollapsable: can't lock index " << index << endl;
282         inset.dispatch(FuncRequest(bv, LFUN_INSET_EDIT, "left"));
283         first_after_edit = true;
284 }
285
286
287 DispatchResult
288 InsetCollapsable::priv_dispatch(FuncRequest const & cmd,
289                                 idx_type & idx, pos_type & pos)
290 {
291         //lyxerr << "InsetCollapsable::localDispatch: "
292         //      << cmd.action << " '" << cmd.argument << "'\n";
293         BufferView * bv = cmd.view();
294         switch (cmd.action) {
295                 case LFUN_INSET_EDIT: {
296                         if (!cmd.argument.empty()) {
297                                 UpdatableInset::priv_dispatch(cmd, idx, pos);
298                                 if (collapsed_) {
299                                         lyxerr << "branch collapsed_" << endl;
300                                         collapsed_ = false;
301                                         if (bv->lockInset(this)) {
302                                                 bv->updateInset(this);
303                                                 bv->buffer()->markDirty();
304                                                 inset.dispatch(cmd);
305                                                 first_after_edit = true;
306                                         }
307                                 } else {
308                                         lyxerr << "branch not collapsed_" << endl;
309                                         if (bv->lockInset(this))
310                                                 inset.dispatch(cmd);
311                                 }
312                                 return DispatchResult(true, true);
313                         }
314
315 #ifdef WITH_WARNINGS
316 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
317 #endif
318                         if (cmd.button() == mouse_button::button3)
319                                 return DispatchResult(true, true);
320
321                         UpdatableInset::priv_dispatch(cmd, idx, pos);
322
323                         if (collapsed_) {
324                                 collapsed_ = false;
325                                 // set this only here as it should be recollapsed only if
326                                 // it was already collapsed!
327                                 first_after_edit = true;
328                                 if (!bv->lockInset(this))
329                                         return DispatchResult(true, true);
330                                 bv->updateInset(this);
331                                 bv->buffer()->markDirty();
332                                 inset.dispatch(cmd);
333                         } else {
334                                 if (!bv->lockInset(this))
335                                         return DispatchResult(true, true);
336                                 if (cmd.y <= button_dim.y2) {
337                                         FuncRequest cmd1 = cmd;
338                                         cmd1.y = 0;
339                                         inset.dispatch(cmd1);
340                                 } else
341                                         inset.dispatch(adjustCommand(cmd));
342                         }
343                         return DispatchResult(true, true);
344                 }
345
346                 case LFUN_MOUSE_PRESS:
347                         if (!collapsed_ && cmd.y > button_dim.y2)
348                                 inset.dispatch(adjustCommand(cmd));
349                         return DispatchResult(true, true);
350
351                 case LFUN_MOUSE_MOTION:
352                         if (!collapsed_ && cmd.y > button_dim.y2)
353                                 inset.dispatch(adjustCommand(cmd));
354                         return DispatchResult(true, true);
355
356                 case LFUN_MOUSE_RELEASE:
357                         lfunMouseRelease(cmd);
358                         return DispatchResult(true, true);
359
360                 default:
361                         DispatchResult const result = inset.dispatch(cmd);
362                         if (result.val() >= 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 int InsetCollapsable::insetInInsetY() const
390 {
391         return inset.insetInInsetY() - (top_baseline - inset.y());
392 }
393
394
395 void InsetCollapsable::validate(LaTeXFeatures & features) const
396 {
397         inset.validate(features);
398 }
399
400
401 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
402 {
403         inset.getCursor(bv, x, y);
404 }
405
406
407 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
408 {
409         inset.getCursorPos(bv, x , y);
410 }
411
412
413 UpdatableInset * InsetCollapsable::getLockingInset() const
414 {
415         UpdatableInset * in = inset.getLockingInset();
416         if (&inset == in)
417                 return const_cast<InsetCollapsable *>(this);
418         return in;
419 }
420
421
422 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
423 {
424         if (c == lyxCode())
425                 return this;
426         return inset.getFirstLockingInsetOfType(c);
427 }
428
429
430 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
431                                bool toggleall, bool selectall)
432 {
433         inset.setFont(bv, font, toggleall, selectall);
434 }
435
436
437 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
438                                        bool const recursive) const
439 {
440         return inset.getLyXText(bv, recursive);
441 }
442
443
444 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
445 {
446         inset.deleteLyXText(bv, recursive);
447 }
448
449
450 void InsetCollapsable::getLabelList(Buffer const & buffer,
451                                     std::vector<string> & list) const
452 {
453         inset.getLabelList(buffer, list);
454 }
455
456
457 int InsetCollapsable::scroll(bool recursive) const
458 {
459         int sx = UpdatableInset::scroll(false);
460
461         if (recursive)
462                 sx += inset.scroll(recursive);
463
464         return sx;
465 }
466
467
468 ParagraphList * InsetCollapsable::getParagraphs(int i) const
469 {
470         return inset.getParagraphs(i);
471 }
472
473
474 int InsetCollapsable::numParagraphs() const
475 {
476         return inset.numParagraphs();
477 }
478
479
480 LyXText * InsetCollapsable::getText(int i) const
481 {
482         return inset.getText(i);
483 }
484
485
486 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
487 {
488         return inset.cursor(bv);
489 }
490
491
492 void InsetCollapsable::open(BufferView * bv)
493 {
494         if (!collapsed_)
495                 return;
496
497         collapsed_ = false;
498         bv->updateInset(this);
499 }
500
501
502 void InsetCollapsable::close(BufferView * bv) const
503 {
504         if (collapsed_)
505                 return;
506
507         collapsed_ = true;
508         bv->updateInset(this);
509 }
510
511
512 void InsetCollapsable::setLabel(string const & l) const
513 {
514         label = l;
515 }
516
517
518 void InsetCollapsable::setCollapsed(bool c) const
519 {
520         collapsed_ = c;
521 }
522
523
524 void InsetCollapsable::markErased()
525 {
526         inset.markErased();
527 }
528
529
530 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
531 {
532         bool found = inset.nextChange(bv, length);
533
534         if (first_after_edit && !found)
535                 close(bv);
536         else if (!found)
537                 first_after_edit = false;
538         return found;
539 }
540
541
542 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
543                                      bool cs, bool mw)
544 {
545         bool found = inset.searchForward(bv, str, cs, mw);
546         if (first_after_edit && !found)
547                 close(bv);
548         else if (!found)
549                 first_after_edit = false;
550         return found;
551 }
552
553
554 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
555                                       bool cs, bool mw)
556 {
557         bool found = inset.searchBackward(bv, str, cs, mw);
558         if (first_after_edit && !found)
559                 close(bv);
560         else if (!found)
561                 first_after_edit = false;
562         return found;
563 }
564
565
566 void InsetCollapsable::addPreview(PreviewLoader & loader) const
567 {
568         inset.addPreview(loader);
569 }