]> git.lyx.org Git - lyx.git/blob - src/insets/insetcollapsable.C
Always use std::endl with lyxerr.
[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 << "\n";
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                 inset.setUpdateStatus(InsetText::FULL);
233                 bv->updateInset(this);
234                 bv->buffer()->markDirty();
235                 return;
236         }
237
238         if (cmd.button() != mouse_button::button3 && hitButton(cmd))
239         {
240                 if (collapsed_) {
241                         collapsed_ = false;
242                         inset.setUpdateStatus(InsetText::FULL);
243                         bv->updateInset(this);
244                         bv->buffer()->markDirty();
245                 } else {
246                         collapsed_ = true;
247                         bv->unlockInset(this);
248                         bv->updateInset(this);
249                         bv->buffer()->markDirty();
250                 }
251         } else if (!collapsed_ && (cmd.y > button_dim.y2)) {
252                 ret = (inset.localDispatch(adjustCommand(cmd)) == DISPATCHED);
253         }
254         if (cmd.button() == mouse_button::button3 && !ret)
255                 showInsetDialog(bv);
256 }
257
258
259 int InsetCollapsable::latex(Buffer const * buf, ostream & os,
260                             LatexRunParams const & runparams) const
261 {
262         return inset.latex(buf, os, runparams);
263 }
264
265
266 int InsetCollapsable::ascii(Buffer const * buf, ostream & os, int ll) const
267 {
268         return inset.ascii(buf, os, ll);
269 }
270
271
272 int InsetCollapsable::linuxdoc(Buffer const * buf, ostream & os) const
273 {
274         return inset.linuxdoc(buf, os);
275 }
276
277
278 int InsetCollapsable::docbook(Buffer const * buf, ostream & os, bool mixcont) const
279 {
280         return inset.docbook(buf, os, mixcont);
281 }
282
283
284 bool InsetCollapsable::hitButton(FuncRequest const & cmd) const
285 {
286         return button_dim.contained(cmd.x, cmd.y);
287 }
288
289
290 InsetOld::RESULT InsetCollapsable::localDispatch(FuncRequest const & cmd)
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::localDispatch(cmd);
299                                 if (collapsed_) {
300                                         lyxerr << "branch collapsed_\n";
301                                         collapsed_ = false;
302                                         if (bv->lockInset(this)) {
303                                                 inset.setUpdateStatus(InsetText::FULL);
304                                                 bv->updateInset(this);
305                                                 bv->buffer()->markDirty();
306                                                 inset.localDispatch(cmd);
307                                                 first_after_edit = true;
308                                         }
309                                 } else {
310                                         lyxerr << "branch not collapsed_\n";
311                                         if (bv->lockInset(this))
312                                                 inset.localDispatch(cmd);
313                                 }
314                                 return DISPATCHED;
315                         }
316
317 #ifdef WITH_WARNINGS
318 #warning Fix this properly in BufferView_pimpl::workAreaButtonRelease
319 #endif
320                         if (cmd.button() == mouse_button::button3)
321                                 return DISPATCHED;
322
323                         UpdatableInset::localDispatch(cmd);
324
325                         if (collapsed_) {
326                                 collapsed_ = false;
327                                 // set this only here as it should be recollapsed only if
328                                 // it was already collapsed!
329                                 first_after_edit = true;
330                                 if (!bv->lockInset(this))
331                                         return DISPATCHED;
332                                 bv->updateInset(this);
333                                 bv->buffer()->markDirty();
334                                 inset.localDispatch(cmd);
335                         } else {
336                                 FuncRequest cmd1 = cmd;
337                                 if (!bv->lockInset(this))
338                                         return DISPATCHED;
339                                 if (cmd.y <= button_dim.y2) {
340                                         cmd1.y = 0;
341                                 } else {
342                                         cmd1.y = ascent() + cmd.y - (height_collapsed() + inset.ascent());
343                                 }
344                                 inset.localDispatch(cmd);
345                         }
346                         return DISPATCHED;
347                 }
348
349                 case LFUN_MOUSE_PRESS:
350                         if (!collapsed_ && cmd.y > button_dim.y2)
351                                 inset.localDispatch(adjustCommand(cmd));
352                         return DISPATCHED;
353
354                 case LFUN_MOUSE_MOTION:
355                         if (!collapsed_ && cmd.y > button_dim.y2)
356                                 inset.localDispatch(adjustCommand(cmd));
357                         return DISPATCHED;
358
359                 case LFUN_MOUSE_RELEASE:
360                         lfunMouseRelease(cmd);
361                         return DISPATCHED;
362
363                 default:
364                         UpdatableInset::RESULT result = inset.localDispatch(cmd);
365                         if (result >= FINISHED)
366                                 bv->unlockInset(this);
367                         first_after_edit = false;
368                         return result;
369         }
370 }
371
372
373 bool InsetCollapsable::lockInsetInInset(BufferView * bv, UpdatableInset * in)
374 {
375         if (&inset == in)
376                 return true;
377         return inset.lockInsetInInset(bv, in);
378 }
379
380
381 bool InsetCollapsable::unlockInsetInInset(BufferView * bv, UpdatableInset * in,
382                                           bool lr)
383 {
384         if (&inset == in) {
385                 bv->unlockInset(this);
386                 return true;
387         }
388         return inset.unlockInsetInInset(bv, in, lr);
389 }
390
391
392 bool InsetCollapsable::updateInsetInInset(BufferView * bv, InsetOld *in)
393 {
394         if (in == this)
395                 return true;
396         return inset.updateInsetInInset(bv, in);
397 }
398
399
400 int InsetCollapsable::insetInInsetY() const
401 {
402         return inset.insetInInsetY() - (top_baseline - inset.y());
403 }
404
405
406 void InsetCollapsable::validate(LaTeXFeatures & features) const
407 {
408         inset.validate(features);
409 }
410
411
412 void InsetCollapsable::getCursor(BufferView & bv, int & x, int & y) const
413 {
414         inset.getCursor(bv, x, y);
415 }
416
417
418 void InsetCollapsable::getCursorPos(BufferView * bv, int & x, int & y) const
419 {
420         inset.getCursorPos(bv, x , y);
421 }
422
423
424 UpdatableInset * InsetCollapsable::getLockingInset() const
425 {
426         UpdatableInset * in = inset.getLockingInset();
427         if (&inset == in)
428                 return const_cast<InsetCollapsable *>(this);
429         return in;
430 }
431
432
433 UpdatableInset * InsetCollapsable::getFirstLockingInsetOfType(InsetOld::Code c)
434 {
435         if (c == lyxCode())
436                 return this;
437         return inset.getFirstLockingInsetOfType(c);
438 }
439
440
441 void InsetCollapsable::setFont(BufferView * bv, LyXFont const & font,
442                                bool toggleall, bool selectall)
443 {
444         inset.setFont(bv, font, toggleall, selectall);
445 }
446
447
448 LyXText * InsetCollapsable::getLyXText(BufferView const * bv,
449                                        bool const recursive) const
450 {
451         return inset.getLyXText(bv, recursive);
452 }
453
454
455 void InsetCollapsable::deleteLyXText(BufferView * bv, bool recursive) const
456 {
457         inset.deleteLyXText(bv, recursive);
458 }
459
460
461 void InsetCollapsable::resizeLyXText(BufferView * bv, bool force) const
462 {
463         inset.resizeLyXText(bv, force);
464         oldWidth = width();
465 }
466
467
468 void InsetCollapsable::getLabelList(std::vector<string> & list) const
469 {
470         inset.getLabelList(list);
471 }
472
473
474 int InsetCollapsable::scroll(bool recursive) const
475 {
476         int sx = UpdatableInset::scroll(false);
477
478         if (recursive)
479                 sx += inset.scroll(recursive);
480
481         return sx;
482 }
483
484
485 ParagraphList * InsetCollapsable::getParagraphs(int i) const
486 {
487         return inset.getParagraphs(i);
488 }
489
490
491 LyXCursor const & InsetCollapsable::cursor(BufferView * bv) const
492 {
493         return inset.cursor(bv);
494 }
495
496
497 InsetOld * InsetCollapsable::getInsetFromID(int id_arg) const
498 {
499         if (id_arg == id())
500                 return const_cast<InsetCollapsable *>(this);
501         return inset.getInsetFromID(id_arg);
502 }
503
504
505 void InsetCollapsable::open(BufferView * bv)
506 {
507         if (!collapsed_) return;
508
509         collapsed_ = false;
510         bv->updateInset(this);
511 }
512
513
514 void InsetCollapsable::close(BufferView * bv) const
515 {
516         if (collapsed_)
517                 return;
518
519         collapsed_ = true;
520         bv->updateInset(const_cast<InsetCollapsable *>(this));
521 }
522
523
524 void InsetCollapsable::setLabel(string const & l) const
525 {
526         label = l;
527 }
528
529
530 void InsetCollapsable::markErased()
531 {
532         inset.markErased();
533 }
534
535
536 bool InsetCollapsable::nextChange(BufferView * bv, lyx::pos_type & length)
537 {
538         bool found = inset.nextChange(bv, length);
539
540         if (first_after_edit && !found)
541                 close(bv);
542         else if (!found)
543                 first_after_edit = false;
544         return found;
545 }
546
547
548 bool InsetCollapsable::searchForward(BufferView * bv, string const & str,
549                                      bool cs, bool mw)
550 {
551         bool found = inset.searchForward(bv, str, cs, mw);
552         if (first_after_edit && !found)
553                 close(bv);
554         else if (!found)
555                 first_after_edit = false;
556         return found;
557 }
558
559
560 bool InsetCollapsable::searchBackward(BufferView * bv, string const & str,
561                                       bool cs, bool mw)
562 {
563         bool found = inset.searchBackward(bv, str, cs, mw);
564         if (first_after_edit && !found)
565                 close(bv);
566         else if (!found)
567                 first_after_edit = false;
568         return found;
569 }
570
571
572 WordLangTuple const
573 InsetCollapsable::selectNextWordToSpellcheck(BufferView * bv, float & value) const
574 {
575         WordLangTuple word = inset.selectNextWordToSpellcheck(bv, value);
576         if (first_after_edit && word.word().empty())
577                 close(bv);
578         first_after_edit = false;
579         return word;
580 }
581
582
583 void InsetCollapsable::addPreview(PreviewLoader & loader) const
584 {
585         inset.addPreview(loader);
586 }
587
588
589 void InsetCollapsable::cache(BufferView * bv) const
590 {
591         view_ = bv->owner()->view();
592 }
593
594
595 BufferView * InsetCollapsable::view() const
596 {
597         return view_.lock().get();
598 }