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